Skip to content

Commit d0c7147

Browse files
fix(pi): rewinding works after the Pi session was released (pingdotgg#26322)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 84478e0 commit d0c7147

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,6 +3666,77 @@ describe("PiAdapterV2", () => {
36663666
}).pipe(Effect.scoped, Effect.provide(layerTest)),
36673667
);
36683668

3669+
it.effect("rolls back a persisted thread on a fresh runtime after its session was released", () =>
3670+
Effect.gen(function* () {
3671+
const previousFake = yield* makeFakePi;
3672+
const { runtime: previousRuntime } = yield* openRuntime(previousFake);
3673+
const providerThread = yield* previousRuntime.ensureThread({
3674+
threadId: THREAD_ID,
3675+
modelSelection: modelSelection("default"),
3676+
runtimePolicy,
3677+
});
3678+
const sessionFile = providerThread.nativeThreadRef?.nativeId;
3679+
assert.isDefined(sessionFile);
3680+
const turn = (ordinal: number, entryId: string): OrchestrationV2ProviderTurn => ({
3681+
id: ProviderTurnId.make(`turn-${ordinal}`),
3682+
providerThreadId: providerThread.id,
3683+
nodeId: NodeId.make(`node-${ordinal}`),
3684+
runAttemptId: null,
3685+
nativeTurnRef: { driver: PI_PROVIDER, nativeId: entryId, strength: "strong" },
3686+
ordinal,
3687+
status: "completed",
3688+
startedAt: null,
3689+
completedAt: null,
3690+
});
3691+
const turns = [turn(1, "u1"), turn(2, "u2")];
3692+
3693+
// Rollback opens the session the way CheckpointRollbackService does, without resumeThread.
3694+
const fake = yield* makeFakePi;
3695+
const adapter = yield* makeAdapter(fake);
3696+
const runtime = yield* adapter.openSession({
3697+
threadId: THREAD_ID,
3698+
providerSessionId: SESSION_ID,
3699+
modelSelection: modelSelection("default"),
3700+
runtimePolicy,
3701+
initialNativeThreadId: sessionFile!,
3702+
});
3703+
yield* runtime.events.pipe(Stream.runDrain, Effect.forkScoped);
3704+
fake.queueState({ sessionFile: sessionFile! });
3705+
fake.queueState({ sessionFile: "/fake/rolled-back-session.jsonl" });
3706+
const result = yield* runtime
3707+
.rollbackThread({
3708+
providerThread,
3709+
target: {
3710+
type: "provider_turn",
3711+
checkpointId: CheckpointId.make("checkpoint-1"),
3712+
appRunOrdinal: 1,
3713+
providerTurn: turns[0]!,
3714+
},
3715+
providerThreadTurns: turns,
3716+
})
3717+
.pipe(Effect.result);
3718+
3719+
assert.equal(
3720+
result._tag,
3721+
"Success",
3722+
`rollback on a fresh Pi runtime failed: ${result._tag === "Failure" ? String(result.failure.cause) : ""}`,
3723+
);
3724+
const requests = fake.allRequests();
3725+
const switchIndex = requests.findIndex(
3726+
(request) => request.type === "switch_session" && request.sessionPath === sessionFile,
3727+
);
3728+
const forkIndex = requests.findIndex((request) => request.type === "fork");
3729+
assert.isAtLeast(switchIndex, 0, "fresh runtime never switched to the persisted session");
3730+
assert.isAbove(forkIndex, switchIndex, "fork ran before switching to the persisted session");
3731+
assert.equal(requests[forkIndex]?.entryId, "u2");
3732+
if (result._tag !== "Success") return;
3733+
assert.equal(
3734+
result.success.providerThread.nativeThreadRef?.nativeId,
3735+
"/fake/rolled-back-session.jsonl",
3736+
);
3737+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
3738+
);
3739+
36693740
it.effect("settles a command-only prompt from its deferred ack and idle probe", () =>
36703741
Effect.gen(function* () {
36713742
const fake = yield* makeFakePi;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,6 +3182,22 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
31823182
),
31833183
rollbackThread: Effect.fn("PiAdapter.rollbackThread")(
31843184
function* (rollbackInput) {
3185+
// A fresh runtime (restart or idle release) must load the persisted session before forking.
3186+
// Unpublished: the rollback service persists the returned snapshot.
3187+
if (
3188+
threadState === null &&
3189+
rollbackInput.providerThread.nativeThreadRef?.nativeId != null
3190+
) {
3191+
yield* registerThread(
3192+
{
3193+
threadId: rollbackInput.providerThread.appThreadId ?? input.threadId,
3194+
modelSelection: input.modelSelection,
3195+
runtimePolicy: input.runtimePolicy,
3196+
existingProviderThread: rollbackInput.providerThread,
3197+
},
3198+
false,
3199+
);
3200+
}
31853201
const state = threadState;
31863202
if (state === null) {
31873203
return yield* protocolError("Pi session has no registered thread");

0 commit comments

Comments
 (0)