@nagi-js/core 0.1.1-rc.7 → 0.1.1-rc.9
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.
- package/dist/index.d.ts +49 -917
- package/dist/index.js +259 -402
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3,9 +3,6 @@ var DEF = "__def";
|
|
|
3
3
|
function attachDef(meta, def) {
|
|
4
4
|
return { kind: meta.kind, id: meta.id, [DEF]: def };
|
|
5
5
|
}
|
|
6
|
-
function attachDefMut(step, def) {
|
|
7
|
-
step[DEF] = def;
|
|
8
|
-
}
|
|
9
6
|
function getDef(step) {
|
|
10
7
|
const def = step[DEF];
|
|
11
8
|
if (def === void 0) {
|
|
@@ -16,26 +13,15 @@ function getDef(step) {
|
|
|
16
13
|
return def;
|
|
17
14
|
}
|
|
18
15
|
function needsStepIds(def) {
|
|
19
|
-
|
|
20
|
-
for (const upstream of Object.values(def.needs)) {
|
|
21
|
-
if (upstream && typeof upstream === "object" && "id" in upstream && typeof upstream.id === "string") {
|
|
22
|
-
out.push(upstream.id);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return out;
|
|
16
|
+
return Object.values(def.needs).map((upstream) => upstream.id);
|
|
26
17
|
}
|
|
27
18
|
function resolveNeeds(def, loadOutput) {
|
|
28
19
|
const result = {};
|
|
29
20
|
for (const [localKey, upstream] of Object.entries(def.needs)) {
|
|
30
|
-
|
|
31
|
-
result[localKey] = loadOutput(upstream.id);
|
|
32
|
-
}
|
|
21
|
+
result[localKey] = loadOutput(upstream.id);
|
|
33
22
|
}
|
|
34
23
|
return result;
|
|
35
24
|
}
|
|
36
|
-
function matchArms(def) {
|
|
37
|
-
return def.mode === "discriminator" ? Object.values(def.arms) : def.arms;
|
|
38
|
-
}
|
|
39
25
|
function readSelectedArm(matchId, runState) {
|
|
40
26
|
let selected = null;
|
|
41
27
|
for (const fact of runState.facts) {
|
|
@@ -48,41 +34,17 @@ function readSelectedArm(matchId, runState) {
|
|
|
48
34
|
return selected;
|
|
49
35
|
}
|
|
50
36
|
function selectArm(def, args) {
|
|
51
|
-
if (def.mode === "discriminator") {
|
|
52
|
-
const key = def.on(args);
|
|
53
|
-
if (def.arms[key] === void 0) {
|
|
54
|
-
const available = Object.keys(def.arms).join(", ");
|
|
55
|
-
throw new Error(
|
|
56
|
-
`match: discriminator returned "${key}" which has no arm (available: ${available || "<none>"})`
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
return key;
|
|
60
|
-
}
|
|
61
37
|
for (const arm of def.arms) {
|
|
62
38
|
if (arm.otherwise) return arm.id;
|
|
63
39
|
if (arm.when?.(args)) return arm.id;
|
|
64
40
|
}
|
|
65
41
|
throw new Error(
|
|
66
|
-
`match: no
|
|
42
|
+
`match: no arm matched and no { otherwise: true } fallback was provided`
|
|
67
43
|
);
|
|
68
44
|
}
|
|
69
45
|
|
|
70
46
|
// src/builder.ts
|
|
71
|
-
var BUILDER_BRAND = /* @__PURE__ */ Symbol.for("nagi.builder");
|
|
72
|
-
function isBuilder(value) {
|
|
73
|
-
return typeof value === "object" && value !== null && value[BUILDER_BRAND] === true;
|
|
74
|
-
}
|
|
75
|
-
function resolveBuildResult(value) {
|
|
76
|
-
if (isBuilder(value)) {
|
|
77
|
-
return Object.fromEntries(value.__steps);
|
|
78
|
-
}
|
|
79
|
-
return value;
|
|
80
|
-
}
|
|
81
|
-
function isDiscriminator(config) {
|
|
82
|
-
return "cases" in config && "on" in config;
|
|
83
|
-
}
|
|
84
47
|
function makeBuilder() {
|
|
85
|
-
const chainSteps = /* @__PURE__ */ new Map();
|
|
86
48
|
function task(config) {
|
|
87
49
|
const def = {
|
|
88
50
|
kind: "task",
|
|
@@ -132,29 +94,11 @@ function makeBuilder() {
|
|
|
132
94
|
);
|
|
133
95
|
}
|
|
134
96
|
function match(config) {
|
|
135
|
-
const needs =
|
|
136
|
-
if (isDiscriminator(config)) {
|
|
137
|
-
const arms2 = {};
|
|
138
|
-
for (const [caseKey, build] of Object.entries(config.cases)) {
|
|
139
|
-
const nested = resolveBuildResult(
|
|
140
|
-
build(makeBuilder())
|
|
141
|
-
);
|
|
142
|
-
arms2[caseKey] = { id: caseKey, stepIds: [], _nested: nested };
|
|
143
|
-
}
|
|
144
|
-
const def2 = {
|
|
145
|
-
kind: "match",
|
|
146
|
-
mode: "discriminator",
|
|
147
|
-
needs,
|
|
148
|
-
on: config.on,
|
|
149
|
-
arms: arms2
|
|
150
|
-
};
|
|
151
|
-
return attachDef({ kind: "match", id: "" }, def2);
|
|
152
|
-
}
|
|
153
|
-
const guardConfig = config;
|
|
97
|
+
const needs = config.needs ?? {};
|
|
154
98
|
const arms = [];
|
|
155
|
-
for (let i = 0; i <
|
|
156
|
-
const arm =
|
|
157
|
-
const nested =
|
|
99
|
+
for (let i = 0; i < config.arms.length; i++) {
|
|
100
|
+
const arm = config.arms[i];
|
|
101
|
+
const nested = arm.build(makeBuilder());
|
|
158
102
|
const armId = arm.otherwise ? "otherwise" : `arm${i}`;
|
|
159
103
|
arms.push({
|
|
160
104
|
id: armId,
|
|
@@ -162,81 +106,25 @@ function makeBuilder() {
|
|
|
162
106
|
when: arm.when
|
|
163
107
|
} : {},
|
|
164
108
|
...arm.otherwise ? { otherwise: true } : {},
|
|
165
|
-
|
|
166
|
-
_nested: nested
|
|
109
|
+
nested
|
|
167
110
|
});
|
|
168
111
|
}
|
|
169
|
-
const def = {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
};
|
|
175
|
-
return attachDef({ kind: "match", id: "" }, def);
|
|
176
|
-
}
|
|
177
|
-
function step(key, config) {
|
|
178
|
-
if (chainSteps.has(key)) {
|
|
179
|
-
throw new Error(
|
|
180
|
-
`b.step: duplicate key "${key}" \u2014 each chain entry must have a unique key.`
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
const shell = { kind: "task", id: "" };
|
|
184
|
-
const needs = {};
|
|
185
|
-
if (config.needs) {
|
|
186
|
-
for (const sibKey of config.needs) {
|
|
187
|
-
const sibling = chainSteps.get(sibKey);
|
|
188
|
-
if (sibling === void 0) {
|
|
189
|
-
throw new Error(
|
|
190
|
-
`b.step("${key}"): unknown sibling "${sibKey}". Available keys so far: ${[...chainSteps.keys()].join(", ") || "<none>"}.`
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
needs[sibKey] = sibling;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
const def = {
|
|
197
|
-
kind: "task",
|
|
198
|
-
needs,
|
|
199
|
-
...config.retry !== void 0 ? { retry: config.retry } : {},
|
|
200
|
-
...config.timeoutMs !== void 0 ? { timeoutMs: config.timeoutMs } : {},
|
|
201
|
-
...config.when !== void 0 ? {
|
|
202
|
-
when: config.when
|
|
203
|
-
} : {},
|
|
204
|
-
run: config.run,
|
|
205
|
-
...config.onStart !== void 0 ? { onStart: config.onStart } : {},
|
|
206
|
-
...config.onComplete !== void 0 ? {
|
|
207
|
-
onComplete: config.onComplete
|
|
208
|
-
} : {},
|
|
209
|
-
...config.onError !== void 0 ? { onError: config.onError } : {},
|
|
210
|
-
...config.onRetry !== void 0 ? { onRetry: config.onRetry } : {}
|
|
211
|
-
};
|
|
212
|
-
attachDefMut(shell, def);
|
|
213
|
-
chainSteps.set(key, shell);
|
|
214
|
-
return builder;
|
|
215
|
-
}
|
|
216
|
-
function include(key, s) {
|
|
217
|
-
if (chainSteps.has(key)) {
|
|
218
|
-
throw new Error(
|
|
219
|
-
`b.include: duplicate key "${key}" \u2014 each chain entry must have a unique key.`
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
chainSteps.set(key, s);
|
|
223
|
-
return builder;
|
|
112
|
+
const def = { kind: "match", needs, arms };
|
|
113
|
+
return attachDef(
|
|
114
|
+
{ kind: "match", id: "" },
|
|
115
|
+
def
|
|
116
|
+
);
|
|
224
117
|
}
|
|
225
|
-
|
|
118
|
+
return {
|
|
226
119
|
task,
|
|
227
120
|
signal,
|
|
228
121
|
subflow,
|
|
229
|
-
match
|
|
230
|
-
step,
|
|
231
|
-
include,
|
|
232
|
-
[BUILDER_BRAND]: true,
|
|
233
|
-
__steps: chainSteps
|
|
122
|
+
match
|
|
234
123
|
};
|
|
235
|
-
return builder;
|
|
236
124
|
}
|
|
237
125
|
function flow(config) {
|
|
238
126
|
const builder = makeBuilder();
|
|
239
|
-
const built =
|
|
127
|
+
const built = config.build(builder);
|
|
240
128
|
const idByIdentity = /* @__PURE__ */ new Map();
|
|
241
129
|
collectIds(built, "", idByIdentity);
|
|
242
130
|
const finalSteps = {};
|
|
@@ -268,10 +156,9 @@ function collectIds(map, prefix, idByIdentity) {
|
|
|
268
156
|
idByIdentity.set(step, id);
|
|
269
157
|
const def = step.__def;
|
|
270
158
|
if (def?.kind === "match") {
|
|
271
|
-
const
|
|
272
|
-
for (const arm of arms) {
|
|
273
|
-
|
|
274
|
-
collectIds(arm._nested, `${id}.${arm.id}`, idByIdentity);
|
|
159
|
+
const pending = def;
|
|
160
|
+
for (const arm of pending.arms) {
|
|
161
|
+
collectIds(arm.nested, `${id}.${arm.id}`, idByIdentity);
|
|
275
162
|
}
|
|
276
163
|
}
|
|
277
164
|
}
|
|
@@ -303,24 +190,22 @@ function walkAndRewrite(args) {
|
|
|
303
190
|
rewrittenNeeds[localKey] = { ...upstream, id: upstreamId };
|
|
304
191
|
}
|
|
305
192
|
if (def.kind === "match") {
|
|
306
|
-
const
|
|
193
|
+
const pending = def;
|
|
307
194
|
const finalizedArms = [];
|
|
308
|
-
for (const arm of
|
|
195
|
+
for (const arm of pending.arms) {
|
|
309
196
|
const armPrefix = `${id}.${arm.id}`;
|
|
310
197
|
const nestedStepIds = [];
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
nestedStepIds.push(`${armPrefix}.${nestedKey}`);
|
|
314
|
-
}
|
|
315
|
-
walkAndRewrite({
|
|
316
|
-
flowId,
|
|
317
|
-
map: arm._nested,
|
|
318
|
-
prefix: armPrefix,
|
|
319
|
-
parentMatch: { matchId: id, armId: arm.id },
|
|
320
|
-
idByIdentity,
|
|
321
|
-
out
|
|
322
|
-
});
|
|
198
|
+
for (const nestedKey of Object.keys(arm.nested)) {
|
|
199
|
+
nestedStepIds.push(`${armPrefix}.${nestedKey}`);
|
|
323
200
|
}
|
|
201
|
+
walkAndRewrite({
|
|
202
|
+
flowId,
|
|
203
|
+
map: arm.nested,
|
|
204
|
+
prefix: armPrefix,
|
|
205
|
+
parentMatch: { matchId: id, armId: arm.id },
|
|
206
|
+
idByIdentity,
|
|
207
|
+
out
|
|
208
|
+
});
|
|
324
209
|
finalizedArms.push({
|
|
325
210
|
id: arm.id,
|
|
326
211
|
...arm.when !== void 0 ? { when: arm.when } : {},
|
|
@@ -328,16 +213,8 @@ function walkAndRewrite(args) {
|
|
|
328
213
|
stepIds: nestedStepIds
|
|
329
214
|
});
|
|
330
215
|
}
|
|
331
|
-
const finalizedDef2 =
|
|
216
|
+
const finalizedDef2 = {
|
|
332
217
|
kind: "match",
|
|
333
|
-
mode: "discriminator",
|
|
334
|
-
needs: rewrittenNeeds,
|
|
335
|
-
on: def.on,
|
|
336
|
-
arms: Object.fromEntries(finalizedArms.map((a) => [a.id, a])),
|
|
337
|
-
...parentMatch ? { parentMatch } : {}
|
|
338
|
-
} : {
|
|
339
|
-
kind: "match",
|
|
340
|
-
mode: "guard",
|
|
341
218
|
needs: rewrittenNeeds,
|
|
342
219
|
arms: finalizedArms,
|
|
343
220
|
...parentMatch ? { parentMatch } : {}
|
|
@@ -383,7 +260,7 @@ function warnSourceHashOnce() {
|
|
|
383
260
|
if (warnedAboutSourceHash) return;
|
|
384
261
|
warnedAboutSourceHash = true;
|
|
385
262
|
console.warn(
|
|
386
|
-
"[nagi] canonicalize: hashing `when`/schema source via Function.prototype.toString(). Cosmetic source changes (minification, whitespace, identifier renames) will flip the flow hash even when behavior is unchanged.
|
|
263
|
+
"[nagi] canonicalize: hashing `when`/schema source via Function.prototype.toString(). Cosmetic source changes (minification, whitespace, identifier renames) will flip the flow hash even when behavior is unchanged."
|
|
387
264
|
);
|
|
388
265
|
}
|
|
389
266
|
async function canonicalize(flow2) {
|
|
@@ -435,12 +312,8 @@ async function canonicalizeSubflow(base, def) {
|
|
|
435
312
|
}
|
|
436
313
|
async function canonicalizeMatch(base, def) {
|
|
437
314
|
const out = { ...base };
|
|
438
|
-
out.matchMode = def.mode;
|
|
439
|
-
if (def.mode === "discriminator") {
|
|
440
|
-
out.matchOnHash = await hashFnSource(def.on);
|
|
441
|
-
}
|
|
442
315
|
const arms = [];
|
|
443
|
-
for (const arm of
|
|
316
|
+
for (const arm of def.arms) {
|
|
444
317
|
arms.push(await canonicalizeArm(arm));
|
|
445
318
|
}
|
|
446
319
|
out.matchArms = arms;
|
|
@@ -581,12 +454,6 @@ function diffFields(before, after, out) {
|
|
|
581
454
|
if (!schemaEq(before.signalSchema, after.signalSchema)) {
|
|
582
455
|
out.push({ stepId: id, field: "signalSchema" });
|
|
583
456
|
}
|
|
584
|
-
if (before.matchMode !== after.matchMode) {
|
|
585
|
-
out.push({ stepId: id, field: "matchMode" });
|
|
586
|
-
}
|
|
587
|
-
if (before.matchOnHash !== after.matchOnHash) {
|
|
588
|
-
out.push({ stepId: id, field: "matchOn" });
|
|
589
|
-
}
|
|
590
457
|
if (!armsEq(before.matchArms, after.matchArms)) {
|
|
591
458
|
out.push({ stepId: id, field: "matchArms" });
|
|
592
459
|
}
|
|
@@ -633,27 +500,9 @@ var InMemoryStore = class {
|
|
|
633
500
|
snapshots = /* @__PURE__ */ new Map();
|
|
634
501
|
refs = /* @__PURE__ */ new Map();
|
|
635
502
|
globalFacts = [];
|
|
636
|
-
/**
|
|
637
|
-
* The runId currently holding the active slot for `flowId::concurrencyKey`.
|
|
638
|
-
* Updated on `tryStartRun` (set when concurrency is supplied) and on
|
|
639
|
-
* terminal facts (cleared via {@link keyByActiveRun}).
|
|
640
|
-
*/
|
|
641
503
|
activeByKey = /* @__PURE__ */ new Map();
|
|
642
|
-
/** Reverse lookup so terminal facts can free the slot in O(1). */
|
|
643
504
|
keyByActiveRun = /* @__PURE__ */ new Map();
|
|
644
|
-
/**
|
|
645
|
-
* Index: parentRunId → set of child run ids spawned by `b.subflow()`.
|
|
646
|
-
* Populated in `tryStartRun` when `fact.parentRunId` is set. Used by
|
|
647
|
-
* `listChildren()` to power transitive cancel cascade in O(children).
|
|
648
|
-
*/
|
|
649
505
|
childrenByParent = /* @__PURE__ */ new Map();
|
|
650
|
-
/**
|
|
651
|
-
* Pruned-but-kept summaries. Populated by `pruneFacts` when `keepSummary`
|
|
652
|
-
* is true so that `queryRuns` still surfaces runs whose facts were dropped.
|
|
653
|
-
* Mirrors the postgres adapter's retained `workflow_run` row after a prune.
|
|
654
|
-
* Also blocks `tryStartRun` from reusing a pruned runId — matching the
|
|
655
|
-
* postgres `INSERT ... ON CONFLICT DO NOTHING` semantic on `run_id`.
|
|
656
|
-
*/
|
|
657
506
|
summaries = /* @__PURE__ */ new Map();
|
|
658
507
|
leaseMs;
|
|
659
508
|
constructor(opts = {}) {
|
|
@@ -691,6 +540,7 @@ var InMemoryStore = class {
|
|
|
691
540
|
if (priorRunId !== void 0) {
|
|
692
541
|
const cancelFact = {
|
|
693
542
|
kind: "flow.canceled",
|
|
543
|
+
cause: "concurrency",
|
|
694
544
|
runId: priorRunId,
|
|
695
545
|
at: fact.at,
|
|
696
546
|
canceledByRunId: runId,
|
|
@@ -772,7 +622,6 @@ var InMemoryStore = class {
|
|
|
772
622
|
async appendGlobalFact(fact) {
|
|
773
623
|
this.globalFacts.push(fact);
|
|
774
624
|
}
|
|
775
|
-
/** Test-only: peek at the in-memory global fact log. */
|
|
776
625
|
readGlobalFacts() {
|
|
777
626
|
return this.globalFacts;
|
|
778
627
|
}
|
|
@@ -1106,7 +955,6 @@ var InMemoryClock = class {
|
|
|
1106
955
|
}, delay);
|
|
1107
956
|
this.timers.set(key, handle);
|
|
1108
957
|
}
|
|
1109
|
-
/** Clear any pending scheduled timers. Call from test teardown. */
|
|
1110
958
|
dispose() {
|
|
1111
959
|
for (const t of this.timers.values()) clearTimeout(t);
|
|
1112
960
|
this.timers.clear();
|
|
@@ -1120,10 +968,6 @@ var InMemoryTrigger = class {
|
|
|
1120
968
|
this.handlers = this.handlers.filter((h) => h !== handler);
|
|
1121
969
|
};
|
|
1122
970
|
}
|
|
1123
|
-
/**
|
|
1124
|
-
* Test-only: synchronously dispatch a wake-up to all subscribers.
|
|
1125
|
-
* Real triggers fire from a Store change (NOTIFY/LISTEN, polling, etc.).
|
|
1126
|
-
*/
|
|
1127
971
|
fire(runId) {
|
|
1128
972
|
for (const h of this.handlers) h(runId);
|
|
1129
973
|
}
|
|
@@ -1183,9 +1027,6 @@ function nextRunnable(args) {
|
|
|
1183
1027
|
}
|
|
1184
1028
|
function checkUpstream(def, runState) {
|
|
1185
1029
|
for (const upstream of Object.values(def.needs)) {
|
|
1186
|
-
if (!upstream || typeof upstream !== "object" || !("id" in upstream) || typeof upstream.id !== "string") {
|
|
1187
|
-
continue;
|
|
1188
|
-
}
|
|
1189
1030
|
const upstreamState = runState.steps[upstream.id];
|
|
1190
1031
|
if (upstreamState === void 0) return "blocked";
|
|
1191
1032
|
if (upstreamState.status === "completed") continue;
|
|
@@ -1229,7 +1070,7 @@ function aggregateMatch(matchId, flow2, runState) {
|
|
|
1229
1070
|
if (def.kind !== "match") return { kind: "pending" };
|
|
1230
1071
|
const selected = readSelectedArm(matchId, runState);
|
|
1231
1072
|
if (selected === null) return { kind: "pending" };
|
|
1232
|
-
const arm = def.
|
|
1073
|
+
const arm = def.arms.find((a) => a.id === selected);
|
|
1233
1074
|
if (!arm) return { kind: "pending" };
|
|
1234
1075
|
const output = {};
|
|
1235
1076
|
let allTerminal = true;
|
|
@@ -1262,6 +1103,75 @@ function flowTermination(flow2, runState) {
|
|
|
1262
1103
|
}
|
|
1263
1104
|
return { done, failed };
|
|
1264
1105
|
}
|
|
1106
|
+
function nextTransition(flow2, runState) {
|
|
1107
|
+
const promotions = readyPromotions(flow2, runState);
|
|
1108
|
+
if (promotions.length > 0) return { kind: "promote-match", promotions };
|
|
1109
|
+
const term = flowTermination(flow2, runState);
|
|
1110
|
+
if (term.done) {
|
|
1111
|
+
if (isFlowTerminal(runState.facts)) return { kind: "settled" };
|
|
1112
|
+
if (term.failed) return { kind: "fail", error: flowFailureError(runState) };
|
|
1113
|
+
return { kind: "complete", output: computeFlowOutput(flow2, runState) };
|
|
1114
|
+
}
|
|
1115
|
+
const input = extractInput(runState);
|
|
1116
|
+
const { runnable, skip } = nextRunnable({ flow: flow2, runState, input });
|
|
1117
|
+
if (runnable.length > 0) return { kind: "dispatch", runnable, skip };
|
|
1118
|
+
if (skip.length > 0) return { kind: "skip", skip };
|
|
1119
|
+
return { kind: "waiting" };
|
|
1120
|
+
}
|
|
1121
|
+
function readyPromotions(flow2, runState) {
|
|
1122
|
+
const out = [];
|
|
1123
|
+
for (const [matchId, step] of Object.entries(flow2.steps)) {
|
|
1124
|
+
const def = getDef(step);
|
|
1125
|
+
if (def.kind !== "match") continue;
|
|
1126
|
+
const state = runState.steps[matchId];
|
|
1127
|
+
if (state?.status !== "running") continue;
|
|
1128
|
+
const agg = aggregateMatch(matchId, flow2, runState);
|
|
1129
|
+
if (agg.kind === "pending") continue;
|
|
1130
|
+
const attempt = state.attempts > 0 ? state.attempts : 1;
|
|
1131
|
+
if (agg.kind === "fail-fast") {
|
|
1132
|
+
const failedNested = runState.steps[agg.failedStepId];
|
|
1133
|
+
const error = failedNested?.error ?? {
|
|
1134
|
+
name: "Error",
|
|
1135
|
+
message: `match "${matchId}": chosen-arm step "${agg.failedStepId}" failed`
|
|
1136
|
+
};
|
|
1137
|
+
out.push({
|
|
1138
|
+
matchId,
|
|
1139
|
+
attempt,
|
|
1140
|
+
result: { kind: "fail", error }
|
|
1141
|
+
});
|
|
1142
|
+
} else {
|
|
1143
|
+
out.push({
|
|
1144
|
+
matchId,
|
|
1145
|
+
attempt,
|
|
1146
|
+
result: { kind: "complete", output: agg.output }
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
return out;
|
|
1151
|
+
}
|
|
1152
|
+
function flowFailureError(runState) {
|
|
1153
|
+
const failedStep = Object.values(runState.steps).find(
|
|
1154
|
+
(s) => s.status === "failed"
|
|
1155
|
+
);
|
|
1156
|
+
return failedStep?.error ?? { name: "Error", message: "step failed" };
|
|
1157
|
+
}
|
|
1158
|
+
function isFlowTerminal(facts) {
|
|
1159
|
+
for (let i = facts.length - 1; i >= 0; i--) {
|
|
1160
|
+
const f = facts[i];
|
|
1161
|
+
if (f !== void 0 && (f.kind === "flow.completed" || f.kind === "flow.failed" || f.kind === "flow.canceled")) {
|
|
1162
|
+
return true;
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
return false;
|
|
1166
|
+
}
|
|
1167
|
+
function computeFlowOutput(flow2, runState) {
|
|
1168
|
+
if (flow2.output === void 0) return null;
|
|
1169
|
+
const stepOutputs = {};
|
|
1170
|
+
for (const [sid, sstate] of Object.entries(runState.steps)) {
|
|
1171
|
+
if (sstate.output !== void 0) stepOutputs[sid] = sstate.output;
|
|
1172
|
+
}
|
|
1173
|
+
return flow2.output(stepOutputs);
|
|
1174
|
+
}
|
|
1265
1175
|
function descendantsOf(flow2, stepId) {
|
|
1266
1176
|
const children = /* @__PURE__ */ new Map();
|
|
1267
1177
|
const addEdge = (from, to) => {
|
|
@@ -1273,7 +1183,7 @@ function descendantsOf(flow2, stepId) {
|
|
|
1273
1183
|
const def = getDef(step);
|
|
1274
1184
|
for (const upstreamId of needsStepIds(def)) addEdge(upstreamId, id);
|
|
1275
1185
|
if (def.kind === "match") {
|
|
1276
|
-
for (const arm of
|
|
1186
|
+
for (const arm of def.arms) {
|
|
1277
1187
|
for (const armStepId of arm.stepIds) addEdge(id, armStepId);
|
|
1278
1188
|
}
|
|
1279
1189
|
}
|
|
@@ -1551,13 +1461,8 @@ var NagiAbortError = class extends Error {
|
|
|
1551
1461
|
}
|
|
1552
1462
|
};
|
|
1553
1463
|
async function executeSubflow(args) {
|
|
1554
|
-
const { deps, def, runId, stepId } = args;
|
|
1464
|
+
const { deps, message, def, runId, stepId } = args;
|
|
1555
1465
|
const { store } = deps;
|
|
1556
|
-
if (deps.lookupFlow === void 0 || deps.startChildRun === void 0) {
|
|
1557
|
-
throw new Error(
|
|
1558
|
-
`subflow dispatch requires DispatchDeps.lookupFlow + .startChildRun (step "${stepId}" referenced child "${def.childFlowId}")`
|
|
1559
|
-
);
|
|
1560
|
-
}
|
|
1561
1466
|
const child = deps.lookupFlow(def.childFlowId);
|
|
1562
1467
|
if (child === void 0) {
|
|
1563
1468
|
throw new Error(
|
|
@@ -1572,7 +1477,8 @@ async function executeSubflow(args) {
|
|
|
1572
1477
|
child,
|
|
1573
1478
|
childInput,
|
|
1574
1479
|
parentRunId: runId,
|
|
1575
|
-
parentStepId: stepId
|
|
1480
|
+
parentStepId: stepId,
|
|
1481
|
+
parentStepAttempt: message.attempt
|
|
1576
1482
|
});
|
|
1577
1483
|
}
|
|
1578
1484
|
async function executeMatch(args) {
|
|
@@ -1599,7 +1505,7 @@ async function handleStepError(args) {
|
|
|
1599
1505
|
const runIsCanceled = postState.status === "canceled";
|
|
1600
1506
|
const stepAborted = hasAbortRequest(postState.facts, stepId, attempt);
|
|
1601
1507
|
if (runIsCanceled || stepAborted) {
|
|
1602
|
-
const isAbort = err instanceof
|
|
1508
|
+
const isAbort = err instanceof NagiAbortError || err instanceof Error && err.name === "AbortError";
|
|
1603
1509
|
const canceledFact = {
|
|
1604
1510
|
kind: "step.canceled",
|
|
1605
1511
|
runId,
|
|
@@ -1668,46 +1574,31 @@ async function handleStepError(args) {
|
|
|
1668
1574
|
}
|
|
1669
1575
|
var MAX_ADVANCE_ITERS = 1024;
|
|
1670
1576
|
async function advance(deps, runId) {
|
|
1671
|
-
const { store, queue
|
|
1577
|
+
const { store, queue } = deps;
|
|
1672
1578
|
const flow2 = await deps.flowFor(runId);
|
|
1673
1579
|
for (let iter = 0; iter < MAX_ADVANCE_ITERS; iter++) {
|
|
1674
1580
|
const runState = await store.loadRunState(runId);
|
|
1675
|
-
const
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
if (decision.skip.length === 0 && decision.runnable.length === 0) return;
|
|
1697
|
-
for (const { stepId, reason } of decision.skip) {
|
|
1698
|
-
await store.appendFact(runId, {
|
|
1699
|
-
kind: "step.skipped",
|
|
1700
|
-
runId,
|
|
1701
|
-
stepId,
|
|
1702
|
-
reason,
|
|
1703
|
-
at: clock.now()
|
|
1704
|
-
});
|
|
1705
|
-
}
|
|
1706
|
-
for (const stepId of decision.runnable) {
|
|
1707
|
-
await queue.enqueue(runId, stepId);
|
|
1708
|
-
}
|
|
1709
|
-
if (decision.runnable.length > 0) {
|
|
1710
|
-
return;
|
|
1581
|
+
const t = nextTransition(flow2, runState);
|
|
1582
|
+
switch (t.kind) {
|
|
1583
|
+
case "settled":
|
|
1584
|
+
case "waiting":
|
|
1585
|
+
return;
|
|
1586
|
+
case "complete":
|
|
1587
|
+
await finalizeFlowCompletion({ deps, flow: flow2, runId, output: t.output });
|
|
1588
|
+
return;
|
|
1589
|
+
case "fail":
|
|
1590
|
+
await finalizeFlowFailure({ deps, flow: flow2, runId, error: t.error });
|
|
1591
|
+
return;
|
|
1592
|
+
case "dispatch":
|
|
1593
|
+
await recordSkips(deps, runId, t.skip);
|
|
1594
|
+
for (const stepId of t.runnable) await queue.enqueue(runId, stepId);
|
|
1595
|
+
return;
|
|
1596
|
+
case "skip":
|
|
1597
|
+
await recordSkips(deps, runId, t.skip);
|
|
1598
|
+
continue;
|
|
1599
|
+
case "promote-match":
|
|
1600
|
+
await applyPromotions(deps, flow2, runState, t.promotions);
|
|
1601
|
+
continue;
|
|
1711
1602
|
}
|
|
1712
1603
|
}
|
|
1713
1604
|
const cycleError = {
|
|
@@ -1719,77 +1610,82 @@ async function advance(deps, runId) {
|
|
|
1719
1610
|
await finalizeFlowFailure({ deps, flow: flow2, runId, error: cycleError });
|
|
1720
1611
|
}
|
|
1721
1612
|
}
|
|
1722
|
-
async function
|
|
1723
|
-
const { deps, flow: flow2, runState } = args;
|
|
1613
|
+
async function recordSkips(deps, runId, skip) {
|
|
1724
1614
|
const { store, clock } = deps;
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1615
|
+
for (const { stepId, reason } of skip) {
|
|
1616
|
+
await store.appendFact(runId, {
|
|
1617
|
+
kind: "step.skipped",
|
|
1618
|
+
runId,
|
|
1619
|
+
stepId,
|
|
1620
|
+
reason,
|
|
1621
|
+
at: clock.now()
|
|
1622
|
+
});
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
async function markStepComplete(args) {
|
|
1626
|
+
const { deps, flow: flow2, runId, stepId, attempt, kind, output } = args;
|
|
1627
|
+
const at = deps.clock.now();
|
|
1628
|
+
const fact = {
|
|
1629
|
+
kind: "step.completed",
|
|
1630
|
+
runId,
|
|
1631
|
+
stepId,
|
|
1632
|
+
attempt,
|
|
1633
|
+
output,
|
|
1634
|
+
at
|
|
1635
|
+
};
|
|
1636
|
+
await deps.store.completeStep(runId, stepId, output, fact);
|
|
1637
|
+
await fireHook(
|
|
1638
|
+
deps.hooks?.onStepComplete,
|
|
1639
|
+
{
|
|
1640
|
+
runId,
|
|
1641
|
+
flowId: flow2.id,
|
|
1642
|
+
stepId,
|
|
1643
|
+
attempt,
|
|
1644
|
+
kind,
|
|
1645
|
+
output,
|
|
1646
|
+
durationMs: 0,
|
|
1647
|
+
at
|
|
1648
|
+
},
|
|
1649
|
+
"onStepComplete",
|
|
1650
|
+
deps
|
|
1651
|
+
);
|
|
1652
|
+
}
|
|
1653
|
+
async function markStepFail(args) {
|
|
1654
|
+
const { deps, flow: flow2, runId, stepId, attempt, kind, error } = args;
|
|
1655
|
+
const at = deps.clock.now();
|
|
1656
|
+
const fact = { kind: "step.failed", runId, stepId, attempt, error, at };
|
|
1657
|
+
await deps.store.failStep(runId, stepId, error, fact);
|
|
1658
|
+
await fireHook(
|
|
1659
|
+
deps.hooks?.onStepError,
|
|
1660
|
+
{ runId, flowId: flow2.id, stepId, attempt, kind, error, at },
|
|
1661
|
+
"onStepError",
|
|
1662
|
+
deps
|
|
1663
|
+
);
|
|
1664
|
+
}
|
|
1665
|
+
async function applyPromotions(deps, flow2, runState, promotions) {
|
|
1666
|
+
for (const { matchId, attempt, result } of promotions) {
|
|
1667
|
+
if (result.kind === "fail") {
|
|
1668
|
+
await markStepFail({
|
|
1669
|
+
deps,
|
|
1670
|
+
flow: flow2,
|
|
1742
1671
|
runId: runState.runId,
|
|
1743
1672
|
stepId: matchId,
|
|
1744
1673
|
attempt,
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
};
|
|
1748
|
-
|
|
1749
|
-
await
|
|
1750
|
-
deps
|
|
1751
|
-
|
|
1752
|
-
runId: runState.runId,
|
|
1753
|
-
flowId: flow2.id,
|
|
1754
|
-
stepId: matchId,
|
|
1755
|
-
attempt,
|
|
1756
|
-
kind: "match",
|
|
1757
|
-
error,
|
|
1758
|
-
at: clock.now()
|
|
1759
|
-
},
|
|
1760
|
-
"onStepError",
|
|
1761
|
-
deps
|
|
1762
|
-
);
|
|
1763
|
-
promoted = true;
|
|
1764
|
-
continue;
|
|
1765
|
-
}
|
|
1766
|
-
const fact = {
|
|
1767
|
-
kind: "step.completed",
|
|
1768
|
-
runId: runState.runId,
|
|
1769
|
-
stepId: matchId,
|
|
1770
|
-
attempt,
|
|
1771
|
-
output: agg.output,
|
|
1772
|
-
at: clock.now()
|
|
1773
|
-
};
|
|
1774
|
-
await store.completeStep(runState.runId, matchId, agg.output, fact);
|
|
1775
|
-
await fireHook(
|
|
1776
|
-
deps.hooks?.onStepComplete,
|
|
1777
|
-
{
|
|
1674
|
+
kind: "match",
|
|
1675
|
+
error: result.error
|
|
1676
|
+
});
|
|
1677
|
+
} else {
|
|
1678
|
+
await markStepComplete({
|
|
1679
|
+
deps,
|
|
1680
|
+
flow: flow2,
|
|
1778
1681
|
runId: runState.runId,
|
|
1779
|
-
flowId: flow2.id,
|
|
1780
1682
|
stepId: matchId,
|
|
1781
1683
|
attempt,
|
|
1782
1684
|
kind: "match",
|
|
1783
|
-
output:
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
},
|
|
1787
|
-
"onStepComplete",
|
|
1788
|
-
deps
|
|
1789
|
-
);
|
|
1790
|
-
promoted = true;
|
|
1685
|
+
output: result.output
|
|
1686
|
+
});
|
|
1687
|
+
}
|
|
1791
1688
|
}
|
|
1792
|
-
return promoted;
|
|
1793
1689
|
}
|
|
1794
1690
|
function makeStepCtx(args) {
|
|
1795
1691
|
const { runId, stepId, attempt, input, store, clock, tx, signal } = args;
|
|
@@ -1800,11 +1696,6 @@ function makeStepCtx(args) {
|
|
|
1800
1696
|
attempt,
|
|
1801
1697
|
signal,
|
|
1802
1698
|
now: () => clock.now(),
|
|
1803
|
-
// `tx` is supplied by `Store.runStep` — for adapters with no real
|
|
1804
|
-
// transaction (in-memory) it is `undefined as Tx`, and handlers that
|
|
1805
|
-
// touch `ctx.tx` will throw on the first call. Adapters that need
|
|
1806
|
-
// typed transactional clients (e.g. `@nagi-js/postgres`) augment
|
|
1807
|
-
// `Register.tx` and pass a real Kysely transaction here.
|
|
1808
1699
|
tx,
|
|
1809
1700
|
logger: args.logger ?? consoleLogger(),
|
|
1810
1701
|
once: makeOnce({ runId, stepId, store }),
|
|
@@ -1835,15 +1726,6 @@ function retryAllows(policy, err) {
|
|
|
1835
1726
|
if (!policy.retryOn) return true;
|
|
1836
1727
|
return policy.retryOn(err);
|
|
1837
1728
|
}
|
|
1838
|
-
function isFlowTerminal(facts) {
|
|
1839
|
-
for (let i = facts.length - 1; i >= 0; i--) {
|
|
1840
|
-
const f = facts[i];
|
|
1841
|
-
if (f !== void 0 && (f.kind === "flow.completed" || f.kind === "flow.failed" || f.kind === "flow.canceled")) {
|
|
1842
|
-
return true;
|
|
1843
|
-
}
|
|
1844
|
-
}
|
|
1845
|
-
return false;
|
|
1846
|
-
}
|
|
1847
1729
|
async function finalizeFlowFailure(args) {
|
|
1848
1730
|
const { deps, flow: flow2, runId, error } = args;
|
|
1849
1731
|
const { store, clock } = deps;
|
|
@@ -1859,9 +1741,8 @@ async function finalizeFlowFailure(args) {
|
|
|
1859
1741
|
await propagateToParent(deps, runId, { kind: "failed", error });
|
|
1860
1742
|
}
|
|
1861
1743
|
async function finalizeFlowCompletion(args) {
|
|
1862
|
-
const { deps, flow: flow2, runId,
|
|
1744
|
+
const { deps, flow: flow2, runId, output } = args;
|
|
1863
1745
|
const { store, clock } = deps;
|
|
1864
|
-
const output = computeFlowOutput(flow2, runState);
|
|
1865
1746
|
await store.appendFact(runId, {
|
|
1866
1747
|
kind: "flow.completed",
|
|
1867
1748
|
runId,
|
|
@@ -1874,7 +1755,7 @@ async function finalizeFlowCompletion(args) {
|
|
|
1874
1755
|
await propagateToParent(deps, runId, { kind: "completed", output });
|
|
1875
1756
|
}
|
|
1876
1757
|
async function propagateToParent(deps, childRunId, outcome) {
|
|
1877
|
-
const { store
|
|
1758
|
+
const { store } = deps;
|
|
1878
1759
|
const childState = await store.loadRunState(childRunId);
|
|
1879
1760
|
const startedFact = childState.facts.find((f) => f.kind === "flow.started");
|
|
1880
1761
|
if (startedFact?.parentRunId === void 0 || startedFact?.parentStepId === void 0) {
|
|
@@ -1912,70 +1793,28 @@ async function propagateToParent(deps, childRunId, outcome) {
|
|
|
1912
1793
|
childRunId,
|
|
1913
1794
|
output: outcome.output
|
|
1914
1795
|
};
|
|
1915
|
-
|
|
1916
|
-
|
|
1796
|
+
await markStepComplete({
|
|
1797
|
+
deps,
|
|
1798
|
+
flow: parentFlow,
|
|
1917
1799
|
runId: parentRunId,
|
|
1918
1800
|
stepId: parentStepId,
|
|
1919
1801
|
attempt,
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
};
|
|
1923
|
-
await store.completeStep(
|
|
1924
|
-
parentRunId,
|
|
1925
|
-
parentStepId,
|
|
1926
|
-
subflowOutput,
|
|
1927
|
-
completedFact
|
|
1928
|
-
);
|
|
1929
|
-
await fireHook(
|
|
1930
|
-
deps.hooks?.onStepComplete,
|
|
1931
|
-
{
|
|
1932
|
-
runId: parentRunId,
|
|
1933
|
-
flowId: parentFlow.id,
|
|
1934
|
-
stepId: parentStepId,
|
|
1935
|
-
attempt,
|
|
1936
|
-
kind: "subflow",
|
|
1937
|
-
output: subflowOutput,
|
|
1938
|
-
durationMs: 0,
|
|
1939
|
-
at: clock.now()
|
|
1940
|
-
},
|
|
1941
|
-
"onStepComplete",
|
|
1942
|
-
deps
|
|
1943
|
-
);
|
|
1802
|
+
kind: "subflow",
|
|
1803
|
+
output: subflowOutput
|
|
1804
|
+
});
|
|
1944
1805
|
} else {
|
|
1945
|
-
|
|
1946
|
-
|
|
1806
|
+
await markStepFail({
|
|
1807
|
+
deps,
|
|
1808
|
+
flow: parentFlow,
|
|
1947
1809
|
runId: parentRunId,
|
|
1948
1810
|
stepId: parentStepId,
|
|
1949
1811
|
attempt,
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
};
|
|
1953
|
-
await store.failStep(parentRunId, parentStepId, outcome.error, failedFact);
|
|
1954
|
-
await fireHook(
|
|
1955
|
-
deps.hooks?.onStepError,
|
|
1956
|
-
{
|
|
1957
|
-
runId: parentRunId,
|
|
1958
|
-
flowId: parentFlow.id,
|
|
1959
|
-
stepId: parentStepId,
|
|
1960
|
-
attempt,
|
|
1961
|
-
kind: "subflow",
|
|
1962
|
-
error: outcome.error,
|
|
1963
|
-
at: clock.now()
|
|
1964
|
-
},
|
|
1965
|
-
"onStepError",
|
|
1966
|
-
deps
|
|
1967
|
-
);
|
|
1812
|
+
kind: "subflow",
|
|
1813
|
+
error: outcome.error
|
|
1814
|
+
});
|
|
1968
1815
|
}
|
|
1969
1816
|
await advance(deps, parentRunId);
|
|
1970
1817
|
}
|
|
1971
|
-
function computeFlowOutput(flow2, runState) {
|
|
1972
|
-
if (flow2.output === void 0) return null;
|
|
1973
|
-
const stepOutputs = {};
|
|
1974
|
-
for (const [sid, sstate] of Object.entries(runState.steps)) {
|
|
1975
|
-
if (sstate.output !== void 0) stepOutputs[sid] = sstate.output;
|
|
1976
|
-
}
|
|
1977
|
-
return flow2.output(stepOutputs);
|
|
1978
|
-
}
|
|
1979
1818
|
function serializeError(err) {
|
|
1980
1819
|
if (err instanceof Error) {
|
|
1981
1820
|
return {
|
|
@@ -2178,7 +2017,14 @@ async function nagi(config) {
|
|
|
2178
2017
|
return flowsById.get(flowId);
|
|
2179
2018
|
}
|
|
2180
2019
|
async function startRunInternal(args) {
|
|
2181
|
-
const {
|
|
2020
|
+
const {
|
|
2021
|
+
flow: flow2,
|
|
2022
|
+
validatedInput,
|
|
2023
|
+
runId,
|
|
2024
|
+
parentRunId,
|
|
2025
|
+
parentStepId,
|
|
2026
|
+
parentStepAttempt
|
|
2027
|
+
} = args;
|
|
2182
2028
|
const startedAt = clock.now();
|
|
2183
2029
|
const flowHash = flowHashById.get(flow2.id);
|
|
2184
2030
|
const fact = {
|
|
@@ -2218,9 +2064,7 @@ async function nagi(config) {
|
|
|
2218
2064
|
concurrencyKey: c.fact.concurrencyKey
|
|
2219
2065
|
});
|
|
2220
2066
|
const serialized = {
|
|
2221
|
-
|
|
2222
|
-
message: cancelError.message,
|
|
2223
|
-
...cancelError.stack !== void 0 ? { stack: cancelError.stack } : {},
|
|
2067
|
+
...serializeError(cancelError),
|
|
2224
2068
|
cause: {
|
|
2225
2069
|
canceledByRunId: runId,
|
|
2226
2070
|
concurrencyKey: c.fact.concurrencyKey
|
|
@@ -2249,7 +2093,17 @@ async function nagi(config) {
|
|
|
2249
2093
|
error: serialized
|
|
2250
2094
|
});
|
|
2251
2095
|
}
|
|
2252
|
-
const startEvent = {
|
|
2096
|
+
const startEvent = parentRunId !== void 0 && parentStepId !== void 0 && parentStepAttempt !== void 0 ? {
|
|
2097
|
+
runId,
|
|
2098
|
+
flowId: flow2.id,
|
|
2099
|
+
input: validatedInput,
|
|
2100
|
+
at: startedAt,
|
|
2101
|
+
parent: {
|
|
2102
|
+
runId: parentRunId,
|
|
2103
|
+
stepId: parentStepId,
|
|
2104
|
+
attempt: parentStepAttempt
|
|
2105
|
+
}
|
|
2106
|
+
} : {
|
|
2253
2107
|
runId,
|
|
2254
2108
|
flowId: flow2.id,
|
|
2255
2109
|
input: validatedInput,
|
|
@@ -2271,7 +2125,7 @@ async function nagi(config) {
|
|
|
2271
2125
|
return { started: true };
|
|
2272
2126
|
}
|
|
2273
2127
|
async function startChildRun(args) {
|
|
2274
|
-
const { child, childInput, parentRunId, parentStepId } = args;
|
|
2128
|
+
const { child, childInput, parentRunId, parentStepId, parentStepAttempt } = args;
|
|
2275
2129
|
if (!flowsById.has(child.id)) {
|
|
2276
2130
|
throw new NagiRuntimeError(
|
|
2277
2131
|
`Subflow child "${child.id}" not registered with nagi(). Pass it to flows[].`
|
|
@@ -2284,7 +2138,8 @@ async function nagi(config) {
|
|
|
2284
2138
|
validatedInput: validated,
|
|
2285
2139
|
runId: childRunId,
|
|
2286
2140
|
parentRunId,
|
|
2287
|
-
parentStepId
|
|
2141
|
+
parentStepId,
|
|
2142
|
+
parentStepAttempt
|
|
2288
2143
|
});
|
|
2289
2144
|
if (!started) {
|
|
2290
2145
|
throw new NagiRuntimeError(
|
|
@@ -2303,30 +2158,32 @@ async function nagi(config) {
|
|
|
2303
2158
|
return;
|
|
2304
2159
|
}
|
|
2305
2160
|
const flow2 = flowsById.get(state.flowId);
|
|
2306
|
-
const canceledFact = {
|
|
2161
|
+
const canceledFact = args.cause === "operator" ? {
|
|
2307
2162
|
kind: "flow.canceled",
|
|
2308
|
-
cause:
|
|
2163
|
+
cause: "operator",
|
|
2309
2164
|
runId,
|
|
2310
2165
|
at: clock.now(),
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2166
|
+
actor: args.actor,
|
|
2167
|
+
reason: args.reason,
|
|
2168
|
+
...args.note !== void 0 ? { note: args.note } : {}
|
|
2169
|
+
} : {
|
|
2170
|
+
kind: "flow.canceled",
|
|
2171
|
+
cause: "explicit",
|
|
2172
|
+
runId,
|
|
2173
|
+
at: clock.now(),
|
|
2174
|
+
reason: args.reason,
|
|
2318
2175
|
...args.note !== void 0 ? { note: args.note } : {}
|
|
2319
2176
|
};
|
|
2320
2177
|
await config.store.appendFact(runId, canceledFact);
|
|
2178
|
+
const cancelError = {
|
|
2179
|
+
name: "NagiCanceledError",
|
|
2180
|
+
message: `Run ${runId} was canceled: ${args.reason}`
|
|
2181
|
+
};
|
|
2321
2182
|
if (flow2 !== void 0) {
|
|
2322
|
-
const cancelError2 = {
|
|
2323
|
-
name: "NagiCanceledError",
|
|
2324
|
-
message: `Run ${runId} was canceled: ${args.reason}`
|
|
2325
|
-
};
|
|
2326
2183
|
const event = {
|
|
2327
2184
|
runId,
|
|
2328
2185
|
flowId: flow2.id,
|
|
2329
|
-
error:
|
|
2186
|
+
error: cancelError,
|
|
2330
2187
|
at: clock.now()
|
|
2331
2188
|
};
|
|
2332
2189
|
await fireHook(flow2.onError, event, "flow.onError", dispatchDeps);
|
|
@@ -2342,13 +2199,9 @@ async function nagi(config) {
|
|
|
2342
2199
|
await cancelRunRecursive(childId, {
|
|
2343
2200
|
cause: "explicit",
|
|
2344
2201
|
reason: `parent ${runId} canceled: ${args.reason}`,
|
|
2345
|
-
note: args.cause === "operator"
|
|
2202
|
+
note: args.cause === "operator" ? `cascade from operator ${args.actor} aborting parent ${runId}` : `cascade from parent ${runId}`
|
|
2346
2203
|
});
|
|
2347
2204
|
}
|
|
2348
|
-
const cancelError = {
|
|
2349
|
-
name: "NagiCanceledError",
|
|
2350
|
-
message: `Run ${runId} was canceled: ${args.reason}`
|
|
2351
|
-
};
|
|
2352
2205
|
await propagateToParent(dispatchDeps, runId, {
|
|
2353
2206
|
kind: "canceled",
|
|
2354
2207
|
error: cancelError
|
|
@@ -2367,9 +2220,13 @@ async function nagi(config) {
|
|
|
2367
2220
|
};
|
|
2368
2221
|
const wf = {
|
|
2369
2222
|
async start(flow2, input, opts) {
|
|
2370
|
-
|
|
2223
|
+
return wf.startById(flow2.id, input, opts);
|
|
2224
|
+
},
|
|
2225
|
+
async startById(flowId, input, opts) {
|
|
2226
|
+
const flow2 = flowsById.get(flowId);
|
|
2227
|
+
if (!flow2) {
|
|
2371
2228
|
throw new NagiRuntimeError(
|
|
2372
|
-
`Flow "${
|
|
2229
|
+
`Flow "${flowId}" not registered with nagi(). Pass it to flows[].`
|
|
2373
2230
|
);
|
|
2374
2231
|
}
|
|
2375
2232
|
let runId;
|