{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://civitas-cerebrum.github.io/element-interactions/schemas/subagent-returns/repair-worker.schema.json",
  "title": "Repair-worker (self-repair) return",
  "description": "Return shape for a self-repair worker scoped to one spec file. Serves both fronts of the self-repair entrypoint: the Agent-subagent return in interactive mode, and the JSON report a `claude -p` worker subprocess writes to disk in script mode (the CLI driver validates it with this same schema).",
  "type": "object",
  "additionalProperties": true,
  "required": ["handover", "file", "tests"],
  "properties": {
    "handover": {
      "allOf": [
        { "$ref": "handover.schema.json" },
        {
          "type": "object",
          "properties": {
            "status": { "enum": ["completed", "blocked"] }
          }
        }
      ]
    },
    "file": {
      "type": "string",
      "minLength": 1,
      "description": "The one spec file this worker was scoped to."
    },
    "tests": {
      "type": "array",
      "minItems": 1,
      "description": "One entry per test the worker was briefed on. No silent drops — every briefed test must appear.",
      "items": {
        "type": "object",
        "additionalProperties": true,
        "required": ["title", "outcome"],
        "properties": {
          "title": { "type": "string", "minLength": 1 },
          "outcome": {
            "enum": ["already-green", "healed", "app-bug", "quarantined", "operator-pending", "unresolved"]
          },
          "baseline-pattern": {
            "enum": ["green", "deterministic-fail", "flaky-consistent", "flaky-chaotic"]
          },
          "root-cause": { "type": "string" },
          "fix": { "type": "string", "description": "What was changed (selector, wait, state isolation) when outcome is healed." },
          "stability-runs": {
            "type": "object",
            "required": ["passed", "total"],
            "properties": {
              "passed": { "type": "integer", "minimum": 0 },
              "total": { "type": "integer", "minimum": 1 }
            }
          },
          "understanding": {
            "type": "string",
            "description": "Stage-5 understanding: the causal chain established by the experiment stage, in one or a few sentences. Recommended for every non-green outcome."
          },
          "bug-report": {
            "type": "object",
            "required": ["summary", "expected-behaviour", "actual-behaviour"],
            "properties": {
              "summary": { "type": "string", "minLength": 1 },
              "expected-behaviour": { "type": "string", "minLength": 1, "description": "What the journey should do at the failure point." },
              "actual-behaviour": { "type": "string", "minLength": 1, "description": "What the app demonstrably does, per the evidence." },
              "confidence": { "enum": ["high", "medium", "low"], "description": "App-bug outcomes require high — mechanism demonstrated and test-side causes excluded by experiment." },
              "evidence": {
                "type": "array",
                "items": { "type": "string" },
                "description": "Paths to the evidence bundle backing the app-bug classification — failure screenshot, error context/trace, failing-run video, and a slow-motion reproduction recording. Paths point at the stable <e2e-root>/bug-evidence/<TEST-ID>/ copies, never at reusable test-results/ dirs."
              }
            }
          },
          "notes": { "type": "string" }
        },
        "allOf": [
          {
            "if": { "type": "object", "required": ["outcome"], "properties": { "outcome": { "const": "healed" } } },
            "then": {
              "type": "object",
              "required": ["fix", "stability-runs"],
              "properties": {
                "fix": { "type": "string" },
                "stability-runs": { "type": "object" }
              }
            }
          },
          {
            "if": { "type": "object", "required": ["outcome"], "properties": { "outcome": { "const": "app-bug" } } },
            "then": {
              "type": "object",
              "required": ["bug-report"],
              "properties": {
                "bug-report": { "type": "object" }
              }
            }
          }
        ]
      }
    },
    "stage-log": {
      "type": "array",
      "description": "Per-stage progress announcements — the structured mirror of the worker's `[self-repair:worker] stage=…` lines.",
      "items": {
        "type": "object",
        "required": ["stage"],
        "properties": {
          "stage": { "enum": ["reproduce", "evidence-analysis", "context-probe", "experiment", "understand", "fix", "verify", "done"] },
          "test": { "type": "string" },
          "detail": { "type": "string" }
        }
      }
    },
    "observations": {
      "type": "array",
      "items": { "type": "string" },
      "description": "Anything worth surfacing in the session report that isn't a per-test outcome (systemic debt, shared-fixture smells, app oddities)."
    },
    "summary": { "type": "string" }
  }
}
