Skip to content

Commit 989898a

Browse files
committed
fix(claude): tear down a rejected resume from its own stream
handleStreamExit left cleanup to the startSession retry, which never runs when startSession is interrupted during the resume wait: the detached stream fiber then marked the session rejected without removing it, so hasSession stayed true and the next turn reused the dead query. The rejected session now closes itself, and the retry signal comes from the attempt rather than the session map it no longer appears in.
1 parent ad6d7e7 commit 989898a

2 files changed

Lines changed: 69 additions & 9 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4315,6 +4315,55 @@ describe("ClaudeAdapterLive", () => {
43154315
);
43164316
});
43174317

4318+
it.effect("tears down a rejected resume even when startSession is interrupted", () => {
4319+
const deadSessionId = "550e8400-e29b-41d4-a716-446655440000";
4320+
const harness = makeHarness();
4321+
return Effect.gen(function* () {
4322+
const adapter = yield* ClaudeAdapter;
4323+
4324+
// session.started is the receipt that the query exists and its stream is
4325+
// forked, so the interrupt below lands inside the resume wait.
4326+
const started = yield* Stream.take(adapter.streamEvents, 1).pipe(
4327+
Stream.runCollect,
4328+
Effect.forkChild,
4329+
);
4330+
const starting = yield* adapter
4331+
.startSession({
4332+
threadId: RESUME_THREAD_ID,
4333+
provider: ProviderDriverKind.make("claudeAgent"),
4334+
resumeCursor: { threadId: RESUME_THREAD_ID, resume: deadSessionId, turnCount: 0 },
4335+
runtimeMode: "full-access",
4336+
})
4337+
.pipe(Effect.forkChild);
4338+
yield* Fiber.join(started);
4339+
assert.equal(harness.getCreateQueryInputs().length, 1);
4340+
4341+
// The stream fiber is detached, so it must clean up on its own: nothing
4342+
// is waiting on the retry any more.
4343+
yield* Fiber.interrupt(starting);
4344+
4345+
harness.query.emit({
4346+
type: "result",
4347+
subtype: "error_during_execution",
4348+
is_error: true,
4349+
num_turns: 0,
4350+
session_id: deadSessionId,
4351+
errors: [`No conversation found with session ID: ${deadSessionId}`],
4352+
uuid: "resume-rejected",
4353+
} as unknown as SDKMessage);
4354+
harness.query.finish();
4355+
yield* Effect.yieldNow;
4356+
yield* Effect.yieldNow;
4357+
yield* Effect.yieldNow;
4358+
4359+
assert.equal(harness.query.closeCalls, 1);
4360+
assert.equal(yield* adapter.hasSession(RESUME_THREAD_ID), false);
4361+
}).pipe(
4362+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
4363+
Effect.provide(harness.layer),
4364+
);
4365+
});
4366+
43184367
it.effect("fails a resume start whose session dies before it says anything", () => {
43194368
const harness = makeHarness();
43204369
return Effect.gen(function* () {

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3732,7 +3732,15 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
37323732
context: ClaudeSessionContext,
37333733
exit: Exit.Exit<void, ProviderAdapterProcessError>,
37343734
) {
3735-
if (context.stopped || context.resumeRejected) {
3735+
if (context.stopped) {
3736+
return;
3737+
}
3738+
3739+
// A rejected resume belongs to startSession's retry, not to the thread, so
3740+
// its exit stays silent. Tear it down here rather than leaving that to the
3741+
// retry, which never runs if startSession was interrupted while waiting.
3742+
if (context.resumeRejected) {
3743+
yield* stopSessionInternal(context, { emitExitEvent: false });
37363744
return;
37373745
}
37383746

@@ -3901,8 +3909,9 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
39013909
return Effect.succeed(context);
39023910
};
39033911

3904-
const startSessionAttempt: ClaudeAdapterShape["startSession"] = Effect.fn("startSessionAttempt")(
3905-
function* (input) {
3912+
const startSessionAttempt = Effect.fn("startSessionAttempt")(function* (
3913+
input: Parameters<ClaudeAdapterShape["startSession"]>[0],
3914+
) {
39063915
const modelCatalog = yield* modelCatalogEffect;
39073916
if (input.provider !== undefined && input.provider !== PROVIDER) {
39083917
return yield* new ProviderAdapterValidationError({
@@ -4618,27 +4627,29 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
46184627
}
46194628

46204629
return {
4621-
...session,
4630+
session: { ...session },
4631+
// Read from the context, not the session map: a rejected session tears
4632+
// itself down, so by now it may already be gone from the map.
4633+
resumeRejected: context.resumeRejected,
46224634
};
4623-
},
4624-
);
4635+
});
46254636

46264637
/**
46274638
* A rejected resume is definitive: retrying the id would leave the thread
46284639
* unable to ever start another turn.
46294640
*/
46304641
const startSession: ClaudeAdapterShape["startSession"] = Effect.fn("startSession")(
46314642
function* (input) {
4632-
const session = yield* startSessionAttempt(input);
4633-
if (sessions.get(input.threadId)?.resumeRejected !== true) {
4643+
const { session, resumeRejected } = yield* startSessionAttempt(input);
4644+
if (!resumeRejected) {
46344645
return session;
46354646
}
46364647
yield* Effect.logWarning("claude.session.resume-rejected", {
46374648
threadId: input.threadId,
46384649
reason: "Claude holds no conversation for the resume id; starting a fresh session",
46394650
});
46404651
const { resumeCursor: _unresumable, ...withoutResumeCursor } = input;
4641-
return yield* startSessionAttempt(withoutResumeCursor);
4652+
return (yield* startSessionAttempt(withoutResumeCursor)).session;
46424653
},
46434654
);
46444655

0 commit comments

Comments
 (0)