chompyy 0.6.0 → 0.7.1

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.
@@ -0,0 +1,233 @@
1
+ // codeburn's own waste checks (src/vendor/codeburn/src/optimize.ts, `codeburn optimize`): about 25 detectors for
2
+ // re-reads, build-folder reads, low cache hit, bloated context, the long-context price, noisy shell output, pasted
3
+ // context repeated in every chat, unused MCP servers and more. We run them as they are and translate each finding
4
+ // into Chompyy's shape, so `chompyy detect` shows ours and theirs in one ranked list. No parser of our own.
5
+ import { loadAgents, prepareCodeburn } from './agentusage.js';
6
+ import { findingBasis, findingClass, scanAndDetect } from './vendor/codeburn/src/optimize.js';
7
+ import { parseAllSessions } from './vendor/codeburn/src/parser.js';
8
+ /** Our rulebook rule (research/detective/RULEBOOK.md) for each codeburn finding id; `CB` when the rulebook has none. */
9
+ export const RULE_FOR = {
10
+ 'redundant-rereads': 'R1',
11
+ 'build-folder-reads': 'R2',
12
+ 'read-edit-ratio': 'R1',
13
+ 'warmup-heavy': 'S1',
14
+ 'unused-mcp': 'S4',
15
+ 'mcp-low-coverage': 'S4',
16
+ 'mcp-project-scope': 'S4',
17
+ 'mcp-deferral-off': 'S4',
18
+ 'mcp-alwaysload-hygiene': 'S4',
19
+ 'mcp-defer-threshold': 'S4',
20
+ 'retry-heavy-capabilities': 'L1',
21
+ 'low-worth-sessions': 'X3',
22
+ 'context-heavy-sessions': 'X3',
23
+ 'cost-outliers': 'X3',
24
+ 'low-cache-hit-sessions': 'C4',
25
+ 'one-hour-cache-unused': 'C4',
26
+ 'claude-md-too-long': 'S2',
27
+ 'bash-output-cap': 'O1',
28
+ 'unused-agents': 'S3',
29
+ 'unused-skills': 'S3',
30
+ 'unused-commands': 'S3',
31
+ 'recurring-context': 'L4',
32
+ 'fast-mode-background': 'X2',
33
+ 'long-context-premium': 'X2',
34
+ 'idle-compaction-blocked': 'X1',
35
+ };
36
+ /**
37
+ * Checks we already run ourselves, with our own measured numbers: skip codeburn's version so a waste isn't listed
38
+ * twice. (CLAUDE.md length S2, unused skills S3, unused MCP S4.)
39
+ */
40
+ const OURS = new Set(['claude-md-too-long', 'unused-skills', 'unused-mcp']);
41
+ const LANE = { fix: 'settings', nudge: 'habit', keep: 'habit' };
42
+ /** One codeburn finding in Chompyy's shape. The first sentence of codeburn's explanation is what it saw. */
43
+ export function toFinding(w) {
44
+ const cls = findingClass(w);
45
+ return {
46
+ rule: RULE_FOR[w.id] ?? 'CB',
47
+ title: w.title,
48
+ say: w.explanation,
49
+ basis: findingBasis(w),
50
+ ...(cls === 'keep' ? { notProve: 'The cost may be worth it: the context or speed may be needed.', worthKnowing: true } : {}),
51
+ fix: w.fix.label.replace(/[:.\s]+$/, '') + (w.fix.type === 'paste' || w.fix.type === 'command' ? `: ${w.fix.text.split('\n')[0]}` : ''),
52
+ lane: LANE[cls],
53
+ tokens: Math.round(w.tokensSaved),
54
+ source: 'codeburn',
55
+ codeburnId: w.id,
56
+ };
57
+ }
58
+ /** Each session's calls in order, subagent transcripts left out (their errors are the subagent's business). */
59
+ function sessionCalls(projects) {
60
+ const out = [];
61
+ for (const p of projects) {
62
+ for (const s of p.sessions) {
63
+ if (s.isSidechain)
64
+ continue;
65
+ const calls = s.turns.flatMap((t) => t.assistantCalls).sort((a, b) => a.timestamp.localeCompare(b.timestamp));
66
+ if (calls.length)
67
+ out.push(calls);
68
+ }
69
+ }
70
+ return out;
71
+ }
72
+ /** New tokens a call sent: fresh input, cache writes and output (cache reads would have been read anyway). */
73
+ const fresh = (c) => c.usage.inputTokens + c.usage.cacheCreationInputTokens + c.usage.outputTokens;
74
+ /** The context the call carried. */
75
+ const context = (c) => c.usage.inputTokens + c.usage.cacheReadInputTokens + c.usage.cacheCreationInputTokens;
76
+ const TOOL_TIP = {
77
+ Edit: 'Read a file before editing it: most failed edits are stale or whitespace-mismatched old text.',
78
+ Write: 'Read a file before overwriting it.',
79
+ NotebookEdit: 'Read the notebook first to get the cell ids right.',
80
+ };
81
+ /** L1 · Tool errors: the request after a failed tool call is the cost of recovering from it, by tool. */
82
+ export function ruleToolErrors(projects) {
83
+ const by = new Map();
84
+ const shell = new Map();
85
+ for (const calls of sessionCalls(projects)) {
86
+ for (let i = 0; i < calls.length - 1; i++) {
87
+ const c = calls[i];
88
+ if (!c.toolErrors || !c.tools.length)
89
+ continue;
90
+ const share = fresh(calls[i + 1]) / c.tools.length;
91
+ for (const cmd of c.bashCommands ?? []) {
92
+ const word = cmd.trim().split(/\s+/)[0];
93
+ if (word)
94
+ shell.set(word, (shell.get(word) ?? 0) + 1);
95
+ }
96
+ for (const t of c.tools) {
97
+ const e = by.get(t) ?? { tokens: 0, fails: 0 };
98
+ e.tokens += share;
99
+ e.fails += 1;
100
+ by.set(t, e);
101
+ }
102
+ }
103
+ }
104
+ const ranked = [...by].filter(([, e]) => e.fails >= 5).sort((a, b) => b[1].tokens - a[1].tokens);
105
+ if (!ranked.length)
106
+ return [];
107
+ const total = ranked.reduce((a, [, e]) => a + e.tokens, 0);
108
+ const [top, e] = ranked[0];
109
+ return [{
110
+ rule: 'L1',
111
+ key: 'L1:errors',
112
+ title: 'Recovering from failed tool calls',
113
+ say: `${ranked.reduce((a, [, x]) => a + x.fails, 0)} failed tool calls; the request after each one sent ~${Math.round(total / 1000)}k new tokens in all. ${top} failed most (${e.fails} times).`,
114
+ basis: 'estimated',
115
+ notProve: 'Some failures are cheap tries that find the answer faster.',
116
+ fix: top === 'Bash' && shell.size
117
+ ? `Look at why \`${[...shell].sort((a, b) => b[1] - a[1])[0][0]}\` fails most (a blocked path, a missing tool, a wrong folder) and put the fix in CLAUDE.md.`
118
+ : TOOL_TIP[top] ?? `Look at what ${top} gets wrong in the failing calls: most failures are in the input.`,
119
+ lane: 'habit',
120
+ tokens: Math.round(total),
121
+ tokensHow: 'the request after each failure',
122
+ }];
123
+ }
124
+ const BUCKETS = [[0, 50_000, '50k'], [50_000, 100_000, '100k'], [100_000, 150_000, '150k'], [150_000, 200_000, '200k'], [200_000, Infinity, '200k+']];
125
+ /** X1 · Context decay: tool calls start failing more once a conversation passes a size. Shows the size, not tokens. */
126
+ export function ruleContextDecay(projects) {
127
+ const total = BUCKETS.map(() => 0);
128
+ const failed = BUCKETS.map(() => 0);
129
+ for (const calls of sessionCalls(projects)) {
130
+ for (const c of calls) {
131
+ if (!c.tools.length)
132
+ continue;
133
+ const i = BUCKETS.findIndex(([lo, hi]) => context(c) >= lo && context(c) < hi);
134
+ total[i] += 1;
135
+ if (c.toolErrors)
136
+ failed[i] += 1;
137
+ }
138
+ }
139
+ const ok = total.map((n, i) => (n >= 30 ? 1 - failed[i] / n : null));
140
+ let best = -1;
141
+ for (let i = 0; i < ok.length; i++) {
142
+ const r = ok[i];
143
+ if (r == null)
144
+ continue;
145
+ if (best >= 0 && ok[best] - r > 0.15) {
146
+ const past = i === 0 ? '0' : BUCKETS[i - 1][2];
147
+ return [{
148
+ rule: 'X1',
149
+ title: `Tool calls fail more past ${past} tokens of context`,
150
+ say: `Tool calls succeed ${Math.round(ok[best] * 100)}% of the time in smaller conversations, ${Math.round(r * 100)}% past ${past} (${total[i]} calls).`,
151
+ basis: 'measured',
152
+ notProve: 'Bigger conversations may also be harder tasks.',
153
+ fix: `Start fresh or /compact before a conversation passes ${past} tokens.`,
154
+ lane: 'habit',
155
+ }];
156
+ }
157
+ if (best < 0 || r > ok[best])
158
+ best = i;
159
+ }
160
+ return [];
161
+ }
162
+ const same = (a, b) => a.tool === b.tool && a.file === b.file && a.command === b.command;
163
+ /** L1 · Retry loops: the same tool with the same file or command three times in a row. */
164
+ export function ruleRetryLoops(projects) {
165
+ let loops = 0;
166
+ let sessions = 0;
167
+ let tokens = 0;
168
+ for (const calls of sessionCalls(projects)) {
169
+ const seq = calls.flatMap((c) => (c.toolSequence ?? []).flat().map((t) => ({ ...t, call: c })));
170
+ let found = 0;
171
+ for (let i = 2; i < seq.length; i++) {
172
+ const [a, b, c] = [seq[i - 2], seq[i - 1], seq[i]];
173
+ if (!(a.file || a.command) || !same(a, b) || !same(b, c))
174
+ continue;
175
+ found += 1;
176
+ tokens += fresh(c.call);
177
+ i += 2;
178
+ }
179
+ if (found) {
180
+ loops += found;
181
+ sessions += 1;
182
+ }
183
+ }
184
+ if (loops < 3)
185
+ return [];
186
+ return [{
187
+ rule: 'L1',
188
+ key: 'L1:loops',
189
+ title: 'The same call three times in a row',
190
+ say: `${loops} times in ${sessions} sessions Claude ran the same tool on the same file or command three times running.`,
191
+ basis: 'estimated',
192
+ notProve: 'Some repeats are on purpose: re-running a test after each fix.',
193
+ fix: 'When Claude repeats a call, stop it and say what changed or what to try instead.',
194
+ lane: 'habit',
195
+ tokens,
196
+ tokensHow: 'the third call of each run',
197
+ }];
198
+ }
199
+ /** codeburn's checks over the last `days` days of Claude Code sessions. Empty (never throws) when its reader fails. */
200
+ export async function wasteChecks(opts = {}) {
201
+ try {
202
+ await prepareCodeburn();
203
+ const end = opts.now ?? new Date();
204
+ const range = { start: new Date(end.getTime() - (opts.days ?? 30) * 86_400_000), end };
205
+ const provider = opts.provider ?? 'claude';
206
+ const projects = await parseAllSessions(range, provider);
207
+ const { findings } = await scanAndDetect(projects, range, provider);
208
+ return [
209
+ ...findings.filter((w) => !OURS.has(w.id) && w.tokensSaved > 0).map(toFinding),
210
+ ...ruleToolErrors(projects),
211
+ ...ruleContextDecay(projects),
212
+ ...ruleRetryLoops(projects),
213
+ ];
214
+ }
215
+ catch {
216
+ return [];
217
+ }
218
+ }
219
+ /**
220
+ * The same checks for the other agents you used this month (most tokens first, up to 3), from codeburn's readers.
221
+ * codeburn runs only the checks that mean something outside Claude Code (heavy sessions, cache hit, long-context
222
+ * price, …). Each finding is tagged with its agent and named in its title, e.g. "Codex: 2 context-heavy sessions".
223
+ */
224
+ export async function otherAgentChecks(opts = {}) {
225
+ const agents = (loadAgents()?.agents ?? []).filter((a) => a.month.tokens > 0).slice(0, 3);
226
+ const out = [];
227
+ for (const a of agents) {
228
+ for (const f of await wasteChecks({ ...opts, provider: a.name })) {
229
+ out.push({ ...f, agent: a.name, title: `${a.displayName}: ${f.title.charAt(0).toLowerCase()}${f.title.slice(1)}`, ...(f.key ? { key: `${a.name}:${f.key}` } : {}), ...(f.codeburnId ? { codeburnId: `${a.name}:${f.codeburnId}` } : {}) });
230
+ }
231
+ }
232
+ return out;
233
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chompyy",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Chompyy: runs the tasks you approve with the Claude Code 5-hour window you'd otherwise lose, then lets you review, keep and send them to GitHub. CLI behind the Chompyy Mac menu bar app.",
5
5
  "keywords": [
6
6
  "claude-code",