@swirls/looms 0.13.0 → 0.13.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @swirls/looms
2
2
 
3
+ ## 0.13.1
4
+
5
+ ### Patch Changes
6
+
7
+ - ab05943: A run on a Durable Object now recovers by itself when its isolate is lost while an in-process effect is running. Until now such a run had no alarm, so after an eviction, deploy, or crash mid-effect it stayed `running` until something signalled it. While a wake waits on in-process effects it now keeps the object's alarm about 30 seconds ahead and renews it; if the isolate goes away the alarm fires in a new one, the run wakes, and the effect is re-attempted as it already is on any later wake. `WakeScheduler` gains an optional `keepAlive` for hosts whose wakes outlive the process, and `livenessLeaseMs` sets the interval. Schedulers without `keepAlive`, including the default in-process one, behave as before.
8
+
3
9
  ## 0.13.0
4
10
 
5
11
  ### Minor Changes
@@ -9,6 +9,8 @@ export interface ActorCellOptions<TModules extends readonly AnyRuntimeModule[] =
9
9
  readonly definitionResolver?: DefinitionResolver;
10
10
  readonly snapshotEvery?: number;
11
11
  readonly maxWakeIterations?: number;
12
+ /** See `CreateRuntimeOptions.livenessLeaseMs`. */
13
+ readonly livenessLeaseMs?: number;
12
14
  readonly maxPendingRunOperations?: number;
13
15
  readonly snapshotStore?: SnapshotStore;
