Skip to content

Commit 862fdc9

Browse files
committed
fix(server): reject sends during auth-failure session teardown
Addresses a review finding: after close() on an auth-failure teardown, the context stayed in `sessions` until the async stream exit ran stopSessionInternal, so a sendTurn in that window passed requireSession and enqueued a prompt onto the closed query. Mark the session closed right after close() so requireSession rejects sends, and make hasSession treat a closed status as not active so a recovery can replace the dying process promptly.
1 parent dee6e42 commit 862fdc9

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎apps/server/src/provider/Layers/ClaudeAdapter.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ import {
4343
SYNTHETIC_CLAUDE_STANDARD_MODEL,
4444
SYNTHETIC_CLAUDE_THINKING_MODEL,
4545
} from "../ClaudeModelCatalog.testFixtures.ts";
46-
import { ProviderAdapterProcessError, ProviderAdapterValidationError } from "../Errors.ts";
46+
import {
47+
ProviderAdapterProcessError,
48+
ProviderAdapterSessionNotFoundError,
49+
ProviderAdapterValidationError,
50+
} from "../Errors.ts";
4751
import type { ClaudeAdapterShape } from "../Services/ClaudeAdapter.ts";
4852
import type { ClaudeScopedLimitNames } from "./claudeUsageLimits.ts";
4953
import { makeClaudeAdapter, type ClaudeAdapterLiveOptions } from "./ClaudeAdapter.ts";
@@ -5683,6 +5687,18 @@ describe("ClaudeAdapterLive", () => {
56835687
const hasSession = yield* adapter.hasSession(session.threadId);
56845688
assert.equal(hasSession, false);
56855689
assert.equal(harness.query.closeCalls > 0, true);
5690+
5691+
// No prompt may be accepted onto the dying runtime: a follow-up turn
5692+
// after the teardown must fail (session no longer routable) rather than
5693+
// enqueue onto the closed query.
5694+
const followUp = yield* adapter
5695+
.sendTurn({
5696+
threadId: session.threadId,
5697+
input: "hello again",
5698+
attachments: [],
5699+
})
5700+
.pipe(Effect.flip);
5701+
assert.instanceOf(followUp, ProviderAdapterSessionNotFoundError);
56865702
}).pipe(
56875703
Effect.provideService(Random.Random, makeDeterministicRandomService()),
56885704
Effect.provide(harness.layer),

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3136,6 +3136,19 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
31363136
cause,
31373137
}),
31383138
});
3139+
3140+
// The stream teardown (handleStreamExit -> stopSessionInternal) runs
3141+
// asynchronously after close(), so until it completes the context is
3142+
// still in `sessions` with `stopped` unset. Mark the session closed now
3143+
// so sendTurn rejects (via requireSession) instead of enqueueing a
3144+
// prompt onto the dying runtime.
3145+
const closedAt = yield* nowIso;
3146+
context.session = {
3147+
...context.session,
3148+
status: "closed",
3149+
activeTurnId: undefined,
3150+
updatedAt: closedAt,
3151+
};
31393152
}
31403153
});
31413154

@@ -4858,7 +4871,12 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
48584871
const hasSession: ClaudeAdapterShape["hasSession"] = (threadId) =>
48594872
Effect.sync(() => {
48604873
const context = sessions.get(threadId);
4861-
return context !== undefined && !context.stopped;
4874+
// A stopped or already-closed context is not a routable session. The
4875+
// closed check matters because an auth-failure teardown marks the
4876+
// session closed (see handleResultMessage) before the async stream exit
4877+
// removes the context, so a recovery must be able to replace it in that
4878+
// window instead of treating the dying process as active.
4879+
return context !== undefined && !context.stopped && context.session.status !== "closed";
48624880
});
48634881

48644882
const stopSessions = Effect.fn("stopSessions")(function* (

0 commit comments

Comments
 (0)