{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://okstra.dev/schemas/execution-manifest-v2.json",
  "title": "OKSTRA Execution Manifest (v2)",
  "type": "object",
  "required": [
    "schemaVersion",
    "executionIdentityVersion",
    "entryMode",
    "participantAssignments",
    "roleExecutions",
    "invocations",
    "attempts",
    "attemptEvidenceSeals",
    "mutationRecoveryDecisions"
  ],
  "properties": {
    "schemaVersion": {"const": "2.0"},
    "executionIdentityVersion": {"const": 2},
    "entryMode": {"enum": ["current-session", "new-session"]},
    "participantAssignments": {
      "type": "array",
      "items": {"$ref": "#/$defs/ParticipantAssignment"}
    },
    "roleExecutions": {
      "type": "array",
      "items": {"$ref": "#/$defs/RoleExecution"}
    },
    "invocations": {
      "type": "array",
      "items": {"$ref": "#/$defs/Invocation"}
    },
    "attempts": {
      "type": "array",
      "items": {"$ref": "#/$defs/Attempt"}
    },
    "attemptEvidenceSeals": {
      "type": "array",
      "items": {"$ref": "#/$defs/AttemptEvidenceSeal"}
    },
    "mutationRecoveryDecisions": {
      "type": "array",
      "items": {"$ref": "#/$defs/MutationRecoveryDecision"}
    }
  },
  "additionalProperties": true,
  "$defs": {
    "Digest": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "NullableDigest": {
      "type": ["string", "null"],
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "WorkerWriteCapability": {
      "type": ["object", "null"],
      "required": ["maxBoundaryPrecision"],
      "properties": {
        "maxBoundaryPrecision": {
          "enum": ["exact-path", "directory-boundary", "none"]
        }
      },
      "additionalProperties": false
    },
    "ParticipantAssignment": {
      "type": "object",
      "required": [
        "participantRef",
        "provider",
        "modelRef",
        "modelId",
        "runner",
        "hostRuntime",
        "hostModelValue",
        "sessionRef",
        "windowRef",
        "workerWriteCapability",
        "entryMode",
        "status"
      ],
      "properties": {
        "participantRef": {"type": "string", "pattern": "\\S"},
        "provider": {"type": "string", "pattern": "\\S"},
        "modelRef": {"type": ["string", "null"]},
        "modelId": {"type": "string", "pattern": "\\S"},
        "runner": {"type": "string", "pattern": "\\S"},
        "hostRuntime": {"type": "string", "pattern": "\\S"},
        "hostModelValue": {"type": ["string", "null"]},
        "sessionRef": {"type": ["string", "null"]},
        "windowRef": {"type": ["string", "null"]},
        "workerWriteCapability": {"$ref": "#/$defs/WorkerWriteCapability"},
        "entryMode": {"enum": ["current-session", "new-session"]},
        "status": {"type": "string", "pattern": "\\S"}
      },
      "additionalProperties": false
    },
    "HostModelBinding": {
      "type": ["object", "null"],
      "required": [
        "runner",
        "catalogExecutionValue",
        "resolvedExecutionValue",
        "hostModelValue",
        "bindingFidelity",
        "workerWriteCapability"
      ],
      "properties": {
        "runner": {"type": "string", "pattern": "\\S"},
        "catalogExecutionValue": {"type": "string"},
        "resolvedExecutionValue": {"type": "string"},
        "hostModelValue": {"type": ["string", "null"]},
        "bindingFidelity": {"enum": ["exact", "channel", "unsupported"]},
        "workerWriteCapability": {"$ref": "#/$defs/WorkerWriteCapability"}
      },
      "additionalProperties": false
    },
    "ServedModelAttestation": {
      "type": "object",
      "required": ["observedModel", "normalizedModelRef", "level", "source"],
      "properties": {
        "observedModel": {"type": ["string", "null"]},
        "normalizedModelRef": {"type": ["string", "null"]},
        "level": {"enum": ["exact", "channel", "unknown"]},
        "source": {
          "enum": ["provider-output", "native-api", "cli-handoff", "unavailable"]
        }
      },
      "additionalProperties": false
    },
    "WritePolicy": {
      "type": "object",
      "required": ["artifactPolicy", "sourcePolicy", "gitPolicy", "auxiliaryPolicy"],
      "properties": {
        "artifactPolicy": {
          "type": "object",
          "required": ["allowedRoot", "allowedPaths"],
          "properties": {
            "allowedRoot": {"type": "string", "pattern": "^/"},
            "allowedPaths": {"type": "array", "items": {"type": "string", "pattern": "\\S"}, "minItems": 1, "uniqueItems": true}
          },
          "additionalProperties": false
        },
        "sourcePolicy": {
          "type": "object",
          "required": ["mode", "allowedRoot", "plannedPaths", "protectedPaths"],
          "properties": {
            "mode": {"enum": ["source-readonly", "project-mutation"]},
            "allowedRoot": {"type": "string", "pattern": "^/"},
            "plannedPaths": {"type": "array", "items": {"type": "string", "pattern": "\\S"}, "uniqueItems": true},
            "protectedPaths": {"type": "array", "items": {"type": "string", "pattern": "\\S"}, "uniqueItems": true}
          },
          "additionalProperties": false
        },
        "gitPolicy": {
          "type": "object",
          "required": ["mode"],
          "properties": {
            "mode": {"enum": ["disabled", "fast-forward-descendant-chain"]},
            "worktreeGitDir": {"type": "string", "pattern": "^/"},
            "gitCommonDir": {"type": "string", "pattern": "^/"},
            "expectedBaseCommit": {"type": "string", "pattern": "\\S"},
            "allowedBranchRef": {"type": "string", "pattern": "^refs/heads/"},
            "commitMode": {"const": "fast-forward-descendant-chain"}
          },
          "additionalProperties": false
        },
        "auxiliaryPolicy": {
          "type": "object",
          "required": ["scratchRoots", "generatedPaths", "externalRoots"],
          "properties": {
            "scratchRoots": {"type": "array", "items": {"type": "string", "pattern": "^/"}, "uniqueItems": true},
            "generatedPaths": {"type": "array", "items": {"type": "string", "pattern": "\\S"}, "uniqueItems": true},
            "externalRoots": {"type": "array", "items": {"type": "string", "pattern": "^/"}, "uniqueItems": true}
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "WriteEnforcement": {
      "type": "object",
      "required": ["boundaryPrecision", "mutationAudit", "attribution", "auditedRoots", "observationCoverage", "unobservedWriteSurfaces"],
      "properties": {
        "boundaryPrecision": {"enum": ["exact-path", "directory-boundary", "none"]},
        "mutationAudit": {"enum": ["none", "batch"]},
        "attribution": {"enum": ["call", "batch-unattributed"]},
        "auditedRoots": {"type": "array", "items": {"type": "string", "pattern": "^/"}, "minItems": 1, "uniqueItems": true},
        "observationCoverage": {"enum": ["complete", "scoped", "partial"]},
        "unobservedWriteSurfaces": {"type": "array", "items": {"type": "string", "pattern": "\\S"}, "uniqueItems": true}
      },
      "additionalProperties": false
    },
    "RoleExecution": {
      "type": "object",
      "required": [
        "roleExecutionRef",
        "participantRef",
        "sourceRoleExecutionRef",
        "role",
        "provider",
        "modelRef",
        "modelId",
        "ordinal",
        "executionLabel",
        "modelSpecDigest",
        "binding",
        "servedModelAttestation",
        "status"
      ],
      "properties": {
        "roleExecutionRef": {"type": "string", "pattern": "\\S"},
        "participantRef": {"type": "string", "pattern": "\\S"},
        "sourceRoleExecutionRef": {"type": ["string", "null"]},
        "role": {"type": "string", "pattern": "\\S"},
        "provider": {"type": "string", "pattern": "\\S"},
        "modelRef": {"type": ["string", "null"]},
        "modelId": {"type": "string", "pattern": "\\S"},
        "ordinal": {"type": "integer", "minimum": 1},
        "executionLabel": {"type": "string", "pattern": "\\S"},
        "modelSpecDigest": {"$ref": "#/$defs/NullableDigest"},
        "binding": {"$ref": "#/$defs/HostModelBinding"},
        "servedModelAttestation": {"$ref": "#/$defs/ServedModelAttestation"},
        "status": {"type": "string", "pattern": "\\S"}
      },
      "additionalProperties": false
    },
    "Invocation": {
      "type": "object",
      "required": [
        "invocationRef",
        "participantRef",
        "roleExecutionRef",
        "sourceInvocationRef",
        "recoveryRef",
        "dutyId",
        "dispatchKind",
        "round",
        "inputDigest",
        "writePolicy",
        "writePolicyDigest",
        "writeEnforcement"
      ],
      "properties": {
        "invocationRef": {"type": "string", "pattern": "\\S"},
        "participantRef": {"type": "string", "pattern": "\\S"},
        "roleExecutionRef": {"type": "string", "pattern": "\\S"},
        "sourceInvocationRef": {"type": ["string", "null"]},
        "recoveryRef": {"type": ["string", "null"]},
        "dutyId": {"type": "string", "pattern": "\\S"},
        "dispatchKind": {"type": "string", "pattern": "\\S"},
        "round": {"type": "integer", "minimum": 1},
        "inputDigest": {"$ref": "#/$defs/Digest"},
        "writePolicy": {"$ref": "#/$defs/WritePolicy"},
        "writePolicyDigest": {"$ref": "#/$defs/Digest"},
        "writeEnforcement": {"$ref": "#/$defs/WriteEnforcement"}
      },
      "additionalProperties": false
    },
    "Attempt": {
      "type": "object",
      "required": [
        "invocationRef",
        "attempt",
        "startedAt",
        "finishedAt",
        "status",
        "resultPath",
        "errorPath",
        "changeSummary",
        "evidenceSealRef"
      ],
      "properties": {
        "invocationRef": {"type": "string", "pattern": "\\S"},
        "attempt": {"type": "integer", "minimum": 1},
        "startedAt": {"type": "string", "pattern": "\\S"},
        "finishedAt": {"type": ["string", "null"]},
        "status": {"type": "string", "pattern": "\\S"},
        "resultPath": {"type": ["string", "null"]},
        "errorPath": {"type": ["string", "null"]},
        "changeSummary": {"type": "object"},
        "evidenceSealRef": {"type": ["string", "null"]}
      },
      "additionalProperties": false
    },
    "AttemptEvidenceSeal": {
      "type": "object",
      "required": [
        "sealRef",
        "attemptRef",
        "capturedAt",
        "sourceDiffDigest",
        "gitProjectionDigest",
        "changedPaths",
        "hostEventStream",
        "artifacts"
      ],
      "properties": {
        "sealRef": {"type": "string", "pattern": "\\S"},
        "attemptRef": {"type": "string", "pattern": "\\S"},
        "capturedAt": {"type": "string", "pattern": "\\S"},
        "sourceDiffDigest": {"$ref": "#/$defs/Digest"},
        "gitProjectionDigest": {"$ref": "#/$defs/Digest"},
        "changedPaths": {"type": "array", "items": {"type": "string"}},
        "hostEventStream": {"type": "object"},
        "hostEventStreamRef": {"type": ["string", "null"]},
        "artifacts": {"type": "array", "items": {"type": "object"}}
      },
      "additionalProperties": true
    },
    "MutationRecoveryDecision": {
      "type": "object",
      "required": [
        "recoveryRef",
        "sourceInvocationRef",
        "sourceAttemptRef",
        "decision"
      ],
      "properties": {
        "recoveryRef": {"type": "string", "pattern": "\\S"},
        "sourceInvocationRef": {"type": "string"},
        "sourceAttemptRef": {"type": "string"},
        "sourceEvidenceSealRef": {"type": ["string", "null"]},
        "originalBaselineDigest": {"type": "string"},
        "terminalSourceDiffDigest": {"type": "string"},
        "terminalGitChainDigest": {"type": "string"},
        "verifierRoleExecutionRef": {"type": ["string", "null"]},
        "verifierInvocationRef": {"type": ["string", "null"]},
        "recoveredEvidenceRefs": {"type": "array"},
        "recoveredOutOfPlanEdits": {"type": "array"},
        "unmetObligations": {"type": "array", "items": {"type": "string"}},
        "decision": {
          "enum": ["carry-forward", "reject", "cleanup-confirmed"]
        },
        "reason": {"type": "string"}
      },
      "additionalProperties": true
    }
  }
}
