@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.
@@ -241,6 +241,27 @@ var NagiSignalTimeoutError = class extends Error {
241
241
  this.fireAt = fireAt;
242
242
  }
243
243
  };
244
+ var NagiStepTimeoutError = class extends Error {
245
+ runId;
246
+ stepId;
247
+ attempt;
248
+ timeoutMs;
249
+ constructor({
250
+ runId,
251
+ stepId,
252
+ attempt,
253
+ timeoutMs
254
+ }) {
255
+ super(
256
+ `Step "${stepId}" (run ${runId}, attempt ${attempt}) exceeded its ${timeoutMs}ms deadline \u2014 ctx.signal aborted.`
257
+ );
258
+ this.name = "NagiStepTimeoutError";
259
+ this.runId = runId;
260
+ this.stepId = stepId;
261
+ this.attempt = attempt;
262
+ this.timeoutMs = timeoutMs;
263
+ }
264
+ };
244
265
  var NagiFlowSnapshotGoneError = class extends Error {
245
266
  runId;
246
267
  flowId;
@@ -557,6 +578,7 @@ var stepFacts = {
557
578
  at: a.at,
558
579
  ...compact({
559
580
  cascadedFrom: a.cascadedFrom,
581
+ scope: a.scope === "cascade" ? void 0 : a.scope,
560
582
  actor: a.actor,
561
583
  note: a.note
562
584
  })
@@ -764,6 +786,98 @@ function decideExpiredLeaseAction(args) {
764
786
  }
765
787
  var DEFAULT_REAPER_INTERVAL_MS = 3e4;
766
788
 
789
+ // src/run-events.ts
790
+ function runEventOf(fact) {
791
+ switch (fact.kind) {
792
+ case "flow.started":
793
+ return { type: "flow.started", flowId: fact.flowId };
794
+ case "flow.completed":
795
+ return { type: "flow.completed", output: fact.output };
796
+ case "flow.failed":
797
+ return { type: "flow.failed", error: fact.error };
798
+ case "flow.canceled":
799
+ return fact.cause === "concurrency" ? {
800
+ type: "flow.canceled",
801
+ cause: "concurrency",
802
+ canceledByRunId: fact.canceledByRunId
803
+ } : { type: "flow.canceled", cause: fact.cause };
804
+ case "step.started":
805
+ return {
806
+ type: "step.started",
807
+ stepId: fact.stepId,
808
+ attempt: fact.attempt
809
+ };
810
+ case "step.completed":
811
+ return {
812
+ type: "step.completed",
813
+ stepId: fact.stepId,
814
+ attempt: fact.attempt,
815
+ output: fact.output
816
+ };
817
+ case "step.failed":
818
+ return {
819
+ type: "step.failed",
820
+ stepId: fact.stepId,
821
+ attempt: fact.attempt,
822
+ error: fact.error
823
+ };
824
+ case "step.retried":
825
+ return {
826
+ type: "step.retried",
827
+ stepId: fact.stepId,
828
+ // Carries the attempt that FAILED; the next one is attempt + 1.
829
+ attempt: fact.attempt
830
+ };
831
+ case "step.skipped":
832
+ return { type: "step.skipped", stepId: fact.stepId, reason: fact.reason };
833
+ default:
834
+ return null;
835
+ }
836
+ }
837
+ function isTerminalRunEvent(event) {
838
+ return event.type === "flow.completed" || event.type === "flow.failed" || event.type === "flow.canceled";
839
+ }
840
+ var InMemoryRunEventHub = class {
841
+ perRun = /* @__PURE__ */ new Map();
842
+ all = /* @__PURE__ */ new Set();
843
+ publish(envelope) {
844
+ const targeted = this.perRun.get(envelope.runId);
845
+ if (targeted !== void 0) {
846
+ for (const h of [...targeted]) safely(h, envelope);
847
+ }
848
+ for (const h of [...this.all]) safely(h, envelope);
849
+ if (targeted !== void 0 && isTerminalRunEvent(envelope)) {
850
+ this.perRun.delete(envelope.runId);
851
+ }
852
+ }
853
+ watchRun(runId, handler) {
854
+ let set = this.perRun.get(runId);
855
+ if (set === void 0) {
856
+ set = /* @__PURE__ */ new Set();
857
+ this.perRun.set(runId, set);
858
+ }
859
+ set.add(handler);
860
+ return () => {
861
+ const current = this.perRun.get(runId);
862
+ if (current === void 0) return;
863
+ current.delete(handler);
864
+ if (current.size === 0) this.perRun.delete(runId);
865
+ };
866
+ }
867
+ watchRuns(handler) {
868
+ this.all.add(handler);
869
+ return () => {
870
+ this.all.delete(handler);
871
+ };
872
+ }
873
+ };
874
+ function safely(handler, envelope) {
875
+ try {
876
+ handler(envelope);
877
+ } catch {
878
+ }
879
+ }
880
+
767
881
  // src/validate.ts
768
882
  async function validate(schema, value) {
769
883
  const result = await schema["~standard"].validate(value);
@@ -1237,6 +1351,8 @@ var InMemoryStore = class {
1237
1351
  }
1238
1352
  list.push(fact);
1239
1353
  this.facts.set(runId, list);
1354
+ const event = runEventOf(fact);
1355
+ if (event !== null) this.eventHub.publish({ ...event, runId });
1240
1356
  switch (fact.kind) {
1241
1357
  case "step.completed":
1242
1358
  this.streamHub.closeOk(runId, fact.stepId);
@@ -1286,7 +1402,7 @@ var InMemoryStore = class {
1286
1402
  }
1287
1403
  }
1288
1404
  async tryStartRun(runId, fact, concurrency) {
1289
- if (this.facts.has(runId) || this.summaries.has(runId)) {
1405
+ if (this.runExists(runId)) {
1290
1406
  return { started: false, canceled: [] };
1291
1407
  }
1292
1408
  const canceled = [];
@@ -1308,6 +1424,7 @@ var InMemoryStore = class {
1308
1424
  this.slotByRun.set(runId, slot);
1309
1425
  }
1310
1426
  this.facts.set(runId, [fact]);
1427
+ this.eventHub.publish({ type: "flow.started", flowId: fact.flowId, runId });
1311
1428
  const parentRunId = fact.parent?.runId;
1312
1429
  if (parentRunId !== void 0) {
1313
1430
  const set = this.childrenByParent.get(parentRunId) ?? /* @__PURE__ */ new Set();
@@ -1542,7 +1659,7 @@ var InMemoryStore = class {
1542
1659
  }
1543
1660
  if (f.kind === "flow.canceled") {
1544
1661
  completedAt = f.at;
1545
- if (f.cause === "concurrency") {
1662
+ if (f.cause === "concurrency" && this.runExists(f.canceledByRunId)) {
1546
1663
  canceledByRunId = f.canceledByRunId;
1547
1664
  }
1548
1665
  break;
@@ -1628,6 +1745,9 @@ var InMemoryStore = class {
1628
1745
  }
1629
1746
  // A run pruned with keepSummary keeps its row in Postgres; the summary is
1630
1747
  // the in-memory equivalent, so describe() stays non-null for it.
1748
+ runExists(runId) {
1749
+ return this.facts.has(runId) || this.summaries.has(runId);
1750
+ }
1631
1751
  describeSummary(runId) {
1632
1752
  const s = this.summaries.get(runId);
1633
1753
  if (s === void 0) return null;
@@ -1688,6 +1808,11 @@ var InMemoryStore = class {
1688
1808
  }
1689
1809
  return out;
1690
1810
  }
1811
+ eventHub = new InMemoryRunEventHub();
1812
+ events = {
1813
+ watchRun: (runId, handler) => this.eventHub.watchRun(runId, handler),
1814
+ watchRuns: (handler) => this.eventHub.watchRuns(handler)
1815
+ };
1691
1816
  stream = {
1692
1817
  subscribeStream: (runId, stepId, opts) => {
1693
1818
  const state = foldRun(runId, this.facts.get(runId) ?? []);
@@ -1828,6 +1953,6 @@ var InMemoryClock = class {
1828
1953
  }
1829
1954
  };
1830
1955
 
1831
- export { DEFAULT_REAPER_INTERVAL_MS, DEFAULT_RETRY, DEFAULT_SWEEP_LIMIT, Facts, InMemoryClock, InMemoryQueue, InMemoryStore, NagiCanceledError, NagiConcurrencyConflictError, NagiFlowSnapshotGoneError, NagiNonRetryableError, NagiRuntimeError, NagiSignalTimeoutError, NagiSnapshotDriftError, NagiValidationError, QUERY_RUNS_DEFAULT_LIMIT, QUERY_RUNS_MAX_LIMIT, asStepMapWithDefs, attachDef, attemptOf, clampQueryLimit, compact, compareRunOrder, decideExpiredLeaseAction, decideSignal, decideTimeout, decodeRunCursor, defaultSnapshotGonePolicy, dequeueBackoff, encodeRunCursor, errorOf, factEffects, foldRun, getDef, handlerDef, isAbortRequested, isPastCursor, isStepTerminal, isTerminalRun, jsonContains, makeEmit, makeSignals, needsStepIds, normalizeNeeds, outputOf, peekDef, resolveNeeds, resolveRetry, resolvedOf, rowDeltaOf, runStatusOf, selectArm, selectExpired, selectPruneBatch, serializeError, setDef, stepBackoff, stepStateOf, stepStatusOf, unwrap, validate, validationError };
1832
- //# sourceMappingURL=chunk-VQTYTI6X.js.map
1833
- //# sourceMappingURL=chunk-VQTYTI6X.js.map
1956
+ export { DEFAULT_REAPER_INTERVAL_MS, DEFAULT_RETRY, DEFAULT_SWEEP_LIMIT, Facts, 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, asStepMapWithDefs, attachDef, attemptOf, clampQueryLimit, compact, compareRunOrder, decideExpiredLeaseAction, decideSignal, decideTimeout, decodeRunCursor, defaultSnapshotGonePolicy, dequeueBackoff, encodeRunCursor, errorOf, factEffects, foldRun, getDef, handlerDef, isAbortRequested, isPastCursor, isStepTerminal, isTerminalRun, isTerminalRunEvent, jsonContains, makeEmit, makeSignals, needsStepIds, normalizeNeeds, outputOf, peekDef, resolveNeeds, resolveRetry, resolvedOf, rowDeltaOf, runEventOf, runStatusOf, selectArm, selectExpired, selectPruneBatch, serializeError, setDef, stepBackoff, stepStateOf, stepStatusOf, unwrap, validate, validationError };
1957
+ //# sourceMappingURL=chunk-VU2LG4VW.js.map
1958
+ //# sourceMappingURL=chunk-VU2LG4VW.js.map