Skip to content

[Performance] Avoid full descendant re-fetches on every token sidebar refresh #9

Description

@juancarlosrodicio

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

  1. Request counts do not scale as a complete tree walk on every unchanged refresh.
  2. A deep tree remains responsive and does not create overlapping traversals.
  3. New/updated child sessions invalidate only the necessary cached data.
  4. Tests or a deterministic mock verify request counts, cancellation, and partial/error behavior.

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions