Skip to content

Commit e7741ec

Browse files
fix(server): a kept Pi turn always has the response that settles it
The prompt or compact send and its pending-response entry are now recorded in one uninterruptible step, so a turn kept after an interrupted start can still be settled or failed by Pi's answer. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent be865b2 commit e7741ec

2 files changed

Lines changed: 90 additions & 11 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/PiAdapterV2.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,75 @@ describe("PiAdapterV2", () => {
12981298
}).pipe(Effect.scoped, Effect.provide(testLayer)),
12991299
);
13001300

1301+
it.effect("fails a kept turn when Pi rejects the prompt it was interrupted after sending", () =>
1302+
Effect.gen(function* () {
1303+
const fake = yield* makeFakePi;
1304+
const { runtime, takeEvent } = yield* openRuntime(fake);
1305+
const providerThread = yield* runtime.ensureThread({
1306+
threadId: THREAD_ID,
1307+
modelSelection: modelSelection("default"),
1308+
runtimePolicy,
1309+
});
1310+
const appThread = yield* makeAppThread("default");
1311+
const runId = RunId.make(`run:${THREAD_ID}:1`);
1312+
// The install block first reads `runOrdinal` after it sent the prompt.
1313+
// Interrupting the starting fiber there is a Stop that lands once Pi is
1314+
// already running the prompt, with no wait to race. Later reads (the
1315+
// event pump settling the turn) are left alone.
1316+
let interruptOnRead = true;
1317+
const turnInput = {
1318+
appThread,
1319+
threadId: THREAD_ID,
1320+
runId,
1321+
get runOrdinal() {
1322+
if (interruptOnRead) {
1323+
interruptOnRead = false;
1324+
Fiber.getCurrent()?.interruptUnsafe();
1325+
}
1326+
return 1;
1327+
},
1328+
providerTurnOrdinal: 1,
1329+
attemptId: RunAttemptId.make(`run-attempt:${runId}:1`),
1330+
rootNodeId: NodeId.make(`node:${runId}:root`),
1331+
providerThread,
1332+
message: {
1333+
messageId: `message:${THREAD_ID}:1` as never,
1334+
text: "Hello pi",
1335+
attachments: [],
1336+
createdBy: "user" as const,
1337+
creationSource: "web" as const,
1338+
},
1339+
modelSelection: modelSelection("default"),
1340+
runtimePolicy,
1341+
};
1342+
const starting = yield* runtime.startTurn(turnInput).pipe(Effect.forkChild);
1343+
const interrupted = yield* Fiber.await(starting);
1344+
assert.isTrue(Exit.isFailure(interrupted) && Cause.hasInterruptsOnly(interrupted.cause));
1345+
yield* fake.takeRequest("prompt");
1346+
const running = yield* takeEvent(
1347+
(event) =>
1348+
event.type === "provider_turn.updated" && event.providerTurn.status === "running",
1349+
);
1350+
const providerTurnId =
1351+
running.type === "provider_turn.updated" ? running.providerTurn.id : undefined;
1352+
assert.isDefined(providerTurnId);
1353+
1354+
// The kept turn still has the pending response that settles or fails it.
1355+
yield* fake.emit({
1356+
type: "response",
1357+
command: "prompt",
1358+
success: false,
1359+
error: "Prompt rejected",
1360+
});
1361+
const terminal = yield* takeEvent((event) => event.type === "turn.terminal");
1362+
assert.isTrue(
1363+
terminal.type === "turn.terminal" &&
1364+
terminal.status === "failed" &&
1365+
terminal.providerTurnId === providerTurnId,
1366+
);
1367+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1368+
);
1369+
13011370
it.effect("leaves /compacted as an ordinary prompt", () =>
13021371
Effect.gen(function* () {
13031372
const fake = yield* makeFakePi;

‎apps/server/src/orchestration-v2/Adapters/PiAdapterV2.ts‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,7 +2342,9 @@ export function makePiAdapterV2(
23422342
activeProviderRetry: null,
23432343
failure: null,
23442344
};
2345-
// Set together with the send, with no gap for an interrupt to land in.
2345+
// Set together with the send and its pending-response entry, with
2346+
// no gap for an interrupt to land in: a sent turn always has the
2347+
// entry that settles or fails it.
23462348
let sent = false;
23472349
// Only the install/send/start-event boundary excludes the event
23482350
// pump. Earlier correlated requests must leave the pump free so
@@ -2352,13 +2354,17 @@ export function makePiAdapterV2(
23522354
state.activeTurn = activeTurn;
23532355
if (compactCommand !== null) {
23542356
yield* connection.send(compactRpcRecord(compactCommand)).pipe(
2355-
Effect.tap(() => Effect.sync(() => (sent = true))),
2357+
Effect.tap(() =>
2358+
Effect.sync(() => {
2359+
pendingCompactResponses.push({
2360+
providerTurnId: providerTurn.id,
2361+
kind: "turn_start",
2362+
});
2363+
sent = true;
2364+
}),
2365+
),
23562366
Effect.uninterruptible,
23572367
);
2358-
pendingCompactResponses.push({
2359-
providerTurnId: providerTurn.id,
2360-
kind: "turn_start",
2361-
});
23622368
} else if (payload !== null) {
23632369
yield* connection
23642370
.send({
@@ -2367,13 +2373,17 @@ export function makePiAdapterV2(
23672373
...(payload.images.length === 0 ? {} : { images: payload.images }),
23682374
})
23692375
.pipe(
2370-
Effect.tap(() => Effect.sync(() => (sent = true))),
2376+
Effect.tap(() =>
2377+
Effect.sync(() => {
2378+
pendingPromptResponses.push({
2379+
providerTurnId: providerTurn.id,
2380+
kind: "turn_start",
2381+
});
2382+
sent = true;
2383+
}),
2384+
),
23712385
Effect.uninterruptible,
23722386
);
2373-
pendingPromptResponses.push({
2374-
providerTurnId: providerTurn.id,
2375-
kind: "turn_start",
2376-
});
23772387
}
23782388
yield* emit({
23792389
type: "provider_turn.updated",

0 commit comments

Comments
 (0)