Skip to content

Cohort model comparison (Compare > Cohorts) over an explicit observation population - #1324

Merged
iamtoruk merged 4 commits into
mainfrom
feat/compare-cohorts
Sep 12, 2026
Merged

iamtoruk merged 4 commits into
mainfrom
feat/compare-cohorts

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

Supersedes #1292 by @ozymandiashh, rebased on main (which since gained #1307, #1316, #1319) and reviewed against real data. All of the original work is his; the last commit is review follow-up.

What it adds

compare --format cohort-json plus --from/--to/--category/--project-id, and a Compare > Cohorts sub-tab. An observation is one edit turn whose behavioral calls carry exactly one model; that model owns it, and only its own calls' cost is attributed. Turns mixing models go to a shared exclusion ledger with their combined cost, visible on screen.

Review follow-up (last commit)

  • Deleted the sampleNavigation.ts stub. It described a registerSampleOpener registry that never landed. Add drill-through: from aggregates to the sessions that compose them #1316 ships drill-through as an onInvestigate prop plus NavState, so sample rows now call that, threaded from App.tsx. Rows open the owning session for real instead of a no-op.
  • CohortObservation now carries provider and uses session.project || project.project, the same triple aggregateSessions keys rows by. Without it a sample could not build sessionRowKey at all. Pinned by a test comparing the two directly.
  • projectArgs() on both cohort spawns (feat(app): Projects pane that scopes the desktop app to chosen projects #1307). The saved project filter now scopes the facet query and the report; --project-id narrows within it rather than replacing it. The scopes-every-fetch test covers both handlers.
  • SERVE_OPTIONS gains --from, --to, --category, --project-id on compare. Verified live over stdio: before, every cohort request except the bare facet query came back command not served and fell back to a full spawn.
  • Invalid --from/--to on compare printed a raw UsageQueryError stack. Now the same friendly error report gives, exit 1.
  • Dropped an unrelated CODEBURN_USER_DATA_DIR hunk.

Verified on real data (frozen home snapshot, Fable 5.1 vs Opus 5, 30 days)

model obs sessions retries retries/obs one-shot cost median P90 mean out median ctx-proxy median
Fable 5.1 172 151 1 0.006 99.4% $0.732 $1.384 $1.040 104 127,800
Opus 5 301 230 312 1.037 65.8% $4.044 $16.029 $6.974 26,730 4,814,548

Excluded: 1 multi-model edit turn ($5.95), 0 turns without a behavioral model.

  • Cohort counts equal each model's editTurns in the classic aggregate for both models above. They diverge by exactly one for claude-fable-5: 300 vs 301, the mixed fable-5 + opus-4-8 turn that classic attributes to its first behavioral call and cohorts exclude. That is the only divergence in 526 edit turns, it is disclosed on screen, and it is now written down in the doc.
  • Retry rate is retries / edit turns, the same formula and the same "Retry rate" label the classic compare already uses, not a new one. retries > 0 implies hasEdits in countRetries, so summing over edit turns only cannot differ.
  • Percentiles: linear interpolation at (N-1)*p, pinned in tests on both the core and the renderer mirror.
  • Volume band: partition is exact across 3 measures x 20 min/max pairs on the real population, kept + outsideBand + missingMeasure == total every time, histogram buckets sum to the cost-known count, and a null band reproduces the CLI stats field for field.
  • compare --format json and the TUI are byte-identical to main on frozen data across today, week, month, 30days, all, lifetime, and with no flags at all.
  • Privacy: compare-cohorts is imported only by src/main.ts. MenubarPayload, the only thing share/MCP/sync transmit, carries no cohort data and none of the observation session ids.

Tests

Root tsc --noEmit clean, npm test 4048 passed / 5 skipped. app typecheck clean, vitest run 951 passed with the 27 known local usePolled/overviewSnapshot failures (localStorage unavailable in this node build), same set as on main.

Mutation checks, each caught:

  • multi-model turns attributed to the first model instead of excluded: 2 tests fail
  • whole-turn cost attributed to the owner: 1 test fails
  • provider dropped from the observation identity: 1 test fails

Timing and payload

call time bytes
cohort-json, 2 models, 30 days 1.04s 416 KB
cohort-json, 2 models, lifetime 1.03s 418 KB
facets, 30 days 1.04s 602 KB
facets, lifetime 1.04s 602 KB

Faster than the classic compare --format json (4.7s), which runs the self-correction git scan. Facets are dominated by the project list (1,674 identities, 513 KB of the 602 KB) which the picker needs; worth a --top later if it gets heavier.

Adjacent, not from this PR

current.topSessions[].projectKey survives redactProjectNames(payload, false) and reaches MCP clients that asked not to see project names. It comes from #1316 and is unrelated to cohorts, so it is left alone here.

ozymandiashh and others added 4 commits September 12, 2026 13:59
compare --format cohort-json answers a facet query (models, canonical
projects, activity categories) and, with --model-a/--model-b, a cohort
comparison whose observation is one edit turn driven by exactly one
behavioral model. Cost attributed to the owning model's own calls in the
turn; turns mixing models are excluded, counted, and shown with their
combined cost; $0 on a model without a free-rate rule is unknown cost,
not zero. Percentiles use linear interpolation at (N-1)*p — [1,2,4,8]
gives median 3 and P90 6.8, pinned in tests. The report carries the full
observation list, so every statistic is reproducible from the payload.
--from/--to and --category are additive on the compare command; the
classic json/tui contracts are unchanged.
…ume band

Desktop cohorts mode talks to the new cohort-json format through two
typed bridge channels (facets + report) with argv validation for models,
repeatable project patterns, and categories. The section keeps the
classic mode side by side; the cohorts view shows the population before
any metric (included observations, distinct sessions, excluded mixed-
model turns with their combined cost, unknown-cost count), an Inspect
samples list of the declared population through a minimal navigation
adapter (sampleNavigation.ts), cost median/P90/mean with a compact
histogram whose top bucket is open-ended, one-shot/retry rates over the
declared population, and token volume with input + cache-read labeled as
a context proxy. The volume band recomputes renderer-side over the
report's own observation list (cohortStats.ts mirrors the core
convention, pinned to the same test vector), excludes and counts
observations without a token measure, and never reads missing data as
small. CODEBURN_USER_DATA_DIR opts into an isolated Electron profile so
parallel dev runs get their own userData/serve.pid.
…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().
@iamtoruk
iamtoruk force-pushed the feat/compare-cohorts branch from 8cd1c94 to 029843c Compare September 12, 2026 21:06
@iamtoruk
iamtoruk merged commit e024758 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