/ratchet:ratchet runs instead · from ratchet
Runs optimization waves on a numeric metric (latency, throughput, memory, bundle size, eval score) using isolated git worktrees, a sealed out-of-tree judge, median-of-N scoring, and a minimum-gain bar to prevent jitter from moving the baseline.
This skill never runs in Claude Code
It ships in ratchet at skills/, but /ratchet:ratchet (a command, or a skill Claude Code finds first) has the same name: typing it, or Claude choosing it, runs that one instead.
One number, one sealed judge, waves of isolated candidates. Best only moves when a candidate clears a real, measured gain, so the number clicks forward and never slips back.
If the host harness exposes a native ratchet tool, drive its init/discover/wave/eval/apply/stop actions directly. Otherwise run the .ratchet/ protocol below.
Use it only when both hold:
Refuse waves on:
1. The judge is sealed out-of-tree. An agent sharing a tree with its grader will game the checks, loosen tolerances, or touch the timers.
python3 bench/run.py, ./scripts/bench.sh. Never an inline sh -c, python -c, or node -e string, because a string's bytes can't be hashed. Refuse sh|bash|zsh|dash|ksh|fish|csh|tcsh|eval with -c or with no script, node|ruby|perl with -e|-c|--eval, and python* with -c.base_commit, hash every judge script, harness, and fixture into .ratchet/sealed/<sha256>. Measure only in the detached .ratchet/judge-wt, never in a candidate's worktree.tampered. Reject it without running it.2. The inner loop is sub-second. Candidates spend about 95% of their tries on a sub-second smoke (or toy) check and on micro-probes under 60s inside their own worktree. They run the full judge only to confirm. A slot that pays full judge cost for every idea gets through a handful of ideas instead of hundreds.
3. Median-of-N plus a minimum-gain bar. A single run rewards noise.
runs = 3 (max 9).(max − min) / |median|. If it exceeds min_gain_pct (default 3.0), raise runs or min_gain_pct before any wave, or jitter will land as wins.min_gain_pct moves refs/ratchet/best.4. Mechanisms and model families stay heterogeneous. Never same-model debate. Copies of one model arguing produce conformity, not search.
ratchet/w<N>-<I>), each in its own worktree off refs/ratchet/best, each on a distinct mechanism.Agent subagents for any family that is missing.5. Near-misses are kept and dead ends are logged.
0 < gain_pct < min_gain_pct) doesn't move best; that keeps jitter out. Its diff is saved as .ratchet/patches/w<N>-s<I>.patch (near_miss).dead_end, and every later brief lists them so no slot repeats them..ratchet/ sits at the repo root and is ignored through $(git rev-parse --git-common-dir)/info/exclude, not .gitignore. Editing .gitignore would dirty the tree that init and apply both need clean. The ref refs/ratchet/best always points at the best measured commit.
.ratchet/
manifest.json # the run: judge, numbers, waves, slots
history.jsonl # one line per scored candidate, append-only
sealed/<sha256> # frozen bytes of every sealed file
patches/w<N>-s<I>.patch
judge-wt/ # detached worktree; the only place the judge runs
worktrees/w<N>-<I>/ # one per candidate slot
manifest.json:
{
"goal": "p50 latency of /search",
"judge": ["python3", "bench/run.py"],
"smoke": ["python3", "bench/smoke.py"],
"sealed": [{ "path": "bench/run.py", "sha256": "<hex>", "executable": false }],
"direction": "min",
"min_gain_pct": 3.0, "runs": 3, "timeout_secs": 300,
"base_commit": "<sha>", "baseline_score": 41.2,
"spread": { "min": 40.8, "max": 41.9, "pct": 2.7 },
"best_commit": "<sha>", "best_score": 41.2,
"waves": [{ "num": 1, "base_commit": "<sha>", "base_score": 41.2, "slots": [{
"index": 1, "family": "codex", "branch": "ratchet/w1-1",
"worktree": ".ratchet/worktrees/w1-1", "hypothesis": "…", "owned_paths": [],
"evaluated": false, "verdict": null, "score": null, "commit": null
}]}],
"stopped": false
}
history.jsonl, one line per scored candidate:
{"at":"<iso>","wave":1,"slot":2,"family":"agent","hypothesis":"…","verdict":"near_miss","score":40.1,"against_score":41.2,"gain_pct":2.67,"commit":"<sha>","patch":".ratchet/patches/w1-s2.patch","note":"below the 3.0% bar"}
verdict is one of win | near_miss | dead_end | no_change | tampered | smoke_failed | out_of_scope.
Math. direction is min or max. Gain is relative to the score a candidate is compared against. For min: (against − score) / |against| × 100. For max: (score − against) / |against| × 100. The win target is best ∓ |best| × min_gain_pct / 100.
Judge output. The last METRIC=<number> on stdout wins, so a judge can log freely before it reports. Without that marker, the last number on the last non-empty line is used. A run that exits non-zero, times out (timeout_secs, default 300; smoke 120), or prints no number is not a score.
/ratchet <goal> (autonomous)Run init. Run discover if the hot path isn't obvious. Then loop wave → wait → eval. Stop after 2 consecutive waves with no win, or once the target is hit. Report baseline → best (gain%), then apply if the tree is clean, or tell the user to run apply if it isn't. Read the judge, smoke, and sealed paths from the user's message. If no judge script exists, write one, commit it, and seal it before measuring.
initHEAD, so uncommitted edits would sit in no candidate's base.judge and smoke (invariant 1).git ls-files). If the result is empty, refuse. For each path, run git show <base>:<path> | shasum -a 256, then copy the bytes to .ratchet/sealed/<sha256>..ratchet/ to info/exclude if it's missing.git worktree add --detach .ratchet/judge-wt <base>.runs times in judge-wt. Record the median and spread. If spread > min_gain_pct, say so and raise runs or the bar before any wave.manifest.json with best = baseline. Run git update-ref refs/ratchet/best <base>.If the baseline fails, remove judge-wt and record nothing, so the retry with a fixed judge starts clean.
discoverLaunch 4 read-only scouts in parallel. Each reports in ≤25 lines, every fact cited file:line, ending with the one mechanism it would try first and the % it expects.
Scouts receive the goal, direction, best and baseline, judge, smoke, and sealed paths, and may not edit anything. If wave 1 has an obvious hot path to attack now, it doesn't wait for scouts.
wave [count]count is 3 by default, clamped to 1–6.
git worktree add -B ratchet/w<N>-<I> .ratchet/worktrees/w<N>-<I> <best_commit>. -B resets a reused branch to the base.owned_paths when the cost splits cleanly by file. A sliced slot's whole job is its slice. Slices must not overlap each other or a sealed path.Agent with run_in_background: true and its cwd set to the slot worktree.<best> from ~/.codex/models_cache.json, as the codex skill does, and omit -m if nothing resolves.
codex exec -m <best> -c model_reasoning_effort=xhigh -s workspace-write \
-C .ratchet/worktrees/w<N>-<I> -o "$TMPDIR/ratchet-w<N>-<I>.md" "$(cat brief)" </dev/null
</dev/null stops codex exec from blocking on stdin when it runs in the background.(cd .ratchet/worktrees/w<N>-<I> && gemini --yolo -p "$(cat brief)" </dev/null > "$TMPDIR/ratchet-w<N>-<I>.md")
Agent.git commit, commit its worktree yourself and use its HYPOTHESIS: line as the message.Candidate brief template:
You are slot <I> of wave <N> in a measured optimization ratchet. Work only inside this
worktree: never the main checkout, another slot's worktree, or .ratchet/.
GOAL: <goal>
DIRECTION: <lower|higher> is better. The judge prints one number.
THE NUMBERS
- baseline: <baseline_score> · current best: <best_score> at <best_commit>
- a win must reach <target> (≥<min_gain_pct>% better). A real gain short of that is kept
as a near-miss patch for a later synthesizer, so an honest small win is worth committing.
A fabricated one is not.
- every candidate is scored as the median of <runs> runs. One lucky run decides nothing.
YOUR SLOT
<role or hypothesis>
HOW TO ITERATE
- inner loop: `<smoke>`. It is sub-second, so run it after every edit and iterate on it
plus micro-probes under 60s.
- the real number: `<judge>`, the same judge eval runs. Confirm with it before claiming a win.
- OWNED SLICE: <owned_paths>. Touch only these. Any commit outside them is rejected unscored.
- SEALED FILES: <sealed paths>. Any commit that edits, moves, or deletes one is rejected
unscored. If the judge looks wrong, say so in your report instead.
WHAT HAS BEEN TRIED
- wins: <last 8 history lines>
- near-misses (patches in .ratchet/patches/; check with `git apply --stat`, combine or repair
if orthogonal to your idea): <last 6>
- dead ends (never repeat without a new mechanism): <last 12>
RULES
- one mechanism per attempt. Write it down before you edit.
- commit with `git add -A && git commit -m "<mechanism and expected win>"`.
No co-author or AI-attribution trailer. Never amend or rebase the base commit.
- if nothing works, leave the branch at its base and say what you ruled out. A clean
no-change costs one slot. A fabricated number costs the whole run.
- end your report with exactly one line: `HYPOTHESIS: <what you changed and the % win you expect>`
Drop the OWNED SLICE line when the slot has no slice and the inner loop line when there is no smoke. In wave 1, WHAT HAS BEEN TRIED reads "nothing yet; the mechanism is yours to choose."
eval [ref]With no argument, score every finished, unevaluated slot against its wave's base_score. That base is the commit the slot branched from, so a sibling's earlier win doesn't shift the bar. With a branch or commit, score that one ref against the live best as w<N>-manual.
For each candidate, in order:
no_change.git diff --name-only <base>..<head>, or whose blob hash no longer matches its seal, is tampered. Don't run it.owned_paths and the diff touches anything outside them, the verdict is out_of_scope. Don't run it.git -C .ratchet/judge-wt checkout --detach --force <head>. Overwrite every sealed path from .ratchet/sealed/<sha256>, mode included, and verify its SHA-256. The worktree is reused, so build caches survive between candidates.smoke_failed, with the output excerpt in note.runs times. Take score as the median and compute gain_pct against the candidate's base. A failed run is dead_end, with the error in note.gain_pct ≥ min_gain_pct is win. 0 < gain_pct < min_gain_pct is near_miss. Anything else is dead_end. For a win or near-miss, write git diff <base>..<head> to .ratchet/patches/w<N>-s<I>.patch and pin the commit at refs/ratchet/candidates/w<N>-s<I>, so the synthesizer can still apply it after the branch and worktree are pruned.Merging slices. If ≥2 sliced slots in the wave came out win or near_miss, merge their commits onto the wave base in the judge worktree, then smoke and score the merge as w<N>-slices. It's a win only if it clears the bar against the base and beats the strongest single slot. A merge that only carries its best part is not a discovery. If the merge conflicts, retry with the wins alone.
Record.
history.jsonl.best_score, best_commit, and refs/ratchet/best to the strongest win that still beats the live best, since an earlier eval in this wave may already have moved it.slot | family | hypothesis | median | gain | verdict, the patches kept, baseline → best, and which slots are still running.statusPrint:
runs, bar, and spreadbaseline → best (gain%) at the best commitapplyIf best_commit == base_commit, there's nothing to apply. Say so and stop.
HEAD is an ancestor of best_commit, run git merge --ff-only <best_commit>. Otherwise run git cherry-pick <base_commit>..<best_commit>. Print baseline → best and git diff --stat <base_commit> <best_commit>, prune judge-wt and every slot worktree, and set stopped: true.git branch -f ratchet/best <best_commit> instead and tell the user. Never stash or overwrite their changes.stopKill every running slot and scout, prune judge-wt and every slot worktree, and set stopped: true. Keep refs/ratchet/best, refs/ratchet/candidates/*, patches/, and history.jsonl, so the record of what was tried outlives the run.
eval in judge-wt against sealed bytes produces a score.stop and init again from a new commit.init on top of a live manifest would orphan its waves, so stop the live one first.apply.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub noahdunnagan/cafe --plugin ratchetOther plugins' skills for the same job