Skip to content

Commit b55a050

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 c6f918d commit b55a050

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ interface FakePi {
102102
readonly vetoNextNewSession: () => void;
103103
/** Make the next `fork` ack report an extension veto. */
104104
readonly vetoNextFork: () => void;
105+
/** Make the next `fork` fail with `success: false`, leaving Pi on its session. */
106+
readonly rejectNextFork: () => void;
105107
/** Every request received by the fake process. */
106108
readonly allRequests: () => ReadonlyArray<PiRpcRecord>;
107109
/** Data returned by the next `get_session_stats` acks, consumed in order. */
@@ -157,6 +159,7 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
157159
let vetoSwitch = false;
158160
let vetoNewSession = false;
159161
let vetoFork = false;
162+
let rejectFork = false;
160163
let deferredLifecycle: string | undefined;
161164
let deferredLifecycleRequest: PiRpcRecord | undefined;
162165
let sessionFile = FAKE_SESSION_FILE;
@@ -208,6 +211,10 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
208211
case "get_commands":
209212
return { ...base, ...(commandsQueue.shift() ?? { data: { commands: [] } }) };
210213
case "fork": {
214+
if (rejectFork) {
215+
rejectFork = false;
216+
return { ...base, success: false, error: "fork refused" };
217+
}
211218
const cancelled = vetoFork;
212219
vetoFork = false;
213220
return { ...base, data: { text: "Hello pi", cancelled } };
@@ -321,6 +328,9 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
321328
vetoNextNewSession: () => {
322329
vetoNewSession = true;
323330
},
331+
rejectNextFork: () => {
332+
rejectFork = true;
333+
},
324334
vetoNextFork: () => {
325335
vetoFork = true;
326336
},
@@ -910,6 +920,61 @@ describe("PiAdapterV2", () => {
910920
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerTest, ContributionStatusStore.layer))),
911921
);
912922

923+
it.effect("puts back the session's statuses when Pi refuses a rollback fork", () =>
924+
Effect.gen(function* () {
925+
const fake = yield* makeFakePi;
926+
const store = yield* ContributionStatusStore.ContributionStatusStore;
927+
const statuses = yield* store.subscribe;
928+
const { runtime } = yield* openRuntime(fake);
929+
const providerThread = yield* runtime.ensureThread({
930+
threadId: THREAD_ID,
931+
modelSelection: modelSelection("default"),
932+
runtimePolicy,
933+
});
934+
yield* emitStatus(fake, "mode", "build");
935+
yield* emitStatus(fake, "plan", "on");
936+
yield* waitForStatuses(statuses, (current) => current[THREAD_ID]?.length === 2);
937+
938+
fake.rejectNextFork();
939+
fake.deferNextLifecycle("fork");
940+
const turn: OrchestrationV2ProviderTurn = {
941+
id: ProviderTurnId.make("turn-1"),
942+
providerThreadId: providerThread.id,
943+
nodeId: NodeId.make("node-1"),
944+
runAttemptId: null,
945+
nativeTurnRef: { driver: PI_PROVIDER, nativeId: "u1", strength: "strong" },
946+
ordinal: 1,
947+
status: "completed",
948+
startedAt: null,
949+
completedAt: null,
950+
};
951+
const rollback = yield* runtime
952+
.rollbackThread({
953+
providerThread,
954+
providerThreadTurns: [turn],
955+
target: {
956+
type: "thread_start",
957+
checkpointId: CheckpointId.make("checkpoint-pi-rollback"),
958+
appRunOrdinal: 0,
959+
},
960+
})
961+
.pipe(Effect.flip, Effect.forkChild);
962+
yield* fake.takeRequest("fork");
963+
// The session Pi stays on keeps writing while its hook decides.
964+
yield* emitStatus(fake, "plan", "off");
965+
yield* fake.resolveDeferredLifecycle;
966+
const error = yield* Fiber.join(rollback);
967+
assert.strictEqual(error._tag, "ProviderAdapterRollbackThreadError");
968+
yield* emitStatus(fake, "zz", "receipt");
969+
970+
// "zz" is the last status event, so the first snapshot showing it is final.
971+
const settled = yield* waitForStatuses(statuses, (current) =>
972+
(current[THREAD_ID] ?? []).includes("zz=receipt"),
973+
);
974+
assert.deepStrictEqual(settled, { [THREAD_ID]: ["mode=build", "plan=off", "zz=receipt"] });
975+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerTest, ContributionStatusStore.layer))),
976+
);
977+
913978
it.effect("restores only the statuses the store held when an extension cancels a rollback", () =>
914979
Effect.gen(function* () {
915980
const fake = yield* makeFakePi;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2788,7 +2788,17 @@ export function makePiAdapterV2(
27882788
Effect.onExit((exit) =>
27892789
Queue.offer(connection.events, {
27902790
type: "t3.status_settle",
2791-
restore: Exit.isSuccess(exit) && recordField(exit.value, "cancelled") === true,
2791+
// Pi stays on its session when an extension cancels the fork or
2792+
// Pi refuses it; a timeout, interruption or dead transport says nothing.
2793+
restore: Exit.isSuccess(exit)
2794+
? recordField(exit.value, "cancelled") === true
2795+
: !Cause.hasInterrupts(exit.cause) &&
2796+
Option.exists(
2797+
Cause.findErrorOption(exit.cause),
2798+
(error) =>
2799+
error._tag === "PiRpcError" &&
2800+
(error.operation === "fork" || error.operation === "request"),
2801+
),
27922802
}),
27932803
),
27942804
);

0 commit comments

Comments
 (0)