@devries-cameron20 / t3code Mirror · read-only / commits / 05ccd6bfb5
Fork 0

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 21, 09:45 UTC

fix(pi): resume the session file an extension switched the thread to (#37472)

Julius Marminge committed Nov 15, 2026 at 20:00 UTC 05ccd6bfb5c49fe73cd979c03ec8eb3e6f45fa6a
2 files changed +70 -11
packages/provider-pi/src/server/adapter.test.ts
+43 -2
@@ -183,7 +183,7 @@ interface FakePi {
183 /** T3 extension's service tier as each prompt was sent; Pi resets it with each session. */
184 readonly promptServiceTiers: ReadonlyArray<string | null>;
185 /** Pi reloads extensions without the adapter seeing it, e.g. a user extension's ctx.newSession(). */
186 - readonly reloadExtensions: () => void;
186 + readonly reloadExtensions: (newSessionFile?: string) => void;
187 /** Pi before 0.99 acknowledges prompts without a disposition. */
188 readonly omitPromptDisposition: () => void;
189 readonly queueStats: (data: unknown) => void;
@@ -433,8 +433,9 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
433 promptThinkingLevels,
434 promptModels,
435 promptServiceTiers,
436 - reloadExtensions: () => {
436 + reloadExtensions: (newSessionFile) => {
437 serviceTier = null;
438 + if (newSessionFile !== undefined) sessionFile = newSessionFile;
439 },
440 omitPromptDisposition: () => {
441 promptDisposition = false;
@@ -2763,6 +2764,46 @@ describe("PiAdapterV2", () => {
2764 }).pipe(Effect.scoped, Effect.provide(layerTest)),
2765 );
2766
2767 + it.effect("tracks the session file a user extension switches to during a turn", () =>
2768 + Effect.gen(function* () {
2769 + const fake = yield* makeFakePi;
2770 + const { runtime, takeEvent } = yield* openRuntime(fake);
2771 + const providerThread = yield* runtime.ensureThread({
2772 + threadId: THREAD_ID,
2773 + modelSelection: modelSelection("default"),
2774 + runtimePolicy,
2775 + });
2776 + assert.equal(providerThread.nativeThreadRef?.nativeId, FAKE_SESSION_FILE);
2777 + const childFile = "/fake/extension-child.jsonl";
2778 +
2779 + // An extension command such as /handoff calls ctx.newSession(); later prompts go to its file.
2780 + yield* startTurn(runtime, providerThread, "default", [], "/handoff", undefined, 1);
2781 + yield* fake.takeRequest("prompt");
2782 + fake.reloadExtensions(childFile);
2783 + yield* fake.emit({ type: "response", command: "prompt", success: true });
2784 + yield* takeEvent((event) => event.type === "turn.terminal");
2785 +
2786 + yield* startTurn(runtime, providerThread, "default", [], "prompt B", undefined, 2);
2787 + yield* fake.takeRequest("prompt");
2788 + yield* fake.emit({ type: "agent_start" });
2789 + yield* fake.emit({ type: "agent_end", messages: [], willRetry: false });
2790 + yield* fake.emit({ type: "agent_settled" });
2791 + const idle = yield* takeEvent(
2792 + (event) =>
2793 + event.type === "provider_thread.updated" && event.providerThread.status === "idle",
2794 + );
2795 + yield* takeEvent((event) => event.type === "turn.terminal");
2796 +
2797 + // The persisted ref is what a later resume passes to switch_session.
2798 + assert.equal(
2799 + idle.type === "provider_thread.updated"
2800 + ? idle.providerThread.nativeThreadRef?.nativeId
2801 + : undefined,
2802 + childFile,
2803 + );
2804 + }).pipe(Effect.scoped, Effect.provide(layerTest)),
2805 + );
2806 +
2807 it.effect.each([false, true, "previous"] as const)(
2808 "resets applied thinking when returning to Pi default (failed first restore: %s)",
2809 (failFirstRestore) =>
packages/provider-pi/src/server/adapter.ts
+27 -9
@@ -431,8 +431,14 @@ interface PiPromptSend {
431 interface PiThreadState {
432 providerThread: OrchestrationV2ProviderThread;
433 activeTurn: ActivePiTurn | null;
434 + /** Files an extension moved this thread off (ctx.newSession); queued turns may still name them. */
435 + readonly supersededSessionFiles: Set<string>;
436 }
437
438 +const isLiveSessionOf = (state: PiThreadState, nativeId: string | null | undefined) =>
439 + nativeId === state.providerThread.nativeThreadRef?.nativeId ||
440 + (nativeId != null && state.supersededSessionFiles.has(nativeId));
441 +
442 interface PiWake {
443 readonly state: PiThreadState;
444 readonly events: Array<PiRpcRecord>;
@@ -2689,7 +2695,20 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
2695 (recordNumber(data, "pendingMessageCount") ?? 0) === 0
2696 ) {
2697 turn.settleWhenIdle = false;
2692 - if (state !== null) yield* finalizeTurn(state);
2698 + if (state === null) return;
2699 + // Extensions can move Pi to another session file (ctx.newSession); resume that one.
2700 + const sessionFile = recordString(data, "sessionFile");
2701 + const previousFile = state.providerThread.nativeThreadRef?.nativeId;
2702 + if (sessionFile !== undefined && sessionFile !== previousFile) {
2703 + if (previousFile != null) state.supersededSessionFiles.add(previousFile);
2704 + state.supersededSessionFiles.delete(sessionFile);
2705 + state.providerThread = {
2706 + ...state.providerThread,
2707 + nativeThreadRef: providerRef(sessionFile),
2708 + };
2709 + lastNativeThreadId = sessionFile;
2710 + }
2711 + yield* finalizeTurn(state);
2712 }
2713 return;
2714 }
@@ -3020,7 +3039,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
3039 "Cannot switch Pi threads while native work is awaiting a turn",
3040 );
3041 }
3023 - threadState = { providerThread, activeTurn: null };
3042 + threadState = { providerThread, activeTurn: null, supersededSessionFiles: new Set() };
3043 }
3044 const baselineEntries = yield* request({ type: "get_entries" }).pipe(
3045 Effect.orElseSucceed(() => undefined),
@@ -3237,10 +3256,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
3256 if (pendingPromptResponsesDrained === null) break;
3257 yield* Deferred.await(pendingPromptResponsesDrained);
3258 }
3240 - if (
3241 - state.providerThread.nativeThreadRef?.nativeId !==
3242 - turnInput.providerThread.nativeThreadRef?.nativeId
3243 - ) {
3259 + if (!isLiveSessionOf(state, turnInput.providerThread.nativeThreadRef?.nativeId)) {
3260 return yield* protocolError("Pi turn requested for a different native session");
3261 }
3262 // Fork adoption uses the orchestrator-allocated row; preserve that identity in later updates.
@@ -3323,8 +3339,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
3339 closed ||
3340 stopRequested ||
3341 rollbackBarrier !== null ||
3326 - state.providerThread.nativeThreadRef?.nativeId !==
3327 - turnInput.providerThread.nativeThreadRef?.nativeId
3342 + !isLiveSessionOf(state, turnInput.providerThread.nativeThreadRef?.nativeId)
3343 ) {
3344 return yield* protocolError("Pi turn ownership changed before it could start");
3345 }
@@ -3334,7 +3349,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
3349 return yield* protocolError("Pi native wake belongs to a different app thread");
3350 }
3351 pendingWake = null;
3337 - state.providerThread = turnInput.providerThread;
3352 + state.providerThread = {
3353 + ...turnInput.providerThread,
3354 + nativeThreadRef: state.providerThread.nativeThreadRef,
3355 + };
3356 state.activeTurn = activeTurn;
3357 activeTurn.adoptedWake = wake !== null && !continuation;
3358 if (compactCommand !== null) {