@echomem/mcp 1.4.8 → 1.4.10

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.
Files changed (64) hide show
  1. package/README.md +23 -3
  2. package/assets/canonical-scorer/README.md +18 -0
  3. package/assets/canonical-scorer/analyze-10-problems.mjs +857 -0
  4. package/assets/canonical-scorer/build-session-waste-dashboard.mjs +1628 -0
  5. package/assets/canonical-scorer/golden_anchors.mjs +83 -0
  6. package/assets/canonical-scorer/optimizable_detail.mjs +633 -0
  7. package/assets/hud/github.svg +1 -0
  8. package/dist/city/README.md +9 -0
  9. package/dist/city/echo-ai-city-only.html +1067 -107
  10. package/dist/codex-session-files.js +283 -0
  11. package/dist/codex-sync.js +7 -2
  12. package/dist/context-analysis/canonical-golden.js +47 -0
  13. package/dist/context-analysis/claude-canonical-adapter.js +315 -0
  14. package/dist/context-analysis/claude-native-canonical.js +1216 -0
  15. package/dist/context-analysis/vendored-canonical.js +793 -0
  16. package/dist/context-analysis/workspace-report.js +1838 -0
  17. package/dist/context-metrics/calculate.js +43 -0
  18. package/dist/context-metrics/estimator.js +45 -0
  19. package/dist/context-metrics/ledger.js +507 -0
  20. package/dist/context-metrics/model-limits.js +26 -0
  21. package/dist/context-metrics/parse-claude.js +227 -0
  22. package/dist/context-metrics/parse-codex.js +276 -0
  23. package/dist/context-metrics/types.js +1 -0
  24. package/dist/forensics-10-problems.js +7 -6
  25. package/dist/forensics.js +863 -132
  26. package/dist/hud/adapters.js +77 -202
  27. package/dist/hud/cli.js +0 -0
  28. package/dist/hud/efficiency.js +447 -0
  29. package/dist/hud/electron-main.js +3 -2
  30. package/dist/hud/fs.js +14 -0
  31. package/dist/hud/metric.js +17 -102
  32. package/dist/hud/monitor.js +136 -28
  33. package/dist/hud/render.js +4 -3
  34. package/dist/hud/server.js +30 -0
  35. package/dist/hud/web.js +622 -353
  36. package/dist/index.js +7 -3
  37. package/dist/local-data-paths.js +87 -0
  38. package/dist/migrate.js +37 -29
  39. package/dist/report.js +101 -40
  40. package/dist/setup-page/client-core.js +475 -0
  41. package/dist/setup-page/client-extraction.js +550 -0
  42. package/dist/setup-page/client-lifecycle.js +116 -0
  43. package/dist/setup-page/client-report-audit.js +818 -0
  44. package/dist/setup-page/client-report-city.js +204 -0
  45. package/dist/setup-page/client-report.js +6 -0
  46. package/dist/setup-page/client.js +15 -0
  47. package/dist/setup-page/document.js +37 -0
  48. package/dist/setup-page/styles-city-report.js +880 -0
  49. package/dist/setup-page/styles-context-audit.js +470 -0
  50. package/dist/setup-page/styles-extraction.js +821 -0
  51. package/dist/setup-page/styles-foundation.js +231 -0
  52. package/dist/setup-page/styles.js +11 -0
  53. package/dist/setup-page.js +15 -2538
  54. package/dist/setup-preview.js +245 -0
  55. package/dist/setup.js +456 -44
  56. package/package.json +8 -7
  57. package/templates/echomem-recall.md +2 -2
  58. package/dist/city/10-problems-report.html +0 -649
  59. package/dist/city/_live.html +0 -37
  60. package/dist/city/_serve.mjs +0 -45
  61. package/dist/city/card-data.json +0 -15
  62. package/dist/city/city-data.json +0 -248
  63. package/dist/city/echo-ai-city-only.template.html +0 -1272
  64. package/dist/city/generate-echo-city-only.mjs +0 -112
