Skip to content

perf: reduce idle cost scan work - #2848

Merged
steipete merged 1 commit into
mainfrom
steipete/intelligent-mccarthy-fe6280
Aug 10, 2026
Merged

perf: reduce idle cost scan work#2848
steipete merged 1 commit into
mainfrom
steipete/intelligent-mccarthy-fe6280

Conversation

@steipete

Copy link
Copy Markdown
Owner

Summary

  • Memoize validated Claude source inventory and reports so stable refreshes skip the 35 MB cache decode, 92k-row rebuild/reprice, and cache rewrite.
  • Reuse unchanged persisted per-file cache entries after a cold process start.
  • Make automatic Codex history catch-up near-idle, remove the separate periodic token timer, and apply a 15-minute local-cost floor (30 minutes in low-power mode) while preserving accelerated mode and the selected provider refresh cadence.

Profiling proof

  • Same signed build, 8.58 GB / approximately 5,960-file local-history backlog.
  • Last-10-minute idle CPU average fell from 9.59% to 1.16%, an approximately 88% reduction.
  • Peak RSS fell from 821 MB to 436.5 MB; post-fix end RSS was also 436.5 MB.
  • Warm seven-minute Time Profiler average: 1.124% app CPU.
  • The warm final trace contained no Claude cost-cache or Codex catch-up stack; the remaining burst was the user-selected five-minute provider refresh.
  • Automatic Codex catch-up duty cycles: 0.10% AC, 0.05% unknown power, and 0.02% battery. Accelerated mode remains 100%.

Tests

  • make check passed.
  • Full make test: 836/836 selections across 70 groups passed.
  • Final focused tests: 22/22 passed.
  • Signed release package/build and launch smoke passed.

No screenshots: this change has no UI impact.

@clawsweeper

clawsweeper Bot commented Aug 10, 2026

Copy link
Copy Markdown

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9e90838497

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +723 to +727
if !options.forceRescan,
let priorMemo,
priorMemo.sourceInventory == sourceInventory,
priorMemo.reportKey == reportKey
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Recheck debounce before returning memoized Claude reports

When the first call in a process happens inside the scanner debounce window after a Claude transcript changed, priorMemo is nil so the code below can build a report from the old disk cache and then store it against the current sourceInventory. Subsequent calls in long-lived CLI/serve processes then match this early-return guard and never reach the nowMs - cache.lastScanUnixMs > refreshMs check, so cost output can stay stale until a force rescan, cache rewrite, or another source change occurs; avoid installing/returning a memo for a debounced, unrefreshed cache whose source inventory has not been proven current.

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. P2 Normal priority bug or improvement with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 10, 2026
@clawsweeper

clawsweeper Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codex review: needs changes before merge. Reviewed August 10, 2026, 2:15 PM ET / 18:15 UTC.

ClawSweeper review

What this changes

This PR memoizes unchanged Claude cost reports, reuses persisted per-file cache rows, reduces automatic Codex catch-up duty cycles, and limits automatic local-history scans to provider refreshes.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep open: the performance approach is useful and profiling is substantial, but the new Claude memo can permanently return stale local-cost data after a debounced cold-process scan.

Priority: P2
Reviewed head: 9e90838497f3d8729ae02254ea8bec17c8548080

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The profiling and test investment are strong, but a concrete stale-cache correctness blocker remains.
Proof confidence 🌊 off-meta tidepool Not applicable: This owner-authored PR is exempt from the external contributor proof gate; its signed-build profiling remains useful supplemental evidence.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This owner-authored PR is exempt from the external contributor proof gate; its signed-build profiling remains useful supplemental evidence.
Evidence reviewed 5 items Stale memoization path: The PR records a memo using the current source inventory even when a first-process call did not refresh the disk cache; a later matching call returns that memo before evaluating refresh eligibility.
Missing regression coverage for the debounced path: The new memo tests set refreshMinIntervalSeconds to zero, so they exercise immediate refreshes rather than a cold process inside the debounce window.
Existing review corroboration: The prior P2 review comment identifies the same sequence: a changed transcript, no process memo, an unrefreshed debounced disk cache, then a memo incorrectly keyed to the new inventory.
Findings 1 actionable finding [P2] Do not memoize an unrefreshed first scan
Security None None.

How this fits together

CodexBar refreshes provider status and local token-cost history, then publishes the resulting usage to its menu and widget snapshots. The changed path inventories Claude transcript files, decides whether cached cost data is current, and returns a report to those consumers.

flowchart LR
A[Scheduled provider refresh] --> B[Cost-history eligibility]
B --> C[Claude source inventory]
C --> D{Memo matches?}
D -->|yes| E[Return cached cost report]
D -->|no| F[Read or update persisted cache]
F --> G[Menu and widget snapshots]
Loading

