merge origin/main into pr996

Resolve src/main.ts to main's #1016 rank-map --unpriced/--top implementation and
drop this branch's inline comparator and topN hunk entirely.
This commit is contained in:
iamtoruk 2026-08-18 09:57:06 -07:00
commit 81eb7310a3
27 changed files with 1147 additions and 82 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"

View file

@ -3,6 +3,7 @@
## 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, or off the ends of the raw line when the entry is too large for the parser to keep them. 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.
@ -28,6 +29,9 @@
- **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
- **`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.

View file

@ -157,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)
@ -508,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 |
| `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 |

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`.

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

@ -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

@ -39,7 +39,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,

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

@ -2111,27 +2111,33 @@ program
const projects = await parseAllSessions(range, opts.provider)
const topN = typeof opts.top === 'number' && Number.isFinite(opts.top) ? opts.top : undefined
const minCost = typeof opts.minCost === 'number' && Number.isFinite(opts.minCost)
? opts.minCost
: opts.unpriced ? undefined : 0.01
let rows = await aggregateModels(projects, {
byTask: !!opts.byTask,
byAgent: !!opts.byAgent,
taskFilter: opts.task,
// `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,
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 => findUnpricedModels([{
model: row.model,
calls: row.calls,
cost: row.costUSD,
tokens: row.totalTokens,
}]).length > 0)
.sort((a, b) => (b.totalTokens - a.totalTokens) || (b.calls - a.calls)
|| (a.provider < b.provider ? -1 : a.provider > b.provider ? 1 : 0)
|| (a.model < b.model ? -1 : a.model > b.model ? 1 : 0))
.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)
}

View file

