Skip to content

Add period-vs-period comparison (Compare periods) with contribution drill-down - #1323

Merged
iamtoruk merged 6 commits into
mainfrom
feat/compare-periods
Sep 12, 2026
Merged

iamtoruk merged 6 commits into
mainfrom
feat/compare-periods

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

Supersedes #1296. All feature work by @ozymandiashh; rebased onto main (post #1316 drill-through, #1307 project scope, #1319 spend-by-branch) with review fixes in one follow-up commit.

Rebase conflicts

Review fixes

  • Stub navigation removed. App.tsx shipped an inspectRange adapter calling setCustomRange, which no longer exists after Add drill-through: from aggregates to the sessions that compose them #1316 — it did not compile on current main. Replaced with the real drill-through: commitNav to Sessions with the clicked side's range and projectFilters/modelFilters for the contribution key, so the destination is the population the row describes, not the whole range.
  • projectArgs() on both new IPC channels (feat(app): Projects pane that scopes the desktop app to chosen projects #1307). A saved project filter scoped neither getPeriodCompare nor getPeriodCompareSessions. The "scopes every fetch" main-process test now covers both.
  • CLI drill-down honoured no project filter. compare-periods --format sessions skipped filterProjectsByName, so a hidden project could surface behind a contribution. Fixed, with a test.
  • Bad dates printed a stack trace. parseDateRangeFlags throws; the PR's if (!a || !b) guard could never fire. Wrapped in the same try/catch every other date-flag command uses.
  • Carried history disclosed at the totals. Detail-only totals can sit far below report/status for the same range; the Coverage card was three cards below the number it qualifies.

Proof on real data (frozen ranges, A 2026-06-01..06-30 vs B 2026-07-01..07-30)

Totals reconcile exactly — B − A − diff == 0 for every component:

metric A B diff
cost 164.65829900000003 1252.589126300002 1087.930827300002
calls 766 13070 12304
sessions 8 1183 1175
inputTokens 20082397 41954732 21872335
outputTokens 305243 7461508 7156265
cacheReadTokens 110178048 1720317942 1610139894
cacheWriteTokens 0 6386419 6386419

Lens sums vs totals (float noise only):

lens rows sumA − totA sumB − totB sumDiff − totDiff
projects 614 0.0 -1.4e-12 -1.4e-12
models 11 +2.8e-14 -1.6e-12 -1.6e-12

Basis: compare-periods totals equal models --format json for the same range to 2e-12 (A exact, B -2.27e-12) — the transcript basis sessions/models/spend use. They do not equal report/status, which read the durable daily history. The gap is reported exactly: history.historyCost A 1214.1750341 / B 5636.89843685 matches report --format json overview cost to the digit on both sides, and aggregateOnly A $1049.52 (10 days) / B $4398.49 (19 days) is excluded from totals and lenses, as the basis line says.

Normalized: cost/day uses each side's own calendar days (A 30, B 30 — printed in denominators), cost/100 calls each side's own calls (A 766, B 13,070). Both recompute by hand to the last digit; a zero denominator yields null, rendered —.

Unpriced models are disclosed, never folded: on a 30-day pair with unpriced usage, coverage listed big-pickle (231 calls), cursor-grok-4.6-high (749), x-preview-f-free (107), Qwen3.8-Flash-Next-FP8 (1) at pricingCoverageA 0.9856, and none of them appear as a zero-cost lens row.

Drill-down reconciles with its lens row: /Users/torukmakto/Projects/eywa B sum 366.53678760000037 vs lens 366.53678760000037 (delta 0.0); Opus 5 1.6350025 vs 1.6350025. Project keys are spendProjectIdentity ids — all 299 keys with B cost matched a sessions --format json --contributions projectId exactly, so the rewired drill-through lands correctly.

Contract

  • No existing command output changed: report, sessions, models, spend --format flow-json byte-identical on a frozen range between main and this branch. status --format menubar-json and compare --format json differ only in generated/liveSessions/history, and differ identically between two runs of main — live-data drift, not this branch.
  • serve --stdio does not allowlist compare-periods (src/serve.ts:81 SERVE_OPTIONS). Per the module's own contract a rejected command falls back to a normal spawn, so this is correct but cold: each desktop fetch is a full CLI start. Left for a follow-up, since the command hydrates the daily cache by default and the allowlist is a reviewed read-only surface.
  • Privacy: session detail never reaches sharing or MCP. sanitizeForSharing (src/sharing/sanitize.ts:9) only takes MenubarPayload; MCP registers get_usage and get_savings only (src/mcp/server.ts:47,95), both from redacted menubar data. period-diff.ts is imported by src/main.ts alone.
  • Cmd-9 is otherwise unused: the only '9' bindings are App.tsx:785 and Sidebar.tsx:33.

Tests

  • root npx tsc --noEmit clean; npm test 4042 passed, 5 skipped, 0 failed.
  • cd app && npm run typecheck clean; npx vitest run 939 passed, 29 failed, 4 skipped. All 29 failures are one local environment gap — jsdom 29.1.1 here exposes no localStorage (Cannot read properties of undefined). 27 are the known usePolled/overviewSnapshot baseline; the 2 in PeriodCompare have the same cause. With a localStorage shim PeriodCompare is 9/9 and overviewSnapshot 6/6.
  • Mutations: flipping costB - costA in contribution() kills 8 tests; flipping b.cost - a.cost in diffTotals kills 8; dropping projectArgs() from getPeriodCompare kills the scopes-every-fetch test; dropping filterProjectsByName from the sessions branch kills the new CLI filter test.

Performance (this machine, ~138 days of history)

  • two 30-day ranges: 9.28s / 9.58s, 168 KB payload
  • lifetime vs lifetime-minus-30: 5.42s / 5.45s, 713 KB payload
  • one drill-down (--format sessions): 9.64s, 712 bytes

Known, not fixed

  • Lens callsA/callsB sums run slightly under totals.calls because zero-cost rows are dropped from a cost-contribution lens (unpriced and free models). Costs still reconcile exactly and the dropped models are listed in coverage.
  • diffSessions counts session.apiCalls while the project lens counts project.totalApiCalls; on one real project that differed by 2 calls in 5255. Cost is unaffected and calls are not rendered in the drill table.

ozymandiashh and others added 6 commits September 12, 2026 13:43
…f engine

Add src/period-diff.ts: pure period-vs-period difference engine. Totals come
from the shared buildPeriodData rules; contributions per project and per model
carry status new/gone/up/down/flat with pct=null at A=0 (labeled New, never an
infinite percentage); normalized views per day and per 100 calls print their
denominators; day arithmetic walks local midnights so DST and month boundaries
count calendar days. historyBasis cross-checks the durable daily cache and
reports aggregate-only carried days separately instead of force-reconciling
them into the totals. diffSessions joins the drill-down on the canonical
session identity (provider + project path + sessionId), attributing the
model's own slice for the model lens.

New command: compare-periods --format json|sessions with explicit
--from-a/--to-a/--from-b/--to-b (local YYYY-MM-DD, validated loudly) defaulting
to the last seven complete days vs the seven before, plus --provider and the
existing --project/--exclude filtering. Range slicing reuses parseAllSessions'
call-granularity behavior, so a session straddling the boundary contributes to
each side by its own calls.

Tests: engine fixtures including the X/Y/Z acceptance reconciliation, A=0/B=0,
unequal lengths, DST and month walks, carried history, a real-parse boundary
session split by call and joined back into one drill-down row, and CLI runs on
a temp HOME fixture.
Two bridge channels follow the existing envelope + argv-validation pattern:
codeburn:getPeriodCompare spawns compare-periods --format json with two
REQUIRED local YYYY-MM-DD range pairs (validated per range), and
codeburn:getPeriodCompareSessions spawns the sessions drill-down for one
project/model contribution key. Contribution keys ride the --key VALUE
position, so a dash-leading slug (Claude sanitizes /work/pricing to
-work-pricing) passes vContributionKey where vToken would wrongly reject it.
Preload and the renderer CodeburnBridge types mirror the two calls.
New sidebar section 'periods' (Compare periods, shortcut 9) rendering
sections/PeriodCompare.tsx: preset last-7-complete-days vs prior 7 or two
custom calendars with a swap control; range meta shows both spans, the
duration delta and any overlap; totals, normalized per-day and per-100-calls
tables (denominators printed, zero denominators render as em dash); project
and model contribution lenses with New/Gone badges and click-to-inspect
session drill-down joined across A and B; coverage block for unpriced models
and aggregate-only carried history. A/B selection, lens and view persist in
localStorage so returning to the screen keeps the selection.

Navigation adapter until the goal-8 navigation lands: onInspectRange opens the
existing Sessions section scoped to the requested range via the app's
custom-range state; the Overview entry card links here. Renderer styling
follows the existing panel/table conventions.

Includes component tests (fixture report, New/Gone labels, per-100-calls
recomputation with zero-call sides, persistence across remounts, lens
switching) and user documentation at docs/compare-periods.md.
…rrors

Rebase fixes on top of #1296.

- App.tsx shipped a stub inspectRange calling a setCustomRange that no longer
  exists after #1316. Replaced with the real navigation: commitNav to Sessions
  with the clicked side's range AND projectFilters/modelFilters for the
  contribution key, so the drill-through lands on the population the row
  describes instead of the whole range.
- getPeriodCompare and getPeriodCompareSessions carried no projectArgs(), so a
  saved project filter did not scope either fetch (#1307). Added, and the
  "scopes every fetch" main-process test now covers both channels.
- compare-periods --format sessions ignored --project/--exclude, so a drill-down
  could surface a session from a hidden project. It now filters like the report.
- A bad --from-a/--to-a printed a raw stack trace: parseDateRangeFlags throws
  and the null check could never fire. Wrapped in the same try/catch every other
  date-flag command uses.
- Totals card now says inline how much daily history has no session detail
  behind it. On a machine with aged-off sources the detail-only total is far
  below report/status for the same range; the Coverage card alone was three
  cards away from the number it qualifies.
@iamtoruk
iamtoruk merged commit 2373f55 into main Sep 12, 2026
20 checks passed
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