Before merge

  • Do not memoize an unrefreshed first scan (P2) - When a fresh process enters the debounce window after a transcript changes, priorMemo is nil, so the changed inventory does not make shouldRefresh true. This stores old disk-cache output under the new inventory here; later calls match the early-return guard and never re-evaluate the debounce. Add a positive-debounce cold-process regression and avoid storing or returning that unproven memo.
  • Resolve merge risk (P1) - Existing users can see stale Claude local-cost totals indefinitely after a changed transcript is first observed during the cache debounce interval.
  • Complete next step (P2) - A focused source and regression-test repair can address the only confirmed merge blocker without changing the intended cadence policy.

Findings

  • [P2] Do not memoize an unrefreshed first scan — Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner+Claude.swift:839-845
Agent review details

Security

None.

Review metrics

Metric Value Why it matters
Patch scope 14 files affected; 881 added, 160 removed The optimization crosses cache invalidation, background scheduling, documentation, and tests.
Production versus test delta non-test +434/-130; tests +447/-30 The sizable implementation has comparable focused test growth, but the debounce edge case remains untested.

Merge-risk options

Maintainer options:

  1. Repair memo freshness before merge (recommended)
    Add a guard and regression test so a debounced first scan cannot associate an old cache report with a newly observed transcript inventory.

Technical review

Best possible solution:

Keep the inventory-based optimization, but store or return a memo only after the cache is proven to represent that inventory; add a regression covering a cold process inside the debounce interval.

Do we have a high-confidence way to reproduce the issue?

Yes—source-reproducible: modify a Claude transcript, start a fresh process inside a positive refresh debounce interval, then call again without another file change; the first call can store old cache output under the new inventory and later calls return it early.

Is this the best way to solve the issue?

No—the optimization is appropriate, but memoization must not certify freshness when the underlying disk cache was deliberately left unrefreshed.

Full review comments:

  • [P2] Do not memoize an unrefreshed first scan — Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner+Claude.swift:839-845
    When a fresh process enters the debounce window after a transcript changes, priorMemo is nil, so the changed inventory does not make shouldRefresh true. This stores old disk-cache output under the new inventory here; later calls match the early-return guard and never re-evaluate the debounce. Add a positive-debounce cold-process regression and avoid storing or returning that unproven memo.
    Confidence: 0.98

Overall correctness: patch is incorrect
Overall confidence: 0.98

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 38c5a516f579.

Labels

Label changes:

  • add P2: The patch can make local cost reporting stale for affected users, without causing a core runtime outage.
  • add merge-risk: 🚨 compatibility: The new persisted-cache and memo behavior can silently replace current cost-report freshness with indefinitely stale output.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This owner-authored PR is exempt from the external contributor proof gate; its signed-build profiling remains useful supplemental evidence.

Label justifications:

  • P2: The patch can make local cost reporting stale for affected users, without causing a core runtime outage.
  • merge-risk: 🚨 compatibility: The new persisted-cache and memo behavior can silently replace current cost-report freshness with indefinitely stale output.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This owner-authored PR is exempt from the external contributor proof gate; its signed-build profiling remains useful supplemental evidence.

Evidence

Acceptance criteria:

  • [P1] swift test --filter CostUsageScannerClaudeMemoTests.
  • [P1] make check.
  • [P1] make test.

What I checked:

Likely related people:

  • steipete: Recent main-branch history includes the prior stable-cost-cache optimization and token refresh cadence work in the affected paths. (role: primary recent area contributor; confidence: high; commits: 17afef7b84eb, f15a66d06eaf; files: Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner+Claude.swift, Sources/CodexBar/UsageStore.swift, Sources/CodexBar/UsageStore+TokenRefreshSequence.swift)
  • Xu Xiang: Introduced the bounded Codex cost catch-up behavior that this PR retunes. (role: Codex catch-up contributor; confidence: medium; commits: 2920019bc16d; files: Sources/CodexBar/CodexCostCatchUpPolicy.swift)
  • Milan Mijatovic: Previously worked on Claude cost-cache invalidation, adjacent to the new memo invalidation logic. (role: cache invalidation contributor; confidence: medium; commits: cdab5d32b533; files: Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner+Claude.swift)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Fix the memo freshness guard and add the positive-debounce cold-process regression test.
  • Re-run the focused memo tests, make check, and the full test suite after the repair.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete merged commit d7934a5 into main Aug 10, 2026
15 of 17 checks passed
@steipete
steipete deleted the steipete/intelligent-mccarthy-fe6280 branch August 10, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. P2 Normal priority bug or improvement with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant