Skip to content

Add Spend by branch & worktree lens (project x branch attribution) - #1319

Merged
iamtoruk merged 5 commits into
mainfrom
feat/spend-by-branch
Sep 12, 2026
Merged

iamtoruk merged 5 commits into
mainfrom
feat/spend-by-branch

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

Supersedes #1297. All feature work by @ozymandiashh; rebased onto main (past #1307, #1316) with one correctness fix on top.

Rebase

Clean, zero conflicts. #1307 and #1316 touched the same files but not the same lines.

Fix on top

codeburn:getBranchSpend was the only data fetch in createBridgeHandlers without projectArgs(), so a project hidden by the Projects pane filter still showed its branches in the lens. Added it, and extended the existing "scopes every fetch" test to cover the channel. Removing the call again fails that test.

Real-data proof

Frozen range 2026-08-15..2026-09-05, 1927 projects, 1746 branch rows.

Math, deltas to 6dp:

check result
per project: named + unknown + no-branch-data vs totalCost 0.000000, all 1927
per project: totalCost vs report --format json cost 0.000000, all 1927
per branch: sum of sessionRows vs branch cost 0 mismatches / 1746
per branch: sum of worktrees vs branch cost 0 over, 0 partial
grand total vs report 13420.418247 vs 13420.418247

Identity is one notion, not two: src/branch-spend.ts:339 and src/session-contributions.ts:171 both key the project through spendProjectIdentity. Cross-checking branch-json against sessions --format json --contributions on the same range, every one of the 1746 (projectId, branch) keys matches to 0.000000. The 266 keys present only in contributions are all null-branch and total $397.528303, exactly totals.noBranchDataCost.

Branch-switching sessions are attributed per turn, not to the last branch: the carry-forward rule in src/branch-spend.ts:234 is identical to src/session-contributions.ts:111. 20 real multi-branch sessions in range; the largest splits $2119.88 across 47 branches.

Worktrees fold into the parent project through the parser's canonical cwd; workingDirectory stays the raw historical path as worktree evidence. Sessions with no branch anywhere go to the explicit coverage bucket, never relabeled.

Contract

spend --format json still rejected: unknown format "json". Valid values: flow-json, branch-json.

Additive: spend --format flow-json byte-identical main vs this branch on two frozen ranges (2026-07-01..07-11 sha 7dcbca97, 2026-08-01..08-20 sha 743d3738).

serve --stdio needs no allowlist change; SERVE_OPTIONS.spend already keys on option names. Verified live over stdio with --project, --exclude, --provider and a custom range, all ok.

Privacy

No new egress. branch-json is CLI and IPC only, never enters MenubarPayload. On a real payload, every worktree path is stripped by sanitizeForSharing and by MCP redactProjectNames.

Pre-existing, out of scope here: current.byBranch[].branch survives both (landed in #788), so branch names already leave the machine.

Tests

Root: tsc --noEmit clean, 4014 passed / 5 skipped, 296 files.
App: typecheck clean, 928 passed / 27 failed. Same 27 fail on main (920 passed there), all localStorage-environment failures in usePolled and overviewSnapshot.

Mutation: breaking the canonical project identity in the branch key, relabeling the Unknown branch, and dropping projectArgs() each fail a test.

Perf

Lifetime on this machine, warm: flow-json 2.58s / 4.6 KB, branch-json 2.70s / 4.07 MB. CPU is a wash; the payload is 883x. JSON.parse of the 4 MB payload is 9ms.

Follow-ups, not blocking

  • The project picker uses the bare basename, so three different projects show as codeburn. assignDistinguishingProjectLabels in src/spend-flow.ts:68 already solves this for the flow lens.
  • The lens builds its own session list instead of reaching the Add drill-through: from aggregates to the sessions that compose them #1316 drill-through. branchFilters(project, branch) at app/renderer/lib/investigation.ts:123 is already there and has no caller; BranchBreakdown is the only component in Spend.tsx not handed onInvestigate.
  • BranchBreakdown mounts unconditionally in the Spend tab, so every visit spawns a branch-json run.

ozymandiashh and others added 5 commits September 12, 2026 13:09
Spend per canonical project x branch, keyed by the project identity the
spend-flow and By project views already use, so identical branch names in
different projects never merge. Reuses the aggregateByBranch rules: last-seen
branch carried forward across turns (the parser resolves the carry across a
range boundary from pre-range evidence), only ever-branched sessions
participate, and turns before a session's first branch stay an explicit
Unknown row.

Rows carry cost, behavioral calls, token components, distinct sessions
(identity-based, rows overlap), activity window, per-session contributions,
recorded worktree evidence (historical working directory, never today's
checkout), and a coverage split: known / Unknown / no-branch-data sources.
Same --period/--provider/--from/--to/--project/--exclude semantics as
spend --format flow-json. docs/by-branch.md documents the lens.
New getBranchSpend bridge channel (codeburn:getBranchSpend) spawning
'spend --format branch-json' with the same period/provider/range flags and
validation as the other first-click reports, plus background-priority
prefetch handling. The renderer keeps the typed contract in lib/types.ts.

BranchBreakdown panel: project picker (auto top project, explicit All
projects view labeled project/branch), expandable branch rows with token
components, worktree evidence and per-session contributions, a real session
inspect detail, and the coverage note (known / Unknown / no-branch-data,
identity-based distinct sessions). Reuses ListRow/Panel/Dropdown/stale-safe
usePolled memo keys; selection is derived so a refresh or a vanished project
falls back instead of mixing data.
The default 5173 collides with parallel CodeBurn worktree sessions sharing
one machine. strictPort stays on so a busy port fails loudly instead of
silently drifting; VITE_DEV_SERVER_URL follows the same port in the dev
script. Note the port does NOT isolate the Electron userData profile —
parallel sessions must still run the app one at a time.
getBranchSpend was the only data fetch in createBridgeHandlers without
projectArgs(), so a project hidden by the Projects pane filter still showed
its branches in the Spend > By branch lens.

Extends the existing "scopes every fetch" test to cover the channel.
@iamtoruk
iamtoruk merged commit 003854c into main Sep 12, 2026
20 checks passed
iamtoruk added a commit that referenced this pull request Sep 12, 2026
…w flags

Rebased on main, which since gained #1307, #1316 and #1319.

- Delete app/renderer/lib/sampleNavigation.ts. It was a placeholder for a
  registry that never landed: #1316 ships drill-through as an onInvestigate
  callback prop plus NavState. The sample rows now call it, threaded from
  App.tsx through Compare, and open the owning session for real.
- Give a CohortObservation the provider/project/session triple the sessions
  report keys rows by (inferSessionProvider, session.project || project.project),
  so a sample resolves to exactly one session row. Without it a cohort sample
  could not build sessionRowKey at all.
- Carry projectArgs() on both cohort spawns, per #1307. The saved project
  filter now scopes the facet query and the report; --project-id still narrows
  within it. Extended the scopes-every-fetch test to cover both.
- Allow --from, --to, --category and --project-id on compare in SERVE_OPTIONS.
  Verified over stdio: before, every cohort request but the bare facet query
  came back "command not served" and fell back to a spawn.
- Catch the invalid-date throw on compare like report does, instead of an
  unhandled UsageQueryError stack trace.
- Drop the unrelated CODEBURN_USER_DATA_DIR hunk in bootstrap().
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