Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

Read kernel protocol output in linear time (#2781)

Sebastian Müller2d24ad4e6b2d
diff --git a/packages/coding-agent/.changes/kernel-framer-linear.md b/packages/coding-agent/.changes/kernel-framer-linear.md
new file mode 100644
index 0000000000..4b2c5d5be0
--- /dev/null
+++ b/packages/coding-agent/.changes/kernel-framer-linear.md
@@ -0,0 +1 @@
+- Fixed host stalls while a kernel cell sends a multi-megabyte display output (such as a large image) or host request: the host now reads kernel output in linear time instead of rescanning the partial line on every pipe chunk.
diff --git a/packages/coding-agent/src/core/kernel/repl-manager.ts b/packages/coding-agent/src/core/kernel/repl-manager.ts
index 5246f7d025..a3c22c79d0 100644
--- a/packages/coding-agent/src/core/kernel/repl-manager.ts
+++ b/packages/coding-agent/src/core/kernel/repl-manager.ts
@@ -421,44 +421,65 @@ export class ReplKernelManager {
private wireChild(child: ChildProcess): void {
const decoder = new StringDecoder("utf8");
- let buffered = "";
+ // The unterminated line accumulates as chunk segments joined once at its
+ // newline: rescanning one growing buffer per chunk is quadratic in the size
+ // of a multi-MB frame arriving in pipe-sized reads.
+ let pending: string[] = [];
+ let pendingLength = 0;
+ // Text an early exit left unparsed (it may hold complete lines); the next
+ // chunk scans it first.
+ let unparsed = "";
// A poisoned child's residue must not grow the buffer again before the
// protocol repair kills it.
let poisoned = false;
child.stdout?.on("data", (buf: Buffer) => {
if (this.child !== child || poisoned) return;
- buffered += decoder.write(buf);
- if (buffered.length > MAX_PROTOCOL_LINE_CHARS) {
+ const text = unparsed + decoder.write(buf);
+ unparsed = "";
+ if (pendingLength + text.length > MAX_PROTOCOL_LINE_CHARS) {
poisoned = true;
- buffered = "";
+ pending = [];
+ pendingLength = 0;
this.failProtocolFrame(child, `oversized protocol line: exceeds ${MAX_PROTOCOL_LINE_CHARS} chars`);
return;
}
- let newline = buffered.indexOf("\n");
+ let start = 0;
+ let newline = text.indexOf("\n");
while (newline !== -1) {
- if (this.child !== child) return;
- const line = buffered.slice(0, newline);
- buffered = buffered.slice(newline + 1);
- newline = buffered.indexOf("\n");
+ if (this.child !== child) break;
+ pending.push(text.slice(start, newline));
+ const line = pending.join("");
+ pending = [];
+ pendingLength = 0;
+ start = newline + 1;
+ newline = text.indexOf("\n", start);
if (!line.trim()) continue;
let event: unknown;
try {
event = JSON.parse(line);
} catch {
this.failProtocolFrame(child, `unparseable protocol line: ${line.slice(0, 200)}`);
- return;
+ break;
}
if (!isRecord(event)) {
this.failProtocolFrame(child, `non-object protocol line: ${line.slice(0, 200)}`);
- return;
+ break;
}
const invalidReason = invalidProtocolFrameReason(event);
if (invalidReason) {
this.failProtocolFrame(child, `${invalidReason}: ${line.slice(0, 200)}`);
- return;
+ break;
}
this.handleEvent(event);
}
+ // A newline-free tail extends the pending line; a tail that still holds
+ // lines (left by an early exit) waits in `unparsed`.
+ if (newline === -1) {
+ pending.push(text.slice(start));
+ pendingLength += text.length - start;
+ } else {
+ unparsed = text.slice(start);
+ }
});
// The runtime dup2's fd 2 into its protocol pump before ready (repl.py
diff --git a/packages/coding-agent/test/repl-kernel-protocol-corruption.test.ts b/packages/coding-agent/test/repl-kernel-protocol-corruption.test.ts
index 89093ab2e2..910c06781c 100644
--- a/packages/coding-agent/test/repl-kernel-protocol-corruption.test.ts
+++ b/packages/coding-agent/test/repl-kernel-protocol-corruption.test.ts
@@ -1,3 +1,4 @@
+import type { ChildProcess } from "node:child_process";
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -64,6 +65,7 @@ input.on("line", (line) => {
return;
}
if (request.code === "corrupt-huge-line") return process.stdout.write("x".repeat(33 * 1024 * 1024));
+ if (request.code === "echo-id") return emit({ event: "stdout", id: request.id, text: request.id });
if (request.code === "corrupt-idle") {
process.stdout.write(JSON.stringify({ event: "done", id: request.id, status: "ok" }) + "\\n42\\n");
return;
@@ -367,6 +369,34 @@ describe("ReplKernelManager corrupt protocol repair", () => {
}
});
+ it("frames protocol lines across arbitrary stdout chunk boundaries", async () => {
+ const { manager } = newManager();
+ try {
+ let onId: (id: string) => void = () => {};
+ const echoed = new Promise<string>((resolve) => {
+ onId = resolve;
+ });
+ const result = manager.execute("echo-id", { onStream: (text) => onId(text) });
+ const id = await echoed;
+ const stdout = (manager as unknown as { child?: ChildProcess }).child?.stdout;
+ const emitChunks = (text: string, size: number) => {
+ const bytes = Buffer.from(text);
+ for (let i = 0; i < bytes.length; i += size) stdout?.emit("data", bytes.subarray(i, i + size));
+ };
+ const frame = (fields: Record<string, unknown>) => JSON.stringify({ event: "stdout", id, ...fields });
+ // Byte-sized chunks split every newline, the CRLF pair and the two-byte "é".
+ emitChunks(`${frame({ text: "-é" })}\n\n \r\n${frame({ text: "-crlf" })}\r\n`, 1);
+ // A line over 1 MiB spans many 64 KiB chunks; its last chunk also carries the done frame.
+ emitChunks(
+ `${frame({ text: "-big", pad: "x".repeat(1 << 20) })}\n${frame({ event: "done", status: "ok" })}\n`,
+ 65_536,
+ );
+ await expect(result).resolves.toMatchObject({ status: "ok", stdout: `${id}-é-crlf-big` });
+ } finally {
+ await manager.shutdown({ snapshot: true, drainHostRequests: true });
+ }
+ });
+
it("repairs an object frame with an unknown event kind instead of hanging the request", async () => {
const { manager, countPath } = newManager();
try {