@probelabs/visor 0.1.181 → 0.1.182

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/defaults/code-talk.yaml +80 -14
  2. package/defaults/engineer.yaml +33 -15
  3. package/defaults/skills/code-explorer.yaml +5 -0
  4. package/dist/agent-protocol/a2a-frontend.d.ts +10 -0
  5. package/dist/agent-protocol/a2a-frontend.d.ts.map +1 -1
  6. package/dist/agent-protocol/task-evaluator.d.ts +52 -0
  7. package/dist/agent-protocol/task-evaluator.d.ts.map +1 -0
  8. package/dist/agent-protocol/task-store.d.ts +5 -3
  9. package/dist/agent-protocol/task-store.d.ts.map +1 -1
  10. package/dist/agent-protocol/tasks-cli-handler.d.ts.map +1 -1
  11. package/dist/agent-protocol/tasks-tui.d.ts +34 -0
  12. package/dist/agent-protocol/tasks-tui.d.ts.map +1 -0
  13. package/dist/agent-protocol/trace-serializer.d.ts +90 -0
  14. package/dist/agent-protocol/trace-serializer.d.ts.map +1 -0
  15. package/dist/agent-protocol/track-execution.d.ts +2 -0
  16. package/dist/agent-protocol/track-execution.d.ts.map +1 -1
  17. package/dist/cli-main.d.ts.map +1 -1
  18. package/dist/defaults/code-talk.yaml +80 -14
  19. package/dist/defaults/engineer.yaml +33 -15
  20. package/dist/defaults/skills/code-explorer.yaml +5 -0
  21. package/dist/docs/commands.md +57 -14
  22. package/dist/docs/configuration.md +2 -0
  23. package/dist/docs/guides/graceful-restart.md +178 -0
  24. package/dist/docs/observability.md +69 -0
  25. package/dist/docs/production-deployment.md +17 -0
  26. package/dist/email/polling-runner.d.ts +4 -0
  27. package/dist/email/polling-runner.d.ts.map +1 -1
  28. package/dist/generated/config-schema.d.ts +70 -6
  29. package/dist/generated/config-schema.d.ts.map +1 -1
  30. package/dist/generated/config-schema.json +73 -6
  31. package/dist/index.js +5006 -886
  32. package/dist/output/traces/{run-2026-03-17T13-58-29-402Z.ndjson → run-2026-03-18T19-02-50-465Z.ndjson} +84 -84
  33. package/dist/{traces/run-2026-03-17T13-59-10-403Z.ndjson → output/traces/run-2026-03-18T19-03-30-428Z.ndjson} +2037 -2037
  34. package/dist/providers/mcp-custom-sse-server.d.ts +4 -0
  35. package/dist/providers/mcp-custom-sse-server.d.ts.map +1 -1
  36. package/dist/runners/graceful-restart.d.ts +46 -0
  37. package/dist/runners/graceful-restart.d.ts.map +1 -0
  38. package/dist/runners/mcp-server-runner.d.ts +12 -0
  39. package/dist/runners/mcp-server-runner.d.ts.map +1 -1
  40. package/dist/runners/runner-factory.d.ts.map +1 -1
  41. package/dist/runners/runner-host.d.ts +12 -0
  42. package/dist/runners/runner-host.d.ts.map +1 -1
  43. package/dist/runners/runner.d.ts +12 -0
  44. package/dist/runners/runner.d.ts.map +1 -1
  45. package/dist/sdk/{a2a-frontend-IWOUJOIZ.mjs → a2a-frontend-4LP3MLTS.mjs} +47 -5
  46. package/dist/sdk/a2a-frontend-4LP3MLTS.mjs.map +1 -0
  47. package/dist/sdk/a2a-frontend-5J3UNFY4.mjs +1718 -0
  48. package/dist/sdk/a2a-frontend-5J3UNFY4.mjs.map +1 -0
  49. package/dist/sdk/{a2a-frontend-BDACLGMA.mjs → a2a-frontend-MU5EO2HZ.mjs} +35 -1
  50. package/dist/sdk/a2a-frontend-MU5EO2HZ.mjs.map +1 -0
  51. package/dist/sdk/{check-provider-registry-4YKTEDKF.mjs → check-provider-registry-MHXQGUNN.mjs} +7 -7
  52. package/dist/sdk/{check-provider-registry-4YFVBGYU.mjs → check-provider-registry-RRWCXSTG.mjs} +3 -3
  53. package/dist/sdk/{check-provider-registry-67ZLGDDQ.mjs → check-provider-registry-Y33CRFVD.mjs} +7 -7
  54. package/dist/sdk/{chunk-DGIH6EX3.mjs → chunk-4AXAVXG5.mjs} +151 -281
  55. package/dist/sdk/chunk-4AXAVXG5.mjs.map +1 -0
  56. package/dist/sdk/{chunk-VMVIM4JB.mjs → chunk-4I3TJ7UJ.mjs} +37 -7
  57. package/dist/sdk/chunk-4I3TJ7UJ.mjs.map +1 -0
  58. package/dist/sdk/{chunk-VXC2XNQJ.mjs → chunk-5J3DNRF7.mjs} +3 -3
  59. package/dist/sdk/{chunk-7YZSSO4X.mjs → chunk-6DPPP7LD.mjs} +10 -10
  60. package/dist/sdk/chunk-7ERVRLDV.mjs +296 -0
  61. package/dist/sdk/chunk-7ERVRLDV.mjs.map +1 -0
  62. package/dist/sdk/{chunk-4DVP6KVC.mjs → chunk-7Z2WHX2J.mjs} +71 -30
  63. package/dist/sdk/chunk-7Z2WHX2J.mjs.map +1 -0
  64. package/dist/sdk/chunk-ANUT54HW.mjs +1502 -0
  65. package/dist/sdk/chunk-ANUT54HW.mjs.map +1 -0
  66. package/dist/sdk/{chunk-J73GEFPT.mjs → chunk-DHETLQIX.mjs} +2 -2
  67. package/dist/sdk/{chunk-QGBASDYP.mjs → chunk-JCOSKBMP.mjs} +71 -30
  68. package/dist/sdk/chunk-JCOSKBMP.mjs.map +1 -0
  69. package/dist/sdk/chunk-MK7ONH47.mjs +739 -0
  70. package/dist/sdk/chunk-MK7ONH47.mjs.map +1 -0
  71. package/dist/sdk/chunk-QXT47ZHR.mjs +390 -0
  72. package/dist/sdk/chunk-QXT47ZHR.mjs.map +1 -0
  73. package/dist/sdk/chunk-V75NEIXL.mjs +296 -0
  74. package/dist/sdk/chunk-V75NEIXL.mjs.map +1 -0
  75. package/dist/sdk/chunk-ZOF5QT6U.mjs +5943 -0
  76. package/dist/sdk/chunk-ZOF5QT6U.mjs.map +1 -0
  77. package/dist/sdk/{config-TSA5FUOM.mjs → config-2STD74CJ.mjs} +2 -2
  78. package/dist/sdk/config-JE4HKTWW.mjs +16 -0
  79. package/dist/sdk/{failure-condition-evaluator-HTPB5FYW.mjs → failure-condition-evaluator-5DZYMCGW.mjs} +4 -4
  80. package/dist/sdk/failure-condition-evaluator-R6DCDJAV.mjs +18 -0
  81. package/dist/sdk/{github-frontend-3SDFCCKI.mjs → github-frontend-3PSCKPAJ.mjs} +4 -4
  82. package/dist/sdk/github-frontend-L3F5JXPJ.mjs +1394 -0
  83. package/dist/sdk/github-frontend-L3F5JXPJ.mjs.map +1 -0
  84. package/dist/sdk/{host-QE4L7UXE.mjs → host-54CHV2LW.mjs} +3 -3
  85. package/dist/sdk/{host-VBBSLUWG.mjs → host-WAU6CT42.mjs} +3 -3
  86. package/dist/sdk/{host-CVH2CSHM.mjs → host-X5ZZCEWN.mjs} +2 -2
  87. package/dist/sdk/{routing-YVMTKFDZ.mjs → routing-CVQT4KHX.mjs} +5 -5
  88. package/dist/sdk/routing-EBAE5SSO.mjs +26 -0
  89. package/dist/sdk/{schedule-tool-Z5VG67JK.mjs → schedule-tool-POY3CDZL.mjs} +7 -7
  90. package/dist/sdk/{schedule-tool-ADUXTCY7.mjs → schedule-tool-R2OAATUS.mjs} +7 -7
  91. package/dist/sdk/{schedule-tool-ZMX3Y7LF.mjs → schedule-tool-Z6QYL2B3.mjs} +3 -3
  92. package/dist/sdk/{schedule-tool-handler-N7UNABOA.mjs → schedule-tool-handler-J4NUETJ6.mjs} +3 -3
  93. package/dist/sdk/{schedule-tool-handler-PCERK6ZZ.mjs → schedule-tool-handler-JMAKHPI7.mjs} +7 -7
  94. package/dist/sdk/{schedule-tool-handler-QOJVFRB4.mjs → schedule-tool-handler-MWFUIQKR.mjs} +7 -7
  95. package/dist/sdk/sdk.d.mts +33 -0
  96. package/dist/sdk/sdk.d.ts +33 -0
  97. package/dist/sdk/sdk.js +2058 -342
  98. package/dist/sdk/sdk.js.map +1 -1
  99. package/dist/sdk/sdk.mjs +6 -6
  100. package/dist/sdk/task-evaluator-HLNXKKVV.mjs +1278 -0
  101. package/dist/sdk/task-evaluator-HLNXKKVV.mjs.map +1 -0
  102. package/dist/sdk/{trace-helpers-KXDOJWBL.mjs → trace-helpers-HL5FBX65.mjs} +3 -3
  103. package/dist/sdk/trace-helpers-WJXYVV4S.mjs +29 -0
  104. package/dist/sdk/trace-helpers-WJXYVV4S.mjs.map +1 -0
  105. package/dist/sdk/trace-reader-ZY77OFNM.mjs +266 -0
  106. package/dist/sdk/trace-reader-ZY77OFNM.mjs.map +1 -0
  107. package/dist/sdk/track-execution-MKIQXP2C.mjs +136 -0
  108. package/dist/sdk/track-execution-MKIQXP2C.mjs.map +1 -0
  109. package/dist/sdk/track-execution-YUXQ6WQH.mjs +136 -0
  110. package/dist/sdk/track-execution-YUXQ6WQH.mjs.map +1 -0
  111. package/dist/sdk/{workflow-check-provider-NTHC5ZBF.mjs → workflow-check-provider-SE5I7EMA.mjs} +7 -7
  112. package/dist/sdk/workflow-check-provider-SE5I7EMA.mjs.map +1 -0
  113. package/dist/sdk/{workflow-check-provider-SRIMWKLQ.mjs → workflow-check-provider-VKYGI5GK.mjs} +3 -3
  114. package/dist/sdk/workflow-check-provider-VKYGI5GK.mjs.map +1 -0
  115. package/dist/sdk/{workflow-check-provider-CJXW2Z4F.mjs → workflow-check-provider-YDGZRI3Z.mjs} +7 -7
  116. package/dist/sdk/workflow-check-provider-YDGZRI3Z.mjs.map +1 -0
  117. package/dist/slack/socket-runner.d.ts +12 -0
  118. package/dist/slack/socket-runner.d.ts.map +1 -1
  119. package/dist/teams/webhook-runner.d.ts +4 -0
  120. package/dist/teams/webhook-runner.d.ts.map +1 -1
  121. package/dist/telegram/polling-runner.d.ts +2 -0
  122. package/dist/telegram/polling-runner.d.ts.map +1 -1
  123. package/dist/traces/{run-2026-03-17T13-58-29-402Z.ndjson → run-2026-03-18T19-02-50-465Z.ndjson} +84 -84
  124. package/dist/{output/traces/run-2026-03-17T13-59-10-403Z.ndjson → traces/run-2026-03-18T19-03-30-428Z.ndjson} +2037 -2037
  125. package/dist/types/config.d.ts +33 -0
  126. package/dist/types/config.d.ts.map +1 -1
  127. package/dist/whatsapp/webhook-runner.d.ts +4 -0
  128. package/dist/whatsapp/webhook-runner.d.ts.map +1 -1
  129. package/package.json +2 -2
  130. package/dist/sdk/a2a-frontend-BDACLGMA.mjs.map +0 -1
  131. package/dist/sdk/a2a-frontend-IWOUJOIZ.mjs.map +0 -1
  132. package/dist/sdk/chunk-4DVP6KVC.mjs.map +0 -1
  133. package/dist/sdk/chunk-DGIH6EX3.mjs.map +0 -1
  134. package/dist/sdk/chunk-QGBASDYP.mjs.map +0 -1
  135. package/dist/sdk/chunk-VMVIM4JB.mjs.map +0 -1
  136. /package/dist/sdk/{check-provider-registry-4YFVBGYU.mjs.map → check-provider-registry-MHXQGUNN.mjs.map} +0 -0
  137. /package/dist/sdk/{check-provider-registry-4YKTEDKF.mjs.map → check-provider-registry-RRWCXSTG.mjs.map} +0 -0
  138. /package/dist/sdk/{check-provider-registry-67ZLGDDQ.mjs.map → check-provider-registry-Y33CRFVD.mjs.map} +0 -0
  139. /package/dist/sdk/{chunk-VXC2XNQJ.mjs.map → chunk-5J3DNRF7.mjs.map} +0 -0
  140. /package/dist/sdk/{chunk-7YZSSO4X.mjs.map → chunk-6DPPP7LD.mjs.map} +0 -0
  141. /package/dist/sdk/{chunk-J73GEFPT.mjs.map → chunk-DHETLQIX.mjs.map} +0 -0
  142. /package/dist/sdk/{config-TSA5FUOM.mjs.map → config-2STD74CJ.mjs.map} +0 -0
  143. /package/dist/sdk/{failure-condition-evaluator-HTPB5FYW.mjs.map → config-JE4HKTWW.mjs.map} +0 -0
  144. /package/dist/sdk/{routing-YVMTKFDZ.mjs.map → failure-condition-evaluator-5DZYMCGW.mjs.map} +0 -0
  145. /package/dist/sdk/{schedule-tool-ADUXTCY7.mjs.map → failure-condition-evaluator-R6DCDJAV.mjs.map} +0 -0
  146. /package/dist/sdk/{github-frontend-3SDFCCKI.mjs.map → github-frontend-3PSCKPAJ.mjs.map} +0 -0
  147. /package/dist/sdk/{host-CVH2CSHM.mjs.map → host-54CHV2LW.mjs.map} +0 -0
  148. /package/dist/sdk/{host-QE4L7UXE.mjs.map → host-WAU6CT42.mjs.map} +0 -0
  149. /package/dist/sdk/{host-VBBSLUWG.mjs.map → host-X5ZZCEWN.mjs.map} +0 -0
  150. /package/dist/sdk/{schedule-tool-Z5VG67JK.mjs.map → routing-CVQT4KHX.mjs.map} +0 -0
  151. /package/dist/sdk/{schedule-tool-ZMX3Y7LF.mjs.map → routing-EBAE5SSO.mjs.map} +0 -0
  152. /package/dist/sdk/{schedule-tool-handler-N7UNABOA.mjs.map → schedule-tool-POY3CDZL.mjs.map} +0 -0
  153. /package/dist/sdk/{schedule-tool-handler-PCERK6ZZ.mjs.map → schedule-tool-R2OAATUS.mjs.map} +0 -0
  154. /package/dist/sdk/{schedule-tool-handler-QOJVFRB4.mjs.map → schedule-tool-Z6QYL2B3.mjs.map} +0 -0
  155. /package/dist/sdk/{trace-helpers-KXDOJWBL.mjs.map → schedule-tool-handler-J4NUETJ6.mjs.map} +0 -0
  156. /package/dist/sdk/{workflow-check-provider-CJXW2Z4F.mjs.map → schedule-tool-handler-JMAKHPI7.mjs.map} +0 -0
  157. /package/dist/sdk/{workflow-check-provider-NTHC5ZBF.mjs.map → schedule-tool-handler-MWFUIQKR.mjs.map} +0 -0
  158. /package/dist/sdk/{workflow-check-provider-SRIMWKLQ.mjs.map → trace-helpers-HL5FBX65.mjs.map} +0 -0
