Commit 2d918f7
committed
fix(cost): scope the subagent remainder bucket to new tokens per turn
CalculateUsageWithCost's remainder bucket exists to price the subagent
tokens a ModelUsageCalculator's main-transcript-only buckets never see. It
derived that shortfall by flattening flat.SubagentTokens, which per the
SubagentAwareExtractor contract is a cumulative-since-session-start
snapshot, not a per-window delta like flat's own scalars and the per-model
buckets. Callers sum the returned buckets and cost across turns
(accumulateModelUsage / accumulateTokenUsage), so every turn re-attributed
the whole cumulative subagent total on top of every earlier turn's:
state.TokenUsage.CostUSD, state.CheckpointTokenUsage.CostUSD and
state.ModelUsage all grew without bound with the turn count, even when the
subagent did nothing after turn 1, and the inflated per-model aggregate
persisted into checkpoint metadata.
Thread the previously-accounted snapshot into CalculateUsageWithCost and
subtract it in remainderBucket, so the remainder is a true increment. The
turn-end hook sources it from state.TokenUsage.SubagentTokens, which
accumulateTokenUsage already keeps at the latest snapshot. One
session-wide baseline is correct for both aggregates: the checkpoint window
is a suffix of the same additive stream and gets its scope from
resetCheckpointWindow, so baselining on SubagentTokensBaseline instead
would reproduce the bug one scope smaller. Condensation passes nil because
it supplies no subagents dir and so never sees a subtree.
Also carry CacheCreation1hTokens, the subset of CacheCreationTokens billed
at 2x input instead of 1.25x, through every accounting helper that dropped
it: flattenTokenUsage (remainder shortfall and EstimateCost's flat
fallback), remainderBucket, SubtractTokenUsage, accumulateTokenUsage, the
checkpoint summary and token-command aggregators, and the two subagent
aggregation loops. Anywhere it was dropped, 1h cache writes silently priced
at the 5-minute rate.
Entire-Checkpoint: 01KZPB94Y1C6A60H13ST3HTDCH1 parent 6795377 commit 2d918f7
13 files changed
Lines changed: 820 additions & 41 deletions
File tree
- cmd/entire/cli
- agent
- claudecode
- factoryaidroid
- types
- checkpoint
- strategy
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
535 | 535 | | |
536 | 536 | | |
537 | 537 | | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
538 | 542 | | |
539 | 543 | | |
540 | 544 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
401 | 401 | | |
402 | 402 | | |
403 | 403 | | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
404 | 411 | | |
405 | 412 | | |
406 | 413 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
64 | 80 | | |
65 | 81 | | |
66 | 82 | | |
| |||
86 | 102 | | |
87 | 103 | | |
88 | 104 | | |
89 | | - | |
| 105 | + | |
90 | 106 | | |
91 | 107 | | |
92 | 108 | | |
| |||
309 | 325 | | |
310 | 326 | | |
311 | 327 | | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
312 | 357 | | |
313 | 358 | | |
314 | | - | |
315 | | - | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
316 | 362 | | |
317 | 363 | | |
318 | | - | |
| 364 | + | |
319 | 365 | | |
320 | 366 | | |
321 | 367 | | |
322 | 368 | | |
323 | 369 | | |
| 370 | + | |
324 | 371 | | |
325 | 372 | | |
326 | 373 | | |
327 | 374 | | |
328 | | - | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
329 | 382 | | |
330 | 383 | | |
331 | 384 | | |
332 | | - | |
333 | | - | |
334 | | - | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
335 | 393 | | |
336 | 394 | | |
337 | 395 | | |
338 | 396 | | |
339 | 397 | | |
340 | 398 | | |
341 | 399 | | |
342 | | - | |
343 | | - | |
344 | | - | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
345 | 403 | | |
346 | 404 | | |
347 | 405 | | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
348 | 413 | | |
349 | 414 | | |
350 | 415 | | |
351 | 416 | | |
352 | 417 | | |
353 | 418 | | |
354 | 419 | | |
| 420 | + | |
355 | 421 | | |
356 | 422 | | |
357 | 423 | | |
358 | 424 | | |
359 | 425 | | |
360 | 426 | | |
| 427 | + | |
361 | 428 | | |
362 | 429 | | |
363 | 430 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
| 81 | + | |
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
| 140 | + | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | | - | |
| 174 | + | |
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
| |||
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
220 | | - | |
| 220 | + | |
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
| |||
256 | 256 | | |
257 | 257 | | |
258 | 258 | | |
259 | | - | |
| 259 | + | |
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
293 | | - | |
| 293 | + | |
294 | 294 | | |
295 | 295 | | |
296 | 296 | | |
| |||
343 | 343 | | |
344 | 344 | | |
345 | 345 | | |
346 | | - | |
| 346 | + | |
347 | 347 | | |
348 | 348 | | |
349 | 349 | | |
| |||
377 | 377 | | |
378 | 378 | | |
379 | 379 | | |
380 | | - | |
| 380 | + | |
381 | 381 | | |
382 | 382 | | |
383 | 383 | | |
| |||
396 | 396 | | |
397 | 397 | | |
398 | 398 | | |
399 | | - | |
| 399 | + | |
400 | 400 | | |
401 | 401 | | |
402 | 402 | | |
| |||
423 | 423 | | |
424 | 424 | | |
425 | 425 | | |
426 | | - | |
| 426 | + | |
427 | 427 | | |
428 | 428 | | |
429 | 429 | | |
| |||
441 | 441 | | |
442 | 442 | | |
443 | 443 | | |
444 | | - | |
| 444 | + | |
445 | 445 | | |
446 | 446 | | |
447 | 447 | | |
| |||
453 | 453 | | |
454 | 454 | | |
455 | 455 | | |
456 | | - | |
| 456 | + | |
457 | 457 | | |
458 | 458 | | |
459 | 459 | | |
| |||
467 | 467 | | |
468 | 468 | | |
469 | 469 | | |
470 | | - | |
| 470 | + | |
471 | 471 | | |
472 | 472 | | |
473 | 473 | | |
| |||
571 | 571 | | |
572 | 572 | | |
573 | 573 | | |
574 | | - | |
| 574 | + | |
575 | 575 | | |
576 | 576 | | |
577 | 577 | | |
| |||
602 | 602 | | |
603 | 603 | | |
604 | 604 | | |
605 | | - | |
| 605 | + | |
606 | 606 | | |
607 | 607 | | |
608 | 608 | | |
| |||
618 | 618 | | |
619 | 619 | | |
620 | 620 | | |
621 | | - | |
| 621 | + | |
622 | 622 | | |
623 | 623 | | |
624 | 624 | | |
| |||
0 commit comments