Compare commits

...
Author SHA1 Message Date
paseo-bot[bot] 282cbae1c1 fix(omp): report startup failures without waiting for readiness
OMP waited for a child exit event that a failed spawn never emits. Use the transport close reason to settle readiness while preserving process exit for session consumers.
2026-12-02 11:30:38 +00:00
3 changed files with 70 additions and 35 deletions
@@ -90,7 +90,7 @@ export class JsonlRpcProcess {
private readonly exitSubscribers = new Set<(exit: JsonlRpcExit) => void>();
private stderrBuffer = "";
private nextRequestId = 1;
private disposed = false;
private readonly closeController = new AbortController();
private streamFailure: ProviderStreamLimitError | null = null;
private exited = false;
private stopTimedOut = false;
@@ -147,6 +147,11 @@ export class JsonlRpcProcess {
});
}
/** Aborts with the error that made the transport unusable, even before child exit. */
get closed(): AbortSignal {
return this.closeController.signal;
}
onMessage(callback: (message: Record<string, unknown>) => void): () => void {
this.messageSubscribers.add(callback);
return () => {
@@ -165,7 +170,7 @@ export class JsonlRpcProcess {
command: { type: string; [key: string]: unknown },
timeoutMs?: number | null,
): { id: string; promise: Promise<unknown> } {
if (this.disposed) {
if (this.closed.aborted) {
return {
id: "",
promise: Promise.reject(new Error(`${this.diagnosticName} process is closed`)),
@@ -258,7 +263,7 @@ export class JsonlRpcProcess {
}
send(message: Record<string, unknown>): void {
if (this.disposed) {
if (this.closed.aborted) {
return;
}
if (this.child.stdin.destroyed || !this.child.stdin.writable) {
@@ -274,7 +279,7 @@ export class JsonlRpcProcess {
async close(error = new Error(`${this.diagnosticName} process is closed`)): Promise<void> {
if (this.closing) return this.closing;
if (this.disposed) return;
if (this.closed.aborted) return;
this.failAll(error);
this.closing = this.terminate();
await this.closing;
@@ -350,7 +355,7 @@ export class JsonlRpcProcess {
}
private handleStdinError(error: unknown): void {
if (this.disposed) {
if (this.closed.aborted) {
return;
}
const err = error instanceof Error ? error : new Error(String(error));
@@ -359,10 +364,10 @@ export class JsonlRpcProcess {
}
private failAll(error: Error): void {
if (this.disposed) {
if (this.closed.aborted) {
return;
}
this.disposed = true;
this.closeController.abort(error);
for (const pending of this.pending.values()) {
if (pending.timer) {
clearTimeout(pending.timer);
@@ -1,11 +1,15 @@
import { getEventListeners } from "node:events";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import pino from "pino";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { JsonlRpcExit } from "../jsonl-rpc-process.js";
import { JsonlRpcProcess } from "../jsonl-rpc-process.js";
import { establishOmpProtocol, type OmpProtocolTransport } from "./protocol-session.js";
function transportHarness() {
let receiveMessage: ((message: Record<string, unknown>) => void) | null = null;
let receiveExit: ((exit: JsonlRpcExit) => void) | null = null;
const closeController = new AbortController();
const requests: Array<{ command: Record<string, unknown>; timeoutMs: number | null }> = [];
let response: unknown = { protocolVersion: 2 };
const transport: OmpProtocolTransport = {
@@ -15,12 +19,7 @@ function transportHarness() {
receiveMessage = null;
};
},
onExit(receiver) {
receiveExit = receiver;
return () => {
receiveExit = null;
};
},
closed: closeController.signal,
async request(command, timeoutMs) {
requests.push({ command, timeoutMs });
return response;
@@ -33,7 +32,7 @@ function transportHarness() {
setResponse: (value: unknown) => {
response = value;
},
exitSubscribed: () => receiveExit !== null,
closeSubscribed: () => getEventListeners(transport.closed, "abort").length > 0,
};
}
@@ -49,6 +48,39 @@ afterEach(() => {
});
describe("establishOmpProtocol", () => {
it("reports an unavailable working directory during startup", async () => {
const root = await mkdtemp(join(tmpdir(), "omp-startup-"));
const logger = pino({ level: "silent" });
const transport = new JsonlRpcProcess({
launch: { command: process.execPath, args: ["--version"], cwd: join(root, "missing") },
logger,
});
try {
await expect(
establishOmpProtocol(transport, logger, { readyTimeoutMs: 300 }),
).rejects.toMatchObject({ code: "ENOENT" });
} finally {
await transport.close();
await rm(root, { recursive: true, force: true });
}
});
it("reports why startup was canceled when the transport closes", async () => {
const logger = pino({ level: "silent" });
const transport = new JsonlRpcProcess({
launch: {
command: process.execPath,
args: ["-e", "process.stdin.resume()"],
cwd: process.cwd(),
},
logger,
});
const reason = new Error("Provider refresh canceled");
const result = expect(establishOmpProtocol(transport, logger)).rejects.toBe(reason);
await transport.close(reason);
await result;
});
it("owns readiness and v2 negotiation", async () => {
const harness = transportHarness();
const negotiation = establishOmpProtocol(harness.transport, pino({ level: "silent" }));
@@ -60,7 +92,7 @@ describe("establishOmpProtocol", () => {
timeoutMs: 30_000,
},
]);
expect(harness.exitSubscribed()).toBe(false);
expect(harness.closeSubscribed()).toBe(false);
});
it("uses the configured RPC timeout while waiting for a cold OMP", async () => {
@@ -92,12 +124,12 @@ describe("establishOmpProtocol", () => {
);
await vi.advanceTimersByTimeAsync(19_999);
expect(harness.exitSubscribed()).toBe(true);
expect(harness.closeSubscribed()).toBe(true);
expect(await Promise.race([outcome, Promise.resolve("pending")])).toBe("pending");
await vi.advanceTimersByTimeAsync(1);
await expect(outcome).resolves.toBe("Timed out waiting for OMP to become ready");
expect(harness.exitSubscribed()).toBe(false);
expect(harness.closeSubscribed()).toBe(false);
});
it("keeps protocol v1 when the ready frame has no matching capability", async () => {
@@ -1,15 +1,12 @@
import { addAbortListener } from "node:events";
import type { Logger } from "pino";
import {
JSONL_RPC_DEFAULT_TIMEOUT_MS,
supportsJsonlRpcProtocolV2,
type JsonlRpcExit,
} from "../jsonl-rpc-process.js";
import { JSONL_RPC_DEFAULT_TIMEOUT_MS, supportsJsonlRpcProtocolV2 } from "../jsonl-rpc-process.js";
const OMP_READY_TIMEOUT_MS = 20_000;
export interface OmpProtocolTransport {
onMessage(callback: (message: Record<string, unknown>) => void): () => void;
onExit(callback: (exit: JsonlRpcExit) => void): () => void;
readonly closed: AbortSignal;
request(command: Record<string, unknown>, timeoutMs: number | null): Promise<unknown>;
}
@@ -37,28 +34,29 @@ export async function establishOmpProtocol(
function waitForReady(
transport: OmpProtocolTransport,
requestTimeoutMs: number,
readyTimeoutMs: number,
): Promise<Record<string, unknown>> {
return new Promise((resolve, reject) => {
transport.closed.throwIfAborted();
let settled = false;
let unsubscribeMessage = (): void => {};
let unsubscribeExit = (): void => {};
const timer = setTimeout(
() => finish(new Error("Timed out waiting for OMP to become ready")),
requestTimeoutMs,
() => finish(() => reject(new Error("Timed out waiting for OMP to become ready"))),
readyTimeoutMs,
);
const finish = (result: Record<string, unknown> | Error): void => {
const finish = (settle: () => void): void => {
if (settled) return;
settled = true;
clearTimeout(timer);
unsubscribeMessage();
unsubscribeExit();
if (result instanceof Error) reject(result);
else resolve(result);
closeListener[Symbol.dispose]();
settle();
};
const closeListener = addAbortListener(transport.closed, () =>
finish(() => reject(transport.closed.reason)),
);
unsubscribeMessage = transport.onMessage((message) => {
if (message.type === "ready") finish(message);
if (message.type === "ready") finish(() => resolve(message));
});
unsubscribeExit = transport.onExit(({ error }) => finish(error));
});
}