Skip to content

fix(parser): stop --provider filters from re-surfacing cached claude sessions - #872

Merged
iamtoruk merged 1 commit into
getagentseal:mainfrom
ozymandiashh:fix/provider-filter-claude-orphan-leak
Aug 3, 2026
Merged

iamtoruk merged 1 commit into
getagentseal:mainfrom
ozymandiashh:fix/provider-filter-claude-orphan-leak

Conversation

@ozymandiashh

Copy link
Copy Markdown
Collaborator

Fixes #871

The bug

--provider <x> filters the headline but not the live-parse panels, so By Model lists Anthropic models under --provider cursor while the total above it shows cursor alone.

overview.cost      3.79      <- correct, from the provider-sliced daily cache
projects summed    2038.99   <- leaked
models summed      2038.99   <- leaked
activities summed  2038.99   <- leaked

Why

Claude is scanned through scanProjectDirs rather than parseProviderSources, and that call had no provider-filter guard. On a --provider <other> run discoverAllSessions correctly returns no claude sources, so claudeDirs is empty, but scanProjectDirs still ran. Its orphan pass reads the whole cached claude section and, with allDiscoveredFiles empty, treats every cached file as no longer discovered, re-injecting PR-bearing entries into the result. In read-only mode (cache refresh lock unavailable, which is the normal state while the menubar app is running) the prLinks gate is bypassed and every cached claude file is re-injected.

Every other provider already has this guard in the durable-orphan loop a few dozen lines below:

// Skip if filtered to a different provider
if (providerFilter && providerFilter !== 'all' && providerFilter !== providerName) continue

The fix

One guard on the claude scan, mirroring the one above.

This is deliberately not a claudeDirs.length > 0 check. When claude is in scope but every transcript has been pruned from disk, that orphan pass is exactly what keeps PR-attributed spend from vanishing, so gating on an empty dirs list would silently break the never-lose-history invariant. Gating on provider scope keeps both behaviours correct. The second test below locks that distinction in.

Nothing changes for --provider all or --provider claude. The paired eviction block already had a dirs.length > 0 guard, so no cache entries were ever wrongly deleted and none are now.

Verification against real data

report -p all --provider cursor, same machine, before and after:

before after
overview.cost 3.79 3.79
projects summed 2038.99 2.63
models summed 2038.99 2.63
models listed Fable 5, Opus 5, Opus 4.8, Cursor (auto), composer-2.5, grok-4.5, deepseek-reasoner Cursor (auto), composer-2.5, grok-4.5, deepseek-reasoner
projects listed -Users-husamsoboh, cursor, -Users-husamsoboh-Downloads cursor

--provider all before and after: identical model set, 489 projects in both, no rows dropped.

--provider claude after the fix: no cursor-only models present.

The residual 3.79 vs 2.63 delta is the pre-existing durable-versus-live difference, not part of this bug. It is one cached day (2026-07-26, €1.16) that the cache retains and the live parse can no longer reproduce because the Cursor SQLite store rotated. That is the never-lose-history invariant working as intended, and it is the same effect #865 documented in its unfiltered control.

Tests

Two new cases in tests/parser.test.ts, using the existing test-synthetic provider harness:

  1. does not surface cached claude sessions when filtering to another provider seeds a claude transcript carrying a pr-link, warms the cache with a provider: all parse, then parses again filtered to the synthetic provider and asserts the result contains only that provider. It is self-calibrating on cost, since the pricing engine re-derives cost from tokens. Without the fix it fails with expected [ 'claude', 'test-synthetic' ] to deeply equal [ 'test-synthetic' ].
  2. still preserves a PR-bearing claude orphan when claude IS in scope deletes the transcript and reparses unfiltered, asserting the total holds. This is the case a naive dirs-length guard would break.

Full suite: 2542 passing. Two failures in tests/parser.test.ts ((a) copilot JSONL file-purge monotonic, (f) durable orphans survive a parse-version bump) reproduce identically on unmodified main in my environment, both failing on their first parse with expected +0 to be 200, so they look environmental here rather than related to this change. tsc --noEmit is clean.

…sessions

Claude is scanned via scanProjectDirs instead of parseProviderSources, and
that call had no provider-filter guard. On a --provider <other> run
discoverAllSessions correctly returns no claude sources, so claudeDirs is
empty, but scanProjectDirs still ran: its orphan pass reads the whole cached
claude section and treats every file as no-longer-discovered, re-injecting
PR-bearing entries (and in read-only mode every cached entry) into the result.

The headline stayed correct because it comes from the provider-sliced daily
cache, so only the live-parse panels were wrong. By Model then listed
Anthropic models under --provider cursor while the total showed cursor alone.

Guard the scan with claudeInScope, mirroring the guard the durable-orphan
loop already applies. Deliberately not a claudeDirs.length check: when claude
is in scope but every transcript has been pruned, the orphan pass is what
keeps PR-attributed spend from vanishing.

@iamtoruk iamtoruk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verified and clean. This fixes a real cross-panel money leak: under --provider <other>, scanProjectDirs's orphan pass re-injected the whole cached Claude section into By Model / By Project / By Activity ($2,038 leaked vs $3.79 correct in your repro), worst in readOnly mode where the prLinks gate is bypassed.

The claudeInScope guard is the right fix — gated on provider scope, not claudeDirs.length, so it preserves the never-lose-history invariant (a PR-bearing claude orphan with pruned transcripts still surfaces under --provider all/--provider claude). Mutation-checked: the leak test fails without the guard and passes with it; the invariant test passes both ways.

An adversarial pass came back clean across all five vectors I care about here: the claude session cache is byte-identical after a filtered run (no corruption/eviction), --provider claude with pruned transcripts still preserves the orphan, no dangling progress event (claudeSources is non-empty only when claude is in scope), provider-name matching agrees across assertProvider/discovery/guard, and both readOnly states hold. Full suite green apart from the two pre-existing parser.test.ts failures that reproduce on main. Good to merge.

@iamtoruk
iamtoruk merged commit 201b289 into getagentseal:main Aug 3, 2026
4 checks passed
iamtoruk added a commit that referenced this pull request Aug 3, 2026
…he all-provider view

buildDurablePeriod derived the today slice of the multi-day, all-provider
headline from the unsliced whole-range parse, so a turn spanning local midnight
kept its category and turn count anchored on its yesterday start. The per-call
cost and calls bucketed onto today correctly, but By Activity and the JSON
daily turn count lost the post-midnight half — categories summed to only the
pre-midnight cost while the headline, By Model and By Project were right.

Slice the today parse with filterProjectsByDays first, which re-anchors the
straddling turn to its surviving today calls, so today's category cost lands on
today. Category cost is the sum of the slice's own calls, so day-N + day-N+1
still equals the whole-range total (no over-count); the per-day turn-count
split matches the cache side and the documented per-day semantics.

Adds a regression test in the straddling-turn conservation suite
(mutation-checked: fails on the pre-fix code). Also fills in the CHANGELOG
Unreleased entries for the batch (#853, #856, #872, #846/#859, #866/#867, #833).
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.

--provider <x> leaks claude spend into every live-parse panel

2 participants