costclaw 0.5.0 → 0.5.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/dist/index.js +294 -84
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -13,8 +13,10 @@ function isPlan(value) {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
// ../../packages/engine/src/pricing.ts
|
|
16
|
-
var PRICING_SNAPSHOT_DATE = "2026-
|
|
16
|
+
var PRICING_SNAPSHOT_DATE = "2026-08-03";
|
|
17
17
|
var PRICES_PER_MTOK = Object.freeze({
|
|
18
|
+
"claude-opus-4-8": { input: 5, output: 25, cache_write: 6.25, cache_read: 0.5 },
|
|
19
|
+
"claude-sonnet-5": { input: 3, output: 15, cache_write: 3.75, cache_read: 0.3 },
|
|
18
20
|
"claude-opus-4-7": { input: 15, output: 75, cache_write: 18.75, cache_read: 1.5 },
|
|
19
21
|
"claude-opus-4-7[1m]": { input: 15, output: 75, cache_write: 18.75, cache_read: 1.5 },
|
|
20
22
|
"claude-opus-4-6": { input: 15, output: 75, cache_write: 18.75, cache_read: 1.5 },
|
|
@@ -25,14 +27,20 @@ var PRICES_PER_MTOK = Object.freeze({
|
|
|
25
27
|
});
|
|
26
28
|
var FALLBACK = { input: 3, output: 15, cache_write: 3.75, cache_read: 0.3 };
|
|
27
29
|
function priceFor(model) {
|
|
28
|
-
|
|
30
|
+
return lookup(model) ?? FALLBACK;
|
|
31
|
+
}
|
|
32
|
+
function isKnownModel(model) {
|
|
33
|
+
return lookup(model) !== null;
|
|
34
|
+
}
|
|
35
|
+
function lookup(model) {
|
|
36
|
+
if (!model) return null;
|
|
29
37
|
const exact = PRICES_PER_MTOK[model];
|
|
30
38
|
if (exact) return exact;
|
|
31
39
|
const noTag = model.replace(/\[[^\]]*\]$/, "");
|
|
32
40
|
if (PRICES_PER_MTOK[noTag]) return PRICES_PER_MTOK[noTag];
|
|
33
41
|
const noDate = noTag.replace(/-\d{8}$/, "");
|
|
34
42
|
if (PRICES_PER_MTOK[noDate]) return PRICES_PER_MTOK[noDate];
|
|
35
|
-
return
|
|
43
|
+
return null;
|
|
36
44
|
}
|
|
37
45
|
var num = (v) => Number(v) || 0;
|
|
38
46
|
function costForUsage(model, usage) {
|
|
@@ -67,6 +75,17 @@ var EM_OR_EN = /[—–]/;
|
|
|
67
75
|
function hasEmDash(text2) {
|
|
68
76
|
return EM_OR_EN.test(text2);
|
|
69
77
|
}
|
|
78
|
+
var PATH_ENCODED = /^-|^[A-Za-z]--|[\\/]/;
|
|
79
|
+
function slugLooksLikePath(slug) {
|
|
80
|
+
return PATH_ENCODED.test(slug);
|
|
81
|
+
}
|
|
82
|
+
function sanitizeProjectSlug(slug) {
|
|
83
|
+
if (slug == null) return null;
|
|
84
|
+
if (!slugLooksLikePath(slug)) return slug;
|
|
85
|
+
let h = 5381;
|
|
86
|
+
for (let i = 0; i < slug.length; i++) h = (h * 33 ^ slug.charCodeAt(i)) >>> 0;
|
|
87
|
+
return `project-${h.toString(36)}`;
|
|
88
|
+
}
|
|
70
89
|
function sanitizeText(input) {
|
|
71
90
|
let out = input;
|
|
72
91
|
out = out.replace(/[‘’‚‛]/g, "'");
|
|
@@ -180,7 +199,7 @@ function parseSession(text2, opts = {}) {
|
|
|
180
199
|
if (rec.type === "user") s.userRecords += 1;
|
|
181
200
|
else s.assistantRecords += 1;
|
|
182
201
|
const model = rec.message?.model;
|
|
183
|
-
if (model) modelCounts.set(model, (modelCounts.get(model) ?? 0) + 1);
|
|
202
|
+
if (model && model !== "<synthetic>") modelCounts.set(model, (modelCounts.get(model) ?? 0) + 1);
|
|
184
203
|
if (rec.type === "assistant") {
|
|
185
204
|
const { names, events } = extractTools(rec.message?.content);
|
|
186
205
|
for (const n of names) s.toolUsage[n] = (s.toolUsage[n] ?? 0) + 1;
|
|
@@ -634,7 +653,9 @@ function runOptimizer(ctx) {
|
|
|
634
653
|
function analyzeUploads(files) {
|
|
635
654
|
const sessions = files.map(
|
|
636
655
|
(f) => parseSession(f.text, {
|
|
637
|
-
|
|
656
|
+
// Path-encoded slugs are anonymized before grouping so no downstream
|
|
657
|
+
// consumer (optimizer prose, synthesize, the AuditRecord) ever sees one.
|
|
658
|
+
projectSlug: sanitizeProjectSlug(f.projectSlug ?? null),
|
|
638
659
|
kind: f.kind ?? "main",
|
|
639
660
|
parentSessionId: f.parentSessionId ?? null
|
|
640
661
|
})
|
|
@@ -1239,7 +1260,7 @@ function synthesize(report, pillars, cmd, plan = "api") {
|
|
|
1239
1260
|
};
|
|
1240
1261
|
const sevForUsd = (n) => n >= 50 ? "critical" : n >= 10 ? "warn" : "info";
|
|
1241
1262
|
const sWord = (k) => k === 1 ? "session" : "sessions";
|
|
1242
|
-
const exposureMaterial =
|
|
1263
|
+
const exposureMaterial = round23(exposure) > 0;
|
|
1243
1264
|
if (exposureMaterial) {
|
|
1244
1265
|
const ranked = report.projects.map((p) => {
|
|
1245
1266
|
let contrib = 0;
|
|
@@ -2133,7 +2154,7 @@ import { spawn } from "child_process";
|
|
|
2133
2154
|
|
|
2134
2155
|
// src/read-sessions.ts
|
|
2135
2156
|
import { readdir } from "fs/promises";
|
|
2136
|
-
import { readFileSync, openSync, closeSync } from "fs";
|
|
2157
|
+
import { readFileSync, openSync, closeSync, readSync } from "fs";
|
|
2137
2158
|
import { join, relative, sep } from "path";
|
|
2138
2159
|
import { homedir } from "os";
|
|
2139
2160
|
function defaultProjectsPath() {
|
|
@@ -2176,6 +2197,41 @@ function lazyLog(full, meta) {
|
|
|
2176
2197
|
}
|
|
2177
2198
|
};
|
|
2178
2199
|
}
|
|
2200
|
+
var CWD_PROBE_BYTES = 65536;
|
|
2201
|
+
var CWD_PROBE_LINES = 40;
|
|
2202
|
+
function projectNameFromLogs(files) {
|
|
2203
|
+
for (const full of files.slice(0, 3)) {
|
|
2204
|
+
let fd;
|
|
2205
|
+
try {
|
|
2206
|
+
fd = openSync(full, "r");
|
|
2207
|
+
} catch {
|
|
2208
|
+
continue;
|
|
2209
|
+
}
|
|
2210
|
+
try {
|
|
2211
|
+
const buf = Buffer.alloc(CWD_PROBE_BYTES);
|
|
2212
|
+
const n = readSync(fd, buf, 0, CWD_PROBE_BYTES, 0);
|
|
2213
|
+
const lines = buf.toString("utf8", 0, n).split("\n").slice(0, CWD_PROBE_LINES);
|
|
2214
|
+
for (const line of lines) {
|
|
2215
|
+
const trimmed = line.trim();
|
|
2216
|
+
if (!trimmed) continue;
|
|
2217
|
+
let rec;
|
|
2218
|
+
try {
|
|
2219
|
+
rec = JSON.parse(trimmed);
|
|
2220
|
+
} catch {
|
|
2221
|
+
continue;
|
|
2222
|
+
}
|
|
2223
|
+
if (typeof rec.cwd === "string" && rec.cwd) {
|
|
2224
|
+
const parts = rec.cwd.split(/[\\/]+/).filter(Boolean);
|
|
2225
|
+
const base = parts.at(-1);
|
|
2226
|
+
if (base) return base;
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
} finally {
|
|
2230
|
+
closeSync(fd);
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
return null;
|
|
2234
|
+
}
|
|
2179
2235
|
async function readSessions(rootPath) {
|
|
2180
2236
|
const logs = [];
|
|
2181
2237
|
const skipped = [];
|
|
@@ -2195,13 +2251,14 @@ async function readSessions(rootPath) {
|
|
|
2195
2251
|
continue;
|
|
2196
2252
|
}
|
|
2197
2253
|
const files = await collectJsonl(projPath);
|
|
2254
|
+
const projectSlug = projectNameFromLogs(files) ?? dir.name;
|
|
2198
2255
|
for (const full of files) {
|
|
2199
2256
|
const rel = relative(projPath, full).split(sep).join("/");
|
|
2200
2257
|
const name = `${dir.name}/${rel}`;
|
|
2201
2258
|
const { kind, parentSessionId } = classify(rel);
|
|
2202
2259
|
try {
|
|
2203
2260
|
closeSync(openSync(full, "r"));
|
|
2204
|
-
logs.push(lazyLog(full, { name, kind, parentSessionId, projectSlug
|
|
2261
|
+
logs.push(lazyLog(full, { name, kind, parentSessionId, projectSlug }));
|
|
2205
2262
|
} catch {
|
|
2206
2263
|
skipped.push(name);
|
|
2207
2264
|
}
|
|
@@ -2233,6 +2290,50 @@ function ansi(enabled) {
|
|
|
2233
2290
|
function usd2(n) {
|
|
2234
2291
|
return `$${(Math.round(n * 100) / 100).toFixed(2)}`;
|
|
2235
2292
|
}
|
|
2293
|
+
var WRAP_WIDTH = 92;
|
|
2294
|
+
function wrapLines(text2, indent = 0) {
|
|
2295
|
+
const pad = " ".repeat(indent);
|
|
2296
|
+
const words = text2.split(/\s+/).filter((w) => w.length > 0);
|
|
2297
|
+
const out = [];
|
|
2298
|
+
let line = "";
|
|
2299
|
+
for (const word of words) {
|
|
2300
|
+
const candidate = line.length > 0 ? `${line} ${word}` : word;
|
|
2301
|
+
if (line.length > 0 && pad.length + candidate.length > WRAP_WIDTH) {
|
|
2302
|
+
out.push(pad + line);
|
|
2303
|
+
line = word;
|
|
2304
|
+
} else {
|
|
2305
|
+
line = candidate;
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
if (line.length > 0) out.push(pad + line);
|
|
2309
|
+
if (out.length < 2) return out.length > 0 ? out : [pad];
|
|
2310
|
+
const last = (out[out.length - 1] ?? "").trim();
|
|
2311
|
+
const prevWords = (out[out.length - 2] ?? "").trim().split(" ");
|
|
2312
|
+
const borrowed = prevWords[prevWords.length - 1];
|
|
2313
|
+
if (borrowed !== void 0 && prevWords.length >= 3 && !last.includes(" ") && pad.length + borrowed.length + 1 + last.length <= WRAP_WIDTH) {
|
|
2314
|
+
out[out.length - 2] = pad + prevWords.slice(0, -1).join(" ");
|
|
2315
|
+
out[out.length - 1] = `${pad}${borrowed} ${last}`;
|
|
2316
|
+
}
|
|
2317
|
+
return out;
|
|
2318
|
+
}
|
|
2319
|
+
function pushProse(lines, text2, indent = 0, style = (s) => s) {
|
|
2320
|
+
for (const line of wrapLines(text2, indent)) lines.push(style(line));
|
|
2321
|
+
}
|
|
2322
|
+
function plural(n, noun) {
|
|
2323
|
+
return n === 1 ? noun : `${noun}s`;
|
|
2324
|
+
}
|
|
2325
|
+
function statRows(rows, a) {
|
|
2326
|
+
const plain = (c) => `${c.k}: ${c.v}`;
|
|
2327
|
+
const leadWidth = Math.max(...rows.map((r) => r[0] ? plain(r[0]).length : 0));
|
|
2328
|
+
const gap = " ";
|
|
2329
|
+
return rows.map(
|
|
2330
|
+
(row) => row.map((c, i) => {
|
|
2331
|
+
const styled = `${a.dim(c.k + ":")} ${a.bold(c.v)}`;
|
|
2332
|
+
const padding = i === 0 ? Math.max(0, leadWidth - plain(c).length) : 0;
|
|
2333
|
+
return styled + " ".repeat(padding);
|
|
2334
|
+
}).join(gap).trimEnd()
|
|
2335
|
+
);
|
|
2336
|
+
}
|
|
2236
2337
|
function bar(score, a, width = 20) {
|
|
2237
2338
|
if (score === null) return a.dim("n/a");
|
|
2238
2339
|
const filled = Math.round(score / 100 * width);
|
|
@@ -2242,6 +2343,18 @@ function shortModel(id) {
|
|
|
2242
2343
|
const m = /(opus|sonnet|haiku|fable)/i.exec(id);
|
|
2243
2344
|
return m && m[1] ? m[1].toLowerCase() : id;
|
|
2244
2345
|
}
|
|
2346
|
+
function unknownModelNote(breakdown) {
|
|
2347
|
+
let unknown = 0;
|
|
2348
|
+
let total = 0;
|
|
2349
|
+
for (const [model, spend] of Object.entries(breakdown)) {
|
|
2350
|
+
if (spend <= 0) continue;
|
|
2351
|
+
total += spend;
|
|
2352
|
+
if (!isKnownModel(model)) unknown += spend;
|
|
2353
|
+
}
|
|
2354
|
+
if (total <= 0 || unknown / total < 0.01) return null;
|
|
2355
|
+
const pct2 = Math.round(unknown / total * 100);
|
|
2356
|
+
return `${pct2}% of spend used models missing from the rate card and is priced at approximate (Sonnet) rates.`;
|
|
2357
|
+
}
|
|
2245
2358
|
function modelMix(breakdown) {
|
|
2246
2359
|
const merged = /* @__PURE__ */ new Map();
|
|
2247
2360
|
for (const [model, spend] of Object.entries(breakdown)) {
|
|
@@ -2272,46 +2385,67 @@ function formatAudit(record, meta, color = false) {
|
|
|
2272
2385
|
const shareText = `${(share * 100).toFixed(1)}%`;
|
|
2273
2386
|
const lines = [];
|
|
2274
2387
|
lines.push(a.bold("CostClaw audit"));
|
|
2275
|
-
|
|
2276
|
-
|
|
2388
|
+
pushProse(
|
|
2389
|
+
lines,
|
|
2390
|
+
`Source: ${meta.root} (${meta.sessionCount} ${plural(meta.sessionCount, "session")} across ${m.projectsCount} ${plural(m.projectsCount, "project")})`,
|
|
2391
|
+
0,
|
|
2392
|
+
a.dim
|
|
2393
|
+
);
|
|
2394
|
+
if (meta.claudeMdSource) pushProse(lines, `CLAUDE.md: ${meta.claudeMdSource}`, 0, a.dim);
|
|
2395
|
+
lines.push(a.dim("-".repeat(WRAP_WIDTH)));
|
|
2277
2396
|
lines.push("");
|
|
2278
2397
|
lines.push(
|
|
2279
2398
|
subscription ? `${a.bold(record.headlineWasteLabel + ":")} ${a.brand(shareText + " of analyzed usage")} (${usd2(record.headlineWasteUsd)} at API rates)` : `${a.bold(record.headlineWasteLabel + ":")} ${a.brand(usd2(record.headlineWasteUsd))}`
|
|
2280
2399
|
);
|
|
2281
2400
|
lines.push("");
|
|
2282
|
-
lines.push(
|
|
2283
|
-
`${a.dim(subscription ? "Usage analyzed (API-equivalent):" : "Spend analyzed:")} ${a.bold(usd2(m.spendUsd))} ${a.dim("Cache hit:")} ${a.bold(`${(m.cacheHitRate * 100).toFixed(1)}%`)} ${a.dim("Active hours:")} ${a.bold(m.durationHours.toFixed(1))}`
|
|
2284
|
-
);
|
|
2285
2401
|
const mix = modelMix(m.modelBreakdown);
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2402
|
+
const statBlock = [
|
|
2403
|
+
[
|
|
2404
|
+
{ k: subscription ? "Usage analyzed (API-equivalent)" : "Spend analyzed", v: usd2(m.spendUsd) },
|
|
2405
|
+
{ k: "Cache hit", v: `${(m.cacheHitRate * 100).toFixed(1)}%` },
|
|
2406
|
+
{ k: "Active hours", v: m.durationHours.toFixed(1) }
|
|
2407
|
+
],
|
|
2408
|
+
[
|
|
2409
|
+
{ k: "Pricing snapshot", v: m.pricingSnapshotDate },
|
|
2410
|
+
...mix ? [{ k: "Model spend", v: mix }] : []
|
|
2411
|
+
]
|
|
2412
|
+
];
|
|
2413
|
+
for (const row of statRows(statBlock, a)) lines.push(row);
|
|
2414
|
+
const unknownNote = unknownModelNote(m.modelBreakdown);
|
|
2415
|
+
if (unknownNote) pushProse(lines, unknownNote, 0, a.dim);
|
|
2289
2416
|
const w = record.usageWindows;
|
|
2290
2417
|
if (subscription) {
|
|
2291
2418
|
lines.push("");
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2419
|
+
pushProse(
|
|
2420
|
+
lines,
|
|
2421
|
+
`Plan: ${record.plan}. Dollar figures are API-equivalent token value, not money back; recovering them buys rate-limit headroom (more work per usage window).`,
|
|
2422
|
+
0,
|
|
2423
|
+
a.dim
|
|
2296
2424
|
);
|
|
2297
2425
|
if (w?.assessed) {
|
|
2298
2426
|
lines.push(
|
|
2299
|
-
`${a.dim("Usage windows (5h, like your rate limit):")} median ${a.bold(usd2(w.medianWindowUsd))}, busiest ${a.bold(usd2(w.p90WindowUsd))}
|
|
2427
|
+
`${a.dim("Usage windows (5h, like your rate limit):")} median ${a.bold(usd2(w.medianWindowUsd))}, busiest ${a.bold(usd2(w.p90WindowUsd))}`
|
|
2300
2428
|
);
|
|
2429
|
+
lines.push(`${a.dim("Busiest week:")} ${a.bold(usd2(w.maxWeekUsd))} ${a.dim("(API-equivalent)")}`);
|
|
2301
2430
|
if (share >= 0.01 && share < 1) {
|
|
2302
2431
|
const gain = share / (1 - share);
|
|
2303
2432
|
const extraMinutes = Math.round(w.p90WindowActiveMinutes * gain);
|
|
2433
|
+
lines.push("");
|
|
2304
2434
|
lines.push(
|
|
2305
|
-
`Recovering ${shareText} buys about ${a.bold(`${Math.round(gain * 100)}% more work per window`)}
|
|
2435
|
+
`Recovering ${shareText} buys about ${a.bold(`${Math.round(gain * 100)}% more work per window`)}:`
|
|
2306
2436
|
);
|
|
2437
|
+
lines.push(` up to ${a.brand(`~${extraMinutes} extra minutes`)} in a window like your busiest.`);
|
|
2307
2438
|
}
|
|
2308
2439
|
}
|
|
2309
2440
|
} else if (meta.planSource === "default") {
|
|
2310
2441
|
const burn = w?.assessed && w.medianWeekUsd >= 50 ? ` Your median week burns ${usd2(w.medianWeekUsd)} at API rates.` : "";
|
|
2311
|
-
lines.push(
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2442
|
+
lines.push("");
|
|
2443
|
+
pushProse(lines, `Dollar figures are API-equivalent rates.${burn}`, 0, a.dim);
|
|
2444
|
+
pushProse(
|
|
2445
|
+
lines,
|
|
2446
|
+
"On a Claude subscription? Rerun with --plan max20x (or pro, max5x) to frame results as token efficiency.",
|
|
2447
|
+
0,
|
|
2448
|
+
a.dim
|
|
2315
2449
|
);
|
|
2316
2450
|
}
|
|
2317
2451
|
if (m.topProjects.length > 0) {
|
|
@@ -2320,8 +2454,10 @@ function formatAudit(record, meta, color = false) {
|
|
|
2320
2454
|
for (const p of m.topProjects.slice(0, 5)) {
|
|
2321
2455
|
const name = p.slug.length > 30 ? p.slug.slice(0, 27) + "..." : p.slug;
|
|
2322
2456
|
const model = p.modelPrimary ? shortModel(p.modelPrimary) : "n/a";
|
|
2457
|
+
const sessions = `${String(p.sessionsCount).padStart(4)} ${plural(p.sessionsCount, "session")}`;
|
|
2458
|
+
const cache = `cache ${(p.cacheHitRate * 100).toFixed(1)}%`;
|
|
2323
2459
|
lines.push(
|
|
2324
|
-
` ${name.padEnd(32)}${a.bold(usd2(p.costUsd).padStart(10))} ${
|
|
2460
|
+
` ${name.padEnd(32)}${a.bold(usd2(p.costUsd).padStart(10))} ${sessions.padEnd(16)}${a.dim(`${cache.padEnd(15)}${model}`)}`
|
|
2325
2461
|
);
|
|
2326
2462
|
}
|
|
2327
2463
|
}
|
|
@@ -2339,10 +2475,11 @@ function formatAudit(record, meta, color = false) {
|
|
|
2339
2475
|
if (dollarFixes.length === 0) {
|
|
2340
2476
|
lines.push(" No directly recoverable dollars found. See issues below.");
|
|
2341
2477
|
} else {
|
|
2342
|
-
for (const f of dollarFixes) {
|
|
2478
|
+
for (const [i, f] of dollarFixes.entries()) {
|
|
2479
|
+
if (i > 0) lines.push("");
|
|
2343
2480
|
lines.push(` - [${a.brand(usd2(f.wasteUsd))}] ${a.bold(f.title)}`);
|
|
2344
|
-
lines
|
|
2345
|
-
if (f.basis) lines
|
|
2481
|
+
pushProse(lines, f.action, 6);
|
|
2482
|
+
if (f.basis) pushProse(lines, `Evidence: ${f.basis}`, 6, a.dim);
|
|
2346
2483
|
}
|
|
2347
2484
|
}
|
|
2348
2485
|
if (otherIssues.length > 0) {
|
|
@@ -2350,12 +2487,12 @@ function formatAudit(record, meta, color = false) {
|
|
|
2350
2487
|
lines.push(a.bold("Other issues to tighten:"));
|
|
2351
2488
|
for (const f of otherIssues) {
|
|
2352
2489
|
lines.push(` - ${f.title}`);
|
|
2353
|
-
if (f.basis) lines
|
|
2490
|
+
if (f.basis) pushProse(lines, `Evidence: ${f.basis}`, 6, a.dim);
|
|
2354
2491
|
}
|
|
2355
2492
|
}
|
|
2356
2493
|
if (meta.skipped.length > 0) {
|
|
2357
2494
|
lines.push("");
|
|
2358
|
-
lines
|
|
2495
|
+
pushProse(lines, `Note: ${meta.skipped.length} file(s) could not be read and were skipped.`, 0, a.dim);
|
|
2359
2496
|
}
|
|
2360
2497
|
const gapLine = unassessedLine(record.pillarScores);
|
|
2361
2498
|
if (gapLine) {
|
|
@@ -2402,20 +2539,22 @@ function projectChart(projects) {
|
|
|
2402
2539
|
if (projects.length === 0) {
|
|
2403
2540
|
return `<p class="muted">No project spend to chart.</p>`;
|
|
2404
2541
|
}
|
|
2405
|
-
const rowH =
|
|
2406
|
-
const gap =
|
|
2407
|
-
const labelW =
|
|
2408
|
-
const
|
|
2409
|
-
const
|
|
2542
|
+
const rowH = 28;
|
|
2543
|
+
const gap = 8;
|
|
2544
|
+
const labelW = 200;
|
|
2545
|
+
const valW = 90;
|
|
2546
|
+
const barMax = 400;
|
|
2547
|
+
const barH = 14;
|
|
2410
2548
|
const width = labelW + barMax + valW;
|
|
2411
2549
|
const height = projects.length * (rowH + gap) - gap;
|
|
2412
2550
|
const maxCost = Math.max(...projects.map((p) => p.costUsd), 0.01);
|
|
2413
2551
|
const rows = projects.map((p, i) => {
|
|
2414
2552
|
const y = i * (rowH + gap);
|
|
2415
|
-
const w = Math.max(
|
|
2553
|
+
const w = Math.max(barH, Math.round(p.costUsd / maxCost * barMax));
|
|
2416
2554
|
const mid = y + rowH / 2;
|
|
2417
|
-
const
|
|
2418
|
-
|
|
2555
|
+
const barY = mid - barH / 2;
|
|
2556
|
+
const label = esc(truncate(p.slug, 30));
|
|
2557
|
+
return `<text x="0" y="${mid}" class="c-lbl" dominant-baseline="middle">${label}</text><rect x="${labelW}" y="${barY}" width="${barMax}" height="${barH}" rx="${barH / 2}" fill="#f5ece6"></rect><rect x="${labelW}" y="${barY}" width="${w}" height="${barH}" rx="${barH / 2}" fill="#e07a5f"></rect><text x="${width}" y="${mid}" class="c-val" text-anchor="end" dominant-baseline="middle">${esc(usd3(p.costUsd))}</text>`;
|
|
2419
2558
|
}).join("");
|
|
2420
2559
|
return `<svg viewBox="0 0 ${width} ${height}" width="100%" height="${height}" role="img" aria-label="Top projects by cost" preserveAspectRatio="xMinYMin meet">${rows}</svg>`;
|
|
2421
2560
|
}
|
|
@@ -2437,64 +2576,129 @@ function otherFixCard(f) {
|
|
|
2437
2576
|
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>`;
|
|
2438
2577
|
}
|
|
2439
2578
|
var STYLE = `
|
|
2440
|
-
:root {
|
|
2579
|
+
:root {
|
|
2580
|
+
color-scheme: light;
|
|
2581
|
+
/* Warm clay-on-paper palette. Clay is graphic only (bars, rules, gradients);
|
|
2582
|
+
colored text is always umber, so every label clears WCAG AA on paper. */
|
|
2583
|
+
--ink: #251d1a;
|
|
2584
|
+
--paper: #fefaf7;
|
|
2585
|
+
--card: #fffdfb;
|
|
2586
|
+
--umber: #822d1d;
|
|
2587
|
+
--clay: #e07a5f;
|
|
2588
|
+
--line: #f0e2da;
|
|
2589
|
+
--track: #f5ece6;
|
|
2590
|
+
--body: #5a4a43;
|
|
2591
|
+
--muted: #6f5f58;
|
|
2592
|
+
--faint: #7f6d64;
|
|
2593
|
+
/* 4px spacing scale; every gap below comes from it. */
|
|
2594
|
+
--s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 20px;
|
|
2595
|
+
--s6: 24px; --s7: 32px; --s8: 40px; --s9: 48px; --s10: 64px;
|
|
2596
|
+
--sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
2597
|
+
--mono: ui-monospace, SFMono-Regular, "SF Mono", "JetBrains Mono", Menlo, Consolas,
|
|
2598
|
+
"Liberation Mono", monospace;
|
|
2599
|
+
}
|
|
2441
2600
|
* { box-sizing: border-box; }
|
|
2442
2601
|
body {
|
|
2443
2602
|
margin: 0;
|
|
2444
|
-
font-family:
|
|
2445
|
-
color:
|
|
2446
|
-
background:
|
|
2603
|
+
font-family: var(--sans);
|
|
2604
|
+
color: var(--ink);
|
|
2605
|
+
background: var(--paper);
|
|
2447
2606
|
line-height: 1.5;
|
|
2607
|
+
-webkit-font-smoothing: antialiased;
|
|
2608
|
+
}
|
|
2609
|
+
.wrap { max-width: 780px; margin: 0 auto; padding: var(--s8) var(--s6) var(--s10); }
|
|
2610
|
+
.mark { width: 28px; height: 3px; border-radius: 2px; background: var(--clay); margin: 0 0 var(--s4); }
|
|
2611
|
+
h1 { font-size: 28px; line-height: 1.2; margin: 0 0 var(--s3); letter-spacing: -0.015em; }
|
|
2612
|
+
h2 {
|
|
2613
|
+
font-size: 17px; line-height: 1.3; letter-spacing: 0.01em; color: var(--umber);
|
|
2614
|
+
margin: var(--s8) 0 var(--s4); padding-top: var(--s5); border-top: 1px solid var(--line);
|
|
2448
2615
|
}
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2616
|
+
/* Numbers, paths, and model ids are data: one monospace face everywhere. */
|
|
2617
|
+
.mono, .meta, .stat .v, .p-score, .score-head .big, .fix-amt, .banner .amount-sub {
|
|
2618
|
+
font-family: var(--mono); font-variant-numeric: tabular-nums;
|
|
2619
|
+
}
|
|
2620
|
+
.meta { color: var(--muted); font-size: 13px; line-height: 1.6; margin: 0; word-break: break-word; }
|
|
2621
|
+
.sub { color: var(--muted); font-size: 14px; margin: var(--s4) 0 0; max-width: 74ch; }
|
|
2453
2622
|
.banner {
|
|
2454
|
-
margin:
|
|
2455
|
-
background: linear-gradient(135deg,
|
|
2623
|
+
margin: var(--s7) 0 0; padding: var(--s6) var(--s6) var(--s5); border-radius: 14px;
|
|
2624
|
+
background: linear-gradient(135deg, var(--umber), var(--clay)); color: var(--paper);
|
|
2456
2625
|
box-shadow: 0 8px 24px rgba(130, 45, 29, 0.18);
|
|
2457
2626
|
}
|
|
2458
|
-
.banner .label { font-size:
|
|
2459
|
-
.banner .amount { font-size: 40px; font-weight: 700; margin-top:
|
|
2460
|
-
.banner .amount-sub { font-size: 15px; font-weight: 500; margin-top:
|
|
2461
|
-
|
|
2462
|
-
.
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
.
|
|
2467
|
-
|
|
2468
|
-
|
|
2627
|
+
.banner .label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.1em; opacity: 0.92; }
|
|
2628
|
+
.banner .amount { font-size: 40px; line-height: 1.1; font-weight: 700; margin-top: var(--s2); letter-spacing: -0.02em; }
|
|
2629
|
+
.banner .amount-sub { font-size: 15px; font-weight: 500; margin-top: var(--s1); opacity: 0.92; }
|
|
2630
|
+
/* One column rhythm for every stat row, so tile edges line up across rows. */
|
|
2631
|
+
.stats {
|
|
2632
|
+
display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr));
|
|
2633
|
+
gap: var(--s3); margin: var(--s4) 0 0;
|
|
2634
|
+
}
|
|
2635
|
+
.stat {
|
|
2636
|
+
display: flex; flex-direction: column; gap: var(--s2);
|
|
2637
|
+
padding: var(--s3) var(--s4); background: var(--card);
|
|
2638
|
+
border: 1px solid var(--line); border-radius: 10px;
|
|
2639
|
+
}
|
|
2640
|
+
.stat .k { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
|
|
2641
|
+
/* Values sit on the tile floor, so a two-line label never drops its number. */
|
|
2642
|
+
.stat .v { font-size: 20px; font-weight: 600; line-height: 1.2; margin-top: auto; }
|
|
2643
|
+
.stat .v-small { font-size: 13px; font-weight: 500; line-height: 1.4; }
|
|
2644
|
+
.nb { white-space: nowrap; }
|
|
2645
|
+
.score-head { display: flex; align-items: baseline; gap: var(--s3); margin: 0 0 var(--s4); }
|
|
2646
|
+
.score-head .big { font-size: 30px; font-weight: 700; letter-spacing: -0.01em; }
|
|
2647
|
+
.score-head .band { font-size: 15px; color: var(--umber); font-weight: 600; }
|
|
2648
|
+
.pillar { display: flex; align-items: center; gap: var(--s3); margin: var(--s2) 0; }
|
|
2469
2649
|
.p-name { flex: 0 0 190px; font-size: 14px; }
|
|
2470
|
-
.p-track { flex: 1; height: 14px; background:
|
|
2650
|
+
.p-track { flex: 1; height: 14px; background: var(--track); border-radius: 7px; overflow: hidden; }
|
|
2471
2651
|
.p-fill { display: block; height: 100%; border-radius: 7px; }
|
|
2472
2652
|
.p-fill.p-na { background: repeating-linear-gradient(45deg, #ece0d8, #ece0d8 6px, #f5ece6 6px, #f5ece6 12px); }
|
|
2473
|
-
.p-score { flex: 0 0 40px; text-align: right; font-
|
|
2474
|
-
.muted { color:
|
|
2475
|
-
.chart {
|
|
2476
|
-
|
|
2477
|
-
|
|
2653
|
+
.p-score { flex: 0 0 40px; text-align: right; font-weight: 600; font-size: 14px; }
|
|
2654
|
+
.muted { color: var(--faint); font-weight: 500; }
|
|
2655
|
+
.chart {
|
|
2656
|
+
background: var(--card); border: 1px solid var(--line); border-radius: 12px;
|
|
2657
|
+
padding: var(--s5); overflow-x: auto;
|
|
2658
|
+
}
|
|
2659
|
+
/* height:auto lets the viewBox aspect drive the box, so the chart fills the
|
|
2660
|
+
card's width exactly with no letterboxing at any viewport. */
|
|
2661
|
+
.chart svg { display: block; height: auto; }
|
|
2662
|
+
.c-lbl { font-size: 13px; fill: var(--body); font-family: var(--mono); }
|
|
2663
|
+
.c-val { font-size: 13px; fill: var(--umber); font-weight: 600; font-family: var(--mono); }
|
|
2478
2664
|
ul.fixes { list-style: none; margin: 0; padding: 0; }
|
|
2479
|
-
.fix {
|
|
2665
|
+
.fix {
|
|
2666
|
+
background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--clay);
|
|
2667
|
+
border-radius: 10px; padding: var(--s4) var(--s5); margin: 0 0 var(--s3);
|
|
2668
|
+
}
|
|
2669
|
+
.fix:last-child { margin-bottom: 0; }
|
|
2480
2670
|
.fix.other { border-left-color: #cbb8ae; }
|
|
2481
|
-
.fix-head { display: flex; align-items: baseline; gap:
|
|
2482
|
-
.fix-amt { font-size: 18px; font-weight: 700; color:
|
|
2483
|
-
.fix-title { font-size: 15px; font-weight: 600; }
|
|
2484
|
-
.fix
|
|
2485
|
-
.fix-
|
|
2486
|
-
.fix-
|
|
2487
|
-
.
|
|
2488
|
-
.footer {
|
|
2489
|
-
|
|
2671
|
+
.fix-head { display: flex; align-items: baseline; gap: var(--s3); }
|
|
2672
|
+
.fix-amt { font-size: 18px; font-weight: 700; color: var(--umber); }
|
|
2673
|
+
.fix-title { font-size: 15px; font-weight: 600; line-height: 1.35; }
|
|
2674
|
+
.fix p { max-width: 74ch; }
|
|
2675
|
+
.fix-problem { margin: var(--s2) 0 0; color: var(--body); font-size: 14px; }
|
|
2676
|
+
.fix-action { margin: var(--s1) 0 0; font-size: 14px; }
|
|
2677
|
+
.fix-basis { margin: var(--s2) 0 0; font-size: 13px; color: var(--faint); }
|
|
2678
|
+
.footer {
|
|
2679
|
+
margin-top: var(--s9); padding-top: var(--s5); border-top: 1px solid var(--line);
|
|
2680
|
+
color: var(--muted); font-size: 13px; max-width: 72ch;
|
|
2681
|
+
}
|
|
2682
|
+
.footer strong { color: var(--ink); }
|
|
2683
|
+
.footer .gen { margin: var(--s2) 0 0; }
|
|
2684
|
+
/* The timestamp is one token; never let it break across lines. */
|
|
2685
|
+
.footer .gen .mono { white-space: nowrap; }
|
|
2686
|
+
@media (max-width: 560px) {
|
|
2687
|
+
.wrap { padding: var(--s6) var(--s4) var(--s8); }
|
|
2688
|
+
h1 { font-size: 24px; }
|
|
2689
|
+
.banner .amount { font-size: 32px; }
|
|
2690
|
+
.p-name { flex: 0 0 130px; font-size: 13px; }
|
|
2691
|
+
}
|
|
2490
2692
|
`;
|
|
2491
2693
|
function renderHtml(record, meta) {
|
|
2492
2694
|
const m = record.usageMetrics;
|
|
2493
2695
|
const mix = modelMix(m.modelBreakdown);
|
|
2696
|
+
const unknownNote = unknownModelNote(m.modelBreakdown);
|
|
2697
|
+
const mixCells = mix ? mix.split(" / ").map((part) => `<span class="nb">${esc(part)}</span>`).join(" / ") : "";
|
|
2494
2698
|
const subscription = record.plan !== void 0 && record.plan !== "api";
|
|
2495
2699
|
const share = record.headlineWasteShare ?? (m.spendUsd > 0 ? record.headlineWasteUsd / m.spendUsd : 0);
|
|
2496
2700
|
const shareText = `${(share * 100).toFixed(1)}%`;
|
|
2497
|
-
const bannerAmount = subscription ? `<div class="amount">${shareText} of analyzed usage</div><div class="amount-sub">${esc(usd3(record.headlineWasteUsd))} at API rates</div>` : `<div class="amount">${esc(usd3(record.headlineWasteUsd))}</div>`;
|
|
2701
|
+
const bannerAmount = subscription ? `<div class="amount">${shareText} of analyzed usage</div><div class="amount-sub">${esc(usd3(record.headlineWasteUsd))} at API rates</div>` : `<div class="amount mono">${esc(usd3(record.headlineWasteUsd))}</div>`;
|
|
2498
2702
|
const spendLabel = subscription ? "Usage analyzed (API-equivalent)" : "Spend analyzed";
|
|
2499
2703
|
const w = record.usageWindows;
|
|
2500
2704
|
let headroom = "";
|
|
@@ -2511,6 +2715,11 @@ function renderHtml(record, meta) {
|
|
|
2511
2715
|
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>`;
|
|
2512
2716
|
const otherSection = otherIssues.length === 0 ? "" : `<h2>Other issues to tighten</h2><ul class="fixes">${otherIssues.map(otherFixCard).join("")}</ul>`;
|
|
2513
2717
|
const skippedNote = meta.skipped.length > 0 ? `<p class="sub">Note: ${meta.skipped.length} file(s) could not be read and were skipped.</p>` : "";
|
|
2718
|
+
const metaBlock = [
|
|
2719
|
+
`Source: ${esc(meta.root)} (${meta.sessionCount} ${plural(meta.sessionCount, "session")} across ${m.projectsCount} ${plural(m.projectsCount, "project")})`,
|
|
2720
|
+
meta.claudeMdSource ? `CLAUDE.md: ${esc(meta.claudeMdSource)}` : "",
|
|
2721
|
+
`Pricing snapshot: ${esc(m.pricingSnapshotDate)}`
|
|
2722
|
+
].filter((line) => line.length > 0).map((line) => `<p class="meta">${line}</p>`).join("\n");
|
|
2514
2723
|
return `<!doctype html>
|
|
2515
2724
|
<html lang="en">
|
|
2516
2725
|
<head>
|
|
@@ -2521,10 +2730,9 @@ function renderHtml(record, meta) {
|
|
|
2521
2730
|
</head>
|
|
2522
2731
|
<body>
|
|
2523
2732
|
<div class="wrap">
|
|
2733
|
+
<div class="mark" aria-hidden="true"></div>
|
|
2524
2734
|
<h1>CostClaw audit</h1>
|
|
2525
|
-
|
|
2526
|
-
${meta.claudeMdSource ? `<p class="sub">CLAUDE.md: ${esc(meta.claudeMdSource)}</p>` : ""}
|
|
2527
|
-
<p class="sub">Pricing snapshot: ${esc(m.pricingSnapshotDate)}</p>
|
|
2735
|
+
${metaBlock}
|
|
2528
2736
|
|
|
2529
2737
|
<div class="banner">
|
|
2530
2738
|
<div class="label">${esc(record.headlineWasteLabel)}</div>
|
|
@@ -2535,8 +2743,9 @@ ${planNote}
|
|
|
2535
2743
|
<div class="stat"><div class="k">${spendLabel}</div><div class="v">${esc(usd3(m.spendUsd))}</div></div>
|
|
2536
2744
|
<div class="stat"><div class="k">Cache hit rate</div><div class="v">${(m.cacheHitRate * 100).toFixed(1)}%</div></div>
|
|
2537
2745
|
<div class="stat"><div class="k">Active hours</div><div class="v">${m.durationHours.toFixed(1)}</div></div>
|
|
2538
|
-
${mix ? `<div class="stat"><div class="k">Model spend</div><div class="v v-small">${
|
|
2746
|
+
${mix ? `<div class="stat"><div class="k">Model spend</div><div class="v v-small">${mixCells}</div></div>` : ""}
|
|
2539
2747
|
</div>
|
|
2748
|
+
${unknownNote ? `<p class="meta">${esc(unknownNote)}</p>` : ""}
|
|
2540
2749
|
${windowTiles}
|
|
2541
2750
|
|
|
2542
2751
|
<h2>Spend by project</h2>
|
|
@@ -2556,7 +2765,8 @@ ${skippedNote}
|
|
|
2556
2765
|
|
|
2557
2766
|
<div class="footer">
|
|
2558
2767
|
<strong>This report was generated locally on your machine.</strong> Nothing was uploaded:
|
|
2559
|
-
your prompts, file contents, and paths never left this computer.
|
|
2768
|
+
your prompts, file contents, and paths never left this computer.
|
|
2769
|
+
<div class="gen">Generated <span class="mono">${esc(record.createdAt)}</span>.</div>
|
|
2560
2770
|
</div>
|
|
2561
2771
|
</div>
|
|
2562
2772
|
</body>
|
|
@@ -3789,7 +3999,7 @@ function parseBadgeFlags(args) {
|
|
|
3789
3999
|
};
|
|
3790
4000
|
}
|
|
3791
4001
|
function cliVersion() {
|
|
3792
|
-
return true ? "0.5.
|
|
4002
|
+
return true ? "0.5.1" : "0.0.0-dev";
|
|
3793
4003
|
}
|
|
3794
4004
|
function helpText() {
|
|
3795
4005
|
return [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "costclaw",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.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": {
|