-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathEffectWorker.ts
More file actions
834 lines (810 loc) · 34.8 KB
/
Copy pathEffectWorker.ts
File metadata and controls
834 lines (810 loc) · 34.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
import { CommandId } from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
import * as Metric from "effect/Metric";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import {
increment,
metricAttributes,
orchestrationEffectClaimsTotal,
orchestrationEffectQueueWait,
} from "../observability/Metrics.ts";
import * as RunFinalizationService from "./RunFinalizationService.ts";
import * as ResourceCleanupService from "./ResourceCleanupService.ts";
import * as EffectOutbox from "./EffectOutbox.ts";
import * as CheckpointRollbackService from "./CheckpointRollbackService.ts";
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
import * as ProviderTurnControlService from "./ProviderTurnControlService.ts";
import * as ProviderTurnStartService from "./ProviderTurnStartService.ts";
import * as RuntimeRequestService from "./RuntimeRequestService.ts";
import * as ThreadTitleRegenerationService from "./ThreadTitleRegenerationService.ts";
import * as ThreadManagementService from "./ThreadManagementService.ts";
import * as ServerSettings from "../serverSettings.ts";
import { continueRestartedRun } from "./RestartContinuation.ts";
export class OrchestrationEffectExecutionError extends Schema.TaggedError<OrchestrationEffectExecutionError>()(
"OrchestrationEffectExecutionError",
{
effectId: Schema.String,
effectType: Schema.String,
cause: Schema.optional(Schema.Defect()),
},
) {}
/**
* Pure interrupt races with hard process teardown or a dead session produce
* "not active" protocol errors. Retrying those only delays recovery.
*
* Do not apply this to `provider-turn.restart`: that compound effect also runs
* detach and start. Swallowing a start failure that happens to mention
* "is not active" would drop the outbox item without ever starting the
* replacement turn.
*/
export function isNonRetryableProviderTurnControlFailure(
effectType: string,
errorText: string,
): boolean {
if (effectType !== "provider-turn.interrupt") {
return false;
}
return (
/is not active/i.test(errorText) ||
/hard teardown is already in progress/i.test(errorText) ||
/treating as already interrupted/i.test(errorText) ||
/treating as already stopped/i.test(errorText)
);
}
export interface OrchestrationEffectExecutorV2Shape {
/**
* Runs one claimed effect. `willRetry` is true when the worker will retry a
* failure, so a step can fail and try again instead of settling the run.
*/
readonly execute: (
effect: EffectOutbox.OrchestrationEffectV2,
options?: { readonly willRetry: boolean },
) => Effect.Effect<void, OrchestrationEffectExecutionError>;
}
export class OrchestrationEffectExecutorV2 extends Context.Service<
OrchestrationEffectExecutorV2,
OrchestrationEffectExecutorV2Shape
>()("t3/orchestration-v2/EffectWorker/OrchestrationEffectExecutorV2") {}
export const executorLayer: Layer.Layer<
OrchestrationEffectExecutorV2,
never,
| ProviderSessionManager.ProviderSessionManagerV2
| RunFinalizationService.RunFinalizationService
| CheckpointRollbackService.CheckpointRollbackServiceV2
| ProviderTurnControlService.ProviderTurnControlServiceV2
| ProviderTurnStartService.ProviderTurnStartServiceV2
| RuntimeRequestService.RuntimeRequestServiceV2
| ThreadTitleRegenerationService.ThreadTitleRegenerationService
| ThreadManagementService.ThreadManagementService
| ServerSettings.ServerSettingsService
> = Layer.effect(
OrchestrationEffectExecutorV2,
Effect.gen(function* () {
const runFinalization = yield* RunFinalizationService.RunFinalizationService;
const resourceCleanup = yield* ResourceCleanupService.ResourceCleanupService;
const checkpointRollback = yield* CheckpointRollbackService.CheckpointRollbackServiceV2;
const providerSessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
const providerTurnControl = yield* ProviderTurnControlService.ProviderTurnControlServiceV2;
const providerTurnStart = yield* ProviderTurnStartService.ProviderTurnStartServiceV2;
const runtimeRequests = yield* RuntimeRequestService.RuntimeRequestServiceV2;
const threadTitleRegeneration =
yield* ThreadTitleRegenerationService.ThreadTitleRegenerationService;
const threads = yield* ThreadManagementService.ThreadManagementService;
const settings = yield* ServerSettings.ServerSettingsService;
return OrchestrationEffectExecutorV2.of({
execute: (effect, options) => {
const willRetry = options?.willRetry ?? false;
switch (effect.request.type) {
case "provider-runtime.continue": {
const sourceRunId = effect.request.sourceRunId;
return continueRestartedRun({ threadId: effect.threadId, sourceRunId }).pipe(
Effect.provideService(ThreadManagementService.ThreadManagementService, threads),
Effect.provideService(ServerSettings.ServerSettingsService, settings),
// A continuation that will never run still owes a delegated parent a result.
Effect.tapError(() =>
willRetry
? Effect.void
: threads.recoverDelegatedTask(effect.threadId, sourceRunId),
),
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
}
case "provider-session.detach":
return providerSessions
.detach({
providerSessionId: effect.request.providerSessionId,
threadId: effect.threadId,
...(effect.request.detail === undefined ? {} : { detail: effect.request.detail }),
...(effect.request.revokeMcpCredential === undefined
? {}
: { revokeMcpCredential: effect.request.revokeMcpCredential }),
})
.pipe(
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "provider-turn.start":
return providerTurnStart
.start({ threadId: effect.threadId, runId: effect.request.runId, willRetry })
.pipe(
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "provider-turn.interrupt":
return providerTurnControl
.interrupt({
threadId: effect.threadId,
providerSessionId: effect.request.providerSessionId,
providerThreadId: effect.request.providerThreadId,
providerTurnId: effect.request.providerTurnId,
})
.pipe(
// The provider has stopped what it still ran and reported it.
// Whatever the thread still shows on that provider thread is
// work no process will report on, so the Stop ends it too.
// One Stop can interrupt several provider threads, so the
// settle is keyed by effect, not by the Stop command.
Effect.andThen(
threads.dispatch({
type: "thread.background-work.settle",
commandId: CommandId.make(`${effect.id}:background-work-settled`),
threadId: effect.threadId,
providerThreadId: effect.request.providerThreadId,
providerTurnId: effect.request.providerTurnId,
}),
),
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "provider-turn.steer":
return providerTurnControl
.steer({
threadId: effect.threadId,
providerSessionId: effect.request.providerSessionId,
providerThreadId: effect.request.providerThreadId,
providerTurnId: effect.request.providerTurnId,
messageId: effect.request.messageId,
})
.pipe(
Effect.tap(() =>
Effect.gen(function* () {
if (effect.request.type !== "provider-turn.steer") return;
const messageId = effect.request.messageId;
const projection = yield* threads.getThreadRecords(
effect.threadId,
["messages", "runs"],
{ messageIds: [effect.request.messageId] },
);
const message = projection.messages.find((row) => row.id === messageId);
if (message?.delegatedCompletion === undefined) return;
yield* threads.dispatch({
type: "notification.delivery.accept",
commandId: CommandId.make(`command:mailbox-accepted:${effect.id}`),
threadId: effect.threadId,
messageId: message.id,
});
}),
),
Effect.catch((error) =>
Effect.gen(function* () {
if (
!("turnCompleted" in error) ||
!error.turnCompleted ||
effect.request.type !== "provider-turn.steer"
) {
return yield* error;
}
const projection = yield* threads.getThreadRecords(
effect.threadId,
["messages", "runs"],
{ messageIds: [effect.request.messageId] },
);
const messageId = effect.request.messageId;
const message = projection.messages.find((item) => item.id === messageId);
const run = projection.runs.find((item) => item.id === message?.runId);
if (message === undefined || run === undefined) return yield* error;
// Reuse the message identity and a stable command receipt so an outbox
// retry cannot append a duplicate message or start a second follow-up.
yield* threads.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`command:steer-follow-up:${effect.id}`),
threadId: effect.threadId,
messageId: message.id,
text: message.text,
...(message.context ? { context: message.context } : {}),
attachments: message.attachments,
// A user's follow-up starts on the thread's saved selection,
// which already holds the steer's choice. A delegated
// completion stays pinned to the run it reports to.
...(message.delegatedCompletion === undefined
? {}
: { modelSelection: run.modelSelection }),
dispatchMode: {
type:
message.delegatedCompletion === undefined
? "start_immediately"
: "queue_after_active",
},
createdBy: message.createdBy,
creationSource: message.creationSource,
...(message.delegatedCompletion === undefined
? {}
: { delegatedCompletion: message.delegatedCompletion }),
...(message.notification === undefined
? {}
: { notification: message.notification }),
...(message.scheduledTaskId === undefined
? {}
: { scheduledTaskId: message.scheduledTaskId }),
...(message.senderThreadId === undefined
? {}
: { senderThreadId: message.senderThreadId }),
});
}),
),
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "provider-turn.restart":
return providerTurnControl
.interruptAndAwaitTerminal({
threadId: effect.threadId,
providerSessionId: effect.request.providerSessionId,
providerThreadId: effect.request.providerThreadId,
providerTurnId: effect.request.providerTurnId,
interruptedAttemptId: effect.request.interruptedAttemptId,
...(effect.request.sessionTransition?.type === "replace"
? {
replacementProviderSessionId:
effect.request.sessionTransition.replacementProviderSessionId,
}
: {}),
})
.pipe(
Effect.andThen(
effect.request.sessionTransition?.type === "replace"
? providerSessions.detach({
providerSessionId: effect.request.providerSessionId,
threadId: effect.threadId,
detail: "Selection change requires a provider session restart.",
})
: effect.request.sessionTransition?.type === "detach"
? providerSessions.detach({
providerSessionId: effect.request.providerSessionId,
threadId: effect.threadId,
detail: "Provider thread handoff replaced this session binding.",
})
: Effect.void,
),
Effect.andThen(
providerTurnStart.start({
threadId: effect.threadId,
runId: effect.request.runId,
willRetry,
}),
),
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "runtime-request.respond":
return runtimeRequests
.respond({
threadId: effect.threadId,
providerSessionId: effect.request.providerSessionId,
requestId: effect.request.requestId,
...(effect.request.decision === undefined
? {}
: { decision: effect.request.decision }),
...(effect.request.answers === undefined
? {}
: { answers: effect.request.answers }),
})
.pipe(
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "provider-thread.rollback":
return checkpointRollback
.execute({
threadId: effect.threadId,
providerThreadId: effect.request.providerThreadId,
checkpointId: effect.request.checkpointId,
scopeId: effect.request.scopeId,
...(effect.request.restoreFiles === undefined
? {}
: { restoreFiles: effect.request.restoreFiles }),
})
.pipe(
// The last failed attempt tells waiting clients it failed,
// instead of leaving them to time out. Clients get a fixed
// message; the worker logs the full cause for each attempt.
Effect.tapCause((cause) =>
willRetry || Cause.hasInterruptsOnly(cause)
? Effect.void
: threads
.dispatch({
type: "checkpoint.rollback.fail",
commandId: CommandId.make(`${effect.commandId}:rollback-failed`),
threadId: effect.threadId,
requestId: effect.commandId,
message: CheckpointRollbackService.ROLLBACK_FAILED_MESSAGE,
})
.pipe(
Effect.catchCause((recordCause) =>
Effect.logWarning("Failed to record rollback failure", {
effectId: effect.id,
cause: recordCause,
}),
),
),
),
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "checkpoint.capture":
return runFinalization
.finalize({
threadId: effect.threadId,
runId: effect.request.runId,
scopeId: effect.request.scopeId,
})
.pipe(
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "terminal.cleanup":
return resourceCleanup.cleanupTerminals(effect.threadId).pipe(
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "attachment.cleanup":
return resourceCleanup.cleanupAttachments(effect.request.attachmentIds).pipe(
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
case "thread-title.generate":
return threadTitleRegeneration
.execute({
threadId: effect.threadId,
requestId: effect.commandId,
kind: effect.request.kind,
})
.pipe(
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
effectId: effect.id,
effectType: effect.request.type,
cause,
}),
),
);
}
},
});
}),
);
export class OrchestrationEffectWorkerError extends Schema.TaggedError<OrchestrationEffectWorkerError>()(
"OrchestrationEffectWorkerError",
{
operation: Schema.String,
effectId: Schema.optional(Schema.String),
cause: Schema.optional(Schema.Defect()),
},
) {}
const isOrchestrationEffectWorkerError = Schema.is(OrchestrationEffectWorkerError);
export interface OrchestrationEffectWorkerV2Shape {
readonly awaitWork: Effect.Effect<void>;
readonly runOnce: Effect.Effect<boolean, OrchestrationEffectWorkerError>;
readonly runRecoveryOnce: Effect.Effect<boolean, OrchestrationEffectWorkerError>;
readonly nextClaimableAt: Effect.Effect<
Option.Option<DateTime.Utc>,
OrchestrationEffectWorkerError
>;
readonly drain: (maxEffects?: number) => Effect.Effect<number, OrchestrationEffectWorkerError>;
}
export class OrchestrationEffectWorkerV2 extends Context.Service<
OrchestrationEffectWorkerV2,
OrchestrationEffectWorkerV2Shape
>()("t3/orchestration-v2/EffectWorker/OrchestrationEffectWorkerV2") {}
export interface OrchestrationEffectWorkerOptions {
readonly workerId?: string;
readonly leaseDurationMs?: number;
readonly maxAttempts?: number;
}
export const layerWithOptions = (
options: OrchestrationEffectWorkerOptions = {},
): Layer.Layer<
OrchestrationEffectWorkerV2,
never,
EffectOutbox.EffectOutboxV2 | OrchestrationEffectExecutorV2
> =>
Layer.effect(
OrchestrationEffectWorkerV2,
Effect.gen(function* () {
const outbox = yield* EffectOutbox.EffectOutboxV2;
const executor = yield* OrchestrationEffectExecutorV2;
const workerId = options.workerId ?? `orchestration-v2:${process.pid}`;
const leaseDurationMs = Math.max(1, options.leaseDurationMs ?? 30_000);
const maxAttempts = Math.max(1, options.maxAttempts ?? 5);
const wasCancelled = (effectId: string) =>
outbox.get(effectId).pipe(
Effect.map(
Option.match({
onNone: () => false,
onSome: (effect) => effect.status === "cancelled",
}),
),
);
const requeueClaim = (
effect: EffectOutbox.OrchestrationEffectV2,
cause: Cause.Cause<unknown>,
) =>
Cause.hasInterruptsOnly(cause)
? Effect.void
: outbox
.retry({
effectId: effect.id,
workerId,
error: `Worker failed before settling the claimed effect: ${Cause.pretty(cause)}`,
delayMs: 0,
})
.pipe(
Effect.flatMap((requeued) =>
requeued
? Effect.logWarning("Requeued effect after unexpected worker failure", {
effectId: effect.id,
effectType: effect.request.type,
})
: Effect.logWarning("Could not requeue effect after worker lost its lease", {
effectId: effect.id,
effectType: effect.request.type,
}),
),
Effect.catchCause((requeueCause) =>
Effect.logError("Failed to requeue effect after unexpected worker failure", {
effectId: effect.id,
effectType: effect.request.type,
error: Cause.pretty(requeueCause),
}),
),
);
const terminalizeClaim = (
effect: EffectOutbox.OrchestrationEffectV2,
cause: Cause.Cause<unknown>,
) => {
if (Cause.hasInterruptsOnly(cause)) return Effect.void;
return outbox
.fail({
effectId: effect.id,
workerId,
error: `Worker failed to settle a process-bound effect after execution started: ${Cause.pretty(cause)}`,
})
.pipe(
Effect.flatMap((failed) =>
failed
? Effect.logError("Terminalized process-bound effect after settlement failure", {
effectId: effect.id,
effectType: effect.request.type,
})
: Effect.logWarning(
"Could not terminalize process-bound effect after worker lost its lease",
{
effectId: effect.id,
effectType: effect.request.type,
},
),
),
Effect.catchCause((failCause) =>
Effect.logError(
"Failed to terminalize process-bound effect after settlement failure",
{
effectId: effect.id,
effectType: effect.request.type,
error: Cause.pretty(failCause),
},
),
),
);
};
const recoverPostSuccessSettlement = (
effect: EffectOutbox.OrchestrationEffectV2,
cause: Cause.Cause<unknown>,
) =>
EffectOutbox.REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS.some(
(effectType) => effectType === effect.request.type,
)
? requeueClaim(effect, cause)
: terminalizeClaim(effect, cause);
const runOnce = (excludeRestartContinuations = false) =>
Effect.gen(function* () {
const claimExit = yield* Effect.exit(
outbox.claimNext({ workerId, leaseDurationMs, excludeRestartContinuations }),
);
yield* increment(orchestrationEffectClaimsTotal, {
result: Exit.isFailure(claimExit)
? "error"
: Option.isNone(claimExit.value)
? "empty"
: "claimed",
});
if (Exit.isFailure(claimExit)) return yield* Effect.failCause(claimExit.cause);
const claimed = claimExit.value;
if (Option.isNone(claimed)) {
return false;
}
const effect = claimed.value;
// Arm the process-local cancellation signal before re-reading durable
// state. A cancellation that commits after the row read has begun can
// then still win the execution race instead of falling into the gap
// between the read and signal registration.
const cancellation = outbox
.awaitCancellation(effect.id)
.pipe(Effect.as("cancelled" as const));
const cancelledBeforeExecution = yield* Effect.gen(function* () {
const claimedAt = DateTime.toEpochMillis(yield* DateTime.now);
const eligibleAt = Math.max(
DateTime.toEpochMillis(DateTime.makeUnsafe(effect.createdAt)),
DateTime.toEpochMillis(DateTime.makeUnsafe(effect.availableAt)),
);
yield* Metric.update(
Metric.withAttributes(
orchestrationEffectQueueWait,
metricAttributes({ effect_type: effect.request.type }),
),
Duration.millis(Math.max(0, claimedAt - eligibleAt)),
);
// Cancellation can commit after the durable claim but before the
// process-local Deferred is registered. Re-read the authoritative row
// once before starting external work; later cancellations use the
// Deferred raced below.
if (yield* wasCancelled(effect.id)) {
yield* outbox.clearCancellation(effect.id);
return true;
}
return false;
}).pipe(Effect.onError((cause) => requeueClaim(effect, cause)));
if (cancelledBeforeExecution) return true;
const execution = executor
.execute(effect, { willRetry: effect.attemptCount < maxAttempts })
.pipe(Effect.as("executed" as const));
const exit = yield* Effect.exit(Effect.raceFirst(execution, cancellation)).pipe(
Effect.ensuring(outbox.clearCancellation(effect.id)),
);
if (Exit.isSuccess(exit) && exit.value === "cancelled") {
return true;
}
if (Exit.isSuccess(exit)) {
return yield* Effect.gen(function* () {
const completed = yield* outbox.succeed({ effectId: effect.id, workerId });
if (!completed) {
if (yield* wasCancelled(effect.id)) return true;
return yield* new OrchestrationEffectWorkerError({
operation: "complete",
effectId: effect.id,
cause: "The worker no longer owns the effect lease.",
});
}
return true;
}).pipe(Effect.onError((cause) => recoverPostSuccessSettlement(effect, cause)));
}
const error = Cause.pretty(exit.cause);
const nonRetryable = isNonRetryableProviderTurnControlFailure(effect.request.type, error);
yield* Effect.logWarning("Orchestration effect execution failed", {
effectId: effect.id,
effectType: effect.request.type,
attemptCount: effect.attemptCount,
nonRetryable,
error,
});
// Prefer succeed for terminal interrupt races so the outbox does not
// keep a failed interrupt around; fail only when we must not retry.
const updated = nonRetryable
? yield* outbox
.succeed({ effectId: effect.id, workerId })
.pipe(Effect.onError((cause) => terminalizeClaim(effect, cause)))
: effect.attemptCount >= maxAttempts
? yield* outbox
.fail({ effectId: effect.id, workerId, error })
.pipe(Effect.onError((cause) => terminalizeClaim(effect, cause)))
: yield* outbox
.retry({
effectId: effect.id,
workerId,
error,
delayMs: Math.min(30_000, 100 * 2 ** Math.max(0, effect.attemptCount - 1)),
})
.pipe(Effect.onError((cause) => requeueClaim(effect, cause)));
if (!updated) {
if (yield* wasCancelled(effect.id)) return true;
return yield* new OrchestrationEffectWorkerError({
operation: "reschedule",
effectId: effect.id,
cause: "The worker no longer owns the effect lease.",
});
}
return true;
}).pipe(
Effect.mapError((cause) =>
isOrchestrationEffectWorkerError(cause)
? cause
: new OrchestrationEffectWorkerError({ operation: "run", cause }),
),
);
return OrchestrationEffectWorkerV2.of({
awaitWork: outbox.awaitAvailable,
runOnce: runOnce(),
runRecoveryOnce: runOnce(true),
nextClaimableAt: outbox.nextClaimableAt.pipe(
Effect.mapError(
(cause) =>
new OrchestrationEffectWorkerError({
operation: "next-claimable",
cause,
}),
),
),
drain: (maxEffects = Number.MAX_SAFE_INTEGER) =>
Effect.gen(function* () {
let completed = 0;
while (completed < maxEffects && (yield* runOnce())) {
completed += 1;
}
return completed;
}),
});
}),
);
export const layer = layerWithOptions();
export interface OrchestrationEffectDaemonOptions {
readonly concurrency?: number;
readonly livenessPollIntervalMs?: number;
}
const DEFAULT_EFFECT_WORKER_CONCURRENCY = 4;
const DEFAULT_EFFECT_WORKER_LIVENESS_POLL_INTERVAL_MS = 30_000;
export const runDaemonWithOptions = (options: OrchestrationEffectDaemonOptions = {}) =>
Effect.scoped(
Effect.gen(function* () {
const worker = yield* OrchestrationEffectWorkerV2;
const requestedConcurrency = options.concurrency ?? DEFAULT_EFFECT_WORKER_CONCURRENCY;
const concurrency = Number.isFinite(requestedConcurrency)
? Math.max(1, Math.floor(requestedConcurrency))
: DEFAULT_EFFECT_WORKER_CONCURRENCY;
const requestedLivenessPollIntervalMs =
options.livenessPollIntervalMs ?? DEFAULT_EFFECT_WORKER_LIVENESS_POLL_INTERVAL_MS;
const livenessPollIntervalMs = Number.isFinite(requestedLivenessPollIntervalMs)
? Math.max(1, Math.floor(requestedLivenessPollIntervalMs))
: DEFAULT_EFFECT_WORKER_LIVENESS_POLL_INTERVAL_MS;
// Post-commit notifications are the low-latency path. `availableAt` is the
// durable retry schedule, and the long liveness poll only recovers from a
// missed in-process notification or work inserted by another process.
const runWorker = Effect.gen(function* () {
while (true) {
const outcome = yield* worker.runOnce.pipe(
Effect.map((worked) => (worked ? ("worked" as const) : ("idle" as const))),
Effect.catchCause((cause) =>
Effect.logWarning("Orchestration effect worker failed", cause).pipe(
Effect.as("failed" as const),
),
),
);
if (outcome === "worked") {
yield* Effect.yieldNow;
continue;
}
if (outcome === "failed") {
// A due row can remain visible when a claim UPDATE fails. Do not
// feed that past deadline back into the scheduler and retry at the
// one-millisecond floor; let transient database failures cool off.
yield* Effect.sleep(Duration.millis(Math.min(1_000, livenessPollIntervalMs)));
continue;
}
const nextClaimableAt = yield* worker.nextClaimableAt.pipe(
Effect.catchCause((cause) =>
Effect.logWarning(
"Failed to read the next orchestration effect deadline",
cause,
).pipe(Effect.as(Option.none<DateTime.Utc>())),
),
);
const now = DateTime.toEpochMillis(yield* DateTime.now);
const sleepMs = Option.match(nextClaimableAt, {
onNone: () => livenessPollIntervalMs,
onSome: (availableAt) => {
const untilAvailable = DateTime.toEpochMillis(availableAt) - now;
return Math.min(livenessPollIntervalMs, untilAvailable > 0 ? untilAvailable : 25);
},
});
yield* Effect.raceFirst(
worker.awaitWork.pipe(Effect.as("notified" as const)),
Effect.sleep(Duration.millis(sleepMs)).pipe(Effect.as("scheduled" as const)),
);
}
});
return yield* Effect.all(
Array.from({ length: concurrency }, () => runWorker),
{
concurrency: "unbounded",
discard: true,
},
);
}),
);
export const runDaemon = runDaemonWithOptions();
const daemonLayer: Layer.Layer<never, never, OrchestrationEffectWorkerV2> = Layer.effectDiscard(
runDaemon.pipe(Effect.forkScoped),
);