Repository navigation
perf(agents): cut codex usage collector memory with SQL filter and cache - #6780
Conversation
The codex collector scanned every row of opencode.db (1.7 GB, 55k+ rows) with Python-side json.loads, peaking around 716 MB of RSS on every run -- including the panel's refreshLimits() call, which passed --limits-only that the collector silently ignored. Filter rows in SQL (LIKE gates + json_valid + json_extract authority, mirroring the old Python filter semantics) so giant blobs are never parsed, and cache the local stats scan in XDG_CACHE_HOME following the claude collector's pattern (atomic writes, flock, schemaVersion). --force rescans, --limits-only and normal mode reuse a fresh cache and fall back to a full scan when it is missing, stale, or corrupt. Measured: cold scan 716 MB -> 158 MB peak; warm --limits-only ~85 MB and ~1.4 s. Output record schema and values are unchanged for the same data (parity verified against the old filter, including malformed rows).
There was a problem hiding this comment.
Pull request overview
Reduces Codex usage collector resource consumption through SQL filtering and cached local statistics.
Changes:
- Filters OpenCode messages in SQLite.
- Adds atomic, locked usage caching and flag handling.
- Expands scanner cache and malformed-data tests.
Tip
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
bin/omarchy-agent-usage-codex |
Adds SQL filtering and local-stat caching. |
test/shell.d/agent-usage-codex-scanner-test.sh |
Tests caching, flags, and failure recovery. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
A no-flag run is the widget's periodic refresh, and refreshIntervalSec is configurable down to 30 seconds; holding every mode to a 15-minute cache meant stats could lag far behind the interval the user asked for. Mirror the claude collector: normal runs reuse a scan for ~20 seconds purely to dedup concurrent collectors, and only --limits-only, which promises just fresh limits, may reuse a scan for up to 15 minutes. Co-Authored-By: Claude Fable 5 <[email protected]>
The cached stats embed date-dependent fields (todayPrompts, todayTotalTokens, recentDays), but only the file's age was checked, so a cache written at 23:58 served yesterday's numbers as "today" for up to 15 minutes past midnight. Stamp the envelope with the scan's local date and treat any other date as a miss. Co-Authored-By: Claude Fable 5 <[email protected]>
A cache whose mtime is ahead of the clock has a negative age, which the freshness check accepted forever: setting the clock backwards froze the stats until real time caught up with the file. Require a non-negative age before trusting the cache. Co-Authored-By: Claude Fable 5 <[email protected]>
A transient lock, schema migration, or corrupted database aborts the opencode scan mid-flight; the partial numbers still serve the current run, but persisting them let a single bad read suppress opencode usage for every cache reader until expiry. The claude collector already skips its opencode cache write on a database error; do the same here. Co-Authored-By: Claude Fable 5 <[email protected]>
The query relied on json_valid(data) evaluating before json_extract(), but SQLite does not promise that AND terms run left to right; a reordered plan would let json_extract raise on a malformed row and silently truncate the scan. Wrap each json_extract in a CASE so the guard is structural rather than positional. Co-Authored-By: Claude Fable 5 <[email protected]>
"These caches were world-readable before" was copied from the claude collector; codex had no caches before this one existed. Explain the chmod on its own terms. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Reviewed. The SQL-side filtering and cache plumbing are well built — the filter agrees with the old Python filter on everything JS
All pinned by new tests (20 assertions in the scanner test, full suites green). — 🤖 Claude, posting on behalf of @dhh |
…che (omacom#6780) * perf(agents): cut codex usage collector memory with SQL filter and cache The codex collector scanned every row of opencode.db (1.7 GB, 55k+ rows) with Python-side json.loads, peaking around 716 MB of RSS on every run -- including the panel's refreshLimits() call, which passed --limits-only that the collector silently ignored. Filter rows in SQL (LIKE gates + json_valid + json_extract authority, mirroring the old Python filter semantics) so giant blobs are never parsed, and cache the local stats scan in XDG_CACHE_HOME following the claude collector's pattern (atomic writes, flock, schemaVersion). --force rescans, --limits-only and normal mode reuse a fresh cache and fall back to a full scan when it is missing, stale, or corrupt. Measured: cold scan 716 MB -> 158 MB peak; warm --limits-only ~85 MB and ~1.4 s. Output record schema and values are unchanged for the same data (parity verified against the old filter, including malformed rows). * Scope the codex scan cache's 15-minute reuse to --limits-only A no-flag run is the widget's periodic refresh, and refreshIntervalSec is configurable down to 30 seconds; holding every mode to a 15-minute cache meant stats could lag far behind the interval the user asked for. Mirror the claude collector: normal runs reuse a scan for ~20 seconds purely to dedup concurrent collectors, and only --limits-only, which promises just fresh limits, may reuse a scan for up to 15 minutes. Co-Authored-By: Claude Fable 5 <[email protected]> * Invalidate the codex scan cache across day boundaries The cached stats embed date-dependent fields (todayPrompts, todayTotalTokens, recentDays), but only the file's age was checked, so a cache written at 23:58 served yesterday's numbers as "today" for up to 15 minutes past midnight. Stamp the envelope with the scan's local date and treat any other date as a miss. Co-Authored-By: Claude Fable 5 <[email protected]> * Reject codex scan caches with a future mtime A cache whose mtime is ahead of the clock has a negative age, which the freshness check accepted forever: setting the clock backwards froze the stats until real time caught up with the file. Require a non-negative age before trusting the cache. Co-Authored-By: Claude Fable 5 <[email protected]> * Never cache an interrupted opencode scan A transient lock, schema migration, or corrupted database aborts the opencode scan mid-flight; the partial numbers still serve the current run, but persisting them let a single bad read suppress opencode usage for every cache reader until expiry. The claude collector already skips its opencode cache write on a database error; do the same here. Co-Authored-By: Claude Fable 5 <[email protected]> * Make the json_valid guard order explicit in the opencode query The query relied on json_valid(data) evaluating before json_extract(), but SQLite does not promise that AND terms run left to right; a reordered plan would let json_extract raise on a malformed row and silently truncate the scan. Wrap each json_extract in a CASE so the guard is structural rather than positional. Co-Authored-By: Claude Fable 5 <[email protected]> * Drop a claude-collector comment that is false for codex "These caches were world-readable before" was copied from the claude collector; codex had no caches before this one existed. Explain the chmod on its own terms. Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: markbusking <[email protected]> Co-authored-by: David Heinemeier Hansson <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]>
Problem
The
omarchy-agent-usage-codexcollector scanned every row ofopencode.dbwith Python-sidejson.loads— on a typical install that is a 1.7 GB DB with 55k+ rows, producing a ~716 MB RSS peak + 8 s CPU on every run. Theomarchy.agentswidget triggers it on shell start and every 15 minutes, and the panel'srefreshLimits()call passes--limits-only— a flag the collector parsed and silently ignored, so opening the panel paid the full scan too.Change
scan_opencode_sessions():LIKEgates (acceleration) +json_valid+json_extract(authority) with the exact same semantics as the old Python filter (role == "assistant"andproviderID == "openai"). Malformed rows are excluded viajson_validinstead of aborting the scan.omarchy-agent-usage-claudepattern:$XDG_CACHE_HOME/omarchy/agent-usage/codex-scan-<digest>.jsonwith atomic writes,fcntl.flockdouble-checked locking, and aschemaVersionenvelope validated on read.--forcerescans and rewrites the cache; normal and--limits-onlyreuse a fresh (≤900 s) cache and fall back to a full scan when it is missing, stale, or corrupt. Cache-layer failures degrade to a direct scan and never drop the record.Measured (this machine, real 1.7 GB opencode.db)
--limits-onlywarmParity
The output record schema and values are unchanged for the same data — verified against the old Python filter including malformed-row cases (valid rows still counted, bad rows skipped, no silent abort).
--forceproduces byte-identical local stats to the pre-change full scan.Tests
test/shell.d/agent-usage-codex-scanner-test.sh: 6 original + 9 new (cache write/perms/schema,--limits-onlyreuse,--forcebypass, stale-cache rescan, corrupt/unwritable cache fallback, malformed-row parity, compact JSON). All 15 pass; claude collector suite (8) still passes.