#1841·happy

CLI SESSION_SCANNER re-reads entire session JSONL files every 3s → sustained high CPU in long-running sessions

Author: OutmanSayCreated Sep 24, 2026Updated Sep 24, 2026

Summary

In long-running sessions, the CLI's SESSION_SCANNER uses a lot of CPU even when nothing is happening. The cause is that readSessionEntries() re-reads and re-parses every watched session JSONL from the beginning on every sync, and a sync fires every 3 s (setInterval(..., 3e3)) plus on every file-watcher event.

Observed (macOS, happy-coder 1.1.9; the same code is still in happy 1.2.5)

  • One happy session had been running for ~1 day 19 h. It was watching 4 transcripts of 10.1 MB, 4.1 MB, 2.3 MB and 0.9 MB, so every sync re-read about 17 MB.
  • That session's node process averaged ~20% CPU and peaked at ~41%, while the session itself was idle.
  • Nearly every scan found nothing new. The log is full of lines like found=635, skipped=635, sent=0. The debug log grew to 105 MB (2.47 M lines) in that time.
  • Once a session has been watched, it stays in watchers for good. The cost therefore grows with both uptime and transcript size, and the machine felt noticeably sluggish.

Root cause

packages/happy-cli/src/claude/utils/sessionScanner.ts:

typescript
file = await readFile(expectedSessionFile, 'utf-8');   // whole file, every time
let lines = file.split('\n');                          // JSON.parse + zod safeParse per line

All three callers (initial marking, the sync loop, and treatExistingAsProcessed) only use the result to add keys to processedEntryKeys or to emit entries that aren't in it yet. That means they only need the lines appended since the last read.

Proposed fix (running locally, works)

Read incrementally. Keep a per-file (inode, byteOffset) in a map owned by each scanner instance (a single process creates more than one scanner, so a shared map would let one scanner consume lines the other never sees), and only read the complete lines appended since the last call. Start from 0 if the file was truncated or replaced:

javascript
// offs: Map created once in createSessionScanner() and passed to readSessionEntries()
const fh = await fsp.open(expectedSessionFile, 'r');
try {
  const st = await fh.stat();
  const prev = offs.get(expectedSessionFile);
  const start = prev && prev.ino === st.ino && st.size >= prev.off ? prev.off : 0;
  const len = st.size - start;
  if (len <= 0) return '';
  const buf = Buffer.alloc(len);
  await fh.read(buf, 0, len, start);
  const consumed = buf.lastIndexOf(10) + 1;        // stop at last '\n'; a partial line waits for the next read
  offs.set(expectedSessionFile, { ino: st.ino, off: start + consumed });
  return buf.subarray(0, consumed).toString('utf-8');
} finally {
  await fh.close();
}

Splitting on the \n byte is safe for UTF-8, because 0x0A never occurs inside a multi-byte sequence. Deduplication through processedEntryKeys is unchanged, so behaviour is the same. The cost per sync drops from "size of all watched transcripts" to "bytes appended since the last sync".

Verified locally on 1.2.5 with the patch applied: each new message is emitted exactly as before, scans only pick up the newly appended entries (found=1/found=2), and an idle session's CPU drops from double digits to ~0%.

It could also help to drop watchers for sessions that have finished, but the incremental read alone removes almost all of the CPU cost.