#10092·pi

compaction: provider usage persisted without `cost` crashes the footer on every resume (un-normalised passthrough at compaction.js:722/:728)

Author: JenpoCreated Sep 27, 2026Updated Sep 28, 2026
Labelslast-read

[Bug] compaction: single-source provider usage is persisted without cost, crashing the footer

Version: pi-coding-agent 0.87.1 Severity: crash-on-resume (the TUI dies while rendering the footer for any session whose compaction entry was written from a provider that omits usage.cost)

Exact location (verified against shipped 0.87.1 dist, read-only install):

  • crash site: dist/core/usage-totals.js:15 — totals.cost += usage.cost.total; reads .total off usage.cost with no guard (every sibling line uses a plain scalar field).
  • unnormalised writers: dist/core/compaction/compaction.js:722 (summaryUsage = historyUsage ? combineUsage(...) : turnPrefixResult.usage) and :728 (summaryUsage = result.usage) — both persist the raw provider usage; :740 writes it into { type: "compaction", usage: summaryUsage }.

Symptom

Uncaught TypeError at startup / on resume of an affected session:

TypeError: Cannot read properties of undefined (reading 'total')
    at addUsageToTotals (.../dist/bundle/chunks/chunk-OJP47DM….js)
    at FooterComponent.render (...)

Once a session contains one malformed compaction entry, every resume of that session crashes.

Why it escapes the crash handler: the throw happens inside FooterComponent.render on a timer (render-time), not in the agent turn loop, so Pi's own crash reporter does not record it — the TUI just dies and crashes.json stays empty for this class.

Root cause

dist/core/compaction/compaction.js writes the compaction entry's usage straight from the provider response on one branch, without normalising it:

javascript
// :710-728
let summaryUsage;
if (isSplitTurn && turnPrefixMessages.length > 0) {
    ...
    summaryUsage = historyUsage ? combineUsage(historyUsage, turnPrefixResult.usage)
                                : turnPrefixResult.usage;   // ← passthrough, unnormalised
} else {
    ...
    summaryUsage = result.usage;                            // ← same
}
// :740
... { type: "compaction", ..., usage: summaryUsage, ... }

combineUsage() (:52-73) does emit a full canonical Usage (it computes totalTokens and the whole cost object). But the two passthrough branches above persist exactly what the provider returned. OpenAI-compatible servers that don't report cost return only:

json
{ "input": 51916, "output": 1949, "cacheRead": 0, "cacheWrite": 0 }

…and addUsageToTotals reads usage.cost.total unguarded.

Reproduced in the wild with a local OpenAI-compatible endpoint (Ollama 0.34.0). Two entries captured from real transcripts (since backfilled by the affected user; the writer still reproduces):

json
{"type":"compaction","timestamp":"2026-09-26T09:31:11.010Z","usage":{"input":51916,"output":1949,"cacheRead":0,"cacheWrite":0}}
{"type":"compaction","timestamp":"2026-09-26T09:32:17.279Z","usage":{"input":48776,"output":2376,"cacheRead":0,"cacheWrite":0}}

Minimal, portable repro (no transcript needed):

The package restricts exports, so import the file by path (adjust the path to your install):

bash
node --input-type=module -e '
import("/ABS/PATH/TO/node_modules/@earendil-works/pi-coding-agent/dist/core/usage-totals.js").then(m => {
  // exact shape compaction.js:722/:728 persists raw from a no-cost provider (e.g. Ollama 0.34.0)
  const raw = { input: 51916, output: 1949, cacheRead: 0, cacheWrite: 0 };
  try { m.addUsageToTotals(m.createUsageTotals(), raw); console.log("no crash"); }
  catch (e) { console.log("CRASHES ->", e.constructor.name + ": " + e.message); }
  // canonical cost-bearing usage is fine (what the fix should guarantee):
  const ok = { input:1, output:1, cacheRead:0, cacheWrite:0, cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}, totalTokens:2 };
  const t = m.createUsageTotals(); m.addUsageToTotals(t, ok); console.log("with cost ->", JSON.stringify(t));
})'
# observed on 0.87.1:
#   CRASHES -> TypeError: Cannot read properties of undefined (reading 'total')
#   with cost -> {"input":1,"output":1,"cacheRead":0,"cacheWrite":0,"cost":0}

Suggested fix

  1. Normalise before persisting, i.e. route both branches through a toCanonicalUsage(usage) helper that guarantees totalTokens + cost{input,output,cacheRead,cacheWrite,total} (cost components default to 0 — Pi only ever sums costs).
  2. Cheap hardening: make addUsageToTotals tolerate a missing cost (usage.cost?.total ?? 0), so a malformed historical entry can never take down the TUI.

Workaround for affected users

A one-off backfill of existing transcripts (set cost to zeros, totalTokens to the component sum) unblocks the session; the crash is data-borne, so the writer should be fixed too.

Notes

This was found by a session-start tripwire that scans recent transcripts for usage nodes missing cost/totalTokens, which is a useful regression guard if you want to ship one as a check.