Problem
The default token sidebar plugin recomputes the complete descendant tree repeatedly:
- `opencode/plugins/token-tree-usage.tsx:61-103` recursively calls `session.children` and then `session.messages` sequentially for every descendant.
- `opencode/plugins/token-tree-usage.tsx:126-163` refreshes on several events and also every five seconds.
- There is no cache keyed by session/update time, no aggregate token endpoint, concurrency cap, or cancellation of an in-flight refresh before starting another one.
Impact
For a tree with N subagent sessions, each refresh performs O(N) child/message API calls and downloads every child message history again. Deep or active trees can make the TUI visibly slow, load the OpenCode server, and create overlapping refreshes when one traversal takes longer than five seconds.
Suggested direction
- Use a server-side aggregate or token-summary endpoint if available.
- Cache per-session token totals and child lists using the session update timestamp.
- Coalesce event/timer refreshes and ensure only one traversal is active; cancel or discard obsolete work.
- Add a bounded concurrency strategy if requests still need to be made individually.
- Refresh only affected branches where event metadata permits it.
Acceptance criteria
- Request counts do not scale as a complete tree walk on every unchanged refresh.
- A deep tree remains responsive and does not create overlapping traversals.
- New/updated child sessions invalidate only the necessary cached data.
- Tests or a deterministic mock verify request counts, cancellation, and partial/error behavior.
Problem
The default token sidebar plugin recomputes the complete descendant tree repeatedly:
Impact
For a tree with N subagent sessions, each refresh performs O(N) child/message API calls and downloads every child message history again. Deep or active trees can make the TUI visibly slow, load the OpenCode server, and create overlapping refreshes when one traversal takes longer than five seconds.
Suggested direction
Acceptance criteria