Skip to content

[Bug]: ThreadHandoff.settle() loads all thread runs and user messages before checking thread.handoff #46314

Done

Description

@alexskatell

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

Summary

Follow-up to #46286 / #46293.

PR #46293 fixed the startup replay storm by switching ThreadHandoff.start_() from threads.streamStoredEvents to threads.streamDomainEvents, but ThreadHandoff.settle() still unconditionally loads all runs and all user messages for every thread before checking whether the thread even has a handoff:

apps/server/src/peer/handoff/ThreadHandoff.ts:880-886:

const settle = Effect.fn("ThreadHandoff.settle")(
  function* (threadId: ThreadId) {
    const records = yield* threads.getThreadRecords(threadId, ["runs", "messages"], {
      messageRoles: ["user"],
    });
    const handoff = records.thread.handoff;
    if (handoff === undefined) return;

Even after #46293, every live terminal run.updated event (completed, failed, cancelled, interrupted, rolled_back) triggers ThreadHandoff.settle(threadId). On long-lived threads (e.g., orchestrator / planning threads with thousands to tens of thousands of runs and user messages), each live turn completion runs ProjectionStore.readCanonicalProjection across all projection_runs and projection_messages, spiking V8 heap by ~165–400 MB per turn end on threads where thread.handoff is undefined.

This compounds directly with #45047 (getTurnStartContext loading all runs/attempts/provider turns at turn start) inside Electron's shared 4 GiB V8 pointer-compression cage.

Steps to reproduce

  1. Run T3 Code desktop server against a statev2.sqlite containing a long-lived thread (thousands of runs and user messages) with no thread handoff (thread.handoff === undefined).
  2. Complete a turn on that thread (emitting terminal run.updated).
  3. Inspect server.trace.ndjson for ThreadHandoff.settle -> ThreadManagementService.getThreadRecords -> ProjectionStore.readCanonicalProjection (projection_runs + projection_messages).

Expected behavior

ThreadHandoff.settle() performs a lightweight thread check first (e.g., threads.getThreadRecords(threadId, []) — matching line 924 — or projections.getThread / threads.getThreadShell) and returns immediately when thread.handoff === undefined (or jumps straight to depart(threadId) when thread.handoff.state === "departing"), loading ["runs", "messages"] only when thread.handoff.state === "pending".

Actual behavior

Every live terminal run.updated event loads full runs and user messages for the thread before checking records.thread.handoff === undefined, producing ~165–400 MB heap spikes per turn completion on large threads with 0 handoffs.

Impact

Causes severe memory spikes and accelerates V8 4 GiB cage exhaustion / OOM crashes on active workspaces with long-lived threads (especially alongside #45047).

Version or commit

Present on main after #46293 (and in v0.1.14-nightly.20261130.5479 / v0.1.14-nightly.20261130.5492; introduced in #44774).

Environment

  • macOS arm64 (Mac Mini, 34 GB RAM)
  • statev2.sqlite: ~53 GiB, long-lived threads up to ~26,000 runs, 0 active thread handoffs

Logs or stack traces

Observed in server.trace.ndjson on v0.1.14-nightly.20261130.5492 during live ThreadHandoff.settle calls on a ~26k-run thread with handoff === undefined:

ThreadHandoff.settle -> ProjectionStore.readCanonicalProjection:
  - max single-call heap growth: +397.7 MB to +404.5 MB
  - crash-second concurrent spans (18:42:06Z):
      ThreadHandoff.settle: projection_runs (+36.8 MB) + projection_messages (+164.5 MB)
      running concurrently with getTurnStartContext (#45047):
      projection_runs (+28.4 MB) + projection_run_attempts (+299.3 MB) + projection_subagents (+59.6 MB) + projection_provider_turns (+48.2 MB)
  -> FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
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