Merge origin/main into feat/copilot-session-store-landing

Resolved against main's newer invariants:

- daily-cache: DAILY_CACHE_VERSION/MIN_SUPPORTED -> 21. Main shipped 19
  (#1015 grok); 18 was burned by this branch's earlier public head and 20 is
  claimed by the unmerged #1040, and isMigratableCache would adopt either as
  finalized without re-deriving. Header keeps main's v19 note and adds v21's.
- session-cache: PROVIDER_PARSE_VERSIONS keeps main's grok/dsh entries and
  appends this branch's `-session-store-v2` to main's copilot value.
- parser: durable age-out takes main's orphan-only rule (#992).
  retainWhilePresent is now redundant under it rather than load-bearing.
- audit-report: both imports (isBehavioralCall + sanitizeModelForDisplay).
- tests/parser (e), (l), (sc): re-pinned to the orphan-only age-out. A
  still-discovered >90d copilot events.jsonl now keeps serving its per-turn
  output alongside the store rows; the (sc) age-out subject is now an orphan.
- daily-cache-version-rederivation: seed 20 so the adjacent-draft-version
  case is what the test pins.
- scripts/upgrade-path: expected daily cache filename -> v21.
This commit is contained in:
iamtoruk 2026-08-19 12:29:33 -07:00
commit f060a2d806
172 changed files with 26871 additions and 552 deletions

View file

@ -0,0 +1,88 @@
name: Build Windows installer
on:
workflow_dispatch:
inputs:
release_tag:
description: Existing desktop-v* release to verify after manual asset upload
required: false
type: string
pull_request:
paths:
- .github/workflows/build-windows-installer.yml
- app/**
- src/**
- scripts/**
- package.json
- package-lock.json
push:
tags:
- 'desktop-v*'
release:
types: [published]
permissions:
contents: read
jobs:
nsis:
if: ${{ github.event_name != 'release' && !(github.event_name == 'workflow_dispatch' && inputs.release_tag != '') }}
runs-on: windows-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
with:
node-version: 22.13.0
cache: npm
cache-dependency-path: |
package-lock.json
app/package-lock.json
- name: Install CLI dependencies
run: npm ci
- name: Install desktop dependencies
run: npm ci --prefix app
- name: Test installer verifier
run: npm --prefix app test -- scripts/verify-windows-installer.test.ts
- name: Build NSIS installer
run: npm --prefix app run package:win
- name: Verify installer manifest
shell: pwsh
run: |
if ($env:GITHUB_REF_TYPE -eq 'tag') {
node app/scripts/verify-windows-installer.mjs --tag $env:GITHUB_REF_NAME
} else {
node app/scripts/verify-windows-installer.mjs
}
- name: Upload installer artifact
uses: actions/upload-artifact@v6
with:
name: CodeBurn-Windows-Installer
path: |
app/release/CodeBurn-Setup-*.exe
app/release/CodeBurn-Setup-*.exe.blockmap
if-no-files-found: error
retention-days: 30
verify-release-assets:
if: ${{ (github.event_name == 'release' && startsWith(github.event.release.tag_name, 'desktop-v')) || (github.event_name == 'workflow_dispatch' && inputs.release_tag != '') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Verify live desktop release assets
env:
GH_TOKEN: ${{ github.token }}
RELEASE_TAG: ${{ github.event.release.tag_name || inputs.release_tag }}
run: |
gh api "repos/${{ github.repository }}/releases/tags/$RELEASE_TAG" \
--jq '[.assets[].name]' > "$RUNNER_TEMP/release-assets.json"
node app/scripts/verify-windows-installer.mjs \
--tag "$RELEASE_TAG" \
--release-assets "$RUNNER_TEMP/release-assets.json"

34
.github/workflows/mac-menubar-ci.yml vendored Normal file
View file

@ -0,0 +1,34 @@
name: macOS Menubar CI
# The macOS menubar (mac/) ships from release-menubar.yml, which only packages the app.
# Its Swift test suite (170+ tests covering the credential stores, Keychain cache,
# serve connection, quota parsing) gated nothing until this workflow. Runs on every
# PR touching mac/** so a red test fails the PR, the same way tests.yml does for the CLI.
on:
push:
branches: [main]
paths:
- .github/workflows/mac-menubar-ci.yml
- mac/**
pull_request:
paths:
- .github/workflows/mac-menubar-ci.yml
- mac/**
permissions:
contents: read
jobs:
test:
runs-on: macos-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v6
- name: Swift toolchain
run: swift --version
- name: Build
run: swift build --package-path mac
- name: Test
run: swift test --package-path mac
- name: Package (same script the release uses)
run: mac/Scripts/package-app.sh ci-smoke

View file

@ -0,0 +1,99 @@
name: Release Windows Menubar
# Triggers on a `windows-v*` tag push (e.g. `git tag windows-v0.9.20 && git push origin
# windows-v0.9.20`), or manually via the Actions tab. Mirrors release-menubar.yml, which
# does the same job for the macOS menubar under the `mac-v*` tags. The produced `.msi` is
# unsigned; users see a SmartScreen prompt on first run until we add signing.
on:
push:
tags:
- 'windows-v*'
workflow_dispatch:
inputs:
version:
description: 'Version label for the bundle (e.g. v0.9.20 or dev-preview)'
required: true
default: 'dev-preview'
permissions:
contents: write # Needed to create the release + upload assets.
jobs:
build:
runs-on: windows-latest
timeout-minutes: 45
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Resolve version label
id: version
shell: bash
run: |
if [[ "${GITHUB_REF}" == refs/tags/windows-v* ]]; then
echo "value=${GITHUB_REF#refs/tags/windows-}" >> "$GITHUB_OUTPUT"
else
echo "value=${{ github.event.inputs.version }}" >> "$GITHUB_OUTPUT"
fi
- uses: actions/setup-node@v6
with:
node-version: 22.13.0
cache: npm
cache-dependency-path: windows/package-lock.json
- uses: dtolnay/rust-toolchain@stable
with:
targets: x86_64-pc-windows-msvc
- uses: Swatinem/rust-cache@v2
with:
workspaces: windows/src-tauri
- name: Install dependencies
working-directory: windows
run: npm ci
- name: Build MSI bundle
working-directory: windows
run: npm run tauri build
- name: Collect artifacts
shell: bash
run: |
set -euo pipefail
mkdir -p release-artifacts
find windows/src-tauri/target/release/bundle -type f -name '*.msi' \
-exec cp -v {} release-artifacts/ \;
(cd release-artifacts && for f in *.msi; do sha256sum "$f" > "$f.sha256"; done)
ls -la release-artifacts
- name: Upload artifact (for manual runs)
if: github.event_name == 'workflow_dispatch'
uses: actions/upload-artifact@v6
with:
name: CodeBurnMenubar-Windows-${{ steps.version.outputs.value }}
path: release-artifacts/*
if-no-files-found: error
- name: Create / update GitHub Release
if: startsWith(github.ref, 'refs/tags/windows-v')
uses: softprops/action-gh-release@v3
with:
tag_name: ${{ github.ref_name }}
name: Windows Menubar ${{ steps.version.outputs.value }}
body: |
Download the `.msi` below and run it. The tray app reads everything through the
CodeBurn CLI, so install that first:
```
npm install -g codeburn
```
Requires codeburn 0.9.9 or newer and the WebView2 Runtime (preinstalled on
Windows 11 and recent Windows 10 updates; installed on demand otherwise).
The bundle is unsigned, so Windows SmartScreen warns on first run: click
"More info", then "Run anyway". Signing is planned.
files: release-artifacts/*
fail_on_unmatched_files: true

View file

@ -9,12 +9,18 @@ jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
# Package floor, and the newest 22.x so paths gated on later node:zlib
# features (zstd, 22.15+) get exercised.
node-version: [22.13.0, 22]
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v4
with:
node-version: 22.13.0
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- name: Typecheck

53
.github/workflows/upgrade-path.yml vendored Normal file
View file

@ -0,0 +1,53 @@
name: Upgrade path
# Every existing user upgrading from the last published CLI (0.9.20) crosses the
# session-cache v7 -> v9 re-layout and the daily-cache v17 -> v19 re-derivation on
# their first run. Unit tests cover the migration in isolation on one platform; this
# job proves it against a cache that the REAL 0.9.20 binary wrote, on all three
# platforms, at both the package floor and the newest 22.x.
on:
pull_request:
paths:
- 'src/**'
- 'scripts/upgrade-path/**'
- '.github/workflows/upgrade-path.yml'
workflow_dispatch:
jobs:
upgrade-path:
runs-on: ${{ matrix.os }}
timeout-minutes: 45
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
# Package floor, and the newest 22.x. The floor matters here: node:zlib
# gained zstd in 22.15, so dsh degrades below it (the corpus writes the
# uncompressed dsh variant so both legs still count the same numbers).
node-version: [22.13.0, 22]
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
# dist/cli.js + dist/parse-worker.js. The dash bundle is not exercised by
# any command this job runs, so the full `npm run build` is not paid for.
- run: npm run build:cli
- name: Upgrade path from codeburn@0.9.20
run: npm run verify:upgrade
env:
# Under runner.temp so the artifact step below can find it. The space
# is deliberate: a real Windows HOME almost always has one.
UPGRADE_PATH_WORK: ${{ runner.temp }}/codeburn upgrade path
- name: Upload payloads on failure
if: failure()
uses: actions/upload-artifact@v4
with:
name: upgrade-path-${{ matrix.os }}-node${{ matrix.node-version }}
path: ${{ runner.temp }}/codeburn upgrade path/payloads
if-no-files-found: ignore

View file

@ -0,0 +1,83 @@
name: Windows Menubar CI
# The Windows menubar (windows/) is a Tauri app: a React frontend plus a Rust binary whose
# interesting code is `#[cfg(windows)]` and therefore only ever compiled on a Windows runner.
# ubuntu-latest is in the matrix because the same crate has to stay clean on the ksni/Linux
# paths and because contributors develop it on non-Windows machines.
on:
push:
branches: [main]
paths:
- .github/workflows/windows-menubar-ci.yml
- windows/**
pull_request:
paths:
- .github/workflows/windows-menubar-ci.yml
- windows/**
permissions:
contents: read
jobs:
check:
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
os: [windows-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v6
# webkit2gtk + libayatana are what the Tauri and ksni crates link against; without
# them the Linux leg cannot even typecheck the Rust side.
- name: Install Linux system dependencies
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y \
libwebkit2gtk-4.1-dev \
libayatana-appindicator3-dev \
librsvg2-dev \
libssl-dev \
libxdo-dev \
libgtk-3-dev \
build-essential
- uses: actions/setup-node@v6
with:
node-version: 22.13.0
cache: npm
cache-dependency-path: windows/package-lock.json
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
with:
workspaces: windows/src-tauri
- name: Install dependencies
working-directory: windows
run: npm ci
- name: Typecheck frontend
working-directory: windows
run: npx tsc --noEmit
- name: Clippy
working-directory: windows/src-tauri
run: cargo clippy --all-targets -- -D warnings
- name: Rust tests
working-directory: windows/src-tauri
run: cargo test
# Release-profile compile of the real Windows binary. `--no-bundle` skips the WiX
# download and MSI packaging, which belong to the release workflow, not to every PR.
- name: Release build smoke
if: runner.os == 'Windows'
working-directory: windows
run: npm run tauri build -- --no-bundle

View file

@ -2,10 +2,25 @@
## Unreleased
### Added
- **`codeburn models --unpriced`.** The dashboard warns about models that price at $0 and points at `codeburn model-alias`, but the list itself was hard to get out of the TUI. This filters the plain-stdout `models` report to exactly those rows, reusing `findUnpricedModels` so local, free, aliased and price-overridden models are treated the same way the warning treats them, and defaulting that mode's min-cost to 0 so $0 rows are not pre-filtered away. Thanks @kocaemre. (#969)
- **`optimize` spots the same long block pasted at the start of many sessions.** The new `recurring-context` detector groups sessions by their opening block — normalized for whitespace and ANSI, hashed over the first 2 KB — and reports a block of at least 1.5 KB that opens 5 or more sessions, with the top three by tokens, their session counts and the project each is confined to. It is a habit, not an apply-able fix: CodeBurn will not move your own text into `CLAUDE.md` for you, so the finding asks Claude to give the block a permanent home (a `CLAUDE.md` rule, or a file read on demand) and hand back a one-line pointer to open sessions with instead. Savings count the repeats only, never the first paste, and are marked `estimated`: provider usage is counted per API call, where the pasted block is mixed in with the system prompt, tool schemas and `CLAUDE.md`, so the block is sized from its own bytes. Injected system reminders and slash-command wrappers are not pastes and are skipped, and neither is a prompt a program wrote — an SDK session or a subagent task — read from the entry's flags, which survive the parser's large-line path. The opening block comes from the session scan that already runs, so nothing extra is read from disk.
- **Applied fixes get re-measured on every `optimize` run, and told plainly whether they worked.** After `codeburn optimize --apply`, every still-applied fix comes back in an `Applied fixes` section on subsequent `codeburn optimize` runs, carrying the verdict `act report` already computes from the same reconciliation: `worked` (at least 70% of its window-scaled estimate realized), `partial` (something, but under that), `no-effect` (no measured reduction, printed with the exact `codeburn act undo <id>` that puts it back), or `measuring` for anything younger than the 3-day measurement window. The numbers are measured — provider-counted usage over the post-apply window — not re-estimated. `--apply` now says when the re-measure will happen, `--format json` gains `appliedFixes[]` (add-only), and the same section appears in the dashboard TUI and the desktop app. New `codeburn optimize --auto-revert` undoes the fixes that measured no reduction at all through the same code path as `codeburn act undo`; it never touches `partial` or still-measuring fixes, and never auto-reverts a `CLAUDE.md` rule (it prints the undo command instead), matching the `--yes` guardrail.
- **Optimize findings say what to do with them and where their number came from.** Every finding now carries a class and a basis, and every surface groups by it: `Fix now (apply-able)` for findings `codeburn optimize --apply` can write itself, `Habits` for the behavioural ones, `FYI` for informational ones whose cost may be justified. A finding only counts as apply-able when a plan can actually be built for that instance, so an `mcp-deferral-off` caused by Vertex policy or a shell-profile override is grouped as a habit rather than promising a fix that does not exist. Alongside it, each finding is marked `measured` (summed from provider-counted usage) or `estimated` (a schema-size or recovery-fraction model), with the split reported in the header as `N measured · M estimated` in place of the blanket "Estimates only." footer. Sessions whose cost the provider never reported are kept out of the `cost-outliers` peer comparison, and a provider that only ever estimates gets the finding marked `estimated` rather than dropped. `--format json` gains `class` and `basis` per finding plus `summary.measuredSavingsUSD` (existing fields unchanged), and the new `docs/optimize.md` covers what is scanned, exactly what `--apply` may write, and how to read the health grade.
- **`CODEBURN_CACHE_SCOPE=all` forces a full session-cache read.** A ranged query reads only the month shards that can contribute a turn to it, which is a real behaviour change on a warm cache; this is the escape hatch for the case where a number looks wrong and you want to know whether the scoped read is why. Set it and every load ignores its scope and reads every shard, one-shot runs and the resident `codeburn serve` alike. It is a read policy, not an input to any cache fingerprint: setting or unsetting it re-parses nothing and invalidates nothing.
### Added (Windows)
- **`codeburn menubar` installs and launches the tray app on Windows.** The same command that installs the macOS menubar now does the Windows one, through the same pinned-release path: it resolves `windows-v<cliVersion>`, falls back to a scan of the newest `windows-v*` release carrying both assets when that tag has none, downloads the `.msi` with the same retry and backoff, and verifies its sha256 before anything executes it — a mismatch aborts without ever handing the file to the installer. It then runs `msiexec` out of `%SystemRoot%\System32` (never a bare name, so nothing dropped next to the CLI can impersonate it) with `/i <msi> /passive /norestart`, treats exit 3010 as installed-pending-restart and 1602 as a cancelled install rather than failures, and launches the exe named by the product's Uninstall registry key. An already-installed matching version skips the download and just launches; `--force` reinstalls.
- **A menubar app for Windows.** `windows/` is a Tauri 2 tray app — Rust binary, React popover — that puts today's spend in the notification area and mirrors the macOS menubar screen for screen: agent tabs, period switcher, Trend, Forecast, Pulse, Stats and Plan insights, activity and model breakdowns, optimize findings, CSV/JSON export, launch at login, currency, and theme. Windows has no menubar title, so the number lives in a second tray icon rendered from the system font at the panel's native icon size (Settings can turn it off; the tooltip always carries it). It reads everything through the CLI like the macOS and GNOME clients do, and gates on **codeburn 0.9.9 or newer** — the first release accepting `status --format menubar-json --no-optimize` — showing a setup screen with the install command until it finds one. Refresh follows popover visibility the way the macOS app does: 60 s with optimize findings while open, 2 minutes for today's total while closed, and immediately on open when what you are looking at has gone stale. The Claude quota view never spends Claude's single-use refresh token; on a 401 it re-reads Claude Code's own credential file for a token it has already rotated, matching the macOS client. Ships as an unsigned `.msi` from the `windows-v*` tag, which `codeburn menubar` now installs for you. The same crate still builds and runs a tray on Linux, but that stays experimental and unreleased — `gnome/` is the supported Linux surface.
### Added (CLI)
- **Copilot input/cache tokens are read per request from `~/.copilot/session-store.db`.** Previously, codeburn relied on `session.shutdown` rollups from the Copilot CLI and GitHub Copilot desktop app. Those rollups are written only after a clean shutdown, stamp all usage on the shutdown day, and reset their counters at in-session compaction — so a crash could lose an entire session's input/cache usage, and even cleanly-closed long sessions were silently truncated. On one machine with long history, reading the per-request rows recovered about 35% of actual Copilot spend. Covered sessions now use per-request tokens with their real timestamps, counted exactly once against existing rollups and never added as extra calls or turns. Pre-store CLI sessions continue using the unchanged rollup path, and a locked or unreadable store defers only its own re-read instead of prematurely sealing daily history. Copilot reasoning tokens are also no longer double-billed: they are a subset of output already priced through the per-turn calls. This triggers a one-time re-parse, with the daily cache bumped from v17 to v19 to re-derive finalized days. (#946)
- **Copilot input/cache tokens are read per request from `~/.copilot/session-store.db`.** Previously, codeburn relied on `session.shutdown` rollups from the Copilot CLI and GitHub Copilot desktop app. Those rollups are written only after a clean shutdown, stamp all usage on the shutdown day, and reset their counters at in-session compaction — so a crash could lose an entire session's input/cache usage, and even cleanly-closed long sessions were silently truncated. On one machine with long history, reading the per-request rows recovered about 35% of actual Copilot spend. Covered sessions now use per-request tokens with their real timestamps, counted exactly once against existing rollups and never added as extra calls or turns. Pre-store CLI sessions continue using the unchanged rollup path, and a locked or unreadable store defers only its own re-read instead of prematurely sealing daily history. Copilot reasoning tokens are also no longer double-billed: they are a subset of output already priced through the per-turn calls. This triggers a one-time re-parse, with the daily cache bumped to v21 to re-derive finalized days. (#946)
- **DeepSeek Harness (`dsh`) is now a supported provider.** Reads DeepSeek's open-source agent harness from `~/.dsh/sessions` (`DSH_HOME` relocates the root), both the default zstd logs and the uncompressed `session.jsonl` variant. A `.zstd` log is a concatenation of independent zstd frames, one per write batch, so it is decoded frame by frame behind a structural frame scan and a torn trailing frame from a crashed writer is ignored rather than failing the file (needs Node 22.15+ for `zlib` zstd; below that dsh is skipped with a notice instead of counted as $0). One call per `(turn, step)`, with the step's final `assistant/message` usage superseding the streamed `assistant/chunk` sample of the same call rather than adding to it, the model taken from the message that served the step, and reasoning tokens billed at the output rate. DSH records tokens but no cost, so calls are priced from the shared tables. The events a forked session replays from its parent are skipped, since codeburn already counts the parent's own log. The session format is pinned at version 0 upstream with no compatibility implied, so a log stamped with any other version is skipped with a notice instead of read under today's assumptions.
### Changed
- **SQLite providers now survive read-only database parents.** A read-only SQLite open is not read-only on disk: on a WAL database SQLite must create `<db>-shm` and `<db>-wal` in the database's own directory, so a source on read-only media, under restrictive permissions, or inside a Flatpak/snap confinement failed with `attempt to write a readonly database` (or `unable to open database file` when a `-wal` was present without its `-shm`), and both discovery sites swallowed it — the provider read as "not installed" rather than as an error. That covers cursor, cursor-agent, opencode, goose, warp, kilo-code, zerostack and the copilot agent-traces database. The direct open stays the fast path and is byte-identical when it succeeds. When it fails for want of sidecars: a database with no WAL frames to lose is opened in place with `immutable=1`, which costs nothing and cannot go stale; a database with a non-empty `-wal` is copied with its `-wal` into the CodeBurn cache and read there, so its un-checkpointed rows are never silently dropped. The copy costs one database's worth of disk and is taken once per change — it is keyed by the main-plus-WAL fingerprint, published under a fingerprint-stamped name so a refresh never overwrites a copy another process is reading, and superseded copies are evicted once a day has passed without a read, keeping at most one predecessor. If the cache itself cannot be written, the database is skipped with a notice naming it and the reason rather than in silence. The original provider database is never opened writable or modified.
- **Grok Build now reads the CLI's own completed-turn usage instead of estimating it.** Usage comes from the `turn_completed.usage` records Grok CLI already writes into `updates.jsonl` (`inputTokens`, `outputTokens`, `cachedReadTokens`, `cacheCreationTokens`, `reasoningTokens`), deduplicated by `prompt_id` and emitted as one session-level call from the top-level totals. The previous parser reconstructed an estimate from the running `_meta.totalTokens` context counter, so **existing Grok totals will change materially on upgrade** - on one real 568-session corpus cache-read went from 150K to 96.3M tokens, total tokens from 20.0M to 113.9M, and cost from $36.98 to $56.79. Cache read and cache creation are subsets of input and reasoning is a subset of output, so reasoning is clamped to the record's reported output and split back out to match this repo's exclusive-reasoning contract. `modelUsage` only selects a priced attribution id; multi-model rate attribution stays out of scope, so one session is priced at one model's rate. `costUsdTicks` is ignored because its scale is undocumented. Sessions with no usable record - older CLI versions - keep the old context-curve heuristic and stay flagged estimated. **In a session that has at least one `turn_completed` record, turns without one are not counted at all** (their tokens are dropped rather than estimated), and the session is marked estimated instead of claiming full provider coverage. Cached Grok sessions re-parse once. The daily cache re-derives once on first run after upgrade: this is a global re-derivation of every day and every provider, since the daily cache has no per-provider invalidation, but it reads the warm session cache rather than re-parsing transcripts, so it costs seconds (~3s on the corpus above), and the superseded cache file is retained on disk as the baseline for days no source can still re-derive. (#998)
- **Codex rollouts parse across worker threads too, and the workload gate now takes bytes or files.** Codex is the bigger half of a real cold parse — a 4 GB rollout corpus against 1.8 GB of Claude sessions — and it was still decoding one file at a time. A whole-file rollout decode now runs on the same pool, against an empty dedup set, and comes back with the calls, the dedup keys it claimed, and the codex-cache entry it would have written; the parent installs all three in the serial loop's order, so `codex-results.json` and every payload come out byte-identical to a serial run. Cross-file state stays where it was: a forked rollout replaying its parent's token_count history collides on the parent's keys and is re-parsed in-process, and no worker ever touches the cache module's per-directory state. Files the Codex cache can serve exactly or resume into from a byte offset never reach a worker — they read a few KB and the resume state belongs to the parent. The workload gate is now pending BYTES alone (200 MB), not file count: 250 pending files holding under a megabyte between them spawned threads that made the run ~5% slower, while a few hundred huge rollouts were being turned away. The count takes `max(pendingFiles / 50, pendingBytes / 200 MB)`, and the per-thread memory budget is derived per parse as `clamp(256 MB, 2 × average pending file + 128 MB, 1 GB)` rather than a flat 256 MB — a 260 MB rollout peaks near 430 MB in its worker and scales linearly with the pool, so the flat figure over-subscribed exactly the workload this adds. The decision is per provider, and at most one pool is alive at a time.
- **A large cold Claude parse now runs across worker threads.** Reading, decoding and line-parsing a session JSONL is per-file work that never touches anything shared, so it moves onto `worker_threads`; each worker ships its parsed turns back as a JSON string and the parent installs them in the exact order the serial loop would. Everything with cross-file state — the streaming-message dedup, canonical project paths, spawn links, PR correlation, progress saves — stays on the main thread, and a file whose message ids were already claimed by an earlier file (or whose worker failed) is simply re-parsed in-process, so the session cache and every payload are identical either way. On a 6 GB corpus a cold `status` drops from 27.5s to 14.8s with peak RSS up 2.27 GB → 2.52 GB. Threads only engage for a genuinely large cold parse: never with under 200 MB behind the pending whole-file re-parses, 2 or fewer cores, or under 4 GB of available memory — so warm and incremental runs are untouched and spawn nothing. Otherwise the count is `min(cores - 1, min(0.25 × available, 2 GB) / 256 MB, pendingFiles / 50)`, where available is `process.availableMemory()` (cgroup-aware in containers) rather than free memory, which on macOS reports free pages and would switch the feature on and off between runs. `CODEBURN_PARSE_WORKERS=0` forces the serial parse and `CODEBURN_PARSE_WORKERS=N` forces N (capped at the core count), both bypassing every gate; `CODEBURN_VERBOSE=1` prints the resolved count and why.
- **A warm launch rewrites only the month that changed, and a ranged query reads only the months it can report on.** Per-provider shards still meant one appended session republished that provider's entire history — 95 MB for Claude on a 6 GB corpus. Each provider's shard is now split again by the UTC month of the cached session's FIRST turn, a bucket that never moves as a session grows, so an append rewrites one month. Every shard records the newest month it holds, which lets `--period today/week` skip the shards that cannot contribute a turn to the range; the skipped months stay on disk untouched across the save, and providers whose cache is the only surviving record (durable) or whose parse fingerprint moved are always read in full. Remaining shards are read concurrently. Existing v8 and v7 caches are re-laid-out losslessly on first load and the old layout removed once the new one is published: nothing re-parses.
@ -15,9 +30,22 @@
- **One rule for every cache file.** `CODEBURN_CACHE_DIR` when set, otherwise `~/.cache/codeburn`. `XDG_CACHE_HOME` is no longer consulted; the sync ledger, the only file that ever honored it, is merged into the canonical location on first read and the legacy copy is retired, so nothing is re-uploaded after the move. (#972)
### Fixed (Desktop & Menubar)
- **The menubar's copies of your Claude and Codex credentials move out of Application Support and into the login Keychain.** Connecting a provider used to leave the copied OAuth material in `~/Library/Application Support/CodeBurn/*-credentials.v1.json`, written world-readable (0644) because macOS ignores `.completeFileProtection` outside iOS. The copy now lives in a CodeBurn-owned login-Keychain item, and the first read after upgrading migrates the old file: it is reopened with `O_NOFOLLOW`, refused if it is a symlink or not owned by you, repaired to 0600 before a single secret byte is read, written to the Keychain, read back and compared, and only then unlinked — a failed or unverified write leaves the (now 0600) file in place so a retry can still find it, and the next read retries the cleanup. Where both a Keychain item and an old file exist, the one that expires later wins before anything is removed, so an item left behind by a much older build cannot displace a fresher token. Claude's entry no longer stores a refresh token at all — the CLI owns that grant and the menubar never spends it — and any refresh token in a historical blob is dropped on read. Disconnect only reports success once the material is actually gone; if the delete fails it says so and leaves the provider connected so you can retry. Keychain reads are non-interactive and are skipped outright while the login Keychain is locked, so a background quota refresh can never raise an unlock panel. (#1037)
- **First launch no longer asks to control System Events.** The macOS menubar registered its login item by driving System Events over AppleScript, which made macOS put up an Automation consent dialog the first time the app ran. It now registers itself through `SMAppService.mainApp`, an in-process call that needs no Automation grant; there is no AppleScript fallback, so a failure logs and leaves the login item unset rather than bringing the prompt back. The same `codeburn.loginItemRegistered` guard still limits this to the first launch, so a login item you removed by hand stays removed. (#1026)
- **The resident `codeburn serve` child.** The first real panel request is also the cache warm-up, so startup never runs an artificial warm-up query beside a duplicate one-shot child; each served command carries its own read-only option allowlist, and anything outside it falls back to a normal spawn; the child exits when its stdin closes, so it can never outlive the app. Requests whose response exceeds the 16 MiB frame limit still replace the child, but that deliberate kill no longer spends the resident's unexpected-death budget. (#972)
### Fixed
- **MiMo sessions price from the LiteLLM Xiaomi rows, and MiMo v2 Flash no longer crashes the display path.** Hermes / Xiaomi token-plan sessions store the bare id (`mimo-v2.5-pro`, `mimo-v2.5`) while LiteLLM namespaces its row (`xiaomi/…`), so those models reported $0. They now alias to the existing snapshot rows — no invented rate, and `kimi-k3` still has none — which means a session Hermes left costless is priced from the shared tables and carries the estimated marker, exactly as `mimo-v2-flash` already did. The same change fixes a **pre-existing** crash that this alias did not introduce: the shipped `mimo-v2-flash -> xiaomi/mimo-v2-flash` alias already cycled through display-name resolution — strip the namespace, alias it back, take the leaf, repeat — so `getShortModelName` blew the stack on any real MiMo v2 Flash session and took every surface that names a model down with it, the `models` table included. Display-name resolution is now cycle-safe, and the `mimo-v2-flash` and `mimo-v2.5` rows are named rather than shown as raw slugs.
- **A date-ranged run no longer republishes the month shards it never read.** A scoped load leaves an out-of-range month on disk, so the files it holds have no visible cache entry and the reconcile re-parses them — re-deriving the entry the shard already stores. That re-parse marked the unloaded month dirty, and the save merged and republished it under a fresh nonce name on every single run, byte-identical content and all, so a repeated `codeburn status --format json` churned old months (on a real corpus: claude/2026-03, cursor/2026-02 and warp/2026-03 renamed every run) and left the retired shards for the sweeper. A merge into an unloaded month that neither adds, changes nor removes an entry now keeps the published shard, so unchanged months keep their names and their bytes. (#1032)
- **`models` and `audit` no longer show two identical `Grok 4.5` rows.** `grok-4.5-build` — the Grok Build harness's variant id — fell into the `grok-4.5` display entry by prefix, and since rows bucket by model id, not display name, the two came out as visually identical rows with different numbers. The variant now shows as `Grok 4.5 (build)`. Display only: no id is rewritten and no cost moves. (#1029)
- **An upgrade no longer loses history for days whose transcripts have only PARTLY aged out.** The never-lose contract carried a cached (day, provider) slice forward only when the re-derivation found NOTHING for it, but transcripts expire per FILE rather than per day: on a day whose sources are mostly gone, a handful of turns from surviving later files still bucket onto it, so the fresh slice came back non-empty but truncated and REPLACED the full cached one. On a real cache upgrading from the last shipped daily-cache version, 2026-07-16 fell from $1,685.17 / 12,530 calls to $385.44 / 560 calls, and 13 days lost $2,765.75, 19,209 calls and 520 sessions in total. A fresh slice now replaces a settled baseline slice only when it carries at least as many CALLS - the same or more evidence; fewer calls means the source set demonstrably lost data, and the baseline is kept whole. The comparison is on calls alone: cost and tokens are re-priced accounting on the same evidence, which is exactly what a legitimate re-derivation changes (the Grok accounting fix keeps its per-day calls and is unaffected), and session counts drift down by a few on days whose sources are entirely intact. Days inside a 7-day settle window stay authoritative - their session files are still on disk, so a shrink there is a real change rather than expiry. The trade-off is deliberate and matches the direction this cache has always chosen: a future fix that legitimately REDUCES calls on a settled day keeps the older, higher value until that day is re-derived at an equal or greater call count. The timezone-change re-derive gets the exact form of the same rule - what the fresh parse can no longer explain under the old bucketing is added on top of the fresh slice instead of being dropped - and the cross-file adoption union is unchanged, where the newer schema still wins per (day, provider).
- **The session chart legend now leads with a visible session disambiguator and title instead of the project path.** Every series in a monorepo shared the same project prefix, so the only thing separating them was a truncated hex fragment — and per-application cost attribution is the main reason to open that chart. `SessionSummary.title` is already parsed and already rendered in the Context tab; the legend now puts the short session id first, prefers the title, and falls back to the previous project-based label when a session never produced one. Titles come from transcripts, so they are stripped of ANSI and control characters and capped before they reach either the legend or the tooltip. (#997)
- **Context-bloat detection now counts reasoning tokens as generated output.** `detectContextBloat` divided context by `totalOutputTokens` alone, but reasoning is stored beside output rather than inside it, so for every reasoning-bearing provider the detector saw a fraction of the tokens actually generated and invented findings - a session whose real ratio was 20:1, under the 25:1 threshold, was reported as 133:1 and "high impact". It now uses the same `output + reasoning` sum the reports use, which corrects grok, codex, kiro, hermes, qwen and cursor-agent alike.
- **The unpriced-models warning in the dashboard is now readable at every terminal width.** It lived in a fixed-width panel with an inline model list and a fix command, so it clipped mid-name at 80 columns and clipped *earlier* at 200, where the three-column layout narrows each panel - neither the affected models nor a runnable command survived. The panel line is now a pointer, `! N unpriced: codeburn models --unpriced` (shortened to `! N: codeburn models --unpriced` below 45 columns of panel), and the model list moves to that command's plain output, which is full width, copyable, and lists every model rather than the first two. The command's hint no longer reads as an unconditional instruction to alias: a subscription or flat-rate model is correctly $0, and mapping it onto another model's per-token rate would invent spend that was never billed. Provider-supplied model IDs are now stripped of terminal control characters in every human-readable report rather than only on the unpriced path, and `--unpriced` shows raw IDs instead of friendly names because `model-alias` keys on the raw ID. (#969)
- **`codeburn models --unpriced --top N` returned nothing for a `--top N` smaller than the number of priced models.** `--top` is applied inside `aggregateModels`, before the unpriced filter, on rows sorted cost-first — and unpriced rows are $0 on both, so they sorted last and the slice removed exactly the rows the flag exists to show. A user with unpriced models was told they had none. The slice now runs after the filter — and after ranking, because unpriced rows tie at $0 on both keys, so slicing them in aggregate order kept whichever models happened to appear earliest in the transcript rather than the largest. The order now matches the one the unpriced-models warning shows. (#969)
- **Old durable sources remain visible while they still exist.** The 90-day session-cache age-out now applies only after a durable source disappears from discovery, so an unchanged older Copilot source keeps reporting usage and reuses its persisted fingerprint instead of being reparsed and immediately discarded. (#987) On long-lived machines this makes previously dropped history reappear, so lifetime totals can jump once after upgrading.
- **`optimize` no longer treats subagent transcripts as your sessions.** Claude Code writes each subagent's transcript to its own `subagents/agent-*.jsonl` file with `isSidechain: true` on every entry, and optimize counted each one as a user-started session. That inflated the session count in the header and fed the session-level detectors a population that fails their tests by construction: a sidechain is handed a large context and returns a short answer (context-heavy), and it never commits or opens a PR because its parent does (low-worth). Excluded from sidechains now: the header session count, the `low-worth-sessions`, `context-bloat`, `cost-outliers` and `capability-reliability` detectors, the coaching notes, the file-churn table, the median time-to-first-edit, the worst one-shot category, and the model-default recommendation - plus `duplicate-reads`, because a subagent starts on a fresh context and re-reading what its parent read is a necessary read, not a repeat. Everything else keeps the full population: `build-folder-reads` and `read-edit-ratio` still count calls made inside a sidechain, since reading `node_modules` or editing without reading is the same waste whoever does it and the `CLAUDE.md` rule they suggest binds subagents too, and so do the MCP, cache-bloat, ghost-command and configuration-overhead findings. Classification is sticky across the whole file, so calls that appear before the first marked entry are reclassified too, and `isSidechain` now survives the compact parser's 32 KB large-line path and warm-cache range rebuilds. Nothing is deleted from spend: sidechain tokens, calls and cost stay in every total and in `status`, and the optimize result cache keys on sidechain identity so a run cannot be served a pre-fix result. Absent markers still read as user-started, so no cache re-parse is needed. (#974)
- **`optimize` no longer offers `claude mcp remove` for claude.ai connectors, and its MCP schema-cost estimate is per session.** A `claude_ai_*` namespace that no readable local MCP config claims is a claude.ai connector, managed through `/mcp` or claude.ai Settings rather than as a local MCP server (a local server that carries the prefix keeps its removal command and gains a same-name connector note); low-coverage findings now render them as a manual follow-up and build `--apply` plans only for exact local server names found in readable MCP config, so mixed findings remove only the local subset and the "apply-able" subtotal counts only that subset. The same change replaces the old global schema-cost cap with per-session, per-server proportional attribution — a more accurate model that lowers `mcp-low-coverage` estimates for everyone, connectors or not (on a large corpus roughly by half). (#975, #991)
- **Bash command splitting was quadratic on long whitespace-heavy commands.** The separator regex retried its leading `\s*` from every offset; matching the separator alone and widening over whitespace by hand makes cold parse ~24% and warm ~40% faster on large corpora, output unchanged.
- **Cold parse no longer retains full message bodies through cached previews.** `flatSlice` skipped its Buffer round-trip for strings already within the bound, but provider adapters pre-truncate user-message previews with `.slice(0, 500)` before the cache-site call — those pre-sliced views are still V8 SlicedStrings pinning their large parent, so the retention that OOM'd cold parses of large histories survived. The round-trip now always runs.
- **Kiro sessions carry the real `projectPath`** (CLI meta.cwd, v2 `workspacePaths[0]`, workspace sessions' `workspaceDirectory`), so git-repo attribution can resolve them; previously they were attribution-blind. Bumps the kiro parse version, so the first run after upgrade re-parses kiro history once, and kiro sessions in linked git worktrees now group under the main repo.

View file

@ -25,7 +25,7 @@
<a href="https://github.com/sponsors/iamtoruk"><img src="https://img.shields.io/badge/sponsor-♥-F97316?logo=github" alt="Sponsor" /></a>
</p>
<p align="center">If CodeBurn shows you something your bill never did, <a href="https://github.com/getagentseal/codeburn/stargazers">star the repo</a> so other developers find it, and consider <a href="https://github.com/sponsors/iamtoruk">sponsoring</a> to keep 40 integrations honest.</p>
<p align="center">If CodeBurn shows you something your bill never did, <a href="https://github.com/getagentseal/codeburn/stargazers">star the repo</a> so other developers find it, and consider <a href="https://github.com/sponsors/iamtoruk">sponsoring</a> to keep 41 integrations honest.</p>
<table align="center">
<tr>
@ -52,20 +52,21 @@
<code>npx codeburn</code>
</td>
<td align="center" width="50%">
<strong>macOS Menubar</strong><br/>
<strong>Menubar</strong><br/>
<img src="https://raw.githubusercontent.com/getagentseal/codeburn/main/assets/menubar-app.jpg" alt="CodeBurn macOS menubar" /><br/>
<code>codeburn menubar</code>
<code>codeburn menubar</code><br/>
<a href="https://github.com/getagentseal/codeburn/releases/tag/windows-v0.9.20"><img src="https://img.shields.io/badge/Windows-Tray_app_(.msi)-F97316?logo=windows&logoColor=white" alt="Download the CodeBurn Windows menubar" /></a>
</td>
</tr>
</table>
<p align="center"><em>Four surfaces, one source of truth: everything reads the session files already on your disk.</em></p>
**CodeBurn is a free, open-source, local-first tool that tracks AI coding token usage and cost across 40 tools and agents (Claude Code, Cursor, Codex, Gemini, Grok and more), broken down by model, project, and task.**
**CodeBurn is a free, open-source, local-first tool that tracks AI coding token usage and cost across 41 tools and agents (Claude Code, Cursor, Codex, Gemini, Grok and more), broken down by model, project, and task.**
You pay for Claude, Codex, Cursor, and a stack of other AI tools. The bill tells you the total. It never tells you that half of it went to conversation instead of code, or that an expensive model burned your budget on work a cheaper one would have one-shot.
CodeBurn does. It reads the session files your tools already write to disk and breaks down every token and dollar by **task, model, tool, and project**, across **40 AI tools**.
CodeBurn does. It reads the session files your tools already write to disk and breaks down every token and dollar by **task, model, tool, and project**, across **41 AI tools**.
Everything runs locally. No wrapper, no proxy, no API keys, nothing leaves your machine. Pricing comes from [LiteLLM](https://github.com/BerriAI/litellm), refreshed daily.
@ -107,7 +108,7 @@ Also runs via `bunx codeburn` or `pnpm dlx codeburn`, or `brew install codeburn`
codeburn menubar
```
On Linux, a GNOME Shell extension gives the same panel view; see [Linux (GNOME)](#linux-gnome).
The same command installs the tray app on Windows; see [Windows](#windows). On Linux, a GNOME Shell extension gives it in the top panel; see [Linux (GNOME)](#linux-gnome).
Requires **Node.js 22.13+** and at least one supported tool with session data on disk. For Cursor and OpenCode, `better-sqlite3` installs automatically.
@ -156,6 +157,14 @@ codeburn optimize --format json # setup health + findings as JSON
`codeburn optimize` scans your sessions and your `~/.claude/` setup for waste patterns:
For Claude Code, the optimize session count, the per-session findings, coaching,
and model-default recommendations use user-started (main) sessions. Subagent
sidechain transcripts are excluded from that population because their delegated
context and delivery behavior are structurally different, and so is the re-read
finding, since a subagent starts on a fresh context. Findings about how Claude
uses tools (junk reads, read:edit ratio) and every spend, MCP, and
configuration-overhead finding keep counting them.
- Files Claude re-reads across sessions (same content, same context, over and over)
- Low Read:Edit ratio (editing without reading leads to retries and wasted tokens)
- Wasted bash output (uncapped `BASH_MAX_OUTPUT_LENGTH`, trailing noise)
@ -167,6 +176,12 @@ codeburn optimize --format json # setup health + findings as JSON
- Possibly low-worth expensive sessions with no edit turns or repeated retries
when no `git`/`gh` delivery command is observed
Findings are grouped into three classes: **Fix now** (CodeBurn can apply it for you), **Habits**
(you change how you drive the next session), and **FYI** (informational, the cost may be justified).
Each one says whether its savings number is `measured` from provider-counted usage or `estimated`
from a model. See [docs/optimize.md](docs/optimize.md) for what is scanned, what `--apply` may write,
and how to read the health grade.
Each finding shows the estimated token and dollar savings plus a ready-to-paste fix: a `CLAUDE.md` line, an environment variable, or a `mv` command to archive unused items. Findings are ranked by urgency (impact weighted against observed waste) and rolled up into an A to F setup health grade. Repeat runs classify each finding as new, improving, or resolved against a 48-hour recent window.
You can also open it inline from the dashboard: press `o` when a finding count appears in the status bar, `b` to return.
@ -180,11 +195,12 @@ codeburn optimize --apply --yes # apply every appliable fix without prompt
codeburn act list # every change CodeBurn has made
codeburn act undo --last # roll the most recent change back
codeburn act report # realized vs estimated savings
codeburn optimize --auto-revert # undo the applied fixes that measured no reduction
```
`codeburn optimize` finds the waste; `--apply` fixes the config-class findings for you: settings values, environment variables, archiving unused agents and skills. Every change is backed up and journaled before it lands. `codeburn act list` shows the history and `codeburn act undo <id>` restores the original files (it refuses if the files changed since being applied, unless you pass `--force`).
The loop closes on honesty: once an applied fix is at least 3 days old, `codeburn act report` compares its estimated savings against what your sessions actually did, and later `codeburn optimize` runs show that realized figure in the header. Estimates get checked against reality, not just claimed.
The loop closes on honesty: once an applied fix is at least 3 days old, `codeburn act report` compares its estimated savings against what your sessions actually did, and every later `codeburn optimize` run lists it under `Applied fixes` with a plain verdict — worked, under its estimate, or did not help, with the undo command for that last case. `--auto-revert` undoes the ones that did nothing (never `CLAUDE.md` rules). Estimates get checked against reality, not just claimed.
## Guard your budget
@ -281,6 +297,8 @@ Pairing is PIN-authorized and stays on your local network. You can also discover
## Menu bar
### macOS
```bash
codeburn menubar
```
@ -321,6 +339,20 @@ defaults write org.agentseal.codeburn-menubar CodeBurnPreferredTerminal -string
Allowed values are `terminal` (macOS Terminal.app, the default) and `iterm2`. Anything else falls back to `terminal`. Only terminals that can script a command into a live window are offered; if the chosen app is missing or fails to accept the command, CodeBurn tries Terminal.app and then runs the command in the background, logging each step to Console.app. Takes effect on the next launch of a command, no relaunch needed.
### Windows
Windows gets the same ambient view from the system tray, from the same one command:
```powershell
codeburn menubar
```
It downloads the `.msi` for your CLI version, verifies its sha256, runs it through `msiexec /passive`, and launches the tray app. Re-run with `--force` to reinstall; an already-installed matching version is just launched. You can also download the `.msi` yourself from the [latest Windows Menubar release](https://github.com/getagentseal/codeburn/releases/tag/windows-v0.9.20).
Today's spend sits in the tray as a number beside the flame icon (turn it off in Settings, and the tooltip always carries it). Click for the same popover the macOS app shows: agent tabs, period switcher, Trend, Forecast, Pulse, Stats and Plan insights, activity and model breakdowns, optimize findings, and CSV/JSON export. Settings covers launch at login, the tray number, theme, and currency. It refreshes every 60 seconds while the popover is open and every 2 minutes while it is closed.
The tray app reads everything through the CLI, so install that first (`npm install -g codeburn`) — it needs **codeburn 0.9.9 or newer**, and shows a setup screen with the install command until it finds one. Source and build instructions are in [`windows/`](windows/) ([windows/DEVELOPMENT.md](windows/DEVELOPMENT.md)). The `.msi` is unsigned for now, so SmartScreen prompts on first run.
### Linux (GNOME)
Linux gets the same ambient view through a GNOME Shell extension (GNOME 45+): spend in the top panel, period switcher, compact mode, and daily budget alerts. It lives in [`gnome/`](gnome/):
@ -331,7 +363,7 @@ git clone https://github.com/getagentseal/codeburn && cd codeburn/gnome
gnome-extensions enable codeburn@codeburn.dev
```
See [gnome/README.md](gnome/README.md) for settings and development notes. On Windows, `codeburn web` is the always-on view for now.
See [gnome/README.md](gnome/README.md) for settings and development notes. The Tauri tray app in `windows/` also builds and runs on Linux, but it is experimental and unreleased there — the GNOME extension is the supported Linux surface.
## CodeBurn in your agent (MCP)
@ -484,6 +516,7 @@ Sync sends token counts, costs, models, and projects, never prompts or code. Thi
| `codeburn models --by-task` | Break each model into per-task-type rows |
| `codeburn models --by-agent` | Break each model into per-agent rows: which agent drove which model's spend (`(main)` covers non-agent sessions; `--min-cost 0` shows sub-cent agents) |
| `codeburn models --top 10` | Only the 10 most expensive models |
| `codeburn models --unpriced` | Only models with usage that currently price at $0 — the copyable form of the unpriced-models warning. Shows raw model IDs (not friendly names) so they can be pasted into `model-alias`; JSON keeps them exact |
| `codeburn models --format markdown` | Emit a paste-friendly markdown table |
| `codeburn models --task feature` | Filter to feature-development work |
| `codeburn models --provider claude` | Filter to a single provider |
@ -683,6 +716,7 @@ These are starting points, not verdicts. A 60% cache hit on a single experimenta
| **Cline / Roo Code / KiloCode** | VS Code `globalStorage` across VS Code, VS Code Insiders, and VSCodium (Cline at `saoudrizwan.claude-dev`, plus `~/.cline/data`) | Cline-family agents. CodeBurn reads `ui_messages.json` from each task directory, extracting token counts from `type: "say"` entries with `say: "api_req_started"`. |
| **Cline CLI** | `~/.cline/data/sessions/<session-id>/` (honors `CLINE_SESSION_DATA_DIR`, `CLINE_DATA_DIR`, `CLINE_DIR`) | The Cline command-line agent, whose layout is unrelated to the VS Code extension's. Reads `<session-id>.json` for session metadata and the rolled-up `usage`, and `<session-id>.messages.json` for the per-message `metrics` block (input, output, cacheRead, cacheWrite, cost) that becomes one call each. |
| **CodeWhale** | `~/.codewhale/sessions/*.json` plus unmigrated legacy `~/.deepseek/sessions/*.json`; `$CODEWHALE_HOME/sessions` is an exact override | Emits one cumulative record per saved session. CodeWhale exposes only `total_tokens`, so CodeBurn preserves that aggregate in the input column rather than inventing an input/output split. Cost is the exact stored parent-session plus subagent USD total; model pricing is used only when the cost snapshot is absent. Tool blocks, shell commands, skills, and subagent types are retained. |
| **DeepSeek Harness** (`dsh`) | `~/.dsh/sessions/--<slug>--/<session-id>/session.jsonl.zstd` (or `session.jsonl` when compression is off); `DSH_HOME` relocates the root | DeepSeek's open-source agent harness, unrelated to the CodeWhale desktop app. The `.zstd` log is a concatenation of independent zstd frames (one per write batch), decoded frame by frame; needs Node 22.15+. One call per `(turn, step)`, with usage from the step's `assistant/message` (the streamed `assistant/chunk` sample is a draft of the same call, never a second one). DSH records tokens but no cost, so calls are priced from the shared tables with reasoning billed at the output rate. |
| **IBM Bob** | `User/globalStorage/ibm.bob-code/tasks/<task-id>/` (GA `IBM Bob` and preview `Bob-IDE` app folders) | Reads `ui_messages.json` for API request token/cost records and `api_conversation_history.json` for the selected model. |
| **Kimi Code CLI** | `$KIMI_SHARE_DIR/sessions/<workdir-hash>/<session-id>/` or `~/.kimi/sessions/<workdir-hash>/<session-id>/` | Reads `wire.jsonl` `StatusUpdate.token_usage` records, mapping `input_other`, `input_cache_read`, `input_cache_creation`, and `output` into the standard token columns; includes subagents under each session's `subagents/` folder. |
| **LingTai TUI** | `~/.lingtai/<agent>/logs/token_ledger.jsonl` plus project homes from `~/.lingtai-tui/registry.jsonl` (`<project>/.lingtai/<agent>/logs/token_ledger.jsonl`); honors `LINGTAI_HOME` / `LINGTAI_TUI_HOME` | Reads LingTai's append-only token ledger, mapping `input - cached` to fresh input, `cached` to cache reads, `output` to output, and `thinking` to reasoning. Nested daemon ledgers are skipped because parent ledgers already mirror daemon usage with `source`/`run_id` tags. |
@ -722,12 +756,12 @@ CodeBurn deduplicates messages (by API message ID for Claude, by cumulative toke
CodeBurn is free, runs entirely on your machine, and exists to cut your AI bill. If it has already saved you more than a sponsorship costs, consider sending a little of that back.
Keeping 40 integrations accurate is constant work. The tools underneath change every week: Cursor reshapes its database, Claude moves a config path, new models ship at new prices. Sponsorship keeps CodeBurn current with all of it, so the numbers you see are always the real ones.
Keeping 41 integrations accurate is constant work. The tools underneath change every week: Cursor reshapes its database, Claude moves a config path, new models ship at new prices. Sponsorship keeps CodeBurn current with all of it, so the numbers you see are always the real ones.
Where your sponsorship goes:
- **Honest numbers.** New models and price changes are mapped quickly, so your cost is the real cost, not a guess.
- **More tools.** Every one of the 40 providers started as a single file. Sponsorship funds the next one.
- **More tools.** Every one of the 41 providers started as a single file. Sponsorship funds the next one.
- **Fast fixes.** When a vendor breaks something, paid time is what gets it patched now instead of someday.
Sponsoring as a team or company? Your logo lands right here, in front of every developer who opens the repo. The first sponsor gets it to themselves until the next one shows up.

View file

@ -1,8 +1,10 @@
# Releasing CodeBurn
This document describes the actual steps a maintainer takes to cut a CLI or macOS menubar release. CLI releases are run by hand with `npm publish`; macOS menubar releases are automated by `.github/workflows/release-menubar.yml` when a `mac-v*` tag is pushed.
This document describes the actual steps a maintainer takes to cut CLI, macOS menubar, and Electron desktop releases. CLI releases are run by hand with `npm publish`; macOS menubar releases are automated by `.github/workflows/release-menubar.yml` when a `mac-v*` tag is pushed.
The Electron desktop app (`app/`) has no CI automation yet, but it is released manually under `desktop-v<version>` tags: build the artifacts on a macOS host (see `app/DISTRIBUTION.md`) and `gh release upload desktop-v<version> … --clobber` them onto the release. See `app/DISTRIBUTION.md` for how to build and distribute it as an ad-hoc-signed, non-notarized macOS build (plus unsigned Windows and Linux builds).
The Electron desktop app (`app/`) is released manually under `desktop-v<version>` tags. Build macOS and Linux artifacts as described in `app/DISTRIBUTION.md`; the tag also runs the read-only `Build Windows installer` workflow on `windows-latest`. Download its `CodeBurn-Windows-Installer` artifact and upload both the `.exe` and `.exe.blockmap` with the other platform assets. The workflow never publishes release assets.
Before announcing a desktop release, the release owner must confirm the live GitHub Release contains all four macOS `.dmg`/`.zip` files, the Linux `.AppImage`, `.deb`, and `.rpm`, and both Windows installer files. Publishing the Release runs the workflow's read-only live-asset verification job. If assets are uploaded after publication, rerun `Build Windows installer` with the `release_tag` input and require that verification job to pass. A failed or missing verification is a release blocker.
## Versioning
@ -197,4 +199,4 @@ For the menubar, tag a new mac-v0.9.9 and let the workflow build and upload it.
## Summary
The CLI release is manual: bump the version, update `CHANGELOG.md`, commit, run `npm publish`, then tag and create a GitHub Release. The macOS menubar release is automated: pushing a `mac-v*` tag fires `.github/workflows/release-menubar.yml`, which builds, signs, zips, and publishes the bundle. The homebrew-core formula is updated automatically or via `brew bump-formula-pr`.
The CLI release is manual: bump the version, update `CHANGELOG.md`, commit, run `npm publish`, then tag and create a GitHub Release. The macOS menubar release is automated: pushing a `mac-v*` tag fires `.github/workflows/release-menubar.yml`, which builds, signs, zips, and publishes the bundle. The Electron desktop release is assembled manually under a `desktop-v*` tag, with the release-authoritative Windows NSIS installer built by the read-only `windows-latest` workflow. The homebrew-core formula is updated automatically or via `brew bump-formula-pr`.

51
THIRD_PARTY_NOTICES.md Normal file
View file

@ -0,0 +1,51 @@
# Third-party notices
CodeBurn is MIT licensed (see `LICENSE`). It also contains code derived from the
projects below, which carry their own terms. Each notice is reproduced here as
those terms require.
---
## @deepseek-ai/dsh-session-persistence-jsonl
`scanZstdFrames` in `src/providers/dsh.ts` is a transcription of the function of
the same name in this package (`src/zstd.ts`), which is what lets CodeBurn read
a DeepSeek Harness session log without depending on the harness itself. No other
part of the package is used.
Upstream declares two different licenses for this package: the published npm
package (0.0.1-rc.1) ships a BSD 3-Clause `LICENSE` and declares
`"license": "BSD-3-Clause"`, while the monorepo source it is built from
(`deepseek-ai/deepseek-harness`, `packages/session/session-persistence-jsonl`)
declares MIT. The stricter of the two is reproduced below.
```
BSD 3-Clause License
Copyright (c) 2026, DeepSeek
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```

View file

@ -3,10 +3,10 @@
This document describes how to produce distributable macOS, Windows, and Linux
builds of the Electron desktop app. The macOS build is ad-hoc-signed and
**not notarized** (no paid Apple Developer account); the Windows and Linux
builds are **unsigned**. There is no CI automation for any of this yet (unlike
the CLI and menubar release processes in `../RELEASING.md`) — packaging is run
by hand on a maintainer's machine. All three targets are produced by
`electron-builder` and can be cross-built from a single macOS host.
builds are **unsigned**. Windows NSIS packages are built and checked by the
`Build Windows installer` GitHub Actions workflow; the other desktop packages
are still produced by hand. All three targets are produced by
`electron-builder`.
## The bundled CLI (no install prerequisite)
@ -93,8 +93,9 @@ self-contained bundle into `app/build/cli`; see "The bundled CLI" above), then
`vite`), then `electron-builder --mac` (whose `afterPack` hook copies the
staged CLI into the app). `package:win` and `package:linux` mirror it exactly,
swapping the final flag for `electron-builder --win` and `electron-builder
--linux`. All three can run on the same macOS host — electron-builder downloads
the NSIS and AppImage tooling on first use.
--linux`. Developers can run all three locally on the same macOS host —
electron-builder downloads the NSIS and AppImage tooling on first use. Release
Windows installers are built by the `windows-latest` workflow described below.
### Artifacts
@ -154,19 +155,20 @@ separate `electron-builder.yml`):
## Windows and Linux builds
Both are cross-built from the same macOS host used for the mac build — no
Windows or Linux machine, and no `wine`, is required. electron-builder 26
embeds the Windows executable's icon/version resources natively and downloads
the NSIS and AppImage tooling on first run.
Developers can cross-build both locally from the same macOS host used for the
mac build — no Windows or Linux machine, and no `wine`, is required.
Release-authoritative Windows NSIS installers are instead built by the `Build
Windows installer` workflow on `windows-latest`. electron-builder 26 embeds the
Windows executable's icon/version resources natively and downloads the NSIS and
AppImage tooling on first run.
### Windows (`package:win`)
`electron-builder --win` produces a single artifact in `app/release/`:
`electron-builder --win` produces a single installer in `app/release/`:
- **`CodeBurn Setup 0.9.15.exe`** — the NSIS installer (the version number
tracks `package.json`; note the spaces in the filename). A `.exe.blockmap`
is written alongside it (differential-update metadata, unused — no
auto-updater yet).
- **`CodeBurn-Setup-0.9.15.exe`** — the NSIS installer (the version number
tracks `package.json`). A `.exe.blockmap` is written alongside it
(differential-update metadata, unused — no auto-updater yet).
Config (`build.win` + `build.nsis`):
@ -236,8 +238,7 @@ taskbar/dock; it does not affect packaging or launch.
## Releases
There is no release CI for the desktop app yet (see the note at the top). When
a maintainer cuts a desktop release by hand, the GitHub tag convention is:
When a maintainer cuts a desktop release, the GitHub tag convention is:
```
desktop-v<version> # e.g. desktop-v0.9.15
@ -245,14 +246,31 @@ desktop-v<version> # e.g. desktop-v0.9.15
This mirrors the menubar's `mac-v<version>` convention (see `../RELEASING.md`)
and keeps the desktop app's tags in their own namespace, separate from the CLI
(`v<version>`) and the menubar (`mac-v<version>`). Upload all of the artifacts
above — the four macOS `.dmg`/`.zip` files, `CodeBurn-Setup-<version>.exe`,
and `CodeBurn-<version>.AppImage` — to the GitHub Release created at that
tag. The website's download links **pin that tag** in their URLs, so the
release name and the artifact filenames must match exactly. (The Windows
installer uses an explicit `nsis.artifactName` of
`CodeBurn-Setup-${version}.${ext}` — electron-builder's default contains
spaces, which make ugly percent-encoded URLs.)
(`v<version>`) and the menubar (`mac-v<version>`).
Pushing a `desktop-v<version>` tag runs the `Build Windows installer` workflow
on `windows-latest`. The workflow requires the tag version, root package
version, and app package version to agree, and it fails unless the build emits
exactly one `CodeBurn-Setup-<version>.exe` and one matching
`.exe.blockmap` at the top level of `app/release/`. It uploads those exact
top-level filenames as the `CodeBurn-Windows-Installer`
Actions artifact. The workflow has read-only repository permissions and does
**not** publish release assets automatically. Artifacts are retained for 30 days.
Before publishing the GitHub Release, the release owner must download that
workflow artifact and manually upload both Windows files along with the four
macOS `.dmg`/`.zip` files, `CodeBurn-<version>.AppImage`,
`codeburn-desktop_<version>_amd64.deb`, and
`codeburn-desktop-<version>.x86_64.rpm`. Confirm the live release contains
every required platform asset before announcing it. The
website's download links **pin that tag** in their URLs, so a release with a
missing installer is broken even when another Windows distribution channel is
available. The Windows installer uses an explicit `nsis.artifactName` of
`CodeBurn-Setup-${version}.${ext}`.
Publishing the Release triggers a read-only live-asset check. If the files are
uploaded afterward, rerun the workflow manually with `release_tag` set to the
existing `desktop-v<version>` tag and require the verification job to pass.
## Verifying a build

View file

@ -169,6 +169,7 @@
"$HOME/.copilot",
"$HOME/.cursor",
"$HOME/.deepseek",
"$HOME/.dsh/sessions",
"$HOME/.factory",
"$HOME/.forge",
"$HOME/.gemini",

View file

@ -138,7 +138,12 @@ function installDefaultMocks() {
summary: {
healthScore: 100, healthGrade: 'A', findingCount: 0, periodCostUSD: 0,
sessions: 0, calls: 0, potentialSavingsTokens: 0, potentialSavingsCostUSD: 0,
potentialSavingsPercent: 0, costRateUSD: 0,
potentialSavingsPercent: 0, costRateUSD: 0, measuredSavingsUSD: 0,
byClass: {
fix: { tokensSaved: 0, savingsUSD: 0, count: 0 },
nudge: { tokensSaved: 0, savingsUSD: 0, count: 0 },
keep: { tokensSaved: 0, savingsUSD: 0, count: 0 },
},
},
findings: [],
})

View file

@ -403,10 +403,12 @@ export type SpendFlow = {
// ————— src/optimize.ts —————
export type WasteAction =
| { type: 'paste'; label: string; text: string; destination?: 'claude-md' | 'session-opener' | 'prompt' | 'shell-config' }
| { type: 'paste'; label: string; text: string; destination?: 'claude-md' | 'session-opener' | 'prompt' | 'shell-config' | 'manual' }
| { type: 'command'; label: string; text: string }
| { type: 'file-content'; label: string; path: string; content: string }
export type FindingClass = 'fix' | 'nudge' | 'keep'
export type OptimizeJsonReport = {
period: { label: string; start: string | null; end: string | null }
summary: {
@ -420,6 +422,8 @@ export type OptimizeJsonReport = {
potentialSavingsCostUSD: number
potentialSavingsPercent: number | null
costRateUSD: number
measuredSavingsUSD: number
byClass: Record<FindingClass, { tokensSaved: number; savingsUSD: number; count: number }>
}
findings: Array<{
id: string
@ -429,8 +433,21 @@ export type OptimizeJsonReport = {
trend: 'active' | 'improving' | null
tokensSaved: number
estimatedSavingsUSD: number
class: FindingClass
basis: 'measured' | 'estimated'
fix: WasteAction
}>
/** Still-applied fixes, re-measured on every run. Absent on older CLIs. */
appliedFixes?: Array<{
id: string
kind: string
findingId: string | null
appliedAt: string
verdict: 'worked' | 'partial' | 'no-effect' | 'pending'
estimatedTokens: number
realizedTokens: number
undoCommand: string
}>
}
// ————— T1b: src/sharing/* (defined by the shared contract) —————

View file

@ -48,23 +48,31 @@ function makeOptimizeReport(): OptimizeJsonReport {
healthScore: 72, healthGrade: 'C', findingCount: 3, periodCostUSD: 612.48,
sessions: 88, calls: 1220, potentialSavingsTokens: 184_000,
potentialSavingsCostUSD: 94.4, potentialSavingsPercent: 15.4, costRateUSD: 0.0005,
measuredSavingsUSD: 27.8,
byClass: {
fix: { tokensSaved: 18_200, savingsUSD: 9.1, count: 1 },
nudge: { tokensSaved: 17_400, savingsUSD: 8.7, count: 1 },
keep: { tokensSaved: 4_800, savingsUSD: 2.4, count: 1 },
},
},
findings: [
{
id: 'cost-outliers', title: 'Opus is doing your small talk',
id: 'unused-mcp', title: 'Opus is doing your small talk',
explanation: 'Small conversational requests are running on an expensive model.',
severity: 'high', trend: 'active', tokensSaved: 18_200, estimatedSavingsUSD: 9.1,
class: 'fix', basis: 'estimated',
fix: { type: 'paste', label: 'Paste into CLAUDE.md', text: 'Use Sonnet for routine questions.', destination: 'claude-md' },
},
{
id: 'context-heavy-sessions', title: 'Cache hit is low in agentseal-dash',
id: 'cost-outliers', title: 'Cache hit is low in agentseal-dash',
explanation: 'Repeated context is not being served from cache.', severity: 'medium',
trend: null, tokensSaved: 17_400, estimatedSavingsUSD: 8.7,
trend: null, tokensSaved: 17_400, estimatedSavingsUSD: 8.7, class: 'nudge', basis: 'measured',
fix: { type: 'command', label: 'Run this command', text: 'codeburn cache inspect' },
},
{
id: 'warmup-heavy', title: 'Batch tiny requests', explanation: 'Many short sessions repeat setup work.',
id: 'context-heavy-sessions', title: 'Batch tiny requests', explanation: 'Many short sessions repeat setup work.',
severity: 'low', trend: 'improving', tokensSaved: 4_800, estimatedSavingsUSD: 2.4,
class: 'keep', basis: 'measured',
fix: { type: 'file-content', label: 'Create configuration', path: '~/.codeburn/config.json', content: '{"batch":true}' },
},
],
@ -121,6 +129,46 @@ describe('Optimize', () => {
Object.defineProperty(navigator, 'clipboard', { configurable: true, value: { writeText } })
})
it('lists applied fixes with a glyph per verdict and the undo hint', async () => {
const report = makeOptimizeReport()
report.appliedFixes = [
{ id: 'a1', kind: 'archive-skill', findingId: 'unused-skills', appliedAt: '2026-07-06T00:00:00.000Z', verdict: 'worked', estimatedTokens: 300_000, realizedTokens: 280_000, undoCommand: 'codeburn act undo a1' },
{ id: 'b2', kind: 'defer-threshold', findingId: 'mcp-defer-threshold', appliedAt: '2026-07-07T00:00:00.000Z', verdict: 'partial', estimatedTokens: 600_000, realizedTokens: 420_000, undoCommand: 'codeburn act undo b2' },
{ id: 'c3', kind: 'shell-config', findingId: 'bash-output-cap', appliedAt: '2026-07-05T00:00:00.000Z', verdict: 'no-effect', estimatedTokens: 41_000, realizedTokens: 0, undoCommand: 'codeburn act undo c3' },
{ id: 'd4', kind: 'mcp-remove', findingId: null, appliedAt: '2026-07-09T00:00:00.000Z', verdict: 'pending', estimatedTokens: 0, realizedTokens: 0, undoCommand: 'codeburn act undo d4' },
]
getOptimizeReport.mockResolvedValue(report)
render(<Optimize period="30days" provider="all" />)
await screen.findByText('Applied fixes')
const rows = [...document.querySelectorAll('.opt-applied-row')]
expect(rows.map(r => r.className.split(' ')[1])).toEqual([
'opt-applied-worked', 'opt-applied-partial', 'opt-applied-no-effect', 'opt-applied-pending',
])
expect(rows[0]!.textContent).toContain('unused-skills')
expect(rows[0]!.textContent).toContain('est. 300K \u2192 280K')
expect(rows[3]!.textContent).toContain('mcp-remove')
expect(screen.getByText('codeburn act undo c3')).toBeTruthy()
})
it('omits the applied-fixes list when nothing is applied', async () => {
render(<Optimize period="30days" provider="all" />)
await screen.findByText('Opus is doing your small talk')
expect(document.querySelector('.opt-applied')).toBeNull()
})
it('groups Waste findings under the fix / habits / FYI headers in order', async () => {
render(<Optimize period="30days" provider="all" />)
await screen.findByText('Opus is doing your small talk')
const groups = document.querySelectorAll('.opt-group')
expect([...groups].map(g => g.textContent)).toEqual([
'Fix now (apply-able) · 18.2K tokens · $9.10 · 1 finding',
'Habits · 17.4K tokens · $8.70 · 1 finding',
'FYI · 4.8K tokens · $2.40 · 1 finding',
])
})
it('renders tabs and actionable Waste findings with impact, savings, explanation, and copy-paste fix', async () => {
render(<Optimize period="30days" provider="all" />)
@ -130,7 +178,7 @@ describe('Optimize', () => {
expect(screen.getByText('Medium')).toHaveClass('opt-impact-medium')
expect(screen.getByText('Low')).toHaveClass('opt-impact-low')
expect(screen.getByText('$9.10')).toHaveClass('opt-finding-savings')
expect(screen.getByText('18.2K tokens')).toBeInTheDocument()
expect(screen.getByText('18.2K tokens · estimated')).toBeInTheDocument()
expect(screen.getByRole('tab', { name: 'Waste $94.40' })).toBeInTheDocument()
expect(screen.getByRole('tab', { name: 'Reverts $107.00' })).toBeInTheDocument()
expect(screen.getByRole('tab', { name: 'Abandoned $65.40' })).toBeInTheDocument()
@ -160,6 +208,32 @@ describe('Optimize', () => {
expect(screen.getByText('{"batch":true}')).toBeInTheDocument()
})
it('renders and copies connector guidance as a manual action', async () => {
const report = makeOptimizeReport()
report.findings.push({
id: 'mcp-low-coverage', title: 'Underused claude.ai connector',
explanation: 'The connector loads unused tools.', severity: 'medium',
trend: null, tokensSaved: 2_000, estimatedSavingsUSD: 1,
// Connector-only: no appliable plan, so the finding is a nudge.
class: 'nudge', basis: 'estimated',
fix: {
type: 'paste', destination: 'manual', label: 'Manage the connector where it loads:',
text: 'Open /mcp and disable claude.ai Google Calendar.',
},
})
report.summary.byClass.nudge = { tokensSaved: 19_400, savingsUSD: 9.7, count: 2 }
getOptimizeReport.mockResolvedValue(report)
render(<Optimize period="30days" provider="all" />)
const row = await screen.findByRole('button', { name: /Underused claude.ai connector/ })
fireEvent.click(row)
expect(screen.getByText('Manage the connector where it loads:')).toBeInTheDocument()
expect(screen.getByText('Open /mcp and disable claude.ai Google Calendar.')).toBeInTheDocument()
expect(row.parentElement?.querySelector('.opt-fix')).toHaveClass('opt-fix-paste')
fireEvent.click(screen.getByRole('button', { name: 'Copy' }))
await waitFor(() => expect(writeText).toHaveBeenCalledWith('Open /mcp and disable claude.ai Google Calendar.'))
})
it('switches to Reverts and Abandoned and shows only the matching yield details', async () => {
render(<Optimize period="30days" provider="all" />)
await screen.findByText('Opus is doing your small talk')

View file

@ -9,7 +9,7 @@ import { StaleBanner } from '../components/StaleBanner'
import { type Polled, usePolled } from '../hooks/usePolled'
import { formatCompact, formatUsd } from '../lib/format'
import { codeburn } from '../lib/ipc'
import type { DateRange, MenubarPayload, OptimizeJsonReport, Period, SessionYieldJson, WasteAction, YieldJsonReport } from '../lib/types'
import type { DateRange, FindingClass, MenubarPayload, OptimizeJsonReport, Period, SessionYieldJson, WasteAction, YieldJsonReport } from '../lib/types'
type OptimizeTab = 'waste' | 'reverts' | 'abandoned' | 'fixes'
@ -101,7 +101,51 @@ function WasteRows({ report }: { report: Polled<OptimizeJsonReport> }) {
<div className="opt-summary">
{report.data.summary.findingCount.toLocaleString('en-US')} findings · {formatUsd(report.data.summary.potentialSavingsCostUSD)} potential · health {report.data.summary.healthScore}/100
</div>
<ActionableFindingRows findings={report.data.findings} />
<ActionableFindingRows findings={report.data.findings} byClass={report.data.summary.byClass} />
<AppliedFixRows fixes={report.data.appliedFixes ?? []} />
</div>
)
}
type AppliedFix = NonNullable<OptimizeJsonReport['appliedFixes']>[number]
const VERDICT_GLYPH: Record<AppliedFix['verdict'], string> = {
worked: '\u2713',
partial: '~',
'no-effect': '\u2717',
pending: '\u2026',
}
const VERDICT_LABEL: Record<AppliedFix['verdict'], string> = {
worked: 'worked',
partial: 'under estimate',
'no-effect': 'did not help',
pending: 'measuring',
}
// Closes the loop after `optimize --apply`: what each applied fix actually
// measured, and for the ones that did nothing, how to put them back.
function AppliedFixRows({ fixes }: { fixes: AppliedFix[] }) {
if (!fixes.length) return null
return (
<div className="opt-findings opt-applied">
<div className="opt-group">Applied fixes</div>
{fixes.map(fix => (
<div className={`opt-applied-row opt-applied-${fix.verdict}`} key={fix.id}>
<span className="opt-applied-glyph" aria-hidden="true">{VERDICT_GLYPH[fix.verdict]}</span>
<b className="opt-finding-title">{fix.findingId ?? fix.kind}</b>
<span className="opt-applied-verdict">{VERDICT_LABEL[fix.verdict]}</span>
<span className="opt-finding-tokens">
{fix.verdict === 'pending'
? '\u2014'
: `est. ${formatCompact(fix.estimatedTokens)} \u2192 ${formatCompact(fix.realizedTokens)}`}
</span>
</div>
))}
{fixes.some(fix => fix.verdict === 'no-effect') && (
<div className="opt-summary opt-applied-hint">Revert one that did not help: <code>{fixes.find(fix => fix.verdict === 'no-effect')!.undoCommand}</code></div>
)}
</div>
)
}
@ -114,11 +158,17 @@ const IMPACT_ICON: Record<'high' | 'medium' | 'low', string> = {
low: '↓',
}
const CLASS_HEADERS: Record<FindingClass, string> = {
fix: 'Fix now (apply-able)',
nudge: 'Habits',
keep: 'FYI',
}
function actionText(fix: WasteAction): string {
return fix.type === 'file-content' ? fix.content : fix.text
}
function ActionableFindingRows({ findings }: { findings: OptimizeFinding[] }) {
function ActionableFindingRows({ findings, byClass }: { findings: OptimizeFinding[]; byClass: OptimizeJsonReport['summary']['byClass'] }) {
const [expandedId, setExpandedId] = useState<string | null>(null)
const [copiedId, setCopiedId] = useState<string | null>(null)
@ -132,10 +182,18 @@ function ActionableFindingRows({ findings }: { findings: OptimizeFinding[] }) {
return (
<div className="opt-findings">
{findings.map(finding => {
{findings.map((finding, i) => {
const expanded = expandedId === finding.id
// Findings arrive class-sorted from the CLI, so a header goes in
// wherever the class changes.
const showHeader = finding.class !== findings[i - 1]?.class
return (
<Fragment key={finding.id}>
{showHeader && (
<div className="opt-group">
{CLASS_HEADERS[finding.class]} · {formatCompact(byClass[finding.class].tokensSaved)} tokens · {formatUsd(byClass[finding.class].savingsUSD)} · {byClass[finding.class].count} {byClass[finding.class].count === 1 ? 'finding' : 'findings'}
</div>
)}
<button
className="opt-finding opt-finding-toggle"
type="button"
@ -153,7 +211,7 @@ function ActionableFindingRows({ findings }: { findings: OptimizeFinding[] }) {
)}
</span>
<span className="opt-finding-savings">{formatUsd(finding.estimatedSavingsUSD)}</span>
<span className="opt-finding-tokens">{formatCompact(finding.tokensSaved)} tokens</span>
<span className="opt-finding-tokens">{formatCompact(finding.tokensSaved)} tokens · {finding.basis}</span>
<span className="opt-finding-chevron" aria-hidden="true"></span>
</button>
{expanded && (

View file

@ -640,6 +640,9 @@ td:first-child { font-size: var(--fs-body); font-weight: var(--fw-body); }
.opt-waste { min-width: 0; }
.opt-summary { padding: 0 0 10px; color: var(--mut); font-size: 11.5px; font-variant-numeric: tabular-nums; }
.opt-findings { display: grid; min-width: 0; }
.opt-group { padding: 11px 0 5px; color: var(--mut2); font-size: 10px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; }
.opt-group:first-child { padding-top: 0; }
.opt-group + .opt-finding { border-top: 0; }
.opt-finding { display: grid; align-items: center; column-gap: 12px; min-height: 43px; border-top: 1px solid var(--line2); }
.opt-finding:first-child { border-top: 0; }
.opt-finding-legacy { grid-template-columns: 28px minmax(0, 1fr) 104px 86px; }
@ -668,6 +671,15 @@ td:first-child { font-size: var(--fs-body); font-weight: var(--fw-body); }
.opt-fix-code { max-width: 100%; overflow-x: auto; margin: 0; padding: 10px 11px; border: 1px solid var(--line); border-radius: 6px; background: var(--phead); color: var(--ink); font-family: var(--mono); font-size: 11px; line-height: 1.5; white-space: pre; }
.opt-fix-command .opt-fix-code code::before { content: '$ '; color: var(--mut2); user-select: none; }
.opt-copy { flex: 0 0 auto; padding: 4px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); color: var(--mut); font: inherit; font-size: 10.5px; cursor: pointer; }
.opt-applied { padding-top: 12px; }
.opt-applied-row { display: grid; grid-template-columns: 16px minmax(0, 1fr) 110px 140px; align-items: center; column-gap: 12px; min-height: 34px; border-top: 1px solid var(--line2); }
.opt-applied-glyph { color: var(--mut2); font-family: var(--mono); font-size: 12px; }
.opt-applied-verdict { color: var(--mut); font-size: 10.5px; }
.opt-applied-worked .opt-applied-glyph, .opt-applied-worked .opt-applied-verdict { color: var(--ok); }
.opt-applied-partial .opt-applied-glyph, .opt-applied-partial .opt-applied-verdict { color: var(--warn); }
.opt-applied-no-effect .opt-applied-glyph, .opt-applied-no-effect .opt-applied-verdict { color: var(--bad); }
.opt-applied-hint { padding: 9px 0 0; }
.opt-applied-hint code { font-family: var(--mono); }
.opt-copy:hover, .opt-copy:focus-visible { border-color: var(--accent); color: var(--ink); outline: none; }
.ov-analytics-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; align-items: stretch; }
.ov-analytics-row > :only-child { grid-column: 1 / -1; }

View file

@ -0,0 +1,101 @@
#!/usr/bin/env node
import { readFileSync, readdirSync } from 'node:fs'
import { basename, join, resolve } from 'node:path'
import { rootFromModuleUrl } from './windows-installer-paths.mjs'
function fail(message) {
console.error(`Windows installer manifest invalid: ${message}`)
process.exitCode = 1
}
function option(name, fallback) {
const index = process.argv.indexOf(name)
if (index === -1) return fallback
if (!process.argv[index + 1]) throw new Error(`${name} requires a value`)
return process.argv[index + 1]
}
function packageVersion(path) {
return JSON.parse(readFileSync(path, 'utf8')).version
}
function filesBelow(directory) {
return readdirSync(directory, { withFileTypes: true })
.filter(entry => entry.isFile())
.map(entry => basename(entry.name))
}
function releaseVersion(tag) {
const match = /^desktop-v(.+)$/.exec(tag)
if (!match) throw new Error(`${tag || '(missing tag)'} is not a desktop release tag`)
return match[1]
}
function verifyLiveRelease(tag, assetPath) {
const version = releaseVersion(tag)
const assets = JSON.parse(readFileSync(assetPath, 'utf8'))
if (!Array.isArray(assets) || assets.some(asset => typeof asset !== 'string')) {
throw new Error('release asset manifest must be a JSON array of names')
}
const required = [
`CodeBurn-${version}-arm64.dmg`,
`CodeBurn-${version}.dmg`,
`CodeBurn-${version}-arm64-mac.zip`,
`CodeBurn-${version}-mac.zip`,
`CodeBurn-${version}.AppImage`,
`codeburn-desktop_${version}_amd64.deb`,
`codeburn-desktop-${version}.x86_64.rpm`,
`CodeBurn-Setup-${version}.exe`,
`CodeBurn-Setup-${version}.exe.blockmap`,
]
for (const expected of required) {
const count = assets.filter(asset => asset === expected).length
if (count === 0) fail(`live release is missing ${expected}`)
if (count > 1) fail(`live release contains ${count} copies of ${expected}`)
}
if (!process.exitCode) console.log(`Live desktop release assets verified for ${version}`)
}
try {
const tag = option('--tag', '')
const releaseAssets = option('--release-assets', '')
if (releaseAssets) {
verifyLiveRelease(tag, resolve(releaseAssets))
} else {
const root = resolve(option('--root', rootFromModuleUrl(import.meta.url)))
const artifacts = resolve(option('--artifacts', join(root, 'app', 'release')))
const rootVersion = packageVersion(join(root, 'package.json'))
const appVersion = packageVersion(join(root, 'app', 'package.json'))
if (rootVersion !== appVersion) {
fail(`root version ${rootVersion} does not match app version ${appVersion}`)
}
if (tag && tag !== `desktop-v${appVersion}`) {
fail(`${tag} does not match app version ${appVersion}`)
}
const files = filesBelow(artifacts)
const expectedArtifacts = [
`CodeBurn-Setup-${appVersion}.exe`,
`CodeBurn-Setup-${appVersion}.exe.blockmap`,
]
for (const expected of expectedArtifacts) {
const count = files.filter(file => file === expected).length
if (count !== 1) fail(`expected exactly one ${expected}, found ${count}`)
}
const installerArtifacts = files.filter(file => /^CodeBurn-Setup-.*\.exe(?:\.blockmap)?$/.test(file))
const unexpected = installerArtifacts.filter(file => !expectedArtifacts.includes(file))
if (unexpected.length > 0) {
fail(`unexpected Windows installer artifacts: ${unexpected.join(', ')}`)
}
if (!process.exitCode) {
console.log(`Windows installer manifest verified for ${appVersion}`)
}
}
} catch (error) {
fail(error instanceof Error ? error.message : String(error))
}

View file

@ -0,0 +1,168 @@
import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { spawnSync } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { rootFromModuleUrl } from './windows-installer-paths.mjs'
const verifier = new URL('./verify-windows-installer.mjs', import.meta.url)
const verifierPath = fileURLToPath(verifier)
function fixture(options: {
appVersion?: string
rootVersion?: string
files?: string[]
tag?: string
} = {}) {
const root = mkdtempSync(join(tmpdir(), 'codeburn-windows-manifest-'))
const appDir = join(root, 'app')
const releaseDir = join(appDir, 'release')
mkdirSync(releaseDir, { recursive: true })
const appVersion = options.appVersion ?? '1.2.3'
writeFileSync(join(root, 'package.json'), JSON.stringify({ version: options.rootVersion ?? appVersion }))
writeFileSync(join(appDir, 'package.json'), JSON.stringify({ version: appVersion }))
for (const file of options.files ?? [
`CodeBurn-Setup-${appVersion}.exe`,
`CodeBurn-Setup-${appVersion}.exe.blockmap`,
]) {
const path = join(releaseDir, file)
mkdirSync(join(path, '..'), { recursive: true })
writeFileSync(path, 'fixture')
}
const args = [verifierPath, '--root', root, '--artifacts', releaseDir]
if (options.tag) args.push('--tag', options.tag)
return spawnSync(process.execPath, args, { encoding: 'utf8' })
}
function releaseFixture(files: string[]) {
const root = mkdtempSync(join(tmpdir(), 'codeburn-windows-release-'))
const assets = join(root, 'assets.json')
writeFileSync(assets, JSON.stringify(files))
return spawnSync(process.execPath, [
verifierPath,
'--tag',
'desktop-v1.2.3',
'--release-assets',
assets,
], { encoding: 'utf8' })
}
describe('Windows installer release manifest verifier', () => {
it('converts a Windows module URL into a valid drive-letter repository root', () => {
expect(rootFromModuleUrl(
'file:///D:/a/codeburn/codeburn/app/scripts/verify-windows-installer.mjs',
true,
)).toBe('D:\\a\\codeburn\\codeburn')
})
it('accepts one exact installer and blockmap for matching package versions and tag', () => {
const result = fixture({ tag: 'desktop-v1.2.3' })
expect(result.status).toBe(0)
expect(result.stdout).toContain('Windows installer manifest verified for 1.2.3')
})
it('rejects a desktop tag that does not match the app version', () => {
const result = fixture({ tag: 'desktop-v1.2.4' })
expect(result.status).toBe(1)
expect(result.stderr).toContain('desktop-v1.2.4 does not match app version 1.2.3')
})
it('rejects divergent root and app versions', () => {
const result = fixture({ rootVersion: '1.2.2' })
expect(result.status).toBe(1)
expect(result.stderr).toContain('root version 1.2.2 does not match app version 1.2.3')
})
it('rejects a missing installer blockmap', () => {
const result = fixture({ files: ['CodeBurn-Setup-1.2.3.exe'] })
expect(result.status).toBe(1)
expect(result.stderr).toContain('expected exactly one CodeBurn-Setup-1.2.3.exe.blockmap, found 0')
})
it('requires installer artifacts at the documented top-level output', () => {
const result = fixture({
files: [
'CodeBurn-Setup-1.2.3.exe.blockmap',
'duplicate/CodeBurn-Setup-1.2.3.exe',
],
})
expect(result.status).toBe(1)
expect(result.stderr).toContain('expected exactly one CodeBurn-Setup-1.2.3.exe, found 0')
})
it('rejects stale installer artifacts from another version', () => {
const result = fixture({
files: [
'CodeBurn-Setup-1.2.3.exe',
'CodeBurn-Setup-1.2.3.exe.blockmap',
'CodeBurn-Setup-1.2.2.exe',
'CodeBurn-Setup-1.2.2.exe.blockmap',
],
})
expect(result.status).toBe(1)
expect(result.stderr).toContain('unexpected Windows installer artifacts')
})
it('accepts a complete live desktop release asset manifest', () => {
const result = releaseFixture([
'CodeBurn-1.2.3-arm64.dmg',
'CodeBurn-1.2.3.dmg',
'CodeBurn-1.2.3-arm64-mac.zip',
'CodeBurn-1.2.3-mac.zip',
'CodeBurn-1.2.3.AppImage',
'codeburn-desktop_1.2.3_amd64.deb',
'codeburn-desktop-1.2.3.x86_64.rpm',
'CodeBurn-Setup-1.2.3.exe',
'CodeBurn-Setup-1.2.3.exe.blockmap',
])
expect(result.status).toBe(0)
expect(result.stdout).toContain('Live desktop release assets verified for 1.2.3')
})
it('rejects a live desktop release missing the Windows installer', () => {
const result = releaseFixture([
'CodeBurn-1.2.3-arm64.dmg',
'CodeBurn-1.2.3.dmg',
'CodeBurn-1.2.3-arm64-mac.zip',
'CodeBurn-1.2.3-mac.zip',
'CodeBurn-1.2.3.AppImage',
'codeburn-desktop_1.2.3_amd64.deb',
'codeburn-desktop-1.2.3.x86_64.rpm',
'CodeBurn-Setup-1.2.3.exe.blockmap',
])
expect(result.status).toBe(1)
expect(result.stderr).toContain('live release is missing CodeBurn-Setup-1.2.3.exe')
})
it.each([
'codeburn-desktop_1.2.3_amd64.deb',
'codeburn-desktop-1.2.3.x86_64.rpm',
])('rejects a live desktop release missing %s', missing => {
const required = [
'CodeBurn-1.2.3-arm64.dmg',
'CodeBurn-1.2.3.dmg',
'CodeBurn-1.2.3-arm64-mac.zip',
'CodeBurn-1.2.3-mac.zip',
'CodeBurn-1.2.3.AppImage',
'codeburn-desktop_1.2.3_amd64.deb',
'codeburn-desktop-1.2.3.x86_64.rpm',
'CodeBurn-Setup-1.2.3.exe',
'CodeBurn-Setup-1.2.3.exe.blockmap',
]
const result = releaseFixture(required.filter(asset => asset !== missing))
expect(result.status).toBe(1)
expect(result.stderr).toContain(`live release is missing ${missing}`)
})
})

View file

@ -0,0 +1 @@
export function rootFromModuleUrl(moduleUrl: string | URL, windows?: boolean): string

View file

@ -0,0 +1,8 @@
import { posix, win32 } from 'node:path'
import { fileURLToPath } from 'node:url'
export function rootFromModuleUrl(moduleUrl, windows = process.platform === 'win32') {
const path = windows ? win32 : posix
const scriptPath = fileURLToPath(moduleUrl, { windows })
return path.resolve(path.dirname(scriptPath), '..', '..')
}

View file

@ -33,7 +33,12 @@ function makeTooltip(labels: Record<string, string>, fmt: (n: number) => string,
{items.slice(0, 6).map((p: any) => (
<div key={p.dataKey} className="flex items-center gap-2">
<span className="h-2.5 w-2.5 shrink-0 rounded-sm" style={{ background: p.color }} />
<span className="flex-1 truncate text-tertiary-foreground">{labels[String(p.dataKey)] ?? String(p.dataKey)}</span>
<span
className="flex-1 truncate text-tertiary-foreground"
title={labels[String(p.dataKey)] ?? String(p.dataKey)}
>
{labels[String(p.dataKey)] ?? String(p.dataKey)}
</span>
<span className="tabular-nums text-muted-foreground">{fmt(p.value)}</span>
</div>
))}
@ -172,7 +177,7 @@ function GranularLines({
{series.map(item => (
<span key={item.key} className="flex min-w-0 items-center gap-1.5">
<span className="h-2 w-2 shrink-0 rounded-full" style={{ background: item.color }} />
<span className="max-w-40 truncate">{item.label}</span>
<span className="max-w-40 truncate" title={item.label}>{item.label}</span>
</span>
))}
</div>

View file

@ -4,27 +4,27 @@ A map of the codebase. Read this once before opening a non-trivial PR.
## Three Surfaces
CodeBurn is one Node.js CLI plus two GUI clients that shell out to it.
CodeBurn is one Node.js CLI plus three ambient GUI clients that shell out to it.
```
+----------------------+ +-----------------+
| mac/ (Swift) | ---> | |
+----------------------+ | src/cli.ts |
| gnome/ (JavaScript) | ---> | (the CLI) |
+----------------------+ | |
| status |
| --format |
| menubar-json |
+-----------------+
|
v
+----------------------------+
| session files on disk |
| (JSONL, SQLite, protobuf) |
+----------------------------+
+---------------------------+ +-----------------+
| mac/ (Swift) | ---> | |
+---------------------------+ | src/cli.ts |
| windows/ (Rust + React) | ---> | (the CLI) |
+---------------------------+ | |
| gnome/ (JavaScript) | ---> | status |
+---------------------------+ | --format |
| menubar-json |
+-----------------+
|
v
+----------------------------+
| session files on disk |
| (JSONL, SQLite, protobuf) |
+----------------------------+
```
The macOS menubar (`mac/`) and the GNOME extension (`gnome/`) both invoke `codeburn status --format menubar-json --period <p>` and parse the JSON. They do not share code with the CLI; they only depend on its output contract.
The macOS menubar (`mac/`), the Windows tray app (`windows/`), and the GNOME extension (`gnome/`) all invoke `codeburn status --format menubar-json --period <p>` and parse the JSON. They do not share code with the CLI; they only depend on its output contract.
## CLI (`src/`)
@ -142,9 +142,11 @@ Three caches under `~/.cache/codeburn/` (override with `CODEBURN_CACHE_DIR`):
All three use atomic write (temp file + `rename`) and write with mode `0o600`. All three carry a numeric `version` field; bumping it forces a recompute next run.
The session cache (`src/session-cache.ts`) sits beside them as a directory of per-provider-month shards. A date-ranged query reads only the shards whose months can contribute a turn to that range; `CODEBURN_CACHE_SCOPE=all` turns that off and reads every shard, whatever the range. It is a read policy only — it is not part of any provider's env fingerprint, so setting or unsetting it never invalidates the cache.
### Optimize Detectors
`src/optimize.ts` exports 14 detectors. Each returns a `WasteFinding | null`. They are composed by `runOptimize()` which collects findings, ranks them by impact, and returns them with `WasteAction` objects (paste-to-CLAUDE.md, paste-to-session-opener, prompt-now, edit shell config).
`src/optimize.ts` exports 20 detectors. Each returns a `WasteFinding | null`. They are composed by `runOptimize()` which collects findings, ranks them by impact, and returns them with `WasteAction` objects (paste-to-CLAUDE.md, paste-to-session-opener, prompt-now, edit shell config).
| Detector | Line | What it catches |
|---|---|---|
@ -191,7 +193,7 @@ type Provider = {
`src/providers/index.ts` registers providers across two tiers:
- **Eager**: `claude`, `cline`, `codewhale`, `codebuff`, `codex`, `copilot`, `devin`, `droid`, `gemini`, `hermes`, `ibm-bob`, `kilo-code`, `kiro`, `kimi`, `lingtai-tui`, `mistral-vibe`, `mux`, `openclaw`, `open-design`, `pi`, `omp`, `qwen`, `roo-code`, `zerostack`, `grok`. Imported at module load.
- **Eager**: `claude`, `cline`, `codewhale`, `codebuff`, `codex`, `copilot`, `devin`, `droid`, `dsh`, `gemini`, `hermes`, `ibm-bob`, `kilo-code`, `kiro`, `kimi`, `lingtai-tui`, `mistral-vibe`, `mux`, `openclaw`, `open-design`, `pi`, `omp`, `qwen`, `roo-code`, `zerostack`, `grok`. Imported at module load.
- **Lazy**: `antigravity`, `forge`, `goose`, `cursor`, `opencode`, `cursor-agent`, `crush`, `warp`, `vercel-gateway`, `zcode`, `zed`. Imported via dynamic `import()` so the heavy dependencies (SQLite, protobuf, network clients) do not touch users who do not have those tools installed.
Both lists hit the same `getAllProviders()` aggregator. A failed lazy import is silent and excludes that provider from the run.
@ -217,6 +219,20 @@ Tests live in `mac/Tests/CodeBurnMenubarTests/` (currently `CapacityEstimatorTes
The build artifact is a zipped `.app` bundle produced by `mac/Scripts/package-app.sh`. See `RELEASING.md` for how the GitHub Actions workflow uses it.
## Windows Menubar (`windows/`)
Tauri 2 app: a Rust binary (`windows/src-tauri/`) owning the tray and the process spawning, plus a React + TypeScript popover (`windows/src/`) rendered in a WebView2 window. Design tokens come from `windows/tokens.json`, the same file `mac/` reads at build time, so both products render as one.
- `src-tauri/src/lib.rs` builds the tray, positions the popover against the taskbar edge, and registers the `#[tauri::command]` surface the frontend calls.
- `src-tauri/src/cli.rs` resolves and spawns the CLI. Only absolute `PATH` directories are searched (an empty entry from `;;` would otherwise resolve against the current directory), `CODEBURN_BIN` is allowlisted, and Windows system tools are spawned by absolute `%SystemRoot%\System32` path because `CreateProcess` searches the current directory first. `MIN_CLI_VERSION` gates the whole app; below it the popover shows a setup screen.
- `src-tauri/src/plan.rs` ports the Claude quota view. Like the macOS `ClaudeCredentialStore`, it never spends Claude's single-use refresh token; on a 401 it re-reads Claude's own credential file for a token Claude Code has already rotated.
- `src-tauri/src/tray_badge.rs` renders today's spend into a second tray icon, since Windows has no menubar title.
- `src/App.tsx` owns the payload cache, the CLI gate, and the refresh cadence, which follows popover visibility the way `mac/`'s `RefreshCadence.swift` does.
`cargo test` covers the PATH filter and the version gate. `windows/DEVELOPMENT.md` has the build, security, and release details; CI is `.github/workflows/windows-menubar-ci.yml` and releases go out on `windows-v*` tags.
The Linux (ksni) paths in the same crate are kept compiling but are experimental and unreleased; `gnome/` is the shipping Linux surface.
## GNOME Extension (`gnome/`)
Plain JavaScript, no bundler. Targets GNOME Shell 45-50 (`metadata.json`).

139
docs/optimize.md Normal file
View file

@ -0,0 +1,139 @@
# optimize
`codeburn optimize` scans your Claude Code sessions and your `~/.claude/` setup, reports what is
costing tokens without earning them, and grades the setup A to F.
## What it scans
- **Session transcripts** for the selected period: tool calls, per-call token usage, turn retries,
per-session cost, and the block each session opens with. This is where re-reads, junk directory
reads, low read:edit ratios, warmup overhead, retries, context pasted into session after session,
and expensive or context-heavy sessions come from.
- **Your configuration**: `~/.claude.json`, user and project `settings.json` / `settings.local.json`,
`.mcp.json`, `CLAUDE.md` (including `@`-imports), and the `skills/`, `agents/`, `commands/`
directories. This is where unused MCP servers, MCP deferral gaps, ghost skills/agents/commands,
the bash output cap, and oversized `CLAUDE.md` files come from.
Nothing is written during a scan. Only `--apply` writes.
## The three classes
Every finding carries a `class`, and both the CLI and the apps group by it:
| Class | Header | Meaning |
|---|---|---|
| `fix` | Fix now (apply-able) | CodeBurn can make this change for you: `codeburn optimize --apply` |
| `nudge` | Habits | Behavioural. Nothing to edit; the fix is how you drive the next session |
| `keep` | FYI | Informational. The cost may well be justified; decide for yourself |
A finding is `fix` only when a plan can actually be built for that instance. The same detector can
report a `fix` in one run and a `nudge` in another: `mcp-deferral-off` is appliable when the cause is
an `ENABLE_TOOL_SEARCH` override in a settings file, but manual when the cause is Vertex AI policy,
an outdated Claude Code, or an override that lives in your shell profile.
## What `--apply` may write
`--apply` builds a plan per finding, shows you the exact files it will touch, and asks before
writing. `--dry-run` prints the plan and stops.
| Finding | File it edits |
|---|---|
| `unused-mcp`, `mcp-low-coverage` | `~/.claude.json`, project `.mcp.json` / `settings.json` (removes the server entry) |
| `mcp-project-scope` | moves a global server entry into the keeper project's `.mcp.json` |
| `mcp-deferral-off` | the settings file carrying the `ENABLE_TOOL_SEARCH` override |
| `mcp-alwaysload-hygiene` | the config files carrying `"alwaysLoad": true` |
| `mcp-defer-threshold` | the settings file carrying the `auto:N` threshold |
| `unused-agents`, `unused-skills`, `unused-commands` | moves the files into `~/.claude/<kind>/.archived/` |
| `bash-output-cap` | appends a marker block to `~/.zshrc` / `~/.bashrc` |
| `read-edit-ratio`, `build-folder-reads` | appends a marker block to the current project's `CLAUDE.md` |
Every write is backed up and journaled first:
```bash
codeburn act list # every change CodeBurn has made
codeburn act undo <id> # restore the original files
codeburn act undo --last
```
Undo refuses if a file changed after the apply, unless you pass `--force`.
### The `--yes` CLAUDE.md guardrail
`--apply --yes` skips the prompt for every plan except `CLAUDE.md` rule blocks. Those land in the
`CLAUDE.md` of whatever directory you happen to be in, so a blanket `--yes` from an unrelated
directory would write advice into the wrong project. To apply one anyway, use the interactive picker
or name it explicitly:
```bash
codeburn optimize --apply --only read-edit-ratio
```
## After you apply
Applying a fix is a claim, so CodeBurn checks it. Every `codeburn optimize` run re-measures the
fixes still in place and prints them under `Applied fixes`, one line each:
| Line | Verdict | Meaning |
|---|---|---|
| `✓ unused-skills (7d ago): est. 300.0K -> measured 280.0K` | worked | at least 70% of the estimate showed up in your sessions |
| `~ mcp-defer-threshold (5d ago): est. 600.0K -> measured 420.0K (-30% vs estimate)` | partial | it helped, but under its estimate |
| `✗ bash-output-cap (6d ago): est. 41.0K -> measured 0 - did not help. Revert: codeburn act undo 3f2a1c04` | no-effect | no measured reduction at all |
| `… mcp-remove (1d ago): measuring, check back after 3 days` | measuring | too young, or the change has not taken effect in a session yet |
The estimate shown is the at-apply estimate scaled to the measured window, so the two numbers are
comparable. Both come from the same reconciliation `codeburn act report` prints — there is one set of
numbers, not two — and they are **measured**: provider-counted usage over the post-apply window.
Anything that cannot be measured (no baseline captured, a fix you reverted by hand, a
correlation-only kind like `guard-install`) stays on the `measuring` line with the reason, never a
claimed saving.
`--format json` carries the same list as `appliedFixes[]`, and the section appears in the dashboard
TUI and the desktop app.
### `--auto-revert`
```bash
codeburn optimize --auto-revert
```
Off by default. It undoes exactly the fixes whose verdict is `no-effect`, through the same code path
as `codeburn act undo` (backups restored, drift check applied, the revert journaled). It never
touches a `partial` or still-measuring fix, and it never auto-reverts a `claude-md-rule` — those land
in whatever project directory you were in, the same reason `--yes` skips them, so it prints the undo
command and leaves the file alone.
## measured vs estimated
Each finding also carries a `basis`, printed next to its savings and summarised in the header as
`N measured · M estimated`:
- **measured** — the token number is summed from provider-counted usage on your own calls. Today
that is `context-heavy-sessions` and `cost-outliers`.
- **estimated** — the token number comes from a model: a per-tool schema size, a per-line `CLAUDE.md`
cost, an average read size, a recovery fraction applied to real turn tokens. A detector that mixes
counted tokens with a model counts as estimated.
Sessions whose cost the provider never reported (Kiro, Cursor, some Cline sessions price from
modelled token counts) are kept out of the `cost-outliers` peer comparison, so a modelled cost is
never called an outlier against provider-reported ones. When a provider only ever estimates, the
comparison falls back to those sessions and the finding reports itself as `estimated`.
In `--format json`, `summary.measuredSavingsUSD` is the share of `summary.potentialSavingsCostUSD`
that comes from measured findings.
## Reading the health grade
Health starts at 100 and loses points per finding: 15 for a high-impact one, 7 for medium, 3 for low.
The total penalty is capped at 80, so a long tail of small findings cannot sink the score to zero on
its own. The grade is a band over that score:
| Grade | Score |
|---|---|
| A | 90-100 |
| B | 75-89 |
| C | 55-74 |
| D | 30-54 |
| F | below 30 |
The grade rates your setup, not your spending: an expensive month with a clean configuration still
scores an A.

View file

@ -18,6 +18,7 @@ For the architectural picture, see `../architecture.md`.
| [Copilot](copilot.md) | JSONL + SQLite (OTel) + Nitrite .db (JetBrains) | `src/providers/copilot.ts` | `tests/providers/copilot.test.ts` |
| [Devin](devin.md) | JSON + SQLite enrichment | `src/providers/devin.ts` | `tests/providers/devin.test.ts` |
| [Droid](droid.md) | JSONL | `src/providers/droid.ts` | `tests/providers/droid.test.ts` |
| [DeepSeek Harness](dsh.md) | JSONL (zstd frames) | `src/providers/dsh.ts` | `tests/providers/dsh.test.ts` |
| [Gemini](gemini.md) | JSON / JSONL | `src/providers/gemini.ts` | none |
| [Hermes Agent](hermes.md) | SQLite | `src/providers/hermes.ts` | `tests/providers/hermes.test.ts` |
| [IBM Bob](ibm-bob.md) | JSON | `src/providers/ibm-bob.ts` | `tests/providers/ibm-bob.test.ts` |

View file

@ -41,7 +41,8 @@ instead of trying to dedupe across stores.
wrong-schema, OTel is skipped and the JSONL/transcript sources are used as a fallback.
- **Durable cache (monotonic totals).** Copilot is marked `durableSources`: OTel-derived
cache entries are never evicted when VS Code prunes old spans from the DB, so
month-to-date totals do not drop as the DB rotates. Entries age out after 90 days.
month-to-date totals do not drop as the DB rotates. Orphaned entries age out after
90 days; sources still present in discovery remain cached regardless of call age.
- **Upgrade note.** The first run after upgrading to the OTel version bumps the copilot
parse version, which discards the prior copilot cache. Spans already pruned from the DB
before the upgrade cannot be recovered, so monotonicity starts from the upgrade point,

71
docs/providers/dsh.md Normal file
View file

@ -0,0 +1,71 @@
# DeepSeek Harness (dsh)
DeepSeek's open-source agent harness (`dsh`, npm `@deepseek-ai/dsh`). Unrelated to the [CodeWhale](codewhale.md) provider, which reads the DeepSeek desktop app.
- **Source:** `src/providers/dsh.ts`
- **Loading:** eager (`src/providers/index.ts`)
- **Test:** `tests/providers/dsh.test.ts`
## Where it reads from
| Level | Env var | Default |
|---|---|---|
| sessions | — | `<root>/sessions` |
| root | `DSH_HOME` | `~/.dsh` |
An empty `DSH_HOME` is treated as unset. `probeRoots()` reports the resolved sessions dir, so `codeburn doctor` distinguishes "dsh not installed" from "`DSH_HOME` pointing somewhere empty".
## Storage format
```
sessions/--<slugified-cwd>--/<session-id>/
session.jsonl.zstd default (compression: zstd)
session.jsonl when compression: none
```
Both variants are read; a session directory never holds both. The log is append-only JSONL whose first line is the session header:
```jsonc
{ "type": "session", "version": 0, "id": "...", "createdAt": 1783352050748,
"cwd": "/home/u/proj", "parentSession": "...", "seedLength": 3, "delegationDepth": 0 }
```
`cwd` becomes `projectPath` / `workingDirectory` (git-repo attribution) and its last segment the project name.
Every later line is one event `{ type, seq, time, data }`. The parser reads:
| Event | Used for |
|---|---|
| `turn/start` | current turn number |
| `user/message` | the turn's preview, when `data.source.kind === 'user'` |
| `request/header` | `data.header.config.model` — the model for steps that follow |
| `assistant/chunk` with `chunk.type === 'usage'` | streamed usage sample for `(turn, step)` |
| `assistant/message` | final usage for `(turn, step)`, plus `data.message.source.model` |
| `tool/call` | tool names, bash commands, skill names |
One parsed call per `(turn, step)` — one model call and the tools it requested. Dedup key: `dsh:<sessionId>:<turn>:<step>`.
`.zstd` logs are a concatenation of **independent** zstd frames, one per write batch, so they are decoded frame by frame behind a structural frame scan ported from `@deepseek-ai/dsh-session-persistence-jsonl`. Needs Node 22.15+ for `zlib.zstdDecompressSync`; below that dsh is skipped with a notice instead of counted as $0.
## Caching
None at the provider level; the log file is the cached source path and the normal parser/cache layers apply. Cache invalidates on `DSH_HOME` (`PROVIDER_ENV_VARS`) and on parser changes (`PROVIDER_PARSE_VERSIONS`).
## Quirks
- **DSH is a developer preview.** `SESSION_FORMAT_VERSION` is pinned at `0` with "no compatibility implied" upstream, and breaking changes are expected. The parser reads version `0` only and skips a log stamped with anything else, with a notice — reading a bumped format under today's assumptions would report confident wrong numbers. **A version bump upstream means this parser needs updating, not just relaxing the check.**
- **The JSONL backend only.** DSH also ships an opt-in SQLite persistence backend (`@deepseek-ai/dsh-session-persistence-sqlite`); it is not the default and is not read.
- **DSH records tokens, never dollars.** `usage` is `{ inputTokens, outputTokens, cacheReadTokens?, cacheWriteTokens?, reasoningTokens? }` with no cost field, so every call is priced from the shared tables. Reasoning bills at the output rate (same as Gemini and Hermes): `outputTokens + reasoningTokens` goes into `calculateCost`, while the two stay separate on the emitted call. Tokens are the provider's own exact counts, so `costIsEstimated` stays false.
- **`assistant/message` usage wins over the `assistant/chunk` sample** for the same `(turn, step)` — the two are adjacent reports of one API call, not two calls. A late chunk never overwrites a final report, so the two are never summed.
- **The model comes from the message, not the request.** `data.message.source.model` is what actually served the step; `request/header` only describes the request DSH was about to make, and is the fallback when a message names no model. The `provider` field there (`deepseek-official`) is the upstream LLM route, not the tool — the codeburn provider name is always `dsh`.
- **A forked session's log replays its parent's events.** The header's `parentSession` + `seedLength` mark that prefix; codeburn parses the parent's own log as its own session, so events with `seq < seedLength` are skipped to avoid billing the same calls twice.
- **`user/message` also carries agent-injected context** (runtime snapshots, skill bodies, file-change notices) under `source.kind: 'plugin'`. Only `kind: 'user'` messages become the preview.
- **Delta chunks are packed.** Runs of streamed deltas are stored as `text-chunks` / `reasoning-chunks` / `tool-call-chunks` storage rows rather than one event per line. They carry no usage and no tool identity the `tool/call` event lacks, so they are ignored — as is any event type the parser does not know.
- **A torn final zstd frame is ignored.** A crashed writer leaves an incomplete trailing frame; the complete frames before it parse normally. A structurally corrupt file is skipped whole with a notice rather than throwing.
## When fixing a bug here
1. Reproduce with a minimal session dir: `sessions/--proj--/<id>/session.jsonl` (uncompressed is easiest to hand-write).
2. `tests/fixtures/dsh/bash-tool-turn.jsonl` is the upstream `examples/acp-agent/tests/snapshots/bash-tool-turn/session.jsonl` snapshot with its template placeholders filled in — refresh it from the DSH repo when the format moves.
3. Run `tests/providers/dsh.test.ts`.
4. `.zstd` fixtures must compress **each batch separately**; one `zstdCompressSync` over the whole file is a single-frame layout DSH never writes.

View file

@ -4,7 +4,7 @@ Grok Build, xAI's coding CLI. Sessions use the `grok-build` model by default.
- **Source:** `src/providers/grok.ts`
- **Loading:** eager (`src/providers/index.ts`)
- **Test:** `tests/providers/grok.test.ts`
- **Test:** `tests/grok-parser-pipeline.test.ts`, `tests/providers/grok.test.ts`
## Where it reads from
@ -13,19 +13,23 @@ Grok Build, xAI's coding CLI. Sessions use the `grok-build` model by default.
## Storage format
JSON + JSONL. `summary.json` holds the session id, cwd, timestamps, and `current_model_id`. `signals.json` holds `modelsUsed`, `toolsUsed`, and `contextTokensUsed`. `updates.jsonl` is the ACP log: each streamed chunk carries `params._meta.totalTokens` (running context size) and `params._meta.promptId` (one per turn).
JSON + JSONL. `summary.json` holds the session id, cwd, timestamps, and `current_model_id`. `signals.json` holds `modelsUsed`, `toolsUsed`, and `contextTokensUsed`. `updates.jsonl` is the ACP log: streamed chunks carry `params._meta.totalTokens` (running context size) and `params._meta.promptId` (one per turn); newer CLI versions also append `params.update.sessionUpdate: "turn_completed"` with snake_case `prompt_id` and a provider-recorded `usage` object.
## Token model
**Estimated.** Grok does not log billable input/output tokens. It only records the running context fill (`totalTokens` per chunk, and `contextTokensUsed` in signals). The parser reconstructs a rough estimate from the per-turn `totalTokens` curve: input is the context entering each turn, output is the context growth during it. The result is flagged `costIsEstimated` and re-priced with `calculateCost`.
**Authoritative when available.** A `turn_completed` record reports the whole input side in `inputTokens` and the whole output side in `outputTokens`. `cachedReadTokens` is treated as a subset of input; `cacheCreationTokens` is treated as another input subset by analogy because the record exposes no separate fresh-input field, with any per-record violation clamped locally. The top-level totals are the accounting basis. `modelUsage` is retained only as a model-attribution signal; multi-model attribution is deliberately out of scope, so one session uses one selected model's rate. `reasoningTokens` is clamped to that record's reported output before the parser emits exclusive output plus reasoning, preserving the reported total through the cache pipeline. These counts are provider-recorded, so `costIsEstimated` is false for fully covered sessions while CodeBurn applies its own pricing table. `costUsdTicks` is ignored because its scale is undocumented.
**Estimated fallback.** Older sessions without any valid `turn_completed.usage` record use the running context fill (`totalTokens` per chunk) and the existing compaction-aware per-turn curve. That path remains flagged `costIsEstimated`; a completed record is never blended with the heuristic.
**Mixed sessions undercount.** The choice between the two paths is per session, not per turn. If a session has at least one usable `turn_completed` record, the whole session is billed from the summed records and any turn WITHOUT a record contributes nothing at all - its tokens are dropped, not estimated, so such a session reads low. The row is marked `costIsEstimated: true` rather than claiming full provider coverage. This is deliberate: blending the heuristic into real records would reintroduce the roughly 5x output over-count this parser exists to remove. It happens when a session straddles a CLI upgrade or a run dies before writing its last record; an open turn is filled by a later parse once it writes one, pre-upgrade turns never are. Measured on a 568-session corpus, 1 turn out of 566 was uncovered.
## Pricing
`grok-build` is aliased to `grok-build-0.1` in `src/models.ts`, so it prices off the bundled LiteLLM fallback. Note that xAI's published API rate and the LiteLLM fallback figure differ, so treat the cost as an estimate and verify against your xAI usage console.
`grok-build` is aliased to `grok-build-0.1` in `src/models.ts`, so it prices off the bundled LiteLLM fallback. If `usage.modelUsage` contains a model id that CodeBurn can price, that id is preferred; when the real id is not priced yet, the existing summary/signals model is retained so a known alias does not become a $0 row. This is a single attribution choice for the session, not a per-model accounting split; multi-model rate attribution is a follow-up. CodeBurn still does not use Grok's undocumented `costUsdTicks`.
## Caching
None.
Authoritative records expose cache-read and cache-creation token counts. The legacy estimate has no cache-creation signal and keeps its inferred cache-read count.
## Deduplication
@ -33,7 +37,8 @@ Per `grok:<session-dir>:<updated_at>:<id>`.
## Quirks
- **No cache or output/tool-token split.** Only context fill is available, so cache fields are `0` and the cost is an estimate (likely an upper bound, since re-sent context is cached server-side and not exposed in the session files).
- **Two token paths.** Completed turns carry provider usage; sessions from older CLI versions have only the context curve and therefore remain estimates (likely an upper bound, since re-sent context is cached server-side and not exposed in those files).
- **A turn with no `turn_completed` record is dropped inside an otherwise-covered session** (see Token model). The session still reports, marked estimated, but reads low by those turns.
- **No bash-command capture.** Tool names come from `signals.toolsUsed`; per-command bash text is not extracted, so `bashCommands` is empty.
- **Whole-session timestamp.** Spend is attributed to `updated_at`, since the context curve is cumulative.
- **Subscription vs API.** Grok Build runs via either a metered xAI API account (tiered) or a SuperGrok subscription; the session files do not record which.
@ -41,5 +46,5 @@ Per `grok:<session-dir>:<updated_at>:<id>`.
## When fixing a bug here
1. Discovery: check the `sessions/<cwd>/<uuid>/` walk and the `GROK_HOME` resolution.
2. Token estimate: see `estimateTokens` (groups `updates.jsonl` by `promptId`).
2. Token accounting: see `parseUpdates` (deduplicates `turn_completed` by snake_case `prompt_id`, then falls back to grouping streamed chunks by camelCase `_meta.promptId`).
3. Add a fixture-format session under `tests/providers/grok.test.ts`; do not mock the filesystem.

View file

@ -1053,7 +1053,7 @@ final class AppStore {
return false
} catch {
guard gen == claudeRefreshGen else { return false }
subscriptionError = sanitizeForUI(String(describing: error))
subscriptionError = sanitizeForUI(error.localizedDescription)
subscriptionLoadState = .failed
return false
}
@ -1064,11 +1064,18 @@ final class AppStore {
/// account or tier) starts clean. capacityEstimates and the snapshot store
/// would otherwise contaminate "Based on last cycle" projections.
func disconnectSubscription() {
ClaudeSubscriptionService.disconnect()
let result = ClaudeSubscriptionService.disconnect()
// Bump the generation token so any in-flight refreshSubscription that
// resumes after this point detects the disconnect and discards its
// result instead of re-populating the cleared state.
claudeRefreshGen &+= 1
guard result.isSuccess else {
// Nothing was removed, so nothing is disconnected. Leave the
// connected state exactly as it was the bootstrap flag is still
// set, Disconnect stays available, and the banner says to retry.
subscriptionError = "Could not fully remove the local Claude credential cache. Disconnect again to retry."
return
}
subscription = nil
subscriptionError = nil
subscriptionLoadState = .notBootstrapped
@ -1091,7 +1098,7 @@ final class AppStore {
} catch let err as CodexSubscriptionService.FetchError {
applyCodexFetchError(err)
} catch {
codexError = sanitizeForUI(String(describing: error))
codexError = sanitizeForUI(error.localizedDescription)
codexLoadState = .failed
}
}
@ -1124,15 +1131,21 @@ final class AppStore {
return false
} catch {
guard gen == codexRefreshGen else { return false }
codexError = sanitizeForUI(String(describing: error))
codexError = sanitizeForUI(error.localizedDescription)
codexLoadState = .failed
return false
}
}
func disconnectCodex() {
CodexSubscriptionService.disconnect()
let result = CodexSubscriptionService.disconnect()
codexRefreshGen &+= 1
guard result.isSuccess else {
// Nothing removed means nothing disconnected; keep state intact so
// Disconnect stays available for a retry.
codexError = "Could not fully remove the local Codex credential cache. Disconnect again to retry."
return
}
codexUsage = nil
codexError = nil
codexLoadState = .notBootstrapped
@ -1176,7 +1189,7 @@ final class AppStore {
applyKimiFetchError(err)
} catch {
guard gen == kimiRefreshGen else { return }
kimiError = sanitizeForUI(String(describing: error))
kimiError = sanitizeForUI(error.localizedDescription)
kimiLoadState = .failed
}
}
@ -1210,7 +1223,7 @@ final class AppStore {
return false
} catch {
guard gen == kimiRefreshGen else { return false }
kimiError = sanitizeForUI(String(describing: error))
kimiError = sanitizeForUI(error.localizedDescription)
kimiLoadState = .failed
return false
}

View file

@ -2,6 +2,7 @@ import Foundation
import SwiftUI
import AppKit
import Observation
import ServiceManagement
private let refreshIntervalSeconds: UInt64 = 30
private let forceRefreshWatchdogSeconds: TimeInterval = 90
@ -281,34 +282,19 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate, NSM
let key = "codeburn.loginItemRegistered"
guard !UserDefaults.standard.bool(forKey: key) else { return }
let appPath = Bundle.main.bundlePath
let script = "tell application \"System Events\" to make login item at end with properties {path:\(appleScriptStringLiteral(appPath)), hidden:false}"
let process = Process()
process.launchPath = "/usr/bin/osascript"
process.arguments = ["-e", script]
process.standardOutput = FileHandle.nullDevice
process.standardError = FileHandle.nullDevice
// Registers in-process. The old path told System Events to make the login
// item, which made macOS ask for Automation access on first launch (#1026).
// No AppleScript fallback: a failure here must not bring that prompt back.
do {
try process.run()
process.waitUntilExit()
if process.terminationStatus == 0 {
UserDefaults.standard.set(true, forKey: key)
if SMAppService.mainApp.status != .enabled {
try SMAppService.mainApp.register()
}
UserDefaults.standard.set(true, forKey: key)
} catch {
NSLog("CodeBurn: Login item registration failed: \(error)")
NSLog("CodeBurn: login item registration failed: \(error.localizedDescription)")
}
}
private func appleScriptStringLiteral(_ value: String) -> String {
var escaped = value.replacingOccurrences(of: "\\", with: "\\\\")
escaped = escaped.replacingOccurrences(of: "\"", with: "\\\"")
escaped = escaped.replacingOccurrences(of: "\r", with: "")
escaped = escaped.replacingOccurrences(of: "\n", with: "")
return "\"\(escaped)\""
}
private var lastRefreshTime: Date = .distantPast
/// Anchors the shallow provider-root snapshot only after a complete usage
/// refresh succeeds. It sits beside the cadence anchor so a failed fetch

View file

@ -16,9 +16,9 @@ import Security
/// the refresh endpoint. If the CLI hasn't rotated yet we report a
/// transient staleness (`sourceTokenStale`) and recover on its next use.
///
/// 3. **In-memory + file cache** so back-to-back reads in the same refresh
/// 3. **In-memory + Keychain cache** so back-to-back reads in the same refresh
/// cycle don't re-hit the source, and we keep serving the last good token
/// across launches.
/// across launches without a plaintext Application Support file.
enum ClaudeCredentialStore {
private static let bootstrapCompletedKey = "codeburn.claude.bootstrapCompleted"
private static let inMemoryTTL: TimeInterval = 5 * 60
@ -28,12 +28,46 @@ enum ClaudeCredentialStore {
private static let credentialsRelativePath = ".claude/.credentials.json"
private static let maxCredentialBytes = 64 * 1024
/// Legacy local cache file. New writes use the macOS Keychain; this path is
/// Legacy local cache file under Application Support. Migration to the
/// CodeBurn-namespaced Keychain item is staged behind an injectable seam.
private static let cacheFilename = "claude-credentials.v1.json"
static let ourKeychainService = CodeBurnKeychainIdentity.claudeService
static let ourKeychainAccount = CodeBurnKeychainIdentity.account
private static let lock = NSLock()
private nonisolated(unsafe) static var memoryCache: CachedRecord?
// MARK: - Injectable seams (tests + staged Keychain migration)
/// Override Application Support root. Nil uses the real user domain.
nonisolated(unsafe) static var applicationSupportDirectoryOverride: URL?
/// Override home for Claude CLI credential discovery. Nil uses the real home.
nonisolated(unsafe) static var homeDirectoryOverride: URL?
/// Override defaults used for bootstrap flags.
nonisolated(unsafe) static var userDefaultsOverride: UserDefaults?
/// Keychain backend. Production uses Live; tests inject InMemory.
nonisolated(unsafe) static var keychainCache: any KeychainCredentialCaching = LiveKeychainCredentialCache()
static func resetTestSeams() {
applicationSupportDirectoryOverride = nil
homeDirectoryOverride = nil
userDefaultsOverride = nil
keychainCache = LiveKeychainCredentialCache()
lastCacheDeleteResult = nil
unlinkLegacyOverride = nil
tightenLegacyOverride = nil
lock.withLock { memoryCache = nil }
}
private static var defaults: UserDefaults {
userDefaultsOverride ?? .standard
}
private static var homeDirectory: URL {
homeDirectoryOverride ?? FileManager.default.homeDirectoryForCurrentUser
}
struct CachedRecord {
let record: CredentialRecord
let cachedAt: Date
@ -88,18 +122,42 @@ enum ClaudeCredentialStore {
/// True once the user has explicitly connected (clicked Connect in the Plan
/// tab AND we successfully read their credentials). Persists across launches.
static var isBootstrapCompleted: Bool {
get { UserDefaults.standard.bool(forKey: bootstrapCompletedKey) }
set { UserDefaults.standard.set(newValue, forKey: bootstrapCompletedKey) }
get { defaults.bool(forKey: bootstrapCompletedKey) }
set { defaults.set(newValue, forKey: bootstrapCompletedKey) }
}
/// Reset bootstrap state. Used when the user explicitly wants to disconnect
/// or when the refresh token has been revoked terminally.
static func resetBootstrap() {
/// or when the refresh token has been revoked terminally. Deletion failures
/// are recorded on `lastCacheDeleteResult` callers must not claim the
/// local copy is gone when `isSuccess` is false.
@discardableResult
static func resetBootstrap() -> CacheDeleteResult {
lock.withLock { memoryCache = nil }
deleteOurCache()
isBootstrapCompleted = false
let result = deleteOurCache()
lastCacheDeleteResult = result
// A failed Keychain delete must not pretend the provider is disconnected.
// Clearing the flag hides Disconnect and orphans the remaining item.
if result.isSuccess {
isBootstrapCompleted = false
}
return result
}
/// Outcome of deleting CodeBurn-owned Claude cache material.
struct CacheDeleteResult: Equatable {
var keychainDeletedOrAbsent: Bool
var legacyDeletedOrAbsent: Bool
var isSuccess: Bool { keychainDeletedOrAbsent && legacyDeletedOrAbsent }
}
/// Last disconnect/cleanup result. Nil until the first delete attempt.
nonisolated(unsafe) static var lastCacheDeleteResult: CacheDeleteResult?
/// Test seam: force legacy unlink to throw so we can assert the 0600 repair.
nonisolated(unsafe) static var unlinkLegacyOverride: ((URL) throws -> Void)?
nonisolated(unsafe) static var tightenLegacyOverride: ((URL) throws -> Void)?
// MARK: - Public API
/// User-initiated entry point. Reads from Claude's source (PROMPTS for the
@ -124,7 +182,19 @@ enum ClaudeCredentialStore {
if let cached = lock.withLock({ memoryCache }), cached.isFresh {
return cached.record
}
if let stored = try readOurCache() {
let fetched: CredentialRecord?
do {
fetched = try readOurCache()
} catch let err as KeychainCredentialCacheError {
// A locked/denied keychain means "can't look right now", not "the
// item is gone". Serve the last known token and leave the bootstrap
// flag alone so we don't silently disconnect the user.
if case .unavailable = err {
return lock.withLock { memoryCache }?.record
}
throw err
}
if let stored = fetched {
cacheInMemory(stored)
return stored
}
@ -190,7 +260,7 @@ enum ClaudeCredentialStore {
}
private static func readClaudeFile() throws -> CredentialRecord? {
let url = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(credentialsRelativePath)
let url = homeDirectory.appendingPathComponent(credentialsRelativePath)
guard FileManager.default.fileExists(atPath: url.path) else { return nil }
let data = try SafeFile.read(from: url.path, maxBytes: maxCredentialBytes)
return try parseClaudeBlob(data: sanitizeClaudeBlob(data))
@ -305,37 +375,207 @@ enum ClaudeCredentialStore {
}
}
// MARK: - Local cache file (no keychain involvement)
// MARK: - Local cache (injectable Application Support + Keychain seam)
private static func cacheFileURL() -> URL {
let support = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
?? FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Library/Application Support")
static func cacheFileURL() -> URL {
let support = applicationSupportDirectoryOverride
?? FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
?? homeDirectory.appendingPathComponent("Library/Application Support")
return support
.appendingPathComponent("CodeBurn", isDirectory: true)
.appendingPathComponent(cacheFilename)
}
/// Production-used write path. Persists to the CodeBurn-namespaced Keychain
/// item. Claude never stores a refresh token in this cache. After a verified
/// Keychain read-back, any legacy JSON is unlinked (not "securely erased").
static func writeOurCache(record: CredentialRecord) throws {
let persisted = encodePersisted(record)
let data = try JSONEncoder().encode(persisted)
try keychainCache.upsert(
service: ourKeychainService,
account: ourKeychainAccount,
data: data
)
try verifyKeychainMatches(persisted)
tryUnlinkLegacyAfterVerifiedKeychain()
}
/// Cache shape stored in Keychain intentionally omits refreshToken.
struct PersistedCacheRecord: Codable, Equatable {
let accessToken: String
let expiresAt: Date?
let rateLimitTier: String?
}
private static func encodePersisted(_ record: CredentialRecord) -> PersistedCacheRecord {
PersistedCacheRecord(
accessToken: record.accessToken,
expiresAt: record.expiresAt,
rateLimitTier: record.rateLimitTier
)
}
private static func decodePersisted(_ data: Data) -> CredentialRecord? {
if let persisted = try? JSONDecoder().decode(PersistedCacheRecord.self, from: data) {
return CredentialRecord(
accessToken: persisted.accessToken,
refreshToken: nil,
expiresAt: persisted.expiresAt,
rateLimitTier: persisted.rateLimitTier
)
}
// Historical blobs may still include refreshToken; drop it on read.
if let legacy = try? JSONDecoder().decode(CredentialRecord.self, from: data) {
return CredentialRecord(
accessToken: legacy.accessToken,
refreshToken: nil,
expiresAt: legacy.expiresAt,
rateLimitTier: legacy.rateLimitTier
)
}
return nil
}
private static func verifyKeychainMatches(_ expected: PersistedCacheRecord) throws {
guard let data = try keychainCache.read(service: ourKeychainService, account: ourKeychainAccount),
let roundTrip = decodePersisted(data),
encodePersisted(roundTrip) == expected
else {
throw StoreError.keychainWriteFailed(-1)
}
}
/// Serializes migrate + unlink across processes so two menubar instances (or the
/// CLI) cannot race on the same legacy file. Only `SafeFile.Error` from acquiring
/// the lock is tolerated `readOurCacheLocked` never lets one escape, because
/// `readLegacyFile` swallows its own read failures.
private static func readOurCache() throws -> CredentialRecord? {
do {
return try SafeFile.withExclusiveLock(at: cacheFileURL().path + ".lock") {
try readOurCacheLocked()
}
} catch is SafeFile.Error {
return try readOurCacheLocked()
}
}
private static func readOurCacheLocked() throws -> CredentialRecord? {
if let data = try keychainCache.read(service: ourKeychainService, account: ourKeychainAccount),
let record = decodePersisted(data) {
// The Keychain item can predate the legacy file by a long way this
// service name has been in use since May 2026, so an upgrading install
// can hold a months-old item beside a file the old build wrote today.
// Adopt whichever expires later before unlinking anything.
if let fresher = legacyRecordIfNewer(than: record) {
try? writeOurCache(record: fresher)
return fresher
}
// Rewrite historical Claude blobs once without refreshToken.
if let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
object.keys.contains("refreshToken") {
try? writeOurCache(record: record)
} else {
tryUnlinkLegacyAfterVerifiedKeychain()
}
return record
}
return try migrateLegacyFileIfPresent()
}
/// Reads the legacy file only to compare recency. Returns it when it expires
/// strictly later than `record`; nil when absent, unreadable, or not newer.
private static func legacyRecordIfNewer(than record: CredentialRecord) -> CredentialRecord? {
guard let legacy = readLegacyFile() else { return nil }
guard let legacyExpiry = legacy.expiresAt else { return nil }
guard let currentExpiry = record.expiresAt else { return legacy }
return legacyExpiry > currentExpiry ? legacy : nil
}
/// Secure-read + decode the legacy JSON, dropping any refreshToken it holds.
/// Returns nil on symlink / ownership / chmod / decode failure, leaving the
/// file in place.
private static func readLegacyFile() -> CredentialRecord? {
let url = cacheFileURL()
guard FileManager.default.fileExists(atPath: url.path) else { return nil }
let data = try SafeFile.read(from: url.path, maxBytes: maxCredentialBytes)
guard let record = try? JSONDecoder().decode(CredentialRecord.self, from: data) else { return nil }
return record
guard let data = try? SafeFile.readAfterSecuringPermissions(
from: url.path,
maxBytes: maxCredentialBytes
) else { return nil }
guard let decoded = try? JSONDecoder().decode(CredentialRecord.self, from: data) else {
// Invalid data stays in place at 0600; do not delete.
return nil
}
return CredentialRecord(
accessToken: decoded.accessToken,
refreshToken: nil,
expiresAt: decoded.expiresAt,
rateLimitTier: decoded.rateLimitTier
)
}
private static func writeOurCache(record: CredentialRecord) throws {
try writeOurFileCache(record: record)
/// Secure-read legacy JSON, upsert Keychain, verify read-back, then unlink.
private static func migrateLegacyFileIfPresent() throws -> CredentialRecord? {
guard let migrated = readLegacyFile() else { return nil }
// Keychain write/read-back failure leaves the repaired 0600 legacy file
// in place; the next read retries the migration.
try? writeOurCache(record: migrated)
return migrated
}
private static func writeOurFileCache(record: CredentialRecord) throws {
/// Unlink the redundant legacy JSON. Called on every successful cache read,
/// so a failure here is retried on the next read without a sticky flag.
private static func tryUnlinkLegacyAfterVerifiedKeychain() {
let url = cacheFileURL()
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
let data = try JSONEncoder().encode(record)
try data.write(to: url, options: [.atomic, .completeFileProtection])
guard FileManager.default.fileExists(atPath: url.path) else { return }
do {
if let unlinkLegacyOverride {
try unlinkLegacyOverride(url)
} else {
try FileManager.default.removeItem(at: url)
}
} catch {
// Could not remove it at least make sure it is not world-readable.
if let tightenLegacyOverride {
try? tightenLegacyOverride(url)
} else {
try? SafeFile.tightenToOwnerReadWrite(at: url.path)
}
}
}
private static func deleteOurCache() {
try? FileManager.default.removeItem(at: cacheFileURL())
@discardableResult
private static func deleteOurCache() -> CacheDeleteResult {
var keychainOK = true
do {
try keychainCache.delete(service: ourKeychainService, account: ourKeychainAccount)
} catch {
keychainOK = false
}
var legacyOK = true
let url = cacheFileURL()
if FileManager.default.fileExists(atPath: url.path) {
do {
if let unlinkLegacyOverride {
try unlinkLegacyOverride(url)
} else {
try FileManager.default.removeItem(at: url)
}
} catch {
legacyOK = false
}
}
return CacheDeleteResult(
keychainDeletedOrAbsent: keychainOK,
legacyDeletedOrAbsent: legacyOK
)
}
/// Clears only the in-memory TTL cache (simulates process restart in tests).
static func clearMemoryCacheForTesting() {
lock.withLock { memoryCache = nil }
}
private static func cacheInMemory(_ record: CredentialRecord) {

View file

@ -99,9 +99,14 @@ enum ClaudeSubscriptionService {
}
/// Reset everything used on user-initiated disconnect.
static func disconnect() {
ClaudeCredentialStore.resetBootstrap()
clearUsageBlock()
/// Returns the delete outcome so callers only tear down UI state once the
/// credential material is actually gone. A failed delete leaves the usage
/// block intact too, so a retry starts from the same state.
@discardableResult
static func disconnect() -> ClaudeCredentialStore.CacheDeleteResult {
let result = ClaudeCredentialStore.resetBootstrap()
if result.isSuccess { clearUsageBlock() }
return result
}
// MARK: - Internal

View file

@ -5,7 +5,7 @@ import Security
/// ClaudeCredentialStore but reads from ~/.codex/auth.json Codex CLI
/// already stores its tokens as plaintext JSON in the home directory, so
/// no keychain prompt is involved on bootstrap. After the user clicks
/// Connect we cache a copy under ~/Library/Application Support/CodeBurn so
/// Connect we cache a CodeBurn-owned copy in the macOS Keychain so
/// we keep using rotated tokens after refresh.
enum CodexCredentialStore {
private static let bootstrapCompletedKey = "codeburn.codex.bootstrapCompleted"
@ -23,9 +23,38 @@ enum CodexCredentialStore {
private static let cacheFilename = "codex-credentials.v1.json"
static let ourKeychainService = CodeBurnKeychainIdentity.codexService
static let ourKeychainAccount = CodeBurnKeychainIdentity.account
private static let lock = NSLock()
private nonisolated(unsafe) static var memoryCache: CachedRecord?
// MARK: - Injectable seams (tests + staged Keychain migration)
nonisolated(unsafe) static var applicationSupportDirectoryOverride: URL?
nonisolated(unsafe) static var homeDirectoryOverride: URL?
nonisolated(unsafe) static var userDefaultsOverride: UserDefaults?
nonisolated(unsafe) static var keychainCache: any KeychainCredentialCaching = LiveKeychainCredentialCache()
static func resetTestSeams() {
applicationSupportDirectoryOverride = nil
homeDirectoryOverride = nil
userDefaultsOverride = nil
keychainCache = LiveKeychainCredentialCache()
lastCacheDeleteResult = nil
unlinkLegacyOverride = nil
tightenLegacyOverride = nil
lock.withLock { memoryCache = nil }
}
private static var defaults: UserDefaults {
userDefaultsOverride ?? .standard
}
private static var homeDirectory: URL {
homeDirectoryOverride ?? FileManager.default.homeDirectoryForCurrentUser
}
struct CachedRecord {
let record: CredentialRecord
let cachedAt: Date
@ -97,16 +126,31 @@ enum CodexCredentialStore {
// MARK: - Bootstrap state
static var isBootstrapCompleted: Bool {
get { UserDefaults.standard.bool(forKey: bootstrapCompletedKey) }
set { UserDefaults.standard.set(newValue, forKey: bootstrapCompletedKey) }
get { defaults.bool(forKey: bootstrapCompletedKey) }
set { defaults.set(newValue, forKey: bootstrapCompletedKey) }
}
static func resetBootstrap() {
static func resetBootstrap() -> CacheDeleteResult {
lock.withLock { memoryCache = nil }
deleteOurCache()
isBootstrapCompleted = false
let result = deleteOurCache()
lastCacheDeleteResult = result
if result.isSuccess {
isBootstrapCompleted = false
}
return result
}
struct CacheDeleteResult: Equatable {
var keychainDeletedOrAbsent: Bool
var legacyDeletedOrAbsent: Bool
var isSuccess: Bool { keychainDeletedOrAbsent && legacyDeletedOrAbsent }
}
nonisolated(unsafe) static var lastCacheDeleteResult: CacheDeleteResult?
nonisolated(unsafe) static var unlinkLegacyOverride: ((URL) throws -> Void)?
nonisolated(unsafe) static var tightenLegacyOverride: ((URL) throws -> Void)?
// MARK: - Public API
@discardableResult
@ -132,7 +176,18 @@ enum CodexCredentialStore {
if let cached = lock.withLock({ memoryCache }), cached.isFresh {
return cached.record
}
if let stored = try readOurCache() {
let fetched: CredentialRecord?
do {
fetched = try readOurCache()
} catch let err as KeychainCredentialCacheError {
// Locked/denied keychain: serve the last known token rather than
// reporting the grant as missing.
if case .unavailable = err {
return lock.withLock { memoryCache }?.record
}
throw err
}
if let stored = fetched {
cacheInMemory(stored)
return stored
}
@ -170,7 +225,7 @@ enum CodexCredentialStore {
// MARK: - Bootstrap source: ~/.codex/auth.json
private static func readCodexAuth() throws -> CredentialRecord {
let url = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(codexAuthPath)
let url = homeDirectory.appendingPathComponent(codexAuthPath)
guard FileManager.default.fileExists(atPath: url.path) else {
throw StoreError.bootstrapNoSource
}
@ -231,7 +286,7 @@ enum CodexCredentialStore {
/// key (OPENAI_API_KEY, auth_mode, ...) and only rewrites the tokens dict and
/// last_refresh. Keeps the CLI and the menubar on the same rotated grant.
private static func writeBackToCodexAuth(record: CredentialRecord) {
let url = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(codexAuthPath)
let url = homeDirectory.appendingPathComponent(codexAuthPath)
var json: [String: Any] = [:]
if let data = try? SafeFile.read(from: url.path, maxBytes: maxCredentialBytes),
let existing = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
@ -250,40 +305,152 @@ enum CodexCredentialStore {
guard let out = try? JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted, .sortedKeys]) else {
return
}
try? out.write(to: url, options: .atomic)
try? SafeFile.write(out, to: url.path, mode: 0o600)
}
// MARK: - Local cache file
// MARK: - Local cache (injectable Application Support + Keychain seam)
private static func cacheFileURL() -> URL {
let support = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
?? FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Library/Application Support")
static func cacheFileURL() -> URL {
let support = applicationSupportDirectoryOverride
?? FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
?? homeDirectory.appendingPathComponent("Library/Application Support")
return support
.appendingPathComponent("CodeBurn", isDirectory: true)
.appendingPathComponent(cacheFilename)
}
/// Production-used write path. Persists rotation fields to the CodeBurn
/// Keychain item. After verified read-back, unlinks any legacy JSON.
static func writeOurCache(record: CredentialRecord) throws {
let data = try JSONEncoder().encode(record)
try keychainCache.upsert(
service: ourKeychainService,
account: ourKeychainAccount,
data: data
)
guard let readBack = try keychainCache.read(service: ourKeychainService, account: ourKeychainAccount),
let roundTrip = try? JSONDecoder().decode(CredentialRecord.self, from: readBack),
roundTrip.accessToken == record.accessToken,
roundTrip.refreshToken == record.refreshToken,
roundTrip.idToken == record.idToken,
roundTrip.accountId == record.accountId
else {
throw StoreError.fileWriteFailed("keychain read-back mismatch")
}
tryUnlinkLegacyAfterVerifiedKeychain()
}
/// Serializes migrate + unlink across processes so two menubar instances (or the
/// CLI) cannot race on the same legacy file. Only `SafeFile.Error` from acquiring
/// the lock is tolerated `readOurCacheLocked` never lets one escape, because
/// `readLegacyFile` swallows its own read failures.
private static func readOurCache() throws -> CredentialRecord? {
do {
return try SafeFile.withExclusiveLock(at: cacheFileURL().path + ".lock") {
try readOurCacheLocked()
}
} catch is SafeFile.Error {
return try readOurCacheLocked()
}
}
private static func readOurCacheLocked() throws -> CredentialRecord? {
if let data = try keychainCache.read(service: ourKeychainService, account: ourKeychainAccount),
let record = try? JSONDecoder().decode(CredentialRecord.self, from: data) {
// Only reached when auth.json is unreadable, but the Keychain item can
// still be older than the legacy file and serving a spent rotating
// refresh token here ends in a terminal invalid_grant. Prefer the
// later `lastRefresh` before unlinking anything.
if let fresher = legacyRecordIfNewer(than: record) {
try? writeOurCache(record: fresher)
return fresher
}
tryUnlinkLegacyAfterVerifiedKeychain()
return record
}
return try migrateLegacyFileIfPresent()
}
/// Returns the legacy file's record when it refreshed strictly later than
/// `record`; nil when absent, unreadable, or not newer.
private static func legacyRecordIfNewer(than record: CredentialRecord) -> CredentialRecord? {
guard let legacy = readLegacyFile() else { return nil }
guard let legacyRefresh = legacy.lastRefresh else { return nil }
guard let currentRefresh = record.lastRefresh else { return legacy }
return legacyRefresh > currentRefresh ? legacy : nil
}
/// Secure-read + decode the legacy JSON. Returns nil on symlink / ownership /
/// chmod / decode failure, leaving the file in place.
private static func readLegacyFile() -> CredentialRecord? {
let url = cacheFileURL()
guard FileManager.default.fileExists(atPath: url.path) else { return nil }
let data = try SafeFile.read(from: url.path, maxBytes: maxCredentialBytes)
guard let record = try? JSONDecoder().decode(CredentialRecord.self, from: data) else { return nil }
return record
guard let data = try? SafeFile.readAfterSecuringPermissions(
from: url.path,
maxBytes: maxCredentialBytes
) else { return nil }
return try? JSONDecoder().decode(CredentialRecord.self, from: data)
}
private static func writeOurCache(record: CredentialRecord) throws {
try writeOurFileCache(record: record)
private static func migrateLegacyFileIfPresent() throws -> CredentialRecord? {
guard let decoded = readLegacyFile() else { return nil }
// Keychain write/read-back failure leaves the repaired 0600 legacy file
// in place; the next read retries the migration.
try? writeOurCache(record: decoded)
return decoded
}
private static func writeOurFileCache(record: CredentialRecord) throws {
/// Unlink the redundant legacy JSON. Called on every successful cache read,
/// so a failure here is retried on the next read without a sticky flag.
private static func tryUnlinkLegacyAfterVerifiedKeychain() {
let url = cacheFileURL()
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
let data = try JSONEncoder().encode(record)
try data.write(to: url, options: [.atomic, .completeFileProtection])
guard FileManager.default.fileExists(atPath: url.path) else { return }
do {
if let unlinkLegacyOverride {
try unlinkLegacyOverride(url)
} else {
try FileManager.default.removeItem(at: url)
}
} catch {
// Could not remove it at least make sure it is not world-readable.
if let tightenLegacyOverride {
try? tightenLegacyOverride(url)
} else {
try? SafeFile.tightenToOwnerReadWrite(at: url.path)
}
}
}
private static func deleteOurCache() {
try? FileManager.default.removeItem(at: cacheFileURL())
@discardableResult
private static func deleteOurCache() -> CacheDeleteResult {
var keychainOK = true
do {
try keychainCache.delete(service: ourKeychainService, account: ourKeychainAccount)
} catch {
keychainOK = false
}
var legacyOK = true
let url = cacheFileURL()
if FileManager.default.fileExists(atPath: url.path) {
do {
if let unlinkLegacyOverride {
try unlinkLegacyOverride(url)
} else {
try FileManager.default.removeItem(at: url)
}
} catch {
legacyOK = false
}
}
return CacheDeleteResult(
keychainDeletedOrAbsent: keychainOK,
legacyDeletedOrAbsent: legacyOK
)
}
static func clearMemoryCacheForTesting() {
lock.withLock { memoryCache = nil }
}
private static func cacheInMemory(_ record: CredentialRecord) {

View file

@ -78,9 +78,14 @@ enum CodexSubscriptionService {
}
}
static func disconnect() {
CodexCredentialStore.resetBootstrap()
clearUsageBlock()
/// Returns the delete outcome so callers only tear down UI state once the
/// credential material is actually gone. A failed delete leaves the usage
/// block intact too, so a retry starts from the same state.
@discardableResult
static func disconnect() -> CodexCredentialStore.CacheDeleteResult {
let result = CodexCredentialStore.resetBootstrap()
if result.isSuccess { clearUsageBlock() }
return result
}
private static func fetchWithToken(_ token: String, allowOne401Recovery: Bool) async throws -> CodexUsage {

View file

@ -72,6 +72,8 @@ enum UsageDataChangeGuard {
add(expand(environment["CODEWHALE_HOME"] ?? path(homeDirectory, ".codewhale"), homeDirectory: homeDirectory), scanFirstLevelDirectories: false)
add(path(homeDirectory, ".deepseek", "sessions"), scanFirstLevelDirectories: false)
add(path(homeDirectory, ".cline", "data"), scanFirstLevelDirectories: false)
let dshHome = expand(environment["DSH_HOME"] ?? path(homeDirectory, ".dsh"), homeDirectory: homeDirectory)
add(path(dshHome, "sessions"))
add(expand(environment["CODEBUFF_DATA_DIR"] ?? path(xdgConfig, "manicode"), homeDirectory: homeDirectory), scanFirstLevelDirectories: false)
let factoryHome = expand(environment["FACTORY_DIR"] ?? path(homeDirectory, ".factory"), homeDirectory: homeDirectory)
add(path(factoryHome, "sessions"), scanFirstLevelDirectories: false)

View file

@ -0,0 +1,248 @@
import Foundation
import LocalAuthentication
import Security
/// Serializes credential-store test harnesses that mutate process-wide seams.
enum CredentialStoreTestIsolation {
static let lock = NSLock()
}
/// Narrow CodeBurn-owned Keychain cache over exact service/account pairs.
///
/// Production uses `LiveKeychainCredentialCache`. Tests inject
/// `InMemoryKeychainCredentialCache` so the suite never touches the login
/// Keychain. Errors carry only operation, service, and OSStatus never blob data.
protocol KeychainCredentialCaching: Sendable {
func read(service: String, account: String) throws -> Data?
func upsert(service: String, account: String, data: Data) throws
func delete(service: String, account: String) throws
}
enum KeychainCredentialCacheError: Error, LocalizedError, Equatable {
case readFailed(service: String, status: OSStatus)
case writeFailed(service: String, status: OSStatus)
case deleteFailed(service: String, status: OSStatus)
/// Keychain is locked or consent was refused. Transient callers keep the
/// last known token and must not treat it as "the item is gone".
case unavailable(service: String, status: OSStatus)
var errorDescription: String? {
switch self {
case let .readFailed(service, status):
return "Keychain read failed for \(service) (status \(status))."
case let .writeFailed(service, status):
return "Keychain write failed for \(service) (status \(status))."
case let .deleteFailed(service, status):
return "Keychain delete failed for \(service) (status \(status))."
case .unavailable:
return "Keychain unavailable — unlock your login keychain to refresh quota."
}
}
/// Statuses that mean "we were not allowed to look right now", as opposed to
/// "the item does not exist". `errSecInteractionNotAllowed` (-25308) is what
/// a locked keychain returns once UI is suppressed.
static func isUnavailable(_ status: OSStatus) -> Bool {
status == errSecInteractionNotAllowed
|| status == errSecAuthFailed
|| status == errSecUserCanceled
|| status == errSecInteractionRequired
}
}
/// Published CodeBurn Keychain identities. Keep these exact Electron contracts
/// on the Codex pair (`app/electron/quota/codex.ts`), and installs going back to
/// May 2026 already hold items under these names.
///
/// Deliberately NOT derived from `CFBundleIdentifier`: the Electron app hardcodes
/// the same strings, so a per-bundle suffix would break that contract. The
/// tradeoff is that a dev/beta build sharing this source shares the item patch
/// these constants when running a second build alongside the release.
enum CodeBurnKeychainIdentity {
static let claudeService = "org.agentseal.codeburn.menubar.claude.oauth.v1"
static let codexService = "org.agentseal.codeburn.menubar.codex.oauth.v1"
static let account = "default"
}
struct LiveKeychainCredentialCache: KeychainCredentialCaching {
/// True when the default (login) keychain exists and is currently locked.
/// Returns false when the state cannot be determined, so an unexpected
/// failure degrades to "just try the read" rather than a hard outage.
///
/// `SecKeychain*` is soft-deprecated with no replacement that reports
/// file-keychain lock state `kSecUseDataProtectionKeychain` would move our
/// item to a different store and orphan every existing install. The
/// This is the one intentional deprecation warning in the file; annotating it
/// away only moves the warning to the call site, so it is left visible.
private func isDefaultKeychainLocked() -> Bool {
var status: SecKeychainStatus = 0
guard SecKeychainGetStatus(nil, &status) == errSecSuccess else { return false }
return (status & SecKeychainStatus(kSecUnlockStateStatus)) == 0
}
func read(service: String, account: String) throws -> Data? {
// Reads happen on the background refresh timer, so they must never be
// able to raise UI. Measured on macOS 15 against a locked test keychain:
// NEITHER `kSecUseAuthenticationUI: Fail` NOR
// `LAContext.interactionNotAllowed` suppresses the unlock panel for a
// file-based keychain both govern the data-protection keychain, while
// unlocking is a keychain-level operation securityd drives itself. The
// only thing that reliably avoids the panel is not issuing the read at
// all, so check lock state first. Same class of bug as the
// partition-list re-prompt in #490.
if isDefaultKeychainLocked() {
throw KeychainCredentialCacheError.unavailable(
service: service, status: errSecInteractionNotAllowed)
}
// Still pass a non-interactive context: it is the supported way to keep
// a data-protection-backed item from raising biometric/passcode UI.
let context = LAContext()
context.interactionNotAllowed = true
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecReturnData as String: true,
kSecUseAuthenticationContext as String: context,
]
var result: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &result)
if status == errSecItemNotFound { return nil }
if KeychainCredentialCacheError.isUnavailable(status) {
throw KeychainCredentialCacheError.unavailable(service: service, status: status)
}
guard status == errSecSuccess, let data = result as? Data else {
throw KeychainCredentialCacheError.readFailed(service: service, status: status)
}
return data
}
func upsert(service: String, account: String, data: Data) throws {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account,
]
let attributes: [String: Any] = [
kSecValueData as String: data,
]
let updateStatus = SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
if updateStatus == errSecSuccess { return }
if updateStatus == errSecItemNotFound {
var add = query
add[kSecValueData as String] = data
let addStatus = SecItemAdd(add as CFDictionary, nil)
if addStatus == errSecSuccess { return }
if addStatus == errSecDuplicateItem {
let retry = SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
guard retry == errSecSuccess else {
throw KeychainCredentialCacheError.writeFailed(service: service, status: retry)
}
return
}
throw KeychainCredentialCacheError.writeFailed(service: service, status: addStatus)
}
throw KeychainCredentialCacheError.writeFailed(service: service, status: updateStatus)
}
func delete(service: String, account: String) throws {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account,
]
let status = SecItemDelete(query as CFDictionary)
if status == errSecSuccess || status == errSecItemNotFound { return }
throw KeychainCredentialCacheError.deleteFailed(service: service, status: status)
}
}
/// Process-local fake for tests. Never writes to the system Keychain.
final class InMemoryKeychainCredentialCache: KeychainCredentialCaching, @unchecked Sendable {
private let lock = NSLock()
private var items: [String: Data] = [:]
private(set) var upsertCount = 0
private(set) var readCount = 0
private(set) var deleteCount = 0
private func key(_ service: String, _ account: String) -> String {
"\(service)\u{1f}\(account)"
}
func read(service: String, account: String) throws -> Data? {
lock.lock(); defer { lock.unlock() }
readCount += 1
return items[key(service, account)]
}
func upsert(service: String, account: String, data: Data) throws {
lock.lock(); defer { lock.unlock() }
upsertCount += 1
items[key(service, account)] = data
}
func delete(service: String, account: String) throws {
lock.lock(); defer { lock.unlock() }
deleteCount += 1
items.removeValue(forKey: key(service, account))
}
func storedJSONObject(service: String, account: String) -> [String: Any]? {
lock.lock(); defer { lock.unlock() }
guard let data = items[key(service, account)] else { return nil }
return (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
}
func storedKeys(service: String, account: String) -> [String]? {
storedJSONObject(service: service, account: account).map { Array($0.keys).sorted() }
}
/// Snapshot for simulated process restart: keep Keychain bytes, drop nothing else.
func cloneStorage() -> InMemoryKeychainCredentialCache {
lock.lock(); defer { lock.unlock() }
let copy = InMemoryKeychainCredentialCache()
copy.items = items
return copy
}
}
/// Test double that wraps another backend and can force upsert/read/delete failures.
final class ControllableKeychainCredentialCache: KeychainCredentialCaching, @unchecked Sendable {
private let inner: any KeychainCredentialCaching
var failUpsert = false
var failRead = false
var failDelete = false
var upsertStatus: OSStatus = -1
var readStatus: OSStatus = -1
var deleteStatus: OSStatus = -1
init(inner: any KeychainCredentialCaching) {
self.inner = inner
}
func read(service: String, account: String) throws -> Data? {
if failRead {
// Mirror the live adapter's mapping so tests exercise the same branch.
if KeychainCredentialCacheError.isUnavailable(readStatus) {
throw KeychainCredentialCacheError.unavailable(service: service, status: readStatus)
}
throw KeychainCredentialCacheError.readFailed(service: service, status: readStatus)
}
return try inner.read(service: service, account: account)
}
func upsert(service: String, account: String, data: Data) throws {
if failUpsert {
throw KeychainCredentialCacheError.writeFailed(service: service, status: upsertStatus)
}
try inner.upsert(service: service, account: account, data: data)
}
func delete(service: String, account: String) throws {
if failDelete {
throw KeychainCredentialCacheError.deleteFailed(service: service, status: deleteStatus)
}
try inner.delete(service: service, account: account)
}
}

View file

@ -21,6 +21,42 @@ enum SafeFile {
/// from exhausting memory in the Swift process.
static let defaultReadLimit = 8 * 1024 * 1024
/// Open the existing regular file with O_NOFOLLOW, fchmod 0600, and
/// fstat-verify the mode. Used when leftover credential JSON cannot be
/// unlinked after a verified Keychain write.
static func tightenToOwnerReadWrite(at path: String) throws {
var linkInfo = stat()
guard lstat(path, &linkInfo) == 0 else {
throw Error.readFailed(path, errno)
}
if (linkInfo.st_mode & S_IFMT) == S_IFLNK {
throw Error.symlinkDetected(path)
}
let fd = Darwin.open(path, O_RDONLY | O_NOFOLLOW)
guard fd >= 0 else {
throw Error.readFailed(path, errno)
}
defer { Darwin.close(fd) }
var opened = stat()
guard fstat(fd, &opened) == 0 else {
throw Error.readFailed(path, errno)
}
guard (opened.st_mode & S_IFMT) == S_IFREG else {
throw SecureReadError.notRegularFile(path)
}
if fchmod(fd, 0o600) != 0 {
throw SecureReadError.chmodFailed(path, errno)
}
var verified = stat()
guard fstat(fd, &verified) == 0 else {
throw Error.readFailed(path, errno)
}
let mode = verified.st_mode & 0o777
guard mode == 0o600 else {
throw SecureReadError.modeVerifyFailed(path, mode)
}
}
/// Refuses to follow symlinks and writes atomically via a tmp file + rename. `mode` is the
/// final file permission (0o600 by default so cache files stay user-private).
static func write(_ data: Data, to path: String, mode: mode_t = 0o600) throws {
@ -101,6 +137,96 @@ enum SafeFile {
return data
}
enum SecureReadError: Swift.Error, Equatable {
case notRegularFile(String)
case wrongOwner(String)
case chmodFailed(String, Int32)
case modeVerifyFailed(String, mode_t)
}
/// Legacy credential migration path: open with `O_NOFOLLOW`, refuse non-regular /
/// non-owned files, `fchmod(0600)` and verify mode, then read bounded bytes from
/// the same descriptor. Permissions are repaired before any secret byte is read.
///
/// The chmod deliberately precedes any content check: validating JSON first would
/// mean reading the secret while it is still world-readable, which is the exact
/// window this function exists to close. The cost is that a non-credential file
/// sitting at the caller's exact cache path also gets tightened to 0600 bounded
/// to our own Application Support directory, and already symlink- and owner-checked.
static func readAfterSecuringPermissions(
from path: String,
maxBytes: Int = defaultReadLimit,
expectedOwner: uid_t = geteuid()
) throws -> Data {
var linkInfo = stat()
guard lstat(path, &linkInfo) == 0 else {
throw Error.readFailed(path, errno)
}
if (linkInfo.st_mode & S_IFMT) == S_IFLNK {
throw Error.symlinkDetected(path)
}
guard (linkInfo.st_mode & S_IFMT) == S_IFREG else {
throw SecureReadError.notRegularFile(path)
}
guard linkInfo.st_uid == expectedOwner else {
throw SecureReadError.wrongOwner(path)
}
let fd = Darwin.open(path, O_RDONLY | O_NOFOLLOW)
guard fd >= 0 else {
throw Error.readFailed(path, errno)
}
defer { Darwin.close(fd) }
var opened = stat()
guard fstat(fd, &opened) == 0 else {
throw Error.readFailed(path, errno)
}
guard (opened.st_mode & S_IFMT) == S_IFREG else {
throw SecureReadError.notRegularFile(path)
}
guard opened.st_uid == expectedOwner else {
throw SecureReadError.wrongOwner(path)
}
if fchmod(fd, 0o600) != 0 {
throw SecureReadError.chmodFailed(path, errno)
}
var verified = stat()
guard fstat(fd, &verified) == 0 else {
throw Error.readFailed(path, errno)
}
let mode = verified.st_mode & 0o777
guard mode == 0o600 else {
throw SecureReadError.modeVerifyFailed(path, mode)
}
let size = Int(verified.st_size)
if size > maxBytes {
throw Error.sizeLimitExceeded(path, size)
}
var data = Data()
data.reserveCapacity(max(size, 0))
var chunk = [UInt8](repeating: 0, count: 4096)
let limit = maxBytes + 1
while data.count < limit {
let n = chunk.withUnsafeMutableBytes { buffer -> Int in
guard let base = buffer.baseAddress else { return 0 }
return Darwin.read(fd, base, min(buffer.count, limit - data.count))
}
guard n >= 0 else {
throw Error.readFailed(path, errno)
}
if n == 0 { break }
data.append(contentsOf: chunk.prefix(n))
}
if data.count > maxBytes {
throw Error.sizeLimitExceeded(path, data.count)
}
return data
}
/// Runs `body` while holding an exclusive POSIX advisory lock on `path`. The lock file is
/// created if missing (with 0o600 permissions) and released on scope exit, so other
/// codeburn processes (the CLI running in a terminal, say) block on the same file instead

View file

@ -508,7 +508,7 @@ private struct CodexSettingsTab: View {
CodexConnectionRow()
}
Section {
Text("Codex live-quota tracking reads `~/.codex/auth.json` once on Connect, then keeps a local copy under Application Support so subsequent quota fetches don't re-read the original. Only ChatGPT-mode auth (Plus / Pro / Team / Business / Edu / Enterprise) is supported. API-key users are billed per request and have a different reporting surface. Credit-metered workspaces report no rate-limit windows, so their monthly credit allowance is shown instead.")
Text("Codex live-quota tracking reads `~/.codex/auth.json` once on Connect, then keeps a CodeBurn-owned copy in your login Keychain instead of a world-readable file, so subsequent quota fetches don't re-read the original. The item is reachable by programs running as you, the same as any login-Keychain entry. Only ChatGPT-mode auth (Plus / Pro / Team / Business / Edu / Enterprise) is supported. API-key users are billed per request and have a different reporting surface. Credit-metered workspaces report no rate-limit windows, so their monthly credit allowance is shown instead.")
.font(.system(size: 11))
.foregroundStyle(.secondary)
} header: {

View file

@ -0,0 +1,149 @@
import Foundation
import Testing
@testable import CodeBurnMenubar
/// Red receipt for 0B: current plaintext writers must fail these assertions.
/// Disposable sentinels only never log or expect raw secret values in receipts.
@Suite("Credential Keychain cache red", .serialized)
struct CredentialKeychainCacheRedTests {
private let accessSentinel = "cb-red-access-sentinel"
private let refreshSentinel = "cb-red-refresh-sentinel"
private let idSentinel = "cb-red-id-sentinel"
private let accountSentinel = "cb-red-account-sentinel"
private func withIsolatedSeams(
_ body: (URL, InMemoryKeychainCredentialCache) throws -> Void
) throws {
CredentialStoreTestIsolation.lock.lock()
defer { CredentialStoreTestIsolation.lock.unlock() }
let root = FileManager.default.temporaryDirectory
.appendingPathComponent("codeburn-0b-red-\(UUID().uuidString)", isDirectory: true)
let support = root.appendingPathComponent("Application Support", isDirectory: true)
try FileManager.default.createDirectory(at: support, withIntermediateDirectories: true)
let suiteName = "codeburn.0b.red.\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suiteName)!
defaults.removePersistentDomain(forName: suiteName)
let fakeKeychain = InMemoryKeychainCredentialCache()
ClaudeCredentialStore.resetTestSeams()
CodexCredentialStore.resetTestSeams()
ClaudeCredentialStore.applicationSupportDirectoryOverride = support
CodexCredentialStore.applicationSupportDirectoryOverride = support
ClaudeCredentialStore.userDefaultsOverride = defaults
CodexCredentialStore.userDefaultsOverride = defaults
ClaudeCredentialStore.keychainCache = fakeKeychain
CodexCredentialStore.keychainCache = fakeKeychain
defer {
ClaudeCredentialStore.resetTestSeams()
CodexCredentialStore.resetTestSeams()
defaults.removePersistentDomain(forName: suiteName)
try? FileManager.default.removeItem(at: root)
}
try body(support, fakeKeychain)
}
private func posixMode(at url: URL) -> mode_t? {
var info = stat()
guard lstat(url.path, &info) == 0 else { return nil }
return info.st_mode & 0o777
}
private func jsonKeys(at url: URL) throws -> [String] {
let data = try Data(contentsOf: url)
let object = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])
return object.keys.sorted()
}
@Test("Claude writeOurCache leaves no JSON and stores Keychain payload without refreshToken")
func claudeWriteUsesKeychainWithoutRefreshToken() throws {
try withIsolatedSeams { support, fakeKeychain in
let record = ClaudeCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
)
try ClaudeCredentialStore.writeOurCache(record: record)
let legacyURL = ClaudeCredentialStore.cacheFileURL()
let legacyExists = FileManager.default.fileExists(atPath: legacyURL.path)
let mode = legacyExists ? posixMode(at: legacyURL) : nil
let fileKeys = legacyExists ? try jsonKeys(at: legacyURL) : []
let keychainKeys = fakeKeychain.storedKeys(
service: ClaudeCredentialStore.ourKeychainService,
account: ClaudeCredentialStore.ourKeychainAccount
)
let keychainObject = fakeKeychain.storedJSONObject(
service: ClaudeCredentialStore.ourKeychainService,
account: ClaudeCredentialStore.ourKeychainAccount
)
let hasRefreshKey = keychainObject?.keys.contains("refreshToken") == true
let refreshValueMatches = (keychainObject?["refreshToken"] as? String) == refreshSentinel
// Intended green behavior (must fail against current plaintext writer):
#expect(!legacyExists, "legacy Claude JSON must not be created under Application Support")
#expect(fakeKeychain.upsertCount >= 1, "fake Keychain must receive a Claude upsert")
#expect(keychainKeys != nil, "Claude Keychain payload must exist")
#expect(!(keychainKeys?.contains("refreshToken") ?? false), "Claude Keychain keys must omit refreshToken")
#expect(!hasRefreshKey && !refreshValueMatches, "Claude Keychain must not persist refreshToken")
// Red diagnostic (keys + mode only; never fixture values):
if legacyExists {
Issue.record(
Comment(rawValue: "RED Claude legacy present mode=\(String(mode.map { String($0, radix: 8) } ?? "nil")) keys=\(fileKeys.joined(separator: ","))")
)
}
if fakeKeychain.upsertCount == 0 {
Issue.record(Comment(rawValue: "RED Claude Keychain upsertCount=0"))
}
_ = support
}
}
@Test("Codex writeOurCache leaves no JSON and stores Keychain rotation fields")
func codexWriteUsesKeychainWithRotationFields() throws {
try withIsolatedSeams { support, fakeKeychain in
let record = CodexCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
idToken: idSentinel,
accountId: accountSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_100),
lastRefresh: Date(timeIntervalSince1970: 1_700_000_000)
)
try CodexCredentialStore.writeOurCache(record: record)
let legacyURL = CodexCredentialStore.cacheFileURL()
let legacyExists = FileManager.default.fileExists(atPath: legacyURL.path)
let mode = legacyExists ? posixMode(at: legacyURL) : nil
let fileKeys = legacyExists ? try jsonKeys(at: legacyURL) : []
let keychainKeys = fakeKeychain.storedKeys(
service: CodexCredentialStore.ourKeychainService,
account: CodexCredentialStore.ourKeychainAccount
)
let required = ["accessToken", "refreshToken", "idToken", "accountId", "lastRefresh"]
let missingRequired = required.filter { !(keychainKeys?.contains($0) ?? false) }
#expect(!legacyExists, "legacy Codex JSON must not be created under Application Support")
#expect(fakeKeychain.upsertCount >= 1, "fake Keychain must receive a Codex upsert")
#expect(keychainKeys != nil, "Codex Keychain payload must exist")
#expect(missingRequired.isEmpty, "Codex Keychain must retain rotation fields")
if legacyExists {
Issue.record(
Comment(rawValue: "RED Codex legacy present mode=\(String(mode.map { String($0, radix: 8) } ?? "nil")) keys=\(fileKeys.joined(separator: ","))")
)
}
if fakeKeychain.upsertCount == 0 {
Issue.record(Comment(rawValue: "RED Codex Keychain upsertCount=0"))
}
_ = support
}
}
}

View file

@ -0,0 +1,557 @@
import Foundation
import Testing
@testable import CodeBurnMenubar
/// Implementation-continuity tests for option-3 evidence bar.
/// Uses only InMemory/Controllable Keychain backends and temp Application Support
/// never the operator login Keychain or live credential files.
@Suite("Credential Keychain implementation continuity", .serialized)
struct CredentialKeychainContinuityTests {
private let accessSentinel = "cb-cont-access-sentinel"
private let refreshSentinel = "cb-cont-refresh-sentinel"
private let idSentinel = "cb-cont-id-sentinel"
private let accountSentinel = "cb-cont-account-sentinel"
private struct Harness {
let root: URL
let support: URL
let defaults: UserDefaults
let suiteName: String
let fakeKeychain: InMemoryKeychainCredentialCache
}
private func withHarness(
_ body: (Harness) throws -> Void
) throws {
CredentialStoreTestIsolation.lock.lock()
defer { CredentialStoreTestIsolation.lock.unlock() }
let root = FileManager.default.temporaryDirectory
.appendingPathComponent("codeburn-0b-cont-\(UUID().uuidString)", isDirectory: true)
let support = root.appendingPathComponent("Application Support", isDirectory: true)
try FileManager.default.createDirectory(at: support, withIntermediateDirectories: true)
let suiteName = "codeburn.0b.cont.\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suiteName)!
defaults.removePersistentDomain(forName: suiteName)
let fake = InMemoryKeychainCredentialCache()
ClaudeCredentialStore.resetTestSeams()
CodexCredentialStore.resetTestSeams()
ClaudeCredentialStore.applicationSupportDirectoryOverride = support
CodexCredentialStore.applicationSupportDirectoryOverride = support
ClaudeCredentialStore.homeDirectoryOverride = root
CodexCredentialStore.homeDirectoryOverride = root
ClaudeCredentialStore.userDefaultsOverride = defaults
CodexCredentialStore.userDefaultsOverride = defaults
ClaudeCredentialStore.keychainCache = fake
CodexCredentialStore.keychainCache = fake
defer {
ClaudeCredentialStore.resetTestSeams()
CodexCredentialStore.resetTestSeams()
defaults.removePersistentDomain(forName: suiteName)
try? FileManager.default.removeItem(at: root)
}
try body(Harness(root: root, support: support, defaults: defaults, suiteName: suiteName, fakeKeychain: fake))
}
private func posixMode(at url: URL) -> mode_t? {
var info = stat()
guard lstat(url.path, &info) == 0 else { return nil }
return info.st_mode & 0o777
}
private func writeLegacyClaude0644(record: ClaudeCredentialStore.CredentialRecord) throws {
let url = ClaudeCredentialStore.cacheFileURL()
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
let data = try JSONEncoder().encode(record)
try data.write(to: url)
try FileManager.default.setAttributes([.posixPermissions: 0o644], ofItemAtPath: url.path)
}
private func writeLegacyCodex0644(record: CodexCredentialStore.CredentialRecord) throws {
let url = CodexCredentialStore.cacheFileURL()
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
let data = try JSONEncoder().encode(record)
try data.write(to: url)
try FileManager.default.setAttributes([.posixPermissions: 0o644], ofItemAtPath: url.path)
}
// MARK: - Continuity lifecycle
@Test("Claude write → simulated restart → read → update → delete")
func claudeWriteRestartReadUpdateDelete() throws {
try withHarness { harness in
let initial = ClaudeCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
)
try ClaudeCredentialStore.writeOurCache(record: initial)
ClaudeCredentialStore.isBootstrapCompleted = true
#expect(!FileManager.default.fileExists(atPath: ClaudeCredentialStore.cacheFileURL().path))
let keys = harness.fakeKeychain.storedKeys(
service: ClaudeCredentialStore.ourKeychainService,
account: ClaudeCredentialStore.ourKeychainAccount
)
#expect(keys?.contains("refreshToken") != true)
#expect(keys?.contains("accessToken") == true)
// Simulated process restart: drop memory, keep Keychain bytes.
ClaudeCredentialStore.clearMemoryCacheForTesting()
let afterRestart = try #require(try ClaudeCredentialStore.currentRecord())
#expect(afterRestart.accessToken == accessSentinel)
#expect(afterRestart.refreshToken == nil)
let updated = ClaudeCredentialStore.CredentialRecord(
accessToken: accessSentinel + "-rotated",
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_100),
rateLimitTier: "default"
)
try ClaudeCredentialStore.writeOurCache(record: updated)
ClaudeCredentialStore.clearMemoryCacheForTesting()
let afterUpdate = try #require(try ClaudeCredentialStore.currentRecord())
#expect(afterUpdate.accessToken == accessSentinel + "-rotated")
let deleteResult = ClaudeCredentialStore.resetBootstrap()
#expect(deleteResult.isSuccess)
let afterDelete = try harness.fakeKeychain.read(
service: ClaudeCredentialStore.ourKeychainService,
account: ClaudeCredentialStore.ourKeychainAccount
)
#expect(afterDelete == nil)
let afterDisconnect = try ClaudeCredentialStore.currentRecord()
#expect(afterDisconnect == nil)
}
}
@Test("Codex write → simulated restart → read → update → delete")
func codexWriteRestartReadUpdateDelete() throws {
try withHarness { harness in
let initial = CodexCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
idToken: idSentinel,
accountId: accountSentinel,
expiresAt: nil,
lastRefresh: Date(timeIntervalSince1970: 1_700_000_000)
)
try CodexCredentialStore.writeOurCache(record: initial)
CodexCredentialStore.isBootstrapCompleted = true
#expect(!FileManager.default.fileExists(atPath: CodexCredentialStore.cacheFileURL().path))
let keys = harness.fakeKeychain.storedKeys(
service: CodexCredentialStore.ourKeychainService,
account: CodexCredentialStore.ourKeychainAccount
)
for required in ["accessToken", "refreshToken", "idToken", "accountId", "lastRefresh"] {
#expect(keys?.contains(required) == true)
}
CodexCredentialStore.clearMemoryCacheForTesting()
// Without ~/.codex/auth.json, currentRecord falls through to Keychain cache.
let afterRestart = try #require(try CodexCredentialStore.currentRecord())
#expect(afterRestart.accessToken == accessSentinel)
#expect(afterRestart.refreshToken == refreshSentinel)
let updated = CodexCredentialStore.CredentialRecord(
accessToken: accessSentinel + "-rotated",
refreshToken: refreshSentinel + "-rotated",
idToken: idSentinel,
accountId: accountSentinel,
expiresAt: nil,
lastRefresh: Date(timeIntervalSince1970: 1_700_000_800)
)
try CodexCredentialStore.writeOurCache(record: updated)
CodexCredentialStore.clearMemoryCacheForTesting()
let afterUpdate = try #require(try CodexCredentialStore.currentRecord())
#expect(afterUpdate.accessToken == accessSentinel + "-rotated")
#expect(afterUpdate.refreshToken == refreshSentinel + "-rotated")
let deleteResult = CodexCredentialStore.resetBootstrap()
#expect(deleteResult.isSuccess)
let afterDisconnect = try CodexCredentialStore.currentRecord()
#expect(afterDisconnect == nil)
}
}
// MARK: - Migration
@Test("successful Claude legacy 0644 migration unlinks JSON and omits refreshToken")
func claudeSuccessfulLegacyMigration() throws {
try withHarness { harness in
let legacy = ClaudeCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
)
try writeLegacyClaude0644(record: legacy)
#expect(posixMode(at: ClaudeCredentialStore.cacheFileURL()) == 0o644)
ClaudeCredentialStore.isBootstrapCompleted = true
let migrated = try #require(try ClaudeCredentialStore.currentRecord())
#expect(migrated.accessToken == accessSentinel)
#expect(migrated.refreshToken == nil)
#expect(!FileManager.default.fileExists(atPath: ClaudeCredentialStore.cacheFileURL().path))
#expect(harness.fakeKeychain.upsertCount >= 1)
let keys = harness.fakeKeychain.storedKeys(
service: ClaudeCredentialStore.ourKeychainService,
account: ClaudeCredentialStore.ourKeychainAccount
)
#expect(keys?.contains("refreshToken") != true)
}
}
@Test("failed Claude Keychain upsert leaves secured legacy file")
func claudeFailedMigrationKeepsLegacy() throws {
try withHarness { harness in
let controllable = ControllableKeychainCredentialCache(inner: harness.fakeKeychain)
controllable.failUpsert = true
ClaudeCredentialStore.keychainCache = controllable
let legacy = ClaudeCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
)
try writeLegacyClaude0644(record: legacy)
ClaudeCredentialStore.isBootstrapCompleted = true
let record = try #require(try ClaudeCredentialStore.currentRecord())
#expect(record.accessToken == accessSentinel)
#expect(FileManager.default.fileExists(atPath: ClaudeCredentialStore.cacheFileURL().path))
#expect(posixMode(at: ClaudeCredentialStore.cacheFileURL()) == 0o600)
#expect(harness.fakeKeychain.upsertCount == 0)
}
}
@Test("successful Codex legacy migration unlinks JSON and keeps rotation fields")
func codexSuccessfulLegacyMigration() throws {
try withHarness { harness in
let legacy = CodexCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
idToken: idSentinel,
accountId: accountSentinel,
expiresAt: nil,
lastRefresh: Date(timeIntervalSince1970: 1_700_000_000)
)
try writeLegacyCodex0644(record: legacy)
CodexCredentialStore.isBootstrapCompleted = true
let migrated = try #require(try CodexCredentialStore.currentRecord())
#expect(migrated.refreshToken == refreshSentinel)
#expect(!FileManager.default.fileExists(atPath: CodexCredentialStore.cacheFileURL().path))
let keys = harness.fakeKeychain.storedKeys(
service: CodexCredentialStore.ourKeychainService,
account: CodexCredentialStore.ourKeychainAccount
)
#expect(keys?.contains("refreshToken") == true)
#expect(keys?.contains("lastRefresh") == true)
}
}
@Test("symlink legacy Claude file is refused and left in place")
func claudeSymlinkLegacyRefused() throws {
try withHarness { _ in
let codeburnDir = ClaudeCredentialStore.cacheFileURL().deletingLastPathComponent()
try FileManager.default.createDirectory(at: codeburnDir, withIntermediateDirectories: true)
let target = codeburnDir.appendingPathComponent("not-a-cred.txt")
try Data("x".utf8).write(to: target)
let link = ClaudeCredentialStore.cacheFileURL()
try FileManager.default.createSymbolicLink(at: link, withDestinationURL: target)
ClaudeCredentialStore.isBootstrapCompleted = true
let afterSymlink = try ClaudeCredentialStore.currentRecord()
#expect(afterSymlink == nil)
#expect(FileManager.default.fileExists(atPath: link.path))
}
}
// MARK: - Disconnect / reinstall
@Test("disconnect not-found is success; delete failure is observable")
func disconnectIdempotentAndPartialFailure() throws {
try withHarness { harness in
let empty = ClaudeCredentialStore.resetBootstrap()
#expect(empty.isSuccess)
try ClaudeCredentialStore.writeOurCache(record: .init(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: nil,
rateLimitTier: nil
))
ClaudeCredentialStore.isBootstrapCompleted = true
let controllable = ControllableKeychainCredentialCache(inner: harness.fakeKeychain)
controllable.failDelete = true
ClaudeCredentialStore.keychainCache = controllable
let failed = ClaudeCredentialStore.resetBootstrap()
#expect(!failed.isSuccess)
#expect(failed.keychainDeletedOrAbsent == false)
#expect(ClaudeCredentialStore.lastCacheDeleteResult?.isSuccess == false)
#expect(ClaudeCredentialStore.isBootstrapCompleted == true)
}
}
@Test("failed unlink after verified Keychain repairs leftover JSON to 0600")
func failedUnlinkRepairsLegacyMode() throws {
try withHarness { _ in
let record = ClaudeCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
)
try ClaudeCredentialStore.writeOurCache(record: record)
try writeLegacyClaude0644(record: record)
ClaudeCredentialStore.isBootstrapCompleted = true
ClaudeCredentialStore.unlinkLegacyOverride = { _ in
throw POSIXError(.EPERM)
}
ClaudeCredentialStore.clearMemoryCacheForTesting()
_ = try ClaudeCredentialStore.currentRecord()
#expect(FileManager.default.fileExists(atPath: ClaudeCredentialStore.cacheFileURL().path))
#expect(posixMode(at: ClaudeCredentialStore.cacheFileURL()) == 0o600)
// No sticky flag: the next read retries the unlink on its own.
ClaudeCredentialStore.unlinkLegacyOverride = nil
ClaudeCredentialStore.clearMemoryCacheForTesting()
_ = try ClaudeCredentialStore.currentRecord()
#expect(!FileManager.default.fileExists(atPath: ClaudeCredentialStore.cacheFileURL().path))
}
}
@Test("failed tighten after failed unlink keeps leftover in place")
func failedTightenLeavesRetrySignal() throws {
try withHarness { _ in
let record = ClaudeCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
)
try ClaudeCredentialStore.writeOurCache(record: record)
try writeLegacyClaude0644(record: record)
ClaudeCredentialStore.isBootstrapCompleted = true
ClaudeCredentialStore.unlinkLegacyOverride = { _ in throw POSIXError(.EPERM) }
ClaudeCredentialStore.tightenLegacyOverride = { _ in throw POSIXError(.EPERM) }
ClaudeCredentialStore.clearMemoryCacheForTesting()
_ = try ClaudeCredentialStore.currentRecord()
#expect(FileManager.default.fileExists(atPath: ClaudeCredentialStore.cacheFileURL().path))
}
}
@Test("legacy-only disconnect failure keeps bootstrap so retry stays")
func legacyOnlyDisconnectKeepsBootstrap() throws {
try withHarness { _ in
try ClaudeCredentialStore.writeOurCache(record: .init(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: nil,
rateLimitTier: nil
))
try writeLegacyClaude0644(record: .init(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: nil,
rateLimitTier: nil
))
ClaudeCredentialStore.isBootstrapCompleted = true
ClaudeCredentialStore.unlinkLegacyOverride = { _ in throw POSIXError(.EPERM) }
let failed = ClaudeCredentialStore.resetBootstrap()
#expect(failed.keychainDeletedOrAbsent == true)
#expect(failed.legacyDeletedOrAbsent == false)
#expect(failed.isSuccess == false)
#expect(ClaudeCredentialStore.isBootstrapCompleted == true)
}
}
@Test("reinstall with empty Keychain and no legacy clears bootstrap on read")
func reinstallMissingCacheClearsBootstrap() throws {
try withHarness { _ in
ClaudeCredentialStore.isBootstrapCompleted = true
let missing = try ClaudeCredentialStore.currentRecord()
#expect(missing == nil)
#expect(ClaudeCredentialStore.isBootstrapCompleted == false)
}
}
// MARK: - Locked / denied Keychain
@Test("unavailable Keychain read is a miss, not a disconnect")
func unavailableKeychainKeepsBootstrap() throws {
try withHarness { harness in
try ClaudeCredentialStore.writeOurCache(record: .init(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
))
ClaudeCredentialStore.isBootstrapCompleted = true
let controllable = ControllableKeychainCredentialCache(inner: harness.fakeKeychain)
controllable.failRead = true
controllable.readStatus = errSecInteractionNotAllowed // -25308
ClaudeCredentialStore.keychainCache = controllable
ClaudeCredentialStore.clearMemoryCacheForTesting()
// Must not throw and must not clear bootstrap: a locked keychain is
// "can't look right now", not "the user disconnected".
#expect(try ClaudeCredentialStore.currentRecord() == nil)
#expect(ClaudeCredentialStore.isBootstrapCompleted == true)
}
}
@Test("a genuine read failure still surfaces as an error")
func nonUnavailableReadStillThrows() throws {
try withHarness { harness in
ClaudeCredentialStore.isBootstrapCompleted = true
let controllable = ControllableKeychainCredentialCache(inner: harness.fakeKeychain)
controllable.failRead = true
controllable.readStatus = errSecDecode
ClaudeCredentialStore.keychainCache = controllable
ClaudeCredentialStore.clearMemoryCacheForTesting()
#expect(throws: KeychainCredentialCacheError.self) {
_ = try ClaudeCredentialStore.currentRecord()
}
}
}
@Test("Keychain errors render a readable message, not a struct dump")
func keychainErrorMessageIsReadable() {
let unavailable = KeychainCredentialCacheError.unavailable(
service: ClaudeCredentialStore.ourKeychainService,
status: errSecInteractionNotAllowed
)
let text = unavailable.localizedDescription
#expect(text.contains("Keychain unavailable"))
// AppStore renders errors via localizedDescription; a struct dump would
// read "unavailable(service:" and leak the raw item name.
#expect(!text.contains("unavailable(service:"))
#expect(!text.contains(ClaudeCredentialStore.ourKeychainService))
}
// MARK: - Recency between Keychain item and legacy file
@Test("newer legacy file beats an older Keychain item and is then unlinked")
func newerLegacyFileWins() throws {
try withHarness { harness in
// Keychain item from an old build: already expired.
try ClaudeCredentialStore.writeOurCache(record: .init(
accessToken: "cb-stale-keychain-token",
refreshToken: nil,
expiresAt: Date(timeIntervalSince1970: 1_700_000_000),
rateLimitTier: "default"
))
// Legacy file written far later by the pre-migration build.
try writeLegacyClaude0644(record: .init(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
))
ClaudeCredentialStore.isBootstrapCompleted = true
ClaudeCredentialStore.clearMemoryCacheForTesting()
let record = try #require(try ClaudeCredentialStore.currentRecord())
#expect(record.accessToken == accessSentinel)
#expect(record.refreshToken == nil)
#expect(!FileManager.default.fileExists(atPath: ClaudeCredentialStore.cacheFileURL().path))
let keys = harness.fakeKeychain.storedKeys(
service: ClaudeCredentialStore.ourKeychainService,
account: ClaudeCredentialStore.ourKeychainAccount
)
#expect(keys?.contains("refreshToken") != true)
}
}
@Test("older legacy file loses to a newer Keychain item and is unlinked")
func olderLegacyFileLoses() throws {
try withHarness { _ in
try ClaudeCredentialStore.writeOurCache(record: .init(
accessToken: accessSentinel,
refreshToken: nil,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
))
try writeLegacyClaude0644(record: .init(
accessToken: "cb-stale-file-token",
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_700_000_000),
rateLimitTier: "default"
))
ClaudeCredentialStore.isBootstrapCompleted = true
ClaudeCredentialStore.clearMemoryCacheForTesting()
let record = try #require(try ClaudeCredentialStore.currentRecord())
#expect(record.accessToken == accessSentinel)
#expect(!FileManager.default.fileExists(atPath: ClaudeCredentialStore.cacheFileURL().path))
}
}
@Test("Codex prefers the legacy file with the later lastRefresh")
func codexNewerLegacyFileWins() throws {
try withHarness { _ in
try CodexCredentialStore.writeOurCache(record: .init(
accessToken: "cb-stale-codex-access",
refreshToken: "cb-stale-codex-refresh",
idToken: nil,
accountId: accountSentinel,
expiresAt: nil,
lastRefresh: Date(timeIntervalSince1970: 1_700_000_000)
))
try writeLegacyCodex0644(record: .init(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
idToken: idSentinel,
accountId: accountSentinel,
expiresAt: nil,
lastRefresh: Date(timeIntervalSince1970: 1_800_000_000)
))
CodexCredentialStore.isBootstrapCompleted = true
CodexCredentialStore.clearMemoryCacheForTesting()
let record = try #require(try CodexCredentialStore.currentRecord())
#expect(record.refreshToken == refreshSentinel)
#expect(!FileManager.default.fileExists(atPath: CodexCredentialStore.cacheFileURL().path))
}
}
@Test("corrupt Keychain plus valid legacy repairs the CodeBurn item")
func corruptKeychainRepairedFromLegacy() throws {
try withHarness { harness in
try harness.fakeKeychain.upsert(
service: ClaudeCredentialStore.ourKeychainService,
account: ClaudeCredentialStore.ourKeychainAccount,
data: Data("%not-json%".utf8)
)
let legacy = ClaudeCredentialStore.CredentialRecord(
accessToken: accessSentinel,
refreshToken: refreshSentinel,
expiresAt: Date(timeIntervalSince1970: 1_900_000_000),
rateLimitTier: "default"
)
try writeLegacyClaude0644(record: legacy)
ClaudeCredentialStore.isBootstrapCompleted = true
let repaired = try #require(try ClaudeCredentialStore.currentRecord())
#expect(repaired.accessToken == accessSentinel)
#expect(repaired.refreshToken == nil)
#expect(!FileManager.default.fileExists(atPath: ClaudeCredentialStore.cacheFileURL().path))
let object = harness.fakeKeychain.storedJSONObject(
service: ClaudeCredentialStore.ourKeychainService,
account: ClaudeCredentialStore.ourKeychainAccount
)
#expect(object?.keys.contains("refreshToken") != true)
}
}
}

View file

@ -89,38 +89,46 @@ struct DataClientProcessTests {
/// Concurrency + timeout smoke test: launch more hung subprocesses than
/// there are cooperative threads, all at once, with a short timeout, and
/// assert every call returns once the timeout kills its sleep.
/// assert every call returns because the timeout killed its sleep.
///
/// NOTE: this does NOT reproduce the production permanent deadlock (16/16
/// cooperative threads parked in waitUntilExit). In a short-lived unit-test
/// process libdispatch spins up replacement threads for blocked workers, so
/// even the old blocking-on-the-pool code completes here. The real deadlock
/// built up over ~2 days under the @MainActor refresh loop and is confirmed
/// by the live `sample`, not by this test. Kept as a guard that the
/// off-pool wait + timeout path stays correct under concurrency.
@Test("concurrent timed-out processes all complete")
func concurrentTimedOutProcessesAllComplete() {
/// cooperative threads parked in waitUntilExit). The real deadlock built up
/// over ~2 days under the @MainActor refresh loop and is confirmed by the
/// live `sample`, not by this test. Kept as a guard that the off-pool wait
/// + timeout path stays correct under concurrency.
///
/// The body must stay `async` and await the group directly. It used to
/// block on a `DispatchSemaphore` with a 15s deadline, on the claim that a
/// test body runs on a real thread. It does not: Swift Testing invokes even
/// synchronous test bodies from a task on the cooperative pool, so the wait
/// parked one of the pool's `activeProcessorCount` workers on the very work
/// it was waiting for. A 16-core dev box has slack, a 3-core CI runner does
/// not, and the wait expired with the group making no progress at all.
/// Keeping it `async` also lets the compiler reject the blocking wait,
/// which is unavailable from asynchronous contexts.
@Test("concurrent timed-out processes all complete", .timeLimit(.minutes(1)))
func concurrentTimedOutProcessesAllComplete() async {
let count = ProcessInfo.processInfo.activeProcessorCount * 2 + 4
let done = DispatchSemaphore(value: 0)
Task {
await withTaskGroup(of: Void.self) { group in
for _ in 0..<count {
group.addTask {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/sleep")
process.arguments = ["30"]
_ = try? await DataClient.runProcess(process, timeoutSeconds: 1, label: "sleep 30")
}
let codes = await withTaskGroup(of: Int32?.self) { group -> [Int32?] in
for _ in 0..<count {
group.addTask {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/sleep")
process.arguments = ["30"]
return try? await DataClient.runProcess(process, timeoutSeconds: 1, label: "sleep 30").exitCode
}
}
done.signal()
var out: [Int32?] = []
for await code in group { out.append(code) }
return out
}
// Wait on the test thread (a real thread, not the cooperative pool) so
// the deadlock is detectable even when the pool is fully starved.
let outcome = done.wait(timeout: .now() + 15)
#expect(outcome == .success, "runProcess deadlocked: \(count) concurrent CLIs starved the cooperative pool")
#expect(codes.count == count)
// A `sleep 30` cannot end within a 1s timeout on its own, so a signal
// status is proof the timeout fired and killed it rather than the child
// racing the deadline and exiting normally.
#expect(codes.allSatisfy { $0 == SIGTERM || $0 == SIGKILL },
"every hung process should be killed by its own timeout, got \(codes)")
}
/// A decode failure surfaces the CLI's actual stdout/stderr so a stray banner

View file

@ -0,0 +1,97 @@
import Foundation
import Security
import Testing
@testable import CodeBurnMenubar
/// The ONLY test that touches a real Keychain. Everything else in the suite runs
/// against `InMemoryKeychainCredentialCache`.
///
/// It writes to a throwaway service name (`menubar.selftest.oauth.v1`) that no
/// build ever reads, never touches the Claude/Codex production items, and deletes
/// what it created. If the login Keychain is locked or unavailable headless CI,
/// SSH session, no login Keychain the whole suite is SKIPPED rather than failed;
/// look for "live Keychain unavailable" in the output to tell a skip from a pass.
@Suite("Live Keychain adapter", .serialized)
struct LiveKeychainCredentialCacheTests {
private static let service = "org.agentseal.codeburn.menubar.selftest.oauth.v1"
private static let account = "selftest"
/// True when this machine can round-trip a generic password right now.
private static let isAvailable: Bool = {
let live = LiveKeychainCredentialCache()
do {
try live.upsert(service: service, account: account, data: Data("probe".utf8))
_ = try live.read(service: service, account: account)
try live.delete(service: service, account: account)
return true
} catch {
try? live.delete(service: service, account: account)
return false
}
}()
@Test("live adapter round-trips write → read → update → delete")
func liveRoundTrip() throws {
guard Self.isAvailable else {
print("SKIP: live Keychain unavailable on this host")
return
}
let live = LiveKeychainCredentialCache()
defer { try? live.delete(service: Self.service, account: Self.account) }
#expect(try live.read(service: Self.service, account: Self.account) == nil)
try live.upsert(service: Self.service, account: Self.account, data: Data(#"{"v":1}"#.utf8))
let first = try #require(try live.read(service: Self.service, account: Self.account))
#expect(String(data: first, encoding: .utf8) == #"{"v":1}"#)
// upsert must update in place, not duplicate.
try live.upsert(service: Self.service, account: Self.account, data: Data(#"{"v":2}"#.utf8))
let second = try #require(try live.read(service: Self.service, account: Self.account))
#expect(String(data: second, encoding: .utf8) == #"{"v":2}"#)
try live.delete(service: Self.service, account: Self.account)
#expect(try live.read(service: Self.service, account: Self.account) == nil)
// Deleting an absent item is success, so disconnect stays idempotent.
#expect(throws: Never.self) {
try live.delete(service: Self.service, account: Self.account)
}
}
/// Documents the measured behaviour that motivates the pre-flight lock check.
/// The locked-keychain case itself is deliberately NOT exercised at runtime:
/// reproducing it requires a keychain operation that raises a password panel
/// on the tester's screen. Measured once by hand on macOS 15 against a
/// throwaway keychain: with the keychain locked, `SecItemCopyMatching` blocks
/// on an unlock panel even when the query carries
/// `kSecUseAuthenticationUI: Fail` or a non-interactive `LAContext` both
/// govern the data-protection keychain, not file-keychain unlocking. Skipping
/// the read while locked is therefore the only reliable suppression.
@Test("unavailable statuses are classified as transient, not as a missing item")
func unavailableClassification() {
#expect(KeychainCredentialCacheError.isUnavailable(errSecInteractionNotAllowed))
#expect(KeychainCredentialCacheError.isUnavailable(errSecAuthFailed))
#expect(KeychainCredentialCacheError.isUnavailable(errSecUserCanceled))
#expect(KeychainCredentialCacheError.isUnavailable(errSecInteractionRequired))
// errSecItemNotFound is a real miss and must never be treated as transient.
#expect(!KeychainCredentialCacheError.isUnavailable(errSecItemNotFound))
#expect(!KeychainCredentialCacheError.isUnavailable(errSecDecode))
}
@Test("live reads never block on an interactive prompt")
func liveReadIsNonInteractive() throws {
guard Self.isAvailable else {
print("SKIP: live Keychain unavailable on this host")
return
}
let live = LiveKeychainCredentialCache()
defer { try? live.delete(service: Self.service, account: Self.account) }
try live.upsert(service: Self.service, account: Self.account, data: Data("x".utf8))
// The read carries a non-interactive LAContext, so it either returns or
// fails fast. A prompt would park this call until a human dismissed it.
let start = Date()
_ = try? live.read(service: Self.service, account: Self.account)
#expect(Date().timeIntervalSince(start) < 5)
}
}

View file

@ -9,6 +9,7 @@
},
"files": [
"dist",
"THIRD_PARTY_NOTICES.md",
"!dist/parse-worker.js.map"
],
"scripts": {
@ -20,6 +21,7 @@
"test": "vitest run tests --exclude \"tests/cache-refresh-lock*\"",
"test:locks": "vitest run tests/cache-refresh-lock.test.ts tests/cache-refresh-lock-corrupt-body.test.ts tests/cache-refresh-lock-process.test.ts --poolOptions.forks.singleFork=true",
"test:watch": "vitest tests --exclude \"tests/cache-refresh-lock*\"",
"verify:upgrade": "node scripts/upgrade-path/run.mjs",
"prepublishOnly": "npm run build"
},
"keywords": [
@ -33,6 +35,7 @@
"pi",
"codebuff",
"codewhale",
"dsh",
"ai-coding",
"token-usage",
"cost-tracking",

View file

@ -0,0 +1,130 @@
// Per-provider payload parity between the published CLI and this build.
//
// node scripts/upgrade-path/compare.mjs <baselineDir> <upgradedDir>
//
// Each dir holds the payloads run.mjs captured: `export.json` (per-call records,
// the token/call source) and `menubar.json` (the unrounded per-provider cost).
// Prints one row per provider and exits non-zero on a diff that is not expected.
//
// Expectations, and why:
// claude, codex, gemini, kiro, cursor parse identically either side of the
// upgrade. Calls and every token field must match EXACTLY; cost is allowed
// COST_TOLERANCE of drift because the two binaries carry different bundled
// LiteLLM price snapshots and only agree when the shared pricing cache in
// CODEBURN_CACHE_DIR is warm (which it is, unless the runner is offline).
// grok changed by design in #1015: usage now comes from the CLI's own
// turn_completed records instead of a context-curve estimate. The change
// is REPORTED, never asserted — not even directionally. On real corpora
// the changelog documents totals rising, but that is a property of real
// Grok sessions, and the direction here would only reflect how the
// generator happened to size its synthetic context curve against its
// synthetic usage records. Both sides must still count the same SESSIONS,
// which is the part the corpus can honestly establish.
// dsh did not exist in the published CLI. Reported; required to be absent
// in the baseline and present after the upgrade.
const EXACT = ['claude', 'codex', 'gemini', 'kiro', 'cursor']
const CHANGED_BY_DESIGN = ['grok']
const NEW_IN_THIS_RELEASE = ['dsh']
const COST_TOLERANCE = 0.005 // 0.5% relative
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
const [baseDir, upDir] = process.argv.slice(2)
if (!baseDir || !upDir) {
console.error('usage: compare.mjs <baselineDir> <upgradedDir>')
process.exit(2)
}
const TOKEN_FIELDS = ['inputTokens', 'outputTokens', 'reasoningTokens', 'cacheWriteTokens', 'cacheReadTokens']
function load(dir) {
const exported = JSON.parse(readFileSync(join(dir, 'export.json'), 'utf8'))
const menubar = JSON.parse(readFileSync(join(dir, 'menubar.json'), 'utf8'))
const byProvider = {}
for (const r of exported.records ?? []) {
const p = r.provider || 'unknown'
const acc = (byProvider[p] ??= { calls: 0, cost: 0, ...Object.fromEntries(TOKEN_FIELDS.map(f => [f, 0])) })
acc.calls++
acc.cost += r.cost ?? 0
for (const f of TOKEN_FIELDS) acc[f] += r[f] ?? 0
}
// Prefer the unrounded cost, keyed by the provider's internal id.
// `providerDetails` is the only place that pairing exists — the sibling
// `providers` map is keyed by lowercased display name. The per-record sum
// above stands in when a binary predates providerDetails; it is rounded per
// record, so it is the coarser of the two.
for (const d of menubar.current?.providerDetails ?? []) {
if (byProvider[d.id]) byProvider[d.id].cost = d.cost
}
return byProvider
}
const base = load(baseDir)
const up = load(upDir)
const providers = [...new Set([...Object.keys(base), ...Object.keys(up)])].sort()
const failures = []
const notes = []
const rows = []
const relDiff = (a, b) => (a === 0 && b === 0 ? 0 : Math.abs(b - a) / Math.max(Math.abs(a), Math.abs(b)))
const fmt = n => (Number.isInteger(n) ? String(n) : n.toFixed(6))
for (const name of providers) {
const b = base[name]
const u = up[name]
let verdict
if (NEW_IN_THIS_RELEASE.includes(name)) {
if (b) failures.push(`${name}: expected to be absent from the 0.9.20 baseline, but it reported ${b.calls} calls`)
else if (!u || u.calls === 0) failures.push(`${name}: new in this release but the upgraded run reported nothing`)
verdict = 'new (expected)'
} else if (!b || !u) {
failures.push(`${name}: present in ${b ? 'baseline' : 'upgraded'} only`)
verdict = 'MISSING'
} else if (CHANGED_BY_DESIGN.includes(name)) {
const bt = TOKEN_FIELDS.reduce((s, f) => s + b[f], 0)
const ut = TOKEN_FIELDS.reduce((s, f) => s + u[f], 0)
if (b.calls !== u.calls) failures.push(`${name}: usage accounting changed in #1015, but the session/call COUNT should not have: ${b.calls} != ${u.calls}`)
verdict = 'changed by design'
notes.push(`${name}: tokens ${bt} -> ${ut}, cost ${fmt(b.cost)} -> ${fmt(u.cost)} (#1015, expected; magnitude here is a property of the fixture, not evidence)`)
} else {
const diffs = []
if (b.calls !== u.calls) diffs.push(`calls ${b.calls} != ${u.calls}`)
for (const f of TOKEN_FIELDS) if (b[f] !== u[f]) diffs.push(`${f} ${b[f]} != ${u[f]}`)
const costDrift = relDiff(b.cost, u.cost)
if (costDrift > COST_TOLERANCE) diffs.push(`cost ${fmt(b.cost)} != ${fmt(u.cost)} (${(costDrift * 100).toFixed(3)}% > ${(COST_TOLERANCE * 100).toFixed(1)}%)`)
if (!EXACT.includes(name)) {
notes.push(`${name}: no expectation declared in compare.mjs; ${diffs.length ? diffs.join(', ') : 'identical'}`)
verdict = diffs.length ? 'differs (unclassified)' : 'identical'
} else if (diffs.length) {
failures.push(`${name}: ${diffs.join(', ')}`)
verdict = 'DIFFERS'
} else {
verdict = costDrift === 0 ? 'identical' : `identical (cost ${(costDrift * 100).toFixed(3)}% drift)`
}
}
rows.push({
provider: name,
calls: `${b?.calls ?? '-'} -> ${u?.calls ?? '-'}`,
tokens: `${b ? TOKEN_FIELDS.reduce((s, f) => s + b[f], 0) : '-'} -> ${u ? TOKEN_FIELDS.reduce((s, f) => s + u[f], 0) : '-'}`,
cost: `${b ? fmt(b.cost) : '-'} -> ${u ? fmt(u.cost) : '-'}`,
verdict,
})
}
const cols = ['provider', 'calls', 'tokens', 'cost', 'verdict']
const width = Object.fromEntries(cols.map(c => [c, Math.max(c.length, ...rows.map(r => r[c].length))]))
const line = r => cols.map(c => String(r[c]).padEnd(width[c])).join(' ')
console.log('')
console.log(line(Object.fromEntries(cols.map(c => [c, c.toUpperCase()]))))
console.log(cols.map(c => '-'.repeat(width[c])).join(' '))
for (const r of rows) console.log(line(r))
console.log('')
for (const n of notes) console.log(`note: ${n}`)
for (const f of failures) console.log(`FAIL: ${f}`)
console.log(failures.length ? `\nparity: ${failures.length} unexpected difference(s)` : '\nparity: ok')
process.exit(failures.length ? 1 : 0)

View file

@ -0,0 +1,377 @@
// Deterministic multi-provider fixture corpus for the upgrade-path check.
//
// node scripts/upgrade-path/gen-corpus.mjs <homeDir>
//
// Lays sessions out at each provider's DEFAULT path under <homeDir>, so the run
// only has to set HOME/USERPROFILE and no per-provider override var. Everything
// is seeded off a fixed constant: two invocations against the same day produce
// byte-identical files, which is what makes the worker-determinism and
// 0.9.20-vs-main payload comparisons meaningful.
//
// Day anchoring is the one thing that moves: sessions are dated relative to
// today so they land inside the daily cache's backfill window. That is fine —
// every comparison this corpus feeds happens inside a single run.
import { mkdirSync, writeFileSync, rmSync, existsSync } from 'node:fs'
import { join } from 'node:path'
import { createRequire } from 'node:module'
const require_ = createRequire(import.meta.url)
const HOME = process.argv[2]
if (!HOME) {
console.error('usage: gen-corpus.mjs <homeDir>')
process.exit(2)
}
// mulberry32 — same seed, same corpus.
let seedState = 0x9e3779b9
function rnd() {
seedState = (seedState + 0x6d2b79f5) | 0
let t = seedState
t = Math.imul(t ^ (t >>> 15), t | 1)
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
}
const pick = arr => arr[Math.floor(rnd() * arr.length)]
const between = (lo, hi) => lo + Math.floor(rnd() * (hi - lo))
// Day 0 = 92 days ago (UTC midnight); the corpus spans day 0..91.
const DAY_MS = 86_400_000
const SPAN_DAYS = 92
const day0 = Math.floor(Date.now() / DAY_MS) * DAY_MS - (SPAN_DAYS - 1) * DAY_MS
const at = (day, hour, min = 0, sec = 0) =>
new Date(day0 + day * DAY_MS + hour * 3_600_000 + min * 60_000 + sec * 1000)
const iso = d => d.toISOString()
const write = (path, body) => {
mkdirSync(join(path, '..'), { recursive: true })
writeFileSync(path, body)
}
const writeLines = (path, lines) => write(path, lines.join('\n') + '\n')
const PROJECTS = ['/work/api-gateway', '/work/billing', '/work/web app', '/work/infra']
// ── claude ───────────────────────────────────────────────────────────────────
// 204 transcripts across the span: 200 plain, 2 parent/sidechain pairs. One
// plain transcript carries a single line over 32 KB (the large-line scanner
// path); the parent/sidechain pairs exercise the v7 spawn-link capture that the
// migration has to carry forward.
const CLAUDE_MODELS = ['claude-sonnet-4-5', 'claude-opus-4-8', 'claude-haiku-4-5']
function claudeUser(sessionId, ts, cwd, text) {
return JSON.stringify({ type: 'user', sessionId, timestamp: iso(ts), cwd, gitBranch: 'main', message: { role: 'user', content: text } })
}
function claudeAssistant(sessionId, ts, cwd, msgId, model, usage, content) {
return JSON.stringify({
type: 'assistant', sessionId, timestamp: iso(ts), cwd, gitBranch: 'main',
message: { id: msgId, type: 'message', role: 'assistant', model, content, usage },
})
}
function claudeSession(sessionId, day, cwd, turns, opts = {}) {
const lines = []
for (let t = 0; t < turns; t++) {
const ts = at(day, 9 + (t % 8), (t * 7) % 60)
lines.push(claudeUser(sessionId, ts, cwd, `task ${t} for ${sessionId}`))
const content = [
{ type: 'text', text: `step ${t}` },
{ type: 'tool_use', id: `tu-${sessionId}-${t}`, name: t % 3 === 0 ? 'Edit' : 'Read', input: { file_path: `${cwd}/src/f${t}.ts` } },
]
// One line north of 32 KB, on the file the caller asked for it on.
if (opts.hugeLineAtTurn === t) content.push({ type: 'text', text: 'y'.repeat(40 * 1024) })
lines.push(claudeAssistant(sessionId, at(day, 9 + (t % 8), (t * 7) % 60, 30), cwd, `msg-${sessionId}-${t}`, pick(CLAUDE_MODELS), {
input_tokens: between(400, 4000),
output_tokens: between(40, 900),
cache_read_input_tokens: between(0, 20000),
cache_creation_input_tokens: between(0, 3000),
}, content))
}
return lines
}
function genClaude() {
const projectsDir = join(HOME, '.claude', 'projects')
let files = 0
for (let i = 0; i < 200; i++) {
const cwd = PROJECTS[i % PROJECTS.length]
const day = (i * 7) % SPAN_DAYS
const sid = `c-${String(i).padStart(4, '0')}`
const dirName = cwd.replace(/[/ ]/g, '-')
writeLines(join(projectsDir, dirName, `${sid}.jsonl`), claudeSession(sid, day, cwd, between(4, 14), i === 137 ? { hugeLineAtTurn: 2 } : {}))
files++
}
// Two parent transcripts, each spawning one subagent whose transcript lives
// under <parent-uuid>/subagents/agent-<id>.jsonl and is marked isSidechain.
for (let p = 0; p < 2; p++) {
const cwd = PROJECTS[p]
const dirName = cwd.replace(/[/ ]/g, '-')
const parent = `p-000${p}`
const agent = `a-000${p}`
const day = 40 + p * 10
const parentLines = claudeSession(parent, day, cwd, 5)
parentLines.push(JSON.stringify({
type: 'assistant', sessionId: parent, timestamp: iso(at(day, 12)), cwd,
message: { id: `m-spawn-${p}`, type: 'message', role: 'assistant', model: 'claude-sonnet-4-5', content: [{ type: 'tool_use', id: `toolu_spawn_${p}`, name: 'Agent', input: {} }], usage: { input_tokens: 120, output_tokens: 30 } },
}))
parentLines.push(JSON.stringify({
type: 'user', sessionId: parent, timestamp: iso(at(day, 12, 1)), cwd,
message: { role: 'user', content: [{ type: 'tool_result', tool_use_id: `toolu_spawn_${p}`, content: 'subagent done' }] },
toolUseResult: { status: 'completed', agentId: agent, content: 'subagent done' },
}))
parentLines.push(JSON.stringify({ type: 'pr-link', sessionId: parent, timestamp: iso(at(day, 12, 2)), cwd, prUrl: `https://github.com/acme/repo/pull/${100 + p}` }))
writeLines(join(projectsDir, dirName, `${parent}.jsonl`), parentLines)
files++
const side = []
for (let t = 0; t < 4; t++) {
side.push(JSON.stringify({ type: 'user', isSidechain: true, sessionId: parent, agentId: agent, timestamp: iso(at(day, 12, 3 + t)), cwd, message: { role: 'user', content: `sub task ${t}` } }))
side.push(JSON.stringify({
type: 'assistant', isSidechain: true, sessionId: parent, agentId: agent, timestamp: iso(at(day, 12, 3 + t, 20)), cwd,
message: { id: `sub-${p}-${t}`, type: 'message', role: 'assistant', model: 'claude-opus-4-8', content: [{ type: 'text', text: 'ok' }], usage: { input_tokens: between(800, 2000), output_tokens: between(100, 400), cache_read_input_tokens: between(0, 5000) } },
}))
}
writeLines(join(projectsDir, dirName, parent, 'subagents', `agent-${agent}.jsonl`), side)
write(join(projectsDir, dirName, parent, 'subagents', `agent-${agent}.meta.json`), JSON.stringify({ agentType: 'reviewer' }))
files++
}
return files
}
// ── codex ────────────────────────────────────────────────────────────────────
// token_count carries a CUMULATIVE total_token_usage; the parser diffs
// consecutive events, so the running totals below must only ever grow.
function genCodex() {
const root = join(HOME, '.codex', 'sessions')
let files = 0
for (let i = 0; i < 24; i++) {
const day = (i * 4) % SPAN_DAYS
const d = new Date(day0 + day * DAY_MS)
const cwd = PROJECTS[i % PROJECTS.length]
const sid = `codex-${String(i).padStart(3, '0')}`
const lines = [JSON.stringify({ type: 'session_meta', timestamp: iso(at(day, 10)), payload: { cwd, originator: 'codex-cli', session_id: sid, model: 'gpt-5.3-codex' } })]
const total = { input_tokens: 0, cached_input_tokens: 0, output_tokens: 0, reasoning_output_tokens: 0, total_tokens: 0 }
for (let t = 0; t < between(3, 9); t++) {
const ts = iso(at(day, 10, t * 5))
const last = { input_tokens: between(500, 6000), cached_input_tokens: between(0, 2000), output_tokens: between(50, 800), reasoning_output_tokens: between(0, 300), total_tokens: 0 }
last.total_tokens = last.input_tokens + last.output_tokens
for (const k of Object.keys(total)) total[k] += last[k]
lines.push(JSON.stringify({ type: 'event_msg', timestamp: ts, payload: { type: 'task_started' } }))
lines.push(JSON.stringify({ type: 'response_item', timestamp: ts, payload: { type: 'message', role: 'user', content: [{ type: 'input_text', text: `task ${t}` }] } }))
lines.push(JSON.stringify({ type: 'response_item', timestamp: ts, payload: { type: 'function_call', name: 'shell', call_id: `c${t}`, arguments: JSON.stringify({ command: 'ls' }) } }))
lines.push(JSON.stringify({ type: 'response_item', timestamp: ts, payload: { type: 'function_call_output', call_id: `c${t}` } }))
lines.push(JSON.stringify({ type: 'response_item', timestamp: ts, payload: { type: 'message', role: 'assistant', content: [{ type: 'output_text', text: 'done' }] } }))
lines.push(JSON.stringify({ type: 'event_msg', timestamp: ts, payload: { type: 'token_count', info: { last_token_usage: last, total_token_usage: { ...total } } } }))
lines.push(JSON.stringify({ type: 'event_msg', timestamp: ts, payload: { type: 'task_complete', duration_ms: 4000 } }))
}
const dir = join(root, String(d.getUTCFullYear()), String(d.getUTCMonth() + 1).padStart(2, '0'), String(d.getUTCDate()).padStart(2, '0'))
writeLines(join(dir, `rollout-${sid}.jsonl`), lines)
files++
}
return files
}
// ── gemini ───────────────────────────────────────────────────────────────────
function genGemini() {
const messages = []
for (let t = 0; t < 12; t++) {
messages.push({ id: `u${t}`, timestamp: iso(at(60, 9, t * 3)), type: 'user', content: `inspect ${t}` })
messages.push({
id: `g${t}`, timestamp: iso(at(60, 9, t * 3, 20)), type: 'gemini', content: 'reading files',
model: 'gemini-3.1-pro-preview',
tokens: { input: between(200, 3000), cached: between(0, 1000), output: between(30, 400), thoughts: between(0, 200) },
toolCalls: [{ id: `t${t}`, name: 'read_file', args: { path: 'src/index.ts' } }],
})
}
write(join(HOME, '.gemini', 'tmp', 'api-gateway', 'chats', 'session-upgrade-1.json'),
JSON.stringify({ sessionId: 'gemini-session-1', startTime: iso(at(60, 9)), messages }))
return 1
}
// ── kiro ─────────────────────────────────────────────────────────────────────
function genKiro() {
const dir = join(HOME, '.kiro', 'sessions', 'cli')
const id = 'kiro-upgrade-1'
const lines = []
for (let t = 0; t < 6; t++) {
lines.push(JSON.stringify({ kind: 'Prompt', data: { content: [{ kind: 'text', data: `add feature ${t}` }] } }))
lines.push(JSON.stringify({ kind: 'AssistantMessage', data: { content: [{ kind: 'text', data: `Done — added feature ${t} and its tests.` }] } }))
}
writeLines(join(dir, `${id}.jsonl`), lines)
write(join(dir, `${id}.json`), JSON.stringify({
session_id: id, cwd: '/work/billing',
created_at: iso(at(70, 10)), updated_at: iso(at(70, 11)),
session_state: {
rts_model_state: { model_info: { model_id: 'auto' } },
conversation_metadata: { user_turn_metadatas: [{ end_timestamp: iso(at(70, 11)), metering_usage: [] }] },
},
}))
return 2
}
// ── dsh ──────────────────────────────────────────────────────────────────────
// Written UNCOMPRESSED on purpose: node:zlib gained zstd in 22.15 and the
// package floor is 22.13, so the .zstd variant would silently drop out of the
// floor matrix leg and the two legs would not be comparable.
function genDsh() {
const cwd = '/work/api-gateway'
const encoded = `--${cwd.replace(/[/\\]/g, '-')}--`
const dir = join(HOME, '.dsh', 'sessions', encoded, 'session-upgrade-0001')
const lines = [
JSON.stringify({ type: 'session', version: 0, id: 'session-upgrade-0001', createdAt: at(75, 10).getTime(), cwd, delegationDepth: 0, agentPreset: 'cordis' }),
JSON.stringify({ type: 'request/header', seq: 1, time: at(75, 10).getTime(), data: { header: { config: { provider: 'deepseek-official', model: 'deepseek-v3.2', reasoningEffort: 'max', maxTokens: 256000 } } } }),
]
let seq = 2
for (let turn = 1; turn <= 8; turn++) {
const base = at(75, 10, turn * 5).getTime()
lines.push(JSON.stringify({ type: 'turn/start', seq: seq++, time: base, data: { turn } }))
lines.push(JSON.stringify({ type: 'user/message', seq: seq++, time: base + 100, data: { content: [{ type: 'text', text: `build ${turn}` }], source: { kind: 'user' }, role: 'user', id: `msg-${turn}` } }))
lines.push(JSON.stringify({ type: 'tool/call', seq: seq++, time: base + 200, data: { turn, step: 1, callId: `call_${turn}`, name: 'bash', arguments: JSON.stringify({ command: 'git status' }) } }))
lines.push(JSON.stringify({
type: 'assistant/message', seq: seq++, time: base + 900,
data: { turn, step: 1, message: { role: 'assistant', content: [{ type: 'text', text: 'done' }] }, usage: { inputTokens: between(2000, 20000), outputTokens: between(100, 900), cacheReadTokens: between(0, 5000), reasoningTokens: between(0, 600) } },
}))
}
writeLines(join(dir, 'session.jsonl'), lines)
return 1
}
// ── grok ─────────────────────────────────────────────────────────────────────
// Uses the authoritative `turn_completed.usage` records that #1015 switched to.
function genGrok() {
const cwd = '/work/infra'
const root = join(HOME, '.grok', 'sessions', encodeURIComponent(cwd))
let files = 0
for (let i = 0; i < 3; i++) {
const id = `019edf9c-0000-7000-8000-00000000000${i + 1}`
const day = 80 + i
const dir = join(root, id)
write(join(dir, 'summary.json'), JSON.stringify({
info: { id, cwd }, created_at: iso(at(day, 11)), updated_at: iso(at(day, 12)), last_active_at: iso(at(day, 12)),
num_messages: 12, current_model_id: 'grok-build', session_summary: 'repo work', generated_title: 'repo work',
}))
write(join(dir, 'signals.json'), JSON.stringify({
primaryModelId: 'grok-build', modelsUsed: ['grok-build'], toolsUsed: ['read_file', 'grep'],
contextTokensUsed: 40000, contextWindowTokens: 512000,
}))
const updates = []
let running = 0
for (let t = 0; t < 5; t++) {
// Streamed chunk carrying the running context counter. This is all the
// published CLI can see, and what it estimates from; main ignores it in
// favour of the turn_completed record below. Both are present in a real
// session, so the corpus carries both and the two versions have something
// to disagree about.
running += between(3000, 12000)
updates.push(JSON.stringify({
timestamp: iso(at(day, 11, t * 5)), method: 'session/update',
params: { sessionId: id, update: { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: `chunk ${t}` } }, _meta: { totalTokens: running, promptId: `p${t}`, updateType: 'AgentMessageChunk', modelId: 'grok-build' } },
}))
const usage = {
inputTokens: between(1000, 9000), outputTokens: between(80, 700), totalTokens: 0,
cachedReadTokens: between(0, 40000), cacheCreationTokens: between(0, 2000), reasoningTokens: between(0, 300),
modelCalls: 1, apiDurationMs: 1000, costUsdTicks: 125117780000, numTurns: 1,
}
usage.totalTokens = usage.inputTokens + usage.outputTokens
usage.modelUsage = { 'grok-4.6-build': { ...usage } }
updates.push(JSON.stringify({
timestamp: Math.floor(at(day, 11, t * 5).getTime() / 1000), method: '_x.ai/session/update',
params: { sessionId: id, update: { sessionUpdate: 'turn_completed', prompt_id: `p${t}`, usage }, _meta: { eventId: `event-${t}`, agentTimestampMs: at(day, 11, t * 5).getTime() } },
}))
}
writeLines(join(dir, 'updates.jsonl'), updates)
files += 3
}
return files
}
// ── cursor (WAL-mode SQLite) ─────────────────────────────────────────────────
// Left with an un-checkpointed -wal sidecar and NO -shm: that is what a live
// Cursor database looks like on disk, and it is the shape that made
// read-only opens fail before #1017.
function genCursor() {
let DatabaseSync
try { ({ DatabaseSync } = require_('node:sqlite')) } catch { return 0 }
const userDir = process.platform === 'darwin'
? join(HOME, 'Library', 'Application Support', 'Cursor', 'User')
: process.platform === 'win32'
? join(HOME, 'AppData', 'Roaming', 'Cursor', 'User')
: join(HOME, '.config', 'Cursor', 'User')
const globalDir = join(userDir, 'globalStorage')
mkdirSync(globalDir, { recursive: true })
const dbPath = join(globalDir, 'state.vscdb')
for (const suffix of ['', '-wal', '-shm']) rmSync(dbPath + suffix, { force: true })
const db = new DatabaseSync(dbPath)
db.exec('PRAGMA journal_mode=WAL')
db.exec('CREATE TABLE cursorDiskKV (key TEXT PRIMARY KEY, value BLOB)')
db.exec('CREATE TABLE ItemTable (key TEXT UNIQUE, value BLOB)')
const ins = db.prepare('INSERT INTO cursorDiskKV (key, value) VALUES (?, ?)')
const composers = []
for (let c = 0; c < 6; c++) {
const composerId = `composer-${c}`
composers.push({ composerId, name: `session-${c}`, unifiedMode: 'agent' })
ins.run(`composerData:${composerId}`, JSON.stringify({
promptTokenBreakdown: { totalUsedTokens: between(10000, 90000) },
createdAt: at(85, 9 + c).getTime(),
}))
for (let b = 0; b < 8; b++) {
const createdAt = iso(at(85, 9 + c, b * 4))
ins.run(`bubbleId:${composerId}:u${b}`, JSON.stringify({ type: 1, conversationId: composerId, createdAt, text: `ask ${b}`, codeBlocks: '[]' }))
ins.run(`bubbleId:${composerId}:a${b}`, JSON.stringify({
type: 2, conversationId: composerId, createdAt, text: `reply ${b}`, codeBlocks: '[]',
tokenCount: { inputTokens: between(300, 4000), outputTokens: between(40, 500) },
modelInfo: { modelName: 'claude-4.6-sonnet' },
requestId: `req-${c}-${b}`,
}))
}
}
// Checkpoint what is written so far, then stop auto-checkpointing and append
// more: the tail rows live only in the -wal the copy below carries.
db.exec('PRAGMA wal_checkpoint(TRUNCATE)')
db.exec('PRAGMA wal_autocheckpoint=0')
ins.run('composerData:composer-tail', JSON.stringify({ promptTokenBreakdown: { totalUsedTokens: 12345 }, createdAt: at(86, 9).getTime() }))
ins.run('bubbleId:composer-tail:a0', JSON.stringify({
type: 2, conversationId: 'composer-tail', createdAt: iso(at(86, 9)), text: 'tail reply', codeBlocks: '[]',
tokenCount: { inputTokens: 2222, outputTokens: 333 }, modelInfo: { modelName: 'claude-4.6-sonnet' },
}))
composers.push({ composerId: 'composer-tail', name: 'session-tail', unifiedMode: 'agent' })
db.close()
// Per-workspace DB naming the composers, plus the workspace.json that gives
// the project its name.
const wsDir = join(userDir, 'workspaceStorage', 'ws0000000000000000000000000000000')
mkdirSync(wsDir, { recursive: true })
for (const suffix of ['', '-wal', '-shm']) rmSync(join(wsDir, 'state.vscdb' + suffix), { force: true })
const wsDb = new DatabaseSync(join(wsDir, 'state.vscdb'))
wsDb.exec('CREATE TABLE ItemTable (key TEXT UNIQUE, value BLOB)')
wsDb.prepare('INSERT INTO ItemTable (key, value) VALUES (?, ?)').run('composer.composerData', JSON.stringify({ allComposers: composers }))
wsDb.close()
write(join(wsDir, 'workspace.json'), JSON.stringify({ folder: 'file:///work/billing' }))
return existsSync(dbPath + '-wal') ? 3 : 2
}
// ── run ──────────────────────────────────────────────────────────────────────
const counts = {
claude: genClaude(),
codex: genCodex(),
gemini: genGemini(),
kiro: genKiro(),
dsh: genDsh(),
grok: genGrok(),
cursor: genCursor(),
}
console.log(JSON.stringify(counts))

View file

@ -0,0 +1,422 @@
// Upgrade-path verification: prove that a cache written by the last PUBLISHED
// CLI survives this build's first run, on this platform, with this Node.
//
// npm run verify:upgrade
//
// What it does, in order:
// 1. generates a deterministic multi-provider corpus into an isolated HOME
// (whose path contains a space, because a real Windows HOME usually does)
// 2. installs codeburn@0.9.20 into an isolated global prefix and runs it,
// producing a genuine session-cache.v7 + daily-cache.v17
// 3. installs THIS build the same way and runs it against the SAME cache dir,
// through the npm bin shim rather than `node dist/cli.js`, so
// dist/parse-worker.js has to resolve from a symlinked entry point
// 4. asserts the migration landed and compares payloads per provider
// 5. serve --stdio smoke, worker determinism, warm-run stability
//
// Env: UPGRADE_PATH_WORK (work dir), UPGRADE_PATH_OLD (published version to
// upgrade from), UPGRADE_PATH_KEEP=1 to leave the work dir behind.
import { spawnSync, spawn } from 'node:child_process'
import { mkdirSync, rmSync, existsSync, readdirSync, statSync, readFileSync, writeFileSync, copyFileSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { tmpdir } from 'node:os'
const HERE = dirname(fileURLToPath(import.meta.url))
const REPO = join(HERE, '..', '..')
const OLD_VERSION = process.env['UPGRADE_PATH_OLD'] || '0.9.20'
const WORK = process.env['UPGRADE_PATH_WORK'] || join(tmpdir(), 'codeburn upgrade path')
// The published binary's cache versions. If a future baseline writes something
// else these two are the knobs to move, and the assertions below will say so.
const OLD_SESSION_CACHE = 'session-cache.v7.json'
const OLD_DAILY_CACHE = 'daily-cache.v17.json'
const NEW_SESSION_CACHE_DIR = 'session-cache.v9'
const NEW_DAILY_CACHE = 'daily-cache.v21.json'
const HOME = join(WORK, 'user home')
const PAYLOADS = join(WORK, 'payloads')
const CACHES = join(WORK, 'caches')
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'
let failures = 0
let skipped = 0
const step = msg => console.log(`\n=== ${msg}`)
const ok = msg => console.log(` ok ${msg}`)
const fail = msg => { failures++; console.log(` FAIL ${msg}`) }
const skip = msg => { skipped++; console.log(` skip ${msg}`) }
const check = (cond, msg) => (cond ? ok(msg) : fail(msg))
function run(cmd, args, opts = {}) {
const r = spawnSync(cmd, args, { encoding: 'utf8', maxBuffer: 1 << 29, shell: false, ...opts })
if (r.error) throw new Error(`${cmd} ${args.join(' ')}: ${r.error.message}`)
return r
}
// Node refuses to spawn a .cmd/.bat without a shell, and a shell spawn quotes
// nothing for you — so on Windows anything holding a space (every path here
// does, by design) has to be quoted by hand.
const quoteForShell = s => (process.platform === 'win32' && /[\s&|^]/.test(s) ? `"${s}"` : s)
function runShell(cmd, args, opts = {}) {
if (process.platform !== 'win32') return run(cmd, args, opts)
return run(quoteForShell(cmd), args.map(quoteForShell), { ...opts, shell: true })
}
function cliEnv(cacheDir, extra = {}) {
// Deliberately minimal: no provider override vars, so every provider resolves
// its own default path under the isolated HOME. APPDATA/LOCALAPPDATA are
// pinned under it too — several providers read them on Windows, and inheriting
// the runner's would let real (or leftover) data into the comparison.
const passthrough = {}
for (const k of ['PATH', 'PATHEXT', 'SystemRoot', 'ComSpec', 'windir', 'TEMP', 'TMP', 'NUMBER_OF_PROCESSORS']) {
if (process.env[k] !== undefined) passthrough[k] = process.env[k]
}
return {
...passthrough,
HOME, USERPROFILE: HOME, TZ: 'UTC', CODEBURN_CACHE_DIR: cacheDir,
APPDATA: join(HOME, 'AppData', 'Roaming'), LOCALAPPDATA: join(HOME, 'AppData', 'Local'),
...extra,
}
}
function cli(bin, args, cacheDir, extra = {}) {
const r = run(bin.cmd, [...bin.args, ...args], { env: cliEnv(cacheDir, extra), cwd: WORK })
if (r.status !== 0) throw new Error(`${args.join(' ')} exited ${r.status}\n${r.stderr?.slice(0, 4000)}`)
return r.stdout
}
// Install into an isolated global prefix, so the CLI runs from a location that
// has nothing to do with this checkout — which is what makes dist/parse-worker.js
// resolution worth testing. On POSIX npm's bin is a symlink INTO the package and
// we drive that directly. On Windows it is a .cmd shim, which Node will not
// spawn without a shell; the payload captures go through the installed
// dist/cli.js there and the shim itself is smoke-tested once, separately.
function installGlobal(prefix, spec) {
const r = runShell(npmCmd, ['install', '-g', '--prefix', prefix, spec, '--no-audit', '--no-fund', '--loglevel', 'error'], { cwd: WORK })
if (r.status !== 0) throw new Error(`npm install -g ${spec} exited ${r.status}\n${r.stdout}\n${r.stderr}`)
const symlink = join(prefix, 'bin', 'codeburn')
if (existsSync(symlink)) return { cmd: process.execPath, args: [symlink], shim: null }
const winCmd = join(prefix, 'codeburn.cmd')
const entry = join(prefix, 'node_modules', 'codeburn', 'dist', 'cli.js')
if (existsSync(entry)) return { cmd: process.execPath, args: [entry], shim: existsSync(winCmd) ? winCmd : null }
throw new Error(`no codeburn bin under ${prefix}`)
}
// The npm shim, exercised once. Needs a shell on Windows, so nothing with a
// space in it is passed through here.
function checkShim(bin, cacheDir) {
if (!bin.shim) { ok("CLI invoked through npm's bin symlink"); return }
const r = runShell(bin.shim, ['--version'], { env: cliEnv(cacheDir), cwd: WORK })
check(r.status === 0 && r.stdout.trim().length > 0, `npm .cmd shim runs: ${r.stdout.trim() || r.stderr?.slice(0, 200)}`)
}
// Captured payloads. `export` is the token/call source, `menubar-json` the
// unrounded per-provider cost; both are stable given a fixed corpus. --period all
// so the whole three-month corpus is in scope on both sides.
function capture(bin, cacheDir, outDir, extra = {}) {
mkdirSync(outDir, { recursive: true })
cli(bin, ['export', '--format', 'json', '--from', '2000-01-01', '--to', '2999-12-31', '-o', join(outDir, 'export.json')], cacheDir, extra)
const menubar = cli(bin, ['status', '--format', 'menubar-json', '--period', 'all', '--no-optimize', '--no-timeline'], cacheDir, extra)
writeFileSync(join(outDir, 'menubar.json'), menubar)
const status = cli(bin, ['status', '--format', 'json', '--period', 'all'], cacheDir, extra)
writeFileSync(join(outDir, 'status.json'), status)
return { menubar: JSON.parse(menubar), status: JSON.parse(status) }
}
// The one field that moves between two runs of the same payload.
const stripGenerated = obj => JSON.parse(JSON.stringify(obj, (k, v) => (k.startsWith('generated') ? undefined : v)))
// Shards carry real stat data and are published under a random filename, so
// "identical" means identical after normalizing both away.
function shardSnapshot(cacheDir) {
const dir = join(cacheDir, NEW_SESSION_CACHE_DIR)
if (!existsSync(dir)) return null
const out = {}
for (const name of readdirSync(dir).sort()) {
if (name === 'envelope.json') continue
const body = JSON.parse(readFileSync(join(dir, name), 'utf8'))
for (const entry of Object.values(body)) {
if (entry && typeof entry === 'object' && entry.fingerprint) {
delete entry.fingerprint.dev
delete entry.fingerprint.ino
delete entry.fingerprint.mtimeMs
}
}
// Key the bucket off the shard's provider.month prefix, dropping the nonce.
out[name.replace(/\.[0-9a-f]{16}\.json$/, '')] = sortDeep(body)
}
return out
}
function sortDeep(v) {
if (Array.isArray(v)) return v.map(sortDeep)
if (v && typeof v === 'object') return Object.fromEntries(Object.keys(v).sort().map(k => [k, sortDeep(v[k])]))
return v
}
function shardMtimes(cacheDir) {
const dir = join(cacheDir, NEW_SESSION_CACHE_DIR)
return Object.fromEntries(readdirSync(dir).sort().map(n => [n, statSync(join(dir, n)).mtimeMs]))
}
// ── 1. corpus ────────────────────────────────────────────────────────────────
step(`work dir: ${WORK}`)
try { rmSync(WORK, { recursive: true, force: true }) } catch (err) { console.log(` note could not clear the work dir (${err.code}); reusing it`) }
mkdirSync(HOME, { recursive: true })
mkdirSync(PAYLOADS, { recursive: true })
const gen = run(process.execPath, [join(HERE, 'gen-corpus.mjs'), HOME])
if (gen.status !== 0) { console.log(gen.stderr); process.exit(1) }
ok(`corpus generated: ${gen.stdout.trim()}`)
const upgradeCache = join(CACHES, 'upgrade')
mkdirSync(upgradeCache, { recursive: true })
// ── 2. baseline: the last published CLI ──────────────────────────────────────
step(`baseline: codeburn@${OLD_VERSION}`)
const oldBin = installGlobal(join(WORK, 'old'), `codeburn@${OLD_VERSION}`)
const oldVersion = cli(oldBin, ['--version'], upgradeCache).trim()
check(oldVersion === OLD_VERSION, `installed baseline reports ${oldVersion}`)
const baseline = capture(oldBin, upgradeCache, join(PAYLOADS, 'baseline'))
check(baseline.menubar.current.calls > 0, `baseline counted ${baseline.menubar.current.calls} calls across ${baseline.menubar.current.sessions} sessions`)
check(existsSync(join(upgradeCache, OLD_SESSION_CACHE)), `${OLD_VERSION} wrote ${OLD_SESSION_CACHE}`)
check(existsSync(join(upgradeCache, OLD_DAILY_CACHE)), `${OLD_VERSION} wrote ${OLD_DAILY_CACHE}`)
// ── 3. upgrade: this build, same cache dir, through the npm bin shim ─────────
step('upgrade: this build against the same cache dir')
const pack = runShell(npmCmd, ['pack', '--ignore-scripts', '--pack-destination', WORK, '--loglevel', 'error'], { cwd: REPO })
if (pack.status !== 0) { console.log(pack.stdout, pack.stderr); process.exit(1) }
const tarball = join(WORK, pack.stdout.trim().split('\n').pop().trim())
const newBin = installGlobal(join(WORK, 'new'), tarball)
ok(`this build installed as ${newBin.args[0] ?? newBin.cmd}`)
checkShim(newBin, upgradeCache)
const upgraded = capture(newBin, upgradeCache, join(PAYLOADS, 'upgraded'))
check(!existsSync(join(upgradeCache, OLD_SESSION_CACHE)), `${OLD_SESSION_CACHE} removed after the re-layout`)
check(existsSync(join(upgradeCache, NEW_SESSION_CACHE_DIR)), `${NEW_SESSION_CACHE_DIR}/ present`)
check(existsSync(join(upgradeCache, NEW_SESSION_CACHE_DIR, 'envelope.json')), `${NEW_SESSION_CACHE_DIR}/envelope.json present`)
const envelope = JSON.parse(readFileSync(join(upgradeCache, NEW_SESSION_CACHE_DIR, 'envelope.json'), 'utf8'))
check(envelope.version === 9 && Object.keys(envelope.providers ?? {}).length > 0,
`envelope at version ${envelope.version} with ${Object.keys(envelope.providers ?? {}).length} providers`)
check(readdirSync(join(upgradeCache, NEW_SESSION_CACHE_DIR)).some(n => n !== 'envelope.json'), 'shards published alongside the envelope')
check(existsSync(join(upgradeCache, NEW_DAILY_CACHE)), `${NEW_DAILY_CACHE} re-derived`)
check(existsSync(join(upgradeCache, OLD_DAILY_CACHE)), `${OLD_DAILY_CACHE} kept as the carry-forward baseline`)
const oldDays = JSON.parse(readFileSync(join(upgradeCache, OLD_DAILY_CACHE), 'utf8')).days.length
const newDays = JSON.parse(readFileSync(join(upgradeCache, NEW_DAILY_CACHE), 'utf8')).days.length
check(newDays >= oldDays, `daily history did not shrink: ${oldDays} -> ${newDays} days`)
// ── 4. payload parity ────────────────────────────────────────────────────────
step('payload parity vs the baseline')
const cmp = run(process.execPath, [join(HERE, 'compare.mjs'), join(PAYLOADS, 'baseline'), join(PAYLOADS, 'upgraded')], { stdio: 'inherit' })
if (cmp.status !== 0) failures++
// ── 5. serve smoke ───────────────────────────────────────────────────────────
step('serve --stdio')
const serveFrames = await serveSmoke()
if (serveFrames) {
for (const [name, args] of [['menubar-json', ['status', '--format', 'menubar-json', '--period', 'all', '--no-optimize', '--no-timeline']], ['models', ['models', '--format', 'json']]]) {
const frame = serveFrames.get(name)
if (!frame?.ok) { fail(`serve returned no ok frame for ${name}: ${JSON.stringify(frame)}`); continue }
ok(`serve ok frame for ${name}`)
const oneShot = cli(newBin, args, upgradeCache)
const a = JSON.stringify(stripGenerated(JSON.parse(frame.output)))
const b = JSON.stringify(stripGenerated(JSON.parse(oneShot)))
check(a === b, `serve ${name} matches the one-shot payload (ignoring generated*)`)
}
}
async function serveSmoke() {
const child = spawn(newBin.cmd, [...newBin.args, 'serve', '--stdio'], { env: cliEnv(upgradeCache), cwd: WORK, stdio: ['pipe', 'pipe', 'pipe'] })
const frames = new Map()
let buf = ''
let ready = false
const done = new Promise(resolve => {
child.stdout.on('data', d => {
buf += d
let nl
while ((nl = buf.indexOf('\n')) >= 0) {
const line = buf.slice(0, nl); buf = buf.slice(nl + 1)
if (!line.trim()) continue
let msg
try { msg = JSON.parse(line) } catch { continue }
if (msg.ready) { ready = true; continue }
if (msg.progress !== undefined) continue
if (msg.id === 1) frames.set('menubar-json', msg)
if (msg.id === 2) frames.set('models', msg)
if (frames.size === 2) resolve()
}
})
})
child.stdin.write(JSON.stringify({ id: 1, args: ['status', '--format', 'menubar-json', '--period', 'all', '--no-optimize', '--no-timeline'] }) + '\n')
child.stdin.write(JSON.stringify({ id: 2, args: ['models', '--format', 'json'] }) + '\n')
const timeout = new Promise(r => setTimeout(() => r('timeout'), 240_000))
if ((await Promise.race([done, timeout])) === 'timeout') { child.kill(); fail('serve did not answer both requests within 240s'); return null }
check(ready, 'serve announced itself with a ready frame')
// Closing stdin is the documented shutdown: the child must exit on its own.
const exited = new Promise(r => child.once('exit', code => r(code)))
child.stdin.end()
const exitCode = await Promise.race([exited, new Promise(r => setTimeout(() => r('hung'), 30_000))])
if (exitCode === 'hung') { child.kill(); fail('serve did not exit when stdin closed') }
else ok(`serve exited on stdin close (code ${exitCode})`)
return frames
}
// ── 6. worker determinism ────────────────────────────────────────────────────
step('parse-worker determinism (CODEBURN_PARSE_WORKERS 0 vs 3)')
const serialCache = join(CACHES, 'workers-0')
const parallelCache = join(CACHES, 'workers-3')
for (const dir of [serialCache, parallelCache]) {
mkdirSync(dir, { recursive: true })
// Seed the shared price table so the two runs cannot be priced differently by
// a cache expiring between them. Parsing is unaffected either way.
const priced = join(upgradeCache, 'litellm-pricing.json')
if (existsSync(priced)) copyFileSync(priced, join(dir, 'litellm-pricing.json'))
}
const serialOut = capture(newBin, serialCache, join(PAYLOADS, 'workers-0'), { CODEBURN_PARSE_WORKERS: '0', CODEBURN_VERBOSE: '1' })
const parallelOut = capture(newBin, parallelCache, join(PAYLOADS, 'workers-3'), { CODEBURN_PARSE_WORKERS: '3', CODEBURN_VERBOSE: '1' })
check(JSON.stringify(stripGenerated(serialOut.menubar)) === JSON.stringify(stripGenerated(parallelOut.menubar)),
'menubar-json payload identical with and without workers')
const readExport = dir => stripGenerated(JSON.parse(readFileSync(join(PAYLOADS, dir, 'export.json'), 'utf8')))
check(JSON.stringify(readExport('workers-0')) === JSON.stringify(readExport('workers-3')),
'export payload identical with and without workers')
check(JSON.stringify(shardSnapshot(serialCache)) === JSON.stringify(shardSnapshot(parallelCache)),
'shard bodies identical with and without workers (fingerprint stat data and shard nonces normalized)')
// A forced pool that never actually spawned would make the check above vacuous.
const verbose = run(newBin.cmd, [...newBin.args, 'status', '--format', 'json', '--period', 'all'], {
env: cliEnv(join(CACHES, 'workers-probe'), { CODEBURN_PARSE_WORKERS: '3', CODEBURN_VERBOSE: '1' }), cwd: WORK,
})
const decision = (verbose.stderr || '').split('\n').filter(l => l.includes('parse workers='))
if (decision.length === 0) skip('no "parse workers=" line on stderr; cannot confirm the pool was forced')
else check(decision.some(l => /parse workers=[1-9]/.test(l)), `worker pool engaged: ${decision.map(l => l.trim()).join(' | ')}`)
// ── 7. second run is warm ────────────────────────────────────────────────────
step('second run is warm')
const beforeBodies = shardSnapshot(upgradeCache)
const beforeMtimes = shardMtimes(upgradeCache)
const warm = capture(newBin, upgradeCache, join(PAYLOADS, 'warm'))
// The direct no-re-parse signal: the worker gate prints how many whole-file
// re-parses are pending. On an unchanged corpus that must be zero for the two
// providers big enough to be gated.
const warmVerbose = run(newBin.cmd, [...newBin.args, 'status', '--format', 'menubar-json', '--period', 'all', '--no-optimize', '--no-timeline'], {
env: cliEnv(upgradeCache, { CODEBURN_VERBOSE: '1' }), cwd: WORK,
})
const pending = (warmVerbose.stderr || '').split('\n').filter(l => l.includes('parse workers='))
if (pending.length === 0) skip('no "parse workers=" line on a warm run; cannot confirm nothing re-parsed')
else check(pending.every(l => /0 pending files|no full parses pending/.test(l)),
`nothing re-parsed on the warm run: ${pending.map(l => l.replace(/^codeburn: /, '').trim()).join(' | ')}`)
check(JSON.stringify(shardSnapshot(upgradeCache)) === JSON.stringify(beforeBodies), 'warm run left every shard body unchanged')
check(JSON.stringify(stripGenerated(warm.menubar)) === JSON.stringify(stripGenerated(upgraded.menubar)),
'warm run reports the same payload as the run that migrated the cache')
// Republication without a content change is wasted I/O, not a correctness
// problem, so it is reported rather than failed. It is real, and it has a
// follow-up issue: a date-RANGED query (`status --format json`, the
// statusline/menubar fast path) republishes the month shards its range skipped,
// on every run, even when nothing changed — partially defeating #1007's
// "a warm launch rewrites only the month that changed". The identical-bodies
// check above is what proves the content survives it.
const afterMtimes = shardMtimes(upgradeCache)
const republished = Object.keys(afterMtimes).filter(n => n !== 'envelope.json' && beforeMtimes[n] !== afterMtimes[n])
const retired = Object.keys(beforeMtimes).filter(n => n !== 'envelope.json' && !(n in afterMtimes))
const churnNote = 'known defect, see #1032: a date-ranged run republishes the month shards it skipped'
if (retired.length) console.log(` note ${retired.length} shard(s) republished under a new name with identical content (${churnNote}): ${retired.join(', ')}`)
else if (republished.length) console.log(` note ${republished.length} shard(s) rewritten in place (${churnNote}): ${republished.join(', ')}`)
else ok('no shard republished on an unchanged corpus')
// ── 8. partial source aging (release blocker, track C) ───────────────────────
// Claude Code deletes its transcripts after ~30 days, so between one run and the
// next a day can go from fully sourced to PARTIALLY sourced. The daily cache's
// never-lose contract says a schema bump re-derives what it can and carries
// forward what it cannot — but on a partially-sourced day the re-derivation
// produces a smaller slice from the surviving files and that slice REPLACES the
// baseline one instead of being unioned with it, so the aged-out portion is lost.
// A day that aged out completely is carried forward correctly, which is what
// makes the partial case a hole rather than a missing feature.
//
// Runs last, and on its own cache dir, so mutating the corpus cannot disturb the
// parity comparison above.
step('never-lose across partial source aging')
const agingCache = join(CACHES, 'aging')
mkdirSync(agingCache, { recursive: true })
// A fresh 0.9.20 cache, taken while every transcript still exists.
capture(oldBin, agingCache, join(PAYLOADS, 'aging-baseline'))
const baseDaily = JSON.parse(readFileSync(join(agingCache, OLD_DAILY_CACHE), 'utf8'))
const sliceOf = (cache, date) => cache.days.find(d => d.date === date)?.providers?.claude
// Group transcripts by the day their turns land on. Sidechain files are left out:
// deleting a parent's subagent transcript entangles this with spawn-link
// carry-forward, which is a different contract.
const projectsDir = join(HOME, '.claude', 'projects')
const byDay = new Map()
for (const rel of readdirSync(projectsDir, { recursive: true })) {
const relPath = String(rel)
if (!relPath.endsWith('.jsonl') || relPath.includes('subagents')) continue
const full = join(projectsDir, relPath)
const first = readFileSync(full, 'utf8').split('\n', 1)[0]
const date = JSON.parse(first).timestamp?.slice(0, 10)
if (!date || !sliceOf(baseDaily, date)) continue
if (!byDay.has(date)) byDay.set(date, [])
byDay.get(date).push(full)
}
// Densest days first, oldest among equals — the ones a retention window reaches
// first, and the ones where losing the aged-out portion shows up largest.
const candidates = [...byDay.entries()].filter(([, files]) => files.length >= 2)
.sort((a, b) => (b[1].length - a[1].length) || a[0].localeCompare(b[0]))
let aged = []
if (candidates.length < 3) fail(`need 3 multi-transcript claude days to age out, found ${candidates.length}`)
else {
for (const [date, files] of candidates.slice(0, 2)) {
// Keep exactly one file, so the day is still sourced — just not fully.
for (const f of files.slice(1)) rmSync(f)
aged.push({ date, kind: 'partially sourceless', kept: 1, removed: files.length - 1 })
}
const [goneDate, goneFiles] = candidates[2]
for (const f of goneFiles) rmSync(f)
aged.push({ date: goneDate, kind: 'fully sourceless', kept: 0, removed: goneFiles.length })
for (const a of aged) ok(`${a.date}: ${a.kind} (removed ${a.removed} of ${a.removed + a.kept} transcripts)`)
capture(newBin, agingCache, join(PAYLOADS, 'aging-upgraded'))
const upDaily = JSON.parse(readFileSync(join(agingCache, NEW_DAILY_CACHE), 'utf8'))
const usd = n => `$${n.toFixed(6)}`
for (const a of aged) {
const b = sliceOf(baseDaily, a.date)
const u = sliceOf(upDaily, a.date)
if (!u) { fail(`${a.date} (${a.kind}): the claude slice is gone entirely; baseline had ${usd(b.cost)} over ${b.calls} calls`); continue }
// A fully sourceless day has nothing to re-derive, so it must come back
// EXACTLY. A partially sourceless one may legitimately grow (a re-parse
// under new accounting), but must never shrink.
const exact = a.kind === 'fully sourceless'
const costOk = exact ? Math.abs(u.cost - b.cost) < 1e-9 : u.cost >= b.cost - 1e-9
const callsOk = exact ? u.calls === b.calls : u.calls >= b.calls
const loss = costOk && callsOk ? '' :
` — LOST ${usd(b.cost - u.cost)} (${(100 * (b.cost - u.cost) / b.cost).toFixed(1)}%) and ${b.calls - u.calls} calls`
check(costOk && callsOk,
`${a.date} (${a.kind}): cost ${usd(b.cost)} -> ${usd(u.cost)}, calls ${b.calls} -> ${u.calls}${loss}`)
}
}
// ── done ─────────────────────────────────────────────────────────────────────
console.log(`\n${failures ? `FAILED: ${failures} check(s)` : 'PASSED'}${skipped ? ` (${skipped} skipped)` : ''}`)
if (failures && process.env['UPGRADE_PATH_KEEP'] !== '0') console.log(`payloads left in: ${PAYLOADS}`)
else if (process.env['UPGRADE_PATH_KEEP'] !== '1') { try { rmSync(WORK, { recursive: true, force: true }) } catch { /* windows file locks */ } }
process.exit(failures ? 1 : 0)

View file

@ -2,6 +2,7 @@ import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { aggregateModelStats, type ModelStats } from '../compare-stats.js'
import { withUserStartedSessions } from '../session-population.js'
import type { ProjectSummary } from '../types.js'
import { sha256File } from './backup.js'
import type { ActionPlan } from './types.js'
@ -73,15 +74,16 @@ function isDebuggingHeavy(project: ProjectSummary): boolean {
}
export function recommendModelDefault(project: ProjectSummary, opts: { now?: Date } = {}): ModelDefaultRecommendation | null {
const behavioralProject = withUserStartedSessions(project)
const now = opts.now ?? new Date()
const stats = aggregateModelStats([project])
const stats = aggregateModelStats([behavioralProject])
.filter(s => s.model !== '<synthetic>' && s.editTurns >= MIN_EDIT_TURNS)
.sort((a, b) => b.editTurns - a.editTurns || b.editCost - a.editCost)
const current = stats[0]
if (!current) return null
const providers = providerByModel(project)
const providers = providerByModel(behavioralProject)
const provider = providers.get(current.model)
if (!provider || !isRecent(current.lastSeen, now)) return null
@ -89,7 +91,7 @@ export function recommendModelDefault(project: ProjectSummary, opts: { now?: Dat
const currentCost = costPerEdit(current)
if (!Number.isFinite(currentCost) || currentCost <= 0) return null
const debuggingHeavy = isDebuggingHeavy(project)
const debuggingHeavy = isDebuggingHeavy(behavioralProject)
const tolerance = debuggingHeavy ? 0 : ONE_SHOT_TOLERANCE
const candidates = stats

View file

@ -7,12 +7,17 @@ import { formatCost } from '../currency.js'
import { formatTokens } from '../format.js'
import { runAction } from './apply.js'
import { shortId } from './journal.js'
import { REPORT_MIN_AGE_DAYS } from './types.js'
import { planFindings, type FindingPlan, type PlanContext } from './plans.js'
export type ApplyOptions = {
yes?: boolean
dryRun?: boolean
only?: string
// Mirrors `optimize --provider`. The scan below only reads Claude
// transcripts, and this path does not just report findings, it plans and
// applies them - a Codex-scoped run must never offer to edit ~/.claude.
provider?: string
actionsDir?: string
ctx?: PlanContext
// Test seams: crafted findings skip the session scan; streams default to
@ -37,16 +42,47 @@ function changeLines(fp: FindingPlan): string[] {
})
}
function planTokensSaved(fp: FindingPlan): number {
if (fp.plan?.mcpSavingsUncertain) return Number.NaN
const byServer = fp.finding.applyTokensSavedByServer
const affected = fp.plan?.affectedMcpServers
if (byServer && affected) return affected.reduce((sum, server) => sum + (byServer[server] ?? 0), 0)
return fp.finding.applyTokensSaved ?? fp.finding.tokensSaved
}
function manualActionLines(fp: FindingPlan): string[] {
if (fp.finding.manualFollowUp) {
return [fp.finding.manualFollowUp.label, fp.finding.manualFollowUp.text]
}
const action = fp.finding.fix
if (action.type === 'paste' && action.destination === 'manual') {
return [action.label, action.text]
}
return []
}
export function renderApplyList(appliable: FindingPlan[], manual: FindingPlan[], costRate: number): string {
const lines: string[] = ['']
lines.push(chalk.bold(' Appliable config-class fixes:'))
appliable.forEach((fp, i) => {
const f = fp.finding
const savings = `~${formatTokens(f.tokensSaved)} tokens${costRate > 0 ? `, ~${formatCost(f.tokensSaved * costRate)}` : ''}`
const actionTokensSaved = planTokensSaved(fp)
const savings = Number.isFinite(actionTokensSaved)
? `~${formatTokens(actionTokensSaved)} tokens${costRate > 0 ? `, ~${formatCost(actionTokensSaved * costRate)}` : ''}`
: 'Savings not estimated'
lines.push('')
lines.push(` ${i + 1}. ${f.title} ${chalk.hex('#FFD700')(`(${savings})`)}`)
if (fp.plan?.affectedMcpServers?.length) {
const servers = fp.plan.affectedMcpServers.join(', ')
lines.push(chalk.yellow(` Removes local MCP server${fp.plan.affectedMcpServers.length === 1 ? '' : 's'}: ${servers}`))
}
for (const line of changeLines(fp)) lines.push(chalk.dim(` ${line}`))
for (const note of fp.notes) lines.push(chalk.yellow(` ! ${note}`))
const manualLines = manualActionLines(fp)
if (manualLines.length > 0) {
lines.push(chalk.cyan(' Manual follow-up (not applied):'))
for (const line of manualLines) lines.push(chalk.cyan(` ${line}`))
}
})
if (manual.length > 0) {
lines.push('')
@ -54,6 +90,7 @@ export function renderApplyList(appliable: FindingPlan[], manual: FindingPlan[],
for (const fp of manual) {
lines.push(chalk.dim(` - ${fp.finding.title} [${fp.finding.id}] manual`))
for (const note of fp.notes) lines.push(chalk.yellow(` ! ${note}`))
for (const line of manualActionLines(fp)) lines.push(chalk.cyan(` ${line}`))
}
}
lines.push('')
@ -103,7 +140,7 @@ export async function runOptimizeApply(
let costRate = opts.costRate ?? 0
if (!findings) {
errout.write(chalk.dim(' Analyzing your sessions...\n'))
const scanned = await scanAndDetect(projects, dateRange)
const scanned = await scanAndDetect(projects, dateRange, opts.provider)
findings = scanned.findings
costRate = scanned.costRate
}
@ -129,6 +166,7 @@ export async function runOptimizeApply(
print(chalk.dim('\n No appliable config-class fixes for this period.'))
for (const fp of manual) {
for (const note of fp.notes) print(chalk.yellow(` ! ${fp.finding.id}: ${note}`))
for (const line of manualActionLines(fp)) print(chalk.cyan(` ${line}`))
}
print()
return
@ -172,15 +210,25 @@ export async function runOptimizeApply(
} catch { /* baseline is optional; apply proceeds without it */ }
print()
let applied = 0
for (const fp of selected) {
try {
const record = await runAction(fp.plan!, opts.actionsDir)
applied++
print(` Applied ${chalk.bold(shortId(record.id))} ${record.description}`)
print(chalk.dim(` Undo anytime: codeburn act undo ${shortId(record.id)}`))
const manualLines = manualActionLines(fp)
if (manualLines.length > 0) {
print(chalk.cyan(' Still requires manual action:'))
for (const line of manualLines) print(chalk.cyan(` ${line}`))
}
} catch (e) {
errout.write(chalk.red(` Failed to apply ${fp.finding.id}: ${e instanceof Error ? e.message : String(e)}`) + '\n')
process.exitCode = 1
}
}
if (applied > 0) {
print(chalk.dim(` CodeBurn will re-measure these on your next optimize run after ${REPORT_MIN_AGE_DAYS} days.`))
}
print()
}

View file

@ -9,6 +9,7 @@ import {
ALWAYSLOAD_STARTUP_CAP_SECONDS,
ENABLE_TOOL_SEARCH_VAR,
parseVersion,
SHELL_PROFILE_SCOPE,
versionPredates,
} from '../optimize.js'
import type { WasteFinding } from '../optimize.js'
@ -275,12 +276,15 @@ function pathNoteAdder(pathNotes: Record<string, string>): (path: string, note:
}
function buildMcpRemove(finding: WasteFinding, r: ResolvedPaths): BuiltPlan {
const servers = finding.apply?.kind === 'mcp-remove' ? finding.apply.servers : []
const servers = finding.apply?.kind === 'mcp-remove'
? [...new Set(finding.apply.servers)]
: []
const searchPaths = [r.projectMcpJson, r.projectSettings, r.projectSettingsLocal, r.userClaudeJson]
const docs = new ConfigDocs(r.homeDir)
const skips: string[] = []
const pathNotes: Record<string, string> = {}
const addPathNote = pathNoteAdder(pathNotes)
const affectedServers: string[] = []
for (const server of servers) {
let removed = false
@ -291,14 +295,24 @@ function buildMcpRemove(finding: WasteFinding, r: ResolvedPaths): BuiltPlan {
if (res.removed) removed = true
if (res.projectEntries.length > 0) addPathNote(path, projectRemovalNote(server, res.projectEntries, r.homeDir))
}
if (!removed) skips.push(`skipped ${server}: not found in editable config (plugin or managed config?)`)
if (removed) affectedServers.push(server)
else skips.push(`skipped ${server}: not found in editable config (plugin or managed config?)`)
}
const changes = docs.changes()
const notes = [...docs.errorNotes(), ...skips]
const attribution = finding.applyTokensSavedByServer
const partialWithoutAttribution = affectedServers.length < servers.length && !attribution
const affectedMissingAttribution = attribution !== undefined
&& affectedServers.some(server => !Object.hasOwn(attribution, server))
const savingsUncertain = docs.errorNotes().length > 0
|| partialWithoutAttribution
|| affectedMissingAttribution
if (changes.length === 0) return { plan: null, notes }
const plan = mcpPlan('mcp-remove', finding.id, `Remove ${affectedServers.length === 1 ? 'an MCP server' : 'MCP servers'} from config`, changes, affectedServers)
if (savingsUncertain) plan.mcpSavingsUncertain = true
return {
plan: mcpPlan('mcp-remove', finding.id, `Remove ${changes.length === 1 ? 'an MCP server' : 'MCP servers'} from config`, changes),
plan,
notes,
...(Object.keys(pathNotes).length > 0 ? { pathNotes } : {}),
}
@ -371,8 +385,8 @@ function buildMcpProjectScope(finding: WasteFinding, r: ResolvedPaths): BuiltPla
}
}
function mcpPlan(kind: ActionKind, findingId: string, description: string, changes: PlannedChange[]): ActionPlan {
return { kind, findingId, description, changes }
function mcpPlan(kind: ActionKind, findingId: string, description: string, changes: PlannedChange[], affectedMcpServers?: string[]): ActionPlan {
return { kind, findingId, description, changes, ...(affectedMcpServers ? { affectedMcpServers } : {}) }
}
// ---------------------------------------------------------------------------
@ -386,7 +400,6 @@ const NEXT_SESSION_NOTE = 'takes effect on the next session (this config is read
// findDeferralEnvSetting (src/optimize.ts) reports shell-profile hits with
// exactly this scope string; the plan layer keys its refusal on it.
const SHELL_PROFILE_SCOPE = 'shell profile'
const SHELL_TOOL_SEARCH_LINE = new RegExp(`^\\s*(?:export\\s+)?${ENABLE_TOOL_SEARCH_VAR}\\s*=.*$`, 'm')

View file

@ -1,7 +1,8 @@
import { existsSync } from 'fs'
import { dirname } from 'node:path'
import type { DateRange, ProjectSummary, SessionSummary } from '../types.js'
import type { ActionBaseline, ActionKind, ActionRecord } from './types.js'
import type { ActionBaseline, ActionKind, ActionRecord, AppliedFix, AppliedVerdict } from './types.js'
import { REPORT_MIN_AGE_DAYS, VERDICT_WORKED_RATIO } from './types.js'
import type { FindingPlan } from './plans.js'
import {
AVG_TOKENS_PER_READ,
@ -20,7 +21,8 @@ import {
} from '../optimize.js'
import { parseAllSessions } from '../parser.js'
import { computeYield, type YieldSummary } from '../yield.js'
import { defaultActionsDir, readRecords } from './journal.js'
import { defaultActionsDir, readRecords, shortId } from './journal.js'
import { undoAction } from './undo.js'
import { renderTable } from '../text-table.js'
import { formatTokens } from '../format.js'
import { formatCost } from '../currency.js'
@ -28,7 +30,6 @@ import { formatCost } from '../currency.js'
const DAY_MS = 24 * 60 * 60 * 1000
const WINDOW_CAP_DAYS = 30
const BASELINE_WINDOW_DAYS = 14
const REPORT_MIN_AGE_DAYS = 3
const MIN_POST_WINDOW_SESSIONS = 20
const VOLUME_SHIFT_FACTOR = 2
@ -59,6 +60,8 @@ const ARCHIVE_DEF_TOKENS: Partial<Record<ActionKind, number>> = {
// 'pending' means the applied change has not taken effect in any post-apply
// session yet (e.g. deferral before a client restart) - distinct from
// 'reverted', which asserts the user undid it.
export { REPORT_MIN_AGE_DAYS }
export type RealizedStatus = 'measured' | 'reverted' | 'not-measurable' | 'pending'
export type ActReportRow = {
@ -102,6 +105,8 @@ export type ActReport = {
// findingId -> earliest apply date of an active applied action; drives the
// optimize "(previously applied ..., re-flagged)" title suffix.
appliedByFinding: Record<string, string>
// One entry per active applied action, including ones too young to measure.
appliedFixes: AppliedFix[]
}
export type ActReportOptions = {
@ -470,6 +475,63 @@ function isSaneRecord(r: ActionRecord): boolean {
return typeof r.at === 'string' && typeof r.status === 'string' && !Number.isNaN(new Date(r.at).getTime())
}
// Turn the measured rows plus the still-young entries into one verdict per
// active applied action. No second reconciliation: everything measurable comes
// straight off the row `act report` already computed.
function buildAppliedFixes(active: ActionRecord[], rows: ActReportRow[], now: Date): AppliedFix[] {
const byId = new Map(rows.map(r => [r.id, r]))
return active.map(rec => {
const row = byId.get(rec.id)
const base = {
id: rec.id,
kind: rec.kind,
findingId: rec.findingId ?? null,
appliedAt: rec.at,
ageDays: ageDays(rec.at, now),
undoCommand: `codeburn act undo ${shortId(rec.id)}`,
}
// No row means too young to measure; a row that is not a measured token
// row (not-measurable, not yet in effect, reverted by the user, or a
// correlation-only kind) has no reduction to judge either.
if (!row) return { ...base, verdict: 'pending' as const, estimatedTokens: rec.baseline?.estimatedTokens ?? 0, realizedTokens: 0, note: '' }
if (row.status !== 'measured' || !isTokenKind(row.kind)) {
return { ...base, verdict: 'pending' as const, estimatedTokens: row.estimatedForWindow, realizedTokens: 0, note: row.note }
}
const estimatedTokens = row.estimatedForWindow
const realizedTokens = row.realizedTokens
const verdict: AppliedVerdict = realizedTokens <= 0
? 'no-effect'
: estimatedTokens <= 0 || realizedTokens >= estimatedTokens * VERDICT_WORKED_RATIO ? 'worked' : 'partial'
return { ...base, verdict, estimatedTokens, realizedTokens, note: row.note }
})
}
// --auto-revert: undo the fixes that measured no reduction at all. CLAUDE.md
// rules are never undone unattended, matching the --yes guardrail - the file
// belongs to whatever project the user happened to be in.
export async function autoRevertNoEffect(
fixes: AppliedFix[], opts: { actionsDir?: string } = {},
): Promise<{ lines: string[]; revertedIds: Set<string> }> {
const lines: string[] = []
const revertedIds = new Set<string>()
for (const fix of fixes) {
if (fix.verdict !== 'no-effect') continue
const label = fix.findingId ?? fix.kind
if (fix.kind === 'claude-md-rule') {
lines.push(`Not auto-reverted: ${label} edits a CLAUDE.md. Revert: ${fix.undoCommand}`)
continue
}
try {
const record = await undoAction({ id: fix.id }, { actionsDir: opts.actionsDir })
revertedIds.add(fix.id)
lines.push(`Reverted ${shortId(record.id)}: ${record.description}`)
} catch (err) {
lines.push(`Could not revert ${label}: ${err instanceof Error ? err.message : String(err)}`)
}
}
return { lines, revertedIds }
}
export async function computeActReport(opts: ActReportOptions = {}): Promise<ActReport> {
const now = opts.now ?? new Date()
const rawRecords = await readRecords(opts.actionsDir ?? defaultActionsDir())
@ -497,6 +559,7 @@ export async function computeActReport(opts: ActReportOptions = {}): Promise<Act
observedDays: 0,
malformedRecords,
appliedByFinding,
appliedFixes: buildAppliedFixes(active, [], now),
}
const eligible = active.filter(r => ageDays(r.at, now) > REPORT_MIN_AGE_DAYS)
@ -550,6 +613,7 @@ export async function computeActReport(opts: ActReportOptions = {}): Promise<Act
observedDays,
malformedRecords,
appliedByFinding,
appliedFixes: buildAppliedFixes(active, rows, now),
}
}
@ -651,6 +715,7 @@ export function buildActReportJson(report: ActReport): unknown {
activeActions: report.activeCount,
observedDays: report.observedDays,
},
appliedFixes: report.appliedFixes,
footer: HONEST_FOOTER,
}
}
@ -666,7 +731,8 @@ type CaptureCtx = {
now: Date
}
function mcpServersFromApply(finding: WasteFinding): string[] {
function mcpServersFromApply(finding: WasteFinding, affectedMcpServers?: string[]): string[] {
if (affectedMcpServers) return affectedMcpServers
if (finding.apply?.kind === 'mcp-remove') return finding.apply.servers
if (finding.apply?.kind === 'mcp-project-scope') return finding.apply.servers.map(s => s.server)
return []
@ -684,24 +750,37 @@ function deferServers(finding: WasteFinding, ctx: CaptureCtx): string[] {
return observedMcpServers(ctx.projects)
}
export function captureBaseline(finding: WasteFinding, kind: ActionKind, ctx: CaptureCtx): ActionBaseline | undefined {
export function captureBaseline(
finding: WasteFinding,
kind: ActionKind,
ctx: CaptureCtx,
affectedMcpServers?: string[],
): ActionBaseline | undefined {
const common = {
windowDays: ctx.windowDays,
capturedAt: ctx.now.toISOString(),
estimatedTokens: Math.max(0, Math.round(finding.tokensSaved)),
estimatedTokens: Math.max(0, Math.round(finding.applyTokensSaved ?? finding.tokensSaved)),
}
if (MCP_KINDS.has(kind)) {
const servers = mcpServersFromApply(finding)
const servers = mcpServersFromApply(finding, affectedMcpServers)
if (servers.length === 0) return undefined
const covByServer = new Map(ctx.coverage.map(c => [c.server, c]))
const metrics: Record<string, number> = {}
for (const server of servers) {
const cov = covByServer.get(server)
const tools = cov && cov.toolsAvailable > 0 ? cov.toolsAvailable : TOOLS_PER_MCP_SERVER
// Removal realizes only the unused schema that the low-coverage
// detector estimated. If coverage is unavailable, omit the numeric
// claim instead of inventing a five-tool baseline.
const tools = finding.id === 'mcp-low-coverage'
? cov?.unusedTools.length ?? 0
: cov && cov.toolsAvailable > 0 ? cov.toolsAvailable : TOOLS_PER_MCP_SERVER
metrics[server] = tools * TOKENS_PER_MCP_TOOL
}
return { ...common, sessions: countSessionsLoading(ctx.projects, servers), metrics }
const estimatedTokens = finding.applyTokensSavedByServer
? Math.round(servers.reduce((sum, server) => sum + (finding.applyTokensSavedByServer?.[server] ?? 0), 0))
: common.estimatedTokens
return { ...common, estimatedTokens, sessions: countSessionsLoading(ctx.projects, servers), metrics }
}
if (DEFER_KINDS.has(kind)) {
@ -750,7 +829,8 @@ export async function captureBaselinesForPlans(
const projects = await loadProjects({ start, end: now })
const ctx: CaptureCtx = { projects, coverage: aggregateMcpCoverage(projects), windowDays: BASELINE_WINDOW_DAYS, now }
for (const fp of applicable) {
const baseline = captureBaseline(fp.finding, fp.plan!.kind, ctx)
if (fp.plan!.mcpSavingsUncertain) continue
const baseline = captureBaseline(fp.finding, fp.plan!.kind, ctx, fp.plan!.affectedMcpServers)
if (baseline) fp.plan!.baseline = baseline
}
}

View file

@ -1,3 +1,5 @@
import { formatTokens } from '../format.js'
export type ActionKind =
| 'mcp-remove' | 'mcp-project-scope'
| 'defer-enable' | 'defer-alwaysload' | 'defer-threshold'
@ -66,4 +68,68 @@ export type ActionPlan = {
findingId?: string | null
changes: PlannedChange[]
baseline?: ActionBaseline
// MCP plans only: exact server identities the generated file mutations own.
// Preview and baseline capture must not claim skipped/managed targets.
affectedMcpServers?: string[]
// Relevant config scopes could not all be read, so removal may proceed
// with warnings but savings/baseline claims must be suppressed.
mcpSavingsUncertain?: boolean
}
// Applied actions are re-measured on every `codeburn optimize` run: only fixes
// at least this old have a post-apply window to measure against.
export const REPORT_MIN_AGE_DAYS = 3
// A fix counts as having worked once it realizes this share of its
// window-scaled estimate; anything above zero but below it is partial.
export const VERDICT_WORKED_RATIO = 0.7
// Per-applied-entry judgement shown by `codeburn optimize` after an --apply.
// Computed in act/report.ts from the same rows `act report` prints - there is
// one reconciliation, not two. Lives here so the optimize renderer can format
// it without importing report.ts back into optimize.ts.
export type AppliedVerdict = 'worked' | 'partial' | 'no-effect' | 'pending'
export type AppliedFix = {
id: string
kind: ActionKind
findingId: string | null
appliedAt: string
ageDays: number
verdict: AppliedVerdict
// Window-scaled estimate, the same column `act report` compares against.
estimatedTokens: number
realizedTokens: number
note: string
undoCommand: string
}
const VERDICT_GLYPH: Record<AppliedVerdict, string> = {
worked: '\u2713',
partial: '~',
'no-effect': '\u2717',
pending: '\u2026',
}
export function appliedFixGlyph(fix: AppliedFix): string {
return VERDICT_GLYPH[fix.verdict]
}
// One plain line per applied fix: what it estimated, what it measured, and for
// a fix that did nothing, how to put it back.
export function formatAppliedFix(fix: AppliedFix): string {
const age = Math.max(0, Math.floor(fix.ageDays))
const head = `${fix.findingId ?? fix.kind} (${age}d ago)`
if (fix.verdict === 'pending') {
const why = fix.note || (age <= REPORT_MIN_AGE_DAYS
? `measuring, check back after ${REPORT_MIN_AGE_DAYS} days`
: 'measuring')
return `${head}: ${why}`
}
const pair = `est. ${formatTokens(fix.estimatedTokens)} -> measured ${formatTokens(fix.realizedTokens)}`
if (fix.verdict === 'worked') return `${head}: ${pair}`
if (fix.verdict === 'partial') {
const under = Math.round((1 - fix.realizedTokens / fix.estimatedTokens) * 100)
return `${head}: ${pair} (-${under}% vs estimate)`
}
return `${head}: ${pair} - did not help. Revert: ${fix.undoCommand}`
}

View file

@ -1,5 +1,5 @@
import { isBehavioralCall } from './behavioral-weight.js'
import { getModelCosts, type ModelCosts } from './models.js'
import { getModelCosts, sanitizeModelForDisplay, type ModelCosts } from './models.js'
import { getProvider } from './providers/index.js'
import { formatCost, formatTokens } from './format.js'
import { renderTable, type TableColumn } from './text-table.js'
@ -114,7 +114,9 @@ export async function aggregateAudit(projects: ProjectSummary[]): Promise<AuditR
const p = await getProvider(name)
const entry = {
displayName: p?.displayName ?? name,
formatModel: p ? (m: string) => p.modelDisplayName(m) : (m: string) => m,
formatModel: p
? (m: string) => sanitizeModelForDisplay(p.modelDisplayName(m))
: sanitizeModelForDisplay,
}
providerCache.set(name, entry)
return entry

View file

@ -6,24 +6,41 @@ import { join } from 'path'
import { getCodeburnCacheDir } from './cache-dir.js'
import type { DateRange, ProjectSummary } from './types.js'
// Bumped to 19, deliberately skipping 18: an earlier head of this same change
// (#946) was pushed publicly claiming v18 under DIFFERENT accounting — no
// supplementary-call weighting, whole-session rollup suppression — and
// adoptOlderDailyCaches/isMigratableCache would take those days forward as
// finalized without re-deriving them. A distinct version is the only thing
// that stops one version number meaning two accountings.
// Bumped to 21: copilot input/cache tokens for sessions covered by the CLI's
// session-store.db move from one shutdown-rollup lump (stamped at session end)
// to per-request DB rows with real timestamps, supplementary accounting calls
// (rollups, residuals, paired rows) stop counting as api/model calls, and
// reasoning tokens leave the copilot cost recompute (they are inside the
// output the per-turn calls already bill). Per-day attribution, call counts and
// costs all move, so days finalized under an earlier version would disagree
// with the live parse.
//
// v18 (never released): copilot input/cache tokens for sessions covered by the CLI's
// session-store.db moved from one shutdown-rollup lump (stamped at session
// end) to per-request DB rows with real timestamps, supplementary accounting
// calls (rollups, covered rows) stopped counting as api/model calls, and
// reasoning tokens left the copilot cost recompute (they are inside the
// output the per-turn calls already bill). Per-day attribution, call counts
// and costs all move, so days finalized at v17 would disagree with the live
// parse. Re-derivation rides the v14 carry-forward semantics; sourceless
// days carry forward as-is.
// 20 is NOT free: an unmerged head of #1040 (codex model attribution) claims
// it, and 18 was burned by an earlier public head of THIS change under
// different accounting (whole-session rollup suppression, no supplementary
// weight). isMigratableCache/adoptOlderDailyCaches carry a same-or-newer
// version forward as FINALIZED without re-deriving it, so a number can never
// mean two accountings. 21 is the first number no other head claims.
//
// v17: copilot CLI sessions were misclassified as VS Code transcripts
// Bumped to 19: Grok authoritative usage now keeps one session-level rollup
// from top-level totals, clamps reasoning to reported output, and labels mixed
// authoritative/heuristic coverage. Every day finalized under the previous
// accounting carries the old Grok totals, and the daily cache has no
// per-provider invalidation, so raising MIN_SUPPORTED_VERSION is the only
// lever: it forces a one-time re-derivation of ALL days, for every provider,
// not just Grok. That pass reads the warm session cache (CACHE_VERSION is
// unchanged and only PROVIDER_PARSE_VERSIONS.grok moved), so it costs seconds
// rather than a full re-parse, and adoptOlderDailyCaches keeps the superseded
// file as the baseline for days no source can still re-derive. Days whose
// sources only PARTLY survive are held by the partial-survival guard in
// mergeDayEntries, which is what makes a global re-derive safe to force: on a
// real 108-day cache the 17 -> 19 pass moves Grok cost and tokens and nothing
// else - the day-by-day call counts come back identical.
//
// The shipped predecessor is v17; v18 was an unreleased draft of this change
// and only exists in pre-release checkouts. 19 clears both.
//
// Bumped to 17: copilot CLI sessions were misclassified as VS Code transcripts
// (#944), so days finalized at v16 or earlier carry output-only copilot costs —
// the session.shutdown rollup's input/cache tokens were dropped. Raising
// MIN_SUPPORTED_VERSION forces the one-time re-derivation under the
@ -91,8 +108,8 @@ import type { DateRange, ProjectSummary } from './types.js'
// that older binaries skipped. v8 added local-model savings to the daily
// rollup; the `savingsConfigHash` field is invalidated separately when the
// user changes their `localModelSavings` mapping.
export const DAILY_CACHE_VERSION = 19
const MIN_SUPPORTED_VERSION = 19
export const DAILY_CACHE_VERSION = 21
const MIN_SUPPORTED_VERSION = 21
// Version-suffixed so different binaries each own a distinct file and never
// clobber an incompatible schema. Bumping the version mints a fresh filename;
// adoptOlderDailyCaches then unions days out of every previous file (including
@ -859,6 +876,52 @@ function hasPositiveDayContent(day: DailyEntry): boolean {
return false
}
/// PARTIAL SURVIVAL (the v14 never-lose contract, extended past all-or-nothing).
/// v14 protected a (date, provider) slice only when the fresh derivation found
/// NOTHING there. But transcripts age out per FILE, not per day: Claude Code
/// deletes them after ~30 days, and turn-anchored bucketing means a handful of
/// turns from surviving later files still land on a mostly-aged-out day. The
/// fresh slice is then non-empty but truncated, and replacing the baseline with
/// it silently deletes the rest (measured on a real cache upgrading 17 -> 19:
/// 2026-07-16 fell from $1,685.17 / 12,530 calls to $385.44 / 560 calls).
///
/// So a fresh slice replaces a settled baseline slice only when it carries at
/// least as many CALLS — the same or more evidence. Fewer calls means the
/// source set demonstrably lost data, and the baseline is kept whole.
///
/// Why calls and not sessions: session counts shrink routinely on days whose
/// sources are entirely intact (a session's turns re-attribute to a neighbouring
/// day), measured at 1-5 sessions on recent days whose call counts were
/// identical across the re-derivation. A sessions test would freeze stale slices
/// on healthy days. Why not cost/tokens: those are re-priced accounting on the
/// same evidence — exactly what a legitimate re-derivation changes (#1015 Grok
/// keeps its per-day calls and raises cost, and is unaffected by this guard).
///
/// Why no source-set test instead: a day entry records no source files, counts
/// or fingerprints, and the session cache is keyed by file rather than by day,
/// so "were this day's sources all present?" cannot be answered from the cache.
/// The calls comparison is the available proxy.
///
/// TRADE-OFF: a future fix that legitimately REDUCES calls on a settled day
/// (deduplication) is blocked, and that day keeps the older, higher value until
/// its slice is re-derived under an equal-or-greater call count. That is the
/// "estimate high, never lose" direction v14 already chose over silent loss.
///
/// Recent days stay authoritative: within the settle window their session files
/// are still on disk, so a shrink there is a real change (the user deleted a
/// transcript), not aged-out sources. Seven days is far inside the ~30-day
/// retention floor of the shortest-lived source we know of, so any shrink older
/// than that is source loss with overwhelming likelihood.
const SETTLE_DAYS = 7
function settleCutoffDate(now: Date): string {
return toDateString(new Date(now.getFullYear(), now.getMonth(), now.getDate() - SETTLE_DAYS))
}
function isPartialSurvival(date: string, baseline: ProviderDaySlice, fresh: ProviderDaySlice, settleCutoff: string): boolean {
return date < settleCutoff && fresh.calls < baseline.calls
}
/// Index `freshUnderOldTz` (the same parse re-aggregated under the cache's OLD
/// tzKey) by date then provider, so the merge can subtract exactly what the
/// fresh parse still explains under the old bucketing.
@ -889,21 +952,34 @@ function buildTzSubtraction(days: DailyEntry[]): ReadonlyMap<string, ReadonlyMap
/// nothing — its day-level totals cannot be attributed without slices.
/// A primary slice blocks a secondary one only when it carries DATA; a
/// zero-data placeholder (sessions only) is merged into, not treated as a
/// re-derivation of the provider's day.
/// re-derivation of the provider's day — UNLESS that data is a strict shrink of
/// the baseline on a settled day, see `isPartialSurvival`.
/// `subtract`, present ONLY on the tz-change re-derive, maps (date, provider)
/// to the content the fresh parse still attributes there under the OLD
/// bucketing. Every baseline slice the merge would otherwise carry has that
/// content subtracted first (clamped at 0, dropped when nothing positive
/// remains), so turns that re-bucketed across local midnight are not counted on
/// both their old and new days. Absent (undefined) on every other path, which
/// keeps those merges byte-identical to the pre-fix behavior.
/// both their old and new days. What remains is by construction content NO
/// surviving source explains, so it is added even when the fresh slice for that
/// (date, provider) already carries data — the tz path's form of the
/// partial-survival rule, exact instead of heuristic. Absent (undefined) on
/// every other path, which keeps those merges byte-identical to the pre-fix
/// behavior apart from that rule.
export function mergeDayEntries(
primary: DailyEntry[],
secondary: DailyEntry[],
markSecondaryCarried: boolean,
subtract?: ReadonlyMap<string, ReadonlyMap<string, ProviderDaySlice>>,
/// Set ONLY by the complete-parse re-derive, where `primary` is a fresh
/// derivation from live sources and `secondary` is the cache baseline: a
/// primary slice with fewer calls there means sources aged out, so the
/// baseline wins on settled days (`isPartialSurvival`). The adoption union
/// leaves it off - both sides are cache generations there and the newer
/// schema deliberately wins per (date, provider).
guardPartialSurvival = false,
): DailyEntry[] {
const byDate = new Map<string, DailyEntry>()
const settleCutoff = settleCutoffDate(new Date())
for (const day of primary) byDate.set(day.date, structuredClone(day))
for (const day of secondary) {
const existing = byDate.get(day.date)
@ -930,7 +1006,6 @@ export function mergeDayEntries(
// day) still carry a real session count — worth preserving.
if (!hasSliceData(slice) && !(slice.sessions ?? 0)) continue
const existingSlice = Object.hasOwn(existing.providers, provider) ? existing.providers[provider] : undefined
if (existingSlice && hasSliceData(existingSlice)) continue
let toAdd = slice
let residual = false
if (subtract) {
@ -946,6 +1021,13 @@ export function mergeDayEntries(
residual = true
}
}
if (existingSlice && hasSliceData(existingSlice) && !residual) {
if (!guardPartialSurvival || !isPartialSurvival(day.date, slice, existingSlice, settleCutoff)) continue
// The baseline holds more evidence than the sources can still produce:
// swap the fresh slice back out for it (inverse of addSliceIntoDay, so
// the day's totals and nested maps stay reconciled with its slices).
subtractSliceFromDay(existing, provider, existingSlice)
}
addSliceIntoDay(existing, provider, toAdd, residual)
if (markSecondaryCarried) existing.carried = true
}
@ -1108,7 +1190,7 @@ export async function ensureCacheHydrated(
tzSubtraction = buildTzSubtraction(aggregateDaysInTz(wideProjects, c.tzKey))
}
const merged = parseWasComplete
? mergeDayEntries(freshDays, baseline, true, tzSubtraction)
? mergeDayEntries(freshDays, baseline, true, tzSubtraction, true)
: mergeDayEntries(baseline, freshDays, false)
c = {
version: DAILY_CACHE_VERSION,

View file

@ -1,7 +1,8 @@
import { homedir } from 'os'
import { EventEmitter } from 'node:events'
import React, { useState, useCallback, useEffect, useLayoutEffect, useMemo, useRef } from 'react'
import { render, Box, Text, measureElement, useInput, useApp, useWindowSize, type DOMElement } from 'ink'
import React, { Fragment, useState, useCallback, useEffect, useLayoutEffect, useMemo, useRef } from 'react'
import { render, Box, Text, measureElement, useInput, useApp, useWindowSize, type DOMElement, type Instance, type RenderOptions } from 'ink'
import { CATEGORY_LABELS, type DateRange, type ProjectSummary, type TaskCategory } from './types.js'
import { formatCost, formatTokens, markEstimated, carriedCostNote } from './format.js'
import { aggregateModelEfficiency } from './model-efficiency.js'
@ -10,7 +11,8 @@ import { findUnpricedModels, isExpectedFreeModel, loadPricing } from './models.j
import { aggregateModelTotals } from './model-breakdown.js'
import { buildDurablePeriod } from './usage-aggregator.js'
import { getAllProviders } from './providers/index.js'
import { scanAndDetect, type WasteFinding, type WasteAction, type OptimizeResult } from './optimize.js'
import { classHeaderLine, classTotals, findingBasis, findingClass, scanAndDetect, type FindingClass, type WasteFinding, type WasteAction, type OptimizeResult } from './optimize.js'
import { appliedFixGlyph, formatAppliedFix, type AppliedFix } from './act/types.js'
import { aggregateFileChurn, buildCoachingNotes, computePricingCoverage, medianTimeToFirstEditMs, scanUserCorrections, worstOneShotCategory, type ReworkedFile } from './workflow-insights.js'
import { estimateContextBudget, type ContextBudget } from './context-budget.js'
import { dateKey } from './day-aggregator.js'
@ -31,6 +33,138 @@ export type DailyActivityRow = {
export const DAILY_ACTIVITY_PAGE_SIZE = 10
export const INTERACTIVE_RENDER_OPTIONS = { alternateScreen: true } as const
export const RESIZE_DEBOUNCE_MS = 150
export type TerminalSize = { columns: number; rows: number }
export type DebouncedResizeStream = NodeJS.WriteStream & {
dispose(): void
onSettledResize(listener: (size: TerminalSize) => void): () => void
}
function normalizeTerminalDimension(value: number | undefined, fallback: number): number {
return Number.isFinite(value) && value! > 0 ? Math.floor(value!) : fallback
}
function terminalSizeOf(source: NodeJS.WriteStream): TerminalSize {
return {
columns: normalizeTerminalDimension(source.columns, 80),
rows: normalizeTerminalDimension(source.rows, 24),
}
}
const RESIZE_LISTENER_METHODS = new Set<PropertyKey>([
'addListener', 'on', 'once', 'prependListener', 'prependOnceListener', 'off', 'removeListener',
])
export function createDebouncedResizeStream(source: NodeJS.WriteStream, delayMs: number): DebouncedResizeStream {
let resizeTimer: ReturnType<typeof setTimeout> | undefined
let disposed = false
let { columns, rows } = terminalSizeOf(source)
const resizeEvents = new EventEmitter()
const settledResizeListeners = new Set<(size: TerminalSize) => void>()
const resize = () => {
if (disposed) return
if (resizeTimer) clearTimeout(resizeTimer)
resizeTimer = setTimeout(() => {
resizeTimer = undefined
if (disposed) return
const next = terminalSizeOf(source)
const changed = next.columns !== columns || next.rows !== rows
columns = next.columns
rows = next.rows
if (!changed) return
// Rerender first so the settled view owns the first paint at the new
// size; then notify Ink/useWindowSize. Writes are never intercepted, so
// a mid-burst state update still reaches the terminal even when net
// size is unchanged.
for (const listener of [...settledResizeListeners]) listener(next)
resizeEvents.emit('resize')
}, delayMs)
}
source.on('resize', resize)
const dispose = () => {
if (disposed) return
disposed = true
source.off('resize', resize)
if (resizeTimer) clearTimeout(resizeTimer)
resizeTimer = undefined
resizeEvents.removeAllListeners()
settledResizeListeners.clear()
}
const stream = new Proxy(source as DebouncedResizeStream, {
get(target, property) {
if (property === 'dispose') return dispose
if (property === 'onSettledResize') {
return (listener: (size: TerminalSize) => void) => {
if (disposed) return () => {}
settledResizeListeners.add(listener)
return () => settledResizeListeners.delete(listener)
}
}
if (property === 'columns') return columns
if (property === 'rows') return rows
if (RESIZE_LISTENER_METHODS.has(property)) {
return (event: string | symbol, ...args: unknown[]) => {
if (event === 'resize') {
if (!disposed) {
Reflect.apply(Reflect.get(resizeEvents, property) as (...args: unknown[]) => unknown, resizeEvents, [event, ...args])
}
return stream
}
Reflect.apply(Reflect.get(target, property, target) as (...args: unknown[]) => unknown, target, [event, ...args])
return stream
}
}
const value = Reflect.get(target, property, target)
return typeof value === 'function' ? value.bind(target) : value
},
})
return stream
}
function DisposeOnUnmount({ dispose, children }: { dispose: () => void; children: React.ReactNode }) {
useLayoutEffect(() => dispose, [dispose])
return children
}
export type DebouncedInteractiveInstance = Instance & {
dispose(): void
stdout: DebouncedResizeStream
}
export function renderDebouncedInteractive(
source: NodeJS.WriteStream,
view: (size: TerminalSize) => React.ReactElement,
options: Omit<RenderOptions, 'stdout'> = INTERACTIVE_RENDER_OPTIONS,
): DebouncedInteractiveInstance {
const stdout = createDebouncedResizeStream(source, RESIZE_DEBOUNCE_MS)
let size = { columns: stdout.columns, rows: stdout.rows }
let unsubscribe = () => {}
let disposed = false
const dispose = () => {
if (disposed) return
disposed = true
unsubscribe()
stdout.dispose()
}
const dashboard = () => <DisposeOnUnmount dispose={dispose}>{view(size)}</DisposeOnUnmount>
let app: Instance
try {
app = render(dashboard(), { ...options, stdout })
} catch (error) {
dispose()
throw error
}
unsubscribe = stdout.onSettledResize(nextSize => {
if (disposed) return
size = nextSize
app.rerender(dashboard())
})
return Object.assign(app, { dispose, stdout })
}
export function getDailyActivityPageSize(columnCount: 1 | 2 | 3, projectRows: number, activityRows: number, dayMode = false): number {
if (dayMode) return 1
@ -653,8 +787,10 @@ function ModelBreakdown({ projects, pw, bw }: { projects: ProjectSummary[]; pw:
)
})}
{unpriced.length > 0 && (
<Text color="yellow" wrap="truncate-end">
{`! ${unpriced.length} model${unpriced.length === 1 ? '' : 's'} unpriced at $0, fix: codeburn model-alias (${unpriced.slice(0, 2).map(u => u.model).join(', ')}${unpriced.length > 2 ? ', ...' : ''})`}
<Text color="yellow" wrap={pw <= 44 ? 'wrap' : 'truncate-end'}>
{pw <= 44
? `! ${unpriced.length}: codeburn models --unpriced`
: `! ${unpriced.length} unpriced: codeburn models --unpriced`}
</Text>
)}
{anyEstimated && (
@ -1047,6 +1183,8 @@ function actionDestinationHeader(action: WasteAction): string {
return '── Ask Claude in the current session '.padEnd(64, '─')
case 'shell-config':
return '── Add to your shell config '.padEnd(64, '─')
case 'manual':
return '── Manual action '.padEnd(64, '─')
default:
return '── Suggested action '.padEnd(64, '─')
}
@ -1080,7 +1218,7 @@ function FindingPanel({ index, finding, costRate, width }: { index: number; find
{trendBadge && <Text color="#5BF5A0">{trendBadge}</Text>}
</Text>
<Text dimColor wrap="wrap">{finding.explanation}</Text>
<Text color={GOLD}>Savings: ~{formatTokens(finding.tokensSaved)} tokens (~{formatCost(costSaved)})</Text>
<Text color={GOLD}>Savings: ~{formatTokens(finding.tokensSaved)} tokens (~{formatCost(costSaved)})<Text dimColor> {findingBasis(finding)}</Text></Text>
<Text> </Text>
<FindingAction action={finding.fix} />
</Box>
@ -1095,7 +1233,14 @@ const GRADE_COLORS: Record<string, string> = { A: '#5BF5A0', B: '#5BF5A0', C: GO
// off the alt-buffer top and the user couldn't see the StatusBar at all.
const FINDINGS_WINDOW_SIZE = 3
function OptimizeView({ findings, costRate, projects, label, width, healthScore, healthGrade, cursor }: { findings: WasteFinding[]; costRate: number; projects: ProjectSummary[]; label: string; width: number; healthScore: number; healthGrade: string; cursor: number }) {
const APPLIED_FIX_COLORS: Record<AppliedFix['verdict'], string> = {
worked: '#5BF5A0',
partial: GOLD,
'no-effect': '#F55B5B',
pending: DIM,
}
function OptimizeView({ findings, costRate, projects, label, width, healthScore, healthGrade, cursor, appliedFixes = [] }: { findings: WasteFinding[]; costRate: number; projects: ProjectSummary[]; label: string; width: number; healthScore: number; healthGrade: string; cursor: number; appliedFixes?: AppliedFix[] }) {
const periodCost = projects.reduce((s, p) => s + p.totalCostUSD, 0)
const totalTokens = findings.reduce((s, f) => s + f.tokensSaved, 0)
const totalCost = totalTokens * costRate
@ -1106,6 +1251,7 @@ function OptimizeView({ findings, costRate, projects, label, width, healthScore,
const start = total === 0 ? 0 : Math.min(cursor, Math.max(0, total - FINDINGS_WINDOW_SIZE))
const end = Math.min(start + FINDINGS_WINDOW_SIZE, total)
const visible = findings.slice(start, end)
const totals = classTotals(findings, costRate)
return (
<Box flexDirection="column" width={width}>
<Box flexDirection="column" borderStyle="round" borderColor={ORANGE} paddingX={1} width={width}>
@ -1120,8 +1266,28 @@ function OptimizeView({ findings, costRate, projects, label, width, healthScore,
<Text dimColor>Showing {start + 1}{end} of {total} · j/k to scroll</Text>
)}
</Box>
{visible.map((f, i) => <FindingPanel key={start + i} index={start + i + 1} finding={f} costRate={costRate} width={width} />)}
<Box paddingX={1} width={width}><Text dimColor>Token estimates are approximate.</Text></Box>
{visible.map((f, i) => {
// Findings arrive class-sorted, so a header goes in wherever the class
// changes (including the top of the window after paging).
const cls = findingClass(f)
const previous: FindingClass | null = i > 0 ? findingClass(visible[i - 1]!) : null
return (
<Fragment key={start + i}>
{cls !== previous && <Box paddingX={1} width={width}><Text bold color={ORANGE} wrap="truncate-end">{classHeaderLine(cls, totals[cls], costRate)}</Text></Box>}
<FindingPanel index={start + i + 1} finding={f} costRate={costRate} width={width} />
</Fragment>
)
})}
{appliedFixes.length > 0 && (
<Box flexDirection="column" paddingX={1} width={width}>
<Text bold color={ORANGE} wrap="truncate-end">Applied fixes</Text>
{appliedFixes.map(fix => (
<Text key={fix.id} color={APPLIED_FIX_COLORS[fix.verdict]} wrap="truncate-end">
{appliedFixGlyph(fix)} {formatAppliedFix(fix)}
</Text>
))}
</Box>
)}
</Box>
)
}
@ -1303,6 +1469,7 @@ export function InteractiveDashboard({ initialProjects, initialDailyHistoryProje
const [detectedProviders, setDetectedProviders] = useState<string[]>([])
const [view, setView] = useState<View>('dashboard')
const [optimizeResult, setOptimizeResult] = useState<OptimizeResult | null>(null)
const [appliedFixes, setAppliedFixes] = useState<AppliedFix[]>([])
const [optimizeLoading, setOptimizeLoading] = useState(false)
const [projectBudgets, setProjectBudgets] = useState<Map<string, ContextBudget>>(new Map())
const [planUsages, setPlanUsages] = useState<PlanUsage[]>(initialPlanUsages ?? [])
@ -1461,14 +1628,20 @@ export function InteractiveDashboard({ initialProjects, initialDailyHistoryProje
const generation = reloadGenerationRef.current
setOptimizeLoading(true)
try {
const result = await scanAndDetect(projects, currentRange())
const result = await scanAndDetect(projects, currentRange(), activeProvider)
if (reloadGenerationRef.current === generation) setOptimizeResult(result)
// Best effort: a bad journal never keeps the findings off screen.
try {
const { computeActReport } = await import('./act/report.js')
const applied = await computeActReport()
if (reloadGenerationRef.current === generation) setAppliedFixes(applied.appliedFixes)
} catch { /* the applied section is optional */ }
} catch (error) {
console.error(error)
} finally {
if (reloadGenerationRef.current === generation) setOptimizeLoading(false)
}
}, [optimizeAvailable, projects, currentRange, optimizeLoading, optimizeResult])
}, [optimizeAvailable, projects, currentRange, optimizeLoading, optimizeResult, activeProvider])
useEffect(() => {
const refreshIntervalMs = getRefreshIntervalMs(refreshSeconds ?? 0)
@ -1627,7 +1800,7 @@ export function InteractiveDashboard({ initialProjects, initialDailyHistoryProje
{view === 'compare'
? <CompareView projects={projects} onBack={() => setView('dashboard')} />
: view === 'optimize' && optimizeResult
? <OptimizeView findings={optimizeResult.findings} costRate={optimizeResult.costRate} projects={projects} label={headerLabel} width={dashWidth} healthScore={optimizeResult.healthScore} healthGrade={optimizeResult.healthGrade} cursor={findingsCursor} />
? <OptimizeView findings={optimizeResult.findings} costRate={optimizeResult.costRate} projects={projects} label={headerLabel} width={dashWidth} healthScore={optimizeResult.healthScore} healthGrade={optimizeResult.healthGrade} cursor={findingsCursor} appliedFixes={appliedFixes} />
: <DashboardContent projects={projects} period={period} columns={columns} maxContentWidth={maxContentWidth} activeProvider={activeProvider} budgets={projectBudgets} planUsages={planUsages} label={headerLabel} dayMode={isDayMode} dailyHistoryProjects={dailyHistoryProjects} dailyHistoryPageSize={dailyHistoryPageSize} scrollableDailyHistory={scrollableDailyHistory} dailyHistoryCursor={Math.min(dailyHistoryCursor, dailyHistoryMaxCursor)} durable={durable} />}
{coachingNote && (
<Box width={dashWidth} paddingX={1}>
@ -1699,23 +1872,13 @@ export async function renderDashboard(period: Period = 'week', provider: string
const label = initialDay ? formatDayRangeLabel(initialDay) : customRangeLabel
patchStdoutForWindows()
if (isTTY) {
let windowColumns = process.stdout.columns
const dashboard = () => (
<InteractiveDashboard initialProjects={filteredProjects} initialDailyHistoryProjects={scrollableDailyHistory ? scannedProjects : undefined} initialPeriod={period} initialProvider={provider} initialPlanUsages={planUsages} initialDurable={initialDurable} refreshSeconds={refreshSeconds} projectFilter={projectFilter} excludeFilter={excludeFilter} customRange={customRange} customRangeLabel={customRangeLabel} initialDay={initialDay} windowColumns={windowColumns} />
)
const app = render(
dashboard(),
INTERACTIVE_RENDER_OPTIONS,
)
const resize = () => {
windowColumns = process.stdout.columns
app.rerender(dashboard())
}
process.stdout.prependListener('resize', resize)
const app = renderDebouncedInteractive(process.stdout, ({ columns }) => (
<InteractiveDashboard initialProjects={filteredProjects} initialDailyHistoryProjects={scrollableDailyHistory ? scannedProjects : undefined} initialPeriod={period} initialProvider={provider} initialPlanUsages={planUsages} initialDurable={initialDurable} refreshSeconds={refreshSeconds} projectFilter={projectFilter} excludeFilter={excludeFilter} customRange={customRange} customRangeLabel={customRangeLabel} initialDay={initialDay} windowColumns={columns} />
))
try {
await app.waitUntilExit()
} finally {
process.stdout.off('resize', resize)
app.dispose()
}
} else {
const { unmount } = render(<StaticDashboard projects={filteredProjects} period={period} activeProvider={provider} planUsages={planUsages} label={label} dayMode={initialDay != null} durable={initialDurable} />, { patchConsole: false })

View file

@ -1,3 +1,5 @@
import stripAnsi from 'strip-ansi'
import type { DateRange, ProjectSummary } from './types.js'
const FIFTEEN_MINUTES = 15
@ -5,6 +7,10 @@ const ONE_HOUR = 60
const ONE_DAY = 24 * 60
const MINUTE_MS = 60 * 1000
const MAX_SERIES_PER_METRIC = 6
// Keep metadata bounded for the legend and tooltip: 80 characters preserves a
// useful title without letting the parser's 200-char transcript cap dominate
// either UI surface.
const MAX_SESSION_TITLE_LENGTH = 80
export type GranularSeries = {
id: string
@ -41,6 +47,25 @@ type RawBucket = {
sessions: Map<string, Totals>
}
type SessionTitleCandidate = {
title: string
lastTimestamp: string
}
type SessionLabelInfo = {
provider: string
projectPath: string
projectNames: Set<string>
sessionId: string
titleCandidates: Map<string, SessionTitleCandidate>
}
type SessionLabelEntry = {
key: string
info: SessionLabelInfo
baseLabel: string
}
function nonNegative(value: number): number {
return Number.isFinite(value) && value > 0 ? value : 0
}
@ -99,6 +124,114 @@ function shortSessionId(sessionId: string): string {
return trimmed.length > 12 ? `${trimmed.slice(0, 6)}${trimmed.slice(-4)}` : trimmed || 'unknown'
}
function cleanSessionTitle(title: string | undefined): string | undefined {
if (title === undefined) return undefined
// Match the control-character range used by the model-name sanitizer. ANSI
// sequences are removed first; remaining controls become spaces so transcript
// line breaks cannot join words before internal whitespace is collapsed.
const cleaned = stripAnsi(title)
.replace(/[\x00-\x1F\x7F-\x9F]/g, ' ')
.replace(/\s+/g, ' ')
.trim()
if (!cleaned) return undefined
return Array.from(cleaned).slice(0, MAX_SESSION_TITLE_LENGTH).join('').trimEnd() || undefined
}
// SessionSummary.lastTimestamp is normally an ISO timestamp, but fixtures and
// older cache entries can be incomplete. Valid timestamps win over invalid
// ones; two invalid values are an exact tie and are resolved alphabetically by
// the caller.
function compareTimestamps(a: string, b: string): number {
const aMs = Date.parse(a)
const bMs = Date.parse(b)
const aValid = Number.isFinite(aMs)
const bValid = Number.isFinite(bMs)
if (aValid && bValid) return aMs - bMs
if (aValid) return 1
if (bValid) return -1
return 0
}
function preferredProjectName(projectNames: Set<string>): string {
return [...projectNames].sort()[0] ?? 'Unknown project'
}
function preferredSessionTitle(titleCandidates: Map<string, SessionTitleCandidate>): string | undefined {
const cleaned = [...titleCandidates.values()]
.map(candidate => {
const title = cleanSessionTitle(candidate.title)
return title === undefined ? undefined : { title, lastTimestamp: candidate.lastTimestamp }
})
.filter((candidate): candidate is SessionTitleCandidate => candidate !== undefined)
cleaned.sort((a, b) => {
const timestampOrder = compareTimestamps(b.lastTimestamp, a.lastTimestamp)
if (timestampOrder !== 0) return timestampOrder
return a.title < b.title ? -1 : a.title > b.title ? 1 : 0
})
return cleaned[0]?.title
}
function buildSessionLabels(inputs: Map<string, SessionLabelInfo>): Map<string, string> {
// Stable raw-key order makes the residual used-label guard independent of
// project/session discovery order when a title happens to match another
// label shape.
const entries: SessionLabelEntry[] = [...inputs.entries()].map(([key, info]) => {
const sessionLabel = preferredSessionTitle(info.titleCandidates)
?? shortProjectLabel(info.projectPath, preferredProjectName(info.projectNames))
return {
key,
info,
baseLabel: `${shortSessionId(info.sessionId)} (${info.provider}) · ${sessionLabel}`,
}
}).sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0)
const byBaseLabel = new Map<string, SessionLabelEntry[]>()
for (const entry of entries) {
const group = byBaseLabel.get(entry.baseLabel) ?? []
group.push(entry)
byBaseLabel.set(entry.baseLabel, group)
}
const labels = new Map<string, string>()
const usedLabels = new Set<string>()
const setUniqueLabel = (entry: SessionLabelEntry, candidate: string): void => {
let label = candidate
if (usedLabels.has(label)) {
const identity = `${candidate} · ${entry.info.projectPath} · ${entry.info.sessionId}`
label = identity
let suffix = 2
while (usedLabels.has(label)) label = `${identity} · ${suffix++}`
}
labels.set(entry.key, label)
usedLabels.add(label)
}
for (const group of byBaseLabel.values()) {
if (group.length === 1) {
setUniqueLabel(group[0]!, group[0]!.baseLabel)
continue
}
const projectLabels = group.map(entry => shortProjectLabel(entry.info.projectPath, preferredProjectName(entry.info.projectNames)))
if (new Set(projectLabels).size === group.length) {
for (let i = 0; i < group.length; i++) {
const entry = group[i]!
setUniqueLabel(entry, `${entry.baseLabel} · ${projectLabels[i]}`)
}
continue
}
// A short project label can still collide (for example two worktrees with
// the same final path segments). The full path + id is only used for this
// residual collision, and is unique because provider/path/id form the key.
for (const entry of group) {
setUniqueLabel(entry, `${entry.baseLabel} · ${entry.info.projectPath} · ${entry.info.sessionId}`)
}
}
return labels
}
// Legend labels: the sanitized project dir ("-Users-name-Projects-app") is
// unreadable, so prefer the real projectPath's last two segments ("app/web").
// Fall back to the sanitized name when no usable path exists.
@ -184,7 +317,7 @@ export function buildGranularHistory(
const modelTotals = new Map<string, Totals>()
const sessionTotals = new Map<string, Totals>()
const modelLabels = new Map<string, string>()
const sessionLabels = new Map<string, string>()
const sessionLabelInputs = new Map<string, SessionLabelInfo>()
let callCount = 0
for (const project of projects) {
@ -214,7 +347,27 @@ export function buildGranularHistory(
add(modelTotals, modelKey, cost, tokens)
add(sessionTotals, sessionKey, cost, tokens)
modelLabels.set(modelKey, modelKey === '<synthetic>' ? 'Other model' : modelKey)
sessionLabels.set(sessionKey, `${shortProjectLabel(project.projectPath, projectName)} · ${shortSessionId(session.sessionId)} (${call.provider})`)
// Collect raw metadata first. Titles are cleaned once per distinct
// session-key candidate after all calls are aggregated, so a late
// cache title can win without putting sanitisation on the call path.
const labelInfo = sessionLabelInputs.get(sessionKey) ?? {
provider: call.provider,
projectPath: project.projectPath,
projectNames: new Set<string>(),
sessionId: session.sessionId,
titleCandidates: new Map<string, SessionTitleCandidate>(),
}
labelInfo.projectNames.add(projectName)
if (session.title !== undefined) {
const existingTitle = labelInfo.titleCandidates.get(session.title)
if (!existingTitle || compareTimestamps(session.lastTimestamp, existingTitle.lastTimestamp) > 0) {
labelInfo.titleCandidates.set(session.title, {
title: session.title,
lastTimestamp: session.lastTimestamp,
})
}
}
sessionLabelInputs.set(sessionKey, labelInfo)
callCount++
}
}
@ -225,6 +378,7 @@ export function buildGranularHistory(
return { bucketMinutes, modelSeries: [], sessionSeries: [], points: [] }
}
const sessionLabels = buildSessionLabels(sessionLabelInputs)
const modelProjection = projectSeries(rawBuckets, 'models', modelTotals, modelLabels)
const sessionProjection = projectSeries(rawBuckets, 'sessions', sessionTotals, sessionLabels)
return {

View file

@ -2,7 +2,7 @@ import { isAbsolute } from 'path'
import { Command, Option } from 'commander'
import { installMenubarApp } from './menubar-installer.js'
import { exportCsv, exportJson, type PeriodExport } from './export.js'
import { findUnpricedModels, loadPricing, setModelAliases, setPriceOverrides, setLocalModelSavings, setProxyPaths, normalizeProxyPath } from './models.js'
import { findUnpricedModels, loadPricing, sanitizeModelForDisplay, setModelAliases, setPriceOverrides, setLocalModelSavings, setProxyPaths, normalizeProxyPath } from './models.js'
import { parseAllSessions, filterProjectsByName, filterProjectsByDateRange, clearSessionCache, setInteractiveScanUI } from './parser.js'
import { allProviderNames, getAllProviders } from './providers/index.js'
import { getProvider } from './providers/index.js'
@ -12,6 +12,7 @@ import { toDateString } from './daily-cache.js'
import { dateKey } from './day-aggregator.js'
import { isBehavioralCall, isBehavioralTurn } from './behavioral-weight.js'
import { CATEGORY_LABELS, type DateRange, type ProjectSummary, type TaskCategory } from './types.js'
import type { AppliedFix } from './act/types.js'
import { aggregateModelEfficiency } from './model-efficiency.js'
import { buildPeriodData, buildMenubarPayloadForRange, buildDurablePeriod, type DurablePeriod } from './usage-aggregator.js'
import { renderDashboard } from './dashboard.js'
@ -1320,12 +1321,13 @@ program
program
.command('menubar')
.description('Install and launch the macOS menubar app (one command, no clone)')
.option('--force', 'Reinstall even if an older copy is already in ~/Applications')
.description('Install and launch the menubar app on macOS and Windows (one command, no clone)')
.option('--force', 'Reinstall even if a copy is already installed')
.action(async (opts: { force?: boolean }) => {
try {
const result = await installMenubarApp({ force: opts.force, cliVersion: version })
console.log(`\n Ready. ${result.installedPath}\n`)
// A cancelled Windows installer leaves nothing to point at.
if (result.installedPath) console.log(`\n Ready. ${result.installedPath}\n`)
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
console.error(`\n Menubar install failed: ${message}\n`)
@ -1816,6 +1818,7 @@ program
.option('--yes', 'With --apply: apply every appliable fix without prompting')
.option('--dry-run', 'With --apply: print the plan and exit without changing anything')
.option('--only <ids>', 'With --apply: restrict to a comma-separated list of finding ids')
.option('--auto-revert', 'Undo applied fixes that measured no reduction (never CLAUDE.md rules)')
.action(async (opts) => {
assertProvider(opts.provider, 'optimize')
const format = opts.json ? 'json' : opts.format
@ -1841,28 +1844,35 @@ program
const projects = await parseAllSessions(range, opts.provider)
if (opts.apply) {
const { runOptimizeApply } = await import('./act/optimize-apply.js')
await runOptimizeApply(projects, range, { yes: opts.yes, dryRun: opts.dryRun, only: opts.only })
await runOptimizeApply(projects, range, { yes: opts.yes, dryRun: opts.dryRun, only: opts.only, provider: opts.provider })
return
}
assertFormat(format, ['text', 'json'], 'optimize')
if (format === 'text') {
// Surface realized savings from applied actions. Best effort: optimize
// must never fail because of journal contents, so any error just drops
// the header. computeActReport returns fast without scanning when the
// journal has no eligible applied actions, so users who never opted in
// see identical output.
let appliedHeader: string | undefined
let previouslyApplied: Record<string, string> | undefined
try {
const { computeActReport, buildOptimizeAppliedHeader } = await import('./act/report.js')
const applied = await computeActReport()
appliedHeader = buildOptimizeAppliedHeader(applied) ?? undefined
previouslyApplied = applied.appliedByFinding
} catch { /* the header is optional; never block the findings */ }
await runOptimize(projects, label, range, { format, appliedHeader, previouslyApplied })
} else {
await runOptimize(projects, label, range, { format })
}
// Surface realized savings from applied actions, and re-measure every one
// of them. Best effort: optimize must never fail because of journal
// contents, so any error just drops the extras. computeActReport returns
// fast without scanning when the journal has no applied actions, so users
// who never opted in see identical output.
let appliedHeader: string | undefined
let previouslyApplied: Record<string, string> | undefined
let appliedFixes: AppliedFix[] | undefined
try {
const { computeActReport, buildOptimizeAppliedHeader, autoRevertNoEffect } = await import('./act/report.js')
const applied = await computeActReport()
appliedHeader = buildOptimizeAppliedHeader(applied) ?? undefined
previouslyApplied = applied.appliedByFinding
appliedFixes = applied.appliedFixes
if (opts.autoRevert) {
const { lines, revertedIds } = await autoRevertNoEffect(appliedFixes)
appliedFixes = appliedFixes.filter(f => !revertedIds.has(f.id))
// JSON output must stay parseable, so the revert log goes to stderr there.
for (const line of lines) {
if (format === 'json') process.stderr.write(` ${line}\n`)
else console.log(` ${line}`)
}
}
} catch { /* the applied section is optional; never block the findings */ }
await runOptimize(projects, label, range, { format, appliedHeader, previouslyApplied, appliedFixes, provider: opts.provider })
})
program
@ -2085,6 +2095,7 @@ program
.option('--by-agent', 'One row per (provider, model, agent) instead of one row per (provider, model). Claude subagent transcripts only; other providers and main sessions bucket under "main"')
.option('--top <n>', 'Show only the top N rows', (v: string) => parseInt(v, 10))
.option('--min-cost <usd>', 'Hide rows below this cost threshold', (v: string) => parseFloat(v))
.option('--unpriced', 'Show only models with usage that currently price at $0')
.option('--no-totals', 'Suppress the footer totals row')
.option('--format <format>', 'Output format: table, markdown, json, csv', 'table')
.action(async (opts) => {
@ -2109,27 +2120,60 @@ program
}
const projects = await parseAllSessions(range, opts.provider)
const rows = await aggregateModels(projects, {
const topN = typeof opts.top === 'number' && Number.isFinite(opts.top) ? opts.top : undefined
let rows = await aggregateModels(projects, {
byTask: !!opts.byTask,
byAgent: !!opts.byAgent,
taskFilter: opts.task,
topN: typeof opts.top === 'number' && Number.isFinite(opts.top) ? opts.top : undefined,
minCost: typeof opts.minCost === 'number' && Number.isFinite(opts.minCost) ? opts.minCost : 0.01,
// `aggregateModels` filters and slices before the unpriced filter. Its
// rows are sorted cost-first, so a small --top would remove exactly the
// rows `--unpriced` exists to show. Take the whole set here and slice
// after filtering and ranking instead.
topN: opts.unpriced ? undefined : topN,
minCost: typeof opts.minCost === 'number' && Number.isFinite(opts.minCost) ? opts.minCost : (opts.unpriced ? 0 : 0.01),
})
if (opts.unpriced) {
const unpriced = findUnpricedModels(rows.map(row => ({
model: row.model,
calls: row.calls,
cost: row.costUSD,
tokens: row.totalTokens,
})))
const unpricedRank = new Map<string, number>()
for (const [rank, usage] of unpriced.entries()) {
// Breakdown modes can emit several rows for one model. Keep the first
// rank so all rows for that model stay together and N still counts rows.
if (!unpricedRank.has(usage.model)) unpricedRank.set(usage.model, rank)
}
rows = rows
.filter(row => unpricedRank.has(row.model))
.sort((a, b) => (unpricedRank.get(a.model)! - unpricedRank.get(b.model)!))
if (topN !== undefined) rows = rows.slice(0, topN)
}
const fmt = (opts.format ?? 'table').toLowerCase()
if (rows.length === 0 && (fmt === 'table' || fmt === 'markdown')) {
process.stdout.write('No model usage found for the selected period.\n')
process.stdout.write(opts.unpriced
? 'No unpriced models found for the selected period.\n'
: 'No model usage found for the selected period.\n')
return
}
// The friendly name is useless for `model-alias`, which keys on the raw ID.
// Sanitized because this bypasses the shared display path in models-report.
const renderRows = opts.unpriced && fmt !== 'json'
? rows.map(row => ({ ...row, modelDisplayName: sanitizeModelForDisplay(row.model) }))
: rows
if (fmt === 'json') {
process.stdout.write(renderJson(rows) + '\n')
} else if (fmt === 'csv') {
process.stdout.write(renderCsv(rows, { byTask: !!opts.byTask, byAgent: !!opts.byAgent }) + '\n')
process.stdout.write(renderCsv(renderRows, { byTask: !!opts.byTask, byAgent: !!opts.byAgent }) + '\n')
} else if (fmt === 'markdown' || fmt === 'md') {
process.stdout.write(renderMarkdown(rows, { byTask: !!opts.byTask, byAgent: !!opts.byAgent, showTotals: opts.totals !== false }) + '\n')
process.stdout.write(renderMarkdown(renderRows, { byTask: !!opts.byTask, byAgent: !!opts.byAgent, showTotals: opts.totals !== false }) + '\n')
} else if (fmt === 'table') {
process.stdout.write(renderTable(rows, { byTask: !!opts.byTask, byAgent: !!opts.byAgent, showTotals: opts.totals !== false }) + '\n')
process.stdout.write(renderTable(renderRows, { byTask: !!opts.byTask, byAgent: !!opts.byAgent, showTotals: opts.totals !== false }) + '\n')
// Never advise aliasing unconditionally: a subscription or flat-rate model
// is correctly $0, and mapping it onto another model's rate invents spend.
if (opts.unpriced) process.stdout.write('If a model is billed per token, map it with: codeburn model-alias "<model>" <known-model>. Subscription or flat-rate models are correctly $0.\n')
} else {
process.stderr.write(`codeburn: unknown --format "${opts.format}". Choose table, markdown, json, or csv.\n`)
process.exit(1)

View file

@ -8,6 +8,7 @@ import { pipeline } from 'node:stream/promises'
import { Readable } from 'node:stream'
import { ProxyAgent, fetch as undiciFetch } from 'undici'
import { getCodeburnCacheDir } from './cache-dir.js'
import {
buildPersistentCodeburnLookupPath,
resolvePersistentCodeburnPathFromWhichOutput,
@ -22,6 +23,11 @@ const EXPECTED_BUNDLE_ID = 'org.agentseal.codeburn-menubar'
const VERSIONED_ASSET_PATTERN = /^CodeBurnMenubar-v.+\.zip$/
const APP_PROCESS_NAME = 'CodeBurnMenubar'
const SUPPORTED_OS = 'darwin'
/// The Windows tray app (windows/) ships as an .msi under its own `windows-v*` tag. GitHub
/// rewrites the spaces in the bundle name to dots when it stores the asset, so both the asset
/// name and its download URL carry `CodeBurn.Menubar_...`.
const WINDOWS_PRODUCT_NAME = 'CodeBurn Menubar'
const WINDOWS_ASSET_PATTERN = /^CodeBurn\.Menubar_.+_x64_en-US\.msi$/
const MIN_MACOS_MAJOR = 14
const PERSISTED_CLI_PATH = join(homedir(), 'Library', 'Application Support', 'CodeBurn', 'codeburn-cli-path.v1')
const PERSISTENT_CLI_REQUIRED_MESSAGE =
@ -31,8 +37,45 @@ export type InstallResult = { installedPath: string; launched: boolean }
export type ReleaseAsset = { name: string; browser_download_url: string }
export type ReleaseResponse = { tag_name: string; assets: ReleaseAsset[] }
/// `zip` is the platform's primary asset: the mac bundle zip, or the Windows .msi.
export type ResolvedAssets = { release: ReleaseResponse; zip: ReleaseAsset; checksum: ReleaseAsset }
export type InstallOptions = { force?: boolean; cliVersion?: string }
export type InstallOptions = {
force?: boolean
cliVersion?: string
platform?: string
windows?: WindowsInstallHooks
}
/// What differs per platform between the mac and Windows installs: which release tag holds the
/// build, and which asset in it is the installable. Everything downstream - versioned URL first,
/// release-API scan as fallback, retrying download, checksum verify - is shared.
export type ReleaseSpec = {
tagPrefix: string
assetPattern: RegExp
assetName: (version: string) => string
missingAsset: (tag: string) => string
noRelease: string
}
const MAC_RELEASE: ReleaseSpec = {
tagPrefix: 'mac-v',
assetPattern: VERSIONED_ASSET_PATTERN,
assetName: version => `CodeBurnMenubar-v${version}.zip`,
missingAsset: tag =>
`No ${APP_BUNDLE_NAME} versioned zip found in release ${tag}. ` +
`Check https://github.com/getagentseal/codeburn/releases.`,
noRelease: 'No mac-v* release with a CodeBurnMenubar-v*.zip and checksum was found.',
}
export const WINDOWS_RELEASE: ReleaseSpec = {
tagPrefix: 'windows-v',
assetPattern: WINDOWS_ASSET_PATTERN,
assetName: version => `CodeBurn.Menubar_${version}_x64_en-US.msi`,
missingAsset: tag =>
`No ${WINDOWS_PRODUCT_NAME} .msi found in release ${tag}. ` +
`Check https://github.com/getagentseal/codeburn/releases.`,
noRelease: 'No windows-v* release with a CodeBurn.Menubar_*.msi and checksum was found.',
}
type ProxyEnv = Partial<Record<'HTTPS_PROXY' | 'https_proxy' | 'HTTP_PROXY' | 'http_proxy' | 'NO_PROXY' | 'no_proxy', string>>
type FetchOptions = Parameters<typeof undiciFetch>[1]
type HeaderGetter = { get(name: string): string | null }
@ -47,6 +90,10 @@ type FetchLikeResponse = {
text(): Promise<string>
}
type FetchImpl = (url: string, options?: FetchOptions) => Promise<FetchLikeResponse>
/// The release-API lookup reads JSON instead of streaming a body, so it takes its own narrow
/// response shape rather than widening FetchLikeResponse for every asset download fake.
export type ReleaseApiFetch = (url: string, options?: FetchOptions) =>
Promise<{ ok: boolean; status: number; headers: HeaderGetter; json(): Promise<unknown> }>
/// Release-asset delivery (github.com -> Azure blob) occasionally returns a transient 5xx or
/// drops the socket. Three attempts with a short exponential backoff (0.5s, then 1s) rides out
@ -96,14 +143,9 @@ function fetchWithProxy(url: string, options: FetchOptions = {}) {
return undiciFetch(url, dispatcher ? { ...options, dispatcher } : options)
}
export function resolveMenubarReleaseAssets(release: ReleaseResponse): ResolvedAssets {
const zip = release.assets.find(a => VERSIONED_ASSET_PATTERN.test(a.name))
if (!zip) {
throw new Error(
`No ${APP_BUNDLE_NAME} versioned zip found in release ${release.tag_name}. ` +
`Check https://github.com/getagentseal/codeburn/releases.`
)
}
export function resolveMenubarReleaseAssets(release: ReleaseResponse, spec: ReleaseSpec = MAC_RELEASE): ResolvedAssets {
const zip = release.assets.find(a => spec.assetPattern.test(a.name))
if (!zip) throw new Error(spec.missingAsset(release.tag_name))
const checksum = release.assets.find(a => a.name === `${zip.name}.sha256`)
if (!checksum) {
throw new Error(`Missing checksum asset ${zip.name}.sha256 in release ${release.tag_name}.`)
@ -111,28 +153,28 @@ export function resolveMenubarReleaseAssets(release: ReleaseResponse): ResolvedA
return { release, zip, checksum }
}
export function resolveLatestMenubarReleaseAssets(releases: ReleaseResponse[]): ResolvedAssets {
export function resolveLatestMenubarReleaseAssets(releases: ReleaseResponse[], spec: ReleaseSpec = MAC_RELEASE): ResolvedAssets {
for (const release of releases) {
if (!release.tag_name.startsWith('mac-v')) continue
if (!release.tag_name.startsWith(spec.tagPrefix)) continue
try {
return resolveMenubarReleaseAssets(release)
return resolveMenubarReleaseAssets(release, spec)
} catch {
continue
}
}
throw new Error('No mac-v* release with a CodeBurnMenubar-v*.zip and checksum was found.')
throw new Error(spec.noRelease)
}
function normalizeCliVersion(cliVersion: string): string {
return cliVersion.trim().replace(/^v/, '')
}
export function resolveVersionedMenubarReleaseAssets(cliVersion: string): ResolvedAssets {
export function resolveVersionedMenubarReleaseAssets(cliVersion: string, spec: ReleaseSpec = MAC_RELEASE): ResolvedAssets {
const version = normalizeCliVersion(cliVersion)
if (!version) throw new Error('Cannot resolve CodeBurn Menubar release without a CLI version.')
const tagName = `mac-v${version}`
const zipName = `CodeBurnMenubar-v${version}.zip`
const tagName = `${spec.tagPrefix}${version}`
const zipName = spec.assetName(version)
const checksumName = `${zipName}.sha256`
const releaseBase = `${RELEASE_DOWNLOAD_BASE}/${tagName}`
const zip = { name: zipName, browser_download_url: `${releaseBase}/${zipName}` }
@ -207,8 +249,8 @@ async function sysProductVersion(): Promise<string> {
})
}
async function fetchLatestReleaseAssets(): Promise<ResolvedAssets> {
const response = await fetchWithProxy(RELEASE_API, {
async function fetchLatestReleaseAssets(spec: ReleaseSpec = MAC_RELEASE, fetchImpl?: ReleaseApiFetch): Promise<ResolvedAssets> {
const response = await (fetchImpl ?? fetchWithProxy)(RELEASE_API, {
headers: {
'User-Agent': 'codeburn-menubar-installer',
Accept: 'application/vnd.github+json',
@ -218,7 +260,7 @@ async function fetchLatestReleaseAssets(): Promise<ResolvedAssets> {
throw new HttpStatusError(formatGitHubReleaseLookupError(response.status, response.headers), response.status)
}
const body = await response.json() as ReleaseResponse[]
return resolveLatestMenubarReleaseAssets(body)
return resolveLatestMenubarReleaseAssets(body, spec)
}
/// 5xx means "GitHub/the CDN is unhappy right now" and is worth another attempt. 4xx is not:
@ -473,7 +515,165 @@ async function killRunningApp(): Promise<void> {
}
}
/// Windows mirror of the mac install below: pin the release to the CLI's own version, fall back
/// to the newest windows-v* release, verify the sha256 before anything executes the file, hand
/// the .msi to msiexec, then launch what it installed.
const WINDOWS_UNINSTALL_KEYS = [
'HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall',
'HKLM\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall',
'HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall',
]
/// 3010 is "installed, reboot to finish"; 1602 is the user closing the UAC/installer prompt.
const MSI_EXIT_REBOOT_REQUIRED = 3010
const MSI_EXIT_USER_CANCEL = 1602
export type WindowsInstallHooks = {
fetchOptions?: AssetFetchOptions
apiFetch?: ReleaseApiFetch
runInstaller?: (exe: string, args: string[]) => Promise<number>
queryRegistry?: () => Promise<string>
launch?: (exePath: string) => void
log?: (message: string) => void
stagingDir?: string
env?: NodeJS.ProcessEnv
}
export type InstalledWindowsMenubar = { version: string; exePath: string }
/// Windows' `CreateProcess` searches the current directory before `PATH`, so spawning `msiexec`
/// or `reg` by bare name lets anything dropped next to the CLI impersonate a system tool. Same
/// rule the tray app follows (windows/src-tauri/src/cli.rs: system32_path).
export function resolveSystem32Path(exe: string, env: NodeJS.ProcessEnv = process.env): string {
const root = env.SystemRoot
const base = root && /^[a-zA-Z]:[\\/]/.test(root) ? root.replace(/[\\/]+$/, '') : 'C:\\Windows'
return `${base}\\System32\\${exe}`
}
/// Reads `reg query ... /s` output, which prints one blank-line separated block per subkey.
export function parseInstalledWindowsMenubar(regOutput: string): InstalledWindowsMenubar | undefined {
for (const block of regOutput.split(/\r?\n\s*\r?\n/)) {
const values = new Map<string, string>()
for (const line of block.split(/\r?\n/)) {
const match = /^\s+(.+?)\s{4}REG_\w+\s{4}(.*)$/.exec(line)
if (match) values.set(match[1]!.trim(), match[2]!.trim())
}
if (values.get('DisplayName') !== WINDOWS_PRODUCT_NAME) continue
const location = values.get('InstallLocation')
// DisplayIcon is `<exe>[,<index>]` and points at the installed binary when there is no
// InstallLocation to join onto.
const icon = values.get('DisplayIcon')?.split(',')[0]?.trim()
const exePath = location
? `${location.replace(/[\\/]+$/, '')}\\${WINDOWS_PRODUCT_NAME}.exe`
: icon
if (!exePath) continue
return { version: values.get('DisplayVersion') ?? '', exePath }
}
return undefined
}
async function queryWindowsUninstallRegistry(env: NodeJS.ProcessEnv): Promise<string> {
const reg = resolveSystem32Path('reg.exe', env)
// reg exits non-zero for a hive the machine does not have; an empty block is the right answer.
const outputs = await Promise.all(
WINDOWS_UNINSTALL_KEYS.map(key => captureCommand(reg, ['query', key, '/s']).catch(() => '')),
)
return outputs.join('\n\n')
}
async function runMsiexec(exe: string, args: string[]): Promise<number> {
return new Promise((resolve, reject) => {
const proc = spawn(exe, args, { stdio: 'inherit' })
proc.on('error', reject)
proc.on('close', code => resolve(code ?? 1))
})
}
function launchWindowsApp(exePath: string): void {
const proc = spawn(exePath, [], { detached: true, stdio: 'ignore' })
proc.on('error', err => console.error(`Could not launch ${exePath}: ${err.message}`))
proc.unref()
}
async function stageWindowsInstaller(
assets: ResolvedAssets,
stagingDir: string,
hooks: WindowsInstallHooks,
log: (message: string) => void,
): Promise<string> {
const { zip: msi, checksum } = assets
const msiPath = join(stagingDir, msi.name)
log(`Downloading ${msi.name}...`)
await downloadToFile(msi.browser_download_url, msiPath, hooks.fetchOptions)
log('Verifying checksum...')
await verifyChecksum(msiPath, checksum.browser_download_url, hooks.fetchOptions)
return msiPath
}
async function installWindowsMenubarApp(options: InstallOptions): Promise<InstallResult> {
const hooks = options.windows ?? {}
const log = hooks.log ?? console.log
const env = hooks.env ?? process.env
const queryRegistry = hooks.queryRegistry ?? (() => queryWindowsUninstallRegistry(env))
const launch = hooks.launch ?? launchWindowsApp
const cliVersion = options.cliVersion ? normalizeCliVersion(options.cliVersion) : ''
const installed = parseInstalledWindowsMenubar(await queryRegistry())
if (installed && !options.force && (!cliVersion || installed.version === cliVersion)) {
launch(installed.exePath)
log('Launched CodeBurn Menubar.')
return { installedPath: installed.exePath, launched: true }
}
let assets: ResolvedAssets
if (cliVersion) {
log(`Resolving CodeBurn Menubar v${cliVersion}...`)
assets = resolveVersionedMenubarReleaseAssets(cliVersion, WINDOWS_RELEASE)
} else {
log('Looking up the latest CodeBurn Menubar release...')
assets = await fetchLatestReleaseAssets(WINDOWS_RELEASE, hooks.apiFetch)
}
const stagingDir = hooks.stagingDir ?? await (async () => {
await mkdir(getCodeburnCacheDir(), { recursive: true })
return mkdtemp(join(getCodeburnCacheDir(), 'menubar-'))
})()
try {
let msiPath: string
try {
msiPath = await stageWindowsInstaller(assets, stagingDir, hooks, log)
} catch (err) {
if (!cliVersion || !isMissingDirectAssetError(err)) throw err
log(`CodeBurn Menubar v${cliVersion} assets were not found. Looking up the latest CodeBurn Menubar release...`)
assets = await fetchLatestReleaseAssets(WINDOWS_RELEASE, hooks.apiFetch)
msiPath = await stageWindowsInstaller(assets, stagingDir, hooks, log)
}
log('Installing...')
const msiexec = resolveSystem32Path('msiexec.exe', env)
const exitCode = await (hooks.runInstaller ?? runMsiexec)(msiexec, ['/i', msiPath, '/passive', '/norestart'])
if (exitCode === MSI_EXIT_USER_CANCEL) {
log('Installation was cancelled; nothing was installed.')
return { installedPath: '', launched: false }
}
if (exitCode !== 0 && exitCode !== MSI_EXIT_REBOOT_REQUIRED) {
throw new Error(`msiexec exited with ${exitCode} while installing ${assets.zip.name}.`)
}
if (exitCode === MSI_EXIT_REBOOT_REQUIRED) log('Windows wants a restart to finish the install.')
const nowInstalled = parseInstalledWindowsMenubar(await queryRegistry())
if (!nowInstalled) {
throw new Error('CodeBurn Menubar installed, but it was not found in the uninstall registry; start it from the Start menu.')
}
launch(nowInstalled.exePath)
log('Launched CodeBurn Menubar.')
return { installedPath: nowInstalled.exePath, launched: true }
} finally {
if (!hooks.stagingDir) await rm(stagingDir, { recursive: true, force: true })
}
}
export async function installMenubarApp(options: InstallOptions = {}): Promise<InstallResult> {
if ((options.platform ?? platform()) === 'win32') return installWindowsMenubarApp(options)
await ensureSupportedPlatform()
await persistCodeburnPath()

View file

@ -4,6 +4,7 @@ import stripAnsi from 'strip-ansi'
import { isBehavioralCall } from './behavioral-weight.js'
import { codexCredits } from './codex-credits.js'
import { formatCost, formatTokens } from './format.js'
import { sanitizeModelForDisplay } from './models.js'
import { getProvider } from './providers/index.js'
import { CATEGORY_LABELS, type ProjectSummary, type TaskCategory } from './types.js'
@ -160,7 +161,9 @@ export async function aggregateModels(projects: ProjectSummary[], opts: Aggregat
const p = await getProvider(name)
const entry = {
displayName: p?.displayName ?? name,
formatModel: p ? (m: string) => p.modelDisplayName(m) : (m: string) => m,
formatModel: p
? (m: string) => sanitizeModelForDisplay(p.modelDisplayName(m))
: sanitizeModelForDisplay,
}
providerCache.set(name, entry)
return entry

View file

@ -297,6 +297,10 @@ const BUILTIN_ALIASES: Record<string, string> = {
'k3-agent': 'kimi-k3',
'k2d6-agent': 'kimi-k2p6',
'mimo-v2-flash': 'xiaomi/mimo-v2-flash',
// Hermes / Xiaomi token-plan sessions store the bare id. LiteLLM's row is
// namespaced. Same class as mimo-v2-flash above — do not invent a rate.
'mimo-v2.5-pro': 'xiaomi/mimo-v2.5-pro',
'mimo-v2.5': 'xiaomi/mimo-v2.5',
'kat-coder-pro-v1': 'kwaipilot/kat-coder-pro',
// Cursor emits dot-version tier-last names plus tier/reasoning suffixes
// that LiteLLM does not index (`-high`, `-low`, `-medium`, `-thinking`,
@ -307,7 +311,7 @@ const BUILTIN_ALIASES: Record<string, string> = {
// reports that quote literal slugs (e.g. forum.cursor.com/t/154933).
'claude-4-sonnet': 'claude-sonnet-4',
'claude-4-sonnet-1m': 'claude-sonnet-4',
'claude-4-sonnet-thinking': 'claude-sonnet-4-5',
'claude-4-sonnet-thinking': 'claude-sonnet-4',
'claude-4.5-sonnet': 'claude-sonnet-4-5',
'claude-4.5-sonnet-thinking': 'claude-sonnet-4-5',
'claude-4.6-sonnet': 'claude-sonnet-4-6',
@ -791,6 +795,11 @@ function shouldWarnAboutUnknownModel(name: string): boolean {
return true
}
/** Render provider-supplied model IDs without terminal control characters. */
export function sanitizeModelForDisplay(model: string): string {
return model.replace(/[\x00-\x1F\x7F-\x9F]/g, '?').slice(0, 200)
}
export function calculateCost(
model: string,
inputTokens: number,
@ -808,7 +817,7 @@ export function calculateCost(
// Strip control characters and cap length: model names come from JSONL
// payloads written by external tools, so a hostile or corrupt file
// could embed terminal escape sequences here.
const safeName = model.replace(/[\x00-\x1F\x7F-\x9F]/g, '?').slice(0, 200)
const safeName = sanitizeModelForDisplay(model)
const aliasHint = `Map it with: codeburn model-alias "${safeName}" <known-model>, or track local-model savings with: codeburn model-savings "${safeName}" <baseline-model>`
process.stderr.write(
`codeburn: no pricing data for model "${safeName}" — costs for this model will show $0. ` +
@ -942,11 +951,17 @@ const SHORT_NAMES: Record<string, string> = {
// The Grok Build harness reports the model it runs (`grok-4.5`), so this is
// the model's own name; `grok-build*` ids still resolve to "Grok Build".
'grok-4.5': 'Grok 4.5',
// The harness also reports a `-build` variant of that model. It is a distinct
// id and reports bucket by id, so without its own entry the prefix match gave
// it the same name as `grok-4.5` and the report showed two identical rows.
'grok-4.5-build': 'Grok 4.5 (build)',
// ClinePass routes models as `cline-pass/<slug>`; getShortModelName's path
// fallback strips the prefix and re-resolves the bare slug through this
// table, the same way it handles `accounts/fireworks/models/<slug>`.
'qwen3.7-max': 'Qwen 3.7 Max',
'mimo-v2.5-pro': 'MiMo v2.5 Pro',
'mimo-v2.5': 'MiMo v2.5',
'mimo-v2-flash': 'MiMo v2 Flash',
// Both spellings occur in the wild: OpenRouter gap-filled keys are lowercase
// slugs while sessions report the capitalized name (see the case-insensitive
// pricing index above). SHORT_NAMES matching is case-sensitive, so map both.
@ -972,28 +987,57 @@ function deriveClaudeShortName(canonical: string): string | undefined {
return `${CLAUDE_FAMILY[family]} ${major}${minor ? `.${minor}` : ''}`
}
export function getShortModelName(model: string): string {
if (autoModelNames[model]) return autoModelNames[model]
const canonical = resolveAlias(getCanonicalName(model))
const claude = deriveClaudeShortName(canonical)
function lookupShortName(id: string): string | undefined {
const claude = deriveClaudeShortName(id)
if (claude) return claude
for (const [key, name] of SORTED_SHORT_NAMES) {
// Match on a version boundary, not a bare prefix: an unlisted future minor
// (e.g. gpt-5.6) must NOT collapse into the base "gpt-5" entry — it should
// fall through to its raw id rather than show a wrong name/tier.
if (canonical === key || canonical.startsWith(key + '-')) return name
if (id === key || id.startsWith(key + '-')) return name
}
// getCanonicalName only strips the leading provider prefix, so a raw
// path-style id (e.g. accounts/fireworks/models/glm-5p2) still has slashes
// here. Take the last path segment and re-resolve it: the segment may itself
// be a known model slug (Fireworks fleet ids), earning a friendly name; a
// genuinely unmapped slug resolves to itself, preserving the raw-segment
// fallback for everything else.
return undefined
}
// Public API stays unary so Array.map/forEach cannot feed index as cycle state.
export function getShortModelName(model: string): string {
return shortModelName(model, new Set())
}
function shortModelName(model: string, seen: Set<string>): string {
if (autoModelNames[model]) return autoModelNames[model]
if (seen.has(model)) {
const leaf = model.includes('/') ? model.slice(model.lastIndexOf('/') + 1) : model
return lookupShortName(leaf) ?? leaf
}
seen.add(model)
// User aliases win over built-in display names. A remap of gpt-4o must
// show the target, not "GPT-4o".
if (Object.hasOwn(userAliases, model)) {
return shortModelName(userAliases[model]!, seen)
}
const stripped = getCanonicalName(model)
if (stripped !== model) {
if (Object.hasOwn(userAliases, stripped)) {
return shortModelName(userAliases[stripped]!, seen)
}
const knownStripped = lookupShortName(stripped)
if (knownStripped && !Object.hasOwn(BUILTIN_ALIASES, stripped) && !Object.hasOwn(BUILTIN_ALIASES, stripped.toLowerCase())) {
return knownStripped
}
}
const canonical = resolveAlias(stripped)
const known = lookupShortName(canonical)
if (known) return known
if (canonical.includes('/')) {
const segment = canonical.slice(canonical.lastIndexOf('/') + 1)
return segment ? getShortModelName(segment) : canonical
if (!segment || seen.has(segment) || segment === stripped) {
return lookupShortName(segment) ?? segment
}
return shortModelName(segment, seen)
}
return canonical
return lookupShortName(canonical) ?? canonical
}
// Pricing is process-global state assembled at CLI startup from the cached

File diff suppressed because it is too large Load diff

View file

@ -559,7 +559,7 @@ function extractObjectFields(
return captured
}
const LARGE_ROOT_FIELDS = ['type', 'timestamp', 'sessionId', 'cwd', 'gitBranch', 'attachment', 'message'] as const
const LARGE_ROOT_FIELDS = ['type', 'timestamp', 'sessionId', 'cwd', 'gitBranch', 'attachment', 'message', 'isSidechain', 'promptSource'] as const
const LARGE_ASSISTANT_MESSAGE_FIELDS = ['model', 'usage', 'id', 'content'] as const
function parseLargeJsonl(line: string | Buffer): JournalEntry | null {
@ -573,14 +573,19 @@ function parseLargeJsonl(line: string | Buffer): JournalEntry | null {
if (!type) return null
const entry: JournalEntry = { type }
if (root['isSidechain']?.kind === 'scalar' && source.slice(root['isSidechain'].start, root['isSidechain'].end) === 'true') {
entry.isSidechain = true
}
const timestamp = readJsonString(source, root['timestamp'])
const sessionId = readJsonString(source, root['sessionId'])
const cwd = readJsonString(source, root['cwd'])
const gitBranch = readJsonString(source, root['gitBranch'])
const promptSource = readJsonString(source, root['promptSource'])
if (timestamp !== undefined) entry.timestamp = timestamp
if (sessionId !== undefined) entry.sessionId = sessionId
if (cwd !== undefined) entry.cwd = cwd
if (gitBranch !== undefined) entry.gitBranch = gitBranch
if (promptSource !== undefined) entry.promptSource = promptSource
const addedNames = extractLargeAddedNames(source, root['attachment'])
if (addedNames.length > 0) {
;(entry as Record<string, unknown>)['attachment'] = { type: 'deferred_tools_delta', addedNames }
@ -2361,6 +2366,7 @@ async function scanProjectDirs(
// on a resumed session) and derive the agent id from the `agent-<agentId>`
// filename. A sidechain whose parent id was never captured stays standalone.
if (cachedFile.isSidechain) {
session.isSidechain = true
if (cachedFile.parentSessionId) session.parentSessionId = cachedFile.parentSessionId
session.agentId = sessionId.startsWith('agent-') ? sessionId.slice('agent-'.length) : sessionId
}
@ -3429,13 +3435,12 @@ async function parseProviderSources(
}
}
// 90-day age-out for durable providers: remove entries whose newest call is
// older than 90 days so the cache doesn't grow unboundedly. One scoped
// exemption: a source that declared retainWhilePresent and is still
// discovered IS the durable record itself (copilot's session-store.db) —
// pruning it would drop crash-only rows the file still holds and force a
// full re-read on the next refresh. Everything else — orphans, and ordinary
// still-present journal files — ages out on the pre-existing schedule.
// 90-day age-out for durable providers: prune only orphaned entries whose
// newest call is older than 90 days. Still-discovered sources remain live
// regardless of age and keep their persisted fingerprint for reuse (#992).
// retainWhilePresent (copilot's session-store.db, the durable record itself)
// states the same intent explicitly for a still-discovered source; under the
// orphan-only rule it is currently redundant rather than load-bearing.
if (!readOnly && provider.durableSources) {
const retainPaths = new Set(sources.filter(s => s.retainWhilePresent).map(s => s.path))
const cutoffMs = Date.now() - 90 * 24 * 60 * 60 * 1000
@ -3446,7 +3451,7 @@ async function parseProviderSources(
.map(c => new Date(c.timestamp).getTime())
.filter(ts => !isNaN(ts))
.reduce((max, ts) => Math.max(max, ts), 0)
if (newestTs > 0 && newestTs < cutoffMs) {
if (!allDiscoveredFiles.has(cachedPath) && newestTs > 0 && newestTs < cutoffMs) {
delete section.files[cachedPath]
markCacheDirty(diskCache, providerName, cachedPath)
}
@ -4091,6 +4096,7 @@ function carryLinkageFields(rebuilt: SessionSummary, original: SessionSummary):
if (original.prLinks?.length) rebuilt.prLinks = original.prLinks
if (original.prAttributionSource) rebuilt.prAttributionSource = original.prAttributionSource
if (original.workingDirectory) rebuilt.workingDirectory = original.workingDirectory
if (original.isSidechain) rebuilt.isSidechain = true
// prRefsAtRangeStart is NOT copied here: a narrower slice needs it recomputed at
// the new boundary (see recomputeRangeStartPrRefs), not the wide range's value.
if (original.parentSessionId) rebuilt.parentSessionId = original.parentSessionId

View file

@ -5,7 +5,16 @@ import { homedir } from 'os'
import { calculateCost } from '../models.js'
import { extractBashCommands } from '../bash-utils.js'
import { readCachedResults, writeCachedResults } from '../cursor-cache.js'
import { isSqliteAvailable, isSqliteBusyError, getSqliteLoadError, openDatabase, blobToText, type SqliteDatabase } from '../sqlite.js'
import {
isSqliteAvailable,
isSqliteBusyError,
getSqliteLoadError,
openDatabase,
blobToText,
isSqliteReadonlyError,
warnSqliteReadonlyOnce,
type SqliteDatabase,
} from '../sqlite.js'
import { estimateTokensFromChars } from '../token-estimate.js'
import type { DateRange } from '../types.js'
import type { ProbeRoot, Provider, SessionSource, SessionParser, ParsedProviderCall } from './types.js'
@ -188,7 +197,8 @@ function loadWorkspaceMap(workspaceStorageDir: string): WorkspaceMapping {
let db: SqliteDatabase
try {
db = openDatabase(wsDbPath)
} catch {
} catch (err) {
if (isSqliteReadonlyError(err)) warnSqliteReadonlyOnce(wsDbPath)
continue
}
try {

591
src/providers/dsh.ts Normal file
View file

@ -0,0 +1,591 @@
import { open, readdir, readFile, stat } from 'fs/promises'
import { join } from 'path'
import { homedir } from 'os'
import zlib from 'zlib'
import { MAX_SESSION_FILE_BYTES, readSessionFile } from '../fs-utils.js'
import { calculateCost, getShortModelName } from '../models.js'
import { extractBashCommands } from '../bash-utils.js'
import type { ProbeRoot, Provider, SessionSource, SessionParser, ParsedProviderCall } from './types.js'
// DeepSeek Harness (dsh) stores one session per directory:
// <DSH_HOME|~/.dsh>/sessions/<encoded-cwd>/session-<uuid>/session.jsonl.zstd
// (or an uncompressed session.jsonl when compression=none). The .zstd file is
// a concatenation of INDEPENDENT zstd frames — one per appended event batch —
// so node:zlib's one-shot zstdDecompressSync (which decodes a single frame)
// must be driven frame-by-frame behind a structural frame-boundary scan. The
// scan below is a port of scanZstdFrames from the official
// @deepseek-ai/dsh-session-persistence-jsonl package, which is third-party code
// under its own license - see THIRD_PARTY_NOTICES.md.
// zstd landed in node:zlib in 22.15 / 23.8; the package floor is lower, so the
// provider degrades with a notice instead of assuming the export exists.
const zstdDecompress = (zlib as { zstdDecompressSync?: (buf: Buffer, opts?: { maxOutputLength?: number }) => Buffer }).zstdDecompressSync
const ZSTD_MAGIC = 0xfd2fb528
// SESSION_FORMAT_VERSION in @deepseek-ai/dsh-session. DSH refuses to load a log
// stamped with any other version, and a bump means an event's meaning changed,
// so a foreign version is skipped rather than read with today's assumptions.
// A zstd frame's declared content size is attacker-controlled, so a few KB of
// crafted input can expand to gigabytes. Every decode is capped: no single
// frame may exceed this, and no file may decode to more than it would have been
// allowed to occupy uncompressed (MAX_SESSION_FILE_BYTES). Overflow throws, and
// the caller skips the WHOLE file rather than counting the frames it got to.
const MAX_FRAME_DECODED_BYTES = 64 * 1024 * 1024
const SESSION_FORMAT_VERSION = 0
const MIN_REASONABLE_TIMESTAMP_MS = 1_000_000_000_000
// Discovery walks every session, so a per-file notice would repeat once per
// log; each distinct message is worth saying exactly once.
const noticed = new Set<string>()
function notice(message: string): void {
if (noticed.has(message)) return
noticed.add(message)
process.stderr.write(message)
}
type ZstdFrame = { start: number; end: number }
// Locate complete frames without decompressing their blocks. An EOF inside the
// final frame (a torn append from a crashed writer) returns its start so the
// caller can ignore the tail; invalid complete structure rejects.
function scanZstdFrames(buffer: Buffer, maxFrames = Number.POSITIVE_INFINITY): { frames: ZstdFrame[]; tornStart?: number } {
const frames: ZstdFrame[] = []
let offset = 0
while (offset < buffer.length) {
const start = offset
if (buffer.length - offset < 4) return { frames, tornStart: start }
if (buffer.readUInt32LE(offset) !== ZSTD_MAGIC) {
throw new Error(`invalid zstd frame magic at byte ${offset}`)
}
offset += 4
if (offset === buffer.length) return { frames, tornStart: start }
const descriptor = buffer.readUInt8(offset)!
offset += 1
if ((descriptor & 24) !== 0) throw new Error(`reserved frame-header bit at byte ${offset - 1}`)
const contentSizeFlag = descriptor >>> 6
const singleSegment = (descriptor & 32) !== 0
const checksum = (descriptor & 4) !== 0
const dictionaryFlag = descriptor & 3
const dictionaryBytes = dictionaryFlag === 3 ? 4 : dictionaryFlag
const contentSizeBytes = contentSizeFlag === 0 ? (singleSegment ? 1 : 0) : 1 << contentSizeFlag
const remainingHeaderBytes = (singleSegment ? 0 : 1) + dictionaryBytes + contentSizeBytes
if (buffer.length - offset < remainingHeaderBytes) return { frames, tornStart: start }
offset += remainingHeaderBytes
for (;;) {
if (buffer.length - offset < 3) return { frames, tornStart: start }
const blockHeader = buffer.readUIntLE(offset, 3)
offset += 3
const lastBlock = (blockHeader & 1) !== 0
const blockType = (blockHeader >>> 1) & 3
const blockSize = blockHeader >>> 3
if (blockType === 3) throw new Error(`reserved block type at byte ${offset - 3}`)
const payloadBytes = blockType === 1 ? 1 : blockSize
if (buffer.length - offset < payloadBytes) return { frames, tornStart: start }
offset += payloadBytes
if (lastBlock) break
}
if (checksum) {
if (buffer.length - offset < 4) return { frames, tornStart: start }
offset += 4
}
frames.push({ start, end: offset })
if (frames.length === maxFrames) return { frames }
}
return { frames }
}
type DshUsage = {
inputTokens?: number
outputTokens?: number
cacheReadTokens?: number
cacheWriteTokens?: number
reasoningTokens?: number
}
type DshEvent = {
type?: string
seq?: number
time?: number
// Session header fields live at the top level of the first event.
version?: number
id?: string
cwd?: string
createdAt?: number
parentSession?: string
seedLength?: number
data?: {
turn?: number
step?: number
content?: Array<{ type?: string; text?: string }>
// `user/message` carries the message author: a real prompt is
// `{ kind: 'user' }`, agent-injected context is `{ kind: 'plugin' }`.
source?: { kind?: string }
header?: { config?: { model?: string; provider?: string } }
message?: { source?: { kind?: string; model?: string; provider?: string } }
chunk?: { type?: string; usage?: DshUsage }
usage?: DshUsage
name?: string
arguments?: string
}
}
type StepBucket = {
usage: DshUsage
// A usage report from assistant/message is the final value for its
// (turn, step) and replaces an earlier assistant/chunk sample (the two are
// adjacent reports of the same API call, per dsh-token-meter's usage
// projection). Time follows the winning report.
final: boolean
time?: number
// Model that produced this step: the reporting assistant/message's own
// `message.source` when it names one, else the most recent request/header
// config (a header can change the model mid-turn between steps).
model: string
tools: string[]
skills: string[]
bashCommands: string[]
}
const toolNameMap: Record<string, string> = {
bash: 'Bash',
pwsh: 'Bash',
read: 'Read',
write: 'Write',
edit: 'Edit',
str_replace_editor: 'Edit',
glob: 'Glob',
grep: 'Grep',
todo_write: 'TodoWrite',
todo: 'TodoWrite',
web_search: 'WebSearch',
skill: 'Skill',
agent: 'Agent',
ask_user_question: 'AskUserQuestion',
}
function mapToolName(raw: string): string {
return toolNameMap[raw] ?? raw
}
// Usage fields are whatever the JSON held. A string or array would flow
// straight into the global token totals and the persisted cache, where
// `0 + [1, 2]` silently becomes "01,2". Same semantics as copilot.ts.
function numberOrZero(raw: unknown): number {
return typeof raw === 'number' && Number.isFinite(raw) && raw > 0 ? raw : 0
}
// A log stamped with a version this parser was not written against is skipped
// whole: a bump means an event's meaning changed, so reading it with today's
// assumptions would report confident wrong numbers.
function isReadableVersion(header: DshEvent): boolean {
if (header.version === SESSION_FORMAT_VERSION) return true
// Keyed on the version, not the path: a DSH upgrade makes EVERY session
// unreadable at once, and one line per session log is noise, not a report.
notice(`codeburn: skipping DSH sessions written in session format version ${String(header.version)}; upgrade codeburn.\n`)
return false
}
// DSH writes epoch milliseconds; promote a seconds-resolution value and reject
// what stays implausible, matching the guard cline-cli.ts uses on the hazard.
function isoTimestamp(value: number | undefined, fallback: string): string {
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) return fallback
const ms = value < MIN_REASONABLE_TIMESTAMP_MS ? value * 1000 : value
const date = new Date(ms)
if (Number.isNaN(date.getTime()) || date.getTime() < MIN_REASONABLE_TIMESTAMP_MS) return fallback
return date.toISOString()
}
function getDshHome(override?: string): string {
// An empty-string DSH_HOME is treated as unset.
return override ?? (process.env['DSH_HOME'] || undefined) ?? join(homedir(), '.dsh')
}
// DSH writes native-platform paths into the header (backslashes on Windows);
// split on both separators so discovery is correct on any host.
function projectFromCwd(cwd: string, fallback: string): string {
const segments = cwd.split(/[\\/]/).filter(Boolean)
return segments[segments.length - 1] ?? fallback
}
// Decode every complete frame and yield its JSONL lines. A torn final frame is
// ignored; a structurally corrupt file, or one that decodes past `budget`,
// throws for the caller to report. Exported for the decode-budget test.
export function* readZstdLines(
buffer: Buffer,
maxFrames = Number.POSITIVE_INFINITY,
budget = MAX_SESSION_FILE_BYTES,
): Generator<string> {
const { frames } = scanZstdFrames(buffer, maxFrames)
let remaining = budget
for (const frame of frames) {
if (remaining <= 0) throw new Error(`decodes past the ${budget}-byte cap`)
// node throws ERR_BUFFER_TOO_LARGE without allocating past the cap, so the
// per-frame limit doubles as the running budget for the frames after it.
const decoded = zstdDecompress!(buffer.subarray(frame.start, frame.end), {
maxOutputLength: Math.min(remaining, MAX_FRAME_DECODED_BYTES),
})
remaining -= decoded.length
for (const line of decoded.toString('utf-8').split('\n')) {
if (line.trim()) yield line
}
}
}
async function readEventLines(filePath: string): Promise<string[] | null> {
if (filePath.endsWith('.zstd')) {
if (!zstdDecompress) {
notice('codeburn: DSH sessions need Node >= 22.15 (zstd support); skipping DSH usage.\n')
return null
}
let buffer: Buffer
try {
// The whole log is buffered to scan its frames, so it needs the same
// oversize guard readSessionFile applies to the uncompressed variant.
const size = (await stat(filePath)).size
if (size > MAX_SESSION_FILE_BYTES) {
notice(`codeburn: skipped oversize DSH session log ${filePath} (${size} bytes)\n`)
return null
}
buffer = await readFile(filePath)
} catch {
return null
}
try {
return [...readZstdLines(buffer)]
} catch (err) {
notice(`codeburn: skipped corrupt DSH session log ${filePath}: ${err instanceof Error ? err.message : err}\n`)
return null
}
}
const content = await readSessionFile(filePath)
if (content === null) return null
return content.split('\n').filter(l => l.trim())
}
// Cheap discovery probe: decompress ONLY the first frame (the session header
// batch) instead of the whole log. The header frame is tiny, so a bounded head
// read almost always contains it; fall back to a full read when it does not.
async function readSessionHeader(filePath: string): Promise<DshEvent | null> {
const firstLine = async (): Promise<string | null> => {
if (filePath.endsWith('.zstd')) {
if (!zstdDecompress) return null
let head: Buffer
try {
const handle = await open(filePath, 'r')
try {
const size = (await handle.stat()).size
const length = Math.min(size, 256 * 1024)
head = Buffer.alloc(length)
await handle.read(head, 0, length, 0)
} finally {
await handle.close()
}
} catch {
return null
}
let { frames } = scanZstdFrames(head, 1)
if (frames.length === 0) {
// Head read did not cover one full frame; take the whole file. A fork's
// first batch carries the whole inherited seed, so this is reachable on
// a real log and needs the same oversize guard as the parse read.
try {
if ((await stat(filePath)).size > MAX_SESSION_FILE_BYTES) return null
const full = await readFile(filePath)
frames = scanZstdFrames(full, 1).frames
if (frames.length === 0) return null
head = full
} catch {
return null
}
}
const text = zstdDecompress(head.subarray(frames[0]!.start, frames[0]!.end), {
maxOutputLength: MAX_FRAME_DECODED_BYTES,
}).toString('utf-8')
return text.split('\n').find(l => l.trim()) ?? null
}
const content = await readSessionFile(filePath)
return content?.split('\n').find(l => l.trim()) ?? null
}
try {
const line = await firstLine()
if (!line) return null
const event = JSON.parse(line) as DshEvent
if (event.type !== 'session') return null
return isReadableVersion(event) ? event : null
} catch {
return null
}
}
async function discoverSessionsInDir(sessionsDir: string): Promise<SessionSource[]> {
const sources: SessionSource[] = []
let projectDirs: string[]
try {
projectDirs = await readdir(sessionsDir)
} catch {
return sources
}
for (const dirName of projectDirs) {
const dirPath = join(sessionsDir, dirName)
const dirStat = await stat(dirPath).catch(() => null)
if (!dirStat?.isDirectory()) continue
let sessionDirs: string[]
try {
sessionDirs = await readdir(dirPath)
} catch {
continue
}
for (const sessionDir of sessionDirs) {
const sessionPath = join(dirPath, sessionDir)
const sessionStat = await stat(sessionPath).catch(() => null)
if (!sessionStat?.isDirectory()) continue
// Compressed log first; the uncompressed variant exists when
// compression=none. Never both for the same session.
let filePath: string | null = null
for (const name of ['session.jsonl.zstd', 'session.jsonl']) {
const candidate = join(sessionPath, name)
const fileStat = await stat(candidate).catch(() => null)
if (fileStat?.isFile()) {
filePath = candidate
break
}
}
if (!filePath) continue
const header = await readSessionHeader(filePath)
if (!header) continue
const cwd = typeof header.cwd === 'string' && header.cwd.trim() ? header.cwd : dirName
sources.push({ path: filePath, project: projectFromCwd(cwd, dirName), provider: 'dsh' })
}
}
return sources
}
function parseToolArguments(raw: string | undefined): Record<string, unknown> | null {
if (!raw) return null
try {
const parsed = JSON.parse(raw) as unknown
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed as Record<string, unknown> : null
} catch {
return null
}
}
function createParser(source: SessionSource, seenKeys: Set<string>): SessionParser {
return {
async *parse(): AsyncGenerator<ParsedProviderCall> {
const lines = await readEventLines(source.path)
if (!lines) return
let sessionId = ''
let cwd = ''
let model = 'unknown'
let currentTurn = 0
let sessionStart = ''
// Events a forked session inherited from its parent. They are a verbatim
// copy of the parent's log, which codeburn parses as its own session, so
// counting them here would bill the same calls twice.
let seedLength = 0
const userMessageByTurn = new Map<number, string>()
const buckets = new Map<string, StepBucket>()
for (const line of lines) {
let event: DshEvent
try {
event = JSON.parse(line) as DshEvent
} catch {
continue
}
if (event.type === 'session') {
if (!isReadableVersion(event)) return
sessionId = event.id ?? sessionId
cwd = event.cwd ?? cwd
sessionStart = isoTimestamp(event.createdAt, sessionStart)
if (typeof event.parentSession === 'string' && event.parentSession && typeof event.seedLength === 'number') {
seedLength = event.seedLength
}
continue
}
if (typeof event.seq === 'number' && event.seq < seedLength) continue
if (event.type === 'turn/start') {
currentTurn = event.data?.turn ?? currentTurn
continue
}
if (event.type === 'request/header') {
// Emitted at most once per request; steps after the last header
// inherit its config as their model.
const headerModel = event.data?.header?.config?.model
if (typeof headerModel === 'string' && headerModel) model = headerModel
continue
}
if (event.type === 'user/message') {
// Plugin-injected context (runtime snapshots, skill bodies, file-change
// notices) rides the same event type as a typed prompt; only the latter
// is a useful preview.
if (event.data?.source?.kind !== 'user') continue
if (userMessageByTurn.has(currentTurn)) continue
const texts = (event.data?.content ?? [])
.filter(c => c.type === 'text' && typeof c.text === 'string' && c.text)
.map(c => c.text!)
if (texts.length > 0) userMessageByTurn.set(currentTurn, texts.join(' ').slice(0, 500))
continue
}
if (event.type === 'tool/call') {
const turn = event.data?.turn ?? currentTurn
const step = event.data?.step ?? 0
const rawName = event.data?.name
if (!rawName) continue
const key = `${turn}:${step}`
let bucket = buckets.get(key)
if (!bucket) {
bucket = { usage: {}, final: false, model, tools: [], skills: [], bashCommands: [] }
buckets.set(key, bucket)
}
bucket.tools.push(mapToolName(rawName))
const args = parseToolArguments(event.data?.arguments)
if ((rawName === 'bash' || rawName === 'pwsh') && typeof args?.['command'] === 'string') {
bucket.bashCommands.push(...extractBashCommands(args['command']))
}
if (rawName === 'skill' && typeof args?.['name'] === 'string') {
bucket.skills.push(args['name'])
}
continue
}
let usage: DshUsage | undefined
let isFinal = false
// The model that actually served the call, when the message records it.
// request/header only describes the request codeburn is about to see.
let reportedModel = model
if (event.type === 'assistant/chunk' && event.data?.chunk?.type === 'usage') {
usage = event.data.chunk.usage
} else if (event.type === 'assistant/message' && event.data?.usage) {
usage = event.data.usage
isFinal = true
const messageModel = event.data.message?.source?.model
if (typeof messageModel === 'string' && messageModel) reportedModel = messageModel
} else {
continue
}
if (!usage) continue
const turn = event.data?.turn ?? currentTurn
const step = event.data?.step ?? 0
const key = `${turn}:${step}`
let bucket = buckets.get(key)
if (!bucket) {
bucket = { usage: {}, final: false, model, tools: [], skills: [], bashCommands: [] }
buckets.set(key, bucket)
}
// A final report replaces an earlier sample; a late sample never
// overwrites a final one. The model snapshot follows the winning
// report (a header can change the model mid-turn between steps).
if (isFinal || !bucket.final) {
bucket.usage = usage
bucket.final = isFinal
bucket.time = event.time
bucket.model = reportedModel
}
}
const sortedKeys = [...buckets.keys()].sort((a, b) => {
const [ta, sa] = a.split(':').map(Number)
const [tb, sb] = b.split(':').map(Number)
return ta! - tb! || sa! - sb!
})
for (const key of sortedKeys) {
const bucket = buckets.get(key)!
const input = numberOrZero(bucket.usage.inputTokens)
const output = numberOrZero(bucket.usage.outputTokens)
const cacheRead = numberOrZero(bucket.usage.cacheReadTokens)
const cacheWrite = numberOrZero(bucket.usage.cacheWriteTokens)
const reasoning = numberOrZero(bucket.usage.reasoningTokens)
if (input + output + cacheRead + cacheWrite + reasoning === 0) continue
const dedupKey = `dsh:${sessionId || source.path}:${key}`
if (seenKeys.has(dedupKey)) continue
seenKeys.add(dedupKey)
// DSH bills reasoning tokens at the output rate (same as Gemini).
const costUSD = calculateCost(bucket.model, input, output + reasoning, cacheWrite, cacheRead, 0)
const [turn] = key.split(':').map(Number)
yield {
provider: 'dsh',
model: bucket.model,
inputTokens: input,
outputTokens: output,
cacheCreationInputTokens: cacheWrite,
cacheReadInputTokens: cacheRead,
cachedInputTokens: cacheRead,
reasoningTokens: reasoning,
webSearchRequests: 0,
costUSD,
tools: [...new Set(bucket.tools)],
bashCommands: bucket.bashCommands,
skills: bucket.skills.length > 0 ? [...new Set(bucket.skills)] : undefined,
timestamp: isoTimestamp(bucket.time, sessionStart),
speed: 'standard',
deduplicationKey: dedupKey,
userMessage: userMessageByTurn.get(turn!) ?? '',
sessionId: sessionId || source.path,
project: cwd ? projectFromCwd(cwd, source.project) : source.project,
projectPath: cwd || undefined,
workingDirectory: cwd || undefined,
}
}
},
}
}
export function createDshProvider(dshHomeOverride?: string): Provider {
const dshHome = getDshHome(dshHomeOverride)
const sessionsDir = join(dshHome, 'sessions')
return {
name: 'dsh',
displayName: 'DeepSeek Harness',
modelDisplayName(model: string): string {
return getShortModelName(model)
},
toolDisplayName(rawTool: string): string {
return mapToolName(rawTool)
},
async probeRoots(): Promise<ProbeRoot[]> {
return [{ path: sessionsDir, label: 'sessions' }]
},
async discoverSessions(): Promise<SessionSource[]> {
return discoverSessionsInDir(sessionsDir)
},
createSessionParser(source: SessionSource, seenKeys: Set<string>): SessionParser {
return createParser(source, seenKeys)
},
}
}
export const dsh = createDshProvider()

View file

@ -3,7 +3,7 @@ import { basename, dirname, join } from 'path'
import { homedir } from 'os'
import { readSessionFile } from '../fs-utils.js'
import { calculateCost, getShortModelName } from '../models.js'
import { calculateCost, getModelCosts, getShortModelName } from '../models.js'
import { extractBashCommands } from '../bash-utils.js'
import type { ProbeRoot, Provider, SessionSource, SessionParser, ParsedProviderCall } from './types.js'
@ -12,14 +12,15 @@ import type { ProbeRoot, Provider, SessionSource, SessionParser, ParsedProviderC
// or ~/.grok. Each session dir holds summary.json, signals.json, and the ACP
// log updates.jsonl.
//
// Grok does NOT record billable input/output tokens. signals.json carries
// `contextTokensUsed` (current context fill) and updates.jsonl carries a running
// `_meta.totalTokens` per streamed chunk; there is no per-call input/output
// split. We reconstruct an ESTIMATE from the per-turn totalTokens curve. Agentic
// turns re-send the growing context every call, and that re-sent context is
// cached server-side, so we bill the unique context (summed per compaction segment) as fresh input,
// the re-sent remainder as cache reads, and the per-turn growth as output. Cost
// is flagged estimated; grok-build is priced via its grok-build-0.1 alias.
// Newer Grok CLI versions append a `turn_completed` update with provider-recorded
// input/output/cache/reasoning usage. That record is authoritative: cached reads
// are part of input, and reasoning is a subset of output. Cache creation is
// treated as another input subset by analogy because the record exposes no
// separate fresh-input field; any per-record violation is clamped before pricing.
// Older sessions only carry
// `signals.json.contextTokensUsed` and the running `_meta.totalTokens` curve; for
// those we retain the old compaction-aware estimate and mark its cost estimated.
// `costUsdTicks` is deliberately ignored because its scale is not documented.
const toolNameMap: Record<string, string> = {
bash: 'Bash',
@ -80,28 +81,136 @@ function safeDecode(name: string): string {
}
}
// updates.jsonl is one ACP JSON-RPC notification per line; streamed chunks carry
// params._meta.{totalTokens, promptId}. totalTokens is the running context size,
// so grouping by promptId (one per turn) gives each turn's first/last value.
// updates.jsonl is one ACP JSON-RPC notification per line. Streamed chunks carry
// params._meta.{totalTokens, promptId}; completed turns carry snake_case
// params.update.{prompt_id, usage}.
type GrokUpdate = {
params?: {
_meta?: { totalTokens?: number; promptId?: string }
update?: { sessionUpdate?: string; title?: string; rawInput?: { command?: unknown; subagent_type?: unknown } }
_meta?: { totalTokens?: unknown; promptId?: unknown }
update?: {
sessionUpdate?: unknown
prompt_id?: unknown
usage?: unknown
title?: unknown
rawInput?: { command?: unknown; subagent_type?: unknown }
}
}
}
// Single pass over updates.jsonl: per-turn totalTokens for the cost estimate,
// plus the real tool calls (each tool_call's title -> a tool, and
// run_terminal_command's rawInput.command -> shell commands).
function parseUpdates(updates: string): {
type GrokUsageValues = {
inputTokens: number
outputTokens: number
cacheReadTokens: number
cacheCreationTokens: number
reasoningTokens: number
}
type GrokAuthoritativeUsage = GrokUsageValues & {
modelUsage: Map<string, GrokUsageValues>
}
type GrokTokenTotals = {
input: number
cacheRead: number
output: number
cacheCreation: number
reasoning: number
}
function emptyTokenTotals(): GrokTokenTotals {
return { input: 0, cacheRead: 0, output: 0, cacheCreation: 0, reasoning: 0 }
}
const authoritativeTokenFields = [
'inputTokens',
'outputTokens',
'cachedReadTokens',
'cacheCreationTokens',
'reasoningTokens',
] as const
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
// JSONL is third-party input. Keep the check local to this provider so bad
// usage fields become absent rather than leaking NaN, negative tokens, or a
// throwing arithmetic operation into the session aggregate.
function finiteNonNegative(value: unknown): number | undefined {
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) return undefined
// Token counts this large are not meaningful in a session and can overflow
// when summed or priced. Capping preserves the non-negative finite invariant.
return Math.min(value, Number.MAX_SAFE_INTEGER)
}
function addTokenCounts(left: number, right: number): number {
return Math.min(Number.MAX_SAFE_INTEGER, left + right)
}
function readUsageNumber(usage: Record<string, unknown>, field: string): number | undefined {
return finiteNonNegative(usage[field])
}
function readModelUsage(usage: Record<string, unknown>): Map<string, GrokUsageValues> {
const modelUsage = usage['modelUsage']
const result = new Map<string, GrokUsageValues>()
if (!isRecord(modelUsage)) return result
for (const [modelId, rawModelUsage] of Object.entries(modelUsage)) {
if (!modelId || !isRecord(rawModelUsage)) continue
const values = authoritativeTokenFields.map((field) => finiteNonNegative(rawModelUsage[field]))
if (!values.some((value) => value !== undefined)) continue
result.set(modelId, {
inputTokens: values[0] ?? 0,
outputTokens: values[1] ?? 0,
cacheReadTokens: values[2] ?? 0,
cacheCreationTokens: values[3] ?? 0,
reasoningTokens: values[4] ?? 0,
})
}
return result
}
function parseAuthoritativeUsage(raw: unknown): GrokAuthoritativeUsage | null {
if (!isRecord(raw)) return null
const values = authoritativeTokenFields.map((field) => readUsageNumber(raw, field))
const modelUsage = readModelUsage(raw)
return {
inputTokens: values[0] ?? 0,
outputTokens: values[1] ?? 0,
cacheReadTokens: values[2] ?? 0,
cacheCreationTokens: values[3] ?? 0,
reasoningTokens: values[4] ?? 0,
modelUsage,
}
}
function chooseAuthoritativeModel(modelIds: string[], existingModel: string): string {
// modelUsage is the best attribution signal, but it may contain a newer
// provider id that this checkout cannot price yet (for example
// `grok-4.6-build`). Prefer an actual model id when it prices; otherwise keep
// the existing summary/signals id when that one prices, avoiding a truthful
// but $0 row. If neither prices, retain the actual model id for attribution.
const pricedActualModel = modelIds.find((modelId) => getModelCosts(modelId) !== null)
if (pricedActualModel) return pricedActualModel
if (getModelCosts(existingModel) !== null) return existingModel
return modelIds[0] ?? existingModel
}
// Single pass over updates.jsonl: retain the old per-turn totalTokens estimate,
// the deduplicated authoritative turn records, and the real tool calls.
function parseUpdates(updates: string): {
usage: GrokTokenTotals
modelIds: string[]
authoritative: boolean
hasUncompletedTurn: boolean
tools: string[]
bashCommands: string[]
subagentTypes: string[]
} {
const turns = new Map<string, { first: number; last: number }>()
const completedUsages = new Map<string, GrokAuthoritativeUsage>()
const tools: string[] = []
const bashCommands: string[] = []
const subagentTypes: string[] = []
@ -111,6 +220,7 @@ function parseUpdates(updates: string): {
let prevTotal = -1
let segmentPeak = 0
let inputFresh = 0
let completedWithoutPromptId = 0
for (const line of updates.split('\n')) {
if (!line.trim()) continue
@ -122,16 +232,16 @@ function parseUpdates(updates: string): {
}
if (!params) continue
const total = params._meta?.totalTokens
if (typeof total === 'number') {
const total = finiteNonNegative(params._meta?.totalTokens)
if (total !== undefined) {
if (prevTotal >= 0 && total < prevTotal * 0.5) {
inputFresh += segmentPeak // close the segment a compaction just ended
inputFresh = addTokenCounts(inputFresh, segmentPeak) // close the segment a compaction just ended
segmentPeak = 0
}
if (total > segmentPeak) segmentPeak = total
prevTotal = total
const promptId = params._meta?.promptId
const promptId = typeof params._meta?.promptId === 'string' ? params._meta.promptId : undefined
if (promptId) {
const turn = turns.get(promptId)
if (!turn) turns.set(promptId, { first: total, last: total })
@ -140,6 +250,18 @@ function parseUpdates(updates: string): {
}
const update = params.update
if (update?.sessionUpdate === 'turn_completed') {
const usage = parseAuthoritativeUsage(update.usage)
if (usage) {
const promptId = typeof update.prompt_id === 'string' && update.prompt_id.length > 0
? update.prompt_id
: `turn_completed:${completedWithoutPromptId++}`
// Re-emitted turn_completed notifications are cumulative updates for
// the same turn. Last write wins so they cannot double count.
completedUsages.set(promptId, usage)
}
}
if (update?.sessionUpdate === 'tool_call' && typeof update.title === 'string') {
tools.push(toolNameMap[update.title] ?? update.title)
if (update.title === 'run_terminal_command' && typeof update.rawInput?.command === 'string') {
@ -151,17 +273,101 @@ function parseUpdates(updates: string): {
}
}
inputFresh += segmentPeak // close the final segment
inputFresh = addTokenCounts(inputFresh, segmentPeak) // close the final segment
let sumFirst = 0
let output = 0
for (const { first, last } of turns.values()) {
sumFirst += first
output += Math.max(0, last - first)
sumFirst = addTokenCounts(sumFirst, first)
output = addTokenCounts(output, Math.max(0, last - first))
}
// Fresh input (summed segment peaks) is billed once; the rest of the per-turn
// re-sends are cache reads (Grok caches them, even though it reports nothing).
const cacheRead = Math.max(0, sumFirst - inputFresh)
return { input: inputFresh, cacheRead, output, tools, bashCommands, subagentTypes }
const estimated = {
input: inputFresh,
cacheRead: Math.max(0, sumFirst - inputFresh),
output,
}
const usageTotals = emptyTokenTotals()
const modelIds: string[] = []
const seenModelIds = new Set<string>()
for (const usage of completedUsages.values()) {
addUsageToTotals(usageTotals, usage)
for (const modelId of usage.modelUsage.keys()) {
if (seenModelIds.has(modelId)) continue
seenModelIds.add(modelId)
modelIds.push(modelId)
}
}
// Decide from the final, prompt-deduplicated records. A positive modelUsage
// entry is attribution metadata only; it is not a substitute for the
// top-level accounting fields. This also prevents a superseded positive
// record from suppressing the streaming fallback.
const hasPositiveCompletedUsage = [...completedUsages.values()].some(hasPositiveTopLevelUsage)
if (!hasPositiveCompletedUsage || !hasPositiveTotals(usageTotals)) {
// Older Grok CLI versions have no completed usage record. Keep the
// heuristic only here; blending it into a real record would reintroduce the
// large over-count this parser is fixing. A record that only has modelUsage,
// or whose final deduplicated values are empty, is treated the same way.
return {
usage: { input: estimated.input, cacheRead: estimated.cacheRead, output: estimated.output, cacheCreation: 0, reasoning: 0 },
modelIds: [],
authoritative: false,
hasUncompletedTurn: false,
tools,
bashCommands,
subagentTypes,
}
}
const hasUncompletedTurn = [...turns.keys()].some(promptId => !completedUsages.has(promptId))
// calculateCost follows the cache-exclusive input convention used by the
// other real-usage providers. Each record is decomposed before its totals
// are added, so an inconsistent record cannot consume another record's fresh
// input budget.
return {
usage: usageTotals,
modelIds,
authoritative: true,
hasUncompletedTurn,
tools,
bashCommands,
subagentTypes,
}
}
function hasPositiveTopLevelUsage(usage: GrokAuthoritativeUsage): boolean {
return usage.inputTokens > 0
|| usage.outputTokens > 0
|| usage.cacheReadTokens > 0
|| usage.cacheCreationTokens > 0
|| usage.reasoningTokens > 0
}
function addUsageToTotals(totals: GrokTokenTotals, usage: GrokUsageValues): void {
// `cacheCreationTokens` is treated as an input subset by analogy. Clamp the
// exclusive portion per record before summing the session. Reasoning is
// reported inside outputTokens by Grok, so clamp it to that same record's
// output before the session totals are accumulated.
const reasoningTokens = Math.min(usage.reasoningTokens, usage.outputTokens)
totals.input = addTokenCounts(
totals.input,
Math.max(0, usage.inputTokens - usage.cacheReadTokens - usage.cacheCreationTokens),
)
totals.cacheRead = addTokenCounts(totals.cacheRead, usage.cacheReadTokens)
totals.output = addTokenCounts(totals.output, usage.outputTokens)
totals.cacheCreation = addTokenCounts(totals.cacheCreation, usage.cacheCreationTokens)
totals.reasoning = addTokenCounts(totals.reasoning, reasoningTokens)
}
function hasPositiveTotals(totals: GrokTokenTotals): boolean {
return totals.input > 0
|| totals.cacheRead > 0
|| totals.output > 0
|| totals.cacheCreation > 0
|| totals.reasoning > 0
}
function createParser(source: SessionSource, seenKeys: Set<string>): SessionParser {
@ -172,37 +378,64 @@ function createParser(source: SessionSource, seenKeys: Set<string>): SessionPars
const updates = await readSessionFile(source.path)
if (!summary || updates === null) return
const { input, cacheRead, output, tools, bashCommands, subagentTypes } = parseUpdates(updates)
if (input === 0 && output === 0) return
const signals = await readJson<GrokSignals>(join(dir, 'signals.json'))
const model =
const existingModel =
summary.current_model_id ?? signals?.primaryModelId ?? signals?.modelsUsed?.[0] ?? 'grok-build'
const parsed = parseUpdates(updates)
if (!hasPositiveTotals(parsed.usage)) return
const timestamp = summary.updated_at ?? summary.last_active_at ?? summary.created_at ?? ''
const sessionId = summary.info?.id ?? basename(dir)
const dedupKey = `${source.provider}:${dir}:${timestamp}:${sessionId}`
if (seenKeys.has(dedupKey)) return
seenKeys.add(dedupKey)
// Multi-model attribution is deliberately out of scope: modelUsage may
// help choose a priced attribution id, but top-level totals remain the
// accounting source and one session uses one model's rate.
const model = parsed.authoritative ? chooseAuthoritativeModel(parsed.modelIds, existingModel) : existingModel
const baseDedupKey = `${source.provider}:${dir}:${timestamp}:${sessionId}`
if (seenKeys.has(baseDedupKey)) return
seenKeys.add(baseDedupKey)
// `addUsageToTotals` clamps reasoning per authoritative record before
// summing, so the aggregate preserves this identity as well.
const reasoningTokens = parsed.usage.reasoning
yield {
provider: source.provider,
model,
inputTokens: input,
outputTokens: output,
cacheCreationInputTokens: 0,
cacheReadInputTokens: cacheRead,
cachedInputTokens: cacheRead,
reasoningTokens: 0,
inputTokens: parsed.usage.input,
// Grok reports reasoning INSIDE outputTokens, but the repo contract is
// the opposite: ParsedProviderCall.reasoningTokens is exclusive of
// outputTokens, and every consumer sums the two (parser.ts's
// cachedCallToApiCall for cost, modelBreakdown for tokens, and the
// models/audit reports). tests/providers/kiro.test.ts states it
// outright. So split it here rather than special-casing grok in five
// downstream places: subtracting reasoning makes `output + reasoning`
// reconstruct exactly the number Grok reported.
outputTokens: parsed.usage.output - reasoningTokens,
cacheCreationInputTokens: parsed.usage.cacheCreation,
cacheReadInputTokens: parsed.usage.cacheRead,
cachedInputTokens: parsed.usage.cacheRead,
reasoningTokens,
webSearchRequests: 0,
costUSD: calculateCost(model, input, output, 0, cacheRead, 0),
costIsEstimated: true,
tools,
bashCommands,
subagentTypes,
// Authoritative token counts are measured even though CodeBurn applies
// its own pricing table; only the legacy context-curve path is an
// estimate. The full provider output is priced once here, which is
// what the downstream `output + reasoning` recompute reproduces.
costUSD: calculateCost(
model,
parsed.usage.input,
parsed.usage.output,
parsed.usage.cacheCreation,
parsed.usage.cacheRead,
0,
),
costIsEstimated: !parsed.authoritative || parsed.hasUncompletedTurn,
tools: parsed.tools,
bashCommands: parsed.bashCommands,
subagentTypes: parsed.subagentTypes,
timestamp,
speed: 'standard',
deduplicationKey: dedupKey,
deduplicationKey: baseDedupKey,
userMessage: summary.session_summary ?? summary.generated_title ?? '',
sessionId,
project: source.project,

View file

@ -7,6 +7,7 @@ import { codex } from './codex.js'
import { copilot } from './copilot.js'
import { droid } from './droid.js'
import { devin } from './devin.js'
import { dsh } from './dsh.js'
import { gemini } from './gemini.js'
import { hermes } from './hermes.js'
import { ibmBob } from './ibm-bob.js'
@ -192,7 +193,7 @@ async function loadZed(): Promise<Provider | null> {
}
}
const coreProviders: Provider[] = [claude, cline, clineCli, codewhale, codebuff, codex, copilot, devin, droid, gemini, hermes, ibmBob, kiloCode, kiro, kimi, kimicode, lingtaiTui, mistralVibe, mux, openclaw, openclaude, openDesign, pi, omp, qwen, quickdesk, rooCode, zerostack, grok]
const coreProviders: Provider[] = [claude, cline, clineCli, codewhale, codebuff, codex, copilot, devin, droid, dsh, gemini, hermes, ibmBob, kiloCode, kiro, kimi, kimicode, lingtaiTui, mistralVibe, mux, openclaw, openclaude, openDesign, pi, omp, qwen, quickdesk, rooCode, zerostack, grok]
// Lazily loaded providers, listed by name so --provider validation works even
// when an optional module fails to load. Must stay in sync with getAllProviders.

View file

@ -2,7 +2,16 @@ import { readdir } from 'fs/promises'
import { join } from 'path'
import { calculateCost } from '../models.js'
import { isSqliteAvailable, getSqliteLoadError, openDatabase, blobToText, isSqliteBusyError, type SqliteDatabase } from '../sqlite.js'
import {
isSqliteAvailable,
getSqliteLoadError,
openDatabase,
blobToText,
isSqliteBusyError,
isSqliteReadonlyError,
warnSqliteReadonlyOnce,
type SqliteDatabase,
} from '../sqlite.js'
import { buildAssistantCall, parseTimestamp, sanitize, type MessageData, type PartData } from './session-message.js'
import type {
SessionSource,
@ -310,7 +319,8 @@ export async function discoverSqliteSessions(
let db: SqliteDatabase
try {
db = openDatabase(dbPath)
} catch {
} catch (err) {
if (isSqliteReadonlyError(err)) warnSqliteReadonlyOnce(dbPath)
continue
}

View file

@ -105,7 +105,7 @@ export type CachedFile = {
// is re-parsed only when the file changes (fingerprint differs). Carries no
// turns, so it contributes no usage. (issue #441 follow-up)
failed?: boolean
// Rich-session-capture, Claude session-level (capture-only; no report yet).
// Rich-session-capture, Claude session-level.
// `title` is the LAST `ai-title` entry's text; `prLinks` accumulates every
// `pr-link` entry's URL. `isSidechain` is true when any entry is a sidechain:
// parentUuid references an intra-file entry uuid, not another session id, so it
@ -230,6 +230,7 @@ export const PROVIDER_ENV_VARS: Record<string, string[]> = {
hermes: ['HERMES_HOME'],
'lingtai-tui': ['LINGTAI_HOME', 'LINGTAI_TUI_HOME', 'LINGTAI_TUI_GLOBAL_DIR'],
droid: ['FACTORY_DIR'],
dsh: ['DSH_HOME'],
cursor: ['CODEBURN_CURSOR_MAX_BUBBLES'],
// XDG_DATA_HOME is stale here (cursor-agent never reads it) but deliberately
// kept: removing it would force a re-parse to fix nothing.
@ -304,7 +305,14 @@ export const PROVIDER_PARSE_VERSIONS: Record<string, string> = {
// they still contribute. v2 (over the never-released v1): store dedup keys
// grew a content discriminator so a same-path DB reset cannot alias rows.
copilot: 'cli-shutdown-cost-v1-skills-source-provenance-v1-session-store-v2',
grok: 'estimated-cost-v1',
// authoritative-usage-v4: persist one Grok session call from top-level
// authoritative totals, use modelUsage only for priced attribution, clamp
// reasoning per record, and label mixed sessions estimated.
grok: 'authoritative-usage-v4',
// seed-aware-v1: the parser now skips the parent events a forked session
// replays (double-counted before), takes the model from the reporting
// assistant/message, and keeps agent-injected context out of the preview.
dsh: 'seed-aware-v1',
hermes: 'reasoning-output-accounting-v1-est-cost',
'lingtai-tui': 'token-ledger-registry-activity-v3',
'ibm-bob': 'worktree-project-grouping-v1',
@ -807,8 +815,15 @@ async function loadShard(path: string): Promise<Record<string, CachedFile> | nul
* full: the first because its cache is the only surviving record of pruned
* usage, the second because a fingerprint change discards the whole section and
* must see every entry it is discarding.
*
* `CODEBURN_CACHE_SCOPE=all` is the escape hatch: it drops the scope here, at
* the one place every caller routes through, so a suspect scoped read can be
* compared against a full one without a rebuild. It is a READ policy and
* deliberately not part of any env fingerprint (PROVIDER_ENV_VARS) setting or
* unsetting it must never invalidate a cache, only change how much of it is read.
*/
export async function loadCache(scope?: CacheLoadScope): Promise<SessionCache> {
if (process.env['CODEBURN_CACHE_SCOPE'] === 'all') scope = undefined
const dir = sessionCacheDir()
const envelope = await readEnvelope(dir)
if (!envelope) return afterMissingShardCache()
@ -1056,6 +1071,15 @@ export async function saveCache(cache: SessionCache, verifyStillOwner?: () => Pr
const files = plan.groups.get(bucket)!
const onDisk = from ? await loadShard(join(dir, from)) : null
if (!onDisk) return writeShard(provider, bucket, files)
// A file whose month this run never loaded has no visible cache entry, so it
// looks uncached and is re-parsed into the same bucket — re-deriving the
// entry the shard already holds. Republishing then churns the shard's nonce
// name on every run for content that never changed (#1032), so a merge that
// neither adds, changes nor removes an entry keeps the published shard.
const adds = Object.entries(files).some(([path, file]) =>
onDisk[path] === undefined || JSON.stringify(onDisk[path]) !== JSON.stringify(file))
const removes = [...plan.moved].some(path => onDisk[path] !== undefined && files[path] === undefined)
if (!adds && !removes) return { name: from!, until: untilMonth(onDisk) }
for (const path of plan.moved) delete onDisk[path]
return writeShard(provider, bucket, { ...onDisk, ...files })
}

34
src/session-population.ts Normal file
View file

@ -0,0 +1,34 @@
import type { ProjectSummary, SessionSummary } from './types.js'
/**
* Sidechains are real usage, but they are not user-started work sessions.
* Behavioral consumers should use this predicate or the projected project
* view below; accounting and configuration consumers should use the originals.
*/
export function isUserStartedSession(session: SessionSummary): boolean {
return session.isSidechain !== true
}
export function withUserStartedSessions(project: ProjectSummary): ProjectSummary {
const sessions = project.sessions.filter(isUserStartedSession)
if (sessions.length === project.sessions.length) return project
const totalCostUSD = sessions.reduce((sum, session) => sum + session.totalCostUSD, 0)
return {
...project,
sessions,
totalCostUSD,
totalSavingsUSD: sessions.reduce((sum, session) => sum + session.totalSavingsUSD, 0),
totalEstimatedCostUSD: project.totalEstimatedCostUSD === undefined
? undefined
: sessions.reduce((sum, session) => sum + (session.totalEstimatedCostUSD ?? 0), 0),
totalApiCalls: sessions.reduce((sum, session) => sum + session.apiCalls, 0),
// Proxy coverage is project-scoped and applies to every retained session
// whenever it applies to the source project.
totalProxiedCostUSD: project.totalProxiedCostUSD > 0 ? totalCostUSD : 0,
}
}
export function userStartedProjects(projects: ProjectSummary[]): ProjectSummary[] {
return projects.map(withUserStartedSessions)
}

View file

@ -1,4 +1,10 @@
import { createRequire } from 'node:module'
import { copyFileSync, existsSync, mkdirSync, readdirSync, renameSync, statSync, unlinkSync, utimesSync } from 'node:fs'
import { createHash, randomBytes } from 'node:crypto'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { getCodeburnCacheDir } from './cache-dir.js'
/// Thin SQLite read-only wrapper over Node's built-in `node:sqlite` module (stable in
/// Node 24, experimental in Node 22 / 23). Replaces the earlier `better-sqlite3` binding
@ -14,12 +20,14 @@ export type SqliteDatabase = {
close(): void
}
type DatabaseSyncCtor = new (path: string, options?: { readOnly?: boolean }) => {
type DatabaseSyncInstance = {
prepare(sql: string): { all(...params: unknown[]): Row[] }
exec?(sql: string): void
close(): void
}
type DatabaseSyncCtor = new (path: string, options?: { readOnly?: boolean }) => DatabaseSyncInstance
let DatabaseSync: DatabaseSyncCtor | null = null
let loadAttempted = false
let loadError: string | null = null
@ -116,12 +124,304 @@ export function isSqliteBusyError(err: unknown): boolean {
)
}
/// SQLite reports SQLITE_READONLY_DIRECTORY as ERR_SQLITE_ERROR with an extended
/// result code on the Node 22 builds CodeBurn supports. Keep the base-code check
/// so this also covers SQLITE_READONLY and its other extended variants, while
/// leaving ENOENT/SQLITE_CANTOPEN distinguishable to callers.
export function isSqliteReadonlyError(err: unknown): boolean {
const e = err as { code?: unknown; errcode?: unknown; errstr?: unknown; message?: unknown } | null
const code = typeof e?.code === 'string' ? e.code : ''
const errcode = typeof e?.errcode === 'number' ? e.errcode : null
const message = [
typeof e?.message === 'string' ? e.message : '',
typeof e?.errstr === 'string' ? e.errstr : '',
].join(' ')
return (
(errcode !== null && (errcode & 0xff) === 8) ||
/SQLITE_READONLY|attempt to write a readonly database|readonly database|read-only database/i.test(`${code} ${message}`)
)
}
/// A read-only parent reports SQLITE_READONLY_DIRECTORY when it must create the
/// sidecars from scratch, but SQLITE_CANTOPEN when a `-wal` is present and the
/// `-shm` it needs to index it is not. openReadonlyCache re-throws the original
/// error when the database itself is missing, which is the other CANTOPEN.
function isSqliteSidecarError(err: unknown): boolean {
if (isSqliteReadonlyError(err)) return true
const errcode = (err as { errcode?: unknown } | null)?.errcode
return typeof errcode === 'number' && (errcode & 0xff) === 14
}
let uriFilenamesSupported: boolean | null = null
/// node:sqlite only enables SQLITE_OPEN_URI from Node 22.15 on (measured: 22.13
/// and 22.14 fail, 22.15 and later work). Below that a `file:...` location is
/// taken literally and fails as CANTOPEN, so the immutable open is not attempted
/// there. The probe is an in-memory URI rather than a version comparison: it
/// answers the question directly and touches no filesystem either way.
export function sqliteSupportsUriFilenames(): boolean {
if (uriFilenamesSupported !== null) return uriFilenamesSupported
uriFilenamesSupported = false
const Driver = loadDriver() ? DatabaseSync : null
if (Driver !== null) {
try {
new Driver('file:codeburn-uri-probe?mode=memory', { readOnly: true }).close()
uriFilenamesSupported = true
} catch {
// An older build: locations are plain paths, and the copy fallback covers
// exactly the case the immutable open would have.
}
}
return uriFilenamesSupported
}
type DatabaseFingerprint = {
dev: number
ino: number
mtimeMs: number
sizeBytes: number
walBytes: number
}
/// A superseded copy is dropped once it has gone this long without being used.
/// The delay is what keeps a concurrent reader of the previous copy from having
/// its file yanked out from under it.
const CACHE_ENTRY_MAX_AGE_MS = 24 * 60 * 60 * 1000
const warnedDatabases = new Set<string>()
/// One notice per source path per run: a provider may discover many sessions
/// from the same database, and the first notice already says what happened.
function warnSqliteOnce(path: string, message: string): void {
if (warnedDatabases.has(path)) return
warnedDatabases.add(path)
process.stderr.write(message)
}
/// A read-only SQLite connection can still need sidecar files.
export function warnSqliteReadonlyOnce(path: string): void {
warnSqliteOnce(
path,
`codeburn: SQLite database ${path} is in a read-only directory and needs sidecar files; using a cache copy when necessary. ` +
'The original database is not modified.\n',
)
}
function errorCode(err: unknown): string | undefined {
if (typeof err !== 'object' || err === null || !('code' in err)) return undefined
const code = err.code
return typeof code === 'string' ? code : undefined
}
function describeError(err: unknown): string {
return err instanceof Error ? err.message : String(err)
}
/// This deliberately mirrors fingerprintSqliteFile/fingerprintFile in
/// session-cache.ts. openDatabase is synchronous, so the fallback uses the
/// synchronous fs APIs only after the direct open has already failed; the
/// ordinary successful open remains probe-free.
function fingerprintDatabase(path: string): DatabaseFingerprint {
const main = statSync(path)
let wal: ReturnType<typeof statSync> | null = null
try {
wal = statSync(path + '-wal')
} catch (err) {
if (errorCode(err) !== 'ENOENT') throw err
}
return {
dev: main.dev,
ino: main.ino,
mtimeMs: wal ? Math.max(main.mtimeMs, wal.mtimeMs) : main.mtimeMs,
sizeBytes: main.size + (wal?.size ?? 0),
walBytes: wal?.size ?? 0,
}
}
function sameFingerprint(a: DatabaseFingerprint, b: DatabaseFingerprint): boolean {
return (
a.dev === b.dev &&
a.ino === b.ino &&
a.mtimeMs === b.mtimeMs &&
a.sizeBytes === b.sizeBytes &&
a.walBytes === b.walBytes
)
}
function unlinkQuietly(path: string): void {
try {
unlinkSync(path)
} catch {
// Already gone, or still held open by another CodeBurn on Windows. Either
// way the next run's eviction pass gets another chance at it.
}
}
function copyOptionalFile(sourcePath: string, destinationPath: string): boolean {
try {
copyFileSync(sourcePath, destinationPath)
return true
} catch (err) {
if (errorCode(err) === 'ENOENT') return false
throw err
}
}
function sourceKeyOf(sourcePath: string): string {
return createHash('sha256').update(sourcePath, 'utf8').digest('hex').slice(0, 32)
}
/// The copy is named after the source it came from AND the fingerprint it was
/// taken at, so a refresh publishes a new file rather than overwriting one that
/// another process may still have open.
function cacheEntryName(sourceKey: string, fingerprint: DatabaseFingerprint): string {
const parts = `${fingerprint.dev}:${fingerprint.ino}:${fingerprint.mtimeMs}:${fingerprint.sizeBytes}:${fingerprint.walBytes}`
return `${sourceKey}.${createHash('sha256').update(parts).digest('hex').slice(0, 16)}.db`
}
function dropCopy(cacheDir: string, name: string): void {
unlinkQuietly(join(cacheDir, name))
unlinkQuietly(join(cacheDir, name + '-wal'))
unlinkQuietly(join(cacheDir, name + '-shm'))
}
/// Superseded copies are cleaned up here rather than by overwriting them: keep
/// the one in use plus at most one predecessor, and drop anything untouched for
/// a day, which is also what a source path that no longer exists looks like.
/// Reuse touches the copy, so its mtime is last-use rather than copy time.
function evictSupersededCopies(cacheDir: string, sourceKey: string, keepName: string): void {
let names: string[]
try {
names = readdirSync(cacheDir)
} catch {
return
}
const now = Date.now()
const superseded: { name: string, mtimeMs: number }[] = []
for (const name of names) {
if (!name.endsWith('.db') || name === keepName) continue
let mtimeMs: number
try {
mtimeMs = statSync(join(cacheDir, name)).mtimeMs
} catch {
continue
}
if (name.startsWith(`${sourceKey}.`)) superseded.push({ name, mtimeMs })
else if (now - mtimeMs > CACHE_ENTRY_MAX_AGE_MS) dropCopy(cacheDir, name)
}
superseded.sort((a, b) => b.mtimeMs - a.mtimeMs)
for (const [index, entry] of superseded.entries()) {
if (index > 0 || now - entry.mtimeMs > CACHE_ENTRY_MAX_AGE_MS) dropCopy(cacheDir, entry.name)
}
}
/// A concurrent CodeBurn may have published the same copy first. The name is
/// the fingerprint, so the content is identical by construction and losing that
/// race is not an error.
function publish(tempPath: string, finalPath: string): void {
try {
renameSync(tempPath, finalPath)
} catch (err) {
if (!existsSync(finalPath)) throw err
}
}
function readOnlyCachePath(sourcePath: string, fingerprint: DatabaseFingerprint): string {
const cacheDir = join(getCodeburnCacheDir(), 'sqlite-ro')
mkdirSync(cacheDir, { recursive: true, mode: 0o700 })
const sourceKey = sourceKeyOf(sourcePath)
const name = cacheEntryName(sourceKey, fingerprint)
const cachePath = join(cacheDir, name)
if (existsSync(cachePath)) {
const now = new Date()
try {
utimesSync(cachePath, now, now)
} catch {
// mtime is only the eviction clock; a copy we cannot touch still reads.
}
evictSupersededCopies(cacheDir, sourceKey, name)
return cachePath
}
const tempBase = `${cachePath}.tmp-${process.pid}-${randomBytes(8).toString('hex')}`
const tempWal = tempBase + '-wal'
try {
copyFileSync(sourcePath, tempBase)
const copiedWal = copyOptionalFile(sourcePath + '-wal', tempWal)
// Do not publish a cache made from a moving database. A live WAL writer will
// normally make the direct open succeed once its sidecars exist; this check
// covers the narrow race where the source changes during the copy fallback.
if (!sameFingerprint(fingerprintDatabase(sourcePath), fingerprint)) {
throw new Error('SQLite database changed while preparing its read-only cache copy')
}
// The -wal goes first: a reader that can see the database must never find it
// without the sidecar holding its most recent rows.
if (copiedWal) publish(tempWal, cachePath + '-wal')
publish(tempBase, cachePath)
evictSupersededCopies(cacheDir, sourceKey, name)
return cachePath
} finally {
unlinkQuietly(tempBase)
unlinkQuietly(tempWal)
}
}
function openReadonlyCache(path: string, originalError: unknown): DatabaseSyncInstance {
const Driver = DatabaseSync
if (Driver === null) throw new Error(getSqliteLoadError())
let fingerprint: DatabaseFingerprint
try {
fingerprint = fingerprintDatabase(path)
} catch {
// Preserve the original SQLite error when the source disappeared or became
// inaccessible between the failed query and the fallback probe.
throw originalError
}
// An absent or empty -wal holds no frames, so there is nothing to go stale and
// nothing worth copying: immutable lets SQLite skip the -shm it cannot create
// and read the source in place.
if (fingerprint.walBytes === 0 && sqliteSupportsUriFilenames()) {
try {
return new Driver(`${pathToFileURL(path).href}?immutable=1`, { readOnly: true })
} catch {
// Understood but refused: the copy covers it.
}
}
let cachedPath: string
try {
cachedPath = readOnlyCachePath(path, fingerprint)
} catch (err) {
warnSqliteOnce(
path,
`codeburn: SQLite database ${path} is in a read-only directory and its cache copy could not be written ` +
`(${describeError(err)}); skipping this database.\n`,
)
throw originalError
}
return new Driver(cachedPath, { readOnly: true })
}
export function openDatabase(path: string): SqliteDatabase {
if (!loadDriver() || DatabaseSync === null) {
throw new Error(getSqliteLoadError())
}
const db = new DatabaseSync(path, { readOnly: true })
let db: DatabaseSyncInstance
let fallbackUsed = false
try {
db = new DatabaseSync(path, { readOnly: true })
} catch (err) {
if (!isSqliteSidecarError(err)) throw err
fallbackUsed = true
db = openReadonlyCache(path, err)
warnSqliteReadonlyOnce(path)
}
try {
db.exec?.('PRAGMA busy_timeout = 1000')
} catch {
@ -130,7 +430,26 @@ export function openDatabase(path: string): SqliteDatabase {
return {
query<T extends Row = Row>(sql: string, params: unknown[] = []): T[] {
return db.prepare(sql).all(...params) as T[]
try {
return db.prepare(sql).all(...params) as T[]
} catch (err) {
if (!isSqliteSidecarError(err)) throw err
if (fallbackUsed) throw err
fallbackUsed = true
try {
db.close()
} catch {
// The failed connection may already have been closed by node:sqlite.
}
db = openReadonlyCache(path, err)
warnSqliteReadonlyOnce(path)
try {
db.exec?.('PRAGMA busy_timeout = 1000')
} catch {
// Best effort, matching the direct-open path above.
}
return db.prepare(sql).all(...params) as T[]
}
},
close() {
db.close()

View file

@ -211,6 +211,11 @@ export type SessionSummary = {
/// correlations performed after all saved sessions have been parsed.
prAttributionSource?: 'transcript' | 'explicit-reference' | 'working-directory' | 'launcher-prompt'
source?: SessionSourceMetadata
/// Claude Code only: true when this record is a subagent (sidechain)
/// transcript rather than a user-started parent session. Sidechain spend is
/// real and remains in every cost/token/call aggregate; consumers that reason
/// about human session populations may exclude it explicitly.
isSidechain?: boolean
// Claude Code only: agent type of a subagent transcript session
// (`workflow-subagent`, `Explore`, `general-purpose`, …); undefined for
// ordinary sessions. Drives the Claude-scoped agent-type breakdown.

View file

@ -941,7 +941,7 @@ export async function buildMenubarPayloadForRange(periodInfo: PeriodInfo, opts:
}
})()
const optimize = opts.optimize === false ? null : await scanAndDetect(scanProjects, scanRange)
const optimize = opts.optimize === false ? null : await scanAndDetect(scanProjects, scanRange, opts.provider)
const granularRange = opts.daysSelection?.range ?? scanRange
const granularHistory = opts.timeline === false ? undefined : buildGranularHistory(scanProjects, granularRange)
return buildMenubarPayload(currentData, providers, optimize, dailyHistory, retryTax, routingWaste, breakdowns, claudeConfigs, granularHistory)

View file

@ -90,6 +90,15 @@ function openBrowser(url: string): void {
}
}
export function injectDashboardBootstrap(html: string, payload: unknown): string {
const json = JSON.stringify(payload)
if (json === undefined) throw new TypeError('dashboard bootstrap payload is not serializable')
// Keep the JSON safe at the boundary where it enters an HTML script. This is
// deliberately inside the helper so callers cannot forget the escape.
const safeJson = json.replace(/</g, String.fromCharCode(92) + 'u003c')
return html.replace('<script type="module"', () => `<script>window.__CODEBURN_BOOTSTRAP__=${safeJson}</script>\n <script type="module"`)
}
export async function runWebDashboard(opts: {
period: string
provider: string
@ -180,9 +189,7 @@ export async function runWebDashboard(opts: {
const html = await readFile(filePath, 'utf8')
const payload = await getLocalPayload(opts.period, opts.provider, opts.from, opts.to)
const devices = [{ id: 'local', name: hostname(), local: true, payload }]
// Escape every '<' so a device/model/project name can't close the <script>.
const json = JSON.stringify({ devices }).replace(/</g, String.fromCharCode(92) + 'u003c')
const injected = html.replace('<script type="module"', `<script>window.__CODEBURN_BOOTSTRAP__=${json}</script>\n <script type="module"`)
const injected = injectDashboardBootstrap(html, { devices })
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' })
res.end(injected)
}

View file

@ -1,6 +1,7 @@
import { homedir } from 'os'
import { EDIT_TOOLS } from './classifier.js'
import { userStartedProjects } from './session-population.js'
import { CATEGORY_LABELS, type ProjectSummary, type TaskCategory } from './types.js'
// User-side mirror of compare-stats.ts scanSelfCorrections (which scans the
@ -44,7 +45,7 @@ export type UserCorrectionStats = {
export function scanUserCorrections(projects: ProjectSummary[]): UserCorrectionStats {
let corrections = 0
let userTurns = 0
for (const project of projects) {
for (const project of userStartedProjects(projects)) {
for (const session of project.sessions) {
// A correction is a FOLLOW-UP by definition: the session's opening
// prompt cannot be correcting this assistant, however correction-shaped
@ -95,7 +96,7 @@ export function sessionTimeToFirstEditMs(session: ProjectSummary['sessions'][num
export function medianTimeToFirstEditMs(projects: ProjectSummary[]): number | null {
const samples: number[] = []
for (const project of projects) {
for (const project of userStartedProjects(projects)) {
for (const session of project.sessions) {
const ms = sessionTimeToFirstEditMs(session)
if (ms !== null) samples.push(ms)
@ -140,7 +141,7 @@ export function aggregateFileChurn(projects: ProjectSummary[], limit = 15): Rewo
type Acc = { path: string; sessions: Set<string>; edits: number }
const byPath = new Map<string, Acc>()
for (const project of projects) {
for (const project of userStartedProjects(projects)) {
for (const session of project.sessions) {
for (const turn of session.turns) {
for (const call of turn.assistantCalls) {
@ -191,7 +192,7 @@ export const MIN_ONE_SHOT_EDIT_TURNS = 5
/// model-efficiency and the report's category one-shot figures.
export function worstOneShotCategory(projects: ProjectSummary[], minEditTurns = MIN_ONE_SHOT_EDIT_TURNS): CategoryOneShot | null {
const acc = new Map<string, { editTurns: number; oneShotTurns: number }>()
for (const project of projects) {
for (const project of userStartedProjects(projects)) {
for (const session of project.sessions) {
for (const [cat, d] of Object.entries(session.categoryBreakdown)) {
const e = acc.get(cat) ?? { editTurns: 0, oneShotTurns: 0 }

View file

@ -212,6 +212,13 @@ describe('model default recommendations', () => {
expect(recommendModelDefault(project, { now: NOW })).toBeNull()
})
it('never recommends an actionable default from sidechain-only behavior', () => {
const project = recommendationProject()
project.sessions[0]!.isSidechain = true
expect(recommendModelDefault(project, { now: NOW })).toBeNull()
})
})
describe('model default apply plan', () => {

View file

@ -5,13 +5,17 @@ import { join } from 'node:path'
import { journalPath } from '../src/act/journal.js'
import {
autoRevertNoEffect,
buildActReportJson,
buildOptimizeAppliedHeader,
captureBaseline,
captureBaselinesForPlans,
computeActReport,
renderActReport,
} from '../src/act/report.js'
import { formatAppliedFix, REPORT_MIN_AGE_DAYS } from '../src/act/types.js'
import type { ActionRecord } from '../src/act/types.js'
import type { FindingPlan } from '../src/act/plans.js'
import type { WasteFinding } from '../src/optimize.js'
import type { ClassifiedTurn, ProjectSummary } from '../src/types.js'
@ -762,3 +766,296 @@ describe('defer baseline capture', () => {
expect(b).toBeUndefined()
})
})
describe('partial-action baseline capture', () => {
it('persists the savings attributable to the local mutation, not the full mixed finding', () => {
const finding: WasteFinding = {
id: 'mcp-low-coverage',
title: '2 MCP servers with low tool coverage',
explanation: '',
impact: 'medium',
tokensSaved: 40_000,
applyTokensSaved: 20_000,
fix: { type: 'command', label: '', text: "claude mcp remove 'filesystem'" },
apply: { kind: 'mcp-remove', servers: ['filesystem'] },
}
const sessions = sessionsAt(2, daysAgo(1), {
mcpInventory: Array.from({ length: 20 }, (_, i) => `mcp__filesystem__t${i}`),
})
const baseline = captureBaseline(finding, 'mcp-remove', {
projects: [projectOf(sessions)],
coverage: [{
server: 'filesystem',
toolsAvailable: 20,
toolsInvoked: 3,
unusedTools: Array.from({ length: 17 }, (_, i) => `mcp__filesystem__unused${i}`),
invocations: 0,
loadedSessions: 2,
coverageRatio: 3 / 20,
}],
windowDays: 14,
now: NOW,
})
expect(baseline).toMatchObject({
estimatedTokens: 20_000,
sessions: 2,
metrics: { filesystem: 6_800 },
})
expect(finding.tokensSaved).toBe(40_000)
})
it('prices and measures only servers owned by the concrete mutation plan', () => {
const finding: WasteFinding = {
id: 'mcp-low-coverage',
title: '2 MCP servers with low tool coverage',
explanation: '',
impact: 'medium',
tokensSaved: 30_000,
applyTokensSaved: 30_000,
applyTokensSavedByServer: { filesystem: 10_000, managed: 20_000 },
fix: { type: 'command', label: '', text: '' },
apply: { kind: 'mcp-remove', servers: ['filesystem', 'managed'] },
}
const sessions = sessionsAt(2, daysAgo(1), {
mcpInventory: [
...Array.from({ length: 17 }, (_, i) => `mcp__filesystem__t${i}`),
...Array.from({ length: 12 }, (_, i) => `mcp__managed__t${i}`),
],
})
const coverage = [
{
server: 'filesystem', toolsAvailable: 20, toolsInvoked: 3,
unusedTools: Array.from({ length: 17 }, (_, i) => `mcp__filesystem__t${i}`),
invocations: 3, loadedSessions: 2, coverageRatio: 3 / 20,
},
{
server: 'managed', toolsAvailable: 20, toolsInvoked: 8,
unusedTools: Array.from({ length: 12 }, (_, i) => `mcp__managed__t${i}`),
invocations: 8, loadedSessions: 2, coverageRatio: 8 / 20,
},
]
const baseline = captureBaseline(finding, 'mcp-remove', {
projects: [projectOf(sessions)], coverage, windowDays: 14, now: NOW,
}, ['filesystem'])
expect(baseline).toMatchObject({
estimatedTokens: 10_000,
sessions: 2,
metrics: { filesystem: 6_800 },
})
expect(baseline!.metrics).not.toHaveProperty('managed')
})
it('does not invent a low-coverage schema baseline when coverage is unavailable', () => {
const finding: WasteFinding = {
id: 'mcp-low-coverage',
title: '1 MCP server with low tool coverage',
explanation: '',
impact: 'medium',
tokensSaved: 10_000,
applyTokensSavedByServer: { filesystem: 10_000 },
fix: { type: 'command', label: '', text: '' },
apply: { kind: 'mcp-remove', servers: ['filesystem'] },
}
const baseline = captureBaseline(finding, 'mcp-remove', {
projects: [projectOf(sessionsAt(2, daysAgo(1)))],
coverage: [],
windowDays: 14,
now: NOW,
}, ['filesystem'])
expect(baseline).toMatchObject({
estimatedTokens: 10_000,
metrics: { filesystem: 0 },
})
})
it('stamps a narrowed plan with only its concrete server baseline', async () => {
const finding: WasteFinding = {
id: 'mcp-low-coverage', title: '2 MCP servers', explanation: '', impact: 'medium',
tokensSaved: 30_000, applyTokensSaved: 30_000,
applyTokensSavedByServer: { filesystem: 10_000, managed: 20_000 },
fix: { type: 'command', label: '', text: '' },
apply: { kind: 'mcp-remove', servers: ['filesystem', 'managed'] },
}
const plan: FindingPlan = {
finding,
notes: [],
plan: {
kind: 'mcp-remove', description: 'Remove filesystem', changes: [],
affectedMcpServers: ['filesystem'],
},
}
const sessions = sessionsAt(2, daysAgo(1), {
mcpInventory: [
...Array.from({ length: 17 }, (_, i) => `mcp__filesystem__t${i}`),
...Array.from({ length: 12 }, (_, i) => `mcp__managed__t${i}`),
],
})
await captureBaselinesForPlans([plan], {
now: NOW,
loadProjects: async () => [projectOf(sessions)],
})
expect(plan.plan?.baseline).toMatchObject({
estimatedTokens: 10_000,
metrics: { filesystem: 6_800 },
})
expect(plan.plan?.baseline?.metrics).not.toHaveProperty('managed')
})
it('does not stamp a numeric baseline onto an uncertain partial mutation', async () => {
const finding: WasteFinding = {
id: 'mcp-low-coverage', title: '1 MCP server', explanation: '', impact: 'medium',
tokensSaved: 10_000, applyTokensSavedByServer: { filesystem: 10_000 },
fix: { type: 'command', label: '', text: '' },
apply: { kind: 'mcp-remove', servers: ['filesystem'] },
}
const plan: FindingPlan = {
finding,
notes: ['could not parse .mcp.json'],
plan: {
kind: 'mcp-remove', description: 'Remove filesystem', changes: [],
affectedMcpServers: ['filesystem'], mcpSavingsUncertain: true,
},
}
await captureBaselinesForPlans([plan], {
now: NOW,
loadProjects: async () => [projectOf(sessionsAt(2, daysAgo(1)))],
})
expect(plan.plan?.baseline).toBeUndefined()
})
})
describe('applied-fix verdicts', () => {
const fixOf = async (records: ActionRecord[], projects: ProjectSummary[]) => {
const actionsDir = await writeJournal(records)
const report = await computeActReport({ actionsDir, now: NOW, loadProjects: load(projects) })
return { report, fixes: report.appliedFixes, actionsDir }
}
it('calls a fix that realized its whole window estimate "worked"', async () => {
const { fixes } = await fixOf([mcpRecord()], [projectOf(sessionsAt(20, daysAgo(5)))])
expect(fixes).toHaveLength(1)
expect(fixes[0]!.verdict).toBe('worked')
expect(fixes[0]!.estimatedTokens).toBe(40_000)
expect(fixes[0]!.realizedTokens).toBe(40_000)
expect(fixes[0]!.undoCommand).toBe('codeburn act undo a1')
})
it('holds the worked/partial boundary at the 70% ratio', async () => {
const rec = mcpRecord({ kind: 'mcp-project-scope' })
// 14 of 20 sessions saved = exactly 70% of the window estimate.
const at70 = await fixOf(
[rec],
[projectOf([...sessionsAt(14, daysAgo(5)), ...sessionsAt(6, daysAgo(4), { mcpInventory: ['mcp__brave-search__search'] })])],
)
expect(at70.fixes[0]!.verdict).toBe('worked')
const below = await fixOf(
[rec],
[projectOf([...sessionsAt(13, daysAgo(5)), ...sessionsAt(7, daysAgo(4), { mcpInventory: ['mcp__brave-search__search'] })])],
)
expect(below.fixes[0]!.verdict).toBe('partial')
expect(formatAppliedFix(below.fixes[0]!)).toContain('-35% vs estimate')
})
it('calls a fix that realized nothing "no-effect" and offers the undo', async () => {
const rec = mcpRecord({ kind: 'mcp-project-scope' })
const stillLoading = sessionsAt(20, daysAgo(5), { mcpInventory: ['mcp__brave-search__search'] })
const { fixes } = await fixOf([rec], [projectOf(stillLoading)])
expect(fixes[0]!.verdict).toBe('no-effect')
expect(fixes[0]!.realizedTokens).toBe(0)
expect(formatAppliedFix(fixes[0]!)).toContain('did not help. Revert: codeburn act undo a1')
})
it('treats an estimate of zero as worked only when something was realized', async () => {
const zeroEstimate = { windowDays: 14, capturedAt: daysAgo(10), estimatedTokens: 0, sessions: 0, metrics: {} }
const { fixes } = await fixOf(
[mcpRecord({ baseline: { ...zeroEstimate, metrics: { 'brave-search': 2000 } } })],
[projectOf(sessionsAt(20, daysAgo(5)))],
)
expect(fixes[0]!.estimatedTokens).toBe(40_000)
expect(fixes[0]!.verdict).toBe('worked')
})
it('leaves entries younger than the measurement window pending', async () => {
const { fixes } = await fixOf([mcpRecord({ at: daysAgo(1) })], [projectOf(sessionsAt(20, daysAgo(1)))])
expect(fixes[0]!.verdict).toBe('pending')
expect(formatAppliedFix(fixes[0]!)).toBe(`unused-mcp (1d ago): measuring, check back after ${REPORT_MIN_AGE_DAYS} days`)
})
it('keeps a user-reverted entry out of the verdicts and carries its note', async () => {
const back = sessionsAt(20, daysAgo(5), { mcpInventory: ['mcp__brave-search__search'] })
const { fixes } = await fixOf([mcpRecord()], [projectOf(back)])
expect(fixes[0]!.verdict).toBe('pending')
expect(fixes[0]!.note).toMatch(/reverted by user/)
})
it('drops undone journal entries entirely', async () => {
const rec = mcpRecord()
const { fixes } = await fixOf(
[rec, { ...rec, status: 'undone', undoneAt: daysAgo(2) }],
[projectOf(sessionsAt(20, daysAgo(5)))],
)
expect(fixes).toHaveLength(0)
})
it('never judges a correlation-only kind, so --auto-revert can never touch it', async () => {
const { fixes } = await fixOf([modelDefaultRecord()], [modelProject('app', '/tmp/app', 'candidate-model', 20, 19)])
expect(fixes[0]!.verdict).toBe('pending')
})
})
describe('autoRevertNoEffect', () => {
const noEffect = (over: Partial<ActionRecord> = {}) => ({
...mcpRecord({ kind: 'mcp-project-scope', ...over }),
})
it('undoes the no-effect entries and leaves the rest alone', async () => {
const worked = noEffect({ id: 'keep1', findingId: 'kept' })
const actionsDir = await writeJournal([noEffect(), worked])
const stillLoading = sessionsAt(20, daysAgo(5), { mcpInventory: ['mcp__brave-search__search'] })
const report = await computeActReport({ actionsDir, now: NOW, loadProjects: load([projectOf(stillLoading)]) })
// Both are no-effect here; pin that only the ones we hand over get undone.
const target = report.appliedFixes.filter(f => f.id === 'a1')
const { lines, revertedIds } = await autoRevertNoEffect(target, { actionsDir })
expect([...revertedIds]).toEqual(['a1'])
expect(lines).toEqual(['Reverted a1: Remove an MCP server from config'])
const after = await computeActReport({ actionsDir, now: NOW, loadProjects: load([projectOf(stillLoading)]) })
expect(after.appliedFixes.map(f => f.id)).toEqual(['keep1'])
})
it('never auto-reverts a CLAUDE.md rule, it prints the undo command instead', async () => {
const rec = mcpRecord({
id: 'cm1',
kind: 'claude-md-rule',
findingId: 'read-edit-ratio',
baseline: { windowDays: 14, capturedAt: daysAgo(10), estimatedTokens: 10_000, sessions: 20, metrics: { reads: 10, edits: 10 } },
})
const actionsDir = await writeJournal([rec])
const sessions = sessionsAt(20, daysAgo(5), { toolBreakdown: { Edit: { calls: 10, tokens: 0 }, Read: { calls: 10, tokens: 0 } } as never })
const report = await computeActReport({ actionsDir, now: NOW, loadProjects: load([projectOf(sessions)]) })
expect(report.appliedFixes[0]!.verdict).toBe('no-effect')
const { lines, revertedIds } = await autoRevertNoEffect(report.appliedFixes, { actionsDir })
expect(revertedIds.size).toBe(0)
expect(lines).toEqual(['Not auto-reverted: read-edit-ratio edits a CLAUDE.md. Revert: codeburn act undo cm1'])
})
it('ignores partial and pending entries', async () => {
const actionsDir = await writeJournal([mcpRecord({ at: daysAgo(1) })])
const report = await computeActReport({ actionsDir, now: NOW, loadProjects: load([projectOf(sessionsAt(20, daysAgo(1)))]) })
const { lines, revertedIds } = await autoRevertNoEffect(report.appliedFixes, { actionsDir })
expect(lines).toEqual([])
expect(revertedIds.size).toBe(0)
})
})

View file

@ -0,0 +1,153 @@
import { spawnSync } from 'node:child_process'
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
function runCli(args: string[], home: string, locale?: string) {
return spawnSync(process.execPath, ['--import', 'tsx', 'src/cli.ts', ...args], {
cwd: process.cwd(),
env: {
...process.env,
HOME: home,
USERPROFILE: home,
CLAUDE_CONFIG_DIR: join(home, '.claude'),
CODEBURN_CACHE_DIR: join(home, '.cache', 'codeburn'),
TZ: 'UTC',
...(locale ? { LANG: locale, LC_ALL: locale } : {}),
},
encoding: 'utf-8',
timeout: 30_000,
})
}
function userLine(timestamp: string): string {
return JSON.stringify({
type: 'user', sessionId: 'unpriced-969', timestamp, cwd: '/tmp/unpriced-969',
message: { role: 'user', content: 'inspect pricing coverage' },
})
}
function assistantLine(model: string, timestamp: string, messageId: string, input: number): string {
return JSON.stringify({
type: 'assistant', sessionId: 'unpriced-969', timestamp, cwd: '/tmp/unpriced-969',
message: {
id: messageId, type: 'message', role: 'assistant', model,
content: [{ type: 'text', text: 'done' }],
usage: {
input_tokens: input, output_tokens: 100,
cache_read_input_tokens: 0, cache_creation_input_tokens: 0,
},
},
})
}
async function withFixture(lines: string[], run: (home: string) => void): Promise<void> {
const home = await mkdtemp(join(tmpdir(), 'codeburn-models-unpriced-'))
try {
const projectDir = join(home, '.claude', 'projects', 'unpriced-969')
await mkdir(projectDir, { recursive: true })
await writeFile(join(projectDir, 'session.jsonl'), `${lines.join('\n')}\n`)
run(home)
} finally {
await rm(home, { recursive: true, force: true })
}
}
const range = ['--from', '2026-05-20', '--to', '2026-05-20', '--provider', 'claude']
describe('codeburn models --unpriced public CLI', () => {
it('filters before --top and returns the largest unpriced raw ID deterministically', async () => {
await withFixture([
userLine('2026-05-20T10:00:00.000Z'),
assistantLine('acme/unknown-small-969', '2026-05-20T10:01:00.000Z', 'small', 1_000),
assistantLine('claude-opus-4-6', '2026-05-20T10:02:00.000Z', 'priced', 20_000),
assistantLine('acme/unknown-large-969', '2026-05-20T10:03:00.000Z', 'large', 9_000),
], home => {
const result = runCli(['models', '--unpriced', '--top', '1', '--format', 'json', ...range], home)
expect(result.status, result.stderr).toBe(0)
expect(JSON.parse(result.stdout)).toEqual([
expect.objectContaining({ model: 'acme/unknown-large-969', totalTokens: 9_100 }),
])
})
})
it('orders tied unpriced rows identically across host locales', async () => {
await withFixture([
userLine('2026-05-20T10:00:00.000Z'),
assistantLine('acme/z-unknown-969', '2026-05-20T10:01:00.000Z', 'z-model', 1_000),
assistantLine('acme/ä-unknown-969', '2026-05-20T10:02:00.000Z', 'a-umlaut-model', 1_000),
], home => {
const args = ['models', '--unpriced', '--format', 'json', ...range]
const english = runCli(args, home, 'en_US.UTF-8')
const swedish = runCli(args, home, 'sv_SE.UTF-8')
expect(english.status, english.stderr).toBe(0)
expect(swedish.status, swedish.stderr).toBe(0)
const models = (stdout: string) => (JSON.parse(stdout) as Array<{ model: string }>).map(row => row.model)
expect(models(english.stdout)).toEqual(['acme/z-unknown-969', 'acme/ä-unknown-969'])
expect(models(swedish.stdout)).toEqual(models(english.stdout))
})
})
it('honors an explicitly supplied finite --min-cost threshold', async () => {
await withFixture([
userLine('2026-05-20T10:00:00.000Z'),
assistantLine('acme/unknown-zero-969', '2026-05-20T10:01:00.000Z', 'zero', 1_000),
], home => {
const result = runCli(['models', '--unpriced', '--min-cost', '0.01', '--format', 'json', ...range], home)
expect(result.status, result.stderr).toBe(0)
expect(JSON.parse(result.stdout)).toEqual([])
})
})
it('lists every unpriced model in table output with an actionable hint', async () => {
await withFixture([
userLine('2026-05-20T10:00:00.000Z'),
assistantLine('acme/unknown-alpha-969', '2026-05-20T10:01:00.000Z', 'alpha', 1_000),
assistantLine('acme/unknown-beta-969', '2026-05-20T10:02:00.000Z', 'beta', 2_000),
assistantLine('claude-opus-4-6', '2026-05-20T10:03:00.000Z', 'priced', 3_000),
], home => {
const result = runCli(['models', '--unpriced', ...range], home)
expect(result.status, result.stderr).toBe(0)
expect(result.stdout).toContain('acme/unknown-alpha-969')
expect(result.stdout).toContain('acme/unknown-beta-969')
expect(result.stdout).not.toContain('claude-opus-4-6')
expect(result.stdout).toContain('If a model is billed per token, map it with: codeburn model-alias "<model>" <known-model>')
// #968: aliasing a subscription-billed model fabricates spend, so the
// hint must never read as an unconditional instruction.
expect(result.stdout).toContain('Subscription or flat-rate models are correctly $0.')
expect(result.stdout).not.toContain('Fix: codeburn model-alias')
})
})
it('reports a clean period explicitly', async () => {
const home = await mkdtemp(join(tmpdir(), 'codeburn-models-unpriced-empty-'))
try {
const result = runCli(['models', '--unpriced', ...range], home)
expect(result.status, result.stderr).toBe(0)
expect(result.stdout).toBe('No unpriced models found for the selected period.\n')
} finally {
await rm(home, { recursive: true, force: true })
}
})
it('sanitizes hostile IDs in human formats while JSON stays lossless', async () => {
const hostile = `acme/alpha\u001b]0;forged\u0007click\u001b[31m\nforged-row-${'x'.repeat(300)}`
await withFixture([
userLine('2026-05-20T10:00:00.000Z'),
assistantLine(hostile, '2026-05-20T10:01:00.000Z', 'hostile', 1_000),
], home => {
for (const format of ['table', 'markdown', 'csv']) {
const result = runCli(['models', '--unpriced', '--format', format, ...range], home)
expect(result.status, `${format}: ${result.stderr}`).toBe(0)
expect(result.stdout).not.toMatch(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/)
expect(result.stdout).not.toContain('\nforged-row-')
expect(result.stdout).not.toContain('x'.repeat(201))
}
const json = runCli(['models', '--unpriced', '--format', 'json', ...range], home)
expect(json.status, json.stderr).toBe(0)
expect((JSON.parse(json.stdout) as Array<{ model: string }>)[0]?.model).toBe(hostile)
})
}, 15_000)
})

View file

@ -712,3 +712,102 @@ describe('adoption union across older cache files', () => {
expect(hydrated.complete).toBe(true)
})
})
describe('partial survival: a truncated fresh slice cannot delete a settled baseline', () => {
// The real 0.9.20 -> next upgrade loss: transcripts age out per FILE, so a
// mostly-forgotten day still gets a handful of turns from surviving later
// files. The fresh slice is non-empty but truncated, and before this guard it
// replaced the baseline outright ($1,685.17 / 12,530 calls -> $385.44 / 560).
const settled = daysAgoStr(33)
it('keeps the baseline slice when the fresh derivation lost calls on a settled day', () => {
const fresh = day(settled, { claude: slice(385.44, 560, { sessions: 0, inputTokens: 10 }) })
const baseline = day(settled, { claude: slice(1685.17, 12530, { sessions: 214, inputTokens: 500 }) })
const merged = mergeDayEntries([fresh], [baseline], true, undefined, true)
const m = merged[0]!
expect(m.providers['claude']).toMatchObject({ cost: 1685.17, calls: 12530, sessions: 214 })
// Day totals track the swap - they must equal the kept slice, not the sum.
expect(m.cost).toBeCloseTo(1685.17, 5)
expect(m.calls).toBe(12530)
expect(m.sessions).toBe(214)
expect(m.inputTokens).toBe(500)
expect(m.carried).toBe(true)
})
it('a truncated slice cannot take other providers down with it', () => {
const fresh = day(settled, { claude: slice(10, 5), codex: slice(7, 3) })
const baseline = day(settled, { claude: slice(100, 50) })
const m = mergeDayEntries([fresh], [baseline], true, undefined, true)[0]!
expect(m.providers['claude']).toMatchObject({ cost: 100, calls: 50 })
expect(m.providers['codex']).toMatchObject({ cost: 7, calls: 3 })
expect(m.cost).toBeCloseTo(107, 5)
expect(m.calls).toBe(53)
})
it('the fresh slice wins on equal calls at a different cost (the Grok re-pricing)', () => {
const fresh = day(settled, { grok: slice(11.89, 21, { sessions: 21, outputTokens: 900 }) })
const baseline = day(settled, { grok: slice(3.29, 21, { sessions: 21, outputTokens: 200 }) })
const m = mergeDayEntries([fresh], [baseline], true, undefined, true)[0]!
expect(m.providers['grok']).toMatchObject({ cost: 11.89, calls: 21, outputTokens: 900 })
expect(m.cost).toBeCloseTo(11.89, 5)
expect(m.carried).toBeUndefined()
})
it('a fresh slice with FEWER sessions but equal calls still wins (sessions drift on healthy days)', () => {
const fresh = day(settled, { claude: slice(1021.11, 3295, { sessions: 71 }) })
const baseline = day(settled, { claude: slice(1021.11, 3295, { sessions: 72 }) })
const m = mergeDayEntries([fresh], [baseline], true, undefined, true)[0]!
expect(m.providers['claude']!.sessions).toBe(71)
})
it('recent days stay authoritative: a shrink inside the settle window is honored', () => {
const recent = daysAgoStr(2)
const fresh = day(recent, { claude: slice(20, 4) })
const baseline = day(recent, { claude: slice(90, 40) })
const m = mergeDayEntries([fresh], [baseline], true, undefined, true)[0]!
expect(m.providers['claude']).toMatchObject({ cost: 20, calls: 4 })
expect(m.cost).toBe(20)
})
it('the adoption union is unguarded: the newer schema still wins per (date, provider)', () => {
const newer = day(settled, { claude: slice(50, 5) })
const older = day(settled, { claude: slice(100, 10) })
const m = mergeDayEntries([newer], [older], true)[0]!
expect(m.providers['claude']).toMatchObject({ cost: 50, calls: 5 })
})
it('end-to-end: a version-bump re-derive whose transcripts aged out keeps the full day', async () => {
const cache: DailyCache = {
version: DAILY_CACHE_VERSION,
savingsConfigHash: 'cfg-A',
tzKey: currentTzKey(),
lastComputedDate: daysAgoStr(1),
days: [day(settled, { claude: slice(1685.17, 12530, { sessions: 214 }) })],
complete: false, // what a version bump / adoption leaves behind
}
await saveDailyCache(cache)
const truncated = [day(settled, { claude: slice(385.44, 560, { sessions: 0 }) })]
const out = await ensureCacheHydrated(noSessions, () => truncated, 'cfg-A')
const kept = out.days.find(d => d.date === settled)!
expect(kept.providers['claude']).toMatchObject({ cost: 1685.17, calls: 12530 })
expect(kept.cost).toBeCloseTo(1685.17, 5)
// A --period all payload sums the kept slices, not the truncated ones.
const period = buildPeriodDataFromDays(out.days, 'all')
expect(period.cost).toBeCloseTo(1685.17, 5)
expect(period.calls).toBe(12530)
})
it('a fully sourceless day is still carried whole (v14 behavior, unchanged)', async () => {
const cache: DailyCache = {
version: DAILY_CACHE_VERSION,
savingsConfigHash: 'cfg-A',
tzKey: currentTzKey(),
lastComputedDate: daysAgoStr(1),
days: [day(settled, { claude: slice(230.06, 400) })],
complete: false,
}
await saveDailyCache(cache)
const out = await ensureCacheHydrated(noSessions, () => [], 'cfg-A')
expect(out.days[0]).toMatchObject({ date: settled, cost: 230.06, calls: 400, carried: true })
})
})

View file

@ -0,0 +1,110 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { mkdir, readFile, rm, writeFile } from 'fs/promises'
import { join } from 'path'
import { tmpdir } from 'os'
import {
currentTzKey,
ensureCacheHydrated,
toDateString,
type DailyEntry,
} from '../src/daily-cache.js'
// One below the current version, so this pins the ADJACENT-version case: 18
// and 20 exist only as unreleased draft heads (#946's earlier public head, and
// #1040), and a draft's days must not be adopted as finalized under a number
// that now means different accounting. Anything below MIN_SUPPORTED_VERSION is
// untrusted, which is what makes the re-derivation global rather than
// provider-scoped.
const PRE_FIX_DAILY_VERSION = 20
const cacheRoot = join(tmpdir(), `codeburn-daily-rederive-${process.pid}-${Date.now()}`)
function day(date: string, cost: number): DailyEntry {
return {
date,
cost,
savingsUSD: 0,
calls: 1,
sessions: 1,
inputTokens: 100,
outputTokens: 20,
cacheReadTokens: 30,
cacheWriteTokens: 0,
editTurns: 0,
oneShotTurns: 0,
models: {
'Grok Build': {
calls: 1,
cost,
savingsUSD: 0,
inputTokens: 100,
outputTokens: 20,
cacheReadTokens: 30,
cacheWriteTokens: 0,
},
},
categories: {},
providers: {
grok: {
calls: 1,
cost,
savingsUSD: 0,
sessions: 1,
inputTokens: 100,
outputTokens: 20,
cacheReadTokens: 30,
cacheWriteTokens: 0,
},
},
}
}
beforeEach(async () => {
process.env['CODEBURN_CACHE_DIR'] = cacheRoot
await rm(cacheRoot, { recursive: true, force: true })
await mkdir(cacheRoot, { recursive: true })
})
afterEach(async () => {
await rm(cacheRoot, { recursive: true, force: true })
})
// Raising MIN_SUPPORTED_VERSION re-derives EVERY day from EVERY provider, not
// only Grok - the daily cache has no per-provider invalidation. A Grok day is
// used here because Grok is the provider whose totals the bump exists to
// correct; the mechanism under test is version-wide.
describe('daily-cache re-derivation on a DAILY_CACHE_VERSION bump', () => {
it('re-derives a day from a below-minimum v20 cache while preserving the old file', async () => {
const date = toDateString(new Date(Date.now() - 7 * 24 * 60 * 60 * 1000))
const yesterday = toDateString(new Date(Date.now() - 24 * 60 * 60 * 1000))
const oldPath = join(cacheRoot, `daily-cache.v${PRE_FIX_DAILY_VERSION}.json`)
const oldCache = {
version: PRE_FIX_DAILY_VERSION,
savingsConfigHash: 'cfg',
tzKey: currentTzKey(),
lastComputedDate: yesterday,
days: [day(date, 99)],
complete: true,
watermarkTrusted: true,
}
await writeFile(oldPath, JSON.stringify(oldCache))
let parseCount = 0
const corrected = day(date, 2)
const hydrated = await ensureCacheHydrated(
async () => {
parseCount++
return []
},
() => [corrected],
'cfg',
() => true,
)
const refreshedDay = hydrated.days.find(entry => entry.date === date)
expect(parseCount).toBe(1)
expect(refreshedDay?.providers.grok?.cost).toBe(2)
expect(refreshedDay?.cost).toBe(2)
expect(JSON.parse(await readFile(oldPath, 'utf8'))).toEqual(oldCache)
})
})

View file

@ -0,0 +1,184 @@
import { readFileSync } from 'node:fs'
import { PassThrough } from 'node:stream'
import React, { useEffect } from 'react'
import { Text } from 'ink'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { RESIZE_DEBOUNCE_MS, createDebouncedResizeStream, renderDebouncedInteractive } from '../src/dashboard.js'
import { stripSyncUpdateEscapes } from '../src/ink-win.js'
function makeTerminal(columns = 100, rows = 24): PassThrough & NodeJS.WriteStream {
const terminal = new PassThrough() as PassThrough & NodeJS.WriteStream
terminal.isTTY = true
terminal.columns = columns
terminal.rows = rows
return terminal
}
function paintedFrames(writes: string[]): string[] {
return writes
.map(chunk => stripSyncUpdateEscapes(chunk))
.flatMap(chunk => chunk.match(/FRAME:[^\r\n]*/g) ?? [])
}
describe('interactive dashboard resize stream', () => {
afterEach(() => vi.useRealTimers())
it('does not intercept writes or parse synchronized-update frames', () => {
const source = readFileSync(new URL('../src/dashboard.tsx', import.meta.url), 'utf8')
expect(source).not.toContain('suppressingFrame')
expect(source).not.toContain('capturingResizeWrites')
expect(source).not.toContain('finalFramePreamble')
expect(source).not.toContain('indexOf(BSU)')
expect(source).not.toContain('indexOf(ESU)')
expect(source).not.toContain('process.stdout.prependListener')
})
it('publishes one settled paint after a resize burst', async () => {
vi.useFakeTimers()
const terminal = makeTerminal()
const writes: string[] = []
terminal.on('data', chunk => writes.push(String(chunk)))
const app = renderDebouncedInteractive(terminal, size => (
React.createElement(Text, null, `FRAME:${size.columns}x${size.rows}`)
), {
interactive: true,
patchConsole: false,
alternateScreen: true,
})
await vi.advanceTimersByTimeAsync(100)
writes.length = 0
terminal.columns = 99
terminal.emit('resize')
await vi.advanceTimersByTimeAsync(50)
terminal.columns = 98
terminal.emit('resize')
await vi.advanceTimersByTimeAsync(50)
terminal.columns = 97
terminal.rows = 30
terminal.emit('resize')
await vi.advanceTimersByTimeAsync(RESIZE_DEBOUNCE_MS + 100)
const frames = paintedFrames(writes)
expect(frames.filter(frame => frame !== 'FRAME:97x30'), 'a resize burst must not paint intermediate sizes').toEqual([])
expect(frames).toContain('FRAME:97x30')
app.unmount()
app.dispose()
await vi.runAllTimersAsync()
await app.waitUntilExit()
})
it('paints a mid-burst state update when the burst nets to no size change', async () => {
vi.useFakeTimers()
const terminal = makeTerminal()
const writes: string[] = []
terminal.on('data', chunk => writes.push(String(chunk)))
let updateVisibleState = () => {}
const StatefulProbe = ({ size }: { size: { columns: number; rows: number } }) => {
const [revision, setRevision] = React.useState(0)
updateVisibleState = () => setRevision(value => value + 1)
return React.createElement(Text, null, `FRAME:revision=${revision}:size=${size.columns}x${size.rows}`)
}
const app = renderDebouncedInteractive(terminal, size => React.createElement(StatefulProbe, { size }), {
interactive: true,
patchConsole: false,
alternateScreen: true,
})
await vi.advanceTimersByTimeAsync(100)
writes.length = 0
terminal.columns = 80
terminal.emit('resize')
updateVisibleState()
terminal.columns = 100
terminal.emit('resize')
await vi.advanceTimersByTimeAsync(RESIZE_DEBOUNCE_MS + 100)
expect(paintedFrames(writes).some(frame => frame.includes('revision=1')), 'a mid-burst state update must reach the terminal even when net size is unchanged').toBe(true)
app.unmount()
app.dispose()
await vi.runAllTimersAsync()
await app.waitUntilExit()
})
it('paints a state update after a spurious identical-dimension SIGWINCH', async () => {
vi.useFakeTimers()
const terminal = makeTerminal()
const writes: string[] = []
terminal.on('data', chunk => writes.push(String(chunk)))
let updateVisibleState = () => {}
const StatefulProbe = ({ size }: { size: { columns: number; rows: number } }) => {
const [revision, setRevision] = React.useState(0)
updateVisibleState = () => setRevision(value => value + 1)
return React.createElement(Text, null, `FRAME:revision=${revision}:size=${size.columns}x${size.rows}`)
}
const app = renderDebouncedInteractive(terminal, size => React.createElement(StatefulProbe, { size }), {
interactive: true,
patchConsole: false,
alternateScreen: true,
})
await vi.advanceTimersByTimeAsync(100)
writes.length = 0
terminal.emit('resize')
updateVisibleState()
await vi.advanceTimersByTimeAsync(RESIZE_DEBOUNCE_MS + 100)
expect(paintedFrames(writes).some(frame => frame.includes('revision=1')), 'a state update must still paint after a no-op SIGWINCH').toBe(true)
app.unmount()
app.dispose()
await vi.runAllTimersAsync()
await app.waitUntilExit()
})
it('removes the source relay and cancels pending resize delivery on dispose', async () => {
vi.useFakeTimers()
const terminal = makeTerminal()
const renderedSizes: Array<{ columns: number; rows: number }> = []
const Probe = ({ size }: { size: { columns: number; rows: number } }) => {
useEffect(() => {
renderedSizes.push(size)
}, [size])
return React.createElement(Text, null, `FRAME:${size.columns}x${size.rows}`)
}
const app = renderDebouncedInteractive(terminal, size => React.createElement(Probe, { size }), {
interactive: true,
patchConsole: false,
})
await vi.advanceTimersByTimeAsync(100)
renderedSizes.length = 0
terminal.columns = 90
terminal.emit('resize')
app.unmount()
app.dispose()
await vi.runAllTimersAsync()
await app.waitUntilExit()
await vi.advanceTimersByTimeAsync(RESIZE_DEBOUNCE_MS)
expect(renderedSizes).toEqual([])
expect(terminal.listenerCount('resize')).toBe(0)
})
it('disposes a stream that never rendered', () => {
const terminal = makeTerminal()
const stdout = createDebouncedResizeStream(terminal, RESIZE_DEBOUNCE_MS)
expect(terminal.listenerCount('resize')).toBe(1)
stdout.dispose()
expect(terminal.listenerCount('resize')).toBe(0)
const resize = vi.fn()
stdout.on('resize', resize)
terminal.emit('resize')
expect(resize).not.toHaveBeenCalled()
expect(terminal.listenerCount('resize')).toBe(0)
})
})

View file

@ -421,6 +421,98 @@ describe('interactive terminal rendering', () => {
expect(INTERACTIVE_RENDER_OPTIONS).toMatchObject({ alternateScreen: true })
})
it.each([
{ columns: 42, expected: '! 10: codeburn models --unpriced' },
{ columns: 43, expected: '! 10: codeburn models --unpriced' },
{ columns: 44, expected: '! 10: codeburn models --unpriced' },
{ columns: 80, expected: '! 10 unpriced: codeburn models --unpriced' },
])('shows an actionable unpriced-model command in a real $columns-column Ink frame', async ({ columns, expected }) => {
const stdin = new PassThrough() as PassThrough & NodeJS.ReadStream
const stdout = new PassThrough() as PassThrough & NodeJS.WriteStream
stdin.isTTY = true
stdin.setRawMode = () => stdin
stdin.ref = () => stdin
stdin.unref = () => stdin
stdout.isTTY = true
stdout.columns = columns
stdout.rows = 100
const frames: string[] = []
stdout.on('data', chunk => frames.push(stripAnsi(String(chunk))))
const session = makeSession('unpriced-session', 0)
for (let index = 0; index < 10; index++) {
const model = `vendor-${index}/unknown-model-${index}-969`
session.modelBreakdown[model] = {
calls: 1,
costUSD: 0,
savingsUSD: 0,
tokens: {
inputTokens: 1_000,
outputTokens: 100,
cacheCreationInputTokens: 0,
cacheReadInputTokens: 0,
cachedInputTokens: 0,
reasoningTokens: 0,
webSearchRequests: 0,
},
}
}
const app = render(React.createElement(InteractiveDashboard, {
initialProjects: [makeProject('unpriced-project', [session])],
initialPeriod: 'today',
initialProvider: 'all',
refreshSeconds: 0,
windowColumns: columns,
}), { stdin, stdout, debug: true, interactive: true, patchConsole: false })
onTestFinished(() => app.unmount())
await app.waitUntilRenderFlush()
const frame = frames.filter(value => value.trim()).at(-1) ?? ''
expect(frame).toContain(expected)
})
it('labels claude.ai connector remediation as a manual action', async () => {
const stdin = new PassThrough() as PassThrough & NodeJS.ReadStream
const stdout = new PassThrough() as PassThrough & NodeJS.WriteStream
stdin.isTTY = true
stdin.setRawMode = () => stdin
stdin.ref = () => stdin
stdin.unref = () => stdin
stdout.isTTY = true
stdout.columns = 120
stdout.rows = 50
const frames: string[] = []
stdout.on('data', chunk => frames.push(stripAnsi(String(chunk))))
const inventory = Array.from({ length: 20 }, (_, i) => `mcp__claude_ai_Google_Calendar__t${i}`)
const sessions = ['connector-a', 'connector-b'].map((id, index) => {
const session = makeSession(id, 91.337 + index)
session.mcpInventory = inventory
return session
})
const app = render(React.createElement(InteractiveDashboard, {
initialProjects: [makeProject('connector-manual-action', sessions)],
initialPeriod: 'today',
initialProvider: 'all',
refreshSeconds: 0,
windowColumns: 120,
}), { stdin, stdout, debug: true, interactive: true, patchConsole: false })
onTestFinished(() => app.unmount())
await app.waitUntilRenderFlush()
stdin.write('o')
let frame = ''
for (let i = 0; i < 100 && !frame.includes('Manual action'); i++) {
await new Promise(resolve => setTimeout(resolve, 10))
frame = frames.filter(value => value.trim()).at(-1) ?? ''
}
expect(frame).toContain('Manual action')
expect(frame).toContain('claude.ai Google Calendar')
expect(frame).not.toContain('Ask Claude in the current session')
})
it('leaves resize frame synchronization entirely to Ink', () => {
const source = readFileSync(new URL('../src/dashboard.tsx', import.meta.url), 'utf8')
expect(source).not.toContain('process.stdout.write(BSU)')
@ -691,7 +783,12 @@ describe('InteractiveDashboard refresh', () => {
})
it('keeps Optimize mounted without a loading frame when auto-refresh fires', async () => {
vi.useFakeTimers()
// The Optimize scan (`o`) does real fs I/O (readdir/stat) that only
// resolves on a real event-loop turn. Leave setImmediate/nextTick/Date
// real (Date stays real so the wait loop below can use a genuine
// wall-clock deadline) and fake only what the 60s auto-refresh
// interval needs.
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'] })
const stdin = new PassThrough() as PassThrough & NodeJS.ReadStream
const stdout = new PassThrough() as PassThrough & NodeJS.WriteStream
stdin.isTTY = true
@ -738,12 +835,20 @@ describe('InteractiveDashboard refresh', () => {
expect(activityHeader.indexOf('turns') + 'turns'.length).toBe(activityRow.indexOf('12') + '12'.length)
expect(activityHeader.indexOf('1-shot') + '1-shot'.length).toBe(activityRow.indexOf('50%') + '50%'.length)
stdin.write('o')
for (let i = 0; i < 20 && !frames.some(frame => frame.includes('Token estimates are approximate.')); i++) {
// The scan does real fs work, so wait on real event-loop turns
// (setImmediate is left un-faked above) rather than counting fake-timer
// hops, bounded by a real wall-clock deadline.
const realDeadline = Date.now() + 10_000
while (!frames.some(frame => frame.includes('Savings: ~'))) {
if (Date.now() > realDeadline) {
throw new Error('Timed out waiting for the Optimize scan to render "Savings: ~"')
}
await new Promise(resolve => setImmediate(resolve))
await vi.advanceTimersByTimeAsync(50)
}
const beforeRefresh = frames.filter(frame => frame.trim()).at(-1) ?? ''
expect(beforeRefresh).toContain('CodeBurn Optimize')
expect(beforeRefresh).toContain('Token estimates are approximate.')
expect(beforeRefresh).toContain('CodeBurn Optimize')
frames.length = 0
await vi.advanceTimersByTimeAsync(60_000)
@ -752,10 +857,10 @@ describe('InteractiveDashboard refresh', () => {
const frame = frames.filter(value => value.trim()).at(-1) ?? beforeRefresh
expect(frame).toBe(beforeRefresh)
expect(frame).toContain('CodeBurn Optimize')
expect(frame).toContain('Token estimates are approximate.')
expect(frame).toContain('CodeBurn Optimize')
expect(frame).toContain('b back')
expect(frame).not.toContain('Loading Today')
expect(frame).not.toContain('Scanning Today')
})
}, 30_000)
})

35
tests/fixtures/dsh/bash-tool-turn.jsonl vendored Normal file
View file

@ -0,0 +1,35 @@
{"type":"session","version":0,"id":"e128dda9-ed11-4868-8266-0ef90d03c3d6","createdAt":1783352050748,"cwd":"/home/u/proj","delegationDepth":0}
{"type":"agent/inbox/spliced","seq":0,"time":1785498771334,"data":{"target":"next-turn","start":0,"inserted":[{"content":[{"type":"text","text":"Use the bash tool to run exactly: echo TERMINAL_OK. Then reply with the single word DONE and stop."}],"source":{"kind":"user"},"role":"user","id":"38694db6-921d-41fd-b1fb-3b0c40caf67c"}]}}
{"type":"turn/start","seq":1,"time":1785821375023,"data":{"turn":1}}
{"type":"agent/inbox/spliced","seq":2,"time":1785821375023,"data":{"target":"next-turn","start":0,"removedCount":1,"inserted":[]}}
{"type":"step/start","seq":3,"time":1783352050755,"data":{"turn":1,"step":1}}
{"type":"user/message","seq":4,"time":1785498771360,"data":{"content":[{"type":"text","text":"Use the bash tool to run exactly: echo TERMINAL_OK. Then reply with the single word DONE and stop."}],"source":{"kind":"user"},"role":"user","id":"38694db6-921d-41fd-b1fb-3b0c40caf67c"},"surfaceOp":"append"}
{"type":"user/message","seq":5,"time":1785730424635,"data":{"content":[{"type":"text","text":"Current runtime context. This snapshot supersedes earlier runtime-context snapshots.\n\nCurrent DSH file policy: danger-full-access. The DSH file sandbox does not restrict file modifications by available operations.\n\nApproval prompts are disabled in this session: actions that require approval are rejected automatically — do not request sandbox escalation (do not set `sandbox_permissions`)."}],"source":{"kind":"plugin","plugin":"@deepseek-ai/dsh-system-prompt","form":"snapshot","sections":[{"name":"sandbox:policy","text":"Current DSH file policy: danger-full-access. The DSH file sandbox does not restrict file modifications by available operations."},{"name":"approval:policy","text":"Approval prompts are disabled in this session: actions that require approval are rejected automatically — do not request sandbox escalation (do not set `sandbox_permissions`)."}]},"role":"user","id":"80474489-442a-4e98-beef-df6cd1e85870"},"surfaceOp":"append"}
{"type":"session/title","seq":6,"time":1785730424635,"data":{"title":"Use the bash tool to","messageSeqs":[4],"source":{"kind":"fallback"}}}
{"type":"request/header","seq":7,"time":1785498771361,"data":{"header":{"config":{"provider":"deepseek-official","model":"deepseek-v4-flash"},"system":"you are dsh","tools":[]},"reason":"initial"}}
{"type":"request/context","seq":8,"time":1785730424636,"data":{"provider":"deepseek-official","model":"deepseek-v4-flash"}}
{"type":"assistant/chunk","seq":9,"time":1783352051590,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}}
{"type":"reasoning-chunks","seq0":10,"time0":1783352051618,"data":{"turn":1,"step":1,"index":0,"dt":[0,1,0,0,26,30,0,0,1,0,27,1,0,0,0,86,1],"texts":["The"," user"," wants"," me"," to"," run"," a"," simple"," bash"," command"," and"," then"," reply"," with"," \"","D","ONE","\"."]}}
{"type":"assistant/chunk","seq":28,"time":1783352051820,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":1,"blockType":"tool-call"}}}
{"type":"tool-call-chunks","seq0":29,"time0":1783352051820,"data":{"turn":1,"step":1,"index":1,"dt":[0,0,0,28,0,0,0,29,0,0,28,1,0,29,0,0,0,32,0,0,0,0,0,74,0,0,13,0,63,1],"id":"call_00_fkbBRJsUrGKd1pWVc4Gn8233","name":"bash","args":["","{","\"","command","\"",": ","\"","echo"," TER","MIN","AL","_OK","\"",", ","\"","description","\"",": ","\"","E","cho"," TER","MIN","AL","_OK"," to"," verify"," terminal"," access","\"","}"]}}
{"type":"assistant/chunk","seq":60,"time":1783352052118,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The user wants me to run a simple bash command and then reply with \"DONE\"."}}}}
{"type":"assistant/chunk","seq":61,"time":1783352052118,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":1,"block":{"type":"tool-call","id":"call_00_fkbBRJsUrGKd1pWVc4Gn8233","name":"bash","arguments":"{\"command\": \"echo TERMINAL_OK\", \"description\": \"Echo TERMINAL_OK to verify terminal access\"}"}}}}
{"type":"assistant/chunk","seq":62,"time":1785498771373,"data":{"turn":1,"step":1,"chunk":{"type":"usage","usage":{"inputTokens":2877,"outputTokens":90,"cacheReadTokens":0,"reasoningTokens":18}}}}
{"type":"assistant/chunk","seq":63,"time":1785730424645,"data":{"turn":1,"step":1,"chunk":{"type":"finish","reason":{"kind":"tool-calls"}}}}
{"type":"assistant/message","seq":64,"time":1785730424645,"data":{"turn":1,"step":1,"message":{"role":"assistant","content":[{"type":"reasoning","text":"The user wants me to run a simple bash command and then reply with \"DONE\"."},{"type":"tool-call","id":"call_00_fkbBRJsUrGKd1pWVc4Gn8233","name":"bash","arguments":"{\"command\": \"echo TERMINAL_OK\", \"description\": \"Echo TERMINAL_OK to verify terminal access\"}"}],"source":{"kind":"model","provider":"deepseek-official","model":"deepseek-v4-flash"},"id":"0a855246-fbf6-4f91-87b4-c6f1889effe7"},"usage":{"inputTokens":2877,"outputTokens":90,"cacheReadTokens":0,"reasoningTokens":18}},"sourceEventSeqs":[9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63],"surfaceOp":"append"}
{"type":"tool/call","seq":65,"time":1785730424646,"data":{"turn":1,"step":1,"callId":"call_00_fkbBRJsUrGKd1pWVc4Gn8233","name":"bash","arguments":"{\"command\": \"echo TERMINAL_OK\", \"description\": \"Echo TERMINAL_OK to verify terminal access\"}"}}
{"type":"tool/result","seq":66,"time":1785730424665,"data":{"turn":1,"step":1,"message":{"source":{"kind":"tool","callId":"call_00_fkbBRJsUrGKd1pWVc4Gn8233"},"content":[{"type":"tool-result","toolCallId":"call_00_fkbBRJsUrGKd1pWVc4Gn8233","content":[{"type":"text","text":"TERMINAL_OK\n"}],"isError":false}],"role":"user","id":"908ca4f5-efbb-443b-9b07-acbf25edf954"}},"sourceEventSeqs":[65],"surfaceOp":"append"}
{"type":"step/end","seq":67,"time":1785730424665,"data":{"turn":1,"step":1}}
{"type":"step/start","seq":68,"time":1785730424676,"data":{"turn":1,"step":2}}
{"type":"assistant/chunk","seq":69,"time":1783352052780,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}}
{"type":"reasoning-chunks","seq0":70,"time0":1783352052809,"data":{"turn":1,"step":2,"index":0,"dt":[29,0,0,29,0,0,0,0,0,28,1,28,1,0,0,32,0,0,0,0,0],"texts":["The"," command"," ran"," successfully"," and"," output"," \"","TER","MIN","AL","_OK","\"."," I"," should"," now"," reply"," with"," just"," \"","D","ONE","\"."]}}
{"type":"assistant/chunk","seq":92,"time":1783352052986,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":1,"blockType":"text"}}}
{"type":"assistant/chunk","seq":93,"time":1783352052986,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":"D"}}}
{"type":"assistant/chunk","seq":94,"time":1783352052986,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":"ONE"}}}
{"type":"assistant/chunk","seq":95,"time":1783352052986,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The command ran successfully and output \"TERMINAL_OK\". I should now reply with just \"DONE\"."}}}}
{"type":"assistant/chunk","seq":96,"time":1783352052986,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":1,"block":{"type":"text","text":"DONE"}}}}
{"type":"assistant/chunk","seq":97,"time":1785498771406,"data":{"turn":1,"step":2,"chunk":{"type":"usage","usage":{"inputTokens":168,"outputTokens":25,"cacheReadTokens":2816,"reasoningTokens":22}}}}
{"type":"assistant/chunk","seq":98,"time":1785730424681,"data":{"turn":1,"step":2,"chunk":{"type":"finish","reason":{"kind":"stop"}}}}
{"type":"assistant/message","seq":99,"time":1785730424681,"data":{"turn":1,"step":2,"message":{"role":"assistant","content":[{"type":"reasoning","text":"The command ran successfully and output \"TERMINAL_OK\". I should now reply with just \"DONE\"."},{"type":"text","text":"DONE"}],"source":{"kind":"model","provider":"deepseek-official","model":"deepseek-v4-flash"},"id":"aa705bf0-9b5b-4af3-9763-dbf93c98e4c4"},"usage":{"inputTokens":168,"outputTokens":25,"cacheReadTokens":2816,"reasoningTokens":22}},"sourceEventSeqs":[69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98],"surfaceOp":"append"}
{"type":"step/end","seq":100,"time":1785730424682,"data":{"turn":1,"step":2}}
{"type":"turn/end","seq":101,"time":1785730424682,"data":{"turn":1,"reason":{"kind":"completed"}}}

View file

@ -45,7 +45,7 @@ function apiCall(options: {
}
}
function project(sessions: Array<{ id: string; project?: string; calls: ParsedApiCall[] }>): ProjectSummary {
function project(sessions: Array<{ id: string; project?: string; title?: string; lastTimestamp?: string; calls: ParsedApiCall[] }>): ProjectSummary {
return {
project: 'demo',
projectPath: '/repos/demo',
@ -56,8 +56,9 @@ function project(sessions: Array<{ id: string; project?: string; calls: ParsedAp
sessions: sessions.map(session => ({
sessionId: session.id,
project: session.project ?? 'demo',
title: session.title,
firstTimestamp: session.calls[0]?.timestamp ?? '',
lastTimestamp: session.calls.at(-1)?.timestamp ?? '',
lastTimestamp: session.lastTimestamp ?? session.calls.at(-1)?.timestamp ?? '',
totalCostUSD: session.calls.reduce((sum, call) => sum + call.costUSD, 0),
totalSavingsUSD: 0,
totalInputTokens: session.calls.reduce((sum, call) => sum + call.usage.inputTokens, 0),
@ -107,6 +108,131 @@ describe('granular history', () => {
expect(granularBucketMinutes(range(24 * 30))).toBe(1440)
})
it('prefers a sanitised session title and preserves the exact project fallback when it is missing or blank', () => {
const timestamp = '2026-07-15T12:05:00.000Z'
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
const history = buildGranularHistory([project([
{ id: 'session-titled-123456', title: 'Refactor billing module', calls: [apiCall({ timestamp, cost: 1 })] },
{ id: 'session-absent-123457', calls: [apiCall({ timestamp, cost: 1 })] },
{ id: 'session-empty-123458', title: '', calls: [apiCall({ timestamp, cost: 1 })] },
{ id: 'session-blank-123459', title: ' \t\n ', calls: [apiCall({ timestamp, cost: 1 })] },
])], { start, end }, end)
expect(history.sessionSeries.map(series => series.label)).toEqual([
'sessio…3456 (claude) · Refactor billing module',
'sessio…3457 (claude) · repos/demo',
'sessio…3458 (claude) · repos/demo',
'sessio…3459 (claude) · repos/demo',
])
})
it('keeps identical session titles distinguishable with the short session id', () => {
const timestamp = '2026-07-15T12:05:00.000Z'
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
const history = buildGranularHistory([project([
{ id: 'session-111111', title: 'Refactor billing module', calls: [apiCall({ timestamp, cost: 1 })] },
{ id: 'session-222222', title: 'Refactor billing module', calls: [apiCall({ timestamp, cost: 1 })] },
])], { start, end }, end)
expect(history.sessionSeries.map(series => series.id)).toEqual(['session_0', 'session_1'])
expect(history.sessionSeries.map(series => series.label)).toEqual([
'sessio…1111 (claude) · Refactor billing module',
'sessio…2222 (claude) · Refactor billing module',
])
expect(new Set(history.sessionSeries.map(series => series.label)).size).toBe(2)
})
it('keeps the disambiguator inside the visible legend prefix for long shared title prefixes', () => {
const timestamp = '2026-07-15T12:05:00.000Z'
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
const sharedPrefix = 'same long title prefix '.repeat(5)
const history = buildGranularHistory([project([
{ id: 'a1b2c3-session-7f01', title: `${sharedPrefix}alpha`, calls: [apiCall({ timestamp, cost: 1 })] },
{ id: 'd4e5f6-session-8a02', title: `${sharedPrefix}beta`, calls: [apiCall({ timestamp, cost: 1 })] },
])], { start, end }, end)
// 160px at the chart's 10px font fits roughly 31-32 lowercase glyphs;
// compare a conservative prefix that must be visible in that budget.
const visibleCharacterBudget = 24
const visiblePrefixes = history.sessionSeries.map(series => series.label.slice(0, visibleCharacterBudget))
expect(new Set(visiblePrefixes).size).toBe(2)
expect(history.sessionSeries.map(series => series.label)).toEqual(expect.arrayContaining([
expect.stringMatching(/^a1b2c3…7f01 \(claude\) · /),
expect.stringMatching(/^d4e5f6…8a02 \(claude\) · /),
]))
})
it('sanitises control characters and ANSI escapes in session titles', () => {
const timestamp = '2026-07-15T12:05:00.000Z'
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
const history = buildGranularHistory([project([{
id: 'session-sanitised-123456',
title: '\x1b[31mRefactor\x1b[0m\t billing\nmodule\x00',
calls: [apiCall({ timestamp, cost: 1 })],
}])], { start, end }, end)
expect(history.sessionSeries[0]?.label).toBe('sessio…3456 (claude) · Refactor billing module')
expect(history.sessionSeries[0]?.label).not.toContain('\x1b')
expect(history.sessionSeries[0]?.label).not.toContain('\x00')
})
it('caps over-long session titles before putting them in the legend label', () => {
const timestamp = '2026-07-15T12:05:00.000Z'
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
const history = buildGranularHistory([project([{
id: 'session-long-title-123456',
title: 'x'.repeat(200),
calls: [apiCall({ timestamp, cost: 1 })],
}])], { start, end }, end)
expect(history.sessionSeries[0]?.label).toBe('sessio…3456 (claude) · ' + 'x'.repeat(80))
})
it('caps session titles by code point without splitting an emoji', () => {
const timestamp = '2026-07-15T12:05:00.000Z'
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
const title = 'x'.repeat(79) + '😀' + ' after the boundary'
const history = buildGranularHistory([project([{
id: 'session-emoji-title-123456',
title,
calls: [apiCall({ timestamp, cost: 1 })],
}])], { start, end }, end)
const label = history.sessionSeries[0]?.label ?? ''
const titlePart = label.slice(label.indexOf(' · ') + 3)
expect(titlePart).toBe('x'.repeat(79) + '😀')
expect([...titlePart]).toEqual([...('x'.repeat(79) + '😀')])
})
it('prefers a title from any duplicate session summary sharing a key', () => {
const timestamp = '2026-07-15T12:05:00.000Z'
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
const history = buildGranularHistory([project([
{
id: 'session-cache-collision-123456',
title: 'A stale session title',
lastTimestamp: '2026-07-15T12:06:00.000Z',
calls: [apiCall({ timestamp, cost: 1 })],
},
{
id: 'session-cache-collision-123456',
title: 'Z recovered session title',
lastTimestamp: '2026-07-15T12:07:00.000Z',
calls: [apiCall({ timestamp, cost: 2 })],
},
])], { start, end }, end)
expect(history.sessionSeries).toHaveLength(1)
expect(history.sessionSeries[0]?.label).toBe('sessio…3456 (claude) · Z recovered session title')
})
it('fills idle buckets and keeps separate model and session lines from real call timestamps', () => {
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
@ -143,8 +269,8 @@ describe('granular history', () => {
// Labels use the real projectPath's last two segments, not the sanitized
// project name.
const alpha = history.sessionSeries.find(series => series.label === 'repos/demo · sessio…3456 (claude)')!
const beta = history.sessionSeries.find(series => series.label === 'repos/demo · sessio…4321 (codex)')!
const alpha = history.sessionSeries.find(series => series.label === 'sessio…3456 (claude) · repos/demo')!
const beta = history.sessionSeries.find(series => series.label === 'sessio…4321 (codex) · repos/demo')!
expect(sumSeries(history, 'sessions', alpha.id, 'cost')).toBe(1.75)
expect(sumSeries(history, 'sessions', beta.id, 'tokens')).toBe(300)
// Cache reads are intentionally not folded into the browser's Tokens line.
@ -216,11 +342,48 @@ describe('granular history', () => {
expect(history.sessionSeries).toHaveLength(2)
expect(history.sessionSeries.map(series => series.label)).toEqual(expect.arrayContaining([
expect.stringContaining('alpha ·'),
expect.stringContaining('beta ·'),
expect.stringContaining('repos/alpha'),
expect.stringContaining('repos/beta'),
]))
})
it('adds the project only when identical title and session id labels collide', () => {
const timestamp = '2026-07-15T12:05:00.000Z'
const alpha = project([{ id: 'shared-session-123456', title: 'Same task', calls: [apiCall({ timestamp, cost: 1 })] }])
const beta = project([{ id: 'shared-session-123456', title: 'Same task', calls: [apiCall({ timestamp, cost: 1 })] }])
alpha.projectPath = '/repos/alpha'
beta.projectPath = '/repos/beta'
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
const history = buildGranularHistory([alpha, beta], { start, end }, end)
const labels = history.sessionSeries.map(series => series.label)
expect(labels).toEqual([
'shared…3456 (claude) · Same task · repos/alpha',
'shared…3456 (claude) · Same task · repos/beta',
])
expect(new Set(labels).size).toBe(2)
})
it('keeps all labels unique even when a title matches another label shape', () => {
const timestamp = '2026-07-15T12:05:00.000Z'
const alpha = project([{ id: 'sameid', title: 'Task', calls: [apiCall({ timestamp, cost: 1 })] }])
const beta = project([{ id: 'sameid', title: 'Task', calls: [apiCall({ timestamp, cost: 1 })] }])
const shaped = project([{ id: 'sameid', title: 'Task · repos/alpha (claude)', calls: [apiCall({ timestamp, cost: 1 })] }])
alpha.projectPath = '/repos/alpha (claude)'
beta.projectPath = '/repos/beta (claude)'
shaped.projectPath = '/other/shaped'
const start = new Date('2026-07-15T00:00:00.000Z')
const end = new Date('2026-07-15T23:59:59.999Z')
const labelsFor = (projects: ProjectSummary[]) => buildGranularHistory(projects, { start, end }, end).sessionSeries.map(series => series.label)
const labels = labelsFor([alpha, beta, shaped])
expect(new Set(labels).size).toBe(labels.length)
expect(labels.slice().sort()).toEqual(labelsFor([shaped, beta, alpha]).slice().sort())
})
it('aligns quarter-hour buckets to local wall time in a fractional-offset timezone', () => {
const previousTz = process.env['TZ']
process.env['TZ'] = 'Asia/Kathmandu'

View file

@ -0,0 +1,279 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mkdir, rm, writeFile } from 'fs/promises'
import { join } from 'path'
import { calculateCost } from '../src/models.js'
import { clearSessionCache, parseAllSessions } from '../src/parser.js'
// `chooseAuthoritativeModel` branches on whether a modelUsage id resolves to a
// price, so pin the reporter's real id from #998 as unpriced here rather than
// letting the bundled LiteLLM snapshot decide it: xAI pricing landing upstream
// would otherwise silently flip these assertions. Only this lookup is stubbed,
// so `calculateCost` still prices off the real tables.
vi.mock('../src/models.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../src/models.js')>()
return {
...actual,
getModelCosts: (model: string) => (model === 'grok-4.6-build' ? null : actual.getModelCosts(model)),
}
})
// The exported Grok provider resolves GROK_HOME when its singleton is created,
// before the test body runs. Set the root during module hoisting, then re-assert
// the call-time cache/env values in beforeEach after env-isolation runs.
const testRoot = vi.hoisted(() => {
const root = `${process.env['TMPDIR'] || '/tmp'}/grok-pipeline-${process.pid}-${Date.now()}`
process.env['GROK_HOME'] = `${root}/grok`
return root
})
const GROK_HOME = join(testRoot, 'grok')
const CACHE_DIR = join(testRoot, 'cache')
type UsageOptions = {
input: number
output: number
cacheRead?: number
cacheCreation?: number
reasoning?: number
model?: string
modelUsage?: Record<string, Record<string, unknown>>
}
type StreamingTurn = {
promptId: string
totals: number[]
}
type CompletedTurn = {
promptId?: string
usage: Record<string, unknown>
}
function usage(opts: UsageOptions): Record<string, unknown> {
const cacheRead = opts.cacheRead ?? 0
const cacheCreation = opts.cacheCreation ?? 0
const reasoning = opts.reasoning ?? 0
const model = opts.model ?? 'grok-build'
const singleModel = {
inputTokens: opts.input,
outputTokens: opts.output,
cachedReadTokens: cacheRead,
cacheCreationTokens: cacheCreation,
reasoningTokens: reasoning,
}
return {
inputTokens: opts.input,
outputTokens: opts.output,
cachedReadTokens: cacheRead,
cacheCreationTokens: cacheCreation,
reasoningTokens: reasoning,
modelUsage: opts.modelUsage ?? { [model]: singleModel },
}
}
async function writeSession(
record: Record<string, unknown>,
uuid = '019edf9c-0000-7000-8000-000000000101',
options: { turns?: StreamingTurn[]; completedTurns?: CompletedTurn[] } = {},
): Promise<void> {
const cwd = '/Users/test/grok-pipeline'
const dir = join(GROK_HOME, 'sessions', '%2FUsers%2Ftest%2Fgrok-pipeline', uuid)
await mkdir(dir, { recursive: true })
await writeFile(join(dir, 'summary.json'), JSON.stringify({
info: { id: uuid, cwd },
created_at: '2026-08-17T09:00:00.000Z',
updated_at: '2026-08-17T09:05:00.000Z',
current_model_id: 'grok-build',
session_summary: 'pipeline regression',
}))
await writeFile(join(dir, 'signals.json'), JSON.stringify({
primaryModelId: 'grok-build',
modelsUsed: ['grok-build'],
}))
const completedTurns = options.completedTurns ?? [{ promptId: 'pipeline-turn', usage: record }]
const lines: Record<string, unknown>[] = []
for (const turn of options.turns ?? []) {
for (const totalTokens of turn.totals) {
lines.push({
method: 'session/update',
params: {
sessionId: uuid,
_meta: { eventId: `stream-${turn.promptId}-${totalTokens}`, totalTokens, promptId: turn.promptId },
},
})
}
}
for (const [index, completed] of completedTurns.entries()) {
lines.push({
method: 'session/update',
params: {
sessionId: uuid,
update: {
sessionUpdate: 'turn_completed',
...(completed.promptId !== undefined ? { prompt_id: completed.promptId } : {}),
usage: completed.usage,
},
_meta: { eventId: `completed-${index}` },
},
})
}
await writeFile(join(dir, 'updates.jsonl'), lines.map(line => JSON.stringify(line)).join('\n') + '\n')
}
async function parseGrokSessions() {
const projects = await parseAllSessions(undefined, 'grok')
return projects.flatMap(project => project.sessions)
}
beforeEach(async () => {
clearSessionCache()
await rm(testRoot, { recursive: true, force: true })
process.env['GROK_HOME'] = GROK_HOME
process.env['CODEBURN_CACHE_DIR'] = CACHE_DIR
})
afterEach(async () => {
clearSessionCache()
await rm(testRoot, { recursive: true, force: true })
})
describe('Grok parser through the session-cache pipeline', () => {
it('keeps reasoning inside output pricing on both cold and warm parses', async () => {
await writeSession(usage({ input: 1000, output: 200, cacheRead: 500, cacheCreation: 100, reasoning: 150 }))
const cold = (await parseGrokSessions())[0]!
const coldCall = cold.turns[0]!.assistantCalls[0]!
clearSessionCache()
const warm = (await parseGrokSessions())[0]!
const warmCall = warm.turns[0]!.assistantCalls[0]!
const expected = calculateCost('grok-build', 400, 200, 100, 500, 0)
expect(cold.apiCalls).toBe(1)
expect(coldCall.costUSD).toBeCloseTo(expected, 12)
expect(warm.apiCalls).toBe(1)
expect(warmCall.costUSD).toBeCloseTo(expected, 12)
expect(warmCall.costUSD).not.toBeCloseTo(calculateCost('grok-build', 400, 350, 100, 500, 0), 12)
// Cost is only half of it: `models` and the audit report sum
// outputTokens + reasoningTokens for the token column. Emitting the
// provider's cache-inclusive output verbatim inflated that column by the
// reasoning tokens even once the cost was right, so pin the split and the
// sum the reports actually render.
const breakdown = Object.values(cold.modelBreakdown)[0]!
expect(breakdown.tokens.outputTokens).toBe(50) // 200 reported - 150 reasoning
expect(breakdown.tokens.reasoningTokens).toBe(150)
expect(breakdown.tokens.outputTokens + breakdown.tokens.reasoningTokens).toBe(200)
})
it('keeps one session call and uses top-level totals for a multi-model record', async () => {
await writeSession(usage({
input: 3000,
output: 300,
cacheRead: 600,
cacheCreation: 100,
reasoning: 30,
modelUsage: {
'grok-4.6-build': {
inputTokens: 2000,
outputTokens: 200,
cachedReadTokens: 500,
cacheCreationTokens: 100,
reasoningTokens: 20,
},
'grok-latest': {
inputTokens: 1000,
outputTokens: 100,
cachedReadTokens: 100,
cacheCreationTokens: 0,
reasoningTokens: 10,
},
},
}), '019edf9c-0000-7000-8000-000000000102')
const cold = (await parseGrokSessions())[0]!
const coldCalls = cold.turns.flatMap(turn => turn.assistantCalls)
const expected = calculateCost('grok-latest', 2300, 300, 100, 600, 0)
expect(cold.turns).toHaveLength(1)
expect(cold.apiCalls).toBe(1)
expect(coldCalls.map(call => call.model)).toEqual(['grok-latest'])
expect(coldCalls.map(call => call.usage.inputTokens)).toEqual([2300])
expect(coldCalls[0]!.usage.outputTokens + coldCalls[0]!.usage.reasoningTokens).toBe(300)
expect(cold.totalCostUSD).toBeCloseTo(expected, 12)
clearSessionCache()
const warm = (await parseGrokSessions())[0]!
expect(warm.turns).toHaveLength(1)
expect(warm.apiCalls).toBe(1)
expect(warm.totalCostUSD).toBeCloseTo(expected, 12)
})
it('falls back to the streaming estimate when usage exists only under modelUsage', async () => {
await writeSession({
modelUsage: {
'grok-4.6-build': {
inputTokens: 1000,
outputTokens: 100,
},
},
}, '019edf9c-0000-7000-8000-000000000103', {
turns: [{ promptId: 'legacy-turn', totals: [1000, 1200] }],
})
const sessions = await parseGrokSessions()
expect(sessions).toHaveLength(1)
const call = sessions[0]!.turns[0]!.assistantCalls[0]!
expect(call.isEstimated).toBe(true)
expect(call.usage.outputTokens).toBe(200)
})
it('uses the final deduplicated record when deciding whether to estimate', async () => {
await writeSession({}, '019edf9c-0000-7000-8000-000000000104', {
turns: [{ promptId: 'superseded-turn', totals: [1000, 1200] }],
completedTurns: [
{ promptId: 'superseded-turn', usage: usage({ input: 1000, output: 100 }) },
{ promptId: 'superseded-turn', usage: {} },
],
})
const sessions = await parseGrokSessions()
expect(sessions).toHaveLength(1)
const call = sessions[0]!.turns[0]!.assistantCalls[0]!
expect(call.isEstimated).toBe(true)
expect(call.usage.outputTokens).toBe(200)
})
it('marks a mixed authoritative session estimated when a streamed turn has no record', async () => {
await writeSession(usage({ input: 1000, output: 100 }), '019edf9c-0000-7000-8000-000000000105', {
turns: [
{ promptId: 'pre-upgrade-turn', totals: [1000, 1400] },
{ promptId: 'authoritative-turn', totals: [1400, 1600] },
],
completedTurns: [{ promptId: 'authoritative-turn', usage: usage({ input: 800, output: 80 }) }],
})
const sessions = await parseGrokSessions()
expect(sessions).toHaveLength(1)
const call = sessions[0]!.turns[0]!.assistantCalls[0]!
expect(call.isEstimated).toBe(true)
expect(call.usage.inputTokens).toBe(800)
expect(call.usage.outputTokens + call.usage.reasoningTokens).toBe(80)
})
it('clamps reasoning to reported output before the real pipeline prices the call', async () => {
await writeSession(usage({ input: 1000, output: 100, cacheRead: 500, cacheCreation: 100, reasoning: 250 }), '019edf9c-0000-7000-8000-000000000106')
const sessions = await parseGrokSessions()
const call = sessions[0]!.turns[0]!.assistantCalls[0]!
const expected = calculateCost('grok-build', 400, 100, 100, 500, 0)
expect(call.usage.outputTokens).toBe(0)
expect(call.usage.reasoningTokens).toBe(100)
expect(call.usage.outputTokens + call.usage.reasoningTokens).toBe(100)
expect(call.costUSD).toBeCloseTo(expected, 12)
clearSessionCache()
const warmCall = (await parseGrokSessions())[0]!.turns[0]!.assistantCalls[0]!
expect(warmCall.costUSD).toBeCloseTo(expected, 12)
})
})

View file

@ -1,10 +1,14 @@
import { describe, it, expect } from 'vitest'
import { describe, it, expect, vi } from 'vitest'
import {
aggregateMcpCoverage,
buildOptimizeJsonReport,
classTotals,
findingClass,
detectMcpProfileAdvisor,
detectMcpToolCoverage,
estimateMcpSchemaCost,
runOptimize,
} from '../src/optimize.js'
import type {
ClassifiedTurn,
@ -313,6 +317,23 @@ describe('estimateMcpSchemaCost', () => {
expect(cost.cacheWriteTokens).toBe(24_000)
})
it('does not count a duplicated server identifier twice', () => {
const inventory = Array.from({ length: 20 }, (_, i) => `mcp__svc__t${i}`)
const sessions = [makeSession({
inventory,
turns: [makeTurn([makeCall({ cacheCreation: 50_000 })])],
})]
const cost = estimateMcpSchemaCost(
{ svc: 20 },
[project(sessions)],
['svc', 'svc'],
)
expect(cost.cacheWriteTokens).toBe(8_000)
expect(cost.effectiveInputTokens).toBe(10_000)
})
it('still works with the single-server signature (backward compat)', () => {
const turns = [makeTurn([makeCall({ cacheCreation: 50_000 })])]
const sessions = [makeSession({
@ -333,6 +354,174 @@ describe('detectMcpToolCoverage', () => {
expect(detectMcpToolCoverage([project([makeSession({})])])).toBeNull()
})
it('keeps claude.ai connector evidence but emits manual guidance instead of a local remove command', () => {
const server = 'claude_ai_Netlify'
const inventory = Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`)
const turns = [makeTurn([makeCall({ cacheCreation: 50_000 })])]
const sessions = [
makeSession({ sessionId: 'a', inventory, turns }),
makeSession({ sessionId: 'b', inventory, turns }),
]
const finding = detectMcpToolCoverage([project(sessions)])
expect(finding).not.toBeNull()
expect(finding!.tokensSaved).toBe(20_000)
// Keep the transcript namespace as evidence, but name the connector the
// way users actually see it in /mcp and claude.ai Settings.
expect(finding!.explanation).toContain(server)
expect(finding!.explanation).toContain('claude.ai Netlify')
expect(finding!.explanation).toContain('/mcp')
expect(finding!.explanation).toContain('claude.ai Settings > Connectors')
expect(finding!.fix.type).toBe('paste')
if (finding!.fix.type === 'paste') {
expect(finding!.fix.destination).toBe('manual')
expect(finding!.fix.text).toContain('/mcp')
expect(finding!.fix.text).toContain('claude.ai Netlify')
expect(finding!.fix.text).not.toContain(server)
expect(finding!.fix.text).toContain('claude.ai Settings > Connectors')
}
expect(JSON.stringify(finding)).not.toContain('claude mcp remove')
expect(finding!.apply).toBeUndefined()
})
it('renders connector-only remediation as a manual action, never an Ask Claude prompt', async () => {
const server = 'claude_ai_Google_Calendar'
const inventory = Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`)
const turns = [makeTurn([makeCall({ cacheCreation: 50_000 })])]
const projects = [project([
makeSession({ sessionId: 'a', inventory, turns }),
makeSession({ sessionId: 'b', inventory, turns }),
])]
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined)
try {
await runOptimize(projects, 'Test period')
const output = log.mock.calls.map(args => args.join(' ')).join('\n')
expect(output).toContain('Manual action')
expect(output).toContain('claude.ai Google Calendar')
expect(output).not.toContain('Ask Claude in the current session')
} finally {
log.mockRestore()
}
})
it('keeps the public optimize JSON envelope while marking connector guidance manual', () => {
const server = 'claude_ai_Slack'
const coverage = [{
server,
toolsAvailable: 20,
toolsInvoked: 0,
unusedTools: Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
invocations: 0,
loadedSessions: 2,
coverageRatio: 0,
}]
const finding = detectMcpToolCoverage([], coverage)!
const report = buildOptimizeJsonReport([], 'Test period', {
findings: [finding],
costRate: 0,
healthScore: 90,
healthGrade: 'A',
})
expect(report.findings[0]).toMatchObject({
id: 'mcp-low-coverage',
tokensSaved: 0,
fix: {
type: 'paste',
destination: 'manual',
text: expect.stringContaining('claude.ai Slack'),
},
})
expect(report.findings[0]).not.toHaveProperty('apply')
expect(report.findings[0]).not.toHaveProperty('applyTokensSaved')
expect(report.findings[0]).not.toHaveProperty('applyTokensSavedByServer')
expect(report.findings[0]).not.toHaveProperty('manualFollowUp')
})
it('intersects globally unused tool identities with each session inventory', () => {
const server = 'filesystem'
const coverage = [{
server,
toolsAvailable: 20,
toolsInvoked: 0,
unusedTools: Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
invocations: 0,
loadedSessions: 2,
coverageRatio: 0,
}]
const sessions = [5, 20].map((count, index) => makeSession({
sessionId: `s${index}`,
inventory: Array.from({ length: count }, (_, i) => `mcp__${server}__t${i}`),
turns: [makeTurn([makeCall({ cacheCreation: 50_000 })])],
}))
const finding = detectMcpToolCoverage([project(sessions)], coverage)
// 5*400 and 20*400, each at 1.25x cache-write pricing.
expect(finding).toMatchObject({ tokensSaved: 12_500 })
expect(finding!.applyTokensSavedByServer?.filesystem).toBe(12_500)
})
it('conserves simultaneous cache-write and cache-read buckets with fractional shares', () => {
const inventory = [
...Array.from({ length: 15 }, (_, i) => `mcp__filesystem__t${i}`),
...Array.from({ length: 11 }, (_, i) => `mcp__claude_ai_Slack__t${i}`),
]
const coverage: McpServerCoverage[] = [
{
server: 'filesystem', toolsAvailable: 15, toolsInvoked: 0,
unusedTools: inventory.slice(0, 15), invocations: 0, loadedSessions: 2, coverageRatio: 0,
},
{
server: 'claude_ai_Slack', toolsAvailable: 11, toolsInvoked: 0,
unusedTools: inventory.slice(15), invocations: 0, loadedSessions: 2, coverageRatio: 0,
},
]
// Duplicate inventory entries must not increase the schema share.
const sessionInventory = [...inventory, inventory[0]!, inventory[15]!]
const sessions = ['a', 'b'].map(sessionId => makeSession({
sessionId,
inventory: sessionInventory,
turns: [makeTurn([makeCall({ cacheCreation: 5_001, cacheRead: 3_333 })])],
}))
const finding = detectMcpToolCoverage([project(sessions)], coverage)!
const total = 2 * (5_001 * 1.25 + 3_333 * 0.10)
const local = total * (15 / 26)
expect(finding.tokensSaved).toBe(Math.round(total))
expect(finding.applyTokensSaved).toBe(Math.round(local))
expect(finding.applyTokensSavedByServer?.filesystem).toBeCloseTo(local, 8)
})
it('pluralises manual guidance when only claude.ai connectors are flagged', () => {
const coverage = ['claude_ai_Slack', 'claude_ai_Google_Calendar'].map(server => ({
server,
toolsAvailable: 20,
toolsInvoked: 0,
unusedTools: Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
invocations: 0,
loadedSessions: 2,
coverageRatio: 0,
}))
const finding = detectMcpToolCoverage([], coverage)
expect(finding).not.toBeNull()
expect(finding!.fix).toMatchObject({
type: 'paste',
destination: 'manual',
label: 'Manage the underused claude.ai connectors where they load:',
})
if (finding!.fix.type === 'paste') {
expect(finding!.fix.text).toContain('manage them in claude.ai Settings > Connectors')
}
expect(finding!.apply).toBeUndefined()
})
it('does not flag a server with healthy coverage', () => {
const inventory = Array.from({ length: 20 }, (_, i) => `mcp__svc__t${i}`)
const turns = [makeTurn(
@ -379,9 +568,93 @@ describe('detectMcpToolCoverage', () => {
expect(finding!.explanation).toContain('1/30')
expect(finding!.fix.type).toBe('command')
expect((finding!.fix as { text: string }).text).toContain("claude mcp remove 'hf'")
expect(finding!.apply).toEqual({ kind: 'mcp-remove', servers: ['hf'] })
expect(finding!.tokensSaved).toBeGreaterThan(0)
})
it('keeps mixed connector guidance visible while making only the local server executable', () => {
const inventory = ['filesystem', 'claude_ai_Slack'].flatMap(server =>
Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
)
const sessions: SessionSummary[] = [
makeSession({ sessionId: 'mixed-a', inventory, turns: [makeTurn([makeCall({ cacheCreation: 50_000 })])] }),
makeSession({ sessionId: 'mixed-b', inventory, turns: [makeTurn([makeCall({ cacheCreation: 50_000 })])] }),
]
const finding = detectMcpToolCoverage([project(sessions)])
expect(finding).not.toBeNull()
// The finding describes both opportunities: 40 unused tool schemas across
// two sessions = 40K effective tokens. The automatic mutation owns only
// the 20 local schemas = 20K; the connector portion remains manual.
expect(finding).toMatchObject({ tokensSaved: 40_000, applyTokensSaved: 20_000 })
expect(finding!.explanation).toContain('claude_ai_Slack')
expect(finding!.explanation).toContain('/mcp')
expect(finding!.explanation).toContain('claude.ai Settings > Connectors')
expect(finding!.fix).toEqual({
type: 'command',
label: 'Remove the underused local server, or trim its tools in your MCP config:',
text: "claude mcp remove 'filesystem'",
})
expect(finding!.apply).toEqual({ kind: 'mcp-remove', servers: ['filesystem'] })
})
it('attributes a capped mixed cache bucket proportionally to the local action', () => {
const inventory = ['filesystem', 'claude_ai_Slack'].flatMap(server =>
Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
)
const sessions = ['a', 'b'].map(sessionId => makeSession({
sessionId,
inventory,
turns: [makeTurn([makeCall({ cacheCreation: 10_000 })])],
}))
const finding = detectMcpToolCoverage([project(sessions)])
// Each call's 10K cache bucket is shared evenly by two 8K schemas.
// Total: 2 * 10K * 1.25 = 25K. The local mutation owns half.
expect(finding).toMatchObject({ tokensSaved: 25_000, applyTokensSaved: 12_500 })
})
it('charges only the flagged servers actually loaded in each session', () => {
const sessions = ['filesystem', 'claude_ai_Slack'].flatMap(server =>
['a', 'b'].map(suffix => makeSession({
sessionId: `${server}-${suffix}`,
inventory: Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
turns: [makeTurn([makeCall({ cacheCreation: 50_000 })])],
})),
)
const finding = detectMcpToolCoverage([project(sessions)])
// Four sessions each load one 8K schema. The combined finding must not
// charge both schemas to every session merely because both are flagged.
expect(finding).toMatchObject({ tokensSaved: 40_000, applyTokensSaved: 20_000 })
})
it('disambiguates a claude.ai connector from a similarly named local server', () => {
const sessions: SessionSummary[] = []
for (const server of ['claude_ai_Netlify', 'netlify']) {
const inventory = Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`)
sessions.push(
makeSession({ sessionId: `${server}-a`, inventory }),
makeSession({ sessionId: `${server}-b`, inventory }),
)
}
const finding = detectMcpToolCoverage([project(sessions)])
expect(finding).not.toBeNull()
expect(finding!.explanation).toContain('claude_ai_Netlify')
expect(finding!.explanation).toContain('separate from any similarly named local MCP server')
expect(finding!.fix.type).toBe('command')
if (finding!.fix.type === 'command') {
expect(finding!.fix.text).toBe("claude mcp remove 'netlify'")
expect(finding!.fix.text).not.toContain('claude_ai_Netlify')
}
expect(finding!.apply).toEqual({ kind: 'mcp-remove', servers: ['netlify'] })
})
it('escalates impact to high when token waste crosses the threshold', () => {
const inventory = Array.from({ length: 60 }, (_, i) => `mcp__big__t${i}`)
// 60 tools * 400 tokens = 24k schema. With many sessions and large
@ -654,3 +927,128 @@ describe('detectMcpProfileAdvisor', () => {
expect(detectMcpProfileAdvisor(projects, coverage)).toBeNull()
})
})
// ---------------------------------------------------------------------------
// Connector findings under the fix/nudge/keep classification (#1019)
// ---------------------------------------------------------------------------
describe('connector findings and finding class', () => {
const inventoryFor = (servers: string[]) => servers.flatMap(server =>
Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
)
const twoSessions = (servers: string[]) => ['a', 'b'].map(sessionId => makeSession({
sessionId,
inventory: inventoryFor(servers),
turns: [makeTurn([makeCall({ cacheCreation: 50_000 })])],
}))
it('classifies a connector-only finding as a nudge, since nothing is appliable', () => {
const finding = detectMcpToolCoverage([project(twoSessions(['claude_ai_Gmail']))])
expect(finding).not.toBeNull()
expect(finding!.apply).toBeUndefined()
expect(findingClass(finding!)).toBe('nudge')
expect(finding!.tokensSaved).toBeGreaterThan(0)
// Never lands in the "apply-able" subtotal.
expect(classTotals([finding!], 0.00002).fix).toEqual({ tokensSaved: 0, savingsUSD: 0, count: 0 })
})
it('counts only the local subset of a mixed finding towards the apply-able subtotal', () => {
const finding = detectMcpToolCoverage([project(twoSessions(['filesystem', 'claude_ai_Slack']))])
expect(finding).not.toBeNull()
expect(findingClass(finding!)).toBe('fix')
expect(finding).toMatchObject({ tokensSaved: 40_000, applyTokensSaved: 20_000 })
expect(classTotals([finding!], 0.00002).fix).toEqual({ tokensSaved: 20_000, savingsUSD: 0.4, count: 1 })
})
it("leaves a local-only finding's subtotal at its full estimate", () => {
const finding = detectMcpToolCoverage([project(twoSessions(['filesystem']))])
expect(finding).not.toBeNull()
expect(finding!.applyTokensSaved).toBeUndefined()
expect(classTotals([finding!], 0.00002).fix.tokensSaved).toBe(finding!.tokensSaved)
})
it('charges each session only for the local schemas it actually loaded', () => {
// Same per-session scoping the connector split relies on, with no
// connector in play: two flagged local servers in disjoint sessions are
// charged one schema each, not both schemas everywhere.
const sessions = ['filesystem', 'playwright'].flatMap(server =>
['a', 'b'].map(suffix => makeSession({
sessionId: `${server}-${suffix}`,
inventory: inventoryFor([server]),
turns: [makeTurn([makeCall({ cacheCreation: 50_000 })])],
})),
)
const finding = detectMcpToolCoverage([project(sessions)])
expect(finding).toMatchObject({ tokensSaved: 40_000 })
expect(finding!.applyTokensSaved).toBeUndefined()
expect(classTotals([finding!], 0.00002).fix.tokensSaved).toBe(40_000)
})
it('treats a claude_ai_* name owned by local config as a local server', () => {
const finding = detectMcpToolCoverage(
[project(twoSessions(['claude_ai_homegrown']))],
undefined,
new Set(['claude_ai_homegrown']),
)
expect(finding!.fix).toEqual({
type: 'command',
label: 'Remove the underused local server, or trim its tools in your MCP config:',
text: "claude mcp remove 'claude_ai_homegrown'",
})
expect(finding!.apply).toEqual({ kind: 'mcp-remove', servers: ['claude_ai_homegrown'] })
expect(findingClass(finding!)).toBe('fix')
// Local config owns the name, so the finding must not claim it is a connector.
expect(finding!.explanation).not.toContain('is a claude.ai connector namespace')
// ...but the transcript cannot rule out a same-name connector.
expect(finding!.explanation).toContain('If you also use a claude.ai connector named claude_ai_homegrown')
expect(finding!.manualFollowUp?.label).toBe('Check for a same-name claude.ai connector:')
// The whole estimate is appliable: nothing is reserved for a connector.
expect(finding!.applyTokensSaved).toBeUndefined()
expect(classTotals([finding!], 0.00002).fix.tokensSaved).toBe(finding!.tokensSaved)
})
it('keeps a claude_ai_* name absent from local config a connector', () => {
const finding = detectMcpToolCoverage(
[project(twoSessions(['claude_ai_Gmail']))],
undefined,
new Set(['filesystem', 'playwright']),
)
expect(finding!.fix.type).toBe('paste')
expect(finding!.apply).toBeUndefined()
expect(findingClass(finding!)).toBe('nudge')
expect(finding!.explanation).toContain('is a claude.ai connector namespace')
})
it('falls back to prefix-only when no local config could be read', () => {
// Unreadable or absent config contributes no names, which leaves every
// claude_ai_* namespace on the conservative connector path.
const finding = detectMcpToolCoverage([project(twoSessions(['claude_ai_homegrown']))])
expect(finding!.fix.type).toBe('paste')
expect(finding!.apply).toBeUndefined()
expect(findingClass(finding!)).toBe('nudge')
})
it('applies the local entry and notes the connector when a name collides', () => {
const finding = detectMcpToolCoverage(
[project(twoSessions(['filesystem', 'claude_ai_Slack']))],
undefined,
new Set(['filesystem', 'claude_ai_Slack']),
)
// Both are local: the removal owns both entries and nothing is deferred.
expect(finding!.apply).toEqual({ kind: 'mcp-remove', servers: ['filesystem', 'claude_ai_Slack'] })
expect(finding!.applyTokensSaved).toBeUndefined()
expect(classTotals([finding!], 0.00002).fix.tokensSaved).toBe(40_000)
expect(finding!.explanation).not.toContain('is a claude.ai connector namespace')
expect(finding!.manualFollowUp?.text)
.toBe('If you also use a claude.ai connector named claude_ai_Slack, manage it with /mcp or in claude.ai Settings > Connectors.')
})
})

View file

@ -0,0 +1,299 @@
import { createHash } from 'node:crypto'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import {
WINDOWS_RELEASE,
installMenubarApp,
parseInstalledWindowsMenubar,
resolveLatestMenubarReleaseAssets,
resolveSystem32Path,
resolveVersionedMenubarReleaseAssets,
type ReleaseResponse,
} from '../src/menubar-installer.js'
function asset(name: string) {
return { name, browser_download_url: `https://example.test/${name}` }
}
const MSI_URL =
'https://github.com/getagentseal/codeburn/releases/download/windows-v0.9.20/CodeBurn.Menubar_0.9.20_x64_en-US.msi'
const MSI_BYTES = 'msi-bytes'
function sha256(text: string): string {
return createHash('sha256').update(Buffer.from(text)).digest('hex')
}
function httpResponse(status: number, body?: string) {
return {
ok: status >= 200 && status < 300,
status,
headers: { get: () => null },
body: body === undefined ? null : new Response(body).body,
text: async () => body ?? '',
}
}
/** reg query /s output, one blank-line separated block per subkey. */
function regBlock(values: Record<string, string>, key = '{9c1e2f0a-0000-0000-0000-000000000001}'): string {
const lines = Object.entries(values).map(([name, value]) => ` ${name} REG_SZ ${value}`)
return [
'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{other}',
' DisplayName REG_SZ Some Other App',
'',
`HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\${key}`,
...lines,
'',
].join('\r\n')
}
const INSTALLED_0_9_20 = regBlock({
DisplayName: 'CodeBurn Menubar',
DisplayVersion: '0.9.20',
InstallLocation: 'C:\\Program Files\\CodeBurn Menubar\\',
Publisher: 'AgentSeal',
})
describe('windows release asset resolution', () => {
it('builds direct release asset URLs from the CLI version', () => {
const resolved = resolveVersionedMenubarReleaseAssets('0.9.20', WINDOWS_RELEASE)
expect(resolved.release.tag_name).toBe('windows-v0.9.20')
expect(resolved.zip.name).toBe('CodeBurn.Menubar_0.9.20_x64_en-US.msi')
expect(resolved.zip.browser_download_url).toBe(MSI_URL)
expect(resolved.checksum.browser_download_url).toBe(`${MSI_URL}.sha256`)
})
it('normalizes a leading v', () => {
expect(resolveVersionedMenubarReleaseAssets('v0.9.20', WINDOWS_RELEASE).release.tag_name).toBe('windows-v0.9.20')
})
it('scans for the newest windows-v release that has both assets', () => {
const releases: ReleaseResponse[] = [
{ tag_name: 'mac-v0.9.20', assets: [asset('CodeBurnMenubar-v0.9.20.zip'), asset('CodeBurnMenubar-v0.9.20.zip.sha256')] },
{ tag_name: 'windows-v0.9.21', assets: [asset('CodeBurn.Menubar_0.9.21_x64_en-US.msi')] },
{
tag_name: 'windows-v0.9.20',
assets: [asset('CodeBurn.Menubar_0.9.20_x64_en-US.msi'), asset('CodeBurn.Menubar_0.9.20_x64_en-US.msi.sha256')],
},
]
const resolved = resolveLatestMenubarReleaseAssets(releases, WINDOWS_RELEASE)
expect(resolved.release.tag_name).toBe('windows-v0.9.20')
expect(resolved.zip.name).toBe('CodeBurn.Menubar_0.9.20_x64_en-US.msi')
})
it('reports when no windows release carries both assets', () => {
expect(() => resolveLatestMenubarReleaseAssets([{ tag_name: 'v0.9.20', assets: [] }], WINDOWS_RELEASE))
.toThrow(/No windows-v\* release/)
})
})
describe('resolveSystem32Path', () => {
it('uses an absolute SystemRoot', () => {
expect(resolveSystem32Path('msiexec.exe', { SystemRoot: 'D:\\Windows' })).toBe('D:\\Windows\\System32\\msiexec.exe')
})
it('falls back to the documented default when SystemRoot is missing or relative', () => {
expect(resolveSystem32Path('reg.exe', {})).toBe('C:\\Windows\\System32\\reg.exe')
expect(resolveSystem32Path('reg.exe', { SystemRoot: 'Windows' })).toBe('C:\\Windows\\System32\\reg.exe')
})
})
describe('parseInstalledWindowsMenubar', () => {
it('reads the version and joins the exe onto InstallLocation', () => {
expect(parseInstalledWindowsMenubar(INSTALLED_0_9_20)).toEqual({
version: '0.9.20',
exePath: 'C:\\Program Files\\CodeBurn Menubar\\CodeBurn Menubar.exe',
})
})
it('falls back to DisplayIcon when there is no InstallLocation', () => {
const output = regBlock({
DisplayName: 'CodeBurn Menubar',
DisplayVersion: '0.9.20',
DisplayIcon: 'C:\\Program Files\\CodeBurn Menubar\\CodeBurn Menubar.exe,0',
})
expect(parseInstalledWindowsMenubar(output)?.exePath).toBe('C:\\Program Files\\CodeBurn Menubar\\CodeBurn Menubar.exe')
})
it('returns undefined when the product is not installed', () => {
expect(parseInstalledWindowsMenubar(regBlock({ DisplayName: 'Something Else', DisplayVersion: '1.0' }))).toBeUndefined()
})
})
describe('installMenubarApp on windows', () => {
let sandbox: string
let logs: string[]
let launched: string[]
let installerCalls: Array<{ exe: string; args: string[] }>
function hooks(overrides: Record<string, unknown> = {}) {
return {
stagingDir: sandbox,
env: { SystemRoot: 'C:\\Windows' },
log: (message: string) => { logs.push(message) },
launch: (exePath: string) => { launched.push(exePath) },
queryRegistry: async () => INSTALLED_0_9_20,
runInstaller: async (exe: string, args: string[]) => { installerCalls.push({ exe, args }); return 0 },
fetchOptions: {
sleep: async () => {},
log: (message: string) => { logs.push(message) },
fetchImpl: async (url: string) => httpResponse(200, url.endsWith('.sha256')
? `${sha256(MSI_BYTES)} CodeBurn.Menubar_0.9.20_x64_en-US.msi`
: MSI_BYTES),
},
...overrides,
}
}
beforeEach(async () => {
sandbox = await mkdtemp(join(tmpdir(), 'menubar-windows-'))
logs = []
launched = []
installerCalls = []
})
afterEach(async () => {
await rm(sandbox, { recursive: true, force: true })
})
it('skips the download and just launches when the pinned version is already installed', async () => {
let fetches = 0
const result = await installMenubarApp({
platform: 'win32',
cliVersion: '0.9.20',
windows: hooks({ fetchOptions: { fetchImpl: async () => { fetches++; return httpResponse(500) } } }),
})
expect(fetches).toBe(0)
expect(installerCalls).toEqual([])
expect(launched).toEqual(['C:\\Program Files\\CodeBurn Menubar\\CodeBurn Menubar.exe'])
expect(result).toEqual({ installedPath: 'C:\\Program Files\\CodeBurn Menubar\\CodeBurn Menubar.exe', launched: true })
})
it('downloads, verifies, runs msiexec from System32 and launches the installed app', async () => {
let queries = 0
const result = await installMenubarApp({
platform: 'win32',
cliVersion: '0.9.20',
windows: hooks({
queryRegistry: async () => (queries++ === 0 ? '' : INSTALLED_0_9_20),
}),
})
expect(installerCalls).toEqual([{
exe: 'C:\\Windows\\System32\\msiexec.exe',
args: ['/i', join(sandbox, 'CodeBurn.Menubar_0.9.20_x64_en-US.msi'), '/passive', '/norestart'],
}])
expect(launched).toEqual(['C:\\Program Files\\CodeBurn Menubar\\CodeBurn Menubar.exe'])
expect(result.launched).toBe(true)
expect(logs).toContain('Downloading CodeBurn.Menubar_0.9.20_x64_en-US.msi...')
expect(logs).toContain('Verifying checksum...')
expect(logs).toContain('Installing...')
expect(logs).toContain('Launched CodeBurn Menubar.')
})
it('reinstalls the same version when --force is passed', async () => {
await installMenubarApp({ platform: 'win32', cliVersion: '0.9.20', force: true, windows: hooks() })
expect(installerCalls).toHaveLength(1)
})
it('aborts on a checksum mismatch without running the installer', async () => {
await expect(installMenubarApp({
platform: 'win32',
cliVersion: '0.9.20',
windows: hooks({
queryRegistry: async () => '',
fetchOptions: {
sleep: async () => {},
log: () => {},
fetchImpl: async (url: string) =>
httpResponse(200, url.endsWith('.sha256') ? `${sha256('other-bytes')} x.msi` : MSI_BYTES),
},
}),
})).rejects.toThrow(/Checksum mismatch/)
expect(installerCalls).toEqual([])
expect(launched).toEqual([])
})
it('treats 3010 as installed and says a restart is pending', async () => {
let queries = 0
const result = await installMenubarApp({
platform: 'win32',
cliVersion: '0.9.20',
windows: hooks({
queryRegistry: async () => (queries++ === 0 ? '' : INSTALLED_0_9_20),
runInstaller: async (exe: string, args: string[]) => { installerCalls.push({ exe, args }); return 3010 },
}),
})
expect(result.launched).toBe(true)
expect(logs.some(line => line.includes('restart'))).toBe(true)
})
it('treats 1602 as a cancelled install: no launch, no error', async () => {
const result = await installMenubarApp({
platform: 'win32',
cliVersion: '0.9.20',
windows: hooks({
queryRegistry: async () => '',
runInstaller: async () => 1602,
}),
})
expect(result).toEqual({ installedPath: '', launched: false })
expect(launched).toEqual([])
expect(logs.some(line => line.includes('cancelled'))).toBe(true)
})
it('fails with the exit code for any other msiexec failure', async () => {
await expect(installMenubarApp({
platform: 'win32',
cliVersion: '0.9.20',
windows: hooks({ queryRegistry: async () => '', runInstaller: async () => 1603 }),
})).rejects.toThrow(/msiexec exited with 1603/)
expect(launched).toEqual([])
})
it('falls back to the release API when the pinned assets are missing', async () => {
let queries = 0
const requested: string[] = []
const latest: ReleaseResponse[] = [{
tag_name: 'windows-v0.9.19',
assets: [
{ name: 'CodeBurn.Menubar_0.9.19_x64_en-US.msi', browser_download_url: 'https://example.test/msi' },
{ name: 'CodeBurn.Menubar_0.9.19_x64_en-US.msi.sha256', browser_download_url: 'https://example.test/msi.sha256' },
],
}]
const result = await installMenubarApp({
platform: 'win32',
cliVersion: '0.9.20',
windows: hooks({
queryRegistry: async () => (queries++ === 0 ? '' : INSTALLED_0_9_20),
apiFetch: async () => ({ ok: true, status: 200, headers: { get: () => null }, json: async () => latest }),
fetchOptions: {
sleep: async () => {},
log: () => {},
fetchImpl: async (url: string) => {
requested.push(url)
if (url.startsWith(MSI_URL)) return httpResponse(404)
return httpResponse(200, url.endsWith('.sha256') ? `${sha256(MSI_BYTES)} msi` : MSI_BYTES)
},
},
}),
})
expect(requested[0]).toBe(MSI_URL)
expect(requested).toContain('https://example.test/msi')
expect(installerCalls[0]?.args[1]).toBe(join(sandbox, 'CodeBurn.Menubar_0.9.19_x64_en-US.msi'))
expect(result.launched).toBe(true)
})
})

View file

@ -1,6 +1,9 @@
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { spawnSync } from 'node:child_process'
import { describe, it, expect } from 'vitest'
import { describe, it, expect, vi } from 'vitest'
import chalk from 'chalk'
import stripAnsi from 'strip-ansi'
@ -734,6 +737,114 @@ describe('renderCsv', () => {
})
describe('models CLI breakdown flags', () => {
vi.setConfig({ testTimeout: 30_000 })
it('filters the models report to unpriced rows', async () => {
const home = await mkdtemp(join(tmpdir(), 'codeburn-models-unpriced-'))
try {
const projectDir = join(home, '.claude', 'projects', 'models-unpriced')
await mkdir(projectDir, { recursive: true })
await writeFile(join(projectDir, 'session.jsonl'), [
JSON.stringify({
type: 'user',
sessionId: 'models-unpriced-session',
timestamp: '2026-05-09T00:00:00.000Z',
cwd: '/tmp/models-unpriced',
message: { role: 'user', content: 'Use one priced and one unpriced model.' },
}),
JSON.stringify({
type: 'assistant',
sessionId: 'models-unpriced-session',
timestamp: '2026-05-09T00:01:00.000Z',
cwd: '/tmp/models-unpriced',
message: {
id: 'priced',
type: 'message',
role: 'assistant',
model: 'claude-sonnet-4-6',
content: [{ type: 'text', text: 'priced' }],
usage: { input_tokens: 1000, output_tokens: 100, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 },
},
}),
JSON.stringify({
type: 'assistant',
sessionId: 'models-unpriced-session',
timestamp: '2026-05-09T00:02:00.000Z',
cwd: '/tmp/models-unpriced',
message: {
id: 'unpriced',
type: 'message',
role: 'assistant',
model: 'zz-unpriced-frontier-model',
content: [{ type: 'text', text: 'unpriced' }],
usage: { input_tokens: 2000, output_tokens: 200, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 },
},
}),
].join('\n') + '\n')
const res = spawnSync(
process.execPath,
['--import', 'tsx', 'src/cli.ts', 'models', '--unpriced', '--from', '2026-05-09', '--to', '2026-05-09', '--provider', 'claude', '--format', 'json'],
{ cwd: process.cwd(), env: { ...process.env, HOME: home, CLAUDE_CONFIG_DIR: join(home, '.claude'), CODEBURN_CACHE_DIR: join(home, '.cache', 'codeburn'), TZ: 'UTC' }, encoding: 'utf-8', timeout: 30_000 },
)
expect(res.status, `stdout: ${res.stdout}\nstderr: ${res.stderr}`).toBe(0)
const rows = JSON.parse(res.stdout) as Array<{ model: string; calls: number }>
expect(rows.map(row => row.model)).toEqual(['zz-unpriced-frontier-model'])
expect(rows[0]?.calls).toBe(1)
} finally {
await rm(home, { recursive: true, force: true })
}
})
// Unpriced rows all sort at $0 in aggregateModels, so the old implementation
// preserved transcript/Map order instead of findUnpricedModels' token order.
it('keeps unpriced rows when --unpriced is combined with --top', async () => {
const home = await mkdtemp(join(tmpdir(), 'codeburn-models-unpriced-top-'))
try {
const projectDir = join(home, '.claude', 'projects', 'models-unpriced-top')
await mkdir(projectDir, { recursive: true })
const assistant = (id: string, model: string, timestamp: string, input: number) => JSON.stringify({
type: 'assistant',
sessionId: 'models-unpriced-top-session',
timestamp,
cwd: '/tmp/models-unpriced-top',
message: {
id, type: 'message', role: 'assistant', model,
content: [{ type: 'text', text: id }],
usage: { input_tokens: input, output_tokens: 100, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 },
},
})
await writeFile(join(projectDir, 'session.jsonl'), [
JSON.stringify({
type: 'user',
sessionId: 'models-unpriced-top-session',
timestamp: '2026-05-09T00:00:00.000Z',
cwd: '/tmp/models-unpriced-top',
message: { role: 'user', content: 'Three unpriced models arrive small-first.' },
}),
// Transcript order is deliberately different from token order:
// 1.1k, 9.1k, 5.1k total tokens. The two largest must survive --top 2.
assistant('small', 'zz-unpriced-small', '2026-05-09T00:01:00.000Z', 1000),
assistant('largest', 'zz-unpriced-largest', '2026-05-09T00:02:00.000Z', 9000),
assistant('middle', 'zz-unpriced-middle', '2026-05-09T00:03:00.000Z', 5000),
].join('\n') + '\n')
const res = spawnSync(
process.execPath,
['--import', 'tsx', 'src/cli.ts', 'models', '--unpriced', '--top', '2', '--from', '2026-05-09', '--to', '2026-05-09', '--provider', 'claude', '--format', 'json'],
{ cwd: process.cwd(), env: { ...process.env, HOME: home, CLAUDE_CONFIG_DIR: join(home, '.claude'), CODEBURN_CACHE_DIR: join(home, '.cache', 'codeburn'), TZ: 'UTC' }, encoding: 'utf-8', timeout: 30_000 },
)
expect(res.status, `stdout: ${res.stdout}\nstderr: ${res.stderr}`).toBe(0)
const rows = JSON.parse(res.stdout) as Array<{ model: string }>
expect(rows).toHaveLength(2)
expect(rows.map(row => row.model)).toEqual(['zz-unpriced-largest', 'zz-unpriced-middle'])
} finally {
await rm(home, { recursive: true, force: true })
}
})
it('rejects --by-task and --by-agent together with a clear error and exit 1', () => {
const res = spawnSync(
process.execPath,

View file

@ -310,6 +310,13 @@ describe('user aliases via setModelAliases', () => {
expect(getModelCosts('anthropic--claude-4.6-opus')).toEqual(getModelCosts('claude-sonnet-4-5'))
})
it('user alias whose source already has a short name displays the target', () => {
setModelAliases({ 'gpt-4o': 'claude-opus-4-6' })
expect(getModelCosts('gpt-4o')).toEqual(getModelCosts('claude-opus-4-6'))
expect(getShortModelName('gpt-4o')).toBe('Opus 4.6')
setModelAliases({})
})
it('resetting aliases restores builtins', () => {
setModelAliases({ 'anthropic--claude-4.6-opus': 'claude-sonnet-4-5' })
setModelAliases({})
@ -505,7 +512,7 @@ describe('Cursor model variants resolve to pricing', () => {
// Sonnet family
['claude-4-sonnet', 'claude-sonnet-4'],
['claude-4-sonnet-1m', 'claude-sonnet-4'],
['claude-4-sonnet-thinking', 'claude-sonnet-4-5'],
['claude-4-sonnet-thinking', 'claude-sonnet-4'],
['claude-4.5-sonnet', 'claude-sonnet-4-5'],
['claude-4.5-sonnet-thinking', 'claude-sonnet-4-5'],
['claude-4.6-sonnet', 'claude-sonnet-4-6'],
@ -558,6 +565,14 @@ describe('Cursor model variants resolve to pricing', () => {
expect(costs!.outputCostPerToken).toBe(expected!.outputCostPerToken)
})
}
// Regression for #912: Cursor's unversioned `claude-4-sonnet-thinking`
// slug is the thinking variant of Sonnet 4, not Sonnet 4.5. The two models
// currently share a price, so the display name pins the canonical identity
// independently of today's pricing coincidence.
it('keeps claude-4-sonnet-thinking in the Sonnet 4 model family', () => {
expect(getShortModelName('claude-4-sonnet-thinking')).toBe('Sonnet 4')
})
})
describe('Cursor house model pricing', () => {
@ -729,6 +744,10 @@ describe('provider pricing suffix variants', () => {
describe('observed provider model aliases', () => {
const cases: Array<[string, string]> = [
['MiMo-V2-Flash', 'xiaomi/mimo-v2-flash'],
['mimo-v2.5-pro', 'xiaomi/mimo-v2.5-pro'],
['MiMo-v2.5-Pro', 'xiaomi/mimo-v2.5-pro'],
['mimo-v2.5', 'xiaomi/mimo-v2.5'],
['MiMo-v2.5', 'xiaomi/mimo-v2.5'],
['KAT-Coder-Pro-V1', 'kwaipilot/kat-coder-pro'],
// Kimi Code wires report bare `k3` in llm.request.model; it must price
// through the kimi-k3 table entry, not fall through to $0.
@ -750,6 +769,39 @@ describe('observed provider model aliases', () => {
expect(getShortModelName('k3')).toBe('Kimi K3')
})
it('does not recurse on vendor-requalified MiMo aliases', () => {
expect(getShortModelName('mimo-v2.5')).toBe('MiMo v2.5')
expect(getShortModelName('MiMo-v2.5')).toBe('MiMo v2.5')
expect(getShortModelName('cline-pass/mimo-v2.5')).toBe('MiMo v2.5')
expect(getShortModelName('cline-pass/mimo-v2.5-pro')).toBe('MiMo v2.5 Pro')
})
// The `mimo-v2-flash -> xiaomi/mimo-v2-flash` alias shipped before this
// change and already cycled: strip the namespace, alias it back, take the
// leaf, repeat. Every display surface (overview's model table included)
// threw RangeError on a real MiMo v2 Flash session. Pin the shipped ids.
it('resolves the already-shipped MiMo v2 Flash alias without blowing the stack', () => {
for (const id of ['mimo-v2-flash', 'MiMo-V2-Flash', 'cline-pass/mimo-v2-flash', 'mimo/mimo-v2-flash']) {
expect(() => getShortModelName(id)).not.toThrow()
expect(getShortModelName(id)).toBe('MiMo v2 Flash')
expect(getModelCosts(id)).toEqual(getModelCosts('xiaomi/mimo-v2-flash'))
}
})
it('names the base MiMo 2.5 row without swallowing the Pro tier', () => {
expect(getShortModelName('mimo-v2.5')).toBe('MiMo v2.5')
expect(getShortModelName('mimo-v2.5-pro')).toBe('MiMo v2.5 Pro')
expect(getModelCosts('mimo-v2.5')).not.toEqual(getModelCosts('mimo-v2.5-pro'))
})
it('stays unary so Array.map cannot feed the index as cycle state', () => {
expect(['mimo-v2.5', 'gpt-4o', 'cline-pass/mimo-v2.5-pro'].map(getShortModelName)).toEqual([
'MiMo v2.5',
'GPT-4o',
'MiMo v2.5 Pro',
])
})
it('does not map dated Qwen3 Max to a reseller price without provider context', () => {
expect(getModelCosts('qwen3-max-2026-01-23')).toBeNull()
expect(calculateCost('qwen3-max-2026-01-23', 1_000_000, 1_000_000, 0, 0, 0)).toBe(0)

View file

@ -5,6 +5,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { createHash } from 'node:crypto'
import { PassThrough, Writable } from 'node:stream'
import stripAnsi from 'strip-ansi'
import { planFor, planFindings, type PlanContext } from '../src/act/plans.js'
import { renderApplyList, runOptimizeApply, type ApplyOptions } from '../src/act/optimize-apply.js'
@ -12,6 +13,9 @@ import { runAction } from '../src/act/apply.js'
import { undoAction } from '../src/act/undo.js'
import { readRecords, shortId } from '../src/act/journal.js'
import {
FINDING_BASIS,
FINDING_CLASS,
findingClass,
detectBloatedClaudeMd,
detectDuplicateReads,
detectJunkReads,
@ -102,6 +106,169 @@ describe('mcp-remove plan', () => {
await undoAction({ id: rec.id }, { actionsDir: fx.actionsDir })
expect(await readFile(claudeJson, 'utf-8')).toBe(original)
})
it('does not plan connector removal and removes only the local server from a mixed finding', async () => {
const coverage = (server: string): McpServerCoverage => ({
server,
toolsAvailable: 20,
toolsInvoked: 0,
unusedTools: Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
invocations: 0,
loadedSessions: 2,
coverageRatio: 0,
})
const connector = coverage('claude_ai_Netlify')
const connectorOnly = detectMcpToolCoverage([], [connector])!
expect(connectorOnly.apply).toBeUndefined()
expect(planFor(connectorOnly)).toBeNull()
const fx = await makeFixture()
const claudeJson = join(fx.home, '.claude.json')
await writeFile(claudeJson, JSON.stringify({
mcpServers: {
filesystem: { command: 'filesystem' },
netlify: { command: 'local-netlify' },
},
}, null, 2) + '\n')
const mixed = detectMcpToolCoverage([], [connector, coverage('filesystem')])!
expect(mixed.apply).toEqual({ kind: 'mcp-remove', servers: ['filesystem'] })
const plan = planFor(mixed, { homeDir: fx.home, cwd: fx.project })
expect(plan).not.toBeNull()
await runAction(plan!, fx.actionsDir)
expect(JSON.parse(await readFile(claudeJson, 'utf-8')).mcpServers).toEqual({
netlify: { command: 'local-netlify' },
})
})
it('previews only the savings attributable to a mixed finding local mutation', async () => {
const fx = await makeFixture()
await writeFile(join(fx.home, '.claude.json'), JSON.stringify({
mcpServers: { filesystem: { command: 'filesystem' } },
}, null, 2) + '\n')
const finding: WasteFinding = {
id: 'mcp-low-coverage',
title: '2 MCP servers with low tool coverage',
explanation: '',
impact: 'medium',
tokensSaved: 80_000,
applyTokensSaved: 20_000,
fix: { type: 'command', label: '', text: "claude mcp remove 'filesystem'" },
apply: { kind: 'mcp-remove', servers: ['filesystem'] },
}
const plans = planFindings([finding], { homeDir: fx.home, cwd: fx.project })
const preview = stripAnsi(renderApplyList(plans, [], 0.000002))
expect(preview).toContain('(~20.0K tokens, ~$0.040)')
expect(preview).not.toContain('~80.0K tokens')
expect(preview).not.toContain('~$0.160')
})
it('scopes targets and savings to local servers actually present in editable config', async () => {
const fx = await makeFixture()
await writeFile(join(fx.home, '.claude.json'), JSON.stringify({
mcpServers: { filesystem: { command: 'filesystem' } },
}, null, 2) + '\n')
const finding: WasteFinding = {
id: 'mcp-low-coverage',
title: '3 MCP servers with low tool coverage',
explanation: '',
impact: 'medium',
tokensSaved: 60_000,
applyTokensSaved: 30_000,
applyTokensSavedByServer: { filesystem: 10_000, managed: 20_000 },
fix: { type: 'command', label: '', text: "claude mcp remove 'filesystem'\nclaude mcp remove 'managed'" },
apply: { kind: 'mcp-remove', servers: ['filesystem', 'managed'] },
}
const plans = planFindings([finding], { homeDir: fx.home, cwd: fx.project })
const preview = stripAnsi(renderApplyList(plans.filter(p => p.plan), plans.filter(p => !p.plan), 0))
expect(plans[0]!.plan?.affectedMcpServers).toEqual(['filesystem'])
expect(preview).toContain('Removes local MCP server: filesystem')
expect(preview).toContain('~10.0K tokens')
expect(preview).not.toContain('~30.0K tokens')
expect(preview).toContain('skipped managed: not found in editable config')
})
it('suppresses savings for a legacy partial plan without per-server attribution', async () => {
const fx = await makeFixture()
await writeFile(join(fx.home, '.claude.json'), JSON.stringify({
mcpServers: { filesystem: { command: 'filesystem' } },
}, null, 2) + '\n')
const finding: WasteFinding = {
id: 'mcp-low-coverage',
title: '2 MCP servers with low tool coverage',
explanation: '',
impact: 'medium',
tokensSaved: 30_000,
applyTokensSaved: 30_000,
fix: { type: 'command', label: '', text: '' },
apply: { kind: 'mcp-remove', servers: ['filesystem', 'managed'] },
}
const plans = planFindings([finding], { homeDir: fx.home, cwd: fx.project })
const preview = stripAnsi(renderApplyList(plans.filter(p => p.plan), plans.filter(p => !p.plan), 0))
expect(plans[0]!.plan?.affectedMcpServers).toEqual(['filesystem'])
expect(plans[0]!.plan?.mcpSavingsUncertain).toBe(true)
expect(preview).toContain('Savings not estimated')
expect(preview).not.toContain('~30.0K tokens')
})
it('deduplicates repeated removal targets before planning and pricing them', async () => {
const fx = await makeFixture()
await writeFile(join(fx.home, '.claude.json'), JSON.stringify({
mcpServers: { filesystem: { command: 'filesystem' } },
}, null, 2) + '\n')
const finding: WasteFinding = {
id: 'mcp-low-coverage',
title: '1 MCP server with low tool coverage',
explanation: '',
impact: 'medium',
tokensSaved: 10_000,
applyTokensSavedByServer: { filesystem: 10_000 },
fix: { type: 'command', label: '', text: '' },
apply: { kind: 'mcp-remove', servers: ['filesystem', 'filesystem'] },
}
const plans = planFindings([finding], { homeDir: fx.home, cwd: fx.project })
const preview = stripAnsi(renderApplyList(plans.filter(p => p.plan), [], 0))
expect(plans[0]!.plan?.affectedMcpServers).toEqual(['filesystem'])
expect(preview).toContain('~10.0K tokens')
expect(preview).not.toContain('~20.0K tokens')
})
it('does not claim savings when another relevant config scope is unreadable', async () => {
const fx = await makeFixture()
await writeFile(join(fx.home, '.claude.json'), JSON.stringify({
mcpServers: { filesystem: { command: 'filesystem' } },
}, null, 2) + '\n')
await writeFile(join(fx.project, '.mcp.json'), 'not json{{{')
const finding: WasteFinding = {
id: 'mcp-low-coverage',
title: '1 MCP server with low tool coverage',
explanation: '',
impact: 'medium',
tokensSaved: 10_000,
applyTokensSavedByServer: { filesystem: 10_000 },
fix: { type: 'command', label: '', text: "claude mcp remove 'filesystem'" },
apply: { kind: 'mcp-remove', servers: ['filesystem'] },
}
const plans = planFindings([finding], { homeDir: fx.home, cwd: fx.project })
const preview = stripAnsi(renderApplyList(plans.filter(p => p.plan), plans.filter(p => !p.plan), 0))
expect(plans[0]!.plan?.affectedMcpServers).toEqual(['filesystem'])
expect(plans[0]!.plan?.mcpSavingsUncertain).toBe(true)
expect(preview).toContain('Savings not estimated')
expect(preview).toContain('could not parse')
expect(preview).not.toContain('~10.0K tokens')
})
})
describe('mcp-project-scope plan', () => {
@ -418,6 +585,80 @@ async function threeFindingFixture(): Promise<{ fx: Fixture; findings: WasteFind
}
describe('runOptimizeApply end-to-end', () => {
it('prints connector-only manual guidance when there is nothing to apply', async () => {
const fx = await makeFixture()
const connector: McpServerCoverage = {
server: 'claude_ai_Netlify',
toolsAvailable: 20,
toolsInvoked: 0,
unusedTools: Array.from({ length: 20 }, (_, i) => `mcp__claude_ai_Netlify__t${i}`),
invocations: 0,
loadedSessions: 2,
coverageRatio: 0,
}
const finding = detectMcpToolCoverage([], [connector])!
const io = makeIo()
await runOptimizeApply([], undefined, applyOpts(fx, io, { findings: [finding], yes: true }))
expect(io.stdout()).toContain('No appliable config-class fixes')
expect(io.stdout()).toContain('/mcp')
expect(io.stdout()).toContain('claude.ai Netlify')
expect(io.stdout()).not.toContain('claude mcp remove')
})
it('names the exact local removal target and preserves connector follow-up in a mixed preview', async () => {
const fx = await makeFixture()
await writeFile(join(fx.home, '.claude.json'), JSON.stringify({
mcpServers: { filesystem: { command: 'filesystem' }, netlify: { command: 'local-netlify' } },
}, null, 2) + '\n')
const coverage = (server: string): McpServerCoverage => ({
server,
toolsAvailable: 20,
toolsInvoked: 0,
unusedTools: Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
invocations: 0,
loadedSessions: 2,
coverageRatio: 0,
})
const finding = detectMcpToolCoverage([], [coverage('filesystem'), coverage('claude_ai_Netlify')])!
const io = makeIo()
await runOptimizeApply([], undefined, applyOpts(fx, io, { findings: [finding], dryRun: true }))
expect(io.stdout()).toContain('Removes local MCP server: filesystem')
expect(io.stdout()).toContain('/mcp')
expect(io.stdout()).toContain('claude.ai Netlify')
expect(io.stdout()).not.toContain("claude mcp remove 'claude_ai_Netlify'")
})
it('keeps mixed connector follow-up explicitly pending after applying the local fix', async () => {
const fx = await makeFixture()
await writeFile(join(fx.home, '.claude.json'), JSON.stringify({
mcpServers: { filesystem: { command: 'filesystem' } },
}, null, 2) + '\n')
const coverage = (server: string): McpServerCoverage => ({
server,
toolsAvailable: 20,
toolsInvoked: 0,
unusedTools: Array.from({ length: 20 }, (_, i) => `mcp__${server}__t${i}`),
invocations: 0,
loadedSessions: 2,
coverageRatio: 0,
})
const finding = detectMcpToolCoverage([], [coverage('filesystem'), coverage('claude_ai_Netlify')])!
const io = makeIo()
await runOptimizeApply([], undefined, applyOpts(fx, io, { findings: [finding], yes: true }))
const out = io.stdout()
expect(out).toContain('Manual follow-up (not applied):')
expect(out).toContain('Still requires manual action:')
expect(out).toContain('claude.ai Netlify')
expect(await readRecords(fx.actionsDir)).toHaveLength(1)
expect(JSON.parse(await readFile(join(fx.home, '.claude.json'), 'utf-8')).mcpServers).toEqual({})
})
it('--yes applies every plan and prints journal short ids with the undo hint', async () => {
const { fx, findings } = await threeFindingFixture()
const io = makeIo()
@ -430,11 +671,19 @@ describe('runOptimizeApply end-to-end', () => {
expect(out).toContain(`Applied ${shortId(rec.id)}`)
expect(out).toContain(`Undo anytime: codeburn act undo ${shortId(rec.id)}`)
}
expect(out).toContain('CodeBurn will re-measure these on your next optimize run after 3 days.')
expect(JSON.parse(await readFile(join(fx.home, '.claude.json'), 'utf-8')).mcpServers).toEqual({})
expect(existsSync(join(fx.home, '.claude', 'skills', '.archived', 'foo'))).toBe(true)
expect(existsSync(join(fx.home, '.zshrc'))).toBe(true)
})
it('does not promise a re-measure when nothing was applied', async () => {
const { fx, findings } = await threeFindingFixture()
const io = makeIo('q\n')
await runOptimizeApply([], undefined, applyOpts(fx, io, { findings }))
expect(io.stdout()).not.toContain('re-measure')
})
it('interactive pick "2" applies only the second plan', async () => {
const { fx, findings } = await threeFindingFixture()
const io = makeIo('2\n')
@ -653,3 +902,79 @@ describe('stale-plan detection', () => {
expect(await readFile(p, 'utf-8')).toBe('overwritten')
})
})
describe('finding class', () => {
it('covers every finding id with a class and a basis', () => {
expect(Object.keys(FINDING_BASIS).sort()).toEqual(Object.keys(FINDING_CLASS).sort())
})
it("classes a finding 'fix' exactly when a plan can be built for it", async () => {
const fx = await makeFixture()
const claudeJson = join(fx.home, '.claude.json')
await writeFile(claudeJson, JSON.stringify({ mcpServers: { srv: { command: 's' } } }, null, 2) + '\n')
const settings = join(fx.project, '.claude', 'settings.json')
await mkdir(join(fx.project, '.claude'), { recursive: true })
await writeFile(settings, JSON.stringify({ env: { ENABLE_TOOL_SEARCH: 'auto' } }, null, 2) + '\n')
const mcpJson = join(fx.project, '.mcp.json')
await writeFile(mcpJson, JSON.stringify({ mcpServers: { pinned: { command: 'x', alwaysLoad: true } } }, null, 2) + '\n')
await mkdir(join(fx.home, '.claude', 'skills', 'ghost'), { recursive: true })
await mkdir(join(fx.home, '.claude', 'agents'), { recursive: true })
await mkdir(join(fx.home, '.claude', 'commands'), { recursive: true })
await writeFile(join(fx.home, '.claude', 'agents', 'ghost.md'), 'x')
await writeFile(join(fx.home, '.claude', 'commands', 'ghost.md'), 'x')
const CLAUDE_MD_FIX: WasteAction = { type: 'paste', destination: 'claude-md', label: '', text: 'rule' }
const SHELL_FIX: WasteAction = { type: 'paste', destination: 'shell-config', label: '', text: 'export X=1' }
const PROMPT_FIX: WasteAction = { type: 'paste', destination: 'prompt', label: '', text: 'ask' }
const OPENER_FIX: WasteAction = { type: 'paste', destination: 'session-opener', label: '', text: 'o' }
// One representative finding per id, carrying the payload its plan
// builder needs. Ids without a builder get a plain prompt fix.
const representatives: Record<FindingId, WasteFinding> = {
'read-edit-ratio': makeFinding('read-edit-ratio', CLAUDE_MD_FIX),
'build-folder-reads': makeFinding('build-folder-reads', CLAUDE_MD_FIX),
'redundant-rereads': makeFinding('redundant-rereads', PROMPT_FIX),
'warmup-heavy': makeFinding('warmup-heavy', SHELL_FIX),
'unused-mcp': makeFinding('unused-mcp', CMD_FIX, { kind: 'mcp-remove', servers: ['srv'] }),
'mcp-low-coverage': makeFinding('mcp-low-coverage', CMD_FIX, { kind: 'mcp-remove', servers: ['srv'] }),
'mcp-project-scope': makeFinding('mcp-project-scope', PROMPT_FIX, {
kind: 'mcp-project-scope',
servers: [{ server: 'srv', keepProjects: [fx.project], removeProjects: [] }],
}),
'mcp-deferral-off': makeFinding('mcp-deferral-off', CMD_FIX, {
kind: 'defer-enable', cause: 'env-false', settingPath: settings, settingScope: 'project settings', value: 'false',
}),
'mcp-alwaysload-hygiene': makeFinding('mcp-alwaysload-hygiene', PROMPT_FIX, {
kind: 'defer-alwaysload',
servers: [{ server: 'pinned', paths: [mcpJson] }],
}),
'mcp-defer-threshold': makeFinding('mcp-defer-threshold', PROMPT_FIX, {
kind: 'defer-threshold', settingPath: settings, settingScope: 'project settings',
value: 'auto', recommendedPercent: 2, removeOverride: false,
}),
'retry-heavy-capabilities': makeFinding('retry-heavy-capabilities', PROMPT_FIX),
'low-worth-sessions': makeFinding('low-worth-sessions', OPENER_FIX),
'context-heavy-sessions': makeFinding('context-heavy-sessions', OPENER_FIX),
'cost-outliers': makeFinding('cost-outliers', OPENER_FIX),
'claude-md-too-long': makeFinding('claude-md-too-long', PROMPT_FIX),
'bash-output-cap': makeFinding('bash-output-cap', SHELL_FIX),
'unused-agents': makeFinding('unused-agents', CMD_FIX, { kind: 'archive', names: ['ghost'] }),
'unused-skills': makeFinding('unused-skills', CMD_FIX, { kind: 'archive', names: ['ghost'] }),
'unused-commands': makeFinding('unused-commands', CMD_FIX, { kind: 'archive', names: ['ghost'] }),
'recurring-context': makeFinding('recurring-context', PROMPT_FIX),
}
const planCtx: PlanContext = { homeDir: fx.home, cwd: fx.project, shell: '/bin/zsh', claudeVersion: () => '2.1.130' }
for (const finding of Object.values(representatives)) {
const hasPlan = planFor(finding, planCtx) !== null
expect([finding.id, hasPlan]).toEqual([finding.id, findingClass(finding) === 'fix'])
}
})
it("drops to 'nudge' when the instance lacks the payload its plan needs", () => {
const finding = makeFinding('mcp-deferral-off', { type: 'paste', destination: 'shell-config', label: '', text: 'x' })
expect(FINDING_CLASS['mcp-deferral-off']).toBe('fix')
expect(findingClass(finding)).toBe('nudge')
expect(planFor(finding)).toBeNull()
})
})

View file

@ -1,4 +1,5 @@
import { describe, it, expect, afterAll, beforeEach, vi } from 'vitest'
import { describe, it, expect, afterAll, afterEach, beforeEach, vi } from 'vitest'
import { Writable } from 'node:stream'
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, utimesSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
@ -20,15 +21,24 @@ import {
detectUnusedMcp,
detectBashBloat,
detectGhostCommands,
detectDuplicateReads,
detectJunkReads,
detectLowReadEditRatio,
loadMcpConfigs,
localMcpServerNames,
scanJsonlFile,
scanAndDetect,
detectRecurringContext,
renderOptimize,
type SessionOpener,
type ToolCall,
} from '../src/optimize.js'
import {
estimateContextBudget,
discoverProjectCwd,
} from '../src/context-budget.js'
import type { ProjectSummary } from '../src/types.js'
import { runOptimizeApply } from '../src/act/optimize-apply.js'
// ============================================================================
// Helpers for filesystem fixtures
@ -170,6 +180,32 @@ describe('loadMcpConfigs', () => {
})
})
describe('localMcpServerNames', () => {
it('adds the ~/.claude.json top-level and per-project servers to the config names', () => {
const root = makeFixtureRoot()
const projectDir = join(root, 'myapp')
mkdirSync(projectDir, { recursive: true })
writeFile(join(projectDir, '.mcp.json'), JSON.stringify({ mcpServers: { fromMcpJson: {} } }))
writeFile(join(FAKE_HOME_FOR_MOCK, '.claude.json'), JSON.stringify({
mcpServers: { 'claude_ai_homegrown': {}, 'plugin:ctx:ctx': {} },
projects: { [projectDir]: { mcpServers: { scoped: {} } } },
}))
const names = localMcpServerNames([projectDir])
expect([...names].sort()).toEqual(['claude_ai_homegrown', 'fromMcpJson', 'plugin_ctx_ctx', 'scoped'])
})
it('contributes no names when ~/.claude.json cannot be parsed', () => {
const root = makeFixtureRoot()
const projectDir = join(root, 'myapp')
mkdirSync(projectDir, { recursive: true })
writeFile(join(FAKE_HOME_FOR_MOCK, '.claude.json'), '{ not valid json')
expect(localMcpServerNames([projectDir]).size).toBe(0)
})
})
describe('detectUnusedMcp', () => {
it('flags servers configured but never called', () => {
const root = makeFixtureRoot()
@ -294,6 +330,78 @@ describe('scanJsonlFile', () => {
expect(result.calls[0].name).toBe('Read')
})
it('marks tool calls from sidechain transcript entries', async () => {
const root = makeFixtureRoot()
const filePath = join(root, 'agent-reviewer.jsonl')
const now = new Date().toISOString()
writeFile(filePath, JSON.stringify({
type: 'assistant', isSidechain: true, timestamp: now,
message: { content: [{ type: 'tool_use', name: 'Edit', input: { file_path: '/x/foo.ts' } }] },
}))
const result = await scanJsonlFile(filePath, 'p1', undefined)
expect(result.calls).toHaveLength(1)
expect(result.calls[0]!.isSidechain).toBe(true)
})
it('classifies every tool call in a transcript when a later large entry marks it as sidechain', async () => {
const root = makeFixtureRoot()
const filePath = join(root, 'agent-reviewer.jsonl')
const now = new Date().toISOString()
const assistant = (name: string, isSidechain?: boolean, padding = '') => JSON.stringify({
type: 'assistant',
...(isSidechain === true ? { isSidechain: true } : {}),
timestamp: now,
cwd: '/x',
padding,
message: {
model: 'claude-sonnet-4-5',
usage: { cache_creation_input_tokens: 1 },
content: [{ type: 'tool_use', name, input: { file_path: `/x/${name}.ts` } }],
},
})
writeFile(filePath, [
JSON.stringify({ type: 'user', timestamp: now, cwd: '/x', message: { content: 'delegate this' } }),
assistant('Read'),
assistant('Edit', true, 'x'.repeat(40_000)),
assistant('Bash'),
].join('\n'))
const result = await scanJsonlFile(filePath, 'p1', undefined)
expect(result.calls.map(call => [call.name, call.isSidechain])).toEqual([
['Read', true],
['Edit', true],
['Bash', true],
])
expect(result.apiCalls).toHaveLength(3)
expect(result.cwds).toHaveLength(4)
expect(result.userMessages).toEqual(['delegate this'])
})
it('keeps sidechain calls out of duplicate reads but in junk reads and the read:edit ratio', () => {
const sidechain = { sessionId: 'agent-reviewer', project: 'p1', isSidechain: true }
const editCalls = Array.from({ length: 10 }, (_, index) => ({
name: 'Edit', input: { file_path: `/src/${index}.ts` }, ...sidechain,
}))
const junkReads = Array.from({ length: 6 }, () => ({
name: 'Read', input: { file_path: '/app/node_modules/pkg/index.js' }, ...sidechain,
}))
const repeatReads = Array.from({ length: 6 }, () => ({
name: 'Read', input: { file_path: '/app/src/a.ts' }, ...sidechain,
}))
// A subagent editing without reading, or reading into node_modules, is the
// same waste as the parent doing it, and the CLAUDE.md rule both suggest
// binds subagents too - so the full call population feeds them.
expect(detectLowReadEditRatio(editCalls)?.id).toBe('read-edit-ratio')
expect(detectJunkReads(junkReads)?.id).toBe('build-folder-reads')
// A re-read is only waste when the context already held the file; a
// sidechain starts fresh and has to read it.
expect(detectDuplicateReads(repeatReads)).toBeNull()
})
it('skips malformed JSONL lines without crashing', async () => {
const root = makeFixtureRoot()
const filePath = join(root, 'session.jsonl')
@ -359,6 +467,134 @@ describe('scanJsonlFile', () => {
})
})
// ============================================================================
// detectRecurringContext
// ============================================================================
describe('detectRecurringContext', () => {
// ~2.2 KB, comfortably over the 1.5 KB floor.
const BRIEF = 'PROJECT BRIEF\n' + 'Ship the invoice importer behind a flag. '.repeat(53)
const TOKENS_PER_CHAR = 0.25
type Opener = { text: string; project?: string }
async function openersFor(sessions: Opener[]): Promise<SessionOpener[]> {
const root = makeFixtureRoot()
const now = new Date().toISOString()
const openers: SessionOpener[] = []
for (const [i, { text, project }] of sessions.entries()) {
const filePath = join(root, `s${i}.jsonl`)
writeFile(filePath, JSON.stringify({ type: 'user', timestamp: now, message: { content: text } }))
const result = await scanJsonlFile(filePath, project ?? 'my-app', undefined)
openers.push(...result.openers)
}
return openers
}
const repeat = (n: number, text = BRIEF, project?: string): Opener[] =>
Array.from({ length: n }, () => ({ text, project }))
it('flags a block that opens the session threshold worth of sessions', async () => {
const finding = detectRecurringContext(await openersFor(repeat(5)))
expect(finding).not.toBeNull()
expect(finding!.id).toBe('recurring-context')
expect(finding!.title).toBe(`Same ${(BRIEF.length / 1024).toFixed(1)} KB block pasted at the start of 5 sessions`)
// Only the four repeats are recoverable; the first paste is the honest cost.
expect(finding!.tokensSaved).toBe(Math.round(4 * BRIEF.length * TOKENS_PER_CHAR))
expect(finding!.fix.type).toBe('paste')
expect(finding!.fix.type === 'paste' && finding!.fix.destination).toBe('prompt')
})
it('renders the finding with its preview and destination header', async () => {
const finding = detectRecurringContext(await openersFor(repeat(5)))!
const out = renderOptimize([finding], 0.00001, '30 Days', 10, 5, 100, 80, 'B', [], [])
.replace(/\u001b\[[0-9;]*m/g, '')
expect(out).toContain(finding.title)
expect(out).toContain('PROJECT BRIEF Ship the invoice importer')
expect(out).toContain('Ask Claude in the current session')
expect(out).toContain('Move it into CLAUDE.md if it is a standing rule')
})
it('stays quiet below the session threshold', async () => {
expect(detectRecurringContext(await openersFor(repeat(4)))).toBeNull()
})
it('ignores short openers however often they repeat', async () => {
expect(detectRecurringContext(await openersFor(repeat(20, 'continue')))).toBeNull()
expect(detectRecurringContext(await openersFor(repeat(20, 'yes')))).toBeNull()
})
it('groups sessions whose block differs only in whitespace', async () => {
const reflowed = BRIEF.replace(/ /g, ' ').replace(/\n/g, '\n\n')
const finding = detectRecurringContext(await openersFor([...repeat(3), ...repeat(2, reflowed)]))
expect(finding).not.toBeNull()
expect(finding!.title).toContain('start of 5 sessions')
})
it('ignores injected system reminders and slash-command wrappers', async () => {
expect(detectRecurringContext(await openersFor(repeat(6, `<system-reminder>${BRIEF}</system-reminder>`)))).toBeNull()
expect(detectRecurringContext(await openersFor(repeat(6, `<command-name>/brief</command-name>${BRIEF}`)))).toBeNull()
})
it('skips prompts a program wrote: SDK sessions and subagent transcripts', async () => {
const root = makeFixtureRoot()
const now = new Date().toISOString()
const openers: SessionOpener[] = []
for (const [i, entry] of [{ promptSource: 'sdk' }, { isSidechain: true }].entries()) {
for (let j = 0; j < 6; j++) {
const filePath = join(root, `machine-${i}-${j}.jsonl`)
writeFile(filePath, JSON.stringify({ type: 'user', timestamp: now, ...entry, message: { content: BRIEF } }))
openers.push(...(await scanJsonlFile(filePath, 'my-app', undefined)).openers)
}
}
expect(openers).toEqual([])
expect(detectRecurringContext(openers)).toBeNull()
})
// Over 32 KB the JSONL parser returns a reduced entry; the root flags are
// part of that reduction, so the markers survive.
it('skips machine-written prompts on lines too large for a full parse', async () => {
const root = makeFixtureRoot()
const now = new Date().toISOString()
const huge = BRIEF + 'x'.repeat(40_000)
const openers: SessionOpener[] = []
for (let i = 0; i < 6; i++) {
const filePath = join(root, `huge-${i}.jsonl`)
writeFile(filePath, JSON.stringify({
isSidechain: false, type: 'user', message: { content: huge }, timestamp: now, promptSource: 'sdk',
}))
openers.push(...(await scanJsonlFile(filePath, 'my-app', undefined)).openers)
}
expect(openers).toEqual([])
})
it('only counts the first message of a session as its opener', async () => {
const root = makeFixtureRoot()
const now = new Date().toISOString()
const openers: SessionOpener[] = []
for (let i = 0; i < 6; i++) {
const filePath = join(root, `late-${i}.jsonl`)
writeFile(filePath, [
JSON.stringify({ type: 'user', timestamp: now, message: { content: 'go on' } }),
JSON.stringify({ type: 'user', timestamp: now, message: { content: BRIEF } }),
].join('\n'))
openers.push(...(await scanJsonlFile(filePath, 'my-app', undefined)).openers)
}
expect(detectRecurringContext(openers)).toBeNull()
})
it('names the project when the block is confined to one, and counts them otherwise', async () => {
const single = detectRecurringContext(await openersFor(repeat(5, BRIEF, '-Users-me-Projects-codeburn')))
expect(single!.explanation).toContain('5 sessions in codeburn')
const spread = detectRecurringContext(await openersFor([
...repeat(3, BRIEF, '-Users-me-Projects-codeburn'),
...repeat(2, BRIEF, '-Users-me-Projects-dash'),
]))
expect(spread!.explanation).toContain('5 sessions in 2 projects')
})
})
// ============================================================================
// scanAndDetect (top-level integration)
// ============================================================================
@ -371,6 +607,94 @@ describe('scanAndDetect', () => {
expect(result.healthGrade).toBe('A')
expect(result.costRate).toBe(0)
})
// The session scan only ever reads Claude Code transcripts, so under a
// non-Claude --provider it used to report Claude-derived findings beside a
// header scoped to the other provider - e.g. `optimize --provider codex`
// printing a read/edit ratio counted from Claude sessions.
describe('provider scoping', () => {
// These fixtures live in the shared fake home, so they have to come back
// out: later suites in this file assert on an otherwise empty ~/.claude.
const CLAUDE_DIR = join(FAKE_HOME_FOR_MOCK, '.claude')
afterEach(() => {
for (const sub of ['projects', 'skills']) {
rmSync(join(CLAUDE_DIR, sub), { recursive: true, force: true })
}
})
function claudeSessionWithEditHeavyTurns(): void {
const projectDir = join(CLAUDE_DIR, 'projects', 'provider-scope')
mkdirSync(projectDir, { recursive: true })
const now = new Date().toISOString()
const entry = (name: string, file: string) => JSON.stringify({
type: 'assistant', timestamp: now,
message: { content: [{ type: 'tool_use', name, input: { file_path: file } }] },
})
const lines = [entry('Read', '/src/a.ts')]
for (let i = 0; i < 12; i++) lines.push(entry('Edit', `/src/f${i}.ts`))
writeFileSync(join(projectDir, 'session.jsonl'), lines.join('\n'))
}
// scanAndDetect memoises on (provider, range, project fingerprint) for 60s,
// and the cache is module-level, so tests that differ only in what is on
// disk would serve each other's results. `seed` moves the fingerprint so
// each case scans for real.
function projectFixture(seed: number): ProjectSummary {
return {
project: 'provider-scope',
projectPath: '/tmp/provider-scope',
sessions: [],
totalCostUSD: 1,
totalApiCalls: 13 + seed,
} as unknown as ProjectSummary
}
it('reports transcript-derived findings when scoped to claude', async () => {
claudeSessionWithEditHeavyTurns()
const result = await scanAndDetect([projectFixture(1)], undefined, 'claude')
expect(result.findings.map(f => f.id)).toContain('read-edit-ratio')
})
it('omits transcript-derived findings when scoped to another provider', async () => {
claudeSessionWithEditHeavyTurns()
mkdirSync(join(CLAUDE_DIR, 'skills', 'never-invoked'), { recursive: true })
writeFileSync(join(CLAUDE_DIR, 'skills', 'never-invoked', 'SKILL.md'), '# skill\n')
const result = await scanAndDetect([projectFixture(2)], undefined, 'codex')
const ids = result.findings.map(f => f.id)
expect(ids).not.toContain('read-edit-ratio')
// An unmeasured skill must not be reported as an unused one: the scan
// returns nothing under this filter, which is not evidence of disuse.
expect(ids).not.toContain('unused-skills')
})
// The apply path reaches scanAndDetect through its own entry point, so it
// needs its own guard: `unused-skills` is appliable, and its plan moves
// directories out of ~/.claude/skills. Reporting a Codex-labelled finding
// is a wrong number; offering to archive every skill off one is a wrong
// number with side effects.
async function applyDryRun(provider: string): Promise<string> {
const chunks: string[] = []
const output = new Writable({ write(c, _e, cb) { chunks.push(String(c)); cb() } })
const errorOutput = new Writable({ write(_c, _e, cb) { cb() } })
await runOptimizeApply([projectFixture(3)], undefined, { provider, dryRun: true, output, errorOutput })
return chunks.join('')
}
it('plans no applies from Claude findings when scoped to another provider', async () => {
claudeSessionWithEditHeavyTurns()
mkdirSync(join(CLAUDE_DIR, 'skills', 'never-invoked'), { recursive: true })
writeFileSync(join(CLAUDE_DIR, 'skills', 'never-invoked', 'SKILL.md'), '# skill\n')
const codex = await applyDryRun('codex')
expect(codex).toContain('No appliable config-class fixes')
expect(codex).not.toContain('never-invoked')
const claude = await applyDryRun('claude')
expect(claude).toContain('never-invoked')
})
})
})
// ============================================================================

View file

@ -0,0 +1,341 @@
import { describe, expect, it, vi } from 'vitest'
vi.mock('../src/providers/index.js', async (importOriginal) => {
type ProvidersModule = typeof import('../src/providers/index.js')
const actual = await importOriginal<ProvidersModule>()
return {
...actual,
async discoverAllSessions() {
return []
},
}
})
import {
buildOptimizeJsonReport,
cacheKey,
computeInputCostRate,
detectCapabilityReliability,
detectSessionOutliers,
findContextBloatCandidates,
findLowWorthCandidates,
runOptimize,
scanAndDetect,
type OptimizeResult,
} from '../src/optimize.js'
import type { ClassifiedTurn, ProjectSummary, SessionSummary } from '../src/types.js'
function behavioralTurn(
model: string,
index: number,
options: { retries?: number; costUSD?: number; userMessage?: string } = {},
): ClassifiedTurn {
const timestamp = new Date(Date.parse('2026-08-01T10:00:00.000Z') + index * 1_000).toISOString()
return {
userMessage: options.userMessage ?? 'edit the code',
timestamp,
sessionId: 'agent-behavior',
category: 'feature',
retries: options.retries ?? 0,
hasEdits: true,
assistantCalls: [{
provider: 'claude',
model,
usage: {
inputTokens: 100,
outputTokens: 50,
cacheCreationInputTokens: 0,
cacheReadInputTokens: 0,
cachedInputTokens: 0,
reasoningTokens: 0,
webSearchRequests: 0,
},
costUSD: options.costUSD ?? 1,
tools: ['Edit'],
mcpTools: [],
skills: [],
subagentTypes: [],
hasAgentSpawn: false,
hasPlanMode: false,
speed: 'standard',
timestamp,
bashCommands: [],
deduplicationKey: `${model}-${index}`,
}],
}
}
function session(
sessionId: string,
overrides: Partial<SessionSummary> = {},
): SessionSummary {
return {
sessionId,
project: 'app',
firstTimestamp: '2026-08-01T10:00:00.000Z',
lastTimestamp: '2026-08-01T10:30:00.000Z',
totalCostUSD: 1,
totalSavingsUSD: 0,
totalInputTokens: 1_000,
totalOutputTokens: 1_000,
totalReasoningTokens: 0,
totalCacheReadTokens: 0,
totalCacheWriteTokens: 0,
apiCalls: 1,
turns: [],
modelBreakdown: {},
toolBreakdown: {},
mcpBreakdown: {},
bashBreakdown: {},
categoryBreakdown: {} as SessionSummary['categoryBreakdown'],
skillBreakdown: {},
subagentBreakdown: {},
...overrides,
}
}
function sidechain(
sessionId: string,
overrides: Partial<SessionSummary> = {},
): SessionSummary {
return session(sessionId, {
isSidechain: true,
parentSessionId: 'parent-session',
agentId: sessionId.replace(/^agent-/, ''),
...overrides,
})
}
function project(sessions: SessionSummary[]): ProjectSummary {
return {
project: 'app',
projectPath: '/tmp/app',
sessions,
totalCostUSD: sessions.reduce((sum, item) => sum + item.totalCostUSD, 0),
totalSavingsUSD: sessions.reduce((sum, item) => sum + item.totalSavingsUSD, 0),
totalApiCalls: sessions.reduce((sum, item) => sum + item.apiCalls, 0),
totalProxiedCostUSD: 0,
}
}
describe('optimize sidechain population (issue #974)', () => {
it('does not recommend a model default from sidechain-only edit behavior', async () => {
const sonnetTurns = Array.from({ length: 35 }, (_, index) =>
behavioralTurn('claude-sonnet-4-20250514', index, {
retries: index >= 32 ? 1 : 0,
costUSD: 2,
}))
const haikuTurns = Array.from({ length: 32 }, (_, index) =>
behavioralTurn('claude-haiku-3-5-20241022', index + 35, {
retries: index >= 29 ? 1 : 0,
costUSD: 0.9,
}))
const child = sidechain('agent-behavior', { turns: [...sonnetTurns, ...haikuTurns] })
const projects = [project([child])]
const result = await scanAndDetect(projects, {
start: new Date('2026-08-01T00:00:00.000Z'),
end: new Date('2026-08-02T00:00:00.000Z'),
})
expect(result.modelRecommendations).toEqual([])
})
it('does not emit coaching from sidechain-only correction behavior', () => {
const turns = Array.from({ length: 66 }, (_, index) =>
behavioralTurn('claude-sonnet-4-20250514', index, {
userMessage: index === 0 ? 'review the code' : 'you missed the edge case',
}))
const child = sidechain('agent-corrections', {
totalCostUSD: 9,
totalInputTokens: 6_600,
totalOutputTokens: 3_300,
apiCalls: 66,
turns,
})
const projects = [project([child])]
const result: OptimizeResult = {
findings: [],
costRate: computeInputCostRate(projects),
healthScore: 100,
healthGrade: 'A',
modelRecommendations: [],
}
const report = buildOptimizeJsonReport(projects, 'fixture', result)
expect(report.coachingNotes).toEqual([])
expect(report.summary.periodCostUSD).toBe(9)
expect(report.summary.calls).toBe(66)
})
it('does not report retry-heavy capabilities from sidechain-only edits', () => {
const turns = Array.from({ length: 5 }, (_, index) => {
const item = behavioralTurn('claude-sonnet-4-20250514', index, {
retries: index < 3 ? 1 : 0,
})
item.assistantCalls[0]!.skills = ['reviewer']
return item
})
const child = sidechain('agent-capability', { turns })
expect(detectCapabilityReliability([project([child])])).toBeNull()
})
it('keeps sidechain spend out of the low-worth candidate population', () => {
const parent = session('parent', {
totalCostUSD: 4,
turns: [],
})
const child = sidechain('agent-child', {
totalCostUSD: 12,
turns: [],
})
expect(findLowWorthCandidates([project([parent, child])]).map(item => item.sessionId))
.toEqual(['parent'])
})
it('does not use a sidechain as a context-heavy candidate or growth baseline', () => {
const baseline = session('parent-baseline', {
firstTimestamp: '2026-08-01T10:00:00.000Z',
totalInputTokens: 20_000,
totalOutputTokens: 2_000,
})
const child = sidechain('agent-child', {
firstTimestamp: '2026-08-02T10:00:00.000Z',
totalInputTokens: 200_000,
totalOutputTokens: 100,
})
const candidate = session('parent-candidate', {
firstTimestamp: '2026-08-03T10:00:00.000Z',
totalInputTokens: 100_000,
totalOutputTokens: 2_000,
})
const candidates = findContextBloatCandidates([project([baseline, child, candidate])])
expect(candidates.map(item => item.sessionId)).toEqual(['parent-candidate'])
expect(candidates[0]!.growthRatio).toBe(5)
})
it('does not let a sidechain satisfy the peer-sample minimum for cost outliers', () => {
const sessions = [
session('parent-cheap', { totalCostUSD: 1 }),
session('parent-expensive', { totalCostUSD: 10 }),
sidechain('agent-cheap', { totalCostUSD: 1 }),
]
expect(detectSessionOutliers([project(sessions)])).toBeNull()
})
it('never reports an expensive sidechain as a parent-session cost outlier', () => {
const sessions = [
session('parent-1', { totalCostUSD: 1 }),
session('parent-2', { totalCostUSD: 1 }),
session('parent-3', { totalCostUSD: 1 }),
sidechain('agent-expensive', { totalCostUSD: 100 }),
]
expect(detectSessionOutliers([project(sessions)])).toBeNull()
})
it('counts only parent sessions while conserving sidechain cost, calls, and tokens', () => {
const projects = [project([
session('parent', {
totalCostUSD: 3,
totalInputTokens: 100,
totalOutputTokens: 20,
apiCalls: 2,
}),
sidechain('agent-child', {
totalCostUSD: 7,
totalInputTokens: 900,
totalOutputTokens: 80,
apiCalls: 4,
}),
])]
const result: OptimizeResult = {
findings: [],
costRate: computeInputCostRate(projects),
healthScore: 100,
healthGrade: 'A',
}
const report = buildOptimizeJsonReport(projects, 'fixture', result)
expect(report.summary.sessions).toBe(1)
expect(report.summary.periodCostUSD).toBe(10)
expect(report.summary.calls).toBe(6)
// Input-cost calibration keeps all spend and all input/cache tokens:
// ($10 * 0.7) / (100 + 900) tokens.
expect(report.summary.costRateUSD).toBeCloseTo(0.007, 12)
})
it('keeps sidechain spend out of every per-session finding in the optimize pipeline', async () => {
const projects = [project([
sidechain('agent-only', {
totalCostUSD: 100,
totalInputTokens: 1_000_000,
totalOutputTokens: 100,
}),
])]
const result = await scanAndDetect(projects, {
start: new Date('2026-08-01T00:00:00.000Z'),
end: new Date('2026-08-02T00:00:00.000Z'),
})
expect(result.findings.map(finding => finding.id)).not.toContain('low-worth-sessions')
expect(result.findings.map(finding => finding.id)).not.toContain('context-heavy-sessions')
expect(result.findings.map(finding => finding.id)).not.toContain('cost-outliers')
})
it('uses the parent-session count in the text optimize headline', async () => {
const projects = [project([
session('parent', {
totalCostUSD: 1,
bashBreakdown: { 'git commit -m shipped': { calls: 1 } },
}),
sidechain('agent-child', {
totalCostUSD: 2,
bashBreakdown: { 'git commit -m irrelevant': { calls: 1 } },
}),
])]
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined)
const stderr = vi.spyOn(process.stderr, 'write').mockImplementation(() => true)
try {
await runOptimize(projects, 'fixture', {
start: new Date('2026-08-01T00:00:00.000Z'),
end: new Date('2026-08-02T00:00:00.000Z'),
})
const output = String(log.mock.calls.at(-1)?.[0] ?? '')
expect(output).toContain('1 session')
expect(output).not.toContain('2 sessions')
} finally {
log.mockRestore()
stderr.mockRestore()
}
})
it('separates cached optimize results when only sidechain classification changes', () => {
const range = {
start: new Date('2026-08-01T00:00:00.000Z'),
end: new Date('2026-08-02T00:00:00.000Z'),
}
const parentOnly = project([session('same')])
const sidechainOnly = project([sidechain('same')])
expect(parentOnly.totalCostUSD).toBe(sidechainOnly.totalCostUSD)
expect(parentOnly.totalApiCalls).toBe(sidechainOnly.totalApiCalls)
expect(cacheKey([parentOnly], range)).not.toBe(cacheKey([sidechainOnly], range))
const firstSidechain = project([session('first'), sidechain('second')])
const secondSidechain = project([sidechain('first'), session('second')])
expect(firstSidechain.sessions.length).toBe(secondSidechain.sessions.length)
expect(firstSidechain.totalCostUSD).toBe(secondSidechain.totalCostUSD)
expect(firstSidechain.sessions.filter(item => item.isSidechain).length)
.toBe(secondSidechain.sessions.filter(item => item.isSidechain).length)
expect(cacheKey([firstSidechain], range)).not.toBe(cacheKey([secondSidechain], range))
})
})

View file

@ -26,12 +26,16 @@ import {
computeHealth,
computeTrend,
buildOptimizeJsonReport,
renderOptimize,
findingBasis,
type FindingId,
type ToolCall,
type ApiCallMeta,
type WasteFinding,
type OptimizeResult,
} from '../src/optimize.js'
import type { ProjectSummary } from '../src/types.js'
import type { AppliedFix } from '../src/act/types.js'
function call(name: string, input: Record<string, unknown>, sessionId = 's1', project = 'p1'): ToolCall {
return { name, input, sessionId, project }
@ -52,6 +56,7 @@ function projectWithSessions(costs: number[], project = 'app'): ProjectSummary {
totalCostUSD: cost,
totalInputTokens: tokens,
totalOutputTokens: tokens,
totalReasoningTokens: 0,
totalCacheReadTokens: 0,
totalCacheWriteTokens: 0,
apiCalls: 1,
@ -105,6 +110,7 @@ function contextSession(
totalCostUSD: 1,
totalInputTokens: 0,
totalOutputTokens: 0,
totalReasoningTokens: 0,
totalCacheReadTokens: 0,
totalCacheWriteTokens: 0,
apiCalls: 1,
@ -365,6 +371,18 @@ describe('detectContextBloat', () => {
expect(detectContextBloat([project])).toBeNull()
})
it('counts reasoning with output when measuring context pressure', () => {
const project = projectWithContextSessions([
contextSession(0, {
totalInputTokens: 100_000,
totalOutputTokens: 3_500,
totalReasoningTokens: 2_000,
}),
])
expect(detectContextBloat([project])).toBeNull()
})
it('discounts cache reads when estimating context pressure', () => {
const project = projectWithContextSessions([
contextSession(0, {
@ -1004,6 +1022,29 @@ describe('detectSessionOutliers', () => {
expect(finding!.tokensSaved).toBeGreaterThan(0)
})
it('keeps estimated-cost sessions out of the peer math', () => {
const project = projectWithSessions([1, 1, 1, 10])
// The expensive session is priced from modelled tokens, so it is not
// comparable against the provider-reported peers and never gets flagged.
project.sessions[3]!.totalEstimatedCostUSD = project.sessions[3]!.totalCostUSD
expect(detectSessionOutliers([project])).toBeNull()
})
it('falls back to estimated costs when nothing else is priced, and says so', () => {
const project = projectWithSessions([1, 1, 1, 10])
for (const s of project.sessions) s.totalEstimatedCostUSD = s.totalCostUSD
const finding = detectSessionOutliers([project])
expect(finding).not.toBeNull()
expect(finding!.basis).toBe('estimated')
expect(findingBasis(finding!)).toBe('estimated')
})
it('reports measured basis when every peer cost is provider-reported', () => {
const finding = detectSessionOutliers([projectWithSessions([1, 1, 1, 10])])
expect(finding!.basis).toBeUndefined()
expect(findingBasis(finding!)).toBe('measured')
})
it('ignores tiny absolute-cost outliers', () => {
expect(detectSessionOutliers([projectWithSessions([0.01, 0.01, 0.01, 0.2])])).toBeNull()
})
@ -1197,9 +1238,9 @@ describe('paste-fix destination tagging (issue #277)', () => {
if (f.fix.type === 'paste') {
expect(
f.fix.destination,
`finding "${f.title}" has paste fix without destination — pick one of: claude-md / session-opener / prompt / shell-config`
`finding "${f.title}" has paste fix without destination — pick one of: claude-md / session-opener / prompt / shell-config / manual`
).toBeDefined()
expect(['claude-md', 'session-opener', 'prompt', 'shell-config'])
expect(['claude-md', 'session-opener', 'prompt', 'shell-config', 'manual'])
.toContain(f.fix.destination)
}
}
@ -1240,6 +1281,7 @@ describe('buildOptimizeJsonReport', () => {
healthGrade: 'C',
findings: [
{
id: 'claude-md-too-long',
title: 'Trim stale context',
explanation: 'Old instructions are loaded every turn.',
impact: 'medium',
@ -1283,12 +1325,25 @@ describe('buildOptimizeJsonReport', () => {
potentialSavingsPercent: 20,
costRateUSD: 0.00002,
})
expect(report.summary.measuredSavingsUSD).toBe(0)
expect(report.summary.byClass).toEqual({
fix: { tokensSaved: 0, savingsUSD: 0, count: 0 },
nudge: { tokensSaved: 50_000, savingsUSD: 1, count: 1 },
keep: { tokensSaved: 0, savingsUSD: 0, count: 0 },
})
const classes = Object.values(report.summary.byClass)
expect(classes.reduce((s, c) => s + c.tokensSaved, 0)).toBe(report.summary.potentialSavingsTokens)
expect(classes.reduce((s, c) => s + c.savingsUSD, 0)).toBeCloseTo(report.summary.potentialSavingsCostUSD, 10)
expect(classes.reduce((s, c) => s + c.count, 0)).toBe(report.summary.findingCount)
expect(report.appliedFixes).toEqual([])
expect(report.findings[0]).toMatchObject({
title: 'Trim stale context',
severity: 'medium',
trend: 'active',
tokensSaved: 50_000,
estimatedSavingsUSD: 1,
class: 'nudge',
basis: 'estimated',
fix: {
type: 'paste',
destination: 'claude-md',
@ -1296,3 +1351,95 @@ describe('buildOptimizeJsonReport', () => {
})
})
})
describe('renderOptimize grouping', () => {
const plain = (s: string): string => s.replace(/\[[0-9;]*m/g, '')
function finding(id: FindingId, title: string): WasteFinding {
return {
id,
title,
explanation: 'why',
impact: 'medium',
tokensSaved: 1000,
fix: { type: 'paste', destination: 'prompt', label: 'ask', text: 'ask' },
}
}
it('groups findings under fix / habits / FYI with continuous numbering and a basis split', () => {
const findings = [
finding('bash-output-cap', 'Cap bash output'),
finding('claude-md-too-long', 'Trim CLAUDE.md'),
finding('context-heavy-sessions', 'Context-heavy sessions'),
]
const out = plain(renderOptimize(findings, 0.00001, '7 Days', 10, 5, 100, 80, 'B', [], []))
const headers = [
'Fix now (apply-able) · ~1.0K tokens (~$0.010) · 1 finding — codeburn optimize --apply',
'Habits · ~1.0K tokens (~$0.010) · 1 finding',
'FYI · ~1.0K tokens (~$0.010) · 1 finding',
].map(h => out.indexOf(h))
expect(headers.every(i => i >= 0)).toBe(true)
expect(headers).toEqual([...headers].sort((a, b) => a - b))
// Headline is the whole board; the apply-able slice is named separately.
expect(out).toContain('Potential savings: ~3.0K tokens (~$0.030, ~0.3% of spend) — apply-able: ~$0.010')
expect(out).toContain('1. Cap bash output')
expect(out).toContain('2. Trim CLAUDE.md')
expect(out).toContain('3. Context-heavy sessions')
expect(out).toContain('1 measured · 2 estimated')
expect(out).not.toContain('Estimates only.')
})
})
describe('renderOptimize applied-fixes section', () => {
const plain = (s: string): string => s.replace(/\[[0-9;]*m/g, '')
function fixture(over: Partial<AppliedFix>): AppliedFix {
return {
id: 'abcdef12',
kind: 'mcp-remove',
findingId: 'unused-mcp',
appliedAt: '2026-05-01T00:00:00.000Z',
ageDays: 4,
verdict: 'worked',
estimatedTokens: 300_000,
realizedTokens: 280_000,
note: '',
undoCommand: 'codeburn act undo abcdef12',
...over,
}
}
const findings: WasteFinding[] = [{
id: 'bash-output-cap',
title: 'Cap bash output',
explanation: 'why',
impact: 'medium',
tokensSaved: 1000,
fix: { type: 'paste', destination: 'prompt', label: 'ask', text: 'ask' },
}]
const render = (appliedFixes: AppliedFix[], f = findings): string =>
plain(renderOptimize(f, 0.00001, '7 Days', 10, 5, 100, 80, 'B', [], [], undefined, undefined, undefined, appliedFixes))
it('renders one line per verdict with its own glyph', () => {
const out = render([
fixture({}),
fixture({ id: 'b', findingId: 'mcp-defer-threshold', verdict: 'partial', ageDays: 3, estimatedTokens: 600_000, realizedTokens: 420_000 }),
fixture({ id: 'c', findingId: 'bash-output-cap', verdict: 'no-effect', ageDays: 5, estimatedTokens: 41_000, realizedTokens: 0, undoCommand: 'codeburn act undo cccccccc' }),
fixture({ id: 'd', findingId: 'mcp-remove-linear', verdict: 'pending', ageDays: 1 }),
])
expect(out).toContain('Applied fixes')
expect(out).toContain('\u2713 unused-mcp (4d ago): est. 300.0K -> measured 280.0K')
expect(out).toContain('~ mcp-defer-threshold (3d ago): est. 600.0K -> measured 420.0K (-30% vs estimate)')
expect(out).toContain('\u2717 bash-output-cap (5d ago): est. 41.0K -> measured 0 - did not help. Revert: codeburn act undo cccccccc')
expect(out).toContain('\u2026 mcp-remove-linear (1d ago): measuring, check back after 3 days')
})
it('shows the section on a clean setup too, and omits it when nothing is applied', () => {
expect(render([fixture({})], [])).toContain('Applied fixes')
expect(render([])).not.toContain('Applied fixes')
expect(render([], [])).not.toContain('Applied fixes')
})
})

View file

@ -21,6 +21,7 @@ function largeUserLine(): string {
function largeAssistantLine(): string {
return JSON.stringify({
type: 'assistant',
isSidechain: true,
sessionId: 's1',
timestamp: '2026-05-01T00:00:01Z',
cwd: '/repo',
@ -43,7 +44,25 @@ function largeAssistantLine(): string {
})
}
// The fields sit either side of the message that makes the line large, which
// is where a generated prompt puts them in the wild.
function largeMachineWrittenLine(): string {
return JSON.stringify({
isSidechain: true,
type: 'user',
message: { role: 'user', content: 'brief ' + 'x'.repeat(40_000) },
timestamp: '2026-05-01T00:00:00Z',
promptSource: 'sdk',
})
}
describe('large JSONL compact scanner', () => {
it('keeps the flags marking a program-written prompt', () => {
const parsed = parseJsonlLine(largeMachineWrittenLine())
expect(parsed?.promptSource).toBe('sdk')
expect(parsed?.isSidechain).toBe(true)
})
it('extracts user text from array content without full JSON.parse', () => {
const parsed = parseJsonlLine(largeUserLine())
expect(parsed?.type).toBe('user')
@ -55,6 +74,7 @@ describe('large JSONL compact scanner', () => {
it('extracts capped tool inputs needed by optimize', () => {
const parsed = parseJsonlLine(Buffer.from(largeAssistantLine()))
expect(parsed?.isSidechain).toBe(true)
const msg = parsed?.message
expect(msg?.role).toBe('assistant')
if (msg?.role !== 'assistant') return

View file

@ -65,7 +65,7 @@ function assistantLine(sessionId: string, timestamp: string, messageId: string,
function messageFirstLargeAssistantLine(sessionId: string, timestamp: string, messageId: string): string {
const hugeText = 'y'.repeat(3_000_000)
return `{"parentUuid":"u1","isSidechain":false,"message":{"model":"claude-sonnet-4-5","id":"${messageId}","type":"message","role":"assistant","content":[{"type":"text","text":"${hugeText}"},{"type":"tool_use","id":"tu-large","name":"Edit","input":{"file_path":"/tmp/x","old_string":"a","new_string":"b"}}],"usage":{"input_tokens":1000,"output_tokens":100,"cache_read_input_tokens":5000}},"uuid":"a1","timestamp":"${timestamp}","type":"assistant","sessionId":"${sessionId}","cwd":"/projects/app"}`
return `{"parentUuid":"u1","isSidechain":true,"message":{"model":"claude-sonnet-4-5","id":"${messageId}","type":"message","role":"assistant","content":[{"type":"text","text":"${hugeText}"},{"type":"tool_use","id":"tu-large","name":"Edit","input":{"file_path":"/tmp/x","old_string":"a","new_string":"b"}}],"usage":{"input_tokens":1000,"output_tokens":100,"cache_read_input_tokens":5000}},"uuid":"a1","timestamp":"${timestamp}","type":"assistant","sessionId":"${sessionId}","cwd":"/projects/app"}`
}
function attachmentLine(sessionId: string, timestamp: string): string {
@ -227,6 +227,7 @@ describe('parseAllSessions with large Claude fixture', () => {
expect(projects.length).toBeGreaterThan(0)
const sess = projects[0]!.sessions[0]!
expect(sess.isSidechain).toBe(true)
expect(sess.apiCalls).toBe(1)
expect(sess.totalInputTokens).toBe(1000)
expect(sess.totalOutputTokens).toBe(100)

View file

@ -4,6 +4,7 @@ import { join } from 'path'
import { tmpdir } from 'os'
import { parseAllSessions, filterProjectsByDays, clearSessionCache } from '../src/parser.js'
import { clearLoadCacheMemo } from '../src/session-cache.js'
import { loadPricing } from '../src/models.js'
import { aggregateByPr, prLinkedTotals } from '../src/sessions-report.js'
@ -64,8 +65,16 @@ describe('subagent fold across a date-range boundary', () => {
const projects = await parseAllSessions(range, 'claude')
// The child session is present (its work is in range) as a standalone session.
const childPresent = projects.some(p => p.sessions.some(s => s.sessionId === `agent-${AGENT}`))
expect(childPresent).toBe(true)
const child = projects.flatMap(p => p.sessions).find(s => s.sessionId === `agent-${AGENT}`)
expect(child).toBeDefined()
expect(child!.isSidechain).toBe(true)
// Drop both in-process memo layers so the second parse reloads the persisted
// session cache. The marker must survive that warm-disk path too, not only
// the cold transcript parse.
clearSessionCache()
clearLoadCacheMemo()
const warmProjects = await parseAllSessions(range, 'claude')
expect(warmProjects.flatMap(p => p.sessions).find(s => s.sessionId === `agent-${AGENT}`)?.isSidechain).toBe(true)
// The anchor parent (0 in-range turns) must NOT contaminate the sessions list;
// it lives in subagentAnchors only.
const anchorInSessions = projects.some(p => p.sessions.some(s => s.sessionId === PARENT))
@ -101,6 +110,7 @@ describe('subagent fold across a date-range boundary', () => {
const dayFiltered = filterProjectsByDays(projects, new Set(['2026-07-20']))
expect(dayFiltered.some(p => p.sessions.some(s => s.sessionId === PARENT))).toBe(false) // parent no longer a session
expect(dayFiltered.some(p => (p.subagentAnchors ?? []).some(s => s.sessionId === PARENT))).toBe(true) // kept as anchor
expect(dayFiltered.flatMap(p => p.sessions).find(s => s.sessionId === `agent-${AGENT}`)?.isSidechain).toBe(true)
// The child's spend still folds to the PR through the anchor.
const row = aggregateByPr(dayFiltered).find(r => r.url === PR)

View file

@ -4,8 +4,8 @@
// (b) OTel-prune monotonic — OTel DB rows pruned → total unchanged
// (c) no double-count — same source parsed twice → counted once
// (d) non-durable evicts — deleted source for non-durable provider IS removed
// (e) 90-day age-out — ≥ 91d pruned (orphans AND unflagged discovered
// sources); retainWhilePresent + discovered kept
// (e) 90-day age-out — only ORPHANS ≥ 91d are pruned (#992); a still-
// discovered source stays, flagged or not
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mkdtemp, mkdir, writeFile, rm, unlink } from 'fs/promises'
@ -28,6 +28,7 @@ import type { SessionSource, SessionParser, ParsedProviderCall } from '../src/pr
let _synthSources: SessionSource[] = []
let _synthDurable = false
let _synthYields: ParsedProviderCall[] = []
let _synthParseCalls = 0
let _synthOnParse: (() => void | Promise<void>) | null = null
vi.mock('../src/providers/index.js', async (importOriginal) => {
@ -58,6 +59,7 @@ vi.mock('../src/providers/index.js', async (importOriginal) => {
createSessionParser(_s: SessionSource, _k: Set<string>): SessionParser {
return {
async *parse(): AsyncGenerator<ParsedProviderCall> {
_synthParseCalls++
await _synthOnParse?.()
for (const call of _synthYields) {
// Respect seenKeys so that when multiple sources share the same
@ -197,6 +199,7 @@ beforeEach(async () => {
_synthSources = []
_synthDurable = false
_synthYields = []
_synthParseCalls = 0
_synthOnParse = null
})
@ -364,7 +367,7 @@ describe('(d) non-durable provider evicts deleted sources', () => {
// (e) 90-day age-out: orphan ≥ 91d old is pruned; ≤ 89d is retained
// ═══════════════════════════════════════════════════════════════════════════
describe('(e) 90-day age-out for durable providers', () => {
it('prunes a 91-day-old entry even while its unflagged source is still discovered', async () => {
it('keeps a discovered 91-day source persisted until discovery removes it', async () => {
const synthFile = join(tmpHome, 'synth-age.txt')
await writeFile(synthFile, 'placeholder')
@ -384,17 +387,76 @@ describe('(e) 90-day age-out for durable providers', () => {
userMessage: 'old', sessionId: 'synth-old',
}]
// Unflagged durable sources age out on the ordinary schedule even while
// the file remains on disk (the pre-existing cap on cache growth for
// provider-pruned journals). Only a retainWhilePresent source — the
// copilot session-store, which IS the durable record — is exempt.
// First refresh: a still-discovered durable source is live and persisted,
// regardless of the age of its newest call.
const proj1 = await parseAllSessions(undefined, 'test-synthetic')
expect(totalOutput(proj1)).toBe(0)
expect.soft(totalOutput(proj1)).toBe(8)
expect.soft(_synthParseCalls).toBe(1)
// Stable on the next pass too (re-parsed, then aged out again).
const cache1 = await loadCache()
const persisted1 = cache1.providers['test-synthetic']?.files[synthFile]
expect.soft(persisted1).toBeDefined()
// Second refresh: force the public seam through the persisted cache. The
// unchanged fingerprint must serve the cached parse without invoking the
// provider parser again.
clearSessionCache()
const proj2 = await parseAllSessions(undefined, 'test-synthetic')
expect(totalOutput(proj2)).toBe(0)
expect.soft(totalOutput(proj2)).toBe(8)
expect.soft(_synthParseCalls).toBe(1)
const cache2 = await loadCache()
expect.soft(cache2.providers['test-synthetic']?.files[synthFile]?.fingerprint)
.toEqual(persisted1?.fingerprint)
// Third refresh: once discovery removes the old source, it becomes an
// orphan and the durable 90-day age-out prunes it from results and disk.
clearSessionCache()
_synthSources = []
const proj3 = await parseAllSessions(undefined, 'test-synthetic')
expect.soft(totalOutput(proj3)).toBe(0)
const cache3 = await loadCache()
expect.soft(cache3.providers['test-synthetic']?.files[synthFile]).toBeUndefined()
})
it('keeps a discovered 91-day source through a month-scoped refresh', async () => {
const synthFile = join(tmpHome, 'synth-scoped.txt')
await writeFile(synthFile, 'placeholder')
const ts91dAgo = new Date(Date.now() - 91 * 24 * 60 * 60 * 1000).toISOString()
_synthDurable = true
_synthSources = [{ path: synthFile, project: 'test', provider: 'test-synthetic' }]
_synthYields = [{
provider: 'test-synthetic', model: 'gpt-4o',
inputTokens: 10, outputTokens: 8,
cacheCreationInputTokens: 0, cacheReadInputTokens: 0,
cachedInputTokens: 0, reasoningTokens: 0, webSearchRequests: 0,
costUSD: 0.002, tools: [], bashCommands: [],
timestamp: ts91dAgo,
speed: 'standard',
deduplicationKey: 'synth-age-out-91d-scoped',
userMessage: 'old', sessionId: 'synth-old-scoped',
}]
expect.soft(totalOutput(await parseAllSessions(undefined, 'test-synthetic'))).toBe(8)
// A today-ranged refresh loads under a month scope that excludes the entry's
// shard. Durable providers are never scoped, so the age-out still sees the
// entry as discovered and the save must carry its month across intact.
clearSessionCache()
const today = new Date()
const start = new Date(today); start.setHours(0, 0, 0, 0)
const end = new Date(today); end.setHours(23, 59, 59, 999)
expect.soft(totalOutput(await parseAllSessions({ start, end }, 'test-synthetic'))).toBe(0)
clearSessionCache()
expect.soft(totalOutput(await parseAllSessions(undefined, 'test-synthetic'))).toBe(8)
expect.soft(_synthParseCalls).toBe(1)
const cache = await loadCache()
expect.soft(cache.providers['test-synthetic']?.files[synthFile]).toBeDefined()
})
it('retains a 91-day-old entry whose still-discovered source declares retainWhilePresent', async () => {
@ -1285,17 +1347,16 @@ describe.skipIf(!isSqliteAvailable())('(l) age-out exempts still-discovered stor
}
}
// Both runs: the store rows must serve — never zero. The session's
// events.jsonl is itself >90d old and follows the ordinary durable
// schedule (pruned even while on disk), so its per-turn output and
// rollup drop out; only the retainWhilePresent store keeps this
// session's record, exactly the crash-only-rows guarantee the flag
// exists for.
// Both runs: the store rows must serve — never zero. Under the orphan-only
// age-out (#992) the >90d events.jsonl is still discovered, so its per-turn
// output stays too; the rollup's input/cache is reconciled away against the
// rows exactly as on a fresh session, which is what makes the >90d case
// indistinguishable from any other. Idempotent across the cache round-trip.
const first = sumUsage(await parseAllSessions(undefined, 'copilot'))
expect(first).toEqual({ input: 600, cacheRead: 17000, output: 0 })
expect(first).toEqual({ input: 600, cacheRead: 17000, output: 25 })
clearSessionCache()
const second = sumUsage(await parseAllSessions(undefined, 'copilot'))
expect(second).toEqual({ input: 600, cacheRead: 17000, output: 0 })
expect(second).toEqual({ input: 600, cacheRead: 17000, output: 25 })
})
})
@ -2457,9 +2518,11 @@ describe.skipIf(!isSqliteAvailable())('(sc) month-sharded cache integration for
})
_synthDurable = true
_synthSources = [{ path: synthFile, project: 'test', provider: 'test-synthetic' }]
// A fresh-timestamped decoy: if the parse wrongly re-reads the unchanged
// file, this call would serve and break the zero assertion below.
// Orphaned: discovery no longer returns the seeded path, which under the
// orphan-only age-out (#992) is what makes the entry eligible at all.
_synthSources = []
// A fresh-timestamped decoy: if the parse wrongly re-reads the file it
// no longer discovers, this call would serve and break the zero assertion.
_synthYields = [{
provider: 'test-synthetic', model: 'gpt-4o',
inputTokens: 10, outputTokens: 99,

View file

@ -34,6 +34,7 @@ const FILE_PROVIDERS: Record<string, string[]> = {
'codex.ts': ['codex'],
'copilot.ts': ['copilot'],
'droid.ts': ['droid'],
'dsh.ts': ['dsh'],
'hermes.ts': ['hermes'],
'lingtai-tui.ts': ['lingtai-tui'],
// Its only literal read is CODEBURN_CURSOR_MAX_BUBBLES (cursor.ts:692).

View file

@ -14,7 +14,7 @@ function fakeProvider(name: string, discover: Provider['discoverSessions']): Pro
describe('provider registry', () => {
it('has core providers registered synchronously', () => {
expect(providers.map(p => p.name)).toEqual(['claude', 'cline', 'cline-cli', 'codewhale', 'codebuff', 'codex', 'copilot', 'devin', 'droid', 'gemini', 'hermes', 'ibm-bob', 'kilo-code', 'kiro', 'kimi', 'kimicode', 'lingtai-tui', 'mistral-vibe', 'mux', 'openclaw', 'openclaude', 'open-design', 'pi', 'omp', 'qwen', 'quickdesk', 'roo-code', 'zerostack', 'grok'])
expect(providers.map(p => p.name)).toEqual(['claude', 'cline', 'cline-cli', 'codewhale', 'codebuff', 'codex', 'copilot', 'devin', 'droid', 'dsh', 'gemini', 'hermes', 'ibm-bob', 'kilo-code', 'kiro', 'kimi', 'kimicode', 'lingtai-tui', 'mistral-vibe', 'mux', 'openclaw', 'openclaude', 'open-design', 'pi', 'omp', 'qwen', 'quickdesk', 'roo-code', 'zerostack', 'grok'])
})
it('codebuff tool display names normalize codebuff-native names to canonical set', () => {

620
tests/providers/dsh.test.ts Normal file
View file

@ -0,0 +1,620 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { mkdtemp, mkdir, writeFile, readFile, rm, stat } from 'fs/promises'
import { join } from 'path'
import { homedir, tmpdir } from 'os'
import zlib from 'zlib'
import { createDshProvider, readZstdLines } from '../../src/providers/dsh.js'
import { calculateCost } from '../../src/models.js'
import type { ParsedProviderCall } from '../../src/providers/types.js'
// DSH session logs are concatenations of INDEPENDENT zstd frames (one per
// appended event batch), so fixtures must compress each batch separately —
// a single zstdCompressSync over the whole file is a different (single-frame)
// format than what DSH writes.
const zstdCompress = (zlib as { zstdCompressSync?: (buf: Buffer) => Buffer }).zstdCompressSync
// node:zlib gained zstd in 22.15; the package floor (and CI's pinned Node) is
// 22.13. Container-specific tests skip there; the rest fall back to plain jsonl
// so the parsing semantics are still exercised.
const itZstd = zstdCompress ? it : it.skip
let tmpDir: string
beforeEach(async () => {
tmpDir = await mkdtemp(join(tmpdir(), 'dsh-test-'))
})
afterEach(async () => {
await rm(tmpDir, { recursive: true, force: true })
})
function sessionHeader(opts: { id?: string; cwd?: string } = {}) {
return JSON.stringify({
type: 'session',
version: 0,
id: opts.id ?? 'session-00000000-0000-0000-0000-000000000001',
createdAt: 1786707336131,
cwd: opts.cwd ?? 'C:\\Users\\test\\myproject',
delegationDepth: 0,
agentPreset: 'cordis',
})
}
function requestHeader(model: string, time = 1786707337000) {
return JSON.stringify({
type: 'request/header',
seq: 10,
time,
data: { header: { config: { provider: 'deepseek-official', model, reasoningEffort: 'max', maxTokens: 256000 } } },
})
}
function turnStart(turn: number, time: number) {
return JSON.stringify({ type: 'turn/start', seq: 1, time, data: { turn } })
}
function userMessage(text: string, time: number) {
return JSON.stringify({
type: 'user/message',
seq: 2,
time,
data: { content: [{ type: 'text', text }], source: { kind: 'user' }, role: 'user', id: 'msg-1' },
})
}
function chunkUsage(turn: number, step: number, usage: Record<string, number>, time: number) {
return JSON.stringify({
type: 'assistant/chunk',
seq: 3,
time,
data: { turn, step, chunk: { type: 'usage', usage } },
})
}
function assistantMessage(turn: number, step: number, usage: Record<string, number> | undefined, time: number) {
return JSON.stringify({
type: 'assistant/message',
seq: 4,
time,
data: {
turn,
step,
message: { role: 'assistant', content: [{ type: 'text', text: 'done' }] },
...(usage ? { usage } : {}),
},
})
}
function toolCall(turn: number, step: number, name: string, args: Record<string, unknown>, time: number) {
return JSON.stringify({
type: 'tool/call',
seq: 5,
time,
data: { turn, step, callId: `call_${name}`, name, arguments: JSON.stringify(args) },
})
}
// Write one frame per batch of lines, matching DSH's append-per-batch layout.
async function writeZstdSession(projectDirName: string, sessionDirName: string, batches: string[][]) {
const dir = join(tmpDir, 'sessions', projectDirName, sessionDirName)
await mkdir(dir, { recursive: true })
if (!zstdCompress) {
const filePath = join(dir, 'session.jsonl')
await writeFile(filePath, batches.map(lines => lines.join('\n') + '\n').join(''))
return filePath
}
const filePath = join(dir, 'session.jsonl.zstd')
const frames = batches.map(lines => zstdCompress(Buffer.from(lines.join('\n') + '\n', 'utf-8')))
await writeFile(filePath, Buffer.concat(frames))
return filePath
}
async function writePlainSession(projectDirName: string, sessionDirName: string, lines: string[]) {
const dir = join(tmpDir, 'sessions', projectDirName, sessionDirName)
await mkdir(dir, { recursive: true })
const filePath = join(dir, 'session.jsonl')
await writeFile(filePath, lines.join('\n') + '\n')
return filePath
}
async function parseAll(provider: ReturnType<typeof createDshProvider>, filePath: string): Promise<ParsedProviderCall[]> {
const source = { path: filePath, project: 'myproject', provider: 'dsh' }
const calls: ParsedProviderCall[] = []
for await (const call of provider.createSessionParser(source, new Set()).parse()) {
calls.push(call)
}
return calls
}
describe('dsh provider - session discovery', () => {
itZstd('discovers a multi-frame zstd session, project from the header cwd', async () => {
await writeZstdSession('--C-Users-test-myproject--', 'session-abc', [
[sessionHeader({ cwd: 'C:\\Users\\test\\myproject' })],
[assistantMessage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000)],
])
const provider = createDshProvider(tmpDir)
const sessions = await provider.discoverSessions()
expect(sessions).toHaveLength(1)
expect(sessions[0]!.provider).toBe('dsh')
expect(sessions[0]!.project).toBe('myproject')
expect(sessions[0]!.path).toContain('session.jsonl.zstd')
})
it('discovers the uncompressed session.jsonl variant (compression=none)', async () => {
await writePlainSession('--home-u-proj--', 'session-plain', [
sessionHeader({ cwd: '/home/u/proj' }),
assistantMessage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000),
])
const provider = createDshProvider(tmpDir)
const sessions = await provider.discoverSessions()
expect(sessions).toHaveLength(1)
expect(sessions[0]!.path).toContain('session.jsonl')
expect(sessions[0]!.path).not.toContain('zstd')
expect(sessions[0]!.project).toBe('proj')
})
it('returns empty for a non-existent home', async () => {
const provider = createDshProvider('/nonexistent/dsh/home')
expect(await provider.discoverSessions()).toEqual([])
})
it('skips session dirs without a session log', async () => {
await mkdir(join(tmpDir, 'sessions', '--x--', 'session-empty'), { recursive: true })
const provider = createDshProvider(tmpDir)
expect(await provider.discoverSessions()).toEqual([])
})
it('DSH_HOME relocates discovery; an empty string is treated as unset', async () => {
const home = join(tmpDir, 'dsh-home')
await mkdir(join(home, 'sessions', '--x--', 'session-env'), { recursive: true })
await writeFile(
join(home, 'sessions', '--x--', 'session-env', 'session.jsonl'),
sessionHeader({ cwd: '/x' }) + '\n',
)
const saved = process.env['DSH_HOME']
process.env['DSH_HOME'] = home
try {
const sessions = await createDshProvider().discoverSessions()
expect(sessions).toHaveLength(1)
} finally {
if (saved === undefined) delete process.env['DSH_HOME']
else process.env['DSH_HOME'] = saved
}
process.env['DSH_HOME'] = ''
try {
const roots = await createDshProvider().probeRoots!()
expect(roots).toEqual([{ path: join(homedir(), '.dsh', 'sessions'), label: 'sessions' }])
} finally {
if (saved === undefined) delete process.env['DSH_HOME']
else process.env['DSH_HOME'] = saved
}
})
it('probeRoots reports the sessions dir under the factory root', async () => {
expect(await createDshProvider('/tmp/dsh-a').probeRoots!()).toEqual([
{ path: join('/tmp/dsh-a', 'sessions'), label: 'sessions' },
])
})
})
describe('dsh provider - parsing', () => {
itZstd('decodes events spread across multiple independent zstd frames', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-multi', [
[sessionHeader({ id: 'session-multi', cwd: 'C:\\Users\\test\\myproject' })],
[turnStart(1, 1786707339000), userMessage('build the thing', 1786707339100)],
[chunkUsage(1, 1, { inputTokens: 500, outputTokens: 50 }, 1786707340000)],
[chunkUsage(1, 2, { inputTokens: 800, outputTokens: 80 }, 1786707341000)],
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(2)
expect(calls[0]!.inputTokens).toBe(500)
expect(calls[1]!.inputTokens).toBe(800)
})
it('a final assistant/message usage REPLACES the earlier chunk sample for the same turn/step', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-replace', [
[sessionHeader({ id: 'session-replace' })],
[turnStart(1, 1786707339000)],
// Early sample, then the final report of the SAME API call: the totals
// must come from the final report only, not the sum of both.
[chunkUsage(1, 1, { inputTokens: 14900, outputTokens: 600, reasoningTokens: 500 }, 1786707340000)],
[assistantMessage(1, 1, { inputTokens: 14981, outputTokens: 656, cacheReadTokens: 0, reasoningTokens: 609 }, 1786707340050)],
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.inputTokens).toBe(14981)
expect(calls[0]!.outputTokens).toBe(656)
expect(calls[0]!.reasoningTokens).toBe(609)
expect(calls[0]!.timestamp).toBe(new Date(1786707340050).toISOString())
})
it('a chunk sample arriving after the final report does not overwrite it', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-late', [
[sessionHeader({ id: 'session-late' })],
[assistantMessage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340050)],
[chunkUsage(1, 1, { inputTokens: 999, outputTokens: 99 }, 1786707340100)],
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.inputTokens).toBe(100)
})
it('falls back to the chunk sample when no assistant/message usage arrives', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-sample', [
[sessionHeader({ id: 'session-sample' })],
[chunkUsage(2, 3, { inputTokens: 42, outputTokens: 7 }, 1786707340000)],
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.inputTokens).toBe(42)
expect(calls[0]!.deduplicationKey).toBe('dsh:session-sample:2:3')
})
it('steps inherit the model of the most recent request/header', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-model', [
[sessionHeader({ id: 'session-model' })],
[requestHeader('deepseek-v4-pro', 1786707337000)],
[assistantMessage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000)],
[assistantMessage(1, 2, { inputTokens: 200, outputTokens: 20 }, 1786707341000)],
[requestHeader('deepseek-v4-flash', 1786707342000)],
[assistantMessage(2, 1, { inputTokens: 300, outputTokens: 30 }, 1786707343000)],
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls.map(c => c.model)).toEqual(['deepseek-v4-pro', 'deepseek-v4-pro', 'deepseek-v4-flash'])
})
it('bills reasoning tokens at the output rate', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-reason', [
[sessionHeader({ id: 'session-reason' })],
[requestHeader('deepseek-v4-pro')],
[assistantMessage(1, 1, { inputTokens: 1000, outputTokens: 100, cacheWriteTokens: 50, cacheReadTokens: 500, reasoningTokens: 400 }, 1786707340000)],
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.costUSD).toBeCloseTo(calculateCost('deepseek-v4-pro', 1000, 500, 50, 500, 0), 12)
})
it('collects mapped tools, skill names and bash commands from tool/call events', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-tools', [
[sessionHeader({ id: 'session-tools' })],
[
toolCall(1, 1, 'read', { path: '/x/a.ts' }, 1786707339500),
toolCall(1, 1, 'edit', { path: '/x/a.ts' }, 1786707339600),
toolCall(1, 1, 'bash', { command: 'git status && bun test' }, 1786707339700),
toolCall(1, 1, 'skill', { name: 'coding-agent-orchestration' }, 1786707339800),
toolCall(1, 1, 'cordis_run', { id: 'j1' }, 1786707339900),
chunkUsage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000),
],
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.tools).toEqual(['Read', 'Edit', 'Bash', 'Skill', 'cordis_run'])
expect(calls[0]!.bashCommands).toEqual(['git', 'bun'])
expect(calls[0]!.skills).toEqual(['coding-agent-orchestration'])
})
it('pairs the user message of the turn and carries session id and project', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-ctx', [
[sessionHeader({ id: 'session-ctx', cwd: 'C:\\Users\\test\\myproject' })],
[turnStart(1, 1786707339000), userMessage('first question', 1786707339100)],
[chunkUsage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000)],
[turnStart(2, 1786707350000), userMessage('second question', 1786707350100)],
[chunkUsage(2, 1, { inputTokens: 200, outputTokens: 20 }, 1786707351000)],
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(2)
expect(calls[0]!.userMessage).toBe('first question')
expect(calls[1]!.userMessage).toBe('second question')
expect(calls[0]!.sessionId).toBe('session-ctx')
expect(calls[0]!.project).toBe('myproject')
expect(calls[0]!.projectPath).toBe('C:\\Users\\test\\myproject')
})
it('parses the uncompressed session.jsonl variant', async () => {
const filePath = await writePlainSession('--home-u-proj--', 'session-plain', [
sessionHeader({ id: 'session-plain', cwd: '/home/u/proj' }),
turnStart(1, 1786707339000),
userMessage('hello', 1786707339100),
chunkUsage(1, 1, { inputTokens: 123, outputTokens: 45 }, 1786707340000),
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.inputTokens).toBe(123)
expect(calls[0]!.outputTokens).toBe(45)
})
it('skips buckets whose usage is all zero', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-zero', [
[sessionHeader({ id: 'session-zero' })],
[assistantMessage(1, 1, { inputTokens: 0, outputTokens: 0 }, 1786707340000)],
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(0)
})
itZstd('ignores a torn final frame appended by a crashed writer', async () => {
const dir = join(tmpDir, 'sessions', '--C-Users-test-myproject--', 'session-torn')
await mkdir(dir, { recursive: true })
const filePath = join(dir, 'session.jsonl.zstd')
const good = zstdCompress!(Buffer.from(
sessionHeader({ id: 'session-torn' }) + '\n' +
chunkUsage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000) + '\n',
))
const torn = zstdCompress!(Buffer.from(chunkUsage(1, 2, { inputTokens: 1, outputTokens: 1 }, 1786707341000) + '\n'))
await writeFile(filePath, Buffer.concat([good, torn.subarray(0, Math.floor(torn.length / 2))]))
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.inputTokens).toBe(100)
})
it('deduplicates (turn, step) calls seen across multiple parses', async () => {
const filePath = await writeZstdSession('--C-Users-test-myproject--', 'session-dedup', [
[sessionHeader({ id: 'session-dedup' })],
[chunkUsage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000)],
])
const provider = createDshProvider(tmpDir)
const source = { path: filePath, project: 'myproject', provider: 'dsh' }
const seenKeys = new Set<string>()
const firstRun: ParsedProviderCall[] = []
for await (const call of provider.createSessionParser(source, seenKeys).parse()) firstRun.push(call)
const secondRun: ParsedProviderCall[] = []
for await (const call of provider.createSessionParser(source, seenKeys).parse()) secondRun.push(call)
expect(firstRun).toHaveLength(1)
expect(secondRun).toHaveLength(0)
})
it('handles a missing session file gracefully', async () => {
const provider = createDshProvider(tmpDir)
const source = { path: join(tmpDir, 'nope', 'session.jsonl.zstd'), project: 'test', provider: 'dsh' }
const calls: ParsedProviderCall[] = []
for await (const call of provider.createSessionParser(source, new Set()).parse()) calls.push(call)
expect(calls).toHaveLength(0)
})
})
describe('dsh provider - display names', () => {
const provider = createDshProvider('/tmp')
it('has correct name and displayName', () => {
expect(provider.name).toBe('dsh')
expect(provider.displayName).toBe('DeepSeek Harness')
})
it('maps deepseek models to readable names and passes unknown ids through', () => {
expect(provider.modelDisplayName('deepseek-v4-pro')).toBe('DeepSeek v4 Pro')
expect(provider.modelDisplayName('some-future-model')).toBe('some-future-model')
})
it('normalizes tool names, keeping unknown names raw', () => {
expect(provider.toolDisplayName('bash')).toBe('Bash')
expect(provider.toolDisplayName('pwsh')).toBe('Bash')
expect(provider.toolDisplayName('todo_write')).toBe('TodoWrite')
expect(provider.toolDisplayName('cordis_run')).toBe('cordis_run')
})
})
describe('dsh provider - real log fidelity', () => {
// The upstream snapshot from deepseek-ai/deepseek-harness
// (examples/acp-agent/tests/snapshots/bash-tool-turn/session.jsonl), with its
// template placeholders filled in. It is the reference for every shape the
// parser reads: packed `reasoning-chunks`/`tool-call-chunks` storage rows, a
// plugin-injected user/message beside the typed one, and both the streamed
// usage chunk and the final assistant/message usage for the same step.
async function writeRealSession(): Promise<string> {
const lines = (await readFile(join(import.meta.dirname, '../fixtures/dsh/bash-tool-turn.jsonl'), 'utf-8'))
.split('\n').filter(l => l.trim())
return writePlainSession('--home-u-proj--', 'e128dda9-ed11-4868-8266-0ef90d03c3d6', lines)
}
it('parses the upstream snapshot: two steps, exact usage, model from the message source', async () => {
const calls = await parseAll(createDshProvider(tmpDir), await writeRealSession())
expect(calls).toHaveLength(2)
expect(calls.map(c => c.model)).toEqual(['deepseek-v4-flash', 'deepseek-v4-flash'])
expect(calls[0]).toMatchObject({
inputTokens: 2877,
outputTokens: 90,
cacheReadInputTokens: 0,
reasoningTokens: 18,
sessionId: 'e128dda9-ed11-4868-8266-0ef90d03c3d6',
project: 'proj',
projectPath: '/home/u/proj',
workingDirectory: '/home/u/proj',
})
expect(calls[1]).toMatchObject({ inputTokens: 168, outputTokens: 25, cacheReadInputTokens: 2816, reasoningTokens: 22 })
// Reasoning bills at the output rate, so it must not appear as input.
expect(calls[0]!.costUSD).toBe(calculateCost('deepseek-v4-flash', 2877, 90 + 18, 0, 0, 0))
expect(calls[0]!.costUSD).toBeGreaterThan(0)
})
it('takes the typed prompt as the preview, not the plugin-injected context', async () => {
const calls = await parseAll(createDshProvider(tmpDir), await writeRealSession())
expect(calls[0]!.userMessage).toBe('Use the bash tool to run exactly: echo TERMINAL_OK. Then reply with the single word DONE and stop.')
expect(calls[0]!.userMessage).not.toContain('Current runtime context')
})
it('reads the tool call through the packed chunk rows around it', async () => {
const calls = await parseAll(createDshProvider(tmpDir), await writeRealSession())
expect(calls[0]!.tools).toEqual(['Bash'])
expect(calls[0]!.bashCommands).toEqual(['echo'])
})
})
describe('dsh provider - defensive reads', () => {
it('skips a log stamped with an unsupported session format version', async () => {
const filePath = await writePlainSession('--home-u-proj--', 'session-future', [
JSON.stringify({ type: 'session', version: 1, id: 'session-future', createdAt: 1786707336131, cwd: '/home/u/proj', delegationDepth: 0 }),
chunkUsage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000),
])
expect(await createDshProvider(tmpDir).discoverSessions()).toEqual([])
expect(await parseAll(createDshProvider(tmpDir), filePath)).toEqual([])
})
it('does not bill a forked session for the events it inherited from its parent', async () => {
const filePath = await writePlainSession('--home-u-proj--', 'session-fork', [
JSON.stringify({
type: 'session', version: 0, id: 'session-fork', createdAt: 1786707336131,
cwd: '/home/u/proj', parentSession: 'session-parent', seedLength: 3, delegationDepth: 0,
}),
// seq 0..2 are a verbatim copy of the parent's log, which codeburn parses
// as its own session; only seq >= 3 is this session's own work.
JSON.stringify({ type: 'turn/start', seq: 0, time: 1786707337000, data: { turn: 1 } }),
JSON.stringify({ type: 'assistant/message', seq: 1, time: 1786707337100, data: { turn: 1, step: 1, message: { role: 'assistant', content: [] }, usage: { inputTokens: 9999, outputTokens: 999 } } }),
JSON.stringify({ type: 'session/end-seed', seq: 2, time: 1786707337200, data: {} }),
JSON.stringify({ type: 'turn/start', seq: 3, time: 1786707338000, data: { turn: 2 } }),
JSON.stringify({ type: 'assistant/message', seq: 4, time: 1786707338100, data: { turn: 2, step: 1, message: { role: 'assistant', content: [] }, usage: { inputTokens: 100, outputTokens: 10 } } }),
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.inputTokens).toBe(100)
})
it('ignores unknown event types, packed chunk rows, and unparsable lines', async () => {
const filePath = await writePlainSession('--home-u-proj--', 'session-noise', [
sessionHeader({ id: 'session-noise', cwd: '/home/u/proj' }),
JSON.stringify({ type: 'agent/inbox/spliced', seq: 0, time: 1786707337000, data: { target: 'next-turn' } }),
JSON.stringify({ type: 'reasoning-chunks', seq0: 1, time0: 1786707337100, data: { turn: 1, step: 1, index: 0, dt: [0], texts: ['a', 'b'] } }),
'{ not json at all',
' ',
chunkUsage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000),
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.inputTokens).toBe(100)
})
it('falls back to the header createdAt when a usage event carries no usable time', async () => {
const filePath = await writePlainSession('--home-u-proj--', 'session-notime', [
JSON.stringify({ type: 'session', version: 0, id: 'session-notime', createdAt: 1786707336131, cwd: '/home/u/proj', delegationDepth: 0 }),
JSON.stringify({ type: 'assistant/message', seq: 1, data: { turn: 1, step: 1, message: { role: 'assistant', content: [] }, usage: { inputTokens: 100, outputTokens: 10 } } }),
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
expect(calls[0]!.timestamp).toBe(new Date(1786707336131).toISOString())
})
})
describe('dsh provider - real log, real container', () => {
itZstd('reads the upstream snapshot out of multi-frame zstd with a torn tail identically to plain jsonl', async () => {
const lines = (await readFile(join(import.meta.dirname, '../fixtures/dsh/bash-tool-turn.jsonl'), 'utf-8'))
.split('\n').filter(l => l.trim())
const plain = await parseAll(
createDshProvider(tmpDir),
await writePlainSession('--home-u-proj--', 'plain', lines),
)
// Header batch, then three append batches — the layout DSH writes.
const dir = join(tmpDir, 'sessions', '--home-u-proj--', 'framed')
await mkdir(dir, { recursive: true })
const filePath = join(dir, 'session.jsonl.zstd')
const frames = [[lines[0]!], lines.slice(1, 10), lines.slice(10, 25), lines.slice(25)]
.map(batch => zstdCompress!(Buffer.from(batch.join('\n') + '\n', 'utf-8')))
// A crashed writer's half-written final batch, carrying usage that must not count.
const torn = zstdCompress!(Buffer.from(assistantMessage(9, 9, { inputTokens: 123456, outputTokens: 1 }, 1785730424999) + '\n', 'utf-8'))
await writeFile(filePath, Buffer.concat([...frames, torn.subarray(0, Math.floor(torn.length / 2))]))
const framed = await parseAll(createDshProvider(tmpDir), filePath)
expect(framed.map(c => [c.inputTokens, c.outputTokens, c.reasoningTokens, c.model]))
.toEqual(plain.map(c => [c.inputTokens, c.outputTokens, c.reasoningTokens, c.model]))
expect(framed).toHaveLength(2)
})
})
describe('dsh provider - hostile input', () => {
itZstd('skips a session whose frames decompress to far more than the file cap', async () => {
const dir = join(tmpDir, 'sessions', '--home-u-proj--', 'session-bomb')
await mkdir(dir, { recursive: true })
const filePath = join(dir, 'session.jsonl.zstd')
// 200 MB of zeros compresses to a few KB. Uncapped this decoded to ~916 MB
// of RSS for a 16 KB file; the per-frame cap now rejects it without
// allocating past the cap.
const bomb = zstdCompress!(Buffer.alloc(200 * 1024 * 1024))
const good = zstdCompress!(Buffer.from(
sessionHeader({ id: 'session-bomb', cwd: '/home/u/proj' }) + '\n'
+ chunkUsage(1, 1, { inputTokens: 100, outputTokens: 10 }, 1786707340000) + '\n',
'utf-8',
))
await writeFile(filePath, Buffer.concat([good, bomb]))
expect((await stat(filePath)).size).toBeLessThan(64 * 1024)
// The whole file is skipped: the frames read before the bomb are not
// counted, so a crafted tail cannot poison a partial total.
expect(await parseAll(createDshProvider(tmpDir), filePath)).toEqual([])
})
itZstd('stops decoding once the frames exceed the running budget', async () => {
const frame = zstdCompress!(Buffer.from('{"type":"turn/start","seq":0,"time":1,"data":{"turn":1}}\n', 'utf-8'))
const buffer = Buffer.concat([frame, frame, frame])
expect([...readZstdLines(buffer, Number.POSITIVE_INFINITY, 4096)]).toHaveLength(3)
// A budget under two frames' plaintext stops at the frame that overruns it.
expect(() => [...readZstdLines(buffer, Number.POSITIVE_INFINITY, 60)]).toThrow()
})
it('coerces non-numeric usage fields instead of poisoning the totals', async () => {
const filePath = await writePlainSession('--home-u-proj--', 'session-poison', [
sessionHeader({ id: 'session-poison', cwd: '/home/u/proj' }),
JSON.stringify({
type: 'assistant/message', seq: 1, time: 1786707340000,
data: {
turn: 1, step: 1, message: { role: 'assistant', content: [] },
usage: { inputTokens: '999', outputTokens: [1, 2], reasoningTokens: 1e308 * 10, cacheReadTokens: -5, cacheWriteTokens: 7 },
},
}),
])
const calls = await parseAll(createDshProvider(tmpDir), filePath)
expect(calls).toHaveLength(1)
// Only the one genuinely numeric field survives; every other shape is 0.
expect(calls[0]).toMatchObject({
inputTokens: 0,
outputTokens: 0,
reasoningTokens: 0,
cacheReadInputTokens: 0,
cacheCreationInputTokens: 7,
})
for (const value of [calls[0]!.inputTokens, calls[0]!.outputTokens, calls[0]!.costUSD]) {
expect(typeof value).toBe('number')
expect(Number.isFinite(value)).toBe(true)
}
})
it('still skips a call whose usage is all non-numeric', async () => {
const filePath = await writePlainSession('--home-u-proj--', 'session-poison-zero', [
sessionHeader({ id: 'session-poison-zero', cwd: '/home/u/proj' }),
JSON.stringify({
type: 'assistant/message', seq: 1, time: 1786707340000,
data: { turn: 1, step: 1, message: { role: 'assistant', content: [] }, usage: { inputTokens: '999', outputTokens: [1, 2] } },
}),
])
expect(await parseAll(createDshProvider(tmpDir), filePath)).toEqual([])
})
})

View file

@ -1,11 +1,25 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mkdtemp, mkdir, writeFile, rm } from 'fs/promises'
import { join } from 'path'
import { tmpdir } from 'os'
import { createGrokProvider } from '../../src/providers/grok.js'
import { calculateCost } from '../../src/models.js'
import type { ParsedProviderCall } from '../../src/providers/types.js'
// `chooseAuthoritativeModel` branches on whether a modelUsage id resolves to a
// price, so pin the reporter's real id from #998 as unpriced here rather than
// letting the bundled LiteLLM snapshot decide it: xAI pricing landing upstream
// would otherwise silently flip these assertions. Only this lookup is stubbed,
// so `calculateCost` still prices off the real tables.
vi.mock('../../src/models.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../src/models.js')>()
return {
...actual,
getModelCosts: (model: string) => (model === 'grok-4.6-build' ? null : actual.getModelCosts(model)),
}
})
let tmpDir: string
beforeEach(async () => {
@ -24,6 +38,7 @@ async function writeSession(opts: {
cwd?: string
model?: string
turns?: Array<{ promptId: string; totals: number[] }>
completedTurns?: Array<{ promptId?: string; usage: unknown }>
toolCalls?: Array<{ title: string; rawInput: Record<string, unknown> }>
toolsUsed?: string[]
} = {}) {
@ -72,6 +87,21 @@ async function writeSession(opts: {
}))
}
}
for (const completed of opts.completedTurns ?? []) {
lines.push(JSON.stringify({
timestamp: 1786724773,
method: '_x.ai/session/update',
params: {
sessionId: uuid,
update: {
sessionUpdate: 'turn_completed',
...(completed.promptId === undefined ? {} : { prompt_id: completed.promptId }),
usage: completed.usage,
},
_meta: { eventId: 'event-1', agentTimestampMs: 1786724773589 },
},
}))
}
for (const tc of opts.toolCalls ?? [
{ title: 'read_file', rawInput: { target_directory: '.' } },
{ title: 'grep', rawInput: { pattern: 'x' } },
@ -89,6 +119,47 @@ async function writeSession(opts: {
return { dir, uuid }
}
function authoritativeUsage(opts: {
input?: number
output?: number
cacheRead?: number
cacheCreation?: number
reasoning?: number
model?: string
modelUsage?: Record<string, Record<string, unknown>>
} = {}): Record<string, unknown> {
const input = opts.input ?? 1000
const output = opts.output ?? 100
const cacheRead = opts.cacheRead ?? 0
const cacheCreation = opts.cacheCreation ?? 0
const reasoning = opts.reasoning ?? 0
const model = opts.model ?? 'grok-4.6-build'
const singleModelUsage = {
inputTokens: input,
outputTokens: output,
totalTokens: input + output,
cachedReadTokens: cacheRead,
cacheCreationTokens: cacheCreation,
reasoningTokens: reasoning,
modelCalls: 1,
apiDurationMs: 1000,
costUsdTicks: 125117780000,
}
return {
inputTokens: input,
outputTokens: output,
totalTokens: input + output,
cachedReadTokens: cacheRead,
cacheCreationTokens: cacheCreation,
reasoningTokens: reasoning,
modelCalls: 1,
apiDurationMs: 1000,
costUsdTicks: 125117780000,
modelUsage: opts.modelUsage ?? { [model]: singleModelUsage },
numTurns: 1,
}
}
describe('grok provider - discovery', () => {
it('discovers each session dir and derives project from cwd', async () => {
await writeSession({ cwd: '/Users/test/myproject' })
@ -123,7 +194,7 @@ describe('grok provider - parsing', () => {
return calls
}
it('emits one estimated call per session from the totalTokens curve', async () => {
it('emits one estimated call per session from the totalTokens fallback curve', async () => {
await writeSession()
const calls = await parse()
expect(calls).toHaveLength(1)
@ -144,6 +215,245 @@ describe('grok provider - parsing', () => {
expect(call.deduplicationKey).toContain('grok:')
})
it('uses one turn_completed usage record as authoritative and splits cache subsets from input', async () => {
await writeSession({
turns: [],
completedTurns: [{
promptId: 'real-prompt-1',
usage: authoritativeUsage({
input: 12851663,
output: 36633,
cacheRead: 12092032,
cacheCreation: 0,
reasoning: 29077,
}),
}],
})
const calls = await parse()
expect(calls).toHaveLength(1)
const call = calls[0]!
expect(call.model).toBe('grok-build')
expect(call.inputTokens).toBe(759631) // 12851663 - 12092032 - 0
expect(call.cacheReadInputTokens).toBe(12092032)
expect(call.cacheCreationInputTokens).toBe(0)
// Grok reports reasoning inside outputTokens; the repo contract wants them
// split, so output is emitted exclusive of reasoning and the two sum back
// to the 36633 the record reported.
expect(call.outputTokens).toBe(7556) // 36633 - 29077
expect(call.reasoningTokens).toBe(29077)
expect(call.outputTokens + call.reasoningTokens).toBe(36633)
expect(call.costIsEstimated).toBe(false)
expect(call.costUSD).toBe(calculateCost('grok-build', 759631, 36633, 0, 12092032, 0))
})
it('sums distinct turn_completed prompt ids exactly once each', async () => {
await writeSession({
turns: [],
completedTurns: [
{ promptId: 'p1', usage: authoritativeUsage({ input: 1000, output: 100, cacheRead: 600, cacheCreation: 50, reasoning: 10 }) },
{ promptId: 'p2', usage: authoritativeUsage({ input: 2000, output: 200, cacheRead: 1000, cacheCreation: 100, reasoning: 20 }) },
],
})
const [call] = await parse()
expect(call).toMatchObject({
inputTokens: 1250,
cacheReadInputTokens: 1600,
cacheCreationInputTokens: 150,
outputTokens: 270, // 300 reported - 30 reasoning
reasoningTokens: 30,
})
})
it('keeps one authoritative call and uses top-level totals for multi-model usage', async () => {
await writeSession({
turns: [],
completedTurns: [{
promptId: 'multi-model',
usage: authoritativeUsage({
input: 3000,
output: 300,
cacheRead: 600,
cacheCreation: 100,
reasoning: 30,
modelUsage: {
'grok-build-0.1': {
inputTokens: 1000,
outputTokens: 100,
cachedReadTokens: 100,
cacheCreationTokens: 0,
reasoningTokens: 10,
},
'grok-latest': {
inputTokens: 2000,
outputTokens: 200,
cachedReadTokens: 500,
cacheCreationTokens: 100,
reasoningTokens: 20,
},
},
}),
}],
})
const calls = await parse()
expect(calls).toHaveLength(1)
expect(calls[0]).toMatchObject({
model: 'grok-build-0.1',
inputTokens: 2300,
outputTokens: 270,
reasoningTokens: 30,
costUSD: calculateCost('grok-build-0.1', 2300, 300, 100, 600, 0),
})
expect(calls[0]!.outputTokens + calls[0]!.reasoningTokens).toBe(300)
expect(calls[0]!.turnId).toBeUndefined()
})
it('uses the last turn_completed record for a duplicate prompt id', async () => {
await writeSession({
turns: [],
completedTurns: [
{ promptId: 'same-prompt', usage: authoritativeUsage({ input: 500, output: 50, cacheRead: 100, reasoning: 5 }) },
{ promptId: 'same-prompt', usage: authoritativeUsage({ input: 800, output: 80, cacheRead: 200, cacheCreation: 25, reasoning: 8 }) },
],
})
const [call] = await parse()
expect(call).toMatchObject({
inputTokens: 575,
cacheReadInputTokens: 200,
cacheCreationInputTokens: 25,
outputTokens: 72, // 80 reported - 8 reasoning
reasoningTokens: 8,
})
})
it('uses unique fallback keys when completed records omit prompt_id', async () => {
await writeSession({
turns: [],
completedTurns: [
{ usage: authoritativeUsage({ input: 100, output: 10 }) },
{ usage: authoritativeUsage({ input: 200, output: 20 }) },
],
})
const [call] = await parse()
expect(call).toMatchObject({ inputTokens: 300, outputTokens: 30, reasoningTokens: 0 })
})
it('ignores a still-streaming turn but marks mixed coverage estimated', async () => {
await writeSession({
turns: [{ promptId: 'still-streaming', totals: [10000, 15000] }],
completedTurns: [{ promptId: 'completed', usage: authoritativeUsage({ input: 900, output: 90, cacheRead: 300, reasoning: 20 }) }],
})
const [call] = await parse()
expect(call).toMatchObject({
inputTokens: 600,
cacheReadInputTokens: 300,
outputTokens: 70, // 90 reported - 20 reasoning
reasoningTokens: 20,
costIsEstimated: true,
})
})
it('treats malformed authoritative fields as absent without throwing or corrupting totals', async () => {
await writeSession({
turns: [],
completedTurns: [{
promptId: 'malformed',
usage: {
inputTokens: -1,
outputTokens: 4,
totalTokens: 'not-a-number',
cachedReadTokens: Number.NaN,
cacheCreationTokens: 'not-a-number',
reasoningTokens: -2,
modelUsage: {},
},
}],
})
const [call] = await parse()
expect(call).toBeDefined()
expect(call!.inputTokens).toBe(0)
expect(call!.outputTokens).toBe(4)
expect(call!.cacheReadInputTokens).toBe(0)
expect(call!.cacheCreationInputTokens).toBe(0)
expect(call!.reasoningTokens).toBe(0)
expect(Number.isFinite(call!.costUSD)).toBe(true)
expect(call!.costUSD).toBeGreaterThanOrEqual(0)
})
it('keeps the heuristic when a completed record reports all-zero usage', async () => {
await writeSession({
turns: [
{ promptId: 'streaming-1', totals: [20000, 25000] },
{ promptId: 'streaming-2', totals: [30000, 35000] },
],
completedTurns: [{
promptId: 'zero-usage',
usage: authoritativeUsage({ input: 0, output: 0, cacheRead: 0, cacheCreation: 0, reasoning: 0 }),
}],
})
const [call] = await parse()
expect(call).toBeDefined()
expect(call!.inputTokens).toBe(35000)
expect(call!.cacheReadInputTokens).toBe(15000)
expect(call!.outputTokens).toBe(10000)
expect(call!.costIsEstimated).toBe(true)
})
it('clamps cache-exclusive input per completed record before summing', async () => {
await writeSession({
turns: [],
completedTurns: [
{ promptId: 'inconsistent', usage: authoritativeUsage({ input: 100, output: 10, cacheRead: 80, cacheCreation: 50 }) },
{ promptId: 'consistent', usage: authoritativeUsage({ input: 100, output: 20 }) },
],
})
const [call] = await parse()
expect(call).toMatchObject({
inputTokens: 100,
cacheReadInputTokens: 80,
cacheCreationInputTokens: 50,
outputTokens: 30,
})
})
it('does not add reasoning tokens on top of provider-reported output for cost', async () => {
await writeSession({
turns: [],
model: 'grok-build',
completedTurns: [{
promptId: 'reasoning-subset',
usage: authoritativeUsage({
input: 1000,
output: 200,
cacheRead: 500,
cacheCreation: 100,
reasoning: 150,
model: 'grok-build',
}),
}],
})
const [call] = await parse()
expect(call).toBeDefined()
expect(call!.inputTokens).toBe(400)
// Output is emitted exclusive of reasoning, and the two sum to the 200 the
// record reported. The cost prices that full 200 once - the downstream
// `outputTokens + reasoningTokens` recompute lands on the same number.
expect(call!.outputTokens).toBe(50) // 200 - 150
expect(call!.reasoningTokens).toBe(150)
expect(call!.outputTokens + call!.reasoningTokens).toBe(200)
expect(call!.costUSD).toBe(calculateCost('grok-build', 400, 200, 100, 500, 0))
expect(call!.costUSD).not.toBe(calculateCost('grok-build', 400, 350, 100, 500, 0))
})
it('skips a session with no token growth', async () => {
await writeSession({ turns: [{ promptId: 'p1', totals: [0, 0] }] })
expect(await parse()).toHaveLength(0)
@ -180,6 +490,13 @@ describe('grok provider - display names', () => {
expect(provider.modelDisplayName('grok-build')).toBe('Grok Build')
})
// Two distinct ids, so two rows; identical names made them look like one row
// printed twice (#1029).
it('distinguishes the build variant of a model from the model itself', () => {
expect(provider.modelDisplayName('grok-4.5')).toBe('Grok 4.5')
expect(provider.modelDisplayName('grok-4.5-build')).toBe('Grok 4.5 (build)')
})
it('normalizes tool names', () => {
expect(provider.toolDisplayName('run_terminal_command')).toBe('Bash')
expect(provider.toolDisplayName('mystery_tool')).toBe('mystery_tool')

View file

@ -499,6 +499,28 @@ describe('scoped load', () => {
.toEqual(['/live/apr.jsonl', '/live/jun.jsonl', '/live/jun2.jsonl', '/live/mar.jsonl'])
})
it('keeps the unloaded month\'s shard name when a re-parse re-derives the same entry', async () => {
await seedThreeMonths()
// The March entry is invisible to a June-scoped run, so the reconcile
// re-parses that file and writes the identical entry straight back. Nothing
// changed, so the March shard must keep its name run after run (#1032).
const nameOf = async (): Promise<string> => (await envelope()).providers['claude']!.shards['2026-03']!.name
const before = await nameOf()
for (let run = 0; run < 2; run++) {
clearLoadCacheMemo()
const scoped = await loadCache(juneScope)
scoped.providers['claude']!.files['/live/mar.jsonl'] = fileSpanning('2026-03-10T10:00:00Z')
markCacheDirty(scoped, 'claude', '/live/mar.jsonl')
await saveCache(scoped)
expect(await nameOf(), `March republished on run ${run + 1}`).toBe(before)
}
clearLoadCacheMemo()
const full = await loadCache()
expect(Object.keys(full.providers['claude']!.files).sort())
.toEqual(['/live/apr.jsonl', '/live/jun.jsonl', '/live/mar.jsonl'])
})
it('merges rather than replaces when a re-parse lands in an unloaded month', async () => {
await seedThreeMonths()
clearLoadCacheMemo()
@ -516,6 +538,24 @@ describe('scoped load', () => {
.toEqual(['/live/apr.jsonl', '/live/jun.jsonl', '/live/mar.jsonl'])
})
it('CODEBURN_CACHE_SCOPE=all reads every month and memoizes as unscoped', async () => {
await seedThreeMonths()
clearLoadCacheMemo()
const unscoped = await loadCache()
clearLoadCacheMemo()
process.env['CODEBURN_CACHE_SCOPE'] = 'all'
try {
const forced = await loadCache(juneScope)
expect(forced).toEqual(unscoped)
// Memoized as a full load, so a resident serve reuses it for any range.
delete process.env['CODEBURN_CACHE_SCOPE']
expect(await loadCache(juneScope)).toBe(forced)
} finally {
delete process.env['CODEBURN_CACHE_SCOPE']
}
})
it('never scopes a provider whose fingerprint moved, or a durable one', async () => {
const cache: SessionCache = {
version: CACHE_VERSION,

Some files were not shown because too many files have changed in this diff Show more