opencode-usage-insights 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -429,6 +429,7 @@ import { basename } from "path";
429
429
 
430
430
  // src/limits.ts
431
431
  var MAX_PANEL_WIDTH = 200;
432
+ var MAX_HISTORY_WIDTH = 120;
432
433
  var MAX_TURNS = 50;
433
434
  function clampPanelWidth(value) {
434
435
  if (!Number.isFinite(value))
@@ -1512,7 +1513,10 @@ var GAP_BUCKETS = [
1512
1513
  ["<1m", 0, 60000],
1513
1514
  ["1\u20135m", 60000, 5 * 60000],
1514
1515
  ["5\u201315m", 5 * 60000, 15 * 60000],
1515
- ["15\u201360m", 15 * 60000, 60 * 60000],
1516
+ ["15\u201320m", 15 * 60000, 20 * 60000],
1517
+ ["20\u201330m", 20 * 60000, 30 * 60000],
1518
+ ["30\u201345m", 30 * 60000, 45 * 60000],
1519
+ ["45\u201360m", 45 * 60000, 60 * 60000],
1516
1520
  ["1\u20136h", 60 * 60000, 6 * 60 * 60000],
1517
1521
  [">6h", 6 * 60 * 60000, undefined]
1518
1522
  ];
@@ -2023,15 +2027,6 @@ function truncateMiddle(value, width) {
2023
2027
  const tail = width - 1 - head;
2024
2028
  return `${clean.slice(0, head)}\u2026${clean.slice(clean.length - tail)}`;
2025
2029
  }
2026
- function tableWidths(width, layouts) {
2027
- for (const layout of layouts) {
2028
- const total = layout.reduce((sum, column) => sum + column, 0) + layout.length - 1;
2029
- if (total <= width)
2030
- return [...layout];
2031
- }
2032
- const last = layouts[layouts.length - 1];
2033
- return last === undefined ? [] : [...last];
2034
- }
2035
2030
  function modelCell(modelID, variant, width) {
2036
2031
  return truncateMiddle(variant === undefined ? modelID : `${modelID} ${variant}`, width);
2037
2032
  }
@@ -2050,6 +2045,47 @@ function formatTableRow(cells, widths, aligns) {
2050
2045
  }
2051
2046
  return parts.join(" ");
2052
2047
  }
2048
+ function fitTableColumns(columns, width) {
2049
+ const chosen = new Set;
2050
+ for (let index = 0;index < columns.length; index += 1) {
2051
+ if (columns[index].priority === undefined)
2052
+ chosen.add(index);
2053
+ }
2054
+ if (chosen.size === 0)
2055
+ return [];
2056
+ const total = () => {
2057
+ let sum = chosen.size - 1;
2058
+ for (const index of chosen)
2059
+ sum += columns[index].width;
2060
+ return sum;
2061
+ };
2062
+ const optional = columns.map((column, index) => ({ column, index })).filter((entry) => entry.column.priority !== undefined).sort((left, right) => left.column.priority - right.column.priority || left.index - right.index);
2063
+ for (const { column, index } of optional) {
2064
+ if (total() + column.width + 1 + (column.reserve ?? 0) <= width)
2065
+ chosen.add(index);
2066
+ }
2067
+ while (chosen.size > 1 && total() > width)
2068
+ chosen.delete(Math.max(...chosen));
2069
+ const widths = new Map;
2070
+ let room = width - total();
2071
+ for (let index = 0;index < columns.length && room > 0; index += 1) {
2072
+ if (!chosen.has(index))
2073
+ continue;
2074
+ const column = columns[index];
2075
+ const grow = Math.min(room, (column.max ?? column.width) - column.width);
2076
+ if (grow <= 0)
2077
+ continue;
2078
+ widths.set(index, column.width + grow);
2079
+ room -= grow;
2080
+ }
2081
+ return [...chosen].sort((left, right) => left - right).map((index) => ({ index, width: widths.get(index) ?? columns[index].width }));
2082
+ }
2083
+ function fitTable(columns, width) {
2084
+ return fitTableColumns(columns, width).map(({ index, width: fitted }) => {
2085
+ const column = columns[index];
2086
+ return { index, header: column.header, width: fitted, align: column.align ?? "left", cell: column.cell };
2087
+ });
2088
+ }
2053
2089
  function count(n, singular, plural = `${singular}s`) {
2054
2090
  return `${formatCount(n)} ${n === 1 ? singular : plural}`;
2055
2091
  }
