config-audit/commands/optimize.md
Kjell Tore Guttormsen 0b763f25c1 fix(commands): router dogfood — five seam defects, plus the placeholder class
Dogfooding `/config-audit` (the router) against the repo, fasit written before
any run (docs/router-fasit.local.md, untouched). Every claim below is measured
behaviour, not a reading of the source.

1. Bare `<target-path>` inside the step-3 fence is a shell REDIRECTION, not an
   argument. Measured in zsh: both CLIs failed before starting, no output file
   was written, and the echoed status was 1 — inside the band the router's own
   gate calls "continue normally". Quoting makes an unsubstituted placeholder
   reach argv, so it fails in the CLI where the exit code means something.
   Swept the whole class: 30 sites across 12 further command files, since a
   defect in one file is a class until the opposite is measured. New guard:
   command-placeholder-shell-safety.test.mjs.

2. The orchestrator's exit code was discarded. Two commands on one line share a
   single trailing `echo $?`, which reports only the last: measured, an
   orchestrator exit 3 echoed as posture's 0, so the "3 -> stop" gate could
   never fire. Both statuses are now captured and echoed.

3. "Running 12 configuration scanners" — the orchestrator registers 16. The new
   test binds the narrated count to the registry so the next scanner added
   cannot re-stale it silently.

4. The Area Breakdown table hardcoded 7 rows; posture emits 9 quality areas.
   Token Efficiency (a B on this repo) and Plugin Hygiene never reached the
   user. Rows added, and the row set is now asserted against lib/scoring.mjs.
   Label aligned: "MCP Servers" -> "MCP", as posture emits it.

5. Step 6 rendered "the headline line from the humanized stderr scorecard" and
   forbade deriving a replacement — while step 3 sent posture's stderr to
   /dev/null, as UX rule 2 requires, and the prose is absent from the JSON
   payload (measured). The slot could only be improvised. posture's stderr now
   goes to a file in the session dir, as commands/posture.md already did; the
   user still never sees raw scanner output.

Also: `grep -q -- "--raw"` matched any argument CONTAINING --raw (measured on
`--rawdog` and on a path with --raw in it) — anchored to whole arguments.
SCOPE_FLAGS renamed SCOPE_FLAG, since zsh does not word-split and the plural
invited the M-BUG-45 shape.

command-shell-state-shape.test.mjs only recognised line-initial assignments, so
it reported the idiomatic `node …; STATUS=$?` capture as never assigned. Widened
to assignments after a separator; verified it still fails on a real cross-block
reference before trusting it.

Suite 1477 -> 1483, frozen v5.0.0 snapshots untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YDAwy1ZXRpZxht1wyCeSbF
2026-08-09 21:18:05 +02:00

6.3 KiB
Raw Blame History

name description argument-hint allowed-tools model
config-audit:optimize Optimization lens — config that works but would fit a better mechanism (procedure→skill, lifecycle→hook, path→rule, never→permission) [path] Read, Write, Glob, Grep, Bash, Agent opus

Config-Audit: Optimization Lens

