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

perf(agent-manager): Pi discovery parses every historical session file on each refresh

Open codeaholicguy opened this issue 2 months ago • 0 comments

Problem

PiSessionLocator.discoverLiveSessions (packages/agent-manager/src/providers/pi/PiSessionLocator.ts) runs for Pi processes not matched through the registry cache or the session tracker.

  • It iterates over every historical Pi session file (discoverHistoricalSessionFiles(), the whole sessions tree) and calls parser.readSession() on each one, which reads and parses the full file.
  • It repeats this on every refresh while such a process exists, so the cost grows with total Pi history, not with live agents.
  • Matched sessions are then fully re-read again by PiSessionParser.readSession in mapSessionFileMatches.

Proposed approach

  • Limit candidates to files whose birthtime or mtime is within a window around each process start time (as Codex does), and/or to the project directory derived from the process cwd.
  • Extract sessionId and project path from a bounded head read, not a full readSession.
  • Cache "no session" per (pid, startTime), re-checked only when the sessions tree changes (directory mtime).
  • Summaries for matched sessions go through the incremental summary utility (#256).

Acceptance criteria

  • [ ] With 1,000 historical Pi session fixtures and 1 unmatched live process, discovery reads only files inside the time/project window (the test asserts which files are opened).
  • [ ] No full-file readSession during discovery; head reads are ≤ 64 KiB per file.
  • [ ] After the first refresh, with no new files, a refresh reads 0 bytes of Pi session content.
  • [ ] Matched sessions use the #256 utility, so a second read of an unchanged file reads 0 bytes.
  • [ ] Existing Pi detection and matching tests pass with identical results.

codeaholicguy avatar Sep 28 '26 08:09 codeaholicguy