{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://law.arxo.io/schema/query.schema.json",
  "title": "Arxo query 0.1",
  "description": "A question to the evaluator — the evaluate input; the answer is an evaluation document. A literal without kind is never evaluated: a query outside this schema is rejected before evaluation with code QUERY_INVALID instead of being answered. The literals, terms, and formulas of a query are the same shapes as in the compiled model.", "$comment": "SPEC §172; E-0102",
  "type": "object",
  "required": [
    "queryId",
    "kind"
  ],
  "properties": {
    "queryId": {
      "$ref": "#/$defs/QueryId"
    },
    "kind": {
      "enum": [
        "truth",
        "focused_truth",
        "why_not",
        "argumentation",
        "precedent",
        "formula",
        "collect",
        "term",
        "positions",
        "weak_permission",
        "interpretation_analysis",
        "calendar_op"
      ]
    }
  },
  "oneOf": [
    {
      "$ref": "#/$defs/LiteralQuery"
    },
    {
      "$ref": "#/$defs/FormulaQuery"
    },
    {
      "$ref": "#/$defs/CollectQuery"
    },
    {
      "$ref": "#/$defs/TermQuery"
    },
    {
      "$ref": "#/$defs/PositionsQuery"
    },
    {
      "$ref": "#/$defs/WeakPermissionQuery"
    },
    {
      "$ref": "#/$defs/InterpretationAnalysisQuery"
    },
    {
      "$ref": "#/$defs/CalendarOpQuery"
    }
  ],
  "$defs": {
    "QueryId": {
      "type": "string",
      "minLength": 1,
      "description": "Query identifier; the tail after the last colon gives the result id and the query’s proof node.", "$comment": "SPEC §174; SPEC §179"
    },
    "LiteralQuery": {
      "description": "Questions over a ground literal: truth, focused truth, why-not, argumentation, and precedent. The literal is a model Literal with kind literal: for a ground goal without kind, matching against a rule head is undefined, which is exactly how the judgment channel used to be lost.", "$comment": "SPEC §62; SPEC §65; SPEC §181.3; SPEC §185; SPEC §274; SPEC §276; DECISION-0368; E-0102",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "queryId",
        "kind",
        "literal"
      ],
      "properties": {
        "queryId": {
          "$ref": "#/$defs/QueryId"
        },
        "kind": {
          "enum": [
            "truth",
            "focused_truth",
            "why_not",
            "argumentation",
            "precedent"
          ]
        },
        "literal": {
          "$ref": "legal-ir.schema.json#/$defs/Literal"
        }
      }
    },
    "FormulaQuery": {
      "description": "A status formula as a query.", "$comment": "SPEC §65",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "queryId",
        "kind",
        "formula"
      ],
      "properties": {
        "queryId": {
          "$ref": "#/$defs/QueryId"
        },
        "kind": {
          "const": "formula"
        },
        "formula": {
          "$ref": "legal-ir.schema.json#/$defs/Formula"
        }
      }
    },
    "CollectQuery": {
      "description": "A collection through a comprehension.", "$comment": "SPEC §52.1",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "queryId",
        "kind",
        "comprehension"
      ],
      "properties": {
        "queryId": {
          "$ref": "#/$defs/QueryId"
        },
        "kind": {
          "const": "collect"
        },
        "comprehension": {
          "$ref": "#/$defs/QueryComprehension"
        }
      }
    },
    "TermQuery": {
      "description": "The value of a term over a ready store. The term is a model Term or a comparison (for a term query a comparison counts as a boolean-valued term, which is how pinned reference outputs supply it); a comprehension inside follows QueryComprehension, where a variable may lack a type. The term query schema looks no deeper than kind — term grammar is fixed by the model schema.", "$comment": "SPEC §57; SPEC §58; SPEC §59; E-0067",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "queryId",
        "kind",
        "term"
      ],
      "properties": {
        "queryId": {
          "$ref": "#/$defs/QueryId"
        },
        "kind": {
          "const": "term"
        },
        "term": {
          "type": "object",
          "required": [
            "kind"
          ],
          "properties": {
            "kind": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      }
    },
    "PositionsQuery": {
      "description": "The normative positions of the whole case; no arguments.", "$comment": "SPEC §134-§143",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "queryId",
        "kind"
      ],
      "properties": {
        "queryId": {
          "$ref": "#/$defs/QueryId"
        },
        "kind": {
          "const": "positions"
        }
      }
    },
    "WeakPermissionQuery": {
      "description": "Weak permission: the action is not forbidden under the closure policy. policy is the closure policy as an object, not a surface call argument.", "$comment": "SPEC §126; SPEC §70; E-0034",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "queryId",
        "kind",
        "actor",
        "action"
      ],
      "properties": {
        "queryId": {
          "$ref": "#/$defs/QueryId"
        },
        "kind": {
          "const": "weak_permission"
        },
        "actor": {
          "$ref": "legal-ir.schema.json#/$defs/Term"
        },
        "action": {
          "$ref": "legal-ir.schema.json#/$defs/Term"
        },
        "policy": {
          "type": "object"
        }
      }
    },
    "InterpretationAnalysisQuery": {
      "description": "One query per reading of the group. The only place where a query takes a query — one level deep.", "$comment": "SPEC §152-§157; SPEC §172",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "queryId",
        "kind",
        "group",
        "query"
      ],
      "properties": {
        "queryId": {
          "$ref": "#/$defs/QueryId"
        },
        "kind": {
          "const": "interpretation_analysis"
        },
        "group": {
          "$ref": "legal-ir.schema.json#/$defs/StableId"
        },
        "query": {
          "description": "Nested query without its own `queryId` (the id is inherited from the outer one) and not of kind interpretation_analysis.",
          "type": "object",
          "required": [
            "kind"
          ],
          "properties": {
            "kind": {
              "enum": [
                "truth",
                "why_not",
                "argumentation",
                "precedent",
                "formula",
                "collect",
                "term",
                "positions",
                "weak_permission",
                "calendar_op"
              ]
            }
          }
        }
      }
    },
    "CalendarOpQuery": {
      "description": "A calendar operation over a pinned snapshot. days is required for add_business_days and deadline; policy for deadline is the deadline policy, taken from the case context when absent; suspensions come from the case data.", "$comment": "SPEC §85; SPEC §86",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "queryId",
        "kind",
        "op",
        "date"
      ],
      "properties": {
        "queryId": {
          "$ref": "#/$defs/QueryId"
        },
        "kind": {
          "const": "calendar_op"
        },
        "op": {
          "enum": [
            "is_working_day",
            "previous_working_day",
            "add_business_days",
            "deadline"
          ]
        },
        "date": {
          "$ref": "legal-ir.schema.json#/$defs/DateString"
        },
        "days": {
          "type": "integer"
        },
        "afterTime": {
          "type": "string",
          "pattern": "^[0-9]{2}:[0-9]{2}(:[0-9]{2})?$"
        },
        "unit": {
          "enum": [
            "business_day",
            "calendar_day"
          ]
        },
        "policy": {
          "type": "object"
        },
        "suspensions": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "start",
              "end"
            ],
            "properties": {
              "start": {
                "$ref": "legal-ir.schema.json#/$defs/DateString"
              },
              "end": {
                "$ref": "legal-ir.schema.json#/$defs/DateString"
              }
            }
          }
        }
      }
    },
    "QueryComprehension": {
      "description": "A comprehension in a query. Same shape as a comprehension term in the model, except that a query variable may lack type — the type is known from the generator predicate’s declaration, which is how pinned reference outputs supply it. The query schema looks no deeper than generator and element: formula and term grammar belongs to the model schema, and a query outside it fails at evaluation with an ordinary diagnostic.", "$comment": "SPEC §52.1",
      "type": "object",
      "required": [
        "kind",
        "variable",
        "element",
        "generator",
        "distinct"
      ],
      "properties": {
        "kind": {
          "const": "comprehension"
        },
        "variable": {
          "type": "object",
          "required": [
            "id"
          ],
          "properties": {
            "id": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "binders": {
          "type": "array"
        },
        "element": {
          "type": "object",
          "required": [
            "kind"
          ]
        },
        "generator": {
          "type": "object",
          "required": [
            "kind"
          ]
        },
        "distinct": {
          "type": "boolean"
        }
      }
    }
  }
}
