Skip to content

Keep agent usage refreshes off pi/omp and opencode history they already counted - #15906

Open
Iann29 wants to merge 1 commit into
omacom:quattrofrom
Iann29:agent-usage-incremental-pi-opencode
Open

Iann29 wants to merge 1 commit into
omacom:quattrofrom
Iann29:agent-usage-incremental-pi-opencode

Conversation

@Iann29

@Iann29 Iann29 commented Oct 18, 2026 •

Copy link
Copy Markdown

The agents panel runs omarchy-agent-usage-update every 15 minutes. Claude transcripts and native Codex rollouts already have per-file indexes, but the pi/omp and opencode scans still read everything on every refresh, and both collectors do it: the Claude collector parses every pi/omp session and json.loads every opencode message row, and the Codex collector runs rg over the pi/omp roots and a LIKE scan over opencode's message table.

That gets expensive with real history. On my machine (6.9 GB under ~/.omp/agent/sessions, a 3.6 GB opencode.db) those scans took about 19 s, 19 s and 21 s per refresh. On 4.0.4, which still rescanned Claude transcripts as well, every refresh pinned the NVMe at ~99% for about a minute and the whole desktop stalled with it (/proc/pressure/io full avg60 around 17%).

What changes

  • pi/omp sessions get the same per-file index as Claude transcripts: an unchanged session is not opened, a grown one is read from the stored offset behind the same head/tail digest check, and anything else is read from the start. resume_point() and index_entry() come out of parse_usage_records() so both scans share them.
  • In the Codex collector the pi index keeps each message's identity (id, timestamp, usage) rather than per-file totals, so fork deduplication still runs over the whole tree on every refresh, just without rereading it. The parent session is taken from the header line while the file is read.
  • opencode rows are tracked by (id, time_updated). Each refresh selects only those two columns, which sit before data, so SQLite never reaches the JSON overflow pages, and only new or changed rows are parsed. The Codex collector keeps its LIKE/json_extract gate on the rows it does read. A table without time_updated falls back to reading every row.
  • --force still rereads everything.

One behavior change

rg searched every file under the pi/omp roots, so omp's *.eval.log files that quote session lines were counted as sessions. The Codex collector now reads *.jsonl only, as the Claude collector already did. On my data that removed 4 phantom sessions (778 → 774); prompts and tokens were unaffected.

Verification

  • Ran the current and new scans side by side on the same real data, one source at a time: identical stats for Claude pi/omp, Claude opencode, Codex opencode, and Codex pi/omp (compared against rg -g '*.jsonl', given the change above).
  • Warm refresh, per source: Claude pi/omp 19.2 s → 0.1 s, Claude opencode 19.3 s → 0.1 s, Codex pi/omp 20.6 s → 0.9 s, Codex opencode 0.55 s → 0.09 s. Each collector now reads ~10 MB per refresh. With both installed on my machine, the panel's own refreshes take about 2 s.
  • New tests in both scanner suites cover appended sessions, unchanged sessions served from the index (the existing unreadable-file trick), logs under the pi roots, and opencode rows reparsed only once time_updated moves (plus a deleted row on the Claude side). Both suites fail at the new tests against the current scripts.
  • The account test's scan_pi_usage/scan_opencode_usage stubs take the new use_index argument, and the Codex suite no longer requires rg.
  • All agent-usage-* suites, agents-panel-test.sh and the i18n checks pass.

Not changed here: several Codex scanner tests run the collector without setting XDG_CACHE_HOME, so with it exported (as on my machine) they write cache files for their temporary homes into the real ~/.cache/omarchy/agent-usage. Happy to send that separately.

Fixes #12411

@greptile-apps

greptile-apps Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Adds indexes so usage refreshes skip pi/omp history and opencode rows they already read.

  • Claude usage refreshes reuse pi/omp sessions and OpenCode rows.
  • Codex refreshes reuse saved history and count only session files.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Usage refresh] --> B{Force?}
  B -->|Yes| C[Read all source data]
  B -->|No| D[Load saved indexes]
  D --> E[Check pi/omp file size and modification time]
  E --> F[Reuse records or read changed files]
  D --> G[Read opencode row IDs and update times]
  G --> H[Reuse records or parse changed rows]
  C --> I[Count usage from records]
  F --> I
  H --> I
  I --> J[Save indexes and usage totals]
Loading

Reviews (1) · Last reviewed commit: "Keep agent usage refreshes off pi/omp an..." · Reviewed by Greptile

@omarchybot omarchybot added bug Something isn't working verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge labels Oct 19, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at 4f80e51 against quattro (b5f8593), on a disposable Omarchy worker. Nothing found that needs changing.

What ran there:

  • The problem, reproduced. A synthetic history of pi, omp and omp-profile sessions plus an opencode database: after one refresh the unchanged session files were made unreadable and the collectors run again. On quattro both collectors lose that usage, because they reread every session. On this head both keep it from the index. Your new scanner tests fail against quattro's collectors and pass here.
  • Same answers as before. Both collectors from quattro and from this head, over the same history (forks with parentSession, lines without ids or timestamps, opencode v1 and v2 tables with malformed rows): identical stats, cold and with the index warm. They still matched after appends, a same-size rewrite, a replaced session, a new and a deleted one, and opencode rows updated, inserted and deleted. --force gave the same result too. The one difference is the *.eval.log change you describe: quattro's Codex collector counts the log as a session, and this head does not.
  • Cost. With 458 MB of sessions, a refresh where nothing changed went from 6.4 s to 0.9 s for the Claude collector and from 12.1 s to 2.1 s for Codex.
  • Suites. Every agent-usage-* suite, agents-panel-test.sh and ./test/cli pass.

