{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://simplest-sdd.dev/schema/execution.schema.json",
  "title": "Simplest SDD execution record",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "specId",
    "title",
    "classification",
    "planning",
    "strategy",
    "tasks",
    "runs"
  ],
  "properties": {
    "schemaVersion": { "const": "1.0.0" },
    "specId": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
    "title": { "type": "string", "minLength": 1 },
    "classification": {
      "type": "object",
      "additionalProperties": false,
      "required": ["primaryCategory", "tags", "overallEffort", "planConfidence", "delegationConfidence"],
      "properties": {
        "primaryCategory": { "$ref": "#/$defs/category" },
        "tags": { "type": "array", "items": { "$ref": "#/$defs/category" }, "uniqueItems": true },
        "overallEffort": { "$ref": "#/$defs/effort" },
        "planConfidence": { "$ref": "#/$defs/confidence" },
        "delegationConfidence": { "$ref": "#/$defs/confidence" }
      }
    },
    "planning": {
      "type": "object",
      "additionalProperties": false,
      "required": ["plannedAt", "plannedAtCommit", "capabilityProfile", "actualModel", "reasoningEffort"],
      "properties": {
        "plannedAt": { "type": "string", "format": "date-time" },
        "plannedAtCommit": { "type": ["string", "null"] },
        "capabilityProfile": { "$ref": "#/$defs/profile" },
        "actualModel": { "type": ["string", "null"] },
        "reasoningEffort": { "type": ["string", "null"] }
      }
    },
    "strategy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["recommended", "optionsPresented", "selected"],
      "properties": {
        "recommended": {
          "type": "object",
          "additionalProperties": false,
          "required": ["mode", "rationale"],
          "properties": {
            "mode": { "$ref": "#/$defs/strategyMode" },
            "rationale": { "type": "string", "minLength": 1 }
          }
        },
        "optionsPresented": {
          "type": "array",
          "items": { "$ref": "#/$defs/strategyMode" },
          "contains": { "const": "same-session" },
          "uniqueItems": true
        },
        "selected": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["mode", "selectedAt", "selectedBy", "overrideReason", "customInstructions"],
              "properties": {
                "mode": { "$ref": "#/$defs/strategyMode" },
                "selectedAt": { "type": "string", "format": "date-time" },
                "selectedBy": { "const": "user" },
                "overrideReason": { "type": ["string", "null"] },
                "customInstructions": { "type": ["string", "null"] }
              }
            }
          ]
        }
      }
    },
    "tasks": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/task" }
    },
    "runs": {
      "type": "array",
      "items": { "$ref": "#/$defs/run" }
    }
  },
  "$defs": {
    "category": {
      "enum": ["feature", "bug", "security", "performance", "tests", "tech-debt", "migration", "dx", "docs", "research", "design"]
    },
    "effort": { "enum": ["S", "M", "L"] },
    "confidence": { "enum": ["low", "medium", "high"] },
    "risk": { "enum": ["low", "medium", "high"] },
    "profile": { "enum": ["strong-planner", "strong-worker", "efficient-worker", "strong-reviewer", "efficient-reviewer"] },
    "strategyMode": { "enum": ["same-session", "delegated", "hybrid", "custom"] },
    "task": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "title",
        "category",
        "effort",
        "risk",
        "planConfidence",
        "delegationConfidence",
        "dependsOn",
        "parallelizable",
        "scope",
        "verification",
        "stopConditions",
        "recommendedExecution",
        "selectedExecution"
      ],
      "properties": {
        "id": { "type": "string", "pattern": "^T[1-9][0-9]*$" },
        "title": { "type": "string", "minLength": 1 },
        "category": { "$ref": "#/$defs/category" },
        "effort": { "$ref": "#/$defs/effort" },
        "risk": { "$ref": "#/$defs/risk" },
        "planConfidence": { "$ref": "#/$defs/confidence" },
        "delegationConfidence": { "$ref": "#/$defs/confidence" },
        "dependsOn": { "type": "array", "items": { "type": "string", "pattern": "^T[1-9][0-9]*$" }, "uniqueItems": true },
        "parallelizable": { "type": "boolean" },
        "scope": {
          "type": "object",
          "additionalProperties": false,
          "required": ["in", "out"],
          "properties": {
            "in": { "type": "array", "items": { "type": "string" }, "minItems": 1, "uniqueItems": true },
            "out": { "type": "array", "items": { "type": "string" }, "uniqueItems": true }
          }
        },
        "verification": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["command", "expected"],
            "properties": {
              "command": { "type": "string", "minLength": 1 },
              "expected": { "type": "string", "minLength": 1 }
            }
          }
        },
        "stopConditions": { "type": "array", "items": { "type": "string", "minLength": 1 }, "minItems": 1 },
        "recommendedExecution": { "$ref": "#/$defs/assignment" },
        "selectedExecution": {
          "oneOf": [
            { "type": "null" },
            { "$ref": "#/$defs/assignment" }
          ]
        }
      }
    },
    "assignment": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "capabilityProfile", "reasoningEffort", "rationale"],
      "properties": {
        "mode": { "enum": ["same-session", "delegated"] },
        "capabilityProfile": { "$ref": "#/$defs/profile" },
        "reasoningEffort": { "enum": ["low", "medium", "high"] },
        "rationale": { "type": "string", "minLength": 1 }
      }
    },
    "run": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "role",
        "taskIds",
        "executionMode",
        "capabilityProfile",
        "actualModel",
        "reasoningEffort",
        "provider",
        "startedAt",
        "completedAt",
        "durationMs",
        "tokenUsage",
        "outcome",
        "revisionCount",
        "verification",
        "worktree",
        "branch",
        "commitBefore",
        "commitAfter",
        "notes"
      ],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "role": { "enum": ["planner", "orchestrator", "executor", "verifier", "reviewer"] },
        "taskIds": { "type": "array", "items": { "type": "string", "pattern": "^T[1-9][0-9]*$" }, "uniqueItems": true },
        "executionMode": { "enum": ["same-session", "delegated"] },
        "capabilityProfile": { "$ref": "#/$defs/profile" },
        "actualModel": { "type": ["string", "null"], "minLength": 1 },
        "reasoningEffort": { "type": ["string", "null"] },
        "provider": { "type": ["string", "null"] },
        "startedAt": { "type": ["string", "null"], "format": "date-time" },
        "completedAt": { "type": ["string", "null"], "format": "date-time" },
        "durationMs": { "type": ["integer", "null"], "minimum": 0 },
        "tokenUsage": {
          "type": "object",
          "additionalProperties": false,
          "required": ["usageId", "source", "scope", "inputTokens", "cachedInputTokens", "outputTokens", "reasoningOutputTokens", "totalTokens"],
          "properties": {
            "usageId": { "type": ["string", "null"] },
            "source": { "enum": ["measured", "reported", "estimated", "unavailable"] },
            "scope": { "enum": ["task", "run", "session"] },
            "inputTokens": { "type": ["integer", "null"], "minimum": 0 },
            "cachedInputTokens": { "type": ["integer", "null"], "minimum": 0 },
            "outputTokens": { "type": ["integer", "null"], "minimum": 0 },
            "reasoningOutputTokens": { "type": ["integer", "null"], "minimum": 0 },
            "totalTokens": { "type": ["integer", "null"], "minimum": 0 }
          }
        },
        "outcome": { "enum": ["complete", "stopped", "blocked", "failed"] },
        "revisionCount": { "type": "integer", "minimum": 0 },
        "verification": { "type": "array", "items": { "type": "string" } },
        "worktree": { "type": ["string", "null"] },
        "branch": { "type": ["string", "null"] },
        "commitBefore": { "type": ["string", "null"] },
        "commitAfter": { "type": ["string", "null"] },
        "notes": { "type": ["string", "null"] }
      }
    }
  }
}