@@ -0,0 +1,447 @@
1
+ import crypto from "node:crypto";
2
+ import { adapters } from "./adapters.js";
3
+ import { readJsonl } from "./fs.js";
4
+ import { formatTokens, shellRead } from "./metric.js";
5
+ const RULES = [
6
+ "repeated_file_read",
7
+ "repeated_git_output",
8
+ "repeated_search_output",
9
+ "compaction_rediscovery",
10
+ "aborted_or_limit_ended_turn",
11
+ ];
12
+ const SEARCH_BINS = new Set(["rg", "grep", "find"]);
13
+ export function runEfficiencyReport(flags = {}) {
14
+ const session = typeof flags.session === "string" ? flags.session : adapters.codex.findActive();
15
+ if (!session)
16
+ throw new Error("No active Codex session found.");
17
+ return analyzeCodexEfficiency(session);
18
+ }
19
+ export function analyzeCodexEfficiency(file) {
20
+ const turns = new Map();
21
+ const calls = new Map();
22
+ const readHist = new Map();
23
+ const editTurn = new Map();
24
+ const gitOutputs = new Map();
25
+ const searchOutputs = new Map();
26
+ const seenBeforeCompaction = new Set();
27
+ const chargedCalls = new Set();
28
+ const abortedTurns = new Map();
29
+ let turn = 0;
30
+ let lastCompactionTurn = -1;
31
+ for (const record of readJsonl(file)) {
32
+ const top = isRecord(record) ? record : {};
33
+ const payload = isRecord(top.payload) ? top.payload : {};
34
+ const payloadType = typeof payload.type === "string" ? payload.type : "";
35
+ if (payloadType === "task_started") {
36
+ turn += 1;
37
+ ensureTurn(turns, turn);
38
+ }
39
+ else if (payloadType === "token_count") {
40
+ const info = isRecord(payload.info) ? payload.info : {};
41
+ const last = isRecord(info.last_token_usage) ? info.last_token_usage : {};
42
+ const inputTokens = readNumber(last.input_tokens);
43
+ if (inputTokens > 0) {
44
+ const row = ensureTurn(turns, turn);
45
+ row.contextTokens = Math.max(row.contextTokens, inputTokens);
46
+ }
47
+ }
48
+ else if (payloadType === "function_call") {
49
+ const meta = callMeta(payload, turn);
50
+ if (meta)
51
+ calls.set(meta.id, meta);
52
+ }
53
+ else if (payloadType === "function_call_output") {
54
+ const callId = typeof payload.call_id === "string" ? payload.call_id : "";
55
+ const meta = calls.get(callId);
56
+ if (!meta)
57
+ continue;
58
+ const output = typeof payload.output === "string" ? payload.output : JSON.stringify(payload.output || "");
59
+ processOutput({
60
+ turns,
61
+ call: meta,
62
+ output,
63
+ readHist,
64
+ editTurn,
65
+ gitOutputs,
66
+ searchOutputs,
67
+ seenBeforeCompaction,
68
+ chargedCalls,
69
+ lastCompactionTurn,
70
+ });
71
+ }
72
+ else if (payloadType === "patch_apply_end") {
73
+ const changes = isRecord(payload.changes) ? payload.changes : {};
74
+ for (const changed of Object.keys(changes))
75
+ editTurn.set(changed, turn);
76
+ }
77
+ else if (payloadType === "context_compacted" || top.type === "compacted") {
78
+ lastCompactionTurn = turn;
79
+ seenBeforeCompaction.clear();
80
+ for (const filePath of readHist.keys())
81
+ seenBeforeCompaction.add(`read:${filePath}`);
82
+ for (const query of searchOutputs.keys())
83
+ seenBeforeCompaction.add(`search:${query}`);
84
+ for (const query of gitOutputs.keys())
85
+ seenBeforeCompaction.add(`git:${query}`);
86
+ }
87
+ else if (payloadType === "agent_message") {
88
+ const message = typeof payload.message === "string" ? payload.message : "";
89
+ if (isAbortOrLimitMessage(message))
90
+ abortedTurns.set(turn, abortLabel(message));
91
+ }
92
+ else if (payloadType === "message") {
93
+ const message = messageText(payload);
94
+ const phase = typeof payload.phase === "string" ? payload.phase : "";
95
+ if (phase === "final_answer" && isAbortOrLimitMessage(message))
96
+ abortedTurns.set(turn, abortLabel(message));
97
+ }
98
+ }
99
+ const rows = [...turns.values()].filter((row) => row.turn > 0).sort((a, b) => a.turn - b.turn);
100
+ for (const row of rows)
101
+ addAbortedTurnWaste(row, abortedTurns.get(row.turn));
102
+ finalizeRows(rows);
103
+ const totals = sumTotals(rows);
104
+ return {
105
+ client: "codex",
106
+ sourcePath: file,
107
+ basis: "deterministic-resident-lower-bound-v1",
108
+ turns: rows,
109
+ totals,
110
+ generatedAt: new Date().toISOString(),
111
+ };
112
+ }
113
+ export function renderEfficiencyReportText(report) {
114
+ const noisy = report.turns
115
+ .filter((turn) => turn.estimatedWasteTokens > 0)
116
+ .sort((a, b) => b.estimatedWasteTokens - a.estimatedWasteTokens)
117
+ .slice(0, 8);
118
+ const lines = [
119
+ `EchoMem deterministic efficiency — ${report.client}`,
120
+ "",
121
+ `Turns: ${report.turns.length}`,
122
+ `Context tokens: ${formatTokens(report.totals.contextTokens)}`,
123
+ `Resident dirty context: ≥ ${formatTokens(report.totals.residentDirtyTokens)}`,
124
+ `New waste created: ≥ ${formatTokens(report.totals.newWasteTokens)}`,
125
+ `Efficiency: ${pct(report.totals.efficiencyEstimate)} clean (${pct(report.totals.noiseEstimate)} noise)`,
126
+ "",
127
+ "Resident dirty by rule",
128
+ ];
129
+ for (const rule of RULES)
130
+ lines.push(` ${rule.padEnd(28)} ${formatTokens(report.totals.residentDirtyTokensByRule[rule])}`);
131
+ lines.push("");
132
+ lines.push("New waste by rule");
133
+ for (const rule of RULES)
134
+ lines.push(` ${rule.padEnd(28)} ${formatTokens(report.totals.newWasteTokensByRule[rule])}`);
135
+ lines.push("");
136
+ lines.push("Noisiest turns");
137
+ if (!noisy.length)
138
+ lines.push(" none detected");
139
+ for (const row of noisy) {
140
+ const topRule = topRuleName(row.wasteTokensByRule);
141
+ lines.push(` T${String(row.turn).padStart(3, "0")} waste ≥ ${formatTokens(row.estimatedWasteTokens).padStart(5)} / ${formatTokens(row.contextTokens).padStart(5)} efficiency ${pct(row.efficiencyEstimate)} ${topRule}`);
142
+ }
143
+ lines.push("");
144
+ lines.push("Honesty: resident dirty context is carried forward as a lower-bound estimate; provider eviction is not observable.");
145
+ return lines.join("\n");
146
+ }
147
+ function processOutput(params) {
148
+ const tokens = outputTokens(params.output);
149
+ const hash = hashText(params.output);
150
+ const call = params.call;
151
+ if (!tokens)
152
+ return;
153
+ if (call.kind === "read" && call.file && call.start !== undefined && call.end !== undefined) {
154
+ const previous = params.readHist.get(call.file) || [];
155
+ const lastEdit = params.editTurn.get(call.file) ?? -1;
156
+ const redundant = previous.some((read) => read.turn >= lastEdit && call.start <= read.end && call.end >= read.start);
157
+ if (redundant) {
158
+ addWaste(params.turns, params.chargedCalls, call, {
159
+ rule: "repeated_file_read",
160
+ turn: call.turn,
161
+ tokens,
162
+ sourceTurn: previous.find((read) => read.turn >= lastEdit && call.start <= read.end && call.end >= read.start)?.turn,
163
+ label: "overlapping file range read again before edit",
164
+ tool: call.name,
165
+ command: call.command,
166
+ file: call.file,
167
+ });
168
+ }
169
+ previous.push({ start: call.start, end: call.end, turn: call.turn });
170
+ params.readHist.set(call.file, previous);
171
+ }
172
+ if (call.kind === "git") {
173
+ const previous = params.gitOutputs.get(call.normalized);
174
+ if (previous && previous.hash === hash) {
175
+ addWaste(params.turns, params.chargedCalls, call, {
176
+ rule: "repeated_git_output",
177
+ turn: call.turn,
178
+ tokens,
179
+ sourceTurn: previous.turn,
180
+ label: "same git command produced identical output",
181
+ tool: call.name,
182
+ command: call.command,
183
+ carriesForward: true,
184
+ });
185
+ }
186
+ params.gitOutputs.set(call.normalized, { hash, turn: call.turn });
187
+ }
188
+ if (call.kind === "search") {
189
+ const previous = params.searchOutputs.get(call.normalized);
190
+ if (previous && previous.hash === hash) {
191
+ addWaste(params.turns, params.chargedCalls, call, {
192
+ rule: "repeated_search_output",
193
+ turn: call.turn,
194
+ tokens,
195
+ sourceTurn: previous.turn,
196
+ label: "same search produced identical output",
197
+ tool: call.name,
198
+ command: call.command,
199
+ carriesForward: true,
200
+ });
201
+ }
202
+ params.searchOutputs.set(call.normalized, { hash, turn: call.turn });
203
+ }
204
+ if (params.lastCompactionTurn > 0 && call.turn > params.lastCompactionTurn && call.turn <= params.lastCompactionTurn + 3) {
205
+ const key = rediscoveryKey(call);
206
+ if (key && params.seenBeforeCompaction.has(key)) {
207
+ addWaste(params.turns, params.chargedCalls, call, {
208
+ rule: "compaction_rediscovery",
209
+ turn: call.turn,
210
+ tokens,
211
+ sourceTurn: params.lastCompactionTurn,
212
+ label: "post-compaction rediscovery output",
213
+ tool: call.name,
214
+ command: call.command,
215
+ file: call.file,
216
+ carriesForward: true,
217
+ });
218
+ }
219
+ }
220
+ }
221
+ function callMeta(payload, turn) {
222
+ const id = typeof payload.call_id === "string" ? payload.call_id : "";
223
+ const name = typeof payload.name === "string" ? payload.name : "";
224
+ if (!id || !name)
225
+ return null;
226
+ const args = parseArguments(payload.arguments);
227
+ const command = isRecord(args) && typeof args.cmd === "string" ? args.cmd : "";
228
+ const read = command && (name === "exec_command" || name === "shell") ? shellRead(command) : null;
229
+ if (read) {
230
+ return {
231
+ id,
232
+ turn,
233
+ name,
234
+ command,
235
+ kind: "read",
236
+ normalized: `read:${read.file}:${read.start}:${read.end}`,
237
+ file: read.file,
238
+ start: read.start,
239
+ end: read.end,
240
+ };
241
+ }
242
+ const normalized = normalizeCommand(command);
243
+ if (isGitCommand(command))
244
+ return { id, turn, name, command, kind: "git", normalized };
245
+ if (isSearchCommand(command))
246
+ return { id, turn, name, command, kind: "search", normalized };
247
+ return { id, turn, name, command, kind: "other", normalized };
248
+ }
249
+ function addWaste(turns, chargedCalls, call, event) {
250
+ if (chargedCalls.has(call.id))
251
+ return;
252
+ chargedCalls.add(call.id);
253
+ const row = ensureTurn(turns, event.turn);
254
+ row.events.push(event);
255
+ row.newWasteTokensByRule[event.rule] += event.tokens;
256
+ row.newWasteTokens += event.tokens;
257
+ }
258
+ function addAbortedTurnWaste(row, label) {
259
+ if (!label || row.contextTokens <= 0)
260
+ return;
261
+ const event = {
262
+ rule: "aborted_or_limit_ended_turn",
263
+ turn: row.turn,
264
+ tokens: row.contextTokens,
265
+ label,
266
+ carriesForward: false,
267
+ };
268
+ row.events.push(event);
269
+ row.newWasteTokensByRule[event.rule] += event.tokens;
270
+ row.newWasteTokens += event.tokens;
271
+ }
272
+ function ensureTurn(turns, turn) {
273
+ const safeTurn = Math.max(0, turn);
274
+ const existing = turns.get(safeTurn);
275
+ if (existing)
276
+ return existing;
277
+ const row = {
278
+ turn: safeTurn,
279
+ contextTokens: 0,
280
+ newWasteTokens: 0,
281
+ residentDirtyTokens: 0,
282
+ estimatedWasteTokens: 0,
283
+ efficiencyEstimate: null,
284
+ noiseEstimate: null,
285
+ newWasteTokensByRule: zeroRules(),
286
+ residentDirtyTokensByRule: zeroRules(),
287
+ wasteTokensByRule: zeroRules(),
288
+ events: [],
289
+ };
290
+ turns.set(safeTurn, row);
291
+ return row;
292
+ }
293
+ function finalizeRows(rows) {
294
+ const residentByRule = zeroRules();
295
+ for (const row of rows) {
296
+ const transientByRule = zeroRules();
297
+ for (const event of row.events) {
298
+ if (event.carriesForward === false)
299
+ transientByRule[event.rule] += event.tokens;
300
+ }
301
+ const combinedByRule = zeroRules();
302
+ for (const rule of RULES)
303
+ combinedByRule[rule] = residentByRule[rule] + transientByRule[rule];
304
+ const dirtyTokens = sumRules(combinedByRule);
305
+ row.residentDirtyTokensByRule = combinedByRule;
306
+ row.wasteTokensByRule = combinedByRule;
307
+ row.residentDirtyTokens = row.contextTokens > 0 ? Math.min(row.contextTokens, dirtyTokens) : dirtyTokens;
308
+ row.estimatedWasteTokens = row.residentDirtyTokens;
309
+ finalizeTurn(row);
310
+ for (const event of row.events) {
311
+ if (event.carriesForward === true)
312
+ residentByRule[event.rule] += event.tokens;
313
+ }
314
+ }
315
+ }
316
+ function finalizeTurn(row) {
317
+ row.noiseEstimate = row.contextTokens > 0 ? round(row.residentDirtyTokens / row.contextTokens) : null;
318
+ row.efficiencyEstimate = row.noiseEstimate === null ? null : round(1 - row.noiseEstimate);
319
+ }
320
+ function sumTotals(rows) {
321
+ const totals = {
322
+ contextTokens: 0,
323
+ newWasteTokens: 0,
324
+ residentDirtyTokens: 0,
325
+ estimatedWasteTokens: 0,
326
+ efficiencyEstimate: null,
327
+ noiseEstimate: null,
328
+ newWasteTokensByRule: zeroRules(),
329
+ residentDirtyTokensByRule: zeroRules(),
330
+ wasteTokensByRule: zeroRules(),
331
+ };
332
+ for (const row of rows) {
333
+ totals.contextTokens += row.contextTokens;
334
+ totals.newWasteTokens += row.newWasteTokens;
335
+ totals.residentDirtyTokens += row.residentDirtyTokens;
336
+ totals.estimatedWasteTokens += row.estimatedWasteTokens;
337
+ for (const rule of RULES) {
338
+ totals.newWasteTokensByRule[rule] += row.newWasteTokensByRule[rule];
339
+ totals.residentDirtyTokensByRule[rule] += row.residentDirtyTokensByRule[rule];
340
+ totals.wasteTokensByRule[rule] += row.wasteTokensByRule[rule];
341
+ }
342
+ }
343
+ totals.noiseEstimate = totals.contextTokens > 0 ? round(totals.residentDirtyTokens / totals.contextTokens) : null;
344
+ totals.efficiencyEstimate = totals.noiseEstimate === null ? null : round(1 - totals.noiseEstimate);
345
+ return totals;
346
+ }
347
+ function zeroRules() {
348
+ return {
349
+ repeated_file_read: 0,
350
+ repeated_git_output: 0,
351
+ repeated_search_output: 0,
352
+ compaction_rediscovery: 0,
353
+ aborted_or_limit_ended_turn: 0,
354
+ };
355
+ }
356
+ function sumRules(map) {
357
+ return RULES.reduce((sum, rule) => sum + map[rule], 0);
358
+ }
359
+ function outputTokens(output) {
360
+ return Math.max(0, Math.round(Buffer.byteLength(output || "", "utf8") / 4));
361
+ }
362
+ function hashText(output) {
363
+ return crypto.createHash("sha256").update(output || "").digest("hex");
364
+ }
365
+ function normalizeCommand(command) {
366
+ return String(command || "").trim().replace(/\s+/g, " ");
367
+ }
368
+ function isGitCommand(command) {
369
+ return /^git(?:\s|$)/.test(normalizeCommand(command));
370
+ }
371
+ function isSearchCommand(command) {
372
+ const first = normalizeCommand(command).split(/[|;&]/)[0].trim();
373
+ const bin = first.split(/\s+/)[0]?.split("/").pop() || "";
374
+ return SEARCH_BINS.has(bin);
375
+ }
376
+ function rediscoveryKey(call) {
377
+ if (call.kind === "read" && call.file)
378
+ return `read:${call.file}`;
379
+ if (call.kind === "search")
380
+ return `search:${call.normalized}`;
381
+ if (call.kind === "git")
382
+ return `git:${call.normalized}`;
383
+ return null;
384
+ }
385
+ function isAbortOrLimitMessage(message) {
386
+ const text = message.trim();
387
+ if (!text || text.length > 320)
388
+ return false;
389
+ return /^(?:you(?:'ve| have) hit your session limit|session limit\b|rate limit(?: reached)?\b|limit reached\b|request timed out\b|timed out\b|timeout\b|cancelled\b|canceled\b|aborted\b|interrupted\b)/i.test(text);
390
+ }
391
+ function abortLabel(message) {
392
+ if (/limit/i.test(message))
393
+ return "turn ended by limit message before useful completion";
394
+ if (/timed?\s*out|timeout/i.test(message))
395
+ return "turn ended by timeout before useful completion";
396
+ if (/cancelled|canceled|aborted|interrupted/i.test(message))
397
+ return "turn ended by abort/cancel before useful completion";
398
+ return "turn ended without useful completion";
399
+ }
400
+ function messageText(payload) {
401
+ const content = payload.content;
402
+ if (typeof content === "string")
403
+ return content;
404
+ if (!Array.isArray(content))
405
+ return "";
406
+ return content
407
+ .map((block) => {
408
+ if (typeof block === "string")
409
+ return block;
410
+ if (!isRecord(block))
411
+ return "";
412
+ return typeof block.text === "string" ? block.text : "";
413
+ })
414
+ .join("\n");
415
+ }
416
+ function topRuleName(map) {
417
+ const [name, value] = Object.entries(map).sort((a, b) => b[1] - a[1])[0] || ["", 0];
418
+ return value ? name : "";
419
+ }
420
+ function pct(value) {
421
+ if (value === null)
422
+ return "n/a";
423
+ return `${Math.round(value * 1000) / 10}%`;
424
+ }
425
+ function parseArguments(value) {
426
+ if (!value)
427
+ return {};
428
+ if (isRecord(value))
429
+ return value;
430
+ if (typeof value !== "string")
431
+ return {};
432
+ try {
433
+ return JSON.parse(value);
434
+ }
435
+ catch {
436
+ return {};
437
+ }
438
+ }
439
+ function readNumber(value) {
440
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
441
+ }
442
+ function isRecord(value) {
443
+ return typeof value === "object" && value !== null && !Array.isArray(value);
444
+ }
445
+ function round(value) {
446
+ return Math.round(value * 10000) / 10000;
447
+ }
@@ -12,8 +12,9 @@ const port = typeof flags.port === "string" ? Number(flags.port) || 17377 : 1737
12
12
  const COLLAPSED_WIDTH = 360;
13
13
  const COLLAPSED_HEIGHT = 112;
14
14
  const EXPANDED_HEIGHT = 460;
15
- // Mini mode: a small always-on pill (status dot + tokens) for users who find the full bubble too big.
16
- const MINI_WIDTH = 190;
15
+ // Mini mode: a slim always-on pill (dot · score · agent icon · session · repo) wide enough to
16
+ // read the full session name.
17
+ const MINI_WIDTH = 300;
17
18
  const MINI_HEIGHT = 48;
18
19
  const AGENT_VISIBILITY_POLL_MS = 900;
19
20
  let miniPref = false;
package/dist/hud/fs.js CHANGED
@@ -61,3 +61,17 @@ export function statSignature(file) {
61
61
  return "";
62
62
  }
63
63
  }
64
+ // The Claude Code active source can be the echo-ctx statusline cache (<sessionId>.json). The real
65
+ // transcript with usage/tool data lives under ~/.claude/projects — resolve it by session id.
66
+ const transcriptCache = new Map();
67
+ export function resolveClaudeTranscript(cacheFile) {
68
+ if (!cacheFile.endsWith(".json") || !cacheFile.includes(`${path.sep}echo-ctx${path.sep}`))
69
+ return null;
70
+ const cached = transcriptCache.get(cacheFile);
71
+ if (cached !== undefined)
72
+ return cached;
73
+ const sessionId = path.basename(cacheFile, ".json");
74
+ const transcript = newestFile(walkFiles(homePath(".claude", "projects"), (f) => path.basename(f) === `${sessionId}.jsonl`));
75
+ transcriptCache.set(cacheFile, transcript);
76
+ return transcript;
77
+ }
@@ -1,3 +1,4 @@
1
+ import { calculateContextMetrics } from "../context-metrics/calculate.js";
1
2
  export const BUCKETS = {
2
3
  rangeRedundant: "range_redundant",
3
4
  staleRead: "stale_read",
@@ -5,19 +6,11 @@ export const BUCKETS = {
5
6
  staleToolOutput: "stale_tool_output",
6
7
  compactionRecoverable: "compaction_recoverable",
7
8
  };
8
- const SHELL_READ_BINS = new Set(["cat", "head", "tail", "sed", "nl", "less", "more", "bat"]);
9
- const EXT = /\.[A-Za-z0-9]{1,8}$/;
10
- // A re-read counts as redundant only if a prior read substantially re-covers THIS read. A shared
11
- // boundary line (sequential paging, e.g. sed 1,260p then 260,620p) is not a re-read; requiring ≥50%
12
- // of the new range to be already-seen keeps paging out while still catching genuine sub-range re-reads.
13
- const REDUNDANT_OVERLAP_FRACTION = 0.5;
14
9
  export function newMetricState() {
15
10
  return {
16
11
  turn: 0,
17
12
  reads: 0,
18
13
  redundantCount: 0,
19
- readHist: new Map(),
20
- editTurn: new Map(),
21
14
  buckets: {
22
15
  [BUCKETS.rangeRedundant]: { tokens: 0, count: 0 },
23
16
  [BUCKETS.staleRead]: { tokens: 0, count: 0 },
@@ -28,42 +21,6 @@ export function newMetricState() {
28
21
  tools: {},
29
22
  };
30
23
  }
31
- export function bumpTurn(state) {
32
- state.turn += 1;
33
- }
34
- export function recordTool(state, name) {
35
- if (!name)
36
- return;
37
- state.tools[name] = (state.tools[name] || 0) + 1;
38
- }
39
- export function recordEdit(state, file) {
40
- if (!file)
41
- return;
42
- state.editTurn.set(file, state.turn);
43
- }
44
- export function recordRead(state, file, start = 1, end = 1e9) {
45
- if (!file)
46
- return;
47
- const safeStart = Number(start) || 1;
48
- const safeEnd = Number(end) || 1e9;
49
- const tokens = estimateReadTokens(safeStart, safeEnd);
50
- const previous = state.readHist.get(file) || [];
51
- const lastEdit = state.editTurn.get(file) ?? -1;
52
- const newLines = Math.max(1, safeEnd - safeStart + 1);
53
- const redundant = previous.some((read) => {
54
- if (read.turn < lastEdit)
55
- return false; // an edit since this read invalidated it — re-read is fresh
56
- const overlap = Math.min(safeEnd, read.end) - Math.max(safeStart, read.start) + 1;
57
- return overlap > 0 && overlap / newLines >= REDUNDANT_OVERLAP_FRACTION;
58
- });
59
- state.reads += 1;
60
- if (redundant) {
61
- addBucket(state, BUCKETS.rangeRedundant, tokens, 1);
62
- state.redundantCount += 1;
63
- }
64
- previous.push({ start: safeStart, end: safeEnd, turn: state.turn, tokens });
65
- state.readHist.set(file, previous);
66
- }
67
24
  export function addBucket(state, bucket, tokens, count = 1) {
68
25
  state.buckets[bucket].tokens += Math.max(0, Math.round(tokens));
69
26
  state.buckets[bucket].count += Math.max(0, count);
@@ -72,16 +29,24 @@ export function scoreMetric(params) {
72
29
  const ct = Number(params.ctTokens) || 0;
73
30
  const modelWindow = Number(params.modelContextWindow) || 0;
74
31
  const pollutionTok = Object.values(params.state.buckets).reduce((sum, bucket) => sum + bucket.tokens, 0);
75
- const pollution = ct > 0 ? Math.min(0.95, pollutionTok / ct) : 0;
32
+ const contextMetrics = calculateContextMetrics({
33
+ latestInputTokens: ct,
34
+ modelContextLimitTokens: modelWindow || null,
35
+ currentResidentWasteTokens: pollutionTok,
36
+ });
37
+ const pollution = Math.min(0.95, contextMetrics.noisePct);
76
38
  const pollutionPct = Math.round(pollution * 100);
77
- const saturationPct = modelWindow > 0 ? Math.round((ct / modelWindow) * 100) : null;
39
+ const saturationPct = contextMetrics.contextFullnessPct !== undefined
40
+ ? Math.round(contextMetrics.contextFullnessPct * 100)
41
+ : null;
78
42
  return {
79
43
  client: params.client,
80
44
  sourcePath: params.sourcePath,
81
45
  turn: params.state.turn,
82
46
  reads: params.state.reads,
83
47
  redundantCount: params.state.redundantCount,
84
- usefulPct: Math.round((1 - pollution) * 100),
48
+ usefulPct: Math.round(Math.max(0, Math.min(1, contextMetrics.usefulPct)) * 100),
49
+ healthScorePct: contextMetrics.healthScorePct,
85
50
  pollutionPct,
86
51
  pollutionTok,
87
52
  ctTokens: ct,
@@ -96,34 +61,6 @@ export function scoreMetric(params) {
96
61
  tools: { ...params.state.tools },
97
62
  };
98
63
  }
99
- export function shellRead(cmd) {
100
- const text = String(cmd || "").trim();
101
- if (!text)
102
- return null;
103
- const firstPipeline = text.split(/[|;&]/)[0].trim();
104
- const tokens = firstPipeline.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) || [];
105
- if (!tokens.length)
106
- return null;
107
- let bin = stripQuotes(tokens[0] || "").split("/").pop() || "";
108
- if (bin === "sudo")
109
- bin = stripQuotes(tokens[1] || "").split("/").pop() || "";
110
- if (!SHELL_READ_BINS.has(bin))
111
- return null;
112
- let file = "";
113
- for (let i = tokens.length - 1; i >= 1; i -= 1) {
114
- const token = stripQuotes(tokens[i] || "");
115
- if (!token || token.startsWith("-") || /^\d+(,\d+)?p?$/.test(token))
116
- continue;
117
- if (token.includes("/") || EXT.test(token)) {
118
- file = token;
119
- break;
120
- }
121
- }
122
- if (!file)
123
- return null;
124
- const [start, end] = rangeFromCmd(text);
125
- return { file, start, end };
126
- }
127
64
  export function formatTokens(tokens) {
128
65
  const n = Number(tokens) || 0;
129
66
  if (n >= 1_000_000)
@@ -134,38 +71,16 @@ export function formatTokens(tokens) {
134
71
  }
135
72
  export function formatGlance(score) {
136
73
  const dot = score.color === "amber" ? "◑" : "●";
137
- return `${dot} ${score.usefulPct}% clean · ${formatTokens(score.ctTokens)}`;
138
- }
139
- function estimateReadTokens(start, end) {
140
- const boundedEnd = Math.min(end, start + 4000);
141
- const lines = Math.max(1, boundedEnd - start + 1);
142
- return Math.min(8000, Math.max(40, lines * 12));
74
+ return `${dot} ${score.healthScorePct}% score · ${formatTokens(score.ctTokens)}`;
143
75
  }
144
- function qualityColor(pollutionPct, saturationPct) {
145
- if (pollutionPct >= 35 ||
146
- (saturationPct !== null && saturationPct >= 95) ||
147
- (saturationPct !== null && saturationPct >= 85 && pollutionPct >= 20)) {
76
+ export function qualityColor(pollutionPct, saturationPct) {
77
+ const usefulPct = 100 - Math.max(0, Math.min(100, pollutionPct));
78
+ if (usefulPct <= 40)
148
79
  return "red";
149
- }
150
- if (pollutionPct >= 18 || (saturationPct !== null && saturationPct >= 75))
80
+ if (usefulPct <= 60)
151
81
  return "amber";
152
82
  return "green";
153
83
  }
154
- function rangeFromCmd(cmd) {
155
- const sed = cmd.match(/\bsed\s+(?:[^\n;|&]*?\s)?-n\s*['"]?\s*(\d+)\s*,\s*(\d+)\s*p/);
156
- if (sed)
157
- return [Number(sed[1]), Number(sed[2])];
158
- const nlSed = cmd.match(/\bnl\b[\s\S]*?\|\s*sed\s+-n\s*['"]?\s*(\d+)\s*,\s*(\d+)\s*p/);
159
- if (nlSed)
160
- return [Number(nlSed[1]), Number(nlSed[2])];
161
- const head = cmd.match(/\bhead\s+(?:-n\s*)?(\d+)\b/);
162
- if (head)
163
- return [1, Number(head[1])];
164
- return [1, 1e9];
165
- }
166
- function stripQuotes(value) {
167
- return value.replace(/^['"]|['"]$/g, "");
168
- }
169
84
  function cloneBuckets(buckets) {
170
85
  return Object.fromEntries(Object.entries(buckets).map(([name, value]) => [name, { ...value }]));
171
86
  }