Skip to content

Commit aa0158d

Browse files
fix(pi): fork completed turns while the next turn is running (pingdotgg#23052)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 6318bfc commit aa0158d

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
import * as Cause from "effect/Cause";
2222
import * as DateTime from "effect/DateTime";
2323
import * as Effect from "effect/Effect";
24+
import * as Exit from "effect/Exit";
2425
import * as Fiber from "effect/Fiber";
2526
import * as Layer from "effect/Layer";
2627
import * as Queue from "effect/Queue";
@@ -1801,6 +1802,51 @@ describe("PiAdapterV2", () => {
18011802
}).pipe(Effect.scoped, Effect.provide(layerTest)),
18021803
);
18031804

1805+
it.effect("natively forks before a running turn whose start entry was not captured yet", () =>
1806+
Effect.gen(function* () {
1807+
const fake = yield* makeFakePi;
1808+
const forkFake = yield* makeFakePi;
1809+
const forkFile = "/fake/forked.jsonl";
1810+
const { runtime } = yield* openRuntime(fake, "default", THREAD_ID, SESSION_ID, forkFake);
1811+
const source = yield* runtime.ensureThread({
1812+
threadId: THREAD_ID,
1813+
modelSelection: modelSelection("default"),
1814+
runtimePolicy,
1815+
});
1816+
// Turn 2 is still in flight, so it has no end to bound the session-tree search.
1817+
const turns = uncapturedTurns(source.id, { startedAt: 100, completedAt: 200 }).map((turn) =>
1818+
turn.ordinal === 2 ? { ...turn, status: "running" as const, completedAt: null } : turn,
1819+
);
1820+
forkFake.queueEntries(UNCAPTURED_TURN_TREE);
1821+
forkFake.queueState({ sessionFile: forkFile });
1822+
fake.queueState({ sessionFile: forkFile });
1823+
1824+
const result = yield* runtime
1825+
.forkThread({
1826+
sourceProviderThread: source,
1827+
sourceProviderTurns: turns,
1828+
providerTurnId: turns[0]!.id,
1829+
targetThreadId: ThreadId.make("fork-target"),
1830+
})
1831+
.pipe(Effect.exit);
1832+
assert.isTrue(
1833+
Exit.isSuccess(result),
1834+
`forking turn 1 while turn 2 runs must locate turn 2's prompt in the session tree, got: ${
1835+
Exit.isFailure(result) ? Cause.pretty(result.cause) : ""
1836+
}`,
1837+
);
1838+
if (!Exit.isSuccess(result)) return;
1839+
assert.equal(result.value.nativeThreadRef?.nativeId, forkFile);
1840+
1841+
const forkedEntries = forkFake
1842+
.allRequests()
1843+
.filter((request) => request.type === "fork")
1844+
.map((request) => request.entryId);
1845+
// Pi keeps only the fork entry's ancestors, so u2 (not steer2) drops all of turn 2.
1846+
assert.deepEqual(forkedEntries, ["u2"]);
1847+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
1848+
);
1849+
18041850
it.effect("presents explicitly namespaced MCP extension tools", () =>
18051851
Effect.gen(function* () {
18061852
const fake = yield* makeFakePi;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3234,21 +3234,22 @@ function piForkEntry<E>(
32343234

32353235
/**
32363236
* Pi shares the server clock. An adopted wake can precede its T3 turn, so search from the target
3237-
* turn's end through the boundary turn's end. At thread start, use the earlier of thread creation
3238-
* and turn start to include adopted wakes while preserving inherited session history.
3237+
* turn's end through the boundary turn's end. A boundary turn still running has no end yet, so
3238+
* the window stays open through the tree read. At thread start, use the earlier of thread
3239+
* creation and turn start to include adopted wakes while preserving inherited session history.
32393240
*/
32403241
function piUncapturedForkEntry(
32413242
listing: PiSessionListing,
32423243
boundary: Extract<PiForkBoundary, { type: "uncaptured" }>,
32433244
providerThreadCreatedAt: DateTime.Utc,
32443245
): string | undefined {
32453246
const { startedAt, completedAt } = boundary.turn;
3246-
if (startedAt === null || completedAt === null) return undefined;
3247+
if (startedAt === null) return undefined;
32473248
const from =
32483249
boundary.target === null
32493250
? Math.min(DateTime.toEpochMillis(providerThreadCreatedAt), DateTime.toEpochMillis(startedAt))
32503251
: DateTime.toEpochMillis(boundary.target.completedAt ?? startedAt);
3251-
const to = DateTime.toEpochMillis(completedAt);
3252+
const to = completedAt === null ? Infinity : DateTime.toEpochMillis(completedAt);
32523253
const targetRef = boundary.target?.nativeTurnRef;
32533254
const stopAt = targetRef?.strength === "strong" ? targetRef.nativeId : null;
32543255
if (stopAt !== null && !listing.entries.has(stopAt)) return undefined;

0 commit comments

Comments
 (0)