14
16
  readonly trimAfterSnapshot?: {
@@ -17,6 +17,7 @@ export function createActorCell(options) {
17
17
  worker: options.worker,
18
18
  snapshotEvery: options.snapshotEvery,
19
19
  maxWakeIterations: options.maxWakeIterations,
20
+ livenessLeaseMs: options.livenessLeaseMs,
20
21
  maxPendingRunOperations: options.maxPendingRunOperations,
21
22
  snapshotStore: options.snapshotStore,
22
23
  trimAfterSnapshot: options.trimAfterSnapshot,
@@ -24,6 +24,252 @@ export declare const callLlmEffect: NoInfer<import("@swirls/looms/core").LocalEf
24
24
  readonly alreadyCompacted?: boolean | undefined;
25
25
  readonly consumedSteerMessageIds?: readonly string[] | undefined;
26
26
  }, AgentDefinitionsTag | LlmTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
27
+ readonly type: "runtime.child.close.requested";
28
+ readonly payload: {
29
+ readonly policy: "terminate" | "wait";
30
+ };
31
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
32
+ readonly type: "runtime.child.settled";
33
+ readonly payload: {
34
+ readonly runId: string;
35
+ readonly status: "cancelled" | "completed" | "failed";
36
+ readonly output: Schema.Json;
37
+ readonly error: string | null;
38
+ };
39
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
40
+ readonly type: "runtime.effect.ambiguous";
41
+ readonly payload: {
42
+ readonly effectId: string;
43
+ readonly attempt: number;
44
+ readonly error: string;
45
+ };
46
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
47
+ readonly type: "runtime.effect.attempt.started";
48
+ readonly payload: {
49
+ readonly effectId: string;
50
+ readonly attempt: number;
51
+ };
52
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
53
+ readonly type: "runtime.effect.cancel.requested";
54
+ readonly payload: {
55
+ readonly effectId: string;
56
+ readonly attempt: number;
57
+ readonly deadlineAt: number | null;
58
+ };
59
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
60
+ readonly type: "runtime.effect.cancelled";
61
+ readonly payload: {
62
+ readonly effectId: string;
63
+ readonly attempt: number;
64
+ };
65
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
66
+ readonly type: "runtime.effect.completed";
67
+ readonly payload: {
68
+ readonly effectId: string;
69
+ readonly attempt: number;
70
+ };
71
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
72
+ readonly type: "runtime.effect.dispatch.deferred";
73
+ readonly payload: {
74
+ readonly effectId: string;
75
+ readonly attempt: number;
76
+ readonly retryAt: number;
77
+ readonly error: string;
78
+ readonly try: number;
79
+ };
80
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
81
+ readonly type: "runtime.effect.dispatched";
82
+ readonly payload: {
83
+ readonly effectId: string;
84
+ readonly attempt: number;
85
+ readonly deadlineAt: number | null;
86
+ };
87
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
88
+ readonly type: "runtime.effect.failed";
89
+ readonly payload: {
90
+ readonly effectId: string;
91
+ readonly error: string;
92
+ readonly nonRetryable?: boolean | undefined;
93
+ readonly retryAfterMs?: number | undefined;
94
+ };
95
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
96
+ readonly type: "runtime.effect.heartbeat";
97
+ readonly payload: {
98
+ readonly effectId: string;
99
+ readonly attempt: number;
100
+ readonly heartbeatAt: number;
101
+ readonly deadlineAt: number | null;
102
+ readonly details?: Schema.Json | undefined;
103
+ };
104
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
105
+ readonly type: "runtime.effect.queued";
106
+ readonly payload: {
107
+ readonly effectId: string;
108
+ readonly attempt: number;
109
+ readonly deadlineAt: number | null;
110
+ };
111
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
112
+ readonly type: "runtime.effect.retry.scheduled";
113
+ readonly payload: {
114
+ readonly effectId: string;
115
+ readonly attempt: number;
116
+ readonly nextAttemptAt: number;
117
+ readonly error: string;
118
+ };
119
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
120
+ readonly type: "runtime.effect.timed_out";
121
+ readonly payload: {
122
+ readonly effectId: string;
123
+ readonly attempt: number;
124
+ readonly timeout: string;
125
+ };
126
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
127
+ readonly type: "runtime.effect.worker.started";
128
+ readonly payload: {
129
+ readonly effectId: string;
130
+ readonly attempt: number;
131
+ readonly deadlineAt: number | null;
132
+ };
133
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
134
+ readonly type: "runtime.fork.elided";
135
+ readonly payload: {
136
+ readonly type: string;
137
+ };
138
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
139
+ readonly type: "runtime.run.cancelled";
140
+ readonly payload: {
141
+ readonly reason?: string | undefined;
142
+ };
143
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
144
+ readonly type: "runtime.run.completed";
145
+ readonly payload: {
146
+ readonly output: Schema.Json;
147
+ readonly error: string | null;
148
+ };
149
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
150
+ readonly type: "runtime.run.forked";
151
+ readonly payload: {
152
+ readonly sourceRunId: string;
153
+ readonly sourceSeq: number;
154
+ };
155
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
156
+ readonly type: "runtime.run.launched";
157
+ readonly payload: {
158
+ readonly runId: string;
159
+ readonly kind: string;
160
+ readonly definitionName: string;
161
+ readonly definitionVersion: string;
162
+ readonly effectId: string;
163
+ readonly close: "terminate" | "wait";
164
+ };
165
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
166
+ readonly type: "runtime.run.started";
167
+ readonly payload: {
168
+ readonly rootThreadId?: string | undefined;
169
+ readonly kind: string;
170
+ readonly definitionName: string;
171
+ readonly definitionVersion: string;
172
+ readonly input: Schema.Json;
173
+ readonly requestedThreadId?: string | null | undefined;
174
+ readonly launchedBy?: {
175
+ readonly runId: string;
176
+ readonly threadId: string;
177
+ readonly effectId: string;
178
+ readonly causationId: string;
179
+ readonly causationSeq: number;
180
+ } | undefined;
181
+ readonly attributes?: {
182
+ readonly [x: string]: string;
183
+ } | undefined;
184
+ readonly trace?: {
185
+ readonly traceparent: string;
186
+ readonly tracestate?: string | undefined;
187
+ } | undefined;
188
+ };
189
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
190
+ readonly type: "runtime.signal.received";
191
+ readonly payload: {
192
+ readonly signalType: string;
193
+ };
194
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
195
+ readonly type: "runtime.snapshot.taken";
196
+ readonly payload: {
197
+ readonly seq: number;
198
+ readonly stateHash: string;
199
+ readonly state?: Schema.Json | undefined;
200
+ };
201
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
202
+ readonly type: "runtime.thread.cancel.requested";
203
+ readonly payload: {
204
+ readonly threadId: string;
205
+ };
206
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
207
+ readonly type: "runtime.thread.cancelled";
208
+ readonly payload: {
209
+ readonly threadId: string;
210
+ readonly reason?: string | undefined;
211
+ readonly resumeSeq?: number | undefined;
212
+ };
213
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
214
+ readonly type: "runtime.thread.completed";
215
+ readonly payload: {
216
+ readonly threadId: string;
217
+ readonly output: Schema.Json;
218
+ };
219
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
220
+ readonly type: "runtime.thread.failed";
221
+ readonly payload: {
222
+ readonly threadId: string;
223
+ readonly error: string;
224
+ };
225
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
226
+ readonly type: "runtime.thread.started";
227
+ readonly payload: {
228
+ readonly threadId: string;
229
+ readonly kind: string;
230
+ readonly definitionName: string;
231
+ readonly definitionVersion: string;
232
+ readonly input: Schema.Json;
233
+ readonly parentThreadId: string | null;
234
+ };
235
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
236
+ readonly type: "runtime.timer.fired";
237
+ readonly payload: {
238
+ readonly timerId: string;
239
+ readonly waitId: string;
240
+ };
241
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
242
+ readonly type: "runtime.timer.set";
243
+ readonly payload: {
244
+ readonly timerId: string;
245
+ readonly waitId: string;
246
+ readonly wakeAt: number;
247
+ };
248
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
249
+ readonly type: "runtime.wait.registered";
250
+ readonly payload: {
251
+ readonly waitId: string;
252
+ readonly threadId?: string | undefined;
253
+ readonly on: {
254
+ readonly type: string | readonly string[];
255
+ readonly match?: Schema.Json | undefined;
256
+ } | {
257
+ readonly timerAt: number;
258
+ };
259
+ readonly tag?: Schema.Json | undefined;
260
+ };
261
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
262
+ readonly type: "runtime.wait.satisfied";
263
+ readonly payload: {
264
+ readonly waitId: string;
265
+ readonly tag?: Schema.Json | undefined;
266
+ readonly event: {
267
+ readonly id: string;
268
+ readonly type: string;
269
+ readonly payload: Schema.Json;
270
+ };
271
+ };
272
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
27
273
  readonly type: "agent.context.compacted";
28
274
  readonly payload: {
29
275
  readonly turn: number;
@@ -271,7 +517,19 @@ export declare const callLlmEffect: NoInfer<import("@swirls/looms/core").LocalEf
271
517
  readonly input: number;
272
518
  readonly output: number;
273
519
  };
274
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
520
+ })>>;
521
+ export declare const executeToolEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
522
+ readonly turn: number;
523
+ readonly definitionName?: string | undefined;
524
+ readonly definitionVersion: string;
525
+ readonly toolCall: {
526
+ readonly id: string;
527
+ readonly name: string;
528
+ readonly arguments: Schema.Json;
529
+ };
530
+ readonly input?: Schema.Json | undefined;
531
+ readonly depth?: number | undefined;
532
+ }, AgentDefinitionsTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
275
533
  readonly type: "runtime.child.close.requested";
