From 44d8a2da9397148cbd96d35cd43eaaf30ce249ed Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:06:06 +0100 Subject: [PATCH 1/7] test(pi): reproduce rewind after returning to a session Co-authored-by: GPT 6.2 Sol --- .../provider-pi/src/server/rollback.test.ts | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/packages/provider-pi/src/server/rollback.test.ts b/packages/provider-pi/src/server/rollback.test.ts index a742af314d50..9262a2ddc2d6 100644 --- a/packages/provider-pi/src/server/rollback.test.ts +++ b/packages/provider-pi/src/server/rollback.test.ts @@ -72,9 +72,11 @@ it.effect.each([ "checkpoint inherited by a prior fork", "repeated rollback after a handoff", "handoff prompt left in the source", + "an extension returning to the original file through a clone", "switch timed out after attaching", ] as const)("restores exactly the retained Pi history for %s", (scenario) => Effect.gen(function* () { + const repro = scenario === "an extension returning to the original file through a clone"; const fs = yield* FileSystem.FileSystem; const dir = yield* fs.makeTempDirectoryScoped(); const oldFile = `${dir}/old.jsonl`; @@ -89,7 +91,7 @@ it.effect.each([ entry("a2", "steer", "assistant", 35), ]; const inherited = scenario === "checkpoint inherited by a prior fork"; - const handoffEntry = entry("u3", inherited ? "a2" : null, "user", 50); + const handoffEntry = entry("u3", inherited ? "a2" : null, "user", repro ? 55 : 50); const newEntries = scenario === "command-only handoff" ? [] @@ -97,11 +99,23 @@ it.effect.each([ ? [...oldEntries, handoffEntry] : [handoffEntry]; const repeated = scenario === "repeated rollback after a handoff"; - const oldSuffix = repeated || scenario === "handoff prompt left in the source"; + const oldSuffix = repro || repeated || scenario === "handoff prompt left in the source"; const sessions = new Map([ - [oldFile, oldSuffix ? [...oldEntries, entry("discarded", "a2", "user", 45)] : oldEntries], + [ + oldFile, + oldSuffix + ? [ + ...oldEntries, + entry("discarded", "a2", "user", repro ? 52 : 45), + ...(repro ? [entry("discarded-answer", "discarded", "assistant", 54)] : []), + ] + : oldEntries, + ], [currentFile, newEntries], ]); + // The extension moved into the unrelated currentFile, then cloned the original at + // its new head. Returning from that clone does not add any entry to the original. + if (repro) sessions.set(previousForkFile, sessions.get(oldFile)!); if (repeated) sessions.set(previousForkFile, [...oldEntries, entry("u3", "a2", "user", 50)]); for (const [file, entries] of sessions) { yield* fs.writeFileString( @@ -115,7 +129,7 @@ it.effect.each([ const output = yield* Queue.unbounded(); const switchApplied = yield* Deferred.make(); const sessionRequests: { readonly type: string; readonly file: string }[] = []; - let liveFile = currentFile; + let liveFile = repro ? oldFile : currentFile; let pending = ""; const encoder = new TextEncoder(); const decoder = new TextDecoder(); @@ -206,23 +220,31 @@ it.effect.each([ }); const events = yield* runtime.events.pipe(Stream.runDrain, Effect.result, Effect.forkScoped); const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy }); - const turns = (repeated ? [1, 2, 3, 4] : [1, 2, 3]).map( + const turns = (repro ? [1, 2, 3, 4, 5] : repeated ? [1, 2, 3, 4] : [1, 2, 3]).map( (ordinal): OrchestrationV2ProviderTurn => ({ id: ProviderTurnId.make(`turn-${ordinal}`), providerThreadId: providerThread.id, nodeId: NodeId.make(`node-${ordinal}`), runAttemptId: null, nativeTurnRef: - ordinal === 3 && scenario === "command-only handoff" + repro && ordinal === 5 ? null - : ordinal === 3 && scenario === "uncaptured handoff" - ? ref("uncaptured", "weak") - : ref(`u${ordinal}`), + : repro && (ordinal === 3 || ordinal === 4) + ? ref(`uncaptured-${ordinal}`, "weak") + : ordinal === 3 && scenario === "command-only handoff" + ? null + : ordinal === 3 && scenario === "uncaptured handoff" + ? ref("uncaptured", "weak") + : ref(`u${ordinal}`), ...(scenario === "legacy turns" ? {} : { nativeThreadRef: ref( - ordinal < 3 ? oldFile : ordinal === 3 && repeated ? previousForkFile : currentFile, + ordinal < 3 || (repro && ordinal === 5) + ? oldFile + : (repro && ordinal === 4) || (ordinal === 3 && repeated) + ? previousForkFile + : currentFile, ), }), ordinal, @@ -254,6 +276,14 @@ it.effect.each([ assert.deepEqual(sessionRequests, [{ type: "switch_session", file: oldFile }]); return; } + if (repro) { + yield* rollback; + assert.deepEqual( + sessions.get(liveFile)?.map(({ id }) => id), + ["u1", "a1", "u2", "steer", "a2"], + ); + return; + } const restored = yield* rollback; assert.equal( restored.providerThread.nativeThreadRef?.nativeId, From 097840751ed2367f1a66be28f0881445c5fddd69 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:07:09 +0100 Subject: [PATCH 2/7] fix(pi): rewind removes discarded prompts after returning to a session Co-authored-by: GPT 6.2 Sol --- packages/provider-pi/src/server/adapter.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/provider-pi/src/server/adapter.ts b/packages/provider-pi/src/server/adapter.ts index 920acee2e200..c54a1f78df61 100644 --- a/packages/provider-pi/src/server/adapter.ts +++ b/packages/provider-pi/src/server/adapter.ts @@ -4084,7 +4084,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( rollbackInput.providerThread.createdAt, request({ type: "get_entries" }), ); - } else if (switchedTo === undefined) { + } else if (switchedTo === undefined && boundary === firstBoundary) { return piThreadSnapshot(state.providerThread); } else { const listing = piSessionListing(yield* request({ type: "get_entries" })); @@ -4096,8 +4096,8 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( return yield* protocolError( "Pi rollback could not verify the retained session head", ); - // A move can leave prompts behind in the source file, even when no later - // app turn points at that file. Keep steers sent during the retained turn. + // A move can leave prompts behind in the source file even if Pi has since + // returned to it. Keep steers sent during the retained turn. for (const id of [...walk.userEntryIds].reverse()) { const timestamp = listing.entries.get(id)?.timestamp; if (timestamp == null || target?.completedAt == null) From 451daa0267870df51b7e750ceb8f1f93d913de7f Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:20:00 +0100 Subject: [PATCH 3/7] test(pi): cover more session round trips during rewind Co-authored-by: GPT 6.2 Sol --- .../provider-pi/src/server/adapter.test.ts | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/packages/provider-pi/src/server/adapter.test.ts b/packages/provider-pi/src/server/adapter.test.ts index 5565f456677f..277ae651dd31 100644 --- a/packages/provider-pi/src/server/adapter.test.ts +++ b/packages/provider-pi/src/server/adapter.test.ts @@ -4634,6 +4634,104 @@ describe("PiAdapterV2", () => { }).pipe(Effect.scoped, Effect.provide(layerTest)), ); + it.effect.each([ + { checkpoint: "strong", handoff: "command", returnedPrompt: false, discarded: true }, + { checkpoint: "strong", handoff: "prompt", returnedPrompt: true, discarded: true }, + { checkpoint: "command", handoff: "prompt", returnedPrompt: false, discarded: false }, + { checkpoint: "weak", handoff: "prompt", returnedPrompt: false, discarded: false }, + ] as const)("restores the Pi checkpoint after a session round trip %j", (scenario) => + Effect.gen(function* () { + const fake = yield* makeFakePi; + const { runtime } = yield* openRuntime(fake); + const providerThread = yield* runtime.ensureThread({ + threadId: THREAD_ID, + modelSelection: modelSelection("default"), + runtimePolicy, + }); + const ref = (nativeId: string, strength: "strong" | "weak" = "strong") => ({ + driver: PI_PROVIDER, + nativeId, + strength, + }); + const sessionFile = providerThread.nativeThreadRef!.nativeId!; + const forkFile = "/fake/rewound.jsonl"; + const turn = ( + ordinal: number, + nativeTurnRef: OrchestrationV2ProviderTurn["nativeTurnRef"], + file = sessionFile, + ): OrchestrationV2ProviderTurn => ({ + id: ProviderTurnId.make(`turn-${ordinal}`), + providerThreadId: providerThread.id, + nodeId: NodeId.make(`node-${ordinal}`), + runAttemptId: null, + nativeTurnRef, + nativeThreadRef: ref(file), + ordinal, + status: "completed", + startedAt: at(ordinal * 50 - 50), + completedAt: at(ordinal * 50), + }); + const previous = turn(1, ref("u1")); + const target = turn( + 2, + scenario.checkpoint === "strong" + ? ref("u2") + : scenario.checkpoint === "weak" + ? ref("uncaptured", "weak") + : null, + ); + const retained = + scenario.checkpoint === "command" + ? TURN_ONE_ENTRIES + : [ + ...TURN_ONE_ENTRIES, + sessionMessage("u2", "a1b", "user", 60), + sessionMessage("a2", "u2", "assistant", 80), + ]; + const retainedHead = retained.at(-1)!.id; + const entries = [ + ...retained, + ...(scenario.discarded + ? [ + sessionMessage("discarded", retainedHead, "user", 110), + sessionMessage("discarded-answer", "discarded", "assistant", 120), + ] + : []), + ...(scenario.returnedPrompt ? [sessionMessage("u5", "discarded-answer", "user", 210)] : []), + ]; + fake.queueEntries({ entries, leafId: entries.at(-1)!.id }); + fake.queueEntries({ entries: retained, leafId: retainedHead }); + if (scenario.discarded) fake.queueState({ sessionFile: forkFile }); + const restored = yield* runtime.rollbackThread({ + providerThread, + target: { + type: "provider_turn", + checkpointId: CheckpointId.make("checkpoint-1"), + appRunOrdinal: target.ordinal, + providerTurn: target, + }, + providerThreadTurns: [ + previous, + target, + turn(3, scenario.handoff === "prompt" ? ref("u3") : null, "/fake/other.jsonl"), + turn(4, null), + ...(scenario.returnedPrompt ? [turn(5, ref("u5"))] : []), + ], + }); + const requests = fake.allRequests(); + assert.isFalse(requests.some((request) => request.type === "switch_session")); + assert.deepEqual( + requests.filter((request) => request.type === "fork").map((request) => request.entryId), + scenario.discarded ? ["discarded"] : [], + ); + assert.equal( + restored.providerThread.nativeThreadRef?.nativeId, + scenario.discarded ? forkFile : sessionFile, + ); + assert.equal(restored.providerThread.nativeConversationHeadRef?.nativeId, retainedHead); + }).pipe(Effect.scoped, Effect.provide(layerTest)), + ); + it.effect("rolls back a persisted thread on a fresh runtime after its session was released", () => Effect.gen(function* () { const previousFake = yield* makeFakePi; From b8c6d69f5356ab741100bdb22a699dbc225a7a04 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:21:07 +0100 Subject: [PATCH 4/7] fix(pi): rewind checks history across session handoffs Co-authored-by: GPT 6.2 Sol --- packages/provider-pi/src/server/adapter.ts | 66 +++++++++++++++++----- 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/packages/provider-pi/src/server/adapter.ts b/packages/provider-pi/src/server/adapter.ts index c54a1f78df61..a8636c29a8a9 100644 --- a/packages/provider-pi/src/server/adapter.ts +++ b/packages/provider-pi/src/server/adapter.ts @@ -3994,6 +3994,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( const isCurrentFile = (file: string) => currentFile != null && path.resolve(cwd, file) === path.resolve(cwd, currentFile); + const targetPath = + targetFile === undefined ? undefined : path.resolve(cwd, targetFile); + const isTargetFile = (file: string | null | undefined) => + file != null && path.resolve(cwd, file) === targetPath; const boundaryFile = firstBoundary.type === "nothing" ? undefined @@ -4004,7 +4008,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( if ( targetFile !== undefined && boundaryFile != null && - path.resolve(cwd, boundaryFile) !== path.resolve(cwd, targetFile) + !isTargetFile(boundaryFile) ) { // A previous fork can carry the checkpoint into the session with the discard boundary. const listing = @@ -4077,28 +4081,53 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( ); } } - let forkEntryId: string | undefined; - if (boundary.type !== "nothing") { - forkEntryId = yield* piForkEntry( - boundary, - rollbackInput.providerThread.createdAt, - request({ type: "get_entries" }), - ); - } else if (switchedTo === undefined && boundary === firstBoundary) { + const leftTargetFile = + target !== null && + isTargetFile(sourceFile) && + rollbackInput.providerThreadTurns.some((turn) => { + const file = turn.nativeThreadRef?.nativeId; + return turn.ordinal > target.ordinal && file != null && !isTargetFile(file); + }); + const verifySource = + leftTargetFile || (boundary.type === "nothing" && switchedTo !== undefined); + if (boundary.type === "nothing" && !verifySource) { return piThreadSnapshot(state.providerThread); - } else { - const listing = piSessionListing(yield* request({ type: "get_entries" })); + } + let forkEntryId: string | undefined; + let readEntries = request({ type: "get_entries" }); + if (verifySource) { + const data = yield* readEntries; + readEntries = Effect.succeed(data); + const listing = piSessionListing(data); + // Pi stores no per-turn head. For a command-only or uncaptured checkpoint, + // anchor the walk at the nearest earlier captured turn in the same file. + const anchorId = + targetId ?? + piTurnEntryId( + rollbackInput.providerThreadTurns + .filter( + (turn) => + target !== null && + turn.ordinal < target.ordinal && + isTargetFile(turn.nativeThreadRef?.nativeId) && + piTurnEntryId(turn) !== undefined, + ) + .sort((a, b) => b.ordinal - a.ordinal)[0], + ); const walk = - listing !== null && targetId !== undefined && listing.entries.has(targetId) - ? walkPiBranch(listing.entries, listing.leafId, targetId) + listing !== null && anchorId !== undefined && listing.entries.has(anchorId) + ? walkPiBranch(listing.entries, listing.leafId, anchorId) : null; if (listing === null || walk?.reachedStop !== true) return yield* protocolError( "Pi rollback could not verify the retained session head", ); - // A move can leave prompts behind in the source file even if Pi has since - // returned to it. Keep steers sent during the retained turn. + // A handoff can leave a prompt before the next recorded turn in this file. for (const id of [...walk.userEntryIds].reverse()) { + if (boundary.type === "entry" && id === boundary.entryId) { + forkEntryId = id; + break; + } const timestamp = listing.entries.get(id)?.timestamp; if (timestamp == null || target?.completedAt == null) return yield* protocolError( @@ -4110,6 +4139,13 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( } } } + if (forkEntryId === undefined && boundary.type !== "nothing") { + forkEntryId = yield* piForkEntry( + boundary, + rollbackInput.providerThread.createdAt, + readEntries, + ); + } if (forkEntryId !== undefined) { const forkData = yield* lifecycleRequest({ type: "fork", From f84d3d3d7e37fff1f8972ef4104b1672178529e2 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:32:25 +0100 Subject: [PATCH 5/7] test(pi): cover first-turn rewind after a session round trip Co-authored-by: GPT 6.2 Sol --- .../provider-pi/src/server/adapter.test.ts | 75 +++++++++++++++---- 1 file changed, 60 insertions(+), 15 deletions(-) diff --git a/packages/provider-pi/src/server/adapter.test.ts b/packages/provider-pi/src/server/adapter.test.ts index 277ae651dd31..abf1a2aabc18 100644 --- a/packages/provider-pi/src/server/adapter.test.ts +++ b/packages/provider-pi/src/server/adapter.test.ts @@ -163,6 +163,10 @@ interface FakePi { readonly emit: (record: PiRpcRecord, chunkBytes?: number) => Effect.Effect; readonly takeRequest: (type: string) => Effect.Effect; readonly queueEntries: (data: unknown) => void; + readonly setSessionEntries: ( + entries: ReadonlyArray>, + forkFile: string, + ) => void; readonly queueMessages: (data: unknown) => void; readonly vetoNextSwitch: () => void; readonly failNextFork: () => void; @@ -229,6 +233,8 @@ const makeFakePi: Effect.Effect = Effect.gen(function* () { const stdout = yield* Queue.unbounded(); const requests = yield* Queue.unbounded(); const entriesQueue: Array = []; + let sessionEntries: ReadonlyArray> | undefined; + let forkFile: string | undefined; const messagesQueue: Array = []; const stateQueue: Array> = []; const statsQueue: Array = []; @@ -351,7 +357,13 @@ const makeFakePi: Effect.Effect = Effect.gen(function* () { return { ...base, data: { cancelled } }; } case "get_entries": - return { ...base, data: entriesQueue.shift() ?? { entries: [], leafId: null } }; + return { + ...base, + data: entriesQueue.shift() ?? { + entries: sessionEntries ?? [], + leafId: sessionEntries?.at(-1)?.id ?? null, + }, + }; case "get_messages": return { ...base, data: messagesQueue.shift() ?? { messages: [] } }; case "get_session_stats": @@ -361,6 +373,13 @@ const makeFakePi: Effect.Effect = Effect.gen(function* () { failFork = false; return { ...base, success: false, error: "Invalid entry ID for forking" }; } + if (sessionEntries !== undefined && forkFile !== undefined) { + const index = sessionEntries.findIndex((entry) => entry.id === record["entryId"]); + if (index === -1) + return { ...base, success: false, error: "Invalid entry ID for forking" }; + sessionEntries = sessionEntries.slice(0, index); + sessionFile = forkFile; + } serviceTier = null; return { ...base, data: { text: "Hello pi", cancelled: false } }; case "clear_queue": @@ -468,6 +487,10 @@ const makeFakePi: Effect.Effect = Effect.gen(function* () { emit, takeRequest, queueEntries: (data) => entriesQueue.push(data), + setSessionEntries: (entries, file) => { + sessionEntries = entries; + forkFile = file; + }, queueMessages: (data) => messagesQueue.push(data), deferNextState: (started) => { deferState = true; @@ -4635,10 +4658,30 @@ describe("PiAdapterV2", () => { ); it.effect.each([ - { checkpoint: "strong", handoff: "command", returnedPrompt: false, discarded: true }, - { checkpoint: "strong", handoff: "prompt", returnedPrompt: true, discarded: true }, - { checkpoint: "command", handoff: "prompt", returnedPrompt: false, discarded: false }, - { checkpoint: "weak", handoff: "prompt", returnedPrompt: false, discarded: false }, + { + checkpoint: "strong", + ordinal: 2, + handoff: "command", + returnedPrompt: false, + discarded: true, + }, + { checkpoint: "strong", ordinal: 2, handoff: "prompt", returnedPrompt: true, discarded: true }, + { + checkpoint: "command", + ordinal: 2, + handoff: "prompt", + returnedPrompt: false, + discarded: false, + }, + { checkpoint: "weak", ordinal: 2, handoff: "prompt", returnedPrompt: false, discarded: false }, + { + checkpoint: "command", + ordinal: 1, + handoff: "prompt", + returnedPrompt: false, + discarded: false, + }, + { checkpoint: "weak", ordinal: 1, handoff: "prompt", returnedPrompt: true, discarded: true }, ] as const)("restores the Pi checkpoint after a session round trip %j", (scenario) => Effect.gen(function* () { const fake = yield* makeFakePi; @@ -4671,9 +4714,8 @@ describe("PiAdapterV2", () => { startedAt: at(ordinal * 50 - 50), completedAt: at(ordinal * 50), }); - const previous = turn(1, ref("u1")); const target = turn( - 2, + scenario.ordinal, scenario.checkpoint === "strong" ? ref("u2") : scenario.checkpoint === "weak" @@ -4681,7 +4723,7 @@ describe("PiAdapterV2", () => { : null, ); const retained = - scenario.checkpoint === "command" + scenario.ordinal === 1 || scenario.checkpoint === "command" ? TURN_ONE_ENTRIES : [ ...TURN_ONE_ENTRIES, @@ -4699,9 +4741,7 @@ describe("PiAdapterV2", () => { : []), ...(scenario.returnedPrompt ? [sessionMessage("u5", "discarded-answer", "user", 210)] : []), ]; - fake.queueEntries({ entries, leafId: entries.at(-1)!.id }); - fake.queueEntries({ entries: retained, leafId: retainedHead }); - if (scenario.discarded) fake.queueState({ sessionFile: forkFile }); + fake.setSessionEntries(entries, forkFile); const restored = yield* runtime.rollbackThread({ providerThread, target: { @@ -4711,11 +4751,16 @@ describe("PiAdapterV2", () => { providerTurn: target, }, providerThreadTurns: [ - previous, + ...(target.ordinal === 2 ? [turn(1, ref("u1"))] : []), target, - turn(3, scenario.handoff === "prompt" ? ref("u3") : null, "/fake/other.jsonl"), - turn(4, null), - ...(scenario.returnedPrompt ? [turn(5, ref("u5"))] : []), + turn( + target.ordinal + 1, + scenario.handoff === "prompt" ? ref("away") : null, + "/fake/other.jsonl", + ), + turn(target.ordinal + 2, null, "/fake/clone.jsonl"), + turn(target.ordinal + 3, null), + ...(scenario.returnedPrompt ? [turn(target.ordinal + 4, ref("u5"))] : []), ], }); const requests = fake.allRequests(); From ec12aa18dcab696b53c4762253de4403b712694c Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:32:59 +0100 Subject: [PATCH 6/7] fix(pi): verify uncaptured first checkpoints from the session root Co-authored-by: GPT 6.2 Sol --- packages/provider-pi/src/server/adapter.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/provider-pi/src/server/adapter.ts b/packages/provider-pi/src/server/adapter.ts index a8636c29a8a9..edf0614e072b 100644 --- a/packages/provider-pi/src/server/adapter.ts +++ b/packages/provider-pi/src/server/adapter.ts @@ -4099,8 +4099,8 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( const data = yield* readEntries; readEntries = Effect.succeed(data); const listing = piSessionListing(data); - // Pi stores no per-turn head. For a command-only or uncaptured checkpoint, - // anchor the walk at the nearest earlier captured turn in the same file. + // Without a captured checkpoint prompt, verify back to an earlier retained + // prompt in the same file or to the root if none was captured. const anchorId = targetId ?? piTurnEntryId( @@ -4113,9 +4113,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( piTurnEntryId(turn) !== undefined, ) .sort((a, b) => b.ordinal - a.ordinal)[0], - ); + ) ?? + null; const walk = - listing !== null && anchorId !== undefined && listing.entries.has(anchorId) + listing !== null && (anchorId === null || listing.entries.has(anchorId)) ? walkPiBranch(listing.entries, listing.leafId, anchorId) : null; if (listing === null || walk?.reachedStop !== true) From e7bddf0dc2b1df6c5d974225c921251abd739ce1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 06:54:53 +0000 Subject: [PATCH 7/7] test(pi): keep session round-trip rewind coverage in the adapter table Co-Authored-By: Claude Opus 5.5 --- .../provider-pi/src/server/rollback.test.ts | 50 ++++--------------- 1 file changed, 10 insertions(+), 40 deletions(-) diff --git a/packages/provider-pi/src/server/rollback.test.ts b/packages/provider-pi/src/server/rollback.test.ts index 9262a2ddc2d6..a742af314d50 100644 --- a/packages/provider-pi/src/server/rollback.test.ts +++ b/packages/provider-pi/src/server/rollback.test.ts @@ -72,11 +72,9 @@ it.effect.each([ "checkpoint inherited by a prior fork", "repeated rollback after a handoff", "handoff prompt left in the source", - "an extension returning to the original file through a clone", "switch timed out after attaching", ] as const)("restores exactly the retained Pi history for %s", (scenario) => Effect.gen(function* () { - const repro = scenario === "an extension returning to the original file through a clone"; const fs = yield* FileSystem.FileSystem; const dir = yield* fs.makeTempDirectoryScoped(); const oldFile = `${dir}/old.jsonl`; @@ -91,7 +89,7 @@ it.effect.each([ entry("a2", "steer", "assistant", 35), ]; const inherited = scenario === "checkpoint inherited by a prior fork"; - const handoffEntry = entry("u3", inherited ? "a2" : null, "user", repro ? 55 : 50); + const handoffEntry = entry("u3", inherited ? "a2" : null, "user", 50); const newEntries = scenario === "command-only handoff" ? [] @@ -99,23 +97,11 @@ it.effect.each([ ? [...oldEntries, handoffEntry] : [handoffEntry]; const repeated = scenario === "repeated rollback after a handoff"; - const oldSuffix = repro || repeated || scenario === "handoff prompt left in the source"; + const oldSuffix = repeated || scenario === "handoff prompt left in the source"; const sessions = new Map([ - [ - oldFile, - oldSuffix - ? [ - ...oldEntries, - entry("discarded", "a2", "user", repro ? 52 : 45), - ...(repro ? [entry("discarded-answer", "discarded", "assistant", 54)] : []), - ] - : oldEntries, - ], + [oldFile, oldSuffix ? [...oldEntries, entry("discarded", "a2", "user", 45)] : oldEntries], [currentFile, newEntries], ]); - // The extension moved into the unrelated currentFile, then cloned the original at - // its new head. Returning from that clone does not add any entry to the original. - if (repro) sessions.set(previousForkFile, sessions.get(oldFile)!); if (repeated) sessions.set(previousForkFile, [...oldEntries, entry("u3", "a2", "user", 50)]); for (const [file, entries] of sessions) { yield* fs.writeFileString( @@ -129,7 +115,7 @@ it.effect.each([ const output = yield* Queue.unbounded(); const switchApplied = yield* Deferred.make(); const sessionRequests: { readonly type: string; readonly file: string }[] = []; - let liveFile = repro ? oldFile : currentFile; + let liveFile = currentFile; let pending = ""; const encoder = new TextEncoder(); const decoder = new TextDecoder(); @@ -220,31 +206,23 @@ it.effect.each([ }); const events = yield* runtime.events.pipe(Stream.runDrain, Effect.result, Effect.forkScoped); const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy }); - const turns = (repro ? [1, 2, 3, 4, 5] : repeated ? [1, 2, 3, 4] : [1, 2, 3]).map( + const turns = (repeated ? [1, 2, 3, 4] : [1, 2, 3]).map( (ordinal): OrchestrationV2ProviderTurn => ({ id: ProviderTurnId.make(`turn-${ordinal}`), providerThreadId: providerThread.id, nodeId: NodeId.make(`node-${ordinal}`), runAttemptId: null, nativeTurnRef: - repro && ordinal === 5 + ordinal === 3 && scenario === "command-only handoff" ? null - : repro && (ordinal === 3 || ordinal === 4) - ? ref(`uncaptured-${ordinal}`, "weak") - : ordinal === 3 && scenario === "command-only handoff" - ? null - : ordinal === 3 && scenario === "uncaptured handoff" - ? ref("uncaptured", "weak") - : ref(`u${ordinal}`), + : ordinal === 3 && scenario === "uncaptured handoff" + ? ref("uncaptured", "weak") + : ref(`u${ordinal}`), ...(scenario === "legacy turns" ? {} : { nativeThreadRef: ref( - ordinal < 3 || (repro && ordinal === 5) - ? oldFile - : (repro && ordinal === 4) || (ordinal === 3 && repeated) - ? previousForkFile - : currentFile, + ordinal < 3 ? oldFile : ordinal === 3 && repeated ? previousForkFile : currentFile, ), }), ordinal, @@ -276,14 +254,6 @@ it.effect.each([ assert.deepEqual(sessionRequests, [{ type: "switch_session", file: oldFile }]); return; } - if (repro) { - yield* rollback; - assert.deepEqual( - sessions.get(liveFile)?.map(({ id }) => id), - ["u1", "a1", "u2", "steer", "a2"], - ); - return; - } const restored = yield* rollback; assert.equal( restored.providerThread.nativeThreadRef?.nativeId,