@@ -0,0 +1,1502 @@
1
+ import {
2
+ FailureConditionEvaluator,
3
+ init_failure_condition_evaluator
4
+ } from "./chunk-DHETLQIX.mjs";
5
+ import {
6
+ addEvent,
7
+ init_trace_helpers
8
+ } from "./chunk-7ERVRLDV.mjs";
9
+ import {
10
+ createExtendedLiquid,
11
+ init_liquid_extensions
12
+ } from "./chunk-PQWZ6NFL.mjs";
13
+ import {
14
+ compileAndRun,
15
+ createSecureSandbox,
16
+ init_sandbox
17
+ } from "./chunk-LW3INISN.mjs";
18
+ import {
19
+ MemoryStore,
20
+ init_memory_store
21
+ } from "./chunk-UFHOIB3R.mjs";
22
+ import {
23
+ init_logger,
24
+ logger
25
+ } from "./chunk-FT3I25QV.mjs";
26
+ import {
27
+ __esm,
28
+ __export,
29
+ __toCommonJS
30
+ } from "./chunk-J7LXIPZS.mjs";
31
+
32
+ // src/snapshot-store.ts
33
+ var snapshot_store_exports = {};
34
+ __export(snapshot_store_exports, {
35
+ ContextView: () => ContextView,
36
+ ExecutionJournal: () => ExecutionJournal
37
+ });
38
+ var ExecutionJournal, ContextView;
39
+ var init_snapshot_store = __esm({
40
+ "src/snapshot-store.ts"() {
41
+ "use strict";
42
+ ExecutionJournal = class {
43
+ commit = 0;
44
+ entries = [];
45
+ beginSnapshot() {
46
+ return this.commit;
47
+ }
48
+ commitEntry(entry) {
49
+ const committed = {
50
+ sessionId: entry.sessionId,
51
+ scope: entry.scope,
52
+ checkId: entry.checkId,
53
+ result: entry.result,
54
+ event: entry.event,
55
+ commitId: ++this.commit
56
+ };
57
+ this.entries.push(committed);
58
+ return committed;
59
+ }
60
+ readVisible(sessionId, commitMax, event) {
61
+ return this.entries.filter(
62
+ (e) => e.sessionId === sessionId && e.commitId <= commitMax && (event ? e.event === event : true)
63
+ );
64
+ }
65
+ // Lightweight helpers for debugging/metrics
66
+ size() {
67
+ return this.entries.length;
68
+ }
69
+ };
70
+ ContextView = class {
71
+ constructor(journal, sessionId, snapshotId, scope, event) {
72
+ this.journal = journal;
73
+ this.sessionId = sessionId;
74
+ this.snapshotId = snapshotId;
75
+ this.scope = scope;
76
+ this.event = event;
77
+ }
78
+ /** Return the nearest result for a check in this scope (exact item → ancestor → latest). */
79
+ get(checkId) {
80
+ const visible = this.journal.readVisible(this.sessionId, this.snapshotId, this.event).filter((e) => e.checkId === checkId);
81
+ if (visible.length === 0) return void 0;
82
+ const exactMatches = visible.filter((e) => this.sameScope(e.scope, this.scope));
83
+ if (exactMatches.length > 0) {
84
+ return exactMatches[exactMatches.length - 1].result;
85
+ }
86
+ let best;
87
+ for (const e of visible) {
88
+ const dist = this.ancestorDistance(e.scope, this.scope);
89
+ if (dist >= 0 && (best === void 0 || dist < best.dist)) {
90
+ best = { entry: e, dist };
91
+ }
92
+ }
93
+ if (best) return best.entry.result;
94
+ return visible[visible.length - 1]?.result;
95
+ }
96
+ /** Return an aggregate (raw) result – the shallowest scope for this check. */
97
+ getRaw(checkId) {
98
+ const visible = this.journal.readVisible(this.sessionId, this.snapshotId, this.event).filter((e) => e.checkId === checkId);
99
+ if (visible.length === 0) return void 0;
100
+ let shallow = visible[0];
101
+ for (const e of visible) {
102
+ if (e.scope.length < shallow.scope.length) shallow = e;
103
+ }
104
+ return shallow.result;
105
+ }
106
+ /** All results for a check up to this snapshot. */
107
+ getHistory(checkId) {
108
+ return this.journal.readVisible(this.sessionId, this.snapshotId, this.event).filter((e) => e.checkId === checkId).map((e) => e.result);
109
+ }
110
+ sameScope(a, b) {
111
+ if (a.length !== b.length) return false;
112
+ for (let i = 0; i < a.length; i++) {
113
+ if (a[i].check !== b[i].check || a[i].index !== b[i].index) return false;
114
+ }
115
+ return true;
116
+ }
117
+ // distance from ancestor to current; -1 if not ancestor
118
+ ancestorDistance(ancestor, current) {
119
+ if (ancestor.length > current.length) return -1;
120
+ if (ancestor.length === 0 && current.length > 0) return -1;
121
+ for (let i = 0; i < ancestor.length; i++) {
122
+ if (ancestor[i].check !== current[i].check || ancestor[i].index !== current[i].index)
123
+ return -1;
124
+ }
125
+ return current.length - ancestor.length;
126
+ }
127
+ };
128
+ }
129
+ });
130
+
131
+ // src/state-machine/states/routing.ts
132
+ async function renderRouteArgs(args, output, dependencyResults, context) {
133
+ if (!args || Object.keys(args).length === 0) {
134
+ return args;
135
+ }
136
+ const liquid = createExtendedLiquid();
137
+ const renderedArgs = {};
138
+ const templateContext = {
139
+ output,
140
+ // Output of the step that triggered routing
141
+ outputs: dependencyResults,
142
+ // All dependency outputs
143
+ env: process.env,
144
+ inputs: context.executionContext?.workflowInputs || {}
145
+ };
146
+ for (const [key, value] of Object.entries(args)) {
147
+ if (typeof value === "string" && value.includes("{{")) {
148
+ try {
149
+ renderedArgs[key] = await liquid.parseAndRender(value, templateContext);
150
+ } catch (error) {
151
+ logger.warn(`[Routing] Failed to render template for arg ${key}: ${error}`);
152
+ renderedArgs[key] = value;
153
+ }
154
+ } else {
155
+ renderedArgs[key] = value;
156
+ }
157
+ }
158
+ return renderedArgs;
159
+ }
160
+ function parseRunItem(item) {
161
+ if (typeof item === "string") {
162
+ return { target: item };
163
+ }
164
+ if ("step" in item) {
165
+ const stepItem = item;
166
+ return { target: stepItem.step, args: stepItem.with };
167
+ }
168
+ if ("workflow" in item) {
169
+ const workflowItem = item;
170
+ return { target: workflowItem.workflow, args: workflowItem.with };
171
+ }
172
+ return { target: String(item) };
173
+ }
174
+ function hasMapFanoutDependents(context, checkId) {
175
+ const checks = context.config.checks || {};
176
+ const reduceProviders = /* @__PURE__ */ new Set(["log", "memory", "script", "workflow", "noop"]);
177
+ for (const [cid, cfg] of Object.entries(checks)) {
178
+ if (cid === checkId) continue;
179
+ const rawDeps = cfg.depends_on || [];
180
+ const depList = Array.isArray(rawDeps) ? rawDeps : [rawDeps];
181
+ let depends = false;
182
+ for (const dep of depList) {
183
+ if (typeof dep !== "string") continue;
184
+ if (dep.includes("|")) {
185
+ const opts = dep.split("|").map((s) => s.trim()).filter(Boolean);
186
+ if (opts.includes(checkId)) {
187
+ depends = true;
188
+ break;
189
+ }
190
+ } else if (dep === checkId) {
191
+ depends = true;
192
+ break;
193
+ }
194
+ }
195
+ if (!depends) continue;
196
+ const explicit = cfg.fanout;
197
+ if (explicit === "map") return true;
198
+ if (explicit === "reduce") continue;
199
+ const providerType = context.checks[cid]?.providerType || checks[cid]?.type || "";
200
+ const inferred = reduceProviders.has(providerType) ? "reduce" : "map";
201
+ if (inferred === "map") return true;
202
+ }
203
+ return false;
204
+ }
205
+ function classifyFailure(result) {
206
+ const issues = result?.issues || [];
207
+ if (!issues || issues.length === 0) return "none";
208
+ let hasLogical = false;
209
+ let hasExecution = false;
210
+ for (const iss of issues) {
211
+ const id = String(iss.ruleId || "");
212
+ const msg = String(iss.message || "");
213
+ const msgLower = msg.toLowerCase();
214
+ if (id.endsWith("_fail_if") || id.includes("contract/guarantee_failed") || id.includes("contract/schema_validation_failed"))
215
+ hasLogical = true;
216
+ if (id.endsWith("/error") || id.includes("/execution_error") || id.includes("timeout") || msgLower.includes("timed out") || msg.includes("Command execution failed"))
217
+ hasExecution = true;
218
+ if (id.includes("forEach/execution_error") || msg.includes("sandbox_runner_error"))
219
+ hasExecution = true;
220
+ }
221
+ if (hasLogical && !hasExecution) return "logical";
222
+ if (hasExecution && !hasLogical) return "execution";
223
+ return hasExecution ? "execution" : "logical";
224
+ }
225
+ function getCriticality(context, checkId) {
226
+ const cfg = context.config.checks?.[checkId];
227
+ return cfg && cfg.criticality || "policy";
228
+ }
229
+ function createMemoryHelpers() {
230
+ const memoryStore = MemoryStore.getInstance();
231
+ return {
232
+ get: (key, ns) => memoryStore.get(key, ns),
233
+ has: (key, ns) => memoryStore.has(key, ns),
234
+ getAll: (ns) => memoryStore.getAll(ns),
235
+ set: (key, value, ns) => {
236
+ const nsName = ns || memoryStore.getDefaultNamespace();
237
+ const data = memoryStore["data"];
238
+ if (!data.has(nsName)) data.set(nsName, /* @__PURE__ */ new Map());
239
+ data.get(nsName).set(key, value);
240
+ },
241
+ clear: (ns) => {
242
+ const data = memoryStore["data"];
243
+ if (ns) data.delete(ns);
244
+ else data.clear();
245
+ },
246
+ increment: (key, amount = 1, ns) => {
247
+ const nsName = ns || memoryStore.getDefaultNamespace();
248
+ const data = memoryStore["data"];
249
+ if (!data.has(nsName)) data.set(nsName, /* @__PURE__ */ new Map());
250
+ const nsMap = data.get(nsName);
251
+ const current = nsMap.get(key);
252
+ const numCurrent = typeof current === "number" ? current : 0;
253
+ const newValue = numCurrent + amount;
254
+ nsMap.set(key, newValue);
255
+ return newValue;
256
+ }
257
+ };
258
+ }
259
+ function getHistoryLimit() {
260
+ const raw = process.env.VISOR_TEST_HISTORY_LIMIT || process.env.VISOR_OUTPUT_HISTORY_LIMIT;
261
+ if (!raw) return void 0;
262
+ const n = parseInt(raw, 10);
263
+ return Number.isFinite(n) && n > 0 ? n : void 0;
264
+ }
265
+ function formatScopeLabel(scope) {
266
+ if (!scope || scope.length === 0) return "";
267
+ return scope.map((item) => `${item.check}:${item.index}`).join("|");
268
+ }
269
+ function recordRoutingEvent(args) {
270
+ const attrs = {
271
+ check_id: args.checkId,
272
+ trigger: args.trigger,
273
+ action: args.action
274
+ };
275
+ if (args.target) attrs.target = args.target;
276
+ if (args.source) attrs.source = args.source;
277
+ const scopeLabel = formatScopeLabel(args.scope);
278
+ if (scopeLabel) attrs.scope = scopeLabel;
279
+ if (args.gotoEvent) attrs.goto_event = args.gotoEvent;
280
+ addEvent("visor.routing", attrs);
281
+ }
282
+ async function handleRouting(context, state, transition, emitEvent, routingContext) {
283
+ const { checkId, scope, result, checkConfig, success } = routingContext;
284
+ logger.info(`[Routing] Evaluating routing for check: ${checkId}, success: ${success}`);
285
+ const failureResult = await evaluateFailIf(checkId, result, checkConfig, context, state);
286
+ if (failureResult.haltExecution) {
287
+ logger.error(
288
+ `[Routing] HALTING EXECUTION due to critical failure in ${checkId}: ${failureResult.haltMessage}`
289
+ );
290
+ const haltIssue = {
291
+ file: "system",
292
+ line: 0,
293
+ ruleId: `${checkId}_halt_execution`,
294
+ message: `Execution halted: ${failureResult.haltMessage || "Critical failure condition met"}`,
295
+ severity: "error",
296
+ category: "logic"
297
+ };
298
+ result.issues = [...result.issues || [], haltIssue];
299
+ emitEvent({
300
+ type: "Shutdown",
301
+ error: {
302
+ message: failureResult.haltMessage || `Execution halted by check ${checkId}`,
303
+ name: "HaltExecution"
304
+ }
305
+ });
306
+ transition("Error");
307
+ return true;
308
+ }
309
+ if (failureResult.failed) {
310
+ if (context.debug) {
311
+ logger.info(`[Routing] fail_if/failure_conditions triggered for ${checkId}`);
312
+ }
313
+ await processOnFail(checkId, scope, result, checkConfig, context, state, emitEvent);
314
+ } else if (success) {
315
+ await processOnSuccess(checkId, scope, result, checkConfig, context, state, emitEvent);
316
+ } else {
317
+ await processOnFail(checkId, scope, result, checkConfig, context, state, emitEvent);
318
+ }
319
+ const shouldProcessOnFinishHere = !!checkConfig.on_finish && (checkConfig.forEach !== true || !hasMapFanoutDependents(context, checkId));
320
+ if (checkConfig.on_finish) {
321
+ logger.info(
322
+ `[Routing] on_finish decision for ${checkId}: forEach=${!!checkConfig.forEach}, processHere=${shouldProcessOnFinishHere}`
323
+ );
324
+ }
325
+ if (shouldProcessOnFinishHere) {
326
+ await processOnFinish(checkId, scope, result, checkConfig, context, state, emitEvent);
327
+ }
328
+ transition("WavePlanning");
329
+ return false;
330
+ }
331
+ async function processOnFinish(checkId, scope, result, checkConfig, context, state, emitEvent) {
332
+ const onFinish = checkConfig.on_finish;
333
+ if (!onFinish) {
334
+ return;
335
+ }
336
+ logger.info(`Processing on_finish for ${checkId}`);
337
+ let queuedForward = false;
338
+ if (onFinish.run && onFinish.run.length > 0) {
339
+ const currentCheckIsForEach = checkConfig.forEach === true;
340
+ const forEachItems = currentCheckIsForEach ? result.forEachItems : void 0;
341
+ const hasForEachItems = Array.isArray(forEachItems) && forEachItems.length > 0;
342
+ for (const targetCheck of onFinish.run) {
343
+ if (checkLoopBudget(context, state, "on_finish", "run")) {
344
+ const errorIssue = {
345
+ file: "system",
346
+ line: 0,
347
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
348
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? DEFAULT_MAX_LOOPS}) during on_finish run`,
349
+ severity: "error",
350
+ category: "logic"
351
+ };
352
+ result.issues = [...result.issues || [], errorIssue];
353
+ return;
354
+ }
355
+ const targetConfig = context.config.checks?.[targetCheck];
356
+ const fanoutMode = targetConfig?.fanout || "reduce";
357
+ if (context.debug) {
358
+ logger.info(
359
+ `[Routing] on_finish.run: scheduling ${targetCheck} with fanout=${fanoutMode}, hasForEachItems=${hasForEachItems}`
360
+ );
361
+ }
362
+ if (fanoutMode === "map" && hasForEachItems) {
363
+ for (let itemIndex = 0; itemIndex < forEachItems.length; itemIndex++) {
364
+ state.routingLoopCount++;
365
+ const itemScope = [
366
+ { check: checkId, index: itemIndex }
367
+ ];
368
+ recordRoutingEvent({
369
+ checkId,
370
+ trigger: "on_finish",
371
+ action: "run",
372
+ target: targetCheck,
373
+ source: "run",
374
+ scope: itemScope
375
+ });
376
+ emitEvent({
377
+ type: "ForwardRunRequested",
378
+ target: targetCheck,
379
+ scope: itemScope,
380
+ origin: "run"
381
+ });
382
+ queuedForward = true;
383
+ }
384
+ } else {
385
+ state.routingLoopCount++;
386
+ recordRoutingEvent({
387
+ checkId,
388
+ trigger: "on_finish",
389
+ action: "run",
390
+ target: targetCheck,
391
+ source: "run",
392
+ scope: []
393
+ });
394
+ emitEvent({
395
+ type: "ForwardRunRequested",
396
+ target: targetCheck,
397
+ scope: [],
398
+ origin: "run"
399
+ });
400
+ queuedForward = true;
401
+ }
402
+ }
403
+ }
404
+ if (onFinish.run_js) {
405
+ const dynamicTargets = await evaluateRunJs(
406
+ onFinish.run_js,
407
+ checkId,
408
+ checkConfig,
409
+ result,
410
+ context,
411
+ state
412
+ );
413
+ for (const runItem of dynamicTargets) {
414
+ const { target: targetCheck, args: runArgs } = parseRunItem(runItem);
415
+ if (checkLoopBudget(context, state, "on_finish", "run")) {
416
+ const errorIssue = {
417
+ file: "system",
418
+ line: 0,
419
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
420
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? DEFAULT_MAX_LOOPS}) during on_finish run`,
421
+ severity: "error",
422
+ category: "logic"
423
+ };
424
+ result.issues = [...result.issues || [], errorIssue];
425
+ return;
426
+ }
427
+ if (context.debug) {
428
+ logger.info(
429
+ `[Routing] on_finish.run_js: scheduling ${targetCheck}${runArgs ? ", args=" + JSON.stringify(runArgs) : ""}`
430
+ );
431
+ }
432
+ state.routingLoopCount++;
433
+ recordRoutingEvent({
434
+ checkId,
435
+ trigger: "on_finish",
436
+ action: "run",
437
+ target: targetCheck,
438
+ source: "run_js",
439
+ scope
440
+ });
441
+ emitEvent({
442
+ type: "ForwardRunRequested",
443
+ target: targetCheck,
444
+ scope,
445
+ origin: "run_js",
446
+ args: runArgs
447
+ });
448
+ queuedForward = true;
449
+ }
450
+ }
451
+ const finishTransTarget = await evaluateTransitions(
452
+ onFinish.transitions,
453
+ checkId,
454
+ checkConfig,
455
+ result,
456
+ context,
457
+ state
458
+ );
459
+ if (finishTransTarget !== void 0) {
460
+ if (finishTransTarget) {
461
+ if (checkLoopBudget(context, state, "on_finish", "goto")) {
462
+ const errorIssue = {
463
+ file: "system",
464
+ line: 0,
465
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
466
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? DEFAULT_MAX_LOOPS}) during on_finish goto`,
467
+ severity: "error",
468
+ category: "logic"
469
+ };
470
+ result.issues = [...result.issues || [], errorIssue];
471
+ return;
472
+ }
473
+ state.routingLoopCount++;
474
+ recordRoutingEvent({
475
+ checkId,
476
+ trigger: "on_finish",
477
+ action: "goto",
478
+ target: finishTransTarget.to,
479
+ source: "transitions",
480
+ scope,
481
+ gotoEvent: finishTransTarget.goto_event
482
+ });
483
+ emitEvent({
484
+ type: "ForwardRunRequested",
485
+ target: finishTransTarget.to,
486
+ scope,
487
+ origin: "goto_js",
488
+ gotoEvent: finishTransTarget.goto_event
489
+ });
490
+ }
491
+ return;
492
+ }
493
+ const gotoTarget = await evaluateGoto(
494
+ onFinish.goto_js,
495
+ onFinish.goto,
496
+ checkId,
497
+ checkConfig,
498
+ result,
499
+ context,
500
+ state
501
+ );
502
+ if (gotoTarget) {
503
+ if (checkLoopBudget(context, state, "on_finish", "goto")) {
504
+ const errorIssue = {
505
+ file: "system",
506
+ line: 0,
507
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
508
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? DEFAULT_MAX_LOOPS}) during on_finish goto`,
509
+ severity: "error",
510
+ category: "logic"
511
+ };
512
+ result.issues = [...result.issues || [], errorIssue];
513
+ return;
514
+ }
515
+ if (context.debug) {
516
+ logger.info(`[Routing] on_finish.goto: ${gotoTarget}`);
517
+ }
518
+ state.routingLoopCount++;
519
+ recordRoutingEvent({
520
+ checkId,
521
+ trigger: "on_finish",
522
+ action: "goto",
523
+ target: gotoTarget,
524
+ source: onFinish.goto_js ? "goto_js" : "goto",
525
+ scope
526
+ });
527
+ emitEvent({
528
+ type: "ForwardRunRequested",
529
+ target: gotoTarget,
530
+ scope,
531
+ origin: "goto_js"
532
+ });
533
+ state.flags.forwardRunRequested = true;
534
+ }
535
+ if (queuedForward) {
536
+ const guardKey = `waveRetry:on_finish:${checkId}:wave:${state.wave}`;
537
+ if (!state.forwardRunGuards?.has(guardKey)) {
538
+ state.forwardRunGuards?.add(guardKey);
539
+ emitEvent({ type: "WaveRetry", reason: "on_finish" });
540
+ }
541
+ }
542
+ }
543
+ async function evaluateFailIf(checkId, result, checkConfig, context, state) {
544
+ const config = context.config;
545
+ const globalFailIf = config.fail_if;
546
+ const checkFailIf = checkConfig.fail_if;
547
+ const globalFailureConditions = config.failure_conditions;
548
+ const checkFailureConditions = checkConfig.failure_conditions;
549
+ if (!globalFailIf && !checkFailIf && !globalFailureConditions && !checkFailureConditions) {
550
+ return { failed: false, haltExecution: false };
551
+ }
552
+ const evaluator = new FailureConditionEvaluator();
553
+ const outputsRecord = {};
554
+ for (const [key] of state.stats.entries()) {
555
+ try {
556
+ const snapshotId = context.journal.beginSnapshot();
557
+ const contextView = new (init_snapshot_store(), __toCommonJS(snapshot_store_exports)).ContextView(
558
+ context.journal,
559
+ context.sessionId,
560
+ snapshotId,
561
+ [],
562
+ context.event
563
+ );
564
+ const journalResult = contextView.get(key);
565
+ if (journalResult) {
566
+ outputsRecord[key] = journalResult;
567
+ }
568
+ } catch {
569
+ outputsRecord[key] = { issues: [] };
570
+ }
571
+ }
572
+ const checkSchema = typeof checkConfig.schema === "object" ? "custom" : checkConfig.schema || "";
573
+ const checkGroup = checkConfig.group || "";
574
+ let anyFailed = false;
575
+ let shouldHalt = false;
576
+ let haltMessage;
577
+ if (globalFailIf) {
578
+ try {
579
+ const failed = await evaluator.evaluateSimpleCondition(
580
+ checkId,
581
+ checkSchema,
582
+ checkGroup,
583
+ result,
584
+ globalFailIf,
585
+ outputsRecord
586
+ );
587
+ if (failed) {
588
+ logger.warn(`[Routing] Global fail_if triggered for ${checkId}: ${globalFailIf}`);
589
+ const failIssue = {
590
+ file: "system",
591
+ line: 0,
592
+ ruleId: "global_fail_if",
593
+ message: `Global failure condition met: ${globalFailIf}`,
594
+ severity: "error",
595
+ category: "logic"
596
+ };
597
+ result.issues = [...result.issues || [], failIssue];
598
+ }
599
+ } catch (error) {
600
+ const msg = error instanceof Error ? error.message : String(error);
601
+ logger.error(`[Routing] Error evaluating global fail_if: ${msg}`);
602
+ }
603
+ }
604
+ if (checkFailIf) {
605
+ try {
606
+ const failed = await evaluator.evaluateSimpleCondition(
607
+ checkId,
608
+ checkSchema,
609
+ checkGroup,
610
+ result,
611
+ checkFailIf,
612
+ outputsRecord
613
+ );
614
+ if (failed) {
615
+ logger.warn(`[Routing] Check fail_if triggered for ${checkId}: ${checkFailIf}`);
616
+ const failIssue = {
617
+ file: "system",
618
+ line: 0,
619
+ ruleId: `${checkId}_fail_if`,
620
+ message: `Check failure condition met: ${checkFailIf}`,
621
+ severity: "error",
622
+ category: "logic"
623
+ };
624
+ result.issues = [...result.issues || [], failIssue];
625
+ anyFailed = true;
626
+ }
627
+ } catch (error) {
628
+ const msg = error instanceof Error ? error.message : String(error);
629
+ logger.error(`[Routing] Error evaluating check fail_if: ${msg}`);
630
+ }
631
+ }
632
+ if (globalFailureConditions || checkFailureConditions) {
633
+ try {
634
+ const conditionResults = await evaluator.evaluateConditions(
635
+ checkId,
636
+ checkSchema,
637
+ checkGroup,
638
+ result,
639
+ globalFailureConditions,
640
+ checkFailureConditions,
641
+ outputsRecord
642
+ );
643
+ for (const condResult of conditionResults) {
644
+ if (condResult.failed) {
645
+ logger.warn(
646
+ `[Routing] Failure condition '${condResult.conditionName}' triggered for ${checkId}: ${condResult.expression}`
647
+ );
648
+ const failIssue = {
649
+ file: "system",
650
+ line: 0,
651
+ ruleId: `${checkId}_${condResult.conditionName}`,
652
+ message: condResult.message || `Failure condition met: ${condResult.expression}`,
653
+ severity: condResult.severity || "error",
654
+ category: "logic"
655
+ };
656
+ result.issues = [...result.issues || [], failIssue];
657
+ anyFailed = true;
658
+ if (condResult.haltExecution) {
659
+ shouldHalt = true;
660
+ haltMessage = condResult.message || `Execution halted: condition '${condResult.conditionName}' triggered`;
661
+ logger.error(
662
+ `[Routing] HALT EXECUTION triggered by '${condResult.conditionName}' for ${checkId}`
663
+ );
664
+ }
665
+ }
666
+ }
667
+ } catch (error) {
668
+ const msg = error instanceof Error ? error.message : String(error);
669
+ logger.error(`[Routing] Error evaluating failure_conditions: ${msg}`);
670
+ }
671
+ }
672
+ return { failed: anyFailed, haltExecution: shouldHalt, haltMessage };
673
+ }
674
+ function checkLoopBudget(context, state, origin, action) {
675
+ const maxLoops = context.config.routing?.max_loops ?? DEFAULT_MAX_LOOPS;
676
+ if (state.routingLoopCount >= maxLoops) {
677
+ const msg = `Routing loop budget exceeded (max_loops=${maxLoops}) during ${origin} ${action}`;
678
+ logger.error(`[Routing] ${msg}`);
679
+ return true;
680
+ }
681
+ return false;
682
+ }
683
+ async function processOnSuccess(checkId, scope, result, checkConfig, context, state, emitEvent) {
684
+ const onSuccess = checkConfig.on_success;
685
+ if (!onSuccess) {
686
+ return;
687
+ }
688
+ if (context.debug) {
689
+ logger.info(`[Routing] Processing on_success for ${checkId}`);
690
+ }
691
+ if (onSuccess.run && onSuccess.run.length > 0) {
692
+ const resForEachItems = result && result.forEachItems || void 0;
693
+ const hasForEachItems = Array.isArray(resForEachItems) && resForEachItems.length > 0;
694
+ const stepOutput = result?.output;
695
+ const depResults = {};
696
+ try {
697
+ const snapshotId = context.journal.beginSnapshot();
698
+ const allEntries = context.journal.readVisible(context.sessionId, snapshotId, context.event);
699
+ for (const entry of allEntries) {
700
+ if (entry.checkId && entry.result) {
701
+ const r = entry.result;
702
+ depResults[entry.checkId] = r.output !== void 0 ? r.output : r;
703
+ }
704
+ }
705
+ } catch (e) {
706
+ logger.warn(`[Routing] Failed to build dependency results for template rendering: ${e}`);
707
+ }
708
+ for (const runItem of onSuccess.run) {
709
+ const { target: targetCheck, args: rawArgs } = parseRunItem(runItem);
710
+ const runArgs = rawArgs ? await renderRouteArgs(rawArgs, stepOutput, depResults, context) : void 0;
711
+ if (checkLoopBudget(context, state, "on_success", "run")) {
712
+ const errorIssue = {
713
+ file: "system",
714
+ line: 0,
715
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
716
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? DEFAULT_MAX_LOOPS}) during on_success run`,
717
+ severity: "error",
718
+ category: "logic"
719
+ };
720
+ result.issues = [...result.issues || [], errorIssue];
721
+ return;
722
+ }
723
+ const targetConfig = context.config.checks?.[targetCheck];
724
+ const fanoutMode = targetConfig?.fanout || "reduce";
725
+ if (context.debug) {
726
+ logger.info(
727
+ `[Routing] on_success.run: scheduling ${targetCheck} with fanout=${fanoutMode}, hasForEachItems=${hasForEachItems}${runArgs ? ", args=" + JSON.stringify(runArgs) : ""}`
728
+ );
729
+ }
730
+ if (fanoutMode === "map" && hasForEachItems) {
731
+ for (let itemIndex = 0; itemIndex < resForEachItems.length; itemIndex++) {
732
+ state.routingLoopCount++;
733
+ const itemScope = [
734
+ { check: checkId, index: itemIndex }
735
+ ];
736
+ recordRoutingEvent({
737
+ checkId,
738
+ trigger: "on_success",
739
+ action: "run",
740
+ target: targetCheck,
741
+ source: "run",
742
+ scope: itemScope
743
+ });
744
+ emitEvent({
745
+ type: "ForwardRunRequested",
746
+ target: targetCheck,
747
+ scope: itemScope,
748
+ origin: "run",
749
+ args: runArgs
750
+ });
751
+ }
752
+ } else {
753
+ state.routingLoopCount++;
754
+ recordRoutingEvent({
755
+ checkId,
756
+ trigger: "on_success",
757
+ action: "run",
758
+ target: targetCheck,
759
+ source: "run",
760
+ scope
761
+ });
762
+ emitEvent({
763
+ type: "ForwardRunRequested",
764
+ target: targetCheck,
765
+ scope,
766
+ origin: "run",
767
+ args: runArgs
768
+ });
769
+ }
770
+ }
771
+ }
772
+ if (onSuccess.run_js) {
773
+ const dynamicTargets = await evaluateRunJs(
774
+ onSuccess.run_js,
775
+ checkId,
776
+ checkConfig,
777
+ result,
778
+ context,
779
+ state
780
+ );
781
+ for (const runItem of dynamicTargets) {
782
+ const { target: targetCheck, args: runArgs } = parseRunItem(runItem);
783
+ if (checkLoopBudget(context, state, "on_success", "run")) {
784
+ const errorIssue = {
785
+ file: "system",
786
+ line: 0,
787
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
788
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? 10}) during on_success run`,
789
+ severity: "error",
790
+ category: "logic"
791
+ };
792
+ result.issues = [...result.issues || [], errorIssue];
793
+ return;
794
+ }
795
+ if (context.debug) {
796
+ logger.info(
797
+ `[Routing] on_success.run_js: scheduling ${targetCheck}${runArgs ? ", args=" + JSON.stringify(runArgs) : ""}`
798
+ );
799
+ }
800
+ state.routingLoopCount++;
801
+ recordRoutingEvent({
802
+ checkId,
803
+ trigger: "on_success",
804
+ action: "run",
805
+ target: targetCheck,
806
+ source: "run_js",
807
+ scope
808
+ });
809
+ emitEvent({
810
+ type: "ForwardRunRequested",
811
+ target: targetCheck,
812
+ scope,
813
+ origin: "run_js",
814
+ args: runArgs
815
+ });
816
+ }
817
+ }
818
+ const successTransTarget = await evaluateTransitions(
819
+ onSuccess.transitions,
820
+ checkId,
821
+ checkConfig,
822
+ result,
823
+ context,
824
+ state
825
+ );
826
+ if (successTransTarget !== void 0) {
827
+ if (successTransTarget) {
828
+ if (checkLoopBudget(context, state, "on_success", "goto")) {
829
+ const errorIssue = {
830
+ file: "system",
831
+ line: 0,
832
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
833
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? DEFAULT_MAX_LOOPS}) during on_success goto`,
834
+ severity: "error",
835
+ category: "logic"
836
+ };
837
+ result.issues = [...result.issues || [], errorIssue];
838
+ return;
839
+ }
840
+ state.routingLoopCount++;
841
+ recordRoutingEvent({
842
+ checkId,
843
+ trigger: "on_success",
844
+ action: "goto",
845
+ target: successTransTarget.to,
846
+ source: "transitions",
847
+ scope,
848
+ gotoEvent: successTransTarget.goto_event
849
+ });
850
+ emitEvent({
851
+ type: "ForwardRunRequested",
852
+ target: successTransTarget.to,
853
+ scope,
854
+ origin: "goto_js",
855
+ gotoEvent: successTransTarget.goto_event
856
+ });
857
+ state.flags.forwardRunRequested = true;
858
+ }
859
+ return;
860
+ }
861
+ const gotoTarget = await evaluateGoto(
862
+ onSuccess.goto_js,
863
+ onSuccess.goto,
864
+ checkId,
865
+ checkConfig,
866
+ result,
867
+ context,
868
+ state
869
+ );
870
+ if (gotoTarget) {
871
+ if (checkLoopBudget(context, state, "on_success", "goto")) {
872
+ const errorIssue = {
873
+ file: "system",
874
+ line: 0,
875
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
876
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? 10}) during on_success goto`,
877
+ severity: "error",
878
+ category: "logic"
879
+ };
880
+ result.issues = [...result.issues || [], errorIssue];
881
+ return;
882
+ }
883
+ if (context.debug) {
884
+ logger.info(`[Routing] on_success.goto: ${gotoTarget}`);
885
+ }
886
+ state.routingLoopCount++;
887
+ recordRoutingEvent({
888
+ checkId,
889
+ trigger: "on_success",
890
+ action: "goto",
891
+ target: gotoTarget,
892
+ source: onSuccess.goto_js ? "goto_js" : "goto",
893
+ scope,
894
+ gotoEvent: onSuccess.goto_event
895
+ });
896
+ emitEvent({
897
+ type: "ForwardRunRequested",
898
+ target: gotoTarget,
899
+ gotoEvent: onSuccess.goto_event,
900
+ scope,
901
+ origin: "goto_js"
902
+ });
903
+ state.flags.forwardRunRequested = true;
904
+ }
905
+ }
906
+ async function processOnFail(checkId, scope, result, checkConfig, context, state, emitEvent) {
907
+ const defaults = context.config.routing?.defaults?.on_fail || {};
908
+ const onFail = checkConfig.on_fail ? { ...defaults, ...checkConfig.on_fail } : void 0;
909
+ if (!onFail) {
910
+ return;
911
+ }
912
+ if (context.debug) {
913
+ logger.info(`[Routing] Processing on_fail for ${checkId}`);
914
+ }
915
+ if (onFail.run && onFail.run.length > 0) {
916
+ const resForEachItems = result && result.forEachItems || void 0;
917
+ const hasForEachItems = Array.isArray(resForEachItems) && resForEachItems.length > 0;
918
+ for (const targetCheck of onFail.run) {
919
+ if (checkLoopBudget(context, state, "on_fail", "run")) {
920
+ const errorIssue = {
921
+ file: "system",
922
+ line: 0,
923
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
924
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? 10}) during on_fail run`,
925
+ severity: "error",
926
+ category: "logic"
927
+ };
928
+ result.issues = [...result.issues || [], errorIssue];
929
+ return;
930
+ }
931
+ const targetConfig = context.config.checks?.[targetCheck];
932
+ const fanoutMode = targetConfig?.fanout || "reduce";
933
+ if (context.debug) {
934
+ logger.info(
935
+ `[Routing] on_fail.run: scheduling ${targetCheck} with fanout=${fanoutMode}, hasForEachItems=${hasForEachItems}`
936
+ );
937
+ }
938
+ if (hasForEachItems) {
939
+ for (let itemIndex = 0; itemIndex < resForEachItems.length; itemIndex++) {
940
+ const itemOut = resForEachItems[itemIndex];
941
+ if (itemOut && typeof itemOut === "object" && itemOut.__failed !== true && fanoutMode !== "map") {
942
+ continue;
943
+ }
944
+ state.routingLoopCount++;
945
+ const itemScope = [
946
+ { check: checkId, index: itemIndex }
947
+ ];
948
+ recordRoutingEvent({
949
+ checkId,
950
+ trigger: "on_fail",
951
+ action: "run",
952
+ target: targetCheck,
953
+ source: "run",
954
+ scope: itemScope
955
+ });
956
+ emitEvent({
957
+ type: "ForwardRunRequested",
958
+ target: targetCheck,
959
+ scope: itemScope,
960
+ origin: "run",
961
+ sourceCheck: checkId
962
+ // The failed check that triggered on_fail.run
963
+ });
964
+ }
965
+ } else {
966
+ state.routingLoopCount++;
967
+ recordRoutingEvent({
968
+ checkId,
969
+ trigger: "on_fail",
970
+ action: "run",
971
+ target: targetCheck,
972
+ source: "run",
973
+ scope
974
+ });
975
+ emitEvent({
976
+ type: "ForwardRunRequested",
977
+ target: targetCheck,
978
+ scope,
979
+ origin: "run",
980
+ sourceCheck: checkId
981
+ // The failed check that triggered on_fail.run
982
+ });
983
+ }
984
+ }
985
+ }
986
+ if (onFail.run_js) {
987
+ const dynamicTargets = await evaluateRunJs(
988
+ onFail.run_js,
989
+ checkId,
990
+ checkConfig,
991
+ result,
992
+ context,
993
+ state
994
+ );
995
+ for (const runItem of dynamicTargets) {
996
+ const { target: targetCheck, args: runArgs } = parseRunItem(runItem);
997
+ if (checkLoopBudget(context, state, "on_fail", "run")) {
998
+ const errorIssue = {
999
+ file: "system",
1000
+ line: 0,
1001
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
1002
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? 10}) during on_fail run`,
1003
+ severity: "error",
1004
+ category: "logic"
1005
+ };
1006
+ result.issues = [...result.issues || [], errorIssue];
1007
+ return;
1008
+ }
1009
+ if (context.debug) {
1010
+ logger.info(
1011
+ `[Routing] on_fail.run_js: scheduling ${targetCheck}${runArgs ? ", args=" + JSON.stringify(runArgs) : ""}`
1012
+ );
1013
+ }
1014
+ state.routingLoopCount++;
1015
+ recordRoutingEvent({
1016
+ checkId,
1017
+ trigger: "on_fail",
1018
+ action: "run",
1019
+ target: targetCheck,
1020
+ source: "run_js",
1021
+ scope
1022
+ });
1023
+ emitEvent({
1024
+ type: "ForwardRunRequested",
1025
+ target: targetCheck,
1026
+ scope,
1027
+ origin: "run_js",
1028
+ sourceCheck: checkId,
1029
+ // The failed check that triggered on_fail.run_js
1030
+ args: runArgs
1031
+ });
1032
+ }
1033
+ }
1034
+ if (onFail.retry && typeof onFail.retry.max === "number" && onFail.retry.max > 0) {
1035
+ const crit = getCriticality(context, checkId);
1036
+ const failureKind = classifyFailure(result);
1037
+ if ((crit === "external" || crit === "internal") && failureKind === "logical") {
1038
+ if (context.debug) {
1039
+ logger.info(
1040
+ `[Routing] on_fail.retry suppressed for ${checkId} (criticality=${crit}, failure=logical)`
1041
+ );
1042
+ }
1043
+ } else {
1044
+ const max = Math.max(0, onFail.retry.max || 0);
1045
+ if (!state.retryAttempts) state.retryAttempts = /* @__PURE__ */ new Map();
1046
+ const attemptsMap = state.retryAttempts;
1047
+ const makeKey = (sc) => {
1048
+ const keyScope = sc && sc.length > 0 ? JSON.stringify(sc) : "root";
1049
+ return `${checkId}::${keyScope}`;
1050
+ };
1051
+ const scheduleRetryForScope = (sc) => {
1052
+ const key = makeKey(sc);
1053
+ const used = attemptsMap.get(key) || 0;
1054
+ if (used >= max) return;
1055
+ attemptsMap.set(key, used + 1);
1056
+ state.routingLoopCount++;
1057
+ recordRoutingEvent({
1058
+ checkId,
1059
+ trigger: "on_fail",
1060
+ action: "retry",
1061
+ source: "retry",
1062
+ scope: sc || []
1063
+ });
1064
+ emitEvent({
1065
+ type: "ForwardRunRequested",
1066
+ target: checkId,
1067
+ scope: sc || [],
1068
+ origin: "run"
1069
+ });
1070
+ };
1071
+ const resForEachItems = result && result.forEachItems || void 0;
1072
+ const hasForEachItems = Array.isArray(resForEachItems) && resForEachItems.length > 0;
1073
+ if (hasForEachItems) {
1074
+ for (let i = 0; i < resForEachItems.length; i++) {
1075
+ const itemOut = resForEachItems[i];
1076
+ if (itemOut && typeof itemOut === "object" && itemOut.__failed === true) {
1077
+ const sc = [{ check: checkId, index: i }];
1078
+ scheduleRetryForScope(sc);
1079
+ }
1080
+ }
1081
+ } else {
1082
+ scheduleRetryForScope(scope);
1083
+ }
1084
+ }
1085
+ }
1086
+ const failTransTarget = await evaluateTransitions(
1087
+ onFail.transitions,
1088
+ checkId,
1089
+ checkConfig,
1090
+ result,
1091
+ context,
1092
+ state
1093
+ );
1094
+ if (failTransTarget !== void 0) {
1095
+ if (failTransTarget) {
1096
+ if (checkLoopBudget(context, state, "on_fail", "goto")) {
1097
+ const errorIssue = {
1098
+ file: "system",
1099
+ line: 0,
1100
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
1101
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? DEFAULT_MAX_LOOPS}) during on_fail goto`,
1102
+ severity: "error",
1103
+ category: "logic"
1104
+ };
1105
+ result.issues = [...result.issues || [], errorIssue];
1106
+ return;
1107
+ }
1108
+ state.routingLoopCount++;
1109
+ recordRoutingEvent({
1110
+ checkId,
1111
+ trigger: "on_fail",
1112
+ action: "goto",
1113
+ target: failTransTarget.to,
1114
+ source: "transitions",
1115
+ scope,
1116
+ gotoEvent: failTransTarget.goto_event
1117
+ });
1118
+ emitEvent({
1119
+ type: "ForwardRunRequested",
1120
+ target: failTransTarget.to,
1121
+ scope,
1122
+ origin: "goto_js",
1123
+ gotoEvent: failTransTarget.goto_event
1124
+ });
1125
+ state.flags.forwardRunRequested = true;
1126
+ }
1127
+ return;
1128
+ }
1129
+ const gotoTarget = await evaluateGoto(
1130
+ onFail.goto_js,
1131
+ onFail.goto,
1132
+ checkId,
1133
+ checkConfig,
1134
+ result,
1135
+ context,
1136
+ state
1137
+ );
1138
+ if (gotoTarget) {
1139
+ if (checkLoopBudget(context, state, "on_fail", "goto")) {
1140
+ const errorIssue = {
1141
+ file: "system",
1142
+ line: 0,
1143
+ ruleId: `${checkId}/routing/loop_budget_exceeded`,
1144
+ message: `Routing loop budget exceeded (max_loops=${context.config.routing?.max_loops ?? 10}) during on_fail goto`,
1145
+ severity: "error",
1146
+ category: "logic"
1147
+ };
1148
+ result.issues = [...result.issues || [], errorIssue];
1149
+ return;
1150
+ }
1151
+ if (context.debug) {
1152
+ logger.info(`[Routing] on_fail.goto: ${gotoTarget}`);
1153
+ }
1154
+ state.routingLoopCount++;
1155
+ recordRoutingEvent({
1156
+ checkId,
1157
+ trigger: "on_fail",
1158
+ action: "goto",
1159
+ target: gotoTarget,
1160
+ source: onFail.goto_js ? "goto_js" : "goto",
1161
+ scope,
1162
+ gotoEvent: onFail.goto_event
1163
+ });
1164
+ emitEvent({
1165
+ type: "ForwardRunRequested",
1166
+ target: gotoTarget,
1167
+ gotoEvent: onFail.goto_event,
1168
+ scope,
1169
+ origin: "goto_js"
1170
+ });
1171
+ state.flags.forwardRunRequested = true;
1172
+ }
1173
+ }
1174
+ function isValidRunItem(item) {
1175
+ if (typeof item === "string" && item) return true;
1176
+ if (typeof item === "object" && item !== null) {
1177
+ const obj = item;
1178
+ if (typeof obj.step === "string" && obj.step) return true;
1179
+ if (typeof obj.workflow === "string" && obj.workflow) return true;
1180
+ }
1181
+ return false;
1182
+ }
1183
+ async function evaluateRunJs(runJs, checkId, checkConfig, result, context, _state) {
1184
+ try {
1185
+ const sandbox = createSecureSandbox();
1186
+ const historyLimit = getHistoryLimit();
1187
+ const snapshotId = context.journal.beginSnapshot();
1188
+ const contextView = new (init_snapshot_store(), __toCommonJS(snapshot_store_exports)).ContextView(
1189
+ context.journal,
1190
+ context.sessionId,
1191
+ snapshotId,
1192
+ [],
1193
+ context.event
1194
+ );
1195
+ const outputsRecord = {};
1196
+ const outputsHistory = {};
1197
+ const allEntries = context.journal.readVisible(context.sessionId, snapshotId, context.event);
1198
+ const uniqueCheckIds = new Set(allEntries.map((e) => e.checkId));
1199
+ for (const checkIdFromJournal of uniqueCheckIds) {
1200
+ try {
1201
+ const journalResult = contextView.get(checkIdFromJournal);
1202
+ if (journalResult) {
1203
+ outputsRecord[checkIdFromJournal] = journalResult.output !== void 0 ? journalResult.output : journalResult;
1204
+ }
1205
+ } catch {
1206
+ outputsRecord[checkIdFromJournal] = { issues: [] };
1207
+ }
1208
+ try {
1209
+ const history = contextView.getHistory(checkIdFromJournal);
1210
+ if (history && history.length > 0) {
1211
+ const trimmed = historyLimit && history.length > historyLimit ? history.slice(history.length - historyLimit) : history;
1212
+ outputsHistory[checkIdFromJournal] = trimmed.map(
1213
+ (r) => r.output !== void 0 ? r.output : r
1214
+ );
1215
+ }
1216
+ } catch {
1217
+ }
1218
+ }
1219
+ outputsRecord.history = outputsHistory;
1220
+ let forEachMeta = void 0;
1221
+ try {
1222
+ const hist = outputsHistory[checkId] || [];
1223
+ const lastArr = hist.slice().reverse().find((x) => Array.isArray(x));
1224
+ if (checkConfig.forEach === true && Array.isArray(lastArr)) {
1225
+ forEachMeta = {
1226
+ is_parent: true,
1227
+ last_wave_size: lastArr.length,
1228
+ last_items: lastArr
1229
+ };
1230
+ }
1231
+ } catch {
1232
+ }
1233
+ const scopeObj = {
1234
+ step: {
1235
+ id: checkId,
1236
+ tags: checkConfig.tags || [],
1237
+ group: checkConfig.group
1238
+ },
1239
+ outputs: outputsRecord,
1240
+ outputs_history: outputsHistory,
1241
+ output: result?.output,
1242
+ memory: createMemoryHelpers(),
1243
+ event: {
1244
+ name: context.event || "manual"
1245
+ },
1246
+ forEach: forEachMeta
1247
+ };
1248
+ const code = `
1249
+ const step = scope.step;
1250
+ const outputs = scope.outputs;
1251
+ const outputs_history = scope.outputs_history;
1252
+ const output = scope.output;
1253
+ const memory = scope.memory;
1254
+ const event = scope.event;
1255
+ const forEach = scope.forEach;
1256
+ const log = (...args) => console.log('\u{1F50D} Debug:', ...args);
1257
+ const __fn = () => {
1258
+ ${runJs}
1259
+ };
1260
+ const __res = __fn();
1261
+ // Return as-is; validation happens after sandbox execution
1262
+ return Array.isArray(__res) ? __res : [];
1263
+ `;
1264
+ try {
1265
+ const evalResult = compileAndRun(
1266
+ sandbox,
1267
+ code,
1268
+ { scope: scopeObj },
1269
+ { injectLog: false, wrapFunction: false }
1270
+ );
1271
+ return Array.isArray(evalResult) ? evalResult.filter(isValidRunItem) : [];
1272
+ } catch (error) {
1273
+ const msg = error instanceof Error ? error.message : String(error);
1274
+ logger.error(`[Routing] Error in run_js sandbox evaluation: ${msg}`);
1275
+ return [];
1276
+ }
1277
+ } catch (error) {
1278
+ const msg = error instanceof Error ? error.message : String(error);
1279
+ logger.error(`[Routing] Error evaluating run_js: ${msg}`);
1280
+ return [];
1281
+ }
1282
+ }
1283
+ async function evaluateGoto(gotoJs, gotoStatic, checkId, checkConfig, result, context, _state) {
1284
+ if (gotoJs) {
1285
+ try {
1286
+ const sandbox = createSecureSandbox();
1287
+ const historyLimit = getHistoryLimit();
1288
+ const snapshotId = context.journal.beginSnapshot();
1289
+ const contextView = new (init_snapshot_store(), __toCommonJS(snapshot_store_exports)).ContextView(
1290
+ context.journal,
1291
+ context.sessionId,
1292
+ snapshotId,
1293
+ [],
1294
+ void 0
1295
+ );
1296
+ const outputsRecord = {};
1297
+ const outputsHistory = {};
1298
+ const allEntries = context.journal.readVisible(context.sessionId, snapshotId, void 0);
1299
+ const uniqueCheckIds = new Set(allEntries.map((e) => e.checkId));
1300
+ for (const checkIdFromJournal of uniqueCheckIds) {
1301
+ try {
1302
+ const journalResult = contextView.get(checkIdFromJournal);
1303
+ if (journalResult) {
1304
+ outputsRecord[checkIdFromJournal] = journalResult.output !== void 0 ? journalResult.output : journalResult;
1305
+ }
1306
+ } catch {
1307
+ outputsRecord[checkIdFromJournal] = { issues: [] };
1308
+ }
1309
+ try {
1310
+ const history = contextView.getHistory(checkIdFromJournal);
1311
+ if (history && history.length > 0) {
1312
+ const trimmed = historyLimit && history.length > historyLimit ? history.slice(history.length - historyLimit) : history;
1313
+ outputsHistory[checkIdFromJournal] = trimmed.map(
1314
+ (r) => r.output !== void 0 ? r.output : r
1315
+ );
1316
+ }
1317
+ } catch {
1318
+ }
1319
+ }
1320
+ outputsRecord.history = outputsHistory;
1321
+ let forEachMeta = void 0;
1322
+ try {
1323
+ const hist = outputsHistory[checkId] || [];
1324
+ const lastArr = hist.slice().reverse().find((x) => Array.isArray(x));
1325
+ if (checkConfig.forEach === true && Array.isArray(lastArr)) {
1326
+ forEachMeta = {
1327
+ is_parent: true,
1328
+ last_wave_size: lastArr.length,
1329
+ last_items: lastArr
1330
+ };
1331
+ }
1332
+ } catch {
1333
+ }
1334
+ const scopeObj = {
1335
+ step: {
1336
+ id: checkId,
1337
+ tags: checkConfig.tags || [],
1338
+ group: checkConfig.group
1339
+ },
1340
+ outputs: outputsRecord,
1341
+ outputs_history: outputsHistory,
1342
+ output: result?.output,
1343
+ memory: createMemoryHelpers(),
1344
+ event: {
1345
+ name: context.event || "manual"
1346
+ },
1347
+ forEach: forEachMeta
1348
+ };
1349
+ if (context.debug) {
1350
+ logger.info(
1351
+ `[Routing] evaluateGoto: checkId=${checkId}, outputs_history keys=${Object.keys(outputsHistory).join(",")}`
1352
+ );
1353
+ for (const [key, values] of Object.entries(outputsHistory)) {
1354
+ logger.info(`[Routing] ${key}: ${values.length} items`);
1355
+ }
1356
+ }
1357
+ const code = `
1358
+ const step = scope.step;
1359
+ const outputs = scope.outputs;
1360
+ const outputs_history = scope.outputs_history;
1361
+ const output = scope.output;
1362
+ const memory = scope.memory;
1363
+ const event = scope.event;
1364
+ const forEach = scope.forEach;
1365
+ const log = (...args) => console.log('\u{1F50D} Debug:', ...args);
1366
+ ${gotoJs}
1367
+ `;
1368
+ try {
1369
+ const evalResult = compileAndRun(
1370
+ sandbox,
1371
+ code,
1372
+ { scope: scopeObj },
1373
+ { injectLog: false, wrapFunction: true }
1374
+ );
1375
+ if (context.debug) {
1376
+ logger.info(`[Routing] evaluateGoto result: ${evalResult}`);
1377
+ }
1378
+ if (typeof evalResult === "string" && evalResult) {
1379
+ return evalResult;
1380
+ }
1381
+ } catch (error) {
1382
+ const msg = error instanceof Error ? error.message : String(error);
1383
+ logger.error(`[Routing] Error in goto_js sandbox evaluation: ${msg}`);
1384
+ }
1385
+ } catch (error) {
1386
+ const msg = error instanceof Error ? error.message : String(error);
1387
+ logger.error(`[Routing] Error evaluating goto_js: ${msg}`);
1388
+ if (gotoStatic) {
1389
+ logger.info(`[Routing] Falling back to static goto: ${gotoStatic}`);
1390
+ return gotoStatic;
1391
+ }
1392
+ }
1393
+ }
1394
+ return gotoStatic || null;
1395
+ }
1396
+ async function evaluateTransitions(transitions, checkId, checkConfig, result, context, _state) {
1397
+ if (!transitions || transitions.length === 0) return void 0;
1398
+ try {
1399
+ const sandbox = createSecureSandbox();
1400
+ const historyLimit = getHistoryLimit();
1401
+ const snapshotId = context.journal.beginSnapshot();
1402
+ const ContextView2 = (init_snapshot_store(), __toCommonJS(snapshot_store_exports)).ContextView;
1403
+ const view = new ContextView2(context.journal, context.sessionId, snapshotId, [], void 0);
1404
+ const outputsRecord = {};
1405
+ const outputsHistory = {};
1406
+ const allEntries = context.journal.readVisible(context.sessionId, snapshotId, void 0);
1407
+ const uniqueCheckIds = new Set(allEntries.map((e) => e.checkId));
1408
+ for (const cid of uniqueCheckIds) {
1409
+ try {
1410
+ const jr = view.get(cid);
1411
+ if (jr) outputsRecord[cid] = jr.output !== void 0 ? jr.output : jr;
1412
+ } catch {
1413
+ }
1414
+ try {
1415
+ const hist = view.getHistory(cid);
1416
+ if (hist && hist.length > 0) {
1417
+ const trimmed = historyLimit && hist.length > historyLimit ? hist.slice(hist.length - historyLimit) : hist;
1418
+ outputsHistory[cid] = trimmed.map((r) => r.output !== void 0 ? r.output : r);
1419
+ }
1420
+ } catch {
1421
+ }
1422
+ }
1423
+ outputsRecord.history = outputsHistory;
1424
+ const scopeObj = {
1425
+ step: { id: checkId, tags: checkConfig.tags || [], group: checkConfig.group },
1426
+ outputs: outputsRecord,
1427
+ outputs_history: outputsHistory,
1428
+ output: result?.output,
1429
+ memory: createMemoryHelpers(),
1430
+ event: { name: context.event || "manual" }
1431
+ };
1432
+ for (const rule of transitions) {
1433
+ const helpers = `
1434
+ const any = (arr, pred) => Array.isArray(arr) && arr.some(x => pred(x));
1435
+ const all = (arr, pred) => Array.isArray(arr) && arr.every(x => pred(x));
1436
+ const none = (arr, pred) => Array.isArray(arr) && !arr.some(x => pred(x));
1437
+ const count = (arr, pred) => Array.isArray(arr) ? arr.filter(x => pred(x)).length : 0;
1438
+ `;
1439
+ const code = `
1440
+ ${helpers}
1441
+ const step = scope.step;
1442
+ const outputs = scope.outputs;
1443
+ const outputs_history = scope.outputs_history;
1444
+ const output = scope.output;
1445
+ const memory = scope.memory;
1446
+ const event = scope.event;
1447
+ const __eval = () => { return (${rule.when}); };
1448
+ return __eval();
1449
+ `;
1450
+ let matched;
1451
+ try {
1452
+ matched = compileAndRun(
1453
+ sandbox,
1454
+ code,
1455
+ { scope: scopeObj },
1456
+ { injectLog: false, wrapFunction: false }
1457
+ );
1458
+ } catch (error) {
1459
+ const msg = error instanceof Error ? error.message : String(error);
1460
+ logger.warn(`[Routing] Error evaluating transition 'when' clause: ${msg}`);
1461
+ matched = false;
1462
+ }
1463
+ if (matched) {
1464
+ if (rule.to === null) return null;
1465
+ if (typeof rule.to === "string" && rule.to.length > 0) {
1466
+ return { to: rule.to, goto_event: rule.goto_event };
1467
+ }
1468
+ return null;
1469
+ }
1470
+ }
1471
+ return void 0;
1472
+ } catch (err) {
1473
+ logger.error(
1474
+ `[Routing] Error evaluating transitions: ${err instanceof Error ? err.message : String(err)}`
1475
+ );
1476
+ return void 0;
1477
+ }
1478
+ }
1479
+ var DEFAULT_MAX_LOOPS;
1480
+ var init_routing = __esm({
1481
+ "src/state-machine/states/routing.ts"() {
1482
+ init_logger();
1483
+ init_trace_helpers();
1484
+ init_failure_condition_evaluator();
1485
+ init_sandbox();
1486
+ init_memory_store();
1487
+ init_liquid_extensions();
1488
+ DEFAULT_MAX_LOOPS = 10;
1489
+ }
1490
+ });
1491
+
1492
+ export {
1493
+ ExecutionJournal,
1494
+ snapshot_store_exports,
1495
+ init_snapshot_store,
1496
+ handleRouting,
1497
+ checkLoopBudget,
1498
+ evaluateGoto,
1499
+ evaluateTransitions,
1500
+ init_routing
1501
+ };
1502
+ //# sourceMappingURL=chunk-ANUT54HW.mjs.map