@probelabs/visor 0.1.124 → 0.1.126

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