Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions packages/provider-pi/src/server/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import * as Queue from "effect/Queue";
import * as Schema from "effect/Schema";
import * as Sink from "effect/Sink";
Expand Down Expand Up @@ -4263,8 +4264,16 @@ describe("PiAdapterV2", () => {
});

describe("PiRpc framing", () => {
it.effect("reassembles records across chunk boundaries and strips CR", () =>
Effect.gen(function* () {
it.effect("reassembles records and warns when dropping oversized records", () => {
const warnings: Array<ReadonlyArray<unknown>> = [];
const warned = Deferred.makeUnsafe<void>();
const logCapture = Logger.make<unknown, void>(({ logLevel, message }) => {
if (logLevel === "Warn" && Array.isArray(message)) {
warnings.push(message);
Deferred.doneUnsafe(warned, Effect.void);
}
});
return Effect.gen(function* () {
const stdout = yield* Queue.unbounded<Uint8Array>();
const spawner = ChildProcessSpawner.make(() =>
Effect.succeed(
Expand Down Expand Up @@ -4295,9 +4304,11 @@ describe("PiRpc framing", () => {
yield* push('{"type":"agent_');
yield* push('start"}\r\n{"type":"agent_settled"}\nnot json\n{"type":"queue_update"}\n');

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

const first = yield* Queue.take(connection.events);
Expand All @@ -4307,8 +4318,20 @@ describe("PiRpc framing", () => {
const third = yield* Queue.take(connection.events);
assert.equal(third["type"], "queue_update");
assert.equal((yield* Queue.take(connection.events))["type"], "after_oversized");
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
assert.lengthOf(warnings, 1);
const [text, annotations] = warnings[0] ?? [];
assert.equal(text, "Dropping oversized pi stdout record.");
assert.deepEqual(annotations, {
recordLength: head.length + 28 * chunk.length,
maxRecordLength: 232084864,
});
}).pipe(
Effect.scoped,
Effect.provide(
Layer.merge(NodeServices.layer, Logger.layer([logCapture], { mergeWithExisting: false })),
),
);
});
});

describe("PiRpc early process exit", () => {
Expand Down
25 changes: 21 additions & 4 deletions packages/provider-pi/src/server/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ const MAX_PI_RECORD_CHARS =
function makeJsonlFramer() {
let buffer = "";
let dropping = false;
return (chunk: string): ReadonlyArray<string> => {
return (chunk: string) => {
const lines: string[] = [];
const droppedLengths: number[] = [];
let start = 0;
while (start < chunk.length) {
const newline = chunk.indexOf("\n", start);
const end = newline < 0 ? chunk.length : newline;
if (!dropping) {
if (buffer.length + end - start > MAX_PI_RECORD_CHARS) {
droppedLengths.push(buffer.length + end - start);
buffer = "";
dropping = true;
} else {
Expand All @@ -118,10 +120,22 @@ function makeJsonlFramer() {
dropping = false;
start = newline + 1;
}
return lines;
return { lines, droppedLengths };
};
}

// Report the observed length before LF arrives; an unterminated response can time out first.
const logDroppedPiRecords = (droppedLengths: ReadonlyArray<number>) =>
Effect.forEach(
droppedLengths,
(recordLength) =>
Effect.logWarning("Dropping oversized pi stdout record.", {
recordLength,
maxRecordLength: MAX_PI_RECORD_CHARS,
}),
{ discard: true },
);

const UnknownFromJsonString = Schema.fromJsonString(Schema.Unknown);
const decodeJsonLine = Schema.decodeSync(UnknownFromJsonString);
const encodeJsonLine = Schema.encodeSync(UnknownFromJsonString);
Expand Down Expand Up @@ -294,7 +308,8 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
Stream.decodeText(),
Stream.runForEach((chunk) =>
Effect.gen(function* () {
const lines = frame(chunk);
const { lines, droppedLengths } = frame(chunk);
yield* logDroppedPiRecords(droppedLengths);
for (const line of lines) {
const record = parsePiRecord(line);
if (record === undefined) {
Expand All @@ -308,7 +323,9 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
}),
),
);
for (const line of frame("\n")) {
const flushed = frame("\n");
yield* logDroppedPiRecords(flushed.droppedLengths);
for (const line of flushed.lines) {
const trailing = parsePiRecord(line);
if (trailing !== undefined) yield* routeRecord(trailing);
}
Expand Down
Loading