mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-16 13:24:55 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34a7942cb5 | ||
|
|
d25e44b146 |
@@ -6,7 +6,7 @@ import {
|
||||
type ChildProcess,
|
||||
type SpawnOptions,
|
||||
} from "node:child_process";
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import net from "node:net";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
@@ -59,7 +59,17 @@ export async function getAvailableHostDaemonPort(): Promise<number> {
|
||||
return port;
|
||||
}
|
||||
|
||||
async function waitForServer(port: number, child: ChildProcess): Promise<void> {
|
||||
/**
|
||||
* 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> {
|
||||
const deadline = Date.now() + 90_000;
|
||||
let lastError: unknown = null;
|
||||
|
||||
@@ -70,22 +80,13 @@ async function waitForServer(port: number, child: ChildProcess): Promise<void> {
|
||||
);
|
||||
}
|
||||
try {
|
||||
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;
|
||||
const serverId = await readServerId(port, identity.paseoHome);
|
||||
if (serverId === identity.serverId) return;
|
||||
lastError = new Error(`Port ${port} is served by ${serverId}, not ${identity.serverId}`);
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
@@ -95,6 +96,25 @@ async function waitForServer(port: number, child: ChildProcess): Promise<void> {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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"]>,
|
||||
@@ -221,7 +241,7 @@ export async function startIsolatedHostDaemon(
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServer(port, spawned);
|
||||
await waitForServer(port, spawned, { serverId, paseoHome });
|
||||
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, killExitingClaudeModelDiscoveries } from "./model-discovery.js";
|
||||
import { discoverClaudeModels } from "./model-discovery.js";
|
||||
import { claudeQuery, type ClaudeOptions, type ClaudeQueryFactory } from "./query.js";
|
||||
import {
|
||||
findSteeredMessageParent,
|
||||
@@ -1751,10 +1751,6 @@ 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,10 +6,6 @@ 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;
|
||||
@@ -22,24 +18,19 @@ 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 waitController = new AbortController();
|
||||
const controller = new AbortController();
|
||||
const signal = options.signal
|
||||
? AbortSignal.any([options.signal, waitController.signal])
|
||||
: waitController.signal;
|
||||
? AbortSignal.any([options.signal, controller.signal])
|
||||
: controller.signal;
|
||||
const timer = setTimeout(
|
||||
() => waitController.abort(new Error("Claude model discovery timed out")),
|
||||
DISCOVERY_WAIT_MS,
|
||||
() => controller.abort(new Error("Claude model discovery timed out")),
|
||||
5_000,
|
||||
);
|
||||
let endInput!: () => void;
|
||||
const ended = new Promise<void>((resolve) => {
|
||||
@@ -55,7 +46,7 @@ export async function discoverClaudeModels(
|
||||
};
|
||||
},
|
||||
};
|
||||
let query: DiscoveryQuery | undefined;
|
||||
let query: ReturnType<OpenDiscoveryQuery> | undefined;
|
||||
let child: ChildProcess | undefined;
|
||||
try {
|
||||
signal.throwIfAborted();
|
||||
@@ -73,8 +64,7 @@ export async function discoverClaudeModels(
|
||||
strictMcpConfig: true,
|
||||
settingSources: ["user"],
|
||||
settings: { disableAllHooks: true },
|
||||
// No abortController: the SDK kills the process when it aborts, and the end of
|
||||
// the wait must not.
|
||||
abortController: controller,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -88,56 +78,12 @@ export async function discoverClaudeModels(
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
endInput();
|
||||
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);
|
||||
try {
|
||||
query?.close();
|
||||
} finally {
|
||||
if (child) {
|
||||
await terminateWithTreeKill(child, { gracefulTimeoutMs: 200, forceTimeoutMs: 200 });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 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,4 +1,3 @@
|
||||
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";
|
||||
@@ -1174,127 +1173,4 @@ 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,36 +2437,6 @@ 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,8 +5385,6 @@ 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