{
  "format": "forma-normative",
  "formatVersion": "1.0.0",
  "forma": {
    "major": 1,
    "latestMinor": 1
  },
  "algorithms": {
    "dialect": {
      "id": "forma.alg.ops",
      "version": "1.0.0",
      "description": "Typed, machine-readable algorithm operations for Forma normative execution.",
      "builtins": {
        "jsonMergePatch": "RFC 7396 JSON Merge Patch",
        "jsonPointer": "RFC 6901 JSON Pointer (get/set primitives)",
        "stringCompare": "Unicode codepoint lexicographic comparison",
        "clock": "Monotonic clock source for debounce windows",
        "regexMatch": "Deterministic regular expression match (ECMAScript-compatible subset; no flags)"
      },
      "ops": [
        "append",
        "break",
        "call",
        "callBuiltin",
        "deleteKey",
        "filter",
        "findFirst",
        "forEach",
        "forEachEntry",
        "if",
        "return",
        "setKey",
        "setVar",
        "sort",
        "switch"
      ],
      "opDocs": {
        "setVar": "Assign variable name := eval(value).",
        "setKey": "Set object[key] := eval(value) (creates key if missing).",
        "deleteKey": "Delete object[key] if present.",
        "append": "Append eval(value) to array.",
        "if": "Branch on eval(cond) boolean.",
        "switch": "Select first case with case.value === eval(expr) (string compare).",
        "forEach": "Iterate array items in index order.",
        "forEachEntry": "Iterate object own-properties in ascending lexicographic key order (deterministic).",
        "filter": "Produce array of items where eval(cond) is true.",
        "findFirst": "Return first item where eval(cond) is true, else null.",
        "sort": "Sort array by key expressions; orders specify asc/desc per key.",
        "call": "Call another procedure by name with evaluated args.",
        "callBuiltin": "Call a named builtin (e.g. jsonPointer.get/set) with evaluated args.",
        "break": "Exit the nearest loop body.",
        "return": "Return eval(value) from the procedure."
      }
    },
    "exprDialect": {
      "id": "forma.alg.expr",
      "version": "1.0.0",
      "description": "Expression AST used inside algorithm ops.",
      "kinds": [
        "object",
        "array",
        "const",
        "var",
        "get",
        "has",
        "typeIs",
        "eq",
        "gt",
        "len",
        "any",
        "and",
        "or",
        "not",
        "coalesce",
        "neg",
        "toString"
      ],
      "kindDocs": {
        "const": "Literal JSON value.",
        "var": "Variable reference by name.",
        "get": "Property lookup: eval(object)[eval(key)].",
        "has": "Property existence: key in object.",
        "typeIs": "Type predicate over eval(value): boolean|number|string|null|array|object.",
        "object": "Object constructor: properties is a map of string -> expression.",
        "array": "Array constructor: items is an array of expressions.",
        "coalesce": "Return first non-null, non-undefined value from values.",
        "eq": "Strict equality over JSON primitives/structures.",
        "and": "All values truthy.",
        "or": "Any value truthy.",
        "not": "Boolean negation.",
        "len": "Length of array or string; non-array/string => 0.",
        "gt": "Numeric greater-than; non-numeric => false.",
        "any": "Quantifier: true if any array element satisfies cond when bound to itemVar.",
        "neg": "Numeric negation.",
        "toString": "Convert value to string deterministically."
      }
    },
    "procedures": {
      "jsonMergePatch": {
        "id": "ALG.JSON_MERGE_PATCH",
        "summary": "RFC 7396 JSON Merge Patch",
        "params": [
          {"name": "target", "type": "json"},
          {"name": "patch", "type": "json"}
        ],
        "returns": [{"name": "result", "type": "json"}],
        "ops": [
          {
            "op": "if",
            "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "patch"}, "type": "object"}},
            "then": [{"op": "return", "value": {"kind": "var", "name": "patch"}}]
          },
          {
            "op": "if",
            "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "target"}, "type": "object"}},
            "then": [{"op": "setVar", "name": "target", "value": {"kind": "const", "value": {}}}]
          },
          {
            "op": "forEachEntry",
            "object": {"kind": "var", "name": "patch"},
            "keyVar": "k",
            "valueVar": "v",
            "body": [
              {
                "op": "if",
                "cond": {"kind": "eq", "left": {"kind": "var", "name": "v"}, "right": {"kind": "const", "value": null}},
                "then": [{"op": "deleteKey", "object": {"kind": "var", "name": "target"}, "key": {"kind": "var", "name": "k"}}],
                "else": [
                  {
                    "op": "call",
                    "proc": "jsonMergePatch",
                    "args": {
                      "target": {"kind": "get", "object": {"kind": "var", "name": "target"}, "key": {"kind": "var", "name": "k"}},
                      "patch": {"kind": "var", "name": "v"}
                    },
                    "assignTo": "merged"
                  },
                  {
                    "op": "setKey",
                    "object": {"kind": "var", "name": "target"},
                    "key": {"kind": "var", "name": "k"},
                    "value": {"kind": "var", "name": "merged"}
                  }
                ]
              }
            ]
          },
          {"op": "return", "value": {"kind": "var", "name": "target"}}
        ]
      },
      "aiValueEval": {
        "id": "ALG.AI.VALUE_EVAL",
        "summary": "Evaluate a predicate value expression (supports {'ref': <JSON Pointer>})",
        "params": [
          {"name": "value", "type": "json"},
          {"name": "ctx", "type": "object"}
        ],
        "returns": [{"name": "out", "type": "json"}],
        "ops": [
          {
            "op": "if",
            "cond": {
              "kind": "and",
              "values": [
                {"kind": "typeIs", "value": {"kind": "var", "name": "value"}, "type": "object"},
                {"kind": "has", "object": {"kind": "var", "name": "value"}, "key": {"kind": "const", "value": "ref"}},
                {"kind": "typeIs", "value": {"kind": "get", "object": {"kind": "var", "name": "value"}, "key": {"kind": "const", "value": "ref"}}, "type": "string"}
              ]
            },
            "then": [
              {
                "op": "callBuiltin",
                "builtin": "jsonPointer.get",
                "args": {
                  "doc": {"kind": "var", "name": "ctx"},
                  "pointer": {"kind": "get", "object": {"kind": "var", "name": "value"}, "key": {"kind": "const", "value": "ref"}}
                },
                "assignTo": "out"
              },
              {"op": "return", "value": {"kind": "var", "name": "out"}}
            ]
          },
          {"op": "return", "value": {"kind": "var", "name": "value"}}
        ],
        "notes": [
          "Value refs are resolved against a stable context object (ctx), typically {state, event}.",
          "If the ref pointer is invalid or does not resolve, the value is null."
        ]
      },
      "aiPredicateEval": {
        "id": "ALG.AI.PREDICATE_EVAL",
        "summary": "Deterministic predicate evaluation for the AI-native deterministic profile",
        "params": [
          {"name": "predicate", "type": "json"},
          {"name": "ctx", "type": "object"}
        ],
        "returns": [{"name": "ok", "type": "boolean"}],
        "ops": [
          {
            "op": "if",
            "cond": {"kind": "typeIs", "value": {"kind": "var", "name": "predicate"}, "type": "boolean"},
            "then": [{"op": "return", "value": {"kind": "var", "name": "predicate"}}]
          },
          {
            "op": "if",
            "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "predicate"}, "type": "object"}},
            "then": [{"op": "return", "value": {"kind": "const", "value": false}}]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "all"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "all"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "setVar", "name": "ok", "value": {"kind": "const", "value": true}},
              {"op": "forEach", "array": {"kind": "var", "name": "arr"}, "itemVar": "p", "body": [
                {"op": "call", "proc": "aiPredicateEval", "args": {"predicate": {"kind": "var", "name": "p"}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "pOk"},
                {"op": "if", "cond": {"kind": "eq", "left": {"kind": "var", "name": "pOk"}, "right": {"kind": "const", "value": false}}, "then": [
                  {"op": "setVar", "name": "ok", "value": {"kind": "const", "value": false}},
                  {"op": "break"}
                ]}
              ]},
              {"op": "return", "value": {"kind": "var", "name": "ok"}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "any"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "any"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "setVar", "name": "ok", "value": {"kind": "const", "value": false}},
              {"op": "forEach", "array": {"kind": "var", "name": "arr"}, "itemVar": "p", "body": [
                {"op": "call", "proc": "aiPredicateEval", "args": {"predicate": {"kind": "var", "name": "p"}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "pOk"},
                {"op": "if", "cond": {"kind": "eq", "left": {"kind": "var", "name": "pOk"}, "right": {"kind": "const", "value": true}}, "then": [
                  {"op": "setVar", "name": "ok", "value": {"kind": "const", "value": true}},
                  {"op": "break"}
                ]}
              ]},
              {"op": "return", "value": {"kind": "var", "name": "ok"}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "not"}},
            "then": [
              {"op": "call", "proc": "aiPredicateEval", "args": {"predicate": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "not"}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "pOk"},
              {"op": "return", "value": {"kind": "not", "value": {"kind": "var", "name": "pOk"}}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "exists"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "exists"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "setVar", "name": "ok", "value": {"kind": "const", "value": false}},
              {"op": "forEach", "array": {"kind": "var", "name": "arr"}, "itemVar": "v", "body": [
                {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "var", "name": "v"}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "vv"},
                {"op": "if", "cond": {"kind": "not", "value": {"kind": "eq", "left": {"kind": "var", "name": "vv"}, "right": {"kind": "const", "value": null}}}, "then": [
                  {"op": "setVar", "name": "ok", "value": {"kind": "const", "value": true}},
                  {"op": "break"}
                ]}
              ]},
              {"op": "return", "value": {"kind": "var", "name": "ok"}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "eq"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "eq"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 0}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "first"},
              {"op": "setVar", "name": "ok", "value": {"kind": "const", "value": true}},
              {"op": "forEach", "array": {"kind": "var", "name": "arr"}, "itemVar": "v", "body": [
                {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "var", "name": "v"}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "vv"},
                {"op": "if", "cond": {"kind": "not", "value": {"kind": "eq", "left": {"kind": "var", "name": "vv"}, "right": {"kind": "var", "name": "first"}}}, "then": [
                  {"op": "setVar", "name": "ok", "value": {"kind": "const", "value": false}},
                  {"op": "break"}
                ]}
              ]},
              {"op": "return", "value": {"kind": "var", "name": "ok"}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "ne"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "ne"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "eq", "left": {"kind": "len", "value": {"kind": "var", "name": "arr"}}, "right": {"kind": "const", "value": 2}}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 0}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "a"},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 1}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "b"},
              {"op": "return", "value": {"kind": "not", "value": {"kind": "eq", "left": {"kind": "var", "name": "a"}, "right": {"kind": "var", "name": "b"}}}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "gt"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "gt"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "eq", "left": {"kind": "len", "value": {"kind": "var", "name": "arr"}}, "right": {"kind": "const", "value": 2}}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 0}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "a"},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 1}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "b"},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "and", "values": [{"kind": "typeIs", "value": {"kind": "var", "name": "a"}, "type": "number"}, {"kind": "typeIs", "value": {"kind": "var", "name": "b"}, "type": "number"}]}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "return", "value": {"kind": "gt", "left": {"kind": "var", "name": "a"}, "right": {"kind": "var", "name": "b"}}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "gte"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "gte"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "eq", "left": {"kind": "len", "value": {"kind": "var", "name": "arr"}}, "right": {"kind": "const", "value": 2}}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 0}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "a"},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 1}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "b"},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "and", "values": [{"kind": "typeIs", "value": {"kind": "var", "name": "a"}, "type": "number"}, {"kind": "typeIs", "value": {"kind": "var", "name": "b"}, "type": "number"}]}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "return", "value": {"kind": "or", "values": [{"kind": "gt", "left": {"kind": "var", "name": "a"}, "right": {"kind": "var", "name": "b"}}, {"kind": "eq", "left": {"kind": "var", "name": "a"}, "right": {"kind": "var", "name": "b"}}]}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "lt"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "lt"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "eq", "left": {"kind": "len", "value": {"kind": "var", "name": "arr"}}, "right": {"kind": "const", "value": 2}}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 0}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "a"},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 1}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "b"},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "and", "values": [{"kind": "typeIs", "value": {"kind": "var", "name": "a"}, "type": "number"}, {"kind": "typeIs", "value": {"kind": "var", "name": "b"}, "type": "number"}]}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "return", "value": {"kind": "gt", "left": {"kind": "var", "name": "b"}, "right": {"kind": "var", "name": "a"}}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "lte"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "lte"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "eq", "left": {"kind": "len", "value": {"kind": "var", "name": "arr"}}, "right": {"kind": "const", "value": 2}}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 0}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "a"},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 1}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "b"},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "and", "values": [{"kind": "typeIs", "value": {"kind": "var", "name": "a"}, "type": "number"}, {"kind": "typeIs", "value": {"kind": "var", "name": "b"}, "type": "number"}]}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "return", "value": {"kind": "or", "values": [{"kind": "gt", "left": {"kind": "var", "name": "b"}, "right": {"kind": "var", "name": "a"}}, {"kind": "eq", "left": {"kind": "var", "name": "a"}, "right": {"kind": "var", "name": "b"}}]}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "matches"}},
            "then": [
              {"op": "setVar", "name": "arr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "matches"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "arr"}, "type": "array"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "eq", "left": {"kind": "len", "value": {"kind": "var", "name": "arr"}}, "right": {"kind": "const", "value": 2}}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 0}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "s"},
              {"op": "call", "proc": "aiValueEval", "args": {"value": {"kind": "get", "object": {"kind": "var", "name": "arr"}, "key": {"kind": "const", "value": 1}}, "ctx": {"kind": "var", "name": "ctx"}}, "assignTo": "re"},
              {"op": "if", "cond": {"kind": "and", "values": [{"kind": "typeIs", "value": {"kind": "var", "name": "s"}, "type": "string"}, {"kind": "typeIs", "value": {"kind": "var", "name": "re"}, "type": "string"}]}, "then": [
                {"op": "callBuiltin", "builtin": "regexMatch", "args": {"value": {"kind": "var", "name": "s"}, "pattern": {"kind": "var", "name": "re"}}, "assignTo": "ok"},
                {"op": "return", "value": {"kind": "var", "name": "ok"}}
              ]},
              {"op": "return", "value": {"kind": "const", "value": false}}
            ]
          },

          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "ref"}},
            "then": [
              {"op": "setVar", "name": "ptr", "value": {"kind": "get", "object": {"kind": "var", "name": "predicate"}, "key": {"kind": "const", "value": "ref"}}},
              {"op": "if", "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "ptr"}, "type": "string"}}, "then": [{"op": "return", "value": {"kind": "const", "value": false}}]},
              {"op": "callBuiltin", "builtin": "jsonPointer.get", "args": {"doc": {"kind": "var", "name": "ctx"}, "pointer": {"kind": "var", "name": "ptr"}}, "assignTo": "vv"},
              {"op": "return", "value": {"kind": "eq", "left": {"kind": "var", "name": "vv"}, "right": {"kind": "const", "value": true}}}
            ]
          },

          {"op": "return", "value": {"kind": "const", "value": false}}
        ],
        "notes": [
          "The evaluation context is a stable object with shape: {state: <stores>, event: {on, scope, name, detail}}.",
          "Value expressions inside predicates MAY use {'ref': <JSON Pointer>} to read from ctx.",
          "Binary operators ne/gt/gte/lt/lte/matches require exactly 2 arguments; other arities evaluate to false."
        ]
      },
      "aiHandlerSortKey": {
        "id": "ALG.AI.HANDLER_SORT_KEY",
        "summary": "Sort key for deterministic handler ordering",
        "params": [{"name": "handler", "type": "object"}],
        "returns": [{"name": "key", "type": "object"}],
        "ops": [
          {
            "op": "setVar",
            "name": "priority",
            "value": {
              "kind": "coalesce",
              "values": [
                {"kind": "get", "object": {"kind": "var", "name": "handler"}, "key": {"kind": "const", "value": "priority"}},
                {"kind": "const", "value": 0}
              ]
            }
          },
          {
            "op": "setVar",
            "name": "id",
            "value": {
              "kind": "coalesce",
              "values": [
                {"kind": "get", "object": {"kind": "var", "name": "handler"}, "key": {"kind": "const", "value": "id"}},
                {"kind": "const", "value": ""}
              ]
            }
          },
          {
            "op": "return",
            "value": {
              "kind": "object",
              "properties": {
                "priorityDesc": {"kind": "const", "value": true},
                "priority": {"kind": "var", "name": "priority"},
                "id": {"kind": "var", "name": "id"}
              }
            }
          }
        ],
        "notes": [
          "Consumers SHOULD treat missing or non-integer priority as 0.",
          "Consumers MUST order by priority desc, then id asc (lexicographic)."
        ]
      },
      "aiDispatchHandlers": {
        "id": "ALG.AI.DISPATCH_HANDLERS",
        "summary": "Select, order, and execute matching handlers for a trigger",
        "params": [
          {"name": "doc", "type": "object"},
          {"name": "state", "type": "object"},
          {"name": "on", "type": "string"},
          {"name": "scope", "type": "string"},
          {"name": "eventName", "type": "string|null"},
          {"name": "detail", "type": "json"}
        ],
        "returns": [
          {"name": "state", "type": "object"},
          {"name": "trace", "type": "array"},
          {"name": "issues", "type": "array"}
        ],
        "ops": [
          {"op": "setVar", "name": "issues", "value": {"kind": "const", "value": []}},
          {"op": "setVar", "name": "trace", "value": {"kind": "const", "value": []}},
          {
            "op": "setVar",
            "name": "ctx",
            "value": {
              "kind": "object",
              "properties": {
                "state": {"kind": "var", "name": "state"},
                "event": {
                  "kind": "object",
                  "properties": {
                    "on": {"kind": "var", "name": "on"},
                    "scope": {"kind": "var", "name": "scope"},
                    "name": {"kind": "var", "name": "eventName"},
                    "detail": {"kind": "var", "name": "detail"}
                  }
                }
              }
            }
          },
          {"op": "setVar", "name": "ai", "value": {"kind": "get", "object": {"kind": "get", "object": {"kind": "var", "name": "doc"}, "key": {"kind": "const", "value": "extensions"}}, "key": {"kind": "const", "value": "ai_native"}}},
          {
            "op": "setVar",
            "name": "handlers",
            "value": {
              "kind": "coalesce",
              "values": [
                {"kind": "get", "object": {"kind": "var", "name": "ai"}, "key": {"kind": "const", "value": "eventHandlers"}},
                {"kind": "const", "value": []}
              ]
            }
          },
          {
            "op": "filter",
            "array": {"kind": "var", "name": "handlers"},
            "itemVar": "h",
            "cond": {
              "kind": "and",
              "values": [
                {"kind": "eq", "left": {"kind": "get", "object": {"kind": "var", "name": "h"}, "key": {"kind": "const", "value": "on"}}, "right": {"kind": "var", "name": "on"}},
                {"kind": "eq", "left": {"kind": "get", "object": {"kind": "var", "name": "h"}, "key": {"kind": "const", "value": "scope"}}, "right": {"kind": "var", "name": "scope"}}
              ]
            },
            "assignTo": "matching"
          },
          {
            "op": "if",
            "cond": {"kind": "eq", "left": {"kind": "var", "name": "on"}, "right": {"kind": "const", "value": "event"}},
            "then": [
              {
                "op": "filter",
                "array": {"kind": "var", "name": "matching"},
                "itemVar": "h",
                "cond": {"kind": "eq", "left": {"kind": "get", "object": {"kind": "var", "name": "h"}, "key": {"kind": "const", "value": "event"}}, "right": {"kind": "var", "name": "eventName"}},
                "assignTo": "matching"
              }
            ]
          },
          {
            "op": "setVar",
            "name": "matching2",
            "value": {"kind": "const", "value": []}
          },
          {
            "op": "forEach",
            "array": {"kind": "var", "name": "matching"},
            "itemVar": "h",
            "body": [
              {"op": "setVar", "name": "whenOk", "value": {"kind": "const", "value": true}},
              {
                "op": "if",
                "cond": {"kind": "has", "object": {"kind": "var", "name": "h"}, "key": {"kind": "const", "value": "when"}},
                "then": [
                  {
                    "op": "call",
                    "proc": "aiPredicateEval",
                    "args": {"predicate": {"kind": "get", "object": {"kind": "var", "name": "h"}, "key": {"kind": "const", "value": "when"}}, "ctx": {"kind": "var", "name": "ctx"}},
                    "assignTo": "whenOk"
                  }
                ]
              },
              {
                "op": "if",
                "cond": {"kind": "eq", "left": {"kind": "var", "name": "whenOk"}, "right": {"kind": "const", "value": true}},
                "then": [
                  {"op": "append", "array": {"kind": "var", "name": "matching2"}, "value": {"kind": "var", "name": "h"}}
                ]
              }
            ]
          },
          {"op": "setVar", "name": "matching", "value": {"kind": "var", "name": "matching2"}},
          {
            "op": "if",
            "cond": {"kind": "and", "values": [
              {"kind": "gt", "left": {"kind": "len", "value": {"kind": "var", "name": "matching"}}, "right": {"kind": "const", "value": 1}},
              {"kind": "any", "array": {"kind": "var", "name": "matching"}, "itemVar": "h", "cond": {"kind": "not", "value": {"kind": "has", "object": {"kind": "var", "name": "h"}, "key": {"kind": "const", "value": "id"}}}}
            ]},
            "then": [
              {"op": "append", "array": {"kind": "var", "name": "issues"}, "value": {"kind": "const", "value": {"code": "ai_handler_order_ambiguous"}}}
            ]
          },
          {
            "op": "sort",
            "array": {"kind": "var", "name": "matching"},
            "by": [
              {
                "kind": "coalesce",
                "values": [
                  {"kind": "get", "object": {"kind": "var", "name": "$item"}, "key": {"kind": "const", "value": "priority"}},
                  {"kind": "const", "value": 0}
                ]
              },
              {
                "kind": "coalesce",
                "values": [
                  {"kind": "get", "object": {"kind": "var", "name": "$item"}, "key": {"kind": "const", "value": "id"}},
                  {"kind": "const", "value": ""}
                ]
              }
            ],
            "orders": ["desc", "asc"],
            "assignTo": "ordered"
          },
          {
            "op": "forEach",
            "array": {"kind": "var", "name": "ordered"},
            "itemVar": "h",
            "body": [
              {
                "op": "call",
                "proc": "aiRunActionList",
                "args": {
                  "doc": {"kind": "var", "name": "doc"},
                  "state": {"kind": "var", "name": "state"},
                  "actionIds": {"kind": "get", "object": {"kind": "var", "name": "h"}, "key": {"kind": "const", "value": "do"}}
                },
                "assignTo": "state"
              }
            ]
          },
          {
            "op": "return",
            "value": {
              "kind": "object",
              "properties": {
                "state": {"kind": "var", "name": "state"},
                "trace": {"kind": "var", "name": "trace"},
                "issues": {"kind": "var", "name": "issues"}
              }
            }
          }
        ],
        "notes": [
          "This procedure is intentionally strict about determinism; consumers may extend predicate support but MUST define it normatively to keep traces stable."
        ]
      },
      "aiRunActionList": {
        "id": "ALG.AI.RUN_ACTION_LIST",
        "summary": "Execute a list of action ids in order, including continuations",
        "params": [
          {"name": "doc", "type": "object"},
          {"name": "state", "type": "object"},
          {"name": "actionIds", "type": "array"}
        ],
        "returns": [{"name": "state", "type": "object"}],
        "ops": [
          {
            "op": "forEach",
            "array": {"kind": "var", "name": "actionIds"},
            "itemVar": "aid",
            "body": [
              {
                "op": "call",
                "proc": "aiRunAction",
                "args": {"doc": {"kind": "var", "name": "doc"}, "state": {"kind": "var", "name": "state"}, "actionId": {"kind": "var", "name": "aid"}},
                "assignTo": "out"
              },
              {"op": "setVar", "name": "state", "value": {"kind": "get", "object": {"kind": "var", "name": "out"}, "key": {"kind": "const", "value": "state"}}},
              {
                "op": "if",
                "cond": {"kind": "eq", "left": {"kind": "get", "object": {"kind": "var", "name": "out"}, "key": {"kind": "const", "value": "status"}}, "right": {"kind": "const", "value": "error"}},
                "then": [
                  {
                    "op": "if",
                    "cond": {"kind": "not", "value": {"kind": "get", "object": {"kind": "var", "name": "out"}, "key": {"kind": "const", "value": "hasCatch"}}},
                    "then": [{"op": "break"}]
                  }
                ]
              }
            ]
          },
          {"op": "return", "value": {"kind": "var", "name": "state"}}
        ]
      },
      "aiRunAction": {
        "id": "ALG.AI.RUN_ACTION",
        "summary": "Execute a single action and its continuations deterministically",
        "params": [
          {"name": "doc", "type": "object"},
          {"name": "state", "type": "object"},
          {"name": "actionId", "type": "string"}
        ],
        "returns": [
          {"name": "out", "type": "object"}
        ],
        "ops": [
          {"op": "setVar", "name": "ai", "value": {"kind": "get", "object": {"kind": "get", "object": {"kind": "var", "name": "doc"}, "key": {"kind": "const", "value": "extensions"}}, "key": {"kind": "const", "value": "ai_native"}}},
          {"op": "setVar", "name": "actions", "value": {"kind": "get", "object": {"kind": "var", "name": "ai"}, "key": {"kind": "const", "value": "actions"}}},
          {"op": "setVar", "name": "action", "value": {"kind": "get", "object": {"kind": "var", "name": "actions"}, "key": {"kind": "var", "name": "actionId"}}},
          {
            "op": "if",
            "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "action"}, "type": "object"}},
            "then": [
              {
                "op": "return",
                "value": {
                  "kind": "object",
                  "properties": {
                    "status": {"kind": "const", "value": "error"},
                    "error": {"kind": "const", "value": "unknown_action"},
                    "hasCatch": {"kind": "const", "value": false},
                    "state": {"kind": "var", "name": "state"}
                  }
                }
              }
            ]
          },
          {"op": "setVar", "name": "type", "value": {"kind": "get", "object": {"kind": "var", "name": "action"}, "key": {"kind": "const", "value": "type"}}},
          {"op": "setVar", "name": "then", "value": {"kind": "get", "object": {"kind": "var", "name": "action"}, "key": {"kind": "const", "value": "then"}}},
          {"op": "setVar", "name": "catch", "value": {"kind": "get", "object": {"kind": "var", "name": "action"}, "key": {"kind": "const", "value": "catch"}}},
          {"op": "setVar", "name": "finally", "value": {"kind": "get", "object": {"kind": "var", "name": "action"}, "key": {"kind": "const", "value": "finally"}}},
          {"op": "setVar", "name": "hasCatch", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "catch"}, "type": "array"}},
          {
            "op": "switch",
            "expr": {"kind": "var", "name": "type"},
            "cases": [
              {
                "value": "setState",
                "ops": [
                  {"op": "setVar", "name": "payload", "value": {"kind": "get", "object": {"kind": "var", "name": "action"}, "key": {"kind": "const", "value": "payload"}}},
                  {"op": "setVar", "name": "store", "value": {"kind": "coalesce", "values": [
                    {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "store"}},
                    {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "state"}}
                  ]}},
                  {"op": "setVar", "name": "mode", "value": {"kind": "coalesce", "values": [
                    {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "mode"}},
                    {"kind": "const", "value": "merge"}
                  ]}},
                  {"op": "setVar", "name": "patch", "value": {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "patch"}}},
                  {
                    "op": "if",
                    "cond": {"kind": "eq", "left": {"kind": "var", "name": "mode"}, "right": {"kind": "const", "value": "replace"}},
                    "then": [
                      {"op": "setKey", "object": {"kind": "var", "name": "state"}, "key": {"kind": "var", "name": "store"}, "value": {"kind": "var", "name": "patch"}}
                    ],
                    "else": [
                      {
                        "op": "call",
                        "proc": "jsonMergePatch",
                        "args": {"target": {"kind": "get", "object": {"kind": "var", "name": "state"}, "key": {"kind": "var", "name": "store"}}, "patch": {"kind": "var", "name": "patch"}},
                        "assignTo": "merged"
                      },
                      {"op": "setKey", "object": {"kind": "var", "name": "state"}, "key": {"kind": "var", "name": "store"}, "value": {"kind": "var", "name": "merged"}}
                    ]
                  },
                  {"op": "setVar", "name": "status", "value": {"kind": "const", "value": "ok"}}
                ]
              },
              {
                "value": "emit",
                "ops": [
                  {"op": "setVar", "name": "payload", "value": {"kind": "get", "object": {"kind": "var", "name": "action"}, "key": {"kind": "const", "value": "payload"}}},
                  {"op": "setVar", "name": "event", "value": {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "event"}}},
                  {"op": "setVar", "name": "detail", "value": {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "detail"}}},
                  {"op": "setVar", "name": "target", "value": {"kind": "get", "object": {"kind": "var", "name": "action"}, "key": {"kind": "const", "value": "target"}}},
                  {
                    "op": "call",
                    "proc": "aiDispatchHandlers",
                    "args": {"doc": {"kind": "var", "name": "doc"}, "state": {"kind": "var", "name": "state"}, "on": {"kind": "const", "value": "event"}, "scope": {"kind": "var", "name": "target"}, "eventName": {"kind": "var", "name": "event"}, "detail": {"kind": "var", "name": "detail"}},
                    "assignTo": "dispatchOut"
                  },
                  {"op": "setVar", "name": "state", "value": {"kind": "get", "object": {"kind": "var", "name": "dispatchOut"}, "key": {"kind": "const", "value": "state"}}},
                  {"op": "setVar", "name": "status", "value": {"kind": "const", "value": "ok"}}
                ]
              },
              {
                "value": "invoke",
                "ops": [
                  {"op": "call", "proc": "aiInvoke", "args": {"doc": {"kind": "var", "name": "doc"}, "state": {"kind": "var", "name": "state"}, "action": {"kind": "var", "name": "action"}}, "assignTo": "inv"},
                  {"op": "setVar", "name": "state", "value": {"kind": "get", "object": {"kind": "var", "name": "inv"}, "key": {"kind": "const", "value": "state"}}},
                  {"op": "setVar", "name": "status", "value": {"kind": "get", "object": {"kind": "var", "name": "inv"}, "key": {"kind": "const", "value": "status"}}}
                ]
              }
            ],
            "default": [
              {"op": "setVar", "name": "status", "value": {"kind": "const", "value": "ok"}}
            ]
          },
          {
            "op": "if",
            "cond": {"kind": "and", "values": [{"kind": "eq", "left": {"kind": "var", "name": "status"}, "right": {"kind": "const", "value": "ok"}}, {"kind": "typeIs", "value": {"kind": "var", "name": "then"}, "type": "array"}]},
            "then": [{"op": "call", "proc": "aiRunActionList", "args": {"doc": {"kind": "var", "name": "doc"}, "state": {"kind": "var", "name": "state"}, "actionIds": {"kind": "var", "name": "then"}}, "assignTo": "state"}]
          },
          {
            "op": "if",
            "cond": {"kind": "and", "values": [{"kind": "eq", "left": {"kind": "var", "name": "status"}, "right": {"kind": "const", "value": "error"}}, {"kind": "typeIs", "value": {"kind": "var", "name": "catch"}, "type": "array"}]},
            "then": [{"op": "call", "proc": "aiRunActionList", "args": {"doc": {"kind": "var", "name": "doc"}, "state": {"kind": "var", "name": "state"}, "actionIds": {"kind": "var", "name": "catch"}}, "assignTo": "state"}]
          },
          {
            "op": "if",
            "cond": {"kind": "typeIs", "value": {"kind": "var", "name": "finally"}, "type": "array"},
            "then": [{"op": "call", "proc": "aiRunActionList", "args": {"doc": {"kind": "var", "name": "doc"}, "state": {"kind": "var", "name": "state"}, "actionIds": {"kind": "var", "name": "finally"}}, "assignTo": "state"}]
          },
          {
            "op": "return",
            "value": {
              "kind": "object",
              "properties": {
                "status": {"kind": "var", "name": "status"},
                "hasCatch": {"kind": "var", "name": "hasCatch"},
                "state": {"kind": "var", "name": "state"}
              }
            }
          }
        ]
      },
      "aiInvoke": {
        "id": "ALG.AI.INVOKE",
        "summary": "Resolve invoke args and execute using fixtures; bind outputs to state",
        "params": [
          {"name": "doc", "type": "object"},
          {"name": "state", "type": "object"},
          {"name": "action", "type": "object"}
        ],
        "returns": [{"name": "out", "type": "object"}],
        "ops": [
          {"op": "setVar", "name": "payload", "value": {"kind": "get", "object": {"kind": "var", "name": "action"}, "key": {"kind": "const", "value": "payload"}}},
          {"op": "setVar", "name": "capId", "value": {"kind": "get", "object": {"kind": "var", "name": "action"}, "key": {"kind": "const", "value": "target"}}},
          {"op": "setVar", "name": "fixtureId", "value": {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "fixtureId"}}},
          {"op": "setVar", "name": "args", "value": {"kind": "const", "value": {}}},
          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "argsFromState"}},
            "then": [
              {"op": "setVar", "name": "afs", "value": {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "argsFromState"}}},
              {"op": "setVar", "name": "store", "value": {"kind": "get", "object": {"kind": "var", "name": "afs"}, "key": {"kind": "const", "value": "store"}}},
              {"op": "setVar", "name": "ptr", "value": {"kind": "coalesce", "values": [{"kind": "get", "object": {"kind": "var", "name": "afs"}, "key": {"kind": "const", "value": "pointer"}}, {"kind": "const", "value": ""}]}},
              {"op": "callBuiltin", "builtin": "jsonPointer.get", "args": {"doc": {"kind": "get", "object": {"kind": "var", "name": "state"}, "key": {"kind": "var", "name": "store"}}, "pointer": {"kind": "var", "name": "ptr"}}, "assignTo": "base"},
              {"op": "if", "cond": {"kind": "typeIs", "value": {"kind": "var", "name": "base"}, "type": "object"}, "then": [{"op": "setVar", "name": "args", "value": {"kind": "var", "name": "base"}}]}
            ]
          },
          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "args"}},
            "then": [
              {"op": "setVar", "name": "direct", "value": {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "args"}}},
              {
                "op": "if",
                "cond": {"kind": "typeIs", "value": {"kind": "var", "name": "direct"}, "type": "object"},
                "then": [
                  {
                    "op": "forEachEntry",
                    "object": {"kind": "var", "name": "direct"},
                    "keyVar": "k",
                    "valueVar": "v",
                    "body": [
                      {"op": "setKey", "object": {"kind": "var", "name": "args"}, "key": {"kind": "var", "name": "k"}, "value": {"kind": "var", "name": "v"}}
                    ]
                  }
                ]
              }
            ]
          },
          {"op": "call", "proc": "aiInvokeFixture", "args": {"doc": {"kind": "var", "name": "doc"}, "capabilityId": {"kind": "var", "name": "capId"}, "fixtureId": {"kind": "var", "name": "fixtureId"}, "args": {"kind": "var", "name": "args"}}, "assignTo": "fx"},
          {"op": "setVar", "name": "status", "value": {"kind": "get", "object": {"kind": "var", "name": "fx"}, "key": {"kind": "const", "value": "status"}}},
          {"op": "setVar", "name": "value", "value": {"kind": "get", "object": {"kind": "var", "name": "fx"}, "key": {"kind": "const", "value": "value"}}},
          {
            "op": "if",
            "cond": {"kind": "eq", "left": {"kind": "var", "name": "status"}, "right": {"kind": "const", "value": "ok"}},
            "then": [
              {"op": "if", "cond": {"kind": "has", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "bindResultTo"}}, "then": [
                {"op": "setVar", "name": "b", "value": {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "bindResultTo"}}},
                {"op": "callBuiltin", "builtin": "jsonPointer.set", "args": {"container": {"kind": "get", "object": {"kind": "var", "name": "state"}, "key": {"kind": "get", "object": {"kind": "var", "name": "b"}, "key": {"kind": "const", "value": "store"}}}, "pointer": {"kind": "get", "object": {"kind": "var", "name": "b"}, "key": {"kind": "const", "value": "pointer"}}, "value": {"kind": "var", "name": "value"}}}
              ]}
            ],
            "else": [
              {"op": "if", "cond": {"kind": "has", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "bindErrorTo"}}, "then": [
                {"op": "setVar", "name": "b", "value": {"kind": "get", "object": {"kind": "var", "name": "payload"}, "key": {"kind": "const", "value": "bindErrorTo"}}},
                {"op": "callBuiltin", "builtin": "jsonPointer.set", "args": {"container": {"kind": "get", "object": {"kind": "var", "name": "state"}, "key": {"kind": "get", "object": {"kind": "var", "name": "b"}, "key": {"kind": "const", "value": "store"}}}, "pointer": {"kind": "get", "object": {"kind": "var", "name": "b"}, "key": {"kind": "const", "value": "pointer"}}, "value": {"kind": "var", "name": "value"}}}
              ]}
            ]
          },
          {
            "op": "return",
            "value": {
              "kind": "object",
              "properties": {
                "status": {"kind": "var", "name": "status"},
                "state": {"kind": "var", "name": "state"}
              }
            }
          }
        ]
      },
      "aiInvokeFixture": {
        "id": "ALG.AI.INVOKE_FIXTURE",
        "summary": "Fixture selection for deterministic replay",
        "params": [
          {"name": "doc", "type": "object"},
          {"name": "capabilityId", "type": "string"},
          {"name": "fixtureId", "type": "string"},
          {"name": "args", "type": "object"}
        ],
        "returns": [{"name": "out", "type": "object"}],
        "ops": [
          {"op": "setVar", "name": "ai", "value": {"kind": "get", "object": {"kind": "get", "object": {"kind": "var", "name": "doc"}, "key": {"kind": "const", "value": "extensions"}}, "key": {"kind": "const", "value": "ai_native"}}},
          {"op": "setVar", "name": "caps", "value": {"kind": "get", "object": {"kind": "var", "name": "ai"}, "key": {"kind": "const", "value": "capabilities"}}},
          {"op": "setVar", "name": "cap", "value": {"kind": "get", "object": {"kind": "var", "name": "caps"}, "key": {"kind": "var", "name": "capabilityId"}}},
          {"op": "setVar", "name": "fixtures", "value": {"kind": "get", "object": {"kind": "var", "name": "cap"}, "key": {"kind": "const", "value": "fixtures"}}},
          {
            "op": "findFirst",
            "array": {"kind": "var", "name": "fixtures"},
            "itemVar": "f",
            "cond": {"kind": "eq", "left": {"kind": "get", "object": {"kind": "var", "name": "f"}, "key": {"kind": "const", "value": "id"}}, "right": {"kind": "var", "name": "fixtureId"}},
            "assignTo": "fixture"
          },
          {
            "op": "if",
            "cond": {"kind": "not", "value": {"kind": "typeIs", "value": {"kind": "var", "name": "fixture"}, "type": "object"}},
            "then": [{"op": "return", "value": {"kind": "const", "value": {"status": "error", "value": {"code": "ai_fixture_missing"}}}}]
          },
          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "fixture"}, "key": {"kind": "const", "value": "result"}},
            "then": [
              {
                "op": "return",
                "value": {
                  "kind": "object",
                  "properties": {
                    "status": {"kind": "const", "value": "ok"},
                    "value": {"kind": "get", "object": {"kind": "var", "name": "fixture"}, "key": {"kind": "const", "value": "result"}}
                  }
                }
              }
            ]
          },
          {
            "op": "if",
            "cond": {"kind": "has", "object": {"kind": "var", "name": "fixture"}, "key": {"kind": "const", "value": "error"}},
            "then": [
              {
                "op": "return",
                "value": {
                  "kind": "object",
                  "properties": {
                    "status": {"kind": "const", "value": "error"},
                    "value": {"kind": "get", "object": {"kind": "var", "name": "fixture"}, "key": {"kind": "const", "value": "error"}}
                  }
                }
              }
            ]
          },
          {"op": "return", "value": {"kind": "const", "value": {"status": "error", "value": {"code": "fixture_invalid"}}}}
        ]
      },
      "defaultTraversalOrder": {
        "id": "ALG.DEFAULT_TRAVERSAL_ORDER",
        "summary": "Default focus/reading order via pre-order traversal",
        "params": [
          {"name": "nodesById", "type": "object"},
          {"name": "rootIds", "type": "array"}
        ],
        "returns": [{"name": "scopes", "type": "array"}],
        "ops": [
          {"op": "setVar", "name": "out", "value": {"kind": "const", "value": []}},
          {
            "op": "forEach",
            "array": {"kind": "var", "name": "rootIds"},
            "itemVar": "rid",
            "body": [
              {"op": "call", "proc": "visitNodePreOrder", "args": {"nodesById": {"kind": "var", "name": "nodesById"}, "nodeId": {"kind": "var", "name": "rid"}, "out": {"kind": "var", "name": "out"}}, "assignTo": "out"}
            ]
          },
          {"op": "return", "value": {"kind": "var", "name": "out"}}
        ]
      },
      "visitNodePreOrder": {
        "id": "ALG.VISIT_NODE_PREORDER",
        "summary": "Helper for defaultTraversalOrder",
        "params": [
          {"name": "nodesById", "type": "object"},
          {"name": "nodeId", "type": "string"},
          {"name": "out", "type": "array"}
        ],
        "returns": [{"name": "out", "type": "array"}],
        "ops": [
          {"op": "append", "array": {"kind": "var", "name": "out"}, "value": {"kind": "var", "name": "nodeId"}},
          {"op": "setVar", "name": "node", "value": {"kind": "get", "object": {"kind": "var", "name": "nodesById"}, "key": {"kind": "var", "name": "nodeId"}}},
          {"op": "setVar", "name": "children", "value": {"kind": "get", "object": {"kind": "var", "name": "node"}, "key": {"kind": "const", "value": "children"}}},
          {
            "op": "if",
            "cond": {"kind": "typeIs", "value": {"kind": "var", "name": "children"}, "type": "array"},
            "then": [
              {
                "op": "forEach",
                "array": {"kind": "var", "name": "children"},
                "itemVar": "cid",
                "body": [
                  {"op": "call", "proc": "visitNodePreOrder", "args": {"nodesById": {"kind": "var", "name": "nodesById"}, "nodeId": {"kind": "var", "name": "cid"}, "out": {"kind": "var", "name": "out"}}, "assignTo": "out"}
                ]
              }
            ]
          },
          {"op": "return", "value": {"kind": "var", "name": "out"}}
        ]
      }
    }
  },
  "profiles": {
    "core": {
      "id": "forma.v1.core",
      "requirements": [
        {
          "id": "CORE.VERSION",
          "level": "MUST",
          "actor": "producer",
          "statement": "Emit a valid document-level version string matching 1.x.y."
        },
        {
          "id": "CORE.NODE_ID_UNIQUE",
          "level": "MUST",
          "actor": "producer",
          "statement": "Node id values MUST be unique within a document."
        },
        {
          "id": "CORE.GRAPH_PARENT_CHILD_CONSISTENT",
          "level": "MUST",
          "actor": "producer",
          "statement": "parentId and children MUST be consistent and form an acyclic forest; child ordering is authoritative."
        },
        {
          "id": "CORE.EXT_IGNORE_UNKNOWN",
          "level": "MUST",
          "actor": "consumer",
          "statement": "Ignore unknown extension fields safely."
        }
      ]
    },
    "aiNativeDeterministic": {
      "id": "forma.v1.ai_native.deterministic",
      "requirements": [
        {
          "id": "AIH.SELECT_MATCHING_HANDLERS",
          "level": "MUST",
          "actor": "consumer",
          "statement": "Select handlers where on matches the input event type, scope matches the target scope, and when (if present) evaluates truthy."
        },
        {
          "id": "AIH.ORDER_HANDLERS",
          "level": "MUST",
          "actor": "consumer",
          "statement": "Order multiple matching handlers by priority (desc), then by id (lexicographic). If id is missing for any handler in a multi-match group, ordering is ambiguous and MUST be treated as a document issue."
        },
        {
          "id": "AIH.DO_ORDER",
          "level": "MUST",
          "actor": "consumer",
          "statement": "Execute eventHandlers[].do action ids in order; then/catch/finally execute immediately after the action completes, before continuing the surrounding list."
        },
        {
          "id": "AI.STATE_INIT",
          "level": "MUST",
          "actor": "consumer",
          "statement": "Initialize state stores from extensions.ai_native.state.*.initial when present."
        },
        {
          "id": "AI.CONTRACTS_NO_REMOTE_REF",
          "level": "MUST",
          "actor": "producer",
          "statement": "Capability/event contract schemas MUST be self-contained JSON Schema fragments (no remote $ref) for deterministic replay."
        }
      ]
    }
  },
  "registries": {
    "issueCodes": [
      "layout_fallback",
      "token_missing",
      "token_ref_invalid",
      "role_ambiguous",
      "role_inferred",
      "instance_unresolved",
      "unsupported_feature",
      "text_missing",
      "asset_missing",
      "ai_handler_order_ambiguous",
      "ai_fixture_missing",
      "ai_contract_remote_ref",
      "source_incomplete"
    ],
    "aiNativeActionTypes": [
      "navigate",
      "openModal",
      "closeModal",
      "submit",
      "setState",
      "emit",
      "invoke"
    ],
    "aiNativeHandlerOn": [
      "click",
      "press",
      "submit",
      "change",
      "focus",
      "blur",
      "open",
      "close",
      "event"
    ],
    "roles": [
      "container",
      "text",
      "image",
      "icon",
      "button",
      "link",
      "input",
      "textarea",
      "select",
      "checkbox",
      "radio",
      "switch",
      "form",
      "card",
      "list",
      "listitem",
      "nav",
      "menu",
      "menuitem",
      "tablist",
      "tab",
      "tabpanel",
      "dialog",
      "modal",
      "toast"
    ]
  }
}
