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 22, 19:30 UTC
fix(pi): fork completed turns while the next turn is running (#23052)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 21, 2026 at 19:17 UTC
aa0158d0d3099bada270b059b7ca7226a547bea1
2 files changed
+51
-4
packages/provider-pi/src/server/adapter.test.ts
+46
@@ -21,6 +21,7 @@ import {
21
import * as Cause from "effect/Cause";
22
import * as DateTime from "effect/DateTime";
23
import * as Effect from "effect/Effect";
24
+import * as Exit from "effect/Exit";
25
import * as Fiber from "effect/Fiber";
26
import * as Layer from "effect/Layer";
27
import * as Queue from "effect/Queue";
@@ -1801,6 +1802,51 @@ describe("PiAdapterV2", () => {
1802
}).pipe(Effect.scoped, Effect.provide(layerTest)),
1803
);
1804
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
+
1850
it.effect("presents explicitly namespaced MCP extension tools", () =>
1851
Effect.gen(function* () {
1852
const fake = yield* makeFakePi;
packages/provider-pi/src/server/adapter.ts
+5
-4
@@ -3234,8 +3234,9 @@ function piForkEntry<E>(
3234
3235
/**
3236
* 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.
3240
*/
3241
function piUncapturedForkEntry(
3242
listing: PiSessionListing,
@@ -3243,12 +3244,12 @@ function piUncapturedForkEntry(
3244
providerThreadCreatedAt: DateTime.Utc,
3245
): string | undefined {
3246
const { startedAt, completedAt } = boundary.turn;
3246
- if (startedAt === null || completedAt === null) return undefined;
3247
+ if (startedAt === null) return undefined;
3248
const from =
3249
boundary.target === null
3250
? Math.min(DateTime.toEpochMillis(providerThreadCreatedAt), DateTime.toEpochMillis(startedAt))
3251
: DateTime.toEpochMillis(boundary.target.completedAt ?? startedAt);
3251
- const to = DateTime.toEpochMillis(completedAt);
3252
+ const to = completedAt === null ? Infinity : DateTime.toEpochMillis(completedAt);
3253
const targetRef = boundary.target?.nativeTurnRef;
3254
const stopAt = targetRef?.strength === "strong" ? targetRef.nativeId : null;
3255
if (stopAt !== null && !listing.entries.has(stopAt)) return undefined;