Skip to content

Keep agent usage refreshes off pi/omp history they already read - #18938

Open
OldJobobo wants to merge 3 commits into
omacom:quattrofrom
OldJobobo:pi-session-index
Open

OldJobobo wants to merge 3 commits into
omacom:quattrofrom
OldJobobo:pi-session-index

Conversation

@OldJobobo

@OldJobobo OldJobobo commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What changes

Every refresh of the agents panel reads the whole pi/omp session history again, once per collector. The scan cache lasts 20 seconds and the panel refreshes every 15 minutes, so nothing is ever reused. On a machine with 9.6 GB of pi sessions, one refresh ran 17 rg processes with 221 threads between them and took the one-minute load from 2.8 to 92.6. No session had changed since the day before. This is #12411 and #17982.

scan_pi_sessions() now keeps a per-file index, the way the Claude Code scan already does:

  • A session file whose size, mtime and inode are unchanged is not opened.
  • A file that grew is read from where the last read stopped, after the same first-and-last-4 KB check the Claude Code index uses.
  • Anything else is read from the start. A deleted file leaves the index, and a moved one keeps its entry.
  • The index stores parsed lines, not totals. Fork deduplication and prompt ownership are still worked out over the whole tree on every scan.
  • A file that couldn't be read in full counts for that scan but isn't stored, and the scan reports itself incomplete, so the scan cache doesn't keep it either.

This is the pi/omp half of #15906 by @Iann29, redone for the shared helper the scan moved to in #14040. It was written against the in-tree Claude Code index, not from that branch. The opencode scan is not covered here.

Two choices a reviewer may want to question:

  • One index per pattern and set of roots, not one shared index. Which lines and files a scan reads depends on its pattern, so a changed file needs a pass per pattern either way. On the history measured below, every reply belongs to one provider: the Codex index is 58 MB and each of the other 16 is about 1.3 MB. A shared index would have every collector load the 58 MB on every refresh and wait on one lock.
  • accept now gets only a reply's provider, model and api, not the whole message without its content. That is all any of the 18 callers reads (Codex and local read api).

Not fixed here: the first refresh after this lands still reads everything, to build the indexes, and is as heavy as a refresh is today (the "builds the index" column below). So is any refresh after the cache is cleared. The last commit softens that: see below.

At most three collectors at once

omarchy-agent-usage-update started every collector together, which is where the 17 rg processes at once come from. It now runs at most three at a time (MAX_COLLECTORS=3), starting the next as one finishes with wait -n, and still exits 1 if any collector failed. This mostly matters for the refresh that builds the indexes, and for collectors this index doesn't cover. The tables below were measured before this commit, so they don't show its effect; agent-usage-update-test.sh and the other agent tests pass with it.

Verification

Measured on an AMD Ryzen 5 5600G (6 cores, 12 threads, 31 GB RAM), NVMe SSD, ripgrep 15.2.0, Python 3.14.10, with 3,216 pi session files (9.6 GB), none changed during the runs. Each column is one full omarchy-agent-usage-update run in an isolated home with the real sessions read in place, at nice -n 19 and idle I/O priority so the desktop stayed usable. Nice doesn't change the CPU time or the process and thread counts; it can stretch wall time.

quattro This branch, builds the index This branch, index built
Wall time 77.4 s 71.8 s 6.1 s
CPU time, whole process tree 489.7 s 502.3 s 13.0 s
One-minute load, before → peak 2.8 → 92.6 2.7 → 77.8 2.5 → 6.0
rg processes at once 17 17 2
rg threads at once 221 221 28
Bytes read by the tree (rchar) 310 GiB 307 GiB 210 MiB
Peak memory, summed over the tree 2,411 MB 1,959 MB 671 MB
Largest single process 346 MB 597 MB 474 MB
  • The 23 usage records were identical between quattro and this branch, with the index being built and with it built. The only field left out of the comparison is updatedAt.
  • With the index built, no session file was opened. The remaining rg runs list file names.
  • The run with the index built used about two cores for six seconds, so most of its load peak of 6.0 came from other work on the machine. An earlier run of the same kind on a quieter machine went from 2.46 to 2.50.
  • quattro was measured once. It has no index, so a second run repeats the first, and I didn't want to put a load of 92 on that desktop twice.
  • The index-building run was taken before the last round of fixes to the failed-read paths, which the real data never reaches. The other two columns are the final code.

