@agentguard-run/burn 0.3.24 → 0.3.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +17 -2
- package/dist/src/cli.js +33 -0
- package/dist/src/history/claude-transcript.d.ts +17 -6
- package/dist/src/history/claude-transcript.js +29 -32
- package/dist/src/history/subagent-signals.d.ts +56 -0
- package/dist/src/history/subagent-signals.js +65 -0
- package/dist/src/insights/calendar-render.js +5 -2
- package/dist/src/insights/calendar.d.ts +27 -0
- package/dist/src/insights/calendar.js +74 -5
- package/dist/src/insights/card.d.ts +34 -0
- package/dist/src/insights/card.js +225 -0
- package/dist/src/insights/i18n.d.ts +23 -4
- package/dist/src/insights/i18n.js +9 -3
- package/dist/src/insights/render.d.ts +19 -0
- package/dist/src/insights/render.js +31 -1
- package/dist/src/insights/transcript.js +26 -0
- package/dist/src/insights/types.d.ts +20 -0
- package/dist/src/subagent-cost.d.ts +27 -10
- package/dist/src/subagent-cost.js +21 -10
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.26 (2026-10-02)
|
|
4
|
+
|
|
5
|
+
- Sub-agents that started, used tokens and ended without finishing now show up in `why`, `calendar` and the STOP line. A sub-agent counts only on terminal evidence: Claude Code recorded the status of its task notification as `failed`, `killed` or `stopped`, and never recorded it as `completed` or resumed (a SendMessage result that started it again). One launched in the background with no end recorded may still be running, so it never counts. A launch refused before it started (a hook, a permission rule, your no) has no transcript and used no tokens, so it is not counted either. Burn has no rule for a session being over, so it relies on these records alone. It knows whether a sub-agent finished, never whether its work was right, so every line says "ended without finishing", never "failed".
|
|
6
|
+
- `why` adds one line after the observed calls when at least one did: `Sub-agents that ended without finishing: 2 of 5, 275,000 tokens (58.9% of session tokens, $0.75 API list equivalent).` The count is out of every sub-agent transcript in the session's `subagents` folder, the tokens are their own responses counted once as everywhere in `why`, and the dollars are left out when a model is unpriced. A sub-agent's own sub-agents count by what its transcript records.
|
|
7
|
+
- `calendar` adds the same totals across every session, on one more summary line: `2 of 5 sub-agents ended without finishing (275,000 tokens, 58.9% of tokens, $0.75 at API prices)`. Each sub-agent counts once, on its first day with tokens, with all of its tokens, so the count and the tokens describe the same sub-agents. The daily count keeps them after the transcripts are deleted. Days an earlier Burn counted carry no sub-agent counts, so their sub-agents are in neither number. `calendar json` gains `subagents`, `unfinishedSubagents`, `unfinishedTokens` and `unfinishedUsd` in `totals`.
|
|
8
|
+
- The STOP line about finished sub-agents gains a sentence when the session's transcripts record any that ended without finishing: `The 15 sub-agents that finished in this session averaged 10.0M tokens each ($4.99 at API prices, not a bill). 2 more ended without finishing and used 3.1M tokens in total. This one could differ.` It still needs three finished sub-agents, and the hook still reads no transcript for it: the ids come from the reading it already does and are saved beside the completed ids, as `ended` and `resumed` in the saved cursor. A cursor saved by an earlier Burn loads with neither, so a sub-agent whose end it had already read is not counted.
|
|
9
|
+
- Spanish and Portuguese say the same, in the same tone: "quedaron sin terminar", "pararam sem terminar".
|
|
10
|
+
- Internals: one function, `subagentSignals` in `src/history/subagent-signals.ts`, reads these records for the hook, `why` and `calendar`, so the three can't disagree. It reads a notification's ids and status only from its header, before the summary and the sub-agent's own result, so a result that quotes a notification can never become an id or a status. A notification that lists several tasks applies its status to each; the finished line reads completions the same way. `why json` gains `subagents` (the ids it counted from) and, on a transcript that records any, `state.subagentSignals`; a session with neither prints the same json as 0.3.25. The calendar's cache is rebuilt once (schema 4).
|
|
11
|
+
- Tests: 536 (535 pass, 1 skipped), up from 517.
|
|
12
|
+
|
|
13
|
+
## 0.3.25 (2026-10-01)
|
|
14
|
+
|
|
15
|
+
- `card` draws the calendar's totals as one 1200 by 675 PNG to post: tokens with the Claude Code and Codex split, dollars at API list price (not a bill), the sub-agent share, the busiest day, the launches AgentGuard held, and one bar per day in whole weeks. `--weeks N` (1 to 53, default 4) and `--out file.png` (default `agentguard-burn-card.png`). It is drawn locally from the figures `calendar` prints; no project, prompt or file name appears on it, and nothing is uploaded.
|
|
16
|
+
|
|
3
17
|
## 0.3.24 (2026-09-29)
|
|
4
18
|
|
|
5
19
|
- `calendar` ends with one next step, so it never stops at a number with nothing to do about it. The rule is the one `why` uses for its tip: sub-agents at a fifth or more of the tokens point at the free plugin, or at giving related work to one sub-agent once AgentGuard has held launches on this machine; anything else points at `why`, which names the change for one session. In English, Spanish and Portuguese, and short enough that the command never wraps at 80 columns.
|
package/README.md
CHANGED
|
@@ -213,6 +213,8 @@ The setup and the re-read conversation split each call's cached reads at the siz
|
|
|
213
213
|
|
|
214
214
|
After a `/compact`, an automatic compaction, a model switch, or a `/clear` that the transcript records, `why` adds one measured line before the tip, for example `Measured change: after /compact on 2026-09-27 at 14:02 UTC, Claude read a median of 85K tokens per message, down from 660K in the 20 messages before.` It compares the median tokens per message of the main conversation in the N messages after the latest such event with the N before it: N is 20, fewer when the session or the next or previous event leaves fewer, and with fewer than 5 on either side that event says nothing. It states a measurement next to an event, never a saving or a cause, and prints whether or not tips are on.
|
|
215
215
|
|
|
216
|
+
When the session's transcripts record sub-agents that ended without finishing, `why` adds one line after the observed calls, for example `Sub-agents that ended without finishing: 2 of 5, 275,000 tokens (58.9% of session tokens, $0.75 API list equivalent).` A sub-agent counts only when Claude Code recorded its status as `failed`, `killed` or `stopped`, and never as `completed` or resumed. One launched in the background with no end recorded may still be running, so it never counts. The count is out of every sub-agent transcript in the session's `subagents` folder, the tokens are their own responses counted once, and the dollars are left out when a model is unpriced. Burn knows whether a sub-agent finished, never whether its work was right. `why json` lists the ids it counted from under `subagents`.
|
|
217
|
+
|
|
216
218
|
`why` prints in Latin American Spanish or Brazilian Portuguese when your machine is set to that language, and `--lang es`, `--lang pt` or `--lang en` picks one explicitly. The order is `--lang`, then `AGENTGUARD_LANG`, then `LC_ALL`, `LC_MESSAGES` and `LANG` (the first one set decides, as POSIX says, so `LC_ALL=C` means English), then the locale Node resolves. Every other language is English. Figures keep their en-US formatting in every language (`1,234,567`, `$1335.37`, `12.3%`), so a translated report matches screenshots digit for digit. `why`, `compare` and `calendar` are translated. Their `json` output is the same in every language, and every other command stays English.
|
|
217
219
|
|
|
218
220
|
Later cache reads up to the baseline go to instruction stack + system. Reads above it go to history re-sent. Fresh input plus cache creation is the measured arriving increment. Burn subtracts the previous response's output before assigning that increment to intervening tool results and user messages. Result byte sizes only split that measured total; bytes are never converted into tokens. A result for a previously read Read path goes to re-read files. Mixed user and tool intervals are marked shared and counted in the footer.
|
|
@@ -287,7 +289,7 @@ agentguard-burn calendar json
|
|
|
287
289
|
|
|
288
290
|
Every Claude Code and Codex session on this machine on one heatmap, sub-agents included: a column per week, a row per weekday, darker for more tokens. Each response's tokens and API price come from the parser and prices `why` uses, counted on the local calendar day the response was recorded. A response recorded in two files (a sub-agent's copy of its parent's last reply, a project that moved, a forked session) counts once, merged the way `why` merges a session. A forked Codex session whose copied history has no verifiable boundary is left out and named in a note, as `why` reports it unavailable.
|
|
289
291
|
|
|
290
|
-
The summary is
|
|
292
|
+
The summary is at most four short lines, however wide the terminal: the total tokens and the dollars at API prices (not a bill; from $1,000 in whole dollars, such as `$28,252 to $28,254 at API prices, not a bill`, and one number when both ends round to the same dollar); the busiest day and the share of tokens sub-agents used (every token of their responses); only when there were any, how many sub-agents ended without finishing, out of all of them, with their tokens, share and dollars, by the rule `why` uses (each sub-agent counts once, on its first day with tokens, with all of its tokens, and days an earlier Burn counted carry no sub-agent counts); and, only when there were any, how many sub-agent launches AgentGuard held: refused by an enforced STOP, or held for your yes, as the decisions ledger records them. On a narrow terminal a line's two items take a line each. A day is amber when sub-agents used more than half of its tokens, and carries a red mark when AgentGuard held a launch that day. The five shades run from Less to More: a day's level is the quarter of the shown days with tokens that it reaches, so the busiest day shown is always the darkest. Tokens on a model without a verified rate are counted but left out of the dollars, and a note names the model.
|
|
291
293
|
|
|
292
294
|
With no flag the calendar starts at the oldest recorded day, capped to the weeks that fit the terminal (its width on a terminal, 80 columns when piped, as when Claude Code runs `! npx agentguard-burn calendar`); `--weeks N` shows exactly N weeks, 1 to 53, ending with this week.
|
|
293
295
|
|
|
@@ -324,6 +326,15 @@ Claude Code deletes transcripts after 30 days by default (`cleanupPeriodDays`),
|
|
|
324
326
|
|
|
325
327
|
`calendar json` is never translated: every day of the span with its figures and level, the totals, the busiest day and where the figures came from. `--lang es|pt` and your locale choose the language exactly as for `why`. Nothing in the calendar compares you with anyone else.
|
|
326
328
|
|
|
329
|
+
## Share card
|
|
330
|
+
|
|
331
|
+
```sh
|
|
332
|
+
agentguard-burn card
|
|
333
|
+
agentguard-burn card --weeks 1 --out this-week.png
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
One 1200 by 675 PNG with the calendar's totals, made to post: the token total with the Claude Code and Codex split, the dollars at API prices (not a bill), the share sub-agents used, the busiest day, the sub-agent launches AgentGuard held (or the transcripts read, when it held none) and one bar per day in whole weeks. `--weeks N` takes 1 to 53 and defaults to 4. The image is saved as `agentguard-burn-card.png` in the current folder unless `--out` names another file. It is drawn on your machine from the same figures `calendar` prints; no project, prompt or file name appears on it, and nothing is uploaded. A span with no recorded usage is refused.
|
|
337
|
+
|
|
327
338
|
## Two safety planes
|
|
328
339
|
|
|
329
340
|
Structural limits use active time rather than the age of the session. The
|
|
@@ -464,7 +475,11 @@ The third line appears once at least three of the session's sub-agents have
|
|
|
464
475
|
finished: what they averaged, measured from their own transcripts as the hook
|
|
465
476
|
reads them anyway (never a second pass), and never a forecast. With fewer, the
|
|
466
477
|
prompt is the other three lines. The box of a refused launch carries the same
|
|
467
|
-
line.
|
|
478
|
+
line. When the transcripts also record sub-agents that ended without finishing
|
|
479
|
+
(status `failed`, `killed` or `stopped`, and never `completed` or resumed), the
|
|
480
|
+
line adds how many and the tokens they used, for example `2 more ended without
|
|
481
|
+
finishing and used 3.1M tokens in total.` A sub-agent that may still be running
|
|
482
|
+
never counts.
|
|
468
483
|
|
|
469
484
|
You answer with one keypress. Claude never sees that text, and auto mode
|
|
470
485
|
cannot approve it silently (Claude Code 2.1.211 and later), so the answer is
|
package/dist/src/cli.js
CHANGED
|
@@ -311,6 +311,38 @@ async function main(argv) {
|
|
|
311
311
|
show({ kind: 'calendar', report, lang: language, columns }, json ? JSON.stringify(report, null, 2) + '\n' : renderCalendar(report, language, columns) + '\n', json);
|
|
312
312
|
return 0;
|
|
313
313
|
}
|
|
314
|
+
case 'card': {
|
|
315
|
+
// `--weeks N` (1 to 53, default 4, also --weeks=N) and `--out file.png` (default agentguard-burn-card.png here).
|
|
316
|
+
let weeksText, out = 'agentguard-burn-card.png', bad = false;
|
|
317
|
+
for (let i = 0; i < rest.length; i++) {
|
|
318
|
+
if (rest[i] === '--weeks')
|
|
319
|
+
weeksText = rest[++i];
|
|
320
|
+
else if (rest[i].startsWith('--weeks='))
|
|
321
|
+
weeksText = rest[i].slice('--weeks='.length);
|
|
322
|
+
else if (rest[i] === '--out')
|
|
323
|
+
out = rest[++i] ?? '';
|
|
324
|
+
else if (rest[i].startsWith('--out='))
|
|
325
|
+
out = rest[i].slice('--out='.length);
|
|
326
|
+
else
|
|
327
|
+
bad = true;
|
|
328
|
+
}
|
|
329
|
+
const weeks = weeksText === undefined ? 4 : /^\d{1,2}$/.test(weeksText) ? Number(weeksText) : 0;
|
|
330
|
+
if (bad || !(weeks >= 1 && weeks <= 53) || !out.trim()) {
|
|
331
|
+
process.stderr.write('Usage: agentguard-burn card [--weeks N] [--out file.png]\n');
|
|
332
|
+
return 64;
|
|
333
|
+
}
|
|
334
|
+
const { calendarReport, collectCalendar } = await Promise.resolve().then(() => __importStar(require('./insights/calendar')));
|
|
335
|
+
const { cardModel, renderCard } = await Promise.resolve().then(() => __importStar(require('./insights/card')));
|
|
336
|
+
const data = collectCalendar({ home: HOME, rates: (0, live_1.readPricing)(HOME, (0, pre_tool_use_1.loadPolicy)(HOME)) });
|
|
337
|
+
const report = calendarReport(data, { now: Date.now(), weeks });
|
|
338
|
+
if (!(report.totals.tokens > 0)) {
|
|
339
|
+
process.stderr.write('No Claude Code or Codex usage is recorded on this machine for that span, so there is nothing to put on a card.\n');
|
|
340
|
+
return 1;
|
|
341
|
+
}
|
|
342
|
+
(0, node_fs_1.writeFileSync)(out, await renderCard(cardModel(report)));
|
|
343
|
+
process.stdout.write(`Card saved: ${out} (1200x675). Dollars on it are API list prices, not a bill; nothing was uploaded.\n`);
|
|
344
|
+
return 0;
|
|
345
|
+
}
|
|
314
346
|
case 'quiet': {
|
|
315
347
|
const [state, ...extra] = rest;
|
|
316
348
|
if (extra.length || (state !== undefined && state !== 'on' && state !== 'off')) {
|
|
@@ -592,6 +624,7 @@ async function main(argv) {
|
|
|
592
624
|
' --lang es|pt the why report in Latin American Spanish or Brazilian Portuguese; your locale picks it by default\n' +
|
|
593
625
|
' compare [sessionA] [sessionB] two sessions side by side: tokens, cache, sub-agents, rewrites, list USD; json and --lang as for why\n' +
|
|
594
626
|
' calendar [--weeks N] tokens per day, Claude Code and Codex together: sub-agent days, launches AgentGuard held, dollars at API prices; json and --lang as for why\n' +
|
|
627
|
+
' card [--weeks N] [--out file.png] the calendar\'s totals as one 1200x675 image to share (default 4 weeks); written locally, nothing uploaded\n' +
|
|
595
628
|
' rewrites [session|all] every full-prefix cache write with its cause, idle gap and list price\n' +
|
|
596
629
|
' ttl [session|all] one-hour cache lifetime counterfactual from your own idle gaps\n' +
|
|
597
630
|
' pace [session] ten-minute pace and next-hour projection\n' +
|
|
@@ -41,6 +41,10 @@ export interface ReaderCursor {
|
|
|
41
41
|
usage?: Map<string, ChildUsage>;
|
|
42
42
|
/** Sub-agent ids the transcripts record as completed; the parent's set, shared with its children. */
|
|
43
43
|
completed?: Set<string>;
|
|
44
|
+
/** Sub-agent ids the transcripts record as failed, killed or stopped; shared the same way. Optional, so older cursors still load. */
|
|
45
|
+
ended?: Set<string>;
|
|
46
|
+
/** Sub-agent ids the transcripts record as resumed, so possibly running again; shared the same way. */
|
|
47
|
+
resumed?: Set<string>;
|
|
44
48
|
}
|
|
45
49
|
export declare function newCursor(): ReaderCursor;
|
|
46
50
|
interface RawLine {
|
|
@@ -86,9 +90,10 @@ export declare function readSessionIncremental(path: string, cursor: ReaderCurso
|
|
|
86
90
|
/**
|
|
87
91
|
* A reader cursor in a form JSON can hold: the byte offset and size reached in
|
|
88
92
|
* the session transcript and in each sub-agent transcript, the usage already
|
|
89
|
-
* counted per response, each sub-agent's own usage, and the ids of
|
|
90
|
-
*
|
|
91
|
-
*
|
|
93
|
+
* counted per response, each sub-agent's own usage, and the ids of sub-agents
|
|
94
|
+
* recorded as completed, ended without finishing, or resumed. Burn's Claude
|
|
95
|
+
* Code hook stores exactly this between calls; any other hook that stores it the
|
|
96
|
+
* same way reads each byte once, as Burn does.
|
|
92
97
|
*/
|
|
93
98
|
export interface PersistedReaderCursor {
|
|
94
99
|
offset: number;
|
|
@@ -103,15 +108,21 @@ export interface PersistedReaderCursor {
|
|
|
103
108
|
usage?: [string, ChildUsage][];
|
|
104
109
|
/** The session transcript's cursor only: sub-agent ids the transcripts record as completed. */
|
|
105
110
|
completed?: string[];
|
|
111
|
+
/** The same: sub-agent ids recorded as failed, killed or stopped. A cursor saved before Burn kept them loads with none. */
|
|
112
|
+
ended?: string[];
|
|
113
|
+
/** The same: sub-agent ids recorded as resumed. */
|
|
114
|
+
resumed?: string[];
|
|
106
115
|
}
|
|
107
116
|
/** A session cursor, sub-agent cursors included, as JSON can store it. Pair with restoreReaderCursor. */
|
|
108
117
|
export declare function saveReaderCursor(cursor: ReaderCursor): PersistedReaderCursor;
|
|
109
118
|
/**
|
|
110
119
|
* The cursor saveReaderCursor stored. Its sub-agent cursors share the session's
|
|
111
|
-
* de-duplication map and
|
|
112
|
-
* from readIncremental alone (no sub-agent cursors) resumes the session
|
|
120
|
+
* de-duplication map and sub-agent outcomes, as they do while reading. A cursor
|
|
121
|
+
* saved from readIncremental alone (no sub-agent cursors) resumes the session
|
|
113
122
|
* transcript where it stopped and reads each sub-agent transcript from its first
|
|
114
|
-
* byte on the next readSessionIncremental.
|
|
123
|
+
* byte on the next readSessionIncremental. A cursor saved before Burn kept the
|
|
124
|
+
* ended and resumed ids loads with none, so a sub-agent whose end was read
|
|
125
|
+
* before then is never counted as ended: absent evidence stays absent.
|
|
115
126
|
*/
|
|
116
127
|
export declare function restoreReaderCursor(saved: PersistedReaderCursor): ReaderCursor;
|
|
117
128
|
/**
|
|
@@ -24,6 +24,7 @@ exports.readAll = readAll;
|
|
|
24
24
|
const node_fs_1 = require("node:fs");
|
|
25
25
|
const node_path_1 = require("node:path");
|
|
26
26
|
const transcript_1 = require("../insights/transcript");
|
|
27
|
+
const subagent_signals_1 = require("./subagent-signals");
|
|
27
28
|
const CHUNK = 64 * 1024;
|
|
28
29
|
const MAX_LINE = 4 * 1024 * 1024;
|
|
29
30
|
const SPAWN_TOOLS = new Set(['Agent', 'Task']);
|
|
@@ -41,7 +42,6 @@ function nonNegative(value) {
|
|
|
41
42
|
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : 0;
|
|
42
43
|
}
|
|
43
44
|
const MODEL_ID = /^[A-Za-z0-9_.:/-]{1,128}$/;
|
|
44
|
-
const AGENT_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
45
45
|
/**
|
|
46
46
|
* Add one response's newly observed usage to its child's running total. The
|
|
47
47
|
* five-minute and one-hour parts of a cache write follow the record's own split;
|
|
@@ -58,28 +58,16 @@ function addChildUsage(usage, raw, [input, output, write, read]) {
|
|
|
58
58
|
usage.set(model, [row[0] + input, row[1] + output, row[2] + write, row[3] + read, row[4] + five, row[5] + hour]);
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
|
-
* Note
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* operation, a queued-command attachment, or the user message it becomes). Only
|
|
65
|
-
* the id of a completed sub-agent is kept; nothing else of the message is.
|
|
61
|
+
* Note how the host records sub-agents ending: completed, ended without
|
|
62
|
+
* finishing (failed, killed or stopped), or resumed (see subagent-signals.ts).
|
|
63
|
+
* Only the ids are kept; nothing else of the message is.
|
|
66
64
|
*/
|
|
67
|
-
function
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
const { status, agentId } = result;
|
|
71
|
-
if (status === 'completed' && typeof agentId === 'string' && AGENT_ID.test(agentId))
|
|
72
|
-
completed.add(agentId);
|
|
73
|
-
}
|
|
74
|
-
const text = raw.type === 'queue-operation' ? raw.content
|
|
75
|
-
: raw.type === 'user' ? raw.message?.content
|
|
76
|
-
: raw.type === 'attachment' && raw.attachment?.type === 'queued_command' ? raw.attachment.prompt : undefined;
|
|
77
|
-
if (typeof text !== 'string' || !text.startsWith('<task-notification>'))
|
|
78
|
-
return;
|
|
79
|
-
for (const [, body] of text.matchAll(/<task-notification>([\s\S]*?)<\/task-notification>/g)) {
|
|
80
|
-
const id = /<task-id>([A-Za-z0-9_-]{1,64})<\/task-id>/.exec(body)?.[1];
|
|
81
|
-
if (id && /<status>completed<\/status>/.test(body))
|
|
65
|
+
function observeSubagents(raw, cursor, completed) {
|
|
66
|
+
for (const [signal, id] of (0, subagent_signals_1.subagentSignals)(raw)) {
|
|
67
|
+
if (signal === 'completed')
|
|
82
68
|
completed.add(id);
|
|
69
|
+
else
|
|
70
|
+
(signal === 'ended' ? cursor.ended ??= new Set() : cursor.resumed ??= new Set()).add(id);
|
|
83
71
|
}
|
|
84
72
|
}
|
|
85
73
|
/** Turn one parsed line into an event, or null if it carries nothing we track. */
|
|
@@ -197,7 +185,7 @@ function readIncremental(path, cursor, options = {}) {
|
|
|
197
185
|
try {
|
|
198
186
|
const parsed = JSON.parse(lineBytes.toString('utf8'));
|
|
199
187
|
if (cursor.completed)
|
|
200
|
-
|
|
188
|
+
observeSubagents(parsed, cursor, cursor.completed);
|
|
201
189
|
const event = normaliseLine(parsed, cursor);
|
|
202
190
|
if (event)
|
|
203
191
|
events.push(event);
|
|
@@ -240,6 +228,8 @@ function readSessionIncremental(path, cursor, options = {}) {
|
|
|
240
228
|
cursor.usageByMessage ??= new Map();
|
|
241
229
|
cursor.children ??= new Map();
|
|
242
230
|
cursor.completed ??= new Set();
|
|
231
|
+
cursor.ended ??= new Set();
|
|
232
|
+
cursor.resumed ??= new Set();
|
|
243
233
|
const events = readIncremental(path, cursor, options);
|
|
244
234
|
const directory = (0, node_path_1.join)(path.replace(/\.jsonl$/, ''), 'subagents');
|
|
245
235
|
let names;
|
|
@@ -254,8 +244,10 @@ function readSessionIncremental(path, cursor, options = {}) {
|
|
|
254
244
|
for (const name of names) {
|
|
255
245
|
const child = cursor.children.get(name) ?? newCursor();
|
|
256
246
|
child.usageByMessage = cursor.usageByMessage;
|
|
257
|
-
// A sub-agent's own sub-agents report
|
|
247
|
+
// A sub-agent's own sub-agents report how they ended in its transcript.
|
|
258
248
|
child.completed = cursor.completed;
|
|
249
|
+
child.ended = cursor.ended;
|
|
250
|
+
child.resumed = cursor.resumed;
|
|
259
251
|
// Measured from its first byte, as it is read anyway: no second pass over any child.
|
|
260
252
|
if (!child.usage && child.offset === 0)
|
|
261
253
|
child.usage = new Map();
|
|
@@ -275,6 +267,8 @@ function persist(cursor, child) {
|
|
|
275
267
|
usageVersion: cursor.usageVersion,
|
|
276
268
|
...(!child ? { usageByMessage: [...(cursor.usageByMessage ?? [])] } : {}),
|
|
277
269
|
...(!child && cursor.completed?.size ? { completed: [...cursor.completed] } : {}),
|
|
270
|
+
...(!child && cursor.ended?.size ? { ended: [...cursor.ended] } : {}),
|
|
271
|
+
...(!child && cursor.resumed?.size ? { resumed: [...cursor.resumed] } : {}),
|
|
278
272
|
...(child && cursor.usage ? { usage: [...cursor.usage] } : {}),
|
|
279
273
|
...(cursor.children?.size ? { children: [...cursor.children].map(([name, value]) => [name, persist(value, true)]) } : {}) };
|
|
280
274
|
}
|
|
@@ -284,18 +278,20 @@ function saveReaderCursor(cursor) {
|
|
|
284
278
|
}
|
|
285
279
|
/**
|
|
286
280
|
* The cursor saveReaderCursor stored. Its sub-agent cursors share the session's
|
|
287
|
-
* de-duplication map and
|
|
288
|
-
* from readIncremental alone (no sub-agent cursors) resumes the session
|
|
281
|
+
* de-duplication map and sub-agent outcomes, as they do while reading. A cursor
|
|
282
|
+
* saved from readIncremental alone (no sub-agent cursors) resumes the session
|
|
289
283
|
* transcript where it stopped and reads each sub-agent transcript from its first
|
|
290
|
-
* byte on the next readSessionIncremental.
|
|
284
|
+
* byte on the next readSessionIncremental. A cursor saved before Burn kept the
|
|
285
|
+
* ended and resumed ids loads with none, so a sub-agent whose end was read
|
|
286
|
+
* before then is never counted as ended: absent evidence stays absent.
|
|
291
287
|
*/
|
|
292
288
|
function restoreReaderCursor(saved) {
|
|
293
|
-
const restore = (raw, usageByMessage,
|
|
289
|
+
const restore = (raw, usageByMessage, outcomes) => ({
|
|
294
290
|
offset: raw.offset, size: raw.size, malformedLines: raw.malformedLines, depthByUuid: new Map(raw.depthByUuid),
|
|
295
|
-
usageVersion: raw.usageVersion, usageByMessage,
|
|
296
|
-
children: new Map((raw.children ?? []).map(([name, child]) => [name, restore(child, usageByMessage,
|
|
291
|
+
usageVersion: raw.usageVersion, usageByMessage, ...outcomes, ...(raw.usage ? { usage: new Map(raw.usage) } : {}),
|
|
292
|
+
children: new Map((raw.children ?? []).map(([name, child]) => [name, restore(child, usageByMessage, outcomes)]))
|
|
297
293
|
});
|
|
298
|
-
return restore(saved, new Map(saved.usageByMessage ?? []), new Set(saved.completed ?? []));
|
|
294
|
+
return restore(saved, new Map(saved.usageByMessage ?? []), { completed: new Set(saved.completed ?? []), ended: new Set(saved.ended ?? []), resumed: new Set(saved.resumed ?? []) });
|
|
299
295
|
}
|
|
300
296
|
/**
|
|
301
297
|
* An independent copy of a session cursor, sub-agent cursors included, to read
|
|
@@ -303,10 +299,11 @@ function restoreReaderCursor(saved) {
|
|
|
303
299
|
* a step that fails reads the same bytes again instead of losing them.
|
|
304
300
|
*/
|
|
305
301
|
function cloneReaderCursor(cursor) {
|
|
306
|
-
const usageByMessage = new Map(cursor.usageByMessage ?? [])
|
|
302
|
+
const usageByMessage = new Map(cursor.usageByMessage ?? []);
|
|
303
|
+
const outcomes = { completed: new Set(cursor.completed ?? []), ended: new Set(cursor.ended ?? []), resumed: new Set(cursor.resumed ?? []) };
|
|
307
304
|
const copy = (item) => ({
|
|
308
305
|
offset: item.offset, size: item.size, malformedLines: item.malformedLines, depthByUuid: new Map(item.depthByUuid),
|
|
309
|
-
usageVersion: item.usageVersion, usageByMessage,
|
|
306
|
+
usageVersion: item.usageVersion, usageByMessage, ...outcomes, ...(item.usage ? { usage: new Map(item.usage) } : {}),
|
|
310
307
|
...(item.children ? { children: new Map([...item.children].map(([name, child]) => [name, copy(child)])) } : {})
|
|
311
308
|
});
|
|
312
309
|
return copy(cursor);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a Claude Code sub-agent ended, as one transcript record states it. The
|
|
3
|
+
* hook's reader (claude-transcript.ts) and the report parser
|
|
4
|
+
* (insights/transcript.ts) both call this, so the STOP line, `why` and
|
|
5
|
+
* `calendar` read every sub-agent by the same signals. Only sub-agent ids and
|
|
6
|
+
* statuses are read; nothing else of a record is kept.
|
|
7
|
+
*
|
|
8
|
+
* The shapes, as Claude Code writes them (checked against real transcripts on
|
|
9
|
+
* 2026-10-02):
|
|
10
|
+
*
|
|
11
|
+
* - completed: a synchronous sub-agent's tool result, `toolUseResult` with
|
|
12
|
+
* `status: "completed"` and its `agentId`; or, for a background one, a
|
|
13
|
+
* `<task-notification>` with `<status>completed</status>`.
|
|
14
|
+
* - ended without finishing: a `<task-notification>` with
|
|
15
|
+
* `<status>failed</status>`, `killed` or `stopped`.
|
|
16
|
+
* - resumed: a SendMessage result that started a sub-agent again,
|
|
17
|
+
* `toolUseResult.resumedAgentId`. A resumed sub-agent may be running, so an
|
|
18
|
+
* earlier end no longer says how it ended.
|
|
19
|
+
*
|
|
20
|
+
* A notification reaches the transcript in a queued message: a queue operation,
|
|
21
|
+
* a queued-command attachment, or the user message it becomes, always one
|
|
22
|
+
* notification at the start of the text. It names its task with `<task-id>` (a
|
|
23
|
+
* sub-agent's id is its `agent-<id>.jsonl`), and one notification can list
|
|
24
|
+
* several tasks under one status, which applies to each. Ids and status are read
|
|
25
|
+
* only from the header, where Claude Code writes them before the summary and the
|
|
26
|
+
* sub-agent's own result, so words inside a result, even a quoted notification,
|
|
27
|
+
* can never become an id or a status.
|
|
28
|
+
*
|
|
29
|
+
* Not signals: a launch result (`status: "async_launched"`) says the sub-agent
|
|
30
|
+
* is running; a notification with no status reports an event, not an end; and
|
|
31
|
+
* an error result to an Agent call (a hook's or a person's refusal, a rejected
|
|
32
|
+
* permission) answers a launch that never started, so it has no transcript and
|
|
33
|
+
* used no tokens.
|
|
34
|
+
*/
|
|
35
|
+
export type SubagentSignal = 'completed' | 'ended' | 'resumed';
|
|
36
|
+
/** A sub-agent's transcript in its session's `subagents` folder, and the sub-agent id the name carries. */
|
|
37
|
+
export declare const SUBAGENT_FILE: RegExp;
|
|
38
|
+
/** Notification statuses that end a sub-agent without it finishing. */
|
|
39
|
+
export declare const UNFINISHED_STATUSES: ReadonlySet<string>;
|
|
40
|
+
/**
|
|
41
|
+
* The sub-agent signals one parsed transcript record carries, as
|
|
42
|
+
* `[signal, sub-agent id]` pairs; almost every record carries none.
|
|
43
|
+
*/
|
|
44
|
+
export declare function subagentSignals(raw: unknown): ReadonlyArray<readonly [SubagentSignal, string]>;
|
|
45
|
+
/**
|
|
46
|
+
* A sub-agent that ended without finishing: recorded as failed, killed or
|
|
47
|
+
* stopped, and never as completed or resumed. A resumed one may be running
|
|
48
|
+
* again, and Burn does not order records across a session's files, so it is
|
|
49
|
+
* left out unless it completes. Anything else may have finished or may still be
|
|
50
|
+
* running, and is never counted.
|
|
51
|
+
*/
|
|
52
|
+
export declare function endedWithoutFinishing(id: string, signals: {
|
|
53
|
+
completed?: ReadonlySet<string>;
|
|
54
|
+
ended?: ReadonlySet<string>;
|
|
55
|
+
resumed?: ReadonlySet<string>;
|
|
56
|
+
}): boolean;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UNFINISHED_STATUSES = exports.SUBAGENT_FILE = void 0;
|
|
4
|
+
exports.subagentSignals = subagentSignals;
|
|
5
|
+
exports.endedWithoutFinishing = endedWithoutFinishing;
|
|
6
|
+
/** A sub-agent's transcript in its session's `subagents` folder, and the sub-agent id the name carries. */
|
|
7
|
+
exports.SUBAGENT_FILE = /^agent-([A-Za-z0-9][A-Za-z0-9_-]{0,63})\.jsonl$/;
|
|
8
|
+
/** Notification statuses that end a sub-agent without it finishing. */
|
|
9
|
+
exports.UNFINISHED_STATUSES = new Set(['failed', 'killed', 'stopped']);
|
|
10
|
+
/** A sub-agent or task id: Claude Code's are letters and digits. One never starts with `_`, so no id can be `__proto__`. */
|
|
11
|
+
const AGENT_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
12
|
+
const NONE = Object.freeze([]);
|
|
13
|
+
const OPEN = '<task-notification>';
|
|
14
|
+
/** The text of a queued message: a queue operation, a queued-command attachment, or the user message it becomes. */
|
|
15
|
+
function queuedText(record) {
|
|
16
|
+
if (record.type === 'queue-operation')
|
|
17
|
+
return record.content;
|
|
18
|
+
if (record.type === 'user')
|
|
19
|
+
return record.message?.content;
|
|
20
|
+
return record.type === 'attachment' && record.attachment?.type === 'queued_command' ? record.attachment.prompt : undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The sub-agent signals one parsed transcript record carries, as
|
|
24
|
+
* `[signal, sub-agent id]` pairs; almost every record carries none.
|
|
25
|
+
*/
|
|
26
|
+
function subagentSignals(raw) {
|
|
27
|
+
if (!raw || typeof raw !== 'object')
|
|
28
|
+
return NONE;
|
|
29
|
+
const record = raw;
|
|
30
|
+
let found;
|
|
31
|
+
const result = record.toolUseResult;
|
|
32
|
+
if (result && typeof result === 'object' && !Array.isArray(result)) {
|
|
33
|
+
const { status, agentId, resumedAgentId } = result;
|
|
34
|
+
if (status === 'completed' && typeof agentId === 'string' && AGENT_ID.test(agentId))
|
|
35
|
+
(found ??= []).push(['completed', agentId]);
|
|
36
|
+
if (typeof resumedAgentId === 'string' && AGENT_ID.test(resumedAgentId))
|
|
37
|
+
(found ??= []).push(['resumed', resumedAgentId]);
|
|
38
|
+
}
|
|
39
|
+
const text = queuedText(record);
|
|
40
|
+
if (typeof text !== 'string' || !text.startsWith(OPEN))
|
|
41
|
+
return found ?? NONE;
|
|
42
|
+
// One notification leads the queued text. Only its header is read: the task
|
|
43
|
+
// ids, then the status, before any summary, note or result.
|
|
44
|
+
const at = text.indexOf('<status>');
|
|
45
|
+
const head = at < 0 ? '' : text.slice(OPEN.length, at);
|
|
46
|
+
if (at < 0 || /<\/?(?:task-notification|summary|note|result|usage|event)>/.test(head))
|
|
47
|
+
return found ?? NONE;
|
|
48
|
+
const status = /^<status>([a-z_]{1,32})<\/status>/.exec(text.slice(at, at + 64))?.[1];
|
|
49
|
+
const signal = status === 'completed' ? 'completed' : status !== undefined && exports.UNFINISHED_STATUSES.has(status) ? 'ended' : null;
|
|
50
|
+
if (!signal)
|
|
51
|
+
return found ?? NONE;
|
|
52
|
+
for (const [, id] of head.matchAll(/<task-id>([A-Za-z0-9][A-Za-z0-9_-]{0,63})<\/task-id>/g))
|
|
53
|
+
(found ??= []).push([signal, id]);
|
|
54
|
+
return found ?? NONE;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A sub-agent that ended without finishing: recorded as failed, killed or
|
|
58
|
+
* stopped, and never as completed or resumed. A resumed one may be running
|
|
59
|
+
* again, and Burn does not order records across a session's files, so it is
|
|
60
|
+
* left out unless it completes. Anything else may have finished or may still be
|
|
61
|
+
* running, and is never counted.
|
|
62
|
+
*/
|
|
63
|
+
function endedWithoutFinishing(id, signals) {
|
|
64
|
+
return Boolean(signals.ended?.has(id)) && !signals.completed?.has(id) && !signals.resumed?.has(id);
|
|
65
|
+
}
|
|
@@ -150,13 +150,16 @@ function calendarLines(report, lang, columns, skin) {
|
|
|
150
150
|
return lines;
|
|
151
151
|
}
|
|
152
152
|
// The summary in short lines: tokens and dollars; the busiest day and the
|
|
153
|
-
// sub-agent share; the
|
|
154
|
-
//
|
|
153
|
+
// sub-agent share; the sub-agents that ended without finishing and the launches
|
|
154
|
+
// AgentGuard held, each when there were any. Two items share a line only when
|
|
155
|
+
// both fit the width; no line takes an item from another.
|
|
155
156
|
const totals = report.totals, spansYears = report.from.slice(0, 4) !== report.to.slice(0, 4);
|
|
157
|
+
const unfinished = totals.unfinishedSubagents ?? 0, unfinishedTokens = totals.unfinishedTokens ?? 0, unfinishedUsd = totals.unfinishedUsd ?? null;
|
|
156
158
|
const summary = [
|
|
157
159
|
[text.tokens((0, render_1.count)(totals.tokens)), text.dollars(totals.usd === null ? null : calendarDollars(totals.usd, words))],
|
|
158
160
|
[...(report.busiest ? [text.busiest(text.date(dateOf(report.busiest.date, text), spansYears), (0, render_1.count)(report.busiest.tokens))] : []),
|
|
159
161
|
text.subagents((0, render_1.share)(totals.subagentTokens, totals.tokens))],
|
|
162
|
+
unfinished > 0 ? [text.unfinished(unfinished, Math.max(unfinished, totals.subagents ?? 0), (0, render_1.count)(unfinishedTokens), (0, render_1.share)(unfinishedTokens, totals.tokens), unfinishedUsd === null ? null : calendarDollars(unfinishedUsd, words))] : [],
|
|
160
163
|
totals.heldLaunches > 0 ? [text.held(totals.heldLaunches)] : [],
|
|
161
164
|
];
|
|
162
165
|
for (const items of summary)
|
|
@@ -24,6 +24,21 @@ export interface CalendarFigures {
|
|
|
24
24
|
unpricedTokens: number;
|
|
25
25
|
/** Those models; null is a response without a model id. */
|
|
26
26
|
unpricedModels: Array<string | null>;
|
|
27
|
+
/**
|
|
28
|
+
* On a stored sub-agent's first day with tokens, the sub-agent itself: 1.
|
|
29
|
+
* Absent on every other day, and on the days an older Burn counted, whose
|
|
30
|
+
* sub-agents are in neither count.
|
|
31
|
+
*/
|
|
32
|
+
subagents?: number;
|
|
33
|
+
/**
|
|
34
|
+
* On that same day, a sub-agent that ended without finishing (recorded as
|
|
35
|
+
* failed, killed or stopped, never as completed or resumed): 1, with all
|
|
36
|
+
* of its tokens, their priced dollars and its tokens on unpriced models.
|
|
37
|
+
*/
|
|
38
|
+
unfinishedSubagents?: number;
|
|
39
|
+
unfinishedTokens?: number;
|
|
40
|
+
unfinishedUsd?: DollarRange;
|
|
41
|
+
unfinishedUnpricedTokens?: number;
|
|
27
42
|
}
|
|
28
43
|
export interface CalendarDayData extends CalendarFigures {
|
|
29
44
|
claudeTokens: number;
|
|
@@ -102,6 +117,18 @@ export interface CalendarTotals {
|
|
|
102
117
|
heldLaunches: number;
|
|
103
118
|
claudeTokens: number;
|
|
104
119
|
codexTokens: number;
|
|
120
|
+
/**
|
|
121
|
+
* Stored sub-agents whose first day with tokens is shown, as this Burn counts
|
|
122
|
+
* them. calendarReport always sets these four; a report an older Burn recorded
|
|
123
|
+
* has none.
|
|
124
|
+
*/
|
|
125
|
+
subagents?: number;
|
|
126
|
+
/** Those of them that ended without finishing: recorded as failed, killed or stopped, and never as completed or resumed. */
|
|
127
|
+
unfinishedSubagents?: number;
|
|
128
|
+
/** All of their tokens. */
|
|
129
|
+
unfinishedTokens?: number;
|
|
130
|
+
/** Those tokens at API list price; null when any of them is on a model without a known price. */
|
|
131
|
+
unfinishedUsd?: DollarRange | null;
|
|
105
132
|
}
|
|
106
133
|
export interface CalendarReport {
|
|
107
134
|
/** The Sunday that starts the first week shown. */
|