fix(desktop): keep the sessions answer under the pipe cap (#1889)
Some checks failed
CI / semgrep (push) Waiting to run
Tests / test (22) (push) Waiting to run
Tests / test (22.15.0) (push) Waiting to run
Tests / test (24) (push) Waiting to run
Tests / test (26) (push) Waiting to run
Tests / test-platforms (macos-latest) (push) Waiting to run
Tests / test-platforms (windows-latest) (push) Waiting to run
Tests / desktop (push) Waiting to run
Tests / vscode (push) Waiting to run
macOS Menubar CI / test (push) Has been cancelled
Windows Menubar CI / check (ubuntu-latest) (push) Has been cancelled
Windows Menubar CI / check (windows-latest) (push) Has been cancelled

The Sessions drill-through asks for contribution segments over the whole
population, and over all time that answer is 28MB, past the 16MiB cap:
the request failed and the resident child was killed with it. The CLI
now answers one of n fixed slices with --part i/n, keyed by session
identity so a re-derived report never duplicates or drops a row, and the
bridge fetches eight slices in turn and restores the report order. The
renderer receives the same rows in the same order as before.
This commit is contained in:
Resham Joshi 2026-11-05 22:55:40 +01:00 • committed by GitHub
parent 7457382977
commit 6ff7204e34
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 120 additions and 20 deletions

View file

@ -3192,6 +3192,7 @@ program
.option('--by-pr', 'Group spend by the pull requests each session referenced')
.option('--by-work-unit', 'Group sessions into provider-recorded work units: one row per orchestration root with its delegated children folded beneath')
.option('--contributions', 'JSON only: attach per-session contribution segments (day, category, branch, model, PR) to each row')
.option('--part <i/n>', 'With --contributions: only slice i of n (0-based), as {index, row} entries in report order')
.option('--id <id>', 'With --why: the Claude Code session to explain')
.option('--why', 'Explain why one session cost what it did: findings, spend by prompt, steps (needs --id; Claude Code only)')
.option('--no-pager', 'Print the complete table directly instead of opening the interactive browser')
@ -3222,6 +3223,11 @@ program
process.stderr.write('codeburn: --contributions requires plain --format json (no --by-pr/--by-work-unit)\n')
process.exit(1)
}
const part = opts.part === undefined ? null : (await import('./session-contributions.js')).parseContributionPart(opts.part)
if (opts.part !== undefined && (!opts.contributions || !part)) {
process.stderr.write('codeburn: --part takes i/n (0 <= i < n <= 64) and requires --contributions\n')
process.exit(1)
}
const { aggregateSessions, buildPrAttribution, foldSubagentRows, renderJson, renderTable, renderWorkUnitJson, renderWorkUnitTable } = await import('./sessions-report.js')
const wantsInteractive = opts.format === 'table' && !opts.byPr && !opts.byWorkUnit && opts.pager !== false && process.stdin.isTTY === true && process.stdout.isTTY === true
if (wantsInteractive) setInteractiveScanUI()
@ -3304,8 +3310,9 @@ program
lineage: session.lineage,
}))))
if (opts.contributions) {
const { foldContributionRows, withContributions } = await import('./session-contributions.js')
process.stdout.write(JSON.stringify(foldContributionRows(withContributions(rows, projects), resolution), null, 2) + '\n')
const { foldContributionRows, selectContributionPart, withContributions } = await import('./session-contributions.js')
const report = foldContributionRows(withContributions(rows, projects), resolution)
process.stdout.write(JSON.stringify(part ? selectContributionPart(report, part) : report, null, 2) + '\n')
return
}
if (opts.byWorkUnit) {

View file

@ -273,7 +273,7 @@ const SERVE_OPTIONS: Readonly<Record<string, Readonly<Record<string, ServeOption
'--provider': 'value', '--route': 'value', '--billing': 'value',
'--format': 'value', '--by-pr': 'flag', '--no-pager': 'flag',
'--project': 'value', '--exclude': 'value',
'--contributions': 'flag',
'--contributions': 'flag', '--part': 'value',
},
compare: {
'-p': 'value', '--period': 'value', '--from': 'value', '--to': 'value',

View file

@ -1,3 +1,5 @@
import { createHash } from 'crypto'
import { behavioralCallWeight } from './behavioral-weight.js'
import { dateKey } from './day-aggregator.js'
import { spendProjectIdentity } from './spend-flow.js'
@ -205,3 +207,25 @@ export function foldContributionRows(rows: SessionDrillRow[], resolution: WorkUn
}
})
}
/// `--part i/n`: one of n fixed slices of the contributions report, so a client
/// can fetch a population too large for one answer in pieces. A row's slice is
/// keyed by its identity, not its position, so a session stays in one slice
/// even when the report is re-derived between the slice requests: the slices
/// always concatenate to the whole population, never a duplicate or a gap.
/// Each kept row carries its position in the full report so the client can
/// restore the report order.
export function parseContributionPart(value: string): { index: number; count: number } | null {
const match = /^(\d+)\/(\d+)$/.exec(value)
if (!match) return null
const index = Number(match[1]), count = Number(match[2])
return count >= 1 && count <= 64 && index < count ? { index, count } : null
}
export function contributionPart(row: SessionRow, count: number): number {
return createHash('sha1').update(JSON.stringify([row.provider, row.project, row.sessionId])).digest().readUInt32BE(0) % count
}
export function selectContributionPart(rows: SessionDrillRow[], part: { index: number; count: number }): Array<{ index: number; row: SessionDrillRow }> {
return rows.flatMap((row, index) => contributionPart(row, part.count) === part.index ? [{ index, row }] : [])
}