Compare commits

..
Author SHA1 Message Date
5a78c1cd16 Let idle cleanup release Codex chats opened only to read history (#9859)
A Codex session opened to read history reads the thread and disposes its
app-server right away, but canStopRuntime() vetoed it because it had no
client. Idle cleanup then kept every such chat loaded in the daemon.

Closes #9804

Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
Co-authored-by: Spencer Shulem <1703265+Spshulem@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 13:01:00 +00:00
paseo-bot[bot]anddaysun-k 8522d18ed8 Keep Claude signed in when model discovery runs during a token refresh (#9855)
* Let Claude model discovery finish a token refresh instead of killing it

Model discovery stopped waiting after 5 s by aborting the SDK query and
tree-killing the CLI. A CLI that is still refreshing an expired OAuth
token at that moment has already rotated the refresh token, so the kill
leaves only the revoked token on disk and logs the user out.

The 5 s limit now only bounds the wait. A CLI that has not answered gets
its input ended and exits on its own; it is tree-killed only after a
60 s grace period or when the daemon shuts down.

Fixes #9314

* fix(server): let Claude model discovery finish a token refresh after it answers

Claude Code answers the model list before its startup token refresh has
saved the rotated refresh token. Discovery killed the CLI right after the
answer, so a refresh still in flight left the revoked token on disk and
every later Claude session failed until /login. Discovery now ends the
CLI's input and lets it exit on its own whether or not it answered, with
the existing grace period and shutdown kill as the bound.

Co-authored-by: daysun-k <8309193+daysun-k@users.noreply.github.com>

---------

Co-authored-by: daysun-k <8309193+daysun-k@users.noreply.github.com>
Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
2026-11-16 12:04:46 +00:00
6 changed files with 244 additions and 50 deletions
@@ -6,7 +6,7 @@ import {
type ChildProcess,
type SpawnOptions,
} from "node:child_process";
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import net from "node:net";
import { tmpdir } from "node:os";
import path from "node:path";
@@ -59,17 +59,7 @@ export async function getAvailableHostDaemonPort(): Promise<number> {
return port;
}
/**
* Resolves once this daemon answers on its port. An open TCP port is not
* enough: the port is released before the daemon binds it, and whatever takes
* it in between also accepts connections while the daemon exits with
* EADDRINUSE. Only the daemon's own status reports its server id.
*/
async function waitForServer(
port: number,
child: ChildProcess,
identity: { serverId: string; paseoHome: string },
): Promise<void> {
async function waitForServer(port: number, child: ChildProcess): Promise<void> {
const deadline = Date.now() + 90_000;
let lastError: unknown = null;
@@ -80,13 +70,22 @@ async function waitForServer(
);
}
try {
const serverId = await readServerId(port, identity.paseoHome);
if (serverId === identity.serverId) return;
lastError = new Error(`Port ${port} is served by ${serverId}, not ${identity.serverId}`);
await new Promise<void>((resolve, reject) => {
const socket = net.connect(port, "127.0.0.1", () => {
socket.end();
resolve();
});
socket.setTimeout(1_000, () => {
socket.destroy();
reject(new Error(`Connection timed out to isolated daemon port ${port}`));
});
socket.on("error", reject);
});
return;
} catch (error) {
lastError = error;
await new Promise((resolve) => setTimeout(resolve, 100));
}
await new Promise((resolve) => setTimeout(resolve, 100));
}
throw new Error(
@@ -96,25 +95,6 @@ async function waitForServer(
);
}
/**
* Reads the server id from the daemon's status route. The route needs a bearer
* on a password-protected daemon; the daemon accepts the local credential it
* writes into its home before listening, as the CLI does for local status.
*/
async function readServerId(port: number, paseoHome: string): Promise<string> {
const localCredential = await readFile(path.join(paseoHome, "local-credential"), "utf8").then(
(token) => token.trim(),
() => null,
);
const response = await fetch(`http://127.0.0.1:${port}/api/status`, {
headers: localCredential ? { authorization: `Bearer ${localCredential}` } : {},
signal: AbortSignal.timeout(1_000),
});
if (!response.ok) throw new Error(`Status request to port ${port} returned ${response.status}`);
const status = (await response.json()) as { serverId?: unknown };
return String(status.serverId);
}
function withMutableRelay(
config: NonNullable<IsolatedHostDaemonOptions["config"]>,
relay: NonNullable<IsolatedHostDaemonOptions["mutableRelay"]>,
@@ -241,7 +221,7 @@ export async function startIsolatedHostDaemon(
});
try {
await waitForServer(port, spawned, { serverId, paseoHome });
await waitForServer(port, spawned);
return spawned;
} catch (error) {
throw new Error(
@@ -83,7 +83,7 @@ import {
type ClaudeProviderOptions,
} from "./options.js";
import { renderPromptAttachmentAsText } from "../../prompt-attachments.js";
import { discoverClaudeModels } from "./model-discovery.js";
import { discoverClaudeModels, killExitingClaudeModelDiscoveries } from "./model-discovery.js";
import { claudeQuery, type ClaudeOptions, type ClaudeQueryFactory } from "./query.js";
import {
findSteeredMessageParent,
@@ -1751,6 +1751,10 @@ export class ClaudeAgentClient implements AgentClient {
};
}
async shutdown(): Promise<void> {
await killExitingClaudeModelDiscoveries();
}
async resolveDefaultModeId({ env: launchEnv }: ResolveAgentDefaultModeInput): Promise<string> {
return claudeModeCatalog(this.buildProviderEnv(launchEnv)).defaultModeId;
}
@@ -6,6 +6,10 @@ import type { ProviderRuntimeSettings } from "../../provider-launch-config.js";
import { terminateWithTreeKill } from "../../../../utils/tree-kill.js";
import { claudeQuery, type ClaudeQueryInput, type ClaudeQueryContext } from "./query.js";
const DISCOVERY_WAIT_MS = 5_000;
// Long enough for the CLI to finish an OAuth token refresh it started before the wait ended.
const DISCOVERY_EXIT_GRACE_MS = 60_000;
interface ClaudeModelDiscoveryOptions {
resolveBinary: () => Promise<string>;
runtimeSettings?: ProviderRuntimeSettings;
@@ -18,19 +22,24 @@ type OpenDiscoveryQuery = (
context: ClaudeQueryContext,
) => Pick<Query, "supportedModels" | "close">;
type DiscoveryQuery = ReturnType<OpenDiscoveryQuery>;
// Discovery processes left to exit on their own after their caller stopped waiting.
const exitingDiscoveries = new Map<ChildProcess, () => Promise<void>>();
// The curated catalog survives a failed or slow probe. This process never receives
// a prompt and must not persist a session or launch user MCP servers/hooks.
export async function discoverClaudeModels(
options: ClaudeModelDiscoveryOptions,
openQuery: OpenDiscoveryQuery = claudeQuery,
): Promise<ModelInfo[]> {
const controller = new AbortController();
const waitController = new AbortController();
const signal = options.signal
? AbortSignal.any([options.signal, controller.signal])
: controller.signal;
? AbortSignal.any([options.signal, waitController.signal])
: waitController.signal;
const timer = setTimeout(
() => controller.abort(new Error("Claude model discovery timed out")),
5_000,
() => waitController.abort(new Error("Claude model discovery timed out")),
DISCOVERY_WAIT_MS,
);
let endInput!: () => void;
const ended = new Promise<void>((resolve) => {
@@ -46,7 +55,7 @@ export async function discoverClaudeModels(
};
},
};
let query: ReturnType<OpenDiscoveryQuery> | undefined;
let query: DiscoveryQuery | undefined;
let child: ChildProcess | undefined;
try {
signal.throwIfAborted();
@@ -64,7 +73,8 @@ export async function discoverClaudeModels(
strictMcpConfig: true,
settingSources: ["user"],
settings: { disableAllHooks: true },
abortController: controller,
// No abortController: the SDK kills the process when it aborts, and the end of
// the wait must not.
},
},
{
@@ -78,12 +88,56 @@ export async function discoverClaudeModels(
} finally {
clearTimeout(timer);
endInput();
try {
query?.close();
} finally {
if (child) {
await terminateWithTreeKill(child, { gracefulTimeoutMs: 200, forceTimeoutMs: 200 });
}
if (query && child && !hasExited(child)) {
// The CLI may be refreshing an expired OAuth token, even after it has answered.
// Claude Code rotates the refresh token before saving the new pair, so killing it now
// leaves a revoked token on disk and logs the user out (#9314). With its input ended
// it exits once that work is done; the grace period and daemon shutdown bound the wait.
letDiscoveryExit(query, child);
} else {
await retireDiscovery(query, child);
}
}
}
/** Kills discovery processes still exiting after their wait ended. Called on daemon shutdown. */
export async function killExitingClaudeModelDiscoveries(): Promise<void> {
await Promise.all(Array.from(exitingDiscoveries.values(), (kill) => kill()));
}
function letDiscoveryExit(query: DiscoveryQuery, child: ChildProcess): void {
function forget(): void {
clearTimeout(graceTimer);
child.off("exit", onExit);
exitingDiscoveries.delete(child);
}
function onExit(): void {
forget();
query.close();
}
async function kill(): Promise<void> {
forget();
await retireDiscovery(query, child);
}
const graceTimer = setTimeout(() => void kill(), DISCOVERY_EXIT_GRACE_MS);
graceTimer.unref();
child.once("exit", onExit);
exitingDiscoveries.set(child, kill);
}
async function retireDiscovery(
query: DiscoveryQuery | undefined,
child: ChildProcess | undefined,
): Promise<void> {
try {
query?.close();
} finally {
if (child) {
await terminateWithTreeKill(child, { gracefulTimeoutMs: 200, forceTimeoutMs: 200 });
}
}
}
function hasExited(child: ChildProcess): boolean {
return child.exitCode !== null || child.signalCode !== null;
}
@@ -1,3 +1,4 @@
import { ChildProcess } from "node:child_process";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
@@ -1173,4 +1174,127 @@ describe("Claude model discovery process", () => {
await expect(result).rejects.toBe(failure);
expect(closed).toBe(true);
});
describe("when the CLI is still starting up at the deadline", () => {
// Without a pid, terminateWithTreeKill signals the child directly instead of a real tree.
class StartingClaudeProcess extends ChildProcess {
readonly signals: string[] = [];
override kill(signal?: NodeJS.Signals | number): boolean {
this.signals.push(String(signal));
this.emit("exit", null, signal);
return true;
}
exitOnItsOwn(): void {
this.emit("exit", 0, null);
}
}
interface StalledDiscoveryState {
closed: boolean;
inputEnded: boolean;
}
async function recordInputEnd(
prompt: AsyncIterable<unknown>,
state: StalledDiscoveryState,
): Promise<void> {
await prompt[Symbol.asyncIterator]().next();
state.inputEnded = true;
}
// By default the CLI never answers: an expired OAuth token is refreshed first.
function startStalledDiscovery(
supportedModels: () => Promise<ModelInfo[]> = () => new Promise<ModelInfo[]>(() => {}),
) {
const child = new StartingClaudeProcess();
const state: StalledDiscoveryState = { closed: false, inputEnded: false };
const result = discoverClaudeModels(
{ resolveBinary: async () => "/claude", env: {} },
(input, context) => {
if (typeof input.prompt === "string") throw new Error("unexpected inference prompt");
void recordInputEnd(input.prompt, state);
context.onChildProcess?.(child);
return {
supportedModels,
close: () => {
state.closed = true;
},
};
},
);
return { child, state, result };
}
afterEach(() => {
vi.useRealTimers();
});
it("stops waiting at the limit but lets the CLI finish instead of killing it", async () => {
vi.useFakeTimers();
const { child, state, result } = startStalledDiscovery();
const settled = expect(result).rejects.toThrow("Claude model discovery timed out");
await vi.advanceTimersByTimeAsync(5_000);
await settled;
expect(state.inputEnded).toBe(true);
expect(child.signals).toEqual([]);
expect(state.closed).toBe(false);
await vi.advanceTimersByTimeAsync(59_999);
expect(child.signals).toEqual([]);
await vi.advanceTimersByTimeAsync(1);
expect(child.signals).toEqual(["SIGTERM"]);
expect(state.closed).toBe(true);
});
async function answerAtOnce(): Promise<ModelInfo[]> {
return [];
}
it("lets the CLI finish after it answers instead of killing it", async () => {
// The CLI answers supportedModels() before its startup token refresh has saved the
// rotated refresh token, so a kill right after the answer also logs the user out.
vi.useFakeTimers();
const { child, state, result } = startStalledDiscovery(answerAtOnce);
await expect(result).resolves.toEqual([]);
await vi.advanceTimersByTimeAsync(0);
expect(state.inputEnded).toBe(true);
expect(child.signals).toEqual([]);
await vi.advanceTimersByTimeAsync(1_000);
child.exitOnItsOwn();
expect(state.closed).toBe(true);
await vi.advanceTimersByTimeAsync(120_000);
expect(child.signals).toEqual([]);
});
it("never signals a CLI that exits on its own during the grace period", async () => {
vi.useFakeTimers();
const { child, state, result } = startStalledDiscovery();
const settled = expect(result).rejects.toThrow("Claude model discovery timed out");
await vi.advanceTimersByTimeAsync(5_000);
await settled;
await vi.advanceTimersByTimeAsync(2_000);
child.exitOnItsOwn();
expect(state.closed).toBe(true);
await vi.advanceTimersByTimeAsync(120_000);
expect(child.signals).toEqual([]);
});
it("tree-kills a still-running CLI when the daemon shuts down", async () => {
vi.useFakeTimers();
const { child, state, result } = startStalledDiscovery();
const settled = expect(result).rejects.toThrow("Claude model discovery timed out");
await vi.advanceTimersByTimeAsync(5_000);
await settled;
expect(child.signals).toEqual([]);
await new ClaudeAgentClient({ logger: createTestLogger() }).shutdown();
expect(child.signals).toEqual(["SIGTERM"]);
expect(state.closed).toBe(true);
});
});
});
@@ -2437,6 +2437,36 @@ describe("Codex app-server provider", () => {
appServer.assertNoErrors();
});
test("lets idle cleanup release a session opened only to read its history", async () => {
let spawns = 0;
const appServer = createFakeCodexAppServer({
"thread/resume": () =>
Promise.reject(new Error(archivedThreadErrorMessage("archived-thread-id"))),
"thread/read": () => ({ thread: { turns: [] } }),
});
const provider = new CodexAppServerAgentClient(createTestLogger(), undefined, {
appServerProcess: createFakeCodexAppServerProcess(() => {
spawns += 1;
return appServer.child;
}),
});
const internals = castInternals<{
goalsEnabledPromise: Promise<boolean> | null;
autoReviewEnabledPromise: Promise<boolean> | null;
}>(provider);
internals.goalsEnabledPromise = Promise.resolve(false);
internals.autoReviewEnabledPromise = Promise.resolve(false);
const session = await provider.resumeSession(archivedThreadHandle(), undefined, undefined, {
purpose: "history",
});
expect(await session.canStopRuntime!()).toBe(true);
expect(spawns).toBe(1);
await session.close();
appServer.assertNoErrors();
});
test("unarchives Codex when an active Paseo agent resumes an archived thread", async () => {
const threadRequests: string[] = [];
let resumeAttempts = 0;
@@ -5385,6 +5385,8 @@ export class CodexAppServerAgentSession implements AgentSession {
}
async canStopRuntime(): Promise<boolean> {
// A history session released its app-server once it read the thread, so nothing runs.
if (this.connectionState === "history-ready") return true;
if (
!this.client ||
this.client.hasPendingRequests() ||