Codex Medium reviewed it as a second opinion. It judged the scope reasonable for the problem and raised six edge cases, none of which I am asking you to change. Three of them describe the design the Claude transcript index already uses on quattro, and this PR reuses it: a final line is counted only once it ends in a newline, an edit in the middle of a file that leaves its first and last 4 KB alone is read as an append, and an unparseable timestamp is given the date of the scan that first read it. The other three need input pi and omp do not write: a reused message id whose first copy had no usage, an index file with the right version but the wrong shape, and a session header holding an integer longer than 4300 digits. It also pointed out that the append tests prove an unchanged file is never opened, but not that a grown file is read from its saved offset. None of this changes the result.

I linked #12411 to this PR. What was left of that issue is the pi/omp rg scan and the opencode query, and this fixes both while keeping fork deduplication across the whole tree. #14550 edits the same two collectors and their scanner tests, so whichever of the two lands second will probably need a rebase.

This is now waiting on the maintainer.

@omarchybot

Copy link
Copy Markdown
Collaborator

This head (4f80e51) no longer merges into quattro (now 48fcf5f), so the verified and ready labels came off. The conflict is in all five files, and resolving it means rewriting the change, not just merging it.

What changed underneath it: #14040 rewrote both collectors as thin wrappers. pi/omp and opencode scanning now live in shared helpers in shell/plugins/agents/collectors.py: scan_pi_sessions (line 916) and scan_opencode (line 1191). bin/omarchy-agent-usage-claude:288-290 and bin/omarchy-agent-usage-codex:164-176 just call them. None of the functions this PR changes (scan_pi_usage, scan_opencode_usage, read_pi_session and the rest) exist on quattro any more. The scanner tests these changes went into were rewritten too, and the shared coverage now sits in agent-usage-shared-test.sh and agent-usage-pi-scan-test.sh.

The problem you're fixing is still there. quattro caches a whole scan for only 20 seconds on a normal refresh (SCAN_REUSE_SECONDS, collectors.py:56), and that cache is per collector. On every rescan, each root is searched again with rg, and both opencode message stores are queried in full. Nothing records which pi/omp files or opencode rows were already counted. The only per-file indexes on quattro cover Claude Code transcripts and native Codex rollouts. Neither covers pi/omp or opencode, so nothing there makes this PR obsolete. Since #14040 the cost has grown: 17 collectors now call both helpers, where only claude and codex did before.

A port would have to go into those helpers and keep what they now do beyond the old code:

  • Fork deduplication and prompt ownership across providers. This needs model switches, typed prompts and summaries, not just this collector's assistant lines.
  • Both opencode stores (V2 session_message and message), with migrated ids deduplicated and subagent sessions left out.
  • The newer pi roots (PI_CODING_AGENT_DIR, PI_CODING_AGENT_SESSION_DIR, a sessionDir in settings).
  • The rule that an incomplete scan is never cached.

There's also a scope choice. An index in the shared helpers changes all 17 collectors at once, and needs one writer and a lock, like the Claude Code index at collectors.py:1606. Per-collector indexes in claude and codex alone would leave the other 15 rescanning.

What was checked: the PR head against current quattro, by reading only. Nothing was run this time: the head hasn't changed since the worker run on 2026-10-19, and that run tested it against code that has since been replaced. Codex Medium and muse-wonder (via pi, thinking high) each reviewed the conflict separately as second opinions. Both found that it needs a rewrite in the shared helpers, that the problem still exists on quattro, and that nothing there replaces this fix. Their answers agree with mine, but their independence isn't guaranteed. I checked the cache window and the count of 17 calling collectors against the source myself. #14550 also touches pi scanning and is still open.

This is waiting on the author: a version of this change built on the shared helpers in collectors.py.

@OldJobobo

Copy link
Copy Markdown
Contributor

I've opened #18938, which ports the pi/omp half of this to scan_pi_sessions() in collectors.py, where the scan moved in #14040. It follows the same approach as this PR and the Claude Code index: unchanged session files aren't opened, and grown ones are read from the stored offset.

It doesn't cover the opencode scan, so that half of this PR is still open for you or anyone else to port.

On a machine with 9.6 GB of pi sessions, a refresh went from 77 s and 490 CPU-seconds to 6 s and 13 CPU-seconds once the index is built, with identical usage records. The full numbers are in #18938.

@Iann29, if you'd rather carry this in your own PR, take whatever is useful from that branch.

@omarchybot

Copy link
Copy Markdown
Collaborator

Since the last review, #18938 by @OldJobobo has ported the pi/omp half of this to scan_pi_sessions() in collectors.py, which is where the scan has run since #14040. This head hasn't moved and still conflicts with quattro (3b36b9e). The functions it changes no longer exist there, so it can't fix the scan that runs today. For the pi/omp part, #18938 is the one to carry forward, and it credits this PR for the approach.

The opencode half of this PR is still the only one anywhere. scan_opencode() (collectors.py:1233) still parses every message row on each rescan, and #18938 doesn't touch it. The *.jsonl-only change is already on quattro. If you want to keep going, the useful next step would be a PR that brings the (id, time_updated) tracking and its tests to scan_opencode(), keeping both message stores and the deduplication it does now.

What was checked: both diffs, read against current quattro. Nothing was run, because this head hasn't changed since it was tested on a worker on 2026-10-19. Codex Medium was asked separately which PR to carry forward and came to the same answer. It saw the same material, so its independence isn't guaranteed.

This is waiting on the maintainer. Choosing between this and #18938, and whether to close this one or keep it open for the opencode work, is their call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex usage collector rereads GB of history and saturates CPU

3 participants