{
  "description": "A composable program fragment.\nAgents can validate fragments independently and compose them into a module.",
  "type": "object",
  "properties": {
    "kind": {
      "type": "string",
      "const": "fragment"
    },
    "id": {
      "type": "string"
    },
    "provides": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "requires": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "imports": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Import"
      }
    },
    "definitions": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Definition"
      }
    },
    "blame": {
      "description": "Structured blame / provenance annotation.\nTracks which agent produced a module or function, when, and with what confidence.",
      "$ref": "#/definitions/BlameAnnotation"
    }
  },
  "required": [
    "definitions",
    "id",
    "imports",
    "kind",
    "provides",
    "requires"
  ],
  "definitions": {
    "Import": {
      "description": "Import names from another module.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "import"
        },
        "id": {
          "type": "string"
        },
        "module": {
          "type": "string"
        },
        "names": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "types": {
          "$ref": "#/definitions/Record%3Cstring%2CTypeExpr%3E"
        }
      },
      "required": [
        "id",
        "kind",
        "module",
        "names"
      ]
    },
    "Record<string,TypeExpr>": {
      "type": "object"
    },
    "Definition": {
      "anyOf": [
        {
          "$ref": "#/definitions/FunctionDef"
        },
        {
          "$ref": "#/definitions/TypeDef"
        },
        {
          "$ref": "#/definitions/RecordDef"
        },
        {
          "$ref": "#/definitions/EnumDef"
        },
        {
          "$ref": "#/definitions/ConstDef"
        },
        {
          "$ref": "#/definitions/ToolDef"
        }
      ],
      "properties": {
        "kind": {
          "type": "string",
          "enum": [
            "fn",
            "type",
            "record",
            "enum",
            "const",
            "tool"
          ]
        }
      }
    },
    "FunctionDef": {
      "description": "Function definition with effects, contracts, and body.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "fn"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "params": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Param"
          }
        },
        "effects": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/ConcreteEffect"
          }
        },
        "returnType": {
          "description": "Union of all type expressions in Edict.",
          "anyOf": [
            {
              "$ref": "#/definitions/BasicType"
            },
            {
              "$ref": "#/definitions/ArrayType"
            },
            {
              "$ref": "#/definitions/OptionType"
            },
            {
              "$ref": "#/definitions/ResultType"
            },
            {
              "$ref": "#/definitions/UnitType"
            },
            {
              "$ref": "#/definitions/RefinedType"
            },
            {
              "$ref": "#/definitions/FunctionType"
            },
            {
              "$ref": "#/definitions/NamedType"
            },
            {
              "$ref": "#/definitions/TupleType"
            },
            {
              "$ref": "#/definitions/ConfidenceType"
            },
            {
              "$ref": "#/definitions/ProvenanceType"
            },
            {
              "$ref": "#/definitions/CapabilityType"
            },
            {
              "$ref": "#/definitions/FreshnessType"
            },
            {
              "$ref": "#/definitions/TypeVarType"
            }
          ]
        },
        "contracts": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Contract"
          }
        },
        "constraints": {
          "description": "Bounds on token complexity and program size to prevent runaway agents.",
          "$ref": "#/definitions/ComplexityConstraints"
        },
        "intent": {
          "description": "Structured intent — what the function is trying to accomplish.\nAgents use this for re-synthesis, blame tracking, and specification diffing.\nInvariants reuse Expression and SemanticAssertion types for automated matching.",
          "$ref": "#/definitions/IntentDeclaration"
        },
        "approval": {
          "description": "Approval gate on a function — requires explicit host approval before execution.\nPropagates through call chains: if a callee requires approval, the caller must too.",
          "$ref": "#/definitions/ApprovalGate"
        },
        "blame": {
          "description": "Structured blame / provenance annotation.\nTracks which agent produced a module or function, when, and with what confidence.",
          "$ref": "#/definitions/BlameAnnotation"
        },
        "body": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        }
      },
      "required": [
        "body",
        "contracts",
        "effects",
        "id",
        "kind",
        "name",
        "params"
      ]
    },
    "Param": {
      "description": "Function parameter.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "param"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "type": {
          "description": "Union of all type expressions in Edict.",
          "anyOf": [
            {
              "$ref": "#/definitions/BasicType"
            },
            {
              "$ref": "#/definitions/ArrayType"
            },
            {
              "$ref": "#/definitions/OptionType"
            },
            {
              "$ref": "#/definitions/ResultType"
            },
            {
              "$ref": "#/definitions/UnitType"
            },
            {
              "$ref": "#/definitions/RefinedType"
            },
            {
              "$ref": "#/definitions/FunctionType"
            },
            {
              "$ref": "#/definitions/NamedType"
            },
            {
              "$ref": "#/definitions/TupleType"
            },
            {
              "$ref": "#/definitions/ConfidenceType"
            },
            {
              "$ref": "#/definitions/ProvenanceType"
            },
            {
              "$ref": "#/definitions/CapabilityType"
            },
            {
              "$ref": "#/definitions/FreshnessType"
            },
            {
              "$ref": "#/definitions/TypeVarType"
            }
          ]
        }
      },
      "required": [
        "id",
        "kind",
        "name"
      ]
    },
    "BasicType": {
      "description": "Primitive types.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "basic"
        },
        "name": {
          "enum": [
            "Bool",
            "Float",
            "Int",
            "Int64",
            "String"
          ],
          "type": "string"
        }
      },
      "required": [
        "kind",
        "name"
      ]
    },
    "ArrayType": {
      "description": "Homogeneous array type.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "array"
        },
        "element": {
          "$ref": "#/definitions/TypeExpr"
        }
      },
      "required": [
        "element",
        "kind"
      ]
    },
    "TypeExpr": {
      "description": "Union of all type expressions in Edict.",
      "anyOf": [
        {
          "$ref": "#/definitions/BasicType"
        },
        {
          "$ref": "#/definitions/ArrayType"
        },
        {
          "$ref": "#/definitions/OptionType"
        },
        {
          "$ref": "#/definitions/ResultType"
        },
        {
          "$ref": "#/definitions/UnitType"
        },
        {
          "$ref": "#/definitions/RefinedType"
        },
        {
          "$ref": "#/definitions/FunctionType"
        },
        {
          "$ref": "#/definitions/NamedType"
        },
        {
          "$ref": "#/definitions/TupleType"
        },
        {
          "$ref": "#/definitions/ConfidenceType"
        },
        {
          "$ref": "#/definitions/ProvenanceType"
        },
        {
          "$ref": "#/definitions/CapabilityType"
        },
        {
          "$ref": "#/definitions/FreshnessType"
        },
        {
          "$ref": "#/definitions/TypeVarType"
        }
      ],
      "properties": {
        "kind": {
          "type": "string",
          "enum": [
            "basic",
            "array",
            "option",
            "result",
            "unit_type",
            "refined",
            "fn_type",
            "named",
            "tuple",
            "confidence",
            "provenance",
            "capability",
            "fresh",
            "type_var"
          ]
        }
      }
    },
    "OptionType": {
      "description": "Optional value — None or Some(T).",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "option"
        },
        "inner": {
          "$ref": "#/definitions/TypeExpr"
        }
      },
      "required": [
        "inner",
        "kind"
      ]
    },
    "ResultType": {
      "description": "Result type for error handling. Interacts with the \"fails\" effect.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "result"
        },
        "ok": {
          "$ref": "#/definitions/TypeExpr"
        },
        "err": {
          "$ref": "#/definitions/TypeExpr"
        }
      },
      "required": [
        "err",
        "kind",
        "ok"
      ]
    },
    "UnitType": {
      "description": "Semantic unit type — compile-time enforcement, zero runtime cost.\nPrevents mixing incompatible units (e.g., currency<usd> + temp<celsius>).",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "unit_type"
        },
        "base": {
          "enum": [
            "Float",
            "Int"
          ],
          "type": "string"
        },
        "unit": {
          "type": "string"
        }
      },
      "required": [
        "base",
        "kind",
        "unit"
      ]
    },
    "RefinedType": {
      "description": "Refinement type — base type + logical predicate verified by Z3 (Phase 4).\nExample: { v: Int | v > 0 } is a positive integer.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "refined"
        },
        "id": {
          "type": "string"
        },
        "base": {
          "$ref": "#/definitions/TypeExpr"
        },
        "variable": {
          "type": "string"
        },
        "predicate": {
          "$ref": "#/definitions/Expression"
        }
      },
      "required": [
        "base",
        "id",
        "kind",
        "predicate",
        "variable"
      ]
    },
    "Expression": {
      "anyOf": [
        {
          "$ref": "#/definitions/Literal"
        },
        {
          "$ref": "#/definitions/Identifier"
        },
        {
          "$ref": "#/definitions/BinaryOp"
        },
        {
          "$ref": "#/definitions/UnaryOp"
        },
        {
          "$ref": "#/definitions/Call"
        },
        {
          "$ref": "#/definitions/IfExpr"
        },
        {
          "$ref": "#/definitions/LetExpr"
        },
        {
          "$ref": "#/definitions/MatchExpr"
        },
        {
          "$ref": "#/definitions/ArrayExpr"
        },
        {
          "$ref": "#/definitions/TupleExpr"
        },
        {
          "$ref": "#/definitions/RecordExpr"
        },
        {
          "$ref": "#/definitions/EnumConstructor"
        },
        {
          "$ref": "#/definitions/FieldAccess"
        },
        {
          "$ref": "#/definitions/LambdaExpr"
        },
        {
          "$ref": "#/definitions/BlockExpr"
        },
        {
          "$ref": "#/definitions/StringInterp"
        },
        {
          "$ref": "#/definitions/ForallExpr"
        },
        {
          "$ref": "#/definitions/ExistsExpr"
        },
        {
          "$ref": "#/definitions/ToolCallExpr"
        }
      ],
      "properties": {
        "kind": {
          "type": "string",
          "enum": [
            "literal",
            "ident",
            "binop",
            "unop",
            "call",
            "if",
            "let",
            "match",
            "array",
            "tuple_expr",
            "record_expr",
            "enum_constructor",
            "access",
            "lambda",
            "block",
            "string_interp",
            "forall",
            "exists",
            "tool_call"
          ]
        }
      }
    },
    "Literal": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "literal"
        },
        "id": {
          "type": "string"
        },
        "value": {
          "type": [
            "string",
            "number",
            "boolean"
          ]
        },
        "type": {
          "description": "Union of all type expressions in Edict.",
          "anyOf": [
            {
              "$ref": "#/definitions/BasicType"
            },
            {
              "$ref": "#/definitions/ArrayType"
            },
            {
              "$ref": "#/definitions/OptionType"
            },
            {
              "$ref": "#/definitions/ResultType"
            },
            {
              "$ref": "#/definitions/UnitType"
            },
            {
              "$ref": "#/definitions/RefinedType"
            },
            {
              "$ref": "#/definitions/FunctionType"
            },
            {
              "$ref": "#/definitions/NamedType"
            },
            {
              "$ref": "#/definitions/TupleType"
            },
            {
              "$ref": "#/definitions/ConfidenceType"
            },
            {
              "$ref": "#/definitions/ProvenanceType"
            },
            {
              "$ref": "#/definitions/CapabilityType"
            },
            {
              "$ref": "#/definitions/FreshnessType"
            },
            {
              "$ref": "#/definitions/TypeVarType"
            }
          ]
        }
      },
      "required": [
        "id",
        "kind",
        "value"
      ]
    },
    "FunctionType": {
      "description": "Function type — for higher-order functions and lambdas.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "fn_type"
        },
        "params": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/TypeExpr"
          }
        },
        "effects": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Effect"
          }
        },
        "returnType": {
          "$ref": "#/definitions/TypeExpr"
        }
      },
      "required": [
        "effects",
        "kind",
        "params",
        "returnType"
      ]
    },
    "Effect": {
      "description": "An effect is either a concrete effect literal or an effect variable.\nConcrete effects appear in function/tool definitions.\nEffect variables appear only in FunctionType (type annotations for callbacks).",
      "anyOf": [
        {
          "$ref": "#/definitions/EffectVariable"
        },
        {
          "enum": [
            "fails",
            "io",
            "pure",
            "reads",
            "writes"
          ],
          "type": "string"
        }
      ]
    },
    "EffectVariable": {
      "description": "Effect variable — a placeholder for an unknown set of effects.\nUsed in FunctionType to express effect polymorphism for higher-order functions.\nOnly valid in type annotations (fn_type), NOT in function/tool definitions.\nNames must be single uppercase ASCII letters (e.g., \"E\", \"F\").",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "effect_var"
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "name"
      ]
    },
    "NamedType": {
      "description": "Reference to a user-defined type (RecordDef or EnumDef) by name.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "named"
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "name"
      ]
    },
    "TupleType": {
      "description": "Fixed-size heterogeneous tuple.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "tuple"
        },
        "elements": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/TypeExpr"
          }
        }
      },
      "required": [
        "elements",
        "kind"
      ]
    },
    "ConfidenceType": {
      "description": "Confidence-typed value — tracks LLM uncertainty at the type level.\nErased after type checking (zero runtime cost). Structurally transparent:\nConfidence<T, 0.9> is assignable to/from T.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "confidence"
        },
        "base": {
          "$ref": "#/definitions/TypeExpr"
        },
        "confidence": {
          "type": "number"
        }
      },
      "required": [
        "base",
        "confidence",
        "kind"
      ]
    },
    "ProvenanceType": {
      "description": "Provenance-typed value — tracks data origin at the type level.\nErased after type checking (zero runtime cost). Structurally transparent:\nProvenance<T, \"api:x\"> is assignable to/from T.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "provenance"
        },
        "base": {
          "$ref": "#/definitions/TypeExpr"
        },
        "sources": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "base",
        "kind",
        "sources"
      ]
    },
    "CapabilityType": {
      "description": "Capability token — compile-time verified, unforgeable permission.\nNot a type wrapper (unlike confidence/provenance). Capabilities ARE the type.\nErased at codegen (zero runtime cost). The host mints them; agents cannot forge them.\nPermissions are hierarchical: \"net:smtp\" subsumes \"net:smtp:max_10\" via prefix matching.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "capability"
        },
        "permissions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "kind",
        "permissions"
      ]
    },
    "FreshnessType": {
      "description": "Freshness-typed value — tracks temporal validity at the type level.\nErased after type checking (zero runtime cost). Structurally transparent:\nFresh<T, \"5m\"> is assignable to/from T.\nmaxAge is a duration string: \"30s\", \"5m\", \"1h\", \"200ms\".",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "fresh"
        },
        "base": {
          "$ref": "#/definitions/TypeExpr"
        },
        "maxAge": {
          "type": "string"
        }
      },
      "required": [
        "base",
        "kind",
        "maxAge"
      ]
    },
    "TypeVarType": {
      "description": "Type variable — placeholder for a concrete type, resolved at call sites.\nUsed in polymorphic builtin signatures (e.g., array_get: Array<T> → T).\nCompile-time only — erased during type checking (unified with concrete types).\nNever appears in user-written ASTs; only in builtin type definitions.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "type_var"
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "name"
      ]
    },
    "Identifier": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "ident"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "id",
        "kind",
        "name"
      ]
    },
    "BinaryOp": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "binop"
        },
        "id": {
          "type": "string"
        },
        "op": {
          "$ref": "#/definitions/BinaryOperator"
        },
        "left": {
          "$ref": "#/definitions/Expression"
        },
        "right": {
          "$ref": "#/definitions/Expression"
        }
      },
      "required": [
        "id",
        "kind",
        "left",
        "op",
        "right"
      ]
    },
    "BinaryOperator": {
      "enum": [
        "!=",
        "%",
        "*",
        "+",
        "-",
        "/",
        "<",
        "<=",
        "==",
        ">",
        ">=",
        "and",
        "implies",
        "or"
      ],
      "type": "string"
    },
    "UnaryOp": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "unop"
        },
        "id": {
          "type": "string"
        },
        "op": {
          "$ref": "#/definitions/UnaryOperator"
        },
        "operand": {
          "$ref": "#/definitions/Expression"
        }
      },
      "required": [
        "id",
        "kind",
        "op",
        "operand"
      ]
    },
    "UnaryOperator": {
      "enum": [
        "-",
        "not"
      ],
      "type": "string"
    },
    "Call": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "call"
        },
        "id": {
          "type": "string"
        },
        "fn": {
          "$ref": "#/definitions/Expression"
        },
        "args": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        }
      },
      "required": [
        "args",
        "fn",
        "id",
        "kind"
      ]
    },
    "IfExpr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "if"
        },
        "id": {
          "type": "string"
        },
        "condition": {
          "$ref": "#/definitions/Expression"
        },
        "then": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        },
        "else": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        }
      },
      "required": [
        "condition",
        "id",
        "kind",
        "then"
      ]
    },
    "LetExpr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "let"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "type": {
          "description": "Union of all type expressions in Edict.",
          "anyOf": [
            {
              "$ref": "#/definitions/BasicType"
            },
            {
              "$ref": "#/definitions/ArrayType"
            },
            {
              "$ref": "#/definitions/OptionType"
            },
            {
              "$ref": "#/definitions/ResultType"
            },
            {
              "$ref": "#/definitions/UnitType"
            },
            {
              "$ref": "#/definitions/RefinedType"
            },
            {
              "$ref": "#/definitions/FunctionType"
            },
            {
              "$ref": "#/definitions/NamedType"
            },
            {
              "$ref": "#/definitions/TupleType"
            },
            {
              "$ref": "#/definitions/ConfidenceType"
            },
            {
              "$ref": "#/definitions/ProvenanceType"
            },
            {
              "$ref": "#/definitions/CapabilityType"
            },
            {
              "$ref": "#/definitions/FreshnessType"
            },
            {
              "$ref": "#/definitions/TypeVarType"
            }
          ]
        },
        "value": {
          "$ref": "#/definitions/Expression"
        }
      },
      "required": [
        "id",
        "kind",
        "name",
        "value"
      ]
    },
    "MatchExpr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "match"
        },
        "id": {
          "type": "string"
        },
        "target": {
          "$ref": "#/definitions/Expression"
        },
        "arms": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/MatchArm"
          }
        }
      },
      "required": [
        "arms",
        "id",
        "kind",
        "target"
      ]
    },
    "MatchArm": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "arm"
        },
        "id": {
          "type": "string"
        },
        "pattern": {
          "$ref": "#/definitions/Pattern"
        },
        "body": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        }
      },
      "required": [
        "body",
        "id",
        "kind",
        "pattern"
      ]
    },
    "Pattern": {
      "anyOf": [
        {
          "$ref": "#/definitions/LiteralPattern"
        },
        {
          "$ref": "#/definitions/WildcardPattern"
        },
        {
          "$ref": "#/definitions/BindingPattern"
        },
        {
          "$ref": "#/definitions/ConstructorPattern"
        }
      ],
      "properties": {
        "kind": {
          "type": "string",
          "enum": [
            "literal_pattern",
            "wildcard",
            "binding",
            "constructor"
          ]
        }
      }
    },
    "LiteralPattern": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "literal_pattern"
        },
        "value": {
          "type": [
            "string",
            "number",
            "boolean"
          ]
        }
      },
      "required": [
        "kind",
        "value"
      ]
    },
    "WildcardPattern": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "wildcard"
        }
      },
      "required": [
        "kind"
      ]
    },
    "BindingPattern": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "binding"
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "name"
      ]
    },
    "ConstructorPattern": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "constructor"
        },
        "name": {
          "type": "string"
        },
        "fields": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Pattern"
          }
        }
      },
      "required": [
        "fields",
        "kind",
        "name"
      ]
    },
    "ArrayExpr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "array"
        },
        "id": {
          "type": "string"
        },
        "elements": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        }
      },
      "required": [
        "elements",
        "id",
        "kind"
      ]
    },
    "TupleExpr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "tuple_expr"
        },
        "id": {
          "type": "string"
        },
        "elements": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        }
      },
      "required": [
        "elements",
        "id",
        "kind"
      ]
    },
    "RecordExpr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "record_expr"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "fields": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/FieldInit"
          }
        }
      },
      "required": [
        "fields",
        "id",
        "kind",
        "name"
      ]
    },
    "FieldInit": {
      "description": "Field initialization in record expressions and enum constructors.\nGives these inline objects a proper `kind` discriminator like every other AST node.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "field_init"
        },
        "name": {
          "type": "string"
        },
        "value": {
          "$ref": "#/definitions/Expression"
        }
      },
      "required": [
        "kind",
        "name",
        "value"
      ]
    },
    "EnumConstructor": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "enum_constructor"
        },
        "id": {
          "type": "string"
        },
        "enumName": {
          "type": "string"
        },
        "variant": {
          "type": "string"
        },
        "fields": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/FieldInit"
          }
        }
      },
      "required": [
        "enumName",
        "fields",
        "id",
        "kind",
        "variant"
      ]
    },
    "FieldAccess": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "access"
        },
        "id": {
          "type": "string"
        },
        "target": {
          "$ref": "#/definitions/Expression"
        },
        "field": {
          "type": "string"
        }
      },
      "required": [
        "field",
        "id",
        "kind",
        "target"
      ]
    },
    "LambdaExpr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "lambda"
        },
        "id": {
          "type": "string"
        },
        "params": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Param"
          }
        },
        "body": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        }
      },
      "required": [
        "body",
        "id",
        "kind",
        "params"
      ]
    },
    "BlockExpr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "block"
        },
        "id": {
          "type": "string"
        },
        "body": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        }
      },
      "required": [
        "body",
        "id",
        "kind"
      ]
    },
    "StringInterp": {
      "description": "String interpolation — desugars to string_concat chains at compile time.\nAll parts must evaluate to String.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "string_interp"
        },
        "id": {
          "type": "string"
        },
        "parts": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Expression"
          }
        }
      },
      "required": [
        "id",
        "kind",
        "parts"
      ]
    },
    "ForallExpr": {
      "description": "Universal quantifier — contract-only.\nforall variable in [range.from, range.to): body\nTranslates to Z3 ForAll. Body must evaluate to Bool.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "forall"
        },
        "id": {
          "type": "string"
        },
        "variable": {
          "type": "string"
        },
        "range": {
          "type": "object",
          "properties": {
            "from": {
              "$ref": "#/definitions/Expression"
            },
            "to": {
              "$ref": "#/definitions/Expression"
            }
          },
          "required": [
            "from",
            "to"
          ]
        },
        "body": {
          "$ref": "#/definitions/Expression"
        }
      },
      "required": [
        "body",
        "id",
        "kind",
        "range",
        "variable"
      ]
    },
    "ExistsExpr": {
      "description": "Existential quantifier — contract-only.\nexists variable in [range.from, range.to): body\nTranslates to Z3 Exists. Body must evaluate to Bool.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "exists"
        },
        "id": {
          "type": "string"
        },
        "variable": {
          "type": "string"
        },
        "range": {
          "type": "object",
          "properties": {
            "from": {
              "$ref": "#/definitions/Expression"
            },
            "to": {
              "$ref": "#/definitions/Expression"
            }
          },
          "required": [
            "from",
            "to"
          ]
        },
        "body": {
          "$ref": "#/definitions/Expression"
        }
      },
      "required": [
        "body",
        "id",
        "kind",
        "range",
        "variable"
      ]
    },
    "ToolCallExpr": {
      "description": "Tool call expression — invokes a declared tool by name.\nNamed args via FieldInit (same pattern as RecordExpr/EnumConstructor).\nAlways returns Result<T, String> where T is the tool's returnType.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "tool_call"
        },
        "id": {
          "type": "string"
        },
        "tool": {
          "type": "string"
        },
        "args": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/FieldInit"
          }
        },
        "timeout": {
          "type": "number"
        },
        "retryPolicy": {
          "description": "Retry policy for tool calls — how often and how to back off.",
          "$ref": "#/definitions/RetryPolicy"
        },
        "fallback": {
          "anyOf": [
            {
              "$ref": "#/definitions/Literal"
            },
            {
              "$ref": "#/definitions/Identifier"
            },
            {
              "$ref": "#/definitions/BinaryOp"
            },
            {
              "$ref": "#/definitions/UnaryOp"
            },
            {
              "$ref": "#/definitions/Call"
            },
            {
              "$ref": "#/definitions/IfExpr"
            },
            {
              "$ref": "#/definitions/LetExpr"
            },
            {
              "$ref": "#/definitions/MatchExpr"
            },
            {
              "$ref": "#/definitions/ArrayExpr"
            },
            {
              "$ref": "#/definitions/TupleExpr"
            },
            {
              "$ref": "#/definitions/RecordExpr"
            },
            {
              "$ref": "#/definitions/EnumConstructor"
            },
            {
              "$ref": "#/definitions/FieldAccess"
            },
            {
              "$ref": "#/definitions/LambdaExpr"
            },
            {
              "$ref": "#/definitions/BlockExpr"
            },
            {
              "$ref": "#/definitions/StringInterp"
            },
            {
              "$ref": "#/definitions/ForallExpr"
            },
            {
              "$ref": "#/definitions/ExistsExpr"
            },
            {
              "$ref": "#/definitions/ToolCallExpr"
            }
          ]
        }
      },
      "required": [
        "args",
        "id",
        "kind",
        "tool"
      ]
    },
    "RetryPolicy": {
      "description": "Retry policy for tool calls — how often and how to back off.",
      "type": "object",
      "properties": {
        "maxRetries": {
          "type": "number"
        },
        "backoff": {
          "$ref": "#/definitions/BackoffKind"
        }
      },
      "required": [
        "backoff",
        "maxRetries"
      ]
    },
    "BackoffKind": {
      "description": "Retry backoff strategy for tool calls.",
      "enum": [
        "exponential",
        "fixed",
        "linear"
      ],
      "type": "string"
    },
    "ConcreteEffect": {
      "description": "The 5 canonical concrete effect categories.\nA function definition's signature includes which concrete effects it may perform.",
      "enum": [
        "fails",
        "io",
        "pure",
        "reads",
        "writes"
      ],
      "type": "string"
    },
    "Contract": {
      "description": "Pre/post contract on a function.\nMust have exactly one of `condition` (manual expression) or `semantic` (pre-built assertion).\n`semantic` is only valid on `post` contracts (v1).",
      "type": "object",
      "properties": {
        "kind": {
          "enum": [
            "post",
            "pre"
          ],
          "type": "string"
        },
        "id": {
          "type": "string"
        },
        "condition": {
          "anyOf": [
            {
              "$ref": "#/definitions/Literal"
            },
            {
              "$ref": "#/definitions/Identifier"
            },
            {
              "$ref": "#/definitions/BinaryOp"
            },
            {
              "$ref": "#/definitions/UnaryOp"
            },
            {
              "$ref": "#/definitions/Call"
            },
            {
              "$ref": "#/definitions/IfExpr"
            },
            {
              "$ref": "#/definitions/LetExpr"
            },
            {
              "$ref": "#/definitions/MatchExpr"
            },
            {
              "$ref": "#/definitions/ArrayExpr"
            },
            {
              "$ref": "#/definitions/TupleExpr"
            },
            {
              "$ref": "#/definitions/RecordExpr"
            },
            {
              "$ref": "#/definitions/EnumConstructor"
            },
            {
              "$ref": "#/definitions/FieldAccess"
            },
            {
              "$ref": "#/definitions/LambdaExpr"
            },
            {
              "$ref": "#/definitions/BlockExpr"
            },
            {
              "$ref": "#/definitions/StringInterp"
            },
            {
              "$ref": "#/definitions/ForallExpr"
            },
            {
              "$ref": "#/definitions/ExistsExpr"
            },
            {
              "$ref": "#/definitions/ToolCallExpr"
            }
          ]
        },
        "semantic": {
          "description": "A pre-built semantic assertion that translates to a proven-correct Z3 encoding.\nAgents use these instead of manually writing Z3-verifiable expressions.",
          "$ref": "#/definitions/SemanticAssertion"
        }
      },
      "required": [
        "id",
        "kind"
      ]
    },
    "SemanticAssertion": {
      "description": "A pre-built semantic assertion that translates to a proven-correct Z3 encoding.\nAgents use these instead of manually writing Z3-verifiable expressions.",
      "type": "object",
      "properties": {
        "assertion": {
          "$ref": "#/definitions/SemanticAssertionKind"
        },
        "target": {
          "type": "string"
        },
        "args": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "assertion",
        "target"
      ]
    },
    "SemanticAssertionKind": {
      "description": "The 7 built-in semantic assertion kinds.",
      "enum": [
        "bounded",
        "length_preserved",
        "no_duplicates",
        "permutation_of",
        "sorted",
        "subset_of",
        "sum_preserved"
      ],
      "type": "string"
    },
    "ComplexityConstraints": {
      "description": "Bounds on token complexity and program size to prevent runaway agents.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "constraints"
        },
        "maxAstNodes": {
          "type": "number"
        },
        "maxCallDepth": {
          "type": "number"
        },
        "maxBranches": {
          "type": "number"
        }
      },
      "required": [
        "kind"
      ]
    },
    "IntentDeclaration": {
      "description": "Structured intent — what the function is trying to accomplish.\nAgents use this for re-synthesis, blame tracking, and specification diffing.\nInvariants reuse Expression and SemanticAssertion types for automated matching.",
      "type": "object",
      "properties": {
        "goal": {
          "type": "string"
        },
        "inputs": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "outputs": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "invariants": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/IntentInvariant"
          }
        }
      },
      "required": [
        "goal",
        "inputs",
        "invariants",
        "outputs"
      ]
    },
    "IntentInvariant": {
      "description": "A structured invariant that can be automatically matched against contracts.\nReuses existing Expression and SemanticAssertionKind — zero new vocabulary.",
      "anyOf": [
        {
          "$ref": "#/definitions/ExpressionInvariant"
        },
        {
          "$ref": "#/definitions/SemanticInvariant"
        }
      ],
      "properties": {
        "kind": {
          "type": "string",
          "enum": [
            "expression",
            "semantic"
          ]
        }
      }
    },
    "ExpressionInvariant": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "expression"
        },
        "expression": {
          "$ref": "#/definitions/Expression"
        }
      },
      "required": [
        "expression",
        "kind"
      ]
    },
    "SemanticInvariant": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "semantic"
        },
        "assertion": {
          "$ref": "#/definitions/SemanticAssertionKind"
        },
        "target": {
          "type": "string"
        },
        "args": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "assertion",
        "kind",
        "target"
      ]
    },
    "ApprovalGate": {
      "description": "Approval gate on a function — requires explicit host approval before execution.\nPropagates through call chains: if a callee requires approval, the caller must too.",
      "type": "object",
      "properties": {
        "required": {
          "type": "boolean"
        },
        "scope": {
          "$ref": "#/definitions/ApprovalScope"
        },
        "reason": {
          "type": "string"
        }
      },
      "required": [
        "reason",
        "required",
        "scope"
      ]
    },
    "ApprovalScope": {
      "description": "Approval scope controls how often approval must be re-obtained.",
      "enum": [
        "per_call",
        "per_module",
        "per_session"
      ],
      "type": "string"
    },
    "BlameAnnotation": {
      "description": "Structured blame / provenance annotation.\nTracks which agent produced a module or function, when, and with what confidence.",
      "type": "object",
      "properties": {
        "author": {
          "type": "string"
        },
        "generatedAt": {
          "type": "string"
        },
        "confidence": {
          "type": "number"
        },
        "sourcePrompt": {
          "type": "string"
        }
      },
      "required": [
        "author",
        "confidence",
        "generatedAt"
      ]
    },
    "TypeDef": {
      "description": "Type alias definition.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "type"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "definition": {
          "$ref": "#/definitions/TypeExpr"
        },
        "blame": {
          "description": "Structured blame / provenance annotation.\nTracks which agent produced a module or function, when, and with what confidence.",
          "$ref": "#/definitions/BlameAnnotation"
        }
      },
      "required": [
        "definition",
        "id",
        "kind",
        "name"
      ]
    },
    "RecordDef": {
      "description": "Record (struct) definition.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "record"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "fields": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/RecordField"
          }
        },
        "blame": {
          "description": "Structured blame / provenance annotation.\nTracks which agent produced a module or function, when, and with what confidence.",
          "$ref": "#/definitions/BlameAnnotation"
        }
      },
      "required": [
        "fields",
        "id",
        "kind",
        "name"
      ]
    },
    "RecordField": {
      "description": "A field in a record definition.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "field"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "type": {
          "$ref": "#/definitions/TypeExpr"
        },
        "defaultValue": {
          "anyOf": [
            {
              "$ref": "#/definitions/Literal"
            },
            {
              "$ref": "#/definitions/Identifier"
            },
            {
              "$ref": "#/definitions/BinaryOp"
            },
            {
              "$ref": "#/definitions/UnaryOp"
            },
            {
              "$ref": "#/definitions/Call"
            },
            {
              "$ref": "#/definitions/IfExpr"
            },
            {
              "$ref": "#/definitions/LetExpr"
            },
            {
              "$ref": "#/definitions/MatchExpr"
            },
            {
              "$ref": "#/definitions/ArrayExpr"
            },
            {
              "$ref": "#/definitions/TupleExpr"
            },
            {
              "$ref": "#/definitions/RecordExpr"
            },
            {
              "$ref": "#/definitions/EnumConstructor"
            },
            {
              "$ref": "#/definitions/FieldAccess"
            },
            {
              "$ref": "#/definitions/LambdaExpr"
            },
            {
              "$ref": "#/definitions/BlockExpr"
            },
            {
              "$ref": "#/definitions/StringInterp"
            },
            {
              "$ref": "#/definitions/ForallExpr"
            },
            {
              "$ref": "#/definitions/ExistsExpr"
            },
            {
              "$ref": "#/definitions/ToolCallExpr"
            }
          ]
        }
      },
      "required": [
        "id",
        "kind",
        "name",
        "type"
      ]
    },
    "EnumDef": {
      "description": "Enum (tagged union / sum type) definition.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "enum"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "variants": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/EnumVariant"
          }
        },
        "blame": {
          "description": "Structured blame / provenance annotation.\nTracks which agent produced a module or function, when, and with what confidence.",
          "$ref": "#/definitions/BlameAnnotation"
        }
      },
      "required": [
        "id",
        "kind",
        "name",
        "variants"
      ]
    },
    "EnumVariant": {
      "description": "A variant of an enum. Fields are empty for unit variants (e.g., None).",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "variant"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "fields": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/RecordField"
          }
        }
      },
      "required": [
        "fields",
        "id",
        "kind",
        "name"
      ]
    },
    "ConstDef": {
      "description": "Constant definition.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "const"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "type": {
          "$ref": "#/definitions/TypeExpr"
        },
        "value": {
          "$ref": "#/definitions/Expression"
        },
        "blame": {
          "description": "Structured blame / provenance annotation.\nTracks which agent produced a module or function, when, and with what confidence.",
          "$ref": "#/definitions/BlameAnnotation"
        }
      },
      "required": [
        "id",
        "kind",
        "name",
        "type",
        "value"
      ]
    },
    "ToolDef": {
      "description": "Tool definition — declares a named external tool with a typed interface.\nThe host provides the actual implementation at runtime.\nTool names are in scope like functions; tool_call expressions reference them by name.",
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "tool"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "uri": {
          "type": "string"
        },
        "params": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Param"
          }
        },
        "returnType": {
          "$ref": "#/definitions/TypeExpr"
        },
        "effects": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/ConcreteEffect"
          }
        },
        "blame": {
          "description": "Structured blame / provenance annotation.\nTracks which agent produced a module or function, when, and with what confidence.",
          "$ref": "#/definitions/BlameAnnotation"
        }
      },
      "required": [
        "effects",
        "id",
        "kind",
        "name",
        "params",
        "returnType",
        "uri"
      ]
    }
  },
  "$schema": "http://json-schema.org/draft-07/schema#"
}
