{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/kitepon/Lattice/blob/main/docs/schemas/lattice.todo_extraction.v4.schema.json",
  "title": "lattice.todo_extraction.v4",
  "description": "AI-authored ToDo migration artifact. Every task carries a non-blank Markdown design_memo. After the complete plan is written, run `lattice plan scope-review` and confirm that every registered task preserves an owner-supplied work specification without adding a product purpose. If no plan exists, write NO_PLAN explicitly. Runtime validation accepts at most one explicit, reasoned cross-plan dependency; an empty tasks array is reserved for that one dependency between existing tasks, with the top-level plan identity naming the source plan and both endpoint topology digests.",
  "$comment": "あなたがこのToDoに対して、何も考えていないならば、設計メモに `NO_PLAN` と書いてください。Runtime validation also enforces UTF-8 byte bounds, exact keys, strict sort order, referential integrity, disposition consistency, the cross-plan dependency limit, and the canonical self-digest.",
  "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.v4" },
    "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": 0, "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" },
          "reason": { "$ref": "#/$defs/text" }
        }
      }
    },
    "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" }
  },
  "allOf": [
    {
      "if": { "properties": { "tasks": { "maxItems": 0 } }, "required": ["tasks"] },
      "then": {
        "properties": {
          "hard_dependencies": { "minItems": 1, "maxItems": 1 },
          "joins": { "maxItems": 0 }
        }
      }
    }
  ],
  "$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 },
    "designMemo": {
      "type": "string", "minLength": 1, "maxLength": 16384, "pattern": "\\S",
      "description": "Markdown design memo. Use the literal NO_PLAN only when no plan exists."
    },
    "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$"
    },
    "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" }] }
      }
    },
    "task": {
      "type": "object", "additionalProperties": false,
      "required": [
        "task_id", "title", "lane", "design_memo", "narrative_ref", "compile_binding",
        "disposition", "start", "completion", "source", "migration_context"
      ],
      "properties": {
        "task_id": { "$ref": "#/$defs/identifier" },
        "title": { "$ref": "#/$defs/text" },
        "lane": { "$ref": "#/$defs/identifier" },
        "design_memo": { "$ref": "#/$defs/designMemo" },
        "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" }
      },
      "allOf": [
        {
          "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" } } }
          }
        }
      ]
    }
  }
}
