TypeScript
fix(pi): fork completed turns while the next turn is running (#23052)
Julius Marmingeaa0158d0d309
diff --git a/packages/provider-pi/src/server/adapter.test.ts b/packages/provider-pi/src/server/adapter.test.tsindex dcc9023c2b9..9c81bab0abb 100644--- a/packages/provider-pi/src/server/adapter.test.ts+++ b/packages/provider-pi/src/server/adapter.test.ts@@ -21,6 +21,7 @@ import {import * as Cause from "effect/Cause";import * as DateTime from "effect/DateTime";import * as Effect from "effect/Effect";+import * as Exit from "effect/Exit";import * as Fiber from "effect/Fiber";import * as Layer from "effect/Layer";import * as Queue from "effect/Queue";@@ -1801,6 +1802,51 @@ describe("PiAdapterV2", () => {}).pipe(Effect.scoped, Effect.provide(layerTest)),);+ it.effect("natively forks before a running turn whose start entry was not captured yet", () =>+ Effect.gen(function* () {+ const fake = yield* makeFakePi;+ const forkFake = yield* makeFakePi;+ const forkFile = "/fake/forked.jsonl";+ const { runtime } = yield* openRuntime(fake, "default", THREAD_ID, SESSION_ID, forkFake);+ const source = yield* runtime.ensureThread({+ threadId: THREAD_ID,+ modelSelection: modelSelection("default"),+ runtimePolicy,+ });+ // Turn 2 is still in flight, so it has no end to bound the session-tree search.+ const turns = uncapturedTurns(source.id, { startedAt: 100, completedAt: 200 }).map((turn) =>+ turn.ordinal === 2 ? { ...turn, status: "running" as const, completedAt: null } : turn,+ );+ forkFake.queueEntries(UNCAPTURED_TURN_TREE);+ forkFake.queueState({ sessionFile: forkFile });+ fake.queueState({ sessionFile: forkFile });++ const result = yield* runtime+ .forkThread({+ sourceProviderThread: source,+ sourceProviderTurns: turns,+ providerTurnId: turns[0]!.id,+ targetThreadId: ThreadId.make("fork-target"),+ })+ .pipe(Effect.exit);+ assert.isTrue(+ Exit.isSuccess(result),+ `forking turn 1 while turn 2 runs must locate turn 2's prompt in the session tree, got: ${+ Exit.isFailure(result) ? Cause.pretty(result.cause) : ""+ }`,+ );+ if (!Exit.isSuccess(result)) return;+ assert.equal(result.value.nativeThreadRef?.nativeId, forkFile);++ const forkedEntries = forkFake+ .allRequests()+ .filter((request) => request.type === "fork")+ .map((request) => request.entryId);+ // Pi keeps only the fork entry's ancestors, so u2 (not steer2) drops all of turn 2.+ assert.deepEqual(forkedEntries, ["u2"]);+ }).pipe(Effect.scoped, Effect.provide(layerTest)),+ );+it.effect("presents explicitly namespaced MCP extension tools", () =>Effect.gen(function* () {const fake = yield* makeFakePi;diff --git a/packages/provider-pi/src/server/adapter.ts b/packages/provider-pi/src/server/adapter.tsindex ac2c321323f..a2a3caf9267 100644--- a/packages/provider-pi/src/server/adapter.ts+++ b/packages/provider-pi/src/server/adapter.ts@@ -3234,8 +3234,9 @@ function piForkEntry<E>(/*** Pi shares the server clock. An adopted wake can precede its T3 turn, so search from the target- * turn's end through the boundary turn's end. At thread start, use the earlier of thread creation- * and turn start to include adopted wakes while preserving inherited session history.+ * turn's end through the boundary turn's end. A boundary turn still running has no end yet, so+ * the window stays open through the tree read. At thread start, use the earlier of thread+ * creation and turn start to include adopted wakes while preserving inherited session history.*/function piUncapturedForkEntry(listing: PiSessionListing,@@ -3243,12 +3244,12 @@ function piUncapturedForkEntry(providerThreadCreatedAt: DateTime.Utc,): string | undefined {const { startedAt, completedAt } = boundary.turn;- if (startedAt === null || completedAt === null) return undefined;+ if (startedAt === null) return undefined;const from =boundary.target === null? Math.min(DateTime.toEpochMillis(providerThreadCreatedAt), DateTime.toEpochMillis(startedAt)): DateTime.toEpochMillis(boundary.target.completedAt ?? startedAt);- const to = DateTime.toEpochMillis(completedAt);+ const to = completedAt === null ? Infinity : DateTime.toEpochMillis(completedAt);const targetRef = boundary.target?.nativeTurnRef;const stopAt = targetRef?.strength === "strong" ? targetRef.nativeId : null;if (stopAt !== null && !listing.entries.has(stopAt)) return undefined;