{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "gno://schemas/publish-artifact@1.0",
  "title": "GNO publish artifact",
  "oneOf": [
    { "$ref": "#/definitions/artifactV1" },
    { "$ref": "#/definitions/artifactV2" }
  ],
  "definitions": {
    "artifactV1": {
      "type": "object",
      "additionalProperties": false,
      "required": ["version", "source", "exportedAt", "spaces"],
      "properties": {
        "version": { "const": 1 },
        "egressLineage": { "$ref": "#/definitions/egressLineage" },
        "source": { "$ref": "#/definitions/slug" },
        "exportedAt": { "type": "string", "format": "date-time" },
        "requiredCapabilities": {
          "$ref": "#/definitions/requiredCapabilities"
        },
        "assets": { "$ref": "#/definitions/assets" },
        "spaces": {
          "type": "array",
          "minItems": 1,
          "items": {
            "oneOf": [
              { "$ref": "#/definitions/publicSpace" },
              { "$ref": "#/definitions/restrictedSpace" }
            ]
          }
        }
      }
    },
    "artifactV2": {
      "type": "object",
      "additionalProperties": false,
      "required": ["version", "source", "exportedAt", "spaces"],
      "properties": {
        "version": { "const": 2 },
        "egressLineage": { "$ref": "#/definitions/egressLineage" },
        "source": { "$ref": "#/definitions/slug" },
        "exportedAt": { "type": "string", "format": "date-time" },
        "requiredCapabilities": {
          "$ref": "#/definitions/requiredCapabilities"
        },
        "spaces": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/definitions/encryptedSpace" }
        }
      }
    },
    "requiredCapabilities": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 128
      }
    },
    "assets": {
      "type": "array",
      "items": { "$ref": "#/definitions/artifactAsset" }
    },
    "artifactAsset": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "mediaType",
        "byteLength",
        "sha256",
        "width",
        "height",
        "encoding",
        "data",
        "references"
      ],
      "properties": {
        "id": { "$ref": "#/definitions/hash" },
        "mediaType": {
          "enum": [
            "image/png",
            "image/jpeg",
            "image/gif",
            "image/webp",
            "image/avif"
          ]
        },
        "byteLength": { "type": "integer", "minimum": 1, "maximum": 104857600 },
        "sha256": { "$ref": "#/definitions/hash" },
        "width": { "type": "integer", "minimum": 1, "maximum": 16384 },
        "height": { "type": "integer", "minimum": 1, "maximum": 16384 },
        "encoding": { "const": "base64" },
        "data": {
          "type": "string",
          "minLength": 1,
          "maxLength": 139810134,
          "pattern": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
        },
        "references": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["noteSlug", "sourceRef"],
            "properties": {
              "noteSlug": { "$ref": "#/definitions/slug" },
              "sourceRef": {
                "type": "string",
                "minLength": 1,
                "maxLength": 1024
              }
            }
          }
        }
      }
    },
    "slug": {
      "type": "string",
      "pattern": "^[a-z0-9](?:[a-z0-9-]{0,78}[a-z0-9])?$",
      "maxLength": 80
    },
    "hash": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    },
    "egressLineage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["effectivePolicy", "digest", "sources"],
      "properties": {
        "effectivePolicy": { "enum": ["local_only", "lan", "remote"] },
        "digest": { "$ref": "#/definitions/hash" },
        "sources": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["collection", "policy", "source"],
            "properties": {
              "collection": {
                "type": "string",
                "pattern": "^[a-z0-9][a-z0-9_-]{0,63}$"
              },
              "policy": { "enum": ["local_only", "lan", "remote"] },
              "source": {
                "enum": ["explicit", "config_default", "legacy_default"]
              }
            }
          }
        }
      }
    },
    "docid": {
      "type": "string",
      "pattern": "^#[a-f0-9]{8}$"
    },
    "metadata": {
      "type": "object",
      "additionalProperties": {
        "oneOf": [
          { "type": "string" },
          {
            "type": "array",
            "items": { "type": "string" }
          }
        ]
      }
    },
    "noteId": {
      "type": "string",
      "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
    },
    "note": {
      "type": "object",
      "additionalProperties": false,
      "required": ["markdown", "slug", "summary", "title"],
      "properties": {
        "id": { "$ref": "#/definitions/noteId" },
        "markdown": { "type": "string" },
        "metadata": { "$ref": "#/definitions/metadata" },
        "slug": { "$ref": "#/definitions/slug" },
        "summary": { "type": "string" },
        "title": { "type": "string", "minLength": 1 }
      }
    },
    "publicSpace": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "notes",
        "routeSlug",
        "sourceType",
        "summary",
        "title",
        "visibility",
        "manifest"
      ],
      "properties": {
        "homeNoteSlug": { "$ref": "#/definitions/slug" },
        "notes": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/definitions/note" }
        },
        "routeSlug": { "$ref": "#/definitions/slug" },
        "sourceType": { "enum": ["note", "collection"] },
        "summary": { "type": "string" },
        "title": { "type": "string", "minLength": 1 },
        "visibility": { "const": "public" },
        "manifest": { "$ref": "#/definitions/publicManifest" }
      }
    },
    "restrictedSpace": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "notes",
        "routeSlug",
        "sourceType",
        "summary",
        "title",
        "visibility"
      ],
      "properties": {
        "homeNoteSlug": { "$ref": "#/definitions/slug" },
        "notes": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/definitions/note" }
        },
        "routeSlug": { "$ref": "#/definitions/slug" },
        "sourceType": { "enum": ["note", "collection"] },
        "summary": { "type": "string" },
        "title": { "type": "string", "minLength": 1 },
        "visibility": { "enum": ["secret-link", "invite-only"] }
      }
    },
    "publicManifest": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "projectionRevision",
        "generatedAt",
        "visibility",
        "capabilities",
        "documents"
      ],
      "properties": {
        "schemaVersion": { "const": "1.0" },
        "projectionRevision": { "$ref": "#/definitions/hash" },
        "egressLineage": { "$ref": "#/definitions/egressLineage" },
        "generatedAt": { "type": "string", "format": "date-time" },
        "visibility": { "const": "public" },
        "capabilities": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "capsuleEvidence",
            "exactLineCitations",
            "llmsTxt",
            "markdownDocuments"
          ],
          "properties": {
            "capsuleEvidence": { "const": true },
            "exactLineCitations": { "const": true },
            "llmsTxt": { "const": true },
            "markdownDocuments": { "const": true }
          }
        },
        "documents": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/definitions/publicDocument" }
        }
      }
    },
    "publicDocument": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "slug",
        "title",
        "summary",
        "markdownPath",
        "contentHash",
        "byteLength",
        "lineCount",
        "evidence"
      ],
      "properties": {
        "slug": { "$ref": "#/definitions/slug" },
        "title": { "type": "string", "minLength": 1 },
        "summary": { "type": "string" },
        "markdownPath": {
          "type": "string",
          "pattern": "^\\./[a-z0-9](?:[a-z0-9-]{0,78}[a-z0-9])?\\.md$"
        },
        "contentHash": { "$ref": "#/definitions/hash" },
        "byteLength": { "type": "integer", "minimum": 0 },
        "lineCount": { "type": "integer", "minimum": 1 },
        "evidence": { "$ref": "#/definitions/publicEvidence" }
      }
    },
    "publicEvidence": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "evidenceId",
        "uri",
        "docid",
        "startLine",
        "endLine",
        "sourceHash",
        "mirrorHash",
        "passageHash",
        "locator"
      ],
      "properties": {
        "evidenceId": { "$ref": "#/definitions/hash" },
        "uri": {
          "type": "string",
          "pattern": "^gno://public/[a-z0-9][a-z0-9-]{0,79}/[a-z0-9][a-z0-9-]{0,79}\\.md$"
        },
        "docid": { "$ref": "#/definitions/docid" },
        "startLine": { "const": 1 },
        "endLine": { "type": "integer", "minimum": 1 },
        "sourceHash": { "$ref": "#/definitions/hash" },
        "mirrorHash": { "$ref": "#/definitions/hash" },
        "passageHash": { "$ref": "#/definitions/hash" },
        "locator": {
          "type": "string",
          "pattern": "^\\./[a-z0-9][a-z0-9-]{0,79}\\.md#L1-L[1-9][0-9]*$"
        }
      }
    },
    "encryptedSpace": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "encryptedPayload",
        "routeSlug",
        "secretToken",
        "sourceType",
        "visibility"
      ],
      "properties": {
        "noteIds": {
          "type": "array",
          "minItems": 1,
          "maxItems": 5000,
          "uniqueItems": true,
          "items": { "$ref": "#/definitions/noteId" }
        },
        "encryptedPayload": {
          "type": "object",
          "additionalProperties": false,
          "required": ["ciphertext", "iterations", "iv", "salt"],
          "properties": {
            "ciphertext": {
              "type": "string",
              "minLength": 1,
              "maxLength": 104857600,
              "pattern": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
            },
            "iterations": {
              "type": "integer",
              "minimum": 1,
              "maximum": 9007199254740991
            },
            "iv": {
              "type": "string",
              "minLength": 1,
              "maxLength": 1024,
              "pattern": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
            },
            "salt": {
              "type": "string",
              "minLength": 1,
              "maxLength": 1024,
              "pattern": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
            }
          }
        },
        "routeSlug": { "$ref": "#/definitions/slug" },
        "secretToken": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512
        },
        "sourceType": { "enum": ["note", "collection"] },
        "visibility": { "const": "encrypted" }
      }
    }
  }
}
