Skip to content

Commit f5ae808

Browse files
fix(provider-pi): running shell output no longer stores a snapshot row per update (#36429)
1 parent 4bab2f3 commit f5ae808

2 files changed

Lines changed: 62 additions & 4 deletions

File tree

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4078,6 +4078,23 @@ describe("PiAdapterV2", () => {
40784078
running.turnItem.status === "running" &&
40794079
running.turnItem.input === "echo hello",
40804080
);
4081+
// Pi resends the whole output so far every 100 ms; each snapshot item is a stored row.
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) {
4088+
yield* fake.emit({
4089+
type: "tool_execution_update",
4090+
toolCallId: "shell-call",
4091+
toolName,
4092+
partialResult: {
4093+
content: [{ type: "text", text }],
4094+
...(totalBytes === undefined ? {} : { details: { truncation: { totalBytes } } }),
4095+
},
4096+
});
4097+
}
40814098
yield* fake.emit({
40824099
type: "tool_execution_end",
40834100
toolCallId: "shell-call",
@@ -4089,10 +4106,18 @@ describe("PiAdapterV2", () => {
40894106
...(legacy ? {} : { structuredContent: { exit_code: exitCode } }),
40904107
},
40914108
});
4092-
const completed = yield* takeEvent(
4093-
(event) =>
4094-
event.type === "turn_item.updated" && event.turnItem.type === "command_execution",
4095-
);
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!"]);
40964121
assert.isTrue(
40974122
completed.type === "turn_item.updated" &&
40984123
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)