costclaw 0.2.1 → 0.2.3
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 +10 -2
- package/dist/index.js +105 -44
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,12 +20,18 @@ That reads `~/.claude/projects` (override with `--path <dir>`) and prints:
|
|
|
20
20
|
```
|
|
21
21
|
CostClaw audit
|
|
22
22
|
Source: ~/.claude/projects (N sessions across M projects)
|
|
23
|
+
CLAUDE.md: ~/.claude/CLAUDE.md
|
|
23
24
|
|
|
24
25
|
Recoverable spend (cache-miss exposure): $XXX.XX
|
|
25
26
|
|
|
26
27
|
Spend analyzed: $X,XXX.XX Cache hit: 96.3% Active hours: XXX.X
|
|
28
|
+
Model spend: opus 71% / sonnet 24% / haiku 5%
|
|
29
|
+
|
|
30
|
+
Top projects by spend:
|
|
31
|
+
C--Projects-demo $XXX.XX 312 sessions cache 96.4% opus
|
|
32
|
+
|
|
27
33
|
Overall setup score: 91 / 100 (Dialed in)
|
|
28
|
-
CLAUDE.md quality
|
|
34
|
+
CLAUDE.md quality [#################...] 85
|
|
29
35
|
Context hygiene [###################.] 95
|
|
30
36
|
Prompting patterns [##################..] 92
|
|
31
37
|
Session management [####################] 100
|
|
@@ -35,6 +41,7 @@ Overall setup score: 91 / 100 (Dialed in)
|
|
|
35
41
|
Top fixes by recoverable spend:
|
|
36
42
|
- [$XXX.XX] Recover spend lost to cache misses
|
|
37
43
|
Keep the start of each session stable so the prompt cache is reused.
|
|
44
|
+
Evidence: XX% of input tokens missed the cache across N sessions.
|
|
38
45
|
```
|
|
39
46
|
|
|
40
47
|
## Options
|
|
@@ -44,7 +51,8 @@ costclaw audit [--path <dir>] [--claude-md <file>] [--json] [--html [file]] [--c
|
|
|
44
51
|
```
|
|
45
52
|
|
|
46
53
|
- `--path <dir>` audit a specific projects directory (default `~/.claude/projects`).
|
|
47
|
-
- `--claude-md <file>` score a CLAUDE.md file's quality
|
|
54
|
+
- `--claude-md <file>` score a specific CLAUDE.md file's quality (by default the
|
|
55
|
+
audit scores `~/.claude/CLAUDE.md` when it exists).
|
|
48
56
|
- `--json` print the raw, derived `AuditRecord` instead of the human report.
|
|
49
57
|
- `--html [file]` also write a self-contained HTML report (default
|
|
50
58
|
`costclaw-report.html` in the current directory) and open it in your browser.
|
package/dist/index.js
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
import { pathToFileURL } from "url";
|
|
5
5
|
|
|
6
6
|
// src/audit-command.ts
|
|
7
|
-
import { readFile as readFile2, writeFile } from "fs/promises";
|
|
8
|
-
import { resolve } from "path";
|
|
7
|
+
import { mkdir, readFile as readFile2, writeFile } from "fs/promises";
|
|
8
|
+
import { join as join3, resolve } from "path";
|
|
9
|
+
import { homedir as homedir3 } from "os";
|
|
9
10
|
import { spawn } from "child_process";
|
|
10
11
|
|
|
11
12
|
// ../../packages/engine/src/types.ts
|
|
@@ -939,7 +940,7 @@ function buildChecks(report, cmd, answers, setup) {
|
|
|
939
940
|
"claude_md",
|
|
940
941
|
"Size",
|
|
941
942
|
cmd.present ? cmd.sizeScore : null,
|
|
942
|
-
cmd.present ? `Your CLAUDE.md is ${cmd.chars.toLocaleString()} characters, about ${cmd.estTokens.toLocaleString()} tokens loaded on every turn.` : "No CLAUDE.md was
|
|
943
|
+
cmd.present ? `Your CLAUDE.md is ${cmd.chars.toLocaleString()} characters, about ${cmd.estTokens.toLocaleString()} tokens loaded on every turn.` : "No CLAUDE.md was provided.",
|
|
943
944
|
"claudemd"
|
|
944
945
|
)
|
|
945
946
|
);
|
|
@@ -949,7 +950,7 @@ function buildChecks(report, cmd, answers, setup) {
|
|
|
949
950
|
"claude_md",
|
|
950
951
|
"Structure",
|
|
951
952
|
cmd.present ? cmd.structureScore : null,
|
|
952
|
-
cmd.present ? `${cmd.headings} heading${cmd.headings === 1 ? "" : "s"} and ${cmd.bullets} bullet point${cmd.bullets === 1 ? "" : "s"} across ${cmd.chars.toLocaleString()} characters.` : "No CLAUDE.md was
|
|
953
|
+
cmd.present ? `${cmd.headings} heading${cmd.headings === 1 ? "" : "s"} and ${cmd.bullets} bullet point${cmd.bullets === 1 ? "" : "s"} across ${cmd.chars.toLocaleString()} characters.` : "No CLAUDE.md was provided.",
|
|
953
954
|
"claudemd"
|
|
954
955
|
)
|
|
955
956
|
);
|
|
@@ -959,7 +960,7 @@ function buildChecks(report, cmd, answers, setup) {
|
|
|
959
960
|
"claude_md",
|
|
960
961
|
"Cache stability",
|
|
961
962
|
cmd.present ? cmd.stabilityScore : null,
|
|
962
|
-
cmd.present ? `${cmd.volatileMarkers} volatile marker${cmd.volatileMarkers === 1 ? "" : "s"} (dates, TODOs, "updated" notes) that change between sessions and reset the cache.` : "No CLAUDE.md was
|
|
963
|
+
cmd.present ? `${cmd.volatileMarkers} volatile marker${cmd.volatileMarkers === 1 ? "" : "s"} (dates, TODOs, "updated" notes) that change between sessions and reset the cache.` : "No CLAUDE.md was provided.",
|
|
963
964
|
"claudemd"
|
|
964
965
|
)
|
|
965
966
|
);
|
|
@@ -2030,12 +2031,29 @@ function bar(score, width = 20) {
|
|
|
2030
2031
|
const filled = Math.round(score / 100 * width);
|
|
2031
2032
|
return `[${"#".repeat(filled)}${".".repeat(width - filled)}] ${score}`;
|
|
2032
2033
|
}
|
|
2034
|
+
function shortModel(id) {
|
|
2035
|
+
const m = /(opus|sonnet|haiku|fable)/i.exec(id);
|
|
2036
|
+
return m && m[1] ? m[1].toLowerCase() : id;
|
|
2037
|
+
}
|
|
2038
|
+
function modelMix(breakdown) {
|
|
2039
|
+
const merged = /* @__PURE__ */ new Map();
|
|
2040
|
+
for (const [model, spend] of Object.entries(breakdown)) {
|
|
2041
|
+
if (spend <= 0) continue;
|
|
2042
|
+
const key = shortModel(model);
|
|
2043
|
+
merged.set(key, (merged.get(key) ?? 0) + spend);
|
|
2044
|
+
}
|
|
2045
|
+
const total = [...merged.values()].reduce((a, b) => a + b, 0);
|
|
2046
|
+
if (total <= 0) return null;
|
|
2047
|
+
const parts = [...merged.entries()].sort((a, b) => b[1] - a[1]).map(([name, spend]) => [name, Math.round(spend / total * 100)]).filter(([, pct]) => pct >= 1).map(([name, pct]) => `${name} ${pct}%`);
|
|
2048
|
+
return parts.length > 0 ? parts.join(" / ") : null;
|
|
2049
|
+
}
|
|
2033
2050
|
function unassessedLine(pillars) {
|
|
2034
2051
|
const gaps = pillars.filter((p) => !p.assessed);
|
|
2035
2052
|
if (gaps.length === 0) return null;
|
|
2036
2053
|
const parts = gaps.map((p) => {
|
|
2037
2054
|
const reason = p.checks[0]?.basis;
|
|
2038
|
-
|
|
2055
|
+
const hint = p.id === "claude_md" ? " Pass --claude-md <file> to score it." : "";
|
|
2056
|
+
return reason ? `${p.name} (${reason}${hint})` : p.name;
|
|
2039
2057
|
});
|
|
2040
2058
|
return `Not yet scored: ${parts.join(" ")}`;
|
|
2041
2059
|
}
|
|
@@ -2044,11 +2062,25 @@ function formatAudit(record, meta) {
|
|
|
2044
2062
|
const lines = [];
|
|
2045
2063
|
lines.push("CostClaw audit");
|
|
2046
2064
|
lines.push(`Source: ${meta.root} (${meta.sessionCount} sessions across ${m.projectsCount} projects)`);
|
|
2065
|
+
if (meta.claudeMdSource) lines.push(`CLAUDE.md: ${meta.claudeMdSource}`);
|
|
2047
2066
|
lines.push("");
|
|
2048
2067
|
lines.push(`${record.headlineWasteLabel}: ${usd2(record.headlineWasteUsd)}`);
|
|
2049
2068
|
lines.push("");
|
|
2050
2069
|
lines.push(`Spend analyzed: ${usd2(m.spendUsd)} Cache hit: ${(m.cacheHitRate * 100).toFixed(1)}% Active hours: ${m.durationHours.toFixed(1)}`);
|
|
2051
2070
|
lines.push(`Pricing snapshot: ${m.pricingSnapshotDate}`);
|
|
2071
|
+
const mix = modelMix(m.modelBreakdown);
|
|
2072
|
+
if (mix) lines.push(`Model spend: ${mix}`);
|
|
2073
|
+
if (m.topProjects.length > 0) {
|
|
2074
|
+
lines.push("");
|
|
2075
|
+
lines.push("Top projects by spend:");
|
|
2076
|
+
for (const p of m.topProjects.slice(0, 5)) {
|
|
2077
|
+
const name = p.slug.length > 30 ? p.slug.slice(0, 27) + "..." : p.slug;
|
|
2078
|
+
const model = p.modelPrimary ? shortModel(p.modelPrimary) : "n/a";
|
|
2079
|
+
lines.push(
|
|
2080
|
+
` ${name.padEnd(32)}${usd2(p.costUsd).padStart(10)} ${String(p.sessionsCount).padStart(4)} sessions cache ${(p.cacheHitRate * 100).toFixed(1)}% ${model}`
|
|
2081
|
+
);
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2052
2084
|
lines.push("");
|
|
2053
2085
|
lines.push(`Overall setup score: ${record.overallScore} / 100 (${record.band.label})`);
|
|
2054
2086
|
for (const p of record.pillarScores) {
|
|
@@ -2061,19 +2093,18 @@ function formatAudit(record, meta) {
|
|
|
2061
2093
|
if (dollarFixes.length === 0) {
|
|
2062
2094
|
lines.push(" No directly recoverable dollars found. See issues below.");
|
|
2063
2095
|
} else {
|
|
2064
|
-
for (const f of dollarFixes
|
|
2096
|
+
for (const f of dollarFixes) {
|
|
2065
2097
|
lines.push(` - [${usd2(f.wasteUsd)}] ${f.title}`);
|
|
2066
2098
|
lines.push(` ${f.action}`);
|
|
2099
|
+
if (f.basis) lines.push(` Evidence: ${f.basis}`);
|
|
2067
2100
|
}
|
|
2068
2101
|
}
|
|
2069
2102
|
if (otherIssues.length > 0) {
|
|
2070
2103
|
lines.push("");
|
|
2071
2104
|
lines.push("Other issues to tighten:");
|
|
2072
|
-
for (const f of otherIssues
|
|
2105
|
+
for (const f of otherIssues) {
|
|
2073
2106
|
lines.push(` - ${f.title}`);
|
|
2074
|
-
|
|
2075
|
-
if (otherIssues.length > 6) {
|
|
2076
|
-
lines.push(` ... and ${otherIssues.length - 6} more`);
|
|
2107
|
+
if (f.basis) lines.push(` ${f.basis}`);
|
|
2077
2108
|
}
|
|
2078
2109
|
}
|
|
2079
2110
|
if (meta.skipped.length > 0) {
|
|
@@ -2154,10 +2185,10 @@ function pillarBars(pillars) {
|
|
|
2154
2185
|
}).join("");
|
|
2155
2186
|
}
|
|
2156
2187
|
function dollarFixCard(f) {
|
|
2157
|
-
return `<li class="fix"><div class="fix-head"><span class="fix-amt">${esc(usd3(f.wasteUsd))}</span><span class="fix-title">${esc(f.title)}</span></div><p class="fix-problem">${esc(f.problem)}</p><p class="fix-action">${esc(f.action)}</p
|
|
2188
|
+
return `<li class="fix"><div class="fix-head"><span class="fix-amt">${esc(usd3(f.wasteUsd))}</span><span class="fix-title">${esc(f.title)}</span></div><p class="fix-problem">${esc(f.problem)}</p><p class="fix-action">${esc(f.action)}</p>` + (f.basis ? `<p class="fix-basis">${esc(f.basis)}</p>` : "") + `</li>`;
|
|
2158
2189
|
}
|
|
2159
2190
|
function otherFixCard(f) {
|
|
2160
|
-
return `<li class="fix other"><div class="fix-head"><span class="fix-title">${esc(f.title)}</span></div><p class="fix-action">${esc(f.action)}</p
|
|
2191
|
+
return `<li class="fix other"><div class="fix-head"><span class="fix-title">${esc(f.title)}</span></div><p class="fix-action">${esc(f.action)}</p>` + (f.basis ? `<p class="fix-basis">${esc(f.basis)}</p>` : "") + `</li>`;
|
|
2161
2192
|
}
|
|
2162
2193
|
var STYLE = `
|
|
2163
2194
|
:root { color-scheme: light; }
|
|
@@ -2205,11 +2236,14 @@ ul.fixes { list-style: none; margin: 0; padding: 0; }
|
|
|
2205
2236
|
.fix-title { font-size: 15px; font-weight: 600; }
|
|
2206
2237
|
.fix-problem { margin: 8px 0 4px; color: #5a4a43; font-size: 14px; }
|
|
2207
2238
|
.fix-action { margin: 4px 0 0; font-size: 14px; }
|
|
2239
|
+
.fix-basis { margin: 6px 0 0; font-size: 13px; color: #9a8a82; }
|
|
2240
|
+
.stat .v-small { font-size: 14px; font-weight: 600; margin-top: 6px; }
|
|
2208
2241
|
.footer { margin-top: 48px; padding-top: 18px; border-top: 1px solid #f0e2da; color: #7a6a63; font-size: 13px; }
|
|
2209
2242
|
.footer strong { color: #251d1a; }
|
|
2210
2243
|
`;
|
|
2211
2244
|
function renderHtml(record, meta) {
|
|
2212
2245
|
const m = record.usageMetrics;
|
|
2246
|
+
const mix = modelMix(m.modelBreakdown);
|
|
2213
2247
|
const dollarFixes = record.fixes.filter((f) => f.wasteUsd > 0).sort((a, b) => b.wasteUsd - a.wasteUsd);
|
|
2214
2248
|
const otherIssues = record.fixes.filter((f) => f.wasteUsd === 0);
|
|
2215
2249
|
const dollarSection = dollarFixes.length === 0 ? `<p class="muted">No directly recoverable dollars found. See issues below.</p>` : `<ul class="fixes">${dollarFixes.map(dollarFixCard).join("")}</ul>`;
|
|
@@ -2227,6 +2261,7 @@ function renderHtml(record, meta) {
|
|
|
2227
2261
|
<div class="wrap">
|
|
2228
2262
|
<h1>CostClaw audit</h1>
|
|
2229
2263
|
<p class="sub">Source: ${esc(meta.root)} (${meta.sessionCount} sessions across ${m.projectsCount} projects)</p>
|
|
2264
|
+
${meta.claudeMdSource ? `<p class="sub">CLAUDE.md: ${esc(meta.claudeMdSource)}</p>` : ""}
|
|
2230
2265
|
<p class="sub">Pricing snapshot: ${esc(m.pricingSnapshotDate)}</p>
|
|
2231
2266
|
|
|
2232
2267
|
<div class="banner">
|
|
@@ -2238,6 +2273,7 @@ function renderHtml(record, meta) {
|
|
|
2238
2273
|
<div class="stat"><div class="k">Spend analyzed</div><div class="v">${esc(usd3(m.spendUsd))}</div></div>
|
|
2239
2274
|
<div class="stat"><div class="k">Cache hit rate</div><div class="v">${(m.cacheHitRate * 100).toFixed(1)}%</div></div>
|
|
2240
2275
|
<div class="stat"><div class="k">Active hours</div><div class="v">${m.durationHours.toFixed(1)}</div></div>
|
|
2276
|
+
${mix ? `<div class="stat"><div class="k">Model spend</div><div class="v v-small">${esc(mix)}</div></div>` : ""}
|
|
2241
2277
|
</div>
|
|
2242
2278
|
|
|
2243
2279
|
<h2>Spend by project</h2>
|
|
@@ -2368,20 +2404,45 @@ async function loadAuditInputs(opts) {
|
|
|
2368
2404
|
throw new Error(`No .jsonl sessions found under ${root}. Set CLAUDE_PROJECTS_PATH or pass --path <dir>.`);
|
|
2369
2405
|
}
|
|
2370
2406
|
let claudeMd;
|
|
2407
|
+
let claudeMdSource = null;
|
|
2371
2408
|
if (opts.claudeMdPath) {
|
|
2372
2409
|
claudeMd = await readFile2(opts.claudeMdPath, "utf8");
|
|
2410
|
+
claudeMdSource = resolve(opts.claudeMdPath);
|
|
2411
|
+
} else {
|
|
2412
|
+
const fallback = join3(opts.home ?? homedir3(), ".claude", "CLAUDE.md");
|
|
2413
|
+
try {
|
|
2414
|
+
claudeMd = await readFile2(fallback, "utf8");
|
|
2415
|
+
claudeMdSource = fallback;
|
|
2416
|
+
} catch {
|
|
2417
|
+
}
|
|
2373
2418
|
}
|
|
2374
|
-
return { logs, claudeMd, root, skipped };
|
|
2419
|
+
return { logs, claudeMd, claudeMdSource, root, skipped };
|
|
2375
2420
|
}
|
|
2376
2421
|
async function runAudit(opts) {
|
|
2377
|
-
const { logs, claudeMd, root, skipped } = await loadAuditInputs(
|
|
2422
|
+
const { logs, claudeMd, claudeMdSource, root, skipped } = await loadAuditInputs({
|
|
2423
|
+
path: opts.path,
|
|
2424
|
+
claudeMdPath: opts.claudeMdPath,
|
|
2425
|
+
home: opts.home
|
|
2426
|
+
});
|
|
2378
2427
|
const setup = await readSetup();
|
|
2379
2428
|
const record = buildAudit({ logs, claudeMd, questionnaire: {}, setup });
|
|
2380
|
-
let
|
|
2429
|
+
let snapshotNotice = null;
|
|
2430
|
+
try {
|
|
2431
|
+
const historyDir = join3(opts.home ?? homedir3(), ".costclaw", "history");
|
|
2432
|
+
await mkdir(historyDir, { recursive: true });
|
|
2433
|
+
const name = record.createdAt.replace(/[:.]/g, "-") + ".json";
|
|
2434
|
+
await writeFile(join3(historyDir, name), JSON.stringify(record, null, 2), "utf8");
|
|
2435
|
+
snapshotNotice = "Snapshot saved for costclaw diff.";
|
|
2436
|
+
} catch (err) {
|
|
2437
|
+
process.stderr.write(`Could not save the audit snapshot: ${err instanceof Error ? err.message : String(err)}
|
|
2438
|
+
`);
|
|
2439
|
+
}
|
|
2440
|
+
let output = opts.json ? JSON.stringify(record, null, 2) : formatAudit(record, { root, sessionCount: logs.length, skipped, claudeMdSource });
|
|
2381
2441
|
const notices = [];
|
|
2442
|
+
if (snapshotNotice) notices.push(snapshotNotice);
|
|
2382
2443
|
if (opts.html) {
|
|
2383
2444
|
const target = resolve(opts.htmlPath || DEFAULT_HTML_FILE);
|
|
2384
|
-
const doc = renderHtml(record, { root, sessionCount: logs.length, skipped });
|
|
2445
|
+
const doc = renderHtml(record, { root, sessionCount: logs.length, skipped, claudeMdSource });
|
|
2385
2446
|
await writeFile(target, doc, "utf8");
|
|
2386
2447
|
openInBrowser(target);
|
|
2387
2448
|
notices.push(`HTML report written to ${target}`);
|
|
@@ -2401,12 +2462,12 @@ async function runAudit(opts) {
|
|
|
2401
2462
|
}
|
|
2402
2463
|
|
|
2403
2464
|
// src/optimize-command.ts
|
|
2404
|
-
import { mkdir, writeFile as writeFile2, readFile as readFile4, stat as stat2 } from "fs/promises";
|
|
2405
|
-
import { join as
|
|
2465
|
+
import { mkdir as mkdir2, writeFile as writeFile2, readFile as readFile4, stat as stat2 } from "fs/promises";
|
|
2466
|
+
import { join as join5, dirname } from "path";
|
|
2406
2467
|
|
|
2407
2468
|
// src/project-scan.ts
|
|
2408
2469
|
import { readdir as readdir2, readFile as readFile3, stat } from "fs/promises";
|
|
2409
|
-
import { join as
|
|
2470
|
+
import { join as join4, basename } from "path";
|
|
2410
2471
|
var NOISE_DIRS = /* @__PURE__ */ new Set([
|
|
2411
2472
|
"node_modules",
|
|
2412
2473
|
".git",
|
|
@@ -2546,7 +2607,7 @@ async function detectTestsLocation(dir, files, dirs) {
|
|
|
2546
2607
|
if (dirs.includes("__tests__")) return "tests in __tests__/";
|
|
2547
2608
|
if (files.some((f) => /\.(test|spec)\.[cm]?[jt]sx?$/.test(f))) return "tests alongside source (*.test.* / *.spec.*)";
|
|
2548
2609
|
if (dirs.includes("src")) {
|
|
2549
|
-
const sub = await listEntries(
|
|
2610
|
+
const sub = await listEntries(join4(dir, "src"));
|
|
2550
2611
|
if (sub.files.some((f) => /\.(test|spec)\.[cm]?[jt]sx?$/.test(f))) {
|
|
2551
2612
|
return "tests alongside source (*.test.* / *.spec.*)";
|
|
2552
2613
|
}
|
|
@@ -2629,7 +2690,7 @@ async function scanNode(dir, files, dirs, pkg, profile) {
|
|
|
2629
2690
|
if (oneFileCmd !== null && "test" in scripts) {
|
|
2630
2691
|
profile.commands.push({ label: "test one file", command: oneFileCmd });
|
|
2631
2692
|
}
|
|
2632
|
-
const tsconfig = parseJson(await readText(
|
|
2693
|
+
const tsconfig = parseJson(await readText(join4(dir, "tsconfig.json")));
|
|
2633
2694
|
if (tsconfig) {
|
|
2634
2695
|
const co = tsconfig.compilerOptions;
|
|
2635
2696
|
if (co && co.strict === true) pushUnique(profile.codeStyle, "TypeScript strict mode");
|
|
@@ -2732,7 +2793,7 @@ async function scanPython(dir, files, profile) {
|
|
|
2732
2793
|
const deps = [];
|
|
2733
2794
|
const tables = /* @__PURE__ */ new Set();
|
|
2734
2795
|
let hasPoetry = false;
|
|
2735
|
-
const pyprojectText = await readText(
|
|
2796
|
+
const pyprojectText = await readText(join4(dir, "pyproject.toml"));
|
|
2736
2797
|
if (pyprojectText !== null) {
|
|
2737
2798
|
const parsed = parsePyproject(pyprojectText);
|
|
2738
2799
|
name = parsed.name;
|
|
@@ -2742,7 +2803,7 @@ async function scanPython(dir, files, profile) {
|
|
|
2742
2803
|
hasPoetry = parsed.hasPoetry;
|
|
2743
2804
|
}
|
|
2744
2805
|
if (files.includes("requirements.txt")) {
|
|
2745
|
-
const reqText = await readText(
|
|
2806
|
+
const reqText = await readText(join4(dir, "requirements.txt"));
|
|
2746
2807
|
if (reqText !== null) for (const d of pyDepsFromRequirements(reqText)) pushUnique(deps, d);
|
|
2747
2808
|
}
|
|
2748
2809
|
if (name !== null && name.trim() !== "") profile.name = name;
|
|
@@ -2776,7 +2837,7 @@ async function scanPython(dir, files, profile) {
|
|
|
2776
2837
|
}
|
|
2777
2838
|
}
|
|
2778
2839
|
async function scanRust(dir, profile) {
|
|
2779
|
-
const text2 = await readText(
|
|
2840
|
+
const text2 = await readText(join4(dir, "Cargo.toml"));
|
|
2780
2841
|
pushUnique(profile.stack, "Rust");
|
|
2781
2842
|
profile.packageManager = "cargo";
|
|
2782
2843
|
if (text2 !== null) {
|
|
@@ -2802,7 +2863,7 @@ async function scanRust(dir, profile) {
|
|
|
2802
2863
|
profile.commands.push({ label: "lint", command: "cargo clippy" });
|
|
2803
2864
|
}
|
|
2804
2865
|
async function scanGo(dir, profile) {
|
|
2805
|
-
const text2 = await readText(
|
|
2866
|
+
const text2 = await readText(join4(dir, "go.mod"));
|
|
2806
2867
|
pushUnique(profile.stack, "Go");
|
|
2807
2868
|
profile.packageManager = "go";
|
|
2808
2869
|
if (text2 !== null) {
|
|
@@ -2867,7 +2928,7 @@ async function scanProject(dir) {
|
|
|
2867
2928
|
const profile = emptyProfile(basename(dir));
|
|
2868
2929
|
const { files, dirs } = await listEntries(dir);
|
|
2869
2930
|
if (files.includes("package.json")) {
|
|
2870
|
-
const pkg = parseJson(await readText(
|
|
2931
|
+
const pkg = parseJson(await readText(join4(dir, "package.json")));
|
|
2871
2932
|
if (pkg) await scanNode(dir, files, dirs, pkg, profile);
|
|
2872
2933
|
}
|
|
2873
2934
|
if (files.includes("pyproject.toml") || files.includes("requirements.txt") || files.includes("setup.py")) {
|
|
@@ -2880,7 +2941,7 @@ async function scanProject(dir) {
|
|
|
2880
2941
|
await scanGo(dir, profile);
|
|
2881
2942
|
}
|
|
2882
2943
|
if (files.includes("Makefile") && profile.commands.length === 0) {
|
|
2883
|
-
const text2 = await readText(
|
|
2944
|
+
const text2 = await readText(join4(dir, "Makefile"));
|
|
2884
2945
|
if (text2 !== null) {
|
|
2885
2946
|
for (const t of parseMakefile(text2)) {
|
|
2886
2947
|
profile.commands.push({ label: t, command: `make ${t}` });
|
|
@@ -2890,7 +2951,7 @@ async function scanProject(dir) {
|
|
|
2890
2951
|
if (profile.description === null || profile.name === basename(dir)) {
|
|
2891
2952
|
const readmeName = files.find((f) => /^readme(\.md|\.markdown|\.txt)?$/i.test(f));
|
|
2892
2953
|
if (readmeName) {
|
|
2893
|
-
const text2 = await readText(
|
|
2954
|
+
const text2 = await readText(join4(dir, readmeName));
|
|
2894
2955
|
if (text2 !== null) {
|
|
2895
2956
|
const parsed = parseReadme(text2);
|
|
2896
2957
|
if (profile.name === basename(dir) && parsed.name !== null) profile.name = parsed.name;
|
|
@@ -2903,7 +2964,7 @@ async function scanProject(dir) {
|
|
|
2903
2964
|
profile.structure = await detectStructure(dir, dirs);
|
|
2904
2965
|
const envFile = files.includes(".env.example") ? ".env.example" : files.includes(".env.sample") ? ".env.sample" : null;
|
|
2905
2966
|
if (envFile) {
|
|
2906
|
-
const text2 = await readText(
|
|
2967
|
+
const text2 = await readText(join4(dir, envFile));
|
|
2907
2968
|
if (text2 !== null) profile.envVars = parseEnvNames(text2);
|
|
2908
2969
|
}
|
|
2909
2970
|
return profile;
|
|
@@ -3006,7 +3067,7 @@ async function applyDerivedSettings(target, suggestedJson, force) {
|
|
|
3006
3067
|
mergedPerms.deny = unionStrings(asStringArray(existingPerms.deny), suggestedDeny);
|
|
3007
3068
|
const merged = { ...existing, permissions: mergedPerms };
|
|
3008
3069
|
const mergedText = JSON.stringify(merged, null, 2) + "\n";
|
|
3009
|
-
await
|
|
3070
|
+
await mkdir2(dirname(target), { recursive: true });
|
|
3010
3071
|
await writeFile2(backup, existingText, "utf8");
|
|
3011
3072
|
await writeFile2(target, mergedText, "utf8");
|
|
3012
3073
|
return [
|
|
@@ -3028,11 +3089,11 @@ async function runOptimize(opts) {
|
|
|
3028
3089
|
const project = await scanProject(projectDir);
|
|
3029
3090
|
const result = optimize({ report, scores: pillars, fixes, currentClaudeMd: claudeMd, project });
|
|
3030
3091
|
const recoverable = cacheMissExposureUsd(report);
|
|
3031
|
-
const outDir = opts.out ||
|
|
3032
|
-
await
|
|
3033
|
-
const playbookPath =
|
|
3092
|
+
const outDir = opts.out || join5(process.cwd(), "costclaw-out");
|
|
3093
|
+
await mkdir2(outDir, { recursive: true });
|
|
3094
|
+
const playbookPath = join5(outDir, PLAYBOOK_NAME);
|
|
3034
3095
|
await writeFile2(playbookPath, result.playbook, "utf8");
|
|
3035
|
-
const settingsPath =
|
|
3096
|
+
const settingsPath = join5(outDir, SETTINGS_NAME);
|
|
3036
3097
|
await writeFile2(settingsPath, result.settingsJson, "utf8");
|
|
3037
3098
|
const lines = [];
|
|
3038
3099
|
lines.push("CostClaw optimize");
|
|
@@ -3062,7 +3123,7 @@ async function runOptimize(opts) {
|
|
|
3062
3123
|
lines.push("Warning: this scaffold has placeholders to fill in. Search it for <!-- ... --> and replace each with your project's real details.");
|
|
3063
3124
|
lines.push("A suggested settings.json was written to the out dir for review; it is not applied in place.");
|
|
3064
3125
|
} else {
|
|
3065
|
-
const optimizedPath =
|
|
3126
|
+
const optimizedPath = join5(outDir, OPTIMIZED_NAME);
|
|
3066
3127
|
await writeFile2(optimizedPath, result.optimizedClaudeMd, "utf8");
|
|
3067
3128
|
lines.push("Dry run (no files of yours were changed).");
|
|
3068
3129
|
lines.push(`Wrote the optimized CLAUDE.md scaffold to ${optimizedPath}`);
|
|
@@ -3079,7 +3140,7 @@ async function runOptimize(opts) {
|
|
|
3079
3140
|
lines.push("To merge it into the project's .claude/settings.json for you (with a backup and a diff first), rerun with --apply-settings.");
|
|
3080
3141
|
}
|
|
3081
3142
|
if (opts.applySettings) {
|
|
3082
|
-
const target = opts.applySettingsPath ||
|
|
3143
|
+
const target = opts.applySettingsPath || join5(projectDir, ".claude", "settings.json");
|
|
3083
3144
|
const applyLines = await applyDerivedSettings(target, result.settingsJson, opts.force ?? false);
|
|
3084
3145
|
lines.push("");
|
|
3085
3146
|
lines.push(...applyLines);
|
|
@@ -3126,9 +3187,9 @@ async function runBadge(opts) {
|
|
|
3126
3187
|
}
|
|
3127
3188
|
|
|
3128
3189
|
// src/license.ts
|
|
3129
|
-
import { mkdir as
|
|
3130
|
-
import { join as
|
|
3131
|
-
import { homedir as
|
|
3190
|
+
import { mkdir as mkdir3, readFile as readFile5, writeFile as writeFile4, rm } from "fs/promises";
|
|
3191
|
+
import { join as join6 } from "path";
|
|
3192
|
+
import { homedir as homedir4, hostname } from "os";
|
|
3132
3193
|
import { createPublicKey, verify as cryptoVerify } from "crypto";
|
|
3133
3194
|
var LICENSE_PRODUCT = "costclaw-optimize";
|
|
3134
3195
|
var DEFAULT_PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY-----
|
|
@@ -3153,10 +3214,10 @@ var INVALID_MESSAGE = [
|
|
|
3153
3214
|
"costclaw audit stays free."
|
|
3154
3215
|
].join("\n");
|
|
3155
3216
|
function defaultConfigDir() {
|
|
3156
|
-
return
|
|
3217
|
+
return join6(homedir4(), ".costclaw");
|
|
3157
3218
|
}
|
|
3158
3219
|
function licenseFile(dir) {
|
|
3159
|
-
return
|
|
3220
|
+
return join6(dir, "license.json");
|
|
3160
3221
|
}
|
|
3161
3222
|
function base64UrlDecode(part) {
|
|
3162
3223
|
return Buffer.from(part.replace(/-/g, "+").replace(/_/g, "/"), "base64");
|
|
@@ -3188,7 +3249,7 @@ async function readStoredLicense(dir) {
|
|
|
3188
3249
|
}
|
|
3189
3250
|
}
|
|
3190
3251
|
async function writeStoredLicense(dir, lic) {
|
|
3191
|
-
await
|
|
3252
|
+
await mkdir3(dir, { recursive: true });
|
|
3192
3253
|
await writeFile4(licenseFile(dir), JSON.stringify(lic, null, 2) + "\n", "utf8");
|
|
3193
3254
|
}
|
|
3194
3255
|
async function clearStoredLicense(dir) {
|
|
@@ -3268,7 +3329,7 @@ function parseBadgeFlags(args) {
|
|
|
3268
3329
|
};
|
|
3269
3330
|
}
|
|
3270
3331
|
function cliVersion() {
|
|
3271
|
-
return true ? "0.2.
|
|
3332
|
+
return true ? "0.2.3" : "0.0.0-dev";
|
|
3272
3333
|
}
|
|
3273
3334
|
function helpText() {
|
|
3274
3335
|
return [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "costclaw",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
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": {
|