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.
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ import { Rpc } from "@opencode/plugin/rpc";
7
7
 
8
8
  // src/limits.ts
9
9
  var MAX_PANEL_WIDTH = 200;
10
+ var MAX_HISTORY_WIDTH = 120;
10
11
  var MAX_TURNS = 50;
11
12
  function clampPanelWidth(value) {
12
13
  if (!Number.isFinite(value))
@@ -183,7 +184,7 @@ var UsageInsights = Rpc.define({
183
184
  properties: {
184
185
  sessionID: { type: "string", maxLength: 200 },
185
186
  limit: { type: "number", minimum: 1, maximum: MAX_TURNS },
186
- width: { type: "number", minimum: 20, maximum: 120 }
187
+ width: { type: "number", minimum: 20, maximum: MAX_HISTORY_WIDTH }
187
188
  },
188
189
  required: ["sessionID", "limit", "width"],
189
190
  additionalProperties: false
@@ -2098,7 +2099,10 @@ var GAP_BUCKETS = [
2098
2099
  ["<1m", 0, 60000],
2099
2100
  ["1\u20135m", 60000, 5 * 60000],
2100
2101
  ["5\u201315m", 5 * 60000, 15 * 60000],
2101
- ["15\u201360m", 15 * 60000, 60 * 60000],
2102
+ ["15\u201320m", 15 * 60000, 20 * 60000],
2103
+ ["20\u201330m", 20 * 60000, 30 * 60000],
2104
+ ["30\u201345m", 30 * 60000, 45 * 60000],
2105
+ ["45\u201360m", 45 * 60000, 60 * 60000],
2102
2106
  ["1\u20136h", 60 * 60000, 6 * 60 * 60000],
2103
2107
  [">6h", 6 * 60 * 60000, undefined]
2104
2108
  ];
@@ -2609,15 +2613,6 @@ function truncateMiddle(value, width) {
2609
2613
  const tail = width - 1 - head;
2610
2614
  return `${clean.slice(0, head)}\u2026${clean.slice(clean.length - tail)}`;
2611
2615
  }
2612
- function tableWidths(width, layouts) {
2613
- for (const layout of layouts) {
2614
- const total = layout.reduce((sum, column) => sum + column, 0) + layout.length - 1;
2615
- if (total <= width)
2616
- return [...layout];
2617
- }
2618
- const last = layouts[layouts.length - 1];
2619
- return last === undefined ? [] : [...last];
2620
- }
2621
2616
  function modelCell(modelID, variant, width) {
2622
2617
  return truncateMiddle(variant === undefined ? modelID : `${modelID} ${variant}`, width);
2623
2618
  }
@@ -2636,6 +2631,47 @@ function formatTableRow(cells, widths, aligns) {
2636
2631
  }
2637
2632
  return parts.join(" ");
2638
2633
  }
2634
+ function fitTableColumns(columns, width) {
2635
+ const chosen = new Set;
2636
+ for (let index = 0;index < columns.length; index += 1) {
2637
+ if (columns[index].priority === undefined)
2638
+ chosen.add(index);
2639
+ }
2640
+ if (chosen.size === 0)
2641
+ return [];
2642
+ const total = () => {
2643
+ let sum = chosen.size - 1;
2644
+ for (const index of chosen)
2645
+ sum += columns[index].width;
2646
+ return sum;
2647
+ };
2648
+ 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);
2649
+ for (const { column, index } of optional) {
2650
+ if (total() + column.width + 1 + (column.reserve ?? 0) <= width)
2651
+ chosen.add(index);
2652
+ }
2653
+ while (chosen.size > 1 && total() > width)
2654
+ chosen.delete(Math.max(...chosen));
2655
+ const widths = new Map;
2656
+ let room = width - total();
2657
+ for (let index = 0;index < columns.length && room > 0; index += 1) {
2658
+ if (!chosen.has(index))
2659
+ continue;
2660
+ const column = columns[index];
2661
+ const grow = Math.min(room, (column.max ?? column.width) - column.width);
2662
+ if (grow <= 0)
2663
+ continue;
2664
+ widths.set(index, column.width + grow);
2665
+ room -= grow;
2666
+ }
2667
+ return [...chosen].sort((left, right) => left - right).map((index) => ({ index, width: widths.get(index) ?? columns[index].width }));
2668
+ }
2669
+ function fitTable(columns, width) {
2670
+ return fitTableColumns(columns, width).map(({ index, width: fitted }) => {
2671
+ const column = columns[index];
2672
+ return { index, header: column.header, width: fitted, align: column.align ?? "left", cell: column.cell };
2673
+ });
2674
+ }
2639
2675
  function count(n, singular, plural = `${singular}s`) {
2640
2676
  return `${formatCount(n)} ${n === 1 ? singular : plural}`;
2641
2677
  }
@@ -2643,21 +2679,30 @@ function kvGrid(pairs, width) {
2643
2679
  const lines = [];
2644
2680
  const label = 11;
2645
2681
  const column = Math.floor(width / 2);
2682
+ const pairLines = (pair) => {
2683
+ const text = `${padCell(pair[0], label)}${pair[1]}`;
2684
+ if (text.length <= width)
2685
+ return [text];
2686
+ return [padCell(pair[0], label), ` ${pair[1]}`].map((line) => truncate(line, width));
2687
+ };
2646
2688
  for (let index = 0;index < pairs.length; index += 2) {
2647
2689
  const left = pairs[index];
2648
2690
  const right = pairs[index + 1];
2649
- const leftText = `${padCell(left[0], label)}${left[1]}`;
2650
2691
  if (right === undefined) {
2651
- lines.push({ text: leftText, tone: "base" });
2692
+ for (const text of pairLines(left))
2693
+ lines.push({ text, tone: "base" });
2652
2694
  continue;
2653
2695
  }
2696
+ const leftText = `${padCell(left[0], label)}${left[1]}`;
2654
2697
  const rightText = `${padCell(right[0], label)}${right[1]}`;
2655
2698
  const gap = Math.max(2, column - leftText.length);
2656
2699
  if (leftText.length + gap + rightText.length <= width) {
2657
2700
  lines.push({ text: `${leftText}${" ".repeat(gap)}${rightText}`, tone: "base" });
2658
2701
  } else {
2659
- lines.push({ text: leftText, tone: "base" });
2660
- lines.push({ text: rightText, tone: "base" });
2702
+ for (const text of pairLines(left))
2703
+ lines.push({ text, tone: "base" });
2704
+ for (const text of pairLines(right))
2705
+ lines.push({ text, tone: "base" });
2661
2706
  }
2662
2707
  }
2663
2708
  return lines;
@@ -2908,14 +2953,16 @@ function overviewLines(data, width) {
2908
2953
  const lines = [];
2909
2954
  const estimates = data.estimates !== false;
2910
2955
  lines.push({
2911
- text: `${rangeLabel(data.range)} \xB7 ${count(data.usage.steps, "step")} \xB7 ${count(data.usage.sessions, "session")} \xB7 ${count(data.usage.prompts, "prompt")}`,
2956
+ text: truncate(`${rangeLabel(data.range)} \xB7 ${count(data.usage.steps, "step")} \xB7 ${count(data.usage.sessions, "session")} \xB7 ${count(data.usage.prompts, "prompt")}`, width),
2912
2957
  tone: "muted"
2913
2958
  });
2914
2959
  lines.push(...kvGrid(buildOverviewPairs(data.usage, "all", estimates), width));
2915
2960
  if (data.activity !== undefined && data.activity.length > 1) {
2916
- const series = reduceSeries(data.activity);
2961
+ const suffix = ` ${count(data.activity.length, "bucket")}`;
2962
+ const bars = Math.max(4, width - "activity ".length - suffix.length);
2963
+ const series = reduceSeries(data.activity, bars);
2917
2964
  lines.push({ text: "", tone: "muted" });
2918
- lines.push({ text: `activity ${sparkline(series)} ${count(series.length, "bucket")}`, tone: "muted" });
2965
+ lines.push({ text: truncate(`activity ${sparkline(series)}${suffix}`, width), tone: "muted" });
2919
2966
  }
2920
2967
  if (data.session !== undefined) {
2921
2968
  lines.push({ text: "", tone: "muted" });
@@ -2926,7 +2973,7 @@ function overviewLines(data, width) {
2926
2973
  const cost = data.subagents.reduce((sum, row) => sum + row.cost, 0);
2927
2974
  const listCost = data.subagents.reduce((sum, row) => sum + row.listCost, 0);
2928
2975
  lines.push({
2929
- text: ` subagents ${count(data.subagents.length, "session")} \xB7 ${count(steps, "step")} \xB7 ${formatSpend(cost, estimates ? listCost : 0)} (their own sessions)`,
2976
+ text: truncate(` subagents ${count(data.subagents.length, "session")} \xB7 ${count(steps, "step")} \xB7 ${formatSpend(cost, estimates ? listCost : 0)} (their own sessions)`, width),
2930
2977
  tone: "muted"
2931
2978
  });
2932
2979
  }
@@ -2958,65 +3005,33 @@ function coveragePairs(coverage) {
2958
3005
  ]
2959
3006
  ];
2960
3007
  }
2961
- function modelColumns(width) {
2962
- const layouts = [
2963
- { model: 30, steps: 6, prompt: 10, cached: 10, hit: 7, out: 9, cost: 9, spread: 24 },
2964
- { model: 26, steps: 6, prompt: 9, cached: 9, hit: 7, out: 8, cost: 8, spread: 20 },
2965
- { model: 20, steps: 6, prompt: 9, cached: 9, hit: 6, out: 8, cost: 8, spread: 0 },
2966
- { model: 18, steps: 5, prompt: 8, cached: 8, hit: 0, out: 7, cost: 8, spread: 0 },
2967
- { model: 14, steps: 5, prompt: 8, cached: 8, hit: 0, out: 7, cost: 0, spread: 0 }
2968
- ];
2969
- for (const layout of layouts) {
2970
- const total = Object.values(layout).reduce((sum, column) => sum + column, 0) + Object.values(layout).filter((column) => column > 0).length - 1;
2971
- if (total <= width)
2972
- return layout;
2973
- }
2974
- return layouts[layouts.length - 1];
2975
- }
2976
3008
  function modelsLines(rows, width, extras, limit = 40, estimates = true) {
2977
- const columns = modelColumns(width);
2978
- const widths = [columns.model, columns.steps, columns.prompt, columns.cached];
2979
- const aligns = ["left", "right", "right", "right"];
2980
- const header = ["model", "steps", "prompt", "cached"];
2981
- if (columns.hit > 0) {
2982
- widths.push(columns.hit);
2983
- aligns.push("right");
2984
- header.push("hit");
2985
- }
2986
- widths.push(columns.out);
2987
- aligns.push("right");
2988
- header.push("out");
2989
- if (columns.cost > 0) {
2990
- widths.push(columns.cost);
2991
- aligns.push("right");
2992
- header.push("cost");
2993
- }
2994
- if (columns.spread > 0) {
2995
- widths.push(columns.spread);
2996
- aligns.push("left");
2997
- header.push("providers");
2998
- }
2999
- const lines = [{ text: formatTableRow(header, widths, aligns), tone: HEADER_TONE }];
3009
+ let modelWidth = 14;
3010
+ const columns = fitTable([
3011
+ { header: "model", width: 14, max: 36, cell: (row) => modelCell(row.modelID, row.variant, modelWidth) },
3012
+ { header: "steps", width: 6, align: "right", cell: (row) => row.steps },
3013
+ { header: "prompt", width: 7, max: 10, align: "right", cell: (row) => row.prompt },
3014
+ { header: "cached", width: 7, max: 10, align: "right", cell: (row) => row.cached },
3015
+ { header: "hit", width: 6, max: 7, priority: 2, reserve: 10, align: "right", cell: (row) => row.hit },
3016
+ { header: "out", width: 7, max: 9, priority: 3, reserve: 10, align: "right", cell: (row) => row.output },
3017
+ { header: "cost", width: 8, max: 9, priority: 1, align: "right", cell: (row) => row.cost },
3018
+ { header: "providers", width: 18, max: 24, priority: 4, reserve: 22, cell: (row) => row.providers }
3019
+ ], width);
3020
+ modelWidth = columns.find((column) => column.index === 0)?.width ?? 0;
3021
+ if (columns.length === 0)
3022
+ return [{ text: truncate("no room for a table at this width", width), tone: "muted" }];
3023
+ const lines = [
3024
+ {
3025
+ text: formatTableRow(columns.map((column) => column.header), columns.map((column) => column.width), columns.map((column) => column.align)),
3026
+ tone: HEADER_TONE
3027
+ }
3028
+ ];
3000
3029
  for (const model of rows.slice(0, limit)) {
3001
3030
  const key = modelKey(model.modelID, model.variant);
3002
- const name = modelCell(model.modelID, model.variant, columns.model);
3003
3031
  const buckets = usageBuckets(model.totals);
3004
3032
  const providers = unique(model.providers.map((provider) => provider.providerID));
3005
3033
  const cache = extras?.cache?.get(key);
3006
3034
  const switches = extras?.switches?.get(key);
3007
- const cells = [name, formatCount(buckets.steps), formatTokens(buckets.prompt), formatTokens(buckets.cached)];
3008
- if (columns.hit > 0)
3009
- cells.push(formatPercent(cache?.hit ?? buckets.hit));
3010
- cells.push(formatTokens(buckets.output));
3011
- if (columns.cost > 0)
3012
- cells.push(formatSpend(buckets.cost, estimates ? buckets.listCost : 0));
3013
- if (columns.spread > 0)
3014
- cells.push(providers.join(", "));
3015
- lines.push({
3016
- text: formatTableRow(cells, widths, aligns),
3017
- tone: "base",
3018
- row: { kind: "model", value: model.modelID, ...model.variant !== undefined ? { variant: model.variant } : {} }
3019
- });
3020
3035
  const details = [];
3021
3036
  if (cache !== undefined)
3022
3037
  details.push(`p50 hit ${formatPercent(cache.hitP50)}`);
@@ -3026,62 +3041,76 @@ function modelsLines(rows, width, extras, limit = 40, estimates = true) {
3026
3041
  if (switches.cacheLost > 0)
3027
3042
  details.push(`${formatTokens(switches.cacheLost)} lost on a switch`);
3028
3043
  }
3029
- if (details.length > 0) {
3030
- lines.push({ text: truncate(`${" ".repeat(columns.model + 1)}${details.join(" \xB7 ")}`, width), tone: "muted" });
3044
+ const view = {
3045
+ modelID: model.modelID,
3046
+ variant: model.variant,
3047
+ steps: formatCount(buckets.steps),
3048
+ prompt: formatTokens(buckets.prompt),
3049
+ cached: formatTokens(buckets.cached),
3050
+ hit: formatPercent(cache?.hit ?? buckets.hit),
3051
+ output: formatTokens(buckets.output),
3052
+ cost: formatSpend(buckets.cost, estimates ? buckets.listCost : 0),
3053
+ providers: providers.join(", "),
3054
+ detail: details.join(" \xB7 ")
3055
+ };
3056
+ lines.push({
3057
+ text: formatTableRow(columns.map((column) => column.cell(view)), columns.map((column) => column.width), columns.map((column) => column.align)),
3058
+ tone: "base",
3059
+ row: { kind: "model", value: model.modelID, ...model.variant !== undefined ? { variant: model.variant } : {} }
3060
+ });
3061
+ if (view.detail !== "") {
3062
+ lines.push({ text: truncate(`${" ".repeat(modelWidth + 1)}${view.detail}`, width), tone: "muted" });
3031
3063
  }
3032
3064
  }
3033
3065
  if (rows.length > limit)
3034
- lines.push({ text: `\u2026 ${count(rows.length - limit, "more model")} \xB7 narrow the range or filter`, tone: "muted" });
3066
+ lines.push({ text: truncate(`\u2026 ${count(rows.length - limit, "more model")} \xB7 narrow the range or filter`, width), tone: "muted" });
3035
3067
  if (rows.length === 0)
3036
- lines.push({ text: "no usage recorded for this range", tone: "muted" });
3068
+ lines.push({ text: truncate("no usage recorded for this range", width), tone: "muted" });
3037
3069
  return lines;
3038
3070
  }
3039
3071
  function unique(values) {
3040
3072
  return [...new Set(values)];
3041
3073
  }
3042
- var tableWidth = (columns) => columns.reduce((sum, column) => sum + column.width, 0) + columns.length - 1;
3043
- function fitColumns(columns, width) {
3044
- const chosen = [...columns];
3045
- while (chosen.length > 1 && tableWidth(chosen) > width) {
3046
- const optional = [...chosen].reverse().findIndex((column) => column.optional === true);
3047
- if (optional === -1)
3048
- break;
3049
- chosen.splice(chosen.length - 1 - optional, 1);
3050
- }
3051
- return chosen;
3052
- }
3053
3074
  function renderTable(rows, columns, width, options) {
3054
- const chosen = fitColumns(columns, width);
3075
+ const chosen = fitTableColumns(columns, width).map(({ index, width: fitted }) => {
3076
+ const column = columns[index];
3077
+ const align = column.align ?? "left";
3078
+ return { ...column, width: fitted, align };
3079
+ });
3080
+ if (chosen.length === 0)
3081
+ return [{ text: truncate(options.empty, width), tone: "muted" }];
3055
3082
  const widths = chosen.map((column) => column.width);
3056
- const aligns = chosen.map((column) => column.align ?? "left");
3057
- const alignsTyped = aligns;
3058
- const lines = [{ text: formatTableRow(chosen.map((column) => column.header), widths, alignsTyped), tone: HEADER_TONE }];
3083
+ const aligns = chosen.map((column) => column.align);
3084
+ const lines = [
3085
+ { text: formatTableRow(chosen.map((column) => column.header), widths, aligns), tone: HEADER_TONE }
3086
+ ];
3059
3087
  if (rows.length === 0)
3060
- lines.push({ text: options.empty, tone: "muted" });
3088
+ lines.push({ text: truncate(options.empty, width), tone: "muted" });
3061
3089
  for (const row of rows.slice(0, options.limit)) {
3062
3090
  const identity = options.row?.(row);
3063
3091
  lines.push({
3064
- text: formatTableRow(chosen.map((column) => column.cell(row)), widths, alignsTyped),
3092
+ text: formatTableRow(chosen.map((column) => column.cell(row, column.width)), widths, aligns),
3065
3093
  tone: options.tone?.(row) ?? "base",
3066
3094
  ...identity === undefined ? {} : { row: identity }
3067
3095
  });
3068
3096
  }
3069
- if (rows.length > options.limit)
3070
- lines.push({ text: `\u2026 ${count(rows.length - options.limit, "more row")}`, tone: "muted" });
3097
+ if (rows.length > options.limit) {
3098
+ lines.push({ text: truncate(`\u2026 ${count(rows.length - options.limit, "more row")}`, width), tone: "muted" });
3099
+ }
3071
3100
  return lines;
3072
3101
  }
3073
3102
  function providerSummaryLines(rows, width, limit = 40, estimates = true) {
3074
3103
  const lines = renderTable(rows, [
3075
- { header: "provider", width: 24, cell: (row) => truncate(row.provider ?? "\u2013 (unknown)", 24) },
3076
- { header: "models", width: 13, align: "right", optional: true, cell: (row) => formatCount(row.models.length) },
3104
+ { header: "provider", width: 14, max: 32, cell: (row, column) => truncate(row.provider ?? "\u2013 (unknown)", column) },
3105
+ { header: "models", width: 6, align: "right", priority: 4, cell: (row) => formatCount(row.models.length) },
3077
3106
  { header: "steps", width: 6, align: "right", cell: (row) => formatCount(row.usage.steps) },
3078
- { header: "share", width: 6, align: "right", optional: true, cell: (row) => formatPercent(row.share, 0) },
3079
- { header: "prompt", width: 10, align: "right", cell: (row) => formatTokens(row.cache.prompt) },
3080
- { header: "cached", width: 10, align: "right", cell: (row) => formatTokens(row.cache.cached) },
3081
- { header: "hit", width: 7, align: "right", optional: true, cell: (row) => formatPercent(row.cache.hit) },
3082
- { header: "tps p50", width: 8, align: "right", optional: true, cell: (row) => formatTps(row.tps) },
3083
- { header: "wall", width: 8, align: "right", optional: true, cell: (row) => formatMs(row.wall) },
3084
- { header: "cost", width: 9, align: "right", optional: true, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
3107
+ { header: "share", width: 6, align: "right", priority: 3, cell: (row) => formatPercent(row.share, 0) },
3108
+ { header: "prompt", width: 7, max: 10, align: "right", cell: (row) => formatTokens(row.cache.prompt) },
3109
+ { header: "cached", width: 7, max: 10, align: "right", cell: (row) => formatTokens(row.cache.cached) },
3110
+ { header: "hit", width: 6, max: 7, align: "right", priority: 2, cell: (row) => formatPercent(row.cache.hit) },
3111
+ { header: "tps p50", width: 8, align: "right", priority: 5, cell: (row) => formatTps(row.tps) },
3112
+ { header: "wall", width: 7, align: "right", priority: 6, cell: (row) => formatMs(row.wall) },
3113
+ { header: "cost", width: 8, max: 9, align: "right", priority: 1, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
3085
3114
  ], width, {
3086
3115
  limit,
3087
3116
  empty: "no usage recorded for this range",
@@ -3092,16 +3121,16 @@ function providerSummaryLines(rows, width, limit = 40, estimates = true) {
3092
3121
  }
3093
3122
  function providerLines(rows, width, limit = 40, estimates = true) {
3094
3123
  return renderTable(rows, [
3095
- { header: "model", width: 26, cell: (row) => modelCell(row.modelID, row.variant, 26) },
3096
- { header: "provider", width: 22, cell: (row) => truncate(row.provider ?? "\u2013 (unknown)", 22) },
3124
+ { header: "model", width: 24, max: 30, cell: (row, column) => modelCell(row.modelID, row.variant, column) },
3125
+ { header: "provider", width: 12, max: 24, cell: (row, column) => truncate(row.provider ?? "\u2013 (unknown)", column) },
3097
3126
  { header: "steps", width: 6, align: "right", cell: (row) => formatCount(row.usage.steps) },
3098
- { header: "share", width: 6, align: "right", optional: true, cell: (row) => formatPercent(row.share, 0) },
3099
- { header: "prompt", width: 10, align: "right", cell: (row) => formatTokens(row.cache.prompt) },
3100
- { header: "cached", width: 10, align: "right", cell: (row) => formatTokens(row.cache.cached) },
3101
- { header: "hit", width: 7, align: "right", optional: true, cell: (row) => formatPercent(row.cache.hit) },
3102
- { header: "tps p50", width: 8, align: "right", optional: true, cell: (row) => formatTps(row.tps) },
3103
- { header: "wall", width: 8, align: "right", optional: true, cell: (row) => formatMs(row.wall) },
3104
- { header: "cost", width: 9, align: "right", optional: true, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
3127
+ { header: "share", width: 6, align: "right", priority: 5, cell: (row) => formatPercent(row.share, 0) },
3128
+ { header: "prompt", width: 7, max: 10, align: "right", priority: 2, cell: (row) => formatTokens(row.cache.prompt) },
3129
+ { header: "cached", width: 7, max: 10, align: "right", priority: 3, cell: (row) => formatTokens(row.cache.cached) },
3130
+ { header: "hit", width: 6, max: 7, align: "right", priority: 4, cell: (row) => formatPercent(row.cache.hit) },
3131
+ { header: "tps p50", width: 8, align: "right", priority: 6, cell: (row) => formatTps(row.tps) },
3132
+ { header: "wall", width: 7, align: "right", priority: 7, cell: (row) => formatMs(row.wall) },
3133
+ { header: "cost", width: 8, max: 9, align: "right", priority: 1, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
3105
3134
  ], width, {
3106
3135
  limit,
3107
3136
  empty: "no usage recorded for this range",
@@ -3111,80 +3140,126 @@ function providerLines(rows, width, limit = 40, estimates = true) {
3111
3140
  }
3112
3141
  function cacheLines(data, width) {
3113
3142
  const lines = [];
3143
+ const push = (text, tone = "base") => {
3144
+ lines.push({ text: truncate(text, width), tone });
3145
+ };
3114
3146
  const total = data.drops.reduce((sum, drop) => sum + drop.tokens, 0);
3115
- lines.push({ text: `reuse losses ${count(data.drops.length, "event")} \xB7 ${formatTokens(total)} tokens`, tone: total > 0 ? "warning" : "base" });
3147
+ push(`reuse losses ${count(data.drops.length, "event")} \xB7 ${formatTokens(total)} tokens`, total > 0 ? "warning" : "base");
3116
3148
  for (const reason of ["expired", "compaction", "unknown"]) {
3117
3149
  const group = data.drops.filter((drop) => drop.reason === reason);
3118
3150
  if (group.length === 0)
3119
3151
  continue;
3120
3152
  const tokens = group.reduce((sum, drop) => sum + drop.tokens, 0);
3121
- lines.push({ text: ` ${padCell(reason, 11)}${formatCount(group.length)} \xB7 ${formatTokens(tokens)}`, tone: reason === "expired" ? "warning" : "muted" });
3153
+ push(` ${padCell(reason, 11)}${formatCount(group.length)} \xB7 ${formatTokens(tokens)}`, reason === "expired" ? "warning" : "muted");
3122
3154
  }
3123
3155
  if (data.drops.length === 0)
3124
- lines.push({ text: " none: prompt cache held through every gap", tone: "muted" });
3156
+ push(" none: prompt cache held through every gap", "muted");
3125
3157
  if (data.models !== undefined && data.models.length > 0) {
3126
3158
  lines.push({ text: "", tone: "muted" });
3127
- lines.push({ text: "reuse by model", tone: HEADER_TONE });
3128
- const widths = width >= 100 ? [30, 7, 11, 10, 8, 8, 10] : [18, 6, 9, 9, 7, 7, 9];
3129
- lines.push({ text: formatTableRow(["model", "steps", "prompt", "cached", "write", "hit", "hit p50"], widths, ["left", "right", "right", "right", "right", "right", "right"]), tone: "muted" });
3130
- for (const row of data.models.slice(0, 10)) {
3131
- const name = modelCell(row.usage.modelID, row.usage.variant, widths[0]);
3132
- lines.push({
3133
- 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"]),
3134
- tone: "base",
3135
- row: { kind: "model", value: row.usage.modelID, ...row.usage.variant !== undefined ? { variant: row.usage.variant } : {} }
3136
- });
3159
+ push("reuse by model", HEADER_TONE);
3160
+ let modelWidth = 12;
3161
+ const reuse = fitTable([
3162
+ { header: "model", width: 12, max: 30, cell: (row) => modelCell(row.usage.modelID, row.usage.variant, modelWidth) },
3163
+ { header: "steps", width: 6, align: "right", cell: (row) => formatCount(row.cache.steps) },
3164
+ { header: "prompt", width: 7, max: 10, align: "right", cell: (row) => formatTokens(row.cache.prompt) },
3165
+ { header: "cached", width: 7, max: 10, align: "right", cell: (row) => formatTokens(row.cache.cached) },
3166
+ { header: "write", width: 6, max: 8, align: "right", priority: 1, cell: (row) => formatTokens(row.cache.cacheWrite) },
3167
+ { header: "hit", width: 6, max: 7, align: "right", priority: 2, cell: (row) => formatPercent(row.cache.hit) },
3168
+ { header: "hit p50", width: 7, max: 9, align: "right", priority: 3, cell: (row) => formatPercent(row.cache.hitP50) }
3169
+ ], width);
3170
+ modelWidth = reuse.find((column) => column.index === 0)?.width ?? 0;
3171
+ if (reuse.length > 0) {
3172
+ lines.push({ text: formatTableRow(reuse.map((column) => column.header), reuse.map((column) => column.width), reuse.map((column) => column.align)), tone: "muted" });
3173
+ for (const row of data.models.slice(0, 10)) {
3174
+ lines.push({
3175
+ text: formatTableRow(reuse.map((column) => column.cell(row)), reuse.map((column) => column.width), reuse.map((column) => column.align)),
3176
+ tone: "base",
3177
+ row: { kind: "model", value: row.usage.modelID, ...row.usage.variant !== undefined ? { variant: row.usage.variant } : {} }
3178
+ });
3179
+ }
3137
3180
  }
3138
3181
  }
3139
3182
  if (data.gap !== undefined && data.gap.samples > 0) {
3140
3183
  lines.push({ text: "", tone: "muted" });
3141
- lines.push({ text: "cache survival against idle time", tone: HEADER_TONE });
3142
- const widths = [9, 8, 9, 9, 10];
3143
- lines.push({ text: formatTableRow(["gap", "pairs", "kept", "drops", "lost"], widths, ["left", "right", "right", "right", "right"]), tone: "muted" });
3144
- for (const bucket of data.gap.buckets) {
3145
- if (bucket.samples === 0)
3146
- continue;
3147
- lines.push({
3148
- text: formatTableRow([bucket.label, formatCount(bucket.samples), formatPercent(bucket.retainedRatio, 0), formatCount(bucket.drops), formatTokens(bucket.lostTokens)], widths, ["left", "right", "right", "right", "right"]),
3149
- tone: bucket.drops > 0 ? "warning" : "base"
3150
- });
3184
+ push("cache survival against idle time", HEADER_TONE);
3185
+ const buckets = data.gap.buckets;
3186
+ const survival = fitTable([
3187
+ { header: "gap", width: 8, max: 12, cell: (bucket) => bucket.label },
3188
+ { header: "pairs", width: 6, align: "right", cell: (bucket) => formatCount(bucket.samples) },
3189
+ { header: "kept", width: 7, align: "right", cell: (bucket) => formatPercent(bucket.retainedRatio, 0) },
3190
+ { header: "drops", width: 7, align: "right", cell: (bucket) => formatCount(bucket.drops) },
3191
+ { header: "lost", width: 8, align: "right", cell: (bucket) => formatTokens(bucket.lostTokens) }
3192
+ ], width);
3193
+ if (survival.length > 0) {
3194
+ lines.push({ text: formatTableRow(survival.map((column) => column.header), survival.map((column) => column.width), survival.map((column) => column.align)), tone: "muted" });
3195
+ for (const bucket of buckets) {
3196
+ if (bucket.samples === 0)
3197
+ continue;
3198
+ lines.push({
3199
+ text: formatTableRow(survival.map((column) => column.cell(bucket)), survival.map((column) => column.width), survival.map((column) => column.align)),
3200
+ tone: bucket.drops > 0 ? "warning" : "base"
3201
+ });
3202
+ }
3151
3203
  }
3152
3204
  if (data.gap.providers.length > 1) {
3153
3205
  lines.push({ text: "", tone: "muted" });
3154
- lines.push({ text: "reuse kept, by provider", tone: HEADER_TONE });
3155
- const providerWidths = [26, 8, 9, 9];
3156
- lines.push({ text: formatTableRow(["provider", "pairs", "kept", "lost"], providerWidths, ["left", "right", "right", "right"]), tone: "muted" });
3157
- for (const row of data.gap.providers.slice(0, 8)) {
3158
- lines.push({
3159
- text: formatTableRow([row.provider, formatCount(row.samples), formatPercent(row.retainedRatio, 0), formatTokens(row.lostTokens)], providerWidths, ["left", "right", "right", "right"]),
3160
- tone: "base"
3161
- });
3206
+ push("reuse kept, by provider", HEADER_TONE);
3207
+ const providers = data.gap.providers;
3208
+ const reuse = fitTable([
3209
+ { header: "provider", width: 12, max: 30, cell: (row) => row.provider },
3210
+ { header: "pairs", width: 6, align: "right", cell: (row) => formatCount(row.samples) },
3211
+ { header: "kept", width: 7, align: "right", cell: (row) => formatPercent(row.retainedRatio, 0) },
3212
+ { header: "lost", width: 8, align: "right", cell: (row) => formatTokens(row.lostTokens) }
3213
+ ], width);
3214
+ if (reuse.length > 0) {
3215
+ lines.push({ text: formatTableRow(reuse.map((column) => column.header), reuse.map((column) => column.width), reuse.map((column) => column.align)), tone: "muted" });
3216
+ for (const row of providers.slice(0, 8)) {
3217
+ lines.push({
3218
+ text: formatTableRow(reuse.map((column) => column.cell(row)), reuse.map((column) => column.width), reuse.map((column) => column.align)),
3219
+ tone: "base"
3220
+ });
3221
+ }
3162
3222
  }
3163
3223
  }
3164
3224
  }
3165
3225
  lines.push({ text: "", tone: "muted" });
3166
- lines.push({ text: "observed cache TTL bounds", tone: HEADER_TONE });
3226
+ push("observed cache TTL bounds", HEADER_TONE);
3167
3227
  if (data.evidence.length === 0) {
3168
- lines.push({ text: " no comparable step pairs yet", tone: "muted" });
3228
+ push(" no comparable step pairs yet", "muted");
3169
3229
  } else {
3170
- const widths = [Math.max(18, Math.floor(width * 0.4)), 14, 12, 9];
3171
- lines.push({ text: formatTableRow(["model", "hits up to", "drops from", "samples"], widths, ["left", "right", "right", "right"]), tone: "muted" });
3172
- for (const row of data.evidence.slice(0, 10)) {
3173
- lines.push({
3174
- text: formatTableRow([`${row.providerID}/${row.modelID}`, formatGap(row.maxHitGapMs), formatGap(row.minDropGapMs), formatCount(row.samples)], widths, ["left", "right", "right", "right"]),
3175
- tone: "base"
3176
- });
3230
+ const evidence = fitTable([
3231
+ { header: "model", width: 12, max: 44, cell: (row) => `${row.providerID}/${row.modelID}` },
3232
+ { header: "hits up to", width: 10, align: "right", cell: (row) => formatGap(row.maxHitGapMs) },
3233
+ { header: "drops from", width: 10, align: "right", cell: (row) => formatGap(row.minDropGapMs) },
3234
+ { header: "samples", width: 7, align: "right", priority: 1, cell: (row) => formatCount(row.samples) }
3235
+ ], width);
3236
+ if (evidence.length > 0) {
3237
+ lines.push({ text: formatTableRow(evidence.map((column) => column.header), evidence.map((column) => column.width), evidence.map((column) => column.align)), tone: "muted" });
3238
+ for (const row of data.evidence.slice(0, 10)) {
3239
+ lines.push({
3240
+ text: formatTableRow(evidence.map((column) => column.cell(row)), evidence.map((column) => column.width), evidence.map((column) => column.align)),
3241
+ tone: "base"
3242
+ });
3243
+ }
3177
3244
  }
3178
3245
  }
3179
3246
  if (data.drops.length > 0) {
3180
3247
  lines.push({ text: "", tone: "muted" });
3181
- lines.push({ text: "recent losses", tone: HEADER_TONE });
3182
- for (const drop of [...data.drops].sort((a, b) => b.at - a.at).slice(0, 8)) {
3183
- const when = new Date(drop.at).toLocaleTimeString("en-GB");
3184
- lines.push({
3185
- 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"]),
3186
- tone: drop.reason === "expired" ? "warning" : "base"
3187
- });
3248
+ push("recent losses", HEADER_TONE);
3249
+ const losses = fitTable([
3250
+ { header: "when", width: 8, cell: (drop) => new Date(drop.at).toLocaleTimeString("en-GB") },
3251
+ { header: "provider/model", width: 14, max: 40, cell: (drop) => `${drop.providerID ?? ""}/${drop.modelID ?? ""}` },
3252
+ { header: "tokens", width: 7, align: "right", cell: (drop) => `\u2212${formatTokens(drop.tokens)}` },
3253
+ { header: "idle", width: 12, max: 24, priority: 1, cell: (drop) => `${formatGap(drop.gapMs)} idle \xB7 ${drop.reason}` }
3254
+ ], width);
3255
+ if (losses.length > 0) {
3256
+ lines.push({ text: formatTableRow(losses.map((column) => column.header), losses.map((column) => column.width), losses.map((column) => column.align)), tone: "muted" });
3257
+ for (const drop of [...data.drops].sort((left, right) => right.at - left.at).slice(0, 8)) {
3258
+ lines.push({
3259
+ text: formatTableRow(losses.map((column) => column.cell(drop)), losses.map((column) => column.width), losses.map((column) => column.align)),
3260
+ tone: drop.reason === "expired" ? "warning" : "base"
3261
+ });
3262
+ }
3188
3263
  }
3189
3264
  }
3190
3265
  return lines;
@@ -3205,110 +3280,162 @@ function periodLines(rows, width, granularity, limit = 40, estimates = true, tot
3205
3280
  }, { steps: 0, sessions: 0, input: 0, cached: 0, cacheWrite: 0, output: 0, reasoning: 0, cost: 0, listCost: 0 });
3206
3281
  const overall = usageBuckets(totals);
3207
3282
  lines.push({
3208
- text: `${granularityLabel(granularity)} \xB7 ${count(rows.length, "period")} \xB7 ${count(totals.steps, "step")} \xB7 hit ${formatPercent(overall.hit)}`,
3283
+ text: truncate(`${granularityLabel(granularity)} \xB7 ${count(rows.length, "period")} \xB7 ${count(totals.steps, "step")} \xB7 hit ${formatPercent(overall.hit)}`, width),
3209
3284
  tone: "muted"
3210
3285
  });
3211
3286
  if (rows.length > 1) {
3212
- lines.push({ text: `steps ${sparkline(rows.map((row) => row.usage.steps))}`, tone: "muted" });
3213
- lines.push({ text: `cached ${sparkline(rows.map((row) => row.usage.cached))}`, tone: "muted" });
3287
+ const bars = Math.max(4, width - 8);
3288
+ lines.push({ text: truncate(`steps ${sparkline(rows.map((row) => row.usage.steps), bars)}`, width), tone: "muted" });
3289
+ lines.push({ text: truncate(`cached ${sparkline(rows.map((row) => row.usage.cached), bars)}`, width), tone: "muted" });
3214
3290
  lines.push({
3215
- text: `cost ${sparkline(rows.map((row) => Math.round((estimates ? row.usage.cost + row.usage.listCost : row.usage.cost) * 1e4)))}`,
3291
+ text: truncate(`cost ${sparkline(rows.map((row) => Math.round((estimates ? row.usage.cost + row.usage.listCost : row.usage.cost) * 1e4)), bars)}`, width),
3216
3292
  tone: "muted"
3217
3293
  });
3218
3294
  }
3219
- const withSessions = tableWidths(width, [[12, 7, 7, 11, 10, 7, 9, 9]]).length === 8;
3220
- const widths = withSessions ? [12, 7, 7, 11, 10, 7, 9, 9] : [12, 7, 11, 10, 7, 9, 9];
3221
- const aligns = widths.map((_, index) => index === 0 ? "left" : "right");
3222
- const header = withSessions ? ["period", "steps", "sess", "prompt", "cached", "hit", "out", "cost"] : ["period", "steps", "prompt", "cached", "hit", "out", "cost"];
3223
- lines.push({ text: formatTableRow(header, widths, aligns), tone: HEADER_TONE });
3295
+ const columns = fitTable([
3296
+ { header: "period", width: 10, max: 24, cell: (row) => row.label },
3297
+ { header: "steps", width: 6, align: "right", cell: (row) => row.steps },
3298
+ { header: "sess", width: 6, align: "right", priority: 5, cell: (row) => row.sessions },
3299
+ { header: "prompt", width: 7, max: 10, align: "right", priority: 1, cell: (row) => row.prompt },
3300
+ { header: "cached", width: 7, max: 10, align: "right", priority: 2, cell: (row) => row.cached },
3301
+ { header: "hit", width: 6, max: 7, align: "right", priority: 3, cell: (row) => row.hit },
3302
+ { header: "out", width: 7, max: 9, align: "right", priority: 4, cell: (row) => row.output },
3303
+ { header: "cost", width: 8, max: 9, align: "right", cell: (row) => row.cost }
3304
+ ], width);
3305
+ if (columns.length === 0)
3306
+ return [...lines, { text: truncate("no room for a table at this width", width), tone: "muted" }];
3307
+ const header = formatTableRow(columns.map((column) => column.header), columns.map((column) => column.width), columns.map((column) => column.align));
3308
+ lines.push({ text: header, tone: HEADER_TONE });
3224
3309
  const newest = [...rows].sort((left, right) => right.at - left.at).slice(0, limit);
3225
3310
  for (const row of newest) {
3226
3311
  const buckets = usageBuckets(row.usage);
3227
- const cells = [
3228
- row.label,
3229
- formatCount(row.steps),
3230
- ...withSessions ? [formatCount(row.sessions)] : [],
3231
- formatTokens(buckets.prompt),
3232
- formatTokens(buckets.cached),
3233
- formatPercent(buckets.hit, 0),
3234
- formatTokens(buckets.output),
3235
- formatSpend(buckets.cost, estimates ? buckets.listCost : 0)
3236
- ];
3237
- lines.push({ text: formatTableRow(cells, widths, aligns), tone: "base", row: { kind: "period", value: row.key } });
3312
+ const view = {
3313
+ label: row.label,
3314
+ steps: formatCount(row.steps),
3315
+ sessions: formatCount(row.sessions),
3316
+ prompt: formatTokens(buckets.prompt),
3317
+ cached: formatTokens(buckets.cached),
3318
+ hit: formatPercent(buckets.hit, 0),
3319
+ output: formatTokens(buckets.output),
3320
+ cost: formatSpend(buckets.cost, estimates ? buckets.listCost : 0)
3321
+ };
3322
+ lines.push({
3323
+ text: formatTableRow(columns.map((column) => column.cell(view)), columns.map((column) => column.width), columns.map((column) => column.align)),
3324
+ tone: "base",
3325
+ row: { kind: "period", value: row.key }
3326
+ });
3238
3327
  }
3239
3328
  lines.push({ text: "", tone: "muted" });
3329
+ const total = {
3330
+ label: "total",
3331
+ steps: formatCount(totals.steps),
3332
+ sessions: formatCount(totalSessions ?? totals.sessions),
3333
+ prompt: formatTokens(overall.prompt),
3334
+ cached: formatTokens(overall.cached),
3335
+ hit: formatPercent(overall.hit, 0),
3336
+ output: formatTokens(overall.output),
3337
+ cost: formatSpend(overall.cost, estimates ? overall.listCost : 0)
3338
+ };
3240
3339
  lines.push({
3241
- text: formatTableRow([
3242
- "total",
3243
- formatCount(totals.steps),
3244
- ...withSessions ? [formatCount(totalSessions ?? totals.sessions)] : [],
3245
- formatTokens(overall.prompt),
3246
- formatTokens(overall.cached),
3247
- formatPercent(overall.hit, 0),
3248
- formatTokens(overall.output),
3249
- formatSpend(overall.cost, estimates ? overall.listCost : 0)
3250
- ], widths, aligns),
3340
+ text: formatTableRow(columns.map((column) => column.cell(total)), columns.map((column) => column.width), columns.map((column) => column.align)),
3251
3341
  tone: HEADER_TONE
3252
3342
  });
3253
3343
  if (rows.length === 0)
3254
- lines.push({ text: "no usage recorded for this range", tone: "muted" });
3344
+ lines.push({ text: truncate("no usage recorded for this range", width), tone: "muted" });
3255
3345
  return lines;
3256
3346
  }
3347
+ function latencyMetric(label, values, width) {
3348
+ const prefix = padCell(label, 11);
3349
+ let text = `${prefix}${values[0] ?? "\u2013"}`;
3350
+ for (let index = 1;index < values.length; index += 1) {
3351
+ const next = `${text} \xB7 ${values[index]}`;
3352
+ if (next.length > width)
3353
+ break;
3354
+ text = next;
3355
+ }
3356
+ return text;
3357
+ }
3257
3358
  function latencyLines(report, width, groups, limit = 12, estimates = true) {
3258
3359
  const lines = [];
3259
- lines.push({
3260
- 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]),
3261
- tone: "base"
3262
- });
3263
- lines.push({
3264
- 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]),
3265
- tone: "base"
3266
- });
3267
- lines.push({
3268
- 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]),
3269
- tone: "base"
3270
- });
3360
+ if (width >= 68) {
3361
+ lines.push({
3362
+ 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]),
3363
+ tone: "base"
3364
+ });
3365
+ lines.push({
3366
+ 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]),
3367
+ tone: "base"
3368
+ });
3369
+ lines.push({
3370
+ 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]),
3371
+ tone: "base"
3372
+ });
3373
+ } else {
3374
+ lines.push({
3375
+ text: latencyMetric("wall", [`p50 ${formatMs(report.wall.p50)}`, `p90 ${formatMs(report.wall.p90)}`, `p99 ${formatMs(report.wall.p99)}`, `n ${formatCount(report.wall.count)}`], width),
3376
+ tone: "base"
3377
+ });
3378
+ lines.push({
3379
+ text: latencyMetric("stream", [`p50 ${formatMs(report.stream.p50)}`, `p90 ${formatMs(report.stream.p90)}`, `p99 ${formatMs(report.stream.p99)}`], width),
3380
+ tone: "base"
3381
+ });
3382
+ lines.push({
3383
+ text: latencyMetric("throughput", [`p50 ${formatTps(report.tps.p50)}`, `p90 ${formatTps(report.tps.p90)}`, `p99 ${formatTps(report.tps.p99)}`], width),
3384
+ tone: "base"
3385
+ });
3386
+ }
3271
3387
  lines.push({ text: "", tone: "muted" });
3272
3388
  lines.push({
3273
3389
  text: truncate("wall = request to completed \xB7 stream = request to last chunk \xB7 throughput = output+reasoning tokens/s", width),
3274
3390
  tone: "muted"
3275
3391
  });
3276
3392
  if (report.wall.count === 0)
3277
- lines.push({ text: "no completed steps for this range", tone: "muted" });
3393
+ lines.push({ text: truncate("no completed steps for this range", width), tone: "muted" });
3278
3394
  if (groups !== undefined && groups.length > 0) {
3279
3395
  lines.push({ text: "", tone: "muted" });
3280
3396
  lines.push({ text: "by group", tone: HEADER_TONE });
3281
- const widths = width >= 110 ? [24, 6, 9, 9, 9, 9, 9, 9] : [16, 5, 8, 8, 8, 8, 8, 8];
3282
- const aligns = ["left", "right", "right", "right", "right", "right", "right", "right"];
3283
- lines.push({ text: formatTableRow(["group", "n", "wall p50", "wall p90", "stream", "tps p50", "tps p90", "cost"], widths, aligns), tone: "muted" });
3284
- for (const row of groups.slice(0, limit)) {
3285
- const thin = row.count < 5;
3286
- lines.push({
3287
- text: formatTableRow([
3288
- row.label,
3289
- formatCount(row.count),
3290
- formatMs(row.wall.p50),
3291
- formatMs(row.wall.p90),
3292
- formatMs(row.stream.p50),
3293
- formatTps(row.tps.p50),
3294
- formatTps(row.tps.p90),
3295
- formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0)
3296
- ], widths, aligns),
3297
- tone: thin ? "muted" : "base",
3298
- row: { kind: "model", value: row.key }
3299
- });
3397
+ const columns = fitTable([
3398
+ { header: "group", width: 8, max: 32, cell: (row) => row.label },
3399
+ { header: "n", width: 4, align: "right", cell: (row) => formatCount(row.count) },
3400
+ { header: "wall p50", width: 8, align: "right", cell: (row) => formatMs(row.wall.p50) },
3401
+ { header: "wall p90", width: 8, max: 9, align: "right", priority: 1, cell: (row) => formatMs(row.wall.p90) },
3402
+ { header: "stream", width: 6, max: 9, align: "right", priority: 2, cell: (row) => formatMs(row.stream.p50) },
3403
+ { header: "tps p50", width: 8, max: 9, align: "right", cell: (row) => formatTps(row.tps.p50) },
3404
+ { header: "tps p90", width: 8, max: 9, align: "right", priority: 3, cell: (row) => formatTps(row.tps.p90) },
3405
+ { header: "cost", width: 8, max: 9, align: "right", cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
3406
+ ], width);
3407
+ if (columns.length > 0) {
3408
+ lines.push({ text: formatTableRow(columns.map((column) => column.header), columns.map((column) => column.width), columns.map((column) => column.align)), tone: "muted" });
3409
+ for (const row of groups.slice(0, limit)) {
3410
+ const thin = row.count < 5;
3411
+ lines.push({
3412
+ text: formatTableRow(columns.map((column) => column.cell(row)), columns.map((column) => column.width), columns.map((column) => column.align)),
3413
+ tone: thin ? "muted" : "base",
3414
+ row: { kind: "model", value: row.key }
3415
+ });
3416
+ }
3300
3417
  }
3301
- lines.push({ text: "n < 5 is dimmed: too few samples to compare", tone: "muted" });
3418
+ lines.push({ text: truncate("n < 5 is dimmed: too few samples to compare", width), tone: "muted" });
3302
3419
  }
3303
3420
  return lines;
3304
3421
  }
3305
3422
  function sessionsLines(rows, width, options) {
3306
3423
  const limit = options?.limit ?? 20;
3307
3424
  const estimates = options?.estimates !== false;
3308
- const widths = [Math.max(18, Math.floor(width * 0.36)), 7, 11, 10, 7, 9, 9];
3309
- const aligns = ["left", "right", "right", "right", "right", "right", "right"];
3425
+ const columns = fitTable([
3426
+ { header: "title", width: 10, max: 48, cell: (row) => row.title },
3427
+ { header: "steps", width: 6, align: "right", cell: (row) => row.steps },
3428
+ { header: "prompt", width: 7, max: 10, align: "right", priority: 1, cell: (row) => row.prompt },
3429
+ { header: "cached", width: 7, max: 10, align: "right", priority: 2, cell: (row) => row.cached },
3430
+ { header: "hit", width: 6, max: 7, align: "right", priority: 3, cell: (row) => row.hit },
3431
+ { header: "out", width: 7, max: 9, align: "right", priority: 4, cell: (row) => row.output },
3432
+ { header: "cost", width: 8, max: 9, align: "right", cell: (row) => row.cost }
3433
+ ], width);
3310
3434
  const lines = [
3311
- { text: formatTableRow(["title", "steps", "prompt", "cached", "hit", "out", "cost"], widths, aligns), tone: HEADER_TONE }
3435
+ {
3436
+ text: formatTableRow(columns.map((column) => column.header), columns.map((column) => column.width), columns.map((column) => column.align)),
3437
+ tone: HEADER_TONE
3438
+ }
3312
3439
  ];
3313
3440
  const byID = new Map(rows.map((row) => [row.sessionID, row]));
3314
3441
  const ordered = rows.filter((row) => row.parentID === undefined || !byID.has(row.parentID));
@@ -3316,18 +3443,18 @@ function sessionsLines(rows, width, options) {
3316
3443
  const orderedAll = [...ordered, ...children];
3317
3444
  for (const row of orderedAll.slice(0, limit)) {
3318
3445
  const child = row.parentID !== undefined && byID.has(row.parentID);
3319
- const title = truncate(row.title ?? row.sessionID, Math.max(8, widths[0] - (child ? 2 : 0)));
3320
3446
  const buckets = usageBuckets(row);
3447
+ const view = {
3448
+ title: `${child ? "\u21B3 " : ""}${row.title ?? row.sessionID}`,
3449
+ steps: formatCount(buckets.steps),
3450
+ prompt: formatTokens(buckets.prompt),
3451
+ cached: formatTokens(buckets.cached),
3452
+ hit: formatPercent(buckets.hit, 0),
3453
+ output: formatTokens(buckets.output),
3454
+ cost: formatSpend(buckets.cost, estimates ? buckets.listCost : 0)
3455
+ };
3321
3456
  lines.push({
3322
- text: formatTableRow([
3323
- `${child ? "\u21B3 " : ""}${title}`,
3324
- formatCount(buckets.steps),
3325
- formatTokens(buckets.prompt),
3326
- formatTokens(buckets.cached),
3327
- formatPercent(buckets.hit, 0),
3328
- formatTokens(buckets.output),
3329
- formatSpend(buckets.cost, estimates ? buckets.listCost : 0)
3330
- ], widths, aligns),
3457
+ text: formatTableRow(columns.map((column) => column.cell(view)), columns.map((column) => column.width), columns.map((column) => column.align)),
3331
3458
  tone: child ? "muted" : "base",
3332
3459
  row: { kind: "session", value: row.sessionID }
3333
3460
  });
@@ -3342,14 +3469,14 @@ function sessionsLines(rows, width, options) {
3342
3469
  const share = rows.reduce((sum, row) => sum + row.steps, 0);
3343
3470
  lines.push({ text: "", tone: "muted" });
3344
3471
  lines.push({
3345
- 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)}`,
3472
+ 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),
3346
3473
  tone: "muted"
3347
3474
  });
3348
3475
  }
3349
3476
  if (rows.length > limit)
3350
- lines.push({ text: `\u2026 ${count(rows.length - limit, "more session")} \xB7 narrow the range`, tone: "muted" });
3477
+ lines.push({ text: truncate(`\u2026 ${count(rows.length - limit, "more session")} \xB7 narrow the range`, width), tone: "muted" });
3351
3478
  if (rows.length === 0)
3352
- lines.push({ text: "no sessions recorded for this range", tone: "muted" });
3479
+ lines.push({ text: truncate("no sessions recorded for this range", width), tone: "muted" });
3353
3480
  return lines;
3354
3481
  }
3355
3482
  function dbLines(data, width) {
@@ -3357,16 +3484,16 @@ function dbLines(data, width) {
3357
3484
  const size = (bytes) => `${(bytes / 1048576).toFixed(1)} MB`;
3358
3485
  lines.push({ text: truncate(data.info.path, width), tone: "base" });
3359
3486
  lines.push({
3360
- text: `${size(data.info.bytes)}${data.info.walBytes === undefined ? "" : ` + ${size(data.info.walBytes)} wal`} \xB7 schema v${data.info.schemaVersion}`,
3487
+ text: truncate(`${size(data.info.bytes)}${data.info.walBytes === undefined ? "" : ` + ${size(data.info.walBytes)} wal`} \xB7 schema v${data.info.schemaVersion}`, width),
3361
3488
  tone: "muted"
3362
3489
  });
3363
3490
  lines.push({ text: "", tone: "muted" });
3364
3491
  lines.push({ text: "what it holds", tone: HEADER_TONE });
3365
3492
  for (const table of data.info.tables) {
3366
- lines.push({ text: ` ${padCell(table.name, 12)}${formatCount(table.rows)} rows`, tone: table.rows === 0 ? "muted" : "base" });
3493
+ lines.push({ text: truncate(` ${padCell(table.name, 12)}${formatCount(table.rows)} rows`, width), tone: table.rows === 0 ? "muted" : "base" });
3367
3494
  }
3368
3495
  lines.push({
3369
- 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")}`,
3496
+ 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),
3370
3497
  tone: "muted"
3371
3498
  });
3372
3499
  lines.push({ text: "", tone: "muted" });
@@ -3382,7 +3509,7 @@ function dbLines(data, width) {
3382
3509
  tone: data.info.schemaVersion < 10 || data.info.unpriced > 0 ? "base" : "muted"
3383
3510
  });
3384
3511
  }
3385
- const widths = [10, 13, 10];
3512
+ const widths = [9, 11, 10];
3386
3513
  lines.push({ text: formatTableRow(["keep", "prunes", "older than"], widths, ["left", "right", "left"]), tone: "muted" });
3387
3514
  for (const option of data.info.removable) {
3388
3515
  const before = new Date(data.now - option.days * 24 * 60 * 60 * 1000).toLocaleDateString("en-GB");
@@ -3402,7 +3529,7 @@ function dbLines(data, width) {
3402
3529
  });
3403
3530
  if (data.busy === true) {
3404
3531
  lines.push({ text: "", tone: "muted" });
3405
- lines.push({ text: "the recorder is writing: try the vacuum again in a moment", tone: "warning" });
3532
+ lines.push({ text: truncate("the recorder is writing: try the vacuum again in a moment", width), tone: "warning" });
3406
3533
  }
3407
3534
  return lines;
3408
3535
  }
@@ -3426,28 +3553,74 @@ function turnStats(turn) {
3426
3553
  steps: turn.steps
3427
3554
  };
3428
3555
  }
3556
+ function compactTurnLine(stats, width, options) {
3557
+ const labels = { ...DEFAULT_LABELS, ...options?.labels };
3558
+ const fields = { ...DEFAULT_FIELDS, ...options?.fields };
3559
+ const estimates = options?.estimates !== false;
3560
+ const headParts = [];
3561
+ if (fields.clock && stats.endAt !== undefined)
3562
+ headParts.push(formatClock(stats.endAt));
3563
+ if (stats.providerID !== undefined) {
3564
+ let identity = stats.providerID;
3565
+ if (fields.model && stats.modelID !== undefined)
3566
+ identity += `/${stats.modelID}`;
3567
+ if (fields.variant && stats.variant)
3568
+ identity += ` ${stats.variant}`;
3569
+ headParts.push(identity);
3570
+ }
3571
+ let head = headParts.join(" ");
3572
+ const totals = options?.turnTotals === "sum" ? stats.totals : stats.latest;
3573
+ const display = displayTotals(totals);
3574
+ const money = fields.cost ? formatMoney(stats.totals.cost, estimates ? stats.totals.listCost : 0, labels.estimate) : undefined;
3575
+ const cost = money === undefined ? undefined : `${labels.cost} ${money}`.trim();
3576
+ const warning = fields.cacheExpiry && stats.cacheExpired ? `\u26A0 ${labels.cacheExpired} (\u2212${formatTokens(stats.cacheDropTokens)}${stats.drops.length > 1 ? ` \xD7${stats.drops.length}` : ""})` : undefined;
3577
+ let suffix = [warning, cost].filter((part) => part !== undefined).join(" \xB7 ");
3578
+ if (suffix !== "" && suffix.length + 3 > width)
3579
+ suffix = cost ?? suffix;
3580
+ head = truncate(head, Math.max(0, width - (suffix === "" ? 0 : suffix.length + 3)));
3581
+ if (!fields.turn || stats.totals.steps === 0)
3582
+ return [head, suffix].filter((part) => part !== "").join(" \xB7 ");
3583
+ const input = display.prompt === display.input ? `${labels.input} ${formatTokens(display.input)}` : `${labels.input} ${formatTokens(display.input)} (${formatTokens(display.prompt)})`;
3584
+ const volumes = [
3585
+ input,
3586
+ `${labels.cached} ${formatTokens(display.cached)}`,
3587
+ `${labels.output} ${formatTokens(display.output)}`,
3588
+ ...fields.reasoning ? [`${labels.reasoning} ${formatTokens(display.reasoning)}`] : []
3589
+ ];
3590
+ let numbers = "";
3591
+ for (const volume of volumes) {
3592
+ const next = numbers === "" ? volume : `${numbers}, ${volume}`;
3593
+ if ([head, next, suffix].filter((part) => part !== "").join(" \xB7 ").length > width)
3594
+ break;
3595
+ numbers = next;
3596
+ }
3597
+ return [head, numbers, suffix].filter((part) => part !== "").join(" \xB7 ");
3598
+ }
3429
3599
  function turnsLines(rows, width, options) {
3430
3600
  const lines = [];
3431
3601
  const warning = rows.filter((turn) => turn.drops.length > 0).length;
3432
3602
  lines.push({
3433
- text: `${count(rows.length, "turn")} \xB7 newest first${warning > 0 ? ` \xB7 ${count(warning, "turn")} with a cache loss` : ""}`,
3603
+ text: truncate(`${count(rows.length, "turn")} \xB7 newest first${warning > 0 ? ` \xB7 ${count(warning, "turn")} with a cache loss` : ""}`, width),
3434
3604
  tone: "muted"
3435
3605
  });
3436
3606
  const showSession = options?.sessionTitles !== undefined;
3437
3607
  for (const turn of rows) {
3438
- const line = formatLine(turnStats(turn), {
3608
+ const stats = turnStats(turn);
3609
+ const line = formatLine(stats, {
3439
3610
  ...options,
3440
3611
  fields: { ...options?.fields, session: false }
3441
3612
  });
3442
3613
  const title = showSession ? `${truncate(options.sessionTitles.get(turn.sessionID) ?? turn.sessionID, 18)} ` : "";
3614
+ const available = Math.max(0, width - title.length);
3615
+ const text = `${title}${line.length <= available ? line : compactTurnLine(stats, available, { ...options, fields: { ...options?.fields, session: false } })}`;
3443
3616
  lines.push({
3444
- text: truncate(`${title}${line}`, width),
3617
+ text: truncate(text, width),
3445
3618
  tone: turn.drops.length > 0 || turn.providerChanged ? "warning" : "base",
3446
3619
  row: { kind: "turn", value: turn.sessionID }
3447
3620
  });
3448
3621
  }
3449
3622
  if (rows.length === 0)
3450
- lines.push({ text: "no finished turns for this range", tone: "muted" });
3623
+ lines.push({ text: truncate("no finished turns for this range", width), tone: "muted" });
3451
3624
  return lines;
3452
3625
  }
3453
3626
  function sessionSummaryLines(usage, budget = 34, estimates = true) {
@@ -3547,11 +3720,12 @@ function detailLabel(row) {
3547
3720
  }
3548
3721
  }
3549
3722
  function buildPanel(request) {
3723
+ const width = clampPanelWidth(request.width);
3550
3724
  try {
3551
- return build(request);
3725
+ return build({ ...request, width }).map((line) => line.text.length <= width ? line : { ...line, text: truncate(line.text, width) });
3552
3726
  } catch (error) {
3553
3727
  const message = error instanceof Error ? error.message : String(error);
3554
- return [{ text: `stats query failed: ${message}`, tone: "warning" }];
3728
+ return [{ text: truncate(`stats query failed: ${message}`, width), tone: "warning" }];
3555
3729
  }
3556
3730
  }
3557
3731
  function build(request) {
@@ -3665,10 +3839,15 @@ function buildDetail(request, range, filters, estimates) {
3665
3839
  const steps = stepRows(db, { range, filters: scoped });
3666
3840
  return [
3667
3841
  head,
3668
- {
3669
- 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)}`,
3670
- tone: "muted"
3671
- },
3842
+ ...kvGrid([
3843
+ ["steps", formatCount(stats.steps)],
3844
+ ["in", formatTokens(stats.prompt - stats.cached)],
3845
+ ["prompt", formatTokens(stats.prompt)],
3846
+ ["cached", formatTokens(stats.cached)],
3847
+ ["write", formatTokens(stats.cacheWrite)],
3848
+ ["hit", percent(stats.hit)],
3849
+ ["p50", percent(stats.hitP50)]
3850
+ ], width),
3672
3851
  { text: "", tone: "muted" },
3673
3852
  ...providerLines(providerBreakdownOf(steps), width, 40, estimates),
3674
3853
  { text: "", tone: "muted" },
@@ -3687,10 +3866,14 @@ function buildDetail(request, range, filters, estimates) {
3687
3866
  const stats = cacheStatsOf(db, { range, filters: scoped });
3688
3867
  return [
3689
3868
  head,
3690
- {
3691
- 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)}`,
3692
- tone: "muted"
3693
- },
3869
+ ...kvGrid([
3870
+ ["steps", formatCount(stats.steps)],
3871
+ ["in", formatTokens(stats.prompt - stats.cached)],
3872
+ ["prompt", formatTokens(stats.prompt)],
3873
+ ["cached", formatTokens(stats.cached)],
3874
+ ["hit", percent(stats.hit)],
3875
+ ["p50", percent(stats.hitP50)]
3876
+ ], width),
3694
3877
  { text: "", tone: "muted" },
3695
3878
  ...providerLines(providerBreakdownOf(steps), width, 40, estimates),
3696
3879
  { text: "", tone: "muted" },
@@ -4054,7 +4237,7 @@ function createService(db, options) {
4054
4237
  }
4055
4238
  function turnHistory(input) {
4056
4239
  const handle = db();
4057
- const width = clamp2(input.width, 20, 120);
4240
+ const width = clamp2(input.width, 20, MAX_HISTORY_WIDTH);
4058
4241
  const limit = clampTurnsLimit(input.limit);
4059
4242
  const summary = sessionSummaryLines(overview(handle, undefined, { sessionID: input.sessionID }), width, options.pricing);
4060
4243
  const history = sidebarLines(turns(handle, { sessionID: input.sessionID, limit }), {
@@ -4581,4 +4764,4 @@ export {
4581
4764
  src_default as default
4582
4765
  };
4583
4766
 
4584
- //# debugId=81E9F82B0FBCADEE64756E2164756E21
4767
+ //# debugId=C871242EEDE4D90F64756E2164756E21