ai-devkit icon indicating copy to clipboard operation
ai-devkit copied to clipboard

perf(console): preview re-parses the whole transcript on every change; read the tail instead

Open codeaholicguy opened this issue 2 months ago • 0 comments

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 tail option to getConversation (backward-compatible): getConversation(path, { verbose, tail }).
  • For JSONL formats:
    • Read backward from the end in chunks (e.g. 64 KiB) until tail messages are collected.
    • Then keep {ino, offset} and parse only appended bytes on later polls.
  • For non-JSONL formats (single-JSON files, e.g. Gemini), keep the existing mtime cache and document the limitation.
  • useAgentConversation passes tail: PREVIEW_TAIL.

Acceptance criteria

  • [ ] getConversation accepts an optional tail; existing callers without it behave as before.
  • [ ] With tail = 20 on 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 tail equal 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.
  • [ ] useAgentConversation passes tail, 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.

codeaholicguy avatar Sep 28 '26 08:09 codeaholicguy