@@ -2057,21 +2093,30 @@ function kvGrid(pairs, width) {
2057
2093
  const lines = [];
2058
2094
  const label = 11;
2059
2095
  const column = Math.floor(width / 2);
2096
+ const pairLines = (pair) => {
2097
+ const text = `${padCell(pair[0], label)}${pair[1]}`;
2098
+ if (text.length <= width)
2099
+ return [text];
2100
+ return [padCell(pair[0], label), ` ${pair[1]}`].map((line) => truncate(line, width));
2101
+ };
2060
2102
  for (let index = 0;index < pairs.length; index += 2) {
2061
2103
  const left = pairs[index];
2062
2104
  const right = pairs[index + 1];
2063
- const leftText = `${padCell(left[0], label)}${left[1]}`;
2064
2105
  if (right === undefined) {
2065
- lines.push({ text: leftText, tone: "base" });
2106
+ for (const text of pairLines(left))
2107
+ lines.push({ text, tone: "base" });
2066
2108
  continue;
2067
2109
  }
2110
+ const leftText = `${padCell(left[0], label)}${left[1]}`;
2068
2111
  const rightText = `${padCell(right[0], label)}${right[1]}`;
2069
2112
  const gap = Math.max(2, column - leftText.length);
2070
2113
  if (leftText.length + gap + rightText.length <= width) {
2071
2114
  lines.push({ text: `${leftText}${" ".repeat(gap)}${rightText}`, tone: "base" });
2072
2115
  } else {
2073
- lines.push({ text: leftText, tone: "base" });
2074
- lines.push({ text: rightText, tone: "base" });
2116
+ for (const text of pairLines(left))
2117
+ lines.push({ text, tone: "base" });
2118
+ for (const text of pairLines(right))
2119
+ lines.push({ text, tone: "base" });
2075
2120
  }
2076
2121
  }
2077
2122
  return lines;
@@ -2322,14 +2367,16 @@ function overviewLines(data, width) {
2322
2367
  const lines = [];
2323
2368
  const estimates = data.estimates !== false;
2324
2369
  lines.push({
2325
- text: `${rangeLabel(data.range)} \xB7 ${count(data.usage.steps, "step")} \xB7 ${count(data.usage.sessions, "session")} \xB7 ${count(data.usage.prompts, "prompt")}`,
2370
+ text: truncate(`${rangeLabel(data.range)} \xB7 ${count(data.usage.steps, "step")} \xB7 ${count(data.usage.sessions, "session")} \xB7 ${count(data.usage.prompts, "prompt")}`, width),
2326
2371
  tone: "muted"
2327
2372
  });
2328
2373
  lines.push(...kvGrid(buildOverviewPairs(data.usage, "all", estimates), width));
2329
2374
  if (data.activity !== undefined && data.activity.length > 1) {
2330
- const series = reduceSeries(data.activity);
2375
+ const suffix = ` ${count(data.activity.length, "bucket")}`;
2376
+ const bars = Math.max(4, width - "activity ".length - suffix.length);
2377
+ const series = reduceSeries(data.activity, bars);
2331
2378
  lines.push({ text: "", tone: "muted" });
2332
- lines.push({ text: `activity ${sparkline(series)} ${count(series.length, "bucket")}`, tone: "muted" });
2379
+ lines.push({ text: truncate(`activity ${sparkline(series)}${suffix}`, width), tone: "muted" });
2333
2380
  }
2334
2381
  if (data.session !== undefined) {
2335
2382
  lines.push({ text: "", tone: "muted" });
@@ -2340,7 +2387,7 @@ function overviewLines(data, width) {
2340
2387
  const cost = data.subagents.reduce((sum, row) => sum + row.cost, 0);
2341
2388
  const listCost = data.subagents.reduce((sum, row) => sum + row.listCost, 0);
2342
2389
  lines.push({
2343
- text: ` subagents ${count(data.subagents.length, "session")} \xB7 ${count(steps, "step")} \xB7 ${formatSpend(cost, estimates ? listCost : 0)} (their own sessions)`,
2390
+ text: truncate(` subagents ${count(data.subagents.length, "session")} \xB7 ${count(steps, "step")} \xB7 ${formatSpend(cost, estimates ? listCost : 0)} (their own sessions)`, width),
2344
2391
  tone: "muted"
2345
2392
  });
2346
2393
  }
@@ -2372,65 +2419,33 @@ function coveragePairs(coverage) {
2372
2419
  ]
2373
2420
  ];
2374
2421
  }
2375
- function modelColumns(width) {
2376
- const layouts = [
2377
- { model: 30, steps: 6, prompt: 10, cached: 10, hit: 7, out: 9, cost: 9, spread: 24 },
2378
- { model: 26, steps: 6, prompt: 9, cached: 9, hit: 7, out: 8, cost: 8, spread: 20 },
2379
- { model: 20, steps: 6, prompt: 9, cached: 9, hit: 6, out: 8, cost: 8, spread: 0 },
2380
- { model: 18, steps: 5, prompt: 8, cached: 8, hit: 0, out: 7, cost: 8, spread: 0 },
2381
- { model: 14, steps: 5, prompt: 8, cached: 8, hit: 0, out: 7, cost: 0, spread: 0 }
2382
- ];
2383
- for (const layout of layouts) {
2384
- const total = Object.values(layout).reduce((sum, column) => sum + column, 0) + Object.values(layout).filter((column) => column > 0).length - 1;
2385
- if (total <= width)
2386
- return layout;
2387
- }
2388
- return layouts[layouts.length - 1];
2389
- }
2390
2422
  function modelsLines(rows, width, extras, limit = 40, estimates = true) {
2391
- const columns = modelColumns(width);
2392
- const widths = [columns.model, columns.steps, columns.prompt, columns.cached];
2393
- const aligns = ["left", "right", "right", "right"];
2394
- const header = ["model", "steps", "prompt", "cached"];
2395
- if (columns.hit > 0) {
2396
- widths.push(columns.hit);
2397
- aligns.push("right");
2398
- header.push("hit");
2399
- }
2400
- widths.push(columns.out);
2401
- aligns.push("right");
2402
- header.push("out");
2403
- if (columns.cost > 0) {
2404
- widths.push(columns.cost);
2405
- aligns.push("right");
2406
- header.push("cost");
2407
- }
2408
- if (columns.spread > 0) {
2409
- widths.push(columns.spread);
2410
- aligns.push("left");
2411
- header.push("providers");
2412
- }
2413
- const lines = [{ text: formatTableRow(header, widths, aligns), tone: HEADER_TONE }];
2423
+ let modelWidth = 14;
2424
+ const columns = fitTable([
2425
+ { header: "model", width: 14, max: 36, cell: (row) => modelCell(row.modelID, row.variant, modelWidth) },
2426
+ { header: "steps", width: 6, align: "right", cell: (row) => row.steps },
2427
+ { header: "prompt", width: 7, max: 10, align: "right", cell: (row) => row.prompt },
2428
+ { header: "cached", width: 7, max: 10, align: "right", cell: (row) => row.cached },
2429
+ { header: "hit", width: 6, max: 7, priority: 2, reserve: 10, align: "right", cell: (row) => row.hit },
2430
+ { header: "out", width: 7, max: 9, priority: 3, reserve: 10, align: "right", cell: (row) => row.output },
2431
+ { header: "cost", width: 8, max: 9, priority: 1, align: "right", cell: (row) => row.cost },
2432
+ { header: "providers", width: 18, max: 24, priority: 4, reserve: 22, cell: (row) => row.providers }
2433
+ ], width);
2434
+ modelWidth = columns.find((column) => column.index === 0)?.width ?? 0;
2435
+ if (columns.length === 0)
2436
+ return [{ text: truncate("no room for a table at this width", width), tone: "muted" }];
2437
+ const lines = [
2438
+ {
2439
+ text: formatTableRow(columns.map((column) => column.header), columns.map((column) => column.width), columns.map((column) => column.align)),
2440
+ tone: HEADER_TONE
2441
+ }
2442
+ ];
2414
2443
  for (const model of rows.slice(0, limit)) {
2415
2444
  const key = modelKey(model.modelID, model.variant);
2416
- const name = modelCell(model.modelID, model.variant, columns.model);
2417
2445
  const buckets = usageBuckets(model.totals);
2418
2446
  const providers = unique(model.providers.map((provider) => provider.providerID));
2419
2447
  const cache = extras?.cache?.get(key);
2420
2448
  const switches = extras?.switches?.get(key);
2421
- const cells = [name, formatCount(buckets.steps), formatTokens(buckets.prompt), formatTokens(buckets.cached)];
2422
- if (columns.hit > 0)
2423
- cells.push(formatPercent(cache?.hit ?? buckets.hit));
2424
- cells.push(formatTokens(buckets.output));
2425
- if (columns.cost > 0)
2426
- cells.push(formatSpend(buckets.cost, estimates ? buckets.listCost : 0));
2427
- if (columns.spread > 0)
2428
- cells.push(providers.join(", "));
2429
- lines.push({
2430
- text: formatTableRow(cells, widths, aligns),
2431
- tone: "base",
2432
- row: { kind: "model", value: model.modelID, ...model.variant !== undefined ? { variant: model.variant } : {} }
2433
- });
2434
2449
  const details = [];
2435
2450
  if (cache !== undefined)
2436
2451
  details.push(`p50 hit ${formatPercent(cache.hitP50)}`);
@@ -2440,62 +2455,76 @@ function modelsLines(rows, width, extras, limit = 40, estimates = true) {
2440
2455
  if (switches.cacheLost > 0)
2441
2456
  details.push(`${formatTokens(switches.cacheLost)} lost on a switch`);
2442
2457
  }
2443
- if (details.length > 0) {
2444
- lines.push({ text: truncate(`${" ".repeat(columns.model + 1)}${details.join(" \xB7 ")}`, width), tone: "muted" });
2458
+ const view = {
2459
+ modelID: model.modelID,
2460
+ variant: model.variant,
2461
+ steps: formatCount(buckets.steps),
2462
+ prompt: formatTokens(buckets.prompt),
2463
+ cached: formatTokens(buckets.cached),
2464
+ hit: formatPercent(cache?.hit ?? buckets.hit),
2465
+ output: formatTokens(buckets.output),
2466
+ cost: formatSpend(buckets.cost, estimates ? buckets.listCost : 0),
2467
+ providers: providers.join(", "),
2468
+ detail: details.join(" \xB7 ")
2469
+ };
2470
+ lines.push({
2471
+ text: formatTableRow(columns.map((column) => column.cell(view)), columns.map((column) => column.width), columns.map((column) => column.align)),
2472
+ tone: "base",
2473
+ row: { kind: "model", value: model.modelID, ...model.variant !== undefined ? { variant: model.variant } : {} }
2474
+ });
2475
+ if (view.detail !== "") {
2476
+ lines.push({ text: truncate(`${" ".repeat(modelWidth + 1)}${view.detail}`, width), tone: "muted" });
2445
2477
  }
2446
2478
  }
2447
2479
  if (rows.length > limit)
2448
- lines.push({ text: `\u2026 ${count(rows.length - limit, "more model")} \xB7 narrow the range or filter`, tone: "muted" });
2480
+ lines.push({ text: truncate(`\u2026 ${count(rows.length - limit, "more model")} \xB7 narrow the range or filter`, width), tone: "muted" });
2449
2481
  if (rows.length === 0)
2450
- lines.push({ text: "no usage recorded for this range", tone: "muted" });
2482
+ lines.push({ text: truncate("no usage recorded for this range", width), tone: "muted" });
2451
2483
  return lines;
2452
2484
  }
2453
2485
  function unique(values) {
2454
2486
  return [...new Set(values)];
2455
2487
  }
2456
- var tableWidth = (columns) => columns.reduce((sum, column) => sum + column.width, 0) + columns.length - 1;
2457
- function fitColumns(columns, width) {
2458
- const chosen = [...columns];
2459
- while (chosen.length > 1 && tableWidth(chosen) > width) {
2460
- const optional = [...chosen].reverse().findIndex((column) => column.optional === true);
2461
- if (optional === -1)
2462
- break;
2463
- chosen.splice(chosen.length - 1 - optional, 1);
2464
- }
2465
- return chosen;
2466
- }
2467
2488
  function renderTable(rows, columns, width, options) {
2468
- const chosen = fitColumns(columns, width);
2489
+ const chosen = fitTableColumns(columns, width).map(({ index, width: fitted }) => {
2490
+ const column = columns[index];
2491
+ const align = column.align ?? "left";
2492
+ return { ...column, width: fitted, align };
2493
+ });
2494
+ if (chosen.length === 0)
2495
+ return [{ text: truncate(options.empty, width), tone: "muted" }];
2469
2496
  const widths = chosen.map((column) => column.width);
2470
- const aligns = chosen.map((column) => column.align ?? "left");
2471
- const alignsTyped = aligns;
2472
- const lines = [{ text: formatTableRow(chosen.map((column) => column.header), widths, alignsTyped), tone: HEADER_TONE }];
2497
+ const aligns = chosen.map((column) => column.align);
2498
+ const lines = [
2499
+ { text: formatTableRow(chosen.map((column) => column.header), widths, aligns), tone: HEADER_TONE }
2500
+ ];
2473
2501
  if (rows.length === 0)
2474
- lines.push({ text: options.empty, tone: "muted" });
2502
+ lines.push({ text: truncate(options.empty, width), tone: "muted" });
2475
2503
  for (const row of rows.slice(0, options.limit)) {
2476
2504
  const identity = options.row?.(row);
2477
2505
  lines.push({
2478
- text: formatTableRow(chosen.map((column) => column.cell(row)), widths, alignsTyped),
2506
+ text: formatTableRow(chosen.map((column) => column.cell(row, column.width)), widths, aligns),
2479
2507
  tone: options.tone?.(row) ?? "base",
2480
2508
  ...identity === undefined ? {} : { row: identity }
2481
2509
  });
2482
2510
  }
2483
- if (rows.length > options.limit)
2484
- lines.push({ text: `\u2026 ${count(rows.length - options.limit, "more row")}`, tone: "muted" });
2511
+ if (rows.length > options.limit) {
2512
+ lines.push({ text: truncate(`\u2026 ${count(rows.length - options.limit, "more row")}`, width), tone: "muted" });
2513
+ }
2485
2514
  return lines;
2486
2515
  }
2487
2516
  function providerSummaryLines(rows, width, limit = 40, estimates = true) {
2488
2517
  const lines = renderTable(rows, [
2489
- { header: "provider", width: 24, cell: (row) => truncate(row.provider ?? "\u2013 (unknown)", 24) },
2490
- { header: "models", width: 13, align: "right", optional: true, cell: (row) => formatCount(row.models.length) },
2518
+ { header: "provider", width: 14, max: 32, cell: (row, column) => truncate(row.provider ?? "\u2013 (unknown)", column) },
2519
+ { header: "models", width: 6, align: "right", priority: 4, cell: (row) => formatCount(row.models.length) },
2491
2520
  { header: "steps", width: 6, align: "right", cell: (row) => formatCount(row.usage.steps) },
2492
- { header: "share", width: 6, align: "right", optional: true, cell: (row) => formatPercent(row.share, 0) },
2493
- { header: "prompt", width: 10, align: "right", cell: (row) => formatTokens(row.cache.prompt) },
2494
- { header: "cached", width: 10, align: "right", cell: (row) => formatTokens(row.cache.cached) },
2495
- { header: "hit", width: 7, align: "right", optional: true, cell: (row) => formatPercent(row.cache.hit) },
2496
- { header: "tps p50", width: 8, align: "right", optional: true, cell: (row) => formatTps(row.tps) },
2497
- { header: "wall", width: 8, align: "right", optional: true, cell: (row) => formatMs(row.wall) },
2498
- { header: "cost", width: 9, align: "right", optional: true, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
2521
+ { header: "share", width: 6, align: "right", priority: 3, cell: (row) => formatPercent(row.share, 0) },
2522
+ { header: "prompt", width: 7, max: 10, align: "right", cell: (row) => formatTokens(row.cache.prompt) },
2523
+ { header: "cached", width: 7, max: 10, align: "right", cell: (row) => formatTokens(row.cache.cached) },
2524
+ { header: "hit", width: 6, max: 7, align: "right", priority: 2, cell: (row) => formatPercent(row.cache.hit) },
2525
+ { header: "tps p50", width: 8, align: "right", priority: 5, cell: (row) => formatTps(row.tps) },
2526
+ { header: "wall", width: 7, align: "right", priority: 6, cell: (row) => formatMs(row.wall) },
2527
+ { header: "cost", width: 8, max: 9, align: "right", priority: 1, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
2499
2528
  ], width, {
2500
2529
  limit,
2501
2530
  empty: "no usage recorded for this range",
@@ -2506,16 +2535,16 @@ function providerSummaryLines(rows, width, limit = 40, estimates = true) {
2506
2535
  }
2507
2536
  function providerLines(rows, width, limit = 40, estimates = true) {
2508
2537
  return renderTable(rows, [
2509
- { header: "model", width: 26, cell: (row) => modelCell(row.modelID, row.variant, 26) },
2510
- { header: "provider", width: 22, cell: (row) => truncate(row.provider ?? "\u2013 (unknown)", 22) },
2538
+ { header: "model", width: 24, max: 30, cell: (row, column) => modelCell(row.modelID, row.variant, column) },
2539
+ { header: "provider", width: 12, max: 24, cell: (row, column) => truncate(row.provider ?? "\u2013 (unknown)", column) },
2511
2540
  { header: "steps", width: 6, align: "right", cell: (row) => formatCount(row.usage.steps) },
2512
- { header: "share", width: 6, align: "right", optional: true, cell: (row) => formatPercent(row.share, 0) },
2513
- { header: "prompt", width: 10, align: "right", cell: (row) => formatTokens(row.cache.prompt) },
2514
- { header: "cached", width: 10, align: "right", cell: (row) => formatTokens(row.cache.cached) },
2515
- { header: "hit", width: 7, align: "right", optional: true, cell: (row) => formatPercent(row.cache.hit) },
2516
- { header: "tps p50", width: 8, align: "right", optional: true, cell: (row) => formatTps(row.tps) },
2517
- { header: "wall", width: 8, align: "right", optional: true, cell: (row) => formatMs(row.wall) },
2518
- { header: "cost", width: 9, align: "right", optional: true, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
2541
+ { header: "share", width: 6, align: "right", priority: 5, cell: (row) => formatPercent(row.share, 0) },
2542
+ { header: "prompt", width: 7, max: 10, align: "right", priority: 2, cell: (row) => formatTokens(row.cache.prompt) },
2543
+ { header: "cached", width: 7, max: 10, align: "right", priority: 3, cell: (row) => formatTokens(row.cache.cached) },
2544
+ { header: "hit", width: 6, max: 7, align: "right", priority: 4, cell: (row) => formatPercent(row.cache.hit) },
2545
+ { header: "tps p50", width: 8, align: "right", priority: 6, cell: (row) => formatTps(row.tps) },
2546
+ { header: "wall", width: 7, align: "right", priority: 7, cell: (row) => formatMs(row.wall) },
2547
+ { header: "cost", width: 8, max: 9, align: "right", priority: 1, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
2519
2548
  ], width, {
2520
2549
  limit,
2521
2550
  empty: "no usage recorded for this range",
@@ -2525,80 +2554,126 @@ function providerLines(rows, width, limit = 40, estimates = true) {
2525
2554
  }
2526
2555
  function cacheLines(data, width) {
2527
2556
  const lines = [];
2557
+ const push = (text, tone = "base") => {
2558
+ lines.push({ text: truncate(text, width), tone });
2559
+ };
2528
2560
  const total = data.drops.reduce((sum, drop) => sum + drop.tokens, 0);
2529
- lines.push({ text: `reuse losses ${count(data.drops.length, "event")} \xB7 ${formatTokens(total)} tokens`, tone: total > 0 ? "warning" : "base" });
2561
+ push(`reuse losses ${count(data.drops.length, "event")} \xB7 ${formatTokens(total)} tokens`, total > 0 ? "warning" : "base");
2530
2562
  for (const reason of ["expired", "compaction", "unknown"]) {
2531
2563
  const group = data.drops.filter((drop) => drop.reason === reason);
2532
2564
  if (group.length === 0)
2533
2565
  continue;
2534
2566
  const tokens = group.reduce((sum, drop) => sum + drop.tokens, 0);
2535
- lines.push({ text: ` ${padCell(reason, 11)}${formatCount(group.length)} \xB7 ${formatTokens(tokens)}`, tone: reason === "expired" ? "warning" : "muted" });
2567
+ push(` ${padCell(reason, 11)}${formatCount(group.length)} \xB7 ${formatTokens(tokens)}`, reason === "expired" ? "warning" : "muted");
2536
2568
  }
2537
2569
  if (data.drops.length === 0)
2538
- lines.push({ text: " none: prompt cache held through every gap", tone: "muted" });
2570
+ push(" none: prompt cache held through every gap", "muted");
2539
2571
  if (data.models !== undefined && data.models.length > 0) {
2540
2572
  lines.push({ text: "", tone: "muted" });
2541
- lines.push({ text: "reuse by model", tone: HEADER_TONE });
2542
- const widths = width >= 100 ? [30, 7, 11, 10, 8, 8, 10] : [18, 6, 9, 9, 7, 7, 9];
2543
- lines.push({ text: formatTableRow(["model", "steps", "prompt", "cached", "write", "hit", "hit p50"], widths, ["left", "right", "right", "right", "right", "right", "right"]), tone: "muted" });
2544
- for (const row of data.models.slice(0, 10)) {
2545
- const name = modelCell(row.usage.modelID, row.usage.variant, widths[0]);
2546
- lines.push({
2547
- text: formatTableRow([name, formatCount(row.cache.steps), formatTokens(row.cache.prompt), formatTokens(row.cache.cached), formatTokens(row.cache.cacheWrite), formatPercent(row.cache.hit), formatPercent(row.cache.hitP50)], widths, ["left", "right", "right", "right", "right", "right", "right"]),
2548
- tone: "base",
2549
- row: { kind: "model", value: row.usage.modelID, ...row.usage.variant !== undefined ? { variant: row.usage.variant } : {} }
2550
- });
2573
+ push("reuse by model", HEADER_TONE);
2574
+ let modelWidth = 12;
2575
+ const reuse = fitTable([
2576
+ { header: "model", width: 12, max: 30, cell: (row) => modelCell(row.usage.modelID, row.usage.variant, modelWidth) },
2577
+ { header: "steps", width: 6, align: "right", cell: (row) => formatCount(row.cache.steps) },
2578
+ { header: "prompt", width: 7, max: 10, align: "right", cell: (row) => formatTokens(row.cache.prompt) },
2579
+ { header: "cached", width: 7, max: 10, align: "right", cell: (row) => formatTokens(row.cache.cached) },
2580
+ { header: "write", width: 6, max: 8, align: "right", priority: 1, cell: (row) => formatTokens(row.cache.cacheWrite) },
2581
+ { header: "hit", width: 6, max: 7, align: "right", priority: 2, cell: (row) => formatPercent(row.cache.hit) },
2582
+ { header: "hit p50", width: 7, max: 9, align: "right", priority: 3, cell: (row) => formatPercent(row.cache.hitP50) }
2583
+ ], width);
2584
+ modelWidth = reuse.find((column) => column.index === 0)?.width ?? 0;
2585
+ if (reuse.length > 0) {
2586
+ lines.push({ text: formatTableRow(reuse.map((column) => column.header), reuse.map((column) => column.width), reuse.map((column) => column.align)), tone: "muted" });
2587
+ for (const row of data.models.slice(0, 10)) {
2588
+ lines.push({
2589
+ text: formatTableRow(reuse.map((column) => column.cell(row)), reuse.map((column) => column.width), reuse.map((column) => column.align)),
2590
+ tone: "base",
2591
+ row: { kind: "model", value: row.usage.modelID, ...row.usage.variant !== undefined ? { variant: row.usage.variant } : {} }
2592
+ });
2593
+ }
2551
2594
  }
2552
2595
  }
2553
2596
  if (data.gap !== undefined && data.gap.samples > 0) {
2554
2597
  lines.push({ text: "", tone: "muted" });
2555
- lines.push({ text: "cache survival against idle time", tone: HEADER_TONE });
2556
- const widths = [9, 8, 9, 9, 10];
2557
- lines.push({ text: formatTableRow(["gap", "pairs", "kept", "drops", "lost"], widths, ["left", "right", "right", "right", "right"]), tone: "muted" });
2558
- for (const bucket of data.gap.buckets) {
2559
- if (bucket.samples === 0)
2560
- continue;
2561
- lines.push({
2562
- text: formatTableRow([bucket.label, formatCount(bucket.samples), formatPercent(bucket.retainedRatio, 0), formatCount(bucket.drops), formatTokens(bucket.lostTokens)], widths, ["left", "right", "right", "right", "right"]),
2563
- tone: bucket.drops > 0 ? "warning" : "base"
2564
- });
2598
+ push("cache survival against idle time", HEADER_TONE);
2599
+ const buckets = data.gap.buckets;
2600
+ const survival = fitTable([
2601
+ { header: "gap", width: 8, max: 12, cell: (bucket) => bucket.label },
2602
+ { header: "pairs", width: 6, align: "right", cell: (bucket) => formatCount(bucket.samples) },
2603
+ { header: "kept", width: 7, align: "right", cell: (bucket) => formatPercent(bucket.retainedRatio, 0) },
2604
+ { header: "drops", width: 7, align: "right", cell: (bucket) => formatCount(bucket.drops) },
2605
+ { header: "lost", width: 8, align: "right", cell: (bucket) => formatTokens(bucket.lostTokens) }
2606
+ ], width);
2607
+ if (survival.length > 0) {
2608
+ lines.push({ text: formatTableRow(survival.map((column) => column.header), survival.map((column) => column.width), survival.map((column) => column.align)), tone: "muted" });
2609
+ for (const bucket of buckets) {
2610
+ if (bucket.samples === 0)
2611
+ continue;
2612
+ lines.push({
2613
+ text: formatTableRow(survival.map((column) => column.cell(bucket)), survival.map((column) => column.width), survival.map((column) => column.align)),
2614
+ tone: bucket.drops > 0 ? "warning" : "base"
2615
+ });
2616
+ }
2565
2617
  }
2566
2618
  if (data.gap.providers.length > 1) {
2567
2619
  lines.push({ text: "", tone: "muted" });
2568
- lines.push({ text: "reuse kept, by provider", tone: HEADER_TONE });
2569
- const providerWidths = [26, 8, 9, 9];
2570
- lines.push({ text: formatTableRow(["provider", "pairs", "kept", "lost"], providerWidths, ["left", "right", "right", "right"]), tone: "muted" });
2571
- for (const row of data.gap.providers.slice(0, 8)) {
2572
- lines.push({
2573
- text: formatTableRow([row.provider, formatCount(row.samples), formatPercent(row.retainedRatio, 0), formatTokens(row.lostTokens)], providerWidths, ["left", "right", "right", "right"]),
2574
- tone: "base"
2575
- });
2620
+ push("reuse kept, by provider", HEADER_TONE);
2621
+ const providers = data.gap.providers;
2622
+ const reuse = fitTable([
2623
+ { header: "provider", width: 12, max: 30, cell: (row) => row.provider },
2624
+ { header: "pairs", width: 6, align: "right", cell: (row) => formatCount(row.samples) },
2625
+ { header: "kept", width: 7, align: "right", cell: (row) => formatPercent(row.retainedRatio, 0) },
2626
+ { header: "lost", width: 8, align: "right", cell: (row) => formatTokens(row.lostTokens) }
2627
+ ], width);
2628
+ if (reuse.length > 0) {
2629
+ lines.push({ text: formatTableRow(reuse.map((column) => column.header), reuse.map((column) => column.width), reuse.map((column) => column.align)), tone: "muted" });
2630
+ for (const row of providers.slice(0, 8)) {
2631
+ lines.push({
2632
+ text: formatTableRow(reuse.map((column) => column.cell(row)), reuse.map((column) => column.width), reuse.map((column) => column.align)),
2633
+ tone: "base"
2634
+ });
2635
+ }
2576
2636
  }
2577
2637
  }
2578
2638
  }
2579
2639
  lines.push({ text: "", tone: "muted" });
2580
- lines.push({ text: "observed cache TTL bounds", tone: HEADER_TONE });
2640
+ push("observed cache TTL bounds", HEADER_TONE);
2581
2641
  if (data.evidence.length === 0) {
2582
- lines.push({ text: " no comparable step pairs yet", tone: "muted" });
2642
+ push(" no comparable step pairs yet", "muted");
2583
2643
  } else {
2584
- const widths = [Math.max(18, Math.floor(width * 0.4)), 14, 12, 9];
2585
- lines.push({ text: formatTableRow(["model", "hits up to", "drops from", "samples"], widths, ["left", "right", "right", "right"]), tone: "muted" });
2586
- for (const row of data.evidence.slice(0, 10)) {
2587
- lines.push({
2588
- text: formatTableRow([`${row.providerID}/${row.modelID}`, formatGap(row.maxHitGapMs), formatGap(row.minDropGapMs), formatCount(row.samples)], widths, ["left", "right", "right", "right"]),
2589
- tone: "base"
2590
- });
2644
+ const evidence = fitTable([
2645
+ { header: "model", width: 12, max: 44, cell: (row) => `${row.providerID}/${row.modelID}` },
2646
+ { header: "hits up to", width: 10, align: "right", cell: (row) => formatGap(row.maxHitGapMs) },
2647
+ { header: "drops from", width: 10, align: "right", cell: (row) => formatGap(row.minDropGapMs) },
2648
+ { header: "samples", width: 7, align: "right", priority: 1, cell: (row) => formatCount(row.samples) }
2649
+ ], width);
2650
+ if (evidence.length > 0) {
2651
+ lines.push({ text: formatTableRow(evidence.map((column) => column.header), evidence.map((column) => column.width), evidence.map((column) => column.align)), tone: "muted" });
2652
+ for (const row of data.evidence.slice(0, 10)) {
2653
+ lines.push({
2654
+ text: formatTableRow(evidence.map((column) => column.cell(row)), evidence.map((column) => column.width), evidence.map((column) => column.align)),
2655
+ tone: "base"
2656
+ });
2657
+ }
2591
2658
  }
2592
2659
  }
2593
2660
  if (data.drops.length > 0) {
2594
2661
  lines.push({ text: "", tone: "muted" });
2595
- lines.push({ text: "recent losses", tone: HEADER_TONE });
2596
- for (const drop of [...data.drops].sort((a, b) => b.at - a.at).slice(0, 8)) {
2597
- const when = new Date(drop.at).toLocaleTimeString("en-GB");
2598
- lines.push({
2599
- text: formatTableRow([when, `${drop.providerID ?? ""}/${drop.modelID ?? ""}`, `\u2212${formatTokens(drop.tokens)}`, `${formatGap(drop.gapMs)} idle \xB7 ${drop.reason}`], [10, Math.max(20, Math.floor(width * 0.42)), 10, 24], ["left", "left", "right", "left"]),
2600
- tone: drop.reason === "expired" ? "warning" : "base"
2601
- });
2662
+ push("recent losses", HEADER_TONE);
2663
+ const losses = fitTable([
2664
+ { header: "when", width: 8, cell: (drop) => new Date(drop.at).toLocaleTimeString("en-GB") },
2665
+ { header: "provider/model", width: 14, max: 40, cell: (drop) => `${drop.providerID ?? ""}/${drop.modelID ?? ""}` },
2666
+ { header: "tokens", width: 7, align: "right", cell: (drop) => `\u2212${formatTokens(drop.tokens)}` },
2667
+ { header: "idle", width: 12, max: 24, priority: 1, cell: (drop) => `${formatGap(drop.gapMs)} idle \xB7 ${drop.reason}` }
2668
+ ], width);
2669
+ if (losses.length > 0) {
2670
+ lines.push({ text: formatTableRow(losses.map((column) => column.header), losses.map((column) => column.width), losses.map((column) => column.align)), tone: "muted" });
2671
+ for (const drop of [...data.drops].sort((left, right) => right.at - left.at).slice(0, 8)) {
2672
+ lines.push({
2673
+ text: formatTableRow(losses.map((column) => column.cell(drop)), losses.map((column) => column.width), losses.map((column) => column.align)),
2674
+ tone: drop.reason === "expired" ? "warning" : "base"
2675
+ });
2676
+ }
2602
2677
  }
2603
2678
  }
2604
2679
  return lines;
@@ -2619,110 +2694,162 @@ function periodLines(rows, width, granularity, limit = 40, estimates = true, tot
2619
2694
  }, { steps: 0, sessions: 0, input: 0, cached: 0, cacheWrite: 0, output: 0, reasoning: 0, cost: 0, listCost: 0 });
2620
2695
  const overall = usageBuckets(totals);
2621
2696
  lines.push({
2622
- text: `${granularityLabel(granularity)} \xB7 ${count(rows.length, "period")} \xB7 ${count(totals.steps, "step")} \xB7 hit ${formatPercent(overall.hit)}`,
2697
+ text: truncate(`${granularityLabel(granularity)} \xB7 ${count(rows.length, "period")} \xB7 ${count(totals.steps, "step")} \xB7 hit ${formatPercent(overall.hit)}`, width),
2623
2698
  tone: "muted"
2624
2699
  });
2625
2700
  if (rows.length > 1) {
2626
- lines.push({ text: `steps ${sparkline(rows.map((row) => row.usage.steps))}`, tone: "muted" });
2627
- lines.push({ text: `cached ${sparkline(rows.map((row) => row.usage.cached))}`, tone: "muted" });
2701
+ const bars = Math.max(4, width - 8);
2702
+ lines.push({ text: truncate(`steps ${sparkline(rows.map((row) => row.usage.steps), bars)}`, width), tone: "muted" });
2703
+ lines.push({ text: truncate(`cached ${sparkline(rows.map((row) => row.usage.cached), bars)}`, width), tone: "muted" });
2628
2704
  lines.push({
2629
- text: `cost ${sparkline(rows.map((row) => Math.round((estimates ? row.usage.cost + row.usage.listCost : row.usage.cost) * 1e4)))}`,
2705
+ text: truncate(`cost ${sparkline(rows.map((row) => Math.round((estimates ? row.usage.cost + row.usage.listCost : row.usage.cost) * 1e4)), bars)}`, width),
2630
2706
  tone: "muted"
2631
2707
  });
2632
2708
  }
2633
- const withSessions = tableWidths(width, [[12, 7, 7, 11, 10, 7, 9, 9]]).length === 8;
2634
- const widths = withSessions ? [12, 7, 7, 11, 10, 7, 9, 9] : [12, 7, 11, 10, 7, 9, 9];
2635
- const aligns = widths.map((_, index) => index === 0 ? "left" : "right");
2636
- const header = withSessions ? ["period", "steps", "sess", "prompt", "cached", "hit", "out", "cost"] : ["period", "steps", "prompt", "cached", "hit", "out", "cost"];
2637
- lines.push({ text: formatTableRow(header, widths, aligns), tone: HEADER_TONE });
2709
+ const columns = fitTable([
2710
+ { header: "period", width: 10, max: 24, cell: (row) => row.label },
2711
+ { header: "steps", width: 6, align: "right", cell: (row) => row.steps },
2712
+ { header: "sess", width: 6, align: "right", priority: 5, cell: (row) => row.sessions },
2713
+ { header: "prompt", width: 7, max: 10, align: "right", priority: 1, cell: (row) => row.prompt },
2714
+ { header: "cached", width: 7, max: 10, align: "right", priority: 2, cell: (row) => row.cached },
2715
+ { header: "hit", width: 6, max: 7, align: "right", priority: 3, cell: (row) => row.hit },
2716
+ { header: "out", width: 7, max: 9, align: "right", priority: 4, cell: (row) => row.output },
2717
+ { header: "cost", width: 8, max: 9, align: "right", cell: (row) => row.cost }
2718
+ ], width);
2719
+ if (columns.length === 0)
2720
+ return [...lines, { text: truncate("no room for a table at this width", width), tone: "muted" }];
2721
+ const header = formatTableRow(columns.map((column) => column.header), columns.map((column) => column.width), columns.map((column) => column.align));
2722
+ lines.push({ text: header, tone: HEADER_TONE });
2638
2723
  const newest = [...rows].sort((left, right) => right.at - left.at).slice(0, limit);
2639
2724
  for (const row of newest) {
2640
2725
  const buckets = usageBuckets(row.usage);
2641
- const cells = [
2642
- row.label,
2643
- formatCount(row.steps),
2644
- ...withSessions ? [formatCount(row.sessions)] : [],
2645
- formatTokens(buckets.prompt),
2646
- formatTokens(buckets.cached),
2647
- formatPercent(buckets.hit, 0),
2648
- formatTokens(buckets.output),
2649
- formatSpend(buckets.cost, estimates ? buckets.listCost : 0)
2650
- ];
2651
- lines.push({ text: formatTableRow(cells, widths, aligns), tone: "base", row: { kind: "period", value: row.key } });
2726
+ const view = {
2727
+ label: row.label,
2728
+ steps: formatCount(row.steps),
2729
+ sessions: formatCount(row.sessions),
2730
+ prompt: formatTokens(buckets.prompt),
2731
+ cached: formatTokens(buckets.cached),
2732
+ hit: formatPercent(buckets.hit, 0),
2733
+ output: formatTokens(buckets.output),
2734
+ cost: formatSpend(buckets.cost, estimates ? buckets.listCost : 0)
2735
+ };
2736
+ lines.push({
2737
+ text: formatTableRow(columns.map((column) => column.cell(view)), columns.map((column) => column.width), columns.map((column) => column.align)),
2738
+ tone: "base",
2739
+ row: { kind: "period", value: row.key }
2740
+ });
2652
2741
  }
2653
2742
  lines.push({ text: "", tone: "muted" });
2743
+ const total = {
2744
+ label: "total",
2745
+ steps: formatCount(totals.steps),
2746
+ sessions: formatCount(totalSessions ?? totals.sessions),
2747
+ prompt: formatTokens(overall.prompt),
2748
+ cached: formatTokens(overall.cached),
2749
+ hit: formatPercent(overall.hit, 0),
2750
+ output: formatTokens(overall.output),
2751
+ cost: formatSpend(overall.cost, estimates ? overall.listCost : 0)
2752
+ };
2654
2753
  lines.push({
2655
- text: formatTableRow([
2656
- "total",
2657
- formatCount(totals.steps),
2658
- ...withSessions ? [formatCount(totalSessions ?? totals.sessions)] : [],
2659
- formatTokens(overall.prompt),
2660
- formatTokens(overall.cached),
2661
- formatPercent(overall.hit, 0),
2662
- formatTokens(overall.output),
2663
- formatSpend(overall.cost, estimates ? overall.listCost : 0)
2664
- ], widths, aligns),
2754
+ text: formatTableRow(columns.map((column) => column.cell(total)), columns.map((column) => column.width), columns.map((column) => column.align)),
2665
2755
  tone: HEADER_TONE
2666
2756
  });
2667
2757
  if (rows.length === 0)
2668
- lines.push({ text: "no usage recorded for this range", tone: "muted" });
2758
+ lines.push({ text: truncate("no usage recorded for this range", width), tone: "muted" });
2669
2759
  return lines;
2670
2760
  }
2761
+ function latencyMetric(label, values, width) {
2762
+ const prefix = padCell(label, 11);
2763
+ let text = `${prefix}${values[0] ?? "\u2013"}`;
2764
+ for (let index = 1;index < values.length; index += 1) {
2765
+ const next = `${text} \xB7 ${values[index]}`;
2766
+ if (next.length > width)
2767
+ break;
2768
+ text = next;
2769
+ }
2770
+ return text;
2771
+ }
2671
2772
  function latencyLines(report, width, groups, limit = 12, estimates = true) {
2672
2773
  const lines = [];
2673
- lines.push({
2674
- text: formatTableRow(["wall", "p50", formatMs(report.wall.p50), "p90", formatMs(report.wall.p90), "p99", formatMs(report.wall.p99), `n ${formatCount(report.wall.count)}`], [10, 4, 9, 4, 9, 4, 9, 12]),
2675
- tone: "base"
2676
- });
2677
- lines.push({
2678
- text: formatTableRow(["stream", "p50", formatMs(report.stream.p50), "p90", formatMs(report.stream.p90), "p99", formatMs(report.stream.p99)], [10, 4, 9, 4, 9, 4, 9]),
2679
- tone: "base"
2680
- });
2681
- lines.push({
2682
- text: formatTableRow(["throughput", "p50", formatTps(report.tps.p50), "p90", formatTps(report.tps.p90), "p99", formatTps(report.tps.p99)], [10, 4, 9, 4, 9, 4, 9]),
2683
- tone: "base"
2684
- });
2774
+ if (width >= 68) {
2775
+ lines.push({
2776
+ text: formatTableRow(["wall", "p50", formatMs(report.wall.p50), "p90", formatMs(report.wall.p90), "p99", formatMs(report.wall.p99), `n ${formatCount(report.wall.count)}`], [10, 4, 9, 4, 9, 4, 9, 12]),
2777
+ tone: "base"
2778
+ });
2779
+ lines.push({
2780
+ text: formatTableRow(["stream", "p50", formatMs(report.stream.p50), "p90", formatMs(report.stream.p90), "p99", formatMs(report.stream.p99)], [10, 4, 9, 4, 9, 4, 9]),
2781
+ tone: "base"
2782
+ });
2783
+ lines.push({
2784
+ text: formatTableRow(["throughput", "p50", formatTps(report.tps.p50), "p90", formatTps(report.tps.p90), "p99", formatTps(report.tps.p99)], [10, 4, 9, 4, 9, 4, 9]),
2785
+ tone: "base"
2786
+ });
2787
+ } else {
2788
+ lines.push({
2789
+ text: latencyMetric("wall", [`p50 ${formatMs(report.wall.p50)}`, `p90 ${formatMs(report.wall.p90)}`, `p99 ${formatMs(report.wall.p99)}`, `n ${formatCount(report.wall.count)}`], width),
2790
+ tone: "base"
2791
+ });
2792
+ lines.push({
2793
+ text: latencyMetric("stream", [`p50 ${formatMs(report.stream.p50)}`, `p90 ${formatMs(report.stream.p90)}`, `p99 ${formatMs(report.stream.p99)}`], width),
2794
+ tone: "base"
2795
+ });
2796
+ lines.push({
2797
+ text: latencyMetric("throughput", [`p50 ${formatTps(report.tps.p50)}`, `p90 ${formatTps(report.tps.p90)}`, `p99 ${formatTps(report.tps.p99)}`], width),
2798
+ tone: "base"
2799
+ });
2800
+ }
2685
2801
  lines.push({ text: "", tone: "muted" });
2686
2802
  lines.push({
2687
2803
  text: truncate("wall = request to completed \xB7 stream = request to last chunk \xB7 throughput = output+reasoning tokens/s", width),
2688
2804
  tone: "muted"
2689
2805
  });
2690
2806
  if (report.wall.count === 0)
2691
- lines.push({ text: "no completed steps for this range", tone: "muted" });
2807
+ lines.push({ text: truncate("no completed steps for this range", width), tone: "muted" });
2692
2808
  if (groups !== undefined && groups.length > 0) {
2693
2809
  lines.push({ text: "", tone: "muted" });
2694
2810
  lines.push({ text: "by group", tone: HEADER_TONE });
2695
- const widths = width >= 110 ? [24, 6, 9, 9, 9, 9, 9, 9] : [16, 5, 8, 8, 8, 8, 8, 8];
2696
- const aligns = ["left", "right", "right", "right", "right", "right", "right", "right"];
2697
- lines.push({ text: formatTableRow(["group", "n", "wall p50", "wall p90", "stream", "tps p50", "tps p90", "cost"], widths, aligns), tone: "muted" });
2698
- for (const row of groups.slice(0, limit)) {
2699
- const thin = row.count < 5;
2700
- lines.push({
2701
- text: formatTableRow([
2702
- row.label,
2703
- formatCount(row.count),
2704
- formatMs(row.wall.p50),
2705
- formatMs(row.wall.p90),
2706
- formatMs(row.stream.p50),
2707
- formatTps(row.tps.p50),
2708
- formatTps(row.tps.p90),
2709
- formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0)
2710
- ], widths, aligns),
2711
- tone: thin ? "muted" : "base",
2712
- row: { kind: "model", value: row.key }
2713
- });
2811
+ const columns = fitTable([
2812
+ { header: "group", width: 8, max: 32, cell: (row) => row.label },
2813
+ { header: "n", width: 4, align: "right", cell: (row) => formatCount(row.count) },
2814
+ { header: "wall p50", width: 8, align: "right", cell: (row) => formatMs(row.wall.p50) },
2815
+ { header: "wall p90", width: 8, max: 9, align: "right", priority: 1, cell: (row) => formatMs(row.wall.p90) },
2816
+ { header: "stream", width: 6, max: 9, align: "right", priority: 2, cell: (row) => formatMs(row.stream.p50) },
2817
+ { header: "tps p50", width: 8, max: 9, align: "right", cell: (row) => formatTps(row.tps.p50) },
2818
+ { header: "tps p90", width: 8, max: 9, align: "right", priority: 3, cell: (row) => formatTps(row.tps.p90) },
2819
+ { header: "cost", width: 8, max: 9, align: "right", cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
2820
+ ], width);
2821
+ if (columns.length > 0) {
2822
+ lines.push({ text: formatTableRow(columns.map((column) => column.header), columns.map((column) => column.width), columns.map((column) => column.align)), tone: "muted" });
2823
+ for (const row of groups.slice(0, limit)) {
2824
+ const thin = row.count < 5;
2825
+ lines.push({
2826
+ text: formatTableRow(columns.map((column) => column.cell(row)), columns.map((column) => column.width), columns.map((column) => column.align)),
2827
+ tone: thin ? "muted" : "base",
2828
+ row: { kind: "model", value: row.key }
2829
+ });
2830
+ }
2714
2831
  }
2715
- lines.push({ text: "n < 5 is dimmed: too few samples to compare", tone: "muted" });
2832
+ lines.push({ text: truncate("n < 5 is dimmed: too few samples to compare", width), tone: "muted" });
2716
2833
  }
2717
2834
  return lines;
2718
2835
  }
2719
2836
  function sessionsLines(rows, width, options) {
2720
2837
  const limit = options?.limit ?? 20;
2721
2838
  const estimates = options?.estimates !== false;
2722
- const widths = [Math.max(18, Math.floor(width * 0.36)), 7, 11, 10, 7, 9, 9];
2723
- const aligns = ["left", "right", "right", "right", "right", "right", "right"];
2839
+ const columns = fitTable([
2840
+ { header: "title", width: 10, max: 48, cell: (row) => row.title },
2841
+ { header: "steps", width: 6, align: "right", cell: (row) => row.steps },
2842
+ { header: "prompt", width: 7, max: 10, align: "right", priority: 1, cell: (row) => row.prompt },
2843
+ { header: "cached", width: 7, max: 10, align: "right", priority: 2, cell: (row) => row.cached },
2844
+ { header: "hit", width: 6, max: 7, align: "right", priority: 3, cell: (row) => row.hit },
2845
+ { header: "out", width: 7, max: 9, align: "right", priority: 4, cell: (row) => row.output },
2846
+ { header: "cost", width: 8, max: 9, align: "right", cell: (row) => row.cost }
2847
+ ], width);
2724
2848
  const lines = [
2725
- { text: formatTableRow(["title", "steps", "prompt", "cached", "hit", "out", "cost"], widths, aligns), tone: HEADER_TONE }
2849
+ {
2850
+ text: formatTableRow(columns.map((column) => column.header), columns.map((column) => column.width), columns.map((column) => column.align)),
2851
+ tone: HEADER_TONE
2852
+ }
2726
2853
  ];
2727
2854
  const byID = new Map(rows.map((row) => [row.sessionID, row]));
2728
2855
  const ordered = rows.filter((row) => row.parentID === undefined || !byID.has(row.parentID));
@@ -2730,18 +2857,18 @@ function sessionsLines(rows, width, options) {
2730
2857
  const orderedAll = [...ordered, ...children];
2731
2858
  for (const row of orderedAll.slice(0, limit)) {
2732
2859
  const child = row.parentID !== undefined && byID.has(row.parentID);
2733
- const title = truncate(row.title ?? row.sessionID, Math.max(8, widths[0] - (child ? 2 : 0)));
2734
2860
  const buckets = usageBuckets(row);
2861
+ const view = {
2862
+ title: `${child ? "\u21B3 " : ""}${row.title ?? row.sessionID}`,
2863
+ steps: formatCount(buckets.steps),
2864
+ prompt: formatTokens(buckets.prompt),
2865
+ cached: formatTokens(buckets.cached),
2866
+ hit: formatPercent(buckets.hit, 0),
2867
+ output: formatTokens(buckets.output),
2868
+ cost: formatSpend(buckets.cost, estimates ? buckets.listCost : 0)
2869
+ };
2735
2870
  lines.push({
2736
- text: formatTableRow([
2737
- `${child ? "\u21B3 " : ""}${title}`,
2738
- formatCount(buckets.steps),
2739
- formatTokens(buckets.prompt),
2740
- formatTokens(buckets.cached),
2741
- formatPercent(buckets.hit, 0),
2742
- formatTokens(buckets.output),
2743
- formatSpend(buckets.cost, estimates ? buckets.listCost : 0)
2744
- ], widths, aligns),
2871
+ text: formatTableRow(columns.map((column) => column.cell(view)), columns.map((column) => column.width), columns.map((column) => column.align)),
2745
2872
  tone: child ? "muted" : "base",
2746
2873
  row: { kind: "session", value: row.sessionID }
2747
2874
  });
@@ -2756,14 +2883,14 @@ function sessionsLines(rows, width, options) {
2756
2883
  const share = rows.reduce((sum, row) => sum + row.steps, 0);
2757
2884
  lines.push({ text: "", tone: "muted" });
2758
2885
  lines.push({
2759
- text: `subagents ${formatCount(options.rollup.subagentSteps)} steps (${formatPercent(share > 0 ? options.rollup.subagentSteps / share : undefined)}) \xB7 ${formatSpend(options.rollup.subagentCost, estimates ? options.rollup.subagentListCost ?? 0 : 0)}`,
2886
+ text: truncate(`subagents ${formatCount(options.rollup.subagentSteps)} steps (${formatPercent(share > 0 ? options.rollup.subagentSteps / share : undefined)}) \xB7 ${formatSpend(options.rollup.subagentCost, estimates ? options.rollup.subagentListCost ?? 0 : 0)}`, width),
2760
2887
  tone: "muted"
2761
2888
  });
2762
2889
  }
2763
2890
  if (rows.length > limit)
2764
- lines.push({ text: `\u2026 ${count(rows.length - limit, "more session")} \xB7 narrow the range`, tone: "muted" });
2891
+ lines.push({ text: truncate(`\u2026 ${count(rows.length - limit, "more session")} \xB7 narrow the range`, width), tone: "muted" });
2765
2892
  if (rows.length === 0)
2766
- lines.push({ text: "no sessions recorded for this range", tone: "muted" });
2893
+ lines.push({ text: truncate("no sessions recorded for this range", width), tone: "muted" });
2767
2894
  return lines;
2768
2895
  }
2769
2896
  function dbLines(data, width) {
@@ -2771,16 +2898,16 @@ function dbLines(data, width) {
2771
2898
  const size = (bytes) => `${(bytes / 1048576).toFixed(1)} MB`;
2772
2899
  lines.push({ text: truncate(data.info.path, width), tone: "base" });
2773
2900
  lines.push({
2774
- text: `${size(data.info.bytes)}${data.info.walBytes === undefined ? "" : ` + ${size(data.info.walBytes)} wal`} \xB7 schema v${data.info.schemaVersion}`,
2901
+ text: truncate(`${size(data.info.bytes)}${data.info.walBytes === undefined ? "" : ` + ${size(data.info.walBytes)} wal`} \xB7 schema v${data.info.schemaVersion}`, width),
2775
2902
  tone: "muted"
2776
2903
  });
2777
2904
  lines.push({ text: "", tone: "muted" });
2778
2905
  lines.push({ text: "what it holds", tone: HEADER_TONE });
2779
2906
  for (const table of data.info.tables) {
2780
- lines.push({ text: ` ${padCell(table.name, 12)}${formatCount(table.rows)} rows`, tone: table.rows === 0 ? "muted" : "base" });
2907
+ lines.push({ text: truncate(` ${padCell(table.name, 12)}${formatCount(table.rows)} rows`, width), tone: table.rows === 0 ? "muted" : "base" });
2781
2908
  }
2782
2909
  lines.push({
2783
- text: ` window ${data.info.firstAt === undefined ? "\u2013" : new Date(data.info.firstAt).toLocaleDateString("en-GB")} \u2192 ${data.info.lastAt === undefined ? "\u2013" : new Date(data.info.lastAt).toLocaleDateString("en-GB")}`,
2910
+ text: truncate(` window ${data.info.firstAt === undefined ? "\u2013" : new Date(data.info.firstAt).toLocaleDateString("en-GB")} \u2192 ${data.info.lastAt === undefined ? "\u2013" : new Date(data.info.lastAt).toLocaleDateString("en-GB")}`, width),
2784
2911
  tone: "muted"
2785
2912
  });
2786
2913
  lines.push({ text: "", tone: "muted" });
@@ -2796,7 +2923,7 @@ function dbLines(data, width) {
2796
2923
  tone: data.info.schemaVersion < 10 || data.info.unpriced > 0 ? "base" : "muted"
2797
2924
  });
2798
2925
  }
2799
- const widths = [10, 13, 10];
2926
+ const widths = [9, 11, 10];
2800
2927
  lines.push({ text: formatTableRow(["keep", "prunes", "older than"], widths, ["left", "right", "left"]), tone: "muted" });
2801
2928
  for (const option of data.info.removable) {
2802
2929
  const before = new Date(data.now - option.days * 24 * 60 * 60 * 1000).toLocaleDateString("en-GB");
@@ -2816,7 +2943,7 @@ function dbLines(data, width) {
2816
2943
  });
2817
2944
  if (data.busy === true) {
2818
2945
  lines.push({ text: "", tone: "muted" });
2819
- lines.push({ text: "the recorder is writing: try the vacuum again in a moment", tone: "warning" });
2946
+ lines.push({ text: truncate("the recorder is writing: try the vacuum again in a moment", width), tone: "warning" });
2820
2947
  }
2821
2948
  return lines;
2822
2949
  }
@@ -2840,28 +2967,74 @@ function turnStats(turn) {
2840
2967
  steps: turn.steps
2841
2968
  };
2842
2969
  }
2970
+ function compactTurnLine(stats, width, options) {
2971
+ const labels = { ...DEFAULT_LABELS, ...options?.labels };
2972
+ const fields = { ...DEFAULT_FIELDS, ...options?.fields };
2973
+ const estimates = options?.estimates !== false;
2974
+ const headParts = [];
2975
+ if (fields.clock && stats.endAt !== undefined)
2976
+ headParts.push(formatClock(stats.endAt));
2977
+ if (stats.providerID !== undefined) {
2978
+ let identity = stats.providerID;
2979
+ if (fields.model && stats.modelID !== undefined)
2980
+ identity += `/${stats.modelID}`;
2981
+ if (fields.variant && stats.variant)
2982
+ identity += ` ${stats.variant}`;
2983
+ headParts.push(identity);
2984
+ }
2985
+ let head = headParts.join(" ");
2986
+ const totals = options?.turnTotals === "sum" ? stats.totals : stats.latest;
2987
+ const display = displayTotals(totals);
2988
+ const money = fields.cost ? formatMoney(stats.totals.cost, estimates ? stats.totals.listCost : 0, labels.estimate) : undefined;
2989
+ const cost = money === undefined ? undefined : `${labels.cost} ${money}`.trim();
2990
+ const warning = fields.cacheExpiry && stats.cacheExpired ? `\u26A0 ${labels.cacheExpired} (\u2212${formatTokens(stats.cacheDropTokens)}${stats.drops.length > 1 ? ` \xD7${stats.drops.length}` : ""})` : undefined;
2991
+ let suffix = [warning, cost].filter((part) => part !== undefined).join(" \xB7 ");
2992
+ if (suffix !== "" && suffix.length + 3 > width)
2993
+ suffix = cost ?? suffix;
2994
+ head = truncate(head, Math.max(0, width - (suffix === "" ? 0 : suffix.length + 3)));
2995
+ if (!fields.turn || stats.totals.steps === 0)
2996
+ return [head, suffix].filter((part) => part !== "").join(" \xB7 ");
2997
+ const input = display.prompt === display.input ? `${labels.input} ${formatTokens(display.input)}` : `${labels.input} ${formatTokens(display.input)} (${formatTokens(display.prompt)})`;
2998
+ const volumes = [
2999
+ input,
3000
+ `${labels.cached} ${formatTokens(display.cached)}`,
3001
+ `${labels.output} ${formatTokens(display.output)}`,
3002
+ ...fields.reasoning ? [`${labels.reasoning} ${formatTokens(display.reasoning)}`] : []
3003
+ ];
3004
+ let numbers = "";
3005
+ for (const volume of volumes) {
3006
+ const next = numbers === "" ? volume : `${numbers}, ${volume}`;
3007
+ if ([head, next, suffix].filter((part) => part !== "").join(" \xB7 ").length > width)
3008
+ break;
3009
+ numbers = next;
3010
+ }
3011
+ return [head, numbers, suffix].filter((part) => part !== "").join(" \xB7 ");
3012
+ }
2843
3013
  function turnsLines(rows, width, options) {
2844
3014
  const lines = [];
2845
3015
  const warning = rows.filter((turn) => turn.drops.length > 0).length;
2846
3016
  lines.push({
2847
- text: `${count(rows.length, "turn")} \xB7 newest first${warning > 0 ? ` \xB7 ${count(warning, "turn")} with a cache loss` : ""}`,
3017
+ text: truncate(`${count(rows.length, "turn")} \xB7 newest first${warning > 0 ? ` \xB7 ${count(warning, "turn")} with a cache loss` : ""}`, width),
2848
3018
  tone: "muted"
2849
3019
  });
2850
3020
  const showSession = options?.sessionTitles !== undefined;
2851
3021
  for (const turn of rows) {
2852
- const line = formatLine(turnStats(turn), {
3022
+ const stats = turnStats(turn);
3023
+ const line = formatLine(stats, {
2853
3024
  ...options,
2854
3025
  fields: { ...options?.fields, session: false }
2855
3026
  });
2856
3027
  const title = showSession ? `${truncate(options.sessionTitles.get(turn.sessionID) ?? turn.sessionID, 18)} ` : "";
3028
+ const available = Math.max(0, width - title.length);
3029
+ const text = `${title}${line.length <= available ? line : compactTurnLine(stats, available, { ...options, fields: { ...options?.fields, session: false } })}`;
2857
3030
  lines.push({
2858
- text: truncate(`${title}${line}`, width),
3031
+ text: truncate(text, width),
2859
3032
  tone: turn.drops.length > 0 || turn.providerChanged ? "warning" : "base",
2860
3033
  row: { kind: "turn", value: turn.sessionID }
2861
3034
  });
2862
3035
  }
2863
3036
  if (rows.length === 0)
2864
- lines.push({ text: "no finished turns for this range", tone: "muted" });
3037
+ lines.push({ text: truncate("no finished turns for this range", width), tone: "muted" });
2865
3038
  return lines;
2866
3039
  }
2867
3040
  function sessionSummaryLines(usage, budget = 34, estimates = true) {
@@ -2961,11 +3134,12 @@ function detailLabel(row) {
2961
3134
  }
2962
3135
  }
2963
3136
  function buildPanel(request) {
3137
+ const width = clampPanelWidth(request.width);
2964
3138
  try {
2965
- return build(request);
3139
+ return build({ ...request, width }).map((line) => line.text.length <= width ? line : { ...line, text: truncate(line.text, width) });
2966
3140
  } catch (error) {
2967
3141
  const message = error instanceof Error ? error.message : String(error);
2968
- return [{ text: `stats query failed: ${message}`, tone: "warning" }];
3142
+ return [{ text: truncate(`stats query failed: ${message}`, width), tone: "warning" }];
2969
3143
  }
2970
3144
  }
2971
3145
  function build(request) {
@@ -3079,10 +3253,15 @@ function buildDetail(request, range, filters, estimates) {
3079
3253
  const steps = stepRows(db, { range, filters: scoped });
3080
3254
  return [
3081
3255
  head,
3082
- {
3083
- text: `steps ${stats.steps} \xB7 in ${formatTokens(stats.prompt - stats.cached)} \xB7 prompt ${formatTokens(stats.prompt)} \xB7 cached ${formatTokens(stats.cached)} \xB7 write ${formatTokens(stats.cacheWrite)} \xB7 hit ${percent(stats.hit)} \xB7 p50 ${percent(stats.hitP50)}`,
3084
- tone: "muted"
3085
- },
3256
+ ...kvGrid([
3257
+ ["steps", formatCount(stats.steps)],
3258
+ ["in", formatTokens(stats.prompt - stats.cached)],
3259
+ ["prompt", formatTokens(stats.prompt)],
3260
+ ["cached", formatTokens(stats.cached)],
3261
+ ["write", formatTokens(stats.cacheWrite)],
3262
+ ["hit", percent(stats.hit)],
3263
+ ["p50", percent(stats.hitP50)]
3264
+ ], width),
3086
3265
  { text: "", tone: "muted" },
3087
3266
  ...providerLines(providerBreakdownOf(steps), width, 40, estimates),
3088
3267
  { text: "", tone: "muted" },
@@ -3101,10 +3280,14 @@ function buildDetail(request, range, filters, estimates) {
3101
3280
  const stats = cacheStatsOf(db, { range, filters: scoped });
3102
3281
  return [
3103
3282
  head,
3104
- {
3105
- text: `steps ${stats.steps} \xB7 in ${formatTokens(stats.prompt - stats.cached)} \xB7 prompt ${formatTokens(stats.prompt)} \xB7 cached ${formatTokens(stats.cached)} \xB7 hit ${percent(stats.hit)} \xB7 p50 ${percent(stats.hitP50)}`,
3106
- tone: "muted"
3107
- },
3283
+ ...kvGrid([
3284
+ ["steps", formatCount(stats.steps)],
3285
+ ["in", formatTokens(stats.prompt - stats.cached)],
3286
+ ["prompt", formatTokens(stats.prompt)],
3287
+ ["cached", formatTokens(stats.cached)],
3288
+ ["hit", percent(stats.hit)],
3289
+ ["p50", percent(stats.hitP50)]
3290
+ ], width),
3108
3291
  { text: "", tone: "muted" },
3109
3292
  ...providerLines(providerBreakdownOf(steps), width, 40, estimates),
3110
3293
  { text: "", tone: "muted" },
@@ -3542,7 +3725,7 @@ function createService(db, options) {
3542
3725
  }
3543
3726
  function turnHistory(input) {
3544
3727
  const handle = db();
3545
- const width = clamp2(input.width, 20, 120);
3728
+ const width = clamp2(input.width, 20, MAX_HISTORY_WIDTH);
3546
3729
  const limit = clampTurnsLimit(input.limit);
3547
3730
  const summary = sessionSummaryLines(overview(handle, undefined, { sessionID: input.sessionID }), width, options.pricing);
3548
3731
  const history = sidebarLines(turns(handle, { sessionID: input.sessionID, limit }), {
@@ -3694,4 +3877,4 @@ scope.onmessage = (event) => {
3694
3877
  }
3695
3878
  };
3696
3879
 
3697
- //# debugId=008189B5EA042DE564756E2164756E21
3880
+ //# debugId=0FF21E19CAE9E2DE64756E2164756E21