@ -1,12 +1,12 @@
import chalk from 'chalk'
import stripAnsi from 'strip-ansi'
import { isReadShapedBashCommand } from './bash-utils.js'
import { createHash } from 'crypto'
import { readdir, stat } from 'fs/promises'
import { existsSync, statSync } from 'fs'
import { basename, join } from 'path'
import { homedir } from 'os'
import { isReadShapedBashCommand } from './bash-utils.js'
import { readSessionLines, readSessionFileSync } from './fs-utils.js'
import { discoverAllSessions } from './providers/index.js'
import { parseJsonlLine, shouldSkipLine } from './parser.js'
@ -15,6 +15,7 @@ import { formatCost } from './currency.js'
import { formatTokens } from './format.js'
import { recommendModelDefault, type ModelDefaultRecommendation } from './act/model-defaults.js'
import { appliedFixGlyph, formatAppliedFix, type AppliedFix } from './act/types.js'
import { isUserStartedSession, userStartedProjects } from './session-population.js'
import { aggregateFileChurn, buildCoachingNotes, scanUserCorrections, medianTimeToFirstEditMs, worstOneShotCategory, type ReworkedFile } from './workflow-insights.js'
// ============================================================================
@ -542,6 +543,7 @@ export type ToolCall = {
sessionId: string
project: string
recent?: boolean
isSidechain?: boolean
}
export type ApiCallMeta = {
@ -726,6 +728,7 @@ export async function scanJsonlFile(
const openers: SessionOpener[] = []
const sessionId = basename(filePath, '.jsonl')
let lastVersion = ''
let fileIsSidechain = false
// The opening block is the first user message carrying text; anything
// later in the session is not what the user opens with.
let sawUserText = false
@ -744,6 +747,11 @@ export async function scanJsonlFile(
if (!parsed) continue
const entry = parsed as Record<string, unknown>
if (entry.isSidechain === true && !fileIsSidechain) {
fileIsSidechain = true
for (const call of calls) call.isSidechain = true
}
if (entry.version && typeof entry.version === 'string') lastVersion = entry.version
const ts = typeof entry.timestamp === 'string' ? entry.timestamp : undefined
@ -804,6 +812,7 @@ export async function scanJsonlFile(
sessionId,
project,
recent,
isSidechain: fileIsSidechain,
})
}
}
@ -999,6 +1008,11 @@ export function detectJunkReads(calls: ToolCall[], dateRange?: DateRange): Waste
}
export function detectDuplicateReads(calls: ToolCall[], dateRange?: DateRange): WasteFinding | null {
// A sidechain re-reading what its parent read is not a repeat: a subagent
// starts on a fresh context and has to read it. Junk reads and the
// read:edit ratio keep the full call population - that waste is waste
// whoever does it, and the CLAUDE.md rule they suggest binds subagents too.
calls = calls.filter(call => call.isSidechain !== true)
const sessionFiles = new Map<string, Map<string, { count: number; recent: number }>>()
for (const call of calls) {
@ -1923,6 +1937,7 @@ function findCapabilityReliabilityCandidates(projects: ProjectSummary[]): Capabi
}
export function detectCapabilityReliability(projects: ProjectSummary[]): WasteFinding | null {
projects = userStartedProjects(projects)
const candidates = findCapabilityReliabilityCandidates(projects)
if (candidates.length === 0) return null
@ -2947,6 +2962,22 @@ function sessionTokenTotal(session: ProjectSummary['sessions'][number]): number
+ session.totalCacheWriteTokens
}
// Sidechain transcripts are real usage, so they stay in project totals and in
// token/cost calibration. They are not user-started sessions, however, and
// should never enter optimize heuristics whose unit is a human work session.
// Keep that distinction local to optimize instead of deleting sidechains from
// ProjectSummary, which would under-report the work delegated to subagents.
function isOptimizeSession(session: ProjectSummary['sessions'][number]): boolean {
return isUserStartedSession(session)
}
function optimizeSessionCount(projects: ProjectSummary[]): number {
return projects.reduce(
(total, project) => total + project.sessions.filter(isOptimizeSession).length,
0,
)
}
function sessionEffectiveContextTokens(session: ProjectSummary['sessions'][number]): number {
return session.totalInputTokens
+ session.totalCacheReadTokens * CACHE_READ_DISCOUNT
@ -3055,6 +3086,7 @@ export function findLowWorthCandidates(projects: ProjectSummary[]): LowWorthCand
for (const project of projects) {
for (const session of project.sessions) {
if (!isOptimizeSession(session)) continue
if (session.totalCostUSD < WORTH_IT_MIN_COST_USD) continue
if (sessionDeliveryCommand(session)) continue
@ -3155,7 +3187,7 @@ export function findContextBloatCandidates(projects: ProjectSummary[]): ContextB
const candidates: ContextBloatCandidate[] = []
for (const project of projects) {
const sessions = [...project.sessions].sort((a, b) =>
const sessions = project.sessions.filter(isOptimizeSession).sort((a, b) =>
new Date(a.firstTimestamp).getTime() - new Date(b.firstTimestamp).getTime()
)
let previousInputTokens: number | null = null
@ -3273,7 +3305,7 @@ export function detectSessionOutliers(projects: ProjectSummary[], excludedSessio
let usedEstimatedCosts = false
for (const project of projects) {
const costed = project.sessions.filter(s => s.totalCostUSD > 0)
const costed = project.sessions.filter(s => isOptimizeSession(s) && s.totalCostUSD > 0)
const exact = costed.filter(s => (s.totalEstimatedCostUSD ?? 0) === 0)
const sessions = exact.length >= MIN_SESSIONS_FOR_OUTLIER ? exact : costed
const fellBack = sessions.length > exact.length
@ -3339,7 +3371,7 @@ function findYoungProjectFirstSessionIds(projects: ProjectSummary[]): Set<string
const firstSessionIds = new Set<string>()
for (const project of projects) {
const costed = project.sessions.filter(s => s.totalCostUSD > 0)
const costed = project.sessions.filter(s => isOptimizeSession(s) && s.totalCostUSD > 0)
if (costed.length >= YOUNG_PROJECT_SESSION_LIMIT) continue
let firstSession: ProjectSummary['sessions'][number] | null = null
@ -3458,15 +3490,25 @@ export function cacheKey(projects: ProjectSummary[], dateRange: DateRange | unde
// stale findings when cost/tokens moved (e.g. a re-price) while call count
// held - reachable in the long-lived menubar process within the 60s TTL.
// Cost is scaled to whole micro-dollars so float jitter cannot thrash the key.
let calls = 0, cost = 0, savings = 0, proxied = 0
let calls = 0, cost = 0, savings = 0, proxied = 0, sessions = 0, sidechains = 0
const sidechainIdentities: string[] = []
for (const p of projects) {
calls += p.totalApiCalls
cost += p.totalCostUSD
savings += p.totalSavingsUSD
proxied += p.totalProxiedCostUSD
sessions += p.sessions.length
for (const session of p.sessions) {
if (session.isSidechain !== true) continue
sidechains++
sidechainIdentities.push(`${p.projectPath}\0${session.sessionId}`)
}
}
const sidechainDigest = createHash('sha256')
.update(sidechainIdentities.sort().join('\0'))
.digest('base64url')
// Costs scaled to whole micro-dollars so float jitter cannot thrash the key.
const fingerprint = `${projects.length}:${calls}:${Math.round(cost * 1e6)}:${Math.round(savings * 1e6)}:${Math.round(proxied * 1e6)}`
const fingerprint = `${projects.length}:${sessions}:${sidechains}:${sidechainDigest}:${calls}:${Math.round(cost * 1e6)}:${Math.round(savings * 1e6)}:${Math.round(proxied * 1e6)}`
// The provider decides whether the Claude session scan runs at all, so two
// filters that happen to share a project fingerprint must not share a result.
return `${provider ?? 'all'}:${dr}:${fingerprint}`
@ -3486,6 +3528,7 @@ export async function scanAndDetect(
if (cached && Date.now() - cached.ts < RESULT_CACHE_TTL_MS) return cached.data
const costRate = computeInputCostRate(projects)
const behavioralProjects = userStartedProjects(projects)
const scanCoversClaude = providerCoversClaude(provider)
const { toolCalls, projectCwds, apiCalls, userMessages, openers } = await scanSessions(dateRange, provider)
const mcpCoverage = aggregateMcpCoverage(projects)
@ -3494,13 +3537,13 @@ export async function scanAndDetect(
// Priority order for the per-session findings: low-worth → context-bloat →
// outliers. Each later detector excludes sessions already named by an
// earlier one so a single session is not listed in three findings.
const lowWorthSessionIds = new Set(findLowWorthCandidates(projects).map(c => c.sessionId))
const lowWorthSessionIds = new Set(findLowWorthCandidates(behavioralProjects).map(c => c.sessionId))
const contextBloatVisibleIds = new Set(
findContextBloatCandidates(projects)
findContextBloatCandidates(behavioralProjects)
.filter(c => !lowWorthSessionIds.has(c.sessionId))
.map(c => c.sessionId),
)
const firstSessionIds = findYoungProjectFirstSessionIds(projects)
const firstSessionIds = findYoungProjectFirstSessionIds(behavioralProjects)
const outlierExclusions = new Set([...lowWorthSessionIds, ...contextBloatVisibleIds, ...firstSessionIds])
// Detectors fed by the session scan or by `~/.claude` config only mean
// anything when the run covers Claude. Under a different `--provider` they
@ -3521,10 +3564,10 @@ export async function scanAndDetect(
claudeOnly(() => detectMcpDeferralOff(toolCalls, projects, projectCwds, apiCalls)),
claudeOnly(() => detectMcpAlwaysLoadHygiene(projects, projectCwds, apiCalls, mcpCoverage)),
claudeOnly(() => detectMcpDeferThreshold(projects, projectCwds)),
() => detectCapabilityReliability(projects),
() => detectLowWorthSessions(projects),
() => detectContextBloat(projects, lowWorthSessionIds),
() => detectSessionOutliers(projects, outlierExclusions),
() => detectCapabilityReliability(behavioralProjects),
() => detectLowWorthSessions(behavioralProjects),
() => detectContextBloat(behavioralProjects, lowWorthSessionIds),
() => detectSessionOutliers(behavioralProjects, outlierExclusions),
claudeOnly(() => detectBloatedClaudeMd(projectCwds)),
claudeOnly(() => detectBashBloat()),
claudeOnly(() => detectRecurringContext(openers)),
@ -3550,7 +3593,7 @@ export async function scanAndDetect(
const { score, grade } = computeHealth(findings)
const modelRecommendations: ModelDefaultRecommendation[] = []
for (const project of projects) {
for (const project of behavioralProjects) {
const rec = recommendModelDefault(project, { now: dateRange?.end })
if (rec) modelRecommendations.push(rec)
}
@ -3717,7 +3760,7 @@ export function renderOptimize(
const issueSuffix = findings.length > 0 ? `, ${findings.length} issue${findings.length > 1 ? 's' : ''}` : ''
const measured = findings.filter(f => findingBasis(f) === 'measured').length
lines.push(' ' + [
`${sessionCount} sessions`,
`${sessionCount} session${sessionCount === 1 ? '' : 's'}`,
`${callCount.toLocaleString()} calls`,
chalk.hex(GOLD)(formatCost(periodCost)),
`Health: ${chalk.bold.hex(GRADE_COLORS[healthGrade])(healthGrade)}${chalk.dim(` (${healthScore}/100${issueSuffix})`)}`,
@ -3823,7 +3866,7 @@ export async function runOptimize(
const result = await scanAndDetect(projects, dateRange, opts.provider)
const { findings, costRate, healthScore, healthGrade } = result
const sessions = projects.flatMap(p => p.sessions)
const sessionCount = optimizeSessionCount(projects)
const periodCost = projects.reduce((s, p) => s + p.totalCostUSD, 0)
const callCount = projects.reduce((s, p) => s + p.totalApiCalls, 0)
@ -3833,7 +3876,7 @@ export async function runOptimize(
}
const { topReworkedFiles, coachingNotes } = buildWorkflowReport(projects)
const output = renderOptimize(findings, costRate, periodLabel, periodCost, sessions.length, callCount, healthScore, healthGrade, topReworkedFiles, coachingNotes, opts.appliedHeader, opts.previouslyApplied, result.modelRecommendations, opts.appliedFixes)
const output = renderOptimize(findings, costRate, periodLabel, periodCost, sessionCount, callCount, healthScore, healthGrade, topReworkedFiles, coachingNotes, opts.appliedHeader, opts.previouslyApplied, result.modelRecommendations, opts.appliedFixes)
console.log(output)
}
@ -3844,7 +3887,6 @@ export function buildOptimizeJsonReport(
dateRange?: DateRange,
appliedFixes: AppliedFix[] = [],
): OptimizeJsonReport {
const sessions = projects.flatMap(p => p.sessions)
const periodCostUSD = projects.reduce((s, p) => s + p.totalCostUSD, 0)
const calls = projects.reduce((s, p) => s + p.totalApiCalls, 0)
const potentialSavingsTokens = result.findings.reduce((s, f) => s + f.tokensSaved, 0)
@ -3864,7 +3906,7 @@ export function buildOptimizeJsonReport(
healthGrade: result.healthGrade,
findingCount: result.findings.length,
periodCostUSD,
sessions: sessions.length,
sessions: optimizeSessionCount(projects),
calls,
potentialSavingsTokens,
potentialSavingsCostUSD,

View file

@ -557,7 +557,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'] as const
const LARGE_ASSISTANT_MESSAGE_FIELDS = ['model', 'usage', 'id', 'content'] as const
function parseLargeJsonl(line: string | Buffer): JournalEntry | null {
@ -571,6 +571,9 @@ 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'])
@ -2350,6 +2353,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
}
@ -3271,8 +3275,9 @@ 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 over time.
// 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.
if (!readOnly && provider.durableSources) {
const cutoffMs = Date.now() - 90 * 24 * 60 * 60 * 1000
for (const [cachedPath, cachedFile] of Object.entries(section.files)) {
@ -3281,7 +3286,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)
}
@ -3574,6 +3579,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

@ -100,7 +100,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

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

@ -205,6 +205,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

@ -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

@ -776,6 +776,54 @@ describe('models CLI breakdown flags', () => {
}
})
// 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

@ -21,6 +21,9 @@ import {
detectUnusedMcp,
detectBashBloat,
detectGhostCommands,
detectDuplicateReads,
detectJunkReads,
detectLowReadEditRatio,
loadMcpConfigs,
localMcpServerNames,
scanJsonlFile,
@ -327,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')

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

@ -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',
@ -55,6 +56,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

@ -24,6 +24,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) => {
@ -54,6 +55,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
@ -193,6 +195,7 @@ beforeEach(async () => {
_synthSources = []
_synthDurable = false
_synthYields = []
_synthParseCalls = 0
_synthOnParse = null
})
@ -360,7 +363,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 an orphaned cache entry whose newest call is 91 days old', 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')
@ -380,15 +383,76 @@ describe('(e) 90-day age-out for durable providers', () => {
userMessage: 'old', sessionId: 'synth-old',
}]
// First parse: cached with 91d-old timestamp → immediately pruned by 90-day check
// 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) // pruned right away
expect.soft(totalOutput(proj1)).toBe(8)
expect.soft(_synthParseCalls).toBe(1)
// Confirm: entry is not in the persistent cache after first parse
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()
_synthSources = [] // no longer discovered
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 an orphaned cache entry whose newest call is 89 days old', async () => {

View file

@ -353,4 +353,25 @@ describe('review-findings regressions', () => {
// 95 local calls + 5 unpriced cloud calls: coverage must be 0, not 0.95.
expect(computePricingCoverage(5, 5)).toBe(0)
})
it('excludes sidechain-only work from every human workflow signal', () => {
const editCall = call({
tools: ['Edit'],
timestamp: '2026-06-01T10:06:00Z',
toolSequence: [[{ tool: 'Edit', file: '/home/u/app/src/a.ts' }]],
})
const sidechain = session('agent-reviewer', [
turn({ userMessage: 'review the change', timestamp: '2026-06-01T10:00:00Z' }),
turn({ userMessage: 'you missed the edge case', calls: [editCall], timestamp: '2026-06-01T10:06:00Z' }),
turn({ userMessage: 'that is still wrong', timestamp: '2026-06-01T10:07:00Z' }),
turn({ userMessage: 'revert that change', timestamp: '2026-06-01T10:08:00Z' }),
], { feature: cat(10, 0) } as SessionSummary['categoryBreakdown'])
sidechain.isSidechain = true
const projects = [project([sidechain])]
expect(scanUserCorrections(projects)).toEqual({ corrections: 0, userTurns: 0, correctionRate: null })
expect(medianTimeToFirstEditMs(projects)).toBeNull()
expect(aggregateFileChurn(projects)).toEqual([])
expect(worstOneShotCategory(projects)).toBeNull()
})
})