ai-devkit
ai-devkit copied to clipboard
perf(console): preview re-parses the whole transcript on every change; read the tail instead
Problem
The console preview (packages/cli/src/tui/console/hooks/useAgentConversation.ts) calls adapter.getConversation(path) synchronously. It does so on the main thread, every 3 s, whenever the file's mtime changes.
Every file-based getConversation implementation reads and parses the entire transcript, and the hook then keeps only the last 20 messages (PREVIEW_TAIL). An active agent changes its mtime on almost every tick, so the preview re-parses the whole file continuously.
This affects the Claude, Codex, Gemini, Copilot, Grok, Pi, Kiro and Antigravity parsers. OpenCode already queries SQLite.
Evidence
| Transcript | Cost per re-parse | Allocation per re-parse |
|---|---|---|
| 48 MB Codex file | ~100 ms CPU | ~57 MB |
| 17 MB Claude file | ~40 ms CPU | – |
| 400 MB synthetic file | ~1.1 s CPU | Peak RSS 1.59 GB |
Proposed approach
- Add an optional
tailoption togetConversation(backward-compatible):getConversation(path, { verbose, tail }). - For JSONL formats:
- Read backward from the end in chunks (e.g. 64 KiB) until
tailmessages are collected. - Then keep
{ino, offset}and parse only appended bytes on later polls.
- Read backward from the end in chunks (e.g. 64 KiB) until
- For non-JSONL formats (single-JSON files, e.g. Gemini), keep the existing mtime cache and document the limitation.
useAgentConversationpassestail: PREVIEW_TAIL.
Acceptance criteria
- [ ]
getConversationaccepts an optionaltail; existing callers without it behave as before. - [ ] With
tail = 20on a 400 MB JSONL fixture: ≤ 2 MB read and ≤ 50 ms for the first call. - [ ] Later polls after appending K bytes read ≤ K + 64 KiB.
- [ ] The last-N messages returned with
tailequal the last N of the full parse, for all existing fixtures of every JSONL-based adapter. - [ ] Truncation/rotation (inode change or size shrink) resets and returns correct results.
- [ ] Implemented for the Claude, Codex, Copilot, Grok, Pi, Kiro and Antigravity adapters; Gemini documented as mtime-cache only.
- [ ]
useAgentConversationpassestail, and preview behaviour is unchanged in existing TUI tests.
Out of scope
- Loading older history when scrolling past the tail.
- Moving parsing off the main thread.