@devries-cameron20 / t3code Mirror · read-only / commits / d0c7147a8e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 23:30 UTC

fix(pi): rewinding works after the Pi session was released (#26322)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 11, 2026 at 11:39 UTC d0c7147a8e1a382fc3bdf7cab291614623b31edb
2 files changed +87
packages/provider-pi/src/server/adapter.test.ts
+71
@@ -3666,6 +3666,77 @@ describe("PiAdapterV2", () => {
3666 }).pipe(Effect.scoped, Effect.provide(layerTest)),
3667 );
3668
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 +
3740 it.effect("settles a command-only prompt from its deferred ack and idle probe", () =>
3741 Effect.gen(function* () {
3742 const fake = yield* makeFakePi;
packages/provider-pi/src/server/adapter.ts
+16
@@ -3182,6 +3182,22 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
3182 ),
3183 rollbackThread: Effect.fn("PiAdapter.rollbackThread")(
3184 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 + }
3201 const state = threadState;
3202 if (state === null) {
3203 return yield* protocolError("Pi session has no registered thread");