{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://local/harness/journal-events.schema.json",
  "title": "Harness v2 journal events",
  "description": "Per-kind payload contracts for coordinator events.ndjson records (pre-envelope).",
  "$defs": {
    "qualityIdentity": {
      "type": "object",
      "properties": {
        "task": { "type": "string", "minLength": 1 },
        "taskId": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 },
        "binding": { "type": "string", "minLength": 1 },
        "runId": { "type": "string", "minLength": 1 },
        "failureClass": { "type": "string", "minLength": 1 },
        "failureFingerprint": { "type": "string", "minLength": 1 },
        "baseHead": { "type": "string", "minLength": 1 },
        "taskHead": { "type": "string", "minLength": 1 },
        "account": { "type": "string", "minLength": 1 },
        "seat": { "type": "string", "minLength": 1 }
      }
    },
    "planStart": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "const": "plan-start" },
        "slug": { "type": "string", "minLength": 1 },
        "runId": { "type": "string", "minLength": 1 },
        "taskCount": { "type": "integer", "minimum": 0 },
        "gateMode": { "type": "string" },
        "branch": { "type": "string" },
        "concurrency": { "type": "integer", "minimum": 1 }
      },
      "additionalProperties": true
    },
    "planEnd": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "const": "plan-end" },
        "landStatus": { "type": "string" },
        "integrationHead": { "type": "string" },
        "pushPending": { "type": "boolean" },
        "statuses": { "type": "object", "additionalProperties": { "type": "string" } }
      },
      "additionalProperties": true
    },
    "planError": {
      "type": "object",
      "required": ["kind", "message"],
      "properties": {
        "kind": { "const": "plan-error" },
        "message": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "taskStart": {
      "type": "object",
      "required": ["kind", "taskId"],
      "properties": {
        "kind": { "const": "task-start" },
        "taskId": { "type": "string", "minLength": 1 },
        "desc": { "type": "string" },
        "seat": { "type": "string" },
        "tier": { "type": "string" }
      },
      "additionalProperties": true
    },
    "taskEnd": {
      "type": "object",
      "required": ["kind", "taskId", "status"],
      "properties": {
        "kind": { "const": "task-end" },
        "taskId": { "type": "string", "minLength": 1 },
        "status": { "type": "string", "minLength": 1 },
        "commitSha": { "type": ["string", "null"] },
        "failure": { "type": "object", "additionalProperties": true },
        "blockedDeps": { "type": "array", "items": { "type": "string" } },
        "blockedBy": { "type": "string" }
      },
      "additionalProperties": true
    },
    "taskCheckpoint": {
      "type": "object",
      "required": ["kind", "checkpointId", "planDigest", "taskId", "taskContractDigest", "dependencyCheckpointIds", "baseCommit", "resultCommit", "qualityReceipt", "leaseToken", "leaseEpoch"],
      "properties": {
        "kind": { "const": "task.checkpoint" },
        "checkpointId": { "type": "string", "minLength": 1 },
        "planDigest": { "type": "string", "minLength": 1 },
        "taskId": { "type": "string", "minLength": 1 },
        "taskContractDigest": { "type": "string", "minLength": 1 },
        "dependencyCheckpointIds": { "type": "array", "items": { "type": "string" } },
        "baseCommit": { "type": "string", "minLength": 1 },
        "resultCommit": { "type": "string", "minLength": 1 },
        "qualityReceipt": { "type": "string", "minLength": 1 },
        "leaseToken": { "type": "string", "minLength": 1 },
        "leaseEpoch": { "type": "integer", "minimum": 1 }
      },
      "additionalProperties": true
    },
    "runOpen": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "const": "run.open" },
        "repoRoot": { "type": "string", "minLength": 1 },
        "baseCommit": { "type": "string", "minLength": 1 },
        "integrationRef": { "type": "string", "minLength": 1 },
        "planDigest": { "type": "string", "minLength": 1 },
        "sourceDigest": { "type": "string", "minLength": 1 },
        "taskContracts": { "type": "object", "additionalProperties": { "type": "string" } },
        "engineSha": { "type": "string", "minLength": 1 },
        "engineRoot": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "engineMigrated": {
      "type": "object",
      "required": ["kind", "fromSha", "toSha"],
      "properties": {
        "kind": { "const": "engine.migrated" },
        "fromSha": { "type": "string", "minLength": 1 },
        "toSha": { "type": "string", "minLength": 1 },
        "engineRoot": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "runComplete": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "const": "run.complete" },
        "status": { "type": "string" },
        "landStatus": { "type": "string" },
        "integrationHead": { "type": "string" }
      },
      "additionalProperties": true
    },
    "runIncomplete": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "const": "run.incomplete" },
        "status": { "type": "string" },
        "landStatus": { "type": "string" },
        "integrationHead": { "type": "string" }
      },
      "additionalProperties": true
    },
    "runFailed": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "const": "run.failed" },
        "status": { "type": "string" },
        "failureClass": { "type": "string" },
        "message": { "type": "string" },
        "reason": { "type": "string" }
      },
      "additionalProperties": true
    },
    "runInterrupted": {
      "type": "object",
      "required": ["kind", "status", "signal"],
      "properties": {
        "kind": { "const": "run.interrupted" },
        "status": { "const": "incomplete" },
        "signal": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "qualityPassed": {
      "type": "object",
      "required": ["kind", "receiptId", "taskId"],
      "properties": {
        "kind": { "const": "quality.passed" },
        "receiptId": { "type": "string", "minLength": 1 },
        "taskId": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "integrationIntent": {
      "type": "object",
      "required": ["kind", "taskId", "planDigest", "taskContractDigest", "qualityReceipt", "baseCommit", "resultCommit"],
      "properties": {
        "kind": { "const": "integration.intent" },
        "taskId": { "type": "string", "minLength": 1 },
        "planDigest": { "type": "string", "minLength": 1 },
        "taskContractDigest": { "type": "string", "minLength": 1 },
        "qualityReceipt": { "type": "string", "minLength": 1 },
        "baseCommit": { "type": "string", "minLength": 1 },
        "resultCommit": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "integrationApplied": {
      "type": "object",
      "required": ["kind", "taskId", "planDigest", "taskContractDigest", "qualityReceipt", "baseCommit", "resultCommit"],
      "properties": {
        "kind": { "const": "integration.applied" },
        "taskId": { "type": "string", "minLength": 1 },
        "planDigest": { "type": "string", "minLength": 1 },
        "taskContractDigest": { "type": "string", "minLength": 1 },
        "qualityReceipt": { "type": "string", "minLength": 1 },
        "baseCommit": { "type": "string", "minLength": 1 },
        "resultCommit": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "integrationRecovered": {
      "type": "object",
      "required": ["kind", "taskId", "resultCommit"],
      "properties": {
        "kind": { "const": "integration.recovered" },
        "taskId": { "type": "string", "minLength": 1 },
        "resultCommit": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "provisionFailed": {
      "type": "object",
      "required": ["kind", "taskId", "cmd", "exitCode", "logTail"],
      "properties": {
        "kind": { "const": "provision.failed" },
        "taskId": { "type": "string", "minLength": 1 },
        "cmd": { "type": ["string", "null"] },
        "exitCode": { "type": "integer" },
        "logTail": { "type": ["string", "null"] }
      },
      "additionalProperties": true
    },
    "budgetExhausted": {
      "type": "object",
      "required": ["kind", "task", "count", "cap"],
      "properties": {
        "kind": { "const": "budget.exhausted" },
        "task": { "type": "string", "minLength": 1 },
        "count": { "type": "integer", "minimum": 0 },
        "cap": { "type": "integer", "minimum": 1 }
      },
      "additionalProperties": true
    },
    "dispatchAttempt": {
      "allOf": [
        { "$ref": "#/$defs/qualityIdentity" },
        {
          "required": ["kind", "task", "taskId", "phase", "binding", "runId", "failureClass", "failureFingerprint", "baseHead", "taskHead"],
          "properties": { "kind": { "const": "dispatch.attempt" } },
          "additionalProperties": true
        }
      ]
    },
    "gateAttempt": {
      "allOf": [
        { "$ref": "#/$defs/qualityIdentity" },
        {
          "required": ["kind", "task", "taskId", "phase", "binding", "runId", "failureClass", "failureFingerprint", "baseHead", "taskHead"],
          "properties": {
            "kind": { "const": "gate.attempt" },
            "outputDigest": { "type": "string", "pattern": "^[a-f0-9]{16}$" }
          },
          "additionalProperties": true
        }
      ]
    },
    "retryAttempt": {
      "allOf": [
        { "$ref": "#/$defs/qualityIdentity" },
        {
          "required": ["kind", "task", "taskId", "phase", "binding", "runId", "failureClass", "failureFingerprint", "baseHead", "taskHead", "outcome"],
          "properties": {
            "kind": { "const": "retry.attempt" },
            "outcome": { "type": "string", "enum": ["failed", "succeeded"] },
            "outputDigest": { "type": "string", "pattern": "^[a-f0-9]{16}$" }
          },
          "additionalProperties": true
        }
      ]
    },
    "retryStopLoss": {
      "allOf": [
        { "$ref": "#/$defs/qualityIdentity" },
        {
          "required": ["kind", "task", "taskId", "phase", "binding", "runId", "failureClass", "failureFingerprint", "baseHead", "taskHead"],
          "properties": { "kind": { "const": "retry.stop-loss" } },
          "additionalProperties": true
        }
      ]
    },
    "rateLimitParked": {
      "allOf": [
        { "$ref": "#/$defs/qualityIdentity" },
        {
          "required": ["kind", "task", "taskId", "phase", "binding", "runId", "failureClass", "failureFingerprint", "baseHead", "taskHead"],
          "properties": {
            "kind": { "const": "rate-limit.parked" },
            "parkCount": { "type": "integer", "minimum": 1 },
            "resetAt": { "type": "string", "minLength": 1 },
            "provider": { "type": "string" }
          },
          "additionalProperties": true
        }
      ]
    },
    "rateLimitHeartbeat": {
      "allOf": [
        { "$ref": "#/$defs/qualityIdentity" },
        {
          "required": ["kind", "task", "taskId", "phase", "binding", "runId", "failureClass", "failureFingerprint", "baseHead", "taskHead"],
          "properties": { "kind": { "const": "rate-limit.heartbeat" } },
          "additionalProperties": true
        }
      ]
    },
    "rateLimitWoken": {
      "allOf": [
        { "$ref": "#/$defs/qualityIdentity" },
        {
          "required": ["kind", "task", "taskId", "phase", "binding", "runId", "failureClass", "failureFingerprint", "baseHead", "taskHead"],
          "properties": { "kind": { "const": "rate-limit.woken" } },
          "additionalProperties": true
        }
      ]
    },
    "reviewAttempt": {
      "type": "object",
      "required": ["kind", "task", "phase", "binding"],
      "properties": {
        "kind": { "const": "review.attempt" },
        "task": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 },
        "binding": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "reviewVerdict": {
      "type": "object",
      "required": ["kind", "task", "phase", "binding", "verdict", "findings"],
      "properties": {
        "kind": { "const": "review.verdict" },
        "task": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 },
        "binding": { "type": "string", "minLength": 1 },
        "verdict": { "type": "string", "enum": ["PASS", "FAIL"] },
        "findings": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["severity", "path", "message"],
            "properties": {
              "severity": { "type": "string", "minLength": 1 },
              "path": { "type": ["string", "null"] },
              "message": { "type": "string", "minLength": 1 }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": true
    },
    "reviewRevise": {
      "type": "object",
      "required": ["kind", "task", "attempt", "findingsCount"],
      "properties": {
        "kind": { "const": "review.revise" },
        "task": { "type": "string", "minLength": 1 },
        "attempt": { "type": "integer", "minimum": 1 },
        "findingsCount": { "type": "integer", "minimum": 0 }
      },
      "additionalProperties": true
    },
    "reviewSkipped": {
      "type": "object",
      "required": ["kind", "task", "phase", "reason"],
      "properties": {
        "kind": { "const": "review.skipped" },
        "task": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 },
        "reason": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "qualityGreen": {
      "type": "object",
      "required": ["kind", "task", "phase"],
      "properties": {
        "kind": { "const": "quality.green" },
        "task": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "verifyPassed": {
      "type": "object",
      "required": ["kind", "task", "phase", "commands"],
      "properties": {
        "kind": { "const": "verify.passed" },
        "task": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 },
        "commands": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": ["command", "code"],
            "properties": {
              "command": { "type": "string", "minLength": 1 },
              "code": { "type": "integer" }
            },
            "additionalProperties": true
          }
        }
      },
      "additionalProperties": true
    },
    "verifyFailed": {
      "type": "object",
      "required": ["kind", "task", "phase", "command", "code"],
      "properties": {
        "kind": { "const": "verify.failed" },
        "task": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 },
        "command": { "type": "string", "minLength": 1 },
        "code": { "type": "integer" },
        "tail": { "type": "string" }
      },
      "additionalProperties": true
    },
    "verifyRetry": {
      "type": "object",
      "required": ["kind", "task", "attempt", "code"],
      "properties": {
        "kind": { "const": "verify.retry" },
        "task": { "type": "string", "minLength": 1 },
        "attempt": { "type": "integer", "minimum": 1 },
        "code": { "type": "integer" }
      },
      "additionalProperties": true
    },
    "replyUnmarked": {
      "type": "object",
      "required": ["kind", "task", "phase"],
      "properties": {
        "kind": { "const": "reply.unmarked" },
        "task": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 },
        "binding": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "fixRung": {
      "type": "object",
      "required": ["kind", "task", "phase", "rung", "binding"],
      "properties": {
        "kind": { "const": "fix.rung" },
        "task": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 },
        "rung": { "type": "string", "enum": ["dependency-repair", "fixer", "stronger-fixer"] },
        "binding": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "quarantine": {
      "type": "object",
      "required": ["kind", "phase", "reason"],
      "properties": {
        "kind": { "const": "quarantine" },
        "phase": { "type": "string", "minLength": 1 },
        "reason": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "supervisionRegistered": {
      "type": "object",
      "required": ["kind", "generation", "token", "unit"],
      "properties": {
        "kind": { "const": "supervision.registered" },
        "generation": { "type": "integer", "minimum": 1 },
        "token": { "type": "string", "pattern": "^[A-Za-z0-9._-]{1,128}$" },
        "unit": { "type": "string", "pattern": "^harness-run@[A-Za-z0-9._-]{1,128}\\.service$" }
      },
      "additionalProperties": true
    },
    "attemptPrompt": {
      "type": "object",
      "required": ["kind", "attemptId", "task", "phase", "seat", "model", "wrapper", "timeoutSecs", "promptPath", "promptSha", "causalInput"],
      "properties": {
        "kind": { "const": "attempt.prompt" },
        "attemptId": { "type": "string", "minLength": 1 },
        "task": { "type": "string", "minLength": 1 },
        "phase": { "type": "string", "minLength": 1 },
        "seat": { "type": "string", "minLength": 1 },
        "account": { "type": "string", "minLength": 1 },
        "model": { "type": "string" },
        "wrapper": { "type": "string", "minLength": 1 },
        "timeoutSecs": { "type": "integer", "minimum": 1 },
        "promptPath": { "type": "string", "minLength": 1 },
        "promptSha": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "causalInput": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "attemptReply": {
      "type": "object",
      "required": ["kind", "attemptId", "valid", "replyPath"],
      "properties": {
        "kind": { "const": "attempt.reply" },
        "attemptId": { "type": "string", "minLength": 1 },
        "valid": { "type": "boolean" },
        "verdict": { "type": "string", "enum": ["PASS", "FAIL"] },
        "replyPath": { "type": "string", "minLength": 1 },
        "account": {
          "type": "string",
          "minLength": 1,
          "description": "Optional executing-account identity from the wrapper identity file (HARNESS_IDENTITY_FILE). Contract: one JSON object {\"kind\":\"wrapper.identity\",\"account\":\"...\"} written by the wrapper after a fail-open status probe; never scraped from stderr/stdout/dispatch logs. Absent/malformed file => field omitted."
        }
      },
      "additionalProperties": true
    },
    "attemptUsage": {
      "type": "object",
      "required": ["kind", "attemptId"],
      "oneOf": [
        { "required": ["unknown", "model"], "properties": { "unknown": { "const": true } } },
        { "required": ["inputTokens", "outputTokens", "model"] }
      ],
      "properties": {
        "kind": { "const": "attempt.usage" },
        "attemptId": { "type": "string", "minLength": 1 },
        "unknown": { "type": "boolean" },
        "model": { "type": "string" },
        "inputTokens": { "type": "integer", "minimum": 0 },
        "outputTokens": { "type": "integer", "minimum": 0 },
        "cachedTokens": { "type": "integer", "minimum": 0 },
        "costUsd": { "type": "number", "minimum": 0 }
      },
      "additionalProperties": true
    },
    "attemptHeartbeat": {
      "type": "object",
      "required": ["kind", "attemptId", "pid", "elapsedSecs", "timeoutSecs", "eventCount", "lastActivity"],
      "properties": {
        "kind": { "const": "attempt.heartbeat" },
        "attemptId": { "type": "string", "minLength": 1 },
        "pid": { "type": "integer", "minimum": 0 },
        "elapsedSecs": { "type": "integer", "minimum": 0 },
        "timeoutSecs": { "type": "integer", "minimum": 1 },
        "eventCount": { "type": "integer", "minimum": 0 },
        "lastActivity": { "type": "string" },
        "usageDelta": {
          "type": "object",
          "properties": {
            "inputTokens": { "type": "integer", "minimum": 0 },
            "outputTokens": { "type": "integer", "minimum": 0 },
            "cachedTokens": { "type": "integer", "minimum": 0 },
            "costUsd": { "type": "number", "minimum": 0 }
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": true
    },
    "attemptActivity": {
      "type": "object",
      "required": ["kind", "attemptId", "toolCalls", "filesTouched", "repeatedCommands", "failureFingerprint"],
      "properties": {
        "kind": { "const": "attempt.activity" },
        "attemptId": { "type": "string", "minLength": 1 },
        "toolCalls": { "type": "integer", "minimum": 0 },
        "filesTouched": { "type": "array", "items": { "type": "string" } },
        "repeatedCommands": {
          "type": "array",
          "items": {
            "type": "array",
            "items": { "type": "string" }
          }
        },
        "failureFingerprint": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    }
  },
  "oneOf": [
    { "$ref": "#/$defs/planStart" },
    { "$ref": "#/$defs/planEnd" },
    { "$ref": "#/$defs/planError" },
    { "$ref": "#/$defs/taskStart" },
    { "$ref": "#/$defs/taskEnd" },
    { "$ref": "#/$defs/taskCheckpoint" },
    { "$ref": "#/$defs/runOpen" },
    { "$ref": "#/$defs/engineMigrated" },
    { "$ref": "#/$defs/runComplete" },
    { "$ref": "#/$defs/runIncomplete" },
    { "$ref": "#/$defs/runFailed" },
    { "$ref": "#/$defs/runInterrupted" },
    { "$ref": "#/$defs/qualityPassed" },
    { "$ref": "#/$defs/integrationIntent" },
    { "$ref": "#/$defs/integrationApplied" },
    { "$ref": "#/$defs/integrationRecovered" },
    { "$ref": "#/$defs/provisionFailed" },
    { "$ref": "#/$defs/budgetExhausted" },
    { "$ref": "#/$defs/dispatchAttempt" },
    { "$ref": "#/$defs/gateAttempt" },
    { "$ref": "#/$defs/retryAttempt" },
    { "$ref": "#/$defs/retryStopLoss" },
    { "$ref": "#/$defs/rateLimitParked" },
    { "$ref": "#/$defs/rateLimitHeartbeat" },
    { "$ref": "#/$defs/rateLimitWoken" },
    { "$ref": "#/$defs/reviewAttempt" },
    { "$ref": "#/$defs/reviewVerdict" },
    { "$ref": "#/$defs/reviewRevise" },
    { "$ref": "#/$defs/reviewSkipped" },
    { "$ref": "#/$defs/qualityGreen" },
    { "$ref": "#/$defs/verifyPassed" },
    { "$ref": "#/$defs/verifyFailed" },
    { "$ref": "#/$defs/verifyRetry" },
    { "$ref": "#/$defs/replyUnmarked" },
    { "$ref": "#/$defs/fixRung" },
    { "$ref": "#/$defs/quarantine" },
    { "$ref": "#/$defs/supervisionRegistered" },
    { "$ref": "#/$defs/attemptPrompt" },
    { "$ref": "#/$defs/attemptReply" },
    { "$ref": "#/$defs/attemptUsage" },
    { "$ref": "#/$defs/attemptHeartbeat" },
    { "$ref": "#/$defs/attemptActivity" }
  ]
}
