costclaw 0.5.2 → 0.5.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/dist/index.js +163 -38
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1546,6 +1546,78 @@ function carriedIntro(currentClaudeMd) {
|
|
|
1546
1546
|
}
|
|
1547
1547
|
return { title, intro, introDropped };
|
|
1548
1548
|
}
|
|
1549
|
+
function parseSections(md) {
|
|
1550
|
+
const lines = md.split(/\r?\n/);
|
|
1551
|
+
const sections = [];
|
|
1552
|
+
let current = null;
|
|
1553
|
+
for (const line of lines) {
|
|
1554
|
+
const m = line.match(/^##\s+(.+?)\s*$/);
|
|
1555
|
+
if (m) {
|
|
1556
|
+
if (current) sections.push({ heading: current.heading, body: current.body.join("\n").trim() });
|
|
1557
|
+
current = { heading: m[1], body: [] };
|
|
1558
|
+
} else if (current) {
|
|
1559
|
+
current.body.push(line);
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
if (current) sections.push({ heading: current.heading, body: current.body.join("\n").trim() });
|
|
1563
|
+
return sections.filter((s) => s.body.length > 0);
|
|
1564
|
+
}
|
|
1565
|
+
var REGENERATED_HEADING = [
|
|
1566
|
+
/^(commands?|scripts?)\b/i,
|
|
1567
|
+
/^(project structure|structure|layout|directory)/i,
|
|
1568
|
+
/^(code style|style|formatting)\b/i,
|
|
1569
|
+
/^test(s|ing)?\b/i,
|
|
1570
|
+
/^env(ironment)?\b/i
|
|
1571
|
+
];
|
|
1572
|
+
var VOLATILE_HEADING = /^(status|changelog|progress|history|roadmap|todos?|next steps)\b/i;
|
|
1573
|
+
var WORKFLOW_HEADING = /workflow|etiquette|contributing|branch|commit/i;
|
|
1574
|
+
var ARCH_HEADING = /architecture|gotcha|footgun|how it works|data flow|overview/i;
|
|
1575
|
+
function carriedSections(currentClaudeMd) {
|
|
1576
|
+
const out = { workflow: null, architecture: null, other: [], droppedVolatile: [], replaced: [] };
|
|
1577
|
+
if (!currentClaudeMd) return out;
|
|
1578
|
+
for (const section of parseSections(currentClaudeMd)) {
|
|
1579
|
+
if (VOLATILE_HEADING.test(section.heading)) {
|
|
1580
|
+
out.droppedVolatile.push(section.heading);
|
|
1581
|
+
} else if (REGENERATED_HEADING.some((re) => re.test(section.heading))) {
|
|
1582
|
+
out.replaced.push(section.heading);
|
|
1583
|
+
} else if (out.workflow === null && WORKFLOW_HEADING.test(section.heading)) {
|
|
1584
|
+
out.workflow = section.body;
|
|
1585
|
+
} else if (out.architecture === null && ARCH_HEADING.test(section.heading)) {
|
|
1586
|
+
out.architecture = section.body;
|
|
1587
|
+
} else {
|
|
1588
|
+
out.other.push(section);
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
return out;
|
|
1592
|
+
}
|
|
1593
|
+
var CANONICAL_LABELS = /* @__PURE__ */ new Set([
|
|
1594
|
+
"install",
|
|
1595
|
+
"dev",
|
|
1596
|
+
"start",
|
|
1597
|
+
"serve",
|
|
1598
|
+
"demo",
|
|
1599
|
+
"build",
|
|
1600
|
+
"test",
|
|
1601
|
+
"test one file",
|
|
1602
|
+
"lint",
|
|
1603
|
+
"typecheck",
|
|
1604
|
+
"format",
|
|
1605
|
+
"e2e",
|
|
1606
|
+
"migrate",
|
|
1607
|
+
"db:migrate"
|
|
1608
|
+
]);
|
|
1609
|
+
var MAX_COMMANDS = 12;
|
|
1610
|
+
function curateCommands(commands) {
|
|
1611
|
+
if (commands.length <= MAX_COMMANDS) return { kept: commands, dropped: 0 };
|
|
1612
|
+
const kept = commands.filter((c) => CANONICAL_LABELS.has(c.label.toLowerCase()));
|
|
1613
|
+
const chosen = (kept.length >= 4 ? kept : commands).slice(0, MAX_COMMANDS);
|
|
1614
|
+
return { kept: chosen, dropped: commands.length - chosen.length };
|
|
1615
|
+
}
|
|
1616
|
+
function commandLine(c) {
|
|
1617
|
+
const tokens = c.command.toLowerCase().split(/[^a-z0-9:_-]+/);
|
|
1618
|
+
const redundant = tokens.includes(c.label.toLowerCase());
|
|
1619
|
+
return redundant ? `- \`${c.command}\`` : `- \`${c.command}\` ${c.label}`;
|
|
1620
|
+
}
|
|
1549
1621
|
function stackOneLiner(stack) {
|
|
1550
1622
|
const items = stack.map((s) => s.trim()).filter((s) => s.length > 0);
|
|
1551
1623
|
if (items.length === 0) return null;
|
|
@@ -1562,6 +1634,7 @@ function stackOneLiner(stack) {
|
|
|
1562
1634
|
function buildClaudeMd(input, recoverable, notes) {
|
|
1563
1635
|
const { currentClaudeMd, project } = input;
|
|
1564
1636
|
const { title, intro, introDropped } = carriedIntro(currentClaudeMd);
|
|
1637
|
+
const carried = carriedSections(currentClaudeMd);
|
|
1565
1638
|
const recoverableLine = recoverable >= 1 ? ` - Your audit valued cache-miss exposure at ${usd(recoverable)}. Keeping this file small and stable is part of recovering it; the matching PLAYBOOK.md has the rest.` : " - Keeping this file small and stable keeps the cached prefix cheap to reuse each turn.";
|
|
1566
1639
|
const parts = [];
|
|
1567
1640
|
parts.push(
|
|
@@ -1618,11 +1691,21 @@ function buildClaudeMd(input, recoverable, notes) {
|
|
|
1618
1691
|
);
|
|
1619
1692
|
}
|
|
1620
1693
|
parts.push([`# ${titleText}`, "", descLine].join("\n"));
|
|
1621
|
-
const
|
|
1694
|
+
const allCommands = project?.commands ?? [];
|
|
1695
|
+
const { kept: commands, dropped: droppedCommands } = curateCommands(allCommands);
|
|
1622
1696
|
if (commands.length > 0) {
|
|
1623
1697
|
const lines = ["## Commands", ""];
|
|
1624
|
-
for (const c of commands) lines.push(
|
|
1698
|
+
for (const c of commands) lines.push(commandLine(c));
|
|
1625
1699
|
if (project?.preCommit) lines.push(`- IMPORTANT: ${project.preCommit}.`);
|
|
1700
|
+
if (droppedCommands > 0) {
|
|
1701
|
+
lines.push("");
|
|
1702
|
+
lines.push(
|
|
1703
|
+
`The remaining ${droppedCommands} scripts stay in \`package.json\`; add one here only when the agent keeps needing it.`
|
|
1704
|
+
);
|
|
1705
|
+
notes.push(
|
|
1706
|
+
`Kept the ${commands.length} canonical commands of ${allCommands.length} detected; the rest stay in the manifest so this file stays lean.`
|
|
1707
|
+
);
|
|
1708
|
+
}
|
|
1626
1709
|
parts.push(lines.join("\n"));
|
|
1627
1710
|
filled.push("Commands");
|
|
1628
1711
|
} else {
|
|
@@ -1646,7 +1729,7 @@ function buildClaudeMd(input, recoverable, notes) {
|
|
|
1646
1729
|
].join("\n")
|
|
1647
1730
|
);
|
|
1648
1731
|
}
|
|
1649
|
-
const structure = project?.structure ?? [];
|
|
1732
|
+
const structure = (project?.structure ?? []).filter((d) => d.note.trim().length > 0).slice(0, 10);
|
|
1650
1733
|
if (structure.length > 0) {
|
|
1651
1734
|
const lines = ["## Project structure", ""];
|
|
1652
1735
|
for (const d of structure) {
|
|
@@ -1671,7 +1754,7 @@ function buildClaudeMd(input, recoverable, notes) {
|
|
|
1671
1754
|
].join("\n")
|
|
1672
1755
|
);
|
|
1673
1756
|
}
|
|
1674
|
-
const codeStyle = project?.codeStyle ?? [];
|
|
1757
|
+
const codeStyle = (project?.codeStyle ?? []).filter((s) => !/\bconfigured\s*$/i.test(s.trim()));
|
|
1675
1758
|
if (codeStyle.length > 0) {
|
|
1676
1759
|
const lines = ["## Code style", ""];
|
|
1677
1760
|
for (const s of codeStyle) lines.push(`- ${s}`);
|
|
@@ -1713,32 +1796,45 @@ function buildClaudeMd(input, recoverable, notes) {
|
|
|
1713
1796
|
].join("\n")
|
|
1714
1797
|
);
|
|
1715
1798
|
}
|
|
1716
|
-
|
|
1717
|
-
[
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
"
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1799
|
+
if (carried.workflow) {
|
|
1800
|
+
parts.push(["## Workflow", "", carried.workflow].join("\n"));
|
|
1801
|
+
filled.push("Workflow");
|
|
1802
|
+
} else {
|
|
1803
|
+
parts.push(
|
|
1804
|
+
[
|
|
1805
|
+
"## Workflow",
|
|
1806
|
+
"",
|
|
1807
|
+
"<!--",
|
|
1808
|
+
" Repository etiquette: branch naming, commit message rules, which branch PRs",
|
|
1809
|
+
" target, merge vs rebase.",
|
|
1810
|
+
"-->",
|
|
1811
|
+
"- <!-- branch naming and commit conventions -->",
|
|
1812
|
+
"- Typecheck and run the tests after a set of changes, before calling it done."
|
|
1813
|
+
].join("\n")
|
|
1814
|
+
);
|
|
1815
|
+
}
|
|
1816
|
+
if (carried.architecture) {
|
|
1817
|
+
parts.push(["## Architecture and gotchas", "", carried.architecture].join("\n"));
|
|
1818
|
+
filled.push("Architecture and gotchas");
|
|
1819
|
+
} else {
|
|
1820
|
+
parts.push(
|
|
1821
|
+
[
|
|
1822
|
+
"## Architecture and gotchas",
|
|
1823
|
+
"",
|
|
1824
|
+
"<!--",
|
|
1825
|
+
" The highest-leverage prose: non-obvious, project-specific facts and footguns.",
|
|
1826
|
+
" Grow this over time. Whenever the agent makes the same mistake twice, add a",
|
|
1827
|
+
" line here. Example:",
|
|
1828
|
+
" - Migrations are not auto-run: run `npm run migrate` after pulling.",
|
|
1829
|
+
" - Auth tokens refresh in `src/auth/refresh.ts`, not per-request.",
|
|
1830
|
+
"-->",
|
|
1831
|
+
"- <!-- a non-obvious fact or footgun the agent should know -->"
|
|
1832
|
+
].join("\n")
|
|
1833
|
+
);
|
|
1834
|
+
}
|
|
1835
|
+
for (const section of carried.other) {
|
|
1836
|
+
parts.push([`## ${section.heading}`, "", section.body].join("\n"));
|
|
1837
|
+
}
|
|
1742
1838
|
const envVars = (project?.envVars ?? []).map((v) => v.trim()).filter((v) => v.length > 0);
|
|
1743
1839
|
if (envVars.length > 0) {
|
|
1744
1840
|
const names = envVars.map((v) => `\`${v}\``).join(", ");
|
|
@@ -1778,11 +1874,30 @@ function buildClaudeMd(input, recoverable, notes) {
|
|
|
1778
1874
|
"-->"
|
|
1779
1875
|
].join("\n")
|
|
1780
1876
|
);
|
|
1877
|
+
const carriedNames = [
|
|
1878
|
+
...carried.workflow ? ["Workflow"] : [],
|
|
1879
|
+
...carried.architecture ? ["Architecture and gotchas"] : [],
|
|
1880
|
+
...carried.other.map((s) => s.heading)
|
|
1881
|
+
];
|
|
1882
|
+
if (carriedNames.length > 0) {
|
|
1883
|
+
notes.push(
|
|
1884
|
+
`Carried ${joinAnd(carriedNames)} across from your existing CLAUDE.md; hold each line to the "earns its place" bar.`
|
|
1885
|
+
);
|
|
1886
|
+
}
|
|
1887
|
+
if (carried.replaced.length > 0) {
|
|
1888
|
+
notes.push(
|
|
1889
|
+
`Replaced your ${joinAnd(carried.replaced)} section${carried.replaced.length === 1 ? "" : "s"} with the version derived from the project scan; compare before applying.`
|
|
1890
|
+
);
|
|
1891
|
+
}
|
|
1892
|
+
if (carried.droppedVolatile.length > 0) {
|
|
1893
|
+
notes.push(
|
|
1894
|
+
`Left out ${joinAnd(carried.droppedVolatile)}: status-style sections churn on every update and bust the prompt cache. Keep that content in docs or a scratch note the agent opens on demand.`
|
|
1895
|
+
);
|
|
1896
|
+
}
|
|
1781
1897
|
if (project) {
|
|
1782
1898
|
if (filled.length > 0) {
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
);
|
|
1899
|
+
const growNote = carried.architecture ? "" : "; Architecture and gotchas is left as a placeholder for you to grow";
|
|
1900
|
+
notes.push(`Filled ${joinAnd(filled)} from your project${growNote}.`);
|
|
1786
1901
|
} else {
|
|
1787
1902
|
notes.push(
|
|
1788
1903
|
"Your project had no detectable facts, so every section is a placeholder and nothing was invented."
|
|
@@ -1884,13 +1999,17 @@ function tokenDiscipline(report) {
|
|
|
1884
1999
|
"- Set `ANTHROPIC_MODEL` and `CLAUDE_CODE_EFFORT_LEVEL` in your shell profile so every session starts on the right model and effort without a per-session step."
|
|
1885
2000
|
].join("\n");
|
|
1886
2001
|
}
|
|
1887
|
-
function buildPlaybook(report, fixes, scores, recoverable) {
|
|
2002
|
+
function buildPlaybook(report, fixes, scores, recoverable, project) {
|
|
1888
2003
|
const dollarFixes = fixes.filter((f) => f.wasteUsd > 0);
|
|
1889
2004
|
const warnFixes = fixes.filter((f) => f.wasteUsd <= 0);
|
|
1890
2005
|
const parts = [];
|
|
1891
2006
|
parts.push("# CostClaw fix playbook");
|
|
2007
|
+
const projectName = project?.name?.trim();
|
|
2008
|
+
const stack = (project?.stack ?? []).filter((s) => s.trim().length > 0);
|
|
2009
|
+
const projectLine = projectName ? `Project: ${projectName}${stack.length > 0 ? ` (${stack.join(", ")})` : ""}.` : null;
|
|
1892
2010
|
parts.push(
|
|
1893
2011
|
[
|
|
2012
|
+
...projectLine ? [projectLine, ""] : [],
|
|
1894
2013
|
`Total recoverable spend: ${usd(recoverable)} (cache-miss exposure).`,
|
|
1895
2014
|
"",
|
|
1896
2015
|
"This is the ranked plan from your audit. The priority fixes are ordered by the dollars they recover; the setup warnings follow. CostClaw generates the plan; working through it is what recovers the spend."
|
|
@@ -1941,7 +2060,7 @@ function optimize(input) {
|
|
|
1941
2060
|
const recoverable = cacheMissExposureUsd(input.report);
|
|
1942
2061
|
const notes = [];
|
|
1943
2062
|
const optimizedClaudeMd = buildClaudeMd(input, recoverable, notes);
|
|
1944
|
-
const playbook = buildPlaybook(input.report, input.fixes, input.scores, recoverable);
|
|
2063
|
+
const playbook = buildPlaybook(input.report, input.fixes, input.scores, recoverable, input.project);
|
|
1945
2064
|
const settingsJson = buildSettings(input.project);
|
|
1946
2065
|
const cleanNotes = notes.map((n) => clean(n));
|
|
1947
2066
|
if (hasEmDash(optimizedClaudeMd) || hasEmDash(playbook) || hasEmDash(settingsJson) || cleanNotes.some(hasEmDash)) {
|
|
@@ -3606,13 +3725,18 @@ async function applyDerivedSettings(target, suggestedJson, force) {
|
|
|
3606
3725
|
];
|
|
3607
3726
|
}
|
|
3608
3727
|
async function runOptimize(opts) {
|
|
3609
|
-
const
|
|
3728
|
+
const projectDir = opts.projectDir || process.cwd();
|
|
3729
|
+
let claudeMdPath = opts.claudeMdPath;
|
|
3730
|
+
if (!claudeMdPath) {
|
|
3731
|
+
const projectClaudeMd = join6(projectDir, "CLAUDE.md");
|
|
3732
|
+
if (await pathExists(projectClaudeMd)) claudeMdPath = projectClaudeMd;
|
|
3733
|
+
}
|
|
3734
|
+
const { logs, claudeMd, claudeMdSource } = await loadAuditInputs({ path: opts.path, claudeMdPath });
|
|
3610
3735
|
const report = analyzeUploads(logs);
|
|
3611
3736
|
const cmd = analyzeClaudeMd(claudeMd);
|
|
3612
3737
|
const { pillars } = scorePillars(report, cmd, {});
|
|
3613
3738
|
const { plan } = await resolvePlan(join6(opts.home ?? homedir4(), ".costclaw"), opts.plan);
|
|
3614
3739
|
const { fixes } = synthesize(report, pillars, cmd, plan);
|
|
3615
|
-
const projectDir = opts.projectDir || process.cwd();
|
|
3616
3740
|
const project = await scanProject(projectDir);
|
|
3617
3741
|
const result = optimize({ report, scores: pillars, fixes, currentClaudeMd: claudeMd, project });
|
|
3618
3742
|
const recoverable = cacheMissExposureUsd(report);
|
|
@@ -3658,6 +3782,7 @@ async function runOptimize(opts) {
|
|
|
3658
3782
|
lines.push(`Wrote a suggested settings.json to ${settingsPath}`);
|
|
3659
3783
|
lines.push("");
|
|
3660
3784
|
lines.push(describeDetection(project));
|
|
3785
|
+
if (claudeMdSource) lines.push(`Existing CLAUDE.md: ${claudeMdSource}`);
|
|
3661
3786
|
if (plan === "api") {
|
|
3662
3787
|
lines.push(`Recoverable spend (cache-miss exposure): ${usd5(recoverable)}`);
|
|
3663
3788
|
} else {
|
|
@@ -4001,7 +4126,7 @@ function parseBadgeFlags(args) {
|
|
|
4001
4126
|
};
|
|
4002
4127
|
}
|
|
4003
4128
|
function cliVersion() {
|
|
4004
|
-
return true ? "0.5.
|
|
4129
|
+
return true ? "0.5.3" : "0.0.0-dev";
|
|
4005
4130
|
}
|
|
4006
4131
|
function helpText() {
|
|
4007
4132
|
return [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "costclaw",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.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": {
|