ADR 0171: Host-owned completed-turn token history
- Status: Accepted (amended by ADR 0173)
- Date: 2026-09-07
- Deciders: PI-Desktop core
- Related: D103, D157, D331, D335, ADR 0014, ADR 0173,
03-runtime/04-data-storage.md§4.6,03-runtime/06-host-rpc-protocol.md,04-ux/06-settings-ia.md, E2E-186
Context
The turns table already stores input_tokens, output_tokens, and usage_json, and session.endTurn already accepted usage. Electron never sent it. Per-message chips (D103) read provider usage from assistant meta_json. Mixing subagent spend into those chips would inflate the context inspector and the composed-turn total.
A durable completed-turn total is still required so a later dashboard can show what the user actually spent, including subagents, without a new schema version.
Decision
- Parent
message.usagestays provider-reported. Subagent totals never merge into an assistant row. - Turn rollup is
session.endTurn.usage. Electron sums every parent assistantmessage_endusage for the durable turn and addsturn_end.subagentUsagedeltas. Only that sum is stored onturns. stats.getTokenUsageHistoryis an additive host RPC. It reads completed turns in a bounded local-calendar window, buckets byday/ ISOweek/month, fills empty buckets, and does not bumpPROTOCOL_VERSIONorSCHEMA_VERSION.idx_turns_ended_atis created withCREATE INDEX IF NOT EXISTSat boot.- The user-facing dashboard is not Settings. ADR 0173 moves that surface to marketplace plugin
pi.token-insights. This RPC still exists so local completed-turn history, including subagent spend, has a host owner. Historical rows from before Electron sentusagemay be zero.
Consequences
- Context inspector and D103 chips keep exact provider values.
- New completed turns populate host history; older turns may be zero.
- The heatmap lives in
pi.token-insights(ADR 0173). - A later backfill from transcript
meta.usagewould be a separate change.
Alternatives
- Rewrite parent
message.usagewith subagent spend: rejected (D103). - Scan JSONL transcripts in the host Settings page: rejected (unbounded, wrong owner). The marketplace plugin may scan local tool metadata, including JSONL.