Compare commits

..
Author SHA1 Message Date
paseo-bot[bot] 37783ba3bf refactor(server): one origin rule for the WebSocket upgrade and HTTP CORS 2026-11-17 05:31:16 +00:00
paseo-bot[bot] eb9e3472fe fix(server): accept loopback-alias origins on HTTP like the WebSocket does
A web UI opened at 127.0.0.1 on a remapped port connects over the
WebSocket, which treats loopback aliases on the request's port as
same-origin, but the HTTP CORS check did not, so the app's download fetch
failed. Both checks now use the same rule.
2026-11-17 05:21:56 +00:00
paseo-bot[bot] 0b34362757 fix(app): send host cookies with direct downloads and check the route in E2E
Direct downloads now use credentials: "include", matching what the link
navigation and the host's WebSocket handshake send, so a proxy that checks
cookies lets the download through. The direct-TCP E2E test asserts that the
daemon's download route served the file.
2026-11-17 05:06:11 +00:00
paseo-bot[bot]andpanta82 afe9344c3a fix(app): keep failed HTTP downloads inside the app
On web and desktop, Download on a direct TCP host clicked a link to the
host's download URL. The app runs on another origin (paseo://app on
desktop), so the link navigated the whole window, and a response that was
not the file, such as a reverse proxy's 401 page, replaced the app. The
download was also marked complete before any response arrived.

Fetch the file instead, check the response, and save it from a blob.
HTTP failures now show "Failed to download file (HTTP <status>)." in the
download toast and the window stays on the workspace.

Fixes #9895

