{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://krive.xyz/schemas/artifact-v1.schema.json",
  "title": "Krive Artifact v1",
  "description": "Portable, human-readable and machine-readable artifact envelope for agent and human workflows.",
  "type": "object",
  "required": ["schema", "schemaVersion", "type", "id", "contentHash", "createdAt", "updatedAt", "status", "title", "summary", "createdBy", "provenance", "content", "sources"],
  "properties": {
    "schema": {"const": "https://krive.xyz/schemas/artifact-v1.schema.json"},
    "schemaVersion": {"const": "1.0"},
    "type": {"enum": ["research-brief", "decision-memo", "task-handoff", "approval-request", "mini-app-capsule"]},
    "id": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"},
    "contentHash": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"},
    "createdAt": {"type": "string", "format": "date-time"},
    "updatedAt": {"type": "string", "format": "date-time"},
    "status": {"enum": ["draft", "proposed", "reviewed", "accepted", "rejected", "expired", "revoked"]},
    "title": {"type": "string", "minLength": 1, "maxLength": 160},
    "summary": {"type": "string", "minLength": 1, "maxLength": 1200},
    "createdBy": {
      "type": "object",
      "required": ["type", "name"],
      "properties": {
        "type": {"enum": ["human", "agent", "tool", "organization"]},
        "name": {"type": "string", "minLength": 1, "maxLength": 120}
      },
      "additionalProperties": false
    },
    "provenance": {
      "type": "object",
      "required": ["revision"],
      "properties": {
        "revision": {"type": "integer", "minimum": 1},
        "parentHash": {"type": ["string", "null"], "pattern": "^sha256:[0-9a-f]{64}$"},
        "editingAgent": {"type": ["string", "null"], "maxLength": 120},
        "humanApprover": {"type": ["string", "null"], "maxLength": 120},
        "acceptedRevision": {"type": ["integer", "null"], "minimum": 1}
      },
      "additionalProperties": false
    },
    "content": {"type": "object"},
    "sources": {
      "type": "array",
      "maxItems": 50,
      "items": {
        "type": "object",
        "required": ["id", "title", "url"],
        "properties": {
          "id": {"type": "string", "pattern": "^source-[1-9][0-9]*$"},
          "title": {"type": "string", "minLength": 1, "maxLength": 240},
          "url": {"type": "string", "format": "uri", "pattern": "^https?://"},
          "publisher": {"type": "string", "maxLength": 160},
          "retrievedAt": {"type": "string", "format": "date-time"}
        },
        "additionalProperties": false
      }
    }
  },
  "$defs": {
    "miniAppContent": {
      "type": "object",
      "required": ["runtime", "html", "css", "javascript", "capabilities"],
      "properties": {
        "runtime": {"const": "krive-capsule-v1"},
        "html": {"type": "string", "maxLength": 40000},
        "css": {"type": "string", "maxLength": 20000},
        "javascript": {"type": "string", "maxLength": 40000},
        "capabilities": {
          "type": "array",
          "uniqueItems": true,
          "maxItems": 1,
          "items": {"enum": ["downloads", "email-self"]}
        }
      },
      "additionalProperties": false
    }
  },
  "allOf": [
    {
      "if": {"properties": {"type": {"const": "mini-app-capsule"}}, "required": ["type"]},
      "then": {"properties": {"content": {"$ref": "#/$defs/miniAppContent"}}}
    }
  ],
  "additionalProperties": false
}
