@nagi-js/core 0.1.0 → 0.1.1-rc.0
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 +333 -20
- package/dist/index.js +463 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3,6 +3,9 @@ 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
|
+
}
|
|
6
9
|
function getDef(step) {
|
|
7
10
|
const def = step[DEF];
|
|
8
11
|
if (def === void 0) {
|
|
@@ -12,6 +15,15 @@ function getDef(step) {
|
|
|
12
15
|
}
|
|
13
16
|
return def;
|
|
14
17
|
}
|
|
18
|
+
function needsStepIds(def) {
|
|
19
|
+
const out = [];
|
|
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;
|
|
26
|
+
}
|
|
15
27
|
function resolveNeeds(def, loadOutput) {
|
|
16
28
|
const result = {};
|
|
17
29
|
for (const [localKey, upstream] of Object.entries(def.needs)) {
|
|
@@ -21,6 +33,9 @@ function resolveNeeds(def, loadOutput) {
|
|
|
21
33
|
}
|
|
22
34
|
return result;
|
|
23
35
|
}
|
|
36
|
+
function matchArms(def) {
|
|
37
|
+
return def.mode === "discriminator" ? Object.values(def.arms) : def.arms;
|
|
38
|
+
}
|
|
24
39
|
function readSelectedArm(matchId, runState) {
|
|
25
40
|
for (const fact of runState.facts) {
|
|
26
41
|
if (fact.kind === "match.arm-selected" && fact.stepId === matchId) {
|
|
@@ -50,16 +65,27 @@ function selectArm(def, args) {
|
|
|
50
65
|
}
|
|
51
66
|
|
|
52
67
|
// src/builder.ts
|
|
68
|
+
var BUILDER_BRAND = /* @__PURE__ */ Symbol.for("nagi.builder");
|
|
69
|
+
function isBuilder(value) {
|
|
70
|
+
return typeof value === "object" && value !== null && value[BUILDER_BRAND] === true;
|
|
71
|
+
}
|
|
72
|
+
function resolveBuildResult(value) {
|
|
73
|
+
if (isBuilder(value)) {
|
|
74
|
+
return Object.fromEntries(value.__steps);
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
53
78
|
function isDiscriminator(config) {
|
|
54
79
|
return "cases" in config && "on" in config;
|
|
55
80
|
}
|
|
56
81
|
function makeBuilder() {
|
|
82
|
+
const chainSteps = /* @__PURE__ */ new Map();
|
|
57
83
|
function task(config) {
|
|
58
84
|
const def = {
|
|
59
85
|
kind: "task",
|
|
60
86
|
needs: config.needs ?? {},
|
|
61
87
|
...config.retry !== void 0 ? { retry: config.retry } : {},
|
|
62
|
-
...config.
|
|
88
|
+
...config.timeoutMs !== void 0 ? { timeoutMs: config.timeoutMs } : {},
|
|
63
89
|
...config.when !== void 0 ? {
|
|
64
90
|
when: config.when
|
|
65
91
|
} : {},
|
|
@@ -72,7 +98,7 @@ function makeBuilder() {
|
|
|
72
98
|
kind: "signal",
|
|
73
99
|
needs: config.needs ?? {},
|
|
74
100
|
schema: config.schema,
|
|
75
|
-
...config.
|
|
101
|
+
...config.timeoutMs !== void 0 ? { timeoutMs: config.timeoutMs } : {},
|
|
76
102
|
...config.when !== void 0 ? {
|
|
77
103
|
when: config.when
|
|
78
104
|
} : {}
|
|
@@ -84,8 +110,8 @@ function makeBuilder() {
|
|
|
84
110
|
if (isDiscriminator(config)) {
|
|
85
111
|
const arms2 = {};
|
|
86
112
|
for (const [caseKey, build] of Object.entries(config.cases)) {
|
|
87
|
-
const nested =
|
|
88
|
-
makeBuilder()
|
|
113
|
+
const nested = resolveBuildResult(
|
|
114
|
+
build(makeBuilder())
|
|
89
115
|
);
|
|
90
116
|
arms2[caseKey] = { id: caseKey, stepIds: [], _nested: nested };
|
|
91
117
|
}
|
|
@@ -102,7 +128,7 @@ function makeBuilder() {
|
|
|
102
128
|
const arms = [];
|
|
103
129
|
for (let i = 0; i < guardConfig.arms.length; i++) {
|
|
104
130
|
const arm = guardConfig.arms[i];
|
|
105
|
-
const nested = arm.build(makeBuilder());
|
|
131
|
+
const nested = resolveBuildResult(arm.build(makeBuilder()));
|
|
106
132
|
const armId = arm.otherwise ? "otherwise" : `arm${i}`;
|
|
107
133
|
arms.push({
|
|
108
134
|
id: armId,
|
|
@@ -122,11 +148,62 @@ function makeBuilder() {
|
|
|
122
148
|
};
|
|
123
149
|
return attachDef({ kind: "match", id: "" }, def);
|
|
124
150
|
}
|
|
125
|
-
|
|
151
|
+
function step(key, config) {
|
|
152
|
+
if (chainSteps.has(key)) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`b.step: duplicate key "${key}" \u2014 each chain entry must have a unique key.`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
const shell = { kind: "task", id: "" };
|
|
158
|
+
const needs = {};
|
|
159
|
+
if (config.needs) {
|
|
160
|
+
for (const sibKey of config.needs) {
|
|
161
|
+
const sibling = chainSteps.get(sibKey);
|
|
162
|
+
if (sibling === void 0) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`b.step("${key}"): unknown sibling "${sibKey}". Available keys so far: ${[...chainSteps.keys()].join(", ") || "<none>"}.`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
needs[sibKey] = sibling;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const def = {
|
|
171
|
+
kind: "task",
|
|
172
|
+
needs,
|
|
173
|
+
...config.retry !== void 0 ? { retry: config.retry } : {},
|
|
174
|
+
...config.timeoutMs !== void 0 ? { timeoutMs: config.timeoutMs } : {},
|
|
175
|
+
...config.when !== void 0 ? {
|
|
176
|
+
when: config.when
|
|
177
|
+
} : {},
|
|
178
|
+
run: config.run
|
|
179
|
+
};
|
|
180
|
+
attachDefMut(shell, def);
|
|
181
|
+
chainSteps.set(key, shell);
|
|
182
|
+
return builder;
|
|
183
|
+
}
|
|
184
|
+
function include(key, s) {
|
|
185
|
+
if (chainSteps.has(key)) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
`b.include: duplicate key "${key}" \u2014 each chain entry must have a unique key.`
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
chainSteps.set(key, s);
|
|
191
|
+
return builder;
|
|
192
|
+
}
|
|
193
|
+
const builder = {
|
|
194
|
+
task,
|
|
195
|
+
signal,
|
|
196
|
+
match,
|
|
197
|
+
step,
|
|
198
|
+
include,
|
|
199
|
+
[BUILDER_BRAND]: true,
|
|
200
|
+
__steps: chainSteps
|
|
201
|
+
};
|
|
202
|
+
return builder;
|
|
126
203
|
}
|
|
127
204
|
function flow(config) {
|
|
128
205
|
const builder = makeBuilder();
|
|
129
|
-
const built = config.build(builder);
|
|
206
|
+
const built = resolveBuildResult(config.build(builder));
|
|
130
207
|
const idByIdentity = /* @__PURE__ */ new Map();
|
|
131
208
|
collectIds(built, "", idByIdentity);
|
|
132
209
|
const finalSteps = {};
|
|
@@ -234,6 +311,235 @@ function walkAndRewrite(args) {
|
|
|
234
311
|
}
|
|
235
312
|
}
|
|
236
313
|
|
|
314
|
+
// src/canonicalize.ts
|
|
315
|
+
var DEFAULT_RETRY = {
|
|
316
|
+
initialDelayMs: 1e3,
|
|
317
|
+
maxDelayMs: 6e4
|
|
318
|
+
};
|
|
319
|
+
var warnedAboutSourceHash = false;
|
|
320
|
+
function warnSourceHashOnce() {
|
|
321
|
+
if (warnedAboutSourceHash) return;
|
|
322
|
+
warnedAboutSourceHash = true;
|
|
323
|
+
console.warn(
|
|
324
|
+
"[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. See RFC 0001."
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
async function canonicalize(flow2) {
|
|
328
|
+
const ids = Object.keys(flow2.steps).sort();
|
|
329
|
+
const steps = [];
|
|
330
|
+
for (const id of ids) {
|
|
331
|
+
const step = flow2.steps[id];
|
|
332
|
+
if (step === void 0) continue;
|
|
333
|
+
steps.push(await canonicalizeStep(id, getDef(step)));
|
|
334
|
+
}
|
|
335
|
+
return {
|
|
336
|
+
flowId: flow2.id,
|
|
337
|
+
inputSchema: await canonicalizeSchema(flow2.input),
|
|
338
|
+
steps
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
async function canonicalizeStep(id, def) {
|
|
342
|
+
const needs = [...needsStepIds(def)].sort();
|
|
343
|
+
const base = { id, kind: def.kind, needs };
|
|
344
|
+
if (def.kind === "task") return canonicalizeTask(base, def);
|
|
345
|
+
if (def.kind === "signal") return canonicalizeSignal(base, def);
|
|
346
|
+
return canonicalizeMatch(base, def);
|
|
347
|
+
}
|
|
348
|
+
async function canonicalizeTask(base, def) {
|
|
349
|
+
const out = { ...base };
|
|
350
|
+
if (def.when !== void 0) out.whenHash = await hashFnSource(def.when);
|
|
351
|
+
if (def.retry !== void 0) out.retry = normalizeRetry(def.retry);
|
|
352
|
+
if (def.timeoutMs !== void 0) out.timeoutMs = def.timeoutMs;
|
|
353
|
+
return out;
|
|
354
|
+
}
|
|
355
|
+
async function canonicalizeSignal(base, def) {
|
|
356
|
+
const out = { ...base };
|
|
357
|
+
if (def.when !== void 0) out.whenHash = await hashFnSource(def.when);
|
|
358
|
+
if (def.timeoutMs !== void 0) out.timeoutMs = def.timeoutMs;
|
|
359
|
+
out.signalSchema = await canonicalizeSchema(def.schema);
|
|
360
|
+
return out;
|
|
361
|
+
}
|
|
362
|
+
async function canonicalizeMatch(base, def) {
|
|
363
|
+
const out = { ...base };
|
|
364
|
+
out.matchMode = def.mode;
|
|
365
|
+
if (def.mode === "discriminator") {
|
|
366
|
+
out.matchOnHash = await hashFnSource(def.on);
|
|
367
|
+
}
|
|
368
|
+
const arms = [];
|
|
369
|
+
for (const arm of matchArms(def)) {
|
|
370
|
+
arms.push(await canonicalizeArm(arm));
|
|
371
|
+
}
|
|
372
|
+
out.matchArms = arms;
|
|
373
|
+
return out;
|
|
374
|
+
}
|
|
375
|
+
async function canonicalizeArm(arm) {
|
|
376
|
+
const out = {
|
|
377
|
+
id: arm.id,
|
|
378
|
+
stepIds: [...arm.stepIds].sort()
|
|
379
|
+
};
|
|
380
|
+
if (arm.otherwise) out.otherwise = true;
|
|
381
|
+
if (arm.when !== void 0) out.whenHash = await hashFnSource(arm.when);
|
|
382
|
+
return out;
|
|
383
|
+
}
|
|
384
|
+
function normalizeRetry(retry) {
|
|
385
|
+
return {
|
|
386
|
+
maxAttempts: retry.maxAttempts,
|
|
387
|
+
backoff: retry.backoff,
|
|
388
|
+
initialDelayMs: retry.initialDelayMs ?? DEFAULT_RETRY.initialDelayMs,
|
|
389
|
+
maxDelayMs: retry.maxDelayMs ?? DEFAULT_RETRY.maxDelayMs
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
async function canonicalizeSchema(schema) {
|
|
393
|
+
const props = schema["~standard"];
|
|
394
|
+
return {
|
|
395
|
+
vendor: props.vendor,
|
|
396
|
+
version: props.version,
|
|
397
|
+
validateHash: await hashFnSource(props.validate)
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
async function hashFnSource(fn) {
|
|
401
|
+
warnSourceHashOnce();
|
|
402
|
+
return sha256Hex(fn.toString());
|
|
403
|
+
}
|
|
404
|
+
async function sha256Canonical(dag) {
|
|
405
|
+
return sha256Hex(stableStringify(dag));
|
|
406
|
+
}
|
|
407
|
+
function stableStringify(value) {
|
|
408
|
+
if (value === void 0) return "null";
|
|
409
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
410
|
+
if (Array.isArray(value)) {
|
|
411
|
+
return `[${value.map(stableStringify).join(",")}]`;
|
|
412
|
+
}
|
|
413
|
+
const entries = Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
414
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`).join(",")}}`;
|
|
415
|
+
}
|
|
416
|
+
async function sha256Hex(input) {
|
|
417
|
+
const bytes = new TextEncoder().encode(input);
|
|
418
|
+
const digest = await crypto.subtle.digest("SHA-256", bytes);
|
|
419
|
+
return hex(new Uint8Array(digest));
|
|
420
|
+
}
|
|
421
|
+
function hex(bytes) {
|
|
422
|
+
let s = "";
|
|
423
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
424
|
+
s += bytes[i].toString(16).padStart(2, "0");
|
|
425
|
+
}
|
|
426
|
+
return s;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/diff.ts
|
|
430
|
+
function diffSnapshots(before, after) {
|
|
431
|
+
const beforeStepsById = new Map(before.steps.map((s) => [s.id, s]));
|
|
432
|
+
const afterStepsById = new Map(after.steps.map((s) => [s.id, s]));
|
|
433
|
+
const addedSteps = [];
|
|
434
|
+
const removedSteps = [];
|
|
435
|
+
for (const id of afterStepsById.keys()) {
|
|
436
|
+
if (!beforeStepsById.has(id)) addedSteps.push(id);
|
|
437
|
+
}
|
|
438
|
+
for (const id of beforeStepsById.keys()) {
|
|
439
|
+
if (!afterStepsById.has(id)) removedSteps.push(id);
|
|
440
|
+
}
|
|
441
|
+
addedSteps.sort();
|
|
442
|
+
removedSteps.sort();
|
|
443
|
+
const changedEdges = [];
|
|
444
|
+
const changedPredicates = [];
|
|
445
|
+
for (const id of afterStepsById.keys()) {
|
|
446
|
+
const beforeStep = beforeStepsById.get(id);
|
|
447
|
+
const afterStep = afterStepsById.get(id);
|
|
448
|
+
if (beforeStep === void 0 || afterStep === void 0) continue;
|
|
449
|
+
diffEdges(id, beforeStep, afterStep, changedEdges);
|
|
450
|
+
diffFields(beforeStep, afterStep, changedPredicates);
|
|
451
|
+
}
|
|
452
|
+
changedEdges.sort(
|
|
453
|
+
(a, b) => a.to !== b.to ? cmp(a.to, b.to) : cmp(a.from, b.from)
|
|
454
|
+
);
|
|
455
|
+
changedPredicates.sort(
|
|
456
|
+
(a, b) => a.stepId !== b.stepId ? cmp(a.stepId, b.stepId) : cmp(a.field, b.field)
|
|
457
|
+
);
|
|
458
|
+
return { addedSteps, removedSteps, changedEdges, changedPredicates };
|
|
459
|
+
}
|
|
460
|
+
function diffEdges(stepId, before, after, out) {
|
|
461
|
+
const beforeSet = new Set(before.needs);
|
|
462
|
+
const afterSet = new Set(after.needs);
|
|
463
|
+
for (const upstream of afterSet) {
|
|
464
|
+
if (!beforeSet.has(upstream)) {
|
|
465
|
+
out.push({
|
|
466
|
+
from: upstream,
|
|
467
|
+
to: stepId,
|
|
468
|
+
before: "absent",
|
|
469
|
+
after: "needed"
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
for (const upstream of beforeSet) {
|
|
474
|
+
if (!afterSet.has(upstream)) {
|
|
475
|
+
out.push({
|
|
476
|
+
from: upstream,
|
|
477
|
+
to: stepId,
|
|
478
|
+
before: "needed",
|
|
479
|
+
after: "absent"
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
function diffFields(before, after, out) {
|
|
485
|
+
const id = after.id;
|
|
486
|
+
if (before.kind !== after.kind) {
|
|
487
|
+
out.push({ stepId: id, field: "kind" });
|
|
488
|
+
}
|
|
489
|
+
if (before.whenHash !== after.whenHash) {
|
|
490
|
+
out.push({ stepId: id, field: "when" });
|
|
491
|
+
}
|
|
492
|
+
if (!retryEq(before.retry, after.retry)) {
|
|
493
|
+
out.push({ stepId: id, field: "retry" });
|
|
494
|
+
}
|
|
495
|
+
if (before.timeoutMs !== after.timeoutMs) {
|
|
496
|
+
out.push({ stepId: id, field: "timeoutMs" });
|
|
497
|
+
}
|
|
498
|
+
if (!schemaEq(before.signalSchema, after.signalSchema)) {
|
|
499
|
+
out.push({ stepId: id, field: "signalSchema" });
|
|
500
|
+
}
|
|
501
|
+
if (before.matchMode !== after.matchMode) {
|
|
502
|
+
out.push({ stepId: id, field: "matchMode" });
|
|
503
|
+
}
|
|
504
|
+
if (before.matchOnHash !== after.matchOnHash) {
|
|
505
|
+
out.push({ stepId: id, field: "matchOn" });
|
|
506
|
+
}
|
|
507
|
+
if (!armsEq(before.matchArms, after.matchArms)) {
|
|
508
|
+
out.push({ stepId: id, field: "matchArms" });
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
function retryEq(a, b) {
|
|
512
|
+
if (a === b) return true;
|
|
513
|
+
if (a === void 0 || b === void 0) return false;
|
|
514
|
+
return a.maxAttempts === b.maxAttempts && a.backoff === b.backoff && a.initialDelayMs === b.initialDelayMs && a.maxDelayMs === b.maxDelayMs;
|
|
515
|
+
}
|
|
516
|
+
function schemaEq(a, b) {
|
|
517
|
+
if (a === b) return true;
|
|
518
|
+
if (a === void 0 || b === void 0) return false;
|
|
519
|
+
return a.vendor === b.vendor && a.version === b.version && a.validateHash === b.validateHash;
|
|
520
|
+
}
|
|
521
|
+
function armsEq(a, b) {
|
|
522
|
+
if (a === b) return true;
|
|
523
|
+
if (a === void 0 || b === void 0) return false;
|
|
524
|
+
if (a.length !== b.length) return false;
|
|
525
|
+
for (let i = 0; i < a.length; i++) {
|
|
526
|
+
const x = a[i];
|
|
527
|
+
const y = b[i];
|
|
528
|
+
if (x === void 0 || y === void 0) return false;
|
|
529
|
+
if (x.id !== y.id) return false;
|
|
530
|
+
if (x.otherwise !== y.otherwise) return false;
|
|
531
|
+
if (x.whenHash !== y.whenHash) return false;
|
|
532
|
+
if (x.stepIds.length !== y.stepIds.length) return false;
|
|
533
|
+
for (let j = 0; j < x.stepIds.length; j++) {
|
|
534
|
+
if (x.stepIds[j] !== y.stepIds[j]) return false;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
function cmp(a, b) {
|
|
540
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
541
|
+
}
|
|
542
|
+
|
|
237
543
|
// src/memory.ts
|
|
238
544
|
var DEFAULT_STORE_LEASE_MS = 6e4;
|
|
239
545
|
var InMemoryStore = class {
|
|
@@ -241,6 +547,9 @@ var InMemoryStore = class {
|
|
|
241
547
|
outputs = /* @__PURE__ */ new Map();
|
|
242
548
|
onces = /* @__PURE__ */ new Map();
|
|
243
549
|
leases = /* @__PURE__ */ new Map();
|
|
550
|
+
snapshots = /* @__PURE__ */ new Map();
|
|
551
|
+
refs = /* @__PURE__ */ new Map();
|
|
552
|
+
globalFacts = [];
|
|
244
553
|
leaseMs;
|
|
245
554
|
constructor(opts = {}) {
|
|
246
555
|
this.leaseMs = opts.leaseMs ?? DEFAULT_STORE_LEASE_MS;
|
|
@@ -296,16 +605,40 @@ var InMemoryStore = class {
|
|
|
296
605
|
}
|
|
297
606
|
return result.output;
|
|
298
607
|
}
|
|
608
|
+
async upsertSnapshot(args) {
|
|
609
|
+
if (this.snapshots.has(args.flowHash)) return;
|
|
610
|
+
this.snapshots.set(args.flowHash, { flowId: args.flowId, dag: args.dag });
|
|
611
|
+
}
|
|
612
|
+
async getRef(flowId) {
|
|
613
|
+
return this.refs.get(flowId) ?? null;
|
|
614
|
+
}
|
|
615
|
+
async setRef(flowId, flowHash) {
|
|
616
|
+
this.refs.set(flowId, flowHash);
|
|
617
|
+
}
|
|
618
|
+
async loadSnapshot(flowHash) {
|
|
619
|
+
return this.snapshots.get(flowHash) ?? null;
|
|
620
|
+
}
|
|
621
|
+
async appendGlobalFact(fact) {
|
|
622
|
+
this.globalFacts.push(fact);
|
|
623
|
+
}
|
|
624
|
+
/** Test-only: peek at the in-memory global fact log. */
|
|
625
|
+
readGlobalFacts() {
|
|
626
|
+
return this.globalFacts;
|
|
627
|
+
}
|
|
299
628
|
};
|
|
300
629
|
function projectRunState(runId, facts) {
|
|
301
630
|
let flowId = "";
|
|
302
631
|
let status = "pending";
|
|
632
|
+
let flowHash;
|
|
633
|
+
let codeVersion;
|
|
303
634
|
const steps = {};
|
|
304
635
|
for (const fact of facts) {
|
|
305
636
|
switch (fact.kind) {
|
|
306
637
|
case "flow.started":
|
|
307
638
|
flowId = fact.flowId;
|
|
308
639
|
status = "running";
|
|
640
|
+
flowHash = fact.flowHash;
|
|
641
|
+
codeVersion = fact.codeVersion;
|
|
309
642
|
break;
|
|
310
643
|
case "flow.completed":
|
|
311
644
|
status = "completed";
|
|
@@ -345,7 +678,15 @@ function projectRunState(runId, facts) {
|
|
|
345
678
|
break;
|
|
346
679
|
}
|
|
347
680
|
}
|
|
348
|
-
return {
|
|
681
|
+
return {
|
|
682
|
+
runId,
|
|
683
|
+
flowId,
|
|
684
|
+
status,
|
|
685
|
+
steps,
|
|
686
|
+
facts,
|
|
687
|
+
...flowHash !== void 0 ? { flowHash } : {},
|
|
688
|
+
...codeVersion !== void 0 ? { codeVersion } : {}
|
|
689
|
+
};
|
|
349
690
|
}
|
|
350
691
|
var DEFAULT_QUEUE_LEASE_MS = 6e4;
|
|
351
692
|
var InMemoryQueue = class {
|
|
@@ -589,7 +930,7 @@ function extractInput(runState) {
|
|
|
589
930
|
}
|
|
590
931
|
|
|
591
932
|
// src/dispatch.ts
|
|
592
|
-
var
|
|
933
|
+
var DEFAULT_RETRY2 = {
|
|
593
934
|
maxAttempts: 3,
|
|
594
935
|
backoff: "exponential",
|
|
595
936
|
initialDelayMs: 1e3,
|
|
@@ -737,7 +1078,7 @@ async function handleStepError(args) {
|
|
|
737
1078
|
const { deps, flow: flow2, message, def, runId, stepId, attempt, err } = args;
|
|
738
1079
|
const { store, queue, clock } = deps;
|
|
739
1080
|
const error = serializeError(err);
|
|
740
|
-
const policy = def.kind === "task" && def.retry ? def.retry : deps.defaultRetry ??
|
|
1081
|
+
const policy = def.kind === "task" && def.retry ? def.retry : deps.defaultRetry ?? DEFAULT_RETRY2;
|
|
741
1082
|
const shouldRetry = attempt < policy.maxAttempts && retryAllows(policy, err);
|
|
742
1083
|
if (shouldRetry) {
|
|
743
1084
|
const delayMs = computeBackoff(policy, attempt);
|
|
@@ -1115,9 +1456,24 @@ var NagiRuntimeError = class extends Error {
|
|
|
1115
1456
|
this.name = "NagiRuntimeError";
|
|
1116
1457
|
}
|
|
1117
1458
|
};
|
|
1118
|
-
|
|
1459
|
+
var NagiSnapshotDriftError = class extends Error {
|
|
1460
|
+
runId;
|
|
1461
|
+
expected;
|
|
1462
|
+
actual;
|
|
1463
|
+
constructor(args) {
|
|
1464
|
+
super(
|
|
1465
|
+
`Run ${args.runId} was pinned to flow hash ${args.expected.slice(0, 12)}\u2026 but the live flow's hash is ${args.actual.slice(0, 12)}\u2026. Pass replayOpts.allowDrift = true to replay against the live code anyway.`
|
|
1466
|
+
);
|
|
1467
|
+
this.name = "NagiSnapshotDriftError";
|
|
1468
|
+
this.runId = args.runId;
|
|
1469
|
+
this.expected = args.expected;
|
|
1470
|
+
this.actual = args.actual;
|
|
1471
|
+
}
|
|
1472
|
+
};
|
|
1473
|
+
async function nagi(config) {
|
|
1119
1474
|
const clock = config.clock ?? new InMemoryClock();
|
|
1120
1475
|
const flowsById = /* @__PURE__ */ new Map();
|
|
1476
|
+
const flowHashById = /* @__PURE__ */ new Map();
|
|
1121
1477
|
for (const f of config.flows) {
|
|
1122
1478
|
if (flowsById.has(f.id)) {
|
|
1123
1479
|
throw new NagiRuntimeError(
|
|
@@ -1125,6 +1481,25 @@ function nagi(config) {
|
|
|
1125
1481
|
);
|
|
1126
1482
|
}
|
|
1127
1483
|
flowsById.set(f.id, f);
|
|
1484
|
+
const dag = await canonicalize(f);
|
|
1485
|
+
const flowHash = await sha256Canonical(dag);
|
|
1486
|
+
flowHashById.set(f.id, flowHash);
|
|
1487
|
+
await config.store.upsertSnapshot({
|
|
1488
|
+
flowHash,
|
|
1489
|
+
flowId: f.id,
|
|
1490
|
+
dag
|
|
1491
|
+
});
|
|
1492
|
+
const previousHash = await config.store.getRef(f.id);
|
|
1493
|
+
if (previousHash !== flowHash) {
|
|
1494
|
+
await config.store.setRef(f.id, flowHash);
|
|
1495
|
+
await config.store.appendGlobalFact({
|
|
1496
|
+
kind: "flow_ref.updated",
|
|
1497
|
+
flowId: f.id,
|
|
1498
|
+
from: previousHash,
|
|
1499
|
+
to: flowHash,
|
|
1500
|
+
at: clock.now()
|
|
1501
|
+
});
|
|
1502
|
+
}
|
|
1128
1503
|
}
|
|
1129
1504
|
async function flowFor(runId) {
|
|
1130
1505
|
const runState = await config.store.loadRunState(runId);
|
|
@@ -1168,12 +1543,15 @@ function nagi(config) {
|
|
|
1168
1543
|
}
|
|
1169
1544
|
const validated = await validate(flow2.input, input);
|
|
1170
1545
|
const startedAt = clock.now();
|
|
1546
|
+
const flowHash = flowHashById.get(flow2.id);
|
|
1171
1547
|
const fact = {
|
|
1172
1548
|
kind: "flow.started",
|
|
1173
1549
|
runId,
|
|
1174
1550
|
flowId: flow2.id,
|
|
1175
1551
|
input: validated,
|
|
1176
|
-
at: startedAt
|
|
1552
|
+
at: startedAt,
|
|
1553
|
+
...flowHash !== void 0 ? { flowHash } : {},
|
|
1554
|
+
...config.codeVersion !== void 0 ? { codeVersion: config.codeVersion } : {}
|
|
1177
1555
|
};
|
|
1178
1556
|
const { started } = await config.store.tryStartRun(runId, fact);
|
|
1179
1557
|
if (!started) {
|
|
@@ -1258,13 +1636,42 @@ function nagi(config) {
|
|
|
1258
1636
|
},
|
|
1259
1637
|
async replay(runId, opts = { mode: "continue" }) {
|
|
1260
1638
|
const runState = await config.store.loadRunState(runId);
|
|
1261
|
-
const
|
|
1262
|
-
if (!
|
|
1639
|
+
const liveFlow = flowsById.get(runState.flowId);
|
|
1640
|
+
if (!liveFlow) {
|
|
1263
1641
|
throw new NagiRuntimeError(
|
|
1264
1642
|
`Run ${runId} references flow "${runState.flowId}" which is not registered with nagi().`
|
|
1265
1643
|
);
|
|
1266
1644
|
}
|
|
1267
1645
|
if (opts.mode === "inspect") return;
|
|
1646
|
+
const pinned = runState.flowHash;
|
|
1647
|
+
if (pinned !== void 0) {
|
|
1648
|
+
const liveHash = flowHashById.get(liveFlow.id);
|
|
1649
|
+
if (liveHash !== void 0 && liveHash !== pinned) {
|
|
1650
|
+
if (!opts.allowDrift) {
|
|
1651
|
+
throw new NagiSnapshotDriftError({
|
|
1652
|
+
runId,
|
|
1653
|
+
expected: pinned,
|
|
1654
|
+
actual: liveHash
|
|
1655
|
+
});
|
|
1656
|
+
}
|
|
1657
|
+
const snapshot = await config.store.loadSnapshot(pinned);
|
|
1658
|
+
if (snapshot === null) {
|
|
1659
|
+
throw new NagiRuntimeError(
|
|
1660
|
+
`Run ${runId} pinned to flow hash ${pinned.slice(0, 12)}\u2026 but no snapshot with that hash was found. Cannot replay with allowDrift.`
|
|
1661
|
+
);
|
|
1662
|
+
}
|
|
1663
|
+
const synthesized = synthesizeReplayFlow(
|
|
1664
|
+
snapshot.dag,
|
|
1665
|
+
liveFlow
|
|
1666
|
+
);
|
|
1667
|
+
const driftDeps = {
|
|
1668
|
+
...dispatchDeps,
|
|
1669
|
+
flowFor: async () => synthesized
|
|
1670
|
+
};
|
|
1671
|
+
await advance(driftDeps, runId);
|
|
1672
|
+
return;
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1268
1675
|
await advance(dispatchDeps, runId);
|
|
1269
1676
|
}
|
|
1270
1677
|
};
|
|
@@ -1279,7 +1686,48 @@ async function validate(schema, value) {
|
|
|
1279
1686
|
}
|
|
1280
1687
|
return result.value;
|
|
1281
1688
|
}
|
|
1689
|
+
function synthesizeReplayFlow(dag, liveFlow) {
|
|
1690
|
+
const synthesized = {};
|
|
1691
|
+
for (const canonStep of dag.steps) {
|
|
1692
|
+
synthesized[canonStep.id] = attachDef(
|
|
1693
|
+
{ kind: canonStep.kind, id: canonStep.id },
|
|
1694
|
+
{ kind: canonStep.kind }
|
|
1695
|
+
);
|
|
1696
|
+
}
|
|
1697
|
+
for (const canonStep of dag.steps) {
|
|
1698
|
+
const liveStep = liveFlow.steps[canonStep.id];
|
|
1699
|
+
if (liveStep === void 0) {
|
|
1700
|
+
throw new NagiRuntimeError(
|
|
1701
|
+
`Drift-allowed replay: step "${canonStep.id}" exists in snapshot but is missing from the live flow "${liveFlow.id}". Cannot synthesize a handler.`
|
|
1702
|
+
);
|
|
1703
|
+
}
|
|
1704
|
+
const liveDef = getDef(liveStep);
|
|
1705
|
+
const synthesizedNeeds = {};
|
|
1706
|
+
for (const upstreamId of canonStep.needs) {
|
|
1707
|
+
const upstreamShell = synthesized[upstreamId];
|
|
1708
|
+
if (upstreamShell === void 0) {
|
|
1709
|
+
throw new NagiRuntimeError(
|
|
1710
|
+
`Drift-allowed replay: step "${canonStep.id}" needs upstream "${upstreamId}" which the snapshot does not declare.`
|
|
1711
|
+
);
|
|
1712
|
+
}
|
|
1713
|
+
synthesizedNeeds[upstreamId] = upstreamShell;
|
|
1714
|
+
}
|
|
1715
|
+
const rewiredDef = {
|
|
1716
|
+
...liveDef,
|
|
1717
|
+
needs: synthesizedNeeds
|
|
1718
|
+
};
|
|
1719
|
+
const shell = synthesized[canonStep.id];
|
|
1720
|
+
if (shell === void 0) continue;
|
|
1721
|
+
shell.__def = rewiredDef;
|
|
1722
|
+
}
|
|
1723
|
+
return {
|
|
1724
|
+
id: liveFlow.id,
|
|
1725
|
+
input: liveFlow.input,
|
|
1726
|
+
steps: synthesized,
|
|
1727
|
+
...liveFlow.output !== void 0 ? { output: liveFlow.output } : {}
|
|
1728
|
+
};
|
|
1729
|
+
}
|
|
1282
1730
|
|
|
1283
|
-
export { InMemoryClock, InMemoryQueue, InMemoryStore, InMemoryTrigger, NagiRuntimeError, NagiValidationError, flow, nagi, projectRunState };
|
|
1731
|
+
export { InMemoryClock, InMemoryQueue, InMemoryStore, InMemoryTrigger, NagiRuntimeError, NagiSnapshotDriftError, NagiValidationError, canonicalize, diffSnapshots, flow, nagi, projectRunState, sha256Canonical };
|
|
1284
1732
|
//# sourceMappingURL=index.js.map
|
|
1285
1733
|
//# sourceMappingURL=index.js.map
|