Skip to content

Commit c3303fb

Browse files
only21milclaudemaria-rcks
authored
fix(pi): log oversized RPC records instead of dropping them silently (pingdotgg#23947)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent cedf8b4 commit c3303fb

2 files changed

Lines changed: 49 additions & 9 deletions

File tree

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as Exit from "effect/Exit";
2727
import * as Fiber from "effect/Fiber";
2828
import * as FileSystem from "effect/FileSystem";
2929
import * as Layer from "effect/Layer";
30+
import * as Logger from "effect/Logger";
3031
import * as Queue from "effect/Queue";
3132
import * as Schema from "effect/Schema";
3233
import * as Sink from "effect/Sink";
@@ -4295,8 +4296,16 @@ describe("PiAdapterV2", () => {
42954296
});
42964297

42974298
describe("PiRpc framing", () => {
4298-
it.effect("reassembles records across chunk boundaries and strips CR", () =>
4299-
Effect.gen(function* () {
4299+
it.effect("reassembles records and warns when dropping oversized records", () => {
4300+
const warnings: Array<ReadonlyArray<unknown>> = [];
4301+
const warned = Deferred.makeUnsafe<void>();
4302+
const logCapture = Logger.make<unknown, void>(({ logLevel, message }) => {
4303+
if (logLevel === "Warn" && Array.isArray(message)) {
4304+
warnings.push(message);
4305+
Deferred.doneUnsafe(warned, Effect.void);
4306+
}
4307+
});
4308+
return Effect.gen(function* () {
43004309
const stdout = yield* Queue.unbounded<Uint8Array>();
43014310
const spawner = ChildProcessSpawner.make(() =>
43024311
Effect.succeed(
@@ -4327,9 +4336,11 @@ describe("PiRpc framing", () => {
43274336
yield* push('{"type":"agent_');
43284337
yield* push('start"}\r\n{"type":"agent_settled"}\nnot json\n{"type":"queue_update"}\n');
43294338

4330-
yield* push('{"type":"must_not_emit","text":"');
4339+
const head = '{"type":"must_not_emit","text":"';
4340+
yield* push(head);
43314341
const chunk = new TextEncoder().encode("x".repeat(8 * 1024 * 1024));
43324342
for (let index = 0; index < 32; index++) yield* Queue.offer(stdout, chunk);
4343+
yield* Deferred.await(warned);
43334344
yield* push('"}\n{"type":"after_oversized"}\n');
43344345

43354346
const first = yield* Queue.take(connection.events);
@@ -4339,8 +4350,20 @@ describe("PiRpc framing", () => {
43394350
const third = yield* Queue.take(connection.events);
43404351
assert.equal(third["type"], "queue_update");
43414352
assert.equal((yield* Queue.take(connection.events))["type"], "after_oversized");
4342-
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
4343-
);
4353+
assert.lengthOf(warnings, 1);
4354+
const [text, annotations] = warnings[0] ?? [];
4355+
assert.equal(text, "Dropping oversized pi stdout record.");
4356+
assert.deepEqual(annotations, {
4357+
recordLength: head.length + 28 * chunk.length,
4358+
maxRecordLength: 232084864,
4359+
});
4360+
}).pipe(
4361+
Effect.scoped,
4362+
Effect.provide(
4363+
Layer.merge(NodeServices.layer, Logger.layer([logCapture], { mergeWithExisting: false })),
4364+
),
4365+
);
4366+
});
43444367
});
43454368

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

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,16 @@ const MAX_PI_RECORD_CHARS =
9696
function makeJsonlFramer() {
9797
let buffer = "";
9898
let dropping = false;
99-
return (chunk: string): ReadonlyArray<string> => {
99+
return (chunk: string) => {
100100
const lines: string[] = [];
101+
const droppedLengths: number[] = [];
101102
let start = 0;
102103
while (start < chunk.length) {
103104
const newline = chunk.indexOf("\n", start);
104105
const end = newline < 0 ? chunk.length : newline;
105106
if (!dropping) {
106107
if (buffer.length + end - start > MAX_PI_RECORD_CHARS) {
108+
droppedLengths.push(buffer.length + end - start);
107109
buffer = "";
108110
dropping = true;
109111
} else {
@@ -118,10 +120,22 @@ function makeJsonlFramer() {
118120
dropping = false;
119121
start = newline + 1;
120122
}
121-
return lines;
123+
return { lines, droppedLengths };
122124
};
123125
}
124126

127+
// Report the observed length before LF arrives; an unterminated response can time out first.
128+
const logDroppedPiRecords = (droppedLengths: ReadonlyArray<number>) =>
129+
Effect.forEach(
130+
droppedLengths,
131+
(recordLength) =>
132+
Effect.logWarning("Dropping oversized pi stdout record.", {
133+
recordLength,
134+
maxRecordLength: MAX_PI_RECORD_CHARS,
135+
}),
136+
{ discard: true },
137+
);
138+
125139
const UnknownFromJsonString = Schema.fromJsonString(Schema.Unknown);
126140
const decodeJsonLine = Schema.decodeSync(UnknownFromJsonString);
127141
const encodeJsonLine = Schema.encodeSync(UnknownFromJsonString);
@@ -294,7 +308,8 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
294308
Stream.decodeText(),
295309
Stream.runForEach((chunk) =>
296310
Effect.gen(function* () {
297-
const lines = frame(chunk);
311+
const { lines, droppedLengths } = frame(chunk);
312+
yield* logDroppedPiRecords(droppedLengths);
298313
for (const line of lines) {
299314
const record = parsePiRecord(line);
300315
if (record === undefined) {
@@ -308,7 +323,9 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
308323
}),
309324
),
310325
);
311-
for (const line of frame("\n")) {
326+
const flushed = frame("\n");
327+
yield* logDroppedPiRecords(flushed.droppedLengths);
328+
for (const line of flushed.lines) {
312329
const trailing = parsePiRecord(line);
313330
if (trailing !== undefined) yield* routeRecord(trailing);
314331
}

0 commit comments

Comments
 (0)