fix(server): retain Claude session after close failure
This commit is contained in:
2 files changed
+34
-22
No files matched your search
@@ -58,6 +58,7 @@ class FakeClaudeQuery implements AsyncIterable<SDKMessage> {
|
||||
public readonly setPermissionModeCalls: Array<string> = [];
|
||||
public readonly setMaxThinkingTokensCalls: Array<number | null> = [];
|
||||
public closeCalls = 0;
|
||||
public closeError: unknown | undefined;
|
||||
|
||||
emit(message: SDKMessage): void {
|
||||
if (this.done) {
|
||||
@@ -107,6 +108,9 @@ class FakeClaudeQuery implements AsyncIterable<SDKMessage> {
|
||||
|
||||
readonly close = (): void => {
|
||||
this.closeCalls += 1;
|
||||
if (this.closeError !== undefined) {
|
||||
throw this.closeError;
|
||||
}
|
||||
this.finish();
|
||||
};
|
||||
|
||||
@@ -1658,6 +1662,32 @@ describe("ClaudeAdapterLive", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("keeps the session available when process close fails", () => {
|
||||
const harness = makeHarness();
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* ClaudeAdapter;
|
||||
const session = yield* adapter.startSession({
|
||||
threadId: THREAD_ID,
|
||||
provider: ProviderDriverKind.make("claudeAgent"),
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
harness.query.closeError = new Error("close failed");
|
||||
|
||||
const result = yield* adapter.interruptTurn(session.threadId).pipe(Effect.result);
|
||||
|
||||
assert.equal(result._tag, "Failure");
|
||||
if (result._tag === "Failure") {
|
||||
assert.equal(result.failure._tag, "ProviderAdapterProcessError");
|
||||
}
|
||||
assert.equal(harness.query.closeCalls, 1);
|
||||
assert.equal(yield* adapter.hasSession(session.threadId), true);
|
||||
assert.equal((yield* adapter.listSessions())[0]?.status, "ready");
|
||||
}).pipe(
|
||||
Effect.provideService(Random.Random, makeDeterministicRandomService()),
|
||||
Effect.provide(harness.layer),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("keeps a resumed replacement session during slow stop cleanup", () => {
|
||||
const queries: FakeClaudeQuery[] = [];
|
||||
let signalUsageStarted: () => void = () => undefined;
|
||||
|
||||
@@ -3595,8 +3595,6 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
) {
|
||||
if (context.stopped) return;
|
||||
|
||||
context.stopped = true;
|
||||
|
||||
// Schedule process termination before any cleanup that can wait on the
|
||||
// provider. The SDK closes stdin, then escalates from SIGTERM to SIGKILL.
|
||||
yield* Effect.try({
|
||||
@@ -3608,16 +3606,9 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
detail: "Failed to close Claude runtime query.",
|
||||
cause,
|
||||
}),
|
||||
}).pipe(
|
||||
Effect.catch((error) =>
|
||||
emitRuntimeError(context, "Failed to close Claude runtime query.", {
|
||||
errorTag: error._tag,
|
||||
provider: error.provider,
|
||||
threadId: error.threadId,
|
||||
detail: error.detail,
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
context.stopped = true;
|
||||
|
||||
for (const taskId of Array.from(context.liveTaskIds)) {
|
||||
if (!context.liveTaskIds.delete(taskId)) {
|
||||
@@ -3743,16 +3734,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
});
|
||||
yield* stopSessionInternal(existingContext, {
|
||||
emitExitEvent: false,
|
||||
}).pipe(
|
||||
// Replacement cleanup is best-effort: never block the new session on
|
||||
// either typed failures or unexpected defects from tearing down the old one.
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.logWarning("claude.session.replace.stop-failed", {
|
||||
threadId: input.threadId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const startedAt = yield* nowIso;
|
||||
|
||||
Reference in new issue
Block a user