Tests:

  • agent-usage-pi-scan-test.sh grows from 10 to 29 checks. It puts an rg on PATH that logs what it is asked to read, and covers: an unchanged history, an append, an unfinished last line, truncation, same-size and larger rewrites, delete and move, a fork tree where the parent, a fork and an orphan change, a corrupt index, a failed read, an unreadable file, and an unfinished listing. On quattro it fails at "an unchanged history opens no session file".
  • All 44 agent-usage-* test files pass: 1,579 checks.
  • Outside the suite, I compared this branch with quattro's scan_pi_sessions() on 190 generated session trees (forks, omp model switches, failed replies, summaries, lines without ids or times), with appends, truncations, deletes, moves and touches between scans. All 4,750 comparisons over five patterns matched.

What it costs:

  • Memory. The Codex collector peaks at 597 MB while it builds its index and 474 MB afterwards, against 346 MB today. The total over all collectors goes down.
  • Disk. The indexes take 78 MB in 17 files under the cache directory, on this history.
  • An index file is left behind when a collector's pattern or roots change, as with the Claude Code index.
  • A session file that becomes unreadable without its size or mtime changing still counts from the index, and the scan reports itself complete. quattro would drop its rows and report incomplete.

What I couldn't check:

  • Reads from a cold disk. Everything came from the page cache.
  • omp sessions and pi profiles on real data. This machine only has pi's default tree; the tests cover the others with made-up sessions.
  • More than one machine.

Screenshots

No visible change: the panel shows the same numbers. Only how the collectors read pi/omp session history changes.

🤖 Generated with Claude Code

A refresh with nothing new reads no session file, and one after a little
activity reads only what was appended.

Co-Authored-By: Claude <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes affect agent usage refreshes across many collectors, but stay within that workflow.

The PR should not merge until successful refreshes reliably return success.

Findings

  1. P1 Successful refresh reports failure ▶

Summary

The PR keeps a per-file index of pi/omp history and limits refreshes to three collectors at once.

  • Pi and omp scans reuse saved session details between refreshes.
  • Agent usage refreshes run no more than three collectors at once.

Reviews (4) · Last reviewed commit: "Run at most three usage collectors at on..." · Reviewed by Greptile

Comment thread shell/plugins/agents/collectors.py Resolved
Comment thread shell/plugins/agents/collectors.py Resolved
Comment thread shell/plugins/agents/collectors.py Resolved
OldJobobo and others added 2 commits November 14, 2026 11:56
…from ending a scan

A fork tree's other files keep only their prompt ids unless they hold a copy of a booked prompt. A cost no float holds skips its row as it did before the index, and no temp file for rg's messages costs a failed run its batch, not the scan.

Co-Authored-By: Claude <noreply@anthropic.com>
Every collector started together is a burst across all cores that the
desktop feels on each refresh. Three at a time take a little longer and
stay out of the way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment on lines +85 to +87
while (( running > 0 )); do
wait -n || status=1
running=$((running - 1))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Successful refresh reports failure

If collectors finish before the next wait, Bash can remove them from the jobs that wait -n checks. running still counts those jobs, so wait -n returns 127 and sets status=1 even though every collector succeeded. Both wait loops have this problem.

The same loop with 24 successful no-op jobs reported failure in 71 of 100 local runs on Bash 5.2. Keep the child PIDs and collect each saved exit status instead of assuming every wait -n accounts for one started job.

@omarchybot

Copy link
Copy Markdown
Collaborator

Classification is unsettled at 5ae01845626e60aa0027beda3bdff72fc97e8e9e. I classified the repeated reading of unchanged pi/omp history as a bug; the configured classification reviewer, deepseek-v4p1-flash, returned enhancement. The classification check therefore recorded a disagreement and no class label was added.

GPT-6 in Codex reviewed the report and existing comments for classification; the session does not expose its effort setting, and the classifier did not report one. Runtime verification and fixes were not attempted because the queue requires classification to agree first. This waits on the maintainer to resolve the classification.

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.

2 participants