{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:orbiplex:schema:nse-hook-offer:v1",
  "title": "NSE Hook Offer v1",
  "description": "Exact host-built offer for one NSE invocation. V1 admits model selection and the closed reference-set policy-hook family.",
  "x-dia-workflow": "project",
  "x-dia-status": "accepted",
  "x-dia-basis": [
    "doc/project/40-proposals/085-operator-sovereign-extensibility-and-experiment-packages.md"
  ],
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema", "schema/v", "invocation/ref", "hook/id", "hook/v", "hook/class",
    "offer/digest", "causal/ref", "backend/bounds", "payload"
  ],
  "properties": {
    "schema": { "const": "nse-hook-offer.v1" },
    "schema/v": { "const": 1 },
    "invocation/ref": { "type": "string", "pattern": "^nse-invocation:[0-9A-HJKMNP-TV-Z]{26}$" },
    "hook/id": { "$ref": "#/$defs/hook-id" },
    "hook/v": { "const": 1 },
    "hook/class": { "enum": ["select", "order", "narrow", "restrict", "raise-risk", "select-profile"] },
    "offer/digest": { "$ref": "#/$defs/digest" },
    "causal/ref": { "$ref": "#/$defs/ref" },
    "backend/bounds": {
      "type": "object", "additionalProperties": false,
      "required": ["timeout/ms", "input/max-bytes", "output/max-bytes"],
      "properties": {
        "timeout/ms": { "type": "integer", "minimum": 1 },
        "input/max-bytes": { "type": "integer", "minimum": 1 },
        "output/max-bytes": { "type": "integer", "minimum": 1 }
      }
    },
    "payload": {
      "oneOf": [
        { "$ref": "#/$defs/select-llm-payload" },
        { "$ref": "#/$defs/policy-hook-payload" }
      ]
    }
  },
  "allOf": [
    {
      "if": { "properties": { "hook/id": { "const": "select-llm-model" } }, "required": ["hook/id"] },
      "then": {
        "properties": {
          "hook/class": { "const": "select" },
          "payload": { "$ref": "#/$defs/select-llm-payload" }
        }
      },
      "else": { "properties": { "payload": { "$ref": "#/$defs/policy-hook-payload" } } }
    },
    { "if": { "properties": { "hook/id": { "enum": ["assemble-prompt", "score-candidate", "select-turn-order", "weigh-bid"] } }, "required": ["hook/id"] }, "then": { "properties": { "hook/class": { "const": "order" } } } },
    { "if": { "properties": { "hook/id": { "enum": ["select-output-schema", "resolve-tie", "choose-next-step"] } }, "required": ["hook/id"] }, "then": { "properties": { "hook/class": { "const": "select" } } } },
    { "if": { "properties": { "hook/id": { "const": "select-repair-profile" } }, "required": ["hook/id"] }, "then": { "properties": { "hook/class": { "const": "select-profile" } } } },
    { "if": { "properties": { "hook/id": { "const": "admit-participant" } }, "required": ["hook/id"] }, "then": { "properties": { "hook/class": { "const": "restrict" } } } },
    { "if": { "properties": { "hook/id": { "const": "shape-fanout" } }, "required": ["hook/id"] }, "then": { "properties": { "hook/class": { "const": "narrow" } } } },
    { "if": { "properties": { "hook/id": { "const": "classify-effect-risk" } }, "required": ["hook/id"] }, "then": { "properties": { "hook/class": { "const": "raise-risk" } } } }
  ],
  "$defs": {
    "hook-id": {
      "enum": [
        "select-llm-model", "assemble-prompt", "select-output-schema",
        "select-repair-profile", "score-candidate", "select-turn-order",
        "weigh-bid", "resolve-tie", "admit-participant", "choose-next-step",
        "shape-fanout", "classify-effect-risk"
      ]
    },
    "select-llm-payload": {
      "type": "object", "additionalProperties": false,
      "required": ["purpose", "candidates", "constraints", "context"],
      "properties": {
        "purpose": { "$ref": "#/$defs/text" },
        "candidates": {
          "type": "array", "minItems": 1, "maxItems": 4096,
          "items": { "$ref": "#/$defs/candidate" }
        },
        "constraints": {
          "type": "object", "additionalProperties": false,
          "required": ["required_capabilities", "preferred_capabilities", "privacy_mode", "max_latency_tier", "max_cost_tier"],
          "properties": {
            "required_capabilities": { "$ref": "#/$defs/text-array" },
            "preferred_capabilities": { "$ref": "#/$defs/text-array" },
            "privacy_mode": { "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/text" }] },
            "max_latency_tier": { "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/text" }] },
            "max_cost_tier": { "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/text" }] }
          }
        },
        "context": { "type": "object", "maxProperties": 64, "additionalProperties": true }
      }
    },
    "policy-hook-payload": {
      "type": "object", "additionalProperties": false,
      "required": ["subject/ref", "candidate/refs", "required/refs", "current/limits", "selected/max", "review/required-floor", "context"],
      "properties": {
        "subject/ref": { "$ref": "#/$defs/ref" },
        "candidate/refs": { "type": "array", "maxItems": 4096, "uniqueItems": true, "items": { "$ref": "#/$defs/ref" } },
        "required/refs": { "type": "array", "maxItems": 4096, "uniqueItems": true, "items": { "$ref": "#/$defs/ref" } },
        "current/limits": { "type": "object", "maxProperties": 64, "propertyNames": { "$ref": "#/$defs/ref" }, "additionalProperties": { "type": "integer", "minimum": 0 } },
        "selected/max": { "type": "integer", "minimum": 0, "maximum": 4096 },
        "review/required-floor": { "type": "boolean" },
        "risk/floor": { "enum": ["low", "elevated", "high", "critical"] },
        "context": { "type": "object", "maxProperties": 64, "additionalProperties": true }
      }
    },
    "digest": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
    "ref": { "type": "string", "minLength": 1, "maxLength": 256, "pattern": "^[^\\u0000-\\u001F\\u007F]+$" },
    "text": { "type": "string", "minLength": 1, "maxLength": 256, "pattern": "^[^\\u0000-\\u001F\\u007F]+$" },
    "text-array": { "type": "array", "maxItems": 128, "uniqueItems": true, "items": { "$ref": "#/$defs/text" } },
    "candidate": {
      "type": "object", "additionalProperties": false,
      "required": ["runtime_id", "id", "provider", "transport_kind", "capability_tags", "metadata"],
      "properties": {
        "runtime_id": { "$ref": "#/$defs/ref" },
        "id": { "$ref": "#/$defs/ref" },
        "provider": { "$ref": "#/$defs/text" },
        "transport_kind": { "$ref": "#/$defs/text" },
        "adapter_instance_ref": { "$ref": "#/$defs/ref" },
        "model_binding_ref": { "$ref": "#/$defs/ref" },
        "operations": { "$ref": "#/$defs/text-array" },
        "capabilities": { "type": "object", "maxProperties": 128, "additionalProperties": true },
        "policy_summary": { "type": "object", "maxProperties": 128, "additionalProperties": true },
        "capability_tags": { "$ref": "#/$defs/text-array" },
        "metadata": { "type": "object", "maxProperties": 128, "additionalProperties": true }
      }
    }
  }
}
