Skip to content

The open-source app everyone uses to manage agents at work

feat(runner): sequence ACPX sidecar input (#12389)

Dotta7a8c6825a531
diff --git a/packages/paperclip-runner/src/cli/acpx-sidecar-input.test.ts b/packages/paperclip-runner/src/cli/acpx-sidecar-input.test.ts
new file mode 100644
index 0000000000..c9f2f613f6
--- /dev/null
+++ b/packages/paperclip-runner/src/cli/acpx-sidecar-input.test.ts
@@ -0,0 +1,115 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ acpxBootstrapBlockedError,
+ enqueueAcpxSidecarInput,
+ recordAcpxBootstrapFailure,
+} from "./acpx-sidecar-input.js";
+
+describe("ACPX sidecar input sequencing", () => {
+ it("drains initialize, session.open, and suspend in input order", async () => {
+ const events: string[] = [];
+ let pending = Promise.resolve();
+ for (const command of ["initialize", "session.open", "session.suspend"]) {
+ pending = enqueueAcpxSidecarInput(
+ pending,
+ async () => {
+ events.push(command);
+ },
+ () => events.push(`${command}:error`),
+ );
+ }
+ const shutdown = pending.then(() => events.push("shutdown"));
+
+ await shutdown;
+ expect(events).toEqual([
+ "initialize",
+ "session.open",
+ "session.suspend",
+ "shutdown",
+ ]);
+ });
+
+ it("keeps the queue live after operation and diagnostic failures", async () => {
+ const events: string[] = [];
+ const failed = enqueueAcpxSidecarInput(
+ Promise.resolve(),
+ async () => {
+ events.push("failed");
+ throw new Error("bad frame");
+ },
+ async () => {
+ events.push("diagnostic");
+ await Promise.resolve();
+ throw new Error("diagnostic transport failed");
+ },
+ );
+ const recovered = enqueueAcpxSidecarInput(
+ failed,
+ async () => {
+ events.push("recovered");
+ },
+ () => events.push("unexpected"),
+ );
+
+ await recovered;
+ expect(events).toEqual(["failed", "diagnostic", "recovered"]);
+ });
+
+ it("does not let an unbounded diagnostic block later input or shutdown", async () => {
+ const events: string[] = [];
+ const neverSettles = new Promise<void>(() => undefined);
+ const failed = enqueueAcpxSidecarInput(
+ Promise.resolve(),
+ async () => {
+ events.push("failed");
+ throw new Error("bad frame");
+ },
+ () => {
+ events.push("diagnostic");
+ return neverSettles;
+ },
+ );
+ const recovered = enqueueAcpxSidecarInput(
+ failed,
+ async () => {
+ events.push("recovered");
+ },
+ () => events.push("unexpected"),
+ );
+
+ await expect(
+ Promise.race([
+ recovered.then(() => "drained"),
+ new Promise<string>((resolve) => {
+ setTimeout(() => resolve("blocked"), 50);
+ }),
+ ]),
+ ).resolves.toBe("drained");
+ expect(events).toEqual(["failed", "diagnostic", "recovered"]);
+ });
+
+ it("preserves the first bootstrap failure and blocks dependent commands", () => {
+ const rootCause = new Error("agent initialize exited");
+ const failure = recordAcpxBootstrapFailure(null, "session.open", rootCause);
+
+ expect(failure).toBe(rootCause);
+ expect(acpxBootstrapBlockedError(failure, "session.suspend")?.message).toBe(
+ "ACPX provider bootstrap failed before session.suspend: agent initialize exited",
+ );
+ expect(
+ recordAcpxBootstrapFailure(
+ failure,
+ "session.suspend",
+ new Error("secondary"),
+ ),
+ ).toBe(rootCause);
+ });
+
+ it("does not make an ordinary command failure a sticky bootstrap error", () => {
+ expect(
+ recordAcpxBootstrapFailure(null, "turn.start", new Error("turn failed")),
+ ).toBeNull();
+ expect(acpxBootstrapBlockedError(null, "turn.start")).toBeNull();
+ });
+});
diff --git a/packages/paperclip-runner/src/cli/acpx-sidecar-input.ts b/packages/paperclip-runner/src/cli/acpx-sidecar-input.ts
new file mode 100644
index 0000000000..f6f4a116c6
--- /dev/null
+++ b/packages/paperclip-runner/src/cli/acpx-sidecar-input.ts
@@ -0,0 +1,30 @@
+import { enqueueSerialInput } from "./serial-input-queue.js";
+
+const ACPX_BOOTSTRAP_COMMANDS = new Set(["initialize", "session.open"]);
+
+export function enqueueAcpxSidecarInput(
+ pending: Promise<void>,
+ operation: () => Promise<void>,
+ onError: (error: unknown) => void | Promise<void>,
+): Promise<void> {
+ return enqueueSerialInput(pending, operation, onError);
+}
+
+export function recordAcpxBootstrapFailure(
+ current: Error | null,
+ command: string,
+ error: Error,
+): Error | null {
+ return current ?? (ACPX_BOOTSTRAP_COMMANDS.has(command) ? error : null);
+}
+
+export function acpxBootstrapBlockedError(
+ failure: Error | null,
+ command: string,
+): Error | null {
+ return failure
+ ? new Error(
+ `ACPX provider bootstrap failed before ${command}: ${failure.message}`,
+ )
+ : null;
+}
diff --git a/packages/paperclip-runner/src/cli/serial-input-queue.ts b/packages/paperclip-runner/src/cli/serial-input-queue.ts
new file mode 100644
index 0000000000..b927b8ece0
--- /dev/null
+++ b/packages/paperclip-runner/src/cli/serial-input-queue.ts
@@ -0,0 +1,21 @@
+/**
+ * Append one asynchronous input operation to a queue that always remains
+ * usable after an operation or diagnostic callback fails.
+ */
+export function enqueueSerialInput(
+ pending: Promise<void>,
+ operation: () => Promise<void>,
+ onError: (error: unknown) => void | Promise<void>,
+): Promise<void> {
+ const reportError = (error: unknown): void => {
+ try {
+ // Diagnostics are best-effort side effects, not input-queue work. Start
+ // them in order, but never give an uncooperative callback authority to
+ // block later frames or the shutdown drain.
+ void Promise.resolve(onError(error)).catch(() => undefined);
+ } catch {
+ // Diagnostics must not poison the queue or skip later input frames.
+ }
+ };
+ return pending.catch(reportError).then(operation).catch(reportError);
+}