{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:orbiplex:schema:topic-resolution:v1",
  "title": "TopicResolution v1",
  "description": "Signed deterministic resolver output over one pinned topic taxonomy. Ambiguous and unresolved are first-class states, not malformed outputs.",
  "type": "object",
  "additionalProperties": false,
  "x-dia-workflow": "project",
  "x-dia-status": "draft",
  "x-dia-basis": [
    "doc/project/40-proposals/069-corpus.md"
  ],
  "required": [
    "schema/v",
    "resolution/id",
    "taxonomy/digest",
    "resolver/version",
    "epsilon",
    "query/keywords",
    "result",
    "matched/labels",
    "candidates",
    "issued-at",
    "resolver/node-id",
    "signature"
  ],
  "properties": {
    "schema/v": { "const": 1 },
    "resolution/id": {
      "type": "string",
      "pattern": "^topic-resolution:[A-Za-z0-9._:-]+$"
    },
    "taxonomy/digest": { "$ref": "#/$defs/sha256_digest" },
    "resolver/version": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128
    },
    "epsilon": {
      "type": "number",
      "minimum": 0
    },
    "query/keywords": {
      "type": "array",
      "minItems": 1,
      "maxItems": 64,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 128
      }
    },
    "result": {
      "type": "string",
      "enum": ["resolved", "ambiguous", "unresolved"]
    },
    "topic/term": { "$ref": "#/$defs/topic_term" },
    "score": {
      "type": "number",
      "minimum": 0
    },
    "matched/labels": {
      "type": "array",
      "maxItems": 256,
      "items": { "$ref": "#/$defs/matched_label" }
    },
    "candidates": {
      "type": "array",
      "maxItems": 32,
      "items": { "$ref": "#/$defs/candidate" }
    },
    "ambiguous/reason": {
      "type": ["string", "null"],
      "minLength": 1,
      "maxLength": 512
    },
    "issued-at": {
      "type": "string",
      "format": "date-time"
    },
    "resolver/node-id": {
      "type": "string",
      "pattern": "^node:did:key:z[1-9A-HJ-NP-Za-km-z]+$"
    },
    "signature": { "$ref": "#/$defs/signature" },
    "extensions": { "$ref": "#/$defs/extensions" }
  },
  "allOf": [
    {
      "if": {
        "properties": { "result": { "const": "resolved" } },
        "required": ["result"]
      },
      "then": {
        "required": ["topic/term", "score"]
      }
    },
    {
      "if": {
        "properties": { "result": { "const": "ambiguous" } },
        "required": ["result"]
      },
      "then": {
        "required": ["ambiguous/reason"]
      }
    }
  ],
  "$defs": {
    "sha256_digest": {
      "type": "string",
      "pattern": "^sha256:[A-Za-z0-9_-]{16,128}$"
    },
    "topic_term": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9:._+/-]*$"
    },
    "matched_label": {
      "type": "object",
      "additionalProperties": false,
      "required": ["keyword", "term", "kind"],
      "properties": {
        "keyword": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "term": { "$ref": "#/$defs/topic_term" },
        "kind": {
          "type": "string",
          "enum": ["leaf", "ancestor", "alias"]
        }
      }
    },
    "candidate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["topic/term", "score"],
      "properties": {
        "topic/term": { "$ref": "#/$defs/topic_term" },
        "score": {
          "type": "number",
          "minimum": 0
        }
      }
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["alg", "value"],
      "properties": {
        "alg": { "const": "ed25519" },
        "value": {
          "type": "string",
          "minLength": 1
        },
        "key/public": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "extensions": {
      "type": "object",
      "additionalProperties": true
    }
  }
}