276
534
  readonly payload: {
277
535
  readonly policy: "terminate" | "wait";
@@ -510,26 +768,14 @@ export declare const callLlmEffect: NoInfer<import("@swirls/looms/core").LocalEf
510
768
  readonly type: "runtime.wait.satisfied";
511
769
  readonly payload: {
512
770
  readonly waitId: string;
513
- readonly tag?: Schema.Json | undefined;
514
- readonly event: {
515
- readonly id: string;
516
- readonly type: string;
517
- readonly payload: Schema.Json;
518
- };
519
- };
520
- })>>;
521
- export declare const executeToolEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
522
- readonly turn: number;
523
- readonly definitionName?: string | undefined;
524
- readonly definitionVersion: string;
525
- readonly toolCall: {
526
- readonly id: string;
527
- readonly name: string;
528
- readonly arguments: Schema.Json;
771
+ readonly tag?: Schema.Json | undefined;
772
+ readonly event: {
773
+ readonly id: string;
774
+ readonly type: string;
775
+ readonly payload: Schema.Json;
776
+ };
529
777
  };
530
- readonly input?: Schema.Json | undefined;
531
- readonly depth?: number | undefined;
532
- }, AgentDefinitionsTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
778
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
533
779
  readonly type: "agent.context.compacted";
534
780
  readonly payload: {
535
781
  readonly turn: number;
@@ -777,251 +1023,5 @@ export declare const executeToolEffect: NoInfer<import("@swirls/looms/core").Loc
777
1023
  readonly input: number;
778
1024
  readonly output: number;
779
1025
  };
