bullswarm 0.33.2 → 0.34.0
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 +10 -0
- package/docs/design/cost-0.34.0/README.md +641 -0
- package/docs/design/cost-0.34.0/meter-precision-study.md +364 -0
- package/docs/guide/cost.md +143 -0
- package/docs/guide/index.md +2 -0
- package/docs/index.md +6 -0
- package/docs/plans/cost-0.34.0.goal.txt +9 -0
- package/docs/plans/cost-0.34.0.program.json +259 -0
- package/docs/plans/cost-audit.goal.txt +9 -0
- package/docs/plans/cost-audit.program.json +135 -0
- package/docs/plans/meter-ledger.program.json +122 -0
- package/docs/reference/cli.md +65 -0
- package/docs/reference/providers.md +57 -0
- package/docs/reference/result.md +36 -2
- package/docs/studies/cost-audit-2026-09-18/budget-page-audit.md +359 -0
- package/docs/studies/cost-audit-2026-09-18/claude-actual-vs-recorded.md +162 -0
- package/docs/studies/cost-audit-2026-09-18/cost-fix-plan.md +231 -0
- package/docs/studies/cost-audit-2026-09-18/estimator-audit.md +188 -0
- package/docs/studies/cost-audit-2026-09-18/grok-codex-actual-vs-recorded.md +274 -0
- package/docs/studies/cost-audit-2026-09-18/owner-budget-page-2026-09-18.png +0 -0
- package/docs/studies/cost-audit-2026-09-18/step-timeline-120.txt +40 -0
- package/docs/studies/cost-audit-2026-09-18/step-timeline-55.txt +26 -0
- package/docs/studies/cost-audit-2026-09-18/step-timeline-design.md +315 -0
- package/package.json +1 -1
- package/src/cli.js +38 -2
- package/src/help.js +26 -0
- package/src/lib/agent-events.js +17 -1
- package/src/lib/cli-flags.js +1 -0
- package/src/lib/providers.js +46 -0
- package/src/lib/quota-snapshot.js +160 -0
- package/src/lib/subscription-cost.js +426 -0
- package/src/lib/transcripts/claude-code.js +429 -0
- package/src/lib/transcripts/codex.js +375 -0
- package/src/lib/transcripts/grok.js +288 -0
- package/src/lib/transcripts/index.js +103 -0
- package/src/lib/transcripts/indexing.js +59 -0
- package/src/lib/usage-basis.js +56 -0
- package/src/lib/usage.js +349 -87
- package/src/lib/watch.js +287 -5
- package/src/provider-cli.js +9 -2
- package/src/providers/_schema.json +8 -0
- package/src/providers/claude-code/connector.json +9 -5
- package/src/providers/claude-code/provider.mjs +5 -0
- package/src/providers/codex/connector.json +36 -8
- package/src/providers/codex/provider.mjs +5 -0
- package/src/providers/grok/connector.json +8 -2
- package/src/providers/grok/provider.mjs +5 -0
- package/src/workflow/cli.js +8 -2
- package/src/workflow/dashboard.js +125 -28
- package/src/workflow/reprice.js +619 -0
- package/src/workflow/rollup.js +234 -35
- package/src/workflow/runs-cli.js +14 -0
- package/src/workflow/stat-kit.js +24 -2
- package/src/workflow/stats-model.js +361 -39
- package/src/workflow/stats-view.js +30 -12
- package/src/workflow/usage-view.js +31 -0
- package/src/workflow/v2-dispatch.js +13 -0
- package/src/workflow/v2-outcome.js +87 -6
- package/src/workflow/v2-runtime.js +73 -3
- package/src/workflow/v2-state.js +109 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# bullswarm changelog
|
|
2
2
|
|
|
3
|
+
## 0.34.0 — every dollar says how it was measured
|
|
4
|
+
|
|
5
|
+
- usage: Claude, Codex, and Grok attempts now retain provider-reported or transcript-summed token classes, with the byte estimate and unknown kept as explicit fallbacks.
|
|
6
|
+
- pricing: API-rate cost now prices standard reads, cache reads, cache writes, output, and reasoning against the dated model card instead of treating cache writes as free.
|
|
7
|
+
- subscription: quota-meter deltas are observed subscription spend, while the calibration ledger supplies a separate calibrated USD-per-percent basis when a meter snapshot is unavailable.
|
|
8
|
+
- rollups: Home, Run, Stats, workflow results, and single-run output now aggregate token, API-cost, subscription-cost, and coverage fields through one money-pair formatter.
|
|
9
|
+
- reprice: `workflow reprice` can dry-run or apply current cards and transcript matches to terminal V2 attempts without appending historical calibration samples; it defaults to 30 days (`--all` overrides), indexes transcript stores once, and streams progress rows.
|
|
10
|
+
- validation: `provider validate` warns when an event stream has no usage rules and when a model profile omits cache-write pricing.
|
|
11
|
+
- providers: a new transcript-usage hook lets each provider resolve durable session records without putting provider quirks in core accounting.
|
|
12
|
+
|
|
3
13
|
## 0.33.2 — one way to draw a chart, and every bar answers
|
|
4
14
|
|
|
5
15
|
- stats: every Stats page now uses one shared set of panels, charts, legends,
|
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
# Bullswarm 0.34.0 cost design record
|
|
2
|
+
|
|
3
|
+
This record copies the implementation contract for 0.34.0 sections A–H. The
|
|
4
|
+
audit that motivated it is preserved under
|
|
5
|
+
[`docs/studies/cost-audit-2026-09-18/`](../../studies/cost-audit-2026-09-18/),
|
|
6
|
+
including the [cost fix plan](../../studies/cost-audit-2026-09-18/cost-fix-plan.md),
|
|
7
|
+
[estimator audit](../../studies/cost-audit-2026-09-18/estimator-audit.md),
|
|
8
|
+
[Budget page audit](../../studies/cost-audit-2026-09-18/budget-page-audit.md),
|
|
9
|
+
[Claude actual-vs-recorded audit](../../studies/cost-audit-2026-09-18/claude-actual-vs-recorded.md),
|
|
10
|
+
and [Grok/Codex actual-vs-recorded audit](../../studies/cost-audit-2026-09-18/grok-codex-actual-vs-recorded.md).
|
|
11
|
+
|
|
12
|
+
## A. `attempt.usage` v2
|
|
13
|
+
|
|
14
|
+
Canonical record:
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
type AttemptUsageV2 = {
|
|
18
|
+
model: string | null;
|
|
19
|
+
sessionId: string | null;
|
|
20
|
+
|
|
21
|
+
tokens: {
|
|
22
|
+
standardRead: number | null;
|
|
23
|
+
cacheRead: number | null;
|
|
24
|
+
cacheWrite5m: number | null;
|
|
25
|
+
cacheWrite1h: number | null;
|
|
26
|
+
cacheWrite: number | null; // compatibility sum/alias
|
|
27
|
+
output: number | null; // excludes separately reported reasoning
|
|
28
|
+
reasoning: number | null;
|
|
29
|
+
totalKnown: number | null;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
tokenSource:
|
|
33
|
+
| "provider-reported"
|
|
34
|
+
| "transcript-summed"
|
|
35
|
+
| "estimated:utf8-bytes/4"
|
|
36
|
+
| "unknown";
|
|
37
|
+
|
|
38
|
+
api: {
|
|
39
|
+
usd: number | null;
|
|
40
|
+
breakdown: {
|
|
41
|
+
standardReadUsd: number | null;
|
|
42
|
+
cacheReadUsd: number | null;
|
|
43
|
+
cacheWrite5mUsd: number | null;
|
|
44
|
+
cacheWrite1hUsd: number | null;
|
|
45
|
+
cacheWriteUsd: number | null; // compatibility sum/alias
|
|
46
|
+
outputUsd: number | null;
|
|
47
|
+
reasoningUsd: number | null;
|
|
48
|
+
};
|
|
49
|
+
pricedFields: string[];
|
|
50
|
+
unpricedFields: string[];
|
|
51
|
+
rateCard: {
|
|
52
|
+
source: string | null;
|
|
53
|
+
updatedAt: string | null;
|
|
54
|
+
};
|
|
55
|
+
basis:
|
|
56
|
+
| "rate-card:complete"
|
|
57
|
+
| "rate-card:partial"
|
|
58
|
+
| "unknown:no-model"
|
|
59
|
+
| "unknown:no-rate-card";
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
subscription: {
|
|
63
|
+
pool: string | null;
|
|
64
|
+
window: "5h" | "weekly" | "monthly" | null;
|
|
65
|
+
deltaPct: number | null;
|
|
66
|
+
usd: number | null;
|
|
67
|
+
monthlyPriceUsd: number | null;
|
|
68
|
+
windowDays: number | null;
|
|
69
|
+
basis:
|
|
70
|
+
| "observed:meter-delta"
|
|
71
|
+
| "calibrated:usd-per-pct"
|
|
72
|
+
| "unknown:no-price"
|
|
73
|
+
| "unknown:no-meter"
|
|
74
|
+
| "unknown:no-cost";
|
|
75
|
+
snapshots: {
|
|
76
|
+
start: {
|
|
77
|
+
at: string;
|
|
78
|
+
usedPct: number;
|
|
79
|
+
resetsAt: string | null;
|
|
80
|
+
} | null;
|
|
81
|
+
end: {
|
|
82
|
+
at: string;
|
|
83
|
+
usedPct: number;
|
|
84
|
+
resetsAt: string | null;
|
|
85
|
+
} | null;
|
|
86
|
+
};
|
|
87
|
+
} | null;
|
|
88
|
+
|
|
89
|
+
// Compatibility aliases:
|
|
90
|
+
pricing: object | null;
|
|
91
|
+
costSource: string | null;
|
|
92
|
+
cost: {
|
|
93
|
+
estimatedUsd: number | null;
|
|
94
|
+
breakdown: object | null;
|
|
95
|
+
basis: string;
|
|
96
|
+
};
|
|
97
|
+
normalizedQuota: {
|
|
98
|
+
estimatedPercent: number | null;
|
|
99
|
+
window: string | null;
|
|
100
|
+
includedValueUsd: number | null;
|
|
101
|
+
basis: string;
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Rules:
|
|
107
|
+
|
|
108
|
+
- Token classes are mutually exclusive. When a provider counter includes another class, subtract before storing.
|
|
109
|
+
- `totalKnown` is the sum of non-null exclusive classes. It is null when no token class is known.
|
|
110
|
+
- Missing or unknowable values are null, never zero.
|
|
111
|
+
- A reported zero remains zero.
|
|
112
|
+
- A missing price produces a null breakdown entry and appears in `unpricedFields`.
|
|
113
|
+
- `api.usd` is null when any positive token class is unpriced. A reported zero with no rate may appear in `unpricedFields` without preventing a complete total.
|
|
114
|
+
- Provider `total_cost_usd` may be retained only as diagnostic evidence; `api.usd` is the dated local rate-card calculation required by requirement 2.
|
|
115
|
+
|
|
116
|
+
Compatibility mapping:
|
|
117
|
+
|
|
118
|
+
```text
|
|
119
|
+
cost.estimatedUsd = api.usd
|
|
120
|
+
cost.breakdown = api.breakdown
|
|
121
|
+
cost.basis = api.basis
|
|
122
|
+
costSource = api.usd != null ? "local-rate-card" : null
|
|
123
|
+
pricing.source/updatedAt = api.rateCard.source/updatedAt
|
|
124
|
+
tokens.cacheWrite = cacheWrite5m + cacheWrite1h, or direct generic cacheWrite
|
|
125
|
+
normalizedQuota.estimatedPercent = subscription.deltaPct
|
|
126
|
+
normalizedQuota.window = subscription.window
|
|
127
|
+
normalizedQuota.basis = subscription.basis
|
|
128
|
+
tokenSource/model/sessionId = canonical fields unchanged
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Keep `normalizedQuota.includedValueUsd` if an older connector declared it; new subscription accounting must not depend on it.
|
|
132
|
+
|
|
133
|
+
Preference ladder:
|
|
134
|
+
|
|
135
|
+
```text
|
|
136
|
+
provider-reported
|
|
137
|
+
> transcript-summed
|
|
138
|
+
> estimated:utf8-bytes/4
|
|
139
|
+
> unknown
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Transcript fallback is attempted only before accepting the last two.
|
|
143
|
+
|
|
144
|
+
## B. `src/lib/transcripts/`
|
|
145
|
+
|
|
146
|
+
`index.js` exports:
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
readTranscriptUsage({
|
|
150
|
+
provider,
|
|
151
|
+
sessionId = null,
|
|
152
|
+
cwd = null,
|
|
153
|
+
startedAt = null,
|
|
154
|
+
endedAt = null,
|
|
155
|
+
home
|
|
156
|
+
})
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Each provider module exports the same signature and returns:
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
{
|
|
163
|
+
tokens: {
|
|
164
|
+
standardRead: number|null,
|
|
165
|
+
cacheRead: number|null,
|
|
166
|
+
cacheWrite5m: number|null,
|
|
167
|
+
cacheWrite1h: number|null,
|
|
168
|
+
cacheWrite: number|null,
|
|
169
|
+
output: number|null,
|
|
170
|
+
reasoning: number|null,
|
|
171
|
+
totalKnown: number|null
|
|
172
|
+
};
|
|
173
|
+
model: string|null;
|
|
174
|
+
sessionId: string|null;
|
|
175
|
+
file: string|null;
|
|
176
|
+
firstAt: string|null;
|
|
177
|
+
lastAt: string|null;
|
|
178
|
+
requests: Array<{
|
|
179
|
+
at: string|null;
|
|
180
|
+
model: string|null;
|
|
181
|
+
tokens: object;
|
|
182
|
+
contextTier?: "short"|"long";
|
|
183
|
+
}>;
|
|
184
|
+
confidence: "exact"|"window"|"ambiguous"|"none";
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Resolution:
|
|
189
|
+
|
|
190
|
+
1. A direct session ID selects a unique provider file/session.
|
|
191
|
+
2. `startedAt`/`endedAt` still slice that session so resumed conversations are not charged repeatedly.
|
|
192
|
+
3. Without an ID, resolve by exact cwd and inclusive attempt time window.
|
|
193
|
+
4. Exactly one candidate gives `confidence:"window"`.
|
|
194
|
+
5. Multiple candidates give `confidence:"ambiguous"` with null tokens/file; no arbitrary candidate is selected.
|
|
195
|
+
6. No candidate gives `confidence:"none"`.
|
|
196
|
+
7. An exact ID gives `confidence:"exact"`.
|
|
197
|
+
|
|
198
|
+
Provider algorithms:
|
|
199
|
+
|
|
200
|
+
- Claude:
|
|
201
|
+
- Search `~/.claude/projects` and every `~/.claude-*/projects`.
|
|
202
|
+
- Derive cwd slug with `cwd.replace(/[^A-Za-z0-9]/g, "-")`.
|
|
203
|
+
- Traverse `<session-id>/subagents/agent-*.jsonl`.
|
|
204
|
+
- Deduplicate across parent and subagents by `message.id:requestId`.
|
|
205
|
+
- Missing message ID uses physical `file:line` identity.
|
|
206
|
+
- Retain the last streaming row for each identity.
|
|
207
|
+
- Sum only assistant rows within the attempt interval.
|
|
208
|
+
- `output_tokens_details.thinking_tokens` becomes reasoning; subtract it from output.
|
|
209
|
+
- Codex:
|
|
210
|
+
- Search `~/.codex/sessions/YYYY/MM/DD/rollout-*-${sessionId}.jsonl`.
|
|
211
|
+
- Otherwise inspect `session_meta.cwd` and timestamps.
|
|
212
|
+
- Never sum cumulative `total_token_usage`.
|
|
213
|
+
- For a bounded attempt, use the last cumulative total at/before `endedAt` minus the last cumulative total strictly before `startedAt`; without a prior baseline, subtract zero.
|
|
214
|
+
- Convert inclusive input/output to exclusive classes.
|
|
215
|
+
- Grok:
|
|
216
|
+
- Filter `~/.grok/logs/unified.jsonl` by `sid`.
|
|
217
|
+
- Sum `shell.turn.inference_done` rows within the interval.
|
|
218
|
+
- `standardRead = prompt_tokens - min(prompt_tokens,cached_prompt_tokens)`.
|
|
219
|
+
- Store each request so pricing can apply the 200,000-token threshold separately.
|
|
220
|
+
- Requests with `prompt_tokens >= 200000` use the long-context tier.
|
|
221
|
+
|
|
222
|
+
## C. Provider hooks and stream rules
|
|
223
|
+
|
|
224
|
+
Each first-class provider exports:
|
|
225
|
+
|
|
226
|
+
```js
|
|
227
|
+
export function readTranscriptUsage(args) {
|
|
228
|
+
return providerTranscriptReader(args);
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
`src/lib/providers.js` must:
|
|
233
|
+
|
|
234
|
+
- expose `entry.hasReadTranscriptUsage`;
|
|
235
|
+
- retain `entry.module.readTranscriptUsage`;
|
|
236
|
+
- provide a helper that resolves a pool through `providerFor()` and returns the hook closure;
|
|
237
|
+
- return null when the provider has no hook.
|
|
238
|
+
|
|
239
|
+
A provider without the hook falls through to byte estimate or unknown. It is not an error.
|
|
240
|
+
|
|
241
|
+
Exact Codex connector rule:
|
|
242
|
+
|
|
243
|
+
```json
|
|
244
|
+
"usage": [
|
|
245
|
+
{
|
|
246
|
+
"match": { "path": "type", "equals": "thread.started" },
|
|
247
|
+
"mode": "last",
|
|
248
|
+
"fields": {
|
|
249
|
+
"sessionId": "thread_id"
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
"match": { "path": "type", "equals": "turn.completed" },
|
|
254
|
+
"mode": "last",
|
|
255
|
+
"fields": {
|
|
256
|
+
"standardRead": "usage.input_tokens",
|
|
257
|
+
"cacheRead": "usage.cached_input_tokens",
|
|
258
|
+
"cacheWrite": "usage.cache_write_input_tokens",
|
|
259
|
+
"output": "usage.output_tokens",
|
|
260
|
+
"reasoning": "usage.reasoning_output_tokens"
|
|
261
|
+
},
|
|
262
|
+
"inclusive": {
|
|
263
|
+
"standardRead": ["cacheRead"],
|
|
264
|
+
"output": ["reasoning"]
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
]
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
`inclusive` is a generic declarative rule: after collection, subtract listed component fields, floor at zero. This keeps the Codex quirk in its connector.
|
|
271
|
+
|
|
272
|
+
Claude’s existing rule must add:
|
|
273
|
+
|
|
274
|
+
```json
|
|
275
|
+
"reasoning": "usage.output_tokens_details.thinking_tokens"
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
and:
|
|
279
|
+
|
|
280
|
+
```json
|
|
281
|
+
"inclusive": {
|
|
282
|
+
"output": ["reasoning"]
|
|
283
|
+
}
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
Grok gets no stdout usage rule until a captured stream proves one. Its transcript hook is authoritative.
|
|
287
|
+
|
|
288
|
+
`provider validate` warnings:
|
|
289
|
+
|
|
290
|
+
```text
|
|
291
|
+
eventStream.usage: eventStream is declared but no usage rules exist; attempts require transcript or byte fallback
|
|
292
|
+
modelProfiles[i].pricing: cache-write rate missing (cacheWrite5mUsdPerMillion/cacheWrite1hUsdPerMillion)
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
The second warning remains valid for Codex/Grok because their published cards do not declare a cache-write rate.
|
|
296
|
+
|
|
297
|
+
## D. `src/lib/subscription-cost.js`
|
|
298
|
+
|
|
299
|
+
Exports:
|
|
300
|
+
|
|
301
|
+
```js
|
|
302
|
+
windowDays(window, resetsAt)
|
|
303
|
+
windowPriceUsd(monthlyPriceUsd, window, resetsAt)
|
|
304
|
+
subscriptionUsdFromPct(pct, windowPriceUsd)
|
|
305
|
+
readCalibration(pool, { home })
|
|
306
|
+
appendCalibration(pool, sample, { home })
|
|
307
|
+
subscriptionCost({ ... })
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Formulas:
|
|
311
|
+
|
|
312
|
+
```text
|
|
313
|
+
windowDays("5h") = 5 / 24
|
|
314
|
+
windowDays("weekly") = 7
|
|
315
|
+
windowDays("monthly") = calendar duration from one UTC month before resetsAt
|
|
316
|
+
through resetsAt, in days
|
|
317
|
+
|
|
318
|
+
windowPriceUsd = monthlyPriceUsd × windowDays / 30.4375
|
|
319
|
+
subscriptionUsdFromPct = windowPriceUsd × pct / 100
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
Ledger path:
|
|
323
|
+
|
|
324
|
+
```text
|
|
325
|
+
$BULLSWARM_HOME/calibration/<pool>.json
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Format:
|
|
329
|
+
|
|
330
|
+
```json
|
|
331
|
+
{
|
|
332
|
+
"schema": "bullswarm.calibration.v1",
|
|
333
|
+
"pool": "codex",
|
|
334
|
+
"window": "weekly",
|
|
335
|
+
"samples": [
|
|
336
|
+
{
|
|
337
|
+
"at": "2026-09-19T15:00:00.000Z",
|
|
338
|
+
"apiUsd": 0.42,
|
|
339
|
+
"deltaPct": 1.2,
|
|
340
|
+
"runId": "wf-...",
|
|
341
|
+
"attemptId": "design-1"
|
|
342
|
+
}
|
|
343
|
+
],
|
|
344
|
+
"usdPerPct": 0.35,
|
|
345
|
+
"sampleCount": 3,
|
|
346
|
+
"updatedAt": "2026-09-19T15:00:00.000Z"
|
|
347
|
+
}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
Derivation:
|
|
351
|
+
|
|
352
|
+
```text
|
|
353
|
+
eligible samples: finite apiUsd >= 0 and finite deltaPct > 0
|
|
354
|
+
usdPerPct = sum(apiUsd) / sum(deltaPct)
|
|
355
|
+
minimum usable sampleCount = 3
|
|
356
|
+
maximum retained samples = newest 500
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
A changed quota-window kind starts a fresh calibration for that pool.
|
|
360
|
+
|
|
361
|
+
Decision tree:
|
|
362
|
+
|
|
363
|
+
1. Resolve plan price. If absent, preserve any observed `deltaPct` but set `usd:null`, basis `unknown:no-price`.
|
|
364
|
+
2. If valid same-window meter snapshots exist, calculate `deltaPct=end-start`, `usd` from window price, basis `observed:meter-delta`.
|
|
365
|
+
3. Otherwise, if `api.usd` is null, basis `unknown:no-cost`.
|
|
366
|
+
4. Otherwise, if a compatible calibration has at least three samples, calculate:
|
|
367
|
+
- `deltaPct = api.usd / usdPerPct`
|
|
368
|
+
- subscription USD from that percentage
|
|
369
|
+
- basis `calibrated:usd-per-pct`.
|
|
370
|
+
5. Otherwise basis `unknown:no-meter`.
|
|
371
|
+
|
|
372
|
+
Append a sample only for `observed:meter-delta`, positive `deltaPct`, and known `api.usd`.
|
|
373
|
+
|
|
374
|
+
## E. `src/lib/quota-snapshot.js`
|
|
375
|
+
|
|
376
|
+
```js
|
|
377
|
+
snapshotPool(poolName, { home, now = Date.now() })
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
Implementation:
|
|
381
|
+
|
|
382
|
+
- `new MeterCache(join(home,"meters")).get(poolName)`;
|
|
383
|
+
- determine the pool’s quota window through the declared subscription first, then connector subscription;
|
|
384
|
+
- select `five_hour`, `seven_day`, or `monthly`;
|
|
385
|
+
- return:
|
|
386
|
+
|
|
387
|
+
```ts
|
|
388
|
+
{
|
|
389
|
+
at: string; // cached captured_at
|
|
390
|
+
window: "5h"|"weekly"|"monthly";
|
|
391
|
+
usedPct: number;
|
|
392
|
+
resetsAt: string|null;
|
|
393
|
+
ageMs: number;
|
|
394
|
+
}
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
Return null for unreadable, missing, undated, or nonnumeric data.
|
|
398
|
+
|
|
399
|
+
```js
|
|
400
|
+
deltaBetween(start, end, { maxStartAgeMs = 60000 })
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
Returns either:
|
|
404
|
+
|
|
405
|
+
```ts
|
|
406
|
+
{ deltaPct: number, reason: null }
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
or:
|
|
410
|
+
|
|
411
|
+
```ts
|
|
412
|
+
{ deltaPct: null, reason:
|
|
413
|
+
"missing-snapshot" |
|
|
414
|
+
"stale-start" |
|
|
415
|
+
"window-changed" |
|
|
416
|
+
"reset-between-snapshots" |
|
|
417
|
+
"counter-decreased"
|
|
418
|
+
}
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
Rules:
|
|
422
|
+
|
|
423
|
+
- start older than 60 seconds is stale;
|
|
424
|
+
- windows must match;
|
|
425
|
+
- reset timestamps must match when both are present;
|
|
426
|
+
- differing reset timestamps, or end utilization below start, invalidates observation;
|
|
427
|
+
- valid delta is `end.usedPct-start.usedPct`, including a measured zero.
|
|
428
|
+
|
|
429
|
+
Wiring placement:
|
|
430
|
+
|
|
431
|
+
- start snapshot: in `watchOnce`, immediately before `runDelegate`;
|
|
432
|
+
- end snapshot: immediately after the child exits;
|
|
433
|
+
- if the cached reading at end is older than 60 seconds, call `getMeterReading(poolName,{bullswarmDir:home,force:true})` once, then reread through `snapshotPool`;
|
|
434
|
+
- a failed forced read retains the stale cache but produces no observed delta.
|
|
435
|
+
|
|
436
|
+
## F. Wiring
|
|
437
|
+
|
|
438
|
+
Exact `watchOnce` sequence:
|
|
439
|
+
|
|
440
|
+
```text
|
|
441
|
+
write task file
|
|
442
|
+
snapshotPool(start)
|
|
443
|
+
runDelegate
|
|
444
|
+
extract output and structured event usage
|
|
445
|
+
estimateInvocationUsage
|
|
446
|
+
if source is estimated/unknown:
|
|
447
|
+
resolve sessionId from structured usage, conversation, or stdout identity
|
|
448
|
+
call provider readTranscriptUsage with session/cwd/time window
|
|
449
|
+
if exact/window match, rebuild usage as transcript-summed
|
|
450
|
+
snapshotPool(end), forcing one meter read if required
|
|
451
|
+
attach subscription result
|
|
452
|
+
append calibration sample only for observed basis + known api.usd
|
|
453
|
+
write output
|
|
454
|
+
judge content
|
|
455
|
+
return verdict.meta.usage
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
`v2-dispatch` must pass:
|
|
459
|
+
|
|
460
|
+
```ts
|
|
461
|
+
{
|
|
462
|
+
bullswarmDir,
|
|
463
|
+
poolName: pool.name,
|
|
464
|
+
runId,
|
|
465
|
+
attemptId,
|
|
466
|
+
conversation,
|
|
467
|
+
startedAt
|
|
468
|
+
}
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
`usage.sessionId` uses the stream-reported ID first, then the Bullswarm conversation ID.
|
|
472
|
+
|
|
473
|
+
`bullswarm run` human output becomes one shared formatted line, for example:
|
|
474
|
+
|
|
475
|
+
```text
|
|
476
|
+
usage: read=14157 cache-read=6912 cache-write=0 output=7 reasoning=13 tokens (provider-reported)
|
|
477
|
+
cost: $0.00 api · · sub unknown (no plan price)
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
JSON output carries the complete v2 usage record without flattening.
|
|
481
|
+
|
|
482
|
+
## G. Aggregation and formatter
|
|
483
|
+
|
|
484
|
+
Per-pool rollup fields:
|
|
485
|
+
|
|
486
|
+
```ts
|
|
487
|
+
{
|
|
488
|
+
attempts: number;
|
|
489
|
+
minutes: number|null;
|
|
490
|
+
tokens: number|null;
|
|
491
|
+
cacheRead: number|null;
|
|
492
|
+
cacheWrite: number|null;
|
|
493
|
+
apiUsd: number|null;
|
|
494
|
+
apiKnownSubtotalUsd: number|null;
|
|
495
|
+
subscriptionUsd: number|null;
|
|
496
|
+
subscriptionKnownSubtotalUsd: number|null;
|
|
497
|
+
measuredAttempts: number;
|
|
498
|
+
pricedAttempts: number;
|
|
499
|
+
subscriptionPricedAttempts: number;
|
|
500
|
+
tokenSource: TokenSource; // worst
|
|
501
|
+
subscriptionBasis: SubscriptionBasis; // worst
|
|
502
|
+
}
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
Top-level run rollup adds the same cost/coverage fields under `usage`. `apiUsd` and `subscriptionUsd` are null unless every relevant attempt has that amount; partial sums live only in the explicitly named subtotal fields.
|
|
506
|
+
|
|
507
|
+
Worst-source order:
|
|
508
|
+
|
|
509
|
+
```text
|
|
510
|
+
unknown
|
|
511
|
+
< estimated:utf8-bytes/4
|
|
512
|
+
< transcript-summed
|
|
513
|
+
< provider-reported
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
Worst subscription order:
|
|
517
|
+
|
|
518
|
+
```text
|
|
519
|
+
unknown:no-price
|
|
520
|
+
< unknown:no-meter
|
|
521
|
+
< unknown:no-cost
|
|
522
|
+
< calibrated:usd-per-pct
|
|
523
|
+
< observed:meter-delta
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
The result envelope retains existing `usage.total`, `usage.byPool`, and `usage.bytes`, and adds:
|
|
527
|
+
|
|
528
|
+
```ts
|
|
529
|
+
usage.steps[actionId] = aggregate
|
|
530
|
+
usage.totals = aggregate
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
Each action entry may also carry its aggregate as `action.usage`; envelope validation must accept it. `workflow runs result --json` then exposes per-step and total cost; `workflow action show` already exposes individual attempts.
|
|
534
|
+
|
|
535
|
+
The one formatter is:
|
|
536
|
+
|
|
537
|
+
```js
|
|
538
|
+
formatMoneyPair({ api, subscription })
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
Exact basis strings:
|
|
542
|
+
|
|
543
|
+
```text
|
|
544
|
+
provider-reported API: "$0.42 api"
|
|
545
|
+
transcript-summed API: "≈ $0.42 api summed"
|
|
546
|
+
estimated API: "~ $0.42 api estimated"
|
|
547
|
+
unknown API: "· api unknown"
|
|
548
|
+
|
|
549
|
+
observed subscription: "1.2% wk $0.84 sub"
|
|
550
|
+
calibrated subscription: "1.2% wk ≈ $0.84 sub"
|
|
551
|
+
unknown:no-price: "· sub unknown (no plan price)"
|
|
552
|
+
unknown:no-meter: "· sub unknown (no meter/calibration)"
|
|
553
|
+
unknown:no-cost: "· sub unknown (no API cost)"
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
Pair example:
|
|
557
|
+
|
|
558
|
+
```text
|
|
559
|
+
$0.42 api · 1.2% wk ≈ $0.84 sub
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
Glyph rule:
|
|
563
|
+
|
|
564
|
+
```text
|
|
565
|
+
$ = measured provider tokens or observed meter delta
|
|
566
|
+
≈ = transcript sum or calibration
|
|
567
|
+
~ = byte estimate
|
|
568
|
+
· = unknown
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
Run, Home, Stats, workflow-result summaries, and single-run CLI must call this formatter; none may construct its own money label.
|
|
572
|
+
|
|
573
|
+
## H. `workflow reprice`
|
|
574
|
+
|
|
575
|
+
Command:
|
|
576
|
+
|
|
577
|
+
```text
|
|
578
|
+
bullswarm workflow reprice [--apply] [--since <date>|--all] [--pool <name>] [--json]
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
Behavior:
|
|
582
|
+
|
|
583
|
+
- dry-run is the default;
|
|
584
|
+
- terminal V2 runs only; ongoing runs are skipped;
|
|
585
|
+
- `--since` filters attempts by `startedAt`, inclusively;
|
|
586
|
+
- `--pool` is an exact pool-name filter;
|
|
587
|
+
- provider-reported attempts retain their measured tokens but are repriced against the current dated card;
|
|
588
|
+
- the default window is 30 days (`--all` overrides it); each transcript store is indexed once from bounded heads/tails, only matches are read in full, and output streams one row per decision;
|
|
589
|
+
- other attempts invoke the provider transcript hook;
|
|
590
|
+
- exact/window match writes `tokenSource:"transcript-summed"`;
|
|
591
|
+
- none/ambiguous writes `tokenSource:"unknown"`, `tokens.totalKnown:null`, `api.usd:null`, `cost.estimatedUsd:null`;
|
|
592
|
+
- historical repricing reads calibration but never appends calibration samples;
|
|
593
|
+
- dry-run writes neither run files nor calibration.
|
|
594
|
+
|
|
595
|
+
Exact dry-run columns:
|
|
596
|
+
|
|
597
|
+
```text
|
|
598
|
+
run action try pool match tokens api subscription
|
|
599
|
+
mu5... integrate-2 1 claude-code:wati exact 34908081 $22.54 unknown:no-meter
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
Use `-` for null values. JSON result:
|
|
603
|
+
|
|
604
|
+
```ts
|
|
605
|
+
{
|
|
606
|
+
action: "reprice";
|
|
607
|
+
apply: boolean;
|
|
608
|
+
filters: { since: string|null, pool: string|null };
|
|
609
|
+
scannedRuns: number;
|
|
610
|
+
scannedAttempts: number;
|
|
611
|
+
matched: number;
|
|
612
|
+
ambiguous: number;
|
|
613
|
+
missing: number;
|
|
614
|
+
changedRuns: number;
|
|
615
|
+
rows: Array<{
|
|
616
|
+
runId, shortId, actionId, attemptId, ordinal, pool,
|
|
617
|
+
confidence, tokenSource, totalKnown, apiUsd,
|
|
618
|
+
subscriptionUsd, subscriptionBasis
|
|
619
|
+
}>;
|
|
620
|
+
failures: Array<{runId:string,error:string}>;
|
|
621
|
+
}
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
On `--apply`, for each changed run:
|
|
625
|
+
|
|
626
|
+
1. atomically rewrite `state.json`;
|
|
627
|
+
2. recompute `state.usage`;
|
|
628
|
+
3. regenerate `result.json` through `createV2ResultEnvelope`, preserving its recorded `finishedAt`;
|
|
629
|
+
4. call `writeRunRollup(runDir,state,result)`;
|
|
630
|
+
5. `writeRunRollup` writes `rollup.json` and calls `appendRollupIndex`, updating `history/runs.jsonl` idempotently.
|
|
631
|
+
|
|
632
|
+
These are the same rollup/index primitives used by `cmdReindex`; do not duplicate index-writing logic.
|
|
633
|
+
|
|
634
|
+
## Open questions
|
|
635
|
+
|
|
636
|
+
- Which provider transcript stores are available in every deployment, and what retention or permission failures should be surfaced separately from an absent match?
|
|
637
|
+
- Should a provider-reported `total_cost_usd` ever be promoted from diagnostic evidence to a dated local card, or remain diagnostic permanently?
|
|
638
|
+
- How should bundled rate cards be refreshed and audited when a provider changes cache-write tiers or adds a long-context tier?
|
|
639
|
+
- Should calibration samples expire by age, in addition to resetting when the quota-window kind changes?
|
|
640
|
+
- What operator-facing policy should govern applying a reprice when a transcript match is ambiguous or a run's old provider session has been deleted?
|
|
641
|
+
- Should `workflow reprice` expose a bounded concurrency control for very large histories, or remain a single deterministic scan?
|