{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://foundable.com/research/idea-to-signal/v0.1/action-receipt.schema.json",
  "title": "Foundable Action Receipt v0.1",
  "description": "A portable data-shape contract for a bounded action-state record, including limited cross-field conditions. Separate semantic and sanitation gates remain required; publication does not claim that benchmark runs or external actions occurred.",
  "type": "object",
  "additionalProperties": false,
  "required": ["version", "scenarioId", "runId", "authorizedScope", "budget", "action", "status", "evidenceReferences", "reversibility", "elapsedTimeMs", "cost", "humanInterventions", "redactions", "policy", "terminalAt"],
  "properties": {
    "version": { "const": "0.1" },
    "scenarioId": { "$ref": "#/$defs/safeId" },
    "runId": { "$ref": "#/$defs/safeId" },
    "authorizedScope": {
      "type": "object",
      "additionalProperties": false,
      "required": ["summary", "allowedActions", "prohibitedActions", "maxExternalActions"],
      "properties": {
        "summary": { "type": "string", "minLength": 12, "maxLength": 240 },
        "allowedActions": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "$ref": "#/$defs/safeAction" } },
        "prohibitedActions": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "$ref": "#/$defs/safeAction" } },
        "maxExternalActions": { "type": "integer", "minimum": 0, "maximum": 100 }
      }
    },
    "budget": {
      "type": "object",
      "additionalProperties": false,
      "required": ["currency", "maximumMinorUnits", "spentMinorUnits"],
      "properties": {
        "currency": { "const": "USD" },
        "maximumMinorUnits": { "type": "integer", "minimum": 0 },
        "spentMinorUnits": { "type": "integer", "minimum": 0 }
      }
    },
    "action": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "targetClass", "summary", "external", "attempted"],
      "properties": {
        "type": { "$ref": "#/$defs/safeAction" },
        "targetClass": { "enum": ["synthetic_fixture", "sandbox_artifact", "approved_external_target"] },
        "summary": { "type": "string", "minLength": 12, "maxLength": 240 },
        "external": { "type": "boolean" },
        "attempted": { "type": "boolean" }
      }
    },
    "status": { "enum": ["not_run", "completed", "blocked", "skipped", "failed"] },
    "evidenceReferences": {
      "type": "array",
      "minItems": 1,
      "maxItems": 20,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["kind", "ref"],
        "properties": {
          "kind": { "enum": ["fixture", "artifact", "policy_log", "external_receipt"] },
          "ref": { "type": "string", "pattern": "^(fixture|artifact|policy|receipt):[a-z0-9][a-z0-9:_-]{2,119}$" },
          "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
        },
        "allOf": [
          {
            "if": { "properties": { "kind": { "const": "fixture" } }, "required": ["kind"] },
            "then": { "properties": { "ref": { "type": "string", "pattern": "^fixture:" } } }
          },
          {
            "if": { "properties": { "kind": { "const": "artifact" } }, "required": ["kind"] },
            "then": { "properties": { "ref": { "type": "string", "pattern": "^artifact:" } } }
          },
          {
            "if": { "properties": { "kind": { "const": "policy_log" } }, "required": ["kind"] },
            "then": { "properties": { "ref": { "type": "string", "pattern": "^policy:" } } }
          },
          {
            "if": { "properties": { "kind": { "const": "external_receipt" } }, "required": ["kind"] },
            "then": { "properties": { "ref": { "type": "string", "pattern": "^receipt:" } } }
          }
        ]
      }
    },
    "reversibility": {
      "type": "object",
      "additionalProperties": false,
      "required": ["reversible", "method"],
      "properties": {
        "reversible": { "type": "boolean" },
        "method": { "type": "string", "minLength": 8, "maxLength": 240 }
      }
    },
    "elapsedTimeMs": { "type": "integer", "minimum": 0 },
    "cost": {
      "type": "object",
      "additionalProperties": false,
      "required": ["currency", "modelMinorUnits", "toolMinorUnits", "externalMinorUnits"],
      "properties": {
        "currency": { "const": "USD" },
        "modelMinorUnits": { "type": "integer", "minimum": 0 },
        "toolMinorUnits": { "type": "integer", "minimum": 0 },
        "externalMinorUnits": { "type": "integer", "minimum": 0 }
      }
    },
    "humanInterventions": { "type": "integer", "minimum": 0 },
    "redactions": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["category", "count"],
        "properties": {
          "category": { "enum": ["fixture_only", "direct_identifier_removed", "secret_removed"] },
          "count": { "type": "integer", "minimum": 0 }
        }
      }
    },
    "policy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["result", "violations"],
      "properties": {
        "result": { "enum": ["not_evaluated", "allowed", "blocked"] },
        "violations": { "type": "array", "uniqueItems": true, "items": { "type": "string", "pattern": "^[a-z][a-z0-9_]{2,63}$" } }
      }
    },
    "terminalAt": { "type": "string", "pattern": "^20[0-9]{2}-(0[1-9]|1[0-2])-([0-2][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\\.[0-9]{3}Z$" }
  },
  "allOf": [
    {
      "if": { "properties": { "status": { "const": "not_run" } }, "required": ["status"] },
      "then": {
        "properties": {
          "action": { "type": "object", "properties": { "attempted": { "const": false }, "external": { "const": false } } },
          "budget": { "type": "object", "properties": { "spentMinorUnits": { "const": 0 } } },
          "elapsedTimeMs": { "const": 0 },
          "policy": { "type": "object", "properties": { "result": { "const": "not_evaluated" }, "violations": { "type": "array", "maxItems": 0 } } }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "completed" } }, "required": ["status"] },
      "then": { "properties": { "action": { "type": "object", "properties": { "attempted": { "const": true } } } } }
    },
    {
      "if": {
        "properties": { "action": { "type": "object", "properties": { "external": { "const": true } }, "required": ["external"] } },
        "required": ["action"]
      },
      "then": {
        "properties": {
          "action": {
            "type": "object",
            "properties": {
              "targetClass": { "const": "approved_external_target" },
              "attempted": { "const": true }
            }
          },
          "authorizedScope": { "type": "object", "properties": { "maxExternalActions": { "type": "integer", "minimum": 1 } } }
        }
      }
    },
    {
      "if": {
        "properties": { "action": { "type": "object", "properties": { "external": { "const": false } }, "required": ["external"] } },
        "required": ["action"]
      },
      "then": {
        "properties": {
          "action": { "type": "object", "properties": { "targetClass": { "enum": ["synthetic_fixture", "sandbox_artifact"] } } },
          "cost": { "type": "object", "properties": { "externalMinorUnits": { "const": 0 } } }
        }
      }
    },
    {
      "if": {
        "properties": { "policy": { "type": "object", "properties": { "result": { "enum": ["not_evaluated", "allowed"] } }, "required": ["result"] } },
        "required": ["policy"]
      },
      "then": { "properties": { "policy": { "type": "object", "properties": { "violations": { "type": "array", "maxItems": 0 } } } } }
    },
    {
      "if": {
        "properties": { "policy": { "type": "object", "properties": { "result": { "const": "blocked" } }, "required": ["result"] } },
        "required": ["policy"]
      },
      "then": { "properties": { "policy": { "type": "object", "properties": { "violations": { "type": "array", "minItems": 1 } } } } }
    }
  ],
  "$defs": {
    "safeId": { "type": "string", "pattern": "^[a-z][a-z0-9-]{2,63}$" },
    "safeAction": { "type": "string", "pattern": "^[a-z][a-z0-9_]{2,63}$" }
  }
}