780
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
781
- readonly type: "runtime.child.close.requested";
782
- readonly payload: {
783
- readonly policy: "terminate" | "wait";
784
- };
785
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
786
- readonly type: "runtime.child.settled";
787
- readonly payload: {
788
- readonly runId: string;
789
- readonly status: "cancelled" | "completed" | "failed";
790
- readonly output: Schema.Json;
791
- readonly error: string | null;
792
- };
793
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
794
- readonly type: "runtime.effect.ambiguous";
795
- readonly payload: {
796
- readonly effectId: string;
797
- readonly attempt: number;
798
- readonly error: string;
799
- };
800
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
801
- readonly type: "runtime.effect.attempt.started";
802
- readonly payload: {
803
- readonly effectId: string;
804
- readonly attempt: number;
805
- };
806
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
807
- readonly type: "runtime.effect.cancel.requested";
808
- readonly payload: {
809
- readonly effectId: string;
810
- readonly attempt: number;
811
- readonly deadlineAt: number | null;
812
- };
813
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
814
- readonly type: "runtime.effect.cancelled";
815
- readonly payload: {
816
- readonly effectId: string;
817
- readonly attempt: number;
818
- };
819
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
820
- readonly type: "runtime.effect.completed";
821
- readonly payload: {
822
- readonly effectId: string;
823
- readonly attempt: number;
824
- };
825
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
826
- readonly type: "runtime.effect.dispatch.deferred";
827
- readonly payload: {
828
- readonly effectId: string;
829
- readonly attempt: number;
830
- readonly retryAt: number;
831
- readonly error: string;
832
- readonly try: number;
833
- };
834
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
835
- readonly type: "runtime.effect.dispatched";
836
- readonly payload: {
837
- readonly effectId: string;
838
- readonly attempt: number;
839
- readonly deadlineAt: number | null;
840
- };
841
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
842
- readonly type: "runtime.effect.failed";
843
- readonly payload: {
844
- readonly effectId: string;
845
- readonly error: string;
846
- readonly nonRetryable?: boolean | undefined;
847
- readonly retryAfterMs?: number | undefined;
848
- };
849
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
850
- readonly type: "runtime.effect.heartbeat";
851
- readonly payload: {
852
- readonly effectId: string;
853
- readonly attempt: number;
854
- readonly heartbeatAt: number;
855
- readonly deadlineAt: number | null;
856
- readonly details?: Schema.Json | undefined;
857
- };
858
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
859
- readonly type: "runtime.effect.queued";
860
- readonly payload: {
861
- readonly effectId: string;
862
- readonly attempt: number;
863
- readonly deadlineAt: number | null;
864
- };
865
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
866
- readonly type: "runtime.effect.retry.scheduled";
867
- readonly payload: {
868
- readonly effectId: string;
869
- readonly attempt: number;
870
- readonly nextAttemptAt: number;
871
- readonly error: string;
872
- };
873
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
874
- readonly type: "runtime.effect.timed_out";
875
- readonly payload: {
876
- readonly effectId: string;
877
- readonly attempt: number;
878
- readonly timeout: string;
879
- };
880
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
881
- readonly type: "runtime.effect.worker.started";
882
- readonly payload: {
883
- readonly effectId: string;
884
- readonly attempt: number;
885
- readonly deadlineAt: number | null;
886
- };
887
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
888
- readonly type: "runtime.fork.elided";
889
- readonly payload: {
890
- readonly type: string;
891
- };
892
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
893
- readonly type: "runtime.run.cancelled";
894
- readonly payload: {
895
- readonly reason?: string | undefined;
896
- };
897
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
898
- readonly type: "runtime.run.completed";
899
- readonly payload: {
900
- readonly output: Schema.Json;
901
- readonly error: string | null;
902
- };
903
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
904
- readonly type: "runtime.run.forked";
905
- readonly payload: {
906
- readonly sourceRunId: string;
907
- readonly sourceSeq: number;
908
- };
909
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
910
- readonly type: "runtime.run.launched";
911
- readonly payload: {
912
- readonly runId: string;
913
- readonly kind: string;
914
- readonly definitionName: string;
915
- readonly definitionVersion: string;
916
- readonly effectId: string;
917
- readonly close: "terminate" | "wait";
918
- };
919
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
920
- readonly type: "runtime.run.started";
921
- readonly payload: {
922
- readonly rootThreadId?: string | undefined;
923
- readonly kind: string;
924
- readonly definitionName: string;
925
- readonly definitionVersion: string;
926
- readonly input: Schema.Json;
927
- readonly requestedThreadId?: string | null | undefined;
928
- readonly launchedBy?: {
929
- readonly runId: string;
930
- readonly threadId: string;
931
- readonly effectId: string;
932
- readonly causationId: string;
933
- readonly causationSeq: number;
934
- } | undefined;
935
- readonly attributes?: {
936
- readonly [x: string]: string;
937
- } | undefined;
938
- readonly trace?: {
939
- readonly traceparent: string;
940
- readonly tracestate?: string | undefined;
941
- } | undefined;
942
- };
943
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
944
- readonly type: "runtime.signal.received";
945
- readonly payload: {
946
- readonly signalType: string;
947
- };
948
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
949
- readonly type: "runtime.snapshot.taken";
950
- readonly payload: {
951
- readonly seq: number;
952
- readonly stateHash: string;
953
- readonly state?: Schema.Json | undefined;
954
- };
955
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
956
- readonly type: "runtime.thread.cancel.requested";
957
- readonly payload: {
958
- readonly threadId: string;
959
- };
960
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
961
- readonly type: "runtime.thread.cancelled";
962
- readonly payload: {
963
- readonly threadId: string;
964
- readonly reason?: string | undefined;
965
- readonly resumeSeq?: number | undefined;
966
- };
967
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
968
- readonly type: "runtime.thread.completed";
969
- readonly payload: {
970
- readonly threadId: string;
971
- readonly output: Schema.Json;
972
- };
973
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
974
- readonly type: "runtime.thread.failed";
975
- readonly payload: {
976
- readonly threadId: string;
977
- readonly error: string;
978
- };
979
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
980
- readonly type: "runtime.thread.started";
981
- readonly payload: {
982
- readonly threadId: string;
983
- readonly kind: string;
984
- readonly definitionName: string;
985
- readonly definitionVersion: string;
986
- readonly input: Schema.Json;
987
- readonly parentThreadId: string | null;
988
- };
989
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
990
- readonly type: "runtime.timer.fired";
991
- readonly payload: {
992
- readonly timerId: string;
993
- readonly waitId: string;
994
- };
995
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
996
- readonly type: "runtime.timer.set";
997
- readonly payload: {
998
- readonly timerId: string;
999
- readonly waitId: string;
1000
- readonly wakeAt: number;
1001
- };
1002
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
1003
- readonly type: "runtime.wait.registered";
1004
- readonly payload: {
1005
- readonly waitId: string;
1006
- readonly threadId?: string | undefined;
1007
- readonly on: {
1008
- readonly type: string | readonly string[];
1009
- readonly match?: Schema.Json | undefined;
1010
- } | {
1011
- readonly timerAt: number;
1012
- };
1013
- readonly tag?: Schema.Json | undefined;
1014
- };
1015
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
1016
- readonly type: "runtime.wait.satisfied";
1017
- readonly payload: {
1018
- readonly waitId: string;
1019
- readonly tag?: Schema.Json | undefined;
1020
- readonly event: {
1021
- readonly id: string;
1022
- readonly type: string;
1023
- readonly payload: Schema.Json;
1024
- };
1025
- };
1026
1026
  })>>;
