{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/kitepon/Lattice/blob/main/docs/schemas/lattice.todo_extraction.v2.schema.json",
  "title": "lattice.todo_extraction.v2",
  "description": "G4 migration-wave-only intermediate artifact authored by AI. Version 2 adds fail-closed historical in-progress registration without changing v1.",
  "$comment": "Runtime validation additionally enforces UTF-8 byte bounds, strict sort order, unique and resolvable task ids, and extraction_digest as the canonical self-digest. Registration separately rejects an extraction with no register_* task.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "project_id",
    "plan_key",
    "plan_version",
    "actor",
    "recorded_at",
    "tasks",
    "hard_dependencies",
    "joins",
    "extraction_digest"
  ],
  "properties": {
    "schema": { "const": "lattice.todo_extraction.v2" },
    "project_id": { "$ref": "#/$defs/identifier" },
    "plan_key": { "$ref": "#/$defs/identifier" },
    "plan_version": { "$ref": "#/$defs/identifier" },
    "actor": { "$ref": "#/$defs/actor" },
    "recorded_at": { "$ref": "#/$defs/strictTimestamp" },
    "tasks": {
      "type": "array",
      "minItems": 1,
      "maxItems": 512,
      "items": { "$ref": "#/$defs/task" }
    },
    "hard_dependencies": {
      "type": "array",
      "maxItems": 2048,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["from", "to"],
        "properties": {
          "from": { "$ref": "#/$defs/nodeRef" },
          "to": { "$ref": "#/$defs/nodeRef" }
        }
      }
    },
    "joins": {
      "type": "array",
      "maxItems": 128,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "after", "before"],
        "properties": {
          "id": { "$ref": "#/$defs/identifier" },
          "after": {
            "type": "array",
            "minItems": 1,
            "maxItems": 512,
            "items": { "$ref": "#/$defs/nodeRef" }
          },
          "before": { "$ref": "#/$defs/nodeRef" }
        }
      }
    },
    "extraction_digest": { "$ref": "#/$defs/digest" }
  },
  "$defs": {
    "identifier": {
      "type": "string",
      "pattern": "^[0-9A-Za-z](?:[0-9A-Za-z._-]{0,127})$"
    },
    "digest": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "repoRef": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1024,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.{1,2}(?:/|$))(?!.*\\\\)(?!.*[\\u0000-\\u001f\\u007f])[^/]+(?:/[^/]+)*$"
    },
    "text": {
      "type": "string",
      "minLength": 1,
      "maxLength": 16384
    },
    "nullableText": {
      "oneOf": [
        { "type": "null" },
        { "$ref": "#/$defs/text" }
      ]
    },
    "strictTimestamp": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$",
      "$comment": "Runtime validation also requires Date.parse(value).toISOString() byte equality."
    },
    "actor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["host", "session", "agent"],
      "properties": {
        "host": { "$ref": "#/$defs/identifier" },
        "session": { "$ref": "#/$defs/identifier" },
        "agent": { "$ref": "#/$defs/identifier" }
      }
    },
    "nodeRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["project_id", "plan_key", "task_id"],
      "properties": {
        "project_id": { "$ref": "#/$defs/identifier" },
        "plan_key": { "$ref": "#/$defs/identifier" },
        "task_id": { "$ref": "#/$defs/identifier" },
        "expected_topology_digest": { "$ref": "#/$defs/digest" }
      }
    },
    "sourceLocation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "origin_plan_ref",
        "origin_line",
        "source_commit",
        "heading_path",
        "markdown_depth",
        "parent_task_id",
        "checkbox_state"
      ],
      "properties": {
        "origin_plan_ref": { "$ref": "#/$defs/repoRef" },
        "origin_line": { "type": "integer", "minimum": 1 },
        "source_commit": { "type": "string", "pattern": "^(?:[0-9a-f]{40}|[0-9a-f]{64})$" },
        "heading_path": {
          "type": "array",
          "maxItems": 32,
          "items": { "type": "string", "minLength": 1, "maxLength": 1024 }
        },
        "markdown_depth": { "type": "integer", "minimum": 0, "maximum": 64 },
        "parent_task_id": {
          "oneOf": [
            { "type": "null" },
            { "$ref": "#/$defs/identifier" }
          ]
        },
        "checkbox_state": {
          "enum": ["checked", "unchecked", "absent", "ambiguous"]
        }
      }
    },
    "migrationContext": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "external_canonical_ref",
        "carry_over_ref",
        "h_required",
        "condition",
        "evidence_refs",
        "notes"
      ],
      "properties": {
        "external_canonical_ref": { "$ref": "#/$defs/nullableText" },
        "carry_over_ref": { "$ref": "#/$defs/nullableText" },
        "h_required": { "type": "boolean" },
        "condition": { "$ref": "#/$defs/nullableText" },
        "evidence_refs": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "minLength": 1, "maxLength": 4096 }
        },
        "notes": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "minLength": 1, "maxLength": 4096 }
        }
      }
    },
    "historicalStart": {
      "type": "object",
      "additionalProperties": false,
      "required": ["start_mode", "status", "started_at"],
      "properties": {
        "start_mode": { "const": "historical_import" },
        "status": { "const": "in-progress" },
        "started_at": {
          "oneOf": [
            { "const": "unknown_requires_evidence" },
            { "$ref": "#/$defs/strictTimestamp" }
          ]
        }
      }
    },
    "historicalCompletion": {
      "type": "object",
      "additionalProperties": false,
      "required": ["done_mode", "completed_at"],
      "properties": {
        "done_mode": { "const": "historical_import" },
        "completed_at": {
          "oneOf": [
            { "const": "unknown_requires_evidence" },
            { "$ref": "#/$defs/strictTimestamp" }
          ]
        }
      }
    },
    "taskBase": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "task_id",
        "title",
        "lane",
        "narrative_ref",
        "compile_binding",
        "disposition",
        "start",
        "completion",
        "source",
        "migration_context"
      ],
      "properties": {
        "task_id": { "$ref": "#/$defs/identifier" },
        "title": { "$ref": "#/$defs/text" },
        "lane": { "$ref": "#/$defs/identifier" },
        "narrative_ref": {
          "oneOf": [
            { "type": "null" },
            { "$ref": "#/$defs/repoRef" }
          ]
        },
        "compile_binding": { "type": "null" },
        "disposition": {
          "enum": [
            "register_pending",
            "register_in_progress",
            "register_done",
            "exclude_superseded",
            "exclude_compatibility_record",
            "unknown_requires_evidence"
          ]
        },
        "start": {
          "oneOf": [
            { "type": "null" },
            { "$ref": "#/$defs/historicalStart" }
          ]
        },
        "completion": {
          "oneOf": [
            { "type": "null" },
            { "$ref": "#/$defs/historicalCompletion" }
          ]
        },
        "source": { "$ref": "#/$defs/sourceLocation" },
        "migration_context": { "$ref": "#/$defs/migrationContext" }
      }
    },
    "task": {
      "allOf": [
        { "$ref": "#/$defs/taskBase" },
        {
          "if": {
            "properties": { "disposition": { "const": "register_in_progress" } },
            "required": ["disposition"]
          },
          "then": {
            "properties": {
              "start": { "$ref": "#/$defs/historicalStart" },
              "completion": { "type": "null" }
            }
          },
          "else": {
            "if": {
              "properties": { "disposition": { "const": "register_done" } },
              "required": ["disposition"]
            },
            "then": {
              "properties": {
                "start": { "type": "null" },
                "completion": { "$ref": "#/$defs/historicalCompletion" }
              }
            },
            "else": {
              "properties": {
                "start": { "type": "null" },
                "completion": { "type": "null" }
              }
            }
          }
        }
      ]
    }
  }
}
