Skip to content

Commit 9370a1d

Browse files
KonstantinKoclaude
authored andcommitted
fix(server): keep reaping other providers when the live session lookup fails
`listSessions` dies on a binding mismatch, which aborted every sweep and stopped all reaping. An unknown live set now keeps Claude sessions and lets the sweep continue for everything else. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent a74e276 commit 9370a1d

2 files changed

Lines changed: 80 additions & 6 deletions

File tree

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ describe("ProviderSessionReaper", () => {
177177
async function createHarness(input: {
178178
readonly readModel: ReturnType<typeof makeReadModel>;
179179
readonly activeSessions?: ReadonlyArray<ProviderSession>;
180+
readonly listSessionsImplementation?: () => ReturnType<ProviderServiceShape["listSessions"]>;
180181
readonly stopSessionImplementation?: (input: {
181182
readonly threadId: ThreadId;
182183
}) => ReturnType<ProviderServiceShape["stopSession"]>;
@@ -199,7 +200,8 @@ describe("ProviderSessionReaper", () => {
199200
respondToRequest: () => unsupported(),
200201
respondToUserInput: () => unsupported(),
201202
stopSession,
202-
listSessions: () => Effect.succeed(input.activeSessions ?? []),
203+
listSessions:
204+
input.listSessionsImplementation ?? (() => Effect.succeed(input.activeSessions ?? [])),
203205
getCapabilities: () => Effect.succeed({ sessionModelSwitch: "in-session" }),
204206
assertConversationRollbackSupported: () => unsupported(),
205207
getInstanceInfo: (instanceId) => {
@@ -381,6 +383,63 @@ describe("ProviderSessionReaper", () => {
381383
},
382384
);
383385

386+
it("keeps reaping other providers when the live session lookup fails", async () => {
387+
const claudeThreadId = ThreadId.make("thread-reaper-lookup-failed-claude");
388+
const codexThreadId = ThreadId.make("thread-reaper-lookup-failed-codex");
389+
const now = "2026-01-01T00:00:00.000Z";
390+
const lastSeenAt = "2026-04-14T00:00:00.000Z";
391+
const harness = await createHarness({
392+
listSessionsImplementation: () => Effect.die(new Error("binding mismatch")),
393+
readModel: makeReadModel(
394+
(
395+
[
396+
[claudeThreadId, "claudeAgent"],
397+
[codexThreadId, "codex"],
398+
] as const
399+
).map(([threadId, providerName]) => ({
400+
id: threadId,
401+
session: {
402+
threadId,
403+
status: "ready",
404+
providerName,
405+
runtimeMode: "full-access",
406+
activeTurnId: null,
407+
lastError: null,
408+
updatedAt: now,
409+
},
410+
})),
411+
),
412+
});
413+
const repository = await runtime!.runPromise(
414+
Effect.service(ProviderSessionRuntime.ProviderSessionRuntimeRepository),
415+
);
416+
417+
for (const [threadId, providerName] of [
418+
[claudeThreadId, "claudeAgent"],
419+
[codexThreadId, "codex"],
420+
] as const) {
421+
await runtime!.runPromise(
422+
repository.upsert({
423+
threadId,
424+
providerName,
425+
providerInstanceId: null,
426+
adapterKey: providerName,
427+
runtimeMode: "full-access",
428+
status: "running",
429+
lastSeenAt,
430+
resumeCursor: null,
431+
runtimePayload: null,
432+
}),
433+
);
434+
}
435+
436+
await sweepAt(Date.parse(lastSeenAt) + 1_000);
437+
438+
expect(harness.stopSession.mock.calls.map(([request]) => request)).toEqual([
439+
{ threadId: codexThreadId },
440+
]);
441+
});
442+
384443
it("skips stale sessions when the thread still has an active turn", async () => {
385444
const threadId = ThreadId.make("thread-reaper-active-turn");
386445
const turnId = TurnId.make("turn-reaper-active");

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,22 @@ const makeProviderSessionReaper = (options?: ProviderSessionReaperLiveOptions) =
3838
// Stopped rows stay for their resume cursors and far outnumber live
3939
// ones, so the query skips them.
4040
const bindings = yield* directory.listBindings({ excludeStopped: true });
41-
const liveClaudeThreadIds = new Set(
42-
(yield* providerService.listSessions())
43-
.filter((session) => session.provider === "claudeAgent")
44-
.map((session) => session.threadId),
41+
// An unknown live set keeps every Claude session instead of aborting the
42+
// sweep, so other providers are still reaped.
43+
const liveClaudeThreadIds = yield* providerService.listSessions().pipe(
44+
Effect.map(
45+
(sessions) =>
46+
new Set(
47+
sessions
48+
.filter((session) => session.provider === "claudeAgent")
49+
.map((session) => session.threadId),
50+
),
51+
),
52+
Effect.catchCause((cause) =>
53+
Effect.logWarning("provider.session.reaper.live-sessions-failed", { cause }).pipe(
54+
Effect.as(undefined),
55+
),
56+
),
4557
);
4658
const now = yield* Clock.currentTimeMillis;
4759
let reapedCount = 0;
@@ -99,7 +111,10 @@ const makeProviderSessionReaper = (options?: ProviderSessionReaperLiveOptions) =
99111

100112
// Claude's process is also its cross-session messaging endpoint, so
101113
// a live idle session must remain addressable by its peers.
102-
if (binding.provider === "claudeAgent" && liveClaudeThreadIds.has(binding.threadId)) {
114+
if (
115+
binding.provider === "claudeAgent" &&
116+
(liveClaudeThreadIds === undefined || liveClaudeThreadIds.has(binding.threadId))
117+
) {
103118
yield* Effect.logDebug("provider.session.reaper.skipped-messaging-endpoint", {
104119
threadId: binding.threadId,
105120
provider: binding.provider,

0 commit comments

Comments
 (0)