@nagi-js/core 0.1.1-rc.13 → 0.1.1-rc.15

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;
@@ -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;
@@ -306,7 +368,7 @@ interface SubflowConfig<Input, N extends NeedsMap, Child extends Flow> extends S
306
368
  }) => FlowInput<Child>;
307
369
  }
308
370
  interface SubflowStepOutput<ChildOutput> {
309
- readonly childRunId: RunId;
371
+ readonly childRunId: RunId$1;
310
372
  readonly output: ChildOutput;
311
373
  }
312
374
  interface Builder<Input = unknown> {
@@ -358,14 +420,14 @@ type FlowInput<F> = F extends Flow<string, infer S, StepMap, unknown> ? InferSch
358
420
  type FlowOutput<F> = F extends Flow<string, StandardSchemaV1, StepMap, infer O> ? O : never;
359
421
  type FlowIdOf<T extends ReadonlyArray<Flow>> = T[number] extends Flow<infer Id> ? Id : never;
360
422
  interface ParentLink {
361
- readonly runId: RunId;
423
+ readonly runId: RunId$1;
362
424
  readonly stepId: StepId;
363
425
  }
364
426
  interface ParentRef extends ParentLink {
365
427
  readonly attempt: AttemptNumber;
366
428
  }
367
429
  interface FlowEvent {
368
- readonly runId: RunId;
430
+ readonly runId: RunId$1;
369
431
  readonly flowId: string;
370
432
  readonly at: Date;
371
433
  }
@@ -454,10 +516,10 @@ type SettleSignalResult = {
454
516
  readonly tag: "noop";
455
517
  };
456
518
  interface Store {
457
- appendFact(runId: RunId, fact: Fact): Promise<void>;
458
- loadRunState(runId: RunId): Promise<RunState>;
519
+ appendFact(runId: RunId$1, fact: Fact): Promise<void>;
520
+ loadRunState(runId: RunId$1): Promise<RunState>;
459
521
  settleSignal(args: {
460
- readonly runId: RunId;
522
+ readonly runId: RunId$1;
461
523
  readonly stepId: StepId;
462
524
  readonly at: Date;
463
525
  readonly incoming?: {
@@ -465,21 +527,37 @@ interface Store {
465
527
  readonly signalName?: string;
466
528
  };
467
529
  }): Promise<SettleSignalResult>;
468
- tryStartRun(runId: RunId, fact: FlowStartedFact, concurrency?: {
530
+ tryStartRun(runId: RunId$1, fact: FlowStartedFact, concurrency?: {
469
531
  readonly key: string;
470
532
  readonly mode: ConcurrencyMode;
471
533
  }): Promise<{
472
534
  readonly started: boolean;
473
535
  readonly canceled: ReadonlyArray<{
474
- readonly runId: RunId;
536
+ readonly runId: RunId$1;
475
537
  readonly fact: FlowCanceledByConcurrencyFact;
476
538
  }>;
477
539
  }>;
478
- claimStep(runId: RunId, stepId: StepId, attempt: AttemptNumber): Promise<ClaimToken | null>;
479
- settleStep(runId: RunId, stepId: StepId, fact: StepCompletedFact | StepFailedFact): Promise<void>;
480
- recordOnce(runId: RunId, stepId: StepId, scope: string, value: Json): Promise<void>;
481
- getOnce(runId: RunId, stepId: StepId, scope: string): Promise<Json | null>;
482
- runStep<T extends Json>(runId: RunId, stepId: StepId, attempt: AttemptNumber, body: (tx: Tx) => Promise<{
540
+ tryStartRunOnTx(tx: Tx, runId: RunId$1, fact: FlowStartedFact, concurrency?: {
541
+ readonly key: string;
542
+ readonly mode: ConcurrencyMode;
543
+ }): Promise<{
544
+ readonly started: boolean;
545
+ readonly canceled: ReadonlyArray<{
546
+ readonly runId: RunId$1;
547
+ readonly fact: FlowCanceledByConcurrencyFact;
548
+ }>;
549
+ }>;
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<{
483
561
  readonly output: T;
484
562
  readonly fact: StepCompletedFact | StepFailedFact | StepCanceledFact;
485
563
  }>): Promise<T>;
@@ -496,14 +574,15 @@ interface Store {
496
574
  } | null>;
497
575
  appendGlobalFact(fact: GlobalFact): Promise<void>;
498
576
  queryRuns(opts: QueryRunsOpts): Promise<QueryRunsResult>;
499
- listChildren(parentRunId: RunId): Promise<ReadonlyArray<RunId>>;
577
+ describe(runId: RunId$1): Promise<RunDescription>;
578
+ listChildren(parentRunId: RunId$1): Promise<ReadonlyArray<RunId$1>>;
500
579
  pruneFacts(opts: Required<PruneOpts>): Promise<PruneResult>;
501
580
  }
502
581
  interface StreamTransport {
503
- subscribeStream(runId: RunId, stepId: StepId, opts?: {
582
+ subscribeStream(runId: RunId$1, stepId: StepId, opts?: {
504
583
  readonly replayBuffered?: boolean;
505
584
  }): AsyncIterable<StreamEvent<Json>>;
506
- publishChunk(runId: RunId, stepId: StepId, chunk: Json): void;
585
+ publishChunk(runId: RunId$1, stepId: StepId, chunk: Json): void;
507
586
  }
508
587
  interface QueryRunsWhere<FlowId extends string = string> {
509
588
  readonly flowId?: FlowId;
@@ -522,7 +601,7 @@ type QueryRunsOpts<FlowId extends string = string> = {
522
601
  readonly cursor?: string;
523
602
  };
524
603
  interface RunSummary<FlowId extends string = string> {
525
- readonly runId: RunId;
604
+ readonly runId: RunId$1;
526
605
  readonly flowId: FlowId;
527
606
  readonly status: RunStatus;
528
607
  readonly startedAt: Date;
@@ -546,7 +625,7 @@ interface PruneResult {
546
625
  }
547
626
  interface QueueMessage {
548
627
  readonly receipt: string;
549
- readonly runId: RunId;
628
+ readonly runId: RunId$1;
550
629
  readonly stepId: StepId;
551
630
  readonly payload: Json;
552
631
  readonly attempt: AttemptNumber;
@@ -560,7 +639,7 @@ interface QueueEnqueueOpts {
560
639
  readonly payload?: Json;
561
640
  }
562
641
  interface Queue {
563
- enqueue(runId: RunId, stepId: StepId, opts?: QueueEnqueueOpts): Promise<void>;
642
+ enqueue(runId: RunId$1, stepId: StepId, opts?: QueueEnqueueOpts): Promise<void>;
564
643
  dequeue(opts: QueueDequeueOpts): Promise<readonly QueueMessage[]>;
565
644
  ack(receipt: string): Promise<void>;
566
645
  nack(receipt: string, opts?: {
@@ -572,14 +651,14 @@ interface Queue {
572
651
  interface Clock {
573
652
  now(): Date;
574
653
  sleep(ms: Millis, signal?: AbortSignal): Promise<void>;
575
- schedule(at: Date, runId: RunId, stepId: StepId): Promise<void>;
654
+ schedule(at: Date, runId: RunId$1, stepId: StepId): Promise<void>;
576
655
  }
577
656
  interface Trigger {
578
- subscribe(handler: (runId: RunId) => void): () => void;
657
+ subscribe(handler: (runId: RunId$1) => void): () => void;
579
658
  }
580
- 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";
581
660
  interface FactBase {
582
- readonly runId: RunId;
661
+ readonly runId: RunId$1;
583
662
  readonly at: Date;
584
663
  }
585
664
  interface FlowStartedFact extends FactBase {
@@ -609,7 +688,7 @@ interface FlowFailedFact extends FactBase {
609
688
  interface FlowCanceledByConcurrencyFact extends FactBase {
610
689
  readonly kind: "flow.canceled";
611
690
  readonly cause: "concurrency";
612
- readonly canceledByRunId: RunId;
691
+ readonly canceledByRunId: RunId$1;
613
692
  readonly concurrencyKey: string;
614
693
  }
615
694
  interface FlowCanceledExplicitlyFact extends FactBase {
@@ -694,6 +773,13 @@ interface MatchArmSelectedFact extends FactBase {
694
773
  readonly stepId: StepId;
695
774
  readonly arm: string;
696
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
+ }
697
783
  interface StepResetFact extends FactBase {
698
784
  readonly kind: "step.reset";
699
785
  readonly stepId: StepId;
@@ -708,7 +794,7 @@ interface StepAbortRequestedFact extends FactBase {
708
794
  readonly actor: string;
709
795
  readonly note?: string;
710
796
  }
711
- 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;
712
798
  type RunStatus = "pending" | "running" | "completed" | "failed" | "canceled";
713
799
  type StepStatus = "pending" | "running" | "completed" | "failed" | "canceled" | "skipped";
714
800
 
@@ -724,9 +810,9 @@ interface OperatorAuditOpts {
724
810
  readonly note?: string;
725
811
  }
726
812
  interface Operator {
727
- skip(runId: RunId, stepId: StepId, opts: OperatorAuditOpts): Promise<void>;
728
- retry(runId: RunId, stepId: StepId, opts: OperatorAuditOpts): Promise<void>;
729
- 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>;
730
816
  }
731
817
 
732
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>;
@@ -798,21 +884,43 @@ declare class NagiRuntimeError extends Error {
798
884
  constructor(message: string);
799
885
  }
800
886
  declare class NagiCanceledError extends Error {
801
- readonly runId: RunId;
802
- readonly canceledByRunId: RunId;
887
+ readonly runId: RunId$1;
888
+ readonly canceledByRunId: RunId$1;
803
889
  readonly concurrencyKey: string;
804
890
  constructor({ runId, canceledByRunId, concurrencyKey, }: {
805
- readonly runId: RunId;
806
- 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;
807
915
  readonly concurrencyKey: string;
808
916
  });
809
917
  }
810
918
  declare class NagiSnapshotDriftError extends Error {
811
- readonly runId: RunId;
919
+ readonly runId: RunId$1;
812
920
  readonly expected: string;
813
921
  readonly actual: string;
814
922
  constructor({ runId, expected, actual, }: {
815
- readonly runId: RunId;
923
+ readonly runId: RunId$1;
816
924
  readonly expected: string;
817
925
  readonly actual: string;
818
926
  });
@@ -835,23 +943,39 @@ declare class InMemoryStore implements Store, StreamTransport {
835
943
  private readonly streamHub;
836
944
  private readonly leaseMs;
837
945
  constructor(opts?: InMemoryStoreOpts);
838
- appendFact(runId: RunId, fact: Fact): Promise<void>;
839
- tryStartRun(runId: RunId, fact: FlowStartedFact, concurrency?: {
946
+ appendFact(runId: RunId$1, fact: Fact): Promise<void>;
947
+ tryStartRun(runId: RunId$1, fact: FlowStartedFact, concurrency?: {
840
948
  readonly key: string;
841
949
  readonly mode: ConcurrencyMode;
842
950
  }): Promise<{
843
951
  readonly started: boolean;
844
952
  readonly canceled: ReadonlyArray<{
845
- readonly runId: RunId;
953
+ readonly runId: RunId$1;
846
954
  readonly fact: FlowCanceledByConcurrencyFact;
847
955
  }>;
848
956
  }>;
849
- listChildren(parentRunId: RunId): Promise<ReadonlyArray<RunId>>;
850
- loadRunState(runId: RunId): Promise<RunState>;
851
- claimStep(runId: RunId, stepId: StepId, attempt: AttemptNumber): Promise<ClaimToken | null>;
852
- settleStep(runId: RunId, _stepId: StepId, fact: StepCompletedFact | StepFailedFact): Promise<void>;
957
+ tryStartRunOnTx(_tx: Tx, runId: RunId$1, fact: FlowStartedFact, concurrency?: {
958
+ readonly key: string;
959
+ readonly mode: ConcurrencyMode;
960
+ }): Promise<{
961
+ readonly started: boolean;
962
+ readonly canceled: ReadonlyArray<{
963
+ readonly runId: RunId$1;
964
+ readonly fact: FlowCanceledByConcurrencyFact;
965
+ }>;
966
+ }>;
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>;
853
977
  settleSignal(args: {
854
- readonly runId: RunId;
978
+ readonly runId: RunId$1;
855
979
  readonly stepId: StepId;
856
980
  readonly at: Date;
857
981
  readonly incoming?: {
@@ -859,9 +983,9 @@ declare class InMemoryStore implements Store, StreamTransport {
859
983
  readonly signalName?: string;
860
984
  };
861
985
  }): Promise<SettleSignalResult>;
862
- recordOnce(runId: RunId, stepId: StepId, scope: string, value: Json): Promise<void>;
863
- getOnce(runId: RunId, stepId: StepId, scope: string): Promise<Json | null>;
864
- 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<{
865
989
  readonly output: T;
866
990
  readonly fact: StepCompletedFact | StepFailedFact | StepCanceledFact;
867
991
  }>): Promise<T>;
@@ -879,11 +1003,12 @@ declare class InMemoryStore implements Store, StreamTransport {
879
1003
  appendGlobalFact(fact: GlobalFact): Promise<void>;
880
1004
  readGlobalFacts(): ReadonlyArray<GlobalFact>;
881
1005
  queryRuns(opts: QueryRunsOpts): Promise<QueryRunsResult>;
1006
+ describe(runId: RunId$1): Promise<RunDescription>;
882
1007
  pruneFacts(opts: Required<PruneOpts>): Promise<PruneResult>;
883
- subscribeStream(runId: RunId, stepId: StepId, opts?: {
1008
+ subscribeStream(runId: RunId$1, stepId: StepId, opts?: {
884
1009
  readonly replayBuffered?: boolean;
885
1010
  }): AsyncIterable<StreamEvent<Json>>;
886
- publishChunk(runId: RunId, stepId: StepId, chunk: Json): void;
1011
+ publishChunk(runId: RunId$1, stepId: StepId, chunk: Json): void;
887
1012
  }
888
1013
 
889
1014
  interface InMemoryQueueOpts {
@@ -894,7 +1019,7 @@ declare class InMemoryQueue implements Queue {
894
1019
  private readonly leased;
895
1020
  private readonly leaseMs;
896
1021
  constructor(opts?: InMemoryQueueOpts);
897
- enqueue(runId: RunId, stepId: StepId, opts?: QueueEnqueueOpts): Promise<void>;
1022
+ enqueue(runId: RunId$1, stepId: StepId, opts?: QueueEnqueueOpts): Promise<void>;
898
1023
  dequeue(opts: QueueDequeueOpts): Promise<readonly QueueMessage[]>;
899
1024
  ack(receipt: string): Promise<void>;
900
1025
  nack(receipt: string, opts?: {
@@ -911,15 +1036,21 @@ declare class InMemoryClock implements Clock {
911
1036
  constructor(opts?: InMemoryClockOpts);
912
1037
  now(): Date;
913
1038
  sleep(ms: Millis, signal?: AbortSignal): Promise<void>;
914
- schedule(at: Date, runId: RunId, stepId: StepId): Promise<void>;
1039
+ schedule(at: Date, runId: RunId$1, stepId: StepId): Promise<void>;
915
1040
  dispose(): void;
916
1041
  }
917
1042
  declare class InMemoryTrigger implements Trigger {
918
1043
  private handlers;
919
- subscribe(handler: (runId: RunId) => void): () => void;
920
- fire(runId: RunId): void;
1044
+ subscribe(handler: (runId: RunId$1) => void): () => void;
1045
+ fire(runId: RunId$1): void;
921
1046
  }
922
1047
 
1048
+ type RunId = RunId$1;
1049
+ declare const RunId: {
1050
+ readonly parse: (s: string) => RunId;
1051
+ readonly fromTrusted: (s: string) => RunId;
1052
+ };
1053
+
923
1054
  interface NagiConfig {
924
1055
  readonly flows: ReadonlyArray<Flow>;
925
1056
  readonly store: Store;
@@ -933,22 +1064,39 @@ interface NagiConfig {
933
1064
  readonly codeVersion?: string;
934
1065
  readonly heartbeatIntervalMs?: Millis;
935
1066
  readonly heartbeatLeaseMs?: Millis;
1067
+ readonly reaperIntervalMs?: Millis;
936
1068
  }
937
1069
  interface StartOpts {
938
- 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>;
939
1080
  }
940
1081
  interface CancelOpts {
941
1082
  readonly reason?: string;
942
1083
  }
943
1084
  interface Wf<TFlows extends ReadonlyArray<Flow> = ReadonlyArray<Flow>> {
944
- start<F extends TFlows[number]>(flow: F, input: FlowInput<F>, opts?: StartOpts): Promise<RunId>;
945
- startById(flowId: string, input: unknown, opts?: StartOpts): Promise<RunId>;
946
- signal(runId: RunId, stepName: string, payload: unknown): Promise<void>;
947
- 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>;
948
1095
  worker(config?: WorkerConfig): Worker;
949
- replay(runId: RunId, opts?: ReplayOpts): Promise<void>;
1096
+ replay(runId: RunId$1, opts?: ReplayOpts): Promise<void>;
950
1097
  queryRuns(opts?: QueryRunsOpts<FlowIdOf<TFlows>>): Promise<QueryRunsResult<FlowIdOf<TFlows>>>;
951
- 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?: {
952
1100
  readonly replayBuffered?: boolean;
953
1101
  }): AsyncIterable<StreamEvent<C>>;
954
1102
  operator(): Operator;
@@ -996,4 +1144,10 @@ declare function decideSignal(args: {
996
1144
  } | undefined;
997
1145
  }): SignalDecision;
998
1146
 
999
- 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 };