ai-devkit
ai-devkit copied to clipboard
perf(agent-manager): Pi discovery parses every historical session file on each refresh
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 callsparser.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.readSessioninmapSessionFileMatches.
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
sessionIdand project path from a bounded head read, not a fullreadSession. - 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
readSessionduring 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.