Skip to content

Commit 83e045f

Browse files
fix(provider-pi): running shell output no longer stores a snapshot row per update
Pi resends the whole output tail of a running bash or powershell call about ten times a second, and the adapter turned each one into a turn_item.updated that was stored as its own event row. Forward only the newly added bytes as an unpersisted command_output.delta instead, as the Claude, Codex and Cursor adapters do; the completed item still carries the final output. Fixes #36425 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 04d6686 commit 83e045f

2 files changed

Lines changed: 55 additions & 6 deletions

File tree

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,12 +4079,20 @@ describe("PiAdapterV2", () => {
40794079
running.turnItem.input === "echo hello",
40804080
);
40814081
// Pi resends the whole output so far every 100 ms; each snapshot item is a stored row.
4082-
for (const text of ["hel", "hello"]) {
4082+
// Past its limit Pi sends only the tail, with the running total in details.truncation.
4083+
for (const [text, totalBytes] of [
4084+
["hel", undefined],
4085+
["hello", undefined],
4086+
["lo\n\u00e9!", 9],
4087+
] as const) {
40834088
yield* fake.emit({
40844089
type: "tool_execution_update",
40854090
toolCallId: "shell-call",
40864091
toolName,
4087-
partialResult: { content: [{ type: "text", text }] },
4092+
partialResult: {
4093+
content: [{ type: "text", text }],
4094+
...(totalBytes === undefined ? {} : { details: { truncation: { totalBytes } } }),
4095+
},
40884096
});
40894097
}
40904098
yield* fake.emit({
@@ -4098,10 +4106,18 @@ describe("PiAdapterV2", () => {
40984106
...(legacy ? {} : { structuredContent: { exit_code: exitCode } }),
40994107
},
41004108
});
4101-
const completed = yield* takeEvent(
4102-
(event) =>
4103-
event.type === "turn_item.updated" && event.turnItem.type === "command_execution",
4104-
);
4109+
const chunks: Array<string> = [];
4110+
let completed: ProviderAdapter.ProviderAdapterV2Event;
4111+
while (true) {
4112+
completed = yield* takeEvent(
4113+
(event) =>
4114+
event.type === "command_output.delta" ||
4115+
(event.type === "turn_item.updated" && event.turnItem.type === "command_execution"),
4116+
);
4117+
if (completed.type !== "command_output.delta") break;
4118+
chunks.push(completed.chunk);
4119+
}
4120+
assert.deepStrictEqual(chunks, ["hel", "lo", "\n\u00e9!"]);
41054121
assert.isTrue(
41064122
completed.type === "turn_item.updated" &&
41074123
completed.turnItem.type === "command_execution" &&

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ interface ActivePiTurn {
359359
readonly streamItems: Map<string, PiStreamItemState>;
360360
readonly toolArgs: Map<string, unknown>;
361361
readonly toolStartedAt: Map<string, DateTime.Utc>;
362+
readonly commandOutputBytes: Map<string, number>;
362363
interrupted: boolean;
363364
nativeAborted: boolean;
364365
sawAgentActivity: boolean;
@@ -1193,6 +1194,33 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
11931194
{ discard: true },
11941195
);
11951196

1197+
// Pi resends the whole output tail on every update; stored snapshots grow quadratically.
1198+
const emitCommandOutputDelta = (turn: ActivePiTurn, toolCallId: string, result: unknown) =>
1199+
Effect.suspend(() => {
1200+
const text = contentText(recordField(result, "content"));
1201+
const tail = Buffer.from(text, "utf8");
1202+
const totalBytes =
1203+
recordNumber(recordField(recordField(result, "details"), "truncation"), "totalBytes") ??
1204+
tail.length;
1205+
const previousBytes = turn.commandOutputBytes.get(toolCallId) ?? 0;
1206+
turn.commandOutputBytes.set(toolCallId, totalBytes);
1207+
if (totalBytes <= previousBytes) return Effect.void;
1208+
let start = Math.max(0, tail.length - (totalBytes - previousBytes));
1209+
while (start < tail.length && (tail[start]! & 0xc0) === 0x80) start++;
1210+
const chunk = tail.subarray(start).toString("utf8");
1211+
if (chunk.length === 0) return Effect.void;
1212+
return emit({
1213+
type: "command_output.delta",
1214+
driver: PI_PROVIDER,
1215+
threadId: turn.turnInput.threadId,
1216+
itemId: idAllocator.derive.turnItemFromProviderItem({
1217+
driver: PI_PROVIDER,
1218+
nativeItemId: toolCallId,
1219+
}),
1220+
chunk,
1221+
});
1222+
});
1223+
11961224
const emitToolItem = Effect.fnUntraced(function* (
11971225
turn: ActivePiTurn,
11981226
event: PiRpcRecord,
@@ -1203,6 +1231,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
12031231
// Pi tags every phase of a call made through ctx.executeTool, such as from a codemode script.
12041232
const parentToolCallId = recordString(event, "parentToolCallId");
12051233
if (toolCallId === undefined) return;
1234+
if (phase === "update" && (toolName === "bash" || toolName === "powershell")) {
1235+
yield* emitCommandOutputDelta(turn, toolCallId, event["partialResult"]);
1236+
return;
1237+
}
12061238
if (phase === "start") {
12071239
turn.toolArgs.set(toolCallId, event["args"]);
12081240
}
@@ -2906,6 +2938,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
29062938
streamItems: new Map(),
29072939
toolArgs: new Map(),
29082940
toolStartedAt: new Map(),
2941+
commandOutputBytes: new Map(),
29092942
interrupted: false,
29102943
nativeAborted: false,
29112944
sawAgentActivity: false,

0 commit comments

Comments
 (0)