opencode icon indicating copy to clipboard operation
opencode copied to clipboard

tui: transcript rebuilds every row on writes that change nothing, so keys stall with many subagents

Open cevr opened this issue 3 weeks ago • 3 comments

Description

createSessionRows (packages/tui/src/routes/session/rows.ts) re-reduces the whole transcript whenever its on(...) deps run, and the deps run on every replacement of the session's info, its pending list or its message list, even when the value they read is the same (a refresh of a running session, an answer streaming in). Each rebuild then re-keys and reconciles every row, even when the only change is a new row at the end.

With 30 background subagents in a session, counting one run gave 203 full rebuilds: 49 from the revert boundary, 38 from the queued inputs, 116 from the message list. Typing in the composer stalled while the subagents finished: key-to-frame p90 49–344 ms, max up to 1.9 s. A rebuild of an 800-message (2,000-row) transcript costs 6.7–7.4 ms, so with large families it adds up. #56170 asked about this without numbers and was closed.

Fix on my fork, two commits:

  • each trigger is a memo that only notifies when its value changes (the boundary as a string, the queued and message triggers as key lists): https://github.com/cevr/opencode/commit/314c9607bf6ca4254f1a7b478a8d5deb15c3b910 (203 → 49 rebuilds in the same run; composer p90 126–146 ms)
  • a rebuild whose rows only grew at the end, or did not change, appends instead of re-keying and reconciling: https://github.com/cevr/opencode/commit/abe903b2824cf1899c02f0839c88b2d2e8093b84 (2,000 rows: 6.7–7.4 → 2.8–3.3 ms per rebuild)

Both have regression tests in test/cli/tui/data.test.tsx. Happy to send a PR.

Plugins

None needed

OpenCode version

2.2.5, still on dev @ 609ee05a23

Steps to reproduce

  1. Start a session that launches ~30 background subagents (any simple task each).
  2. Type in the composer while they work and finish.
  3. Keys lag and stall as each subagent's result lands; a CPU profile puts rebuild in rows.ts on top.

Operating System

macOS and Linux

Terminal

Ghostty

cevr avatar Nov 11 '26 18:11 cevr

Not observed; this looks likely from reading the code.

Thanks for the detailed report and the numbers. I could not run your many-subagent scenario here, so this is confirmed from reading the code only. I have not measured the stalls myself.

The code on the latest dev (609ee05a23), the same commit you tested, matches what you describe in packages/tui/src/routes/session/rows.ts:

  • Revert boundary. This trigger reads the session info's revert?.messageID. A session refresh in packages/client/src/solid/data.ts replaces the whole info object, so rebuild() runs even when the boundary hasn't changed.
  • Queued and message triggers. These are on(() => list.flatMap(...), rebuild). They build a new array on every run, and on() doesn't compare values, so any write to the pending or message list rebuilds. That includes a message appended at the end.
  • Cost. rebuild() re-keys every row and reconciles the whole reduced list each time.

I've passed this to the maintainers along with your fork commits.

opencodejr[bot] avatar Nov 11 '26 18:11 opencodejr[bot]

I'm working on this. I have a fix that stops the redundant transcript rebuilds in rows.ts and a PR coming soon.

thefrikidude avatar Nov 19 '26 17:11 thefrikidude

I reproduced a related typing stall on v2.4.2 and found a second path through createSessionRows worth checking alongside the rebuild() fix.

With 3,650 messages loaded in the TUI, I streamed session.text.delta events about 18 times per second into an existing text part in the active session. I kept themessage ID and ordinal fixed and alternated two equal-length payloads.

Delta payload Time for typed input to appear
" x" 10 of 12 probes reached the 2-second timeout
" " (two spaces) All 12 appeared within 70–145 ms

The whitespace difference matters because the handler checks delta.trim(). For " x",it calls appendPart, which enters setRows(produce(...)) before hasPart checkswhether the row exists. Even when the row is already there, the array setter recursivelyunwraps the transcript rows. That unwrap appeared in 163 of 273 main-thread CPUsamples during a reproduced stall. Delta handler · Row update

Could you include repeated deltas to an already-visible text part in the regression test for the proposed fix? The first non-whitespace chunk should create the row; subsequent chunks should update its text without walking the transcript again.

rahulrajaram avatar Nov 28 '26 19:11 rahulrajaram