costclaw 0.5.1 → 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.
Files changed (2) hide show
  1. package/dist/index.js +165 -38
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -15,6 +15,8 @@ function isPlan(value) {
15
15
  // ../../packages/engine/src/pricing.ts
16
16
  var PRICING_SNAPSHOT_DATE = "2026-08-03";
17
17
  var PRICES_PER_MTOK = Object.freeze({
18
+ "claude-fable-5": { input: 10, output: 50, cache_write: 12.5, cache_read: 1 },
19
+ "claude-opus-5": { input: 5, output: 25, cache_write: 6.25, cache_read: 0.5 },
18
20
  "claude-opus-4-8": { input: 5, output: 25, cache_write: 6.25, cache_read: 0.5 },
19
21
  "claude-sonnet-5": { input: 3, output: 15, cache_write: 3.75, cache_read: 0.3 },
20
22
  "claude-opus-4-7": { input: 15, output: 75, cache_write: 18.75, cache_read: 1.5 },
@@ -1544,6 +1546,78 @@ function carriedIntro(currentClaudeMd) {
1544
1546
  }
1545
1547
  return { title, intro, introDropped };
1546
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
+ }
1547
1621
  function stackOneLiner(stack) {
1548
1622
  const items = stack.map((s) => s.trim()).filter((s) => s.length > 0);
1549
1623
  if (items.length === 0) return null;
@@ -1560,6 +1634,7 @@ function stackOneLiner(stack) {
1560
1634
  function buildClaudeMd(input, recoverable, notes) {
1561
1635
  const { currentClaudeMd, project } = input;
1562
1636
  const { title, intro, introDropped } = carriedIntro(currentClaudeMd);
1637
+ const carried = carriedSections(currentClaudeMd);
1563
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.";
1564
1639
  const parts = [];
1565
1640
  parts.push(
@@ -1616,11 +1691,21 @@ function buildClaudeMd(input, recoverable, notes) {
1616
1691
  );
1617
1692
  }
1618
1693
  parts.push([`# ${titleText}`, "", descLine].join("\n"));
1619
- const commands = project?.commands ?? [];
1694
+ const allCommands = project?.commands ?? [];
1695
+ const { kept: commands, dropped: droppedCommands } = curateCommands(allCommands);
1620
1696
  if (commands.length > 0) {
1621
1697
  const lines = ["## Commands", ""];
1622
- for (const c of commands) lines.push(`- \`${c.command}\` ${c.label}`);
1698
+ for (const c of commands) lines.push(commandLine(c));
1623
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
+ }
1624
1709
  parts.push(lines.join("\n"));
1625
1710
  filled.push("Commands");
1626
1711
  } else {
@@ -1644,7 +1729,7 @@ function buildClaudeMd(input, recoverable, notes) {
1644
1729
  ].join("\n")
1645
1730
  );
1646
1731
  }
1647
- const structure = project?.structure ?? [];
1732
+ const structure = (project?.structure ?? []).filter((d) => d.note.trim().length > 0).slice(0, 10);
1648
1733
  if (structure.length > 0) {
1649
1734
  const lines = ["## Project structure", ""];
1650
1735
  for (const d of structure) {
@@ -1669,7 +1754,7 @@ function buildClaudeMd(input, recoverable, notes) {
1669
1754
  ].join("\n")
1670
1755
  );
1671
1756
  }
1672
- const codeStyle = project?.codeStyle ?? [];
1757
+ const codeStyle = (project?.codeStyle ?? []).filter((s) => !/\bconfigured\s*$/i.test(s.trim()));
1673
1758
  if (codeStyle.length > 0) {
1674
1759
  const lines = ["## Code style", ""];
1675
1760
  for (const s of codeStyle) lines.push(`- ${s}`);
@@ -1711,32 +1796,45 @@ function buildClaudeMd(input, recoverable, notes) {
1711
1796
  ].join("\n")
1712
1797
  );
1713
1798
  }
1714
- parts.push(
1715
- [
1716
- "## Workflow",
1717
- "",
1718
- "<!--",
1719
- " Repository etiquette: branch naming, commit message rules, which branch PRs",
1720
- " target, merge vs rebase.",
1721
- "-->",
1722
- "- <!-- branch naming and commit conventions -->",
1723
- "- Typecheck and run the tests after a set of changes, before calling it done."
1724
- ].join("\n")
1725
- );
1726
- parts.push(
1727
- [
1728
- "## Architecture and gotchas",
1729
- "",
1730
- "<!--",
1731
- " The highest-leverage prose: non-obvious, project-specific facts and footguns.",
1732
- " Grow this over time. Whenever the agent makes the same mistake twice, add a",
1733
- " line here. Example:",
1734
- " - Migrations are not auto-run: run `npm run migrate` after pulling.",
1735
- " - Auth tokens refresh in `src/auth/refresh.ts`, not per-request.",
1736
- "-->",
1737
- "- <!-- a non-obvious fact or footgun the agent should know -->"
1738
- ].join("\n")
1739
- );
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
+ }
1740
1838
  const envVars = (project?.envVars ?? []).map((v) => v.trim()).filter((v) => v.length > 0);
1741
1839
  if (envVars.length > 0) {
1742
1840
  const names = envVars.map((v) => `\`${v}\``).join(", ");
@@ -1776,11 +1874,30 @@ function buildClaudeMd(input, recoverable, notes) {
1776
1874
  "-->"
1777
1875
  ].join("\n")
1778
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
+ }
1779
1897
  if (project) {
1780
1898
  if (filled.length > 0) {
1781
- notes.push(
1782
- `Filled ${joinAnd(filled)} from your project; Architecture and gotchas is left as a placeholder for you to grow.`
1783
- );
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}.`);
1784
1901
  } else {
1785
1902
  notes.push(
1786
1903
  "Your project had no detectable facts, so every section is a placeholder and nothing was invented."
@@ -1882,13 +1999,17 @@ function tokenDiscipline(report) {
1882
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."
1883
2000
  ].join("\n");
1884
2001
  }
1885
- function buildPlaybook(report, fixes, scores, recoverable) {
2002
+ function buildPlaybook(report, fixes, scores, recoverable, project) {
1886
2003
  const dollarFixes = fixes.filter((f) => f.wasteUsd > 0);
1887
2004
  const warnFixes = fixes.filter((f) => f.wasteUsd <= 0);
1888
2005
  const parts = [];
1889
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;
1890
2010
  parts.push(
1891
2011
  [
2012
+ ...projectLine ? [projectLine, ""] : [],
1892
2013
  `Total recoverable spend: ${usd(recoverable)} (cache-miss exposure).`,
1893
2014
  "",
1894
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."
@@ -1939,7 +2060,7 @@ function optimize(input) {
1939
2060
  const recoverable = cacheMissExposureUsd(input.report);
1940
2061
  const notes = [];
1941
2062
  const optimizedClaudeMd = buildClaudeMd(input, recoverable, notes);
1942
- const playbook = buildPlaybook(input.report, input.fixes, input.scores, recoverable);
2063
+ const playbook = buildPlaybook(input.report, input.fixes, input.scores, recoverable, input.project);
1943
2064
  const settingsJson = buildSettings(input.project);
1944
2065
  const cleanNotes = notes.map((n) => clean(n));
1945
2066
  if (hasEmDash(optimizedClaudeMd) || hasEmDash(playbook) || hasEmDash(settingsJson) || cleanNotes.some(hasEmDash)) {
@@ -3604,13 +3725,18 @@ async function applyDerivedSettings(target, suggestedJson, force) {
3604
3725
  ];
3605
3726
  }
3606
3727
  async function runOptimize(opts) {
3607
- const { logs, claudeMd } = await loadAuditInputs({ path: opts.path, claudeMdPath: opts.claudeMdPath });
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 });
3608
3735
  const report = analyzeUploads(logs);
3609
3736
  const cmd = analyzeClaudeMd(claudeMd);
3610
3737
  const { pillars } = scorePillars(report, cmd, {});
3611
3738
  const { plan } = await resolvePlan(join6(opts.home ?? homedir4(), ".costclaw"), opts.plan);
3612
3739
  const { fixes } = synthesize(report, pillars, cmd, plan);
3613
- const projectDir = opts.projectDir || process.cwd();
3614
3740
  const project = await scanProject(projectDir);
3615
3741
  const result = optimize({ report, scores: pillars, fixes, currentClaudeMd: claudeMd, project });
3616
3742
  const recoverable = cacheMissExposureUsd(report);
@@ -3656,6 +3782,7 @@ async function runOptimize(opts) {
3656
3782
  lines.push(`Wrote a suggested settings.json to ${settingsPath}`);
3657
3783
  lines.push("");
3658
3784
  lines.push(describeDetection(project));
3785
+ if (claudeMdSource) lines.push(`Existing CLAUDE.md: ${claudeMdSource}`);
3659
3786
  if (plan === "api") {
3660
3787
  lines.push(`Recoverable spend (cache-miss exposure): ${usd5(recoverable)}`);
3661
3788
  } else {
@@ -3999,7 +4126,7 @@ function parseBadgeFlags(args) {
3999
4126
  };
4000
4127
  }
4001
4128
  function cliVersion() {
4002
- return true ? "0.5.1" : "0.0.0-dev";
4129
+ return true ? "0.5.3" : "0.0.0-dev";
4003
4130
  }
4004
4131
  function helpText() {
4005
4132
  return [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "costclaw",
3
- "version": "0.5.1",
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": {