{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/markjaquith/agency/schemas/agency-execution-v1.schema.json",
  "title": "Agency execution contract v1",
  "oneOf": [
    { "$ref": "#/$defs/validationEvidence" },
    { "$ref": "#/$defs/executionContract" }
  ],
  "$defs": {
    "recalledContext": {
      "type": "object",
      "required": ["preferredSlug", "authoritativeSources"],
      "properties": {
        "repo": { "type": "string", "minLength": 1 },
        "base": { "type": "string", "minLength": 1 },
        "preferredSlug": { "type": "string", "minLength": 1 },
        "authoritativeSources": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      },
      "additionalProperties": false
    },
    "validationEvidence": {
      "type": "object",
      "required": [
        "version",
        "workbaseRoot",
        "target",
        "documentPath",
        "documentRevision",
        "workbaseRevision",
        "configRevision",
        "repositoryMappingRevision",
        "valid",
        "recalledContext",
        "digest"
      ],
      "properties": {
        "version": { "const": 1 },
        "workbaseRoot": { "type": "string" },
        "target": { "type": "string" },
        "documentPath": { "type": "string" },
        "documentRevision": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "workbaseRevision": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "configRevision": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "repositoryMappingRevision": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "valid": { "const": true },
        "recalledContext": { "$ref": "#/$defs/recalledContext" },
        "digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
      },
      "additionalProperties": false
    },
    "command": {
      "type": "object",
      "required": ["cwd", "argv"],
      "properties": {
        "cwd": { "type": "string" },
        "argv": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string" }
        }
      },
      "additionalProperties": false
    },
    "executionContract": {
      "type": "object",
      "required": [
        "version",
        "capability",
        "mode",
        "executionIdentity",
        "workbaseRoot",
        "sourceLocations",
        "workspace",
        "commands"
      ],
      "properties": {
        "version": { "const": 1 },
        "capability": { "const": "agency-execution-v1" },
        "mode": { "enum": ["preview", "applied"] },
        "executionIdentity": {
          "type": "object",
          "required": ["key", "target", "documentRevision"],
          "properties": {
            "key": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
            "target": { "type": "string" },
            "documentRevision": {
              "type": "string",
              "pattern": "^[a-f0-9]{64}$"
            }
          },
          "additionalProperties": false
        },
        "workbaseRoot": { "type": "string" },
        "sourceLocations": {
          "type": "array",
          "items": { "type": "string" }
        },
        "workspace": {
          "type": "object",
          "required": [
            "state",
            "executionDirectory",
            "taskDocument",
            "phaseDocument",
            "checkoutPath"
          ],
          "properties": {
            "state": { "enum": ["planned", "materialized"] },
            "executionDirectory": { "type": "string" },
            "taskDocument": { "type": "string" },
            "phaseDocument": { "type": ["string", "null"] },
            "checkoutPath": { "type": ["string", "null"] }
          },
          "additionalProperties": false
        },
        "plannedActions": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["kind", "target", "checkoutPath"],
            "properties": {
              "kind": { "const": "workspace-materialization" },
              "target": { "type": "string" },
              "checkoutPath": { "type": ["string", "null"] }
            },
            "additionalProperties": false
          }
        },
        "commands": {
          "type": "object",
          "required": ["work", "context"],
          "properties": {
            "work": { "$ref": "#/$defs/command" },
            "context": { "$ref": "#/$defs/command" }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    }
  }
}