1027
1027
  export declare function readToolCall(value: JsonValue): ToolCall | null;
@@ -10,10 +10,19 @@ export interface DurableObjectAlarms {
10
10
  readonly scheduler: WakeScheduler;
11
11
  /** Ensures the alarm fires no later than `at` without forgetting the runtime deadline. */
12
12
  readonly scheduleAtEarliest: (at: number) => Effect.Effect<void>;
13
+ /**
14
+ * Run when the alarm fires, before waking. A runtime deadline that is already due is being
15
+ * delivered by this alarm, so it is forgotten instead of being set again by a later rearm.
16
+ */
17
+ readonly fired: Effect.Effect<void>;
13
18
  }
14
19
  /**
15
20
  * The single owner of a Durable Object's one alarm. Every write is serialized and sets the alarm to
16
- * the earliest of the runtime deadline, `additionalDeadline` (such as projector retries), and the
17
- * requested time, so concurrent writers cannot overwrite each other's deadlines.
21
+ * the earliest of the runtime deadline, the liveness deadline of a wake with in-process effects in
22
+ * flight, `additionalDeadline` (such as projector retries), and the requested time, so concurrent
23
+ * writers cannot overwrite each other's deadlines.
24
+ *
25
+ * The liveness deadline is what recovers a run whose isolate is lost mid-effect: the live wake
26
+ * keeps pushing it forward, and once nothing does it fires in a fresh isolate, which wakes the run.
18
27
  */
19
28
  export declare function durableObjectAlarms(storage: AlarmStorage, additionalDeadline?: () => Effect.Effect<number | null>): DurableObjectAlarms;
@@ -1,16 +1,21 @@
1
- import { Effect, Semaphore } from 'effect';
1
+ import { Clock, Effect, Semaphore } from 'effect';
2
2
  function earliest(...deadlines) {
3
3
  const known = deadlines.filter((deadline) => deadline !== null && deadline !== undefined);
4
4
  return known.length === 0 ? null : Math.min(...known);
5
5
  }
6
6
  /**
7
7
  * The single owner of a Durable Object's one alarm. Every write is serialized and sets the alarm to
8
- * the earliest of the runtime deadline, `additionalDeadline` (such as projector retries), and the
9
- * requested time, so concurrent writers cannot overwrite each other's deadlines.
8
+ * the earliest of the runtime deadline, the liveness deadline of a wake with in-process effects in
9
+ * flight, `additionalDeadline` (such as projector retries), and the requested time, so concurrent
10
+ * writers cannot overwrite each other's deadlines.
11
+ *
12
+ * The liveness deadline is what recovers a run whose isolate is lost mid-effect: the live wake
13
+ * keeps pushing it forward, and once nothing does it fires in a fresh isolate, which wakes the run.
10
14
  */
