Skip to content

Commit 44c06ee

Browse files
committed
fix(server): retain awaited completions across query replacement
1 parent 2ae782e commit 44c06ee

2 files changed

Lines changed: 89 additions & 68 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 87 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10562,83 +10562,103 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1056210562
),
1056310563
);
1056410564

10565-
it.effect("preserves settled background work until an explicit stop", () =>
10566-
Effect.scoped(
10567-
Effect.gen(function* () {
10568-
let closes = 0;
10569-
const harness = yield* makeWakeHarnessWithOptions({
10570-
close: (sdkMessages) =>
10571-
Effect.sync(() => {
10572-
closes++;
10573-
}).pipe(Effect.andThen(Queue.shutdown(sdkMessages))),
10574-
});
10575-
const now = yield* DateTime.now;
10576-
const attemptId = RunAttemptId.make("attempt-claude-settled-stop");
10577-
yield* harness.runtime.startTurn(
10578-
makeClaudeTestTurnInput({
10579-
threadId: harness.threadId,
10580-
providerThread: harness.providerThread,
10581-
now,
10582-
attemptId,
10583-
text: "Run the build in the background.",
10584-
attachments: [],
10585-
}),
10586-
);
10587-
yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
10588-
yield* Queue.offer(harness.sdkMessages, turnOneResult);
10589-
yield* harness.awaitTerminal(1);
10590-
const settledThread = providerThreadRosterEvents(harness.events).at(-1)?.providerThread;
10591-
assert.equal(settledThread?.pendingBackgroundTasks?.[0]?.taskId, WAKE_TASK_ID);
10592-
assert.isTrue(yield* harness.hasPendingBackgroundWork);
10593-
10594-
const replacementAttempt = yield* harness.runtime
10595-
.startTurn(
10565+
it.effect.each(["running", "completed-awaited"] as const)(
10566+
"preserves settled background work until an explicit stop: %s",
10567+
(taskState) =>
10568+
Effect.scoped(
10569+
Effect.gen(function* () {
10570+
let closes = 0;
10571+
const harness = yield* makeWakeHarnessWithOptions({
10572+
close: (sdkMessages) =>
10573+
Effect.sync(() => {
10574+
closes++;
10575+
}).pipe(Effect.andThen(Queue.shutdown(sdkMessages))),
10576+
});
10577+
const now = yield* DateTime.now;
10578+
const attemptId = RunAttemptId.make("attempt-claude-settled-stop");
10579+
yield* harness.runtime.startTurn(
1059610580
makeClaudeTestTurnInput({
1059710581
threadId: harness.threadId,
10598-
providerThread: {
10599-
...harness.providerThread,
10600-
nativeThreadRef: {
10601-
...harness.providerThread.nativeThreadRef!,
10602-
nativeId: "replacement-session",
10603-
},
10604-
},
10582+
providerThread: harness.providerThread,
1060510583
now,
10606-
attemptId: RunAttemptId.make("attempt-claude-background-replacement"),
10607-
text: "Continue in a fresh session.",
10584+
attemptId,
10585+
text: "Run the build in the background.",
1060810586
attachments: [],
10609-
providerTurnOrdinal: 2,
1061010587
}),
10611-
)
10612-
.pipe(Effect.result);
10613-
assert.equal(replacementAttempt._tag, "Failure");
10614-
assert.equal(closes, 0);
10615-
assert.isTrue(yield* harness.hasPendingBackgroundWork);
10588+
);
10589+
yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
10590+
yield* Queue.offer(harness.sdkMessages, turnOneResult);
10591+
yield* harness.awaitTerminal(1);
10592+
const settledThread = providerThreadRosterEvents(harness.events).at(-1)?.providerThread;
10593+
assert.equal(settledThread?.pendingBackgroundTasks?.[0]?.taskId, WAKE_TASK_ID);
10594+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
1061610595

10617-
yield* harness.runtime.interruptTurn({
10618-
providerThread: settledThread ?? harness.providerThread,
10619-
providerTurnId: harness.terminalEvents()[0]!.providerTurnId,
10620-
requestRuntimeRestart: true,
10621-
});
10596+
if (taskState === "completed-awaited") {
10597+
if (harness.runtime.awaitBackgroundTasks === undefined) {
10598+
return yield* Effect.die("Claude must expose background task awaiting.");
10599+
}
10600+
yield* harness.runtime.awaitBackgroundTasks({
10601+
providerThread: harness.providerThread,
10602+
taskIds: [WAKE_TASK_ID],
10603+
awaited: true,
10604+
});
10605+
yield* harness.offerAndWait(wakeNotification);
10606+
assert.deepStrictEqual(
10607+
providerThreadRosterEvents(harness.events)
10608+
.at(-1)
10609+
?.providerThread.pendingBackgroundTasks?.map((task) => [task.taskId, task.awaited]),
10610+
[[WAKE_TASK_ID, true]],
10611+
);
10612+
}
1062210613

10623-
assert.equal(closes, 1, "Stop must close the CLI process that owns the task");
10624-
yield* harness.until(
10625-
() =>
10626-
(providerThreadRosterEvents(harness.events).at(-1)?.providerThread
10627-
.pendingBackgroundTasks?.length ?? 0) === 0,
10628-
);
10629-
assert.isFalse(yield* harness.hasPendingBackgroundWork);
10630-
assert.lengthOf(harness.continuationRequests, 0);
10631-
}).pipe(
10632-
Effect.provide(
10633-
Layer.mergeAll(
10634-
IdAllocator.layer,
10635-
McpProviderSessions.layer,
10636-
NodeServices.layer,
10637-
NodeCrypto.layer,
10614+
const replacementAttempt = yield* harness.runtime
10615+
.startTurn(
10616+
makeClaudeTestTurnInput({
10617+
threadId: harness.threadId,
10618+
providerThread: {
10619+
...harness.providerThread,
10620+
nativeThreadRef: {
10621+
...harness.providerThread.nativeThreadRef!,
10622+
nativeId: "replacement-session",
10623+
},
10624+
},
10625+
now,
10626+
attemptId: RunAttemptId.make("attempt-claude-background-replacement"),
10627+
text: "Continue in a fresh session.",
10628+
attachments: [],
10629+
providerTurnOrdinal: 2,
10630+
}),
10631+
)
10632+
.pipe(Effect.result);
10633+
assert.equal(replacementAttempt._tag, "Failure");
10634+
assert.equal(closes, 0);
10635+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
10636+
10637+
yield* harness.runtime.interruptTurn({
10638+
providerThread: settledThread ?? harness.providerThread,
10639+
providerTurnId: harness.terminalEvents()[0]!.providerTurnId,
10640+
requestRuntimeRestart: true,
10641+
});
10642+
10643+
assert.equal(closes, 1, "Stop must close the CLI process that owns the task");
10644+
yield* harness.until(
10645+
() =>
10646+
(providerThreadRosterEvents(harness.events).at(-1)?.providerThread
10647+
.pendingBackgroundTasks?.length ?? 0) === 0,
10648+
);
10649+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
10650+
assert.lengthOf(harness.continuationRequests, 0);
10651+
}).pipe(
10652+
Effect.provide(
10653+
Layer.mergeAll(
10654+
IdAllocator.layer,
10655+
McpProviderSessions.layer,
10656+
NodeServices.layer,
10657+
NodeCrypto.layer,
10658+
),
1063810659
),
1063910660
),
1064010661
),
10641-
),
1064210662
);
1064310663

1064410664
it.effect("a settled Stop leaves a turn that replaced the closing process alone", () =>

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10954,7 +10954,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1095410954
(existing.nativeThreadId === nativeThreadId ||
1095510955
existing.lastTurn?.input.threadId === turnInput.threadId) &&
1095610956
!existing.stopping &&
10957-
(yield* liveProcessRunsBackgroundWork(existing))
10957+
((yield* liveProcessRunsBackgroundWork(existing)) ||
10958+
(awaitedCompletionsByNativeThread.get(existing.nativeThreadId)?.size ?? 0) > 0)
1095810959
) {
1095910960
return yield* new ClaudeBackgroundWorkBlocksQueryReplacementError();
1096010961
}

0 commit comments

Comments
 (0)