The "is the config optimal?" axis (vs. the health scanners' "is it correct?"). It finds configuration that works but uses a mechanism a better one would fit — and frames every one as a Missed opportunity, never a mistake.

Mechanism-fit rules come from the provenance-stamped best-practices register (knowledge/best-practices.json); only CONFIRMED rules are surfaced. The motor is hybrid: a cheap deterministic pre-filter finds candidates, then the opus optimization-lens-agent judges each in context (precision-gated).

What the user gets

  • Procedures → skills (deterministic, CA-OPT-001)
  • Lifecycle phrasing → hooks (BP-MECH-001)
  • Unscoped path-specific instructions → path-scoped rules (BP-MECH-002)
  • Absolute "never" prohibitions → permissions / hooks (BP-MECH-004)
  • --subtract: instructions that no longer earn their always-loaded rent (BP-SUB-001)

Each finding cites its register rule + source URL. A clean CLAUDE.md returns "no opportunities" — that is a good result, not a failure.

Implementation

Step 1: Determine target

Split $ARGUMENTS into a path (first non-flag argument; default: current working directory) and flags. Recognized flags: --global (include the user ~/.claude cascade in discovery) and --subtract (add the subtraction axis, below).

--subtract — the inverse question. Every other lens asks what to add or move; this one asks what no longer earns its always-loaded rent. It is opt-in because it asks something different, and because deleting is not undoable by reading. Pair it with --global to reach the user-level CLAUDE.md, where the always-loaded cost actually sits (it loads in every repo, every session).

If --subtract is present, say so up front:

Also running the subtraction axis — instructions that cost tokens every turn
without telling me anything I couldn't work out. Load-bearing local facts
(remotes, versions, paths, policy) are excluded before anything is judged.

Tell the user:

## Optimization Lens

Looking for configuration that works but would fit a better Claude Code mechanism...

Step 2: Run the lens CLI

Generate a session ID (YYYYMMDD_HHmmss) if no active session exists.

mkdir -p ~/.claude/config-audit/sessions/{session-id} 2>/dev/null
GLOBAL_FLAG=""
if echo "$ARGUMENTS" | grep -q -- "--global"; then GLOBAL_FLAG="--global"; fi
SUBTRACT_FLAG=""
if echo "$ARGUMENTS" | grep -q -- "--subtract"; then SUBTRACT_FLAG="--subtract"; fi
node ${CLAUDE_PLUGIN_ROOT}/scanners/optimize-lens-cli.mjs "<target-path>" --output-file ~/.claude/config-audit/sessions/{session-id}/optimize-lens.json $GLOBAL_FLAG $SUBTRACT_FLAG 2>/dev/null; echo $?

Exit code 0 is normal. Only exit code 3 is a real error → "The lens couldn't run. Check that the path exists and contains a CLAUDE.md."

Step 3: Read the payload

Read ~/.claude/config-audit/sessions/{session-id}/optimize-lens.json with the Read tool. It has deterministic (already-confirmed OPT findings), candidates (pre-filter candidates with register provenance), register, and counts.

Under --subtract it also has a subtract block (candidates, register) and counts.subtractCandidates. Each subtraction candidate spans lineendLine (a whole block). Include the whole subtract block when spawning the agent.

Early exit: if counts.deterministic === 0 and counts.candidates === 0 (and, under --subtract, counts.subtractCandidates === 0), skip the agent and tell the user plainly:

✓ No mechanism-fit opportunities found.

Your CLAUDE.md holds facts, not procedures/automation/prohibitions that would be
better as skills, hooks, rules, or permissions. Nothing to change here.

Then go to Step 5.

Step 4: Spawn the precision gate

Tell the user what's happening and set expectations:

Found {counts.candidates} candidate line(s) + {counts.deterministic} deterministic finding(s).
Asking the optimization-lens agent to judge each in context (~20-40 seconds)...

Spawn the optimization-lens-agent (Agent tool) with:

  • the full payload from Step 3 (deterministic + candidates + register),
  • the session directory path so it can write optimization-lens-report.md.

The agent reads the actual CLAUDE.md, drops low-confidence candidates, and keeps only genuine opportunities — each citing its register rule + source.

Step 5: Present results

Read the agent's optimization-lens-report.md and present it formatted (markdown tables / grouped sections). Follow the UX rules: never show raw JSON or scanner progress; lead with a one-sentence summary of what was found before the detail. Make clear these are LOW-severity opportunities.

If the agent kept nothing from the candidates (all dropped) but there were deterministic findings, show those; if it kept nothing at all, show the clean result from Step 3.

Step 6: Next steps

End with context-sensitive next steps, explaining WHY each is useful:

  • /config-audit plan — turn the kept opportunities into an action plan with backups before you change anything.
  • /config-audit feature-gap — the complementary lens: features you don't use yet (this command is about mechanisms you do use that could fit better).
  • Re-run /config-audit optimize anytime after editing CLAUDE.md.

Notes

  • This command is agent-driven and not byte-stable — its output is a human-facing report, deliberately outside the deterministic snapshot suite.
  • --subtract proposes, never writes. Nothing is deleted; act on a finding via /config-audit plan/config-audit implement (backup + rollback).
  • The subtraction floor is deterministic and runs before the agent, so a load-bearing block is never a candidate. It errs toward keeping: on a well-maintained config this axis is mostly a no-op, and that is a good result.
  • The deterministic half (CA-OPT-001) also rides in the normal orchestrated audit; this command adds the prose-judgment half on top.
  • No files are modified. To act on a finding, use /config-audit plan/config-audit implement (backup + rollback) or edit by hand.