Skip to content

Commit 01690cd

Browse files
only21milclaude
andcommitted
fix(pi): log oversized RPC records instead of dropping them silently
The Pi stdout framer drops any record over MAX_PI_RECORD_CHARS with no trace. When the dropped record is a response, its request only fails later as a 30-second PiRpcTimeoutError, and nothing in the logs says why. The framer now reports the length of each dropped record, and the reader logs a warning with that length and the cap. Warning level shows at the server's default Info level. The log carries lengths only, never record content. Dropping behavior is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 05696c6 commit 01690cd

2 files changed

Lines changed: 90 additions & 12 deletions

File tree

‎packages/provider-pi/src/server/adapter.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as DateTime from "effect/DateTime";
2323
import * as Effect from "effect/Effect";
2424
import * as Fiber from "effect/Fiber";
2525
import * as Layer from "effect/Layer";
26+
import * as Logger from "effect/Logger";
2627
import * as Queue from "effect/Queue";
2728
import * as Schema from "effect/Schema";
2829
import * as Sink from "effect/Sink";
@@ -4001,6 +4002,57 @@ describe("PiRpc framing", () => {
40014002
assert.equal((yield* Queue.take(connection.events))["type"], "after_oversized");
40024003
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
40034004
);
4005+
4006+
it.effect("logs the length of an oversized record it drops", () => {
4007+
const warnings: Array<ReadonlyArray<unknown>> = [];
4008+
const logCapture = Logger.make<unknown, void>(({ logLevel, message }) => {
4009+
if (logLevel === "Warn" && Array.isArray(message)) warnings.push(message);
4010+
});
4011+
return Effect.gen(function* () {
4012+
const stdout = yield* Queue.unbounded<Uint8Array>();
4013+
const spawner = ChildProcessSpawner.make(() =>
4014+
Effect.succeed(
4015+
ChildProcessSpawner.makeHandle({
4016+
pid: ChildProcessSpawner.ProcessId(FAKE_PID),
4017+
exitCode: Effect.never,
4018+
isRunning: Effect.succeed(true),
4019+
kill: () => Effect.void,
4020+
unref: Effect.succeed(Effect.void),
4021+
stdin: Sink.drain,
4022+
stdout: Stream.fromQueue(stdout),
4023+
stderr: Stream.empty,
4024+
all: Stream.empty,
4025+
getInputFd: () => Sink.drain,
4026+
getOutputFd: () => Stream.empty,
4027+
}),
4028+
),
4029+
);
4030+
const connection = yield* makePiRpcConnection({
4031+
command: "pi",
4032+
args: ["--mode", "rpc"],
4033+
cwd: undefined,
4034+
env: {},
4035+
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
4036+
4037+
const head = '{"type":"response","id":"t3-0","success":true,"data":"';
4038+
const tail = '"}\n{"type":"after_oversized"}\n';
4039+
const chunk = new TextEncoder().encode("x".repeat(8 * 1024 * 1024));
4040+
yield* Queue.offer(stdout, new TextEncoder().encode(head));
4041+
for (let index = 0; index < 32; index++) yield* Queue.offer(stdout, chunk);
4042+
yield* Queue.offer(stdout, new TextEncoder().encode(tail));
4043+
4044+
assert.equal((yield* Queue.take(connection.events))["type"], "after_oversized");
4045+
assert.lengthOf(warnings, 1);
4046+
const [text, annotations] = warnings[0] ?? [];
4047+
assert.equal(text, "Dropping oversized pi stdout record.");
4048+
assert.propertyVal(annotations, "recordLength", head.length + 32 * chunk.length + 2);
4049+
}).pipe(
4050+
Effect.scoped,
4051+
Effect.provide(
4052+
Layer.merge(NodeServices.layer, Logger.layer([logCapture], { mergeWithExisting: false })),
4053+
),
4054+
);
4055+
});
40044056
});
40054057

40064058
describe("PiRpc early process exit", () => {

‎packages/provider-pi/src/server/rpc.ts‎

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,35 +99,58 @@ interface PendingPiRequest {
9999
const MAX_PI_RECORD_CHARS =
100100
2 * MAX_TOOL_OUTPUT_IMAGES * MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH + 8 * 1024 * 1024;
101101

102+
interface PiJsonlFrames {
103+
readonly lines: ReadonlyArray<string>;
104+
readonly droppedLengths: ReadonlyArray<number>;
105+
}
106+
102107
function makeJsonlFramer() {
103108
let buffer = "";
104109
let dropping = false;
105-
return (chunk: string): ReadonlyArray<string> => {
110+
let droppedLength = 0;
111+
return (chunk: string): PiJsonlFrames => {
106112
const lines: string[] = [];
113+
const droppedLengths: number[] = [];
107114
let start = 0;
108115
while (start < chunk.length) {
109116
const newline = chunk.indexOf("\n", start);
110117
const end = newline < 0 ? chunk.length : newline;
111-
if (!dropping) {
112-
if (buffer.length + end - start > MAX_PI_RECORD_CHARS) {
113-
buffer = "";
114-
dropping = true;
115-
} else {
116-
buffer += chunk.slice(start, end);
117-
}
118+
if (dropping) {
119+
droppedLength += end - start;
120+
} else if (buffer.length + end - start > MAX_PI_RECORD_CHARS) {
121+
droppedLength = buffer.length + end - start;
122+
buffer = "";
123+
dropping = true;
124+
} else {
125+
buffer += chunk.slice(start, end);
118126
}
119127
if (newline < 0) break;
120-
if (!dropping && buffer.length > 0) {
128+
if (dropping) {
129+
droppedLengths.push(droppedLength);
130+
} else if (buffer.length > 0) {
121131
lines.push(buffer.endsWith("\r") ? buffer.slice(0, -1) : buffer);
122132
}
123133
buffer = "";
124134
dropping = false;
125135
start = newline + 1;
126136
}
127-
return lines;
137+
return { lines, droppedLengths };
128138
};
129139
}
130140

141+
// A dropped `response` leaves its request to time out, so the drop must be
142+
// visible at the default log level. Lengths only: records carry prompt text.
143+
const logDroppedPiRecords = (droppedLengths: ReadonlyArray<number>) =>
144+
Effect.forEach(
145+
droppedLengths,
146+
(recordLength) =>
147+
Effect.logWarning("Dropping oversized pi stdout record.", {
148+
recordLength,
149+
maxRecordLength: MAX_PI_RECORD_CHARS,
150+
}),
151+
{ discard: true },
152+
);
153+
131154
const UnknownFromJsonString = Schema.fromJsonString(Schema.Unknown);
132155
const decodeJsonLine = Schema.decodeSync(UnknownFromJsonString);
133156
const encodeJsonLine = Schema.encodeSync(UnknownFromJsonString);
@@ -310,7 +333,8 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
310333
Stream.decodeText(),
311334
Stream.runForEach((chunk) =>
312335
Effect.gen(function* () {
313-
const lines = frame(chunk);
336+
const { lines, droppedLengths } = frame(chunk);
337+
yield* logDroppedPiRecords(droppedLengths);
314338
for (const line of lines) {
315339
const record = parsePiRecord(line);
316340
if (record === undefined) {
@@ -324,7 +348,9 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
324348
}),
325349
),
326350
);
327-
for (const line of frame("\n")) {
351+
const flushed = frame("\n");
352+
yield* logDroppedPiRecords(flushed.droppedLengths);
353+
for (const line of flushed.lines) {
328354
const trailing = parsePiRecord(line);
329355
if (trailing !== undefined) yield* routeRecord(trailing);
330356
}

0 commit comments

Comments
 (0)