{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://hypercarrier.local/schemas/rarebit-materialization.json",
  "title": "Rarebit compact one-file sidecar protocol",
  "oneOf": [
    { "$ref": "#/definitions/summary" },
    { "$ref": "#/definitions/head" },
    { "$ref": "#/definitions/title" }
  ],
  "definitions": {
    "sha256": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    },
    "nullableString": {
      "type": ["string", "null"]
    },
    "nullableNonNegativeNumber": {
      "type": ["number", "null"],
      "minimum": 0
    },
    "branch": {
      "type": "object",
      "additionalProperties": false,
      "required": ["leafId", "entryCount", "pathHash"],
      "properties": {
        "leafId": { "type": ["string", "null"] },
        "entryCount": { "type": "integer", "minimum": 0 },
        "pathHash": { "$ref": "#/definitions/sha256" }
      }
    },
    "selection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "manifestHash",
        "selectorVersion",
        "occurrenceCount",
        "uniquePayloadCount",
        "latestUserSourceEntryId"
      ],
      "properties": {
        "manifestHash": { "$ref": "#/definitions/sha256" },
        "selectorVersion": { "type": "string", "minLength": 1 },
        "occurrenceCount": { "type": "integer", "minimum": 0 },
        "uniquePayloadCount": { "type": "integer", "minimum": 0 },
        "latestUserSourceEntryId": { "$ref": "#/definitions/nullableString" }
      }
    },
    "model": {
      "oneOf": [
        { "type": "null" },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["provider", "id"],
          "properties": {
            "provider": { "type": "string", "minLength": 1 },
            "id": { "type": "string", "minLength": 1 }
          }
        }
      ]
    },
    "modelProvenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source", "status"],
      "properties": {
        "source": { "type": "string", "minLength": 1 },
        "status": { "type": "string", "minLength": 1 },
        "settingsKey": { "type": "string", "minLength": 1 }
      }
    },
    "inputCoveragePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["strategy", "maxPromptChars"],
      "properties": {
        "strategy": {
          "enum": [
            "complete_or_explicit_overflow",
            "newest_suffix_with_explicit_omission"
          ]
        },
        "maxPromptChars": { "type": "integer", "minimum": 1 }
      }
    },
    "inputCoverage": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "totalMessageCount",
        "includedMessageCount",
        "omittedMessageCount",
        "omittedTextChars",
        "promptChars",
        "complete"
      ],
      "properties": {
        "totalMessageCount": { "type": "integer", "minimum": 0 },
        "includedMessageCount": { "type": "integer", "minimum": 0 },
        "omittedMessageCount": { "type": "integer", "minimum": 0 },
        "omittedTextChars": { "type": "integer", "minimum": 0 },
        "promptChars": { "type": "integer", "minimum": 0 },
        "complete": { "type": "boolean" }
      }
    },
    "error": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "message"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "message": { "type": "string" }
      }
    },
    "overflow": {
      "type": "object",
      "additionalProperties": false,
      "required": ["promptChars", "maxPromptChars", "strategy"],
      "properties": {
        "promptChars": { "type": "integer", "minimum": 0 },
        "maxPromptChars": { "type": "integer", "minimum": 1 },
        "strategy": {
          "enum": ["none", "fixed_contract_exceeds_limit"]
        }
      }
    },
    "synthesis": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "kind",
        "outcome",
        "requestedModel",
        "timing",
        "provider",
        "usage"
      ],
      "properties": {
        "schemaVersion": { "const": 1 },
        "kind": { "const": "rarebit_model_synthesis" },
        "outcome": { "type": "string", "minLength": 1 },
        "requestedModel": { "$ref": "#/definitions/model" },
        "timing": {
          "type": "object",
          "additionalProperties": false,
          "required": ["startedAt", "completedAt", "durationMs", "provenance"],
          "properties": {
            "startedAt": { "$ref": "#/definitions/nullableString" },
            "completedAt": { "$ref": "#/definitions/nullableString" },
            "durationMs": { "$ref": "#/definitions/nullableNonNegativeNumber" },
            "provenance": { "const": "local_monotonic_clock" }
          }
        },
        "provider": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "responseProvider",
            "responseProviderSource",
            "responseModel",
            "responseModelSource",
            "responseId",
            "responseIdSource",
            "requestId",
            "requestIdSource"
          ],
          "properties": {
            "responseProvider": { "$ref": "#/definitions/nullableString" },
            "responseProviderSource": {
              "$ref": "#/definitions/nullableString"
            },
            "responseModel": { "$ref": "#/definitions/nullableString" },
            "responseModelSource": { "$ref": "#/definitions/nullableString" },
            "responseId": { "$ref": "#/definitions/nullableString" },
            "responseIdSource": { "$ref": "#/definitions/nullableString" },
            "requestId": { "$ref": "#/definitions/nullableString" },
            "requestIdSource": { "$ref": "#/definitions/nullableString" }
          }
        },
        "usage": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "availability",
            "inputTokens",
            "outputTokens",
            "totalTokens",
            "cacheReadTokens",
            "cacheWriteTokens",
            "reasoningTokens",
            "estimatedCostUsd",
            "provenance"
          ],
          "properties": {
            "availability": { "enum": ["unavailable", "partial", "reported"] },
            "inputTokens": {
              "$ref": "#/definitions/nullableNonNegativeNumber"
            },
            "outputTokens": {
              "$ref": "#/definitions/nullableNonNegativeNumber"
            },
            "totalTokens": {
              "$ref": "#/definitions/nullableNonNegativeNumber"
            },
            "cacheReadTokens": {
              "$ref": "#/definitions/nullableNonNegativeNumber"
            },
            "cacheWriteTokens": {
              "$ref": "#/definitions/nullableNonNegativeNumber"
            },
            "reasoningTokens": {
              "$ref": "#/definitions/nullableNonNegativeNumber"
            },
            "estimatedCostUsd": {
              "$ref": "#/definitions/nullableNonNegativeNumber"
            },
            "provenance": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "inputTokens",
                "outputTokens",
                "totalTokens",
                "cacheReadTokens",
                "cacheWriteTokens",
                "reasoningTokens",
                "estimatedCostUsd"
              ],
              "properties": {
                "inputTokens": { "$ref": "#/definitions/nullableString" },
                "outputTokens": { "$ref": "#/definitions/nullableString" },
                "totalTokens": { "$ref": "#/definitions/nullableString" },
                "cacheReadTokens": { "$ref": "#/definitions/nullableString" },
                "cacheWriteTokens": { "$ref": "#/definitions/nullableString" },
                "reasoningTokens": { "$ref": "#/definitions/nullableString" },
                "estimatedCostUsd": { "$ref": "#/definitions/nullableString" }
              }
            }
          }
        }
      }
    },
    "automaticSummaryPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "contractVersion",
        "decision",
        "queryStatus",
        "queryId",
        "provider",
        "reason",
        "observedAt",
        "validUntil",
        "queriedAt",
        "provenance"
      ],
      "properties": {
        "contractVersion": { "const": "rarebit-automatic-summary-policy/1" },
        "decision": { "const": "inhibit" },
        "queryStatus": { "const": "inhibited" },
        "queryId": { "type": "string", "minLength": 1 },
        "provider": { "type": "string", "minLength": 1 },
        "reason": { "type": "string", "minLength": 1 },
        "observedAt": { "type": "string", "format": "date-time" },
        "validUntil": { "type": "string", "format": "date-time" },
        "queriedAt": { "type": "string", "format": "date-time" },
        "provenance": {
          "type": "object",
          "additionalProperties": false,
          "required": ["identity", "generation", "association"],
          "properties": {
            "identity": { "type": "string", "minLength": 1 },
            "generation": { "type": "string", "minLength": 1 },
            "association": { "type": "string", "minLength": 1 }
          }
        }
      }
    },
    "summary": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "type",
        "status",
        "jobId",
        "sessionId",
        "branch",
        "observedAt",
        "selection",
        "lifecycleBoundary",
        "implementationVersion",
        "synthesisMode",
        "inputCoveragePolicy",
        "promptVersion",
        "model",
        "modelProvenance"
      ],
      "properties": {
        "schemaVersion": { "const": 4 },
        "type": { "const": "rarebit_summary" },
        "status": {
          "enum": [
            "ok",
            "ineligible",
            "inhibited",
            "unavailable_overflow",
            "failure"
          ]
        },
        "jobId": { "$ref": "#/definitions/sha256" },
        "sessionId": { "type": "string", "minLength": 1 },
        "branch": { "$ref": "#/definitions/branch" },
        "observedAt": { "type": "string", "format": "date-time" },
        "selection": { "$ref": "#/definitions/selection" },
        "lifecycleBoundary": {
          "enum": ["owner_request", "agent_settled", "session_start", "manual"]
        },
        "implementationVersion": {
          "enum": [
            "hc-rarebit-summary-v4",
            "hc-rarebit-summary-v5",
            "hc-rarebit-summary-v6"
          ]
        },
        "synthesisMode": { "enum": ["forced", "automatic"] },
        "inputCoveragePolicy": { "$ref": "#/definitions/inputCoveragePolicy" },
        "promptVersion": { "type": "string", "minLength": 1 },
        "model": { "$ref": "#/definitions/model" },
        "modelProvenance": { "$ref": "#/definitions/modelProvenance" },
        "summary": { "type": "string", "minLength": 1 },
        "sessionStatus": {
          "enum": ["user_requested", "finished", "needs_attention"]
        },
        "statusReason": {
          "enum": [
            "owner_request_recorded",
            "all_requests_accomplished",
            "decision",
            "input",
            "approval",
            "blocker",
            "unfinished",
            "uncertain",
            "conflicting_evidence"
          ]
        },
        "inputCoverage": { "$ref": "#/definitions/inputCoverage" },
        "synthesis": { "$ref": "#/definitions/synthesis" },
        "automaticSummaryPolicy": {
          "$ref": "#/definitions/automaticSummaryPolicy"
        },
        "overflow": { "$ref": "#/definitions/overflow" },
        "retryable": { "type": "boolean" },
        "error": { "$ref": "#/definitions/error" }
      },
      "allOf": [
        {
          "oneOf": [
            {
              "properties": { "status": { "const": "ok" } },
              "required": [
                "summary",
                "sessionStatus",
                "statusReason",
                "synthesis"
              ],
              "not": {
                "anyOf": [
                  { "required": ["automaticSummaryPolicy"] },
                  { "required": ["overflow"] },
                  { "required": ["retryable"] },
                  { "required": ["error"] }
                ]
              }
            },
            {
              "properties": { "status": { "const": "ineligible" } },
              "not": {
                "anyOf": [
                  { "required": ["summary"] },
                  { "required": ["sessionStatus"] },
                  { "required": ["statusReason"] },
                  { "required": ["synthesis"] },
                  { "required": ["automaticSummaryPolicy"] },
                  { "required": ["overflow"] },
                  { "required": ["retryable"] },
                  { "required": ["error"] }
                ]
              }
            },
            {
              "properties": { "status": { "const": "inhibited" } },
              "required": ["automaticSummaryPolicy"],
              "not": {
                "anyOf": [
                  { "required": ["summary"] },
                  { "required": ["sessionStatus"] },
                  { "required": ["statusReason"] },
                  { "required": ["synthesis"] },
                  { "required": ["overflow"] },
                  { "required": ["retryable"] },
                  { "required": ["error"] }
                ]
              }
            },
            {
              "properties": { "status": { "const": "unavailable_overflow" } },
              "required": ["overflow"],
              "not": {
                "anyOf": [
                  { "required": ["summary"] },
                  { "required": ["sessionStatus"] },
                  { "required": ["statusReason"] },
                  { "required": ["synthesis"] },
                  { "required": ["automaticSummaryPolicy"] },
                  { "required": ["retryable"] },
                  { "required": ["error"] }
                ]
              }
            },
            {
              "properties": { "status": { "const": "failure" } },
              "required": ["retryable", "error"],
              "not": {
                "anyOf": [
                  { "required": ["summary"] },
                  { "required": ["sessionStatus"] },
                  { "required": ["statusReason"] },
                  { "required": ["synthesis"] },
                  { "required": ["automaticSummaryPolicy"] },
                  { "required": ["overflow"] }
                ]
              }
            }
          ]
        },
        {
          "if": {
            "properties": { "sessionStatus": { "const": "finished" } },
            "required": ["sessionStatus"]
          },
          "then": {
            "properties": {
              "statusReason": { "const": "all_requests_accomplished" }
            }
          }
        },
        {
          "if": {
            "properties": { "sessionStatus": { "const": "needs_attention" } },
            "required": ["sessionStatus"]
          },
          "then": {
            "properties": {
              "statusReason": {
                "enum": [
                  "decision",
                  "input",
                  "approval",
                  "blocker",
                  "unfinished"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": { "lifecycleBoundary": { "const": "owner_request" } },
            "required": ["lifecycleBoundary"]
          },
          "then": {
            "properties": {
              "selection": {
                "properties": {
                  "latestUserSourceEntryId": {
                    "type": "string",
                    "minLength": 1
                  }
                }
              },
              "sessionStatus": { "const": "user_requested" },
              "statusReason": { "const": "owner_request_recorded" }
            }
          },
          "else": {
            "not": {
              "properties": { "sessionStatus": { "const": "user_requested" } },
              "required": ["sessionStatus"]
            }
          }
        }
      ]
    },
    "head": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "protocolVersion",
        "sessionId",
        "receiptOffset",
        "receiptLength",
        "receiptHash"
      ],
      "properties": {
        "type": { "const": "rarebit_head" },
        "protocolVersion": { "const": 1 },
        "sessionId": { "type": "string", "minLength": 1 },
        "receiptOffset": { "type": "integer", "minimum": 0 },
        "receiptLength": { "type": "integer", "minimum": 1, "maximum": 49152 },
        "receiptHash": { "$ref": "#/definitions/sha256" }
      }
    },
    "titleEvidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["provenance", "sourceEntryId"],
      "properties": {
        "provenance": { "type": "string", "minLength": 1 },
        "sourceEntryId": { "$ref": "#/definitions/nullableString" }
      }
    },
    "title": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "type",
        "status",
        "jobId",
        "implementationVersion",
        "sessionId",
        "branch",
        "selectionManifestHash",
        "titleEvidence",
        "promptVersion",
        "model",
        "modelProvenance",
        "applicationMode",
        "priorTitle",
        "title",
        "observedAt"
      ],
      "properties": {
        "schemaVersion": { "const": 4 },
        "type": { "const": "rarebit_title" },
        "status": {
          "enum": [
            "proposal",
            "applied",
            "skipped_session_changed",
            "skipped_title_changed",
            "skipped_existing_title",
            "failure"
          ]
        },
        "jobId": { "$ref": "#/definitions/sha256" },
        "implementationVersion": { "const": "hc-rarebit-title-v4" },
        "sessionId": { "type": "string", "minLength": 1 },
        "branch": { "$ref": "#/definitions/branch" },
        "selectionManifestHash": { "$ref": "#/definitions/sha256" },
        "titleEvidence": { "$ref": "#/definitions/titleEvidence" },
        "promptVersion": { "type": "string", "minLength": 1 },
        "model": { "$ref": "#/definitions/model" },
        "modelProvenance": { "$ref": "#/definitions/modelProvenance" },
        "applicationMode": { "enum": ["apply", "proposal"] },
        "priorTitle": { "$ref": "#/definitions/nullableString" },
        "title": { "$ref": "#/definitions/nullableString" },
        "observedAt": { "type": "string", "format": "date-time" },
        "synthesis": { "$ref": "#/definitions/synthesis" },
        "retryable": { "type": "boolean" },
        "error": { "$ref": "#/definitions/error" }
      },
      "oneOf": [
        {
          "properties": {
            "status": { "const": "failure" },
            "title": { "type": "null" }
          },
          "required": ["retryable", "error"],
          "not": { "required": ["synthesis"] }
        },
        {
          "properties": {
            "status": { "enum": ["proposal", "applied"] },
            "title": { "type": "string", "minLength": 1 }
          },
          "required": ["synthesis"],
          "not": {
            "anyOf": [{ "required": ["retryable"] }, { "required": ["error"] }]
          }
        },
        {
          "properties": {
            "status": {
              "enum": [
                "skipped_session_changed",
                "skipped_title_changed",
                "skipped_existing_title"
              ]
            },
            "title": { "type": "null" }
          },
          "required": ["synthesis"],
          "not": {
            "anyOf": [{ "required": ["retryable"] }, { "required": ["error"] }]
          }
        }
      ]
    }
  }
}
