{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://aiox.synkra.dev/schemas/build-state.json",
  "title": "Build State",
  "description": "Schema for autonomous build state tracking (Story 8.4)",
  "type": "object",
  "required": ["storyId", "status", "startedAt", "checkpoints"],
  "properties": {
    "storyId": {
      "type": "string",
      "description": "Story identifier (e.g., 'story-8.4')"
    },
    "startedAt": {
      "type": "string",
      "format": "date-time",
      "description": "Build start timestamp"
    },
    "lastCheckpoint": {
      "type": "string",
      "format": "date-time",
      "description": "Last successful checkpoint timestamp"
    },
    "status": {
      "type": "string",
      "enum": ["pending", "in_progress", "paused", "abandoned", "failed", "completed"],
      "description": "Current build status"
    },
    "currentPhase": {
      "type": "string",
      "description": "Current phase identifier (e.g., 'phase-2')"
    },
    "currentSubtask": {
      "type": "string",
      "description": "Current subtask identifier (e.g., '2.3')"
    },
    "completedSubtasks": {
      "type": "array",
      "items": { "type": "string" },
      "description": "List of completed subtask IDs"
    },
    "failedAttempts": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["subtaskId", "attempt", "timestamp"],
        "properties": {
          "subtaskId": { "type": "string" },
          "attempt": { "type": "integer", "minimum": 1 },
          "error": { "type": "string" },
          "timestamp": { "type": "string", "format": "date-time" },
          "approach": { "type": "string" },
          "duration": { "type": "string" }
        }
      },
      "description": "History of failed attempts"
    },
    "worktree": {
      "type": "object",
      "properties": {
        "path": { "type": "string" },
        "branch": { "type": "string" },
        "createdAt": { "type": "string", "format": "date-time" }
      },
      "description": "Git worktree information for isolated builds"
    },
    "plan": {
      "type": "object",
      "properties": {
        "path": { "type": "string" },
        "totalPhases": { "type": "integer" },
        "totalSubtasks": { "type": "integer" }
      },
      "description": "Implementation plan reference"
    },
    "checkpoints": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["id", "timestamp", "subtaskId", "status"],
        "properties": {
          "id": { "type": "string" },
          "timestamp": { "type": "string", "format": "date-time" },
          "subtaskId": { "type": "string" },
          "status": { "type": "string" },
          "gitCommit": { "type": "string" },
          "filesModified": {
            "type": "array",
            "items": { "type": "string" }
          },
          "metrics": {
            "type": "object",
            "properties": {
              "duration": { "type": "integer" },
              "attempts": { "type": "integer" }
            }
          }
        }
      },
      "description": "Checkpoint history for resume capability"
    },
    "metrics": {
      "type": "object",
      "properties": {
        "totalSubtasks": { "type": "integer", "minimum": 0 },
        "completedSubtasks": { "type": "integer", "minimum": 0 },
        "totalAttempts": { "type": "integer", "minimum": 0 },
        "totalFailures": { "type": "integer", "minimum": 0 },
        "averageTimePerSubtask": { "type": "integer", "minimum": 0 },
        "totalDuration": { "type": "integer", "minimum": 0 }
      },
      "description": "Build execution metrics"
    },
    "abandoned": {
      "type": "boolean",
      "default": false,
      "description": "Flag indicating build was abandoned"
    },
    "abandonedAt": {
      "type": "string",
      "format": "date-time",
      "description": "Timestamp when build was marked abandoned"
    },
    "abandonedReason": {
      "type": "string",
      "description": "Reason for abandonment"
    },
    "notifications": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "type": { "type": "string", "enum": ["info", "warning", "error", "stuck", "abandoned"] },
          "message": { "type": "string" },
          "timestamp": { "type": "string", "format": "date-time" },
          "acknowledged": { "type": "boolean", "default": false }
        }
      },
      "description": "Build notifications history"
    },
    "config": {
      "type": "object",
      "properties": {
        "maxIterations": { "type": "integer", "default": 10 },
        "globalTimeout": { "type": "integer", "default": 1800000 },
        "abandonedThreshold": { "type": "integer", "default": 3600000 },
        "autoCheckpoint": { "type": "boolean", "default": true }
      },
      "description": "Build configuration overrides"
    },
    "schemaVersion": {
      "type": "string",
      "default": "1.0.0",
      "description": "Schema version for migrations"
    }
  },
  "additionalProperties": false
}
