{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://miruntime.com/downloads/miruntime-runtime-contract.schema.json",
  "title": "MiRuntime Conceptual Runtime Contract",
  "description": "Editorial reference schema for a governed Machine Intelligence Runtime run. It is implementation-neutral and does not represent a shipped SDK.",
  "type": "object",
  "additionalProperties": false,
  "required": ["contractVersion", "runId", "intent", "context", "authority", "execution", "evidence"],
  "properties": {
    "contractVersion": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$" },
    "runId": { "type": "string", "minLength": 1, "maxLength": 200 },
    "intent": { "$ref": "#/$defs/intent" },
    "context": { "$ref": "#/$defs/context" },
    "authority": { "$ref": "#/$defs/authority" },
    "execution": { "$ref": "#/$defs/execution" },
    "evidence": { "$ref": "#/$defs/evidence" }
  },
  "$defs": {
    "intent": {
      "type": "object",
      "additionalProperties": false,
      "required": ["objective", "riskClass", "successCriteria"],
      "properties": {
        "objective": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "riskClass": { "type": "string", "enum": ["read-only", "draft", "reversible-change", "irreversible-change", "high-impact-decision"] },
        "successCriteria": { "type": "array", "minItems": 1, "items": { "type": "string", "minLength": 1 } },
        "requestedBy": { "type": "string", "minLength": 1 }
      }
    },
    "contextSource": {
      "type": "object",
      "additionalProperties": false,
      "required": ["sourceId", "uri", "provenance", "scope"],
      "properties": {
        "sourceId": { "type": "string", "minLength": 1 },
        "uri": { "type": "string", "format": "uri-reference" },
        "provenance": { "type": "string", "minLength": 1 },
        "scope": { "type": "string", "minLength": 1 },
        "classification": { "type": "string" },
        "contentHash": { "type": "string" }
      }
    },
    "context": {
      "type": "object",
      "additionalProperties": false,
      "required": ["sources", "memoryScopes", "prohibitedDataClasses"],
      "properties": {
        "sources": { "type": "array", "items": { "$ref": "#/$defs/contextSource" } },
        "memoryScopes": { "type": "array", "uniqueItems": true, "items": { "type": "string" } },
        "prohibitedDataClasses": { "type": "array", "uniqueItems": true, "items": { "type": "string" } },
        "freshnessPolicy": { "type": "string" }
      }
    },
    "authority": {
      "type": "object",
      "additionalProperties": false,
      "required": ["subject", "allowedTools", "writePolicy", "requiredApprovals"],
      "properties": {
        "subject": { "type": "string", "minLength": 1 },
        "allowedTools": { "type": "array", "uniqueItems": true, "items": { "type": "string" } },
        "writePolicy": { "type": "string", "enum": ["none", "draft-only", "reversible", "approved-irreversible"] },
        "requiredApprovals": { "type": "array", "items": { "type": "string" } },
        "credentialAudience": { "type": "array", "items": { "type": "string" } },
        "allowedEgress": { "type": "array", "items": { "type": "string" } }
      }
    },
    "execution": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "checkpointPolicy", "budgets"],
      "properties": {
        "state": { "type": "string", "enum": ["created", "provisioning", "running", "awaiting-approval", "recovering", "completed", "failed", "terminated"] },
        "checkpointPolicy": { "type": "string", "enum": ["none", "periodic", "before-side-effect", "before-risk-transition"] },
        "budgets": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "maxDurationSeconds": { "type": "integer", "minimum": 1 },
            "maxModelCalls": { "type": "integer", "minimum": 0 },
            "maxToolCalls": { "type": "integer", "minimum": 0 },
            "maxCost": { "type": "number", "minimum": 0 },
            "maxTokens": { "type": "integer", "minimum": 0 }
          }
        },
        "startedAtUtc": { "type": "string", "format": "date-time" },
        "stoppingConditions": { "type": "array", "items": { "type": "string" } },
        "recoveryPolicy": { "type": "string" }
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["events", "artifacts", "unresolvedUncertainty"],
      "properties": {
        "events": { "type": "array", "items": { "type": "string" } },
        "artifacts": { "type": "array", "items": { "type": "string" } },
        "unresolvedUncertainty": { "type": "array", "items": { "type": "string" } },
        "ledgerUri": { "type": "string", "format": "uri-reference" }
      }
    }
  },
  "examples": [
    {
      "contractVersion": "1.0.0",
      "runId": "run-dependency-update-001",
      "intent": {
        "objective": "Prepare and validate a repository dependency update",
        "riskClass": "reversible-change",
        "successCriteria": ["Patch prepared", "Tests pass", "No deployment without approval"]
      },
      "context": { "sources": [], "memoryScopes": ["project"], "prohibitedDataClasses": ["production-secrets"] },
      "authority": { "subject": "developer-agent", "allowedTools": ["repository.read", "sandbox.test"], "writePolicy": "draft-only", "requiredApprovals": ["deployment"] },
      "execution": { "state": "running", "checkpointPolicy": "before-side-effect", "budgets": { "maxDurationSeconds": 1800, "maxToolCalls": 20 } },
      "evidence": { "events": [], "artifacts": [], "unresolvedUncertainty": [] }
    }
  ]
}
