{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/boshu2/agentops/docs/contracts/swarm-worker-result.schema.json",
  "title": "Swarm Worker Result",
  "description": "Schema for .agents/swarm/results/<task-id>.json artifacts emitted by swarm workers and consumed by crank wave acceptance.",
  "type": "object",
  "required": ["type", "issue_id", "status", "detail"],
  "properties": {
    "schema_version": {
      "type": "integer",
      "description": "Optional schema version marker for forward compatibility.",
      "const": 1
    },
    "type": {
      "type": "string",
      "description": "Worker outcome type.",
      "enum": ["completion", "blocked"]
    },
    "issue_id": {
      "type": "string",
      "description": "Issue/task identifier this worker executed.",
      "minLength": 1
    },
    "status": {
      "type": "string",
      "description": "Outcome status for the worker task.",
      "enum": ["done", "blocked"]
    },
    "detail": {
      "type": "string",
      "description": "Short human-readable summary.",
      "minLength": 1,
      "maxLength": 400
    },
    "artifacts": {
      "type": "array",
      "description": "Paths to files changed or produced by the worker.",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "uniqueItems": true
    },
    "worktree_path": {
      "type": "string",
      "description": "Optional worktree path used by isolated worker execution.",
      "minLength": 1
    },
    "evidence": {
      "$ref": "#/$defs/evidence"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "type": { "const": "completion" }
        },
        "required": ["type"]
      },
      "then": {
        "required": ["artifacts"],
        "properties": {
          "status": { "const": "done" }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": { "const": "blocked" }
        },
        "required": ["type"]
      },
      "then": {
        "properties": {
          "status": { "const": "blocked" }
        }
      }
    }
  ],
  "$defs": {
    "evidence_check": {
      "type": "object",
      "required": ["verdict"],
      "properties": {
        "verdict": {
          "type": "string",
          "description": "Outcome of this check.",
          "enum": ["PASS", "FAIL", "SKIP"]
        },
        "command": {
          "type": "string",
          "description": "Command used to produce this evidence."
        },
        "exit_code": {
          "type": "integer",
          "description": "Exit code from command execution, when applicable."
        },
        "summary": {
          "type": "string",
          "description": "Short machine-friendly summary."
        },
        "run_at": {
          "type": "string",
          "format": "date-time",
          "description": "ISO 8601 timestamp when this check ran."
        },
        "artifacts": {
          "type": "array",
          "description": "Optional evidence artifact files (logs, reports).",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        }
      },
      "additionalProperties": false
    },
    "evidence": {
      "type": "object",
      "description": "Machine-readable validation evidence used for fail-closed crank wave acceptance.",
      "required": ["required_checks", "checks"],
      "properties": {
        "required_checks": {
          "type": "array",
          "description": "Checks that are mandatory for this task. Crank must fail closed if any required check is absent.",
          "items": {
            "type": "string",
            "enum": ["red_green", "full_suite"]
          },
          "uniqueItems": true,
          "minItems": 1
        },
        "checks": {
          "type": "object",
          "description": "Evidence payload keyed by check name.",
          "minProperties": 1,
          "properties": {
            "red_green": { "$ref": "#/$defs/evidence_check" },
            "full_suite": { "$ref": "#/$defs/evidence_check" }
          },
          "additionalProperties": {
            "$ref": "#/$defs/evidence_check"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "required_checks": {
                "contains": { "const": "red_green" }
              }
            },
            "required": ["required_checks"]
          },
          "then": {
            "properties": {
              "checks": {
                "required": ["red_green"]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "required_checks": {
                "contains": { "const": "full_suite" }
              }
            },
            "required": ["required_checks"]
          },
          "then": {
            "properties": {
              "checks": {
                "required": ["full_suite"]
              }
            }
          }
        }
      ],
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
