costclaw 0.5.6 → 0.5.7

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 +157 -6
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1666,10 +1666,9 @@ function stackOneLiner(stack) {
1666
1666
  }
1667
1667
  return `A ${joined} project.`;
1668
1668
  }
1669
- function buildClaudeMd(input, recoverable, notes, stats) {
1669
+ function buildClaudeMd(input, recoverable, notes, stats, carried) {
1670
1670
  const { currentClaudeMd, project } = input;
1671
1671
  const { title, intro, introDropped } = carriedIntro(currentClaudeMd);
1672
- const carried = carriedSections(currentClaudeMd);
1673
1672
  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.";
1674
1673
  const parts = [];
1675
1674
  parts.push(
@@ -2110,24 +2109,167 @@ function buildSettings(project) {
2110
2109
  const settings = { permissions: { allow, deny: [...BASE_DENY] } };
2111
2110
  return clean(JSON.stringify(settings, null, 2) + "\n");
2112
2111
  }
2112
+ function buildHarnessPrompt(input, carried) {
2113
+ const project = input.project;
2114
+ const name = project?.name?.trim() || "this project";
2115
+ const findings = input.report.findings;
2116
+ const has = (id) => findings.some((f) => f.ruleId === id);
2117
+ const heavyTools = [
2118
+ ...new Set(
2119
+ findings.filter((f) => f.ruleId === "HEAVY_TOOL_LOOPS").map((f) => String(f.evidence?.["tool"] ?? "")).filter((t) => t.length > 0)
2120
+ )
2121
+ ];
2122
+ const parts = [];
2123
+ parts.push(`# CostClaw harness upgrade for ${name}`);
2124
+ parts.push(
2125
+ [
2126
+ "You are Claude Code, working in this repository. costclaw generated this",
2127
+ "work order from a local audit of real usage logs plus a scan of this",
2128
+ "project; every task is grounded in that evidence. Execute the tasks in",
2129
+ "order and report what changed at the end."
2130
+ ].join("\n")
2131
+ );
2132
+ parts.push(
2133
+ [
2134
+ "## Ground rules",
2135
+ "",
2136
+ "- Never invent project facts. When a merge choice is unclear, ask.",
2137
+ "- Show a diff before replacing any human-written file, and back the",
2138
+ " file up first (CLAUDE.md -> CLAUDE.md.bak).",
2139
+ "- Keep the final CLAUDE.md lean: under about 200 lines, and every line",
2140
+ ' must pass "would removing this make the agent make a mistake?"',
2141
+ "- Commit per this repo's conventions when a task completes cleanly."
2142
+ ].join("\n")
2143
+ );
2144
+ parts.push(
2145
+ [
2146
+ "## Task 1: Merge the optimized CLAUDE.md",
2147
+ "",
2148
+ "Merge `costclaw-out/CLAUDE.optimized.md` into `./CLAUDE.md`:",
2149
+ "",
2150
+ "- Conflicting facts: the existing CLAUDE.md wins (it is the human's).",
2151
+ "- Structure and curation: the optimized file wins (section set, command",
2152
+ " curation, dropped filler).",
2153
+ "- Commands: start from the optimized curated list with its annotations;",
2154
+ " re-add a dropped script only when the workflow genuinely needs it",
2155
+ " pinned in an always-loaded file.",
2156
+ "- Strip the generator's HTML comment blocks from the final file.",
2157
+ "- Back up, show the diff, then write."
2158
+ ].join("\n")
2159
+ );
2160
+ parts.push(
2161
+ [
2162
+ "## Task 2: Permission allowlist",
2163
+ "",
2164
+ "Review `costclaw-out/settings.suggested.json` (read-only tools plus",
2165
+ "this project's safe commands; denies secret reads and destructive",
2166
+ "ops). Merge the rules you agree with additively into",
2167
+ "`.claude/settings.json`, keeping every existing entry.",
2168
+ "`npx costclaw optimize --apply-settings` performs the mechanical merge",
2169
+ "with a backup and a diff if you prefer."
2170
+ ].join("\n")
2171
+ );
2172
+ let n = 3;
2173
+ if (carried.droppedVolatile.length > 0) {
2174
+ parts.push(
2175
+ [
2176
+ `## Task ${n++}: Rehome volatile content`,
2177
+ "",
2178
+ `The optimized file left out ${joinAnd(carried.droppedVolatile)}:`,
2179
+ "status-style sections churn on every update and bust the prompt",
2180
+ "cache. Move that content from the old CLAUDE.md into docs/ (or a",
2181
+ "scratch note the agent opens on demand) so nothing is lost when the",
2182
+ "merge lands."
2183
+ ].join("\n")
2184
+ );
2185
+ }
2186
+ if (project?.preCommit) {
2187
+ parts.push(
2188
+ [
2189
+ `## Task ${n++}: Enforce the pre-commit gate`,
2190
+ "",
2191
+ `The project's gate is: ${project.preCommit}.`,
2192
+ "Prose in CLAUDE.md is advisory; make the gate real. If no pre-commit",
2193
+ "hook runs it, add one in the repo's existing style (husky, a plain",
2194
+ ".git/hooks script, or the project's hook framework), and verify it",
2195
+ "fires on a throwaway commit."
2196
+ ].join("\n")
2197
+ );
2198
+ }
2199
+ if (heavyTools.length > 0) {
2200
+ parts.push(
2201
+ [
2202
+ `## Task ${n++}: Stop-condition guardrails`,
2203
+ "",
2204
+ `The audit found sessions where ${joinAnd(heavyTools)} fired far past`,
2205
+ "a sane loop bound. Add one line to CLAUDE.md's workflow section",
2206
+ "telling the agent to state a stop condition before search or edit",
2207
+ "loops, and consider a PreToolUse hook that warns on a high",
2208
+ "per-session count for those tools."
2209
+ ].join("\n")
2210
+ );
2211
+ }
2212
+ if (has("MODEL_MISROUTE")) {
2213
+ parts.push(
2214
+ [
2215
+ `## Task ${n++}: Model routing defaults`,
2216
+ "",
2217
+ "The audit found short, trivial sessions running on premium models",
2218
+ "(Opus, Fable). Add a routing line to CLAUDE.md (cheap model for",
2219
+ "short, simple tasks; premium for hard reasoning), and if the user",
2220
+ "agrees, set shell defaults (ANTHROPIC_MODEL,",
2221
+ "CLAUDE_CODE_EFFORT_LEVEL) so sessions start right without a",
2222
+ "per-session step."
2223
+ ].join("\n")
2224
+ );
2225
+ }
2226
+ if (has("MARATHON_SESSION")) {
2227
+ parts.push(
2228
+ [
2229
+ `## Task ${n++}: Marathon-session hygiene`,
2230
+ "",
2231
+ "The audit found long sessions whose cache decayed turn over turn.",
2232
+ "Add a compact-between-tasks line to CLAUDE.md: run /compact before",
2233
+ "the context outgrows the cache, and /clear between unrelated tasks."
2234
+ ].join("\n")
2235
+ );
2236
+ }
2237
+ const verify = ["## Verify", ""];
2238
+ const testCmd = project?.commands.find((c) => c.label === "test")?.command;
2239
+ const lintCmd = project?.commands.find((c) => c.label === "lint")?.command;
2240
+ if (testCmd) verify.push(`- Run \`${testCmd}\` and read the output.`);
2241
+ if (lintCmd) verify.push(`- Run \`${lintCmd}\` and read the output.`);
2242
+ verify.push(
2243
+ "- Confirm CLAUDE.md is under about 200 lines with no generator comments",
2244
+ " left in it.",
2245
+ "- After about a week of normal usage, run `npx costclaw audit`: the",
2246
+ ' "Since optimize" line measures the cache-hit movement these changes',
2247
+ " are meant to produce."
2248
+ );
2249
+ parts.push(verify.join("\n"));
2250
+ return clean(parts.join("\n\n").trimEnd() + "\n");
2251
+ }
2113
2252
  function optimize(input) {
2114
2253
  const recoverable = cacheMissExposureUsd(input.report);
2115
2254
  const notes = [];
2116
2255
  const stats = { placeholders: 0 };
2117
- const optimizedClaudeMd = buildClaudeMd(input, recoverable, notes, stats);
2256
+ const carried = carriedSections(input.currentClaudeMd);
2257
+ const optimizedClaudeMd = buildClaudeMd(input, recoverable, notes, stats, carried);
2118
2258
  const playbook = buildPlaybook(input.report, input.fixes, input.scores, recoverable, input.project);
2119
2259
  const settingsJson = buildSettings(input.project);
2260
+ const harnessPrompt = buildHarnessPrompt(input, carried);
2120
2261
  const cleanNotes = notes.map((n) => clean(n));
2121
- if (hasEmDash(optimizedClaudeMd) || hasEmDash(playbook) || hasEmDash(settingsJson) || cleanNotes.some(hasEmDash)) {
2262
+ if (hasEmDash(optimizedClaudeMd) || hasEmDash(playbook) || hasEmDash(settingsJson) || hasEmDash(harnessPrompt) || cleanNotes.some(hasEmDash)) {
2122
2263
  return {
2123
2264
  optimizedClaudeMd: sanitizeText(optimizedClaudeMd),
2124
2265
  playbook: sanitizeText(playbook),
2125
2266
  settingsJson: sanitizeText(settingsJson),
2267
+ harnessPrompt: sanitizeText(harnessPrompt),
2126
2268
  notes: cleanNotes.map(sanitizeText),
2127
2269
  placeholdersRemaining: stats.placeholders
2128
2270
  };
2129
2271
  }
2130
- return { optimizedClaudeMd, playbook, settingsJson, notes: cleanNotes, placeholdersRemaining: stats.placeholders };
2272
+ return { optimizedClaudeMd, playbook, settingsJson, harnessPrompt, notes: cleanNotes, placeholdersRemaining: stats.placeholders };
2131
2273
  }
2132
2274
 
2133
2275
  // ../../packages/engine/src/audit.ts
@@ -3732,6 +3874,7 @@ async function scanProject(dir) {
3732
3874
  var OPTIMIZED_NAME = "CLAUDE.optimized.md";
3733
3875
  var PLAYBOOK_NAME = "PLAYBOOK.md";
3734
3876
  var SETTINGS_NAME = "settings.suggested.json";
3877
+ var HARNESS_NAME = "HARNESS-UPGRADE.md";
3735
3878
  function usd5(n) {
3736
3879
  return `$${(Math.round(n * 100) / 100).toFixed(2)}`;
3737
3880
  }
@@ -3870,6 +4013,8 @@ async function runOptimize(opts) {
3870
4013
  await writeFile3(playbookPath, result.playbook, "utf8");
3871
4014
  const settingsPath = join6(outDir, SETTINGS_NAME);
3872
4015
  await writeFile3(settingsPath, result.settingsJson, "utf8");
4016
+ const harnessPath = join6(outDir, HARNESS_NAME);
4017
+ await writeFile3(harnessPath, result.harnessPrompt, "utf8");
3873
4018
  const lines = [];
3874
4019
  lines.push("CostClaw optimize");
3875
4020
  lines.push("");
@@ -3891,6 +4036,7 @@ async function runOptimize(opts) {
3891
4036
  lines.push(`Wrote the optimized CLAUDE.md to ${target}`);
3892
4037
  lines.push(`Wrote the playbook to ${playbookPath}`);
3893
4038
  lines.push(`Wrote a suggested settings.json to ${settingsPath}`);
4039
+ lines.push(`Wrote the Claude Code work order to ${harnessPath}`);
3894
4040
  lines.push("");
3895
4041
  lines.push("Diff (old vs new):");
3896
4042
  lines.push(lineDiff(original, result.optimizedClaudeMd));
@@ -3906,6 +4052,7 @@ async function runOptimize(opts) {
3906
4052
  lines.push(`Wrote the optimized CLAUDE.md scaffold to ${optimizedPath}`);
3907
4053
  lines.push(`Wrote the playbook to ${playbookPath}`);
3908
4054
  lines.push(`Wrote a suggested settings.json to ${settingsPath}`);
4055
+ lines.push(`Wrote the Claude Code work order to ${harnessPath}`);
3909
4056
  lines.push("");
3910
4057
  lines.push(describeDetection(project));
3911
4058
  if (claudeMdSource) lines.push(`Existing CLAUDE.md: ${claudeMdSource}`);
@@ -3930,6 +4077,10 @@ async function runOptimize(opts) {
3930
4077
  lines.push("To apply it in place (with a backup and a diff first), rerun with --write <your-CLAUDE.md>.");
3931
4078
  lines.push("The settings.suggested.json is a conservative permission allowlist. Review it, then merge the rules you want into ~/.claude/settings.json or the project's .claude/settings.json. It is never applied unless you ask.");
3932
4079
  lines.push("To merge it into the project's .claude/settings.json for you (with a backup and a diff first), rerun with --apply-settings.");
4080
+ lines.push("");
4081
+ lines.push("To apply everything with AI judgment, hand the work order to your own Claude Code:");
4082
+ lines.push(' claude "Follow costclaw-out/HARNESS-UPGRADE.md"');
4083
+ 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.");
3933
4084
  }
3934
4085
  if (opts.applySettings) {
3935
4086
  const target = opts.applySettingsPath || join6(projectDir, ".claude", "settings.json");
@@ -4271,7 +4422,7 @@ function parseBadgeFlags(args) {
4271
4422
  };
4272
4423
  }
4273
4424
  function cliVersion() {
4274
- return true ? "0.5.6" : "0.0.0-dev";
4425
+ return true ? "0.5.7" : "0.0.0-dev";
4275
4426
  }
4276
4427
  function helpText() {
4277
4428
  return [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "costclaw",
3
- "version": "0.5.6",
3
+ "version": "0.5.7",
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": {