From 40a29d7f2a59fb9e775a3f02e98fb64020a2cc7a Mon Sep 17 00:00:00 2001 From: LordMelkor Date: Sat, 21 Nov 2026 23:43:52 -0500 Subject: [PATCH 1/3] Search new process output for the first newline Co-authored-by: Jello Signed-off-by: LordMelkor --- src/bundled/pi/pi.ts | 6 ++++- src/features/agent-harness/acp/client.test.ts | 23 +++++++++++++++++++ src/features/agent-harness/acp/process.ts | 14 +++++++---- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/bundled/pi/pi.ts b/src/bundled/pi/pi.ts index 1276d8b1d5..f4ab8c9493 100644 --- a/src/bundled/pi/pi.ts +++ b/src/bundled/pi/pi.ts @@ -275,6 +275,7 @@ export class Pi { }; }); let buffer = ""; + let newline = -1; let stderr = ""; const connection: Connection = { lifetime: new AbortController(), @@ -402,8 +403,11 @@ export class Pi { } : {}), onStdout: (data) => { + if (newline < 0) { + const next = data.indexOf("\n"); + if (next >= 0) newline = buffer.length + next; + } buffer += data; - let newline = buffer.indexOf("\n"); while (newline >= 0) { const line = buffer.slice(0, newline).trim(); buffer = buffer.slice(newline + 1); diff --git a/src/features/agent-harness/acp/client.test.ts b/src/features/agent-harness/acp/client.test.ts index 0f85a1f109..1292138802 100644 --- a/src/features/agent-harness/acp/client.test.ts +++ b/src/features/agent-harness/acp/client.test.ts @@ -256,6 +256,29 @@ describe("starting a harness", () => { }); }); + it("delivers complete stdout replies in order and holds the unfinished reply", async () => { + const harness = fakeHarness(); + const acp = await start(harness); + const delivered: unknown[] = []; + const replies = [1, 2, 3].map(() => + acp.request("example", {}).then((answer) => delivered.push(answer)), + ); + const lines = harness + .sent("example") + .map(({ id }, index) => + JSON.stringify({ jsonrpc: "2.0", id, result: index + 1 }), + ); + const stdout = harness.started[0]?.options.onStdout; + stdout?.(" \r\n" + lines[0]?.slice(0, 5)); + stdout?.(lines[0]?.slice(5) + "\r"); + stdout?.("\n " + lines[1] + " \n\n" + lines[2]); + await Promise.all(replies.slice(0, 2)); + expect(delivered).toEqual([1, 2]); + stdout?.("\n"); + await Promise.all(replies); + expect(delivered).toEqual([1, 2, 3]); + }); + it("refuses a harness that cannot take Buzz tools over HTTP MCP", async () => { for (const http of [false, undefined, "yes"]) { const harness = fakeHarness({ diff --git a/src/features/agent-harness/acp/process.ts b/src/features/agent-harness/acp/process.ts index fc8d495094..1c98cfed58 100644 --- a/src/features/agent-harness/acp/process.ts +++ b/src/features/agent-harness/acp/process.ts @@ -101,6 +101,7 @@ export class AcpProcess { private readonly calls = new Map(); private requests = 0; private buffer = ""; + private newline = -1; private stderr = ""; private alive = true; private code: number | null | undefined; @@ -520,12 +521,15 @@ export class AcpProcess { this.stderr = (this.stderr + data).slice(-STDERR_LIMIT); } private read(data: string) { + if (this.newline < 0) { + const next = data.indexOf("\n"); + if (next >= 0) this.newline = this.buffer.length + next; + } this.buffer += data; - let newline = this.buffer.indexOf("\n"); - while (newline >= 0) { - const line = this.buffer.slice(0, newline).trim(); - this.buffer = this.buffer.slice(newline + 1); - newline = this.buffer.indexOf("\n"); + while (this.newline >= 0) { + const line = this.buffer.slice(0, this.newline).trim(); + this.buffer = this.buffer.slice(this.newline + 1); + this.newline = this.buffer.indexOf("\n"); if (!line) continue; let message: Wire; try { From cd5b2e62252f390a851ca2d732be289040abb5ec Mon Sep 17 00:00:00 2001 From: LordMelkor Date: Sat, 21 Nov 2026 23:48:31 -0500 Subject: [PATCH 2/3] Check ACP and Pi replies at every stdout split Co-authored-by: Jello Signed-off-by: LordMelkor --- src/bundled/pi/pi.test.ts | 24 +++++++++++ src/features/agent-harness/acp/client.test.ts | 33 ++++++--------- tests/reply-chunking.ts | 40 +++++++++++++++++++ 3 files changed, 76 insertions(+), 21 deletions(-) create mode 100644 tests/reply-chunking.ts diff --git a/src/bundled/pi/pi.test.ts b/src/bundled/pi/pi.test.ts index 98e448e7dd..fddee1fa89 100644 --- a/src/bundled/pi/pi.test.ts +++ b/src/bundled/pi/pi.test.ts @@ -1,4 +1,5 @@ import { expect, it, vi } from "vitest"; +import { checkReplyChunking } from "../../../tests/reply-chunking"; import { AGENT_KEY } from "../../features/agents/membership"; import hostSource from "./host.mjs?raw"; import manifest from "./manifest.json"; @@ -104,6 +105,29 @@ it("reads messages split across chunks, and several in one", async () => { }); }); +it("delivers the same Pi replies at every stdout split", async () => { + await checkReplyChunking(async () => { + const written = deferred(); + const hold = deferred(); + const host = fakeHost({ + example: ({ sequence }) => { + if (sequence === 3) written.resolve(); + return hold.promise; + }, + }); + const pi = new Pi(() => host.spawn, handlers()); + const replies = [1, 2, 3].map((sequence) => + pi.request("example", { sequence }), + ); + await written.promise; + return { + replies, + ids: host.requests("example").map((request) => request.id), + stdout: (data) => host.processes[0]?.options.onStdout?.(data), + }; + }); +}); + it("says why a host could not start, and starts another for the next request", async () => { const host = fakeHost({ ping: () => ({ pong: true }) }, { ready: false }); const pi = new Pi(() => host.spawn, handlers()); diff --git a/src/features/agent-harness/acp/client.test.ts b/src/features/agent-harness/acp/client.test.ts index 1292138802..cc2a82eb3c 100644 --- a/src/features/agent-harness/acp/client.test.ts +++ b/src/features/agent-harness/acp/client.test.ts @@ -4,6 +4,7 @@ import { fakeSpawn as fakeGoose, flush, } from "../../../bundled/goose/goose-testing"; +import { checkReplyChunking } from "../../../../tests/reply-chunking"; import { GOOSE } from "../../../bundled/goose/goose"; import { AgentProcess } from "../../../bundled/goose/sessions"; import type { @@ -256,27 +257,17 @@ describe("starting a harness", () => { }); }); - it("delivers complete stdout replies in order and holds the unfinished reply", async () => { - const harness = fakeHarness(); - const acp = await start(harness); - const delivered: unknown[] = []; - const replies = [1, 2, 3].map(() => - acp.request("example", {}).then((answer) => delivered.push(answer)), - ); - const lines = harness - .sent("example") - .map(({ id }, index) => - JSON.stringify({ jsonrpc: "2.0", id, result: index + 1 }), - ); - const stdout = harness.started[0]?.options.onStdout; - stdout?.(" \r\n" + lines[0]?.slice(0, 5)); - stdout?.(lines[0]?.slice(5) + "\r"); - stdout?.("\n " + lines[1] + " \n\n" + lines[2]); - await Promise.all(replies.slice(0, 2)); - expect(delivered).toEqual([1, 2]); - stdout?.("\n"); - await Promise.all(replies); - expect(delivered).toEqual([1, 2, 3]); + it("delivers the same ACP replies at every stdout split", async () => { + await checkReplyChunking(async () => { + const harness = fakeHarness({ silent: ["example"] }); + const acp = await start(harness); + const replies = [1, 2, 3].map(() => acp.request("example", {})); + return { + replies, + ids: harness.sent("example").map((request) => request.id), + stdout: (data) => harness.started[0]?.options.onStdout?.(data), + }; + }); }); it("refuses a harness that cannot take Buzz tools over HTTP MCP", async () => { diff --git a/tests/reply-chunking.ts b/tests/reply-chunking.ts new file mode 100644 index 0000000000..7fd01e063f --- /dev/null +++ b/tests/reply-chunking.ts @@ -0,0 +1,40 @@ +import { expect } from "vitest"; + +type Wire = { + ids: (number | string | undefined)[]; + replies: Promise[]; + stdout: (chunk: string) => void; +}; + +export async function checkReplyChunking(open: () => Promise) { + async function deliver(split: (payload: string) => string[]) { + const { ids, replies, stdout } = await open(); + expect(ids).toHaveLength(3); + const lines = ids.map((id, index) => + JSON.stringify({ jsonrpc: "2.0", id, result: index + 1 }), + ); + const payload = `${lines[1]}\r\n\n ${lines[0]} \n${lines[2]}`; + const delivered: unknown[] = []; + const answers = replies.map((reply) => + reply.then((value) => delivered.push(value)), + ); + for (const chunk of split(payload)) stdout(chunk); + await Promise.all(answers.slice(0, 2)); + const beforeNewline = [...delivered]; + stdout("\n"); + await Promise.all(answers); + return { length: payload.length, beforeNewline, delivered }; + } + + const whole = await deliver((payload) => [payload]); + expect(whole.beforeNewline).toEqual([2, 1]); + expect(whole.delivered).toEqual([2, 1, 3]); + for (let cut = 0; cut <= whole.length; cut++) { + const split = await deliver((payload) => [ + payload.slice(0, cut), + payload.slice(cut), + ]); + expect(split, `split at ${cut}`).toEqual(whole); + } + expect(await deliver((payload) => [...payload])).toEqual(whole); +} From c40797c0122473dba517a5f06a134133859c52bf Mon Sep 17 00:00:00 2001 From: LordMelkor Date: Sun, 22 Nov 2026 00:42:43 -0500 Subject: [PATCH 3/3] Frame Codex output without rescanning partial lines Co-authored-by: Jello Signed-off-by: LordMelkor --- src/bundled/codex/rpc.test.ts | 18 ++++++++++++++++++ src/bundled/codex/rpc.ts | 17 +++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/bundled/codex/rpc.test.ts b/src/bundled/codex/rpc.test.ts index da2112f2a7..5794ab5064 100644 --- a/src/bundled/codex/rpc.test.ts +++ b/src/bundled/codex/rpc.test.ts @@ -1,4 +1,5 @@ import { expect, it, vi } from "vitest"; +import { checkReplyChunking } from "../../../tests/reply-chunking"; import type { HostProcessOptions } from "../../features/host/service"; import { AppServer, type Wire } from "./rpc"; function fixture() { @@ -60,6 +61,23 @@ it("correlates out-of-order JSONL responses across chunk boundaries and rejects expect(two).not.toHaveBeenCalled(); await f.rpc.close(); }); +it("delivers the same Codex replies at every stdout split", async () => { + const servers: AppServer[] = []; + try { + await checkReplyChunking(async () => { + const f = fixture(); + servers.push(f.rpc); + await f.open(); + const replies = Array.from({ length: 3 }, () => f.rpc.request("example")); + const ids = f.sent + .filter((wire) => wire.method === "example") + .map((wire) => wire.id); + return { replies, ids, stdout: f.raw }; + }); + } finally { + await Promise.all(servers.map((rpc) => rpc.close())); + } +}); it("contains consumer exceptions and closes malformed output while rejecting pending work", async () => { const f = fixture(); await f.open(); diff --git a/src/bundled/codex/rpc.ts b/src/bundled/codex/rpc.ts index fb01468627..e11e901ab0 100644 --- a/src/bundled/codex/rpc.ts +++ b/src/bundled/codex/rpc.ts @@ -57,6 +57,7 @@ export class AppServer { private process?: HostProcess; private failure?: Error; private buffer = ""; + private newline = -1; private stderr = ""; private listeners = new Set<(message: Wire) => void>(); private readonly unconfirmedReleases = new Set(); @@ -135,6 +136,7 @@ export class AppServer { this.closing = undefined; this.gone = false; this.buffer = this.stderr = ""; + this.newline = -1; this.unconfirmedReleases.clear(); return this.open(spawn, cwd, protectedRuntime); } @@ -172,13 +174,16 @@ export class AppServer { // Always return to Tauri's channel handler, including malformed output or // a throwing consumer; otherwise the final exit event can be stranded. try { + if (this.newline < 0) { + const next = data.indexOf("\n"); + if (next >= 0) this.newline = this.buffer.length + next; + } this.buffer += data; - let newline = this.buffer.indexOf("\n"); - while (newline >= 0) { - if (newline > 4 * 1024 * 1024) throw new Oversized(); - const line = this.buffer.slice(0, newline); - this.buffer = this.buffer.slice(newline + 1); - newline = this.buffer.indexOf("\n"); + while (this.newline >= 0) { + if (this.newline > 4 * 1024 * 1024) throw new Oversized(); + const line = this.buffer.slice(0, this.newline); + this.buffer = this.buffer.slice(this.newline + 1); + this.newline = this.buffer.indexOf("\n"); if (!line.trim()) continue; const message: Wire = JSON.parse(line); const waiter =