costclaw 0.7.0 → 0.8.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.
- package/README.md +36 -3
- package/dist/index.js +136 -26
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -111,7 +111,9 @@ costclaw audit [--path <dir>] [--claude-md <file>] [--plan <plan>] [--json] [--h
|
|
|
111
111
|
everything, so nothing is fetched and nothing is uploaded; the terminal
|
|
112
112
|
report still prints (in color on interactive terminals; set `NO_COLOR` to
|
|
113
113
|
disable). `--html <file>` picks the path; `--no-html` skips the file;
|
|
114
|
-
`--json` runs stay file-free unless `--html` is passed explicitly.
|
|
114
|
+
`--json` runs stay file-free unless `--html` is passed explicitly. Set
|
|
115
|
+
`COSTCLAW_NO_OPEN=1` to write reports without launching a browser (CI and
|
|
116
|
+
test runners are detected and never open one).
|
|
115
117
|
- `--card [file]` also write a shareable score card SVG (default
|
|
116
118
|
`costclaw-card.svg`): your score, band, recoverable spend, cache hit rate, and
|
|
117
119
|
strongest pillar. A static file, no fonts or network calls.
|
|
@@ -179,8 +181,9 @@ npx costclaw optimize
|
|
|
179
181
|
```
|
|
180
182
|
|
|
181
183
|
Turns the same audit into done-for-you artifacts: an optimized `CLAUDE.md`
|
|
182
|
-
scaffold, a prioritized fix playbook,
|
|
183
|
-
`settings.suggested.json` permission allowlist
|
|
184
|
+
scaffold, a prioritized fix playbook, a conservative, project-derived
|
|
185
|
+
`settings.suggested.json` permission allowlist, and a `HARNESS-UPGRADE.md` work
|
|
186
|
+
order for your own Claude Code. It scans `--project <dir>`
|
|
184
187
|
(default: the current directory) to auto-fill the scaffold from your real files.
|
|
185
188
|
By default it dry-runs into `./costclaw-out` and touches none of your files.
|
|
186
189
|
|
|
@@ -195,6 +198,36 @@ And it tells you what it actually did to the file that rides on every turn:
|
|
|
195
198
|
Your CLAUDE.md: an estimated 5,266 tokens to 2,479 (52.9% smaller), paid on every turn.
|
|
196
199
|
```
|
|
197
200
|
|
|
201
|
+
costclaw never calls a model, so it does not rewrite your prose: your own sections
|
|
202
|
+
are carried through exactly as written. When that carried prose is most of what is
|
|
203
|
+
left, the receipt says so rather than presenting a small percentage as a win:
|
|
204
|
+
|
|
205
|
+
```
|
|
206
|
+
About 1,714 of those tokens are your own prose, carried through as written.
|
|
207
|
+
costclaw does not rewrite prose, so trimming it is judgment work: HARNESS-UPGRADE.md
|
|
208
|
+
ranks your heaviest sections and flags the dated lines to move into docs/.
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
`HARNESS-UPGRADE.md` is the fourth artifact: a work order you hand to your own
|
|
212
|
+
Claude Code (`claude "Follow costclaw-out/HARNESS-UPGRADE.md"`). It carries the
|
|
213
|
+
merge and permission tasks plus evidence-conditional ones, and when your carried
|
|
214
|
+
prose is heavy it ranks it so the trimming is a worklist instead of a guess:
|
|
215
|
+
|
|
216
|
+
```
|
|
217
|
+
Heaviest carried sections, largest first:
|
|
218
|
+
|
|
219
|
+
- Core Philosophy: est. 1,382 tokens.
|
|
220
|
+
- How to Work: est. 1,267 tokens, 2 dated lines.
|
|
221
|
+
- Communication and Output: est. 550 tokens, 3 dated lines.
|
|
222
|
+
|
|
223
|
+
The remaining 6 carried sections account for the other 1,880 tokens.
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Those rows plus the remainder line add up to the stated total, the same way the
|
|
227
|
+
playbook's per-project table reconciles. costclaw sets no target line count
|
|
228
|
+
anywhere: it measures what your sections cost and which lines carry dates, and
|
|
229
|
+
leaves the keep-or-move judgment to you.
|
|
230
|
+
|
|
198
231
|
The playbook breaks the recoverable headline down by project, so you can see
|
|
199
232
|
which repo owes which dollars and at what cache hit rate:
|
|
200
233
|
|
package/dist/index.js
CHANGED
|
@@ -128,13 +128,25 @@ function projectFromCwd(cwd) {
|
|
|
128
128
|
const parts = cwd.split(/[\\/]+/).filter(Boolean);
|
|
129
129
|
return parts.length ? parts[parts.length - 1] : null;
|
|
130
130
|
}
|
|
131
|
+
function commandTarget(command) {
|
|
132
|
+
let s = command.trim();
|
|
133
|
+
const hop = /^(?:cd|Set-Location|pushd)\s+(?:"[^"]*"|'[^']*'|\S+)\s*(?:&&|;)\s*/i;
|
|
134
|
+
const env = /^[A-Za-z_][A-Za-z0-9_]*=(?:"[^"]*"|'[^']*'|\S*)\s+/;
|
|
135
|
+
for (; ; ) {
|
|
136
|
+
const next = s.replace(hop, "").replace(env, "");
|
|
137
|
+
if (next === s) break;
|
|
138
|
+
s = next;
|
|
139
|
+
}
|
|
140
|
+
const words = s.split(/\s+/).filter(Boolean);
|
|
141
|
+
if (words.length === 0) return null;
|
|
142
|
+
return words.slice(0, 2).join(" ").slice(0, 60);
|
|
143
|
+
}
|
|
131
144
|
function targetFor(name, input) {
|
|
132
145
|
const s = (v, n) => typeof v === "string" && v.length ? v.slice(0, n) : null;
|
|
133
146
|
if (["Read", "Edit", "Write", "NotebookEdit"].includes(name)) return s(input.file_path, 200);
|
|
134
147
|
if (["Grep", "Glob"].includes(name)) return s(input.path ?? input.glob ?? input.pattern, 160);
|
|
135
148
|
if (["Bash", "PowerShell"].includes(name)) {
|
|
136
|
-
|
|
137
|
-
return cmd ? cmd.slice(0, 60) : null;
|
|
149
|
+
return typeof input.command === "string" ? commandTarget(input.command) : null;
|
|
138
150
|
}
|
|
139
151
|
if (name.startsWith("Task")) return s(input.subject ?? input.description, 80);
|
|
140
152
|
if (["WebFetch", "WebSearch"].includes(name)) return s(input.url ?? input.query, 60);
|
|
@@ -353,6 +365,8 @@ var HIT_FLOOR = 0.5;
|
|
|
353
365
|
var STREAK = 3;
|
|
354
366
|
var HOT_PROJECT_SHARE = 0.6;
|
|
355
367
|
var HEAVY_TOOL_FIRES = 100;
|
|
368
|
+
var HEAVY_TOOL_MIN_SHARE = 0.6;
|
|
369
|
+
var REDUNDANT_TARGET_MIN_SHARE = 0.25;
|
|
356
370
|
var SHORT_SESSION_MAX_MINUTES = 10;
|
|
357
371
|
var SHORT_SESSION_MIN_COST = 5;
|
|
358
372
|
var MARATHON_MIN_TURNS = 40;
|
|
@@ -368,6 +382,11 @@ var MISROUTE_BASELINE = "claude-sonnet-4-6";
|
|
|
368
382
|
var WORKFLOW_MIN_COST = 5;
|
|
369
383
|
var WORKFLOW_WARN_SHARE = 0.5;
|
|
370
384
|
var REDUNDANT_TARGET_MIN_FIRES = 10;
|
|
385
|
+
function toolCallTotal(s) {
|
|
386
|
+
let sum = 0;
|
|
387
|
+
for (const n of Object.values(s.toolUsage)) sum += n;
|
|
388
|
+
return Math.max(sum, s.toolEvents.length);
|
|
389
|
+
}
|
|
371
390
|
function thirdHit(turns, lo, hi) {
|
|
372
391
|
let read = 0;
|
|
373
392
|
let total = 0;
|
|
@@ -436,8 +455,9 @@ function ruleHeavyToolLoops(ctx) {
|
|
|
436
455
|
const byTool = /* @__PURE__ */ new Map();
|
|
437
456
|
for (const s of proj.sessions) {
|
|
438
457
|
if (s.kind !== "main") continue;
|
|
458
|
+
const total = toolCallTotal(s);
|
|
439
459
|
for (const [tool, fires] of Object.entries(s.toolUsage)) {
|
|
440
|
-
if (fires < HEAVY_TOOL_FIRES) continue;
|
|
460
|
+
if (fires < HEAVY_TOOL_FIRES || fires < total * HEAVY_TOOL_MIN_SHARE) continue;
|
|
441
461
|
const entry = byTool.get(tool) ?? { sessionsAffected: 0, maxFires: 0, sessionUuids: [] };
|
|
442
462
|
entry.sessionsAffected += 1;
|
|
443
463
|
entry.maxFires = Math.max(entry.maxFires, fires);
|
|
@@ -473,6 +493,7 @@ function ruleRedundantTargetThrash(ctx) {
|
|
|
473
493
|
const byTool = /* @__PURE__ */ new Map();
|
|
474
494
|
for (const s of proj.sessions) {
|
|
475
495
|
if (s.kind !== "main") continue;
|
|
496
|
+
const total = toolCallTotal(s);
|
|
476
497
|
const counts = /* @__PURE__ */ new Map();
|
|
477
498
|
for (const e of s.toolEvents) {
|
|
478
499
|
if (e.target == null) continue;
|
|
@@ -483,7 +504,7 @@ function ruleRedundantTargetThrash(ctx) {
|
|
|
483
504
|
const perToolMax = /* @__PURE__ */ new Map();
|
|
484
505
|
for (const [tool, byTarget] of counts) {
|
|
485
506
|
for (const n of byTarget.values()) {
|
|
486
|
-
if (n < REDUNDANT_TARGET_MIN_FIRES) continue;
|
|
507
|
+
if (n < REDUNDANT_TARGET_MIN_FIRES || n < total * REDUNDANT_TARGET_MIN_SHARE) continue;
|
|
487
508
|
perToolMax.set(tool, Math.max(perToolMax.get(tool) ?? 0, n));
|
|
488
509
|
}
|
|
489
510
|
}
|
|
@@ -1068,7 +1089,7 @@ function buildChecks(report, cmd, answers, setup) {
|
|
|
1068
1089
|
"prompting",
|
|
1069
1090
|
"Tool looping",
|
|
1070
1091
|
s.sessionsCount > 0 ? clamp2(100 - thrashShare * 180) : null,
|
|
1071
|
-
s.sessionsCount === 0 ? "No sessions to assess." : thrashSessions === 0 ? "No sign of a stuck tool loop." : `${thrashSessions} of ${s.sessionsCount} sessions (${thrashPct}%) showed heavy tool repetition: one tool
|
|
1092
|
+
s.sessionsCount === 0 ? "No sessions to assess." : thrashSessions === 0 ? "No sign of a stuck tool loop." : `${thrashSessions} of ${s.sessionsCount} sessions (${thrashPct}%) showed heavy tool repetition: one tool making 100 or more calls and most of the session's tool calls, or the same file, command, or target hit 10 or more times and a quarter of the session's tool calls.`,
|
|
1072
1093
|
"metrics"
|
|
1073
1094
|
)
|
|
1074
1095
|
);
|
|
@@ -1189,7 +1210,7 @@ function scorePillars(report, cmd, answers, setup) {
|
|
|
1189
1210
|
const cfg = PILLARS[id];
|
|
1190
1211
|
const pillarChecks = checks.filter((c) => c.pillar === id);
|
|
1191
1212
|
const assessedChecks = pillarChecks.filter((c) => c.assessed);
|
|
1192
|
-
const assessed = assessedChecks.length > 0;
|
|
1213
|
+
const assessed = assessedChecks.length > 0 && assessedChecks.length * 2 >= pillarChecks.length;
|
|
1193
1214
|
const score = assessed ? Math.round(assessedChecks.reduce((sum, c) => sum + c.score, 0) / assessedChecks.length) : null;
|
|
1194
1215
|
return { id, name: cfg.name, weight: cfg.weight, assessed, score, checks: pillarChecks };
|
|
1195
1216
|
});
|
|
@@ -1260,11 +1281,14 @@ var HEAVY_TOOL_PATTERN_MIN_SESSIONS = 2;
|
|
|
1260
1281
|
function severityRank(s) {
|
|
1261
1282
|
return s === "critical" ? 3 : s === "warn" ? 2 : 1;
|
|
1262
1283
|
}
|
|
1284
|
+
function num2(n) {
|
|
1285
|
+
return String(Math.round(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
1286
|
+
}
|
|
1263
1287
|
function sizeFix(cmd) {
|
|
1264
1288
|
if (cmd.chars > 6e3) {
|
|
1265
1289
|
return {
|
|
1266
1290
|
title: "Trim your CLAUDE.md",
|
|
1267
|
-
action: `Your CLAUDE.md is an estimated ${cmd.estTokens
|
|
1291
|
+
action: `Your CLAUDE.md is an estimated ${num2(cmd.estTokens)} tokens (${num2(cmd.chars)} characters), paid on every turn of every session. Cut it to the durable conventions the agent actually needs and move the rest into docs it can open on demand.`
|
|
1268
1292
|
};
|
|
1269
1293
|
}
|
|
1270
1294
|
return {
|
|
@@ -1620,6 +1644,9 @@ function usd(n) {
|
|
|
1620
1644
|
const grouped = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
1621
1645
|
return `${neg ? "-" : ""}$${grouped}${cents}`;
|
|
1622
1646
|
}
|
|
1647
|
+
function num3(n) {
|
|
1648
|
+
return String(Math.round(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
1649
|
+
}
|
|
1623
1650
|
function clean(text2) {
|
|
1624
1651
|
return sanitizeText(text2);
|
|
1625
1652
|
}
|
|
@@ -1664,16 +1691,25 @@ function parseSections(md) {
|
|
|
1664
1691
|
const lines = md.split(/\r?\n/);
|
|
1665
1692
|
const sections = [];
|
|
1666
1693
|
let current = null;
|
|
1694
|
+
const finish = (c) => {
|
|
1695
|
+
const body = c.body.join("\n").trim();
|
|
1696
|
+
return {
|
|
1697
|
+
heading: c.heading,
|
|
1698
|
+
body,
|
|
1699
|
+
tokens: Math.round(body.length / 4),
|
|
1700
|
+
volatileLines: c.body.filter((l) => VOLATILE_MARKER.test(l)).length
|
|
1701
|
+
};
|
|
1702
|
+
};
|
|
1667
1703
|
for (const line of lines) {
|
|
1668
1704
|
const m = line.match(/^##\s+(.+?)\s*$/);
|
|
1669
1705
|
if (m) {
|
|
1670
|
-
if (current) sections.push(
|
|
1706
|
+
if (current) sections.push(finish(current));
|
|
1671
1707
|
current = { heading: m[1], body: [] };
|
|
1672
1708
|
} else if (current) {
|
|
1673
1709
|
current.body.push(line);
|
|
1674
1710
|
}
|
|
1675
1711
|
}
|
|
1676
|
-
if (current) sections.push(
|
|
1712
|
+
if (current) sections.push(finish(current));
|
|
1677
1713
|
return sections.filter((s) => s.body.length > 0);
|
|
1678
1714
|
}
|
|
1679
1715
|
var REGENERATED_HEADING = [
|
|
@@ -1693,7 +1729,9 @@ function carriedSections(currentClaudeMd) {
|
|
|
1693
1729
|
other: [],
|
|
1694
1730
|
droppedVolatile: [],
|
|
1695
1731
|
replaced: [],
|
|
1696
|
-
commandsBody: null
|
|
1732
|
+
commandsBody: null,
|
|
1733
|
+
carriedTokens: 0,
|
|
1734
|
+
carriedList: []
|
|
1697
1735
|
};
|
|
1698
1736
|
if (!currentClaudeMd) return out;
|
|
1699
1737
|
for (const section of parseSections(currentClaudeMd)) {
|
|
@@ -1706,10 +1744,16 @@ function carriedSections(currentClaudeMd) {
|
|
|
1706
1744
|
}
|
|
1707
1745
|
} else if (out.workflow === null && WORKFLOW_HEADING.test(section.heading)) {
|
|
1708
1746
|
out.workflow = section.body;
|
|
1747
|
+
out.carriedTokens += section.tokens;
|
|
1748
|
+
out.carriedList.push(section);
|
|
1709
1749
|
} else if (out.architecture === null && ARCH_HEADING.test(section.heading)) {
|
|
1710
1750
|
out.architecture = section.body;
|
|
1751
|
+
out.carriedTokens += section.tokens;
|
|
1752
|
+
out.carriedList.push(section);
|
|
1711
1753
|
} else {
|
|
1712
1754
|
out.other.push(section);
|
|
1755
|
+
out.carriedTokens += section.tokens;
|
|
1756
|
+
out.carriedList.push(section);
|
|
1713
1757
|
}
|
|
1714
1758
|
}
|
|
1715
1759
|
return out;
|
|
@@ -1800,8 +1844,8 @@ function buildClaudeMd(input, recoverable, notes, stats, carried, dispositions)
|
|
|
1800
1844
|
" How to use this file:",
|
|
1801
1845
|
" - Replace every placeholder comment with your project's real details, then",
|
|
1802
1846
|
" delete any line that does not earn its place.",
|
|
1803
|
-
|
|
1804
|
-
'
|
|
1847
|
+
' - Keep it lean, but do not cut to a line count. For each line ask "would',
|
|
1848
|
+
' removing this make the agent make a mistake?" If not, cut it.',
|
|
1805
1849
|
" - Only include what the agent cannot infer from the code: commands it cannot",
|
|
1806
1850
|
" guess, conventions that differ from the language defaults, and non-obvious",
|
|
1807
1851
|
" gotchas. Skip anything self-evident.",
|
|
@@ -2245,7 +2289,7 @@ function claudeMdReceiptLine(claudeMdTokens) {
|
|
|
2245
2289
|
const grew = delta > 0;
|
|
2246
2290
|
const pct2 = before > 0 ? ` (${(Math.abs(delta) / before * 100).toFixed(1)}% ${grew ? "larger" : "smaller"})` : "";
|
|
2247
2291
|
const tail = grew ? "It is re-read on every turn, so the larger file costs a little more every time too." : "It is re-read on every turn, so that saving repeats every session.";
|
|
2248
|
-
return `Your optimized CLAUDE.md goes from an estimated ${before
|
|
2292
|
+
return `Your optimized CLAUDE.md goes from an estimated ${num3(before)} tokens to ${num3(after)}${pct2}. ${tail}`;
|
|
2249
2293
|
}
|
|
2250
2294
|
function buildPlaybook(report, fixes, scores, recoverable, claudeMdTokens, project) {
|
|
2251
2295
|
const dollarFixes = fixes.filter((f) => f.wasteUsd > 0);
|
|
@@ -2315,7 +2359,9 @@ function buildSettings(project) {
|
|
|
2315
2359
|
const settings = { permissions: { allow, deny: [...BASE_DENY] } };
|
|
2316
2360
|
return clean(JSON.stringify(settings, null, 2) + "\n");
|
|
2317
2361
|
}
|
|
2318
|
-
|
|
2362
|
+
var CARRY_HEAVY_TOKENS = 1500;
|
|
2363
|
+
var CARRY_WORKLIST_CAP = 8;
|
|
2364
|
+
function buildHarnessPrompt(input, carried, claudeMdTokens) {
|
|
2319
2365
|
const project = input.project;
|
|
2320
2366
|
const name = project?.name?.trim() || "this project";
|
|
2321
2367
|
const findings = input.report.findings;
|
|
@@ -2342,8 +2388,9 @@ function buildHarnessPrompt(input, carried) {
|
|
|
2342
2388
|
"- Never invent project facts. When a merge choice is unclear, ask.",
|
|
2343
2389
|
"- Show a diff before replacing any human-written file, and back the",
|
|
2344
2390
|
" file up first (CLAUDE.md -> CLAUDE.md.bak).",
|
|
2345
|
-
|
|
2346
|
-
'
|
|
2391
|
+
'- Keep the final CLAUDE.md lean: every line must pass "would removing',
|
|
2392
|
+
' this make the agent make a mistake?" Cut what fails that test, not down',
|
|
2393
|
+
" to a line count. There is no target number of lines.",
|
|
2347
2394
|
"- Commit per this repo's conventions when a task completes cleanly."
|
|
2348
2395
|
].join("\n")
|
|
2349
2396
|
);
|
|
@@ -2376,6 +2423,43 @@ function buildHarnessPrompt(input, carried) {
|
|
|
2376
2423
|
].join("\n")
|
|
2377
2424
|
);
|
|
2378
2425
|
let n = 3;
|
|
2426
|
+
const worklist = [...carried.carriedList].sort((a, b) => b.tokens - a.tokens).slice(0, CARRY_WORKLIST_CAP);
|
|
2427
|
+
if (carried.carriedTokens >= CARRY_HEAVY_TOKENS && worklist.length > 0) {
|
|
2428
|
+
const rows = worklist.map((s) => {
|
|
2429
|
+
const n2 = s.volatileLines;
|
|
2430
|
+
const dated = n2 > 0 ? `, ${num3(n2)} dated ${n2 === 1 ? "line" : "lines"}` : "";
|
|
2431
|
+
return `- ${s.heading}: est. ${num3(s.tokens)} tokens${dated}.`;
|
|
2432
|
+
});
|
|
2433
|
+
const shown = worklist.reduce((sum, s) => sum + s.tokens, 0);
|
|
2434
|
+
const rest = carried.carriedList.length - worklist.length;
|
|
2435
|
+
const tail = rest > 0 ? [`The remaining ${num3(rest)} carried ${rest === 1 ? "section accounts" : "sections account"} for the other ${num3(carried.carriedTokens - shown)} tokens.`] : [];
|
|
2436
|
+
const before = claudeMdTokens.before;
|
|
2437
|
+
const saved = before != null && before > 0 ? (before - claudeMdTokens.after) / before : 0;
|
|
2438
|
+
const consequence = saved <= 0 ? "that is why the optimized file is no smaller." : saved < 0.2 ? "that is why the optimized file is barely smaller." : "it is the weight that is left.";
|
|
2439
|
+
parts.push(
|
|
2440
|
+
[
|
|
2441
|
+
`## Task ${n++}: Reclaim the carried sections`,
|
|
2442
|
+
"",
|
|
2443
|
+
`Your CLAUDE.md carries an estimated ${num3(carried.carriedTokens)} tokens of your`,
|
|
2444
|
+
"own prose, paid on every turn of every session.",
|
|
2445
|
+
`costclaw does not rewrite your prose, so it passed all of it through untouched:`,
|
|
2446
|
+
`${consequence} Doing the actual trimming is this task,`,
|
|
2447
|
+
"and it needs judgment costclaw will not fake.",
|
|
2448
|
+
"",
|
|
2449
|
+
"Heaviest carried sections, largest first:",
|
|
2450
|
+
"",
|
|
2451
|
+
...rows,
|
|
2452
|
+
...tail,
|
|
2453
|
+
"",
|
|
2454
|
+
"For each one decide, line by line: is this a durable operating rule the agent",
|
|
2455
|
+
"needs loaded every turn, or is it narrative history? Dated lines are the usual",
|
|
2456
|
+
"giveaway for history. Rules stay in CLAUDE.md. History moves to docs/ (a",
|
|
2457
|
+
"DECISIONS.md for choices, an ERRORS.md for incidents) where the agent can open",
|
|
2458
|
+
"it on demand. Move it, do not delete it. Ask before cutting anything you cannot",
|
|
2459
|
+
"confidently classify."
|
|
2460
|
+
].join("\n")
|
|
2461
|
+
);
|
|
2462
|
+
}
|
|
2379
2463
|
if (carried.droppedVolatile.length > 0) {
|
|
2380
2464
|
parts.push(
|
|
2381
2465
|
[
|
|
@@ -2445,9 +2529,15 @@ function buildHarnessPrompt(input, carried) {
|
|
|
2445
2529
|
const lintCmd = project?.commands.find((c) => c.label === "lint")?.command;
|
|
2446
2530
|
if (testCmd) verify.push(`- Run \`${testCmd}\` and read the output.`);
|
|
2447
2531
|
if (lintCmd) verify.push(`- Run \`${lintCmd}\` and read the output.`);
|
|
2532
|
+
verify.push("- Confirm no generator comment blocks are left in CLAUDE.md.");
|
|
2533
|
+
if (carried.carriedTokens >= CARRY_HEAVY_TOKENS) {
|
|
2534
|
+
verify.push(
|
|
2535
|
+
"- Confirm every line moved out of CLAUDE.md landed in a file under",
|
|
2536
|
+
" docs/, and that re-reading CLAUDE.md start to finish still answers",
|
|
2537
|
+
" what the agent needs to know. Report the token count before and after."
|
|
2538
|
+
);
|
|
2539
|
+
}
|
|
2448
2540
|
verify.push(
|
|
2449
|
-
"- Confirm CLAUDE.md is under about 200 lines with no generator comments",
|
|
2450
|
-
" left in it.",
|
|
2451
2541
|
"- After about a week of normal usage, run `npx costclaw audit`: the",
|
|
2452
2542
|
' "Since optimize" line measures the cache-hit movement these changes',
|
|
2453
2543
|
" are meant to produce."
|
|
@@ -2466,9 +2556,10 @@ function optimize(input) {
|
|
|
2466
2556
|
before: input.currentClaudeMd ? Math.round(input.currentClaudeMd.trim().length / 4) : null,
|
|
2467
2557
|
after: Math.round(optimizedClaudeMd.trim().length / 4)
|
|
2468
2558
|
};
|
|
2559
|
+
const carriedTokens = carried.carriedTokens;
|
|
2469
2560
|
const playbook = buildPlaybook(input.report, input.fixes, input.scores, recoverable, claudeMdTokens, input.project);
|
|
2470
2561
|
const settingsJson = buildSettings(input.project);
|
|
2471
|
-
const harnessPrompt = buildHarnessPrompt(input, carried);
|
|
2562
|
+
const harnessPrompt = buildHarnessPrompt(input, carried, claudeMdTokens);
|
|
2472
2563
|
const cleanNotes = notes.map((n) => clean(n));
|
|
2473
2564
|
const cleanDispositions = dispositions.map((d) => ({ ...d, name: clean(d.name), why: clean(d.why) }));
|
|
2474
2565
|
if (hasEmDash(optimizedClaudeMd) || hasEmDash(playbook) || hasEmDash(settingsJson) || hasEmDash(harnessPrompt) || cleanNotes.some(hasEmDash) || cleanDispositions.some((d) => hasEmDash(d.name) || hasEmDash(d.why))) {
|
|
@@ -2480,6 +2571,7 @@ function optimize(input) {
|
|
|
2480
2571
|
notes: cleanNotes.map(sanitizeText),
|
|
2481
2572
|
placeholdersRemaining: stats.placeholders,
|
|
2482
2573
|
claudeMdTokens,
|
|
2574
|
+
carriedTokens,
|
|
2483
2575
|
sectionDispositions: cleanDispositions.map((d) => ({ ...d, name: sanitizeText(d.name), why: sanitizeText(d.why) }))
|
|
2484
2576
|
};
|
|
2485
2577
|
}
|
|
@@ -2491,6 +2583,7 @@ function optimize(input) {
|
|
|
2491
2583
|
notes: cleanNotes,
|
|
2492
2584
|
placeholdersRemaining: stats.placeholders,
|
|
2493
2585
|
claudeMdTokens,
|
|
2586
|
+
carriedTokens,
|
|
2494
2587
|
sectionDispositions: cleanDispositions
|
|
2495
2588
|
};
|
|
2496
2589
|
}
|
|
@@ -2735,6 +2828,7 @@ import { homedir as homedir3 } from "os";
|
|
|
2735
2828
|
// src/open.ts
|
|
2736
2829
|
import { spawn } from "child_process";
|
|
2737
2830
|
function openInBrowser(target) {
|
|
2831
|
+
if (process.env["VITEST"] || process.env["CI"] || process.env["COSTCLAW_NO_OPEN"]) return;
|
|
2738
2832
|
try {
|
|
2739
2833
|
const platform = process.platform;
|
|
2740
2834
|
const [cmd, args] = platform === "win32" ? ["cmd", ["/c", "start", "", target]] : platform === "darwin" ? ["open", [target]] : ["xdg-open", [target]];
|
|
@@ -2998,9 +3092,11 @@ function unassessedLine(pillars) {
|
|
|
2998
3092
|
const gaps = pillars.filter((p) => !p.assessed);
|
|
2999
3093
|
if (gaps.length === 0) return null;
|
|
3000
3094
|
const parts = gaps.map((p) => {
|
|
3001
|
-
const reason = p.checks
|
|
3095
|
+
const reason = p.checks.find((c) => !c.assessed)?.basis;
|
|
3002
3096
|
const hint = p.id === "claude_md" ? " Pass --claude-md <file> to score it." : "";
|
|
3003
|
-
|
|
3097
|
+
const partial = p.checks.filter((c) => c.assessed && c.score !== null).map((c) => `${c.label} alone scores ${c.score}`).join(", ");
|
|
3098
|
+
const tail = partial ? ` ${partial}.` : "";
|
|
3099
|
+
return reason ? `${p.name} (${reason}${hint}${tail})` : p.name;
|
|
3004
3100
|
});
|
|
3005
3101
|
return `Not yet scored: ${parts.join(" ")}`;
|
|
3006
3102
|
}
|
|
@@ -3699,6 +3795,9 @@ function approxTokens(n) {
|
|
|
3699
3795
|
function plural2(n, noun) {
|
|
3700
3796
|
return n === 1 ? noun : `${noun}s`;
|
|
3701
3797
|
}
|
|
3798
|
+
function num4(n) {
|
|
3799
|
+
return String(Math.round(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
3800
|
+
}
|
|
3702
3801
|
var STYLE2 = `
|
|
3703
3802
|
:root{
|
|
3704
3803
|
--bg:#faf9f7; --panel:#fff; --ink:#1a1a19; --ink2:#5c5a55; --line:#e3e0da;
|
|
@@ -4026,11 +4125,13 @@ function renderArtifactCards(input) {
|
|
|
4026
4125
|
function renderApplyCta(input) {
|
|
4027
4126
|
return `<div class="cta"><b>Hand the work order to your own Claude Code.</b> It merges the optimized CLAUDE.md (your facts win, the optimized structure wins), applies the permissions you approve, and fixes the harness-level findings above.<code class="cmd">claude "Follow ${esc(input.artifactsDir)}/${HARNESS_NAME}"</code><p class="note">CostClaw itself never calls a model. That is the privacy line: every number on this page came from logs already on your disk.</p></div>`;
|
|
4028
4127
|
}
|
|
4029
|
-
function renderCarriedDisclosure(sections) {
|
|
4128
|
+
function renderCarriedDisclosure(sections, carriedTokens) {
|
|
4030
4129
|
const carried = sections.filter((s) => s.disposition === "carried");
|
|
4031
4130
|
if (carried.length === 0) return "";
|
|
4032
4131
|
const names = carried.map((s) => esc(s.name)).join(" · ");
|
|
4033
|
-
|
|
4132
|
+
const weight = carriedTokens > 0 ? `, about ${num4(carriedTokens)} tokens` : "";
|
|
4133
|
+
const note = carriedTokens > 0 ? `<p class="note">Carried through as written. costclaw does not rewrite your prose, so this is where the remaining weight sits. HARNESS-UPGRADE.md ranks these sections and flags their dated lines for you to move into docs/.</p>` : "";
|
|
4134
|
+
return `<details style="margin-top:34px"><summary>All ${carried.length} ${plural2(carried.length, "section")} carried across from your existing CLAUDE.md${weight}</summary><div class="cols">${names}</div>${note}</details>`;
|
|
4034
4135
|
}
|
|
4035
4136
|
function renderHeader(input) {
|
|
4036
4137
|
const parts = [];
|
|
@@ -4071,7 +4172,7 @@ ${renderArtifactCards(input)}
|
|
|
4071
4172
|
<h2>Apply it</h2>
|
|
4072
4173
|
${renderApplyCta(input)}
|
|
4073
4174
|
|
|
4074
|
-
${renderCarriedDisclosure(input.sectionDispositions)}
|
|
4175
|
+
${renderCarriedDisclosure(input.sectionDispositions, input.carriedTokens)}
|
|
4075
4176
|
|
|
4076
4177
|
<footer>Generated locally by CostClaw v${esc(input.version)}. Dry run: none of your files were changed.
|
|
4077
4178
|
No network calls, no telemetry, no model calls.</footer>
|
|
@@ -4772,6 +4873,7 @@ async function runOptimize(opts) {
|
|
|
4772
4873
|
currentClaudeMd: claudeMd ?? null,
|
|
4773
4874
|
optimizedClaudeMd: result.optimizedClaudeMd,
|
|
4774
4875
|
claudeMdTokens: result.claudeMdTokens,
|
|
4876
|
+
carriedTokens: result.carriedTokens,
|
|
4775
4877
|
sectionDispositions: result.sectionDispositions,
|
|
4776
4878
|
placeholdersRemaining: result.placeholdersRemaining,
|
|
4777
4879
|
fixes,
|
|
@@ -4840,9 +4942,17 @@ async function runOptimize(opts) {
|
|
|
4840
4942
|
if (before != null && before !== after) {
|
|
4841
4943
|
const pct2 = before > 0 ? (Math.abs(after - before) / before * 100).toFixed(1) : "0.0";
|
|
4842
4944
|
const dir = after > before ? "larger" : "smaller";
|
|
4945
|
+
const g = (n) => String(Math.round(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
4843
4946
|
lines.push(
|
|
4844
|
-
`Your CLAUDE.md: an estimated ${before
|
|
4947
|
+
`Your CLAUDE.md: an estimated ${g(before)} tokens to ${g(after)} (${pct2}% ${dir}), paid on every turn.`
|
|
4845
4948
|
);
|
|
4949
|
+
if (result.carriedTokens >= after / 2) {
|
|
4950
|
+
lines.push(
|
|
4951
|
+
`About ${g(result.carriedTokens)} of those tokens are your own prose, carried through as written.`,
|
|
4952
|
+
"costclaw does not rewrite prose, so trimming it is judgment work: HARNESS-UPGRADE.md",
|
|
4953
|
+
"ranks your heaviest sections and flags the dated lines to move into docs/."
|
|
4954
|
+
);
|
|
4955
|
+
}
|
|
4846
4956
|
}
|
|
4847
4957
|
lines.push("");
|
|
4848
4958
|
if (result.placeholdersRemaining > 0) {
|
|
@@ -5207,7 +5317,7 @@ function parseBadgeFlags(args) {
|
|
|
5207
5317
|
};
|
|
5208
5318
|
}
|
|
5209
5319
|
function cliVersion() {
|
|
5210
|
-
return true ? "0.
|
|
5320
|
+
return true ? "0.8.1" : "0.0.0-dev";
|
|
5211
5321
|
}
|
|
5212
5322
|
function isHelpRequest(command, args) {
|
|
5213
5323
|
if (command === void 0) return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "costclaw",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Local cost and setup audit for Claude Code. Finds recoverable token spend and scores your setup across six pillars, with your prompts never leaving your machine.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -34,10 +34,12 @@
|
|
|
34
34
|
"diff": "tsx src/index.ts diff",
|
|
35
35
|
"optimize": "tsx src/index.ts optimize",
|
|
36
36
|
"dev": "tsx src/index.ts",
|
|
37
|
-
"build": "tsup",
|
|
38
|
-
"test": "vitest run",
|
|
37
|
+
"build": "npm run check:privacy && tsup",
|
|
38
|
+
"test": "npm run check:no-network && vitest run",
|
|
39
39
|
"typecheck": "tsc --noEmit",
|
|
40
|
-
"
|
|
40
|
+
"check:no-network": "node ../../scripts/check-no-network.mjs",
|
|
41
|
+
"check:privacy": "npm --prefix ../../packages/engine run test -- tests/audit.test.ts",
|
|
42
|
+
"prepublishOnly": "npm run check:no-network && npm run build"
|
|
41
43
|
},
|
|
42
44
|
"devDependencies": {
|
|
43
45
|
"@costclaw/engine": "*",
|