@nagi-js/core 0.1.1-rc.25 → 0.1.1-rc.27

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.
@@ -204,6 +204,49 @@ var NagiSignalTimeoutError = class extends Error {
204
204
  this.fireAt = fireAt;
205
205
  }
206
206
  };
207
+ var NagiRunStrandedError = class extends Error {
208
+ runId;
209
+ flowId;
210
+ stepIds;
211
+ constructor({
212
+ runId,
213
+ flowId,
214
+ stepIds,
215
+ reason
216
+ }) {
217
+ super(
218
+ `Run ${runId} (flow "${flowId}") is stranded: ${reason}${stepIds.length > 0 ? ` (steps: ${stepIds.join(", ")})` : ""}.`
219
+ );
220
+ this.name = "NagiRunStrandedError";
221
+ this.runId = runId;
222
+ this.flowId = flowId;
223
+ this.stepIds = stepIds;
224
+ }
225
+ };
226
+ var NagiDeliveryExhaustedError = class extends Error {
227
+ runId;
228
+ stepId;
229
+ attempt;
230
+ readCount;
231
+ constructor({
232
+ runId,
233
+ flowId,
234
+ stepId,
235
+ attempt,
236
+ readCount,
237
+ cause
238
+ }) {
239
+ super(
240
+ `Step "${stepId}" (attempt ${attempt}, run ${runId}, flow "${flowId}") could not be dispatched in ${readCount} deliveries: ${cause instanceof Error ? cause.message : String(cause)}`,
241
+ { cause }
242
+ );
243
+ this.name = "NagiDeliveryExhaustedError";
244
+ this.runId = runId;
245
+ this.stepId = stepId;
246
+ this.attempt = attempt;
247
+ this.readCount = readCount;
248
+ }
249
+ };
207
250
  var NagiStepTimeoutError = class extends Error {
208
251
  runId;
209
252
  stepId;
@@ -287,11 +330,26 @@ function serializeError(err) {
287
330
  return {
288
331
  name: err.name,
289
332
  message: err.message,
290
- ...compact({ stack: err.stack })
333
+ ...compact({ stack: err.stack, cause: serializeCause(err.cause, 3) })
291
334
  };
292
335
  }
293
336
  return { name: "Error", message: String(err) };
294
337
  }
338
+ function serializeCause(cause, depth) {
339
+ if (cause === void 0 || cause === null || depth === 0) return void 0;
340
+ if (cause instanceof Error) {
341
+ return {
342
+ name: cause.name,
343
+ message: cause.message,
344
+ ...compact({ cause: serializeCause(cause.cause, depth - 1) })
345
+ };
346
+ }
347
+ try {
348
+ return JSON.parse(JSON.stringify(cause));
349
+ } catch {
350
+ return String(cause);
351
+ }
352
+ }
295
353
 
296
354
  // src/facts/flow.ts
297
355
  function isRunEnd(fact) {
@@ -864,10 +922,9 @@ function dequeueBackoff(pollIntervalMs) {
864
922
  const base = Math.max(pollIntervalMs, MIN_DEQUEUE_BACKOFF_MS);
865
923
  return (consecutiveFailures) => Math.min(base * 2 ** (consecutiveFailures - 1), MAX_DEQUEUE_BACKOFF_MS);
866
924
  }
867
- var SNAPSHOT_GONE_DELIVERY_BUDGET = 60;
868
- var MAX_SNAPSHOT_GONE_BACKOFF_MS = 3e5;
869
- var snapshotGoneBackoff = (readCount) => Math.min(readCount * readCount * 1e3, MAX_SNAPSHOT_GONE_BACKOFF_MS);
870
- var defaultSnapshotGonePolicy = (readCount) => readCount > SNAPSHOT_GONE_DELIVERY_BUDGET ? { action: "fail" } : { action: "retry", delayMs: snapshotGoneBackoff(readCount) };
925
+ var MAX_DELIVERIES = 10;
926
+ var SNAPSHOT_GONE_RETRY_DELAY_MS = 6e4;
927
+ var defaultSnapshotGonePolicy = (readCount) => readCount >= MAX_DELIVERIES ? { action: "fail" } : { action: "retry", delayMs: SNAPSHOT_GONE_RETRY_DELAY_MS };
871
928
  var reapBackoff = () => 0;
872
929
 
873
930
  // src/lease-reaper.ts
@@ -1250,12 +1307,14 @@ function synthesizeReplayFlow(dag, liveFlow) {
1250
1307
  }
1251
1308
  needs[alias] = { step: upstreamShell, optional: ref.optional };
1252
1309
  }
1253
- const liveNeedIds = new Set(
1254
- Object.values(liveDef.needs).map((ref) => ref.step.id)
1255
- );
1256
1310
  const canonNeedIds = new Set(canonStep.needs);
