Skip to content

perf(bots): folded bot history re-reads only changed workspaces, and its clock runs only while a fold is ahead (STA-18283, STA-18288) - #49352

Merged
AmethystLiang merged 3 commits into
mainfrom
AmethystLiang/sta-18283-18288-older-chats-perf
Nov 26, 2026
Merged

AmethystLiang merged 3 commits into
mainfrom
AmethystLiang/sta-18283-18288-older-chats-perf

Conversation

@AmethystLiang

@AmethystLiang AmethystLiang commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

A bot in the sidebar folds old, finished chats under "Show N older". To decide what may fold, it checks whether any of those chats' workspaces is busy. Before this PR it re-checked every one of those workspaces whenever anything in the app changed, and it woke a five-minute timer even when nothing could ever fold. Now it re-checks only the workspaces that actually changed, and the timer runs only while something is still waiting to fold.

Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​254 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​254
Prod 6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​210 $\color{#cf222e}{\Huge{\mathbf{−}}}$​36 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​174

What Changed

Follow-ups to #48722. One commit per ticket.

  • STA-18283, folded history scanned every watched workspace on unrelated store writes. useBotSidebarOlderChats ran a zustand selector that, on every store write, called three status selectors for every watched workspace (each workspace under a due chat, plus unclaimed ones). Now:
    • The agent-status projection publishes, per commit, a bounded journal of the workspaces whose rows, arrangement or summary changed (worktreeChanges, using the existing KeyedChangeJournal; a full resync publishes "unknown").
    • A new createWatchedAttentionReader (bot-sidebar-attention.ts) keeps each list's answer. On a write it re-reads only the watched workspaces named by that journal, or by keyed diffs of the tab and workspace records the status selectors read (via the existing forEachChangedKey). A change to an input that is read for every workspace (chat sleep answers, WSL host state, the repo and folder catalog) re-reads them all.
    • The reader persists per list. A redraw with the same watched list reads nothing, and a new list reads only the ids it adds.
  • STA-18288, the older-chat clock woke with no finite fold deadline. The hook now passes enabled to useNowSnapshot only while some fold time is still ahead. A Home-only list, or one whose fold times have all passed, holds no timer. A new fold time ahead turns it back on during the same render.

Why

Both are structural costs found in source review: per-write work proportional to every watched workspace, and a timer that could never change the result. The reader reuses existing journals and record diffs instead of adding a parallel invalidation scheme. The sidebar's slot journal was considered and rejected: it finds a pane's previous workspace only through cards the sidebar has drawn, so it can miss folded rows. No comparable: mechanical.

Linked Issue

Visual Proof

N/A. No visual change: which chats fold, and when, is unchanged. Only how often the inputs are re-read changes.

Testing

  • bot-sidebar-attention.performance.test.ts (perf contract): an agent status update outside a 300-workspace watched list reads 0 workspaces, the changed workspace reads 1, and a redraw with the same list reads 0. It fails on the old full scan (300 reads).

  • use-bot-sidebar-older-chats.performance.test.ts (perf contract, fake timers): a Home-only list and an all-passed list hold 0 timers, and a list with a fold time ahead holds 1. It fails on the old code (1 timer with no deadline).

  • bot-sidebar-attention.test.ts: the reader's answer equals a full read after each kind of write: an agent working, done or orphaned (tab not listed), pane titles, ptys, a tab moving between workspaces, the watched list changing, browser tabs, a structured chat tab and its sleep answer. Removing the agent journal, the browser or unified tab diff, or the sleep input from the reader each fails it.

  • Locally: these three files, web typecheck, oxfmt and oxlint on the changed files. CI runs the rest of the suite.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Every added test fails without this change and avoids the test anti-patterns

Performance

Reasoned from the code, not measured.

  • Per store write, while a list watches workspaces: about 12 reference comparisons when nothing relevant changed, instead of three selector reads per watched workspace (750 or 7,500 at the ticket's scales).
  • A relevant write: reads only the touched watched workspaces. Keyed diffs cost about the number of changed keys when the record journal addresses them; otherwise they walk that record's keys comparing references, with no status reads.
  • Input change that is read for every workspace (sleep answers, WSL host state, the catalog; rare): one full re-read, as before.
  • Agent-status commit: one set add per touched workspace, plus one set copy when any was touched. The journal is bounded to 16 batches of at most 4,096 ids.
  • Memory: each reader retains one prior snapshot's record references.
  • Clock: 0 timer callbacks while no fold time is ahead (was 1 per 300 s). It is unchanged while one is ahead.

Risk

Level: Medium. The riskiest part is the reader missing a change, which would leave a chat folded or unfolded until the next relevant write. The invariant test covers every input class except a stopped WSL host, which takes the full re-read path. The projection change is additive: one set and one journal, with no change to rows, summaries or notices. A plain revert backs it out.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance. Renderer-only and platform-neutral. Remote rows reach the same projection, so they are covered.

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The projection now publishes worktree-level change information. A stateful reader uses that information to update attention results for watched workspaces, and the older-chats hook uses the reader. Added tests check reader consistency, workspace status reads, and timer counts.

Merge Risk: Minimal · up to c3242

This change reduces redundant workspace reads and timer use in folded bot history without changing which chats fold. Checked inputs for attention are all covered by invalidation, so no merge-blocking risk is identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the two changes in the PR: folded bot history re-reads only changed workspaces (STA-18283), and its clock runs only while a fold is ahead (STA-18288). The changeset matches both parts.…
Description check Passed The description follows the template closely. It has an ELI5 section, a What Changed section with the mechanism for both tickets, a Why section, linked issues, Visual Proof marked N/A with a reason, T…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two changes in the PR: folded bot history re-reads only changed workspaces (STA-18283), and its clock runs only while a fold is ahead (STA-18288). The changeset matches both parts. The title is long (133 characters, 20 words) and uses ticket IDs, but it is specific and accurate.

Full details: Description check

Explanation

The description follows the template closely. It has an ELI5 section, a What Changed section with the mechanism for both tickets, a Why section, linked issues, Visual Proof marked N/A with a reason, Testing with the three test files and checklist items, a Performance section with measured-by-reasoning notes, and a Risk section with level and revert path. Gaps: the Linked Issue section lists tickets but uses no 'Fixes #' line, which is acceptable for a follow-up. The Review, Agent skill upstream boundary, and AI Disclosure sections are present or partly blank. The Risk section does not use the template's 'Level' placeholder value beyond 'Medium', which is filled. The description states that the performance numbers were reasoned from code rather than measured, which is disclosed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e3b5510d-17c9-44ef-a498-fd77e288054e
Commits

Reviewing files that changed from the base of the PR and between 41026e2 and c324229.

Files selected for processing: 3

  • src/renderer/src/components/bots/bot-sidebar-attention.test.ts
  • src/renderer/src/components/bots/bot-sidebar-attention.ts
  • src/renderer/src/components/bots/use-bot-sidebar-older-chats.performance.test.ts

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick comments: 1

src/renderer/src/components/bots/bot-sidebar-attention.performance.test.ts: 1

40-40: Trivial · Maintainability & Code Quality · Quick win

Use fake clocks for this performance contract.

The setup calls makeTab and setAgentStatus with the real clock. Add fake-clock setup with a fixed time and restore real timers after each test. This keeps freshness-dependent status work independent of elapsed test time.

As per coding guidelines, a perf contract “runs in seconds, with fake clocks.”

Source: Coding guidelines

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 49f42fe5-a242-4f0a-a02a-d062ea7a22af
Commits

Reviewing files that changed from the base of the PR and between 80dbfa7 and 41026e2.

Files selected for processing: 9

  • src/renderer/src/components/bots/bot-sidebar-attention.performance.test.ts
  • src/renderer/src/components/bots/bot-sidebar-attention.test.ts
  • src/renderer/src/components/bots/bot-sidebar-attention.ts
  • src/renderer/src/components/bots/use-bot-sidebar-older-chats.performance.test.ts
  • src/renderer/src/components/bots/use-bot-sidebar-older-chats.ts
  • src/renderer/src/store/slices/agent-status/projection/agent-status-projection-publish-arrangement.ts
  • src/renderer/src/store/slices/agent-status/projection/agent-status-projection-publish-rows.ts
  • src/renderer/src/store/slices/agent-status/projection/agent-status-projection-state.ts
  • src/renderer/src/store/slices/agent-status/projection/agent-status-projection.ts

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

…rite changed (STA-18283)

The older-chats fold watched every workspace under a due chat and re-ran three status selectors for
all of them on every store write. The agent-status projection now publishes which workspaces each
commit touched, and the list's attention reader combines that with keyed tab and workspace record
diffs, so an update elsewhere, or a redraw of the same list, reads none of them. Follow-up to #48722.
… (STA-18288)

A bot list subscribed to the shared five-minute clock even with no finite fold time ahead (a
Home-only list, or every fold time already passed), so no tick could change what folds. It now
subscribes only while some fold time lies in the future. Follow-up to #48722.
…-field name lists

Replaces type assertions with typed builders and the shared tab fixtures, reads the attention inputs
through explicit property reads instead of field-name lists, and reads folder workspaces through
selectFolderWorkspaces, so the code-quality, layout-writer and workspace-record ratchets pass.
@AmethystLiang
AmethystLiang force-pushed the AmethystLiang/sta-18283-18288-older-chats-perf branch from 41026e2 to c324229 Compare November 26, 2026 00:35
@AmethystLiang
AmethystLiang merged commit d42503f into main Nov 26, 2026
58 checks passed
AmethystLiang added a commit that referenced this pull request Nov 26, 2026
…A-18521)

setBrowserPageUrl and updateBrowserPageState rebuilt browserTabsByWorktree with a
plain spread, so the record carried no change journal. The bot sidebar attention
reader from #49352 then fell back to comparing every browser workspace key, once per
armed bot list, on every navigation, load, title or favicon update.

Each of those writes now records the one bucket it replaced with linkRecordWrite.
The spread itself is unchanged, so surviving keys keep their order and the layout
writer ratchet still reads it as a content update. Journal readers touch only that
workspace; readers that compare identities see the same records as before.
AmethystLiang added a commit that referenced this pull request Nov 26, 2026
…(STA-18523)

When a write touched a tab-keyed record (pane titles, PTY ids, layouts), the bot
sidebar attention reader from #49352 rebuilt a tab-to-workspace map from every tab
bucket. A combined pane and tab title update replaces tabsByWorktree too, so the
cached map missed on each such write and the rebuild read every tab in the fleet.

The reader now asks the shared terminal tab owner index for each changed tab. That
index follows tab-bucket writes by the buckets they name (the title writer already
adopts its result), so the lookup costs nothing per other bucket. Ownership rules
are unchanged: the last bucket holding a tab owns it.
AmethystLiang added a commit that referenced this pull request Nov 26, 2026
… their own workspace (STA-18521, STA-18523) (#49495)

* perf(browser): navigation names the browser workspace it replaced (STA-18521)

setBrowserPageUrl and updateBrowserPageState rebuilt browserTabsByWorktree with a
plain spread, so the record carried no change journal. The bot sidebar attention
reader from #49352 then fell back to comparing every browser workspace key, once per
armed bot list, on every navigation, load, title or favicon update.

Each of those writes now records the one bucket it replaced with linkRecordWrite.
The spread itself is unchanged, so surviving keys keep their order and the layout
writer ratchet still reads it as a content update. Journal readers touch only that
workspace; readers that compare identities see the same records as before.

* perf(bots): a terminal title write looks up only its own tab's owner (STA-18523)

When a write touched a tab-keyed record (pane titles, PTY ids, layouts), the bot
sidebar attention reader from #49352 rebuilt a tab-to-workspace map from every tab
bucket. A combined pane and tab title update replaces tabsByWorktree too, so the
cached map missed on each such write and the rebuild read every tab in the fleet.

The reader now asks the shared terminal tab owner index for each changed tab. That
index follows tab-bucket writes by the buckets they name (the title writer already
adopts its result), so the lookup costs nothing per other bucket. Ownership rules
are unchanged: the last bucket holding a tab owns it.

* test(bots): count bucket reads with accessors instead of Reflect.get (STA-18521, STA-18523)

The perf contracts counted reads through a Proxy whose get trap called Reflect.get,
which the low-evidence lint rejects. They now count through a copy whose own entries
are getters. Production code is unchanged.
registered2nd pushed a commit to registered2nd/orca that referenced this pull request Nov 26, 2026
…its clock runs only while a fold is ahead (STA-18283, STA-18288) (stablyai#49352)

* perf(bots): folded bot history re-reads only the workspaces a store write changed (STA-18283)

The older-chats fold watched every workspace under a due chat and re-ran three status selectors for
all of them on every store write. The agent-status projection now publishes which workspaces each
commit touched, and the list's attention reader combines that with keyed tab and workspace record
diffs, so an update elsewhere, or a redraw of the same list, reads none of them. Follow-up to stablyai#48722.

* perf(bots): the older-chat clock runs only while a fold time is ahead (STA-18288)

A bot list subscribed to the shared five-minute clock even with no finite fold time ahead (a
Home-only list, or every fold time already passed), so no tick could change what folds. It now
subscribes only while some fold time lies in the future. Follow-up to stablyai#48722.

* fix(bots): type the attention reader's inputs without casts or layout-field name lists

Replaces type assertions with typed builders and the shared tab fixtures, reads the attention inputs
through explicit property reads instead of field-name lists, and reads folder workspaces through
selectFolderWorkspaces, so the code-quality, layout-writer and workspace-record ratchets pass.

(cherry picked from commit d42503f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant