Skip to content

[Bug]: Pi thread resumes its first session file after an extension switches to a new session, so later context is lost #37465

Open

Description

@tpgionfriddo

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/server (packages/provider-pi/src/server/adapter.ts)

Steps to reproduce

  1. Load a Pi extension with a command that starts a new session, for example:

    import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
    
    export default function (pi: ExtensionAPI) {
      pi.registerCommand("handoff", {
        description: "Continue in a new child session",
        handler: async (_args, ctx) => {
          await ctx.newSession({ parentSession: ctx.sessionManager.getSessionFile() });
        },
      });
    }
  2. Start a Pi thread in T3 Code. Send prompt A and let the turn complete.

  3. Send /handoff. Pi now writes to a new session file whose header has parentSession set to the first file.

  4. Send prompt B and let the turn complete. It is written to the new file.

  5. Restart the T3 server, or wait for the idle release, so that the Pi process ends.

  6. Send prompt C.

Expected behavior

T3 Code resumes the session that Pi used last (the child file). Prompt C sees prompt B and its reply.

Actual behavior

T3 Code resumes the first session file. Prompt C does not see prompt B or anything else from the child session. The T3 timeline still shows B, so the user does not see that the model lost it.

Each later handoff then starts from the first file again, so every new child session also misses the previous child's history.

Analysis

When registerThread resumes a thread, it switches Pi to the stored native ref:

const resumeId = existing?.nativeThreadRef?.nativeId;
// ...
{ type: "switch_session", sessionPath: resumeId }

nativeThreadRef is set only here, from get_state.sessionFile, when T3 attaches to Pi. Nothing updates it when Pi changes sessions during the thread. The settle probe already calls get_state after each turn, but it reads only isStreaming, isCompacting and pendingMessageCount. It ignores sessionFile.

Evidence from one affected thread (statev2.sqlite, orchestration_v2_projection_provider_threads.payload_json):

  • nativeThreadRef.nativeId points at the first session file (created 2026-11-15T07:01Z). handoffIds is [].
  • An extension created 6 child session files over about 20 hours. All have parentSession set to that first file.
  • The first file contains 7 reattach points (model_change followed by session_info). After each one, the user's next message went into the first file, not into the newest child.
  • In the last case, the user had merged a PR in the newest child session. After a restart, the thread resumed the first file, and the model reported that it was "working from an old state".

Related, but different:

Possible fix: after each turn, compare get_state.sessionFile with nativeThreadRef.nativeId. If they differ, save the new file as the thread's native ref. The settle probe already has this data. Another option is for T3's injected Pi extension to report session_start events with reason new, fork or resume.

Impact

Major degradation or frequent failure

Version or commit

T3 Code 0.1.9 (desktop)

Environment

macOS 15.7.7, Apple silicon. Pi 1.5.2 (Homebrew). The extension starts a new child session at workflow boundaries with ctx.newSession({ parentSession }).

Logs or stack traces

No error is raised. The fault shows in the session files and in statev2.sqlite, as described above.

Workaround

After a restart, do not continue a thread that has switched sessions. Start a new thread and point it at the newest child session file.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions