perf(client): stop thread streams when unused #9740
main fromrefs/pull/9740/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubClosing a thread kept its detail stream active for five minutes. Closed threads could still receive and reduce live events.
The last detail consumer now closes the stream. A separate cache retains completed thread data and its replay cursor for five idle minutes, so a quick return keeps the data and resumes without another snapshot download.
Keep pagination and deletion state. Clear canceled loading state on return. Prevent an old scope from replacing its successor's cache.
Skip final saves for data that is already on disk. Keep dirty-data flushes and failed-write retries. This avoids repeated snapshot encoding on navigation.
Verified with 50 focused tests, client-runtime typecheck, and changed-file lint. The tests cover stream ownership, warm resume, expiry, canceled page loads, interrupted event application, overlapping scopes, and final persistence. This shared change applies to web, desktop, and mobile. Wire contracts are unchanged. A real Node GC check confirms that warm data and its cursor survive collection of the old atom definitions. No browser or device ran.
Part of the performance audit, item C3.
Created with GPT-6 Astra (preview) in Codex.
Note
Medium Risk Changes core thread streaming, resume cursors, and disk cache semantics in client-runtime; behavior is heavily tested but affects all platforms’ thread detail lifecycle.
Overview
Live thread subscriptions now end when the last detail consumer unmounts, instead of keeping the WebSocket stream alive for five minutes. Detail projection atoms (threadDetail.ts) use zero idle TTL so they tear down with their subscribers.
A separate registry-local resume cache (five-minute idle TTL on THREAD_SNAPSHOT_IDLE_TTL_MS) holds the last committed thread state and replay cursor. A quick remount emits that snapshot immediately and reconnects with afterSequence, avoiding another disk/HTTP snapshot when the cache is still warm. Only fully applied state/cursor pairs are remembered; canceled in-flight events and loadingOlder are cleared on warm return.
Persistence and scope safety tighten: skip writes when the on-disk snapshot already matches; final flush uses committed state; an owner token blocks stale scopes from overwriting cache, deleting persistence, or resuming past interrupted events. Deletions stay retained across warm resume.
Docs add thread detail ownership and new verification cases; atom and sync tests cover shared streams, expiry, pagination cancel, and overlapping scopes.
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 731dcf00b7a003b6dc862b5ef3189fe6c3d96934. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).Note
Stop client thread streams when unused and add 5-min resume cache#
- Live thread-state subscriptions are now created and disposed with active mounts; thread detail atoms use an idle TTL of zero so they release when unmounted, ending the shared live stream after the last consumer leaves.
- A per-key resume cache (
createEnvironmentThreadStateAtomsresume-cache atom family, 5-min TTL) holds an in-memory snapshot with thread state, sequence, pagination cursor, and an ownership token. Warm mounts emit the retained state and resume the stream from its cursor instead of loading a fresh disk snapshot. - Cache writes and scope-finalization persistence are restricted to completed state/cursor pairs owned by the current scope via
makeEnvironmentThreadStateowner validation; unchanged snapshots skip re-saving and failed writes remain eligible for finalization retry. - Deletion, canceled event, and no-pagination fallback paths are fixed so an old scope cannot overwrite or remove newer successor cache data, a canceled stream item cannot advance the resume cursor ahead of applied data, and window reset is atomic under the apply lock.
- Risk:
THREAD_SNAPSHOT_IDLE_TTL_MSis renamed from thread-state retention to snapshot retention; any out-of-tree reference to the prior export name will break. Reviewers should check threadRetention.ts and threadDetail.ts for the TTL constants.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
Approvability#
Verdict: Not approved
Macroscope's review found this PR not approvable — The PR changes the default lifecycle of existing thread streams and adds a substantial resume-cache and persistence state machine used across all client platforms. Its behavior is well covered by focused tests, but the runtime and concurrency scope is broad enough to require human review.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).Note
🤖 GPT-6 Astra (preview) responding on behalf of Theo
The real GC check found a cache lookup defect before merge. After the old raw atom was collected, its replacement created another cache holder and reloaded an older snapshot.
Fixed in
731dcf00b. Resume atoms now have their own family. Both independent GC runs kept one holder, immediate warm data, cursor 8, and unchanged disk/HTTP load counts after collecting the old atom definitions and unmount closures. All 50 focused tests, typecheck, and lint pass. The new commit is back through CI and review.Note
🤖 GPT-6 Astra (preview) responding on behalf of Theo
The final head passes all CI and code-review checks. Approvability is neutral because the shared lifetime change needs review. I completed that review with separate server and web agent checks.
All 50 focused tests pass. They cover final-observer shutdown, shared streams, completed data/cursor pairs, pagination, deletion, old owners, failed-write retries, and unchanged-save suppression. The final GC proof collects both old atom definitions and unmount closures, then restores the same data at cursor 8 without another disk or HTTP read.
The persisted format and wire contracts do not change. Native fingerprints are unchanged. This is within Theo's merge authorization, and I am satisfied with the final behavior. No browser/device result or unanimous automatic approval is claimed.
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
088cc3f· PR result:731dcf0· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.