mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-16 13:24:55 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a78c1cd16 | ||
|
|
8522d18ed8 |
@@ -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() ||
|
||||
|
||||
Reference in New Issue
Block a user