Skip to content
TypeScript

fix(server): bound hung delegated stop effects so later runs can start (#25675)

Julius Marminge36d4d711b79c
diff --git a/apps/server/src/orchestration-v2/EffectWorker.ts b/apps/server/src/orchestration-v2/EffectWorker.ts
index 6c0feea3526..493874a87bc 100644
--- a/apps/server/src/orchestration-v2/EffectWorker.ts
+++ b/apps/server/src/orchestration-v2/EffectWorker.ts
@@ -562,6 +562,29 @@ export const layerExecutor: Layer.Layer<
}),
);
+// A running effect blocks later work on its thread and its lease is never reclaimed in-process,
+// so a stop that hangs must give up and fall through to the normal retry/fail path.
+const DELEGATED_TASKS_STOP_TIMEOUT = Duration.seconds(30);
+
+const boundExecution =
+ (effect: EffectOutbox.OrchestrationEffectV2) =>
+ (execution: Effect.Effect<void, OrchestrationEffectExecutionError>) =>
+ effect.request.type === "delegated-tasks.stop"
+ ? execution.pipe(
+ Effect.timeoutOrElse({
+ duration: DELEGATED_TASKS_STOP_TIMEOUT,
+ orElse: () =>
+ Effect.fail(
+ new OrchestrationEffectExecutionError({
+ effectId: effect.id,
+ effectType: effect.request.type,
+ cause: `Timed out after ${Duration.format(DELEGATED_TASKS_STOP_TIMEOUT)}.`,
+ }),
+ ),
+ }),
+ )
+ : execution;
+
export class OrchestrationEffectWorkerError extends Schema.TaggedError<OrchestrationEffectWorkerError>()(
"OrchestrationEffectWorkerError",
{
@@ -747,7 +770,7 @@ export const layerWithOptions = (
const execution = executor
.execute(effect, { willRetry: effect.attemptCount < maxAttempts })
- .pipe(Effect.as("executed" as const));
+ .pipe(boundExecution(effect), Effect.as("executed" as const));
const exit = yield* Effect.exit(Effect.raceFirst(execution, cancellation)).pipe(
Effect.ensuring(outbox.clearCancellation(effect.id)),
);
diff --git a/apps/server/src/orchestration-v2/FoundationPersistence.test.ts b/apps/server/src/orchestration-v2/FoundationPersistence.test.ts
index b21f76b3244..f025b34e676 100644
--- a/apps/server/src/orchestration-v2/FoundationPersistence.test.ts
+++ b/apps/server/src/orchestration-v2/FoundationPersistence.test.ts
@@ -2594,6 +2594,94 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
}),
);
+ it.effect.each([{ replayed: false }, { replayed: true }] as const)(
+ "starts later thread work behind a delegated-task stop that never returns (replayed: $replayed)",
+ ({ replayed }) =>
+ Effect.gen(function* () {
+ const outbox = yield* EffectOutbox.EffectOutboxV2;
+ const key = `foundation-hung-delegated-stop:${replayed}`;
+ const threadId = ThreadId.make(`thread:${key}`);
+ const stopEffectId = `effect:${key}:stop`;
+ const stopStarted = yield* Deferred.make<void>();
+ const turnStarted = yield* Deferred.make<void>();
+ yield* outbox.enqueue([
+ {
+ id: stopEffectId,
+ commandId: CommandId.make(`command:${key}:stop`),
+ threadId,
+ request: { type: "delegated-tasks.stop" },
+ },
+ ]);
+ if (replayed) {
+ assert.isTrue(
+ Option.isSome(
+ yield* outbox.claimNext({ workerId: "crashed-worker", leaseDurationMs: 30_000 }),
+ ),
+ );
+ assert.equal((yield* outbox.reconcileAfterProcessLoss).requeued, 1);
+ }
+
+ const layerExecutor = Layer.succeed(
+ EffectWorker.OrchestrationEffectExecutorV2,
+ EffectWorker.OrchestrationEffectExecutorV2.of({
+ execute: (effect) =>
+ effect.request.type === "delegated-tasks.stop"
+ ? Deferred.succeed(stopStarted, undefined).pipe(Effect.andThen(Effect.never))
+ : Deferred.succeed(turnStarted, undefined),
+ }),
+ );
+ const layerWorker = EffectWorker.layerWithOptions({
+ workerId: "hung-delegated-stop-worker",
+ }).pipe(
+ Layer.provide(
+ Layer.merge(Layer.succeed(EffectOutbox.EffectOutboxV2, outbox), layerExecutor),
+ ),
+ );
+
+ yield* Effect.gen(function* () {
+ yield* EffectWorker.runDaemonWithOptions({ concurrency: 2 }).pipe(Effect.forkScoped);
+ yield* Deferred.await(stopStarted);
+ yield* outbox.enqueue([
+ {
+ id: `effect:${key}:turn`,
+ commandId: CommandId.make(`command:${key}:turn`),
+ threadId,
+ request: { type: "provider-turn.start", runId: RunId.make(`run:${key}`) },
+ },
+ ]);
+ yield* outbox.notifyAvailable();
+
+ const turn = yield* Deferred.await(turnStarted).pipe(
+ Effect.timeoutOption("1 hour"),
+ Effect.forkChild,
+ );
+ yield* TestClock.adjust("1 hour");
+ assert.isTrue(
+ Option.isSome(yield* Fiber.join(turn)),
+ "provider-turn.start never ran behind the hung delegated-tasks.stop",
+ );
+ const stop = yield* outbox.get(stopEffectId);
+ assert.isTrue(Option.isSome(stop));
+ if (Option.isSome(stop)) {
+ assert.equal(stop.value.status, "failed");
+ assert.include(stop.value.lastError ?? "", "Timed out");
+ }
+ }).pipe(
+ Effect.provide(layerWorker),
+ Effect.scoped,
+ Effect.ensuring(
+ outbox
+ .cancelUnsettled({
+ threadId,
+ effectTypes: ["delegated-tasks.stop", "provider-turn.start"],
+ reason: "fixture complete",
+ })
+ .pipe(Effect.orDie),
+ ),
+ );
+ }),
+ );
+
it.effect(
"persists shutdown continuation intent through the real event sink without domain events",
() =>