{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://futurecraft.pro/artifacts/agent-memory-lifecycle/memory-record.schema.json",
  "title": "Agent memory record",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "memoryId",
    "tenantId",
    "subjectId",
    "namespace",
    "kind",
    "status",
    "fact",
    "source",
    "confidence",
    "sensitivity",
    "validFrom",
    "validUntil",
    "createdAt",
    "supersedes"
  ],
  "properties": {
    "schemaVersion": { "const": "1.0" },
    "memoryId": { "type": "string", "pattern": "^mem_[A-Za-z0-9_-]+$" },
    "tenantId": { "type": "string", "minLength": 1 },
    "subjectId": { "type": "string", "minLength": 1 },
    "namespace": {
      "type": "array",
      "minItems": 1,
      "maxItems": 8,
      "items": { "type": "string", "minLength": 1 },
      "uniqueItems": true
    },
    "kind": {
      "enum": ["semantic", "episodic", "procedural"]
    },
    "status": {
      "enum": ["candidate", "confirmed", "superseded"]
    },
    "fact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["field", "value"],
      "properties": {
        "field": { "type": "string", "minLength": 1 },
        "value": {
          "type": ["array", "boolean", "null", "number", "object", "string"]
        }
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "eventId", "occurredAt"],
      "properties": {
        "type": {
          "enum": [
            "system_of_record",
            "explicit_user_statement",
            "approved_project_record",
            "verified_outcome",
            "model_inference",
            "summary"
          ]
        },
        "eventId": { "type": "string", "minLength": 1 },
        "occurredAt": { "type": "string", "format": "date-time" }
      }
    },
    "confidence": {
      "enum": ["low", "medium", "high", "confirmed"]
    },
    "sensitivity": {
      "enum": ["public", "internal", "personal", "restricted"]
    },
    "validFrom": { "type": "string", "format": "date-time" },
    "validUntil": {
      "oneOf": [
        { "type": "string", "format": "date-time" },
        { "type": "null" }
      ]
    },
    "createdAt": { "type": "string", "format": "date-time" },
    "supersedes": {
      "oneOf": [
        { "type": "string", "pattern": "^mem_[A-Za-z0-9_-]+$" },
        { "type": "null" }
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "source": {
            "properties": { "type": { "const": "model_inference" } },
            "required": ["type"]
          }
        },
        "required": ["source"]
      },
      "then": {
        "properties": {
          "status": { "const": "candidate" },
          "confidence": { "enum": ["low", "medium", "high"] }
        }
      }
    }
  ]
}
