{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "gno://schemas/query-diagnose@1.2",
  "title": "GNO Query Diagnose",
  "type": "object",
  "required": ["schemaVersion", "query", "target", "stages", "chunk", "meta"],
  "properties": {
    "schemaVersion": { "type": "string", "enum": ["1.0", "1.1", "1.2"] },
    "query": { "type": "string" },
    "target": {
      "type": "object",
      "required": [
        "ref",
        "status",
        "docid",
        "uri",
        "title",
        "contentType",
        "contentTypeSource",
        "categories",
        "graphHints",
        "contentTypeRulesFingerprint",
        "contentTypeFingerprintMatches",
        "mirrorHash",
        "chunkCount",
        "filterReasons"
      ],
      "properties": {
        "ref": { "type": "string" },
        "status": {
          "type": "string",
          "enum": [
            "not_found",
            "inactive",
            "no_indexed_content",
            "filtered_out",
            "diagnosed"
          ]
        },
        "docid": { "type": ["string", "null"], "pattern": "^#[a-f0-9]{6,}$" },
        "uri": { "type": ["string", "null"], "format": "uri" },
        "title": { "type": ["string", "null"] },
        "contentType": { "type": ["string", "null"] },
        "contentTypeSource": { "type": ["string", "null"] },
        "categories": { "type": "array", "items": { "type": "string" } },
        "graphHints": { "type": "array", "items": { "type": "string" } },
        "contentTypeRulesFingerprint": { "type": ["string", "null"] },
        "contentTypeFingerprintMatches": { "type": ["boolean", "null"] },
        "mirrorHash": { "type": ["string", "null"] },
        "chunkCount": { "type": "integer", "minimum": 0 },
        "filterReasons": { "type": "array", "items": { "type": "string" } }
      },
      "additionalProperties": false
    },
    "stages": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "status",
          "sourceCount",
          "present",
          "rank",
          "score",
          "survived",
          "dropReason"
        ],
        "properties": {
          "id": {
            "type": "string",
            "enum": ["bm25", "vector", "fusion", "graph", "rerank"]
          },
          "status": { "type": "string", "enum": ["active", "skipped"] },
          "sourceCount": { "type": "integer", "minimum": 0 },
          "present": { "type": "boolean" },
          "rank": { "type": ["integer", "null"], "minimum": 1 },
          "score": { "type": ["number", "null"] },
          "survived": { "type": "boolean" },
          "dropReason": {
            "type": ["string", "null"],
            "enum": ["not_in_candidate_set", "below_cutoff", "skipped", null]
          },
          "reason": { "type": "string" }
        },
        "additionalProperties": false
      }
    },
    "affinity": {
      "type": "object",
      "required": [
        "affinityAdjustedScore",
        "affinityApplied",
        "affinityRequested",
        "affinityWeight",
        "baseScore",
        "collectionAlias",
        "combinedAuxiliaryApplied",
        "combinedAuxiliaryCap",
        "combinedAuxiliaryRequested",
        "finalBlendedScore",
        "finalScore",
        "matched",
        "rawScore",
        "rawScoreKind",
        "rootAlias",
        "source"
      ],
      "properties": {
        "affinityAdjustedScore": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "affinityApplied": { "type": "number" },
        "affinityRequested": { "type": "number" },
        "affinityWeight": {
          "type": "number",
          "minimum": 0,
          "maximum": 0.03
        },
        "baseScore": { "type": "number", "minimum": 0, "maximum": 1 },
        "collectionAlias": {
          "type": ["string", "null"],
          "pattern": "^collection_[a-f0-9]{12}$"
        },
        "combinedAuxiliaryApplied": {
          "type": "number",
          "minimum": -0.08,
          "maximum": 0.08
        },
        "combinedAuxiliaryCap": { "type": "number", "const": 0.08 },
        "combinedAuxiliaryRequested": { "type": "number" },
        "finalBlendedScore": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "finalScore": { "type": "number", "minimum": 0, "maximum": 1 },
        "matched": { "type": "boolean" },
        "rawScore": { "type": "number" },
        "rawScoreKind": {
          "type": "string",
          "enum": ["bm25", "hybrid_blended", "normalized", "vector_distance"]
        },
        "rootAlias": {
          "type": ["string", "null"],
          "pattern": "^root_[a-f0-9]{12}$"
        },
        "source": {
          "type": ["string", "null"],
          "enum": [
            "cli_cwd",
            "cli_explicit",
            "cli_worktree",
            "project_profile",
            null
          ]
        }
      },
      "additionalProperties": false
    },
    "contentTypeBoost": {
      "type": "object",
      "required": [
        "baseScore",
        "cappedContribution",
        "combinedAuxiliaryApplied",
        "combinedAuxiliaryCap",
        "combinedAuxiliaryRequested",
        "configuredFactor",
        "contentType",
        "finalScore",
        "rawContribution",
        "rawScore",
        "rawScoreKind",
        "ruleSource",
        "rulesFingerprint"
      ],
      "properties": {
        "baseScore": { "type": "number", "minimum": 0, "maximum": 1 },
        "cappedContribution": {
          "type": "number",
          "minimum": -0.05,
          "maximum": 0.05
        },
        "combinedAuxiliaryApplied": {
          "type": "number",
          "minimum": -0.08,
          "maximum": 0.08
        },
        "combinedAuxiliaryCap": { "type": "number", "const": 0.08 },
        "combinedAuxiliaryRequested": { "type": "number" },
        "configuredFactor": { "type": "number", "minimum": 0.5, "maximum": 2 },
        "contentType": { "type": "string" },
        "finalScore": { "type": "number", "minimum": 0, "maximum": 1 },
        "rawContribution": { "type": "number" },
        "rawScore": { "type": "number" },
        "rawScoreKind": {
          "type": "string",
          "enum": ["bm25", "hybrid_blended", "normalized", "vector_distance"]
        },
        "ruleSource": { "type": "string", "enum": ["configured-id", "prefix"] },
        "rulesFingerprint": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
      },
      "additionalProperties": false
    },
    "chunk": {
      "type": "object",
      "required": ["seq", "startLine", "endLine", "language"],
      "properties": {
        "seq": { "type": ["integer", "null"], "minimum": 0 },
        "startLine": { "type": ["integer", "null"], "minimum": 1 },
        "endLine": { "type": ["integer", "null"], "minimum": 1 },
        "language": { "type": ["string", "null"] }
      },
      "additionalProperties": false
    },
    "meta": {
      "type": "object",
      "required": ["mode", "vectorsUsed", "reranked", "totalResults"],
      "properties": {
        "mode": { "type": "string", "enum": ["bm25_only", "hybrid"] },
        "vectorsUsed": { "type": "boolean" },
        "reranked": { "type": "boolean" },
        "totalResults": { "type": "integer", "minimum": 0 },
        "queryModes": {
          "type": "object",
          "required": ["term", "intent", "hyde"],
          "properties": {
            "term": { "type": "integer", "minimum": 0 },
            "intent": { "type": "integer", "minimum": 0 },
            "hyde": { "type": "boolean" }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "schemaVersion": { "const": "1.0" } },
        "required": ["schemaVersion"]
      },
      "then": {
        "properties": { "affinity": false, "contentTypeBoost": false }
      }
    },
    {
      "if": {
        "properties": { "schemaVersion": { "const": "1.1" } },
        "required": ["schemaVersion"]
      },
      "then": {
        "properties": { "affinity": {}, "contentTypeBoost": false },
        "required": ["affinity"]
      }
    },
    {
      "if": {
        "properties": { "schemaVersion": { "const": "1.2" } },
        "required": ["schemaVersion"]
      },
      "then": {
        "properties": { "contentTypeBoost": {} },
        "required": ["contentTypeBoost"]
      }
    }
  ],
  "additionalProperties": false
}