Co-authored-by: panta82 <4291324+panta82@users.noreply.github.com>
2026-11-17 04:53:39 +00:00
30 changed files with 231 additions and 763 deletions
@@ -466,12 +466,12 @@ test("downloads a file over direct TCP with its original filename and bytes", as
await gotoWorkspace(page, workspace.workspaceId);
await openFileExplorer(page);
const daemonRequest = page.waitForRequest("**/api/files/download?*");
const downloaded = await downloadFileFromExplorer(page, fileName);
const url = new URL(downloaded.url());
expect(url.protocol).toBe("http:");
expect(url.port).toBe(getE2EDaemonPort());
expect(url.pathname).toBe("/api/files/download");
expect(url.searchParams.get("token")).toBeTruthy();
const requestUrl = new URL((await daemonRequest).url());
expect(requestUrl.port).toBe(getE2EDaemonPort());
expect(requestUrl.searchParams.get("token")).toBeTruthy();
expect(new URL(downloaded.url()).protocol).toBe("blob:");
expect(downloaded.suggestedFilename()).toBe(fileName);
const savedFile = testInfo.outputPath(fileName);
await downloaded.saveAs(savedFile);
@@ -479,6 +479,45 @@ test("downloads a file over direct TCP with its original filename and bytes", as
expect(await readFile(savedFile)).toEqual(source);
});
test("keeps the app open and shows the error when the host rejects a download", async ({
page,
}, testInfo) => {
const fileName = "removed-during-download.bin";
const filePath = path.join(workspace.workspaceDirectory, fileName);
const source = Buffer.from([0, 1, 2, 255]);
await writeFile(filePath, source);
await gotoWorkspace(page, workspace.workspaceId);
await openFileExplorer(page);
const workspaceUrl = page.url();
// The file disappears after the token is issued, so the real daemon answers 404.
await page.route(
"**/api/files/download?*",
async (route) => {
await rm(filePath);
await route.continue();
},
{ times: 1 },
);
await page
.getByTestId("file-explorer-tree-scroll")
.getByText(fileName, { exact: true })
.click({ button: "right" });
await page.getByRole("menuitem", { name: "Download", exact: true }).click();
await expect(
page.getByText("Failed to download file (HTTP 404).", { exact: true }),
).toBeVisible();
expect(page.url()).toBe(workspaceUrl);
await writeFile(filePath, source);
const downloaded = await downloadFileFromExplorer(page, fileName);
const savedFile = testInfo.outputPath(fileName);
await downloaded.saveAs(savedFile);
expect(await readFile(savedFile)).toEqual(source);
expect(page.url()).toBe(workspaceUrl);
});
test("downloads an unsupported binary from its preview with original filename and bytes", async ({
page,
}, testInfo) => {
+1
View File
@@ -2017,6 +2017,7 @@ export const ar: TranslationResources = {
hostUnavailable: "مضيف التنزيل غير متاح.",
cancelled: "تم إلغاء التنزيل.",
failed: "فشل تنزيل الملف.",
httpFailed: "فشل تنزيل الملف (HTTP {{status}}).",
shareFile: "مشاركة الملف",
shareFileNamed: "مشاركة{{fileName}}",
},
+1
View File
@@ -2045,6 +2045,7 @@ export const en = {
hostUnavailable: "Download host is unavailable.",
cancelled: "Download was cancelled.",
failed: "Failed to download file.",
httpFailed: "Failed to download file (HTTP {{status}}).",
shareFile: "Share file",
shareFileNamed: "Share {{fileName}}",
},
+1
View File
@@ -2066,6 +2066,7 @@ export const es: TranslationResources = {
hostUnavailable: "El host de descarga no está disponible.",
cancelled: "La descarga fue cancelada.",
failed: "No se pudo descargar el archivo.",
httpFailed: "No se pudo descargar el archivo (HTTP {{status}}).",
shareFile: "compartir archivo",
shareFileNamed: "Compartir{{fileName}}",
},
+1
View File
@@ -2065,6 +2065,7 @@ export const fr: TranslationResources = {
hostUnavailable: "L’hôte de téléchargement est indisponible.",
cancelled: "Téléchargement annulé.",
failed: "Impossible de télécharger le fichier.",
httpFailed: "Impossible de télécharger le fichier (HTTP {{status}}).",
shareFile: "Partager le fichier",
shareFileNamed: "Partager {{fileName}}",
},
+1
View File
@@ -2038,6 +2038,7 @@ export const ja: TranslationResources = {
hostUnavailable: "ダウンロードホストが利用できません。",
cancelled: "ダウンロードがキャンセルされました。",
failed: "ファイルのダウンロードに失敗しました。",
httpFailed: "ファイルのダウンロードに失敗しました (HTTP {{status}}).",
shareFile: "ファイルを共有",
shareFileNamed: "{{fileName}}を共有",
},
+1
View File
@@ -2028,6 +2028,7 @@ export const ko: TranslationResources = {
hostUnavailable: "다운로드 호스트를 사용할 수 없습니다.",
cancelled: "다운로드가 취소되었습니다.",
failed: "파일을 다운로드하지 못했습니다.",
httpFailed: "파일을 다운로드하지 못했습니다 (HTTP {{status}}).",
shareFile: "파일 공유",
shareFileNamed: "{{fileName}} 공유",
},
+1
View File
@@ -2051,6 +2051,7 @@ export const ptBR: TranslationResources = {
hostUnavailable: "Host de download indisponível.",
cancelled: "Download cancelado.",
failed: "Falha ao baixar arquivo.",
httpFailed: "Falha ao baixar arquivo (HTTP {{status}}).",
shareFile: "Compartilhar arquivo",
shareFileNamed: "Compartilhar {{fileName}}",
},
+1
View File
@@ -2051,6 +2051,7 @@ export const ru: TranslationResources = {
hostUnavailable: "Хост загрузки недоступен.",
cancelled: "Загрузка отменена.",
failed: "Не удалось загрузить файл.",
httpFailed: "Не удалось загрузить файл (HTTP {{status}}).",
shareFile: "Поделиться файлом",
shareFileNamed: "Поделиться {{fileName}}",
},
+1
View File
@@ -1994,6 +1994,7 @@ export const zhCN: TranslationResources = {
hostUnavailable: "下载 Host 不可用。",
cancelled: "下载已取消。",
failed: "下载文件失败。",
httpFailed: "下载文件失败 (HTTP {{status}}).",
shareFile: "共享文件",
shareFileNamed: "共享 {{fileName}}",
},
@@ -90,6 +90,7 @@ interface StartInput {
token?: ReturnType<typeof createTokenRequest>;
reader?: ReturnType<typeof createFileReader>;
saver?: ReturnType<typeof createFileSaver>;
fetchFile?: typeof fetch;
}
async function startDownload(input: StartInput): Promise<Download | undefined> {
@@ -108,6 +109,7 @@ async function startDownload(input: StartInput): Promise<Download | undefined> {
requestFileDownloadToken: token.request,
readFile: reader.read,
saveFile: saver.save,
fetchFile: input.fetchFile ?? (async () => new Response(FILE_BYTES)),
});
const { activeDownloadId, downloads } = useDownloadStore.getState();
return activeDownloadId ? downloads.get(activeDownloadId) : undefined;
@@ -203,6 +205,39 @@ describe("download store transport selection", () => {
expect(saver.savedFiles).toEqual([]);
});
it("fails with the HTTP status when a proxy in front of the host rejects the download", async () => {
const requests: Array<RequestInit | undefined> = [];
const download = await startDownload({
profile: profileWith([localTcpConnection]),
activeConnectionId: localTcpConnection.id,
fetchFile: async (_url, init) => {
requests.push(init);
return new Response("<h1>401 Authorization Required</h1>", {
status: 401,
headers: { "WWW-Authenticate": 'Basic realm="Paseo"' },
});
},
});
expect(download).toMatchObject({
status: "error",
message: i18n.t("downloads.httpFailed", { status: 401 }),
});
expect(requests).toEqual([{ credentials: "include", redirect: "error" }]);
});
it("fails with a readable message when the download request cannot reach the host", async () => {
const download = await startDownload({
profile: profileWith([localTcpConnection]),
activeConnectionId: localTcpConnection.id,
fetchFile: async () => {
throw new TypeError("Failed to fetch");
},
});
expect(download).toMatchObject({ status: "error", message: i18n.t("downloads.failed") });
});
it("fails the download and saves nothing when the session read fails", async () => {
const reader = createFileReader(async () => {
throw new Error("Connection lost");
+34 -34
View File
@@ -4,7 +4,6 @@ import * as LegacyFileSystem from "expo-file-system/legacy";
import * as Sharing from "expo-sharing";
import type { HostProfile } from "@/types/host-connection";
import { buildDaemonWebSocketUrl } from "@/utils/daemon-endpoints";
import { openExternalUrl } from "@/utils/open-external-url";
import { isWeb } from "@/constants/platform";
import { i18n } from "@/i18n/i18next";
@@ -59,6 +58,7 @@ interface DownloadState {
/** Reads the whole file over the session socket; used when no direct HTTP endpoint is active. */
readFile: (path: string, maxBytes: number) => Promise<{ bytes: Uint8Array; size: number }>;
saveFile?: typeof saveDownloadedBytes;
fetchFile?: typeof fetch;
}) => Promise<void>;
updateProgress: (id: string, progress: DownloadProgress) => void;
@@ -86,6 +86,7 @@ export const useDownloadStore = create<DownloadState>()((set, get) => ({
requestFileDownloadToken,
readFile,
saveFile = saveDownloadedBytes,
fetchFile = fetch,
}) => {
const id = generateDownloadId();
const download: Download = {
@@ -144,14 +145,11 @@ export const useDownloadStore = create<DownloadState>()((set, get) => ({
throw new Error(i18n.t("downloads.hostUnavailable"));
}
const downloadUrl = buildDownloadUrl(
downloadTarget.baseUrl,
tokenResponse.token,
isWeb ? downloadTarget.authCredentials : null,
);
const downloadUrl = buildDownloadUrl(downloadTarget.baseUrl, tokenResponse.token);
if (isWeb) {
triggerBrowserDownload(downloadUrl, resolvedFileName);
const file = await fetchDownload(fetchFile, downloadUrl);
saveBrowserFile(file, resolvedFileName);
get().completeDownload(id);
return;
}
@@ -198,11 +196,6 @@ export const useDownloadStore = create<DownloadState>()((set, get) => ({
await shareSavedFile(result.uri, tokenResponse.mimeType, resolvedFileName);
} catch (error) {
const message = error instanceof Error ? error.message : i18n.t("downloads.failed");
if (isWeb) {
console.warn("[DownloadStore] Download failed:", message);
get().failDownload(id, message);
return;
}
get().failDownload(id, message);
}
},
@@ -283,7 +276,6 @@ function findMostRecentDownloadId(downloads: Map<string, Download>): string | nu
interface DownloadTarget {
baseUrl: string | null;
authHeader: string | null;
authCredentials: { username: string; password: string } | null;
}
/**
@@ -306,7 +298,7 @@ function resolveDaemonDownloadTarget(
buildDaemonWebSocketUrl(connection.endpoint, { useTls: connection.useTls ?? false }),
);
} catch {
return { baseUrl: null, authHeader: null, authCredentials: null };
return { baseUrl: null, authHeader: null };
}
if (parsed.protocol === "ws:") {
@@ -332,23 +324,36 @@ function resolveDaemonDownloadTarget(
? `Basic ${btoa(`${authCredentials.username}:${authCredentials.password}`)}`
: null;
return { baseUrl, authHeader, authCredentials };
return { baseUrl, authHeader };
}
function buildDownloadUrl(
baseUrl: string,
token: string,
authCredentials: { username: string; password: string } | null,
): string {
function buildDownloadUrl(baseUrl: string, token: string): string {
const url = new URL("/api/files/download", baseUrl);
url.searchParams.set("token", token);
if (authCredentials) {
url.username = authCredentials.username;
url.password = authCredentials.password;
}
return url.toString();
}
/**
* Fetches the file instead of navigating to it. The app runs on another origin than the host
* (paseo://app on desktop), so a link to the download URL navigates the whole window, and any
* response that is not the file (a proxy's 401 page, a 404) replaces the app.
*/
async function fetchDownload(fetchFile: typeof fetch, url: string): Promise<Blob> {
let response: Response;
try {
// Send the same cookies the host's WebSocket handshake sends, so a proxy in front of the
// host lets the download through. Refuse redirects: a proxy that redirects to a login page
// would otherwise be saved as the file.
response = await fetchFile(url, { credentials: "include", redirect: "error" });
} catch {
throw new Error(i18n.t("downloads.failed"));
}
if (!response.ok) {
throw new Error(i18n.t("downloads.httpFailed", { status: response.status }));
}
return response.blob();
}
/**
* Saves bytes read over the session. Web and Electron hand them to the browser's download
* manager and return `null`; native writes the file and returns its URI for the share sheet.
@@ -361,12 +366,7 @@ async function saveDownloadedBytes({
if (isWeb) {
const buffer = new ArrayBuffer(bytes.byteLength);
new Uint8Array(buffer).set(bytes);
const url = URL.createObjectURL(
new Blob([buffer], { type: mimeType ?? "application/octet-stream" }),
);
triggerBrowserDownload(url, fileName);
// The browser may still be reading the blob while its save prompt is open.
setTimeout(() => URL.revokeObjectURL(url), 60_000);
saveBrowserFile(new Blob([buffer], { type: mimeType ?? "application/octet-stream" }), fileName);
return null;
}
// Resolve and write in one tick so two downloads of the same name cannot pick the same path.
@@ -390,14 +390,12 @@ async function shareSavedFile(
}
}
function triggerBrowserDownload(url: string, fileName: string) {
function saveBrowserFile(file: Blob, fileName: string) {
if (typeof document === "undefined") {
if (typeof window !== "undefined") {
void openExternalUrl(url);
}
return;
}
const url = URL.createObjectURL(file);
const link = document.createElement("a");
link.href = url;
link.download = fileName;
@@ -405,6 +403,8 @@ function triggerBrowserDownload(url: string, fileName: string) {
document.body.appendChild(link);
link.click();
link.remove();
// The browser may still be reading the blob while its save prompt is open.
setTimeout(() => URL.revokeObjectURL(url), 60_000);
}
function resolveDownloadTargetFile(fileName: string): FSFile {
@@ -20,10 +20,6 @@ import {
type ManagedAgent,
} from "./agent-manager.js";
import { AgentStorage, parseStoredAgentRecord } from "./agent-storage.js";
import { AgentTurnStartCanceledError } from "./agent-sdk-types.js";
import { ForegroundPromptStart } from "./agent-run-state.js";
import { CodexAppServerAgentSession } from "./providers/codex-app-server-agent.js";
import { createFakeCodexAppServer } from "./providers/codex/test-utils/fake-app-server.js";
import { InMemoryAgentTimelineStore } from "./agent-timeline-store.js";
import { limitAgentTimelineItemContent } from "./agent-timeline-content.js";
import { buildStoredAgentPayload, toAgentPayload } from "./agent-projections.js";
@@ -1214,7 +1210,7 @@ test("starts its own turn when the previous turn fails to start under a steer se
);
startHeld.resolve();
await expect(second).resolves.toMatchObject({ disposition: "turn_started" });
await expect(second).resolves.toEqual({ disposition: "turn_started" });
await vi.waitFor(() => expect(session.startPrompts).toEqual(["FIRST", "SECOND"]));
expect(session.interruptCount).toBe(0);
} finally {
@@ -9753,464 +9749,6 @@ test("streamAgent clears pending run when startTurn fails before a turn id exist
);
});
test("canceling a pending Codex start settles as canceled without accepting its prompt", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-codex-cold-cancel-"));
const threadLoaded = deferred<{ data: string[] }>();
const appServer = createFakeCodexAppServer({ "thread/loaded/list": () => threadLoaded.promise });
class ColdStartClient extends TestAgentClient {
override async createSession(config: AgentSessionConfig): Promise<AgentSession> {
return new CodexAppServerAgentSession(config, null, logger, async () => appServer.child);
}
}
const manager = new AgentManager({
clients: { codex: new ColdStartClient() },
registry: new AgentStorage(join(workdir, "agents"), logger),
logger,
});
const events: AgentStreamEvent[] = [];
manager.subscribe((event) => {
if (event.type === "agent_stream") events.push(event.event);
});
try {
const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, {
workspaceId: undefined,
});
// Drain the stream: a fresh Codex agent has no session handle for runAgent before its first turn.
const senderStart = new ForegroundPromptStart(agent.id);
const firstRun = drainAsyncGenerator(
manager.streamAgent(
agent.id,
"Canceled prompt",
{ clientMessageId: "canceled-prompt" },
senderStart,
),
);
void firstRun.catch(() => undefined);
await appServer.waitForRequest("thread/loaded/list");
const senderRejection = expect(senderStart.started).rejects.toThrow(
"its prompt was not delivered",
);
const cancel = manager.cancelAgentRun(agent.id);
await new Promise((resolve) => setTimeout(resolve, 0));
threadLoaded.resolve({ data: ["thread-1"] });
await expect(cancel).resolves.toEqual({ status: "settled" });
await expect(firstRun).resolves.toBeUndefined();
await senderRejection;
expect(manager.getAgent(agent.id)).toMatchObject({ lifecycle: "idle", lastError: undefined });
expect(manager.hasInFlightRun(agent.id)).toBe(false);
expect(events.map((event) => event.type)).toEqual(["timeline", "turn_canceled"]);
// The canceled attempt stays in history like a rejected one, without being delivered.
expect(manager.getTimeline(agent.id)).toEqual([
{
type: "user_message",
text: "Canceled prompt",
clientMessageId: "canceled-prompt",
messageId: "canceled-prompt",
},
]);
expect(appServer.requests().some((request) => request.method === "turn/start")).toBe(false);
const nextRun = manager.runAgent(agent.id, "Next prompt", { clientMessageId: "next-prompt" });
await appServer.waitForRequest("turn/start");
appServer.startsTurn({ threadId: "thread-1", turnId: "next-turn" });
appServer.completeTurn();
await expect(nextRun).resolves.toMatchObject({ canceled: false });
appServer.assertNoErrors();
} finally {
threadLoaded.resolve({ data: ["thread-1"] });
manager.prepareForShutdown();
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
await manager.flushForShutdown();
rmSync(workdir, { recursive: true, force: true });
}
});
test("stopping a Codex replacement of a stalled start clears replacement activity", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-codex-replacement-cancel-"));
const firstLoaded = deferred<{ data: string[] }>();
const firstInterruptRequested = deferred<void>();
const replacementLoaded = deferred<{ data: string[] }>();
const replacementEntered = deferred<void>();
let loadCount = 0;
const appServer = createFakeCodexAppServer({
"thread/loaded/list": () => {
loadCount += 1;
if (loadCount === 1) return firstLoaded.promise;
replacementEntered.resolve();
return replacementLoaded.promise;
},
});
class ColdStartSession extends CodexAppServerAgentSession {
override async interrupt(): Promise<void> {
const interruption = super.interrupt();
firstInterruptRequested.resolve();
await interruption;
}
}
class ColdStartClient extends TestAgentClient {
override async createSession(config: AgentSessionConfig): Promise<AgentSession> {
return new ColdStartSession(config, null, logger, async () => appServer.child);
}
}
const manager = new AgentManager({
clients: { codex: new ColdStartClient() },
registry: new AgentStorage(join(workdir, "agents"), logger),
logger,
rescueTimeouts: { interruptSessionMs: 10 },
});
try {
const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, {
workspaceId: undefined,
});
const firstRun = drainAsyncGenerator(manager.streamAgent(agent.id, "Original prompt"));
await appServer.waitForRequest("thread/loaded/list");
const replacement = manager.replaceAgentRun(agent.id, "Replacement prompt");
await firstInterruptRequested.promise;
firstLoaded.resolve({ data: ["thread-1"] });
const replacementStream = await replacement;
await expect(firstRun).resolves.toBeUndefined();
expect(manager.getAgent(agent.id)?.lifecycle).toBe("running");
const replacementEvent = replacementStream.next();
await replacementEntered.promise;
const cancel = manager.cancelAgentRun(agent.id);
await new Promise((resolve) => setTimeout(resolve, 0));
replacementLoaded.resolve({ data: ["thread-1"] });
await expect(cancel).resolves.toEqual({ status: "settled" });
await expect(replacementEvent).resolves.toMatchObject({
value: { type: "turn_canceled" },
});
await expect(replacementStream.next()).resolves.toMatchObject({ done: true });
expect(manager.hasInFlightRun(agent.id)).toBe(false);
expect(manager.getAgent(agent.id)?.lifecycle).toBe("idle");
expect(appServer.requests().some((request) => request.method === "turn/start")).toBe(false);
appServer.assertNoErrors();
} finally {
firstLoaded.resolve({ data: ["thread-1"] });
replacementLoaded.resolve({ data: ["thread-1"] });
manager.prepareForShutdown();
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
await manager.flushForShutdown();
rmSync(workdir, { recursive: true, force: true });
}
});
test("a stalled Codex start canceled by its replacement rejects its sender's start wait", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-codex-replaced-start-wait-"));
const firstLoaded = deferred<{ data: string[] }>();
const firstInterruptRequested = deferred<void>();
let loadCount = 0;
const appServer = createFakeCodexAppServer({
"thread/loaded/list": () => {
loadCount += 1;
return loadCount === 1 ? firstLoaded.promise : { data: ["thread-1"] };
},
});
class ColdStartSession extends CodexAppServerAgentSession {
override async interrupt(): Promise<void> {
const interruption = super.interrupt();
firstInterruptRequested.resolve();
await interruption;
}
}
class ColdStartClient extends TestAgentClient {
override async createSession(config: AgentSessionConfig): Promise<AgentSession> {
return new ColdStartSession(config, null, logger, async () => appServer.child);
}
}
const manager = new AgentManager({
clients: { codex: new ColdStartClient() },
registry: new AgentStorage(join(workdir, "agents"), logger),
logger,
rescueTimeouts: { interruptSessionMs: 10 },
});
try {
const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, {
workspaceId: undefined,
});
const firstStart = new ForegroundPromptStart(agent.id);
const secondStart = new ForegroundPromptStart(agent.id);
const firstRun = drainAsyncGenerator(
manager.streamAgent(agent.id, "First prompt", undefined, firstStart),
);
await appServer.waitForRequest("thread/loaded/list");
const firstWait = firstStart.started;
const replacement = manager.replaceAgentRun(agent.id, "Second prompt", undefined, secondStart);
await firstInterruptRequested.promise;
firstLoaded.resolve({ data: ["thread-1"] });
const replacementStream = await replacement;
await expect(firstRun).resolves.toBeUndefined();
const secondWait = secondStart.started;
const replacementDrained = drainAsyncGenerator(replacementStream);
await appServer.waitForRequest("turn/start");
appServer.startsTurn({ threadId: "thread-1", turnId: "second-turn" });
await expect(secondWait).resolves.toBeUndefined();
await expect(firstWait).rejects.toThrow("its prompt was not delivered");
appServer.completeTurn();
await replacementDrained;
expect(appServer.requests().filter((request) => request.method === "turn/start")).toEqual([
expect.objectContaining({
params: expect.objectContaining({
input: [expect.objectContaining({ text: "Second prompt" })],
}),
}),
]);
appServer.assertNoErrors();
} finally {
firstLoaded.resolve({ data: ["thread-1"] });
manager.prepareForShutdown();
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
await manager.flushForShutdown();
rmSync(workdir, { recursive: true, force: true });
}
});
test("a sender's start wait follows its prompt across a stale-session retry", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-stale-start-wait-"));
const releaseStart = deferred<void>();
class StaleStartSession extends TestAgentSession {
override async startTurn(): Promise<{ turnId: string }> {
await releaseStart.promise;
throw new StaleProviderSessionError("stale-bridge");
}
}
class StaleStartClient extends ResumeTrackingTestAgentClient {
override async createSession(config: AgentSessionConfig): Promise<AgentSession> {
return new StaleStartSession(config);
}
}
const client = new StaleStartClient();
const storage = new AgentStorage(join(workdir, "agents"), logger);
const manager = new AgentManager({ clients: { codex: client }, registry: storage, logger });
const startedTurns: AgentStreamEvent[] = [];
manager.subscribe((event) => {
if (event.type === "agent_stream" && event.event.type === "turn_started") {
startedTurns.push(event.event);
}
});
try {
const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, {
workspaceId: undefined,
});
const senderWait = dispatchStarted(
await startAgentRun(manager, agent.id, "Retry this prompt", logger, {
runOptions: { clientMessageId: "stale-start-wait" },
}),
);
releaseStart.resolve();
await client.waitForRetryStart();
await manager.waitForAgentEvent(agent.id);
expect(client.resumeOverrides).toHaveLength(1);
expect(startedTurns).toHaveLength(1);
expect(manager.getTimeline(agent.id).filter((item) => item.type === "user_message")).toEqual([
expect.objectContaining({ text: "Retry this prompt", clientMessageId: "stale-start-wait" }),
]);
await expect(senderWait).resolves.toBeUndefined();
} finally {
releaseStart.resolve();
manager.prepareForShutdown();
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
await manager.flushForShutdown();
rmSync(workdir, { recursive: true, force: true });
}
});
test("a sender's start wait follows its prompt when the session is stale before the wait begins", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-immediate-stale-start-"));
class StaleStartSession extends TestAgentSession {
override async startTurn(): Promise<{ turnId: string }> {
throw new StaleProviderSessionError("stale-bridge");
}
}
class StaleStartClient extends ResumeTrackingTestAgentClient {
override async createSession(config: AgentSessionConfig): Promise<AgentSession> {
return new StaleStartSession(config);
}
}
const client = new StaleStartClient();
const storage = new AgentStorage(join(workdir, "agents"), logger);
const manager = new AgentManager({ clients: { codex: client }, registry: storage, logger });
try {
const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, {
workspaceId: undefined,
});
const dispatch = await startAgentRun(manager, agent.id, "Retry this prompt", logger, {
runOptions: { clientMessageId: "immediate-stale-start" },
});
// The stale start has failed and been cleared before the sender begins waiting.
await new Promise((resolve) => setTimeout(resolve, 0));
expect(manager.hasInFlightRun(agent.id)).toBe(false);
await expect(dispatchStarted(dispatch)).resolves.toBeUndefined();
await client.waitForRetryStart();
expect(manager.getTimeline(agent.id).filter((item) => item.type === "user_message")).toEqual([
expect.objectContaining({
text: "Retry this prompt",
clientMessageId: "immediate-stale-start",
}),
]);
} finally {
manager.prepareForShutdown();
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
await manager.flushForShutdown();
rmSync(workdir, { recursive: true, force: true });
}
});
test.each(["reload", "retry", "stale retry"])(
"a sender's start wait rejects when its stale-session %s fails",
async (failure) => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-stale-start-failure-"));
const releaseStart = deferred<void>();
class StaleStartSession extends TestAgentSession {
override async startTurn(): Promise<{ turnId: string }> {
await releaseStart.promise;
throw new StaleProviderSessionError("stale-bridge");
}
}
class FailedRetrySession extends TestAgentSession {
override async startTurn(): Promise<{ turnId: string }> {
if (failure === "stale retry") throw new StaleProviderSessionError("retry-bridge");
throw new Error("retry failed");
}
}
class FailedRetryClient extends TestAgentClient {
override async createSession(config: AgentSessionConfig): Promise<AgentSession> {
return new StaleStartSession(config);
}
override async resumeSession(): Promise<AgentSession> {
if (failure === "reload") throw new Error("reload failed");
return new FailedRetrySession({ provider: "codex", cwd: workdir });
}
}
const manager = new AgentManager({ clients: { codex: new FailedRetryClient() }, logger });
try {
const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, {
workspaceId: undefined,
});
const senderWait = dispatchStarted(
await startAgentRun(manager, agent.id, "Retry this prompt", logger),
);
const rejection = expect(senderWait).rejects.toThrow(
failure === "retry" ? "retry failed" : "is stale after its plugin reloaded",
);
releaseStart.resolve();
await rejection;
} finally {
releaseStart.resolve();
manager.prepareForShutdown();
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
await manager.flushForShutdown();
rmSync(workdir, { recursive: true, force: true });
}
},
);
test("a sender's start wait rejects when Stop cancels its pending stale-session retry", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-stale-retry-stop-"));
const releaseStart = deferred<void>();
const retryEntered = deferred<void>();
const retryCanceled = deferred<void>();
class StaleStartSession extends TestAgentSession {
override async startTurn(): Promise<{ turnId: string }> {
await releaseStart.promise;
throw new StaleProviderSessionError("stale-bridge");
}
}
class CanceledRetrySession extends TestAgentSession {
override async startTurn(): Promise<{ turnId: string }> {
retryEntered.resolve();
await retryCanceled.promise;
throw new AgentTurnStartCanceledError();
}
override async interrupt(): Promise<void> {
retryCanceled.resolve();
}
}
class RetryClient extends TestAgentClient {
override async createSession(config: AgentSessionConfig): Promise<AgentSession> {
return new StaleStartSession(config);
}
override async resumeSession(): Promise<AgentSession> {
return new CanceledRetrySession({ provider: "codex", cwd: workdir });
}
}
const manager = new AgentManager({ clients: { codex: new RetryClient() }, logger });
try {
const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, {
workspaceId: undefined,
});
const senderWait = dispatchStarted(
await startAgentRun(manager, agent.id, "Retry this prompt", logger),
);
const rejection = expect(senderWait).rejects.toThrow("its prompt was not delivered");
releaseStart.resolve();
await retryEntered.promise;
await expect(manager.cancelAgentRun(agent.id)).resolves.toEqual({ status: "settled" });
await rejection;
expect(manager.getAgent(agent.id)?.lifecycle).toBe("idle");
expect(manager.getTimeline(agent.id)).toEqual([]);
} finally {
releaseStart.resolve();
retryCanceled.resolve();
manager.prepareForShutdown();
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
await manager.flushForShutdown();
rmSync(workdir, { recursive: true, force: true });
}
});
test("a sender's start wait never adopts an unlinked run after a stale start", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-unlinked-start-"));
const releaseStart = deferred<void>();
const staleError = new StaleProviderSessionError("stale-bridge");
class StaleStartSession extends TestAgentSession {
override async startTurn(): Promise<{ turnId: string }> {
await releaseStart.promise;
throw staleError;
}
}
class RetryClient extends TestAgentClient {
override async createSession(config: AgentSessionConfig): Promise<AgentSession> {
return new StaleStartSession(config);
}
}
const manager = new AgentManager({ clients: { codex: new RetryClient() }, logger });
try {
const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, {
workspaceId: undefined,
});
const senderStart = new ForegroundPromptStart(agent.id);
const firstRun = drainAsyncGenerator(
manager.streamAgent(agent.id, "First prompt", undefined, senderStart),
);
const firstFailure = expect(firstRun).rejects.toBe(staleError);
const rejection = expect(senderStart.started).rejects.toThrow(staleError.message);
releaseStart.resolve();
await firstFailure;
try {
await manager.reloadAgentSession(agent.id);
const otherStart = new ForegroundPromptStart(agent.id);
const otherRun = drainAsyncGenerator(
manager.streamAgent(agent.id, "Another sender", undefined, otherStart),
);
await expect(otherStart.started).resolves.toBeUndefined();
await otherRun;
} finally {
senderStart.end();
}
await rejection;
expect(manager.getTimeline(agent.id).filter((item) => item.type === "user_message")).toEqual(
[],
);
} finally {
releaseStart.resolve();
manager.prepareForShutdown();
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
await manager.flushForShutdown();
rmSync(workdir, { recursive: true, force: true });
}
});
test("acknowledged cancellation settles a pending run before it has a turn id", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-cancel-pending-start-"));
const startEntered = deferred<void>();
@@ -15533,11 +15071,3 @@ test("plugin instructions are recomputed for each launch without changing saved
expect(client.resumeOverrides.at(-1)?.daemonAppendSystemPrompt).toBe("Daemon");
expect((await storage.get(snapshot.id))?.config?.systemPrompt).toBe("Saved");
});
function dispatchStarted(dispatch: Awaited<ReturnType<typeof startAgentRun>>): Promise<void> {
if (dispatch.disposition !== "turn_started") {
throw new Error(`Expected a started turn, got ${dispatch.disposition}`);
}
void dispatch.started.catch(() => undefined);
return dispatch.started;
}
@@ -29,7 +29,6 @@ import { z } from "zod";
import type { TerminalManager } from "../../terminal/terminal-manager.js";
import {
AgentTurnStartCanceledError,
getAgentStreamEventTurnId,
type AgentCapabilityFlags,
type AgentClient,
@@ -93,7 +92,6 @@ import {
import { limitAgentTimelineItemContent } from "./agent-timeline-content.js";
import {
AgentRunState,
type ForegroundPromptStart,
type ForegroundTurnWaiter,
type PendingForegroundRun,
} from "./agent-run-state.js";
@@ -3145,7 +3143,7 @@ export class AgentManager {
pendingRun: PendingForegroundRun;
prompt: AgentPromptInput;
options?: AgentRunOptions;
}): Promise<string | null> {
}): Promise<string> {
const { agent, agentId, pendingRun, prompt, options } = params;
try {
const result = await agent.session.startTurn(prompt, options);
@@ -3154,32 +3152,11 @@ export class AgentManager {
}
return result.turnId;
} catch (error) {
if (error instanceof AgentTurnStartCanceledError) {
if (!pendingRun.settled) {
if (options?.clientMessageId) {
this.recordSubmittedPrompt(agent, prompt, options.clientMessageId, "rejected", {
messageId: options.clientMessageId,
});
}
await this.handleStreamEvent(agent, {
type: "turn_canceled",
provider: agent.provider,
reason: "interrupted",
});
this.finalizeForegroundTurn(agent);
this.runs.settleForegroundRun(agentId, pendingRun.token);
}
return null;
}
if (pendingRun.settled) {
throw error;
}
if (isStaleProviderSessionError(error)) {
this.runs.settleForegroundStart(pendingRun, {
status: "failed",
error: error.message,
stale: true,
});
this.runs.settleForegroundStart(pendingRun, { status: "failed", error: error.message });
agent.pendingReplacement = false;
if (!agent.activeForegroundTurnId) agent.lifecycle = "idle";
this.runs.settleForegroundRun(agentId, pendingRun.token);
@@ -3235,17 +3212,15 @@ export class AgentManager {
agentId: string,
prompt: AgentPromptInput,
options?: AgentRunOptions,
promptStart?: ForegroundPromptStart,
): AsyncGenerator<AgentStreamEvent> {
await this.ensureInteractiveSession(agentId);
yield* this.streamAgent(agentId, prompt, options, promptStart);
yield* this.streamAgent(agentId, prompt, options);
}
private async *streamAfterRuntimeCollection(
agentId: string,
prompt: AgentPromptInput,
options?: AgentRunOptions,
promptStart?: ForegroundPromptStart,
): AsyncGenerator<AgentStreamEvent> {
await this.waitForAgentClose(agentId);
await ensureAgentLoaded(agentId, {
@@ -3253,20 +3228,19 @@ export class AgentManager {
agentStorage: this.requireRegistry(),
logger: this.logger,
});
yield* this.streamAgent(agentId, prompt, options, promptStart);
yield* this.streamAgent(agentId, prompt, options);
}
streamAgent(
agentId: string,
prompt: AgentPromptInput,
options?: AgentRunOptions,
promptStart?: ForegroundPromptStart,
): AsyncGenerator<AgentStreamEvent> {
if (this.collectingRuntimes.has(agentId) || (!this.agents.has(agentId) && this.registry))
return this.streamAfterRuntimeCollection(agentId, prompt, options, promptStart);
return this.streamAfterRuntimeCollection(agentId, prompt, options);
const existingAgent = this.requireSessionAgent(agentId);
if (this.historySessions.has(existingAgent.session))
return this.streamAfterHistoryLoad(agentId, prompt, options, promptStart);
return this.streamAfterHistoryLoad(agentId, prompt, options);
this.logger.trace(
{
agentId,
@@ -3301,10 +3275,9 @@ export class AgentManager {
agent.lastError = undefined;
const pendingRun = this.runs.createPendingRun(agentId);
promptStart?.bind(pendingRun);
const streamForwarder = async function* streamForwarder(this: AgentManager) {
let turnId: string | null;
let turnId: string;
let turnStream: ReturnType<AgentRunState["createTurnStream"]> | null = null;
turnId = await this.startPendingForegroundTurn({
agent,
@@ -3313,15 +3286,6 @@ export class AgentManager {
prompt,
options,
});
if (turnId === null) {
const canceledEvent: AgentStreamEvent = {
type: "turn_canceled",
provider: agent.provider,
reason: "interrupted",
};
yield canceledEvent;
return;
}
if (isReplacement) {
agent.pendingReplacement = false;
@@ -3476,10 +3440,9 @@ export class AgentManager {
agentId: string,
prompt: AgentPromptInput,
options?: AgentRunOptions,
promptStart?: ForegroundPromptStart,
): Promise<AsyncGenerator<AgentStreamEvent>> {
this.requireSessionAgent(agentId);
return this.replaceRunInForegroundLane(agentId, prompt, options, promptStart);
return this.replaceRunInForegroundLane(agentId, prompt, options);
}
async steerAgentRun(
@@ -3520,7 +3483,6 @@ export class AgentManager {
agentId: string,
prompt: AgentPromptInput,
options?: AgentSteerOptions,
promptStart?: ForegroundPromptStart,
): Promise<ActiveTurnSteerDispatchResult> {
// A turn that is still starting has no id to steer yet. Replacing it would drop its
// prompt, so wait until it is accepted (or fails) and steer the turn it became.
@@ -3568,7 +3530,6 @@ export class AgentManager {
expectedTurnId,
prompt,
stripSteerOptions(options),
promptStart,
),
};
}
@@ -3634,10 +3595,9 @@ export class AgentManager {
expectedTurnId: string,
prompt: AgentPromptInput,
options?: AgentRunOptions,
promptStart?: ForegroundPromptStart,
): Promise<AsyncGenerator<AgentStreamEvent>> {
this.assertSteerAdmissionOwnsTurn(agent, expectedTurnId);
return this.replaceRunInForegroundLane(agent.id, prompt, options, promptStart, (live) =>
return this.replaceRunInForegroundLane(agent.id, prompt, options, (live) =>
this.assertSteerAdmissionOwnsTurn(live, expectedTurnId),
);
}
@@ -3650,13 +3610,12 @@ export class AgentManager {
agentId: string,
prompt: AgentPromptInput,
options: AgentRunOptions | undefined,
promptStart?: ForegroundPromptStart,
assertAdmission?: (agent: ActiveManagedAgent) => void,
): Promise<AsyncGenerator<AgentStreamEvent>> {
// Replacements queue in arrival order; each one holds the foreground lane only while
// it cancels and reserves, so Stop is never stuck behind a replacement that is waiting.
return this.runQueuedMutation(this.replacementMutationTails, agentId, () =>
this.replaceRunWhenAdmitted(agentId, prompt, options, promptStart, assertAdmission),
this.replaceRunWhenAdmitted(agentId, prompt, options, assertAdmission),
);
}
@@ -3664,7 +3623,6 @@ export class AgentManager {
agentId: string,
prompt: AgentPromptInput,
options: AgentRunOptions | undefined,
promptStart?: ForegroundPromptStart,
assertAdmission?: (agent: ActiveManagedAgent) => void,
): Promise<AsyncGenerator<AgentStreamEvent>> {
let stalledStart: PendingForegroundRun | null = null;
@@ -3683,7 +3641,7 @@ export class AgentManager {
if (!this.hasInFlightRun(agent.id)) {
return {
status: "reserved" as const,
iterator: this.streamAgent(agent.id, prompt, options, promptStart),
iterator: this.streamAgent(agent.id, prompt, options),
};
}
agent.pendingReplacement = true;
@@ -3697,7 +3655,7 @@ export class AgentManager {
}
return {
status: "reserved" as const,
iterator: this.streamAgent(agent.id, prompt, options, promptStart),
iterator: this.streamAgent(agent.id, prompt, options),
};
} catch (error) {
agent.pendingReplacement = false;
@@ -3888,10 +3846,7 @@ export class AgentManager {
}
async cancelAgentRun(agentId: string): Promise<AgentRunCancellationResult> {
return this.runForegroundMutation(agentId, () => {
this.requireSessionAgent(agentId).pendingReplacement = false;
return this.cancelAgentRunNow(agentId);
});
return this.runForegroundMutation(agentId, () => this.cancelAgentRunNow(agentId));
}
private async cancelAgentRunNow(agentId: string): Promise<AgentRunCancellationResult> {
@@ -15,7 +15,6 @@ import { join } from "node:path";
import { createTestLogger } from "../../test-utils/test-logger.js";
import { AgentManager } from "./agent-manager.js";
import { waitForAgentRunStartWithTimeout } from "./agent-prompt.js";
import { ForegroundPromptStart } from "./agent-run-state.js";
import type {
AgentClient,
AgentRunResult,
@@ -162,7 +161,7 @@ class SlowStartAgentClient implements AgentClient {
async function createRunStartScenario(startDelayMs: number | null): Promise<{
agentManager: AgentManager;
agentId: string;
startRun: () => Promise<{ started: Promise<void> }>;
startRun: () => Promise<void>;
cleanup: () => Promise<void>;
}> {
const workdir = mkdtempSync(join(tmpdir(), "agent-run-start-budget-"));
@@ -179,16 +178,14 @@ async function createRunStartScenario(startDelayMs: number | null): Promise<{
return {
agentManager,
agentId: snapshot.id,
// Returns the sender's start, bound to the run streamAgent registers.
// streamAgent registers the pending run synchronously, so the wait always observes it.
startRun: async () => {
const start = new ForegroundPromptStart(snapshot.id);
const run = agentManager.streamAgent(snapshot.id, "start the run", undefined, start);
const run = agentManager.streamAgent(snapshot.id, "start the run");
drained = (async () => {
for await (const _event of run) {
// Drain whatever the turn produces.
}
})().catch(() => undefined);
return { started: start.started };
},
cleanup: async () => {
// Release any turn still held open, then close. The drain is deliberately not
@@ -210,8 +207,8 @@ test("waiting for a run start outlasts the slowest provider startup budget", asy
vi.useFakeTimers();
try {
const { started } = await scenario.startRun();
const wait = waitForAgentRunStartWithTimeout(scenario.agentManager, scenario.agentId, started);
await scenario.startRun();
const wait = waitForAgentRunStartWithTimeout(scenario.agentManager, scenario.agentId);
let settled = false;
const markSettled = () => {
settled = true;
@@ -236,8 +233,8 @@ test("waiting for a run start still gives up at the run start budget", async ()
vi.useFakeTimers();
try {
const { started } = await scenario.startRun();
const wait = waitForAgentRunStartWithTimeout(scenario.agentManager, scenario.agentId, started);
await scenario.startRun();
const wait = waitForAgentRunStartWithTimeout(scenario.agentManager, scenario.agentId);
const rejection = expect(wait).rejects.toThrow(
"codex run did not start within 60 seconds (phase: run start)",
);
@@ -3,7 +3,6 @@ import type { Logger } from "pino";
import type { AgentPromptInput, AgentRunOptions } from "./agent-sdk-types.js";
import type { AgentManager, ManagedAgent } from "./agent-manager.js";
import { ForegroundPromptStart } from "./agent-run-state.js";
import type { AgentStorage } from "./agent-storage.js";
import { ensureAgentLoaded } from "./agent-loading.js";
import { isStaleProviderSessionError } from "./stale-provider-session-error.js";
@@ -39,17 +38,11 @@ export interface StartAgentRunOptions {
export type PromptDispatchDisposition = "out_of_band" | "steered" | "turn_started";
/** A dispatched prompt. A started turn carries its sender's start outcome. */
export type PromptDispatchResult =
| { disposition: "turn_started"; started: Promise<void> }
| { disposition: Exclude<PromptDispatchDisposition, "turn_started"> };
async function steerOrReplaceActiveRun(
agentManager: AgentRunController,
agentId: string,
prompt: AgentPromptInput,
options: StartAgentRunOptions | undefined,
promptStart: ForegroundPromptStart,
): Promise<
| { disposition: "steered" }
| {
@@ -64,12 +57,7 @@ async function steerOrReplaceActiveRun(
const steerOptions = options.clearPendingPermissions
? { ...options.runOptions, clearPendingPermissions: true }
: options.runOptions;
const result = await agentManager.steerOrReplaceActiveTurn(
agentId,
prompt,
steerOptions,
promptStart,
);
const result = await agentManager.steerOrReplaceActiveTurn(agentId, prompt, steerOptions);
if (result.status === "steered") {
return { disposition: "steered" };
}
@@ -84,15 +72,14 @@ async function startOrReplaceRun(
agentId: string,
prompt: AgentPromptInput,
options: StartAgentRunOptions | undefined,
promptStart: ForegroundPromptStart,
): Promise<{
iterator: AsyncGenerator<import("./agent-sdk-types.js").AgentStreamEvent>;
replaced: boolean;
}> {
const replaced = Boolean(options?.replaceRunning && agentManager.hasInFlightRun(agentId));
const iterator = replaced
? await agentManager.replaceAgentRun(agentId, prompt, options?.runOptions, promptStart)
: agentManager.streamAgent(agentId, prompt, options?.runOptions, promptStart);
? await agentManager.replaceAgentRun(agentId, prompt, options?.runOptions)
: agentManager.streamAgent(agentId, prompt, options?.runOptions);
return { iterator, replaced };
}
@@ -110,7 +97,7 @@ export async function startAgentRun(
prompt: AgentPromptInput,
logger: Logger,
options?: StartAgentRunOptions,
): Promise<PromptDispatchResult> {
): Promise<{ disposition: PromptDispatchDisposition }> {
const snapshot = agentManager.getAgent(agentId);
logger.trace(
{
@@ -131,25 +118,16 @@ export async function startAgentRun(
options?.onDispatch?.("out_of_band");
return { disposition: "out_of_band" };
}
const promptStart = new ForegroundPromptStart(agentId);
try {
try {
await agentManager.reloadStalePluginSession(agentId);
return await startAgentRunInner(agentManager, agentId, prompt, logger, options, promptStart);
} catch (error) {
if (!isStaleProviderSessionError(error)) throw error;
logger.info(
{ agentId, err: error },
"Provider session went stale; reopening from persistence",
);
// The live session belongs to a retired plugin runtime. Reload swaps in a
// fresh session on the current runtime while preserving history and labels.
await agentManager.reloadAgentSession(agentId);
return await startAgentRunInner(agentManager, agentId, prompt, logger, options, promptStart);
}
await agentManager.reloadStalePluginSession(agentId);
return await startAgentRunInner(agentManager, agentId, prompt, logger, options);
} catch (error) {
promptStart.end();
throw error;
if (!isStaleProviderSessionError(error)) throw error;
logger.info({ agentId, err: error }, "Provider session went stale; reopening from persistence");
// The live session belongs to a retired plugin runtime. Reload swaps in a
// fresh session on the current runtime while preserving history and labels.
await agentManager.reloadAgentSession(agentId);
return await startAgentRunInner(agentManager, agentId, prompt, logger, options);
}
}
@@ -158,13 +136,12 @@ async function startAgentRunInner(
agentId: string,
prompt: AgentPromptInput,
logger: Logger,
options: StartAgentRunOptions | undefined,
promptStart: ForegroundPromptStart,
): Promise<PromptDispatchResult> {
options?: StartAgentRunOptions,
): Promise<{ disposition: PromptDispatchDisposition }> {
const snapshot = agentManager.getAgent(agentId);
const steered = options?.idleOnly
? null
: await steerOrReplaceActiveRun(agentManager, agentId, prompt, options, promptStart);
: await steerOrReplaceActiveRun(agentManager, agentId, prompt, options);
if (options?.idleOnly && agentManager.hasInFlightRun(agentId)) {
throw new AgentTurnBusyError(`Agent ${agentId} is running`);
}
@@ -174,7 +151,7 @@ async function startAgentRunInner(
}
const { iterator, replaced } = steered
? { iterator: steered.iterator, replaced: true }
: await startOrReplaceRun(agentManager, agentId, prompt, options, promptStart);
: await startOrReplaceRun(agentManager, agentId, prompt, options);
logger.trace(
{
agentId,
@@ -196,7 +173,7 @@ async function startAgentRunInner(
"Provider session went stale; reopening from persistence",
);
await agentManager.reloadAgentSession(agentId);
const retry = await startOrReplaceRun(agentManager, agentId, prompt, options, promptStart);
const retry = await startOrReplaceRun(agentManager, agentId, prompt, options);
await drainAgentRunIterator(retry.iterator);
}
logger.trace(
@@ -218,11 +195,9 @@ async function startAgentRunInner(
"agent.session.iterator.error",
);
logger.error({ err: error, agentId }, "Agent stream failed");
} finally {
promptStart.end();
}
})();
return { disposition: "turn_started", started: promptStart.started };
return { disposition: "turn_started" };
}
/**
@@ -295,9 +270,8 @@ export interface StartCreatedAgentInitialPromptParams {
const AGENT_RUN_START_TIMEOUT_MS = 60_000;
export async function waitForAgentRunStartWithTimeout(
agentManager: Pick<AgentManager, "getAgent">,
agentManager: AgentManager,
agentId: string,
started: Promise<void>,
signal?: AbortSignal,
): Promise<void> {
const provider = agentManager.getAgent(agentId)?.provider ?? "provider";
@@ -312,28 +286,15 @@ export async function waitForAgentRunStartWithTimeout(
AGENT_RUN_START_TIMEOUT_MS,
);
const abortSignal = signal ? AbortSignal.any([startAbort.signal, signal]) : startAbort.signal;
let onAbort: (() => void) | null = null;
const aborted = new Promise<never>((_, reject) => {
onAbort = () => reject(createStartAbortError(abortSignal.reason));
if (abortSignal.aborted) onAbort();
else abortSignal.addEventListener("abort", onAbort, { once: true });
});
try {
await Promise.race([started, aborted]);
await agentManager.waitForAgentRunStart(agentId, {
signal: signal ? AbortSignal.any([startAbort.signal, signal]) : startAbort.signal,
});
} finally {
clearTimeout(startTimeout);
if (onAbort) abortSignal.removeEventListener("abort", onAbort);
}
}
function createStartAbortError(reason: unknown): Error {
let message = "wait_for_agent_start aborted";
if (typeof reason === "string") message = reason;
else if (reason instanceof Error) message = reason.message;
return Object.assign(new Error(message), { name: "AbortError" });
}
async function resolvePromptSource(
source: AgentPromptSource | undefined,
manager: Pick<AgentManager, "getAgent">,
@@ -359,7 +320,7 @@ async function resolvePromptSource(
*/
export async function sendPromptToAgent(
params: SendPromptToAgentParams,
): Promise<PromptDispatchResult> {
): Promise<{ disposition: PromptDispatchDisposition }> {
const source = await resolvePromptSource(params.source, params.agentManager, params.agentStorage);
const delivery = prepareAgentMessage(params.prompt, source, params.messageId);
return sendPreparedPromptToAgent({
@@ -374,14 +335,14 @@ export async function sendPromptToAgent(
/** Dispatch daemon-prepared context without reclassifying it as human input. */
export async function sendPreparedPromptToAgent(
params: Omit<SendPromptToAgentParams, "source" | "messageId">,
): Promise<PromptDispatchResult> {
): Promise<{ disposition: PromptDispatchDisposition }> {
const unarchive = params.unarchive ?? true;
const record = await params.agentStorage.get(params.agentId);
let archivedAtToRestore: string | null = null;
if (record?.archivedAt) {
if (!unarchive) {
return { disposition: "turn_started", started: Promise.resolve() };
return { disposition: "turn_started" };
}
if (await unarchiveAgentState(params.agentStorage, params.agentManager, params.agentId)) {
archivedAtToRestore = record.archivedAt;
@@ -447,11 +408,7 @@ export async function startCreatedAgentInitialPrompt(
);
if (dispatchResult.disposition === "turn_started") {
await waitForAgentRunStartWithTimeout(
params.agentManager,
params.agentId,
dispatchResult.started,
);
await waitForAgentRunStartWithTimeout(params.agentManager, params.agentId);
}
const refreshedSnapshot = params.agentManager.getAgent(params.agentId) ?? params.snapshot ?? null;
@@ -13,7 +13,7 @@ export interface ForegroundTurnWaiter {
type ForegroundRunStart =
| { status: "pending" }
| { status: "started"; turnId: string }
| { status: "failed"; error: string; stale?: true };
| { status: "failed"; error: string };
export interface PendingForegroundRun {
token: string;
@@ -343,59 +343,3 @@ function settleTrackedRun(run: TrackedAgentRun): void {
run.settled = true;
run.resolveSettled();
}
/**
* Follows one sender's prompt to the foreground run that starts it. Each run created for the
* prompt, including a stale-session retry, is bound here, so `started` settles for this
* sender's prompt and never for another run the agent tracks.
*/
export class ForegroundPromptStart {
readonly started: Promise<void>;
private resolveStarted!: () => void;
private rejectStarted!: (error: Error) => void;
private run: PendingForegroundRun | null = null;
private done = false;
constructor(private readonly agentId: string) {
this.started = new Promise<void>((resolve, reject) => {
this.resolveStarted = resolve;
this.rejectStarted = reject;
});
// Steered and fire-and-forget dispatches never wait on the start.
this.started.catch(() => undefined);
}
/** A run was created for this prompt. */
bind(run: PendingForegroundRun): void {
this.run = run;
void Promise.race([run.turnStartedPromise, run.settledPromise]).then(() =>
this.settle(run, { ended: false }),
);
}
/** The prompt's dispatch is over; no further run will be created for it. */
end(): void {
this.settle(this.run, { ended: true });
}
private settle(run: PendingForegroundRun | null, { ended }: { ended: boolean }): void {
if (this.done || run !== this.run) return;
if (run?.start.status === "started") {
this.done = true;
this.resolveStarted();
return;
}
// A stale-session failure is retried with a new run; wait for it unless dispatch ended.
if (run?.start.status === "failed" && run.start.stale && !ended) return;
this.done = true;
this.rejectStarted(new Error(this.describeUnstarted(run)));
}
private describeUnstarted(run: PendingForegroundRun | null): string {
if (run?.start.status === "failed") return run.start.error;
if (run?.settled) {
return `Agent ${this.agentId} run was canceled before its turn started; its prompt was not delivered`;
}
return `Agent ${this.agentId} run finished before starting`;
}
}
@@ -528,14 +528,6 @@ export type AgentPermissionResponse =
interrupt?: boolean;
};
/** A requested turn was canceled before the provider accepted it. */
export class AgentTurnStartCanceledError extends Error {
constructor() {
super("Turn was canceled before starting");
this.name = "AgentTurnStartCanceledError";
}
}
export interface AgentRunResult {
sessionId: string;
finalText: string;
@@ -10,7 +10,6 @@ import { AgentManager } from "../agent-manager.js";
import { AgentStorage } from "../agent-storage.js";
import type { CreatePaseoWorktreeWorkflowResult } from "../../worktree-session.js";
import { createAgentCommand } from "./create.js";
import { ForegroundPromptStart } from "../agent-run-state.js";
import type { ManagedAgent } from "../agent-manager.js";
const logger = createTestLogger();
@@ -90,12 +89,9 @@ test("session create forwards clientMessageId to the initial prompt run options"
firstAgentContext: { attachments: [] },
});
expect(streamAgent).toHaveBeenCalledWith(
"agent-1",
"hello from create",
{ clientMessageId: "msg-create-1" },
expect.any(ForegroundPromptStart),
);
expect(streamAgent).toHaveBeenCalledWith("agent-1", "hello from create", {
clientMessageId: "msg-create-1",
});
});
test("session create validates the requested mode against the provider's modes", async () => {
@@ -1,4 +1,3 @@
import { AgentRunState, ForegroundPromptStart } from "./agent-run-state.js";
import {
createFinishSubscriptions,
createFinishObservation,
@@ -280,15 +279,8 @@ function buildAgentManagerSpies() {
steerOrReplaceActiveTurn: async () => ({ status: "inactive" as const }),
tryRunOutOfBand: vi.fn().mockReturnValue(false),
subscribe: vi.fn().mockReturnValue(() => {}),
streamAgent: vi.fn(
(agentId: string, _prompt: unknown, _options: unknown, start?: ForegroundPromptStart) => {
const runs = new AgentRunState();
const run = runs.createPendingRun(agentId);
runs.settleForegroundStart(run, { status: "started", turnId: `${agentId}-turn` });
start?.bind(run);
return (async function* noop() {})();
},
),
streamAgent: vi.fn(() => (async function* noop() {})()),
waitForAgentRunStart: vi.fn().mockResolvedValue(undefined),
respondToPermission: vi.fn(),
cancelAgentRun: vi.fn(),
getPendingPermissions: vi.fn(),
@@ -5428,7 +5428,7 @@ describe("Codex app-server provider", () => {
threadStart.resolve({ thread: { id: "thread-1" } });
await expect(interruptPromise).resolves.toBeUndefined();
await expect(resultPromise).resolves.toMatchObject({ canceled: true, timeline: [] });
await expect(resultPromise).rejects.toThrow("interrupted before reaching Codex");
expect(appServer.requests()).not.toContainEqual(
expect.objectContaining({ method: "turn/start" }),
);
@@ -3,7 +3,6 @@ import { applyProviderOptionsPatch } from "../option-controls.js";
import { assertChangesAreLive, validateProviderOptions } from "../provider-options.js";
import { CodexBackgroundInventory } from "./codex/background-inventory.js";
import {
AgentTurnStartCanceledError,
getAgentStreamEventTurnId,
type AgentPermissionAction,
type AgentCapabilityFlags,
@@ -4697,7 +4696,7 @@ export class CodexAppServerAgentSession implements AgentSession {
// outlives it into the new turn.
await this.waitForInterruptedTurnCleanup();
if (pendingStart.cancelRequested) {
throw new AgentTurnStartCanceledError();
throw new Error("Codex turn start was interrupted before reaching Codex");
}
await this.client.request("turn/start", turnStart.params, TURN_START_TIMEOUT_MS);
return { turnId };
@@ -1,5 +1,4 @@
import {
AgentTurnStartCanceledError,
getAgentStreamEventTurnId,
type AgentPromptInput,
type AgentRunOptions,
@@ -91,9 +90,6 @@ export async function runProviderTurn({
processEvent(event);
}
await completion;
} catch (error) {
if (!(error instanceof AgentTurnStartCanceledError)) throw error;
return { sessionId: await getSessionId(), finalText: "", timeline: [], canceled: true };
} finally {
unsubscribe();
}
@@ -2051,7 +2051,7 @@ function createToolCatalog(
};
}
const dispatch = await sendPromptToAgent({
const { disposition } = await sendPromptToAgent({
agentManager,
agentStorage,
agentId,
@@ -2070,14 +2070,11 @@ function createToolCatalog(
});
// Return once the provider has accepted the turn, so the status reports it running.
const { disposition } = dispatch;
if (dispatch.disposition === "turn_started") {
await waitForAgentRunStartWithTimeout(agentManager, agentId, dispatch.started).catch(
(error) => {
rejectNotification();
throw error;
},
);
if (disposition === "turn_started") {
await waitForAgentRunStartWithTimeout(agentManager, agentId).catch((error) => {
rejectNotification();
throw error;
});
}
const currentSnapshot = agentManager.getAgent(agentId);
+2 -2
View File
@@ -128,7 +128,7 @@ export async function fanOutReconciledWorkspaceUpdates(input: {
);
}
import { VoiceAssistantWebSocketServer } from "./websocket-server.js";
import { VoiceAssistantWebSocketServer, isDaemonOriginAllowed } from "./websocket-server.js";
import { WorkspaceSetupRuntime } from "./workspace-setup-runtime.js";
import { createWorkspaceLabelService } from "./workspace-labels/index.js";
import { createGitHubService } from "../services/github-service.js";
@@ -813,7 +813,7 @@ export async function createPaseoDaemon(
app.use((req, res, next) => {
const origin = req.headers.origin;
if (origin && (allowedOrigins.has("*") || allowedOrigins.has(origin))) {
if (origin && isDaemonOriginAllowed(origin, req.headers.host ?? null, allowedOrigins)) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
@@ -9,7 +9,7 @@ import {
symlinkSync,
} from "fs";
import { once } from "node:events";
import { get } from "node:http";
import { get, type IncomingMessage } from "node:http";
import { tmpdir } from "os";
import path from "path";
import { createDaemonTestContext, type DaemonTestContext } from "../test-utils/index.js";
@@ -69,6 +69,29 @@ describe("daemon E2E", () => {
}, 60000);
describe("file download tokens", () => {
test("lets a page on a loopback alias of the request's host and port fetch a download", async () => {
const cwd = tmpCwd();
writeFileSync(path.join(cwd, "download.bin"), "download bytes");
try {
// The web UI behind a remapped port (for example Docker -p 8080:6767): the page is at
// http://127.0.0.1:8080 and the app sends the request to localhost:8080. The WebSocket
// accepts this origin, so the download must as well.
const token = await ctx.client.requestDownloadToken(cwd, "download.bin");
const request = get({
host: "127.0.0.1",
port: ctx.daemon.port,
path: `/api/files/download?token=${token.token}`,
headers: { Host: "localhost:8080", Origin: "http://127.0.0.1:8080" },
});
const [response] = (await once(request, "response")) as [IncomingMessage];
response.resume();
expect(response.statusCode).toBe(200);
expect(response.headers["access-control-allow-origin"]).toBe("http://127.0.0.1:8080");
} finally {
rmSync(cwd, { recursive: true, force: true });
}
});
test.skipIf(process.platform !== "linux")(
"releases file descriptors after repeated HTTP download cancellations",
async () => {
@@ -38,7 +38,6 @@ import {
type ScheduleServiceOptions,
} from "./service.js";
import { ScheduleStore } from "./store.js";
import { ForegroundPromptStart } from "../agent/agent-run-state.js";
import type { ScheduleExecutionResult, StoredSchedule } from "@getpaseo/protocol/schedule/types";
interface ScheduleServiceInternals {
@@ -755,7 +754,6 @@ describe("ScheduleService", () => {
agent.id,
expect.stringContaining(`Schedule fired (id=${schedule.id}, run=`),
undefined,
expect.any(ForegroundPromptStart),
]);
});
-1
View File
@@ -8993,7 +8993,6 @@ export class Session {
await waitForAgentRunStartWithTimeout(
this.agentManager,
agentId,
result.started,
this.delivery.requestSignal,
);
}
@@ -50,11 +50,7 @@ export function createTestAgentMessages(params: {
runOptions: { clientMessageId: request.messageIds[0] },
});
if (result.disposition === "turn_started")
await waitForAgentRunStartWithTimeout(
params.agentManager,
request.recipientId,
result.started,
);
await waitForAgentRunStartWithTimeout(params.agentManager, request.recipientId);
return result.disposition;
},
onError: (error, request) =>
+21 -7
View File
@@ -745,7 +745,7 @@ export class VoiceAssistantWebSocketServer {
{ replayState: false },
),
dispatch: async (batch) => {
const dispatch = await sendPreparedPromptToAgent({
const { disposition } = await sendPreparedPromptToAgent({
agentManager,
agentStorage,
agentId: batch.recipientId,
@@ -758,9 +758,9 @@ export class VoiceAssistantWebSocketServer {
runOptions: { clientMessageId: batch.messageIds[0] },
logger: this.logger,
});
if (dispatch.disposition === "turn_started")
await waitForAgentRunStartWithTimeout(agentManager, batch.recipientId, dispatch.started);
return dispatch.disposition;
if (disposition === "turn_started")
await waitForAgentRunStartWithTimeout(agentManager, batch.recipientId);
return disposition;
},
onError: (err, batch) =>
this.logger.error(
@@ -998,9 +998,7 @@ export class VoiceAssistantWebSocketServer {
callback(false, 403, "Host not allowed");
return;
}
const sameOrigin = isWebSocketSameOrigin(origin, requestHost);
if (!origin || allowedOrigins.has("*") || allowedOrigins.has(origin) || sameOrigin) {
if (!origin || isDaemonOriginAllowed(origin, requestHost, allowedOrigins)) {
callback(true);
} else {
this.incrementRuntimeCounter("originRejected");
@@ -3267,6 +3265,22 @@ function isLoopbackAlias(hostname: string): boolean {
return /^127(?:\.\d{1,3}){3}$/.test(normalized);
}
/**
* Whether a page at `origin` may talk to the daemon reached at `requestHost`. The WebSocket
* upgrade and the HTTP CORS check both use it, so a page that can connect can also fetch.
*/
export function isDaemonOriginAllowed(
origin: string,
requestHost: string | null,
allowedOrigins: Set<string>,
): boolean {
return (
allowedOrigins.has("*") ||
allowedOrigins.has(origin) ||
isWebSocketSameOrigin(origin, requestHost)
);
}
export function isWebSocketSameOrigin(
origin: string | undefined,
requestHost: string | null,