Skip to content

Prevent AI session listing from exhausting Electron memory - #4592

Draft
shaunandrews wants to merge 3 commits into
trunkfrom
fix-ai-session-list-memory
Draft

Prevent AI session listing from exhausting Electron memory#4592
shaunandrews wants to merge 3 commits into
trunkfrom
fix-ai-session-list-memory

Conversation

@shaunandrews

@shaunandrews shaunandrews commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Related issues

  • None. Standalone reliability fix for AI task history.

Problem

Refreshing AI task history loads every transcript in the archive into the Electron main process at once. With a large history (big tool results, image payloads), this exhausts the heap and kills Studio right after an AI task completes. The original report: a 651 MB archive crashed Electron near 1.2 GB RSS.

How this fixes it

  • Listings stream each transcript line by line and keep only a small summary per session, instead of holding whole files in memory. A few files are read at a time, never the entire archive.
  • Summaries are cached by file size + mtime, so refreshes only re-read transcripts that actually changed.
  • Concurrent requests for the task list share one scan instead of each re-reading the archive.
  • Corrupted files (giant single-line entries, odd line endings, malformed JSON) are skipped or handled safely — they can't crash the process or get rewritten incorrectly.

Opening a task still loads its full conversation, unchanged.

How AI was used in this PR

AI traced the heap exhaustion to the session-listing path, implemented the streaming reader and cache, reviewed the result, and added test coverage.

Testing Instructions

  1. Populate the AI sessions directory with a large archive, including transcripts with large tool-result or image-payload entries.
  2. Launch Studio, run an AI task to completion, and confirm Studio stays up and task history loads.
  3. Refresh history again and confirm it's fast (unchanged transcripts aren't re-read).
  4. Open an individual task and confirm its full conversation loads.

Verified locally: npm run typecheck clean, npx vitest run packages/common — 690 tests passing. The originally reported 651 MB archive now lists in ~1s at ~295 MB peak RSS (previously crashed near 1.2 GB).

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

shaunandrews and others added 2 commits August 18, 2026 14:03
Fixes from review of the streamed-summary work:

- New shared jsonl.ts line reader used by listing, loading, and migration:
  one splitting rule (LF/CRLF/CR) everywhere, streamed without holding the
  file in memory, and a max-line-length guard so a corrupted newline-free
  file is skipped instead of crashing the main process with an uncatchable
  RangeError past V8's string cap.
- Migration decides everything from one content snapshot and re-detects the
  format after the probe, so two concurrent migrations can no longer parse
  pi output as legacy and rewrite a transcript down to a lone header; tmp
  files are uniquely named and cleaned up on failure. CR-delimited legacy
  files now migrate instead of losing every event.
- Summary cache stores the pre-read stat, so a write landing mid-read costs
  one re-read instead of serving a stale summary until the file changes
  again. Deletes evict their cache entry immediately.
- In-flight listing dedup is generation-gated: a caller that writes and then
  lists never joins a scan that started before its write (read-your-writes,
  as on trunk).
- Cold listings scan through a small bounded pool instead of fully
  serialized, and drop the redundant re-stat and separate migration probe
  (one stat + one open per file in the common case).
- Entries that parse to null/primitives no longer inflate eventCount or
  produce ghost summaries; firstPrompt is truncated at summary build so the
  process-lifetime cache stays bounded.
- PI_SESSION_VERSION / PI_SESSION_CWD live in migration.ts only.
- Tests for the new line reader, cache refresh/eviction, CR handling,
  concurrent migration, and the summary fields the fixture never exercised
  (endReason, site selection, environment).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant