{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://dirtydishes.dev/schemas/dirtyloops/program-decision.schema.json",
  "title": "dirtyloops Program decision",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "kind",
    "decisionCode",
    "action",
    "certificationFailures",
    "programRevision",
    "programState",
    "operatorDecision",
    "reason",
    "wakeConditions",
    "graph"
  ],
  "properties": {
    "schemaVersion": { "const": 1 },
    "kind": { "enum": ["wait", "effects"] },
    "decisionCode": {
      "enum": [
        "graph_snapshot",
        "recertification_required",
        "mutable_phase",
        "admission_complete",
        "admission_blocked",
        "budget_exhausted"
      ]
    },
    "action": { "$ref": "#/$defs/action" },
    "certificationFailures": {
      "type": "array",
      "items": {
        "enum": [
          "repository_identity_mismatch",
          "integration_ref_mismatch",
          "dirtyloops_generation_mismatch",
          "dirtyloops_adapter_mismatch",
          "source_parity_stale"
        ]
      },
      "uniqueItems": true
    },
    "programRevision": { "type": "integer", "minimum": 0 },
    "programState": {
      "enum": [
        "draft",
        "certifying",
        "ready",
        "running",
        "pausing",
        "paused",
        "attention_required",
        "stopping",
        "stopped",
        "completed"
      ]
    },
    "operatorDecision": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "code", "message"],
      "properties": {
        "status": { "enum": ["accepted", "rejected"] },
        "code": { "enum": ["accepted", "invalid_state"] },
        "message": { "type": "string", "minLength": 1 }
      }
    },
    "reason": { "type": "string", "minLength": 1 },
    "wakeConditions": {
      "type": "array",
      "items": { "type": "string", "minLength": 1 },
      "uniqueItems": true
    },
    "graph": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "programId",
        "title",
        "outcome",
        "beadsRevision",
        "graphDigest",
        "budgets",
        "phases",
        "sourceIdentity",
        "repository",
        "receipts",
        "observedAt"
      ],
      "properties": {
        "programId": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "outcome": { "type": "string", "minLength": 1 },
        "beadsRevision": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
        "graphDigest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
        "observedAt": { "type": "string", "format": "date-time" },
        "receipts": { "type": "array" },
        "budgets": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "activeThreads",
            "nativeHelpers",
            "helperDepth",
            "providerTurns",
            "tokens",
            "costMilliUsd",
            "wallClockMinutes",
            "actions",
            "concurrentWorktrees",
            "cpuMillis",
            "memoryMiB",
            "diskMiB",
            "repairs",
            "retries"
          ],
          "properties": {
            "activeThreads": { "$ref": "#/$defs/budget" },
            "nativeHelpers": { "$ref": "#/$defs/budget" },
            "helperDepth": { "$ref": "#/$defs/budget" },
            "providerTurns": { "$ref": "#/$defs/budget" },
            "tokens": { "$ref": "#/$defs/budget" },
            "costMilliUsd": { "$ref": "#/$defs/budget" },
            "wallClockMinutes": { "$ref": "#/$defs/budget" },
            "actions": { "$ref": "#/$defs/budget" },
            "concurrentWorktrees": { "$ref": "#/$defs/budget" },
            "cpuMillis": { "$ref": "#/$defs/budget" },
            "memoryMiB": { "$ref": "#/$defs/budget" },
            "diskMiB": { "$ref": "#/$defs/budget" },
            "repairs": { "$ref": "#/$defs/budget" },
            "retries": { "$ref": "#/$defs/budget" }
          }
        },
        "phases": {
          "type": "array",
          "items": { "$ref": "#/$defs/phase" }
        },
        "sourceIdentity": { "$ref": "#/$defs/sourceIdentity" },
        "repository": { "$ref": "#/$defs/repository" }
      }
    }
  },
  "$defs": {
    "action": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind"],
          "properties": { "kind": { "const": "wait" } }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "phaseId"],
          "properties": {
            "kind": { "const": "launch_phase_coordinator" },
            "phaseId": { "type": "string", "minLength": 1 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "phaseId", "ownerThreadId", "permit"],
          "properties": {
            "kind": { "const": "bind_prepared_worktree" },
            "phaseId": { "type": "string", "minLength": 1 },
            "ownerThreadId": { "type": "string", "minLength": 1 },
            "permit": { "$ref": "#/$defs/preparedWorktreePermit" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "integrationAdmissionRequestId",
            "phaseId",
            "programCoordinatorThreadId",
            "integrationCoordinatorThreadId",
            "sourceThreadId",
            "phaseCallbackId",
            "phaseCallbackNonce",
            "candidateCommit",
            "expectedParent",
            "integrationRef",
            "leaseId",
            "leaseEpoch",
            "expiresAt",
            "integrationAdmissionNonce"
          ],
          "properties": {
            "kind": { "const": "deliver_integration_admission_request" },
            "integrationAdmissionRequestId": { "type": "string", "minLength": 1 },
            "phaseId": { "type": "string", "minLength": 1 },
            "programCoordinatorThreadId": { "type": "string", "minLength": 1 },
            "integrationCoordinatorThreadId": { "type": "string", "minLength": 1 },
            "sourceThreadId": { "type": "string", "minLength": 1 },
            "phaseCallbackId": { "type": "string", "minLength": 1 },
            "phaseCallbackNonce": { "type": "string", "minLength": 1 },
            "candidateCommit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
            "expectedParent": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
            "integrationRef": { "type": "string", "pattern": "^refs/heads/" },
            "leaseId": { "type": "string", "minLength": 1 },
            "leaseEpoch": { "type": "integer", "minimum": 1 },
            "expiresAt": { "type": "string", "format": "date-time" },
            "integrationAdmissionNonce": { "type": "string", "minLength": 1 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "admissionId",
            "phaseId",
            "integrationCoordinatorThreadId",
            "integrationRef",
            "expectedParent",
            "candidateCommit",
            "preparedCommit",
            "refUpdated",
            "beadsTaskId",
            "beadsClosed",
            "finding"
          ],
          "properties": {
            "kind": { "const": "admission_blocked" },
            "admissionId": { "type": "string", "minLength": 1 },
            "phaseId": { "type": "string", "minLength": 1 },
            "integrationCoordinatorThreadId": { "type": "string", "minLength": 1 },
            "integrationRef": { "type": "string", "pattern": "^refs/heads/" },
            "expectedParent": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
            "candidateCommit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
            "preparedCommit": {
              "type": ["string", "null"],
              "pattern": "^[a-f0-9]{40}$"
            },
            "refUpdated": { "type": "boolean" },
            "beadsTaskId": { "type": "string", "minLength": 1 },
            "beadsClosed": { "type": "boolean" },
            "finding": {
              "type": "object",
              "additionalProperties": false,
              "required": ["id", "message"],
              "properties": {
                "id": { "type": "string", "minLength": 1 },
                "message": { "type": "string", "minLength": 1 },
                "evidence": { "type": "object" }
              }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "phaseCallbackId",
            "phaseId",
            "phaseCoordinatorThreadId",
            "programCoordinatorThreadId",
            "sourceThreadId",
            "nonce",
            "ownerResultIds",
            "candidateCommit",
            "disposition",
            "evidence"
          ],
          "properties": {
            "kind": {
              "enum": ["deliver_phase_callback", "acknowledge_phase_callback"]
            },
            "phaseCallbackId": { "type": "string", "minLength": 1 },
            "phaseId": { "type": "string", "minLength": 1 },
            "phaseCoordinatorThreadId": { "type": "string", "minLength": 1 },
            "programCoordinatorThreadId": { "type": "string", "minLength": 1 },
            "sourceThreadId": { "type": "string", "minLength": 1 },
            "nonce": { "type": "string", "minLength": 1 },
            "ownerResultIds": {
              "type": "array",
              "items": { "type": "string", "minLength": 1 },
              "minItems": 1,
              "uniqueItems": true
            },
            "candidateCommit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
            "disposition": { "enum": ["approved", "failed", "cancelled"] },
            "evidence": {
              "type": "array",
              "items": { "$ref": "#/$defs/evidence" }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "admissionId",
            "phaseId",
            "integrationCoordinatorThreadId",
            "integrationRef",
            "expectedParent",
            "candidateCommit",
            "preparedCommit",
            "refUpdated",
            "beadsTaskId",
            "beadsClosed",
            "evidence"
          ],
          "properties": {
            "kind": { "const": "admission_complete" },
            "admissionId": { "type": "string", "minLength": 1 },
            "phaseId": { "type": "string", "minLength": 1 },
            "integrationCoordinatorThreadId": { "type": "string", "minLength": 1 },
            "integrationRef": { "type": "string", "pattern": "^refs/heads/" },
            "expectedParent": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
            "candidateCommit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
            "preparedCommit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
            "refUpdated": { "const": true },
            "beadsTaskId": { "type": "string", "minLength": 1 },
            "beadsClosed": { "const": true },
            "evidence": {
              "type": "array",
              "items": { "$ref": "#/$defs/evidence" }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "phaseId",
            "attemptId",
            "reviewOwnerThreadId",
            "candidateId",
            "reviewId",
            "candidateCommit",
            "reviewKind",
            "prompt"
          ],
          "properties": {
            "kind": { "const": "launch_review_owner" },
            "phaseId": { "type": "string", "minLength": 1 },
            "attemptId": { "type": "string", "minLength": 1 },
            "reviewOwnerThreadId": { "type": "string", "minLength": 1 },
            "candidateId": { "type": "string", "minLength": 1 },
            "reviewId": { "type": "string", "minLength": 1 },
            "candidateCommit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
            "reviewKind": { "enum": ["broad", "focused"] },
            "prompt": { "type": "string", "minLength": 1 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "phaseId",
            "ownerThreadId",
            "attemptId",
            "permit",
            "prompt",
            "teamPolicy"
          ],
          "properties": {
            "kind": { "enum": ["launch_owner_attempt", "cancel_owner_attempt"] },
            "phaseId": { "type": "string", "minLength": 1 },
            "ownerThreadId": { "type": "string", "minLength": 1 },
            "attemptId": { "type": "string", "minLength": 1 },
            "permit": { "$ref": "#/$defs/preparedWorktreePermit" },
            "prompt": { "type": "string", "minLength": 1 },
            "teamPolicy": { "$ref": "#/$defs/teamPolicy" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "phaseId",
            "ownerResult",
            "leaseId",
            "leaseEpoch",
            "expiresAt"
          ],
          "properties": {
            "kind": { "const": "acknowledge_owner_result" },
            "phaseId": { "type": "string", "minLength": 1 },
            "ownerResult": { "$ref": "#/$defs/ownerResult" },
            "leaseId": { "type": "string", "minLength": 1 },
            "leaseEpoch": { "type": "integer", "minimum": 1 },
            "expiresAt": { "type": "string", "format": "date-time" }
          }
        }
      ]
    },
    "ownerResult": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "ownerResultId",
        "programId",
        "phaseId",
        "phaseCoordinatorThreadId",
        "ownerThreadId",
        "attemptId",
        "ownerKind",
        "terminalKind",
        "resultDigest",
        "evidence"
      ],
      "properties": {
        "ownerResultId": { "type": "string", "minLength": 1 },
        "programId": { "type": "string", "minLength": 1 },
        "phaseId": { "type": "string", "minLength": 1 },
        "phaseCoordinatorThreadId": { "type": "string", "minLength": 1 },
        "ownerThreadId": { "type": "string", "minLength": 1 },
        "attemptId": { "type": "string", "minLength": 1 },
        "ownerKind": { "enum": ["implementation", "review"] },
        "terminalKind": {
          "enum": [
            "succeeded",
            "failed",
            "cancelled",
            "t3_restart_interrupted",
            "launch_rejected",
            "identity_mismatch"
          ]
        },
        "resultDigest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
        "evidence": {
          "type": "array",
          "items": { "$ref": "#/$defs/evidence" }
        },
        "reviewDecision": { "$ref": "#/$defs/reviewDecision" }
      }
    },
    "teamPolicy": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["mode"],
          "properties": { "mode": { "const": "solo" } }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["mode", "maxHelpers", "maxConcurrent", "maxDepth"],
          "properties": {
            "mode": {
              "enum": ["delegated", "native_collaborative", "cross_provider"]
            },
            "maxHelpers": { "type": "integer", "minimum": 1 },
            "maxConcurrent": { "type": "integer", "minimum": 1 },
            "maxDepth": { "const": 1 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "mode",
            "maxHelpers",
            "maxConcurrent",
            "maxDepth",
            "maxRounds",
            "criteria"
          ],
          "properties": {
            "mode": { "const": "layered_hybrid" },
            "maxHelpers": { "type": "integer", "minimum": 1 },
            "maxConcurrent": { "type": "integer", "minimum": 1 },
            "maxDepth": { "const": 1 },
            "maxRounds": { "type": "integer", "minimum": 1 },
            "criteria": {
              "type": "array",
              "minItems": 1,
              "items": { "type": "string", "minLength": 1 }
            }
          }
        }
      ]
    },
    "reviewDecision": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "candidateCommit",
        "reviewId",
        "reviewKind",
        "verdict",
        "findings",
        "ciState",
        "evidence"
      ],
      "properties": {
        "candidateCommit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
        "reviewId": { "type": "string", "minLength": 1 },
        "reviewKind": { "enum": ["broad", "focused"] },
        "verdict": { "enum": ["approved", "rejected"] },
        "findings": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["id", "message"],
            "properties": {
              "id": { "type": "string", "minLength": 1 },
              "message": { "type": "string", "minLength": 1 }
            }
          }
        },
        "ciState": {
          "enum": [
            "ci-green",
            "ci-repaired-and-green",
            "ci-unavailable-with-evidence",
            "ci-blocked-with-cause"
          ]
        },
        "evidence": {
          "type": "array",
          "items": { "$ref": "#/$defs/evidence" }
        }
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "id"],
      "properties": {
        "kind": {
          "enum": ["thread", "commit", "check", "receipt", "log", "diff", "task"]
        },
        "id": { "type": "string", "minLength": 1 },
        "label": { "type": "string", "minLength": 1 },
        "href": { "type": "string", "minLength": 1 },
        "digest": { "type": "string", "minLength": 1 }
      }
    },
    "preparedWorktreePermit": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "programId",
        "phaseId",
        "phaseCoordinatorThreadId",
        "leaseId",
        "leaseEpoch",
        "repositoryIdentity",
        "repositoryRoot",
        "gitCommonDir",
        "realPath",
        "expectedIntegrationHead",
        "integrationRef",
        "budgetIdentity",
        "symbolicBranch",
        "startingCommit",
        "clean",
        "declaredPaths",
        "expiresAt"
      ],
      "properties": {
        "programId": { "type": "string", "minLength": 1 },
        "phaseId": { "type": "string", "minLength": 1 },
        "phaseCoordinatorThreadId": { "type": "string", "minLength": 1 },
        "leaseId": { "type": "string", "minLength": 1 },
        "leaseEpoch": { "type": "integer", "minimum": 1 },
        "repositoryIdentity": { "type": "string", "minLength": 1 },
        "repositoryRoot": { "type": "string", "minLength": 1 },
        "gitCommonDir": { "type": "string", "minLength": 1 },
        "realPath": { "type": "string", "minLength": 1 },
        "expectedIntegrationHead": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
        "integrationRef": { "type": "string", "pattern": "^refs/heads/" },
        "budgetIdentity": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
        "symbolicBranch": { "type": "string", "minLength": 1 },
        "startingCommit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
        "clean": { "const": true },
        "declaredPaths": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        },
        "expiresAt": { "type": "string", "format": "date-time" }
      }
    },
    "budget": {
      "type": "object",
      "additionalProperties": false,
      "required": ["used", "limit"],
      "properties": {
        "used": { "type": "integer", "minimum": 0 },
        "limit": { "type": "integer", "minimum": 1 }
      }
    },
    "phase": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "phaseId",
        "title",
        "beadsStatus",
        "state",
        "dependencyIds",
        "blockedBy",
        "blockerPath",
        "policy",
        "budgets"
      ],
      "properties": {
        "phaseId": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "beadsStatus": { "type": "string", "minLength": 1 },
        "state": { "enum": ["blocked", "ready", "integrated"] },
        "dependencyIds": { "type": "array", "items": { "type": "string" } },
        "blockedBy": { "type": "array", "items": { "type": "string" } },
        "blockerPath": { "type": "array", "items": { "type": "string" } },
        "policy": { "type": "object" },
        "budgets": {
          "type": "object",
          "additionalProperties": false,
          "required": ["attempts", "wallClockMinutes", "tokens"],
          "properties": {
            "attempts": { "$ref": "#/$defs/budget" },
            "wallClockMinutes": { "$ref": "#/$defs/budget" },
            "tokens": { "$ref": "#/$defs/budget" }
          }
        }
      }
    },
    "sourceIdentity": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "sourceCommit",
        "sourceDigest",
        "installedDigest",
        "schemaGeneration",
        "adapterDigest",
        "generationId",
        "parity"
      ],
      "properties": {
        "sourceCommit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
        "sourceDigest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
        "installedDigest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
        "schemaGeneration": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
        "adapterDigest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
        "generationId": { "type": "string", "pattern": "^dirtyloops:[a-f0-9]{64}$" },
        "parity": { "enum": ["current", "stale"] }
      }
    },
    "repository": {
      "type": "object",
      "additionalProperties": false,
      "required": ["repositoryId", "head", "gitCommonDir", "symbolicRef", "integrationRef"],
      "properties": {
        "repositoryId": { "type": "string", "minLength": 1 },
        "head": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
        "gitCommonDir": { "type": "string", "minLength": 1 },
        "symbolicRef": { "type": "string", "pattern": "^refs/heads/" },
        "integrationRef": { "type": "string", "pattern": "^refs/heads/" }
      }
    }
  }
}
