{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/kitepon/Lattice/blob/main/docs/schemas/lattice.todo_structure_set.v1.schema.json",
  "title": "lattice.todo_structure_set.v1",
  "description": "AI-authored logical dataflow overlay for one registered Lattice plan version.",
  "$comment": "Runtime validation additionally enforces UTF-8 byte bounds, strict canonical ordering, referential integrity, exact plan task coverage when supplied, unique data IDs, constant canonical-byte limits, the canonical self-digest, and plan-wide totals of at most 512 anchors, 4096 ports, 2048 operations, and 4096 sinks.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema", "project_id", "plan_key", "plan_version", "topology_digest", "profile",
    "baseline_sha", "external_contracts", "tasks", "structure_set_digest"
  ],
  "properties": {
    "schema": { "const": "lattice.todo_structure_set.v1" },
    "project_id": { "$ref": "#/$defs/identifier" },
    "plan_key": { "$ref": "#/$defs/identifier" },
    "plan_version": { "$ref": "#/$defs/identifier" },
    "topology_digest": { "$ref": "#/$defs/digest" },
    "profile": { "const": "code-dataflow" },
    "baseline_sha": { "$ref": "#/$defs/gitSha" },
    "external_contracts": {
      "type": "array", "maxItems": 256, "uniqueItems": true,
      "items": { "$ref": "#/$defs/externalContract" }
    },
    "tasks": {
      "type": "array", "minItems": 1, "maxItems": 512, "uniqueItems": true,
      "items": { "oneOf": [{ "$ref": "#/$defs/graphTask" }, { "$ref": "#/$defs/excludedTask" }] }
    },
    "structure_set_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}$" },
    "gitSha": { "type": "string", "pattern": "^[0-9a-f]{40}$" },
    "repoRef": {
      "type": "string", "minLength": 1, "maxLength": 1024,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.{1,2}(?:/|$))(?!.*\\\\)(?!.*[\\u0000-\\u001f\\u007f])[^/]+(?:/[^/]+)*$"
    },
    "text": {
      "type": "string", "minLength": 1, "maxLength": 4096,
      "pattern": "^[^\\u0000-\\u001f\\u007f]+$"
    },
    "schemaRef": {
      "type": "object", "additionalProperties": false,
      "required": ["path", "symbol", "json_pointer"],
      "properties": {
        "path": { "$ref": "#/$defs/repoRef" },
        "symbol": { "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/text" }] },
        "json_pointer": {
          "oneOf": [
            { "type": "null" },
            { "type": "string", "pattern": "^(?:/(?:[^~/]|~[01])*)*$" }
          ]
        }
      }
    },
    "dataContract": {
      "type": "object", "additionalProperties": false,
      "required": [
        "shape_id", "schema_ref", "identity_fields", "lifecycle", "cardinality",
        "compatible_shape_ids"
      ],
      "properties": {
        "shape_id": { "$ref": "#/$defs/identifier" },
        "schema_ref": { "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/schemaRef" }] },
        "identity_fields": {
          "type": "array", "maxItems": 256, "uniqueItems": true,
          "items": { "$ref": "#/$defs/identifier" }
        },
        "lifecycle": {
          "enum": ["snapshot", "event", "stream", "mutable_state", "immutable_artifact"]
        },
        "cardinality": { "enum": ["one", "optional", "many"] },
        "compatible_shape_ids": {
          "type": "array", "maxItems": 256, "uniqueItems": true,
          "items": { "$ref": "#/$defs/identifier" }
        }
      }
    },
    "codeAnchor": {
      "type": "object", "additionalProperties": false,
      "required": ["anchor_id", "effect", "path", "symbol", "expected_at"],
      "properties": {
        "anchor_id": { "$ref": "#/$defs/identifier" },
        "effect": { "enum": ["read", "modify", "create", "delete"] },
        "path": { "$ref": "#/$defs/repoRef" },
        "symbol": { "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/text" }] },
        "expected_at": { "enum": ["baseline", "current", "after_task"] }
      }
    },
    "sourceRef": {
      "oneOf": [
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "anchor_id"],
          "properties": { "kind": { "const": "code" }, "anchor_id": { "$ref": "#/$defs/identifier" } }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "task_id", "port_id"],
          "properties": {
            "kind": { "const": "task_output" },
            "task_id": { "$ref": "#/$defs/identifier" },
            "port_id": { "$ref": "#/$defs/identifier" }
          }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "contract_id"],
          "properties": {
            "kind": { "const": "external" },
            "contract_id": { "$ref": "#/$defs/identifier" }
          }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "constant_id", "value"],
          "properties": {
            "kind": { "const": "constant" },
            "constant_id": { "$ref": "#/$defs/identifier" },
            "value": true
          }
        }
      ]
    },
    "inputPort": {
      "type": "object", "additionalProperties": false,
      "required": ["port_id", "source", "access", "contract"],
      "properties": {
        "port_id": { "$ref": "#/$defs/identifier" },
        "source": { "$ref": "#/$defs/sourceRef" },
        "access": { "enum": ["read", "consume", "observe"] },
        "contract": { "$ref": "#/$defs/dataContract" }
      }
    },
    "outputSink": {
      "oneOf": [
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "task_id", "port_id"],
          "properties": {
            "kind": { "const": "task" },
            "task_id": { "$ref": "#/$defs/identifier" },
            "port_id": { "$ref": "#/$defs/identifier" }
          }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "anchor_id"],
          "properties": { "kind": { "const": "code" }, "anchor_id": { "$ref": "#/$defs/identifier" } }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "contract_id"],
          "properties": {
            "kind": { "const": "external" },
            "contract_id": { "$ref": "#/$defs/identifier" }
          }
        },
        {
          "type": "object", "additionalProperties": false,
          "required": ["kind", "product_id"],
          "properties": {
            "kind": { "const": "final_product" },
            "product_id": { "$ref": "#/$defs/identifier" }
          }
        }
      ]
    },
    "outputPort": {
      "type": "object", "additionalProperties": false,
      "required": ["port_id", "data_id", "contract", "sinks"],
      "properties": {
        "port_id": { "$ref": "#/$defs/identifier" },
        "data_id": { "$ref": "#/$defs/identifier" },
        "contract": { "$ref": "#/$defs/dataContract" },
        "sinks": {
          "type": "array", "maxItems": 256, "uniqueItems": true,
          "items": { "$ref": "#/$defs/outputSink" }
        }
      }
    },
    "operation": {
      "type": "object", "additionalProperties": false,
      "required": ["operation_id", "input_port_ids", "output_port_ids", "summary"],
      "properties": {
        "operation_id": { "$ref": "#/$defs/identifier" },
        "input_port_ids": {
          "type": "array", "maxItems": 256, "uniqueItems": true,
          "items": { "$ref": "#/$defs/identifier" }
        },
        "output_port_ids": {
          "type": "array", "minItems": 1, "maxItems": 256, "uniqueItems": true,
          "items": { "$ref": "#/$defs/identifier" }
        },
        "summary": { "$ref": "#/$defs/text" }
      }
    },
    "transform": {
      "type": "object", "additionalProperties": false,
      "required": [
        "outcome", "inputs", "operations", "outputs", "code_anchors", "failures",
        "first_live_e2e", "non_goals"
      ],
      "properties": {
        "outcome": { "$ref": "#/$defs/text" },
        "inputs": {
          "type": "array", "maxItems": 256, "uniqueItems": true,
          "items": { "$ref": "#/$defs/inputPort" }
        },
        "operations": {
          "type": "array", "maxItems": 256, "uniqueItems": true,
          "items": { "$ref": "#/$defs/operation" }
        },
        "outputs": {
          "type": "array", "maxItems": 256, "uniqueItems": true,
          "items": { "$ref": "#/$defs/outputPort" }
        },
        "code_anchors": {
          "type": "array", "maxItems": 256, "uniqueItems": true,
          "items": { "$ref": "#/$defs/codeAnchor" }
        },
        "failures": {
          "type": "array", "maxItems": 128, "uniqueItems": true,
          "items": { "$ref": "#/$defs/text" }
        },
        "first_live_e2e": { "$ref": "#/$defs/text" },
        "non_goals": {
          "type": "array", "maxItems": 128, "uniqueItems": true,
          "items": { "$ref": "#/$defs/text" }
        }
      }
    },
    "externalContract": {
      "type": "object", "additionalProperties": false,
      "required": ["contract_id", "description", "contract"],
      "properties": {
        "contract_id": { "$ref": "#/$defs/identifier" },
        "description": { "$ref": "#/$defs/text" },
        "contract": { "$ref": "#/$defs/dataContract" }
      }
    },
    "graphTask": {
      "type": "object", "additionalProperties": false,
      "required": ["task_id", "applicability", "planned"],
      "properties": {
        "task_id": { "$ref": "#/$defs/identifier" },
        "applicability": { "const": "graph" },
        "planned": { "$ref": "#/$defs/transform" }
      }
    },
    "excludedTask": {
      "type": "object", "additionalProperties": false,
      "required": ["task_id", "applicability", "excluded_reason"],
      "properties": {
        "task_id": { "$ref": "#/$defs/identifier" },
        "applicability": { "const": "excluded" },
        "excluded_reason": { "$ref": "#/$defs/text" }
      }
    }
  }
}
