@nagi-js/core 0.1.1-rc.12 → 0.1.1-rc.14

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 CHANGED
@@ -1,3 +1,65 @@
1
+ type StepRunStatus = StepStatus;
2
+ interface RunView {
3
+ readonly runId: RunId$1;
4
+ readonly flowId: string;
5
+ readonly flowHash: string;
6
+ readonly status: RunStatus;
7
+ readonly startedAt: Date;
8
+ readonly completedAt?: Date;
9
+ readonly input: Json;
10
+ readonly output?: Json;
11
+ readonly error?: Json;
12
+ readonly canceledByRunId?: RunId$1;
13
+ readonly concurrencyKey?: string;
14
+ readonly parent?: {
15
+ readonly runId: RunId$1;
16
+ readonly stepId: StepId;
17
+ };
18
+ readonly children: readonly RunId$1[];
19
+ }
20
+ interface StepView {
21
+ readonly stepId: StepId;
22
+ readonly attempt: AttemptNumber;
23
+ readonly status: StepRunStatus;
24
+ readonly startedAt?: Date;
25
+ readonly completedAt?: Date;
26
+ readonly output?: Json;
27
+ readonly error?: Json;
28
+ readonly lease?: {
29
+ readonly expiresAt: Date;
30
+ };
31
+ }
32
+ type RunDescription = {
33
+ readonly run: RunView;
34
+ readonly steps: readonly StepView[];
35
+ } | null;
36
+
37
+ type ReaperDecision = {
38
+ readonly tag: "skip";
39
+ readonly reason: "terminal" | "still-live";
40
+ } | {
41
+ readonly tag: "reap";
42
+ readonly nextAttempt: AttemptNumber;
43
+ readonly backoffMs: Millis;
44
+ };
45
+ declare function decideExpiredLeaseAction(args: {
46
+ readonly lease: {
47
+ readonly runId: RunId$1;
48
+ readonly stepId: StepId;
49
+ readonly attempt: AttemptNumber;
50
+ readonly expiresAt: Date;
51
+ };
52
+ readonly stepStatus: StepRunStatus;
53
+ readonly now: Date;
54
+ }): ReaperDecision;
55
+ interface ReapedLease {
56
+ readonly runId: RunId$1;
57
+ readonly stepId: StepId;
58
+ readonly attempt: AttemptNumber;
59
+ readonly nextAttempt: AttemptNumber;
60
+ }
61
+ declare const DEFAULT_REAPER_INTERVAL_MS: Millis;
62
+
1
63
  type SkipReason = "when-false" | "transitive" | "manual";
