burnoff 1.4.1 → 2.1.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/.claude-plugin/plugin.json +1 -1
- package/.github/workflows/burnoff-pr-gate.yml +5 -3
- package/.github/workflows/burnoff-startup-lint.yml +2 -2
- package/CHANGELOG.md +74 -0
- package/README.md +187 -213
- package/SECURITY.md +38 -0
- package/bin/burnoff.mjs +307 -32
- package/commands/burnoff.md +6 -4
- package/docs/CLAUDE_CODE_PLUGIN.md +29 -28
- package/docs/DESIGN_PARTNER_INTAKE.md +3 -3
- package/hooks/hook-io.mjs +216 -11
- package/hooks/hooks.json +44 -0
- package/hooks/post-tool-use.mjs +3 -1
- package/hooks/pre-tool-use.mjs +47 -8
- package/hooks/session-lifecycle.mjs +24 -3
- package/hooks/user-prompt-submit.mjs +98 -0
- package/package.json +6 -4
- package/scripts/bench-hooks.mjs +34 -3
- package/scripts/bench-savings.mjs +318 -0
- package/scripts/burnoff-statusline.mjs +138 -6
- package/src/ci/ci-gate.mjs +199 -13
- package/src/ci/pr-cost.mjs +417 -45
- package/src/ci/pr-summary.mjs +467 -0
- package/src/cli/audit.mjs +179 -28
- package/src/cli/doctor.mjs +32 -6
- package/src/cli/hook-health.mjs +9 -1
- package/src/cli/init.mjs +226 -39
- package/src/cli/preview.mjs +299 -0
- package/src/cli/startup-audit/formatter.mjs +248 -0
- package/src/cli/startup-audit/jsonc.mjs +601 -0
- package/src/cli/startup-audit/mcp-audit.mjs +735 -0
- package/src/cli/startup-audit/rules-hygiene.mjs +645 -0
- package/src/cli/startup-audit/telemetry-scan.mjs +975 -0
- package/src/cli/startup-audit.mjs +451 -2471
- package/src/clients/claude-code.mjs +41 -4
- package/src/config/catalog.json +295 -0
- package/src/config/effort.mjs +145 -0
- package/src/config/harness-quirks.mjs +340 -0
- package/src/config/loader.mjs +27 -2
- package/src/config/policy.mjs +81 -9
- package/src/config/pricing.mjs +247 -19
- package/src/config/semver.mjs +25 -0
- package/src/config/uncached-attribution.mjs +280 -41
- package/src/guards/compaction-baton.mjs +248 -0
- package/src/guards/loop-breaker.mjs +328 -47
- package/src/guards/model-switch-hook.mjs +126 -0
- package/src/guards/phase5-detectors.mjs +275 -21
- package/src/guards/post-tool-engine.mjs +333 -11
- package/src/guards/pre-tool-engine.mjs +607 -45
- package/src/guards/prefix-guard.mjs +609 -0
- package/src/guards/prompt-guard.mjs +309 -0
- package/src/guards/read-clamp.mjs +192 -8
- package/src/guards/session-hook.mjs +149 -7
- package/src/guards/shell-guard.mjs +56 -5
- package/src/guards/snooze.mjs +169 -5
- package/src/ipc/daemon.mjs +76 -0
- package/src/ipc/fast-client.mjs +165 -4
- package/src/ipc/protocol.mjs +16 -3
- package/src/storage/antigravity-parser.mjs +16 -2
- package/src/storage/file-lock.mjs +16 -3
- package/src/storage/jsonl-parser.mjs +133 -22
- package/src/storage/telemetry-metrics.mjs +254 -4
|
@@ -30,12 +30,15 @@ export const ATTRIBUTION_BUCKETS = Object.freeze([
|
|
|
30
30
|
export const UNEXPLAINED_RESET_LIKELY_CAUSES = Object.freeze([
|
|
31
31
|
'dynamic tool-list mutation or /mcp reload',
|
|
32
32
|
'/reload-plugins --force mid-session',
|
|
33
|
+
'Anthropic 20-block cache lookback exceeded (likely_lookback_miss)',
|
|
33
34
|
'gateway cache_control stripping or volatile system-prompt header',
|
|
34
35
|
'provider implicit-cache eviction under server load'
|
|
35
36
|
]);
|
|
36
37
|
|
|
37
38
|
const UPSTREAM_EVICTION_ADVICE =
|
|
38
|
-
'
|
|
39
|
+
'Uncached tokens attributed to upstream Gemini 4 serving eviction or shard cut; do not run /compact or shorten prompts (compacting forces another uncached rebuild).';
|
|
40
|
+
const LIKELY_LOOKBACK_MISS_ADVICE =
|
|
41
|
+
"Cold rebuild after a turn appended >=20 cache positions with no prefix change -- likely the provider's 20-block lookback, not a mutation. Not fixable from hooks; narrower per-turn tool fan-out reduces it.";
|
|
39
42
|
const CLAUDE_55_COMPACT_GUIDANCE =
|
|
40
43
|
'Claude 5.5 /compact missed the warm prompt cache (#99624), rewriting full history; avoid unnecessary /compact on warm <1h sessions.';
|
|
41
44
|
const ORCHESTRATOR_THRASH_GUIDANCE =
|
|
@@ -101,28 +104,40 @@ function isVersionAtLeast(ver, minVer) {
|
|
|
101
104
|
*/
|
|
102
105
|
export function normalizeCacheTokenAccounting(raw = {}, canonicalModelId = '') {
|
|
103
106
|
const modelText = String(canonicalModelId || raw.model || raw.modelId || raw.rawModel || '').toLowerCase();
|
|
107
|
+
// OpenAI usage payloads are inclusive: prompt_tokens (chat) / input_tokens (responses) already contain
|
|
108
|
+
// the cached portion reported under *_tokens_details.cached_tokens.
|
|
109
|
+
const openAiCached = Number(
|
|
110
|
+
raw.prompt_tokens_details?.cached_tokens ?? raw.input_tokens_details?.cached_tokens
|
|
111
|
+
);
|
|
112
|
+
const isOpenAiInclusive =
|
|
113
|
+
raw.prompt_tokens !== undefined ||
|
|
114
|
+
raw.prompt_tokens_details?.cached_tokens !== undefined ||
|
|
115
|
+
raw.input_tokens_details?.cached_tokens !== undefined;
|
|
116
|
+
const isGeminiInclusive =
|
|
117
|
+
raw.promptTokenCount !== undefined || raw.prompt_token_count !== undefined;
|
|
104
118
|
const isInclusive =
|
|
105
|
-
raw.promptTokenAccounting === 'inclusive' ||
|
|
106
|
-
raw.promptTokenCount !== undefined ||
|
|
107
|
-
raw.prompt_token_count !== undefined;
|
|
119
|
+
raw.promptTokenAccounting === 'inclusive' || isGeminiInclusive || isOpenAiInclusive;
|
|
108
120
|
|
|
109
121
|
if (isInclusive) {
|
|
110
|
-
const cacheRead =
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
+
const cacheRead = Math.max(
|
|
123
|
+
readNum(raw, [
|
|
124
|
+
'cachedContentTokenCount',
|
|
125
|
+
'cached_content_token_count',
|
|
126
|
+
'cachedTokens',
|
|
127
|
+
'cached_tokens',
|
|
128
|
+
'cacheRead',
|
|
129
|
+
'cacheReadTokens',
|
|
130
|
+
'cacheReadInputTokens',
|
|
131
|
+
'cache_read_input_tokens'
|
|
132
|
+
]),
|
|
133
|
+
Number.isFinite(openAiCached) ? Math.max(0, openAiCached) : 0
|
|
122
134
|
);
|
|
135
|
+
const promptKeys = ['promptTokenCount', 'prompt_token_count', 'prompt_tokens', 'promptTokens', 'totalPromptTokens'];
|
|
136
|
+
if (raw.input_tokens_details?.cached_tokens !== undefined) promptKeys.push('input_tokens');
|
|
137
|
+
const totalPrompt = Math.max(cacheRead, readNum(raw, promptKeys));
|
|
123
138
|
const uncached = Math.max(0, totalPrompt - cacheRead);
|
|
124
139
|
return {
|
|
125
|
-
accounting: 'gemini_inclusive',
|
|
140
|
+
accounting: isOpenAiInclusive && !isGeminiInclusive ? 'openai_inclusive' : 'gemini_inclusive',
|
|
126
141
|
freshInputTokens: uncached,
|
|
127
142
|
cacheCreationTokens: 0,
|
|
128
143
|
uncached,
|
|
@@ -160,6 +175,7 @@ export function normalizeCacheTokenAccounting(raw = {}, canonicalModelId = '') {
|
|
|
160
175
|
]);
|
|
161
176
|
const cacheRead = readNum(raw, [
|
|
162
177
|
'cachedTokens',
|
|
178
|
+
'cached_tokens',
|
|
163
179
|
'cacheRead',
|
|
164
180
|
'cacheReadTokens',
|
|
165
181
|
'cacheReadInputTokens',
|
|
@@ -225,17 +241,20 @@ function toSec(raw) {
|
|
|
225
241
|
return null;
|
|
226
242
|
}
|
|
227
243
|
|
|
244
|
+
const isInclusiveAccounting = (accounting) =>
|
|
245
|
+
accounting === 'gemini_inclusive' || accounting === 'openai_inclusive';
|
|
246
|
+
|
|
228
247
|
function normalizeCallRecord(raw = {}, orderIdx = 0) {
|
|
229
248
|
const preAcct = normalizeCacheTokenAccounting(raw);
|
|
230
|
-
const valInput = preAcct.accounting
|
|
249
|
+
const valInput = isInclusiveAccounting(preAcct.accounting)
|
|
231
250
|
? { ...raw, uncachedTokens: preAcct.uncached, cachedTokens: preAcct.cacheRead, promptTokens: preAcct.totalPrompt }
|
|
232
251
|
: raw;
|
|
233
252
|
const val = valuateCall(valInput);
|
|
234
253
|
const acct = normalizeCacheTokenAccounting(raw, val.card.modelId);
|
|
235
|
-
const uncached = preAcct.accounting
|
|
254
|
+
const uncached = isInclusiveAccounting(preAcct.accounting)
|
|
236
255
|
? preAcct.uncached
|
|
237
256
|
: (val.uncachedTokens + val.write5mTokens + val.write1hTokens);
|
|
238
|
-
const cacheRead = preAcct.accounting
|
|
257
|
+
const cacheRead = isInclusiveAccounting(preAcct.accounting)
|
|
239
258
|
? preAcct.cacheRead
|
|
240
259
|
: val.cachedTokens;
|
|
241
260
|
const prompt = uncached + cacheRead;
|
|
@@ -243,9 +262,14 @@ function normalizeCallRecord(raw = {}, orderIdx = 0) {
|
|
|
243
262
|
const endSec = (typeof raw.end === 'number' && Number.isFinite(raw.end)) ? raw.end : startSec;
|
|
244
263
|
const uncachedUsd = (val.card.unmappedModel || val.inputUncachedUsd === null)
|
|
245
264
|
? null
|
|
246
|
-
: (val.inputUncachedUsd + (val.
|
|
265
|
+
: (val.inputUncachedUsd + (val.cacheWriteTotalUsd || 0));
|
|
247
266
|
const model = raw.model || raw.modelId || val.card.rawModel || 'unknown';
|
|
248
267
|
const label = raw.label || null;
|
|
268
|
+
const effort = String(
|
|
269
|
+
raw.effort ?? raw.reasoning_effort ?? raw.reasoningEffort ?? raw.thinking_level ?? ''
|
|
270
|
+
)
|
|
271
|
+
.trim()
|
|
272
|
+
.toLowerCase();
|
|
249
273
|
const gemini4Family = detectGemini4Family(val.card.modelId) || detectGemini4Family(label) || detectGemini4Family(model);
|
|
250
274
|
const cacheCreationTokens = val.write5mTokens + val.write1hTokens;
|
|
251
275
|
|
|
@@ -255,11 +279,12 @@ function normalizeCallRecord(raw = {}, orderIdx = 0) {
|
|
|
255
279
|
orderIdx,
|
|
256
280
|
model,
|
|
257
281
|
label,
|
|
282
|
+
effort,
|
|
258
283
|
canonicalModelId: val.card.modelId,
|
|
259
284
|
gemini4Family,
|
|
260
285
|
isClaude55: isClaude55Model(val.card.modelId, model),
|
|
261
286
|
accounting: acct.accounting,
|
|
262
|
-
streamKey: `${raw.model || raw.modelId || ''}|${raw.label || ''}`,
|
|
287
|
+
streamKey: `${raw.model || raw.modelId || ''}|${raw.label || ''}|${effort}`,
|
|
263
288
|
uncached,
|
|
264
289
|
cacheRead,
|
|
265
290
|
cacheCreationTokens,
|
|
@@ -277,6 +302,21 @@ function normalizeCallRecord(raw = {}, orderIdx = 0) {
|
|
|
277
302
|
hadToolsetChange: Boolean(raw.hadToolsetChange),
|
|
278
303
|
hadMcpReload: Boolean(raw.hadMcpReload),
|
|
279
304
|
hadHookEdit: Boolean(raw.hadHookEdit),
|
|
305
|
+
toolCallsInTurn: Math.max(0, Number(raw.toolCallsInTurn ?? raw.tool_calls_in_turn ?? raw.toolUseCount ?? 0) || 0),
|
|
306
|
+
contentBlocksSinceBreakpoint: Math.max(
|
|
307
|
+
0,
|
|
308
|
+
Number(raw.contentBlocksSinceBreakpoint ?? raw.content_blocks_since_breakpoint ?? raw.contentBlockCount ?? 0) || 0
|
|
309
|
+
),
|
|
310
|
+
appendedPositionsSincePrevCall: Math.max(
|
|
311
|
+
0,
|
|
312
|
+
Number(raw.appendedPositionsSincePrevCall ?? raw.appended_positions_since_prev_call ?? 0) || 0
|
|
313
|
+
),
|
|
314
|
+
disableExperimentalBetas: Boolean(
|
|
315
|
+
raw.disableExperimentalBetas ||
|
|
316
|
+
raw.claudeCodeDisableExperimentalBetas ||
|
|
317
|
+
raw.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS === '1' ||
|
|
318
|
+
raw.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS === 'true'
|
|
319
|
+
),
|
|
280
320
|
claudeCodeVersion: raw.claudeCodeVersion || null,
|
|
281
321
|
had1hTtl: Boolean(raw.cacheTtlTier === '1h' || Number(raw.cacheCreationEphemeral1hInputTokens || 0) > 0)
|
|
282
322
|
};
|
|
@@ -358,10 +398,25 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
358
398
|
const newTurnDeltaState = { calls: 0, tokens: 0, rawUsd: 0 };
|
|
359
399
|
const upstreamEvictionState = { calls: 0, tokens: 0, rawUsd: 0, byPattern: {} };
|
|
360
400
|
const actionableResetState = { calls: 0, tokens: 0, rawUsd: 0 };
|
|
401
|
+
const likelyLookbackMissState = { calls: 0, tokens: 0, rawUsd: 0 };
|
|
361
402
|
const claude55MissState = { calls: 0, tokens: 0, cacheCreationTokens: 0, rawUsd: 0 };
|
|
362
403
|
const orchestratorThrashState = { calls: 0, tokens: 0, rawUsd: 0 };
|
|
404
|
+
const routerPingPongState = { events: 0, calls: 0, tokens: 0, rawUsd: 0 };
|
|
405
|
+
const truncationSummary = {
|
|
406
|
+
front_slice: { calls: 0, tokens: 0 },
|
|
407
|
+
mid_history_mutation: { calls: 0, tokens: 0 },
|
|
408
|
+
toolset_or_mcp_mutation: { calls: 0, tokens: 0 },
|
|
409
|
+
anthropic_20_block_lookback: { calls: 0, tokens: 0 },
|
|
410
|
+
compaction_reset: { calls: 0, tokens: 0 }
|
|
411
|
+
};
|
|
412
|
+
const subThresholdSummary = {
|
|
413
|
+
sessions: 0,
|
|
414
|
+
calls: 0,
|
|
415
|
+
guidance: 'Consolidate useful static instructions across turns instead of padding prompts with filler.'
|
|
416
|
+
};
|
|
363
417
|
const accountingTotals = {
|
|
364
418
|
gemini_inclusive: { calls: 0, promptTokens: 0, cacheReadTokens: 0 },
|
|
419
|
+
openai_inclusive: { calls: 0, promptTokens: 0, cacheReadTokens: 0 },
|
|
365
420
|
anthropic_exclusive: { calls: 0, promptTokens: 0, cacheReadTokens: 0 },
|
|
366
421
|
presplit_exclusive: { calls: 0, promptTokens: 0, cacheReadTokens: 0 }
|
|
367
422
|
};
|
|
@@ -395,9 +450,14 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
395
450
|
|
|
396
451
|
let prevAny = null;
|
|
397
452
|
let prevPrevAny = null;
|
|
453
|
+
let subThresholdCount = 0;
|
|
454
|
+
const recentTurnStreams = [];
|
|
398
455
|
const lastByKey = new Map();
|
|
399
456
|
|
|
400
457
|
for (const c of list) {
|
|
458
|
+
if (c.prompt > 0 && c.prompt < c.minCacheableTokens) {
|
|
459
|
+
subThresholdCount += 1;
|
|
460
|
+
}
|
|
401
461
|
let bucket = 'incremental_tail';
|
|
402
462
|
let resetContext = null;
|
|
403
463
|
let prevRefForFloor = prevAny;
|
|
@@ -472,18 +532,84 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
472
532
|
) {
|
|
473
533
|
bucket = 'unexplained_prefix_reset';
|
|
474
534
|
const eviction = classifyUpstreamProviderEviction(c, prevAny, idleSec, idleThreshold);
|
|
535
|
+
const matchedPrefixTokens = c.cacheRead;
|
|
536
|
+
const matchedPrefixRatio = prevAnyPrompt > 0
|
|
537
|
+
? Number((c.cacheRead / prevAnyPrompt).toFixed(4))
|
|
538
|
+
: 0;
|
|
539
|
+
const hasToolsetMutation = Boolean(
|
|
540
|
+
c.hadToolsetChange ||
|
|
541
|
+
c.hadMcpReload ||
|
|
542
|
+
c.hadHookEdit ||
|
|
543
|
+
(c.toolsetHash && prevAny.toolsetHash && c.toolsetHash !== prevAny.toolsetHash)
|
|
544
|
+
);
|
|
545
|
+
const isAnthropicModel = /claude|anthropic/i.test(String(c.canonicalModelId || c.model || ''));
|
|
546
|
+
const is20BlockLookback = Boolean(
|
|
547
|
+
isAnthropicModel &&
|
|
548
|
+
!hasToolsetMutation &&
|
|
549
|
+
!(c.hadCompactionBefore || c.isCompactionBoundary || c.isCompactSummary) &&
|
|
550
|
+
(c.contentBlocksSinceBreakpoint >= 20 ||
|
|
551
|
+
c.toolCallsInTurn >= 20 ||
|
|
552
|
+
(prevAny && (prevAny.contentBlocksSinceBreakpoint >= 20 || prevAny.toolCallsInTurn >= 20)))
|
|
553
|
+
);
|
|
554
|
+
const effectiveEviction =
|
|
555
|
+
eviction ||
|
|
556
|
+
(is20BlockLookback
|
|
557
|
+
? { pattern: 'anthropic_20_block_lookback', issueRef: 'anthropic-20-block-lookback' }
|
|
558
|
+
: null);
|
|
559
|
+
const isLikelyLookbackMiss = Boolean(
|
|
560
|
+
!effectiveEviction &&
|
|
561
|
+
isAnthropicModel &&
|
|
562
|
+
!hasToolsetMutation &&
|
|
563
|
+
!(c.hadCompactionBefore || c.isCompactionBoundary || c.isCompactSummary) &&
|
|
564
|
+
c.appendedPositionsSincePrevCall >= 20
|
|
565
|
+
);
|
|
566
|
+
const truncationKind = (c.hadCompactionBefore || c.isCompactionBoundary || c.isCompactSummary)
|
|
567
|
+
? 'compaction_reset'
|
|
568
|
+
: hasToolsetMutation
|
|
569
|
+
? 'toolset_or_mcp_mutation'
|
|
570
|
+
: (is20BlockLookback || isLikelyLookbackMiss)
|
|
571
|
+
? 'anthropic_20_block_lookback'
|
|
572
|
+
: c.cacheRead === 0
|
|
573
|
+
? 'front_slice'
|
|
574
|
+
: 'mid_history_mutation';
|
|
575
|
+
const diagnosis = truncationKind === 'compaction_reset'
|
|
576
|
+
? `compaction rebuild (${matchedPrefixTokens.toLocaleString('en-US')} matched prefix tok of prior ${prevAnyPrompt.toLocaleString('en-US')})`
|
|
577
|
+
: truncationKind === 'toolset_or_mcp_mutation'
|
|
578
|
+
? `MCP/toolset or hook mutation invalidated prefix (${matchedPrefixTokens.toLocaleString('en-US')} matched of prior ${prevAnyPrompt.toLocaleString('en-US')})`
|
|
579
|
+
: truncationKind === 'anthropic_20_block_lookback'
|
|
580
|
+
? (isLikelyLookbackMiss
|
|
581
|
+
? `likely 20-block lookback miss (${c.appendedPositionsSincePrevCall} appended positions since prev call; ${matchedPrefixTokens.toLocaleString('en-US')} matched of prior ${prevAnyPrompt.toLocaleString('en-US')})`
|
|
582
|
+
: `Anthropic 20-block lookback window exceeded (>=20 blocks/tool uses between cache breakpoints; ${matchedPrefixTokens.toLocaleString('en-US')} matched of prior ${prevAnyPrompt.toLocaleString('en-US')})`)
|
|
583
|
+
: truncationKind === 'front_slice'
|
|
584
|
+
? `history sliced at the front (0 matched prefix tokens of prior ${prevAnyPrompt.toLocaleString('en-US')})`
|
|
585
|
+
: `history truncated/mutated after ~${matchedPrefixTokens.toLocaleString('en-US')} matched prefix tokens (${(matchedPrefixRatio * 100).toFixed(1)}% of prior ${prevAnyPrompt.toLocaleString('en-US')})`;
|
|
586
|
+
truncationSummary[truncationKind].calls += 1;
|
|
587
|
+
truncationSummary[truncationKind].tokens += c.uncached;
|
|
475
588
|
resetContext = {
|
|
476
589
|
conv: c.conv,
|
|
477
590
|
idx: c.idx,
|
|
478
591
|
idleSec: idleSec === null ? null : Math.round(idleSec),
|
|
479
592
|
prevPrompt: prevAnyPrompt,
|
|
480
593
|
cacheRead: c.cacheRead,
|
|
594
|
+
matchedPrefixTokens,
|
|
595
|
+
matchedPrefixRatio,
|
|
596
|
+
truncationKind,
|
|
597
|
+
diagnosis,
|
|
481
598
|
uncached: c.uncached,
|
|
482
599
|
model: c.model,
|
|
483
600
|
label: c.label,
|
|
484
|
-
classification:
|
|
485
|
-
|
|
486
|
-
|
|
601
|
+
classification: effectiveEviction
|
|
602
|
+
? 'upstream_provider_eviction'
|
|
603
|
+
: (isLikelyLookbackMiss ? 'likely_lookback_miss' : 'actionable_prefix_reset'),
|
|
604
|
+
...(isLikelyLookbackMiss
|
|
605
|
+
? {
|
|
606
|
+
attributionStatus: 'heuristic',
|
|
607
|
+
advice: LIKELY_LOOKBACK_MISS_ADVICE,
|
|
608
|
+
appendedPositionsSincePrevCall: c.appendedPositionsSincePrevCall
|
|
609
|
+
}
|
|
610
|
+
: {}),
|
|
611
|
+
evictionPattern: effectiveEviction ? effectiveEviction.pattern : null,
|
|
612
|
+
issueRef: effectiveEviction ? effectiveEviction.issueRef : null
|
|
487
613
|
};
|
|
488
614
|
} else {
|
|
489
615
|
bucket = 'incremental_tail';
|
|
@@ -529,6 +655,10 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
529
655
|
if (c.uncachedUsd !== null) upstreamEvictionState.rawUsd += c.uncachedUsd;
|
|
530
656
|
upstreamEvictionState.byPattern[resetContext.evictionPattern] =
|
|
531
657
|
(upstreamEvictionState.byPattern[resetContext.evictionPattern] || 0) + 1;
|
|
658
|
+
} else if (resetContext.classification === 'likely_lookback_miss') {
|
|
659
|
+
likelyLookbackMissState.calls += 1;
|
|
660
|
+
likelyLookbackMissState.tokens += c.uncached;
|
|
661
|
+
if (c.uncachedUsd !== null) likelyLookbackMissState.rawUsd += c.uncachedUsd;
|
|
532
662
|
} else {
|
|
533
663
|
actionableResetState.calls += 1;
|
|
534
664
|
actionableResetState.tokens += c.uncached;
|
|
@@ -538,20 +668,27 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
538
668
|
}
|
|
539
669
|
|
|
540
670
|
if (bucket === 'compaction_rebuild' && c.isClaude55) {
|
|
541
|
-
const
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
671
|
+
const optionBetasDisabled = Boolean(
|
|
672
|
+
options.disableExperimentalBetas ||
|
|
673
|
+
options.env?.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS === '1' ||
|
|
674
|
+
options.env?.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS === 'true'
|
|
675
|
+
);
|
|
676
|
+
if (!c.disableExperimentalBetas && !optionBetasDisabled) {
|
|
677
|
+
const effVersion = c.claudeCodeVersion || options.claudeCodeVersion || null;
|
|
678
|
+
const versionAffected = isVersionAtLeast(effVersion, PROVIDER_QUIRK_CONSTANTS.CLAUDE_55_COMPACT_MISS_MIN_VERSION);
|
|
679
|
+
const warmCache = !prevAny ||
|
|
680
|
+
c.startSec === null ||
|
|
681
|
+
prevAny.endSec === null ||
|
|
682
|
+
Math.max(0, c.startSec - prevAny.endSec) < (prevAny.had1hTtl ? idle1hTtlSeconds : idleMissSeconds);
|
|
683
|
+
const cacheMiss = prevAny
|
|
684
|
+
? c.cacheRead < hitMinCachedShare * prevAny.prompt
|
|
685
|
+
: c.cacheRead === 0;
|
|
686
|
+
if (versionAffected && warmCache && cacheMiss && c.cacheCreationTokens >= c.minCacheableTokens) {
|
|
687
|
+
claude55MissState.calls += 1;
|
|
688
|
+
claude55MissState.tokens += c.uncached;
|
|
689
|
+
claude55MissState.cacheCreationTokens += c.cacheCreationTokens;
|
|
690
|
+
if (c.uncachedUsd !== null) claude55MissState.rawUsd += c.uncachedUsd;
|
|
691
|
+
}
|
|
555
692
|
}
|
|
556
693
|
}
|
|
557
694
|
|
|
@@ -574,10 +711,45 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
574
711
|
}
|
|
575
712
|
}
|
|
576
713
|
|
|
714
|
+
// Detect A -> B -> A model/effort ping-pong within <= 5 turns at prevPrompt >= 50,000
|
|
715
|
+
if (recentTurnStreams.length >= 2) {
|
|
716
|
+
let sawDifferent = false;
|
|
717
|
+
let detourEntry = null;
|
|
718
|
+
for (let k = recentTurnStreams.length - 1; k >= Math.max(0, recentTurnStreams.length - 5); k -= 1) {
|
|
719
|
+
const item = recentTurnStreams[k];
|
|
720
|
+
if (item.streamKey !== c.streamKey) {
|
|
721
|
+
sawDifferent = true;
|
|
722
|
+
if (!detourEntry) detourEntry = item;
|
|
723
|
+
} else if (sawDifferent) {
|
|
724
|
+
const refPrompt = Math.max(Number(detourEntry?.prompt || 0), Number(item.prompt || 0));
|
|
725
|
+
if (refPrompt >= 50_000) {
|
|
726
|
+
routerPingPongState.events += 1;
|
|
727
|
+
routerPingPongState.calls += 1;
|
|
728
|
+
routerPingPongState.tokens += Number(detourEntry?.uncached || c.uncached || 0);
|
|
729
|
+
if (detourEntry?.uncachedUsd !== null && detourEntry?.uncachedUsd !== undefined) {
|
|
730
|
+
routerPingPongState.rawUsd += Number(detourEntry.uncachedUsd || 0);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
break;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
recentTurnStreams.push({
|
|
738
|
+
streamKey: c.streamKey,
|
|
739
|
+
prompt: c.prompt,
|
|
740
|
+
uncached: c.uncached,
|
|
741
|
+
uncachedUsd: c.uncachedUsd
|
|
742
|
+
});
|
|
743
|
+
if (recentTurnStreams.length > 6) recentTurnStreams.shift();
|
|
744
|
+
|
|
577
745
|
prevPrevAny = prevAny;
|
|
578
746
|
prevAny = c;
|
|
579
747
|
lastByKey.set(c.streamKey, c);
|
|
580
748
|
}
|
|
749
|
+
if (subThresholdCount >= 3) {
|
|
750
|
+
subThresholdSummary.sessions += 1;
|
|
751
|
+
subThresholdSummary.calls += subThresholdCount;
|
|
752
|
+
}
|
|
581
753
|
}
|
|
582
754
|
|
|
583
755
|
let reconciledSumTokens = 0;
|
|
@@ -600,7 +772,8 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
600
772
|
calls: appendFloorState.calls,
|
|
601
773
|
tokens: appendFloorState.tokens,
|
|
602
774
|
shareOfBucketPct: b.tokens > 0 ? Number(((appendFloorState.tokens / b.tokens) * 100).toFixed(1)) : 0,
|
|
603
|
-
estimatedUsd: Number(appendFloorState.rawUsd.toFixed(2))
|
|
775
|
+
estimatedUsd: Number(appendFloorState.rawUsd.toFixed(2)),
|
|
776
|
+
attributionStatus: 'attributed_to_upstream'
|
|
604
777
|
},
|
|
605
778
|
new_turn_delta: {
|
|
606
779
|
calls: newTurnDeltaState.calls,
|
|
@@ -629,6 +802,15 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
629
802
|
guidance: ORCHESTRATOR_THRASH_GUIDANCE
|
|
630
803
|
};
|
|
631
804
|
}
|
|
805
|
+
if (name === 'model_or_effort_switch' && routerPingPongState.events > 0) {
|
|
806
|
+
outBucket.routerPingPong = {
|
|
807
|
+
events: routerPingPongState.events,
|
|
808
|
+
calls: routerPingPongState.calls,
|
|
809
|
+
tokens: routerPingPongState.tokens,
|
|
810
|
+
estimatedUsd: Number(routerPingPongState.rawUsd.toFixed(2)),
|
|
811
|
+
guidance: 'Use a subagent for side questions instead of switching models/effort mid-thread (avoids prefix invalidation).'
|
|
812
|
+
};
|
|
813
|
+
}
|
|
632
814
|
if (name === 'unexplained_prefix_reset') {
|
|
633
815
|
outBucket.likelyCauses = [...UNEXPLAINED_RESET_LIKELY_CAUSES];
|
|
634
816
|
outBucket.subBuckets = {
|
|
@@ -638,6 +820,7 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
638
820
|
shareOfBucketPct: b.tokens > 0 ? Number(((upstreamEvictionState.tokens / b.tokens) * 100).toFixed(1)) : 0,
|
|
639
821
|
estimatedUsd: Number(upstreamEvictionState.rawUsd.toFixed(2)),
|
|
640
822
|
byPattern: { ...upstreamEvictionState.byPattern },
|
|
823
|
+
attributionStatus: 'attributed_to_upstream',
|
|
641
824
|
advice: UPSTREAM_EVICTION_ADVICE
|
|
642
825
|
},
|
|
643
826
|
actionable_prefix_reset: {
|
|
@@ -645,8 +828,18 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
645
828
|
tokens: actionableResetState.tokens,
|
|
646
829
|
shareOfBucketPct: b.tokens > 0 ? Number(((actionableResetState.tokens / b.tokens) * 100).toFixed(1)) : 0,
|
|
647
830
|
estimatedUsd: Number(actionableResetState.rawUsd.toFixed(2))
|
|
831
|
+
},
|
|
832
|
+
likely_lookback_miss: {
|
|
833
|
+
calls: likelyLookbackMissState.calls,
|
|
834
|
+
tokens: likelyLookbackMissState.tokens,
|
|
835
|
+
shareOfBucketPct: b.tokens > 0 ? Number(((likelyLookbackMissState.tokens / b.tokens) * 100).toFixed(1)) : 0,
|
|
836
|
+
estimatedUsd: Number(likelyLookbackMissState.rawUsd.toFixed(2)),
|
|
837
|
+
classification: 'likely_lookback_miss',
|
|
838
|
+
attributionStatus: 'heuristic',
|
|
839
|
+
advice: LIKELY_LOOKBACK_MISS_ADVICE
|
|
648
840
|
}
|
|
649
841
|
};
|
|
842
|
+
outBucket.truncationSummary = truncationSummary;
|
|
650
843
|
outBucket.examples = unexplainedExamples
|
|
651
844
|
.sort((a, b) => b.uncached - a.uncached)
|
|
652
845
|
.slice(0, maxExamples);
|
|
@@ -664,6 +857,30 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
664
857
|
};
|
|
665
858
|
}
|
|
666
859
|
|
|
860
|
+
const appendFloorTokens = appendFloorState.tokens;
|
|
861
|
+
const upstreamEvictionTokens = upstreamEvictionState.tokens;
|
|
862
|
+
const excludedUpstreamTokens = appendFloorTokens + upstreamEvictionTokens;
|
|
863
|
+
const actionableWasteTokens =
|
|
864
|
+
bucketState.idle_ttl_miss.tokens +
|
|
865
|
+
bucketState.model_or_effort_switch.tokens +
|
|
866
|
+
actionableResetState.tokens +
|
|
867
|
+
likelyLookbackMissState.tokens;
|
|
868
|
+
const grossWasteTokens = actionableWasteTokens + excludedUpstreamTokens;
|
|
869
|
+
const excludedUpstreamSharePct =
|
|
870
|
+
totalUncachedTokens > 0 ? Number(((excludedUpstreamTokens / totalUncachedTokens) * 100).toFixed(1)) : 0;
|
|
871
|
+
const highUpstreamExclusionShare =
|
|
872
|
+
totalUncachedTokens >= 4096 && excludedUpstreamTokens / totalUncachedTokens > 0.50;
|
|
873
|
+
const notices = [];
|
|
874
|
+
if (highUpstreamExclusionShare) {
|
|
875
|
+
notices.push({
|
|
876
|
+
code: 'HIGH_UPSTREAM_EXCLUSION_SHARE',
|
|
877
|
+
excludedUpstreamTokens,
|
|
878
|
+
totalUncachedTokens,
|
|
879
|
+
excludedUpstreamSharePct,
|
|
880
|
+
message: `${excludedUpstreamSharePct}% of uncached input tokens (${excludedUpstreamTokens.toLocaleString('en-US')} / ${totalUncachedTokens.toLocaleString('en-US')}) were attributed to upstream heuristic patterns (<=60s, 150-260s, 701+/-25, shard-cut, append-floor). Heuristic exclusion >50% may mask real prompt-prefix churn.`
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
|
|
667
884
|
return {
|
|
668
885
|
totalUncachedTokens,
|
|
669
886
|
reconciledSumTokens,
|
|
@@ -674,13 +891,15 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
674
891
|
calls: appendFloorState.calls,
|
|
675
892
|
tokens: appendFloorState.tokens,
|
|
676
893
|
estimatedUsd: Number(appendFloorState.rawUsd.toFixed(2)),
|
|
677
|
-
floorTokens: gemini4AppendFloorTokens
|
|
894
|
+
floorTokens: gemini4AppendFloorTokens,
|
|
895
|
+
attributionStatus: 'attributed_to_upstream'
|
|
678
896
|
},
|
|
679
897
|
upstream_provider_eviction: {
|
|
680
898
|
calls: upstreamEvictionState.calls,
|
|
681
899
|
tokens: upstreamEvictionState.tokens,
|
|
682
900
|
estimatedUsd: Number(upstreamEvictionState.rawUsd.toFixed(2)),
|
|
683
901
|
byPattern: { ...upstreamEvictionState.byPattern },
|
|
902
|
+
attributionStatus: 'attributed_to_upstream',
|
|
684
903
|
advice: UPSTREAM_EVICTION_ADVICE
|
|
685
904
|
},
|
|
686
905
|
claude55_compact_cache_miss: {
|
|
@@ -697,8 +916,28 @@ export function attributeUncachedTokens(calls = [], options = {}) {
|
|
|
697
916
|
estimatedUsd: Number(orchestratorThrashState.rawUsd.toFixed(2)),
|
|
698
917
|
issueRef: '#1638',
|
|
699
918
|
guidance: ORCHESTRATOR_THRASH_GUIDANCE
|
|
919
|
+
},
|
|
920
|
+
router_ping_pong: {
|
|
921
|
+
events: routerPingPongState.events,
|
|
922
|
+
calls: routerPingPongState.calls,
|
|
923
|
+
tokens: routerPingPongState.tokens,
|
|
924
|
+
estimatedUsd: Number(routerPingPongState.rawUsd.toFixed(2)),
|
|
925
|
+
guidance: 'Use a subagent for side questions instead of A->B->A model/effort detours mid-thread.'
|
|
700
926
|
}
|
|
701
927
|
},
|
|
928
|
+
wasteSummary: {
|
|
929
|
+
attributionStatus: 'attributed_to_upstream',
|
|
930
|
+
grossWasteTokens,
|
|
931
|
+
excludedUpstreamTokens,
|
|
932
|
+
actionableWasteTokens,
|
|
933
|
+
appendFloorTokens,
|
|
934
|
+
upstreamEvictionTokens,
|
|
935
|
+
evictionByPattern: { ...upstreamEvictionState.byPattern },
|
|
936
|
+
excludedUpstreamSharePct,
|
|
937
|
+
highUpstreamExclusionShare
|
|
938
|
+
},
|
|
939
|
+
notices,
|
|
940
|
+
subThresholdSummary,
|
|
702
941
|
normalizedHitRate: {
|
|
703
942
|
totalPromptTokens,
|
|
704
943
|
cacheReadTokens: totalCacheReadTokens,
|