Compare commits

...
Author SHA1 Message Date
paseo-bot[bot] 23f78e7a3e refactor(server): name OpenCode HTTP error arguments 2026-12-04 14:40:24 +00:00
paseo-bot[bot] 80cc7ac84b fix(server): release idle OpenCode runtimes after worktree removal
Keep checking the remaining locations when OpenCode reports that one location no longer exists. Other shell inventory failures still prevent cleanup.
2026-12-04 14:02:48 +00:00
5 changed files with 105 additions and 14 deletions
@@ -1,22 +1,46 @@
export class OpenCodeHttpError extends Error {
readonly name = "OpenCodeHttpError";
constructor(
readonly operation: string,
readonly status: number | undefined,
readonly html: boolean,
) {
readonly operation: string;
readonly status: number | undefined;
readonly html: boolean;
readonly errorTag: string | undefined;
constructor({
operation,
status,
html,
errorTag,
}: {
operation: string;
status: number | undefined;
html: boolean;
errorTag?: string;
}) {
const statusLabel = status === undefined ? "" : ` (HTTP ${status})`;
super(
html
? `OpenCode ${operation} returned HTML instead of JSON; incompatible OpenCode API${statusLabel}`
: `OpenCode ${operation} failed${statusLabel}`,
);
this.operation = operation;
this.status = status;
this.html = html;
this.errorTag = errorTag;
}
}
/** True when OpenCode answered 404, directly or as the cause of the client's transport error. */
export function isOpenCodeNotFound(error: unknown): boolean {
return getOpenCodeHttpError(error)?.status === 404;
}
export function isOpenCodeLocationNotFound(error: unknown): boolean {
const failure = getOpenCodeHttpError(error);
return failure?.status === 404 && failure.errorTag === "LocationNotFoundError";
}
function getOpenCodeHttpError(error: unknown): OpenCodeHttpError | undefined {
const failure = error instanceof OpenCodeHttpError ? error : (error as Error | null)?.cause;
return failure instanceof OpenCodeHttpError && failure.status === 404;
return failure instanceof OpenCodeHttpError ? failure : undefined;
}
@@ -13,6 +13,7 @@ import { OpenCodeV2Session } from "./session.js";
import { V2Harness } from "../test-utils/v2-harness.js";
import type { V2Api } from "./api.js";
import { V2Runtime } from "./runtime.js";
import { validateOpenCodeResponse } from "./http-response.js";
function terminalEvents(events: AgentStreamEvent[]) {
return events.filter(
@@ -2315,6 +2316,59 @@ test("OpenCode v2 runtime safety follows descendant locations before releasing t
}
});
test("OpenCode v2 runtime safety skips a missing location and still checks surviving locations", async () => {
const harness = new V2Harness();
const client = new OpenCodeV2AgentClient({
logger: createTestLogger(),
runtime: harness.runtime,
});
const session = await client.createSession({ provider: "opencode", cwd: "/tmp/project" });
const child = {
...harness.info,
id: "child",
parentID: session.id!,
location: { directory: "/tmp/child-worktree" },
};
harness.api.session.list = async (input) => ({
data: input.parentID === session.id ? [child] : [],
cursor: {},
});
let missingResponse = Response.json({ _tag: "LocationNotFoundError" }, { status: 404 });
let childRunning = true;
harness.api.shell.list = async (input) => {
const directory = input?.location?.directory ?? "";
if (directory === harness.info.location.directory) {
await validateOpenCodeResponse("http://localhost/api/shell", missingResponse);
}
return {
location: { directory },
data: childRunning
? [
{
id: "child-shell",
status: "running",
command: "sleep 7200",
cwd: directory,
shell: "sh",
file: "/tmp/out",
metadata: {},
time: { started: 1 },
},
]
: [],
};
};
try {
expect(await session.canStopRuntime!()).toBe(false);
childRunning = false;
expect(await session.canStopRuntime!()).toBe(true);
missingResponse = Response.json({}, { status: 404 });
await expect(session.canStopRuntime!()).rejects.toThrow("HTTP 404");
} finally {
await session.close();
}
});
test("reconstructs only promoted session-owned running shells and reconciles exits", async () => {
const harness = new V2Harness();
harness.history.push({
@@ -9,6 +9,7 @@ export async function validateOpenCodeResponse(
const html = contentType?.includes("text/html") ?? false;
if (response.ok && !html) return response;
let errorTag: string | undefined;
// The SDK needs the original body to decode the provider's missing-session error.
if (response.status === 404 && !html && contentType?.includes("application/json")) {
const body: unknown = await response
@@ -16,8 +17,15 @@ export async function validateOpenCodeResponse(
.json()
.catch(() => null);
if (isSessionNotFoundError(body)) return response;
if (body && typeof body === "object" && "_tag" in body && typeof body._tag === "string")
errorTag = body._tag;
}
const url = request instanceof Request ? request.url : String(request);
throw new OpenCodeHttpError(new URL(url).pathname, response.status, html);
throw new OpenCodeHttpError({
operation: new URL(url).pathname,
status: response.status,
html,
errorTag,
});
}
@@ -398,11 +398,11 @@ describe("OpenCode v2 requests that OpenCode dropped", () => {
if (state.dropped)
// The client reports a failed response as a transport error caused by Paseo's check.
throw new ClientError("Transport", {
cause: new OpenCodeHttpError(
`/api/session/${input.sessionID}/permission/${input.requestID}/reply`,
404,
false,
),
cause: new OpenCodeHttpError({
operation: `/api/session/${input.sessionID}/permission/${input.requestID}/reply`,
status: 404,
html: false,
}),
});
state.dropped = true;
};
@@ -1,6 +1,7 @@
import type { ProviderOptions } from "@getpaseo/protocol/agent-types";
import { applyProviderOptionsPatch } from "../../../option-controls.js";
import { assertChangesAreLive, validateProviderOptions } from "../../../provider-options.js";
import { isOpenCodeLocationNotFound } from "../http-error.js";
import { OpenCodeProviderOptionsSchema } from "../options.js";
import { OpenCodeBackgroundInventory } from "./background-inventory.js";
import { SessionChildren } from "./children.js";
@@ -813,8 +814,12 @@ export class OpenCodeV2Session implements AgentSession {
} while (cursor);
}
for (const directory of directories) {
const shells = await this.client.shell.list({ location: { directory } }, request);
if (shells.data.some((shell) => shell.status === "running")) return false;
try {
const shells = await this.client.shell.list({ location: { directory } }, request);
if (shells.data.some((shell) => shell.status === "running")) return false;
} catch (error) {
if (!isOpenCodeLocationNotFound(error)) throw error;
}
}
return !this.turns.id && !this.dirty;
}