costclaw 0.9.0 → 0.9.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 +3 -1
- package/dist/index.js +313 -213
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -113,7 +113,9 @@ costclaw audit [--path <dir>] [--claude-md <file>] [--plan <plan>] [--json] [--h
|
|
|
113
113
|
disable). `--html <file>` picks the path; `--no-html` skips the file;
|
|
114
114
|
`--json` runs stay file-free unless `--html` is passed explicitly. Set
|
|
115
115
|
`COSTCLAW_NO_OPEN=1` to write reports without launching a browser (CI and
|
|
116
|
-
test runners are detected and never open one).
|
|
116
|
+
test runners are detected and never open one). The report ends with a
|
|
117
|
+
"What to do next" ramp: the commands to run next, how the optimize license
|
|
118
|
+
works, and a command reference.
|
|
117
119
|
- `--card [file]` also write a shareable score card SVG (default
|
|
118
120
|
`costclaw-card.svg`): your score, band, recoverable spend, cache hit rate, and
|
|
119
121
|
strongest pillar. A static file, no fonts or network calls.
|
package/dist/index.js
CHANGED
|
@@ -1133,7 +1133,7 @@ function hasAnyDuration(report) {
|
|
|
1133
1133
|
}
|
|
1134
1134
|
return false;
|
|
1135
1135
|
}
|
|
1136
|
-
function buildChecks(report,
|
|
1136
|
+
function buildChecks(report, cmd2, answers, setup) {
|
|
1137
1137
|
const checks = [];
|
|
1138
1138
|
const s = report.summary;
|
|
1139
1139
|
checks.push(
|
|
@@ -1141,8 +1141,8 @@ function buildChecks(report, cmd, answers, setup) {
|
|
|
1141
1141
|
"cmd_size",
|
|
1142
1142
|
"claude_md",
|
|
1143
1143
|
"Size",
|
|
1144
|
-
|
|
1145
|
-
|
|
1144
|
+
cmd2.present ? cmd2.sizeScore : null,
|
|
1145
|
+
cmd2.present ? `Your CLAUDE.md is ${cmd2.chars.toLocaleString()} characters, about ${cmd2.estTokens.toLocaleString()} tokens loaded on every turn.` : "No CLAUDE.md was provided.",
|
|
1146
1146
|
"claudemd"
|
|
1147
1147
|
)
|
|
1148
1148
|
);
|
|
@@ -1151,8 +1151,8 @@ function buildChecks(report, cmd, answers, setup) {
|
|
|
1151
1151
|
"cmd_structure",
|
|
1152
1152
|
"claude_md",
|
|
1153
1153
|
"Structure",
|
|
1154
|
-
|
|
1155
|
-
|
|
1154
|
+
cmd2.present ? cmd2.structureScore : null,
|
|
1155
|
+
cmd2.present ? `${cmd2.headings} heading${cmd2.headings === 1 ? "" : "s"} and ${cmd2.bullets} bullet point${cmd2.bullets === 1 ? "" : "s"} across ${cmd2.chars.toLocaleString()} characters.` : "No CLAUDE.md was provided.",
|
|
1156
1156
|
"claudemd"
|
|
1157
1157
|
)
|
|
1158
1158
|
);
|
|
@@ -1161,8 +1161,8 @@ function buildChecks(report, cmd, answers, setup) {
|
|
|
1161
1161
|
"cmd_stability",
|
|
1162
1162
|
"claude_md",
|
|
1163
1163
|
"Cache stability",
|
|
1164
|
-
|
|
1165
|
-
|
|
1164
|
+
cmd2.present ? cmd2.stabilityScore : null,
|
|
1165
|
+
cmd2.present ? `${cmd2.volatileMarkers} volatile marker${cmd2.volatileMarkers === 1 ? "" : "s"} (dates, TODOs, "updated" notes) that change between sessions and reset the cache.` : "No CLAUDE.md was provided.",
|
|
1166
1166
|
"claudemd"
|
|
1167
1167
|
)
|
|
1168
1168
|
);
|
|
@@ -1384,8 +1384,8 @@ function buildChecks(report, cmd, answers, setup) {
|
|
|
1384
1384
|
checks.push(questionnaireCheck("model_choice", "Model fit", answers));
|
|
1385
1385
|
return checks;
|
|
1386
1386
|
}
|
|
1387
|
-
function scorePillars(report,
|
|
1388
|
-
const checks = buildChecks(report,
|
|
1387
|
+
function scorePillars(report, cmd2, answers, setup) {
|
|
1388
|
+
const checks = buildChecks(report, cmd2, answers, setup);
|
|
1389
1389
|
const pillars = PILLAR_ORDER.map((id) => {
|
|
1390
1390
|
const cfg = PILLARS[id];
|
|
1391
1391
|
const pillarChecks = checks.filter((c) => c.pillar === id);
|
|
@@ -1484,11 +1484,11 @@ function severityRank(s) {
|
|
|
1484
1484
|
function num2(n) {
|
|
1485
1485
|
return String(Math.round(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
1486
1486
|
}
|
|
1487
|
-
function sizeFix(
|
|
1488
|
-
if (
|
|
1487
|
+
function sizeFix(cmd2) {
|
|
1488
|
+
if (cmd2.chars > 6e3) {
|
|
1489
1489
|
return {
|
|
1490
1490
|
title: "Trim your CLAUDE.md",
|
|
1491
|
-
action: `Your CLAUDE.md is an estimated ${num2(
|
|
1491
|
+
action: `Your CLAUDE.md is an estimated ${num2(cmd2.estTokens)} tokens (${num2(cmd2.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.`
|
|
1492
1492
|
};
|
|
1493
1493
|
}
|
|
1494
1494
|
return {
|
|
@@ -1496,7 +1496,7 @@ function sizeFix(cmd) {
|
|
|
1496
1496
|
action: "Your CLAUDE.md is thin. Add the conventions, commands, and constraints the agent keeps getting wrong so it stops re-deriving them every session."
|
|
1497
1497
|
};
|
|
1498
1498
|
}
|
|
1499
|
-
function synthesize(report, pillars,
|
|
1499
|
+
function synthesize(report, pillars, cmd2, plan = "api") {
|
|
1500
1500
|
const headlineWasteUsd = cacheMissExposureUsd(report);
|
|
1501
1501
|
const headlineWasteLabel = plan === "api" ? "Recoverable spend (cache-miss exposure)" : "Recoverable usage (cache-miss exposure)";
|
|
1502
1502
|
const spend = report.summary.cost_usd;
|
|
@@ -1576,8 +1576,8 @@ function synthesize(report, pillars, cmd, plan = "api") {
|
|
|
1576
1576
|
let action;
|
|
1577
1577
|
if (marathonBest && marathonUsd >= 0.25 * exposure && marathonBest.lateHit < marathonBest.earlyHit) {
|
|
1578
1578
|
action = `The longest sessions started at ${pct2(marathonBest.earlyHit)}% cache reuse and finished at ${pct2(marathonBest.lateHit)}%. Compact or start a fresh session before the context outgrows the cache: the later turns re-pay for context you already bought.`;
|
|
1579
|
-
} else if (
|
|
1580
|
-
action = `Your CLAUDE.md carries ${
|
|
1579
|
+
} else if (cmd2.present && cmd2.stabilityScore != null && cmd2.stabilityScore < GAP_THRESHOLD && cmd2.volatileMarkers >= 5) {
|
|
1580
|
+
action = `Your CLAUDE.md carries ${cmd2.volatileMarkers} volatile markers (dates, TODOs, status notes). Every edit invalidates the cached prefix, so the next session re-pays full price for the whole file. Move the churn into a scratch note the agent opens on demand.`;
|
|
1581
1581
|
} else if (badCacheBest) {
|
|
1582
1582
|
action = `The last ${badCacheBest.sessionsConsidered} sessions in ${badCacheBest.slug} reused only ${pct2(badCacheBest.recentHitRate)}% from the cache. Open each session the same way (same CLAUDE.md, same first reads) so turn two onward reads the prefix at about a tenth of the input price.`;
|
|
1583
1583
|
} else {
|
|
@@ -1691,7 +1691,7 @@ function synthesize(report, pillars, cmd, plan = "api") {
|
|
|
1691
1691
|
for (const p of pillars) {
|
|
1692
1692
|
for (const c of p.checks) {
|
|
1693
1693
|
if (!c.gap || covered.has(c.id)) continue;
|
|
1694
|
-
const tmpl = c.id === "cmd_size" ? sizeFix(
|
|
1694
|
+
const tmpl = c.id === "cmd_size" ? sizeFix(cmd2) : CHECK_FIX[c.id];
|
|
1695
1695
|
if (!tmpl) continue;
|
|
1696
1696
|
const severity = (c.score ?? 100) < 40 ? "warn" : "info";
|
|
1697
1697
|
fixes.push({
|
|
@@ -1991,7 +1991,7 @@ var NOTE_MAX = 80;
|
|
|
1991
1991
|
function extractCommandNotes(body) {
|
|
1992
1992
|
const notes = /* @__PURE__ */ new Map();
|
|
1993
1993
|
if (!body) return notes;
|
|
1994
|
-
const keyOf = (
|
|
1994
|
+
const keyOf = (cmd2) => cmd2.trim().replace(/\s+/g, " ");
|
|
1995
1995
|
for (const raw of body.split("\n")) {
|
|
1996
1996
|
const line = raw.trim();
|
|
1997
1997
|
if (!line || line.startsWith("```") || line.startsWith("#")) continue;
|
|
@@ -2551,9 +2551,9 @@ function buildSettings(project) {
|
|
|
2551
2551
|
const projectAllow = /* @__PURE__ */ new Set();
|
|
2552
2552
|
for (const c of project?.commands ?? []) {
|
|
2553
2553
|
if (!SAFE_COMMAND_LABELS.has(c.label)) continue;
|
|
2554
|
-
const
|
|
2555
|
-
if (!
|
|
2556
|
-
projectAllow.add(`Bash(${
|
|
2554
|
+
const cmd2 = c.command.trim();
|
|
2555
|
+
if (!cmd2 || cmd2.includes("<")) continue;
|
|
2556
|
+
projectAllow.add(`Bash(${cmd2}:*)`);
|
|
2557
2557
|
}
|
|
2558
2558
|
const allow = [...BASE_ALLOW, ...[...projectAllow].sort()];
|
|
2559
2559
|
const settings = { permissions: { allow, deny: [...BASE_DENY] } };
|
|
@@ -2868,10 +2868,10 @@ function cleanPillar(p) {
|
|
|
2868
2868
|
}
|
|
2869
2869
|
function buildAudit(input, opts = {}) {
|
|
2870
2870
|
const report = analyzeUploads(input.logs);
|
|
2871
|
-
const
|
|
2872
|
-
const { pillars, overall, band } = scorePillars(report,
|
|
2871
|
+
const cmd2 = analyzeClaudeMd(input.claudeMd);
|
|
2872
|
+
const { pillars, overall, band } = scorePillars(report, cmd2, input.questionnaire, input.setup);
|
|
2873
2873
|
const plan = input.plan ?? "api";
|
|
2874
|
-
const { headlineWasteUsd, headlineWasteLabel, headlineWasteShare, fixes } = synthesize(report, pillars,
|
|
2874
|
+
const { headlineWasteUsd, headlineWasteLabel, headlineWasteShare, fixes } = synthesize(report, pillars, cmd2, plan);
|
|
2875
2875
|
const answered = Object.values(input.questionnaire).filter((v) => typeof v === "string" && v.length > 0).length;
|
|
2876
2876
|
return {
|
|
2877
2877
|
schemaVersion: "1",
|
|
@@ -2880,8 +2880,8 @@ function buildAudit(input, opts = {}) {
|
|
|
2880
2880
|
contactEmail: input.contactEmail?.trim() || null,
|
|
2881
2881
|
inputsSummary: {
|
|
2882
2882
|
logFiles: input.logs.length,
|
|
2883
|
-
hasClaudeMd:
|
|
2884
|
-
claudeMdChars:
|
|
2883
|
+
hasClaudeMd: cmd2.present,
|
|
2884
|
+
claudeMdChars: cmd2.chars,
|
|
2885
2885
|
questionnaireAnswered: answered
|
|
2886
2886
|
},
|
|
2887
2887
|
usageMetrics: toUsageMetrics(report),
|
|
@@ -3021,9 +3021,9 @@ var MAX_TOTAL_BYTES = 60 * 1024 * 1024;
|
|
|
3021
3021
|
var MAX_CLAUDEMD_BYTES = 1 * 1024 * 1024;
|
|
3022
3022
|
|
|
3023
3023
|
// src/audit-command.ts
|
|
3024
|
-
import { mkdir as
|
|
3025
|
-
import { join as
|
|
3026
|
-
import { homedir as
|
|
3024
|
+
import { mkdir as mkdir3, readFile as readFile4, readdir as readdir2, rm as rm2, writeFile as writeFile3 } from "fs/promises";
|
|
3025
|
+
import { join as join5, resolve } from "path";
|
|
3026
|
+
import { homedir as homedir4 } from "os";
|
|
3027
3027
|
|
|
3028
3028
|
// src/open.ts
|
|
3029
3029
|
import { spawn } from "child_process";
|
|
@@ -3031,8 +3031,8 @@ function openInBrowser(target) {
|
|
|
3031
3031
|
if (process.env["VITEST"] || process.env["CI"] || process.env["COSTCLAW_NO_OPEN"]) return;
|
|
3032
3032
|
try {
|
|
3033
3033
|
const platform = process.platform;
|
|
3034
|
-
const [
|
|
3035
|
-
const child = spawn(
|
|
3034
|
+
const [cmd2, args] = platform === "win32" ? ["cmd", ["/c", "start", "", target]] : platform === "darwin" ? ["open", [target]] : ["xdg-open", [target]];
|
|
3035
|
+
const child = spawn(cmd2, args, { detached: true, stdio: "ignore" });
|
|
3036
3036
|
child.on("error", () => {
|
|
3037
3037
|
});
|
|
3038
3038
|
child.unref();
|
|
@@ -3539,6 +3539,61 @@ function dollarFixCard(f) {
|
|
|
3539
3539
|
function otherFixCard(f) {
|
|
3540
3540
|
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>`;
|
|
3541
3541
|
}
|
|
3542
|
+
var SITE_URL = "https://costclaw.io";
|
|
3543
|
+
var PRICING_URL = "https://costclaw.io/#pricing";
|
|
3544
|
+
var CHECKOUT_URL = "https://buy.stripe.com/00wfZhgCH3X62v0cX9ew807";
|
|
3545
|
+
var SUPPORT_EMAIL = "wes@dashclaw.io";
|
|
3546
|
+
var LICENSE_PRICE = "$39";
|
|
3547
|
+
function cmd(c) {
|
|
3548
|
+
return `<pre class="cmd"><code>${esc(c)}</code></pre>`;
|
|
3549
|
+
}
|
|
3550
|
+
function step(n, title, body) {
|
|
3551
|
+
return `<li class="step"><div class="step-n">${n}</div><div class="step-body"><h3>${esc(title)}</h3>${body}</div></li>`;
|
|
3552
|
+
}
|
|
3553
|
+
function nextStepsSection(record, meta) {
|
|
3554
|
+
const licensed = meta.license?.active === true;
|
|
3555
|
+
const steps = [];
|
|
3556
|
+
let n = 1;
|
|
3557
|
+
if (meta.planSource === "default") {
|
|
3558
|
+
steps.push(step(
|
|
3559
|
+
n++,
|
|
3560
|
+
"Tell costclaw how you pay",
|
|
3561
|
+
`<p>On a Claude subscription the dollar figures above are token value, not money back. Declare your plan once and the report reframes everything as usage you win back.</p>` + cmd("npx costclaw audit --plan max20x # or pro, max5x, api")
|
|
3562
|
+
));
|
|
3563
|
+
}
|
|
3564
|
+
const waste = record.headlineWasteUsd;
|
|
3565
|
+
const wasteLine = waste > 0 ? `This audit found <strong>${esc(usd3(waste))}</strong> of recoverable input spend. ` : "";
|
|
3566
|
+
if (licensed) {
|
|
3567
|
+
const who = meta.license?.email ? ` (${esc(meta.license.email)})` : "";
|
|
3568
|
+
steps.push(step(
|
|
3569
|
+
n++,
|
|
3570
|
+
"Apply the fixes with optimize",
|
|
3571
|
+
`<p class="ok">License active on this machine${who}.</p><p>${wasteLine}Run optimize inside each project you work in. It scans the project, writes a filled-in CLAUDE.md, a ranked playbook, suggested settings, a work order, and a dashboard into <span class="mono">./costclaw-out</span>, then opens the dashboard.</p>` + cmd("cd <your-project>\nnpx costclaw optimize") + `<p>Happy with the dry run? Apply it in place (both back up the original and print a diff), then hand the work order to your own Claude Code:</p>` + cmd('npx costclaw optimize --write CLAUDE.md --apply-settings\nclaude "Follow costclaw-out/HARNESS-UPGRADE.md"') + `<p class="fine">Moving machines? <span class="mono">costclaw deactivate</span> here, <span class="mono">costclaw activate <key></span> there.</p>`
|
|
3572
|
+
));
|
|
3573
|
+
} else {
|
|
3574
|
+
steps.push(step(
|
|
3575
|
+
n++,
|
|
3576
|
+
"Apply the fixes with optimize",
|
|
3577
|
+
`<p>${wasteLine}<strong>costclaw optimize</strong> turns this audit into the files that recover it: a filled-in CLAUDE.md, a ranked fix playbook, a suggested <span class="mono">settings.json</span> allowlist, a work order your own Claude Code applies, and a dashboard showing your CLAUDE.md before and after.</p><div class="offer"><div class="offer-price"><span class="big">${esc(LICENSE_PRICE)}</span> one-time</div><ul class="offer-list"><li>Key emailed in about a minute</li><li>Verified offline on your machine, no license server</li><li>Every project, unlimited runs</li><li>Move it between machines with activate and deactivate</li></ul><a class="buy" href="${CHECKOUT_URL}">Buy the optimize license</a><p class="fine">Then, once the key arrives:</p>` + cmd("npx costclaw activate <key>\ncd <your-project>\nnpx costclaw optimize") + `<p class="fine">Questions or a key that will not activate: <a href="mailto:${SUPPORT_EMAIL}">${SUPPORT_EMAIL}</a>. The audit stays free either way.</p></div>`
|
|
3578
|
+
));
|
|
3579
|
+
}
|
|
3580
|
+
steps.push(step(
|
|
3581
|
+
n++,
|
|
3582
|
+
"Track the movement",
|
|
3583
|
+
`<p>Every audit snapshots the derived record locally. Rerun in a week and compare: new spend, the cache hit rate for just that period, pillar movement, and which fixes resolved.</p>` + cmd("npx costclaw audit\nnpx costclaw diff")
|
|
3584
|
+
));
|
|
3585
|
+
steps.push(step(
|
|
3586
|
+
n++,
|
|
3587
|
+
"Share the score",
|
|
3588
|
+
`<p>A 600x315 score card for a post, and a shields-style badge for a README. Both are static SVGs with only derived numbers.</p>` + cmd("npx costclaw audit --card\nnpx costclaw badge")
|
|
3589
|
+
));
|
|
3590
|
+
const reference = `<table class="ref"><tbody><tr><td class="mono">costclaw audit</td><td>Cost report, setup score, ranked fixes, this HTML report. Free.</td></tr><tr><td class="mono">costclaw audit --plan <p></td><td>Declare api, pro, max5x, or max20x; remembered for later runs.</td></tr><tr><td class="mono">costclaw diff</td><td>Compare the latest two audits, or two --json records. Free.</td></tr><tr><td class="mono">costclaw badge</td><td>Fresh audit plus a README badge SVG. Free.</td></tr><tr><td class="mono">costclaw optimize</td><td>Write the optimized CLAUDE.md, playbook, settings, work order, and dashboard. Licensed.</td></tr><tr><td class="mono">costclaw activate / deactivate</td><td>Install or move the license on this machine. Offline.</td></tr><tr><td class="mono">costclaw --help</td><td>Every flag, per command.</td></tr></tbody></table>`;
|
|
3591
|
+
return `<h2>What to do next</h2>
|
|
3592
|
+
<ol class="steps">${steps.join("")}</ol>
|
|
3593
|
+
<h3 class="ref-head">Command reference</h3>
|
|
3594
|
+
${reference}
|
|
3595
|
+
<p class="sub">Docs and pricing: <a href="${SITE_URL}">${SITE_URL.replace("https://", "")}</a>. A hosted dashboard with history and team views is in progress; the waitlist is on the <a href="${PRICING_URL}">pricing section</a>. It will only ever receive the derived record, never your logs.</p>`;
|
|
3596
|
+
}
|
|
3542
3597
|
var STYLE = `
|
|
3543
3598
|
:root {
|
|
3544
3599
|
color-scheme: light;
|
|
@@ -3647,8 +3702,49 @@ ul.fixes { list-style: none; margin: 0; padding: 0; }
|
|
|
3647
3702
|
.footer .gen { margin: var(--s2) 0 0; }
|
|
3648
3703
|
/* The timestamp is one token; never let it break across lines. */
|
|
3649
3704
|
.footer .gen .mono { white-space: nowrap; }
|
|
3705
|
+
/* Next-steps ramp */
|
|
3706
|
+
ol.steps { list-style: none; margin: 0; padding: 0; counter-reset: none; }
|
|
3707
|
+
.step {
|
|
3708
|
+
display: flex; gap: var(--s4); background: var(--card); border: 1px solid var(--line);
|
|
3709
|
+
border-radius: 10px; padding: var(--s4) var(--s5); margin: 0 0 var(--s3);
|
|
3710
|
+
}
|
|
3711
|
+
.step-n {
|
|
3712
|
+
flex: 0 0 28px; height: 28px; border-radius: 14px; background: var(--umber); color: var(--paper);
|
|
3713
|
+
font-family: var(--mono); font-weight: 700; font-size: 14px; display: flex; align-items: center; justify-content: center;
|
|
3714
|
+
}
|
|
3715
|
+
.step-body { flex: 1; min-width: 0; }
|
|
3716
|
+
.step-body h3 { font-size: 15px; margin: 3px 0 var(--s2); }
|
|
3717
|
+
.step-body p { margin: var(--s2) 0 0; font-size: 14px; max-width: 74ch; }
|
|
3718
|
+
.step-body .ok { color: #1f6b3a; font-weight: 600; }
|
|
3719
|
+
.step-body .fine { font-size: 13px; color: var(--muted); }
|
|
3720
|
+
.cmd {
|
|
3721
|
+
margin: var(--s3) 0 0; padding: var(--s3) var(--s4); background: #2a211d; color: #f4ebe5;
|
|
3722
|
+
border-radius: 8px; font-family: var(--mono); font-size: 13px; line-height: 1.6;
|
|
3723
|
+
overflow-x: auto; white-space: pre;
|
|
3724
|
+
}
|
|
3725
|
+
.cmd code { font-family: inherit; }
|
|
3726
|
+
.offer {
|
|
3727
|
+
margin: var(--s4) 0 0; padding: var(--s4) var(--s5); border: 1px solid var(--line);
|
|
3728
|
+
border-left: 4px solid var(--clay); border-radius: 10px; background: var(--paper);
|
|
3729
|
+
}
|
|
3730
|
+
.offer-price { font-size: 14px; color: var(--muted); }
|
|
3731
|
+
.offer-price .big { font-size: 28px; font-weight: 700; color: var(--umber); font-family: var(--mono); margin-right: var(--s2); }
|
|
3732
|
+
.offer-list { margin: var(--s3) 0 0; padding-left: 18px; font-size: 14px; }
|
|
3733
|
+
.offer-list li { margin: 2px 0; }
|
|
3734
|
+
.buy {
|
|
3735
|
+
display: inline-block; margin: var(--s4) 0 0; padding: 10px 18px; border-radius: 8px;
|
|
3736
|
+
background: linear-gradient(135deg, var(--umber), var(--clay)); color: var(--paper);
|
|
3737
|
+
font-weight: 700; text-decoration: none; font-size: 14px;
|
|
3738
|
+
}
|
|
3739
|
+
.buy:hover { filter: brightness(1.05); }
|
|
3740
|
+
a { color: var(--umber); }
|
|
3741
|
+
.ref-head { font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin: var(--s6) 0 var(--s3); }
|
|
3742
|
+
table.ref { width: 100%; border-collapse: collapse; font-size: 14px; }
|
|
3743
|
+
table.ref td { padding: var(--s2) var(--s3); border-top: 1px solid var(--line); vertical-align: top; }
|
|
3744
|
+
table.ref td:first-child { white-space: nowrap; color: var(--umber); font-size: 13px; }
|
|
3650
3745
|
@media (max-width: 560px) {
|
|
3651
3746
|
.wrap { padding: var(--s6) var(--s4) var(--s8); }
|
|
3747
|
+
table.ref td:first-child { white-space: normal; }
|
|
3652
3748
|
h1 { font-size: 24px; }
|
|
3653
3749
|
.banner .amount { font-size: 32px; }
|
|
3654
3750
|
.p-name { flex: 0 0 130px; font-size: 13px; }
|
|
@@ -3736,6 +3832,8 @@ ${dollarSection}
|
|
|
3736
3832
|
${otherSection}
|
|
3737
3833
|
${skippedNote}
|
|
3738
3834
|
|
|
3835
|
+
${nextStepsSection(record, meta)}
|
|
3836
|
+
|
|
3739
3837
|
<div class="footer">
|
|
3740
3838
|
<strong>This report was generated locally on your machine.</strong> Nothing was uploaded:
|
|
3741
3839
|
your prompts, file contents, and paths never left this computer.
|
|
@@ -3870,6 +3968,125 @@ async function saveCliConfig(dir, patch) {
|
|
|
3870
3968
|
await writeFile(configFile(dir), JSON.stringify(merged, null, 2), "utf8");
|
|
3871
3969
|
}
|
|
3872
3970
|
|
|
3971
|
+
// src/license.ts
|
|
3972
|
+
import { mkdir as mkdir2, readFile as readFile3, writeFile as writeFile2, rm } from "fs/promises";
|
|
3973
|
+
import { join as join4 } from "path";
|
|
3974
|
+
import { homedir as homedir3, hostname } from "os";
|
|
3975
|
+
|
|
3976
|
+
// ../../packages/license/src/keys.ts
|
|
3977
|
+
var LICENSE_PRODUCT = "costclaw-optimize";
|
|
3978
|
+
var KEY_PREFIX = "CC1";
|
|
3979
|
+
var DEFAULT_PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY-----
|
|
3980
|
+
MCowBQYDK2VwAyEAXKdEIiMqeB4HIUb8si3LxRPBnrDpCIJrWvJWUCQLROg=
|
|
3981
|
+
-----END PUBLIC KEY-----`;
|
|
3982
|
+
|
|
3983
|
+
// ../../packages/license/src/verify.ts
|
|
3984
|
+
import { createPublicKey, verify as cryptoVerify } from "crypto";
|
|
3985
|
+
function base64UrlDecode(part) {
|
|
3986
|
+
return Buffer.from(part.replace(/-/g, "+").replace(/_/g, "/"), "base64");
|
|
3987
|
+
}
|
|
3988
|
+
function verifyLicenseKey(licenseKey, publicKeyPem = DEFAULT_PUBLIC_KEY_PEM) {
|
|
3989
|
+
const parts = licenseKey.trim().split(".");
|
|
3990
|
+
if (parts.length !== 3 || parts[0] !== KEY_PREFIX) return null;
|
|
3991
|
+
const [prefix, payloadPart, signaturePart] = parts;
|
|
3992
|
+
if (prefix === void 0 || payloadPart === void 0 || signaturePart === void 0) return null;
|
|
3993
|
+
try {
|
|
3994
|
+
const publicKey = createPublicKey(publicKeyPem);
|
|
3995
|
+
const signed = Buffer.from(`${prefix}.${payloadPart}`, "utf8");
|
|
3996
|
+
const signature = base64UrlDecode(signaturePart);
|
|
3997
|
+
if (!cryptoVerify(null, signed, publicKey, signature)) return null;
|
|
3998
|
+
const payload = JSON.parse(base64UrlDecode(payloadPart).toString("utf8"));
|
|
3999
|
+
if (payload.p !== LICENSE_PRODUCT) return null;
|
|
4000
|
+
return payload;
|
|
4001
|
+
} catch {
|
|
4002
|
+
return null;
|
|
4003
|
+
}
|
|
4004
|
+
}
|
|
4005
|
+
|
|
4006
|
+
// ../../packages/license/src/mint.ts
|
|
4007
|
+
import { createPrivateKey, sign } from "crypto";
|
|
4008
|
+
|
|
4009
|
+
// ../../packages/license/src/stripe.ts
|
|
4010
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
4011
|
+
|
|
4012
|
+
// src/license.ts
|
|
4013
|
+
var NOT_ACTIVATED_MESSAGE = [
|
|
4014
|
+
"costclaw optimize needs a license.",
|
|
4015
|
+
"",
|
|
4016
|
+
"Buy a one-time license at https://costclaw.io, then run:",
|
|
4017
|
+
" costclaw activate <your-license-key>",
|
|
4018
|
+
"",
|
|
4019
|
+
"costclaw audit is free and does not need a license."
|
|
4020
|
+
].join("\n");
|
|
4021
|
+
var INVALID_MESSAGE = [
|
|
4022
|
+
"The saved CostClaw license is not valid for this version.",
|
|
4023
|
+
"",
|
|
4024
|
+
"Re-activate with the key from your purchase email:",
|
|
4025
|
+
" costclaw activate <your-license-key>",
|
|
4026
|
+
"",
|
|
4027
|
+
"Key missing or not working? Email wes@dashclaw.io with your order number.",
|
|
4028
|
+
"costclaw audit stays free."
|
|
4029
|
+
].join("\n");
|
|
4030
|
+
function defaultConfigDir() {
|
|
4031
|
+
return join4(homedir3(), ".costclaw");
|
|
4032
|
+
}
|
|
4033
|
+
function licenseFile(dir) {
|
|
4034
|
+
return join4(dir, "license.json");
|
|
4035
|
+
}
|
|
4036
|
+
async function readStoredLicense(dir) {
|
|
4037
|
+
try {
|
|
4038
|
+
const data = JSON.parse(await readFile3(licenseFile(dir), "utf8"));
|
|
4039
|
+
if (!data || typeof data.licenseKey !== "string") return null;
|
|
4040
|
+
return data;
|
|
4041
|
+
} catch {
|
|
4042
|
+
return null;
|
|
4043
|
+
}
|
|
4044
|
+
}
|
|
4045
|
+
async function writeStoredLicense(dir, lic) {
|
|
4046
|
+
await mkdir2(dir, { recursive: true });
|
|
4047
|
+
await writeFile2(licenseFile(dir), JSON.stringify(lic, null, 2) + "\n", "utf8");
|
|
4048
|
+
}
|
|
4049
|
+
async function clearStoredLicense(dir) {
|
|
4050
|
+
await rm(licenseFile(dir), { force: true });
|
|
4051
|
+
}
|
|
4052
|
+
async function activateLicense(licenseKey, opts = {}) {
|
|
4053
|
+
const key = licenseKey.trim();
|
|
4054
|
+
if (!key) throw new Error("Provide your license key: costclaw activate <key>");
|
|
4055
|
+
const configDir = opts.configDir ?? defaultConfigDir();
|
|
4056
|
+
const payload = verifyLicenseKey(key, opts.publicKeyPem);
|
|
4057
|
+
if (!payload) {
|
|
4058
|
+
throw new Error(
|
|
4059
|
+
"Could not activate this license key: it is not a valid CostClaw optimize license.\nCopy the whole key from your purchase email, or buy one at https://costclaw.io."
|
|
4060
|
+
);
|
|
4061
|
+
}
|
|
4062
|
+
const stored = {
|
|
4063
|
+
licenseKey: key,
|
|
4064
|
+
instanceId: "local",
|
|
4065
|
+
instanceName: opts.instanceName?.trim() || hostname(),
|
|
4066
|
+
email: payload.e,
|
|
4067
|
+
activatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
4068
|
+
};
|
|
4069
|
+
await writeStoredLicense(configDir, stored);
|
|
4070
|
+
return stored;
|
|
4071
|
+
}
|
|
4072
|
+
async function checkLicense(opts = {}) {
|
|
4073
|
+
const configDir = opts.configDir ?? defaultConfigDir();
|
|
4074
|
+
const stored = await readStoredLicense(configDir);
|
|
4075
|
+
if (!stored) return { ok: false, message: NOT_ACTIVATED_MESSAGE };
|
|
4076
|
+
const payload = verifyLicenseKey(stored.licenseKey, opts.publicKeyPem);
|
|
4077
|
+
if (payload) {
|
|
4078
|
+
return { ok: true, email: payload.e ?? stored.email };
|
|
4079
|
+
}
|
|
4080
|
+
await clearStoredLicense(configDir);
|
|
4081
|
+
return { ok: false, message: INVALID_MESSAGE };
|
|
4082
|
+
}
|
|
4083
|
+
async function deactivateLicense(opts = {}) {
|
|
4084
|
+
const configDir = opts.configDir ?? defaultConfigDir();
|
|
4085
|
+
const stored = await readStoredLicense(configDir);
|
|
4086
|
+
if (!stored) throw new Error("No license is active on this machine, so there is nothing to deactivate.");
|
|
4087
|
+
await clearStoredLicense(configDir);
|
|
4088
|
+
}
|
|
4089
|
+
|
|
3873
4090
|
// src/audit-command.ts
|
|
3874
4091
|
async function resolvePlan(configDir, flag) {
|
|
3875
4092
|
if (flag) {
|
|
@@ -3896,12 +4113,12 @@ async function loadAuditInputs(opts) {
|
|
|
3896
4113
|
let claudeMd;
|
|
3897
4114
|
let claudeMdSource = null;
|
|
3898
4115
|
if (opts.claudeMdPath) {
|
|
3899
|
-
claudeMd = await
|
|
4116
|
+
claudeMd = await readFile4(opts.claudeMdPath, "utf8");
|
|
3900
4117
|
claudeMdSource = resolve(opts.claudeMdPath);
|
|
3901
4118
|
} else {
|
|
3902
|
-
const fallback =
|
|
4119
|
+
const fallback = join5(opts.home ?? homedir4(), ".claude", "CLAUDE.md");
|
|
3903
4120
|
try {
|
|
3904
|
-
claudeMd = await
|
|
4121
|
+
claudeMd = await readFile4(fallback, "utf8");
|
|
3905
4122
|
claudeMdSource = fallback;
|
|
3906
4123
|
} catch {
|
|
3907
4124
|
}
|
|
@@ -3915,7 +4132,7 @@ async function runAudit(opts) {
|
|
|
3915
4132
|
home: opts.home
|
|
3916
4133
|
});
|
|
3917
4134
|
const setup = await readSetup();
|
|
3918
|
-
const configDir =
|
|
4135
|
+
const configDir = join5(opts.home ?? homedir4(), ".costclaw");
|
|
3919
4136
|
const { plan, planSource } = await resolvePlan(configDir, opts.plan);
|
|
3920
4137
|
const record = buildAudit({
|
|
3921
4138
|
logs,
|
|
@@ -3927,15 +4144,17 @@ async function runAudit(opts) {
|
|
|
3927
4144
|
});
|
|
3928
4145
|
const baseline = (await readCliConfig(configDir)).lastOptimize;
|
|
3929
4146
|
const sinceOptimize = baseline ? { at: baseline.at, result: sinceBaseline(baseline, record) } : void 0;
|
|
4147
|
+
const gate = await checkLicense({ configDir });
|
|
4148
|
+
const license = gate.ok ? { active: true, ...gate.email ? { email: gate.email } : {} } : { active: false };
|
|
3930
4149
|
let snapshotNotice = null;
|
|
3931
4150
|
try {
|
|
3932
|
-
const historyDir =
|
|
3933
|
-
await
|
|
4151
|
+
const historyDir = join5(opts.home ?? homedir4(), ".costclaw", "history");
|
|
4152
|
+
await mkdir3(historyDir, { recursive: true });
|
|
3934
4153
|
const name = record.createdAt.replace(/[:.]/g, "-") + ".json";
|
|
3935
|
-
await
|
|
4154
|
+
await writeFile3(join5(historyDir, name), JSON.stringify(record, null, 2), "utf8");
|
|
3936
4155
|
const snapshots = (await readdir2(historyDir)).filter((f) => f.endsWith(".json")).sort();
|
|
3937
4156
|
for (const stale of snapshots.slice(0, Math.max(0, snapshots.length - SNAPSHOT_KEEP))) {
|
|
3938
|
-
await
|
|
4157
|
+
await rm2(join5(historyDir, stale), { force: true });
|
|
3939
4158
|
}
|
|
3940
4159
|
snapshotNotice = "Snapshot saved for costclaw diff.";
|
|
3941
4160
|
} catch (err) {
|
|
@@ -3948,15 +4167,15 @@ async function runAudit(opts) {
|
|
|
3948
4167
|
if (snapshotNotice) notices.push(snapshotNotice);
|
|
3949
4168
|
if (opts.html) {
|
|
3950
4169
|
const target = resolve(opts.htmlPath || DEFAULT_HTML_FILE);
|
|
3951
|
-
const doc = renderHtml(record, { root, sessionCount: logs.length, skipped, claudeMdSource, planSource, sinceOptimize });
|
|
3952
|
-
await
|
|
4170
|
+
const doc = renderHtml(record, { root, sessionCount: logs.length, skipped, claudeMdSource, planSource, sinceOptimize, license });
|
|
4171
|
+
await writeFile3(target, doc, "utf8");
|
|
3953
4172
|
openInBrowser(target);
|
|
3954
4173
|
notices.push(`HTML report written to ${target}`);
|
|
3955
4174
|
}
|
|
3956
4175
|
if (opts.card) {
|
|
3957
4176
|
const target = resolve(opts.cardPath || DEFAULT_CARD_FILE);
|
|
3958
4177
|
const svg = renderCard(record);
|
|
3959
|
-
await
|
|
4178
|
+
await writeFile3(target, svg, "utf8");
|
|
3960
4179
|
notices.push(`Score card written to ${target}`);
|
|
3961
4180
|
}
|
|
3962
4181
|
if (opts.json) {
|
|
@@ -3968,9 +4187,9 @@ async function runAudit(opts) {
|
|
|
3968
4187
|
}
|
|
3969
4188
|
|
|
3970
4189
|
// src/optimize-command.ts
|
|
3971
|
-
import { mkdir as
|
|
3972
|
-
import { join as
|
|
3973
|
-
import { homedir as
|
|
4190
|
+
import { mkdir as mkdir4, writeFile as writeFile4, readFile as readFile6, stat as stat2 } from "fs/promises";
|
|
4191
|
+
import { join as join7, dirname } from "path";
|
|
4192
|
+
import { homedir as homedir5 } from "os";
|
|
3974
4193
|
|
|
3975
4194
|
// src/optimize-dashboard.ts
|
|
3976
4195
|
function usd5(n) {
|
|
@@ -4384,8 +4603,8 @@ No network calls, no telemetry, no model calls.</footer>
|
|
|
4384
4603
|
}
|
|
4385
4604
|
|
|
4386
4605
|
// src/project-scan.ts
|
|
4387
|
-
import { readdir as readdir3, readFile as
|
|
4388
|
-
import { join as
|
|
4606
|
+
import { readdir as readdir3, readFile as readFile5, stat } from "fs/promises";
|
|
4607
|
+
import { join as join6, basename } from "path";
|
|
4389
4608
|
var NOISE_DIRS = /* @__PURE__ */ new Set([
|
|
4390
4609
|
"node_modules",
|
|
4391
4610
|
".git",
|
|
@@ -4414,7 +4633,7 @@ var DIR_NOTES = {
|
|
|
4414
4633
|
};
|
|
4415
4634
|
async function readText(p) {
|
|
4416
4635
|
try {
|
|
4417
|
-
return await
|
|
4636
|
+
return await readFile5(p, "utf8");
|
|
4418
4637
|
} catch {
|
|
4419
4638
|
return null;
|
|
4420
4639
|
}
|
|
@@ -4524,7 +4743,7 @@ async function containsTestFiles(dir) {
|
|
|
4524
4743
|
const top = await listEntries(dir);
|
|
4525
4744
|
if (top.files.some((f) => TEST_FILE.test(f))) return true;
|
|
4526
4745
|
for (const sub of top.dirs) {
|
|
4527
|
-
const inner = await listEntries(
|
|
4746
|
+
const inner = await listEntries(join6(dir, sub));
|
|
4528
4747
|
if (inner.files.some((f) => TEST_FILE.test(f))) return true;
|
|
4529
4748
|
}
|
|
4530
4749
|
return false;
|
|
@@ -4534,14 +4753,14 @@ async function detectTestsLocation(dir, files, dirs) {
|
|
|
4534
4753
|
if (candidates.length > 0) {
|
|
4535
4754
|
const withTests = [];
|
|
4536
4755
|
for (const c of candidates) {
|
|
4537
|
-
if (await containsTestFiles(
|
|
4756
|
+
if (await containsTestFiles(join6(dir, c))) withTests.push(c);
|
|
4538
4757
|
}
|
|
4539
4758
|
const chosen = withTests.length > 0 ? withTests : candidates;
|
|
4540
4759
|
return `tests in ${chosen.map((c) => `${c}/`).join(" and ")}`;
|
|
4541
4760
|
}
|
|
4542
4761
|
if (files.some((f) => TEST_FILE.test(f))) return "tests alongside source (*.test.* / *.spec.*)";
|
|
4543
4762
|
if (dirs.includes("src")) {
|
|
4544
|
-
const sub = await listEntries(
|
|
4763
|
+
const sub = await listEntries(join6(dir, "src"));
|
|
4545
4764
|
if (sub.files.some((f) => TEST_FILE.test(f))) {
|
|
4546
4765
|
return "tests alongside source (*.test.* / *.spec.*)";
|
|
4547
4766
|
}
|
|
@@ -4624,7 +4843,7 @@ async function scanNode(dir, files, dirs, pkg, profile) {
|
|
|
4624
4843
|
if (oneFileCmd !== null && "test" in scripts) {
|
|
4625
4844
|
profile.commands.push({ label: "test one file", command: oneFileCmd });
|
|
4626
4845
|
}
|
|
4627
|
-
const tsconfig = parseJson(await readText(
|
|
4846
|
+
const tsconfig = parseJson(await readText(join6(dir, "tsconfig.json")));
|
|
4628
4847
|
if (tsconfig) {
|
|
4629
4848
|
const co = tsconfig.compilerOptions;
|
|
4630
4849
|
if (co && co.strict === true) pushUnique(profile.codeStyle, "TypeScript strict mode");
|
|
@@ -4727,7 +4946,7 @@ async function scanPython(dir, files, profile) {
|
|
|
4727
4946
|
const deps = [];
|
|
4728
4947
|
const tables = /* @__PURE__ */ new Set();
|
|
4729
4948
|
let hasPoetry = false;
|
|
4730
|
-
const pyprojectText = await readText(
|
|
4949
|
+
const pyprojectText = await readText(join6(dir, "pyproject.toml"));
|
|
4731
4950
|
if (pyprojectText !== null) {
|
|
4732
4951
|
const parsed = parsePyproject(pyprojectText);
|
|
4733
4952
|
name = parsed.name;
|
|
@@ -4737,7 +4956,7 @@ async function scanPython(dir, files, profile) {
|
|
|
4737
4956
|
hasPoetry = parsed.hasPoetry;
|
|
4738
4957
|
}
|
|
4739
4958
|
if (files.includes("requirements.txt")) {
|
|
4740
|
-
const reqText = await readText(
|
|
4959
|
+
const reqText = await readText(join6(dir, "requirements.txt"));
|
|
4741
4960
|
if (reqText !== null) for (const d of pyDepsFromRequirements(reqText)) pushUnique(deps, d);
|
|
4742
4961
|
}
|
|
4743
4962
|
if (name !== null && name.trim() !== "") profile.name = name;
|
|
@@ -4771,7 +4990,7 @@ async function scanPython(dir, files, profile) {
|
|
|
4771
4990
|
}
|
|
4772
4991
|
}
|
|
4773
4992
|
async function scanRust(dir, profile) {
|
|
4774
|
-
const text2 = await readText(
|
|
4993
|
+
const text2 = await readText(join6(dir, "Cargo.toml"));
|
|
4775
4994
|
pushUnique(profile.stack, "Rust");
|
|
4776
4995
|
profile.packageManager = "cargo";
|
|
4777
4996
|
if (text2 !== null) {
|
|
@@ -4797,7 +5016,7 @@ async function scanRust(dir, profile) {
|
|
|
4797
5016
|
profile.commands.push({ label: "lint", command: "cargo clippy" });
|
|
4798
5017
|
}
|
|
4799
5018
|
async function scanGo(dir, profile) {
|
|
4800
|
-
const text2 = await readText(
|
|
5019
|
+
const text2 = await readText(join6(dir, "go.mod"));
|
|
4801
5020
|
pushUnique(profile.stack, "Go");
|
|
4802
5021
|
profile.packageManager = "go";
|
|
4803
5022
|
if (text2 !== null) {
|
|
@@ -4862,7 +5081,7 @@ async function scanProject(dir) {
|
|
|
4862
5081
|
const profile = emptyProfile(basename(dir));
|
|
4863
5082
|
const { files, dirs } = await listEntries(dir);
|
|
4864
5083
|
if (files.includes("package.json")) {
|
|
4865
|
-
const pkg = parseJson(await readText(
|
|
5084
|
+
const pkg = parseJson(await readText(join6(dir, "package.json")));
|
|
4866
5085
|
if (pkg) await scanNode(dir, files, dirs, pkg, profile);
|
|
4867
5086
|
}
|
|
4868
5087
|
if (files.includes("pyproject.toml") || files.includes("requirements.txt") || files.includes("setup.py")) {
|
|
@@ -4875,7 +5094,7 @@ async function scanProject(dir) {
|
|
|
4875
5094
|
await scanGo(dir, profile);
|
|
4876
5095
|
}
|
|
4877
5096
|
if (files.includes("Makefile") && profile.commands.length === 0) {
|
|
4878
|
-
const text2 = await readText(
|
|
5097
|
+
const text2 = await readText(join6(dir, "Makefile"));
|
|
4879
5098
|
if (text2 !== null) {
|
|
4880
5099
|
for (const t of parseMakefile(text2)) {
|
|
4881
5100
|
profile.commands.push({ label: t, command: `make ${t}` });
|
|
@@ -4885,7 +5104,7 @@ async function scanProject(dir) {
|
|
|
4885
5104
|
if (profile.description === null || profile.name === basename(dir)) {
|
|
4886
5105
|
const readmeName = files.find((f) => /^readme(\.md|\.markdown|\.txt)?$/i.test(f));
|
|
4887
5106
|
if (readmeName) {
|
|
4888
|
-
const text2 = await readText(
|
|
5107
|
+
const text2 = await readText(join6(dir, readmeName));
|
|
4889
5108
|
if (text2 !== null) {
|
|
4890
5109
|
const parsed = parseReadme(text2);
|
|
4891
5110
|
if (profile.name === basename(dir) && parsed.name !== null) profile.name = parsed.name;
|
|
@@ -4898,7 +5117,7 @@ async function scanProject(dir) {
|
|
|
4898
5117
|
profile.structure = await detectStructure(dir, dirs);
|
|
4899
5118
|
const envFile = files.includes(".env.example") ? ".env.example" : files.includes(".env.sample") ? ".env.sample" : null;
|
|
4900
5119
|
if (envFile) {
|
|
4901
|
-
const text2 = await readText(
|
|
5120
|
+
const text2 = await readText(join6(dir, envFile));
|
|
4902
5121
|
if (text2 !== null) profile.envVars = parseEnvNames(text2);
|
|
4903
5122
|
}
|
|
4904
5123
|
return profile;
|
|
@@ -4986,7 +5205,7 @@ async function applyDerivedSettings(target, suggestedJson, force) {
|
|
|
4986
5205
|
let existingText = "";
|
|
4987
5206
|
let existing = {};
|
|
4988
5207
|
if (await pathExists(target)) {
|
|
4989
|
-
existingText = await
|
|
5208
|
+
existingText = await readFile6(target, "utf8");
|
|
4990
5209
|
if (existingText.trim() !== "") {
|
|
4991
5210
|
let parsed;
|
|
4992
5211
|
try {
|
|
@@ -5013,9 +5232,9 @@ async function applyDerivedSettings(target, suggestedJson, force) {
|
|
|
5013
5232
|
mergedPerms.deny = unionStrings(asStringArray(existingPerms.deny), suggestedDeny);
|
|
5014
5233
|
const merged = { ...existing, permissions: mergedPerms };
|
|
5015
5234
|
const mergedText = JSON.stringify(merged, null, 2) + "\n";
|
|
5016
|
-
await
|
|
5017
|
-
await
|
|
5018
|
-
await
|
|
5235
|
+
await mkdir4(dirname(target), { recursive: true });
|
|
5236
|
+
await writeFile4(backup, existingText, "utf8");
|
|
5237
|
+
await writeFile4(target, mergedText, "utf8");
|
|
5019
5238
|
return [
|
|
5020
5239
|
`Merged the derived permission allowlist into ${target}`,
|
|
5021
5240
|
`Backed up the previous file to ${backup}`,
|
|
@@ -5029,16 +5248,16 @@ async function runOptimize(opts) {
|
|
|
5029
5248
|
const projectDir = opts.projectDir || process.cwd();
|
|
5030
5249
|
let claudeMdPath = opts.claudeMdPath;
|
|
5031
5250
|
if (!claudeMdPath) {
|
|
5032
|
-
const projectClaudeMd =
|
|
5251
|
+
const projectClaudeMd = join7(projectDir, "CLAUDE.md");
|
|
5033
5252
|
if (await pathExists(projectClaudeMd)) claudeMdPath = projectClaudeMd;
|
|
5034
5253
|
}
|
|
5035
5254
|
const { logs, claudeMd, claudeMdSource } = await loadAuditInputs({ path: opts.path, claudeMdPath });
|
|
5036
5255
|
const report = analyzeUploads(logs);
|
|
5037
|
-
const
|
|
5038
|
-
const { pillars } = scorePillars(report,
|
|
5039
|
-
const configDir =
|
|
5256
|
+
const cmd2 = analyzeClaudeMd(claudeMd);
|
|
5257
|
+
const { pillars } = scorePillars(report, cmd2, {});
|
|
5258
|
+
const configDir = join7(opts.home ?? homedir5(), ".costclaw");
|
|
5040
5259
|
const { plan } = await resolvePlan(configDir, opts.plan);
|
|
5041
|
-
const { fixes } = synthesize(report, pillars,
|
|
5260
|
+
const { fixes } = synthesize(report, pillars, cmd2, plan);
|
|
5042
5261
|
const project = await scanProject(projectDir);
|
|
5043
5262
|
const result = optimize({ report, scores: pillars, fixes, currentClaudeMd: claudeMd, project });
|
|
5044
5263
|
const recoverable = cacheMissExposureUsd(report);
|
|
@@ -5052,14 +5271,14 @@ async function runOptimize(opts) {
|
|
|
5052
5271
|
spendUsd: baselineSpend
|
|
5053
5272
|
}
|
|
5054
5273
|
});
|
|
5055
|
-
const outDir = opts.out ||
|
|
5056
|
-
await
|
|
5057
|
-
const playbookPath =
|
|
5058
|
-
await
|
|
5059
|
-
const settingsPath =
|
|
5060
|
-
await
|
|
5061
|
-
const harnessPath =
|
|
5062
|
-
await
|
|
5274
|
+
const outDir = opts.out || join7(process.cwd(), "costclaw-out");
|
|
5275
|
+
await mkdir4(outDir, { recursive: true });
|
|
5276
|
+
const playbookPath = join7(outDir, PLAYBOOK_NAME2);
|
|
5277
|
+
await writeFile4(playbookPath, result.playbook, "utf8");
|
|
5278
|
+
const settingsPath = join7(outDir, SETTINGS_NAME2);
|
|
5279
|
+
await writeFile4(settingsPath, result.settingsJson, "utf8");
|
|
5280
|
+
const harnessPath = join7(outDir, HARNESS_NAME2);
|
|
5281
|
+
await writeFile4(harnessPath, result.harnessPrompt, "utf8");
|
|
5063
5282
|
const wantDashboard = opts.dashboard !== false;
|
|
5064
5283
|
let dashboardPath = null;
|
|
5065
5284
|
if (wantDashboard) {
|
|
@@ -5081,8 +5300,8 @@ async function runOptimize(opts) {
|
|
|
5081
5300
|
headlineWasteShare: spend > 0 ? recoverable / spend : 0,
|
|
5082
5301
|
headlineWasteUsd: recoverable
|
|
5083
5302
|
});
|
|
5084
|
-
dashboardPath =
|
|
5085
|
-
await
|
|
5303
|
+
dashboardPath = join7(outDir, DASHBOARD_NAME);
|
|
5304
|
+
await writeFile4(dashboardPath, html, "utf8");
|
|
5086
5305
|
}
|
|
5087
5306
|
const lines = [];
|
|
5088
5307
|
lines.push("CostClaw optimize");
|
|
@@ -5101,10 +5320,10 @@ async function runOptimize(opts) {
|
|
|
5101
5320
|
}
|
|
5102
5321
|
let original = "";
|
|
5103
5322
|
if (await pathExists(target)) {
|
|
5104
|
-
original = await
|
|
5323
|
+
original = await readFile6(target, "utf8");
|
|
5105
5324
|
}
|
|
5106
|
-
await
|
|
5107
|
-
await
|
|
5325
|
+
await writeFile4(backup, original, "utf8");
|
|
5326
|
+
await writeFile4(target, result.optimizedClaudeMd, "utf8");
|
|
5108
5327
|
lines.push(`Backed up the original to ${backup}`);
|
|
5109
5328
|
lines.push(`Wrote the optimized CLAUDE.md to ${target}`);
|
|
5110
5329
|
lines.push(`Wrote the playbook to ${playbookPath}`);
|
|
@@ -5119,8 +5338,8 @@ async function runOptimize(opts) {
|
|
|
5119
5338
|
}
|
|
5120
5339
|
lines.push("A suggested settings.json was written to the out dir for review; it is not applied in place.");
|
|
5121
5340
|
} else {
|
|
5122
|
-
const optimizedPath =
|
|
5123
|
-
await
|
|
5341
|
+
const optimizedPath = join7(outDir, OPTIMIZED_NAME2);
|
|
5342
|
+
await writeFile4(optimizedPath, result.optimizedClaudeMd, "utf8");
|
|
5124
5343
|
lines.push("Dry run (no files of yours were changed).");
|
|
5125
5344
|
lines.push(`Wrote the optimized CLAUDE.md scaffold to ${optimizedPath}`);
|
|
5126
5345
|
lines.push(`Wrote the playbook to ${playbookPath}`);
|
|
@@ -5172,7 +5391,7 @@ async function runOptimize(opts) {
|
|
|
5172
5391
|
lines.push("It merges the CLAUDE.md, applies the permissions you approve, and fixes the audit's harness-level findings. costclaw itself never calls a model.");
|
|
5173
5392
|
}
|
|
5174
5393
|
if (opts.applySettings) {
|
|
5175
|
-
const target = opts.applySettingsPath ||
|
|
5394
|
+
const target = opts.applySettingsPath || join7(projectDir, ".claude", "settings.json");
|
|
5176
5395
|
const applyLines = await applyDerivedSettings(target, result.settingsJson, opts.force ?? false);
|
|
5177
5396
|
lines.push("");
|
|
5178
5397
|
lines.push(...applyLines);
|
|
@@ -5195,7 +5414,7 @@ async function runOptimize(opts) {
|
|
|
5195
5414
|
}
|
|
5196
5415
|
|
|
5197
5416
|
// src/badge-command.ts
|
|
5198
|
-
import { writeFile as
|
|
5417
|
+
import { writeFile as writeFile5 } from "fs/promises";
|
|
5199
5418
|
import { resolve as resolve2 } from "path";
|
|
5200
5419
|
|
|
5201
5420
|
// src/badge.ts
|
|
@@ -5221,14 +5440,14 @@ async function runBadge(opts) {
|
|
|
5221
5440
|
const { record } = await runAudit({ path: opts.path, claudeMdPath: opts.claudeMdPath, home: opts.home });
|
|
5222
5441
|
const svg = renderBadge(record);
|
|
5223
5442
|
const target = resolve2(opts.out || DEFAULT_BADGE_FILE);
|
|
5224
|
-
await
|
|
5443
|
+
await writeFile5(target, svg, "utf8");
|
|
5225
5444
|
return { output: `Badge written to ${target}`, record, target };
|
|
5226
5445
|
}
|
|
5227
5446
|
|
|
5228
5447
|
// src/diff-command.ts
|
|
5229
|
-
import { readdir as readdir4, readFile as
|
|
5230
|
-
import { join as
|
|
5231
|
-
import { homedir as
|
|
5448
|
+
import { readdir as readdir4, readFile as readFile7 } from "fs/promises";
|
|
5449
|
+
import { join as join8 } from "path";
|
|
5450
|
+
import { homedir as homedir6 } from "os";
|
|
5232
5451
|
|
|
5233
5452
|
// src/format-diff.ts
|
|
5234
5453
|
function usd7(n) {
|
|
@@ -5316,7 +5535,7 @@ function formatDiff(d) {
|
|
|
5316
5535
|
async function readRecord(path) {
|
|
5317
5536
|
let text2;
|
|
5318
5537
|
try {
|
|
5319
|
-
text2 = await
|
|
5538
|
+
text2 = await readFile7(path, "utf8");
|
|
5320
5539
|
} catch {
|
|
5321
5540
|
throw new Error(`Could not read ${path}.`);
|
|
5322
5541
|
}
|
|
@@ -5332,7 +5551,7 @@ async function runDiff(opts) {
|
|
|
5332
5551
|
let pathA = opts.fileA;
|
|
5333
5552
|
let pathB = opts.fileB;
|
|
5334
5553
|
if (!pathA || !pathB) {
|
|
5335
|
-
const dir =
|
|
5554
|
+
const dir = join8(opts.home ?? homedir6(), ".costclaw", "history");
|
|
5336
5555
|
let files = [];
|
|
5337
5556
|
try {
|
|
5338
5557
|
files = (await readdir4(dir)).filter((f) => f.endsWith(".json")).sort();
|
|
@@ -5343,133 +5562,14 @@ async function runDiff(opts) {
|
|
|
5343
5562
|
"Need two audits to diff. Run costclaw audit now and again after you have made changes; each run saves a snapshot."
|
|
5344
5563
|
);
|
|
5345
5564
|
}
|
|
5346
|
-
pathA =
|
|
5347
|
-
pathB =
|
|
5565
|
+
pathA = join8(dir, files[files.length - 2]);
|
|
5566
|
+
pathB = join8(dir, files[files.length - 1]);
|
|
5348
5567
|
}
|
|
5349
5568
|
const diff = diffAudits(await readRecord(pathA), await readRecord(pathB));
|
|
5350
5569
|
const output = opts.json ? JSON.stringify(diff, null, 2) : formatDiff(diff);
|
|
5351
5570
|
return { output, diff };
|
|
5352
5571
|
}
|
|
5353
5572
|
|
|
5354
|
-
// src/license.ts
|
|
5355
|
-
import { mkdir as mkdir4, readFile as readFile7, writeFile as writeFile5, rm as rm2 } from "fs/promises";
|
|
5356
|
-
import { join as join8 } from "path";
|
|
5357
|
-
import { homedir as homedir6, hostname } from "os";
|
|
5358
|
-
|
|
5359
|
-
// ../../packages/license/src/keys.ts
|
|
5360
|
-
var LICENSE_PRODUCT = "costclaw-optimize";
|
|
5361
|
-
var KEY_PREFIX = "CC1";
|
|
5362
|
-
var DEFAULT_PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY-----
|
|
5363
|
-
MCowBQYDK2VwAyEAXKdEIiMqeB4HIUb8si3LxRPBnrDpCIJrWvJWUCQLROg=
|
|
5364
|
-
-----END PUBLIC KEY-----`;
|
|
5365
|
-
|
|
5366
|
-
// ../../packages/license/src/verify.ts
|
|
5367
|
-
import { createPublicKey, verify as cryptoVerify } from "crypto";
|
|
5368
|
-
function base64UrlDecode(part) {
|
|
5369
|
-
return Buffer.from(part.replace(/-/g, "+").replace(/_/g, "/"), "base64");
|
|
5370
|
-
}
|
|
5371
|
-
function verifyLicenseKey(licenseKey, publicKeyPem = DEFAULT_PUBLIC_KEY_PEM) {
|
|
5372
|
-
const parts = licenseKey.trim().split(".");
|
|
5373
|
-
if (parts.length !== 3 || parts[0] !== KEY_PREFIX) return null;
|
|
5374
|
-
const [prefix, payloadPart, signaturePart] = parts;
|
|
5375
|
-
if (prefix === void 0 || payloadPart === void 0 || signaturePart === void 0) return null;
|
|
5376
|
-
try {
|
|
5377
|
-
const publicKey = createPublicKey(publicKeyPem);
|
|
5378
|
-
const signed = Buffer.from(`${prefix}.${payloadPart}`, "utf8");
|
|
5379
|
-
const signature = base64UrlDecode(signaturePart);
|
|
5380
|
-
if (!cryptoVerify(null, signed, publicKey, signature)) return null;
|
|
5381
|
-
const payload = JSON.parse(base64UrlDecode(payloadPart).toString("utf8"));
|
|
5382
|
-
if (payload.p !== LICENSE_PRODUCT) return null;
|
|
5383
|
-
return payload;
|
|
5384
|
-
} catch {
|
|
5385
|
-
return null;
|
|
5386
|
-
}
|
|
5387
|
-
}
|
|
5388
|
-
|
|
5389
|
-
// ../../packages/license/src/mint.ts
|
|
5390
|
-
import { createPrivateKey, sign } from "crypto";
|
|
5391
|
-
|
|
5392
|
-
// ../../packages/license/src/stripe.ts
|
|
5393
|
-
import { createHmac, timingSafeEqual } from "crypto";
|
|
5394
|
-
|
|
5395
|
-
// src/license.ts
|
|
5396
|
-
var NOT_ACTIVATED_MESSAGE = [
|
|
5397
|
-
"costclaw optimize needs a license.",
|
|
5398
|
-
"",
|
|
5399
|
-
"Buy a one-time license at https://costclaw.io, then run:",
|
|
5400
|
-
" costclaw activate <your-license-key>",
|
|
5401
|
-
"",
|
|
5402
|
-
"costclaw audit is free and does not need a license."
|
|
5403
|
-
].join("\n");
|
|
5404
|
-
var INVALID_MESSAGE = [
|
|
5405
|
-
"The saved CostClaw license is not valid for this version.",
|
|
5406
|
-
"",
|
|
5407
|
-
"Re-activate with the key from your purchase email:",
|
|
5408
|
-
" costclaw activate <your-license-key>",
|
|
5409
|
-
"",
|
|
5410
|
-
"Key missing or not working? Email wes@dashclaw.io with your order number.",
|
|
5411
|
-
"costclaw audit stays free."
|
|
5412
|
-
].join("\n");
|
|
5413
|
-
function defaultConfigDir() {
|
|
5414
|
-
return join8(homedir6(), ".costclaw");
|
|
5415
|
-
}
|
|
5416
|
-
function licenseFile(dir) {
|
|
5417
|
-
return join8(dir, "license.json");
|
|
5418
|
-
}
|
|
5419
|
-
async function readStoredLicense(dir) {
|
|
5420
|
-
try {
|
|
5421
|
-
const data = JSON.parse(await readFile7(licenseFile(dir), "utf8"));
|
|
5422
|
-
if (!data || typeof data.licenseKey !== "string") return null;
|
|
5423
|
-
return data;
|
|
5424
|
-
} catch {
|
|
5425
|
-
return null;
|
|
5426
|
-
}
|
|
5427
|
-
}
|
|
5428
|
-
async function writeStoredLicense(dir, lic) {
|
|
5429
|
-
await mkdir4(dir, { recursive: true });
|
|
5430
|
-
await writeFile5(licenseFile(dir), JSON.stringify(lic, null, 2) + "\n", "utf8");
|
|
5431
|
-
}
|
|
5432
|
-
async function clearStoredLicense(dir) {
|
|
5433
|
-
await rm2(licenseFile(dir), { force: true });
|
|
5434
|
-
}
|
|
5435
|
-
async function activateLicense(licenseKey, opts = {}) {
|
|
5436
|
-
const key = licenseKey.trim();
|
|
5437
|
-
if (!key) throw new Error("Provide your license key: costclaw activate <key>");
|
|
5438
|
-
const configDir = opts.configDir ?? defaultConfigDir();
|
|
5439
|
-
const payload = verifyLicenseKey(key, opts.publicKeyPem);
|
|
5440
|
-
if (!payload) {
|
|
5441
|
-
throw new Error(
|
|
5442
|
-
"Could not activate this license key: it is not a valid CostClaw optimize license.\nCopy the whole key from your purchase email, or buy one at https://costclaw.io."
|
|
5443
|
-
);
|
|
5444
|
-
}
|
|
5445
|
-
const stored = {
|
|
5446
|
-
licenseKey: key,
|
|
5447
|
-
instanceId: "local",
|
|
5448
|
-
instanceName: opts.instanceName?.trim() || hostname(),
|
|
5449
|
-
email: payload.e,
|
|
5450
|
-
activatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
5451
|
-
};
|
|
5452
|
-
await writeStoredLicense(configDir, stored);
|
|
5453
|
-
return stored;
|
|
5454
|
-
}
|
|
5455
|
-
async function checkLicense(opts = {}) {
|
|
5456
|
-
const configDir = opts.configDir ?? defaultConfigDir();
|
|
5457
|
-
const stored = await readStoredLicense(configDir);
|
|
5458
|
-
if (!stored) return { ok: false, message: NOT_ACTIVATED_MESSAGE };
|
|
5459
|
-
const payload = verifyLicenseKey(stored.licenseKey, opts.publicKeyPem);
|
|
5460
|
-
if (payload) {
|
|
5461
|
-
return { ok: true, email: payload.e ?? stored.email };
|
|
5462
|
-
}
|
|
5463
|
-
await clearStoredLicense(configDir);
|
|
5464
|
-
return { ok: false, message: INVALID_MESSAGE };
|
|
5465
|
-
}
|
|
5466
|
-
async function deactivateLicense(opts = {}) {
|
|
5467
|
-
const configDir = opts.configDir ?? defaultConfigDir();
|
|
5468
|
-
const stored = await readStoredLicense(configDir);
|
|
5469
|
-
if (!stored) throw new Error("No license is active on this machine, so there is nothing to deactivate.");
|
|
5470
|
-
await clearStoredLicense(configDir);
|
|
5471
|
-
}
|
|
5472
|
-
|
|
5473
5573
|
// src/index.ts
|
|
5474
5574
|
process.on("unhandledRejection", (reason) => {
|
|
5475
5575
|
console.error("Unhandled rejection:", reason);
|
|
@@ -5517,7 +5617,7 @@ function parseBadgeFlags(args) {
|
|
|
5517
5617
|
};
|
|
5518
5618
|
}
|
|
5519
5619
|
function cliVersion() {
|
|
5520
|
-
return true ? "0.9.
|
|
5620
|
+
return true ? "0.9.1" : "0.0.0-dev";
|
|
5521
5621
|
}
|
|
5522
5622
|
function isHelpRequest(command, args) {
|
|
5523
5623
|
if (command === void 0) return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "costclaw",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.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": {
|