Skip to content

fix(client-runtime): a branch switch shared by many archived threads reloads the archive once - #38336

Open
juliusmarminge wants to merge 1 commit into
mainfrom
factory/issue-38321
Open

juliusmarminge wants to merge 1 commit into
mainfrom
factory/issue-38321

Conversation

@juliusmarminge

Copy link
Copy Markdown
Member

Cause: WorktreeBranchSync dispatches one thread.metadata.update per thread sharing the worktree, archived ones included. Each update of an archived thread reaches active-shell subscribers as a removal, which replaces EnvironmentShellState.removedAt, and the archivedShellSnapshot query's refreshTrigger refreshed (and interrupted the in-flight request) on every such change. Fix: the archivedShellSnapshot refresh trigger in packages/client-runtime/src/state/orchestration.ts now waits until removedAt has been quiet for 500 ms (an Effect.sleep in a runtime atom that restarts on each change), so a burst of archived-thread changes reloads the archive once, after the last change. The trigger keeps the current value until then, so mounting causes no extra reload. User actions (unarchive, delete) still refresh explicitly through refreshArchivedThreadsForEnvironment, so they are not delayed. The change is in shared client-runtime code, so it covers web, desktop and mobile (ThreadRelationshipsPanel, Settings > Archived threads, mobile useArchivedThreadSnapshots). Measured on Linux with triage's seed driver on a fresh T3CODE_DEV_SEED=0 dev database (one active coordinator plus 300 archived threads sharing one worktree, 2,700 other archived threads), Settings > Archived threads open, then git switch -c in the shared worktree, reading the server trace for 90 s. BEFORE (main b387e3a): 1 reconcile, 301 dispatch.threadMutation, 36 ws.rpc.orchestration.getArchivedShellSnapshot requests in 27.2 s (30 Interrupted, 6 Success, median 145 ms), 132 ProjectionStore.readShellSnapshotRows (112 completed despite cancellation). AFTER: 1 reconcile, 301 dispatch.threadMutation, 1 getArchivedShellSnapshot request (Success, 162 ms), issued about 0.54 s after the last mutation, 51 readShellSnapshotRows in total. Triage counted about 130 requests with the thread details panel open; my run used the Settings page, which mounts the same atom. No new regex or parser.

Fixes #38321

Reproduction on main

Commit 13ecf5b (origin/main). Ran T3CODE_DEV_SEED=0 vp run dev on a fresh database. A scratch driver (uploaded: https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/7e5113fbb78ffe54/seed-38321.ts) used the server's HTTP API and WebSocket RPC to create a project on a new git repo, one active 'coordinator' thread plus 300 archived threads all with worktreePath = one git worktree on branch feature-a, and 2,700 archived threads without a worktree (3,000 archived total). In the web client (preview browser) I paired, opened the coordinator thread with the thread details panel (Lineage section) visible, and confirmed one getArchivedShellSnapshot load (420 ms server-side). Then I ran git switch -c another-branch in the shared worktree and read the server trace (all rotated server.trace.ndjson files) for the following ~90 s. Observed: one WorktreeBranchSync.reconcile run dispatched 301 separate thread mutations (302 orchestrationV2.dispatch spans, 301 dispatch.threadMutation); the web client sent 130 ws.rpc.orchestration.getArchivedShellSnapshot requests within 21.0 s, of which 129 ended Interrupted and 1 Success; per-request server duration median 138 ms, min 49 ms, max 575 ms at 3,000 archived threads. 101 ProjectionStore.readShellSnapshotRows reads still completed with Success during the burst while nearly every RPC was interrupted, so most cancelled reloads still cost a full read. Baseline before the switch: 1 archived snapshot request in total. At the reporter's scale (~8,000 archived, 48 GB DB, 17-25 s per read) the same one-read-per-updated-thread pattern accounts for the sustained CPU they measured.

Evidence

Being captured: before and after in the real app. This section is filled in before the pull request can merge.

Tests

vp test run src/state/runtime.test.ts in packages/client-runtime: 42/42 pass. The extended case 're-reads an open archive when a thread leaves the shell' fails on main (expected true to be false) and passes with the fix. vp run check:changed: format, lint, knip, typecheck (7 packages) and test all pass.

Not in this change

Who did the work

  • Written by Claude Opus 5.5, in Claude Code
  • Reviewed by GPT-6.1 Sol, in Codex, and by Claude Opus 5.5, each on its own
  • Proof captured by GPT-6.1 Sol and checked by Claude Opus 5.5

Run by the t3code factory in T3 Code.

…reloads the archive once

A branch switch in a worktree that many archived threads share updates each
thread with its own command, and every update reached clients as a removal
that re-requested, and interrupted, the whole archived thread list.

The archived snapshot now reloads once removals have been quiet for 500 ms,
so a burst of archived-thread changes costs one read instead of one per thread.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:S 10-29 changed lines (additions + deletions). labels Nov 17, 2026
@macroscopeapp

macroscopeapp Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 6a3abe5

