{
	"$schema": "http://json-schema.org/draft-07/schema#",
	"title": "Amber Harness Run (v1)",
	"description": "ADR-0101 (F065 H0): one auditable, bounded execution unit bound to an admitted Harness Contract snapshot. Task != Run: one task may produce many runs. Terminal states are preserved records; runs are never edited after completion. Distinct from the Session, which spans framing/approval/review.",
	"type": "object",
	"required": ["apiVersion", "kind", "id", "subject", "harness", "state", "stateHistory"],
	"additionalProperties": false,
	"properties": {
		"apiVersion": { "const": "amber.dev/v1" },
		"kind": { "const": "HarnessRun" },
		"id": {
			"type": "string",
			"minLength": 1,
			"maxLength": 128,
			"pattern": "^[A-Za-z0-9][A-Za-z0-9._-]*$"
		},
		"subject": {
			"type": "object",
			"required": ["agent"],
			"additionalProperties": false,
			"properties": {
				"agent": { "type": "string", "minLength": 1, "maxLength": 128 },
				"session": { "type": "string", "maxLength": 128 },
				"task": { "type": "string", "maxLength": 128 }
			}
		},
		"harness": {
			"type": "object",
			"required": ["contract", "contractSnapshotHash"],
			"additionalProperties": false,
			"properties": {
				"contract": { "type": "string", "minLength": 1, "maxLength": 128 },
				"contractSnapshotHash": {
					"type": "string",
					"pattern": "^sha256:[0-9a-f]{64}$",
					"description": "Snapshot Hash of the admitted contract this run is bound to (ADR-0100)."
				},
				"policy": { "type": "string", "maxLength": 256 }
			}
		},
		"execution": {
			"type": "object",
			"additionalProperties": false,
			"properties": {
				"ref": { "type": "string", "maxLength": 256 },
				"contract": { "type": "string", "minLength": 1, "maxLength": 128 },
				"contractSnapshotHash": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
				"effective": { "$ref": "#/$defs/executionBoundary" },
				"observed": {
					"type": "object",
					"required": ["entries"],
					"additionalProperties": false,
					"properties": {
						"entries": {
							"type": "array",
							"items": { "$ref": "#/$defs/observedEntry" },
							"maxItems": 256
						},
						"collectedAt": { "type": "string", "format": "date-time" }
					}
				},
				"comparison": {
					"type": "object",
					"required": ["verdict", "findings"],
					"additionalProperties": false,
					"properties": {
						"verdict": { "enum": ["ok", "violation", "unevaluated"] },
						"findings": {
							"type": "array",
							"items": {
								"type": "object",
								"required": ["severity", "kind", "detail"],
								"additionalProperties": false,
								"properties": {
									"severity": { "enum": ["violation", "info"] },
									"kind": { "type": "string", "minLength": 1, "maxLength": 64 },
									"detail": { "type": "string", "maxLength": 1000 },
									"source": { "type": "string", "maxLength": 256 }
								}
							},
							"maxItems": 256
						},
						"evaluatedAt": { "type": "string", "format": "date-time" }
					}
				},
				"releasedAt": {
					"type": "string",
					"format": "date-time",
					"description": "F070 H2b: when the prepared workspace was released as a recorded step (additive, optional; the ExecutionRecord stays the full-fidelity boundary record)."
				}
			}
		},
		"events": {
			"type": "object",
			"additionalProperties": false,
			"properties": {
				"stream": { "type": "string", "maxLength": 256 }
			}
		},
		"evidence": {
			"type": "object",
			"additionalProperties": false,
			"properties": {
				"ref": { "type": "string", "maxLength": 256 }
			}
		},
		"state": {
			"enum": [
				"created",
				"admitted",
				"running",
				"paused",
				"blocked",
				"settling",
				"failed",
				"completed",
				"cancelled",
				"expired"
			]
		},
		"stateHistory": {
			"type": "array",
			"minItems": 1,
			"items": {
				"type": "object",
				"required": ["from", "to", "at"],
				"additionalProperties": false,
				"properties": {
					"from": { "type": ["string", "null"] },
					"to": { "type": "string" },
					"at": { "type": "string", "format": "date-time" },
					"reason": { "type": "string", "maxLength": 2000 }
				}
			}
		},
		"outcome": {
			"type": "object",
			"additionalProperties": false,
			"properties": {
				"startedAt": { "type": "string", "format": "date-time" },
				"finishedAt": { "type": "string", "format": "date-time" }
			}
		},
		"tools": {
			"description": "F067: additive optional — the tool-registry snapshot this run operated under (Harness v2 §30 acceptance 5). Absent when no tool was admitted at run creation, so pre-H1 records stay schema-valid. A changed tool set changes the snapshot hash, making tool drift per-run detectable.",
			"type": "object",
			"required": ["snapshotHash", "ids"],
			"additionalProperties": false,
			"properties": {
				"snapshotHash": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
				"ids": { "type": "array", "items": { "type": "string", "minLength": 1 }, "maxItems": 64 }
			}
		},
		"attempts": {
			"description": "F072 H4: additive optional — a summary of this run's first-class attempt records (the full-fidelity records live under .amber/harness/attempts/<runId>/). Absent when no attempt has been recorded, so pre-H4 records stay schema-valid.",
			"type": "object",
			"required": ["count", "lastAttemptId", "lastAttemptState"],
			"additionalProperties": false,
			"properties": {
				"count": { "type": "integer", "minimum": 1, "maximum": 1024 },
				"lastAttemptId": { "type": "string", "minLength": 1, "maxLength": 128 },
				"lastAttemptState": { "enum": ["running", "completed", "failed", "refused"] }
			}
		},
		"checkpoints": {
			"description": "F072 H4: additive optional — a summary of this run's checkpoint records (the full-fidelity records live under .amber/harness/checkpoints/<runId>/). Absent when no checkpoint has been captured, so pre-H4 records stay schema-valid.",
			"type": "object",
			"required": ["count", "lastCheckpointId", "lastCheckpointAt"],
			"additionalProperties": false,
			"properties": {
				"count": { "type": "integer", "minimum": 1, "maximum": 1024 },
				"lastCheckpointId": { "type": "string", "minLength": 1, "maxLength": 128 },
				"lastCheckpointAt": { "type": "string", "format": "date-time" }
			}
		},
		"validation": {
			"description": "F073 H5: additive optional — the latest ValidationReceipt's status for this run (full-fidelity receipts live under .amber/harness/validations/<runId>/). Absent when the run was never validated, so pre-H5 records stay schema-valid. Validation is a separate fact from the run's own state machine (the receipt never transitions a run).",
			"type": "object",
			"required": ["status", "receiptId", "receiptedAt"],
			"additionalProperties": false,
			"properties": {
				"status": { "enum": ["accepted", "rejected"] },
				"receiptId": { "type": "string", "minLength": 1, "maxLength": 128 },
				"receiptedAt": { "type": "string", "format": "date-time" }
			}
		},
		"replay": {
			"description": "F073 H5: additive optional — the latest replay verdict for this run (full-fidelity results live under .amber/harness/replays/<runId>/). Absent when the run was never replayed, so pre-H5 records stay schema-valid.",
			"type": "object",
			"required": ["verdict", "replayId", "replayedAt"],
			"additionalProperties": false,
			"properties": {
				"verdict": { "enum": ["equivalent", "drift"] },
				"replayId": { "type": "string", "minLength": 1, "maxLength": 128 },
				"replayedAt": { "type": "string", "format": "date-time" }
			}
		},
		"admission": {
			"description": "F066 (ADR-0100 decision 3): the AdmissionReceipt. Additive section — records created before F066 stay schema-valid without it. The receipt is only ever written complete: each of the six checks names the existing governed artifact it witnessed; a missing or refused check refuses the whole admission, so a stored receipt is always all-pass.",
			"type": "object",
			"required": ["receiptedAt", "checks"],
			"additionalProperties": false,
			"properties": {
				"receiptedAt": { "type": "string", "format": "date-time" },
				"checks": {
					"type": "object",
					"required": ["identity", "contract", "policy", "context", "execution", "approval"],
					"additionalProperties": false,
					"properties": {
						"identity": { "$ref": "#/$defs/admissionCheck" },
						"contract": { "$ref": "#/$defs/admissionCheck" },
						"policy": { "$ref": "#/$defs/admissionCheck" },
						"context": { "$ref": "#/$defs/admissionCheck" },
						"execution": { "$ref": "#/$defs/admissionCheck" },
						"approval": { "$ref": "#/$defs/admissionCheck" }
					}
				}
			}
		}
	},
	"$defs": {
		"prefixList": {
			"type": "array",
			"items": { "type": "string", "minLength": 1, "maxLength": 256 },
			"maxItems": 64
		},
		"executionBoundary": {
			"type": "object",
			"required": ["workspace"],
			"additionalProperties": false,
			"properties": {
				"workspace": {
					"type": "object",
					"required": ["type", "path"],
					"additionalProperties": false,
					"properties": {
						"type": { "enum": ["git-worktree", "local"] },
						"path": { "type": "string", "minLength": 1, "maxLength": 512 },
						"base": { "type": "string", "maxLength": 128 },
						"branch": { "type": "string", "maxLength": 128 }
					}
				},
				"filesystem": {
					"type": "object",
					"additionalProperties": false,
					"properties": {
						"read": { "$ref": "#/$defs/prefixList" },
						"write": { "$ref": "#/$defs/prefixList" },
						"deny": { "$ref": "#/$defs/prefixList" }
					}
				},
				"network": {
					"type": "object",
					"additionalProperties": false,
					"properties": { "mode": { "enum": ["deny", "allow"] } }
				},
				"resources": {
					"type": "object",
					"additionalProperties": false,
					"properties": {
						"timeoutMinutes": { "type": "integer", "minimum": 1, "maximum": 1440 },
						"maxChildren": { "type": "integer", "minimum": 1, "maximum": 256 }
					}
				},
				"mutation": {
					"type": "object",
					"additionalProperties": false,
					"properties": { "mode": { "enum": ["isolated", "restricted"] } }
				},
				"enforcement": {
					"enum": ["unsupported", "observed-only", "enforced"],
					"description": "R-ET-1 (ADR-0104): the provider ENFORCEMENT level the effective boundary actually delivers. Derived from the declared workspace.type (git-worktree/local -> observed-only); effective can only DOWNGRADE from declared, and an observation above this level yields verdict `unevaluated`, never `ok`."
				}
			}
		},
		"observedEntry": {
			"type": "object",
			"required": ["kind"],
			"additionalProperties": false,
			"properties": {
				"kind": { "type": "string", "minLength": 1, "maxLength": 32 },
				"paths": {
					"type": "array",
					"items": { "type": "string", "minLength": 1, "maxLength": 256 },
					"maxItems": 64
				},
				"timeoutMinutes": { "type": "integer", "minimum": 1, "maximum": 1440 },
				"source": { "type": "string", "minLength": 1, "maxLength": 256 },
				"attemptId": {
					"type": "string",
					"minLength": 1,
					"maxLength": 128,
					"description": "F070 H2b: the governed attempt this observation is bound to (additive, optional)."
				},
				"at": {
					"type": "string",
					"format": "date-time",
					"description": "F070 H2b: when the attempt's mutation observation was collected (additive, optional)."
				},
				"observedVia": {
					"type": "string",
					"enum": ["git-status", "bounded-root-listing"],
					"description": "F070 H2b: the deterministic observation mechanism (additive, optional)."
				},
				"enforcement": {
					"enum": ["unsupported", "observed-only", "enforced"],
					"description": "R-ET-1 (ADR-0104): the enforcement level this observation claims. A claim above the boundary's effective level cannot be substantiated, so the fold records it and the verdict is `unevaluated`, never `ok` (additive, optional)."
				}
			}
		},
		"admissionCheck": {
			"type": "object",
			"required": ["result", "pointer"],
			"additionalProperties": false,
			"properties": {
				"result": { "const": "pass" },
				"pointer": {
					"type": "string",
					"minLength": 1,
					"maxLength": 256,
					"description": "Pointer to the EXISTING governed artifact this check witnessed (path, id, or ledger entry). Harness re-derives nothing and owns no new gate."
				}
			}
		}
	}
}