2
64
  type StepCancelCause = {
3
65
  readonly kind: "run-canceled";
@@ -7,7 +69,7 @@ type StepCancelCause = {
7
69
  };
8
70
  type RunCancelCause = {
9
71
  readonly kind: "concurrency";
10
- readonly canceledByRunId: RunId;
72
+ readonly canceledByRunId: RunId$1;
11
73
  readonly concurrencyKey: string;
12
74
  } | {
13
75
  readonly kind: "explicit";
@@ -80,7 +142,7 @@ interface Anomaly {
80
142
  readonly fact: Fact["kind"];
81
143
  }
82
144
  interface RunState {
83
- readonly runId: RunId;
145
+ readonly runId: RunId$1;
84
146
  readonly flowId: string;
85
147
  readonly input: Json;
86
148
  readonly phase: RunPhase;
@@ -106,13 +168,13 @@ declare function resolvedOf(s: StepState): Resolved;
106
168
  declare function unwrap<T>(r: Resolved<T>): T;
107
169
  declare function isTerminalRun(state: RunState): boolean;
108
170
  declare function isStepTerminal(s: StepState): boolean;
109
- declare function foldRun(runId: RunId, facts: readonly Fact[]): RunState;
171
+ declare function foldRun(runId: RunId$1, facts: readonly Fact[]): RunState;
110
172
 
111
173
  type Json = string | number | boolean | null | Json[] | {
112
174
  [key: string]: Json;
113
175
  };
114
176
  type Millis = number;
115
- type RunId = string & {
177
+ type RunId$1 = string & {
116
178
  readonly __brand: "RunId";
117
179
  };
118
180
  type StepId = string;
@@ -154,7 +216,7 @@ interface Register {
154
216
  type Tx = Register extends {
155
217
  tx: infer T;
156
218
  } ? T : unknown;
157
- type StepKind = "task" | "signal" | "match" | "subflow" | "streaming";
219
+ type StepKind = "task" | "activity" | "signal" | "match" | "subflow" | "streaming";
158
220
  interface Step<Output = unknown> {
159
221
  readonly kind: StepKind;
160
222
  readonly id: StepId;
@@ -188,7 +250,7 @@ interface Logger {
188
250
  interface StepCtx<Input = unknown> {
189
251
  readonly input: Input;
190
252
  readonly tx: Tx;
191
- readonly runId: RunId;
253
+ readonly runId: RunId$1;
192
254
  readonly stepId: StepId;
193
255
  readonly attempt: AttemptNumber;
194
256
  readonly signal: AbortSignal;
@@ -197,6 +259,7 @@ interface StepCtx<Input = unknown> {
197
259
  once<T extends Json>(scope: string, fn: () => Promise<T>): Promise<T>;
198
260
  idempotencyKey(scope: string): string;
199
261
  }
262
+ type ActivityCtx<Input = unknown> = Omit<StepCtx<Input>, "tx">;
200
263
  type StreamEvent<C = Json> = {
201
264
  readonly kind: "chunk";
202
265
  readonly chunk: C;
@@ -245,6 +308,14 @@ interface TaskConfig<Input, N extends NeedsMap, Output> extends StepConfigBase<I
245
308
  readonly ctx: StepCtx<NoInfer<Input>>;
246
309
  }) => Promise<Output>;
247
310
  }
311
+ interface ActivityConfig<Input, N extends NeedsMap, Output> extends StepConfigBase<Input, N>, StepLifecycleHooks<Output> {
312
+ readonly retry?: RetryPolicy;
313
+ readonly run: (args: {
314
+ readonly input: NoInfer<Input>;
315
+ readonly needs: NoInfer<ResolvedNeeds<N>>;
316
+ readonly ctx: ActivityCtx<NoInfer<Input>>;
317
+ }) => Promise<Output>;
318
+ }
248
319
  interface StreamingTaskConfig<Input, N extends NeedsMap, Output, Chunk> extends StepConfigBase<Input, N>, StepLifecycleHooks<Output> {
249
320
  readonly retry?: RetryPolicy;
250
321
  readonly run: (args: {
@@ -297,11 +368,12 @@ interface SubflowConfig<Input, N extends NeedsMap, Child extends Flow> extends S
297
368
  }) => FlowInput<Child>;
298
369
  }
299
370
  interface SubflowStepOutput<ChildOutput> {
300
- readonly childRunId: RunId;
371
+ readonly childRunId: RunId$1;
301
372
  readonly output: ChildOutput;
302
373
  }
303
374
  interface Builder<Input = unknown> {
304
375
  task<N extends NeedsMap, Output>(config: TaskConfig<Input, N, Output>): Step<Output>;
376
+ activity<N extends NeedsMap, Output>(config: ActivityConfig<Input, N, Output>): Step<Output>;
305
377
  streamingTask<N extends NeedsMap, O, C = Json>(config: StreamingTaskConfig<Input, N, O, C>): Step<O>;
306
378
  signal<N extends NeedsMap, S extends StandardSchemaV1>(config: SignalConfig<Input, N, S>): Step<InferSchemaOutput<S>>;
307
379
  subflow<N extends NeedsMap, Child extends Flow>(child: Child, config: SubflowConfig<Input, N, Child>): Step<SubflowStepOutput<FlowOutput<Child>>>;
@@ -348,14 +420,14 @@ type FlowInput<F> = F extends Flow<string, infer S, StepMap, unknown> ? InferSch
348
420
  type FlowOutput<F> = F extends Flow<string, StandardSchemaV1, StepMap, infer O> ? O : never;
349
421
  type FlowIdOf<T extends ReadonlyArray<Flow>> = T[number] extends Flow<infer Id> ? Id : never;
350
422
  interface ParentLink {
351
- readonly runId: RunId;
423
+ readonly runId: RunId$1;
352
424
  readonly stepId: StepId;
353
425
  }
354
426
  interface ParentRef extends ParentLink {
355
427
  readonly attempt: AttemptNumber;
356
428
  }
357
429
  interface FlowEvent {
358
- readonly runId: RunId;
430
+ readonly runId: RunId$1;
359
431
  readonly flowId: string;
360
432
  readonly at: Date;
361
433
  }
@@ -444,10 +516,10 @@ type SettleSignalResult = {
444
516
  readonly tag: "noop";
445
517
  };
446
518
  interface Store {
447
- appendFact(runId: RunId, fact: Fact): Promise<void>;
448
- loadRunState(runId: RunId): Promise<RunState>;
519
+ appendFact(runId: RunId$1, fact: Fact): Promise<void>;
520
+ loadRunState(runId: RunId$1): Promise<RunState>;
449
521
  settleSignal(args: {
450
- readonly runId: RunId;
522
+ readonly runId: RunId$1;
451
523
  readonly stepId: StepId;
452
524
  readonly at: Date;
453
525
  readonly incoming?: {
@@ -455,21 +527,37 @@ interface Store {
455
527
  readonly signalName?: string;
456
528
  };
457
529
  }): Promise<SettleSignalResult>;
458
- tryStartRun(runId: RunId, fact: FlowStartedFact, concurrency?: {
530
+ tryStartRun(runId: RunId$1, fact: FlowStartedFact, concurrency?: {
531
+ readonly key: string;
532
+ readonly mode: ConcurrencyMode;
533
+ }): Promise<{
534
+ readonly started: boolean;
535
+ readonly canceled: ReadonlyArray<{
536
+ readonly runId: RunId$1;
537
+ readonly fact: FlowCanceledByConcurrencyFact;
538
+ }>;
539
+ }>;
540
+ tryStartRunOnTx(tx: Tx, runId: RunId$1, fact: FlowStartedFact, concurrency?: {
459
541
  readonly key: string;
460
542
  readonly mode: ConcurrencyMode;
461
543
  }): Promise<{
462
544
  readonly started: boolean;
463
545
  readonly canceled: ReadonlyArray<{
464
- readonly runId: RunId;
546
+ readonly runId: RunId$1;
465
547
  readonly fact: FlowCanceledByConcurrencyFact;
466
548
  }>;
467
549
  }>;
468
- claimStep(runId: RunId, stepId: StepId, attempt: AttemptNumber): Promise<ClaimToken | null>;
469
- settleStep(runId: RunId, stepId: StepId, fact: StepCompletedFact | StepFailedFact): Promise<void>;
470
- recordOnce(runId: RunId, stepId: StepId, scope: string, value: Json): Promise<void>;
471
- getOnce(runId: RunId, stepId: StepId, scope: string): Promise<Json | null>;
472
- runStep<T extends Json>(runId: RunId, stepId: StepId, attempt: AttemptNumber, body: (tx: Tx) => Promise<{
550
+ claimStep(runId: RunId$1, stepId: StepId, attempt: AttemptNumber): Promise<ClaimToken | null>;
551
+ extendLease(runId: RunId$1, stepId: StepId, attempt: AttemptNumber, leaseMs: Millis): Promise<void>;
552
+ sweepLeases(args: {
553
+ readonly now: Date;
554
+ readonly queue: Queue;
555
+ readonly limit?: number;
556
+ }): Promise<readonly ReapedLease[]>;
557
+ settleStep(runId: RunId$1, stepId: StepId, fact: StepCompletedFact | StepFailedFact): Promise<void>;
558
+ recordOnce(runId: RunId$1, stepId: StepId, scope: string, value: Json): Promise<void>;
559
+ getOnce(runId: RunId$1, stepId: StepId, scope: string): Promise<Json | null>;
560
+ runStep<T extends Json>(runId: RunId$1, stepId: StepId, attempt: AttemptNumber, body: (tx: Tx) => Promise<{
473
561
  readonly output: T;
474
562
  readonly fact: StepCompletedFact | StepFailedFact | StepCanceledFact;
475
563
  }>): Promise<T>;
@@ -486,14 +574,15 @@ interface Store {
486
574
  } | null>;
487
575
  appendGlobalFact(fact: GlobalFact): Promise<void>;
488
576
  queryRuns(opts: QueryRunsOpts): Promise<QueryRunsResult>;
489
- listChildren(parentRunId: RunId): Promise<ReadonlyArray<RunId>>;
577
+ describe(runId: RunId$1): Promise<RunDescription>;
578
+ listChildren(parentRunId: RunId$1): Promise<ReadonlyArray<RunId$1>>;
490
579
  pruneFacts(opts: Required<PruneOpts>): Promise<PruneResult>;
491
580
  }
492
581
  interface StreamTransport {
493
- subscribeStream(runId: RunId, stepId: StepId, opts?: {
582
+ subscribeStream(runId: RunId$1, stepId: StepId, opts?: {
494
583
  readonly replayBuffered?: boolean;
495
584
  }): AsyncIterable<StreamEvent<Json>>;
496
- publishChunk(runId: RunId, stepId: StepId, chunk: Json): void;
585
+ publishChunk(runId: RunId$1, stepId: StepId, chunk: Json): void;
497
586
  }
498
587
  interface QueryRunsWhere<FlowId extends string = string> {
499
588
  readonly flowId?: FlowId;
@@ -512,7 +601,7 @@ type QueryRunsOpts<FlowId extends string = string> = {
512
601
  readonly cursor?: string;
513
602
  };
514
603
  interface RunSummary<FlowId extends string = string> {
515
- readonly runId: RunId;
604
+ readonly runId: RunId$1;
516
605
  readonly flowId: FlowId;
517
606
  readonly status: RunStatus;
518
607
  readonly startedAt: Date;
@@ -536,7 +625,7 @@ interface PruneResult {
536
625
  }
537
626
  interface QueueMessage {
538
627
  readonly receipt: string;
539
- readonly runId: RunId;
628
+ readonly runId: RunId$1;
540
629
  readonly stepId: StepId;
541
630
  readonly payload: Json;
542
631
  readonly attempt: AttemptNumber;
@@ -550,7 +639,7 @@ interface QueueEnqueueOpts {
550
639
  readonly payload?: Json;
551
640
  }
552
641
  interface Queue {
553
- enqueue(runId: RunId, stepId: StepId, opts?: QueueEnqueueOpts): Promise<void>;
642
+ enqueue(runId: RunId$1, stepId: StepId, opts?: QueueEnqueueOpts): Promise<void>;
554
643
  dequeue(opts: QueueDequeueOpts): Promise<readonly QueueMessage[]>;
555
644
  ack(receipt: string): Promise<void>;
556
645
  nack(receipt: string, opts?: {
@@ -562,14 +651,14 @@ interface Queue {
562
651
  interface Clock {
563
652
  now(): Date;
564
653
  sleep(ms: Millis, signal?: AbortSignal): Promise<void>;
565
- schedule(at: Date, runId: RunId, stepId: StepId): Promise<void>;
654
+ schedule(at: Date, runId: RunId$1, stepId: StepId): Promise<void>;
566
655
  }
567
656
  interface Trigger {
568
- subscribe(handler: (runId: RunId) => void): () => void;
657
+ subscribe(handler: (runId: RunId$1) => void): () => void;
569
658
  }
570
- type FactKind = "flow.started" | "flow.completed" | "flow.failed" | "flow.canceled" | "step.started" | "step.completed" | "step.failed" | "step.canceled" | "step.retried" | "step.skipped" | "step.reset" | "step.abort-requested" | "signal.sent" | "signal.received" | "signal.buffered" | "once.recorded" | "match.arm-selected";
659
+ type FactKind = "flow.started" | "flow.completed" | "flow.failed" | "flow.canceled" | "step.started" | "step.completed" | "step.failed" | "step.canceled" | "step.retried" | "step.skipped" | "step.reset" | "step.abort-requested" | "signal.sent" | "signal.received" | "signal.buffered" | "once.recorded" | "match.arm-selected" | "lease.reaped";
571
660
  interface FactBase {
572
- readonly runId: RunId;
661
+ readonly runId: RunId$1;
573
662
  readonly at: Date;
574
663
  }
575
664
  interface FlowStartedFact extends FactBase {
@@ -599,7 +688,7 @@ interface FlowFailedFact extends FactBase {
599
688
  interface FlowCanceledByConcurrencyFact extends FactBase {
600
689
  readonly kind: "flow.canceled";
601
690
  readonly cause: "concurrency";
602
- readonly canceledByRunId: RunId;
691
+ readonly canceledByRunId: RunId$1;
603
692
  readonly concurrencyKey: string;
604
693
  }
605
694
  interface FlowCanceledExplicitlyFact extends FactBase {
@@ -684,6 +773,13 @@ interface MatchArmSelectedFact extends FactBase {
684
773
  readonly stepId: StepId;
685
774
  readonly arm: string;
686
775
  }
776
+ interface LeaseReapedFact extends FactBase {
777
+ readonly kind: "lease.reaped";
778
+ readonly stepId: StepId;
779
+ readonly attempt: AttemptNumber;
780
+ readonly reapedAt: Date;
781
+ readonly reason: "expired";
782
+ }
687
783
  interface StepResetFact extends FactBase {
688
784
  readonly kind: "step.reset";
689
785
  readonly stepId: StepId;
@@ -698,7 +794,7 @@ interface StepAbortRequestedFact extends FactBase {
698
794
  readonly actor: string;
699
795
  readonly note?: string;
700
796
  }
701
- type Fact = FlowStartedFact | FlowCompletedFact | FlowFailedFact | FlowCanceledFact | StepStartedFact | StepCompletedFact | StepFailedFact | StepCanceledFact | StepRetriedFact | StepSkippedFact | StepResetFact | StepAbortRequestedFact | SignalSentFact | SignalReceivedFact | SignalBufferedFact | OnceRecordedFact | MatchArmSelectedFact;
797
+ type Fact = FlowStartedFact | FlowCompletedFact | FlowFailedFact | FlowCanceledFact | StepStartedFact | StepCompletedFact | StepFailedFact | StepCanceledFact | StepRetriedFact | StepSkippedFact | StepResetFact | StepAbortRequestedFact | SignalSentFact | SignalReceivedFact | SignalBufferedFact | OnceRecordedFact | MatchArmSelectedFact | LeaseReapedFact;
702
798
  type RunStatus = "pending" | "running" | "completed" | "failed" | "canceled";
703
799
  type StepStatus = "pending" | "running" | "completed" | "failed" | "canceled" | "skipped";
704
800
 
@@ -714,9 +810,9 @@ interface OperatorAuditOpts {
714
810
  readonly note?: string;
715
811
  }
716
812
  interface Operator {
717
- skip(runId: RunId, stepId: StepId, opts: OperatorAuditOpts): Promise<void>;
718
- retry(runId: RunId, stepId: StepId, opts: OperatorAuditOpts): Promise<void>;
719
- abort(runId: RunId, opts: OperatorAuditOpts): Promise<void>;
813
+ skip(runId: RunId$1, stepId: StepId, opts: OperatorAuditOpts): Promise<void>;
814
+ retry(runId: RunId$1, stepId: StepId, opts: OperatorAuditOpts): Promise<void>;
815
+ abort(runId: RunId$1, opts: OperatorAuditOpts): Promise<void>;
720
816
  }
721
817
 
722
818
  declare function flow<const Id extends string, InputSchema extends StandardSchemaV1, R extends StepMap, Output = unknown>(config: FlowConfig<Id, InputSchema, R, Output>): Flow<Id, InputSchema, R, Output>;
@@ -788,21 +884,43 @@ declare class NagiRuntimeError extends Error {
788
884
  constructor(message: string);
789
885
  }
790
886
  declare class NagiCanceledError extends Error {
791
- readonly runId: RunId;
792
- readonly canceledByRunId: RunId;
887
+ readonly runId: RunId$1;
888
+ readonly canceledByRunId: RunId$1;
793
889
  readonly concurrencyKey: string;
794
890
  constructor({ runId, canceledByRunId, concurrencyKey, }: {
795
- readonly runId: RunId;
796
- readonly canceledByRunId: RunId;
891
+ readonly runId: RunId$1;
892
+ readonly canceledByRunId: RunId$1;
893
+ readonly concurrencyKey: string;
894
+ });
895
+ }
896
+ declare class NagiFlowSnapshotGoneError extends Error {
897
+ readonly runId: RunId$1;
898
+ readonly flowId: string;
899
+ readonly pinnedHash: string;
900
+ readonly currentHash: string | null;
901
+ constructor({ runId, flowId, pinnedHash, currentHash, }: {
902
+ readonly runId: RunId$1;
903
+ readonly flowId: string;
904
+ readonly pinnedHash: string;
905
+ readonly currentHash: string | null;
906
+ });
907
+ }
908
+ declare class NagiConcurrencyConflictError extends Error {
909
+ readonly runId: RunId$1;
910
+ readonly flowId: string;
911
+ readonly concurrencyKey: string;
912
+ constructor({ runId, flowId, concurrencyKey, }: {
913
+ readonly runId: RunId$1;
914
+ readonly flowId: string;
797
915
  readonly concurrencyKey: string;
798
916
  });
799
917
  }
800
918
  declare class NagiSnapshotDriftError extends Error {
801
- readonly runId: RunId;
919
+ readonly runId: RunId$1;
802
920
  readonly expected: string;
803
921
  readonly actual: string;
804
922
  constructor({ runId, expected, actual, }: {
805
- readonly runId: RunId;
923
+ readonly runId: RunId$1;
806
924
  readonly expected: string;
807
925
  readonly actual: string;
808
926
  });
@@ -825,23 +943,39 @@ declare class InMemoryStore implements Store, StreamTransport {
825
943
  private readonly streamHub;
826
944
  private readonly leaseMs;
827
945
  constructor(opts?: InMemoryStoreOpts);
828
- appendFact(runId: RunId, fact: Fact): Promise<void>;
829
- tryStartRun(runId: RunId, fact: FlowStartedFact, concurrency?: {
946
+ appendFact(runId: RunId$1, fact: Fact): Promise<void>;
947
+ tryStartRun(runId: RunId$1, fact: FlowStartedFact, concurrency?: {
948
+ readonly key: string;
949
+ readonly mode: ConcurrencyMode;
950
+ }): Promise<{
951
+ readonly started: boolean;
952
+ readonly canceled: ReadonlyArray<{
953
+ readonly runId: RunId$1;
954
+ readonly fact: FlowCanceledByConcurrencyFact;
955
+ }>;
956
+ }>;
957
+ tryStartRunOnTx(_tx: Tx, runId: RunId$1, fact: FlowStartedFact, concurrency?: {
830
958
  readonly key: string;
831
959
  readonly mode: ConcurrencyMode;
832
960
  }): Promise<{
833
961
  readonly started: boolean;
834
962
  readonly canceled: ReadonlyArray<{
835
- readonly runId: RunId;
963
+ readonly runId: RunId$1;
836
964
  readonly fact: FlowCanceledByConcurrencyFact;
837
965
  }>;
838
966
  }>;
839
- listChildren(parentRunId: RunId): Promise<ReadonlyArray<RunId>>;
840
- loadRunState(runId: RunId): Promise<RunState>;
841
- claimStep(runId: RunId, stepId: StepId, attempt: AttemptNumber): Promise<ClaimToken | null>;
842
- settleStep(runId: RunId, _stepId: StepId, fact: StepCompletedFact | StepFailedFact): Promise<void>;
967
+ listChildren(parentRunId: RunId$1): Promise<ReadonlyArray<RunId$1>>;
968
+ loadRunState(runId: RunId$1): Promise<RunState>;
969
+ claimStep(runId: RunId$1, stepId: StepId, attempt: AttemptNumber): Promise<ClaimToken | null>;
970
+ extendLease(runId: RunId$1, stepId: StepId, attempt: AttemptNumber, leaseMs: Millis): Promise<void>;
971
+ sweepLeases(args: {
972
+ readonly now: Date;
973
+ readonly queue: Queue;
974
+ readonly limit?: number;
975
+ }): Promise<readonly ReapedLease[]>;
976
+ settleStep(runId: RunId$1, _stepId: StepId, fact: StepCompletedFact | StepFailedFact): Promise<void>;
843
977
  settleSignal(args: {
844
- readonly runId: RunId;
978
+ readonly runId: RunId$1;
845
979
  readonly stepId: StepId;
846
980
  readonly at: Date;
847
981
  readonly incoming?: {
@@ -849,9 +983,9 @@ declare class InMemoryStore implements Store, StreamTransport {
849
983
  readonly signalName?: string;
850
984
  };
851
985
  }): Promise<SettleSignalResult>;
852
- recordOnce(runId: RunId, stepId: StepId, scope: string, value: Json): Promise<void>;
853
- getOnce(runId: RunId, stepId: StepId, scope: string): Promise<Json | null>;
854
- runStep<T extends Json>(runId: RunId, _stepId: StepId, _attempt: AttemptNumber, body: (tx: Tx) => Promise<{
986
+ recordOnce(runId: RunId$1, stepId: StepId, scope: string, value: Json): Promise<void>;
987
+ getOnce(runId: RunId$1, stepId: StepId, scope: string): Promise<Json | null>;
988
+ runStep<T extends Json>(runId: RunId$1, _stepId: StepId, _attempt: AttemptNumber, body: (tx: Tx) => Promise<{
855
989
  readonly output: T;
856
990
  readonly fact: StepCompletedFact | StepFailedFact | StepCanceledFact;
857
991
  }>): Promise<T>;
@@ -869,11 +1003,12 @@ declare class InMemoryStore implements Store, StreamTransport {
869
1003
  appendGlobalFact(fact: GlobalFact): Promise<void>;
870
1004
  readGlobalFacts(): ReadonlyArray<GlobalFact>;
871
1005
  queryRuns(opts: QueryRunsOpts): Promise<QueryRunsResult>;
1006
+ describe(runId: RunId$1): Promise<RunDescription>;
872
1007
  pruneFacts(opts: Required<PruneOpts>): Promise<PruneResult>;
873
- subscribeStream(runId: RunId, stepId: StepId, opts?: {
1008
+ subscribeStream(runId: RunId$1, stepId: StepId, opts?: {
874
1009
  readonly replayBuffered?: boolean;
875
1010
  }): AsyncIterable<StreamEvent<Json>>;
876
- publishChunk(runId: RunId, stepId: StepId, chunk: Json): void;
1011
+ publishChunk(runId: RunId$1, stepId: StepId, chunk: Json): void;
877
1012
  }
878
1013
 
879
1014
  interface InMemoryQueueOpts {
@@ -884,7 +1019,7 @@ declare class InMemoryQueue implements Queue {
884
1019
  private readonly leased;
885
1020
  private readonly leaseMs;
886
1021
  constructor(opts?: InMemoryQueueOpts);
887
- enqueue(runId: RunId, stepId: StepId, opts?: QueueEnqueueOpts): Promise<void>;
1022
+ enqueue(runId: RunId$1, stepId: StepId, opts?: QueueEnqueueOpts): Promise<void>;
888
1023
  dequeue(opts: QueueDequeueOpts): Promise<readonly QueueMessage[]>;
889
1024
  ack(receipt: string): Promise<void>;
890
1025
  nack(receipt: string, opts?: {
@@ -901,15 +1036,21 @@ declare class InMemoryClock implements Clock {
901
1036
  constructor(opts?: InMemoryClockOpts);
902
1037
  now(): Date;
903
1038
  sleep(ms: Millis, signal?: AbortSignal): Promise<void>;
904
- schedule(at: Date, runId: RunId, stepId: StepId): Promise<void>;
1039
+ schedule(at: Date, runId: RunId$1, stepId: StepId): Promise<void>;
905
1040
  dispose(): void;
906
1041
  }
907
1042
  declare class InMemoryTrigger implements Trigger {
908
1043
  private handlers;
909
- subscribe(handler: (runId: RunId) => void): () => void;
910
- fire(runId: RunId): void;
1044
+ subscribe(handler: (runId: RunId$1) => void): () => void;
1045
+ fire(runId: RunId$1): void;
911
1046
  }
912
1047
 
1048
+ type RunId = RunId$1;
1049
+ declare const RunId: {
1050
+ readonly parse: (s: string) => RunId;
1051
+ readonly fromTrusted: (s: string) => RunId;
1052
+ };
1053
+
913
1054
  interface NagiConfig {
914
1055
  readonly flows: ReadonlyArray<Flow>;
915
1056
  readonly store: Store;
@@ -921,22 +1062,41 @@ interface NagiConfig {
921
1062
  readonly onLog?: (entry: LogEntry) => void;
922
1063
  readonly defaultRetry?: RetryPolicy;
923
1064
  readonly codeVersion?: string;
1065
+ readonly heartbeatIntervalMs?: Millis;
1066
+ readonly heartbeatLeaseMs?: Millis;
1067
+ readonly reaperIntervalMs?: Millis;
924
1068
  }
925
1069
  interface StartOpts {
926
- readonly runId?: RunId;
1070
+ readonly runId?: RunId$1;
1071
+ }
1072
+ interface StartStagedOpts {
1073
+ readonly tx: Tx;
1074
+ readonly runId?: RunId$1;
1075
+ }
1076
+ interface StartStagedResult {
1077
+ readonly started: boolean;
1078
+ readonly canceled: readonly RunId$1[];
1079
+ readonly applyOnCommit: () => Promise<void>;
927
1080
  }
928
1081
  interface CancelOpts {
929
1082
  readonly reason?: string;
930
1083
  }
931
1084
  interface Wf<TFlows extends ReadonlyArray<Flow> = ReadonlyArray<Flow>> {
932
- start<F extends TFlows[number]>(flow: F, input: FlowInput<F>, opts?: StartOpts): Promise<RunId>;
933
- startById(flowId: string, input: unknown, opts?: StartOpts): Promise<RunId>;
934
- signal(runId: RunId, stepName: string, payload: unknown): Promise<void>;
935
- cancel(runId: RunId, opts?: CancelOpts): Promise<void>;
1085
+ start<F extends TFlows[number]>(flow: F, input: FlowInput<F>, opts?: StartOpts): Promise<RunId$1>;
1086
+ startById(flowId: string, input: unknown, opts?: StartOpts): Promise<RunId$1>;
1087
+ startStaged<F extends TFlows[number]>(flow: F, input: FlowInput<F>, opts: StartStagedOpts): Promise<StartStagedResult & {
1088
+ readonly runId: RunId$1;
1089
+ }>;
1090
+ startStagedById(flowId: string, input: unknown, opts: StartStagedOpts): Promise<StartStagedResult & {
1091
+ readonly runId: RunId$1;
1092
+ }>;
1093
+ signal(runId: RunId$1, stepName: string, payload: unknown): Promise<void>;
1094
+ cancel(runId: RunId$1, opts?: CancelOpts): Promise<void>;
936
1095
  worker(config?: WorkerConfig): Worker;
937
- replay(runId: RunId, opts?: ReplayOpts): Promise<void>;
1096
+ replay(runId: RunId$1, opts?: ReplayOpts): Promise<void>;
938
1097
  queryRuns(opts?: QueryRunsOpts<FlowIdOf<TFlows>>): Promise<QueryRunsResult<FlowIdOf<TFlows>>>;
939
- subscribe<C = Json>(runId: RunId, stepId: StepId, opts?: {
1098
+ describe(runId: RunId$1): Promise<RunDescription>;
1099
+ subscribe<C = Json>(runId: RunId$1, stepId: StepId, opts?: {
940
1100
  readonly replayBuffered?: boolean;
941
1101
  }): AsyncIterable<StreamEvent<C>>;
942
1102
  operator(): Operator;
@@ -984,4 +1144,10 @@ declare function decideSignal(args: {
984
1144
  } | undefined;
985
1145
  }): SignalDecision;
986
1146
 
987
- export { type Anomaly, type AttemptNumber, type BackoffStrategy, type Builder, type CancelArgs, type CanonicalDag, type CanonicalMatchArm, type CanonicalRetryPolicy, type CanonicalSchema, type CanonicalStep, type ClaimToken, type Clock, type ConcurrencyMode, type Fact, type FactKind, type Flow, type FlowCanceledByConcurrencyFact, type FlowCanceledByOperatorFact, type FlowCanceledExplicitlyFact, type FlowCanceledFact, type FlowCompleteEvent, type FlowCompletedFact, type FlowConcurrency, type FlowConfig, type FlowErrorEvent, type FlowEvent, type FlowFailedFact, type FlowHooks, type FlowIdOf, type FlowInput, type FlowOutput, type FlowRefUpdatedFact, type FlowStartEvent, type FlowStartedFact, type GlobalFact, InMemoryClock, InMemoryQueue, InMemoryStore, InMemoryTrigger, type InferSchemaInput, type InferSchemaOutput, type Json, type LogEntry, type LogLevel, type Logger, type MatchArm, type MatchArmGuard, type MatchArmOtherwise, type MatchArmOutput, type MatchArmSelectedFact, type MatchArmShape, type MatchGuardConfig, type Millis, NagiCanceledError, type NagiConfig, type NagiRunConfig, NagiRuntimeError, NagiSnapshotDriftError, NagiValidationError, type NeedRef, type NeedsMap, type NeedsOutputs, type OnceRecordedFact, type Operator, type OperatorAuditOpts, type Optional, type ParentLink, type ParentRef, type PrunableStatus, type PruneOpts, type PruneResult, type QueryRunsOpts, type QueryRunsResult, type QueryRunsWhere, type Queue, type QueueDequeueOpts, type QueueEnqueueOpts, type QueueMessage, type Register, type ReplayMode, type ReplayOpts, type Resolved, type ResolvedConcurrency, type ResolvedNeeds, type RetryPolicy, type RunCancelCause, type RunId, type RunPhase, type RunState, type RunStatus, type RunSummary, type RuntimeHandle, type SerializedError, type SettleSignalResult, type SignalBufferedFact, type SignalConfig, type SignalDecision, type SignalReceivedEvent, type SignalReceivedFact, type SignalSentEvent, type SignalSentFact, type SkipReason, type SnapshotChangedEdge, type SnapshotChangedField, type SnapshotDiff, StandardSchemaV1, type StartOpts, type Step, type StepAbortRequestedFact, type StepCancelCause, type StepCanceledFact, type StepCompleteEvent, type StepCompletedFact, type StepCtx, type StepErrorEvent, type StepEvent, type StepFailedFact, type StepId, type StepKind, type StepMap, type StepOutput, type StepResetFact, type StepRetriedFact, type StepRetryEvent, type StepSkippedFact, type StepStartEvent, type StepStartedFact, type StepState, type StepStatus, type Store, type StreamEvent, type StreamTransport, type StreamingStepCtx, type StreamingTaskConfig, type StringKeyOf, type SubflowConfig, type SubflowStepOutput, type TaskConfig, type Trigger, type Tx, type Wf, type Worker, type WorkerConfig, type WorkerRunOnceOpts, type WorkerRunResult, type WorkerRunUntilEmptyOpts, attemptOf, canonicalize, decideSignal, diffSnapshots, errorOf, fingerprintFlows, flow, isStepTerminal, isTerminalRun, nagi, optional, outputOf, foldRun as projectRunState, resolvedOf, runStatusOf, sha256Canonical, stepStateOf, stepStatusOf, unwrap };
1147
+ declare class NagiAbortError extends Error {
1148
+ readonly runId: RunId$1;
1149
+ readonly kind: "run" | "step";
1150
+ constructor(runId: RunId$1, kind: "run" | "step");
1151
+ }
1152
+
1153
+ export { type Anomaly, type AttemptNumber, type BackoffStrategy, type Builder, type CancelArgs, type CanonicalDag, type CanonicalMatchArm, type CanonicalRetryPolicy, type CanonicalSchema, type CanonicalStep, type ClaimToken, type Clock, type ConcurrencyMode, DEFAULT_REAPER_INTERVAL_MS, type Fact, type FactKind, type Flow, type FlowCanceledByConcurrencyFact, type FlowCanceledByOperatorFact, type FlowCanceledExplicitlyFact, type FlowCanceledFact, type FlowCompleteEvent, type FlowCompletedFact, type FlowConcurrency, type FlowConfig, type FlowErrorEvent, type FlowEvent, type FlowFailedFact, type FlowHooks, type FlowIdOf, type FlowInput, type FlowOutput, type FlowRefUpdatedFact, type FlowStartEvent, type FlowStartedFact, type GlobalFact, InMemoryClock, InMemoryQueue, InMemoryStore, InMemoryTrigger, type InferSchemaInput, type InferSchemaOutput, type Json, type LeaseReapedFact, type LogEntry, type LogLevel, type Logger, type MatchArm, type MatchArmGuard, type MatchArmOtherwise, type MatchArmOutput, type MatchArmSelectedFact, type MatchArmShape, type MatchGuardConfig, type Millis, NagiAbortError, NagiCanceledError, NagiConcurrencyConflictError, type NagiConfig, NagiFlowSnapshotGoneError, type NagiRunConfig, NagiRuntimeError, NagiSnapshotDriftError, NagiValidationError, type NeedRef, type NeedsMap, type NeedsOutputs, type OnceRecordedFact, type Operator, type OperatorAuditOpts, type Optional, type ParentLink, type ParentRef, type PrunableStatus, type PruneOpts, type PruneResult, type QueryRunsOpts, type QueryRunsResult, type QueryRunsWhere, type Queue, type QueueDequeueOpts, type QueueEnqueueOpts, type QueueMessage, type ReapedLease, type ReaperDecision, type Register, type ReplayMode, type ReplayOpts, type Resolved, type ResolvedConcurrency, type ResolvedNeeds, type RetryPolicy, type RunCancelCause, type RunDescription, RunId, type RunPhase, type RunState, type RunStatus, type RunSummary, type RunView, type RuntimeHandle, type SerializedError, type SettleSignalResult, type SignalBufferedFact, type SignalConfig, type SignalDecision, type SignalReceivedEvent, type SignalReceivedFact, type SignalSentEvent, type SignalSentFact, type SkipReason, type SnapshotChangedEdge, type SnapshotChangedField, type SnapshotDiff, StandardSchemaV1, type StartOpts, type StartStagedOpts, type StartStagedResult, type Step, type StepAbortRequestedFact, type StepCancelCause, type StepCanceledFact, type StepCompleteEvent, type StepCompletedFact, type StepCtx, type StepErrorEvent, type StepEvent, type StepFailedFact, type StepId, type StepKind, type StepMap, type StepOutput, type StepResetFact, type StepRetriedFact, type StepRetryEvent, type StepRunStatus, type StepSkippedFact, type StepStartEvent, type StepStartedFact, type StepState, type StepStatus, type StepView, type Store, type StreamEvent, type StreamTransport, type StreamingStepCtx, type StreamingTaskConfig, type StringKeyOf, type SubflowConfig, type SubflowStepOutput, type TaskConfig, type Trigger, type Tx, type Wf, type Worker, type WorkerConfig, type WorkerRunOnceOpts, type WorkerRunResult, type WorkerRunUntilEmptyOpts, attemptOf, canonicalize, decideExpiredLeaseAction, decideSignal, diffSnapshots, errorOf, fingerprintFlows, flow, isStepTerminal, isTerminalRun, nagi, optional, outputOf, foldRun as projectRunState, resolvedOf, runStatusOf, sha256Canonical, stepStateOf, stepStatusOf, unwrap };