Skip to content

Commit 890b0c5

Browse files
fix(pi): keep statuses when Pi refuses a rollback fork
A rollback holds the session's statuses until Pi answers the fork, and put them back only when an extension cancelled it. When Pi refused the fork outright it also stayed on its session, but the held statuses were dropped. They are now restored for a refusal too; a timeout, interruption or dead transport still drops them. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 3d2de4d commit 890b0c5

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ interface FakePi {
108108
readonly vetoNextNewSession: () => void;
109109
/** Make the next `fork` ack report an extension veto. */
110110
readonly vetoNextFork: () => void;
111+
/** Make the next `fork` fail with `success: false`, leaving Pi on its session. */
112+
readonly rejectNextFork: () => void;
111113
/** Every request received by the fake process. */
112114
readonly allRequests: () => ReadonlyArray<PiRpcRecord>;
113115
/** Data returned by the next `get_session_stats` acks, consumed in order. */
@@ -158,6 +160,7 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
158160
let vetoSwitch = false;
159161
let vetoNewSession = false;
160162
let vetoFork = false;
163+
let rejectFork = false;
161164
let deferredLifecycle: string | undefined;
162165
let deferredLifecycleRequest: PiRpcRecord | undefined;
163166
let sessionFile = FAKE_SESSION_FILE;
@@ -207,6 +210,10 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
207210
case "get_session_stats":
208211
return { ...base, data: statsQueue.shift() ?? {} };
209212
case "fork": {
213+
if (rejectFork) {
214+
rejectFork = false;
215+
return { ...base, success: false, error: "fork refused" };
216+
}
210217
const cancelled = vetoFork;
211218
vetoFork = false;
212219
return { ...base, data: { text: "Hello pi", cancelled } };
@@ -323,6 +330,9 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
323330
vetoNextNewSession: () => {
324331
vetoNewSession = true;
325332
},
333+
rejectNextFork: () => {
334+
rejectFork = true;
335+
},
326336
vetoNextFork: () => {
327337
vetoFork = true;
328338
},
@@ -1704,6 +1714,61 @@ describe("PiAdapterV2", () => {
17041714
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerTest, ContributionStatusStore.layer))),
17051715
);
17061716

1717+
it.effect("puts back the session's statuses when Pi refuses a rollback fork", () =>
1718+
Effect.gen(function* () {
1719+
const fake = yield* makeFakePi;
1720+
const store = yield* ContributionStatusStore.ContributionStatusStore;
1721+
const statuses = yield* store.subscribe;
1722+
const { runtime } = yield* openRuntime(fake);
1723+
const providerThread = yield* runtime.ensureThread({
1724+
threadId: THREAD_ID,
1725+
modelSelection: modelSelection("default"),
1726+
runtimePolicy,
1727+
});
1728+
yield* emitStatus(fake, "mode", "build");
1729+
yield* emitStatus(fake, "plan", "on");
1730+
yield* waitForStatuses(statuses, (current) => current[THREAD_ID]?.length === 2);
1731+
1732+
fake.rejectNextFork();
1733+
fake.deferNextLifecycle("fork");
1734+
const turn: OrchestrationV2ProviderTurn = {
1735+
id: ProviderTurnId.make("turn-1"),
1736+
providerThreadId: providerThread.id,
1737+
nodeId: NodeId.make("node-1"),
1738+
runAttemptId: null,
1739+
nativeTurnRef: { driver: PI_PROVIDER, nativeId: "u1", strength: "strong" },
1740+
ordinal: 1,
1741+
status: "completed",
1742+
startedAt: null,
1743+
completedAt: null,
1744+
};
1745+
const rollback = yield* runtime
1746+
.rollbackThread({
1747+
providerThread,
1748+
providerThreadTurns: [turn],
1749+
target: {
1750+
type: "thread_start",
1751+
checkpointId: CheckpointId.make("checkpoint-pi-rollback"),
1752+
appRunOrdinal: 0,
1753+
},
1754+
})
1755+
.pipe(Effect.flip, Effect.forkChild);
1756+
yield* fake.takeRequest("fork");
1757+
// The session Pi stays on keeps writing while its hook decides.
1758+
yield* emitStatus(fake, "plan", "off");
1759+
yield* fake.resolveDeferredLifecycle;
1760+
const error = yield* Fiber.join(rollback);
1761+
assert.strictEqual(error._tag, "ProviderAdapterRollbackThreadError");
1762+
yield* emitStatus(fake, "zz", "receipt");
1763+
1764+
// "zz" is the last status event, so the first snapshot showing it is final.
1765+
const settled = yield* waitForStatuses(statuses, (current) =>
1766+
(current[THREAD_ID] ?? []).includes("zz=receipt"),
1767+
);
1768+
assert.deepStrictEqual(settled, { [THREAD_ID]: ["mode=build", "plan=off", "zz=receipt"] });
1769+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerTest, ContributionStatusStore.layer))),
1770+
);
1771+
17071772
it.effect("restores only the statuses the store held when an extension cancels a rollback", () =>
17081773
Effect.gen(function* () {
17091774
const fake = yield* makeFakePi;

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,8 +3022,17 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
30223022
Effect.onExit((exit) =>
30233023
Queue.offer(connection.events, {
30243024
type: "t3.status_settle",
3025-
restore:
3026-
Exit.isSuccess(exit) && recordField(exit.value, "cancelled") === true,
3025+
// Pi stays on its session when an extension cancels the fork or
3026+
// Pi refuses it; a timeout, interruption or dead transport says nothing.
3027+
restore: Exit.isSuccess(exit)
3028+
? recordField(exit.value, "cancelled") === true
3029+
: !Cause.hasInterrupts(exit.cause) &&
3030+
Option.exists(
3031+
Cause.findErrorOption(exit.cause),
3032+
(error) =>
3033+
error._tag === "PiRpcError" &&
3034+
(error.operation === "fork" || error.operation === "request"),
3035+
),
30273036
}),
30283037
),
30293038
);

0 commit comments

Comments
 (0)