The best way to control Claude Code, Codex and more from any computer or phone
fix(pi): Reasoning that Pi starts again no longer repeats the abandoned attempt (#38334)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Mike Olson committed
Nov 17, 2026 at 16:48 UTC
4ecbe55087ade20daea476d67b479aab4ce5039a
2 files changed
+71
-2
packages/provider-pi/src/server/adapter.test.ts
+54
@@ -6117,6 +6117,60 @@ describe("PiAdapterV2", () => {
6117
}).pipe(Effect.scoped, Effect.provide(layerTest)),
6118
);
6119
6120
+ // Streamed text is published on a real timer, so this runs on the live clock.
6121
+ it.live("shows only the new reasoning when Pi starts a streaming block again", () =>
6122
+ Effect.gen(function* () {
6123
+ const fake = yield* makeFakePi;
6124
+ const { runtime, takeEvent } = yield* openRuntime(fake);
6125
+ const providerThread = yield* runtime.ensureThread({
6126
+ threadId: THREAD_ID,
6127
+ modelSelection: modelSelection("default"),
6128
+ runtimePolicy,
6129
+ });
6130
+ yield* startTurn(runtime, providerThread);
6131
+ yield* fake.takeRequest("prompt");
6132
+ yield* fake.emit({ type: "agent_start" });
6133
+ yield* fake.emit({ type: "message_start", message: { role: "assistant" } });
6134
+ // A provider that reconnects mid-reasoning streams the same block from its start.
6135
+ const thinking = (event: Record<string, unknown>) =>
6136
+ fake.emit({
6137
+ type: "message_update",
6138
+ assistantMessageEvent: { contentIndex: 0, ...event },
6139
+ });
6140
+ const reasoningUpdate = Effect.fnUntraced(function* (
6141
+ wanted: (item: { readonly status: string; readonly text: string }) => boolean,
6142
+ ) {
6143
+ const update = yield* takeEvent(
6144
+ (event) =>
6145
+ event.type === "turn_item.updated" &&
6146
+ event.turnItem.type === "reasoning" &&
6147
+ wanted(event.turnItem),
6148
+ );
6149
+ return update.type === "turn_item.updated" && update.turnItem.type === "reasoning"
6150
+ ? { status: update.turnItem.status, text: update.turnItem.text }
6151
+ : null;
6152
+ });
6153
+ yield* thinking({ type: "thinking_start" });
6154
+ yield* thinking({ type: "thinking_delta", delta: "Abandoned attempt" });
6155
+ yield* thinking({ type: "thinking_start" });
6156
+ yield* thinking({ type: "thinking_delta", delta: "Replacement attempt" });
6157
+ assert.deepEqual(
6158
+ yield* reasoningUpdate((item) => item.text.endsWith("Replacement attempt")),
6159
+ {
6160
+ status: "running",
6161
+ text: "Replacement attempt",
6162
+ },
6163
+ );
6164
+ // A third attempt that ends with no reasoning leaves nothing of the second on screen.
6165
+ yield* thinking({ type: "thinking_start" });
6166
+ yield* thinking({ type: "thinking_end" });
6167
+ assert.deepEqual(yield* reasoningUpdate((item) => item.status === "completed"), {
6168
+ status: "completed",
6169
+ text: "",
6170
+ });
6171
+ }).pipe(Effect.scoped, Effect.provide(layerTest)),
6172
+ );
6173
+
6174
it.effect.each([
6175
{
6176
pi: "clears its native queue",
packages/provider-pi/src/server/adapter.ts
+17
-2
@@ -327,6 +327,7 @@ interface PiStreamItemState {
327
text: string;
328
completed: boolean;
329
flushScheduled: boolean;
330
+ published: boolean;
331
readonly startedAt: DateTime.Utc;
332
}
333
@@ -1196,6 +1197,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
1197
item: PiStreamItemState,
1198
streaming: boolean,
1199
) {
1200
+ item.published = true;
1201
const emittedAt = yield* DateTime.now;
1202
const base = baseItemFields(turn, item.nativeItemId, item.startedAt, emittedAt);
1203
yield* emitItemNode(
@@ -1279,12 +1281,15 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
1281
);
1282
});
1283
1284
+ const streamItemId = (turn: ActivePiTurn, contentIndex: number) =>
1285
+ `${turn.providerTurn.id}:m${turn.messageOrdinal}:c${contentIndex}`;
1286
+
1287
const streamItemFor = Effect.fnUntraced(function* (
1288
turn: ActivePiTurn,
1289
kind: PiStreamItemState["kind"],
1290
contentIndex: number,
1291
) {
1287
- const nativeItemId = `${turn.providerTurn.id}:m${turn.messageOrdinal}:c${contentIndex}`;
1292
+ const nativeItemId = streamItemId(turn, contentIndex);
1293
const existing = turn.streamItems.get(nativeItemId);
1294
if (existing !== undefined) return existing;
1295
const startedAt = yield* DateTime.now;
@@ -1294,6 +1299,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
1299
text: "",
1300
completed: false,
1301
flushScheduled: false,
1302
+ published: false,
1303
startedAt,
1304
};
1305
turn.streamItems.set(nativeItemId, item);
@@ -1306,7 +1312,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
1312
if (item.completed) return Effect.void;
1313
item.completed = true;
1314
if (text !== undefined && text.length > 0) item.text = text;
1309
- return item.text.length === 0 ? Effect.void : emitStreamItem(turn, item, false);
1315
+ // A block that was shown and then started again empty still replaces what it showed.
1316
+ return item.text.length === 0 && !item.published
1317
+ ? Effect.void
1318
+ : emitStreamItem(turn, item, false);
1319
});
1320
1321
const completeOpenStreamItems = (turn: ActivePiTurn) =>
@@ -2309,6 +2318,12 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
2318
const delta = event["assistantMessageEvent"];
2319
const deltaType = recordString(delta, "type");
2320
const contentIndex = recordNumber(delta, "contentIndex") ?? 0;
2321
+ if (deltaType === "text_start" || deltaType === "thinking_start") {
2322
+ // A provider that reconnects mid-block starts it again; its earlier text is abandoned.
2323
+ const restarted = turn.streamItems.get(streamItemId(turn, contentIndex));
2324
+ if (restarted !== undefined && !restarted.completed) restarted.text = "";
2325
+ return;
2326
+ }
2327
if (deltaType === "text_delta" || deltaType === "thinking_delta") {
2328
const item = yield* streamItemFor(
2329
turn,