Skip to content

Commit 5bdf0e7

Browse files
committed
fix(server): retain background shells during Claude login recovery
1 parent 0ae4dcd commit 5bdf0e7

2 files changed

Lines changed: 50 additions & 9 deletions

File tree

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4024,20 +4024,41 @@ describe("ClaudeAdapterV2 background wake turns", () => {
40244024
});
40254025

40264026
it.effect.each([
4027-
{ failure: "authentication_failed", apiErrorStatus: undefined, opensFreshProcess: true },
4028-
{ failure: "server_error", apiErrorStatus: undefined, opensFreshProcess: false },
4029-
{ failure: "authentication_failed", apiErrorStatus: 403, opensFreshProcess: false },
4027+
{
4028+
failure: "authentication_failed",
4029+
apiErrorStatus: undefined,
4030+
opensFreshProcess: true,
4031+
backgroundShell: false,
4032+
},
4033+
{
4034+
failure: "authentication_failed",
4035+
apiErrorStatus: undefined,
4036+
opensFreshProcess: true,
4037+
backgroundShell: true,
4038+
},
4039+
{
4040+
failure: "server_error",
4041+
apiErrorStatus: undefined,
4042+
opensFreshProcess: false,
4043+
backgroundShell: false,
4044+
},
4045+
{
4046+
failure: "authentication_failed",
4047+
apiErrorStatus: 403,
4048+
opensFreshProcess: false,
4049+
backgroundShell: false,
4050+
},
40304051
] as const)(
4031-
"after a turn fails with $failure (API status: $apiErrorStatus), the next turn opens a fresh process: $opensFreshProcess",
4032-
({ failure, apiErrorStatus, opensFreshProcess }) =>
4052+
"after a turn fails with $failure (API status: $apiErrorStatus), the next turn opens a fresh process: $opensFreshProcess (background shell: $backgroundShell)",
4053+
({ failure, apiErrorStatus, opensFreshProcess, backgroundShell }) =>
40334054
Effect.gen(function* () {
40344055
let closeCount = 0;
40354056
const harness = yield* makeWakeHarnessWithOptions({
40364057
freshQueueOnReopen: true,
4037-
close: () =>
4058+
close: (messages) =>
40384059
Effect.sync(() => {
40394060
closeCount += 1;
4040-
}),
4061+
}).pipe(Effect.andThen(Queue.shutdown(messages))),
40414062
});
40424063
const now = yield* DateTime.now;
40434064
const startTurn = (attempt: string, text: string) =>
@@ -4053,6 +4074,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
40534074
);
40544075

40554076
yield* startTurn("attempt-claude-relogin-1", "Continue.");
4077+
if (backgroundShell) yield* harness.offerAndWait(wakeTaskStarted);
40564078
yield* Queue.offerAll(harness.sdkMessages, [
40574079
makeAssistantErrorFrame({ uuid: "00000000-0000-4000-8000-000000000611", error: failure }),
40584080
makeResultFrame({
@@ -4066,6 +4088,25 @@ describe("ClaudeAdapterV2 background wake turns", () => {
40664088
const failed = yield* Queue.take(harness.terminalReceipts);
40674089
assert.equal(failed.status, "failed");
40684090

4091+
if (backgroundShell) {
4092+
const refused = yield* startTurn("attempt-claude-relogin-blocked", "Try again.").pipe(
4093+
Effect.result,
4094+
);
4095+
assert.equal(refused._tag, "Failure");
4096+
if (refused._tag === "Failure") {
4097+
assert.instanceOf(
4098+
refused.failure.cause,
4099+
ClaudeAdapterV2.ClaudeBackgroundWorkBlocksQueryReplacementError,
4100+
);
4101+
}
4102+
assert.lengthOf(harness.processQueues, 1);
4103+
assert.equal(closeCount, 0);
4104+
yield* harness.runtime.interruptTurn({
4105+
providerThread: harness.providerThread,
4106+
providerTurnId: failed.providerTurnId,
4107+
requestRuntimeRestart: true,
4108+
});
4109+
}
40694110
yield* startTurn("attempt-claude-relogin-2", "Try again.");
40704111
assert.equal(harness.processQueues.length, opensFreshProcess ? 2 : 1);
40714112
assert.equal(closeCount, opensFreshProcess ? 1 : 0);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7150,8 +7150,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
71507150
Effect.gen(function* () {
71517151
const nativeThreadId =
71527152
input.context.input.providerThread.nativeThreadRef?.nativeId ?? null;
7153+
const liveQuery = yield* Ref.get(queryContext);
71537154
if (nativeThreadId !== null) {
7154-
if (input.status !== "completed") {
7155+
if (input.status !== "completed" && liveQuery?.signedOut !== true) {
71557156
yield* clearPendingBackgroundTasksForNativeThread(nativeThreadId);
71567157
yield* clearNativeThreadTaskIdSet(
71577158
wakeEligibleBackgroundTasksByNativeThread,
@@ -7184,7 +7185,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
71847185
nativeThreadId === null ? undefined : goalsByNativeThread.get(nativeThreadId);
71857186
const goalChecked = goalCheckedTurns.delete(input.context.providerTurnId);
71867187
const terminalReason = input.result?.terminal_reason;
7187-
const liveQuery = yield* Ref.get(queryContext);
71887188
if (
71897189
nativeThreadId !== null &&
71907190
goal?.status === "active" &&

0 commit comments

Comments
 (0)