@nagi-js/core 0.1.1-rc.20 → 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/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, stepStateOf, serializeError, NagiCanceledError, NagiSignalTimeoutError, dequeueBackoff, defaultSnapshotGonePolicy, isStepTerminal, stepStatusOf, NagiSnapshotDriftError, attachDef, setDef, attemptOf, DEFAULT_RETRY, outputOf, resolveNeeds, resolvedOf, handlerDef, resolveRetry, isAbortRequested, selectArm, stepBackoff, peekDef, NagiNonRetryableError, normalizeNeeds } from './chunk-VQTYTI6X.js';
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-VQTYTI6X.js';
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) {
@@ -1937,15 +1976,15 @@ function makeOperator(o) {
1937
1976
  ABORT_SETTLE_DEADLINE_MS
1938
1977
  );
1939
1978
  }
1940
- const cascade = descendantsOf(flow2, stepId);
1979
+ const resetSet = resetSetOf(flow2, stepId, opts.scope);
1941
1980
  const at = clock.now();
1942
- for (const id of cascade) {
1981
+ for (const id of resetSet) {
1943
1982
  const fact = id === stepId ? Facts.stepReset({
1944
1983
  runId,
1945
1984
  stepId: id,
1946
1985
  at,
1947
1986
  actor: opts.actor,
1948
- ...compact({ note: opts.note })
1987
+ ...compact({ scope: opts.scope, note: opts.note })
1949
1988
  }) : Facts.stepReset({ runId, stepId: id, at, cascadedFrom: stepId });
1950
1989
  await store.appendFact(runId, fact);
1951
1990
  }
@@ -2012,10 +2051,15 @@ function makeReplay(deps) {
2012
2051
  ["from"]
2013
2052
  );
2014
2053
  }
2015
- const cascade = descendantsOf(effectiveFlow, opts.from);
2054
+ const resetSet = resetSetOf(effectiveFlow, opts.from, opts.scope);
2016
2055
  const at = clock.now();
2017
- for (const stepId of cascade) {
2018
- const fact = stepId === opts.from ? Facts.stepReset({ runId, stepId, at }) : Facts.stepReset({ runId, stepId, at, cascadedFrom: opts.from });
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 });
2019
2063
  await store.appendFact(runId, fact);
2020
2064
  }
2021
2065
  }
@@ -2519,12 +2563,21 @@ async function nagiImpl(config) {
2519
2563
  const emitLog = makeEmit(config.onLog);
2520
2564
  await config.queue.ensureSchema?.();
2521
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
+ };
2522
2575
  if (streamTransport === void 0) {
2523
2576
  for (const f of config.flows) {
2524
2577
  for (const [stepId, step] of Object.entries(asStepMapWithDefs(f.steps))) {
2525
2578
  if (getDef(step).kind !== "streaming") continue;
2526
2579
  throw new NagiRuntimeError(
2527
- `Flow "${f.id}" has a streaming step "${stepId}" (b.streamingTask), but the store has no \`stream\` transport \u2014 it cannot carry ephemeral chunks. Use a store that exposes \`stream\` (e.g. the in-memory store) or remove the streaming step.`
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.`
2528
2581
  );
2529
2582
  }
2530
2583
  }
@@ -2708,6 +2761,12 @@ async function nagiImpl(config) {
2708
2761
  opts
2709
2762
  );
2710
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
+ },
2711
2770
  async pruneFacts(opts) {
2712
2771
  if (!(opts.olderThan instanceof Date) || Number.isNaN(opts.olderThan.getTime())) {
2713
2772
  throw validationError("pruneFacts: `olderThan` must be a valid Date.", [