[bug] TUI Freezes While Highlighting Long Shell Commands
When an agent runs a long single-line shell command, the TUI can freeze for minutes on a stale "Thinking…" frame: nothing renders, input is ignored, and no transcript events are journaled. The agent keeps working in the background, and everything it did appears at once when the UI recovers. Seen on 0.8.3 (syntect 5.3.0 with regex-onig).
Sampling a frozen session shows the main thread at ~100% CPU, highlighting the command inside the render path:
EventLoop::present → Transcript::render → LayoutCache::layout
→ tool::shell::command_spans → highlight::line
→ syntect HighlightLines::highlight_line → onig match_at
The commands that triggered it were 15-21 KB one-liners that write files from base64:
echo '<base64>' | base64 -d > a && echo '<base64>=' | base64 -d > b
The Bash grammar on Oniguruma is cubic in this shape (the trailing = padding is what sets it off). Timing one highlight_line call in a release build, with two blobs of n bytes each:
n |
regex-onig |
regex-fancy |
|---|---|---|
| 500 | 0.23 s | 1 ms |
| 1000 | 2.1 s | 1 ms |
| 2000 | 16 s | 4 ms |
| 8000 | > 30 s | 29 ms |
One real 15 KB command froze a session for 13.5 minutes. The layout cache is keyed on the entry's revision, width, and expanded state, so the same command is highlighted again when its result arrives, on resize, and on expand. That session froze a second time as soon as the result came in.
Transcript records are appended from the same loop, so they queue behind the freeze and are all stamped with the recovery time. Quitting during the freeze would likely drop them.
Proposal
- Build syntect with
regex-fancyinstead ofregex-onig. - Highlight only a bounded prefix of each line in
highlight::line(4096 bytes, say) and render the rest as plain code, so no grammar or regex engine can stall a frame on one long line. This covers the Markdown and diff callers too.
Open questions
- Should highlighting, or at least transcript persistence, move off the event loop so a slow render cannot stall input and journaling?