1257
- const sameNeeds = liveNeedIds.size === canonNeedIds.size && [...liveNeedIds].every((id) => canonNeedIds.has(id));
1258
- if (!sameNeeds) {
1311
+ const addsRequiredNeed = Object.values(liveDef.needs).some(
1312
+ (ref) => !ref.optional && !canonNeedIds.has(ref.step.id)
1313
+ );
1314
+ if (addsRequiredNeed) {
1315
+ const liveNeedIds = new Set(
1316
+ Object.values(liveDef.needs).map((ref) => ref.step.id)
1317
+ );
1259
1318
  throw new NagiRuntimeError(
1260
1319
  `Drift-allowed replay: step "${canonStep.id}" needs [${[...canonNeedIds].sort().join(", ")}] in the snapshot but [${[...liveNeedIds].sort().join(", ")}] live.`
1261
1320
  );
@@ -1955,6 +2014,8 @@ var InMemoryStore = class {
1955
2014
  return foldRun(runId, this.facts.get(runId) ?? []);
1956
2015
  }
1957
2016
  async claimStep(runId, stepId, attempt) {
2017
+ const status = this.stepRows.get(runId)?.get(stepId)?.status;
2018
+ if (status !== void 0 && SETTLED.has(status)) return null;
1958
2019
  const key = leaseKey(runId, stepId, attempt);
1959
2020
  const now = Date.now();
1960
2021
  const existing = this.leases.get(key);
@@ -2065,6 +2126,20 @@ var InMemoryStore = class {
2065
2126
  }
2066
2127
  return timedOut;
2067
2128
  }
2129
+ async listQuietRuns(args) {
2130
+ const { quietSince, limit = DEFAULT_SWEEP_LIMIT } = args;
2131
+ const busy = /* @__PURE__ */ new Set([
2132
+ ...[...this.leases.values()].map((l) => l.runId),
2133
+ ...[...this.signalTimers.values()].map((t) => t.runId)
2134
+ ]);
2135
+ const live = (status) => status === "pending" || status === "running";
2136
+ return [...this.runRows].filter(([runId, row]) => {
2137
+ if (!live(row.status) || busy.has(runId)) return false;
2138
+ if (this.childRows(runId).some(([, c]) => live(c.status))) return false;
2139
+ const last = (this.facts.get(runId) ?? []).at(-1);
2140
+ return last === void 0 || last.at < quietSince;
2141
+ }).map(([runId]) => runId).slice(0, limit);
2142
+ }
2068
2143
  async recordOnce(runId, stepId, scope, value) {
2069
2144
  const key = `${runId}::${stepId}::${scope}`;
2070
2145
  if (!this.onces.has(key)) this.onces.set(key, value);
@@ -2232,6 +2307,11 @@ var InMemoryQueue = class {
2232
2307
  }
2233
2308
  async dequeue(opts) {
2234
2309
  const now = Date.now();
2310
+ for (const [receipt, item] of this.leased) {
2311
+ if (item.visibleAt > now) continue;
2312
+ this.leased.delete(receipt);
2313
+ this.pending.push(item);
2314
+ }
2235
2315
  const claimed = [];
2236
2316
  for (let i = 0; i < this.pending.length && claimed.length < opts.count; i++) {
2237
2317
  const item = this.pending[i];
@@ -2299,6 +2379,6 @@ var InMemoryClock = class {
2299
2379
  }
2300
2380
  };
2301
2381
 
2302
- export { DEFAULT_REAPER_INTERVAL_MS, DEFAULT_SWEEP_LIMIT, Facts, InMemoryClock, InMemoryQueue, InMemoryRunEventHub, InMemoryStore, InMemoryStreamHub, NagiCanceledError, NagiConcurrencyConflictError, NagiFlowSnapshotGoneError, NagiNonRetryableError, NagiRuntimeError, NagiSignalTimeoutError, NagiSnapshotDriftError, NagiStepTimeoutError, NagiValidationError, STREAM_REPLAY_BUFFER_CAP, STREAM_SUBSCRIBER_BUFFER_CAP, admitsRunEnd, asStepMapWithDefs, attachDef, attemptOf, compact, decideExpiredLeaseAction, decideSignal, decideTimeout, defaultSnapshotGonePolicy, dequeueBackoff, errorOf, factConsequences, fingerprintFlows, foldRun, getDef, handlerDef, isAbortRequested, isRunEnd, isStepTerminal, isTerminalRun, isTerminalRunEvent, makeEmit, makeFlowOf, makeSignals, needsStepIds, normalizeNeeds, outputOf, peekDef, pinRun, registerFlows, requireCurrent, resolveNeeds, resolveRetry, resolvedOf, runCancelCause, runStatusOf, selectExpired, selectPruneBatch, serializeError, stepBackoff, stepStateOf, stepStatusOf, streamEndOf, supersede, unwrap, validate, validationError };
2303
- //# sourceMappingURL=chunk-TXWUVL6W.js.map
2304
- //# sourceMappingURL=chunk-TXWUVL6W.js.map
2382
+ export { DEFAULT_REAPER_INTERVAL_MS, DEFAULT_SWEEP_LIMIT, Facts, InMemoryClock, InMemoryQueue, InMemoryRunEventHub, InMemoryStore, InMemoryStreamHub, MAX_DELIVERIES, NagiCanceledError, NagiConcurrencyConflictError, NagiDeliveryExhaustedError, NagiFlowSnapshotGoneError, NagiNonRetryableError, NagiRunStrandedError, NagiRuntimeError, NagiSignalTimeoutError, NagiSnapshotDriftError, NagiStepTimeoutError, NagiValidationError, STREAM_REPLAY_BUFFER_CAP, STREAM_SUBSCRIBER_BUFFER_CAP, admitsRunEnd, asStepMapWithDefs, attachDef, attemptOf, compact, decideExpiredLeaseAction, decideSignal, decideTimeout, defaultSnapshotGonePolicy, dequeueBackoff, errorOf, factConsequences, fingerprintFlows, foldRun, getDef, handlerDef, isAbortRequested, isRunEnd, isStepTerminal, isTerminalRun, isTerminalRunEvent, makeEmit, makeFlowOf, makeSignals, needsStepIds, normalizeNeeds, outputOf, peekDef, pinRun, registerFlows, requireCurrent, resolveNeeds, resolveRetry, resolvedOf, runCancelCause, runStatusOf, selectExpired, selectPruneBatch, serializeError, stepBackoff, stepStateOf, stepStatusOf, streamEndOf, supersede, unwrap, validate, validationError };
2383
+ //# sourceMappingURL=chunk-7SKD73QT.js.map
2384
+ //# sourceMappingURL=chunk-7SKD73QT.js.map