Macroscope's review found this PR approvable — Archive refreshes now wait for 500 ms of quiet after removal events, avoiding repeated full-archive reads during bulk updates. The change is limited to client-side refresh timing and does not alter stored archive data.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

coderabbitai Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Archived shell snapshot refreshes now wait for a 500 ms quiet period after removal updates. Runtime tests provide a controllable clock and verify that updates within the period result in one reload.

Changes: Archived shell refresh

Layer / File(s) Summary
Debounced archived shell refresh
packages/client-runtime/src/state/orchestration.ts, packages/client-runtime/src/state/runtime.test.ts
The refresh trigger waits 500 ms after the latest removal update before exposing it. The test runtime uses the active clock, and tests verify the debounce and subsequent reload behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: bugfix

Suggested reviewers: stienswout, macroscopeapp

Merge Risk: Minimal · up to 6a3ab

Archived-thread list refreshes are now debounced, so a burst of thread updates produces one reload instead of many. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix (archived-thread reload after a branch switch in a shared worktree) and matches the main change, which debounces the archived-shell snapshot refresh so a burst of removals relo…
Description check Passed The description covers the problem (WorktreeBranchSync dispatching one update per sharing thread, each triggering an archive reload), the change (500 ms quiet period in the refresh trigger), scope (li…
Linked Issues check Passed Issue #38321 requires that a branch switch in a shared worktree stop reloading the whole archived list once per updated thread. Its diagnosis names the chain: archived-thread removals replace removedA…
Out of Scope Changes check Passed The whole-PR diff (merge base b387e3a to head 6a3abe5) changes packages/client-runtime/src/state/orchestration.ts. The change adds the AsyncResult import, the ARCHIVE_REFRESH_QUIET_MS constant, and …
Full details: Title check

Explanation

The title names the fix (archived-thread reload after a branch switch in a shared worktree) and matches the main change, which debounces the archived-shell snapshot refresh so a burst of removals reloads once. It is specific, conventional-commit formatted, and at 93 characters it is slightly long but still readable.

Full details: Description check

Explanation

The description covers the problem (WorktreeBranchSync dispatching one update per sharing thread, each triggering an archive reload), the change (500 ms quiet period in the refresh trigger), scope (linked issue #38321, explicit user actions unaffected), verification (before/after trace measurements and test results), and out-of-scope items. The 'Evidence' section is a placeholder stating it will be filled in before merge, so the template's before/after requirement for UI-visible behavior is not yet met. Minor template deviation: the 'Scope' section links an issue, which satisfies the requirement.

Resolution

Complete the 'Evidence' section with the before/after proof described as pending, or confirm that the existing trace measurements satisfy the template's verification requirement before merge.

Full details: Linked Issues check

Explanation

Issue #38321 requires that a branch switch in a shared worktree stop reloading the whole archived list once per updated thread. Its diagnosis names the chain: archived-thread removals replace removedAt, removedAt is the refreshTrigger of archivedShellSnapshot, and each change reloads and interrupts the in-flight request. The issue lists 'reload the archived list at most once per interval while removals keep arriving' as one possible fix and states that it does not require a particular implementation.

The reviewed head breaks this chain in packages/client-runtime/src/state/orchestration.ts. The refresh trigger now derives from an Atom that sleeps for ARCHIVE_REFRESH_QUIET_MS (500 ms) after each removedAt change. Each new change restarts the sleep. A burst of removals therefore produces one trigger value after the last change. While the sleep runs, the trigger returns the previous settled value through AsyncResult.getOrElse, and the initial read returns the current value through get.once. Mounting therefore does not cause an extra reload.

The issue's server-side point (#32805, reads continue after cancellation) is not a stated requirement of this issue's fix list. It is excluded from this change, as the PR description states.

The automated test requirement is supported by the PR's reported test changes in packages/client-runtime/src/state/runtime.test.ts (debounce case for removals 400 ms apart). I did not read that test file directly, so the test result rests on the reported run. The empty Evidence section in the PR description does not affect coding compliance.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff (merge base b387e3a to head 6a3abe5) changes packages/client-runtime/src/state/orchestration.ts. The change adds the AsyncResult import, the ARCHIVE_REFRESH_QUIET_MS constant, and the debounced refresh trigger for archivedShellSnapshot. These changes implement issue #38321.

The reported test change in runtime.test.ts adds the Clock service to the test runtimes and a debounce test. This supports the implemented behavior. The PR description and objectives state that the change does not batch WorktreeBranchSync.reconcile, does not change server-side read cancellation, and does not alter the explicit refreshArchivedThreadsForEnvironment path. The diff shows no unrelated changes in the inspected scope.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b387e3a and 6a3abe5.

Files selected for processing: 2

  • packages/client-runtime/src/state/orchestration.ts
  • packages/client-runtime/src/state/runtime.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

This branch has not been deployed

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

Labels

size:S 10-29 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A branch switch in a worktree shared by many archived threads makes the client reload the whole archived list once per thread

1 participant