(s, indent = ' ')
| 358 | |
| 359 | /** The occupancy block, as printed under a run and reused by the aggregator. */ |
| 360 | export function formatOccupancy(s, indent = ' ') { |
| 361 | const o = s.occupancy; |
| 362 | const n = (x) => x.toLocaleString('en-US'); |
| 363 | const pctCtx = (t) => (o.ctxFinal > 0 ? ((t / o.ctxFinal) * 100).toFixed(1) : '0.0'); |
| 364 | const pctWin = (t) => ((t / o.windowTokens) * 100).toFixed(1); |
| 365 | const rows = []; |
| 366 | const row = (label, tok, chars, results) => rows.push( |
| 367 | `${indent} ${label.padEnd(18)}${(n(tok) + ' tok').padStart(12)} ${(pctCtx(tok) + '%').padStart(6)} of ctx ` + |
| 368 | `${(pctWin(tok) + '%').padStart(6)} of ${Math.round(o.windowTokens / 1000)}k win` + |
| 369 | (chars !== undefined ? ` (${n(chars)} chars, ${results} result${results === 1 ? '' : 's'})` : '') |
| 370 | ); |
| 371 | const out = [`${indent}Residual context occupancy at end of run:`]; |
| 372 | out.push(`${indent} ${'final context'.padEnd(18)}${(n(o.ctxFinal) + ' tok').padStart(12)} ${(pctWin(o.ctxFinal) + '%').padStart(6)} of ${Math.round(o.windowTokens / 1000)}k window`); |
| 373 | row('codegraph', o.residual.codegraph, o.chars.codegraph, o.results.codegraph); |
| 374 | row('Read', o.residual.read, o.chars.read, o.results.read); |
| 375 | row('Grep/Glob', o.residual.search, o.chars.search, o.results.search); |
| 376 | row('Bash', o.residual.bash, o.chars.bash, o.results.bash); |
| 377 | row('→ file-access', o.residualFileAccess, o.charsFileAccess, |
| 378 | o.results.read + o.results.search + o.results.bash); |
| 379 | row('other tools', o.residual.other, o.chars.other, o.results.other); |
| 380 | const toolTotal = Object.values(o.residual).reduce((a, b) => a + b, 0); |
| 381 | row('base (prompt+prose)', Math.max(0, o.ctxFinal - toolTotal)); |
| 382 | out.push(`${indent} ${' of which fixed'.padEnd(18)}${(n(o.ctxBase) + ' tok').padStart(12)} system + tool schemas + question, before any tool answered`); |
| 383 | out.push(...rows); |
| 384 | const dropped = o.contributed.codegraph + o.contributedFileAccess + o.contributed.other |
| 385 | - (o.residual.codegraph + o.residualFileAccess + o.residual.other); |
| 386 | out.push( |
| 387 | `${indent} measure: ${o.charsPerToken.toFixed(2)} chars/tok ${o.calibrated ? 'measured' : '(FALLBACK — no clean gap to calibrate on)'}` + |
| 388 | (o.dispersion !== null ? ` ±${(o.dispersion * 100).toFixed(1)}%` : '') + |
| 389 | ` · turns ${s.turns} · compactions ${o.compactions}` + |
| 390 | (o.evicted > 0 || dropped > 1 ? ` · evicted ${n(o.evicted)} tok` : '') |
| 391 | ); |
| 392 | return out.join('\n'); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * One codegraph_explore response, split into the per-file source sections the |
no test coverage detected