@nagi-js/core 0.1.1-rc.19 → 0.1.1-rc.21
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/{chunk-VQTYTI6X.js → chunk-VU2LG4VW.js} +130 -5
- package/dist/chunk-VU2LG4VW.js.map +1 -0
- package/dist/index.d.ts +48 -3
- package/dist/index.js +104 -15
- package/dist/index.js.map +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +115 -1
- package/dist/testing.js.map +1 -1
- package/dist/{types-DfxwD2MH.d.ts → types-KjCWK9pN.d.ts} +62 -7
- package/package.json +1 -1
- package/dist/chunk-VQTYTI6X.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { InMemoryClock, makeEmit, asStepMapWithDefs, getDef, NagiRuntimeError, compact, makeSignals, DEFAULT_REAPER_INTERVAL_MS, Facts, validationError, NagiFlowSnapshotGoneError, isTerminalRun, runStatusOf, validate, foldRun, needsStepIds,
|
|
2
|
-
export { DEFAULT_REAPER_INTERVAL_MS, DEFAULT_SWEEP_LIMIT, InMemoryClock, InMemoryQueue, InMemoryStore, NagiCanceledError, NagiConcurrencyConflictError, NagiFlowSnapshotGoneError, NagiNonRetryableError, NagiRuntimeError, NagiSignalTimeoutError, NagiSnapshotDriftError, NagiValidationError, QUERY_RUNS_DEFAULT_LIMIT, QUERY_RUNS_MAX_LIMIT, attemptOf, clampQueryLimit, compareRunOrder, decideExpiredLeaseAction, decideSignal, decideTimeout, decodeRunCursor, defaultSnapshotGonePolicy, encodeRunCursor, errorOf, factEffects, isPastCursor, isStepTerminal, isTerminalRun, jsonContains, outputOf, foldRun as projectRunState, resolvedOf, rowDeltaOf, runStatusOf, selectExpired, selectPruneBatch, stepStateOf, stepStatusOf, unwrap } from './chunk-
|
|
1
|
+
import { InMemoryClock, makeEmit, asStepMapWithDefs, getDef, NagiRuntimeError, compact, makeSignals, DEFAULT_REAPER_INTERVAL_MS, Facts, validationError, NagiFlowSnapshotGoneError, isTerminalRun, runStatusOf, validate, foldRun, needsStepIds, stepStateOf, serializeError, NagiCanceledError, NagiSignalTimeoutError, dequeueBackoff, defaultSnapshotGonePolicy, isStepTerminal, stepStatusOf, NagiSnapshotDriftError, attachDef, setDef, attemptOf, DEFAULT_RETRY, outputOf, resolveNeeds, resolvedOf, handlerDef, resolveRetry, isAbortRequested, selectArm, stepBackoff, NagiStepTimeoutError, peekDef, NagiNonRetryableError, normalizeNeeds } from './chunk-VU2LG4VW.js';
|
|
2
|
+
export { DEFAULT_REAPER_INTERVAL_MS, DEFAULT_SWEEP_LIMIT, InMemoryClock, InMemoryQueue, InMemoryRunEventHub, InMemoryStore, InMemoryStreamHub, NagiCanceledError, NagiConcurrencyConflictError, NagiFlowSnapshotGoneError, NagiNonRetryableError, NagiRuntimeError, NagiSignalTimeoutError, NagiSnapshotDriftError, NagiStepTimeoutError, NagiValidationError, QUERY_RUNS_DEFAULT_LIMIT, QUERY_RUNS_MAX_LIMIT, STREAM_REPLAY_BUFFER_CAP, STREAM_SUBSCRIBER_BUFFER_CAP, attemptOf, clampQueryLimit, compareRunOrder, decideExpiredLeaseAction, decideSignal, decideTimeout, decodeRunCursor, defaultSnapshotGonePolicy, encodeRunCursor, errorOf, factEffects, isPastCursor, isStepTerminal, isTerminalRun, isTerminalRunEvent, jsonContains, outputOf, foldRun as projectRunState, resolvedOf, rowDeltaOf, runEventOf, runStatusOf, selectExpired, selectPruneBatch, stepStateOf, stepStatusOf, unwrap } from './chunk-VU2LG4VW.js';
|
|
3
3
|
|
|
4
4
|
// src/builder.ts
|
|
5
5
|
function makeBuilder() {
|
|
@@ -10,6 +10,7 @@ function makeBuilder() {
|
|
|
10
10
|
run: config.run,
|
|
11
11
|
...compact({
|
|
12
12
|
retry: config.retry,
|
|
13
|
+
timeoutMs: config.timeoutMs,
|
|
13
14
|
when: config.when,
|
|
14
15
|
onStart: config.onStart,
|
|
15
16
|
onComplete: config.onComplete,
|
|
@@ -26,6 +27,7 @@ function makeBuilder() {
|
|
|
26
27
|
run: config.run,
|
|
27
28
|
...compact({
|
|
28
29
|
retry: config.retry,
|
|
30
|
+
timeoutMs: config.timeoutMs,
|
|
29
31
|
when: config.when,
|
|
30
32
|
onStart: config.onStart,
|
|
31
33
|
onComplete: config.onComplete,
|
|
@@ -42,6 +44,7 @@ function makeBuilder() {
|
|
|
42
44
|
run: config.run,
|
|
43
45
|
...compact({
|
|
44
46
|
retry: config.retry,
|
|
47
|
+
timeoutMs: config.timeoutMs,
|
|
45
48
|
when: config.when,
|
|
46
49
|
onStart: config.onStart,
|
|
47
50
|
onComplete: config.onComplete,
|
|
@@ -706,6 +709,9 @@ function computeFlowOutput(flow2, runState) {
|
|
|
706
709
|
}
|
|
707
710
|
return flow2.output(stepOutputs);
|
|
708
711
|
}
|
|
712
|
+
function resetSetOf(flow2, stepId, scope) {
|
|
713
|
+
return scope === "step" ? [stepId] : descendantsOf(flow2, stepId);
|
|
714
|
+
}
|
|
709
715
|
function descendantsOf(flow2, stepId) {
|
|
710
716
|
const children = /* @__PURE__ */ new Map();
|
|
711
717
|
const addEdge = (from, to) => {
|
|
@@ -815,6 +821,19 @@ function startCancelWatcher(args) {
|
|
|
815
821
|
}
|
|
816
822
|
};
|
|
817
823
|
}
|
|
824
|
+
function startDeadline(args) {
|
|
825
|
+
const { runId, stepId, attempt, timeoutMs, ac } = args;
|
|
826
|
+
const timer = setTimeout(() => {
|
|
827
|
+
if (!ac.signal.aborted)
|
|
828
|
+
ac.abort(new NagiStepTimeoutError({ runId, stepId, attempt, timeoutMs }));
|
|
829
|
+
}, timeoutMs);
|
|
830
|
+
return {
|
|
831
|
+
stop: () => clearTimeout(timer)
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
function unwrapDeadline(err, signal) {
|
|
835
|
+
return signal.reason instanceof NagiStepTimeoutError ? signal.reason : err;
|
|
836
|
+
}
|
|
818
837
|
var DEFAULT_HEARTBEAT_LEASE_MS = 12e4;
|
|
819
838
|
var DEFAULT_HEARTBEAT_INTERVAL_MS = 4e4;
|
|
820
839
|
var DEFAULT_LEASE_HOLD_WARN_MS = 3e5;
|
|
@@ -1200,6 +1219,13 @@ function makeMessage(deps, hooks, progression) {
|
|
|
1200
1219
|
ac,
|
|
1201
1220
|
intervalMs: deps.cancelPollIntervalMs ?? CANCEL_POLL_INTERVAL_MS
|
|
1202
1221
|
});
|
|
1222
|
+
const deadline = def.timeoutMs === void 0 ? void 0 : startDeadline({
|
|
1223
|
+
runId,
|
|
1224
|
+
stepId,
|
|
1225
|
+
attempt,
|
|
1226
|
+
timeoutMs: def.timeoutMs,
|
|
1227
|
+
ac
|
|
1228
|
+
});
|
|
1203
1229
|
try {
|
|
1204
1230
|
let stepAbortedHere = false;
|
|
1205
1231
|
const output = await store.runStep(
|
|
@@ -1239,8 +1265,11 @@ function makeMessage(deps, hooks, progression) {
|
|
|
1239
1265
|
}
|
|
1240
1266
|
);
|
|
1241
1267
|
return { output, skipAdvance: stepAbortedHere };
|
|
1268
|
+
} catch (err) {
|
|
1269
|
+
throw unwrapDeadline(err, ac.signal);
|
|
1242
1270
|
} finally {
|
|
1243
1271
|
watcher.stop();
|
|
1272
|
+
deadline?.stop();
|
|
1244
1273
|
}
|
|
1245
1274
|
}
|
|
1246
1275
|
async function executeActivity(args) {
|
|
@@ -1257,6 +1286,13 @@ function makeMessage(deps, hooks, progression) {
|
|
|
1257
1286
|
ac,
|
|
1258
1287
|
intervalMs: deps.cancelPollIntervalMs ?? CANCEL_POLL_INTERVAL_MS
|
|
1259
1288
|
});
|
|
1289
|
+
const deadline = def.timeoutMs === void 0 ? void 0 : startDeadline({
|
|
1290
|
+
runId,
|
|
1291
|
+
stepId,
|
|
1292
|
+
attempt,
|
|
1293
|
+
timeoutMs: def.timeoutMs,
|
|
1294
|
+
ac
|
|
1295
|
+
});
|
|
1260
1296
|
try {
|
|
1261
1297
|
const ctx = makeActivityCtx({
|
|
1262
1298
|
runId,
|
|
@@ -1289,8 +1325,11 @@ function makeMessage(deps, hooks, progression) {
|
|
|
1289
1325
|
}
|
|
1290
1326
|
);
|
|
1291
1327
|
return { output, skipAdvance: stepAbortedHere };
|
|
1328
|
+
} catch (err) {
|
|
1329
|
+
throw unwrapDeadline(err, ac.signal);
|
|
1292
1330
|
} finally {
|
|
1293
1331
|
watcher.stop();
|
|
1332
|
+
deadline?.stop();
|
|
1294
1333
|
}
|
|
1295
1334
|
}
|
|
1296
1335
|
async function executeSubflow(args) {
|
|
@@ -1503,7 +1542,17 @@ async function registerFlows(deps) {
|
|
|
1503
1542
|
});
|
|
1504
1543
|
return isTerminalRun(state) ? { kind: "gone-terminal", error, live } : { kind: "gone-live", error, live };
|
|
1505
1544
|
}
|
|
1506
|
-
|
|
1545
|
+
const synthesizedByHash = /* @__PURE__ */ new Map();
|
|
1546
|
+
function synthesize(gone) {
|
|
1547
|
+
const { pinnedHash } = gone.error;
|
|
1548
|
+
const cached = synthesizedByHash.get(pinnedHash);
|
|
1549
|
+
if (cached !== void 0) return cached;
|
|
1550
|
+
const pending = synthesizeUncached(gone);
|
|
1551
|
+
synthesizedByHash.set(pinnedHash, pending);
|
|
1552
|
+
pending.catch(() => synthesizedByHash.delete(pinnedHash));
|
|
1553
|
+
return pending;
|
|
1554
|
+
}
|
|
1555
|
+
async function synthesizeUncached(gone) {
|
|
1507
1556
|
const { runId, flowId, pinnedHash } = gone.error;
|
|
1508
1557
|
if (gone.live === void 0) {
|
|
1509
1558
|
throw new NagiRuntimeError(
|
|
@@ -1513,7 +1562,7 @@ async function registerFlows(deps) {
|
|
|
1513
1562
|
const snapshot = await store.loadSnapshot(pinnedHash);
|
|
1514
1563
|
if (snapshot === null) {
|
|
1515
1564
|
throw new NagiRuntimeError(
|
|
1516
|
-
`Run ${runId} pinned to flow hash ${pinnedHash.slice(0, 12)}\u2026 but no snapshot with that hash was found.
|
|
1565
|
+
`Run ${runId} pinned to flow hash ${pinnedHash.slice(0, 12)}\u2026 but no snapshot with that hash was found. Nothing can resume it.`
|
|
1517
1566
|
);
|
|
1518
1567
|
}
|
|
1519
1568
|
return synthesizeReplayFlow(
|
|
@@ -1927,15 +1976,15 @@ function makeOperator(o) {
|
|
|
1927
1976
|
ABORT_SETTLE_DEADLINE_MS
|
|
1928
1977
|
);
|
|
1929
1978
|
}
|
|
1930
|
-
const
|
|
1979
|
+
const resetSet = resetSetOf(flow2, stepId, opts.scope);
|
|
1931
1980
|
const at = clock.now();
|
|
1932
|
-
for (const id of
|
|
1981
|
+
for (const id of resetSet) {
|
|
1933
1982
|
const fact = id === stepId ? Facts.stepReset({
|
|
1934
1983
|
runId,
|
|
1935
1984
|
stepId: id,
|
|
1936
1985
|
at,
|
|
1937
1986
|
actor: opts.actor,
|
|
1938
|
-
...compact({ note: opts.note })
|
|
1987
|
+
...compact({ scope: opts.scope, note: opts.note })
|
|
1939
1988
|
}) : Facts.stepReset({ runId, stepId: id, at, cascadedFrom: stepId });
|
|
1940
1989
|
await store.appendFact(runId, fact);
|
|
1941
1990
|
}
|
|
@@ -1955,7 +2004,7 @@ function makeOperator(o) {
|
|
|
1955
2004
|
|
|
1956
2005
|
// src/replay.ts
|
|
1957
2006
|
function makeReplay(deps) {
|
|
1958
|
-
const { registry, store, queue, clock } = deps;
|
|
2007
|
+
const { registry, store, queue, clock, driftPolicy } = deps;
|
|
1959
2008
|
async function replay(runId, opts = { mode: "continue" }) {
|
|
1960
2009
|
const runState = await store.loadRunState(runId);
|
|
1961
2010
|
const resolution = registry.resolve(runState);
|
|
@@ -1976,7 +2025,7 @@ function makeReplay(deps) {
|
|
|
1976
2025
|
let replayDeps = baseDeps;
|
|
1977
2026
|
let effectiveFlow = liveFlow;
|
|
1978
2027
|
if (resolution.kind !== "current") {
|
|
1979
|
-
if (!opts.allowDrift) {
|
|
2028
|
+
if (!opts.allowDrift && driftPolicy === "freeze") {
|
|
1980
2029
|
throw new NagiSnapshotDriftError({
|
|
1981
2030
|
runId,
|
|
1982
2031
|
expected: resolution.error.pinnedHash,
|
|
@@ -2002,10 +2051,15 @@ function makeReplay(deps) {
|
|
|
2002
2051
|
["from"]
|
|
2003
2052
|
);
|
|
2004
2053
|
}
|
|
2005
|
-
const
|
|
2054
|
+
const resetSet = resetSetOf(effectiveFlow, opts.from, opts.scope);
|
|
2006
2055
|
const at = clock.now();
|
|
2007
|
-
for (const stepId of
|
|
2008
|
-
const fact = stepId === opts.from ? Facts.stepReset({
|
|
2056
|
+
for (const stepId of resetSet) {
|
|
2057
|
+
const fact = stepId === opts.from ? Facts.stepReset({
|
|
2058
|
+
runId,
|
|
2059
|
+
stepId,
|
|
2060
|
+
at,
|
|
2061
|
+
...compact({ scope: opts.scope })
|
|
2062
|
+
}) : Facts.stepReset({ runId, stepId, at, cascadedFrom: opts.from });
|
|
2009
2063
|
await store.appendFact(runId, fact);
|
|
2010
2064
|
}
|
|
2011
2065
|
}
|
|
@@ -2509,12 +2563,21 @@ async function nagiImpl(config) {
|
|
|
2509
2563
|
const emitLog = makeEmit(config.onLog);
|
|
2510
2564
|
await config.queue.ensureSchema?.();
|
|
2511
2565
|
const streamTransport = config.store.stream;
|
|
2566
|
+
const requireEvents = (caller) => {
|
|
2567
|
+
const events = config.store.events;
|
|
2568
|
+
if (events === void 0) {
|
|
2569
|
+
throw new NagiRuntimeError(
|
|
2570
|
+
`${caller}: this store has no \`events\` transport, so lifecycle events cannot be observed. The in-memory store always has one; postgresStore() needs a \`listener\`.`
|
|
2571
|
+
);
|
|
2572
|
+
}
|
|
2573
|
+
return events;
|
|
2574
|
+
};
|
|
2512
2575
|
if (streamTransport === void 0) {
|
|
2513
2576
|
for (const f of config.flows) {
|
|
2514
2577
|
for (const [stepId, step] of Object.entries(asStepMapWithDefs(f.steps))) {
|
|
2515
2578
|
if (getDef(step).kind !== "streaming") continue;
|
|
2516
2579
|
throw new NagiRuntimeError(
|
|
2517
|
-
`Flow "${f.id}" has a streaming step "${stepId}" (b.streamingTask), but the store has no \`stream\` transport \u2014 it cannot carry ephemeral chunks.
|
|
2580
|
+
`Flow "${f.id}" has a streaming step "${stepId}" (b.streamingTask), but the store has no \`stream\` transport \u2014 it cannot carry ephemeral chunks. The in-memory store always exposes one; postgresStore() needs a \`listener\`. Otherwise remove the streaming step.`
|
|
2518
2581
|
);
|
|
2519
2582
|
}
|
|
2520
2583
|
}
|
|
@@ -2525,8 +2588,28 @@ async function nagiImpl(config) {
|
|
|
2525
2588
|
clock
|
|
2526
2589
|
});
|
|
2527
2590
|
const codeVersion = config.codeVersion ?? await fingerprintFlows(config.flows);
|
|
2591
|
+
const driftPolicy = config.driftPolicy ?? "freeze";
|
|
2528
2592
|
async function resolveFlow(runId) {
|
|
2529
|
-
|
|
2593
|
+
const resolution = registry.resolve(await config.store.loadRunState(runId));
|
|
2594
|
+
if (resolution.kind !== "gone-live" || driftPolicy === "freeze") {
|
|
2595
|
+
return resolution;
|
|
2596
|
+
}
|
|
2597
|
+
try {
|
|
2598
|
+
return { kind: "current", flow: await registry.synthesize(resolution) };
|
|
2599
|
+
} catch (err) {
|
|
2600
|
+
if (!(err instanceof NagiRuntimeError)) throw err;
|
|
2601
|
+
emitLog({
|
|
2602
|
+
level: "warn",
|
|
2603
|
+
msg: "resolveFlow: drift synthesis failed \u2014 treating run as snapshot-gone",
|
|
2604
|
+
attrs: {
|
|
2605
|
+
runId: resolution.error.runId,
|
|
2606
|
+
flowId: resolution.error.flowId,
|
|
2607
|
+
pinnedHash: resolution.error.pinnedHash,
|
|
2608
|
+
error: err.message
|
|
2609
|
+
}
|
|
2610
|
+
});
|
|
2611
|
+
return resolution;
|
|
2612
|
+
}
|
|
2530
2613
|
}
|
|
2531
2614
|
function lookupFlow(flowId) {
|
|
2532
2615
|
return registry.get(flowId);
|
|
@@ -2575,7 +2658,7 @@ async function nagiImpl(config) {
|
|
|
2575
2658
|
emitLog,
|
|
2576
2659
|
...compact({ flowHooks: config.hooks })
|
|
2577
2660
|
});
|
|
2578
|
-
const replayer = makeReplay({ ...dispatchDeps, registry });
|
|
2661
|
+
const replayer = makeReplay({ ...dispatchDeps, registry, driftPolicy });
|
|
2579
2662
|
const wf = {
|
|
2580
2663
|
async start(flow2, input, opts) {
|
|
2581
2664
|
return wf.startById(flow2.id, input, opts);
|
|
@@ -2678,6 +2761,12 @@ async function nagiImpl(config) {
|
|
|
2678
2761
|
opts
|
|
2679
2762
|
);
|
|
2680
2763
|
},
|
|
2764
|
+
watchRun(runId, handler) {
|
|
2765
|
+
return requireEvents("wf.watchRun").watchRun(runId, handler);
|
|
2766
|
+
},
|
|
2767
|
+
watchRuns(handler) {
|
|
2768
|
+
return requireEvents("wf.watchRuns").watchRuns(handler);
|
|
2769
|
+
},
|
|
2681
2770
|
async pruneFacts(opts) {
|
|
2682
2771
|
if (!(opts.olderThan instanceof Date) || Number.isNaN(opts.olderThan.getTime())) {
|
|
2683
2772
|
throw validationError("pruneFacts: `olderThan` must be a valid Date.", [
|