SLOPSHOPPER

usage-ledger

Shows where the session's tokens go and when a fresh session would be cheaper

panebandguardcommandtoastprompt
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · usage-ledger
│ ┃ トークンの内訳 ✕ › fix the failing auth test and add an audit log call │ ┃ トークンの内訳 │ ┃ 集計開始 08:53(主は速報値) · 要求 0 回 · ⏺ Read(src/auth.ts) │ ┃ 重み計 0* ⎿ Read 6 lines │ ┃ ⏺ Update(src/auth.ts) │ ┃ [ 状況 ] [ 概要 ] [ スレッド ] [ 種類・モデ ⎿ Added 2 lines, removed 1 line │ ┃ ⏺ Bash(bun test) │ ┃ 今の状況 ⎿ 3 pass, 1 fail │ ┃ まだこのセッションの要求がありません。会 │ ┃ 話を始めると、ここに目安が出ます ● Done. refresh now rejects expired claims and logs an audit event. │ ┃ │ ┃ [ 詳しい数字 ] [ 書き出す ] [ 引き継ぎについ ✻ Worked for 42s · done 4:20 PM │ ┃ │ ┃ *重みは推計。サブスクの計算式は非公開 › /usage-ledger │ ⎿ usage-ledger: トークンの内訳を開きました │ │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Pane · トークンの内訳
トークンの内訳 集計開始 08:53(主は速報値) · 要求 0 回 · 重み計 0* [ 状況 ] [ 概要 ] [ スレッド ] [ 種類・モデル ] [ 高い要求 今の状況 まだこのセッションの要求がありません。会話を始めると、こ こに目安が出ます [ 詳しい数字 ] [ 書き出す ] [ 引き継ぎについて ] *重みは推計。サブスクの計算式は非公開
README

usage-ledger

会話が長くなると、1 回のやり取りで使うトークンが増えていきます。この mod は、今のセッションをあとどのくらい気にせず続けてよいかを入力欄の上に目安として出し、新しいセッションに切り替えたほうが安くなったら知らせます。 切り替えるときは、引き継ぎ文の下書きを入力欄に入れるところまで手伝います。 トークンがどこに使われているか(主スレッドとサブエージェント、種類とモデル、キャッシュの読み書き、期限切れ後の再書き込み、文脈の大きさ)は、パネルで数字として見られます。 その数字はファイルに書き出せるので、会話の中で Claude に読ませて相談できます。

表示

入力欄の上の帯

このセッションで最初の応答があった後は、いつも 1 行で出ています。数字は出さず、目安のゲージと、今の段階を一言で出します。

トークン ■■■■■■□□□□ Soon · 区切りで引き継ぐと節約に [引き継ぐ…] [詳しく]

ゲージは、新しいセッションを始めた直後の大きさで空、新しいセッションに引き継いだほうが得になる大きさ(引き継ぎの手間を 5 回以内のやり取りで取り戻せるところ)で満杯です。

段階いつ色
OKゲージが 6 割未満色なし
Soonゲージが 6 割以上黄
Switchゲージが満杯、または休憩でキャッシュが切れた後赤

段階の後ろの短い一言と [引き継ぐ…] は、することがあるときだけ出ます。

  • Soon: 「区切りで引き継ぐと節約に」
  • Switch: 「今引き継ぐと節約に」
  • キャッシュの残りが 10 分を切り、Claude が作業していないとき: 「10 分操作がないと割高に」
  • キャッシュが切れた後: 「続きは新しいセッションが安い」

OK のときは、ゲージと「OK」と [詳しく] だけです。

トークン ■■□□□□□□□□ OK [詳しく]

[引き継ぐ…] で引き継ぎの下書きを入力欄に入れ、[詳しく] で下のパネルを開きます。何を意味するか、何をすればよいかは、パネルの「状況」に文で出ます。 引き継ぎの途中は「引き継ぎの下書きを入力欄に入れました」と [取り消す]、新しいセッションが始まった後は「引き継ぎ済み」が、同じ行に出ます。引き継ぎの詳しい結果はパネルで見られます。

トークン ■■■■■■■■■■ Switch · 引き継ぎの下書きを入力欄に入れました [取り消す] [詳しく]

会話がまだ新しいセッションの最初の大きさより小さいうちは、ゲージは空のままで、一言も出ません。 幅が狭いときは、短い一言、ゲージの長さ、ゲージ、「トークン」の順に省きます。段階とボタンは残ります。 ほかの mod が帯に出しているもの(usage-band など)は、その下にそのまま残ります。

パネル

/usage-ledger か、帯の [詳しく] で開きます。最初は「状況」の画面です。上のボタンで画面を切り替えます。行の [▸] で、その行の詳細を開きます。

画面内容
状況今の段階、それが何を意味するか、何をすればよいかを文で出す。[引き継ぐ…]、詳しい数字と引き継ぎの画面へのボタン、[書き出す]
概要今の文脈、キャッシュの残り、次の 1 回と再書き込みの重み、新しいセッションの損益分岐、合計、トークンの種類別、[書き出す]
スレッド主スレッドと各サブエージェントの種類、使ったモデル(opus・sonnet・haiku、複数なら haiku/opus のように)、要求数、重み、割合。幅が狭いときは、説明、種類の順に短くし、種類、要求数、重みの順に省く(省いたものは [▸] の詳細に出る)。モデルと割合は残る
種類・モデルエージェントの種類別、モデル別
高い要求重みの大きい要求の上位 15 件
待ちと再書込5 分を超えて間が空いた要求と、キャッシュが切れた後の再書き込み
主のツール主スレッドの要求を、その応答が呼んだツールで分けたもの
引き継ぎ引き継ぎの設定、[引き継ぐ…]、引き継ぎの結果

書き出して Claude と話す

パネルの「状況」か「概要」の [書き出す] を押すと、このセッションの集計を exportDir(既定 .claude/usage-ledger/)に 2 つのファイルで書き出します。

ファイル中身
<YYYYMMDD>-<セッション id の先頭 8 文字>.mdパネルと同じ集計を Markdown の表にしたもの。概要、スレッド、種類とモデル、高い要求、主のツール、待ちと再書込の節に分かれる
同じ名前の .jsonl要求 1 回につき 1 行。時刻、要求の id(記録から読み直した要求は応答のメッセージ id)、スレッド、エージェントの種類と説明、モデル、トークンの種類別の数、文脈、重み、呼んだツール、記録から読み直した値か速報値か

書き出す前に、主スレッドを記録から読み直します(下の「数える範囲」)。日付は集計開始の日です。同じセッションで押し直すと、同じ 2 つのファイルを上書きします。会話の本文は書きません(数字、モデル名、ツール名、エージェントの種類と説明だけ)。

書き出すと、場所をトーストで知らせ、入力欄に「usage-ledger の集計を <.md の場所> に書き出しました。」という一文を入れます。入力欄に書きかけの文があるときは、その後ろに足します。送る前に「どのサブエージェントがいちばん使っている?」のような質問を書き足して送ってください。

このプラグインには、書き出したファイルの読み方を Claude に教えるスキル usage-ledger-data が入っています。Claude は質問に合う節や行だけを読むので、集計の全体を会話に流し込まずに済みます。書き出しは mod が直接ファイルに書くので、書く時点ではトークンを使いません。

exportDir は git で管理しないフォルダにしておくと、集計がコミットに混ざりません。例えば .gitignore に .claude/usage-ledger/ を足します。

重みについて

「重み」は、利用制限への響き方の目安です。サブスクの利用制限の計算式は公開されていないので、公開 API の価格比から推計しています。

  • トークンの種類: 入力 1、キャッシュ読み 0.1、キャッシュ書き込み(5 分)1.25、キャッシュ書き込み(1 時間)2、出力 5
  • モデル: haiku 1、sonnet 3、opus 5

重みの付いた数字には * を付け、「*重みは推計。サブスクの計算式は非公開」と添えています。

数える範囲

  • 正しい値は、セッションの記録(Claude Code が書く会話の記録ファイル)にあります。応答があるたびに数える値は速報値で、記録を読み直した時点で記録の値に置き換えます。記録は Claude Code が置く場所(~/.claude/projects/ の下。CLAUDE_CONFIG_DIR を設定していればその下の projects/)から探します
  • 主スレッドは、ターンが終わるたびと [書き出す] の前に記録を読み直します。読み直した後は、この mod が読み込まれる前の要求も含めて、セッション全体を数えます。同じ応答が記録に何度か書かれているときは 1 回として数えます。パネルの上の行が「セッション全体」になり、スレッドの [▸] の「数え方」が「記録から読み直した(ターンの終わりごと)」になります
  • サブエージェントは、終わった時点でその記録を読み直します
  • 読み直すまでの値(速報値)では、キャッシュ書き込みが 5 分と 1 時間のどちらだったかが要求ごとには分かりません。設定の ttlMain と ttlSub で振り分けます。読み直した値は、5 分と 1 時間の内訳まで記録のとおりです
  • 記録が 4 MiB を超えるとき、記録が見つからないとき(記録を残さない設定のときなど)や読めないとき、記録の場所が分からないとき(ホームフォルダーが分からないとき)は、読み直しません。主スレッドは、この mod が読み込まれてから(パネルの「集計開始」の時刻から)の速報値のままで、パネルと書き出しに「速報値(記録と未照合)」と理由を出します

引き継ぎ

新しいセッションに切り替えるときの手順です。mod が自分で送信したり、セッションを開いたりすることはありません。

  1. 帯かパネルの [引き継ぐ…] を押すと、入力欄に下書きが入ります。入力欄に書きかけの文があるときは、その後ろに足します
  2. 下書きは、引き継ぎ文(目標・今の段階・決めたこと・未解決・最初に読むファイル)をファイルに書き、その本文と目印 [usage-ledger handoff <id>] を最初の発言にして新しいセッションを開くよう、Claude に頼む内容です。読んで、送るかどうかを決めてください
  3. 送ると Claude が引き継ぎ文を書きます。下書きでは、新しいセッションを開く前に、どの方法で何を開くかをあなたに確認するよう頼んでいます。開く方法がない場合は、書かれた引き継ぎ文を自分で新しいセッションに貼ってください
  4. 新しいセッションの usage-ledger は目印を見つけ、最初の 3 回の要求の文脈を記録します。元のセッションの帯は「引き継ぎ済み」に変わり、パネルに「元 370k → 初回 52k(予測 70k)」のように出ます

新しいセッションの最初の文脈の実測は、次からの損益分岐の計算に使います。 下書きを入れた後にやめるときは、帯かパネルの [取り消す] を押します。

設定

設定既定内容
ttlMain1h主スレッドのキャッシュ書き込みを 5m と 1h のどちらで数えるか
ttlSub5mサブエージェントのキャッシュ書き込みを 5m と 1h のどちらで数えるか
freshCtx70000新しいセッションの最初の文脈の見込み(実測があればそちらを使う)
baseCtx40000どのセッションにも入る部分(システムプロンプトやツールの説明など)の見込み
handoffDir.claude/handoffs/引き継ぎ文を書くフォルダ(プロジェクトからの相対パス)
exportDir.claude/usage-ledger/[書き出す] で集計を書くフォルダ(プロジェクトからの相対パス。絶対パスも使える)
showBandtrue入力欄の上の帯(目安のゲージ)を出すかどうか

設定は /config で変えます。例えば引き継ぎ文の置き場を notes/handoffs/ にするには、次のように入力します。

/config usage-ledger.handoffDir=notes/handoffs/

handoffDir は git で管理しないフォルダにしておくと、引き継ぎ文がコミットに混ざりません。

使い方

Claude Code v2.1.286 以降向けです。0.4.0 はデスクトップアプリ(v2.1.299・v2.1.304)で動作を確認しています。 インストール方法は、リポジトリの README を見てください。

token-ledger から移る

この mod は以前 token-ledger という名前でした。名前に token を含むパスへのアクセスを権限設定で絞っていると、同梱のスキルや書き出し先が読み書きできなくなるため、改名しました。 token-ledger を入れている場合は、外してから入れ直してください。

/plugin uninstall token-ledger@codingway-claude-mods
/plugin install usage-ledger@codingway-claude-mods

/config で設定を変えていた場合は、usage-ledger.<設定名> で設定し直してください。書き出し先の既定は .claude/usage-ledger/ に変わりました。.gitignore に .claude/token-ledger/ を足していた場合は、.claude/usage-ledger/ も足してください。目印も [usage-ledger handoff <id>] に変わったため、改名前に作った引き継ぎの下書きは記録されません。

Source 10 files
hooks/register.js 299 lines
1// Shows where the session's tokens go and when a fresh session would be cheaper.
2//
3// register.js  the events: each model request (turn.step) into $.state, which subagent is
4//              which ($.agent.list, the Agent tool's result), when to read threads back from
5//              their transcripts (turn.complete), the
6//              handoff marker in a new session's first prompt, the timers, /usage-ledger's
7//              registration
8// backfill.js  reading a thread's requests back from its transcript
9// pane.js      the pane (one, with views), /usage-ledger, the band above the prompt,
10//              [引き継ぐ…] and [書き出す]
11// export.js    the two files [書き出す] writes, a .md summary and a .jsonl of the requests (pure)
12// meter.js     the gauge line `トークン ■■■□□ OK`, drawn as usage-band draws its meters (pure)
13// aggregate.js the aggregation and the break-even (pure; also used outside the mod)
14// ledger.js    options, the gauge's stage and advice, formatting, threads, the handoff's draft
15//              and marker (pure)
16// style.js     the shared look (pure)
17// press-guard.js  runs a pane press again that did not reach its Button (pure)
18//
19// The transcripts are the source of truth. turn.step's live values are provisional ("速報値"):
20// a subagent's are replaced by its transcript when its run ends, the main thread's at the end of
21// each main turn and before [書き出す], which also brings in the main requests made before the
22// module loaded. The transcripts are found where Claude Code keeps them (backfill.js). Where the
23// main one is not found, is over 4 MiB or is unreadable, counting starts when the module first
24// loads ("集計開始") and the main thread stays live, labelled as such.
25//
26// No hook here is on a classic.* event: where a built-in plugin sits outermost (an organisation
27// manages Claude Code) those are not passed to installed mods.
28
29import { normalizeStep, contextOf } from './aggregate.js'
30import {
31  HANDOFF_MARK,
32  handoffKey,
33  setConfig,
34  getConfig,
35  setObserved,
36  getObserved,
37  addLive,
38  summarize,
39  assumed,
40} from './ledger.js'
41import { registerPane } from './pane.js'
42import { findMainTranscript, findSubagentTranscript, backfillSubagent, backfillMain } from './backfill.js'
43import { update } from 'claude-code'
44
45// The state this file writes (declared in types/index.d.ts)
46const REQUESTS = { plugin: 'usage-ledger', key: 'requests' }
47const THREADS = { plugin: 'usage-ledger', key: 'threads' }
48const STARTED_AT = { plugin: 'usage-ledger', key: 'startedAt' }
49const HANDOFF = { plugin: 'usage-ledger', key: 'handoff' }
50const INCOMING = { plugin: 'usage-ledger', key: 'incoming' }
51
52// The $.store key of the context sizes observed in earlier sessions: { fresh, base, at }
53const OBSERVED_KEY = 'observed'
54// How many of a handed-off session's first requests are reported back
55const OBSERVE_FIRST = 3
56// How often a drafted handoff looks in the store for the new session's report
57const WATCH_MS = 60_000
58// The cache's last ten minutes, when the band starts showing while idle
59const WARN_BEFORE_MS = 10 * 60_000
60
61// Subagent ids already looked up in $.agent.list(), so each is looked up once
62const looked = new Set()
63// True when this session had no turn when the module loaded: its first request is the base
64let isFreshSession = false
65// The redraw at the cache's next edge (ten minutes left, expired), and the handoff watcher
66let edgeTimer = null
67let watcher = null
68
69export function register(on, options) {
70  setConfig(options)
71
72  // Fires on the session's start, and again after a hot reload (which drops the timers)
73  on('session.start', async ($, e, next) => {
74    await $.command.register({ name: 'usage-ledger', description: 'このセッションのトークンの内訳を開く' })
75    const now = await $.clock.now()
76    const started = await $.state.get(STARTED_AT)
77    if (typeof started.value !== 'number') await $.state.set(STARTED_AT, now)
78    try {
79      isFreshSession = (await $.session.turns()) === 0
80    } catch {
81      isFreshSession = false
82    }
83    try {
84      setObserved(await $.store.get(OBSERVED_KEY))
85    } catch {}
86    try {
87      await mapAgents($)
88    } catch {}
89    // Where the main transcript is (or will be, for a new session), so the pane can say so
90    try {
91      await findMainTranscript($)
92    } catch {}
93    const { value: requests } = await $.state.get(REQUESTS)
94    scheduleEdges($, requests ?? [], now)
95    watcher?.cancel()
96    watcher = $.clock.every(WATCH_MS, () => void watchHandoff($))
97    return next(e)
98  })
99
100  // One model request, of the main thread or a subagent; its result goes on unchanged
101  on('turn.step', async function* ($, e, next) {
102    let sentAt = null
103    try {
104      sentAt = await $.clock.now()
105    } catch {}
106    const result = yield* next(e)
107    try {
108      await recordStep($, e, result, sentAt)
109    } catch {}
110    return result
111  })
112
113  // A subagent's run starts: which one it is, from $.agent.list() (once per subagent)
114  on('turn.start', async ($, e, next) => {
115    try {
116      if (typeof e.agentId === 'string' && !looked.has(e.agentId)) {
117        looked.add(e.agentId)
118        await mapAgents($)
119      }
120    } catch {}
121    return next(e)
122  })
123
124  // A turn ended. On the main loop: the main thread's requests read back from its transcript.
125  // A subagent's run: its transcript has every request with the exact cache-write split, so its
126  // live tally is replaced by it (left as it is when the file is not there or too large)
127  on('turn.complete', async ($, e, next) => {
128    const result = await next(e)
129    try {
130      if (e.agentId === undefined) await readBackMain($)
131      else {
132        await mapAgents($)
133        const path = await findSubagentTranscript($, e.agentId)
134        if (path !== null) await backfillSubagent($, e.agentId, path)
135      }
136    } catch {}
137    return result
138  })
139
140  // The Agent tool: its description and type, by the agentId its result names
141  on('tool.call', { tool: 'Agent' }, async ($, e, next) => {
142    const result = await next(e)
143    try {
144      const agentId = result?.result?.agentId
145      if (typeof agentId === 'string') {
146        await noteThread($, agentId, {
147          agentType: e.subagent_type ?? result.result.agentType,
148          description: typeof e.description === 'string' ? e.description : undefined,
149        })
150      }
151    } catch {}
152    return result
153  })
154
155  // A prompt carrying the handoff marker: this session was started from a handoff (unless it
156  // is the session that drafted it, sending the draft)
157  on('prompt.submit', async ($, e, next) => {
158    try {
159      const match = HANDOFF_MARK.exec(e.text ?? '')
160      if (match) await noteIncoming($, match[1])
161    } catch {}
162    return next(e)
163  })
164
165  // The pane, /usage-ledger and the band
166  registerPane(on)
167}
168
169// ---- The live tally
170
171async function recordStep($, e, result, sentAt) {
172  const now = await $.clock.now()
173  const step = normalizeStep(e, result, now, getConfig())
174  if (!step) return
175  // When it was sent: a read-back keeps the live requests its transcript does not have yet
176  const request = typeof sentAt === 'number' ? { ...step, sentAt } : step
177  await update($, REQUESTS, (list) => addLive(list, request))
178  if (request.thread !== 'main') {
179    if (!looked.has(request.thread)) {
180      looked.add(request.thread)
181      await mapAgents($)
182    }
183    return
184  }
185  const { value: requests } = await $.state.get(REQUESTS)
186  scheduleEdges($, requests ?? [], now)
187  await observeMain($, contextOf(request.tok))
188}
189
190// The context of a main request, as an observation: the first of a fresh session is the base
191// every session starts with; the first three of a handed-off session go back to the handoff's
192// record, the first of them as the size a fresh session starts at
193async function observeMain($, context) {
194  const { value: incoming } = await $.state.get(INCOMING)
195  if (incoming && incoming.observed.length < OBSERVE_FIRST) {
196    const observed = [...incoming.observed, context]
197    await $.state.set(INCOMING, { ...incoming, observed })
198    const record = await $.store.get(handoffKey(incoming.id))
199    if (record && typeof record === 'object') await $.store.set(handoffKey(incoming.id), { ...record, observed })
200    if (observed.length === 1) await saveObserved($, { fresh: context })
201    isFreshSession = false
202    return
203  }
204  // A first request larger than a fresh session's assumed start opened with a long prompt
205  // (or is no fresh session at all): it says nothing about the base
206  if (isFreshSession) {
207    isFreshSession = false
208    if (context < assumed().freshCtx) await saveObserved($, { base: context })
209  }
210}
211
212async function saveObserved($, patch) {
213  const now = await $.clock.now()
214  const value = { ...getObserved(), ...patch, at: now }
215  setObserved(value)
216  await $.store.set(OBSERVED_KEY, value)
217}
218
219// Redraws the band and the pane when the main cache reaches its last ten minutes and when it
220// expires, since what they show changes then without a new request. Nothing else redraws on a
221// timer: in the desktop app every redraw rebuilds every mod's drawing, other mods' open panes
222// included
223function scheduleEdges($, requests, now) {
224  edgeTimer?.cancel()
225  edgeTimer = null
226  const s = summarize(requests, now)
227  if (s.expiresAt == null || s.isExpired) return
228  const warnAt = s.expiresAt - WARN_BEFORE_MS
229  const at = now < warnAt ? warnAt : s.expiresAt
230  const own = $.clock.after(Math.max(0, at - now), async () => {
231    if (edgeTimer === own) edgeTimer = null
232    $.ui.invalidate('ui.render')
233    const { value } = await $.state.get(REQUESTS)
234    scheduleEdges($, value ?? [], await $.clock.now())
235  })
236  edgeTimer = own
237}
238
239// ---- Which subagent is which
240
241async function mapAgents($) {
242  const agents = await $.agent.list()
243  if (!Array.isArray(agents) || agents.length === 0) return
244  await update($, THREADS, (threads) => {
245    const next = { ...(threads ?? {}) }
246    for (const a of agents) {
247      looked.add(a.id)
248      next[a.id] = { ...next[a.id], agentType: next[a.id]?.agentType ?? a.type, description: next[a.id]?.description ?? a.description }
249    }
250    return next
251  })
252}
253
254async function noteThread($, agentId, patch) {
255  if (typeof agentId !== 'string' || agentId === '') return
256  const clean = Object.fromEntries(Object.entries(patch).filter(([, v]) => typeof v === 'string' && v !== ''))
257  await update($, THREADS, (threads) => ({ ...(threads ?? {}), [agentId]: { ...(threads ?? {})[agentId], ...clean } }))
258}
259
260// The main thread read back; the cache's edges follow its last request's time
261async function readBackMain($) {
262  if (!(await backfillMain($))) return
263  const { value: requests } = await $.state.get(REQUESTS)
264  scheduleEdges($, requests ?? [], await $.clock.now())
265}
266
267// ---- Handoff
268
269async function noteIncoming($, id) {
270  const { value: current } = await $.state.get(INCOMING)
271  if (current) return
272  const record = await $.store.get(handoffKey(id))
273  const self = await $.session.id()
274  if (record && typeof record === 'object' && record.fromSession === self) return
275  const { value: drafted } = await $.state.get(HANDOFF)
276  if (drafted?.id === id) return
277  const known = record && typeof record === 'object'
278  await $.state.set(INCOMING, {
279    id,
280    fromSession: known && typeof record.fromSession === 'string' ? record.fromSession : null,
281    ctxAtHandoff: known && typeof record.ctxAtHandoff === 'number' ? record.ctxAtHandoff : null,
282    predictedCtx: known && typeof record.predictedCtx === 'number' ? record.predictedCtx : null,
283    observed: [],
284  })
285}
286
287// A drafted handoff turns 'done' once the new session stored its first context; its later
288// observations are picked up until there are OBSERVE_FIRST
289async function watchHandoff($) {
290  try {
291    const { value: handoff } = await $.state.get(HANDOFF)
292    if (!handoff || handoff.observed.length >= OBSERVE_FIRST) return
293    const record = await $.store.get(handoffKey(handoff.id))
294    const observed = Array.isArray(record?.observed) ? record.observed.filter((n) => typeof n === 'number') : []
295    if (observed.length <= handoff.observed.length) return
296    await $.state.set(HANDOFF, { ...handoff, status: 'done', observed })
297  } catch {}
298}
299
hooks/aggregate.js 414 lines
1// Pure token-usage aggregation for one Claude Code session.
2//
3// No Node, fs, process or Date: input is already-parsed transcript records
4// (or the mod's live tally), output is a plain JSON-serialisable summary.
5// Timestamps are read and written by the ISO helpers below, so the module
6// runs the same in Node and in a mod's environment. Deterministic, so a mod
7// can import it and feed it either transcript records or its own live tally
8// (see aggregateThreads, which takes normalized requests directly).
9//
10// Normalized request: { id, ts (epoch ms), model, tools: [name],
11//   tok: { input, cache_read, cache_write_5m, cache_write_1h, output } }
12
13export const TYPES = ['input', 'cache_read', 'cache_write_5m', 'cache_write_1h', 'output'];
14
15// ---- ISO 8601 timestamps without Date (civil-date arithmetic, proleptic Gregorian)
16
17const DAY_MS = 86400000;
18const ISO_RE = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|[+-]\d{2}:?\d{2})$/;
19
20function daysFromCivil(y, m, d) {
21  y -= m <= 2 ? 1 : 0;
22  const era = Math.floor(y / 400), yoe = y - era * 400;
23  const doy = Math.floor((153 * (m + (m > 2 ? -3 : 9)) + 2) / 5) + d - 1;
24  return era * 146097 + yoe * 365 + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy - 719468;
25}
26
27function civilFromDays(z) {
28  z += 719468;
29  const era = Math.floor(z / 146097), doe = z - era * 146097;
30  const yoe = Math.floor((doe - Math.floor(doe / 1460) + Math.floor(doe / 36524) - Math.floor(doe / 146096)) / 365);
31  const doy = doe - (365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100));
32  const mp = Math.floor((5 * doy + 2) / 153);
33  const m = mp + (mp < 10 ? 3 : -9);
34  return [yoe + era * 400 + (m <= 2 ? 1 : 0), m, doy - Math.floor((153 * mp + 2) / 5) + 1];
35}
36
37/** Epoch ms of an ISO timestamp with a zone (`...Z`, `+09:00`); NaN for anything else. */
38export function parseIso(s) {
39  const t = ISO_RE.exec(String(s ?? ''));
40  if (!t) return NaN;
41  const ms = t[7] ? Number((t[7] + '00').slice(0, 3)) : 0;
42  let v = daysFromCivil(+t[1], +t[2], +t[3]) * DAY_MS + ((+t[4] * 60 + +t[5]) * 60 + (+t[6] || 0)) * 1000 + ms;
43  if (t[8] !== 'Z') {
44    const z = t[8].replace(':', '');
45    v -= (z[0] === '-' ? -1 : 1) * (Number(z.slice(1, 3)) * 60 + Number(z.slice(3, 5))) * 60000;
46  }
47  return v;
48}
49
50/** `YYYY-MM-DDTHH:MM:SS.sssZ` for epoch ms, as Date#toISOString writes it (4-digit years). */
51export function isoString(ms) {
52  if (!Number.isFinite(ms)) return 'Invalid';
53  const days = Math.floor(ms / DAY_MS), rest = ms - days * DAY_MS;
54  const [y, m, d] = civilFromDays(days);
55  const p = (n, w = 2) => String(n).padStart(w, '0');
56  return `${p(y, 4)}-${p(m)}-${p(d)}T${p(Math.floor(rest / 3600000))}:${p(Math.floor(rest / 60000) % 60)}:` +
57    `${p(Math.floor(rest / 1000) % 60)}.${p(rest % 1000, 3)}Z`;
58}
59
60/** `HH:MM` of epoch ms shifted by `tzOffsetMinutes` (local time when that is the local offset). */
61export function clockOf(ms, tzOffsetMinutes = 0) {
62  return isoString(ms + tzOffsetMinutes * 60000).slice(11, 16);
63}
64
65// Rough "limit weight" (assumption, NOT the subscription formula, which is
66// not public). Token-type ratios follow public API prices relative to an
67// uncached input token; model ratios follow public input prices per MTok
68// (Haiku 4.5 $1, Sonnet 4.5 $3, Opus 4.5 $5), unverified for newer models.
69export const DEFAULT_WEIGHTS = {
70  type: { input: 1, cache_read: 0.1, cache_write_5m: 1.25, cache_write_1h: 2, output: 5 },
71  model: [['haiku', 1], ['sonnet', 3], ['opus', 5]],
72  modelFallback: 5,
73};
74
75/** The model ratio of DEFAULT_WEIGHTS (or `W`) for a model id: the first name it contains. */
76export function modelWeight(model, W = DEFAULT_WEIGHTS) {
77  const s = String(model || '').toLowerCase();
78  for (const [k, w] of W.model) if (s.includes(k)) return w;
79  return W.modelFallback;
80}
81
82const zero = () => ({ input: 0, cache_read: 0, cache_write_5m: 0, cache_write_1h: 0, output: 0 });
83const addTok = (acc, t) => { for (const k of TYPES) acc[k] += t[k] || 0; return acc; };
84const ctxOf = (t) => t.input + t.cache_read + t.cache_write_5m + t.cache_write_1h;
85const writeOf = (t) => t.cache_write_5m + t.cache_write_1h;
86const median = (xs) => {
87  if (!xs.length) return 0;
88  const s = [...xs].sort((a, b) => a - b), m = s.length >> 1;
89  return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
90};
91const basename = (p) => String(p).replace(/\\/g, '/').split('/').pop();
92
93export function usageToTok(u) {
94  const cc = u.cache_creation || {};
95  let w5 = cc.ephemeral_5m_input_tokens || 0;
96  const w1 = cc.ephemeral_1h_input_tokens || 0;
97  const total = u.cache_creation_input_tokens || 0;
98  if (w5 + w1 < total) w5 += total - w5 - w1; // no TTL split reported: count as 5m
99  return { input: u.input_tokens || 0, cache_read: u.cache_read_input_tokens || 0,
100    cache_write_5m: w5, cache_write_1h: w1, output: u.output_tokens || 0 };
101}
102
103// One thread's transcript records -> requests, tool results, skills, errors.
104// Streamed responses repeat one message id over several records; the copy
105// with the largest output_tokens carries the final counts.
106export function parseThread(records) {
107  const reqs = new Map();
108  const toolName = new Map(), readTarget = new Map();
109  const results = [], errors = [], skills = {};
110  for (const r of records) {
111    if (!r || typeof r !== 'object') continue;
112    if (r.type === 'assistant') {
113      const m = r.message || {};
114      if (r.isApiErrorMessage) {
115        errors.push({ ts: parseIso(r.timestamp), status: r.apiErrorStatus ?? null,
116          limit: (r.quotaLimits && r.quotaLimits.rateLimitType) || null });
117      }
118      if (m.model === '<synthetic>' || !m.usage) continue;
119      const id = m.id || r.requestId || r.uuid;
120      const tok = usageToTok(m.usage);
121      let q = reqs.get(id);
122      if (!q) {
123        q = { id, ts: parseIso(r.timestamp), model: m.model || '?', tools: [], tok, stop: m.stop_reason || null };
124        reqs.set(id, q);
125      } else if (tok.output >= q.tok.output) {
126        q.tok = tok;
127        q.stop = m.stop_reason || q.stop;
128      }
129      for (const b of m.content || []) {
130        if (b && b.type === 'tool_use') {
131          const name = b.name || '?', inp = b.input || {};
132          toolName.set(b.id, name);
133          q.tools.push(name);
134          if (name === 'Skill') skills[String(inp.skill)] = (skills[String(inp.skill)] || 0) + 1;
135          if (name === 'Read' && inp.file_path) readTarget.set(b.id, basename(inp.file_path));
136        }
137      }
138    } else if (r.type === 'user') {
139      const c = r.message && r.message.content;
140      if (!Array.isArray(c)) continue;
141      for (const b of c) {
142        if (!b || b.type !== 'tool_result') continue;
143        const ct = b.content;
144        const chars = Array.isArray(ct)
145          ? ct.reduce((n, x) => n + ((x && x.text) ? x.text.length : 0), 0)
146          : (ct || '').length;
147        results.push({ tool: toolName.get(b.tool_use_id) || '?', target: readTarget.get(b.tool_use_id) || '', chars });
148      }
149    }
150  }
151  const requests = [...reqs.values()].sort((a, b) => a.ts - b.ts);
152  return { requests, results, skills, errors };
153}
154
155// input: { main: records[], subagents: [{ id, meta, records }] }
156export function aggregate(input, opts = {}) {
157  const threads = [{ key: 'main', label: 'main', agentType: '(main)', description: '', ...parseThread(input.main || []) }];
158  for (const s of input.subagents || []) {
159    const meta = s.meta || {};
160    threads.push({ key: s.id, label: String(s.id).replace(/^agent-/, '').slice(0, 8),
161      agentType: meta.agentType || '?', description: meta.description || '', toolUseId: meta.toolUseId || null,
162      ...parseThread(s.records || []) });
163  }
164  return aggregateThreads(threads, opts);
165}
166
167// threads: [{ key, label, agentType, description, requests, results?, skills?, errors? }]
168// opts: { weights, top, tzOffsetMinutes (for hour buckets), gapSeconds }
169export function aggregateThreads(threads, opts = {}) {
170  const W = opts.weights || DEFAULT_WEIGHTS;
171  const top = opts.top ?? 15;
172  const tzOff = (opts.tzOffsetMinutes || 0) * 60000;
173  const gapMin = opts.gapSeconds ?? 300;
174  const modelW = (m) => modelWeight(m, W);
175  const weighOf = (tok, model) => modelW(model) * TYPES.reduce((n, t) => n + W.type[t] * (tok[t] || 0), 0);
176  const writeWeigh = (tok, model) => modelW(model) * (W.type.cache_write_5m * tok.cache_write_5m + W.type.cache_write_1h * tok.cache_write_1h);
177  const hourKey = (ts) => isoString(ts + tzOff).slice(0, 13); // shifted ISO, "YYYY-MM-DDTHH"
178
179  const all = [];
180  for (const t of threads) for (const q of t.requests) all.push({ ...q, thread: t.key });
181  const sumUp = (reqs) => {
182    const tok = zero(); let weighted = 0;
183    for (const q of reqs) { addTok(tok, q.tok); weighted += weighOf(q.tok, q.model); }
184    return { requests: reqs.length, tok, weighted };
185  };
186  const total = sumUp(all);
187  const ctxTotal = ctxOf(total.tok);
188  const byType = TYPES.map((t) => ({ type: t, tokens: total.tok[t],
189    weighted: all.reduce((n, q) => n + modelW(q.model) * W.type[t] * q.tok[t], 0) }));
190
191  const threadRows = threads.map((t) => ({
192    key: t.key, label: t.label, agentType: t.agentType, description: t.description,
193    models: [...new Set(t.requests.map((q) => q.model))].sort(),
194    start: t.requests.length ? t.requests[0].ts : null,
195    end: t.requests.length ? t.requests[t.requests.length - 1].ts : null,
196    firstContext: t.requests.length ? ctxOf(t.requests[0].tok) : 0,
197    lastContext: t.requests.length ? ctxOf(t.requests[t.requests.length - 1].tok) : 0,
198    ...sumUp(t.requests),
199  })).sort((a, b) => b.weighted - a.weighted);
200
201  const group = (rows, keyFn) => {
202    const m = new Map();
203    for (const r of rows) {
204      const k = keyFn(r);
205      const e = m.get(k) || { key: k, runs: 0, requests: 0, tok: zero(), weighted: 0 };
206      e.runs += 1; e.requests += r.requests; addTok(e.tok, r.tok); e.weighted += r.weighted;
207      m.set(k, e);
208    }
209    return [...m.values()].sort((a, b) => b.weighted - a.weighted);
210  };
211  const byAgentType = group(threadRows, (r) => r.agentType);
212  const byModel = group(all.map((q) => ({ requests: 1, tok: q.tok, weighted: weighOf(q.tok, q.model), model: q.model })), (r) => r.model)
213    .map(({ runs, ...e }) => e);
214
215  const main = (threads.find((t) => t.key === 'main') || { requests: [] }).requests;
216  const mainWeighted = main.reduce((n, q) => n + weighOf(q.tok, q.model), 0);
217  const byTool = group(main.map((q) => ({ requests: 1, tok: q.tok, weighted: weighOf(q.tok, q.model),
218    tool: q.tools.length ? [...new Set(q.tools)].sort().join('+') : 'text' })), (r) => r.tool).map(({ runs, ...e }) => e);
219
220  const biggest = [...all].sort((a, b) => weighOf(b.tok, b.model) - weighOf(a.tok, a.model)).slice(0, top)
221    .map((q) => ({ ts: q.ts, thread: q.thread, model: q.model, context: ctxOf(q.tok), write: writeOf(q.tok),
222      output: q.tok.output, weighted: weighOf(q.tok, q.model), tools: q.tools }));
223
224  const toolResults = new Map(), reads = new Map(), skills = {};
225  for (const t of threads) {
226    const where = t.key === 'main' ? 'main' : 'subagents';
227    for (const r of t.results || []) {
228      const k = where + '\u0000' + r.tool;
229      const e = toolResults.get(k) || { where, tool: r.tool, calls: 0, chars: 0 };
230      e.calls += 1; e.chars += r.chars; toolResults.set(k, e);
231      if (r.tool === 'Read' && r.target) {
232        const f = reads.get(r.target) || { file: r.target, reads: 0, chars: 0, threads: new Set() };
233        f.reads += 1; f.chars += r.chars; f.threads.add(t.key); reads.set(r.target, f);
234      }
235    }
236    for (const [k, v] of Object.entries(t.skills || {})) {
237      const kk = (t.key === 'main' ? 'main:' : 'sub:') + k;
238      skills[kk] = (skills[kk] || 0) + v;
239    }
240  }
241
242  const hours = new Map();
243  for (const q of [...all].sort((a, b) => a.ts - b.ts)) {
244    const k = hourKey(q.ts);
245    const e = hours.get(k) || { hour: k, requests: 0, cache_read: 0, cache_write: 0, output: 0, weighted: 0 };
246    e.requests += 1; e.cache_read += q.tok.cache_read; e.cache_write += writeOf(q.tok);
247    e.output += q.tok.output; e.weighted += weighOf(q.tok, q.model);
248    hours.set(k, e);
249  }
250
251  // Gaps: TTL follows what the previous request of the same thread wrote.
252  const gaps = [];
253  const tax = { expired: { requests: 0, write: 0, weighted: 0 }, within: { requests: 0, write: 0, weighted: 0 } };
254  for (const t of threads) {
255    let ttl = 300;
256    for (let i = 1; i < t.requests.length; i++) {
257      const prev = t.requests[i - 1], cur = t.requests[i];
258      if (prev.tok.cache_write_1h) ttl = 3600; else if (prev.tok.cache_write_5m) ttl = 300;
259      const gap = (cur.ts - prev.ts) / 1000;
260      if (gap <= gapMin) continue;
261      const state = gap > ttl ? 'expired' : 'within';
262      const ww = writeWeigh(cur.tok, cur.model);
263      gaps.push({ ts: cur.ts, thread: t.key, gapSeconds: gap, ttlSeconds: ttl, context: ctxOf(cur.tok),
264        write: writeOf(cur.tok), cacheRead: cur.tok.cache_read, weighted: ww, state });
265      tax[state].requests += 1; tax[state].write += writeOf(cur.tok); tax[state].weighted += ww;
266    }
267  }
268  gaps.sort((a, b) => a.ts - b.ts);
269  const writeWeightTotal = all.reduce((n, q) => n + writeWeigh(q.tok, q.model), 0);
270
271  const ctxs = main.map((q) => ctxOf(q.tok));
272  const drops = [];
273  for (let i = 1; i < ctxs.length; i++) if (ctxs[i] < 0.6 * ctxs[i - 1]) drops.push({ ts: main[i].ts, from: ctxs[i - 1], to: ctxs[i] });
274  const ctxHours = new Map();
275  for (const q of main) {
276    const k = hourKey(q.ts);
277    const e = ctxHours.get(k) || [];
278    e.push(ctxOf(q.tok)); ctxHours.set(k, e);
279  }
280  const buckets = [[0, 50e3], [50e3, 100e3], [100e3, 150e3], [150e3, 200e3], [200e3, Infinity]]
281    .map(([lo, hi]) => ({ from: lo, to: hi === Infinity ? null : hi, requests: ctxs.filter((c) => c >= lo && c < hi).length }));
282
283  return {
284    weights: W,
285    span: all.length ? { start: Math.min(...all.map((q) => q.ts)), end: Math.max(...all.map((q) => q.ts)) } : null,
286    threadCount: threads.length,
287    total: { ...total, all: ctxTotal + total.tok.output, cacheHitRatio: ctxTotal ? total.tok.cache_read / ctxTotal : 0 },
288    byType,
289    errors: threads.flatMap((t) => (t.errors || []).map((e) => ({ ...e, thread: t.key }))).sort((a, b) => a.ts - b.ts),
290    threads: threadRows,
291    byAgentType,
292    byModel,
293    main: { weighted: mainWeighted, byTool },
294    biggest,
295    toolResults: [...toolResults.values()].sort((a, b) => b.chars - a.chars),
296    reads: [...reads.values()].map((f) => ({ file: f.file, reads: f.reads, chars: f.chars, threads: f.threads.size }))
297      .sort((a, b) => b.chars - a.chars),
298    skills,
299    cacheByHour: [...hours.values()],
300    gaps,
301    gapTax: { ...tax, writeWeightTotal },
302    context: {
303      requests: ctxs.length,
304      min: ctxs.length ? Math.min(...ctxs) : 0, max: ctxs.length ? Math.max(...ctxs) : 0,
305      median: median(ctxs), mean: ctxs.length ? ctxs.reduce((a, b) => a + b, 0) / ctxs.length : 0,
306      sum: ctxs.reduce((a, b) => a + b, 0),
307      drops, buckets,
308      byHour: [...ctxHours.entries()].map(([hour, v]) => ({ hour, requests: v.length, min: Math.min(...v),
309        median: median(v), max: Math.max(...v), sum: v.reduce((a, b) => a + b, 0) })),
310    },
311  };
312}
313
314// ---- Live use (usage-ledger): one turn.step result, the break-even, the band's summary
315
316const TTL_MS = { '5m': 300000, '1h': 3600000 };
317/** Context at or above this is "heavy": the pane's overview marks it. */
318export const HEAVY_CONTEXT = 300000;
319/** The band warns while idle once the main cache has this much or less left. */
320export const IDLE_WARN_MS = 600000;
321/** Output tokens assumed for the one turn that writes the handoff. */
322export const HANDOFF_OUTPUT = 2000;
323
324const ctxOfTok = (t) => (t.input || 0) + (t.cache_read || 0) + (t.cache_write_5m || 0) + (t.cache_write_1h || 0);
325export { ctxOfTok as contextOf };
326
327/**
328 * One turn.step as a normalized request, or null when the response carried no usage.
329 * `e` is the step's input (turnId, index, model, agentId?), `result` what next(e) returned,
330 * `now` the clock's ms. turn.step does not say which lifetime a cache write used, so the
331 * thread's assumed TTL decides: `ttlMain` for the main thread, `ttlSub` for a subagent.
332 */
333export function normalizeStep(e, result, now, opts = {}) {
334  const u = result && result.usage;
335  if (!u) return null;
336  const thread = e.agentId == null ? 'main' : String(e.agentId);
337  const ttl = thread === 'main' ? (opts.ttlMain || '1h') : (opts.ttlSub || '5m');
338  const write = u.cache_creation_input_tokens || 0;
339  return {
340    id: `${e.turnId}:${e.index}`,
341    ts: now,
342    model: u.model || e.model || '?',
343    tools: (result.toolUses || []).map((t) => String(t.name)),
344    tok: {
345      input: u.input_tokens || 0,
346      cache_read: u.cache_read_input_tokens || 0,
347      cache_write_5m: ttl === '1h' ? 0 : write,
348      cache_write_1h: ttl === '1h' ? write : 0,
349      output: u.output_tokens || 0,
350    },
351    thread,
352  };
353}
354
355/**
356 * When a fresh session pays for itself, in the weights' units (model ratio included).
357 *   r context now, b the base every session starts with (cached elsewhere), n a fresh
358 *   session's first context, m the model ratio, w the cache-write weight of `ttl`.
359 *   next    = 0.1·m·r            one more request on a live cache
360 *   rewrite = w·m·(r−b)          the next request once the cache has expired
361 *   saving  = 0.1·m·(r−n)        what each later request saves in a fresh session
362 *   fixed   = w·m·(n−b) + 0.1·m·r + 5·m·HANDOFF_OUTPUT   writing the fresh cache + the handoff turn
363 *   runs    = ⌈fixed/saving⌉, 0 once expired ("now"); null when r ≤ n (applies: false)
364 */
365export function breakEven({ context, base, fresh, model, ttl = '1h', isExpired = false, weights = DEFAULT_WEIGHTS }) {
366  const m = modelWeight(model, weights), T = weights.type;
367  const w = ttl === '5m' ? T.cache_write_5m : T.cache_write_1h;
368  const next = T.cache_read * m * context;
369  const rewrite = w * m * Math.max(0, context - base);
370  if (!(context > fresh)) return { applies: false, next, rewrite, saving: 0, fixed: 0, runs: null };
371  const saving = T.cache_read * m * (context - fresh);
372  const fixed = w * m * Math.max(0, fresh - base) + T.cache_read * m * context + T.output * m * HANDOFF_OUTPUT;
373  return { applies: true, next, rewrite, saving, fixed, runs: isExpired ? 0 : Math.ceil(fixed / saving) };
374}
375
376/**
377 * The smallest context at which breakEven's runs come to `runs` or fewer (on a live cache),
378 * solved from fixed ≤ runs·saving. The model ratio cancels out, so no model is needed:
379 *   w·(n−b) + 0.1·r + 5·HANDOFF_OUTPUT ≤ runs·0.1·(r−n)
380 *   r ≥ (w·(n−b) + 5·HANDOFF_OUTPUT + runs·0.1·n) / ((runs−1)·0.1)
381 * Infinity for runs ≤ 1 (the handoff turn alone costs a request's read).
382 */
383export function contextForRuns({ runs = 5, base, fresh, ttl = '1h', weights = DEFAULT_WEIGHTS }) {
384  if (!(runs > 1)) return Infinity;
385  const T = weights.type;
386  const w = ttl === '5m' ? T.cache_write_5m : T.cache_write_1h;
387  const c = T.cache_read;
388  return (w * Math.max(0, fresh - base) + T.output * HANDOFF_OUTPUT + runs * c * fresh) / ((runs - 1) * c);
389}
390
391/**
392 * The main thread's state the band and the pane draw from, from the live requests.
393 * opts: { now, ttlMain, freshCtx, baseCtx, handoff ('drafted' | 'done' | undefined) }
394 */
395export function summaryForBand(requests, opts = {}) {
396  let last = null;
397  for (const q of requests) if (q.thread === 'main' && (!last || q.ts >= last.ts)) last = q;
398  const ttlMs = TTL_MS[opts.ttlMain] || TTL_MS['1h'];
399  const out = { context: null, model: null, lastMainAt: null, expiresAt: null, remainingMs: null,
400    isExpired: false, isHeavy: false, breakEven: null, handoff: opts.handoff || null };
401  if (!last) return out;
402  const now = opts.now ?? last.ts;
403  out.context = ctxOfTok(last.tok);
404  out.model = last.model;
405  out.lastMainAt = last.ts;
406  out.expiresAt = last.ts + ttlMs;
407  out.remainingMs = Math.max(0, out.expiresAt - now);
408  out.isExpired = now >= out.expiresAt;
409  out.isHeavy = out.context >= HEAVY_CONTEXT;
410  out.breakEven = breakEven({ context: out.context, base: opts.baseCtx ?? 40000, fresh: opts.freshCtx ?? 70000,
411    model: last.model, ttl: opts.ttlMain || '1h', isExpired: out.isExpired });
412  return out;
413}
414
hooks/ledger.js 392 lines
1// The pure parts usage-ledger's hook files share: its options, the context sizes observed in
2// earlier sessions, the live requests turned into aggregate.js's threads, number and time
3// formatting, and the handoff's id, file name, draft and marker.
4//
5// Pure: no $ here, so the hook files keep every $ call themselves.
6
7import { summaryForBand, contextForRuns, IDLE_WARN_MS, isoString, clockOf } from './aggregate.js'
8
9/** The pane's id ($.ui.open, the Pane matcher) and the slash command's name. */
10export const PANE = 'usage-ledger'
11
12/** At most this many requests are kept in $.state; the oldest go first. */
13export const MAX_REQUESTS = 5000
14
15/** A transcript (main or subagent) larger than this is not read back ($.fs.read refuses over 4 MiB). */
16export const MAX_TRANSCRIPT_BYTES = 4 * 1024 * 1024
17
18/**
19 * The folder of `<config>/projects/` Claude Code keeps a session's transcripts in, named after
20 * the session's first working directory: every character but an ASCII letter or digit as `-`
21 * (`/home/me/app` -> `-home-me-app`, `C:\\work\\app` -> `C--work-app`).
22 */
23export const projectFolderOf = (dir) => String(dir).replace(/[^a-zA-Z0-9]/g, '-')
24
25/** Path parts joined with `/` (accepted on Windows too), no separator doubled. */
26export const joinPath = (...parts) => parts.map((p, i) => (i === 0 ? String(p).replace(/[\\/]+$/, '') : String(p).replace(/^[\\/]+|[\\/]+$/g, ''))).join('/')
27
28/** A subagent's transcript, beside the main one: `<main without .jsonl>/subagents/agent-<agentId>.jsonl`. */
29export const subagentTranscriptOf = (main, agentId) => joinPath(String(main).replace(/\.jsonl$/, ''), 'subagents', `agent-${agentId}.jsonl`)
30
31/** Always shown beside a weighted figure. */
32export const ESTIMATE_NOTE = '*重みは推計。サブスクの計算式は非公開'
33
34/** How the weights are assumed, for the pane. */
35export const WEIGHTS_NOTE =
36  '公開 API の価格比で推計: 入力 1、キャッシュ読み 0.1、書き 5m 1.25、書き 1h 2、出力 5。モデル比 haiku 1、sonnet 3、opus 5'
37
38/** The pane's views, in the order of its navigation. */
39export const VIEWS = [
40  { id: 'status', label: '状況' },
41  { id: 'overview', label: '概要' },
42  { id: 'threads', label: 'スレッド' },
43  { id: 'kinds', label: '種類・モデル' },
44  { id: 'costly', label: '高い要求' },
45  { id: 'gaps', label: '待ちと再書込' },
46  { id: 'tools', label: '主のツール' },
47  { id: 'handoff', label: '引き継ぎ' },
48]
49
50const DEFAULTS = { ttlMain: '1h', ttlSub: '5m', freshCtx: 70000, baseCtx: 40000, handoffDir: '.claude/handoffs/', exportDir: '.claude/usage-ledger/', showBand: true }
51
52// The options register() received, defaults filled in
53let config = { ...DEFAULTS }
54// Context sizes observed in earlier sessions ($.store 'observed'): { fresh, base } or nulls
55let observed = { fresh: null, base: null }
56
57/** Takes register()'s options; a value of the wrong type keeps the default. */
58export function setConfig(options = {}) {
59  const pick = (key, ok) => (ok(options?.[key]) ? options[key] : DEFAULTS[key])
60  const ttl = (v) => v === '1h' || v === '5m'
61  const count = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0
62  config = {
63    ttlMain: pick('ttlMain', ttl),
64    ttlSub: pick('ttlSub', ttl),
65    freshCtx: pick('freshCtx', count),
66    baseCtx: pick('baseCtx', count),
67    handoffDir: pick('handoffDir', (v) => typeof v === 'string' && v.trim() !== ''),
68    exportDir: pick('exportDir', (v) => typeof v === 'string' && v.trim() !== ''),
69    showBand: pick('showBand', (v) => typeof v === 'boolean'),
70  }
71  return config
72}
73
74export function getConfig() {
75  return config
76}
77
78/** Takes the store's 'observed' value (anything else is ignored). */
79export function setObserved(value) {
80  const n = (v) => (typeof v === 'number' && v > 0 ? v : null)
81  observed = { fresh: n(value?.fresh), base: n(value?.base) }
82}
83
84export function getObserved() {
85  return observed
86}
87
88/**
89 * The fresh-session and base context the break-even assumes: observed first, else the options;
90 * the base never above the fresh size.
91 */
92export function assumed() {
93  const freshCtx = observed.fresh ?? config.freshCtx
94  return { freshCtx, baseCtx: Math.min(observed.base ?? config.baseCtx, freshCtx) }
95}
96
97/** summaryForBand with the options and observations filled in. */
98export function summarize(requests, now, { handoff } = {}) {
99  return summaryForBand(requests ?? [], { now, ttlMain: config.ttlMain, ...assumed(), handoff })
100}
101
102// ---- The gauge: how long the session can go on before a handoff pays
103
104/** The gauge is full where a fresh session pays back within this many requests. */
105export const FULL_RUNS = 5
106/** At or above this percentage of the gauge, the stage is 'soon'. */
107export const SOON_PERCENT = 60
108
109/** The stages' words, for the band and the pane. */
110export const STAGE_LABEL = { ok: 'OK', soon: 'Soon', switch: 'Switch' }
111
112/**
113 * The band's short phrases, by gaugeOf's `advice`. The band stays one line; the pane's 状況
114 * view says the same in full sentences.
115 */
116export const ADVICE = {
117  soon: '区切りで引き継ぐと節約に',
118  switch: '今引き継ぐと節約に',
119  idle: '10 分操作がないと割高に',
120  expired: '続きは新しいセッションが安い',
121}
122
123/**
124 * Where the main context sits on the gauge, from summaryForBand's `s`.
125 * opts: { freshCtx, baseCtx, ttl, isWorking }
126 *   percent  0 at a fresh session's context, 100 at contextForRuns(FULL_RUNS), linear between;
127 *            100 once the cache expired (when a fresh session is smaller at all); null with no
128 *            main request yet
129 *   stage    'ok' below SOON_PERCENT, 'soon' below 100, 'switch' at 100
130 *   advice   which ADVICE phrase to show, or null: expired, else idle in the cache's last ten
131 *            minutes, else the stage's own ('soon', 'switch')
132 * A context no larger than a fresh session's stays at 0 and never advises: a fresh session
133 * would not be cheaper.
134 */
135export function gaugeOf(s, { freshCtx, baseCtx, ttl = '1h', isWorking = false }) {
136  const full = contextForRuns({ runs: FULL_RUNS, base: baseCtx, fresh: freshCtx, ttl })
137  if (s?.context == null) return { percent: null, stage: null, advice: null, fullAt: full }
138  const pays = s.context > freshCtx
139  let percent = pays ? Math.min(100, (100 * (s.context - freshCtx)) / (full - freshCtx)) : 0
140  if (pays && s.isExpired) percent = 100
141  const stage = percent >= 100 ? 'switch' : percent >= SOON_PERCENT ? 'soon' : 'ok'
142  let advice = null
143  if (pays && s.isExpired) advice = 'expired'
144  else if (pays && !isWorking && s.remainingMs != null && s.remainingMs <= IDLE_WARN_MS) advice = 'idle'
145  else if (stage !== 'ok') advice = stage
146  return { percent, stage, advice, fullAt: full }
147}
148
149/** gaugeOf with the options and observations filled in. */
150export function gauge(s, { isWorking = false } = {}) {
151  return gaugeOf(s, { ...assumed(), ttl: config.ttlMain, isWorking })
152}
153
154/** Appends one request, dropping the oldest beyond MAX_REQUESTS. */
155export function appendRequest(list, request) {
156  const next = [...(list ?? []), request]
157  return next.length > MAX_REQUESTS ? next.slice(next.length - MAX_REQUESTS) : next
158}
159
160/** Replaces one thread's requests with `requests` (read back from its transcript). */
161export function replaceThread(list, thread, requests) {
162  const next = [...(list ?? []).filter((q) => q.thread !== thread), ...requests].sort((a, b) => a.ts - b.ts)
163  return next.length > MAX_REQUESTS ? next.slice(next.length - MAX_REQUESTS) : next
164}
165
166/**
167 * One thread's requests as parseThread read them from its transcript, in the shape $.state
168 * keeps: the response's message id as `id`, the exact 5m / 1h split, `src` 'transcript'.
169 */
170export function transcriptRequests(parsed, thread) {
171  return (parsed ?? []).map((q) => ({ id: String(q.id), ts: q.ts, model: q.model, tools: q.tools, tok: q.tok, thread, src: 'transcript' }))
172}
173
174// When a live main request was sent: `sentAt` (the step's start), else its `ts`
175const sentOf = (q) => (typeof q.sentAt === 'number' ? q.sentAt : q.ts)
176
177/**
178 * The main thread's requests replaced by `requests`, the ones its transcript records. A live
179 * main request stays only when it was sent after the newest of them started, so it is one the
180 * transcript does not have yet; every request the transcript has started after its step was
181 * sent (the main thread makes one request at a time), so none is counted twice.
182 */
183export function mergeMain(list, requests) {
184  let newest = -Infinity
185  for (const q of requests) if (Number.isFinite(q.ts) && q.ts > newest) newest = q.ts
186  const live = (list ?? []).filter((q) => q.thread === 'main' && q.src !== 'transcript' && sentOf(q) > newest)
187  return replaceThread(list, 'main', [...requests, ...live])
188}
189
190/**
191 * Adds one live request. A main request whose response the transcript already had when it was
192 * last read (a read-back request that started after this step was sent: read mid-response, its
193 * output not final yet) is not added again; that request takes the larger output instead.
194 */
195export function addLive(list, request) {
196  if (request.thread === 'main') {
197    const at = sentOf(request)
198    const i = (list ?? []).findIndex((q) => q.thread === 'main' && q.src === 'transcript' && q.ts >= at)
199    if (i >= 0) {
200      const next = [...list]
201      const q = next[i]
202      next[i] = {
203        ...q,
204        tok: { ...q.tok, output: Math.max(q.tok.output || 0, request.tok.output || 0) },
205        tools: (q.tools ?? []).length >= request.tools.length ? q.tools : request.tools,
206      }
207      return next
208    }
209  }
210  return appendRequest(list, request)
211}
212
213// Why the main thread was not read back, as a sentence
214const MAIN_REASON = {
215  noPath: '記録の場所が分からないため、読み直せない',
216  tooLarge: '記録が 4 MiB を超えるため、読み直せない',
217  unreadable: '記録を読めなかった',
218  pending: 'ターンが終わると、記録から読み直す',
219}
220
221/**
222 * How the main thread is counted, for the pane and the export. source: $.state `mainSource`
223 * (unset before the first read-back was tried); hasPath: whether the transcript's path is known.
224 *   isBackfilled  read back from the transcript at least once: the totals cover the session
225 *   isCurrent     and the last read-back worked
226 *   method        the sentence for 数え方
227 *   label, note   the same split for a narrow pane: a short label, and the rest (may be '')
228 *   live          which of main is provisional (速報値), for the totals' headings: null when
229 *                 none is, else 'HH:MM 以降の主' after a read-back that stopped, else '主'
230 */
231export function mainSourceOf(source, { hasPath = false, tzOffsetMinutes = 0 } = {}) {
232  const at = typeof source?.backfilledAt === 'number' ? source.backfilledAt : null
233  const reasonKey = source?.reason && MAIN_REASON[source.reason] ? source.reason : source?.reason === 'empty' || hasPath ? 'pending' : 'noPath'
234  const reason = MAIN_REASON[reasonKey]
235  if (at != null && !source.reason) {
236    return { isBackfilled: true, isCurrent: true, at, live: null, method: '記録から読み直した(ターンの終わりごと)', label: '記録から(ターンごと)', note: '' }
237  }
238  if (at != null) {
239    const t = clockOf(at, tzOffsetMinutes)
240    return {
241      isBackfilled: true,
242      isCurrent: false,
243      at,
244      live: `${t} 以降の主`,
245      method: `${t} までは記録から読み直した値、その後は速報値(記録と未照合)。${reason}`,
246      label: `${t} まで記録・以降は速報値`,
247      note: `速報値は記録と未照合。${reason}`,
248    }
249  }
250  return {
251    isBackfilled: false,
252    isCurrent: false,
253    at: null,
254    live: '主',
255    method: `速報値(記録と未照合)。${reason}。書き込みの 5m / 1h は設定で振り分け`,
256    label: '速報値',
257    note: `記録と未照合。${reason}。書き込みの 5m / 1h は設定で振り分け`,
258  }
259}
260
261/** A JSONL text as records; a line that does not parse (a partial last line) is skipped. */
262export function parseJsonl(text) {
263  const out = []
264  for (const line of String(text ?? '').split('\n')) {
265    if (!line.trim()) continue
266    try {
267      out.push(JSON.parse(line))
268    } catch {}
269  }
270  return out
271}
272
273/** A subagent's label: its description cut short, else its id's first 8 characters. */
274export function threadLabel(key, meta) {
275  if (key === 'main') return '主'
276  const d = meta?.description?.trim()
277  if (d) return d
278  return String(key).replace(/^agent-/, '').slice(0, 8)
279}
280
281/**
282 * The live requests as aggregate.js's threads: main first, then each subagent in order of its
283 * first request; each thread's requests oldest first.
284 */
285export function toThreads(requests, metas) {
286  const by = new Map([['main', []]])
287  for (const q of requests ?? []) {
288    if (!by.has(q.thread)) by.set(q.thread, [])
289    by.get(q.thread).push(q)
290  }
291  const out = []
292  for (const [key, list] of by) {
293    list.sort((a, b) => a.ts - b.ts)
294    const meta = key === 'main' ? {} : metas?.[key] ?? {}
295    out.push({
296      key,
297      label: threadLabel(key, meta),
298      agentType: key === 'main' ? '(主)' : meta.agentType || '?',
299      description: meta.description || '',
300      requests: list,
301    })
302  }
303  return out
304}
305
306// ---- Formatting
307
308/** 370k, 1.2M, 950: a token count or weight, short. */
309export function short(n) {
310  if (n == null || !Number.isFinite(n)) return '—'
311  const a = Math.abs(n)
312  if (a < 1000) return String(Math.round(n))
313  if (a < 10000) return (n / 1000).toFixed(1) + 'k'
314  if (a < 1e6) return Math.round(n / 1000) + 'k'
315  return (n / 1e6).toFixed(a < 1e7 ? 2 : 1) + 'M'
316}
317
318/** 12%, 0.4%: a share; '—' with no whole. */
319export function pct(part, whole) {
320  if (!whole) return '—'
321  const p = (100 * part) / whole
322  return (p < 1 && p > 0 ? p.toFixed(1) : Math.round(p)) + '%'
323}
324
325/** 42 分, 1 時間 5 分, 30 秒 */
326export function duration(ms) {
327  const s = Math.max(0, Math.round(ms / 1000))
328  if (s < 60) return s + ' 秒'
329  const m = Math.floor(s / 60)
330  if (m < 60) return m + ' 分'
331  return Math.floor(m / 60) + ' 時間' + (m % 60 ? ' ' + (m % 60) + ' 分' : '')
332}
333
334/** The model id without its `claude-` prefix. */
335export function modelShort(model) {
336  return String(model ?? '?').replace(/^claude-/, '')
337}
338
339/** The model's family (opus, sonnet, haiku), else modelShort's name. */
340export function modelFamily(model) {
341  const s = String(model ?? '').toLowerCase()
342  for (const k of ['opus', 'sonnet', 'haiku']) if (s.includes(k)) return k
343  return modelShort(model)
344}
345
346/** A thread's models in a list row: their families, each once, joined by '/'. */
347export const modelsLine = (models) => [...new Set((models ?? []).map(modelFamily))].join('/') || '—'
348
349/** The local offset in minutes; 0 where Date is unavailable. */
350export function localOffsetMinutes() {
351  try {
352    return typeof Date === 'function' ? -new Date().getTimezoneOffset() : 0
353  } catch {
354    return 0
355  }
356}
357
358// ---- Handoff
359
360/** The marker the new session's first message carries. */
361export const HANDOFF_MARK = /\[usage-ledger handoff ([0-9]{8}-[0-9]{4}-[0-9a-z]{4})\]/
362
363/** The $.store key of one handoff's record. */
364export const handoffKey = (id) => 'handoff:' + id
365
366/** `YYYYMMDD-HHMM-xxxx` in local time; xxxx from the clock's milliseconds, so ids rarely repeat. */
367export function handoffId(now, tzOffsetMinutes) {
368  return stamp(now, tzOffsetMinutes) + '-' + (Math.floor(now) % 1679616).toString(36).padStart(4, '0')
369}
370
371function stamp(now, tzOffsetMinutes) {
372  const d = isoString(now + tzOffsetMinutes * 60000)
373  return d.slice(0, 4) + d.slice(5, 7) + d.slice(8, 10) + '-' + d.slice(11, 13) + d.slice(14, 16)
374}
375
376/** `<handoffDir>/handoff-YYYYMMDD-HHMM.md`, with forward slashes. */
377export function handoffFile(dir, now, tzOffsetMinutes) {
378  const base = String(dir || DEFAULTS.handoffDir).replace(/\\/g, '/').replace(/\/+$/, '')
379  return `${base}/handoff-${stamp(now, tzOffsetMinutes)}.md`
380}
381
382/** The prompt the band's and the pane's [引き継ぐ…] put in the prompt box. Never sent by the mod. */
383export function handoffDraft({ file, id }) {
384  return [
385    `${file} に引き継ぎ文を書いてください。`,
386    '中身は「目標・今の段階・決めたこと・未解決・最初に読むファイル」の 5 項目で、各 5 行以内、全体で 1.5k トークン以内にします。会話の本文は写さないでください。',
387    `書いたら、その本文と目印 [usage-ledger handoff ${id}] を最初の発言にして、新しいセッションを開いてください。`,
388    '新しいセッションからはこのファイルが見えないことがあるので、本文は最初の発言に含めてください。',
389    '開く前に、どのツールで何を開くかを私に確認してください。',
390  ].join('\n')
391}
392
hooks/pane.js 767 lines
1// The pane /usage-ledger opens, the band above the prompt, and the handoff the pane and the
2// band start.
3//
4// The pane is one, its view ($.state `view`) switching what it draws, since a second pane
5// opened from a button may not come to the front. Its first view, 状況, says in plain words
6// where the session stands and what to do; the others are tables of one-line rows, a row's
7// [▸] opening its details under it ($.state `open`, one row per view). Everything is
8// aggregated afresh from $.state at each drawing, and the drawing reads that state, so a new
9// request redraws it; nothing here calls $.ui.invalidate (register.js redraws at the cache's
10// two edges only, since every redraw rebuilds every mod's drawing in the desktop app).
11//
12// The band shows whenever the main context is known: a gauge with no number of how far the
13// context is from where a handoff pays (ledger.js gaugeOf, drawn by meter.js), the stage's
14// word and [詳しく], all on one line; a short phrase and [引き継ぐ…] join it only when there is
15// something to do. On a narrow line it gives way as meter.js fitMeter says, without a redraw
16// of its own: a change of width draws it again.
17// What mods beneath draw in the band (usage-band) stays under it.
18//
19// [引き継ぐ…] only fills the prompt box with a draft asking the model to write a handoff and
20// open a new session; the person reads it and decides whether to send it.
21
22import { aggregateThreads, TYPES, clockOf } from './aggregate.js'
23import {
24  VIEWS,
25  ADVICE,
26  gauge,
27  ESTIMATE_NOTE,
28  WEIGHTS_NOTE,
29  handoffKey,
30  getConfig,
31  getObserved,
32  assumed,
33  summarize,
34  toThreads,
35  short,
36  pct,
37  duration,
38  modelShort,
39  modelsLine,
40  localOffsetMinutes,
41  handoffId,
42  handoffFile,
43  handoffDraft,
44  mainSourceOf,
45} from './ledger.js'
46import { SPACE, COLOR, BUTTON, choice, dim, inline, cell, slot, tableRow, field, fieldsFor, isStacked, stackedField, section, details, page } from './style.js'
47import { fitMeter, meterParts, cells } from './meter.js'
48import { exportPaths, exportMarkdown, exportJsonl, promptLine } from './export.js'
49import { GRACE_MS, guardDrawing, beginPress, hasStarted, takeOver, endPress } from './press-guard.js'
50import { backfillMain } from './backfill.js'
51import { atom, read, update } from 'claude-code'
52
53// The state this file reads and writes (declared in types/index.d.ts)
54const view = atom({ plugin: 'usage-ledger', key: 'view' }, 'status')
55const REQUESTS = { plugin: 'usage-ledger', key: 'requests' }
56const THREADS = { plugin: 'usage-ledger', key: 'threads' }
57const STARTED_AT = { plugin: 'usage-ledger', key: 'startedAt' }
58const OPEN = { plugin: 'usage-ledger', key: 'open' }
59const HANDOFF = { plugin: 'usage-ledger', key: 'handoff' }
60const INCOMING = { plugin: 'usage-ledger', key: 'incoming' }
61const TRANSCRIPT = { plugin: 'usage-ledger', key: 'transcriptPath' }
62const MAIN_SOURCE = { plugin: 'usage-ledger', key: 'mainSource' }
63
64const TITLE = 'トークンの内訳'
65const TYPE_LABEL = { input: '入力', cache_read: 'キャッシュ読み', cache_write_5m: '書き 5m', cache_write_1h: '書き 1h', output: '出力' }
66// Rows a long list shows
67const MAX_ROWS = 40
68
69// Element keys allow a plain set of characters; agent ids and tool names may carry others
70const keyOf = (prefix, id) => prefix + '-' + String(id).replace(/[^A-Za-z0-9_-]/g, '_')
71
72// ===== Actions: called from press handlers only (never while drawing) =====
73
74async function go($, id) {
75  await update($, view, () => id)
76  try {
77    await $.ui.scroll({ in: 'usage-ledger', to: 'start' })
78  } catch {}
79}
80
81// Opens the pane at view `id`: /usage-ledger and the band's [詳しく]
82async function openPane($, id) {
83  await update($, view, () => id)
84  return $.ui.open({ id: 'usage-ledger', title: TITLE })
85}
86
87// Opens one row's details in a view, or closes them when pressed again
88function toggleOpen($, viewId, rowKey) {
89  return update($, { ...OPEN, id: viewId }, (value) => (value === rowKey ? '' : rowKey))
90}
91
92// Puts the handoff draft in the prompt box (never sends it) and records the handoff, here
93// ($.state) and for the new session ($.store)
94async function startHandoff($) {
95  const now = await $.clock.now()
96  const tz = localOffsetMinutes()
97  const { value: requests } = await $.state.get(REQUESTS)
98  const s = summarize(requests, now)
99  const id = handoffId(now, tz)
100  const file = handoffFile(getConfig().handoffDir, now, tz)
101  const text = handoffDraft({ file, id })
102  const box = await $.prompt.read()
103  const hasDraft = typeof box?.text === 'string' && box.text.trim() !== ''
104  const filled = await $.prompt.fill(hasDraft ? { text: '\n\n' + text, mode: 'append' } : { text, mode: 'replace' })
105  if (!filled?.isFilled) {
106    $.ui.toast('入力欄に下書きを入れられませんでした' + (filled?.refusal ? `(${filled.refusal})` : ''))
107    return
108  }
109  const predictedCtx = assumed().freshCtx
110  await $.store.set(handoffKey(id), { fromSession: await $.session.id(), ctxAtHandoff: s.context, predictedCtx, createdAt: now, observed: [] })
111  await $.state.set(HANDOFF, { id, status: 'drafted', createdAt: now, file, ctxAtHandoff: s.context, predictedCtx, observed: [] })
112  $.ui.toast('引き継ぎの下書きを入力欄に入れました。読んでから、送るかどうかを決めてください')
113}
114
115async function cancelHandoff($) {
116  await $.state.set(HANDOFF, null)
117}
118
119// Writes the session's numbers to the export folder (export.js: a .md summary and a .jsonl of
120// the requests, the pair overwritten on each press), then names the .md in a toast and in a
121// line added to the prompt box (never sent). The main thread is read back from its transcript
122// first (backfill.js), the only part that writes $.state and so redraws.
123async function exportData($) {
124  try {
125    await backfillMain($)
126  } catch {}
127  const now = await $.clock.now()
128  const tz = localOffsetMinutes()
129  const { value: requests = [] } = await $.state.get(REQUESTS)
130  const { value: threads = {} } = await $.state.get(THREADS)
131  const { value: startedAt } = await $.state.get(STARTED_AT)
132  const { value: transcriptPath } = await $.state.get(TRANSCRIPT)
133  const { value: mainSource } = await $.state.get(MAIN_SOURCE)
134  const main = mainSourceOf(mainSource, { hasPath: typeof transcriptPath === 'string' && transcriptPath !== '', tzOffsetMinutes: tz })
135  const sessionId = await $.session.id()
136  const paths = exportPaths({
137    dir: getConfig().exportDir,
138    root: await $.session.root(),
139    sessionId,
140    startedAt: typeof startedAt === 'number' ? startedAt : now,
141    tzOffsetMinutes: tz,
142  })
143  const jsonlName = paths.name + '.jsonl'
144  try {
145    await $.fs.write(paths.md, exportMarkdown({ requests, threads, startedAt, now, tzOffsetMinutes: tz, sessionId, jsonlName, main }))
146    await $.fs.write(paths.jsonl, exportJsonl(requests, threads, tz))
147  } catch (error) {
148    $.ui.toast(`集計を書き出せませんでした(${paths.shown.md}): ${String(error?.message ?? error)}`)
149    return
150  }
151  const line = promptLine(paths.shown.md)
152  const box = await $.prompt.read()
153  const hasDraft = typeof box?.text === 'string' && box.text.trim() !== ''
154  const filled = await $.prompt.fill(hasDraft ? { text: '\n\n' + line, mode: 'append' } : { text: line, mode: 'replace' })
155  $.ui.toast(`集計を ${paths.shown.md} に書き出しました` + (filled?.isFilled ? '' : '(入力欄には入れられませんでした)'))
156}
157
158// ===== The pane =====
159
160// Reads everything a view draws from; each read subscribes the drawing
161async function gather($, e) {
162  const current = await read($, view)
163  const { value: requests = [] } = await $.state.get(REQUESTS)
164  const { value: threads = {} } = await $.state.get(THREADS)
165  const { value: startedAt } = await $.state.get(STARTED_AT)
166  const { value: open = '' } = await $.state.get({ ...OPEN, id: current })
167  const { value: handoff = null } = await $.state.get(HANDOFF)
168  const { value: incoming = null } = await $.state.get(INCOMING)
169  const { value: transcriptPath } = await $.state.get(TRANSCRIPT)
170  const { value: mainSource } = await $.state.get(MAIN_SOURCE)
171  const now = await $.clock.now()
172  const tz = localOffsetMinutes()
173  const main = mainSourceOf(mainSource, { hasPath: typeof transcriptPath === 'string' && transcriptPath !== '', tzOffsetMinutes: tz })
174  const agg = aggregateThreads(toThreads(requests, threads), { tzOffsetMinutes: tz, top: 15 })
175  const s = summarize(requests, now, { handoff: handoff?.status })
176  // The pane is not told whether a turn runs: the idle warning is left to the band
177  const g = gauge(s, { isWorking: true })
178  return { view: current, requests, threads, startedAt, open, handoff, incoming, main, now, tz, agg, s, g, surface: e.surface, width: widthOf(e) ?? 84 }
179}
180
181// The cells a section's body has across, and a row's details (inside the page's padding and
182// the section's indent, the details indented again)
183const bodyRoom = (d) => d.width - 2 * SPACE.page - SPACE.indent
184const detailsRoom = (d) => bodyRoom(d) - 2 * SPACE.indent
185
186// What the totals cover: once the main thread was read back, the whole session, else from
187// when counting started; and which of main is still provisional (null: none)
188function scopeOf(d) {
189  return {
190    scope: d.main.isBackfilled ? 'セッション全体' : typeof d.startedAt === 'number' ? '集計開始 ' + clockOf(d.startedAt, d.tz) : '集計開始 —',
191    live: d.main.live ? `${d.main.live}は速報値` : null,
192  }
193}
194
195// The line under the title. When it is wider than the pane, its parts go one a line (the
196// scope and the provisional part together when they fit), never cut at the edge.
197function headerAbout(ui, d) {
198  const { scope, live } = scopeOf(d)
199  const counts = `要求 ${d.agg.total.requests} 回 · 重み計 ${short(d.agg.total.weighted)}*`
200  const first = scope + (live ? `(${live})` : '')
201  const line = `${first} · ${counts}`
202  const room = d.width - 2 * SPACE.page
203  if (cells(line) <= room) return dim(ui, 'header-about', line)
204  const parts = cells(first) <= room ? [first, counts] : [scope, live, counts].filter(Boolean)
205  return ui.Box({ key: 'header-about', flexDirection: 'column', width: '100%', children: parts.map((t, i) => dim(ui, 'header-about-' + i, t)) })
206}
207
208function drawHeader($, ui, d) {
209  const nav = VIEWS.map((v) =>
210    ui.Button({ key: 'view-' + v.id, label: v.label, ...choice(d.view === v.id), onPress: () => go($, v.id) }),
211  )
212  nav.push(ui.Button({ key: 'close', label: '閉じる', role: 'dismiss', ...BUTTON.nav, onPress: () => $.ui.close({ id: 'usage-ledger' }) }))
213  return ui.Box({
214    key: 'header',
215    flexDirection: 'column',
216    width: '100%',
217    children: [
218      ui.Text({ bold: true, children: [TITLE] }),
219      headerAbout(ui, d),
220      inline(ui, 'header-nav', nav, 1),
221    ],
222  })
223}
224
225// A [▸] button that opens a row's details
226function openButton($, ui, d, rowKey) {
227  const isOpen = d.open === rowKey
228  return ui.Button({ key: keyOf('open-' + d.view, rowKey), label: isOpen ? '▾' : '▸', ...choice(isOpen), onPress: () => toggleOpen($, d.view, rowKey) })
229}
230
231const num = (ui, width, text, props = {}) => cell(ui, width, text, props, 'flex-end')
232const head = (ui, width, text, align) => cell(ui, width, text, { dimColor: true }, align)
233const tokLine = (t) => TYPES.map((k) => `${TYPE_LABEL[k]} ${short(t[k])}`).join(' · ')
234
235// ----- 状況 -----
236
237// What each stage means and what to do, in plain words: [what it is, what to do]
238const STATUS_TEXT = {
239  ok: [
240    'まだ余裕があります。このまま続けて大丈夫です。',
241    '会話が長くなるほど、1 回のやり取りで使う量が増えます。目安が右端に近づいたら、新しいセッションに引き継ぐと節約になります。',
242  ],
243  soon: [
244    '会話が長くなってきました。',
245    'このまま続けるより、新しいセッションに引き継いだほうが使う量が少なく済むようになってきています。作業の区切りで [引き継ぐ…] を押してください。',
246  ],
247  switch: [
248    '会話がかなり長くなりました。',
249    '新しいセッションに引き継いだほうが、使う量が少なく済みます。今の区切りで [引き継ぐ…] を押してください。',
250  ],
251  expired: [
252    '休憩の間に割高になりました。',
253    'しばらく操作がなかったので、次の 1 回は会話全体を読み込み直すことになり、使う量が増えます。続きは新しいセッションのほうが安く済みます。[引き継ぐ…] を押してください。',
254  ],
255}
256
257function drawStatus($, ui, d) {
258  const { g } = d
259  const lines = []
260  if (g.percent == null) {
261    lines.push(dim(ui, 'status-empty', 'まだこのセッションの要求がありません。会話を始めると、ここに目安が出ます'))
262  } else {
263    // The page's padding and the section's indent come off the pane's width
264    const fit = fitMeter({ surface: d.surface, stage: g.stage, columns: d.width - 2 * SPACE.page - SPACE.indent })
265    lines.push(ui.Box({ key: 'status-meter', flexDirection: 'row', flexWrap: 'nowrap', columnGap: SPACE.inline, alignItems: 'center', children: meterParts(ui, d.surface, fit, g) }))
266    const [what, todo] = STATUS_TEXT[g.advice === 'expired' ? 'expired' : g.stage]
267    lines.push(ui.Box({ key: 'status-what', children: [ui.Text({ wrap: 'wrap', ...(g.stage === 'ok' ? {} : { bold: true }), children: [what] })] }))
268    lines.push(ui.Box({ key: 'status-todo', children: [ui.Text({ wrap: 'wrap', children: [todo] })] }))
269  }
270  if (d.handoff) lines.push(fieldsFor(bodyRoom(d))(ui, 'status-handoff-state', '引き継ぎ', handoffState(d.handoff), 10))
271
272  const actions = []
273  if (d.handoff?.status === 'drafted') {
274    actions.push(ui.Button({ key: 'status-cancel', label: '取り消す', ...BUTTON.minor, onPress: () => cancelHandoff($) }))
275  } else if (!d.handoff && g.advice) {
276    actions.push(ui.Button({ key: 'status-handoff', label: '引き継ぐ…', ...BUTTON.main, onPress: () => startHandoff($) }))
277  }
278  actions.push(ui.Button({ key: 'status-details', label: '詳しい数字', ...BUTTON.nav, onPress: () => go($, 'overview') }))
279  actions.push(ui.Button({ key: 'status-export', label: '書き出す', ...BUTTON.nav, onPress: () => exportData($) }))
280  actions.push(ui.Button({ key: 'status-about-handoff', label: '引き継ぎについて', ...BUTTON.nav, onPress: () => go($, 'handoff') }))
281  return [section(ui, 'status', '今の状況', lines), inline(ui, 'status-actions', actions)]
282}
283
284// ----- 概要 -----
285// The main thread's 数え方: the sentence beside its name, or on a narrow pane its short label
286// under the name and the rest of it as a dim line below
287function methodField(ui, key, name, main, width, room) {
288  if (!isStacked(room, width, main.method)) return field(ui, key, name, main.method, width)
289  return stackedField(ui, key, name, main.label, {}, main.note ? [dim(ui, key + '-note', main.note)] : [])
290}
291
292function drawOverview($, ui, d) {
293  const { s, agg } = d
294  const room = bodyRoom(d)
295  const field = fieldsFor(room)
296  const be = s.breakEven
297  const { freshCtx, baseCtx } = assumed()
298  const obs = getObserved()
299  const now = []
300  if (s.context == null) {
301    now.push(dim(ui, 'now-empty', d.main.isBackfilled ? 'まだ主スレッドの要求がありません' : 'まだ主スレッドの要求がありません。今は集計開始より後の要求だけを数えます(速報値)'))
302  } else {
303    // The model goes when even the line under the name is too narrow for it
304    const ctx = `${short(s.context)}(${modelShort(s.model)})`
305    const ctxShown = isStacked(room, 18, ctx) && cells(ctx) > room - SPACE.indent ? short(s.context) : ctx
306    now.push(field(ui, 'now-ctx', '主の文脈', ctxShown, 18, s.isHeavy ? { color: COLOR.warn } : {}))
307    now.push(
308      field(
309        ui,
310        'now-cache',
311        'キャッシュ',
312        s.isExpired
313          ? `切れた(${duration(d.now - s.expiresAt)} 前)`
314          : `残り ${duration(s.remainingMs)}(${getConfig().ttlMain} で計算)`,
315        18,
316        s.isExpired ? { color: COLOR.bad } : {},
317      ),
318    )
319    now.push(field(ui, 'now-next', '次の1回', `≈${short(be.next)}*`))
320    now.push(field(ui, 'now-rewrite', '切れたら再書込', `≈${short(be.rewrite)}*`))
321    const premise = `初期 ${short(freshCtx)}・基礎 ${short(baseCtx)} を前提${obs.fresh || obs.base ? '(実測を含む)' : ''}`
322    let fresh = '今の文脈が新しいセッションの初期文脈より小さいので、引き継ぐ得はない'
323    if (be.applies) fresh = (be.runs === 0 ? 'キャッシュが切れたので、今なら新しいセッションのほうが安い' : `${be.runs} 回の要求で回収できる`) + '。' + premise
324    now.push(field(ui, 'now-fresh', '新しいセッション', fresh))
325  }
326  if (d.handoff) now.push(field(ui, 'now-handoff', '引き継ぎ', handoffState(d.handoff)))
327
328  const main = agg.threads.find((t) => t.key === 'main') ?? { requests: 0, weighted: 0 }
329  const subs = { requests: agg.total.requests - main.requests, weighted: agg.total.weighted - main.weighted }
330  const tax = agg.gapTax.expired
331  const totals = [
332    field(ui, 'total-requests', '要求', `${agg.total.requests} 回(主 ${main.requests}・サブ ${subs.requests})`),
333    field(ui, 'total-weighted', '重み*', `${short(agg.total.weighted)}(主 ${pct(main.weighted, agg.total.weighted)}・サブ ${pct(subs.weighted, agg.total.weighted)})`),
334    field(ui, 'total-hit', 'キャッシュ読みの割合', pct(agg.total.cacheHitRatio, 1)),
335    field(ui, 'total-expired', '期限切れ後の再書込', `${tax.requests} 回 · 重み ${short(tax.weighted)}*(全体の ${pct(tax.weighted, agg.total.weighted)})`),
336    methodField(ui, 'total-main-source', '主の数え方', d.main, 18, room),
337  ]
338
339  const typeRows = [
340    tableRow(ui, 'type-head', [head(ui, 16, '種類'), head(ui, 10, 'トークン', 'flex-end'), head(ui, 10, '重み*', 'flex-end'), head(ui, 6, '割合', 'flex-end')]),
341    ...agg.byType.map((r) =>
342      tableRow(ui, keyOf('type', r.type), [
343        cell(ui, 16, TYPE_LABEL[r.type]),
344        num(ui, 10, short(r.tokens)),
345        num(ui, 10, short(r.weighted)),
346        num(ui, 6, pct(r.weighted, agg.total.weighted)),
347      ]),
348    ),
349  ]
350
351  // The totals' heading says which of main is provisional; when the heading is wider than the
352  // pane, that part becomes the section's first line
353  const { live } = scopeOf(d)
354  const totalsName = d.main.isBackfilled ? 'セッション全体の合計' : '集計開始からの合計'
355  const totalsNote = live ?? '主は記録から読み直した値'
356  const totalsTitle = `${totalsName}(${totalsNote})`
357  const isTitleNarrow = cells(totalsTitle) > d.width - 2 * SPACE.page
358
359  return [
360    section(ui, 'now', '今の文脈', now),
361    isTitleNarrow
362      ? section(ui, 'totals', totalsName, [dim(ui, 'totals-note', totalsNote), ...totals])
363      : section(ui, 'totals', totalsTitle, totals),
364    section(ui, 'types', 'トークンの種類別', typeRows),
365    dim(ui, 'weights-note', WEIGHTS_NOTE),
366    section(ui, 'export', '書き出し', [
367      dim(ui, 'export-about', `この集計を ${getConfig().exportDir} にファイルで書き出し、会話で Claude に読ませられます`),
368      inline(ui, 'overview-actions', [ui.Button({ key: 'overview-export', label: '書き出す', ...BUTTON.nav, onPress: () => exportData($) })]),
369    ]),
370  ]
371}
372
373function handoffState(h) {
374  if (h.status === 'drafted') return '下書きを入力欄に入れた(新しいセッションはまだ)'
375  const first = h.observed[0]
376  return `済み · 元 ${short(h.ctxAtHandoff)} → 初回 ${short(first)}(予測 ${short(h.predictedCtx)})`
377}
378
379// ----- スレッド -----
380
381// The widths of the thread list's columns in `room` cells, a row being one line cut at the
382// pane's edge: the thread (its description), its kind, its models, 要求, 重み*, 割合 and [▸].
383// As room runs out, the thread's text shortens first, then the kind's, then the kind goes,
384// then 要求, then 重み*; the models and 割合 stay. 0 is a column left out.
385const THREAD_COL = { label: [12, 28], type: [8, 16], model: [6, 13], req: 5, weight: 8, share: 6, open: 3, labelMin: 6 }
386export function threadColumns(room, modelCells) {
387  const C = THREAD_COL
388  let model = Math.min(Math.max(modelCells, C.model[0]), C.model[1])
389  const steps = [
390    { type: true, req: true, weight: true, labelMin: C.label[0] },
391    { type: false, req: true, weight: true, labelMin: C.labelMin },
392    { type: false, req: false, weight: true, labelMin: C.labelMin },
393    { type: false, req: false, weight: false, labelMin: C.labelMin },
394  ]
395  for (const [i, s] of steps.entries()) {
396    const nums = (s.req ? C.req : 0) + (s.weight ? C.weight : 0) + C.share + C.open
397    const count = 4 + (s.type ? 1 : 0) + (s.req ? 1 : 0) + (s.weight ? 1 : 0)
398    const free = room - model - nums - SPACE.column * (count - 1)
399    const isLast = i === steps.length - 1
400    if (s.type) {
401      if (free - C.label[0] < C.type[0]) continue
402      const type = Math.min(C.type[1], free - C.label[0])
403      return { label: Math.min(C.label[1], free - type), type, model, req: C.req, weight: C.weight }
404    }
405    if (free < s.labelMin && !isLast) continue
406    // Last: the models give way to keep the thread's text, then both stay at their least and
407    // what does not fit is cut at the edge
408    if (free < s.labelMin) model = Math.max(C.model[0] - 2, model - (s.labelMin - free))
409    const label = Math.max(s.labelMin, Math.min(C.label[1], room - model - nums - SPACE.column * (count - 1)))
410    return { label, type: 0, model, req: s.req ? C.req : 0, weight: s.weight ? C.weight : 0 }
411  }
412}
413
414function drawThreads($, ui, d) {
415  const { agg } = d
416  const shown = agg.threads.slice(0, MAX_ROWS)
417  const models = new Map(shown.map((t) => [t.key, modelsLine(t.models)]))
418  // The page's padding and the section's indent come off the pane's width
419  const col = threadColumns(d.width - 2 * SPACE.page - SPACE.indent, Math.max(0, ...[...models.values()].map(cells)))
420  const rows = [
421    tableRow(ui, 'thread-head', [
422      head(ui, col.label, 'スレッド'),
423      col.type ? head(ui, col.type, '種類') : null,
424      head(ui, col.model, 'モデル'),
425      col.req ? head(ui, col.req, '要求', 'flex-end') : null,
426      col.weight ? head(ui, col.weight, '重み*', 'flex-end') : null,
427      head(ui, THREAD_COL.share, '割合', 'flex-end'),
428    ].filter(Boolean)),
429  ]
430  for (const t of shown) {
431    const meta = d.threads[t.key] ?? {}
432    rows.push(
433      tableRow(ui, keyOf('thread', t.key), [
434        cell(ui, col.label, t.label, t.key === 'main' ? { bold: true } : {}),
435        col.type ? cell(ui, col.type, t.agentType, { dimColor: true }) : null,
436        cell(ui, col.model, models.get(t.key)),
437        col.req ? num(ui, col.req, t.requests) : null,
438        col.weight ? num(ui, col.weight, short(t.weighted)) : null,
439        num(ui, THREAD_COL.share, pct(t.weighted, agg.total.weighted)),
440        slot(ui, THREAD_COL.open, [openButton($, ui, d, t.key)]),
441      ].filter(Boolean)),
442    )
443    if (d.open === t.key) {
444      const span = t.start != null ? `${clockOf(t.start, d.tz)}–${clockOf(t.end, d.tz)}` : '—'
445      const field = fieldsFor(detailsRoom(d))
446      rows.push(
447        details(ui, keyOf('thread-details', t.key), [
448          t.description ? field(ui, 'd-desc', '説明', t.description, 14) : null,
449          // What the row leaves out on a narrow pane
450          col.type ? null : field(ui, 'd-type', '種類', t.agentType, 14),
451          col.req && col.weight ? null : field(ui, 'd-weight', '要求・重み', `${t.requests} 回 · 重み ${short(t.weighted)}*`, 14),
452          field(ui, 'd-id', 'id', t.key, 14),
453          field(ui, 'd-models', 'モデル', t.models.map(modelShort).join('、') || '—', 14),
454          field(ui, 'd-span', '期間', span, 14),
455          field(ui, 'd-ctx', '文脈', `初回 ${short(t.firstContext)} → 最後 ${short(t.lastContext)}`, 14),
456          field(ui, 'd-tok', 'トークン', tokLine(t.tok), 14),
457          t.key === 'main'
458            ? methodField(ui, 'd-source', '数え方', d.main, 14, detailsRoom(d))
459            : field(ui, 'd-source', '数え方', meta.isBackfilled ? '終了後に記録から読み直した' : 'ライブ(書き込みの 5m / 1h は設定で振り分け)', 14),
460        ]),
461      )
462    }
463  }
464  if (agg.threads.length > MAX_ROWS) rows.push(dim(ui, 'thread-more', `ほか ${agg.threads.length - MAX_ROWS} 件`))
465  return [section(ui, 'threads', '主スレッドとサブエージェント(重みの大きい順)', rows)]
466}
467
468// ----- 種類・モデル -----
469function drawKinds($, ui, d) {
470  const { agg } = d
471  const kindRows = [
472    tableRow(ui, 'kind-head', [head(ui, 22, '種類'), head(ui, 4, '回', 'flex-end'), head(ui, 5, '要求', 'flex-end'), head(ui, 8, '重み*', 'flex-end'), head(ui, 6, '割合', 'flex-end')]),
473    ...agg.byAgentType.map((r) =>
474      tableRow(ui, keyOf('kind', r.key), [
475        cell(ui, 22, r.key),
476        num(ui, 4, r.runs),
477        num(ui, 5, r.requests),
478        num(ui, 8, short(r.weighted)),
479        num(ui, 6, pct(r.weighted, agg.total.weighted)),
480      ]),
481    ),
482  ]
483  const modelRows = [
484    tableRow(ui, 'model-head', [head(ui, 28, 'モデル'), head(ui, 5, '要求', 'flex-end'), head(ui, 8, '重み*', 'flex-end'), head(ui, 6, '割合', 'flex-end')]),
485    ...agg.byModel.map((r) =>
486      tableRow(ui, keyOf('model', r.key), [
487        cell(ui, 28, modelShort(r.key)),
488        num(ui, 5, r.requests),
489        num(ui, 8, short(r.weighted)),
490        num(ui, 6, pct(r.weighted, agg.total.weighted)),
491      ]),
492    ),
493  ]
494  return [section(ui, 'kinds', 'エージェントの種類別', kindRows), section(ui, 'models', 'モデル別', modelRows)]
495}
496
497// ----- 高い要求 -----
498function drawCostly($, ui, d) {
499  const { agg } = d
500  const label = Object.fromEntries(agg.threads.map((t) => [t.key, t.label]))
501  const rows = [
502    tableRow(ui, 'costly-head', [
503      head(ui, 5, '時刻'),
504      head(ui, 18, 'スレッド'),
505      head(ui, 7, '文脈', 'flex-end'),
506      head(ui, 7, '書込', 'flex-end'),
507      head(ui, 6, '出力', 'flex-end'),
508      head(ui, 8, '重み*', 'flex-end'),
509    ]),
510  ]
511  agg.biggest.forEach((q, i) => {
512    const rowKey = String(i)
513    rows.push(
514      tableRow(ui, keyOf('costly', i), [
515        cell(ui, 5, clockOf(q.ts, d.tz), { dimColor: true }),
516        cell(ui, 18, label[q.thread] ?? q.thread),
517        num(ui, 7, short(q.context)),
518        num(ui, 7, short(q.write), q.write > 0.5 * q.context ? { color: COLOR.warn } : {}),
519        num(ui, 6, short(q.output)),
520        num(ui, 8, short(q.weighted)),
521        slot(ui, 3, [openButton($, ui, d, rowKey)]),
522      ]),
523    )
524    if (d.open === rowKey) {
525      const field = fieldsFor(detailsRoom(d))
526      rows.push(
527        details(ui, keyOf('costly-details', i), [
528          field(ui, 'd-model', 'モデル', modelShort(q.model), 14),
529          field(ui, 'd-tools', 'ツール', q.tools.length ? q.tools.join('、') : '(文字だけ)', 14),
530        ]),
531      )
532    }
533  })
534  if (agg.biggest.length === 0) rows.push(dim(ui, 'costly-empty', 'まだ要求がありません'))
535  return [section(ui, 'costly', '重みの大きい要求(全スレッド、上位 15)', rows)]
536}
537
538// ----- 待ちと再書込 -----
539function drawGaps($, ui, d) {
540  const { agg } = d
541  const label = Object.fromEntries(agg.threads.map((t) => [t.key, t.label]))
542  const tax = agg.gapTax
543  const field = fieldsFor(bodyRoom(d))
544  const summary = [
545    field(ui, 'gap-expired', '期限切れの後', `${tax.expired.requests} 回 · 書込 ${short(tax.expired.write)} · 重み ${short(tax.expired.weighted)}*(全体の ${pct(tax.expired.weighted, agg.total.weighted)})`),
546    field(ui, 'gap-within', '期限内', `${tax.within.requests} 回 · 書込 ${short(tax.within.write)} · 重み ${short(tax.within.weighted)}*`),
547  ]
548  const rows = [
549    tableRow(ui, 'gap-head', [
550      head(ui, 5, '時刻'),
551      head(ui, 18, 'スレッド'),
552      head(ui, 7, '待ち', 'flex-end'),
553      head(ui, 3, 'TTL'),
554      head(ui, 4, '状態'),
555      head(ui, 7, '書込', 'flex-end'),
556      head(ui, 8, '重み*', 'flex-end'),
557    ]),
558  ]
559  for (const g of [...agg.gaps].reverse().slice(0, MAX_ROWS)) {
560    const expired = g.state === 'expired'
561    rows.push(
562      tableRow(ui, keyOf('gap', g.thread + '-' + g.ts), [
563        cell(ui, 5, clockOf(g.ts, d.tz), { dimColor: true }),
564        cell(ui, 18, label[g.thread] ?? g.thread),
565        num(ui, 7, duration(g.gapSeconds * 1000)),
566        cell(ui, 3, g.ttlSeconds === 3600 ? '1h' : '5m', { dimColor: true }),
567        cell(ui, 4, expired ? '切れ' : '内', expired ? { color: COLOR.bad } : { dimColor: true }),
568        num(ui, 7, short(g.write)),
569        num(ui, 8, short(g.weighted)),
570      ]),
571    )
572  }
573  if (agg.gaps.length === 0) rows.push(dim(ui, 'gap-empty', '5 分を超えて間が空いた要求はまだありません'))
574  return [
575    section(ui, 'gap-summary', 'キャッシュの再書込', summary),
576    section(ui, 'gaps', '5 分を超えて間が空いた要求(新しい順)', rows),
577    dim(ui, 'gap-note', 'TTL は同じスレッドの直前の要求の書き込みで判断します。ライブで数えた分の 5m / 1h は設定による推定です'),
578  ]
579}
580
581// ----- 主のツール -----
582function drawTools($, ui, d) {
583  const { agg } = d
584  const rows = [
585    tableRow(ui, 'tool-head', [head(ui, 30, '応答で呼んだツール'), head(ui, 5, '要求', 'flex-end'), head(ui, 8, '重み*', 'flex-end'), head(ui, 6, '主の中', 'flex-end')]),
586    ...agg.main.byTool.slice(0, MAX_ROWS).map((r) =>
587      tableRow(ui, keyOf('tool', r.key), [
588        cell(ui, 30, r.key === 'text' ? '(文字だけ)' : r.key),
589        num(ui, 5, r.requests),
590        num(ui, 8, short(r.weighted)),
591        num(ui, 6, pct(r.weighted, agg.main.weighted)),
592      ]),
593    ),
594  ]
595  if (agg.main.byTool.length === 0) rows.push(dim(ui, 'tool-empty', 'まだ主スレッドの要求がありません'))
596  return [section(ui, 'tools', '主スレッドの要求を、その応答が呼んだツールで分けたもの', rows)]
597}
598
599// ----- 引き継ぎ -----
600function drawHandoff($, ui, d) {
601  const { freshCtx, baseCtx } = assumed()
602  const field = fieldsFor(bodyRoom(d))
603  const blocks = [
604    dim(
605      ui,
606      'handoff-about',
607      '[引き継ぐ…] は、引き継ぎ文を書いて新しいセッションを開くよう頼む文を、入力欄に入れるだけです。送るかどうかはあなたが決めます。新しいセッションを開く前にも、Claude があなたに確認します',
608    ),
609    section(ui, 'handoff-setup', '設定', [
610      field(ui, 'handoff-dir', '置き場', getConfig().handoffDir),
611      field(ui, 'handoff-premise', '前提', `初期文脈 ${short(freshCtx)}・基礎部分 ${short(baseCtx)}`),
612    ]),
613    inline(ui, 'handoff-actions', [ui.Button({ key: 'handoff-start', label: '引き継ぐ…', ...BUTTON.main, onPress: () => startHandoff($) })]),
614  ]
615  const h = d.handoff
616  if (h) {
617    const lines = [
618      field(ui, 'out-state', '状態', handoffState(h)),
619      field(ui, 'out-file', 'ファイル', h.file),
620      field(ui, 'out-id', '目印', `[usage-ledger handoff ${h.id}]`),
621    ]
622    if (h.observed.length > 0) lines.push(field(ui, 'out-observed', '新しいセッション', h.observed.map(short).join(' → ')))
623    blocks.push(section(ui, 'handoff-out', 'このセッションからの引き継ぎ', lines))
624    if (h.status === 'drafted') {
625      blocks.push(inline(ui, 'handoff-out-actions', [ui.Button({ key: 'handoff-cancel', label: '取り消す', ...BUTTON.minor, onPress: () => cancelHandoff($) })]))
626    }
627  }
628  const inc = d.incoming
629  if (inc) {
630    const first = inc.observed[0]
631    blocks.push(
632      section(ui, 'handoff-in', 'このセッションへの引き継ぎ', [
633        field(ui, 'in-result', '文脈', `元 ${short(inc.ctxAtHandoff)} → 初回 ${first == null ? '—' : short(first)}(予測 ${short(inc.predictedCtx)})`),
634        field(ui, 'in-observed', '最初の要求', inc.observed.length ? inc.observed.map(short).join(' → ') : 'まだありません'),
635        field(ui, 'in-id', '目印', inc.id),
636      ]),
637    )
638  }
639  return blocks
640}
641
642function drawView($, ui, d) {
643  switch (d.view) {
644    case 'threads':
645      return drawThreads($, ui, d)
646    case 'kinds':
647      return drawKinds($, ui, d)
648    case 'costly':
649      return drawCostly($, ui, d)
650    case 'gaps':
651      return drawGaps($, ui, d)
652    case 'tools':
653      return drawTools($, ui, d)
654    case 'handoff':
655      return drawHandoff($, ui, d)
656    case 'overview':
657      return drawOverview($, ui, d)
658    default:
659      return drawStatus($, ui, d)
660  }
661}
662
663// ===== The band =====
664
665// The cells across a drawing: the site's own width, else what the surface measured; null when
666// neither is known
667function widthOf(e) {
668  const n = e.props?.bodyColumns
669  if (typeof n === 'number' && n > 0) return n
670  const v = e.viewport?.columns
671  return typeof v === 'number' && v > 0 ? v : null
672}
673
674// The band, one line: the gauge, the stage's word, a short phrase and its button when there is
675// something to do (or a handoff under way), then [詳しく]. The longer details are the pane's.
676// null before the main context is known.
677function drawBand($, ui, s, g, surface, columns) {
678  const { Box, Text, Button } = ui
679  if (g.percent == null) return null
680  let phrase = null
681  let color = null
682  let action = null
683  if (s.handoff === 'drafted') {
684    phrase = '引き継ぎの下書きを入力欄に入れました'
685    action = { key: 'band-cancel', label: '取り消す', ...BUTTON.minor, onPress: () => cancelHandoff($) }
686  } else if (s.handoff === 'done') {
687    phrase = '引き継ぎ済み'
688  } else if (g.advice) {
689    phrase = ADVICE[g.advice]
690    color = g.advice === 'expired' || g.advice === 'switch' ? COLOR.bad : COLOR.warn
691    action = { key: 'band-handoff', label: '引き継ぐ…', ...BUTTON.main, onPress: () => startHandoff($) }
692  }
693  const actions = [...(action ? [action] : []), { key: 'band-details', label: '詳しく', ...BUTTON.nav, onPress: () => openPane($, 'status') }]
694
695  const fit = fitMeter({ surface, stage: g.stage, columns, phrase, buttons: actions.map((a) => a.label) })
696  const children = meterParts(ui, surface, fit, g)
697  if (fit.hasPhrase) {
698    children.push(Box({ key: 'band-sep', flexShrink: 0, children: [Text({ dimColor: true, children: ['·'] })] }))
699    // The only part that shrinks: cut with an ellipsis, never wrapped
700    children.push(Box({ key: 'band-phrase', flexShrink: 1, minWidth: 0, children: [Text({ wrap: 'truncate-end', ...(color ? { color } : {}), children: [phrase] })] }))
701  }
702  children.push(...actions.map((a) => Box({ key: a.key + '-slot', flexShrink: 0, children: [Button(a)] })))
703  return Box({ key: 'usage-ledger-band', flexDirection: 'row', flexWrap: 'nowrap', columnGap: 1, alignItems: 'center', children })
704}
705
706export function registerPane(on) {
707  // /usage-ledger opens the pane at its first view, 状況
708  on('command.run', { command: 'usage-ledger' }, async ($) => {
709    const opened = await openPane($, 'status')
710    return { text: opened.isPlaced ? 'トークンの内訳を開きました' : 'トークンの内訳を開きました(まだ表示されていません)' }
711  })
712
713  on('ui.render', { component: 'Pane', requestId: 'usage-ledger' }, async ($, e) => {
714    const guard = guardDrawing(e.requestId)
715    const ui = guard.wrap($.ui.resolve(e))
716    const d = await gather($, e)
717    return guard.done(page(ui, [drawHeader($, ui, d), ...drawView($, ui, d), dim(ui, 'estimate-note', ESTIMATE_NOTE)]))
718  })
719
720  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
721    const rest = await next(e)
722    if (!getConfig().showBand || e.props.hasSurvey) return rest
723    const { value: requests } = await $.state.get(REQUESTS)
724    const { value: handoff } = await $.state.get(HANDOFF)
725    const s = summarize(requests, await $.clock.now(), { handoff: handoff?.status })
726    const ui = $.ui.resolve(e)
727    const mine = drawBand($, ui, s, gauge(s, { isWorking: e.props.isWorking }), e.surface, widthOf(e))
728    if (!mine) return rest
729    if (!rest) return mine
730    return ui.Box({ flexDirection: 'column', children: [mine, rest] })
731  })
732
733  // A press on this mod's elements (the pane and the band). On the pane
734  // (press-guard.js) it also checks that the press reached the Button's onPress; when the chain
735  // settled, threw, or stayed silent for GRACE_MS without it, the press runs once with the
736  // latest drawing's closure under the same key.
737  on('ui.press', { plugin: 'usage-ledger' }, async ($, e, next) => {
738    const record = beginPress(e)
739    const chain = next(e).then(
740      (value) => ({ kind: 'value', value }),
741      (error) => ({ kind: 'error', error }),
742    )
743    let outcome
744    if (record) {
745      const timer = new AbortController()
746      const grace = $.clock.sleep(GRACE_MS, { signal: timer.signal }).then(
747        () => ({ kind: 'grace' }),
748        () => new Promise(() => {}),
749      )
750      outcome = await Promise.race([chain, grace])
751      timer.abort()
752      if (outcome.kind === 'grace' && hasStarted(record)) outcome = await chain
753    } else {
754      outcome = await chain
755    }
756    if (!record || hasStarted(record)) {
757      if (record) endPress(e, record)
758      if (outcome.kind === 'error') throw outcome.error
759      return outcome.value
760    }
761    try {
762      await takeOver(record, e)
763    } catch {}
764    return { element: e.element }
765  })
766}
767
hooks/backfill.js 141 lines
1// Reading a thread's requests back from its transcript, which records every response with its
2// message id and the exact 5m / 1h split of its cache writes. The live tally (turn.step) is a
3// provisional figure until then.
4//
5// A subagent is read back when its run ends (turn.complete with its agentId). The main thread is
6// read back at the end of each main turn (turn.complete) and before [書き出す]. No event an
7// installed mod is sure to hear carries the transcripts' paths (the classic hooks that did are not
8// passed to installed mods everywhere), so they are found where Claude Code keeps them: the main
9// transcript is `<sessionId>.jsonl` in a folder of `<config>/projects/` named after the session's
10// first working directory (ledger.js projectFolderOf), and a subagent's is
11// `<sessionId>/subagents/agent-<agentId>.jsonl` beside it (subagentTranscriptOf). `<config>` is
12// CLAUDE_CONFIG_DIR, else `.claude` in the home folder. The main path found is kept in $.state
13// `transcriptPath`. Where it is not found, the file is over MAX_TRANSCRIPT_BYTES or it cannot be
14// read, the live values stay and $.state `mainSource` says why.
15
16import { parseThread } from './aggregate.js'
17import { MAX_TRANSCRIPT_BYTES, parseJsonl, replaceThread, mergeMain, transcriptRequests, projectFolderOf, subagentTranscriptOf, joinPath } from './ledger.js'
18import { update } from 'claude-code'
19
20// The state this file reads and writes (declared in types/index.d.ts)
21const REQUESTS = { plugin: 'usage-ledger', key: 'requests' }
22const THREADS = { plugin: 'usage-ledger', key: 'threads' }
23const TRANSCRIPT = { plugin: 'usage-ledger', key: 'transcriptPath' }
24const MAIN_SOURCE = { plugin: 'usage-ledger', key: 'mainSource' }
25
26// A transcript's requests for `thread`, or why there are none: { requests } | { reason }
27async function readTranscript($, path, thread) {
28  if (typeof path !== 'string' || path === '') return { reason: 'noPath' }
29  try {
30    const stat = await $.fs.stat(path)
31    if (typeof stat?.size === 'number' && stat.size > MAX_TRANSCRIPT_BYTES) return { reason: 'tooLarge' }
32  } catch {
33    return { reason: 'unreadable' }
34  }
35  let text
36  try {
37    text = await $.fs.read(path)
38  } catch {
39    return { reason: 'unreadable' }
40  }
41  return { requests: transcriptRequests(parseThread(parseJsonl(text)).requests, thread) }
42}
43
44// Session ids whose project folders were all looked through in this load, so a transcript that
45// is nowhere (a session that keeps none) is not looked for in every folder at each turn's end
46const scanned = new Set()
47
48// `<config>/projects`, or null when neither CLAUDE_CONFIG_DIR nor a home folder is set
49async function projectsDir($) {
50  const config = await $.env.get('CLAUDE_CONFIG_DIR')
51  if (config) return joinPath(config, 'projects')
52  const home = (await $.env.get('USERPROFILE')) || (await $.env.get('HOME'))
53  return home ? joinPath(home, '.claude', 'projects') : null
54}
55
56/**
57 * The main transcript's path, kept in $.state `transcriptPath` once found: the kept one while it
58 * is this session's and is there; else `<sessionId>.jsonl` in the folder named after the session
59 * root or the working directory; else (with `scan`, once per session id in a load) in any project
60 * folder (a long path's folder name is shortened, and the session may have moved). Not found, the
61 * root's folder is kept as where it will be (a new session writes it with its first turn), so the
62 * pane says the read-back waits for a turn's end. Returns the path, or null when no folder is known.
63 */
64export async function findMainTranscript($, { scan = false } = {}) {
65  const sid = await $.session.id()
66  const file = sid + '.jsonl'
67  const { value: kept } = await $.state.get(TRANSCRIPT)
68  const isOwn = typeof kept === 'string' && kept.split(/[\\/]/).at(-1) === file
69  if (isOwn && (await $.fs.exists(kept))) return kept
70  const dir = await projectsDir($)
71  if (dir === null) return isOwn ? kept : null
72  const folders = []
73  for (const where of [await $.session.root(), await $.session.cwd()]) {
74    const name = typeof where === 'string' && where !== '' ? projectFolderOf(where) : ''
75    if (name !== '' && !folders.includes(name)) folders.push(name)
76  }
77  const keep = async (path) => {
78    if (kept !== path) await $.state.set(TRANSCRIPT, path)
79    return path
80  }
81  for (const name of folders) {
82    const path = joinPath(dir, name, file)
83    if (await $.fs.exists(path)) return keep(path)
84  }
85  if (scan && !scanned.has(sid)) {
86    scanned.add(sid)
87    let entries = []
88    try {
89      entries = await $.fs.list(dir)
90    } catch {}
91    for (const entry of Array.isArray(entries) ? entries : []) {
92      if (entry?.kind !== 'dir' || folders.includes(entry.name)) continue
93      const path = joinPath(dir, entry.name, file)
94      if (await $.fs.exists(path)) return keep(path)
95    }
96  }
97  if (isOwn) return kept
98  return folders.length > 0 ? keep(joinPath(dir, folders[0], file)) : null
99}
100
101/** A subagent's transcript path, beside the main one; null while the main one is not known. */
102export async function findSubagentTranscript($, agentId) {
103  const main = await findMainTranscript($)
104  return typeof main === 'string' ? subagentTranscriptOf(main, agentId) : null
105}
106
107/** A finished subagent: its live requests replaced by its transcript's (left when unreadable). */
108export async function backfillSubagent($, agentId, path) {
109  const got = await readTranscript($, path, agentId)
110  if (!got.requests || got.requests.length === 0) return false
111  await update($, REQUESTS, (list) => replaceThread(list, agentId, got.requests))
112  await update($, THREADS, (threads) => ({ ...(threads ?? {}), [agentId]: { ...(threads ?? {})[agentId], isBackfilled: true } }))
113  return true
114}
115
116/**
117 * The main thread: its requests replaced by its transcript's, the live ones it does not have
118 * yet kept (ledger.js mergeMain). `mainSource` records when it last worked, or why not.
119 * Returns whether it was read back.
120 */
121export async function backfillMain($) {
122  let path = null
123  try {
124    path = await findMainTranscript($, { scan: true })
125  } catch {}
126  const got = await readTranscript($, path, 'main')
127  const { value: prev } = await $.state.get(MAIN_SOURCE)
128  if (got.requests && got.requests.length > 0) {
129    await update($, REQUESTS, (list) => mergeMain(list, got.requests))
130    await $.state.set(MAIN_SOURCE, { backfilledAt: await $.clock.now(), requests: got.requests.length, reason: null })
131    return true
132  }
133  const reason = got.reason ?? 'empty'
134  // Written only when it changes, so a failed read does not redraw the pane each time; with no
135  // path there is nothing to record (mainSourceOf tells that from the missing path)
136  if (reason !== 'noPath' && prev?.reason !== reason) {
137    await $.state.set(MAIN_SOURCE, { backfilledAt: prev?.backfilledAt ?? null, requests: prev?.requests ?? 0, reason })
138  }
139  return false
140}
141
hooks/style.js 127 lines
1// The look the pane and the band share: spacing, colors, button roles, and small builders for
2// the recurring blocks (a header, a section, a table row, a key/value field). Pure: each
3// builder takes the element table `$.ui.resolve(e)` handed out (`ui`) and never touches $.
4
5import { cells } from './meter.js'
6
7/** Spacing, in character cells. */
8export const SPACE = {
9  page: 1, // the left and right padding of the pane
10  section: 1, // blank rows between sections
11  inline: 1, // columns between buttons in a row
12  column: 2, // columns between the cells of a table row
13  indent: 2, // the indent of a section's body, and of a row's details
14}
15
16/** Theme color names only, so the surface's theme decides the shade. */
17export const COLOR = {
18  warn: 'warning',
19  bad: 'error',
20  good: 'success',
21}
22
23/**
24 * Button roles, spread into a Button's props.
25 * main: the one action a place leads to, and the picked one of several choices.
26 * nav: moving between views, closing.
27 * minor: an unpicked choice, a row's closed [▸].
28 */
29export const BUTTON = {
30  main: { variant: 'primary' },
31  nav: { variant: 'secondary' },
32  minor: { variant: 'secondary', dimColor: true },
33}
34
35/** The role of a button that is one of several choices: picked is main, the rest minor. */
36export const choice = (isPicked) => (isPicked ? BUTTON.main : BUTTON.minor)
37
38/** Dim text that wraps at the pane's edge. */
39export const dim = (ui, key, text) => ui.Box({ key, children: [ui.Text({ dimColor: true, wrap: 'wrap', children: [text] })] })
40
41/** A row of buttons (or other inline items), wrapping when narrow. */
42export const inline = (ui, key, children, marginTop = 0) =>
43  ui.Box({ key, marginTop, flexDirection: 'row', columnGap: SPACE.inline, rowGap: 1, alignItems: 'center', flexWrap: 'wrap', children })
44
45/** A cell of fixed width holding one line of text, cut with … when too long. */
46export const cell = (ui, width, text, props = {}, align = 'flex-start') =>
47  ui.Box({
48    width,
49    flexShrink: 0,
50    flexDirection: 'row',
51    justifyContent: align,
52    children: [ui.Text({ wrap: 'truncate-end', ...props, children: [String(text)] })],
53  })
54
55/** A cell of fixed width holding elements (a button). */
56export const slot = (ui, width, children) => ui.Box({ width, flexShrink: 0, flexDirection: 'row', children })
57
58/** A table row: cells on one line. */
59export const tableRow = (ui, key, children) =>
60  ui.Box({ key, flexDirection: 'row', columnGap: SPACE.column, alignItems: 'center', width: '100%', children })
61
62/** A key/value line: the name dim in a fixed column, the value wrapping beside it. */
63export const field = (ui, key, name, value, width = 18, props = {}) =>
64  ui.Box({
65    key,
66    flexDirection: 'row',
67    columnGap: SPACE.column,
68    width: '100%',
69    children: [
70      ui.Box({ width, flexShrink: 0, children: [ui.Text({ dimColor: true, children: [name] })] }),
71      ui.Box({ flexGrow: 1, flexShrink: 1, children: [ui.Text({ wrap: 'wrap', ...props, children: [value] })] }),
72    ],
73  })
74
75// The least cells a field's value keeps beside its name; with less, a value too long for one
76// line goes under its name instead of wrapping a few characters a line
77const FIELD_MIN = 20
78
79/**
80 * Whether a field whose name column is `width` cells goes under its name in a body `room`
81 * cells wide (null: unknown, kept beside).
82 */
83export const isStacked = (room, width, value) => {
84  if (room == null) return false
85  const beside = room - width - SPACE.column
86  return beside < FIELD_MIN && cells(value) > beside
87}
88
89/**
90 * A key/value field under its name, for a narrow pane: the name dim on its own line, the value
91 * under it at the full width, indented; `extra` (such as a dim note) under the value.
92 */
93export const stackedField = (ui, key, name, value, props = {}, extra = []) =>
94  ui.Box({
95    key,
96    flexDirection: 'column',
97    width: '100%',
98    children: [
99      ui.Text({ dimColor: true, children: [name] }),
100      ui.Box({ flexDirection: 'column', paddingLeft: SPACE.indent, width: '100%', children: [ui.Text({ wrap: 'wrap', ...props, children: [value] }), ...extra] }),
101    ],
102  })
103
104/** `field` for a body `room` cells wide: beside its name, or under it when narrow (isStacked). */
105export const fieldsFor = (room) => (ui, key, name, value, width = 18, props = {}) =>
106  isStacked(room, width, value) ? stackedField(ui, key, name, value, props) : field(ui, key, name, value, width, props)
107
108/** A section: a bold heading, then its body indented under it. */
109export const section = (ui, key, title, children) =>
110  ui.Box({
111    key,
112    flexDirection: 'column',
113    width: '100%',
114    children: [
115      ui.Text({ bold: true, children: [title] }),
116      ui.Box({ flexDirection: 'column', paddingLeft: SPACE.indent, width: '100%', children }),
117    ],
118  })
119
120/** A row's details, indented under it. */
121export const details = (ui, key, children) =>
122  ui.Box({ key, flexDirection: 'column', paddingLeft: SPACE.indent * 2, width: '100%', children })
123
124/** The whole pane: its blocks one blank row apart, inside the page padding. */
125export const page = (ui, children) =>
126  ui.Box({ flexDirection: 'column', rowGap: SPACE.section, paddingX: SPACE.page, width: '100%', children })
127
hooks/meter.js 130 lines
1// The gauge line, `トークン ■■■■■■□□□□ OK`: a label, a bar with no number, and the stage's
2// word. Drawn the way usage-band draws its meters, so the two look alike in the band: an SVG
3// bar on the desktop app, a text bar elsewhere.
4//
5// On a narrow line the parts give way in a fixed order (fitMeter): the band's short phrase
6// first, then the bar shortens, then the bar goes, then the label; the stage's word and the
7// buttons always stay. Every part but the phrase keeps its width (flexShrink 0), so none of
8// them wraps inside the row.
9//
10// Pure: no $ here, so the hook files keep every $ call themselves.
11
12import { STAGE_LABEL } from './ledger.js'
13
14/** The line's label. */
15export const LABEL = 'トークン'
16
17// The text bar's cells, at full length and shortened
18const BAR_CELLS = { full: 10, short: 5 }
19// The SVG bar's size in pixels, at full length and shortened
20const SVG_BAR = { full: 96, short: 48, height: 10 }
21// Pixels taken as one cell of the surface's monospace metric, to size the SVG bar in cells
22// (an estimate: the desktop app does not report its font's advance)
23const PX_PER_CELL = 8
24// The line's last columns, which the terminal may draw over
25const RESERVED_COLUMNS = 2
26// The fewest cells the phrase keeps, its ellipsis included; below this it is left out
27const MIN_PHRASE_CELLS = 8
28const SVG_COLORS = { success: '#4caf50', warning: '#e0a526', error: '#e5534b', track: 'rgba(128,128,128,0.3)' }
29
30/** The theme color of the bar's fill and of the stage's word, by stage ('ok' leaves the word as it is). */
31const STAGE_COLOR = { ok: 'success', soon: 'warning', switch: 'error' }
32
33/** Character cells a text takes: two for a wide (CJK, full-width) character, one otherwise. */
34export function cells(text) {
35  let n = 0
36  for (const ch of String(text)) {
37    const c = ch.codePointAt(0)
38    n += c >= 0x1100 && (c <= 0x115f || (c >= 0x2e80 && c <= 0xa4cf) || (c >= 0xac00 && c <= 0xd7a3) || (c >= 0xf900 && c <= 0xfaff) || (c >= 0xfe30 && c <= 0xfe4f) || (c >= 0xff00 && c <= 0xff60) || (c >= 0xffe0 && c <= 0xffe6)) ? 2 : 1
39  }
40  return n
41}
42
43/** The cells a Button takes: its label in brackets on a terminal, inside a frame elsewhere (an estimate). */
44export const buttonCells = (surface, label) => cells(label) + (surface === 'terminal' ? 2 : 4)
45
46// The cells the bar takes at a length ('full', 'short')
47const barCells = (surface, length) => (surface === 'desktop' ? Math.ceil(SVG_BAR[length] / PX_PER_CELL) : BAR_CELLS[length])
48
49// How the line may give way, widest first
50const STEPS = [
51  { hasLabel: true, bar: 'full', hasPhrase: true },
52  { hasLabel: true, bar: 'full', hasPhrase: false },
53  { hasLabel: true, bar: 'short', hasPhrase: false },
54  { hasLabel: true, bar: null, hasPhrase: false },
55  { hasLabel: false, bar: null, hasPhrase: false },
56]
57
58/**
59 * Which parts of the line fit in `columns`: { hasLabel, bar ('full' | 'short' | null),
60 * hasPhrase }. `phrase` is the band's short phrase (null for none), `buttons` the labels of the
61 * Buttons after it. Parts sit a column apart. With no width known (0, absent) everything is
62 * drawn; when nothing fits, the narrowest step (the stage's word and the buttons).
63 */
64export function fitMeter({ surface, stage, columns, phrase = null, buttons = [] }) {
65  const steps = STEPS.map((s) => ({ ...s, hasPhrase: s.hasPhrase && phrase != null }))
66  if (!(typeof columns === 'number' && columns > 0)) return steps[0]
67  const room = columns - RESERVED_COLUMNS
68  for (const step of steps) {
69    const widths = [cells(STAGE_LABEL[stage] ?? ''), ...buttons.map((b) => buttonCells(surface, b))]
70    if (step.hasLabel) widths.push(cells(LABEL))
71    if (step.bar) widths.push(barCells(surface, step.bar))
72    if (step.hasPhrase) widths.push(1, Math.min(MIN_PHRASE_CELLS, cells(phrase)))
73    if (widths.reduce((a, b) => a + b, 0) + widths.length - 1 <= room) return step
74  }
75  return steps[steps.length - 1]
76}
77
78/**
79 * The line's parts, to sit in a row: the label, the bar and the stage's word as `fit`
80 * (fitMeter's result) says, each in a keyed Box that does not shrink (a Text carries no key).
81 * `g` is ledger.js gaugeOf's result with a known percent.
82 */
83export function meterParts(ui, surface, fit, g) {
84  const color = STAGE_COLOR[g.stage]
85  const word = STAGE_LABEL[g.stage]
86  const part = (key, child) => ui.Box({ key, flexDirection: 'row', flexShrink: 0, children: [child] })
87  const parts = []
88  if (fit.hasLabel) parts.push(part('meter-label', ui.Text({ wrap: 'truncate-end', children: [LABEL] })))
89  if (fit.bar && surface === 'desktop') {
90    const width = SVG_BAR[fit.bar]
91    parts.push(part('meter-bar', ui.Svg({ source: svgBar(g.percent, color, width), alt: LABEL + ' ' + word, width, height: SVG_BAR.height })))
92  } else if (fit.bar) {
93    parts.push(part('meter-bar', textBar(ui.Text, g.percent, color, BAR_CELLS[fit.bar])))
94  }
95  parts.push(part('meter-stage', ui.Text({ wrap: 'truncate-end', ...(g.stage === 'ok' ? {} : { color }), children: [word] })))
96  return parts
97}
98
99/** How many of a text bar's `total` cells are filled. */
100export const filledCells = (percent, total = BAR_CELLS.full) => Math.round((clamp(percent) / 100) * total)
101
102// Filled cells in the stage's color, the rest dim, nested in one Text so they stay on one line
103function textBar(Text, percent, color, total) {
104  const filled = filledCells(percent, total)
105  return Text({
106    wrap: 'truncate-end',
107    children: [Text({ color, children: ['█'.repeat(filled)] }), Text({ dimColor: true, children: ['░'.repeat(total - filled)] })],
108  })
109}
110
111/** An SVG bar's fill width in pixels, out of `width`. */
112export const filledPixels = (percent, width = SVG_BAR.full) => Math.round((clamp(percent) / 100) * width)
113
114function svgBar(percent, color, width) {
115  const { height } = SVG_BAR
116  const fill = filledPixels(percent, width)
117  return [
118    `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">`,
119    `<clipPath id="c"><rect width="${width}" height="${height}" rx="${height / 2}"/></clipPath>`,
120    `<g clip-path="url(#c)">`,
121    `<rect width="${width}" height="${height}" fill="${SVG_COLORS.track}"/>`,
122    fill > 0 ? `<rect width="${fill}" height="${height}" fill="${SVG_COLORS[color] ?? SVG_COLORS.success}"/>` : '',
123    '</g></svg>',
124  ].join('')
125}
126
127function clamp(percent) {
128  return Math.min(Math.max(percent, 0), 100)
129}
130
hooks/export.js 241 lines
1// The export [書き出す] writes: the session's numbers as a Markdown summary and as one JSON
2// line per request, for the main conversation to read only the part a question needs (the
3// format is explained in the usage-ledger-data skill the plugin ships).
4//
5// Pure: no $ here; pane.js does the writing. Neither file carries conversation text: numbers,
6// model ids, tool names, agent types and the Agent tool's short descriptions only.
7
8import { aggregateThreads, TYPES, isoString, modelWeight, DEFAULT_WEIGHTS } from './aggregate.js'
9import { MAX_REQUESTS, WEIGHTS_NOTE, ESTIMATE_NOTE, getConfig, summarize, toThreads, duration, mainSourceOf } from './ledger.js'
10
11/** The skill that explains the files' format. */
12export const SKILL = 'usage-ledger-data'
13
14/** The .md's `##` sections, in order. */
15export const SECTIONS = ['概要', 'スレッド', '種類とモデル', '高い要求', '主のツール', '待ちと再書込']
16
17/** The .jsonl's fields, in the order each line writes them. */
18export const JSONL_FIELDS = ['ts', 'id', 'thread', 'agentType', 'description', 'model', ...TYPES, 'context', 'weight', 'tools', 'src']
19
20// ---- Names and paths
21
22const isAbsolute = (p) => /^([A-Za-z]:)?[\\/]/.test(p)
23const trimDir = (dir) => String(dir).trim().replace(/\\/g, '/').replace(/\/+$/, '')
24
25/**
26 * The pair's paths: `shown` is what the toast and the prompt line name (relative to the
27 * project unless exportDir is absolute), `md`/`jsonl` what $.fs.write takes (under `root`, the
28 * session's project root, when relative). The date is the local day counting started, so a
29 * session keeps one pair across midnight.
30 */
31export function exportPaths({ dir, root, sessionId, startedAt, tzOffsetMinutes = 0 }) {
32  const d = trimDir(dir || getConfig().exportDir) || '.'
33  const day = isoString(startedAt + tzOffsetMinutes * 60000).slice(0, 10).replace(/-/g, '')
34  const sid = String(sessionId ?? '').replace(/[^A-Za-z0-9_-]/g, '').slice(0, 8) || 'session'
35  const name = `${day}-${sid}`
36  const shownBase = d === '.' ? name : `${d}/${name}`
37  const base = isAbsolute(d) || !root ? shownBase : `${String(root).replace(/[\\/]+$/, '')}/${shownBase}`
38  return { name, shown: { md: shownBase + '.md', jsonl: shownBase + '.jsonl' }, md: base + '.md', jsonl: base + '.jsonl' }
39}
40
41/** The line [書き出す] adds to the prompt box. */
42export const promptLine = (shownMd) => `usage-ledger の集計を ${shownMd} に書き出しました。`
43
44// ---- Formatting
45
46/** `2026-10-03T09:00:00.000+09:00`: local time with its offset, so a grep by hour is local. */
47export function isoLocal(ms, tzOffsetMinutes = 0) {
48  const sign = tzOffsetMinutes < 0 ? '-' : '+'
49  const a = Math.abs(tzOffsetMinutes)
50  const zone = `${sign}${String(Math.floor(a / 60)).padStart(2, '0')}:${String(a % 60).padStart(2, '0')}`
51  return isoString(ms + tzOffsetMinutes * 60000).slice(0, 23) + zone
52}
53
54/** `2026-10-03 09:00`, local. */
55const when = (ms, tz) => (Number.isFinite(ms) ? isoString(ms + tz * 60000).slice(0, 16).replace('T', ' ') : '—')
56const int = (n) => (Number.isFinite(n) ? String(Math.round(n)) : '—')
57const share = (part, whole) => (whole ? ((100 * part) / whole).toFixed(1) + '%' : '—')
58// A table cell: one line, its pipes escaped
59const esc = (v) => String(v ?? '').replace(/\r?\n/g, ' ').replace(/\|/g, '\\|')
60const row = (cells) => `| ${cells.map(esc).join(' | ')} |`
61function table(head, rows) {
62  const out = [row(head), row(head.map(() => '---'))]
63  if (rows.length === 0) out.push(row(head.map((_, i) => (i === 0 ? '(なし)' : ''))))
64  else for (const r of rows) out.push(row(r))
65  return out
66}
67
68/** The weight of one request, as aggregate.js weighs it. */
69export function weightOf(tok, model, W = DEFAULT_WEIGHTS) {
70  return modelWeight(model, W) * TYPES.reduce((n, t) => n + W.type[t] * (tok?.[t] || 0), 0)
71}
72
73const contextOf = (t) => (t.input || 0) + (t.cache_read || 0) + (t.cache_write_5m || 0) + (t.cache_write_1h || 0)
74
75// ---- The two files
76
77/**
78 * The .jsonl: one line per request, oldest first, as they are in state. `id` is the response's
79 * message id for a request read back from its transcript (`src` 'transcript'), `turnId:index`
80 * for a live one (`src` 'live'). metas: $.state `threads`.
81 */
82export function exportJsonl(requests, metas, tzOffsetMinutes = 0) {
83  const lines = [...(requests ?? [])]
84    .sort((a, b) => a.ts - b.ts)
85    .map((q) => {
86      const isMain = q.thread === 'main'
87      const meta = isMain ? {} : metas?.[q.thread] ?? {}
88      const tok = Object.fromEntries(TYPES.map((t) => [t, q.tok?.[t] || 0]))
89      return JSON.stringify({
90        ts: isoLocal(q.ts, tzOffsetMinutes),
91        id: String(q.id ?? ''),
92        thread: isMain ? 'main' : String(q.thread),
93        agentType: isMain ? '(主)' : meta.agentType || '?',
94        description: isMain ? '' : meta.description || '',
95        model: q.model ?? '?',
96        ...tok,
97        context: contextOf(tok),
98        weight: Math.round(weightOf(tok, q.model)),
99        tools: Array.isArray(q.tools) ? q.tools.map(String) : [],
100        src: q.src === 'transcript' ? 'transcript' : 'live',
101      })
102    })
103  return lines.length ? lines.join('\n') + '\n' : ''
104}
105
106/**
107 * The .md summary. d: { requests, threads (metas), startedAt, now, tzOffsetMinutes, sessionId,
108 * jsonlName (the .jsonl's file name, mentioned at the top), main (ledger.js mainSourceOf; unset
109 * means not read back) }
110 */
111export function exportMarkdown(d) {
112  const tz = d.tzOffsetMinutes ?? 0
113  const requests = d.requests ?? []
114  const agg = aggregateThreads(toThreads(requests, d.threads), { tzOffsetMinutes: tz, top: 15 })
115  const s = summarize(requests, d.now)
116  const total = agg.total.weighted
117  const keyOf = (t) => (t === 'main' ? 'main' : String(t))
118  const out = []
119
120  out.push('# usage-ledger の集計', '')
121  out.push(`この形式の説明は、スキル ${SKILL} にあります。`, '')
122  out.push(`- 書き出し: ${when(d.now, tz)}(${isoLocal(d.now, tz).slice(23)})`)
123  const source = d.main ?? mainSourceOf(null)
124  if (source.isBackfilled && source.isCurrent) {
125    out.push(`- 集計範囲: セッション全体から書き出しまで(主スレッドは記録から読み直した値、${when(source.at, tz)} に読み直した)`)
126  } else if (source.isBackfilled) {
127    // Read back once, not since: main after that read-back is provisional
128    out.push(`- 集計範囲: セッション全体から書き出しまで(主スレッドは ${when(source.at, tz)} までは記録から読み直した値、それより後は速報値)`)
129  } else {
130    out.push(`- 集計範囲: 集計開始 ${when(d.startedAt, tz)} から書き出しまで。集計開始は usage-ledger が読み込まれた時刻で、それより前の主スレッドの要求は含まない`)
131  }
132  out.push(`- 主スレッドの数え方: ${source.method}`)
133  if (requests.length >= MAX_REQUESTS) out.push(`- 要求は新しい ${MAX_REQUESTS} 件だけを残している(古いものは捨てた)`)
134  out.push(`- セッション: ${d.sessionId ?? '—'}`)
135  if (d.jsonlName) out.push(`- 要求ごとの記録: ${d.jsonlName}(1 行 1 要求)`)
136  out.push(`- 目次: ${SECTIONS.join(' / ')}`)
137  out.push(`- 重み: ${WEIGHTS_NOTE}。サブスクの計算式は非公開なので、あくまで推計`)
138  out.push('- 数字: トークン数と重みは丸めない整数、割合は重みの割合、時刻は書き出した PC のローカル時刻', '')
139
140  // ----- 概要
141  out.push('## 概要', '', '### 今の文脈', '')
142  const now = []
143  if (s.context == null) {
144    now.push(['主の文脈', 'まだ主スレッドの要求がない'])
145  } else {
146    const be = s.breakEven
147    now.push(['主の文脈', `${s.context}(${s.model})`])
148    now.push(['キャッシュ', s.isExpired ? `切れた(${duration(d.now - s.expiresAt)} 前)` : `残り ${duration(s.remainingMs)}(${getConfig().ttlMain} で計算)`])
149    now.push(['次の1回の重み', int(be.next)])
150    now.push(['切れたら再書込の重み', int(be.rewrite)])
151    now.push([
152      '新しいセッション',
153      be.applies ? (be.runs === 0 ? 'キャッシュが切れたので、今なら新しいセッションのほうが安い' : `${be.runs} 回の要求で回収できる`) : '今の文脈が新しいセッションの初期文脈より小さく、引き継ぐ得はない',
154    ])
155  }
156  out.push(...table(['項目', '値'], now), '')
157
158  const main = agg.threads.find((t) => t.key === 'main') ?? { requests: 0, weighted: 0 }
159  const tax = agg.gapTax.expired
160  out.push('### 合計', '')
161  out.push(
162    ...table(
163      ['項目', '値'],
164      [
165        ['要求', `${agg.total.requests}(主 ${main.requests}・サブ ${agg.total.requests - main.requests})`],
166        ['重み計', `${int(total)}(主 ${share(main.weighted, total)}・サブ ${share(total - main.weighted, total)})`],
167        ['キャッシュ読みの割合', share(agg.total.cacheHitRatio, 1)],
168        ['期限切れ後の再書込', `${tax.requests} 回・重み ${int(tax.weighted)}(全体の ${share(tax.weighted, total)})`],
169      ],
170    ),
171    '',
172  )
173  out.push('### トークンの種類別', '')
174  out.push(...table(['種類', 'トークン', '重み', '割合'], agg.byType.map((r) => [r.type, r.tokens, int(r.weighted), share(r.weighted, total)])), '')
175
176  // ----- スレッド
177  out.push('## スレッド', '', '重みの大きい順。スレッドは main か、サブエージェントの agentId。', '')
178  out.push(
179    ...table(
180      ['スレッド', '種類', '説明', 'モデル', '要求', ...TYPES, '重み', '割合'],
181      agg.threads.map((t) => [keyOf(t.key), t.agentType, t.description, t.models.join(', ') || '—', t.requests, ...TYPES.map((k) => t.tok[k]), int(t.weighted), share(t.weighted, total)]),
182    ),
183    '',
184  )
185
186  // ----- 種類とモデル
187  out.push('## 種類とモデル', '', '### エージェントの種類別', '')
188  out.push(
189    ...table(
190      ['種類', '回', '要求', ...TYPES, '重み', '割合'],
191      agg.byAgentType.map((r) => [r.key, r.runs, r.requests, ...TYPES.map((k) => r.tok[k]), int(r.weighted), share(r.weighted, total)]),
192    ),
193    '',
194  )
195  out.push('### モデル別', '')
196  out.push(
197    ...table(
198      ['モデル', '要求', ...TYPES, '重み', '割合'],
199      agg.byModel.map((r) => [r.key, r.requests, ...TYPES.map((k) => r.tok[k]), int(r.weighted), share(r.weighted, total)]),
200    ),
201    '',
202  )
203
204  // ----- 高い要求
205  out.push('## 高い要求', '', `重みの大きい要求(全スレッド、上位 ${agg.biggest.length})。文脈は入力とキャッシュの読み書きの合計、書込はキャッシュ書き込み。`, '')
206  out.push(
207    ...table(
208      ['時刻', 'スレッド', 'モデル', '文脈', '書込', '出力', '重み', 'ツール'],
209      agg.biggest.map((q) => [when(q.ts, tz), keyOf(q.thread), q.model, q.context, q.write, q.output, int(q.weighted), q.tools.join(', ') || '(文字だけ)']),
210    ),
211    '',
212  )
213
214  // ----- 主のツール
215  out.push('## 主のツール', '', '主スレッドの要求を、その応答が呼んだツールで分けたもの。text は文字だけの応答、複数は + でつなぐ。', '')
216  out.push(...table(['ツール', '要求', '重み', '主の中'], agg.main.byTool.map((r) => [r.key, r.requests, int(r.weighted), share(r.weighted, agg.main.weighted)])), '')
217
218  // ----- 待ちと再書込
219  const gt = agg.gapTax
220  out.push('## 待ちと再書込', '', '同じスレッドで直前の要求から 5 分を超えて空いた要求。TTL は直前の要求の書き込みで判断(ライブで数えた分の 5m / 1h は設定による推定)。状態 expired はキャッシュが切れた後、within は期限内。', '')
221  out.push(
222    ...table(
223      ['状態', '要求', '書込', '重み', '割合'],
224      [
225        ['expired', gt.expired.requests, gt.expired.write, int(gt.expired.weighted), share(gt.expired.weighted, total)],
226        ['within', gt.within.requests, gt.within.write, int(gt.within.weighted), share(gt.within.weighted, total)],
227      ],
228    ),
229    '',
230  )
231  out.push(
232    ...table(
233      ['時刻', 'スレッド', '待ち秒', 'TTL', '状態', '書込', '重み'],
234      agg.gaps.map((g) => [when(g.ts, tz), keyOf(g.thread), Math.round(g.gapSeconds), g.ttlSeconds === 3600 ? '1h' : '5m', g.state, g.write, int(g.weighted)]),
235    ),
236    '',
237  )
238  out.push(ESTIMATE_NOTE.replace(/^\*/, ''), '')
239  return out.join('\n')
240}
241
hooks/press-guard.js 97 lines
1// Runs a pane's Button press again when it did not reach the Button's onPress (copied from
2// ui-sampler, without its diagnostics log).
3//
4// A Button's onPress stays in this mod's environment under a handle the host holds for the
5// life of one drawing. A pane is drawn again whenever its props change, so a press that comes
6// in just as the drawing it was aimed at is replaced (the first click on an unfocused pane
7// focuses it, and `isFocused` changes) can find its handle gone and never reach the closure.
8//
9// So each guarded drawing keeps its own Buttons' closures here, by requestId and key, and wraps
10// each onPress so that a run of it is seen. The ui.press hook (pane.js) opens a press record
11// before next(e); if the chain settles, fails, or stays silent past a grace without the
12// Button's onPress having started, the hook takes the press over and runs the closure the
13// latest drawing holds under the same key. A press runs at most once: a closure the engine
14// starts after the take-over finds the record taken and does nothing.
15//
16// Pure: no $ here, so the hook files keep every $ call themselves.
17
18/** How long the hook waits for the press to reach an onPress before it takes it over. */
19export const GRACE_MS = 2000
20
21// The closures of the latest finished drawing, by requestId, then key
22const drawn = new Map()
23// The press in flight per `requestId key`: { started, takenOver }
24const pending = new Map()
25
26const slot = (requestId, key) => requestId + ' ' + key
27
28/**
29 * Starts one drawing of the pane `requestId`. `wrap(table)` hands back the element table with
30 * its Button keeping each onPress here; `done(tree)` makes this drawing the latest and returns
31 * the tree. A drawing that fails before done() leaves the previous one in place.
32 */
33export function guardDrawing(requestId) {
34  const handlers = new Map()
35  return {
36    wrap(table) {
37      const Button = props => {
38        const onPress = props.onPress
39        if (typeof onPress !== 'function') return table.Button(props)
40        handlers.set(props.key, onPress)
41        return table.Button({ ...props, onPress: press => runGuarded(requestId, props.key, onPress, press) })
42      }
43      return { ...table, Button }
44    },
45    done(tree) {
46      drawn.set(requestId, handlers)
47      return tree
48    },
49  }
50}
51
52// The onPress the surface runs: marks the press as reached, unless the hook already took it
53// over, in which case it does nothing (the press has run once already)
54function runGuarded(requestId, key, onPress, press) {
55  const record = pending.get(slot(requestId, key))
56  if (record?.takenOver) return undefined
57  if (record) record.started = true
58  return onPress(press)
59}
60
61/**
62 * Opens the record of one press before next(e). Undefined when the latest drawing of that
63 * pane has no guarded Button under the key (another site, or a key this mod does not keep):
64 * such a press is only watched.
65 */
66export function beginPress(e) {
67  if (!drawn.get(e.requestId)?.has(e.element)) return undefined
68  const record = { started: false, takenOver: false }
69  pending.set(slot(e.requestId, e.element), record)
70  return record
71}
72
73/** Whether the press has reached (started) its Button's onPress. */
74export function hasStarted(record) {
75  return record.started
76}
77
78/**
79 * Takes the press over and runs the latest drawing's closure for its key with `press` (the
80 * ui.press argument). Marks the record first, so the engine's own onPress, if it still
81 * comes, does nothing. Resolves to what the closure returned.
82 */
83export function takeOver(record, press) {
84  record.takenOver = true
85  const onPress = drawn.get(press.requestId)?.get(press.element)
86  return onPress ? onPress(press) : undefined
87}
88
89/**
90 * Closes the record of a press the engine ran itself. A taken-over record stays until the
91 * next press on the key replaces it, so the engine's own onPress, arriving late, still finds it.
92 */
93export function endPress(press, record) {
94  const key = slot(press.requestId, press.element)
95  if (!record.takenOver && pending.get(key) === record) pending.delete(key)
96}
97
types/index.d.ts 101 lines
1// The values usage-ledger keeps in $.state for the session. Small summaries that outlive the
2// session (handoff records, observed context sizes) go to $.store instead.
3
4/** The five token kinds of one request, the cache writes split by their assumed lifetime. */
5export type UsageLedgerTokens = {
6  input: number
7  cache_read: number
8  cache_write_5m: number
9  cache_write_1h: number
10  output: number
11}
12
13/**
14 * One model request: `id` is `turnId:index` (live) or the response's message id (read back
15 * from the thread's transcript), `ts` epoch ms, `thread` 'main' or the subagent's agentId.
16 */
17export type UsageLedgerRequest = {
18  id: string
19  ts: number
20  model: string
21  tools: string[]
22  tok: UsageLedgerTokens
23  thread: string
24  /** 'transcript' when read back from the transcript; unset for a live one (turn.step). */
25  src?: 'transcript'
26  /** A live one: when its step started, epoch ms. */
27  sentAt?: number
28}
29
30/** What is known of a subagent, by its agentId. */
31export type UsageLedgerThread = {
32  agentType?: string
33  description?: string
34  /** True once its requests were replaced by the ones its transcript records. */
35  isBackfilled?: boolean
36}
37
38/**
39 * How the main thread was last read back from its transcript. Unset before the first try.
40 * `reason` null: the last try worked; else why it did not ('empty': no request in it yet).
41 */
42export type UsageLedgerMainSource = {
43  /** When the last read-back that worked was, epoch ms; null when none has. */
44  backfilledAt: number | null
45  /** How many requests that read-back found. */
46  requests: number
47  reason: 'tooLarge' | 'unreadable' | 'empty' | null
48}
49
50/** The pane's views. */
51export type UsageLedgerView = 'status' | 'overview' | 'threads' | 'kinds' | 'costly' | 'gaps' | 'tools' | 'handoff'
52
53/** A handoff this session drafted: 'drafted' once the prompt box holds it, 'done' once a new session reported in. */
54export type UsageLedgerHandoff = {
55  id: string
56  status: 'drafted' | 'done'
57  createdAt: number
58  file: string
59  ctxAtHandoff: number | null
60  predictedCtx: number
61  /** The new session's context on its first requests, as it stored them. */
62  observed: number[]
63}
64
65/** The handoff this session was started from: its first requests' context goes back to the store. */
66export type UsageLedgerIncoming = {
67  id: string
68  fromSession: string | null
69  ctxAtHandoff: number | null
70  predictedCtx: number | null
71  observed: number[]
72}
73
74declare module 'claude-code' {
75  interface PluginState {
76    'usage-ledger': {
77      /**
78       * The requests: the transcripts' once read back (the whole session's for main), else
79       * every one seen since `startedAt`; oldest first, capped. Unset before the first.
80       */
81      requests: UsageLedgerRequest[]
82      /** Subagents by agentId. */
83      threads: Record<string, UsageLedgerThread>
84      /** When counting started (the first load in this session), epoch ms. */
85      startedAt: number
86      /** The main thread's transcript, absolute, as the classic hooks name it. Unset when none did. */
87      transcriptPath: string
88      /** Unset before the main thread was first tried to be read back. */
89      mainSource: UsageLedgerMainSource
90      /** Unset means 'status'. */
91      view: UsageLedgerView
92      /** The row whose details are open, by view; '' or unset means none. */
93      open: StateFamily<string>
94      /** Unset or null: no handoff drafted here. */
95      handoff: UsageLedgerHandoff | null
96      /** Unset or null: this session was not started from a handoff. */
97      incoming: UsageLedgerIncoming | null
98    }
99  }
100}
101