ctxtop 4.0.0 → 4.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/.antigravity-plugin/plugin.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/CHECKSUMS.sha256 +59 -42
- package/README.md +351 -330
- package/bin/ah-statusline.mjs +30 -0
- package/bin/ctxtop.mjs +330 -5
- package/config/default_flightcheck.json +19 -0
- package/config/modelCatalog.json +165 -0
- package/hooks/handlers/compact-lifecycle.mjs +323 -8
- package/hooks/handlers/config-change.mjs +402 -0
- package/hooks/handlers/hookIo.mjs +56 -0
- package/hooks/handlers/post-model-switch.mjs +111 -0
- package/hooks/handlers/post-tool-use.mjs +117 -16
- package/hooks/handlers/pre-model-switch.mjs +823 -24
- package/hooks/handlers/pre-tool-use.mjs +271 -4
- package/hooks/handlers/stop-gate.mjs +297 -11
- package/hooks/handlers/user-prompt-submit.mjs +224 -0
- package/hooks/hooks.json +50 -0
- package/hooks/post_tool_use.mjs +8 -0
- package/hooks/register.js +48 -9
- package/hooks/session_start.mjs +11 -1
- package/hooks/stop.mjs +11 -1
- package/package.json +11 -4
- package/packages/antigravity/package.json +1 -1
- package/packages/claude-code/package.json +1 -1
- package/packages/core/package.json +1 -1
- package/packages/engine-node/package.json +1 -1
- package/packages/mcp/package.json +1 -1
- package/packages/mcp/src/server.mjs +1 -1
- package/packages/pro/package.json +1 -1
- package/plugin.json +1 -1
- package/sidecars/horizon-daemon/server.mjs +38 -2
- package/skills/handoff/SKILL.md +8 -0
- package/src/adapters/antigravity/index.mjs +31 -1
- package/src/adapters/claude-code/index.mjs +40 -1
- package/src/cli/horizon_hud.mjs +254 -52
- package/src/config/modelCatalog.json +84 -0
- package/src/core/bucket_b_runtime.mjs +1200 -0
- package/src/core/constants.mjs +191 -4
- package/src/core/detectors_det01_to_16.mjs +54 -5
- package/src/core/doc_indexer.mjs +338 -0
- package/src/core/layer5_hygiene.mjs +118 -0
- package/src/core/parser.mjs +2 -1
- package/src/core/paths.mjs +3 -2
- package/src/core/rsl_clash_auditor.mjs +423 -0
- package/src/core/rsl_distiller.mjs +372 -0
- package/src/core/rsl_ledger.mjs +969 -0
- package/src/core/rsl_triage.mjs +909 -0
- package/src/core/session_auditor.mjs +658 -0
- package/src/core/shield_manager.mjs +1417 -0
- package/src/core/task_quota_router.mjs +1 -1
- package/src/core-isomorphic/coach/cacheGravityRouter.mjs +874 -47
- package/src/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
- package/src/core-isomorphic/coach/smartCompactAdvisor.mjs +305 -0
- package/src/core-isomorphic/coach/ttlWatchdog.mjs +13 -6
- package/src/core-isomorphic/config/anthropicModelCatalog.json +404 -1
- package/src/core-isomorphic/config/catalogLoader.mjs +546 -46
- package/src/core-isomorphic/config/horizonConfig.mjs +47 -1
- package/src/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +266 -0
- package/src/core-isomorphic/economics/cacheGravity.mjs +18 -0
- package/src/core-isomorphic/economics/cliffBreaker.mjs +98 -12
- package/src/core-isomorphic/economics/driftLatency.mjs +114 -0
- package/src/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
- package/src/core-isomorphic/guards/bucketBGamma.mjs +442 -0
- package/src/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
- package/src/core-isomorphic/guards/circuitBreakers.mjs +191 -191
- package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +154 -5
- package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +103 -11
- package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +311 -1
- package/src/core-isomorphic/guards/docIndexCore.mjs +446 -0
- package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +16 -0
- package/src/core-isomorphic/guards/slopsquatGate.mjs +551 -0
- package/src/core-isomorphic/index.js +1 -0
- package/src/core-isomorphic/telemetry/telemetryBridge.mjs +36 -12
- package/src/core-isomorphic/ui/horizonPanelHtml.mjs +293 -237
- package/src/core-isomorphic/ui/modViews.mjs +35 -9
- package/src/diagnostics/claudeHookCapabilities.mjs +396 -0
- package/src/diagnostics/doctorEngine.mjs +95 -3
- package/src/engine-node/db/probe.mjs +234 -2
- package/src/engine-node/index.mjs +3 -0
- package/src/engine-node/sensors/blindspots.mjs +13 -6
- package/src/hooks/antigravity_rsl_hook.mjs +250 -0
- package/src/hooks/claude_rsl_hook.mjs +288 -0
- package/src/hooks/post_invocation.mjs +196 -4
- package/src/hooks/pre_invocation.mjs +732 -22
- package/src/hooks/session_start.mjs +39 -1
- package/src/utils/modelSwitchTelemetry.mjs +308 -0
- package/src/utils/sessionCalibration.mjs +657 -0
- package/src/webview/horizon_panel.html +1325 -317
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/core/session_auditor.mjs
|
|
3
|
+
* Zero-Write Cross-Platform Historical Session Auditor (`ctxtop audit`).
|
|
4
|
+
*
|
|
5
|
+
* Scans local Antigravity (`transcript_full.jsonl` / `transcript.jsonl`) and Claude Code
|
|
6
|
+
* (`~/.claude/projects/**\/*.jsonl`, deduplicating streaming chunks by `message.id`) transcripts
|
|
7
|
+
* alongside `.agents/telemetry/*.hints.jsonl` and `.claude/cache/horizon_ledger.jsonl`.
|
|
8
|
+
*
|
|
9
|
+
* Guardrails:
|
|
10
|
+
* - Strictly read-only: zero workspace mutations, zero network calls, zero OAuth file reads.
|
|
11
|
+
* - Strictly excludes `packages/airlock/` and sister SKU paths.
|
|
12
|
+
* - Framed strictly around Session ROI & Quota Shield (`Sludge Tax`, `Cold-Cache Spikes`,
|
|
13
|
+
* `Thrash Loops`, `Oversized Reads`, `Top 3 Offending Turns`) — never developer grades.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
import os from 'node:os';
|
|
19
|
+
import { normalizeModelKey, resolveModelRates, resolveModelCeiling } from './constants.mjs';
|
|
20
|
+
import { isForbiddenSkuPath } from './doc_indexer.mjs';
|
|
21
|
+
import { getSavedQuotaReceiptSummary } from '../core-isomorphic/coach/savedQuotaLedger.mjs';
|
|
22
|
+
|
|
23
|
+
export const COLD_CACHE_IDLE_GAP_MS = 300_000; // 5m TTL
|
|
24
|
+
export const OVERSIZED_READ_CHARS_THRESHOLD = 48_000; // ~1,200 lines / ~12k tokens
|
|
25
|
+
export const OVERSIZED_TOOL_OUTPUT_CHARS_THRESHOLD = 16_000; // ~4k tokens
|
|
26
|
+
|
|
27
|
+
function resolveHome(options = {}) {
|
|
28
|
+
return (
|
|
29
|
+
options.homeDir ||
|
|
30
|
+
process.env.USERPROFILE ||
|
|
31
|
+
process.env.HOME ||
|
|
32
|
+
os.homedir()
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseIsoOrNumMs(ts) {
|
|
37
|
+
if (typeof ts === 'number' && Number.isFinite(ts)) return ts;
|
|
38
|
+
if (typeof ts === 'string' && ts.trim()) {
|
|
39
|
+
const parsed = Date.parse(ts.trim());
|
|
40
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
41
|
+
}
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function listClaudeJsonlFiles(claudeRoot, cutoffMs) {
|
|
46
|
+
const results = [];
|
|
47
|
+
if (!claudeRoot || !fs.existsSync(claudeRoot)) return results;
|
|
48
|
+
const walk = (dir, depth = 0) => {
|
|
49
|
+
if (depth > 5) return;
|
|
50
|
+
let entries = [];
|
|
51
|
+
try {
|
|
52
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
53
|
+
} catch {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
for (const ent of entries) {
|
|
57
|
+
if (ent.name === 'node_modules' || ent.name === '.git' || ent.name === 'airlock') continue;
|
|
58
|
+
const full = path.join(dir, ent.name);
|
|
59
|
+
if (isForbiddenSkuPath(full)) continue;
|
|
60
|
+
if (ent.isDirectory()) {
|
|
61
|
+
walk(full, depth + 1);
|
|
62
|
+
} else if (ent.isFile() && ent.name.endsWith('.jsonl')) {
|
|
63
|
+
try {
|
|
64
|
+
const st = fs.statSync(full);
|
|
65
|
+
if (st.mtimeMs >= cutoffMs) {
|
|
66
|
+
results.push({ path: full, mtimeMs: st.mtimeMs, sessionId: path.basename(ent.name, '.jsonl') });
|
|
67
|
+
}
|
|
68
|
+
} catch {
|
|
69
|
+
// Ignore unreadable file
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
walk(claudeRoot, 0);
|
|
75
|
+
return results.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function listAntigravityTranscriptFiles(brainDirs, cutoffMs) {
|
|
79
|
+
const results = [];
|
|
80
|
+
const seen = new Set();
|
|
81
|
+
for (const brainRoot of brainDirs) {
|
|
82
|
+
if (!brainRoot || !fs.existsSync(brainRoot)) continue;
|
|
83
|
+
let convDirs = [];
|
|
84
|
+
try {
|
|
85
|
+
convDirs = fs.readdirSync(brainRoot, { withFileTypes: true }).filter((d) => d.isDirectory());
|
|
86
|
+
} catch {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
for (const d of convDirs) {
|
|
90
|
+
const convDir = path.join(brainRoot, d.name);
|
|
91
|
+
if (seen.has(convDir) || isForbiddenSkuPath(convDir)) continue;
|
|
92
|
+
seen.add(convDir);
|
|
93
|
+
const logsDir = path.join(convDir, '.system_generated', 'logs');
|
|
94
|
+
const fullLog = path.join(logsDir, 'transcript_full.jsonl');
|
|
95
|
+
const compactLog = path.join(logsDir, 'transcript.jsonl');
|
|
96
|
+
const chosen = fs.existsSync(fullLog) ? fullLog : fs.existsSync(compactLog) ? compactLog : null;
|
|
97
|
+
if (!chosen) continue;
|
|
98
|
+
try {
|
|
99
|
+
const st = fs.statSync(chosen);
|
|
100
|
+
if (st.mtimeMs >= cutoffMs) {
|
|
101
|
+
results.push({
|
|
102
|
+
path: chosen,
|
|
103
|
+
mtimeMs: st.mtimeMs,
|
|
104
|
+
sessionId: d.name
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
} catch {
|
|
108
|
+
// Ignore unreadable transcript
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return results.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function estimateSludgeCostUSD(modelId, wastedTokens, tsMs, isColdCacheWrite = false) {
|
|
116
|
+
const tok = Math.max(0, Number(wastedTokens) || 0);
|
|
117
|
+
if (tok === 0) return 0;
|
|
118
|
+
const rates = resolveModelRates(modelId || 'claude-sonnet-4-6', tsMs || Date.now(), 'standard', {
|
|
119
|
+
totalPromptTokens: tok
|
|
120
|
+
});
|
|
121
|
+
if (isColdCacheWrite) {
|
|
122
|
+
const writeDiff = Math.max(
|
|
123
|
+
0.25,
|
|
124
|
+
Number(rates.write5mPer1M || 3.75) - Number(rates.cachedReadPer1M || 0.3)
|
|
125
|
+
);
|
|
126
|
+
return Number(((tok / 1_000_000) * writeDiff).toFixed(4));
|
|
127
|
+
}
|
|
128
|
+
return Number(((tok / 1_000_000) * Number(rates.inputPer1M || 3.0)).toFixed(4));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Audits a single Claude Code `.jsonl` session transcript, deduplicating streaming
|
|
133
|
+
* assistant chunks by `message.id`.
|
|
134
|
+
*
|
|
135
|
+
* @param {{ path: string, sessionId: string, mtimeMs: number }} item
|
|
136
|
+
*/
|
|
137
|
+
export function scoreClaudeTranscriptFile(item) {
|
|
138
|
+
const lines = fs.readFileSync(item.path, 'utf8').split(/\r?\n/).filter(Boolean);
|
|
139
|
+
const assistantByMsgId = new Map();
|
|
140
|
+
const orderedEvents = [];
|
|
141
|
+
let fallbackIdx = 0;
|
|
142
|
+
|
|
143
|
+
for (const line of lines) {
|
|
144
|
+
let row;
|
|
145
|
+
try {
|
|
146
|
+
row = JSON.parse(line);
|
|
147
|
+
} catch {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (!row || typeof row !== 'object') continue;
|
|
151
|
+
const tsMs = parseIsoOrNumMs(row.timestamp || row.created_at || row.createdAt);
|
|
152
|
+
|
|
153
|
+
if (row.type === 'assistant' || row.message?.role === 'assistant') {
|
|
154
|
+
const msg = row.message || row;
|
|
155
|
+
const msgId = String(msg.id || row.uuid || `anon_${++fallbackIdx}`);
|
|
156
|
+
const usage = msg.usage || row.usage || {};
|
|
157
|
+
const outTok = Math.max(0, Number(usage.output_tokens || 0));
|
|
158
|
+
const inTok = Math.max(0, Number(usage.input_tokens || 0));
|
|
159
|
+
const cacheRead = Math.max(0, Number(usage.cache_read_input_tokens || 0));
|
|
160
|
+
const cacheWrite = Math.max(
|
|
161
|
+
0,
|
|
162
|
+
Number(
|
|
163
|
+
usage.cache_creation_input_tokens ||
|
|
164
|
+
(Number(usage.cache_creation?.ephemeral_5m_input_tokens || 0) +
|
|
165
|
+
Number(usage.cache_creation?.ephemeral_1h_input_tokens || 0)) ||
|
|
166
|
+
0
|
|
167
|
+
)
|
|
168
|
+
);
|
|
169
|
+
const contentBlocks = Array.isArray(msg.content) ? msg.content : [];
|
|
170
|
+
const prior = assistantByMsgId.get(msgId);
|
|
171
|
+
if (!prior) {
|
|
172
|
+
const entry = {
|
|
173
|
+
kind: 'assistant',
|
|
174
|
+
msgId,
|
|
175
|
+
tsMs,
|
|
176
|
+
modelId: normalizeModelKey(msg.model || row.model || 'claude-sonnet-4-6') || 'claude-sonnet-4-6',
|
|
177
|
+
inputTokens: inTok,
|
|
178
|
+
outputTokens: outTok,
|
|
179
|
+
cacheReadTokens: cacheRead,
|
|
180
|
+
cacheWriteTokens: cacheWrite,
|
|
181
|
+
toolUses: contentBlocks.filter((b) => b && b.type === 'tool_use')
|
|
182
|
+
};
|
|
183
|
+
assistantByMsgId.set(msgId, entry);
|
|
184
|
+
orderedEvents.push(entry);
|
|
185
|
+
} else {
|
|
186
|
+
prior.inputTokens = Math.max(prior.inputTokens, inTok);
|
|
187
|
+
prior.outputTokens = Math.max(prior.outputTokens, outTok);
|
|
188
|
+
prior.cacheReadTokens = Math.max(prior.cacheReadTokens, cacheRead);
|
|
189
|
+
prior.cacheWriteTokens = Math.max(prior.cacheWriteTokens, cacheWrite);
|
|
190
|
+
if (tsMs > prior.tsMs) prior.tsMs = tsMs;
|
|
191
|
+
const moreTools = contentBlocks.filter((b) => b && b.type === 'tool_use');
|
|
192
|
+
if (moreTools.length > prior.toolUses.length) {
|
|
193
|
+
prior.toolUses = moreTools;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
} else if (row.type === 'user' || row.message?.role === 'user') {
|
|
197
|
+
const content = row.message?.content ?? row.content;
|
|
198
|
+
const toolResults = Array.isArray(content)
|
|
199
|
+
? content.filter((b) => b && b.type === 'tool_result')
|
|
200
|
+
: [];
|
|
201
|
+
orderedEvents.push({
|
|
202
|
+
kind: 'user',
|
|
203
|
+
tsMs,
|
|
204
|
+
toolResults
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let turnIndex = 0;
|
|
210
|
+
let lastAssistantTsMs = 0;
|
|
211
|
+
let consecutiveToolErrors = 0;
|
|
212
|
+
let thrashLoops = 0;
|
|
213
|
+
let coldCacheSpikes = 0;
|
|
214
|
+
let oversizedReads = 0;
|
|
215
|
+
let oversizedOutputs = 0;
|
|
216
|
+
let sludgeTaxTokens = 0;
|
|
217
|
+
let sludgeTaxUSD = 0;
|
|
218
|
+
let totalPromptTokens = 0;
|
|
219
|
+
let activeModelId = 'claude-sonnet-4-6';
|
|
220
|
+
const toolUseById = new Map();
|
|
221
|
+
const offendingTurns = [];
|
|
222
|
+
|
|
223
|
+
for (const ev of orderedEvents) {
|
|
224
|
+
if (ev.kind === 'assistant') {
|
|
225
|
+
turnIndex += 1;
|
|
226
|
+
activeModelId = ev.modelId || activeModelId;
|
|
227
|
+
totalPromptTokens += ev.inputTokens + ev.cacheReadTokens + ev.cacheWriteTokens;
|
|
228
|
+
|
|
229
|
+
for (const tu of ev.toolUses) {
|
|
230
|
+
if (tu?.id) toolUseById.set(tu.id, { name: tu.name, input: tu.input || {}, turnIndex });
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Detect >5m idle Cold-Cache Resume Spike
|
|
234
|
+
if (
|
|
235
|
+
lastAssistantTsMs > 0 &&
|
|
236
|
+
ev.tsMs > 0 &&
|
|
237
|
+
ev.tsMs - lastAssistantTsMs > COLD_CACHE_IDLE_GAP_MS &&
|
|
238
|
+
ev.cacheWriteTokens >= 10_000
|
|
239
|
+
) {
|
|
240
|
+
coldCacheSpikes += 1;
|
|
241
|
+
const usd = estimateSludgeCostUSD(activeModelId, ev.cacheWriteTokens, ev.tsMs, true);
|
|
242
|
+
sludgeTaxTokens += ev.cacheWriteTokens;
|
|
243
|
+
sludgeTaxUSD += usd;
|
|
244
|
+
offendingTurns.push({
|
|
245
|
+
sessionId: item.sessionId,
|
|
246
|
+
platform: 'claude',
|
|
247
|
+
turnIndex,
|
|
248
|
+
modelId: activeModelId,
|
|
249
|
+
category: 'COLD_CACHE_SPIKE',
|
|
250
|
+
wastedTokens: ev.cacheWriteTokens,
|
|
251
|
+
wastedUSD: usd,
|
|
252
|
+
citation: `${item.sessionId}#T${turnIndex} (${Math.round((ev.tsMs - lastAssistantTsMs) / 60000)}m idle gap rebuilt ${ev.cacheWriteTokens.toLocaleString('en-US')} prefix tok)`,
|
|
253
|
+
fix: 'Finish tool batch before 5m TTL expiry or compact before resuming cold threads.'
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
if (ev.tsMs > 0) lastAssistantTsMs = ev.tsMs;
|
|
257
|
+
} else if (ev.kind === 'user') {
|
|
258
|
+
for (const tr of ev.toolResults) {
|
|
259
|
+
const linked = tr.tool_use_id ? toolUseById.get(tr.tool_use_id) : null;
|
|
260
|
+
const toolName = String(linked?.name || tr.tool_name || '');
|
|
261
|
+
const toolInput = linked?.input || {};
|
|
262
|
+
const tIdx = linked?.turnIndex || Math.max(1, turnIndex);
|
|
263
|
+
const text =
|
|
264
|
+
typeof tr.content === 'string'
|
|
265
|
+
? tr.content
|
|
266
|
+
: Array.isArray(tr.content)
|
|
267
|
+
? tr.content.map((c) => (typeof c?.text === 'string' ? c.text : '')).join('\n')
|
|
268
|
+
: '';
|
|
269
|
+
|
|
270
|
+
const isError = Boolean(tr.is_error);
|
|
271
|
+
if (isError) {
|
|
272
|
+
consecutiveToolErrors += 1;
|
|
273
|
+
if (consecutiveToolErrors === 3) {
|
|
274
|
+
thrashLoops += 1;
|
|
275
|
+
const thrashTok = 6_000;
|
|
276
|
+
const usd = estimateSludgeCostUSD(activeModelId, thrashTok, ev.tsMs, false);
|
|
277
|
+
sludgeTaxTokens += thrashTok;
|
|
278
|
+
sludgeTaxUSD += usd;
|
|
279
|
+
offendingTurns.push({
|
|
280
|
+
sessionId: item.sessionId,
|
|
281
|
+
platform: 'claude',
|
|
282
|
+
turnIndex: tIdx,
|
|
283
|
+
modelId: activeModelId,
|
|
284
|
+
category: 'THRASH_3X_LOOP',
|
|
285
|
+
wastedTokens: thrashTok,
|
|
286
|
+
wastedUSD: usd,
|
|
287
|
+
citation: `${item.sessionId}#T${tIdx} (3 consecutive failed ${toolName || 'tool'} calls)`,
|
|
288
|
+
fix: 'Enable Active Shield (`ctxtop shield --enable`) to halt 3x failure loops and inspect error lines first.'
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
} else {
|
|
292
|
+
consecutiveToolErrors = 0;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const isRead = /^Read$/i.test(toolName);
|
|
296
|
+
const hasSlice = toolInput.offset !== undefined || toolInput.limit !== undefined || toolInput.pages !== undefined;
|
|
297
|
+
if (isRead && !hasSlice && text.length >= OVERSIZED_READ_CHARS_THRESHOLD) {
|
|
298
|
+
oversizedReads += 1;
|
|
299
|
+
const estTok = Math.max(0, Math.ceil(text.length / 4) - 600);
|
|
300
|
+
const usd = estimateSludgeCostUSD(activeModelId, estTok, ev.tsMs, false);
|
|
301
|
+
sludgeTaxTokens += estTok;
|
|
302
|
+
sludgeTaxUSD += usd;
|
|
303
|
+
offendingTurns.push({
|
|
304
|
+
sessionId: item.sessionId,
|
|
305
|
+
platform: 'claude',
|
|
306
|
+
turnIndex: tIdx,
|
|
307
|
+
modelId: activeModelId,
|
|
308
|
+
category: 'UNSLICED_LARGE_READ',
|
|
309
|
+
wastedTokens: estTok,
|
|
310
|
+
wastedUSD: usd,
|
|
311
|
+
citation: `${item.sessionId}#T${tIdx} (un-sliced Read of ${path.basename(String(toolInput.file_path || 'file'))}, ~${estTok.toLocaleString('en-US')} excess tok)`,
|
|
312
|
+
fix: 'Run `ctxtop shield --enable` to auto-index files >1,200 lines and enforce offset/limit slices.'
|
|
313
|
+
});
|
|
314
|
+
} else if (!isRead && text.length >= OVERSIZED_TOOL_OUTPUT_CHARS_THRESHOLD) {
|
|
315
|
+
oversizedOutputs += 1;
|
|
316
|
+
const estTok = Math.max(0, Math.ceil(text.length / 4) - 350);
|
|
317
|
+
const usd = estimateSludgeCostUSD(activeModelId, estTok, ev.tsMs, false);
|
|
318
|
+
sludgeTaxTokens += estTok;
|
|
319
|
+
sludgeTaxUSD += usd;
|
|
320
|
+
offendingTurns.push({
|
|
321
|
+
sessionId: item.sessionId,
|
|
322
|
+
platform: 'claude',
|
|
323
|
+
turnIndex: tIdx,
|
|
324
|
+
modelId: activeModelId,
|
|
325
|
+
category: 'OVERSIZED_TOOL_DUMP',
|
|
326
|
+
wastedTokens: estTok,
|
|
327
|
+
wastedUSD: usd,
|
|
328
|
+
citation: `${item.sessionId}#T${tIdx} (${toolName || 'Bash'} dumped ~${Math.ceil(text.length / 4).toLocaleString('en-US')} raw tok)`,
|
|
329
|
+
fix: 'Enable Highbeam PostToolUse sanitizer (`ctxtop shield --enable`) to offload >4k-token logs with 100% error recovery.'
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return {
|
|
337
|
+
sessionId: item.sessionId,
|
|
338
|
+
platform: 'claude',
|
|
339
|
+
transcriptPath: item.path,
|
|
340
|
+
turns: turnIndex,
|
|
341
|
+
totalPromptTokens,
|
|
342
|
+
sludgeTaxTokens,
|
|
343
|
+
sludgeTaxUSD: Number(sludgeTaxUSD.toFixed(4)),
|
|
344
|
+
coldCacheSpikes,
|
|
345
|
+
thrashLoops,
|
|
346
|
+
oversizedReads,
|
|
347
|
+
oversizedOutputs,
|
|
348
|
+
offendingTurns
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Audits a single Antigravity `transcript_full.jsonl` / `transcript.jsonl` session file.
|
|
354
|
+
*
|
|
355
|
+
* @param {{ path: string, sessionId: string, mtimeMs: number }} item
|
|
356
|
+
*/
|
|
357
|
+
export function scoreAntigravityTranscriptFile(item) {
|
|
358
|
+
const lines = fs.readFileSync(item.path, 'utf8').split(/\r?\n/).filter(Boolean);
|
|
359
|
+
let turnIndex = 0;
|
|
360
|
+
let lastStepTsMs = 0;
|
|
361
|
+
let consecutiveErrors = 0;
|
|
362
|
+
let thrashLoops = 0;
|
|
363
|
+
let coldCacheSpikes = 0;
|
|
364
|
+
let oversizedReads = 0;
|
|
365
|
+
let oversizedOutputs = 0;
|
|
366
|
+
let sludgeTaxTokens = 0;
|
|
367
|
+
let sludgeTaxUSD = 0;
|
|
368
|
+
let cumulativeContextTokens = 0;
|
|
369
|
+
let activeModelId = 'gemini-4-carbon';
|
|
370
|
+
let lastToolCall = null;
|
|
371
|
+
const offendingTurns = [];
|
|
372
|
+
|
|
373
|
+
for (const line of lines) {
|
|
374
|
+
let step;
|
|
375
|
+
try {
|
|
376
|
+
step = JSON.parse(line);
|
|
377
|
+
} catch {
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (!step || typeof step !== 'object') continue;
|
|
381
|
+
const tsMs = parseIsoOrNumMs(step.created_at || step.timestamp);
|
|
382
|
+
if (step.model || step.modelName) {
|
|
383
|
+
activeModelId = normalizeModelKey(step.model || step.modelName) || activeModelId;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const stepType = String(step.type || '').toUpperCase();
|
|
387
|
+
const contentStr =
|
|
388
|
+
typeof step.content === 'string'
|
|
389
|
+
? step.content
|
|
390
|
+
: step.content
|
|
391
|
+
? JSON.stringify(step.content)
|
|
392
|
+
: '';
|
|
393
|
+
const contentTok = Math.ceil(contentStr.length / 4);
|
|
394
|
+
const modelCeiling = resolveModelCeiling(activeModelId) || 256_000;
|
|
395
|
+
|
|
396
|
+
if (
|
|
397
|
+
stepType.includes('COMPACT') ||
|
|
398
|
+
(stepType === 'USER_INPUT' && /^\s*\/compact\b/i.test(contentStr))
|
|
399
|
+
) {
|
|
400
|
+
cumulativeContextTokens = Math.min(cumulativeContextTokens, 16_000);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (stepType === 'PLANNER_RESPONSE' || Array.isArray(step.tool_calls)) {
|
|
404
|
+
turnIndex += 1;
|
|
405
|
+
const activeWindowTok = Math.min(cumulativeContextTokens, modelCeiling);
|
|
406
|
+
if (
|
|
407
|
+
lastStepTsMs > 0 &&
|
|
408
|
+
tsMs > 0 &&
|
|
409
|
+
tsMs - lastStepTsMs > COLD_CACHE_IDLE_GAP_MS &&
|
|
410
|
+
activeWindowTok >= 15_000
|
|
411
|
+
) {
|
|
412
|
+
coldCacheSpikes += 1;
|
|
413
|
+
const usd = estimateSludgeCostUSD(activeModelId, activeWindowTok, tsMs, true);
|
|
414
|
+
sludgeTaxTokens += activeWindowTok;
|
|
415
|
+
sludgeTaxUSD += usd;
|
|
416
|
+
offendingTurns.push({
|
|
417
|
+
sessionId: item.sessionId,
|
|
418
|
+
platform: 'antigravity',
|
|
419
|
+
turnIndex,
|
|
420
|
+
modelId: activeModelId,
|
|
421
|
+
category: 'COLD_CACHE_SPIKE',
|
|
422
|
+
wastedTokens: activeWindowTok,
|
|
423
|
+
wastedUSD: usd,
|
|
424
|
+
citation: `${item.sessionId}#T${turnIndex} (${Math.round((tsMs - lastStepTsMs) / 60000)}m idle gap on ~${activeWindowTok.toLocaleString('en-US')}-tok context)`,
|
|
425
|
+
fix: 'Compact or checkpoint to task.md before >5m idle pauses to avoid cold-prefix rebuild spikes.'
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
if (tsMs > 0) lastStepTsMs = tsMs;
|
|
429
|
+
const calls = Array.isArray(step.tool_calls) ? step.tool_calls : [];
|
|
430
|
+
lastToolCall = calls.length > 0 ? calls[0] : null;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
cumulativeContextTokens = Math.min(modelCeiling, cumulativeContextTokens + contentTok);
|
|
434
|
+
|
|
435
|
+
const statusUpper = String(step.status || '').toUpperCase();
|
|
436
|
+
const failed =
|
|
437
|
+
statusUpper === 'ERROR' ||
|
|
438
|
+
/Command exited with code [1-9]/i.test(contentStr);
|
|
439
|
+
if (failed) {
|
|
440
|
+
consecutiveErrors += 1;
|
|
441
|
+
if (consecutiveErrors === 3) {
|
|
442
|
+
thrashLoops += 1;
|
|
443
|
+
const thrashTok = 6_000;
|
|
444
|
+
const usd = estimateSludgeCostUSD(activeModelId, thrashTok, tsMs, false);
|
|
445
|
+
sludgeTaxTokens += thrashTok;
|
|
446
|
+
sludgeTaxUSD += usd;
|
|
447
|
+
offendingTurns.push({
|
|
448
|
+
sessionId: item.sessionId,
|
|
449
|
+
platform: 'antigravity',
|
|
450
|
+
turnIndex: Math.max(1, turnIndex),
|
|
451
|
+
modelId: activeModelId,
|
|
452
|
+
category: 'THRASH_3X_LOOP',
|
|
453
|
+
wastedTokens: thrashTok,
|
|
454
|
+
wastedUSD: usd,
|
|
455
|
+
citation: `${item.sessionId}#T${Math.max(1, turnIndex)} (3 consecutive tool/command errors)`,
|
|
456
|
+
fix: 'Pause blind retries after 2 failures and inspect the indexed error log.'
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
} else if (stepType.includes('TOOL') || contentStr.includes('Command exited with code 0')) {
|
|
460
|
+
consecutiveErrors = 0;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const callName = String(lastToolCall?.name || lastToolCall?.tool_name || '');
|
|
464
|
+
const callArgs = lastToolCall?.args || lastToolCall?.parameters || lastToolCall?.arguments || {};
|
|
465
|
+
const isRead = /^(?:default_api:)?(?:read_file|view_file)$/i.test(callName);
|
|
466
|
+
const hasBounds =
|
|
467
|
+
callArgs.StartLine !== undefined ||
|
|
468
|
+
callArgs.EndLine !== undefined ||
|
|
469
|
+
callArgs.offset !== undefined ||
|
|
470
|
+
callArgs.limit !== undefined;
|
|
471
|
+
|
|
472
|
+
if (stepType !== 'PLANNER_RESPONSE' && stepType !== 'USER_INPUT' && contentStr.length >= OVERSIZED_TOOL_OUTPUT_CHARS_THRESHOLD) {
|
|
473
|
+
if (isRead && !hasBounds && contentStr.length >= OVERSIZED_READ_CHARS_THRESHOLD) {
|
|
474
|
+
oversizedReads += 1;
|
|
475
|
+
const estTok = Math.max(0, contentTok - 600);
|
|
476
|
+
const usd = estimateSludgeCostUSD(activeModelId, estTok, tsMs, false);
|
|
477
|
+
sludgeTaxTokens += estTok;
|
|
478
|
+
sludgeTaxUSD += usd;
|
|
479
|
+
offendingTurns.push({
|
|
480
|
+
sessionId: item.sessionId,
|
|
481
|
+
platform: 'antigravity',
|
|
482
|
+
turnIndex: Math.max(1, turnIndex),
|
|
483
|
+
modelId: activeModelId,
|
|
484
|
+
category: 'UNSLICED_LARGE_READ',
|
|
485
|
+
wastedTokens: estTok,
|
|
486
|
+
wastedUSD: usd,
|
|
487
|
+
citation: `${item.sessionId}#T${Math.max(1, turnIndex)} (un-sliced ${callName || 'read_file'} of ${path.basename(String(callArgs.Path || callArgs.AbsolutePath || 'file'))}, ~${estTok.toLocaleString('en-US')} excess tok)`,
|
|
488
|
+
fix: 'Enable Active Shield (`ctxtop shield --enable`) to index >1,200-line files and enforce StartLine/EndLine.'
|
|
489
|
+
});
|
|
490
|
+
} else if (!isRead) {
|
|
491
|
+
oversizedOutputs += 1;
|
|
492
|
+
const estTok = Math.max(0, contentTok - 350);
|
|
493
|
+
const usd = estimateSludgeCostUSD(activeModelId, estTok, tsMs, false);
|
|
494
|
+
sludgeTaxTokens += estTok;
|
|
495
|
+
sludgeTaxUSD += usd;
|
|
496
|
+
offendingTurns.push({
|
|
497
|
+
sessionId: item.sessionId,
|
|
498
|
+
platform: 'antigravity',
|
|
499
|
+
turnIndex: Math.max(1, turnIndex),
|
|
500
|
+
modelId: activeModelId,
|
|
501
|
+
category: 'OVERSIZED_TOOL_DUMP',
|
|
502
|
+
wastedTokens: estTok,
|
|
503
|
+
wastedUSD: usd,
|
|
504
|
+
citation: `${item.sessionId}#T${Math.max(1, turnIndex)} (~${contentTok.toLocaleString('en-US')} raw tool output tok)`,
|
|
505
|
+
fix: 'Pipe verbose test/grep commands or enable Highbeam output compaction.'
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
lastToolCall = null;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
return {
|
|
513
|
+
sessionId: item.sessionId,
|
|
514
|
+
platform: 'antigravity',
|
|
515
|
+
transcriptPath: item.path,
|
|
516
|
+
turns: turnIndex,
|
|
517
|
+
totalPromptTokens: cumulativeContextTokens,
|
|
518
|
+
sludgeTaxTokens,
|
|
519
|
+
sludgeTaxUSD: Number(sludgeTaxUSD.toFixed(4)),
|
|
520
|
+
coldCacheSpikes,
|
|
521
|
+
thrashLoops,
|
|
522
|
+
oversizedReads,
|
|
523
|
+
oversizedOutputs,
|
|
524
|
+
offendingTurns
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Runs a zero-write cross-platform audit across historical Antigravity and Claude Code sessions.
|
|
530
|
+
*
|
|
531
|
+
* @param {object} [options={}]
|
|
532
|
+
* @param {'all' | 'antigravity' | 'claude'} [options.platform='all']
|
|
533
|
+
* @param {number} [options.days=7]
|
|
534
|
+
* @param {number} [options.limit=10]
|
|
535
|
+
* @param {string} [options.cwd]
|
|
536
|
+
* @param {string} [options.homeDir]
|
|
537
|
+
* @param {string[]} [options.antigravityDirs]
|
|
538
|
+
* @param {string} [options.claudeProjectsDir]
|
|
539
|
+
* @param {number} [options.nowMs]
|
|
540
|
+
*/
|
|
541
|
+
export function runSessionAudit(options = {}) {
|
|
542
|
+
const platform = String(options.platform || 'all').toLowerCase();
|
|
543
|
+
const days = Math.max(1, Number(options.days) || 7);
|
|
544
|
+
const limit = Math.max(1, Number(options.limit) || 10);
|
|
545
|
+
const nowMs = Number(options.nowMs) || Date.now();
|
|
546
|
+
const cutoffMs = nowMs - days * 86_400_000;
|
|
547
|
+
const home = resolveHome(options);
|
|
548
|
+
const baseCwd = path.resolve(options.cwd || process.cwd());
|
|
549
|
+
|
|
550
|
+
const includeAg = platform === 'all' || platform === 'antigravity';
|
|
551
|
+
const includeClaude = platform === 'all' || platform === 'claude';
|
|
552
|
+
|
|
553
|
+
const agDirs = Array.isArray(options.antigravityDirs)
|
|
554
|
+
? options.antigravityDirs
|
|
555
|
+
: [
|
|
556
|
+
path.join(home, '.gemini', 'antigravity', 'brain'),
|
|
557
|
+
path.join(home, '.gemini', 'antigravity-cli', 'brain')
|
|
558
|
+
];
|
|
559
|
+
const claudeDir =
|
|
560
|
+
options.claudeProjectsDir || path.join(home, '.claude', 'projects');
|
|
561
|
+
|
|
562
|
+
const candidates = [];
|
|
563
|
+
if (includeAg) {
|
|
564
|
+
for (const f of listAntigravityTranscriptFiles(agDirs, cutoffMs)) {
|
|
565
|
+
candidates.push({ ...f, platform: 'antigravity' });
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
if (includeClaude) {
|
|
569
|
+
for (const f of listClaudeJsonlFiles(claudeDir, cutoffMs)) {
|
|
570
|
+
candidates.push({ ...f, platform: 'claude' });
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
candidates.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
575
|
+
const selected = candidates.slice(0, limit);
|
|
576
|
+
|
|
577
|
+
const sessions = [];
|
|
578
|
+
const allOffendingTurns = [];
|
|
579
|
+
let totalSludgeTokens = 0;
|
|
580
|
+
let totalSludgeUSD = 0;
|
|
581
|
+
let totalColdCacheSpikes = 0;
|
|
582
|
+
let totalThrashLoops = 0;
|
|
583
|
+
let totalOversizedReads = 0;
|
|
584
|
+
let totalOversizedOutputs = 0;
|
|
585
|
+
|
|
586
|
+
for (const item of selected) {
|
|
587
|
+
try {
|
|
588
|
+
const scored =
|
|
589
|
+
item.platform === 'claude'
|
|
590
|
+
? scoreClaudeTranscriptFile(item)
|
|
591
|
+
: scoreAntigravityTranscriptFile(item);
|
|
592
|
+
sessions.push(scored);
|
|
593
|
+
totalSludgeTokens += scored.sludgeTaxTokens;
|
|
594
|
+
totalSludgeUSD += scored.sludgeTaxUSD;
|
|
595
|
+
totalColdCacheSpikes += scored.coldCacheSpikes;
|
|
596
|
+
totalThrashLoops += scored.thrashLoops;
|
|
597
|
+
totalOversizedReads += scored.oversizedReads;
|
|
598
|
+
totalOversizedOutputs += scored.oversizedOutputs;
|
|
599
|
+
allOffendingTurns.push(...scored.offendingTurns);
|
|
600
|
+
} catch {
|
|
601
|
+
// Skip unreadable transcript cleanly
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
allOffendingTurns.sort((a, b) => b.wastedUSD - a.wastedUSD || b.wastedTokens - a.wastedTokens);
|
|
606
|
+
const topOffendingTurns = allOffendingTurns.slice(0, 3);
|
|
607
|
+
|
|
608
|
+
const ledger = getSavedQuotaReceiptSummary({
|
|
609
|
+
cwd: baseCwd,
|
|
610
|
+
homeDir: home
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
return {
|
|
614
|
+
ok: true,
|
|
615
|
+
platform,
|
|
616
|
+
days,
|
|
617
|
+
limit,
|
|
618
|
+
sessionsScanned: sessions.length,
|
|
619
|
+
sludgeTaxTokens: totalSludgeTokens,
|
|
620
|
+
sludgeTaxUSD: Number(totalSludgeUSD.toFixed(2)),
|
|
621
|
+
coldCacheSpikes: totalColdCacheSpikes,
|
|
622
|
+
thrashLoops: totalThrashLoops,
|
|
623
|
+
oversizedReads: totalOversizedReads,
|
|
624
|
+
oversizedOutputs: totalOversizedOutputs,
|
|
625
|
+
savedTokens30d: Number(ledger.workspace30dSavedTokens || 0),
|
|
626
|
+
savedUSD30d: Number(Number(ledger.workspace30dSavedUSD || 0).toFixed(2)),
|
|
627
|
+
topOffendingTurns,
|
|
628
|
+
sessions
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Formats a human-readable Quota Shield & Session ROI console report for `ctxtop audit`.
|
|
634
|
+
*
|
|
635
|
+
* @param {ReturnType<typeof runSessionAudit>} res
|
|
636
|
+
* @returns {string}
|
|
637
|
+
*/
|
|
638
|
+
export function formatSessionAuditReport(res) {
|
|
639
|
+
const lines = [
|
|
640
|
+
`[Highbeam Session ROI Audit] Window: ${res.days}d | Platform: ${res.platform} | Sessions: ${res.sessionsScanned}`,
|
|
641
|
+
` Sludge Tax Preventable : ${res.sludgeTaxTokens.toLocaleString('en-US')} tok ($${res.sludgeTaxUSD.toFixed(2)} USD)`,
|
|
642
|
+
` Cold-Cache Spikes (>5m): ${res.coldCacheSpikes} | 3x Thrash Loops: ${res.thrashLoops}`,
|
|
643
|
+
` Un-Sliced Large Reads : ${res.oversizedReads} | Oversized Tool Dumps: ${res.oversizedOutputs}`,
|
|
644
|
+
` Quota Shield Saved (30d): +${res.savedTokens30d.toLocaleString('en-US')} tok ($${res.savedUSD30d.toFixed(2)} USD)`
|
|
645
|
+
];
|
|
646
|
+
if (res.topOffendingTurns.length > 0) {
|
|
647
|
+
lines.push(' Top Offending Turns:');
|
|
648
|
+
res.topOffendingTurns.forEach((turn, idx) => {
|
|
649
|
+
lines.push(
|
|
650
|
+
` ${idx + 1}. [${turn.platform}/${turn.category}] ${turn.citation} -> ~$${turn.wastedUSD.toFixed(2)} (${turn.wastedTokens.toLocaleString('en-US')} tok)`
|
|
651
|
+
);
|
|
652
|
+
lines.push(` Fix: ${turn.fix}`);
|
|
653
|
+
});
|
|
654
|
+
} else {
|
|
655
|
+
lines.push(' Top Offending Turns: None detected in scanned window.');
|
|
656
|
+
}
|
|
657
|
+
return lines.join('\n');
|
|
658
|
+
}
|