Skip to content

Commit a240756

Browse files
committed
fix(server): a Pi session replacement no longer hides earlier runs
1 parent f6db95e commit a240756

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,52 @@ describe("PiAdapterV2", () => {
22412241
}).pipe(Effect.scoped, Effect.provide(layerTest)),
22422242
);
22432243

2244+
it.effect("does not reconcile a replacement session tree as a native rewind", () =>
2245+
Effect.gen(function* () {
2246+
const fake = yield* makeFakePi;
2247+
fake.queueEntries({
2248+
entries: [{ type: "model_change", id: "old-leaf", parentId: null }],
2249+
leafId: "old-leaf",
2250+
});
2251+
const { runtime, takeEvent } = yield* openRuntime(fake);
2252+
const providerThread = yield* runtime.ensureThread({
2253+
threadId: THREAD_ID,
2254+
modelSelection: modelSelection("default"),
2255+
runtimePolicy,
2256+
});
2257+
const finish = Effect.fnUntraced(function* () {
2258+
yield* startTurn(runtime, providerThread);
2259+
yield* fake.takeRequest("prompt");
2260+
yield* fake.emit({ type: "response", command: "prompt", success: true });
2261+
yield* fake.emit({ type: "agent_start" });
2262+
yield* fake.emit({ type: "agent_settled" });
2263+
const update = yield* takeEvent(
2264+
(event) =>
2265+
event.type === "provider_thread.updated" && event.providerThread.status === "idle",
2266+
);
2267+
yield* takeEvent((event) => event.type === "turn.terminal");
2268+
assert.isUndefined(
2269+
update.type === "provider_thread.updated" ? update.retainedNativeTurnIds : null,
2270+
);
2271+
});
2272+
// A failed cursor read is followed by a full listing from a new session.
2273+
fake.queueEntries({ entries: [] });
2274+
yield* finish();
2275+
fake.queueEntries({
2276+
entries: [
2277+
{
2278+
type: "message",
2279+
id: "new-user",
2280+
parentId: null,
2281+
message: { role: "user", content: [] },
2282+
},
2283+
],
2284+
leafId: "new-user",
2285+
});
2286+
yield* finish();
2287+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
2288+
);
2289+
22442290
it.effect("rolls back past turns that left nothing in the session tree", () =>
22452291
Effect.gen(function* () {
22462292
const fake = yield* makeFakePi;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,12 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
14491449
const turnStartEntryId = turnWalk.userEntryIds.at(-1);
14501450
// Only a branch walked to its root lists every survivor; a partial or
14511451
// cyclic walk would roll back runs that are still in the conversation.
1452-
const branch = rewound ? walkPiBranch(tree.entries, listing.leafId, null) : undefined;
1452+
// A replaced session has a different tree. Only an old leaf still in
1453+
// this tree proves an in-session rewind rather than a session switch.
1454+
const branch =
1455+
rewound && previousLeaf !== null && tree.entries.has(previousLeaf)
1456+
? walkPiBranch(tree.entries, listing.leafId, null)
1457+
: undefined;
14531458
return {
14541459
// Without a trustworthy cursor the walk spans more than one turn, so
14551460
// its earliest user entry belongs to an earlier turn. Keep the

0 commit comments

Comments
 (0)