t3code icon indicating copy to clipboard operation
t3code copied to clipboard

[Bug]: Pi exiting while its session is idle puts "The provider event stream closed unexpectedly" on the thread

Open ristoxy opened this issue 1 week ago • 0 comments

Before submitting

  • [x] I searched existing issues and did not find a duplicate. Same banner, other triggers: #22748, #15135. Other Pi exit paths: #31519 (crash on resume), #15589 (unsolicited wake), #28653 (stderr not logged).
  • [x] I included enough detail to reproduce or investigate the problem.

Area

apps/server (Pi adapter in packages/provider-pi)

Summary

When the Pi process ends while no turn is running and T3 did not ask it to stop, the Pi adapter marks the provider session error and fails its event stream. The thread then shows "The provider event stream closed unexpectedly. Retry the turn; if it keeps failing, check the provider and server logs." No turn was affected, and the next message starts Pi again and continues the conversation normally.

T3's own idle release of the same session (30 minutes for Pi, NO_NATIVE_RESUME_IDLE_TIMEOUT_MS) ends it as stopped with no banner.

We hit this with a host tool that sends SIGTERM to idle Pi processes after Pi extensions are updated, so that the next prompt starts Pi with the new code (Pi loads extensions only at startup).

Steps to reproduce

  1. Open a Pi thread and let a turn finish.
  2. While the thread is idle, end its Pi process from outside: kill -TERM <pi pid>.
  3. Look at the thread.

Adapter-level repro on main: append this to packages/provider-pi/src/server/adapter.test.ts and run vp test run src/server/adapter.test.ts -t "repro: Pi exits" --silent=false in packages/provider-pi.

it.effect("repro: Pi exits while no turn is running", () =>
  Effect.gen(function* () {
    const fake = yield* makeFakePi;
    const adapter = yield* makeAdapter(fake, "");
    const runtime = yield* adapter.openSession({
      threadId: THREAD_ID,
      providerSessionId: SESSION_ID,
      modelSelection: modelSelection("default"),
      runtimePolicy,
    });
    const statuses: Array<string> = [];
    const drained = yield* runtime.events.pipe(
      Stream.runForEach((event) =>
        Effect.sync(() => {
          if (event.type === "provider_session.updated")
            statuses.push(`${event.providerSession.status}: ${event.providerSession.lastError}`);
        }),
      ),
      Effect.exit,
      Effect.forkScoped,
    );
    yield* runtime.ensureThread({
      threadId: THREAD_ID,
      modelSelection: modelSelection("default"),
      runtimePolicy,
    });
    // Pi ends on its own (e.g. SIGTERM from outside) while the session is idle.
    yield* fake.closeStdout;
    const exit = yield* Fiber.join(drained);
    console.log("session updates:", statuses);
    console.log(
      "event stream:",
      Exit.isSuccess(exit) ? "ended" : `failed with ${Cause.squash(exit.cause)?.constructor?.name}`,
    );
  }).pipe(Effect.scoped, Effect.provide(layerTest)),
);

Output:

session updates: [ 'error: Pi process exited unexpectedly.' ]
event stream: failed with ProviderAdapterEventStreamError

Expected behavior

With no active turn, a Pi exit is handled like an idle release: the session becomes stopped, no banner appears, and the next message resumes the session as it already does.

Actual behavior

The session becomes error with "Pi process exited unexpectedly.", and the thread shows "The provider event stream closed unexpectedly. Retry the turn; ..." although there is no turn to retry.

Code path at d7ae1e1:

  1. packages/provider-pi/src/server/adapter.ts:2903-2934: with no activeTurn the adapter only cancels pending prompts. Unless stopRequested (or unsolicited activity) is set, it calls updateProviderSession("error", "Pi process exited unexpectedly.") and Queue.fail(events, new ProviderAdapterEventStreamError(...)). Only the stopRequested branch (:2919-2921) ends as stopped with Queue.end.
  2. apps/server/src/orchestration-v2/ProviderSessionManager.ts:3173-3204: a stream that ended after a stopped status is released as manual_shutdown; a failed stream is released as runtime_error with makeProviderFailure({ cause }).message as the detail.
  3. ProviderSessionManager.ts:839-840 writes that detail as lastError (the thread banner). The text comes from packages/provider-core/src/server/failure.ts:57-60.
  4. For comparison, the idle release (ProviderSessionManager.ts:1722-1726, reason idle_timeout) gets status stopped and lastError: null (:374, :839-840).

One way to fix it: when no turn is active, end the session like the stopRequested branch. Whether an outside exit should count as a clean stop is your call.

Impact

Minor bug or occasional failure

Version or commit

main @ d7ae1e17915c40602e7eb47dce27a6a36167f4c4

Environment

Linux, Pi provider. The repro above uses the fake Pi from the adapter tests, with no network.

Workaround

Ignore the banner and send the next message. We found no outside way (CLI, API or MCP tool) to release an idle provider session cleanly, so the tool cannot avoid the banner.

Prepared by Opus 5.5, reviewed by human before posting.

ristoxy avatar Nov 24 '26 13:11 ristoxy