11
15
  export function durableObjectAlarms(storage, additionalDeadline) {
12
16
  // `undefined` until the runtime reports a deadline; until then an existing alarm is preserved.
13
17
  let runtimeDeadline;
18
+ let livenessDeadline = null;
14
19
  const writes = Semaphore.makeUnsafe(1);
15
20
  const rearm = (requested) => Semaphore.withPermits(writes, 1, Effect.gen(function* () {
16
21
  const [additional, current] = yield* Effect.all([
@@ -18,7 +23,7 @@ export function durableObjectAlarms(storage, additionalDeadline) {
18
23
  Effect.promise(() => storage.getAlarm?.() ?? Promise.resolve(null)),
19
24
  ], { concurrency: 'unbounded' });
20
25
  const preserved = runtimeDeadline === undefined ? current : null;
21
- const desired = earliest(runtimeDeadline, additional, requested, preserved);
26
+ const desired = earliest(runtimeDeadline, livenessDeadline, additional, requested, preserved);
22
27
  if (desired === current) {
23
28
  return;
24
29
  }
@@ -34,7 +39,16 @@ export function durableObjectAlarms(storage, additionalDeadline) {
34
39
  runtimeDeadline = null;
35
40
  return rearm(null);
36
41
  }),
42
+ keepAlive: (_runId, until) => Effect.suspend(() => {
43
+ livenessDeadline = until;
44
+ return rearm(null);
45
+ }),
37
46
  },
38
47
  scheduleAtEarliest: rearm,
48
+ fired: Clock.currentTimeMillis.pipe(Effect.map((now) => {
49
+ if (runtimeDeadline !== undefined && runtimeDeadline !== null && runtimeDeadline <= now) {
50
+ runtimeDeadline = null;
51
+ }
52
+ })),
39
53
  };
40
54
  }
@@ -14,6 +14,11 @@ export interface LoomsDurableObjectConfig {
14
14
  readonly blobs?: BlobStore;
15
15
  readonly snapshotEvery?: number;
16
16
  readonly maxWakeIterations?: number;
17
+ /**
18
+ * How far ahead the alarm is kept while in-process effects run, in milliseconds. If the isolate
19
+ * is lost mid-effect the run wakes and re-attempts within about this long. Default 30000.
20
+ */
21
+ readonly livenessLeaseMs?: number;
17
22
  readonly maxPendingRunOperations?: number;
18
23
  readonly trimAfterSnapshot?: {
19
24
  readonly keepSnapshots: number;
@@ -41,6 +46,7 @@ export interface LoomsDurableObjectConfig {
41
46
  export declare abstract class LoomsDurableObject<Env = unknown> extends DurableObject<Env> {
42
47
  private cellPromise?;
43
48
  private store?;
49
+ private alarms?;
44
50
  private authorize;
45
51
  abstract configure(env: Env): LoomsDurableObjectConfig | Promise<LoomsDurableObjectConfig>;
46
52
  private getCell;
@@ -15,6 +15,7 @@ import { attachSocketFanout, isRunSocketUpgrade, openRunSocket } from './websock
15
15
  export class LoomsDurableObject extends DurableObject {
16
16
  cellPromise;
17
17
  store;
18
+ alarms;
18
19
  authorize = defaultAuthorize;
19
20
  getCell() {
20
21
  if (this.cellPromise) {
@@ -44,6 +45,7 @@ export class LoomsDurableObject extends DurableObject {
44
45
  blobs: config.blobs,
45
46
  });
46
47
  this.store = store;
48
+ this.alarms = alarms;
47
49
  this.authorize = config.authorize ?? defaultAuthorize;
48
50
  const cell = createActorCell({
49
51
  runId,
@@ -54,6 +56,7 @@ export class LoomsDurableObject extends DurableObject {
54
56
  attributes: config.attributes,
55
57
  snapshotEvery: config.snapshotEvery,
56
58
  maxWakeIterations: config.maxWakeIterations,
59
+ livenessLeaseMs: config.livenessLeaseMs,
57
60
  maxPendingRunOperations: config.maxPendingRunOperations,
58
61
  trimAfterSnapshot: config.trimAfterSnapshot,
59
62
  authorize: config.authorize,
@@ -142,6 +145,7 @@ export class LoomsDurableObject extends DurableObject {
142
145
  return cell.getRun().then((state) => deleteTerminalRunStorage(this.ctx.storage, store, runId, state, options).then(() => {
143
146
  this.cellPromise = undefined;
144
147
  this.store = undefined;
148
+ this.alarms = undefined;
145
149
  }));
146
150
  });
147
151
  }
@@ -157,7 +161,9 @@ export class LoomsDurableObject extends DurableObject {
157
161
  if (delivery) {
158
162
  this.ctx.waitUntil(Effect.runPromise(delivery.deliver(runId)));
159
163
  }
160
- return cell.wake();
164
+ // Otherwise a wake still running here would keep rearming for the deadline that just
165
+ // fired, alongside its liveness deadline, and the alarm would fire again at once.
166
+ return Effect.runPromise(this.alarms?.fired ?? Effect.void).then(() => cell.wake());
161
167
  })
162
168
  .then(() => undefined);
163
169
  }
@@ -21,6 +21,8 @@ export interface CreateLoomsOptions<Fields extends RunAttributeFields = RunAttri
21
21
  hostname?: string;
22
22
  };
23
23
  readonly maxWakeIterations?: number;
24
+ /** See `CreateRuntimeOptions.livenessLeaseMs`. Only used when `scheduler` implements `keepAlive`. */
25
+ readonly livenessLeaseMs?: number;
24
26
  /** Maximum active and queued mutation operations per run. Default 256. */
25
27
  readonly maxPendingRunOperations?: number;
26
28
  /** Durable events between mid-wake snapshots. Default 200; `0` disables. Parking always snapshots. */
@@ -41,6 +41,7 @@ export function createLooms(options = {}) {
41
41
  modules,
42
42
  store,
43
43
  maxWakeIterations: options.maxWakeIterations,
44
+ livenessLeaseMs: options.livenessLeaseMs,
44
45
  maxPendingRunOperations: options.maxPendingRunOperations,
45
46
  snapshotEvery: options.snapshotEvery,
46
47
  snapshotStore: options.blobStore
@@ -253,6 +253,7 @@ export function createRuntime(options) {
253
253
  repository,
254
254
  snapshotEvery,
255
255
  maxWakeIterations,
256
+ livenessLeaseMs: options.livenessLeaseMs,
256
257
  maxInFlightWorkerEffects: options.maxInFlightWorkerEffects,
257
258
  worker: options.worker,
258
259
  observer: options.observer,
@@ -21,6 +21,12 @@ export interface CreateRuntimeOptions<TModules extends readonly AnyRuntimeModule
21
21
  */
22
22
  readonly snapshotEvery?: number;
23
23
  readonly maxWakeIterations?: number;
24
+ /**
25
+ * How far ahead a wake with in-process effects in flight keeps its `WakeScheduler.keepAlive`
26
+ * deadline, in milliseconds. Renewed every third of it. Default 30000. Only used when the
27
+ * scheduler implements `keepAlive`.
28
+ */
29
+ readonly livenessLeaseMs?: number;
24
30
  /** Maximum concurrent in-flight worker effects dispatched per run (fairness guard). */
25
31
  readonly maxInFlightWorkerEffects?: number;
26
32
  /** Maximum active and queued mutation operations per run. Default 256. */
@@ -13,6 +13,7 @@ export interface WakeEngineOptions {
13
13
  readonly repository: RuntimeRepository;
14
14
  readonly snapshotEvery: number;
15
15
  readonly maxWakeIterations: number;
16
+ readonly livenessLeaseMs?: number;
16
17
  readonly maxInFlightWorkerEffects?: number;
17
18
  readonly worker?: EffectWorker;
18
19
  readonly observer?: RuntimeObserver;
@@ -9,6 +9,7 @@ import { executeLocalEffects } from './local-effect-executor.js';
9
9
  import { notifyObserver } from './observer.js';
10
10
  import { requestWakeAgain, } from './run-execution-context.js';
11
11
  import { dispatchWorkerEffect, isWorkerEffect, planWorkerEffects } from './worker-effect-dispatch.js';
12
+ const DEFAULT_LIVENESS_LEASE_MS = 30_000;
12
13
  function currentTimeMillis() {
13
14
  return Math.floor(performance.timeOrigin + performance.now());
14
15
  }
@@ -20,6 +21,8 @@ export function createWakeEngine(options) {
20
21
  set: repository.setCachedCursor,
21
22
  };
22
23
  const observe = (event) => notifyObserver(options.observer, event);
24
+ const livenessLeaseMs = Math.max(1, options.livenessLeaseMs ?? DEFAULT_LIVENESS_LEASE_MS);
25
+ const livenessRenewMs = Math.max(1, Math.floor(livenessLeaseMs / 3));
23
26
  const closeLaunchedRuns = (store, runId, cursor) => Effect.gen(function* () {
24
27
  let next = cursor;
25
28
  const state = next.state;
@@ -74,6 +77,34 @@ export function createWakeEngine(options) {
74
77
  executionContext.resolveWakeCompletion = resolveCompletion;
75
78
  executionContext.waking = true;
76
79
  let released = false;
80
+ // When the liveness deadline is next due for renewal; `null` while this wake holds none.
81
+ let livenessRenewAt = null;
82
+ // In-process effects have no deadline of their own, so nothing would wake the run if this
83
+ // process were lost while they run. Where the scheduler supports it, keep a wake pending a
84
+ // short time ahead for as long as the wake waits on them. Never returns.
85
+ const holdLiveness = Effect.gen(function* () {
86
+ const scheduler = options.scheduler();
87
+ if (!scheduler.keepAlive) {
88
+ return yield* Effect.never;
89
+ }
90
+ while (true) {
91
+ const now = yield* Clock.currentTimeMillis;
92
+ if (livenessRenewAt !== null && now < livenessRenewAt) {
93
+ yield* Effect.sleep(livenessRenewAt - now);
94
+ continue;
95
+ }
96
+ // Claimed before the write so a failed or interrupted write is still cleared on exit.
97
+ livenessRenewAt = now + livenessRenewMs;
98
+ yield* scheduler.keepAlive(runId, now + livenessLeaseMs);
99
+ }
100
+ });
101
+ const releaseLiveness = Effect.suspend(() => {
102
+ if (livenessRenewAt === null) {
103
+ return Effect.void;
104
+ }
105
+ livenessRenewAt = null;
106
+ return options.scheduler().keepAlive?.(runId, null) ?? Effect.void;
107
+ });
77
108
  const releaseWake = () => {
78
109
  released = true;
79
110
  executionContext.waking = false;
@@ -361,11 +392,16 @@ export function createWakeEngine(options) {
361
392
  if (executionContext.wakeAgain) {
362
393
  Deferred.doneUnsafe(wakeGate, Effect.void);
363
394
  }
364
- const progress = yield* Effect.raceFirst(Queue.take(settled).pipe(Effect.map((item) => ({ _tag: 'settled', ...item }))), Deferred.await(wakeGate).pipe(Effect.as({ _tag: 'wake' })));
395
+ const progress = yield* Effect.raceFirst(Effect.raceFirst(Queue.take(settled).pipe(Effect.map((item) => ({ _tag: 'settled', ...item }))), Deferred.await(wakeGate).pipe(Effect.as({ _tag: 'wake' }))), holdLiveness);
365
396
  if (executionContext.wakeSignal === wakeGate) {
366
397
  executionContext.wakeSignal = null;
367
398
  }
368
399
  if (progress._tag === 'wake') {
400
+ // A joined wake may be the host's pending wake firing, which uses it up. Ask again
401
+ // on the next wait instead of at the next renewal.
402
+ if (livenessRenewAt !== null) {
403
+ livenessRenewAt = Number.NEGATIVE_INFINITY;
404
+ }
369
405
  continue;
370
406
  }
371
407
  inFlight.delete(progress.groupKey);
@@ -453,7 +489,10 @@ export function createWakeEngine(options) {
453
489
  return yield* new RuntimeExecutionError(`Wake for run "${runId}" completed without state`);
454
490
  }
455
491
  return finalState;
456
- }).pipe(Effect.onExit((exit) => Effect.sync(() => {
492
+ }).pipe(
493
+ // Before the claim is released, so a wake that starts next takes a fresh deadline after
494
+ // this one is cleared rather than having its own cleared.
495
+ Effect.onExit(() => releaseLiveness), Effect.onExit((exit) => Effect.sync(() => {
457
496
  if (!released) {
458
497
  releaseWake();
459
498
  }
@@ -2,6 +2,14 @@ import { Effect } from 'effect';
2
2
  export interface WakeScheduler {
3
3
  schedule(runId: string, at: number): Effect.Effect<void>;
4
4
  cancel(runId: string): Effect.Effect<void>;
5
+ /**
6
+ * Optional. While a wake has in-process effects in flight it asks to be woken at `until`, renews
7
+ * that before it passes, and clears it with `null` when the wake ends. Implement it only where
8
+ * the wake outlives the process (a Durable Object alarm): if the process is lost mid-effect the
9
+ * deadline fires elsewhere and the run re-attempts the effect. It must not replace the deadline
10
+ * set through `schedule`; the host wakes at the earlier of the two.
11
+ */
12
+ keepAlive?(runId: string, until: number | null): Effect.Effect<void>;
5
13
  dispose?(): void;
6
14
  }
7
15
  export declare function createTimeoutScheduler(wake: (runId: string) => Effect.Effect<void>): WakeScheduler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swirls/looms",
3
- "version": "0.13.0",
3
+ "version": "0.13.1",
4
4
  "description": "Pluggable event-sourced runtime for durable agents and workflows",
5
5
  "homepage": "https://github.com/ByteSliceHQ/looms#readme",
6
6
  "bugs": {