mechanical-refactor-verify

Make mechanical refactoring (file splits, function moves, module extractions, renames) machine-checkable instead of eyeballed. Reproduce a relocation commit byte-for-byte from faithful primitives, and split an extraction into a verifiable prepare + move + postpare. Use when doing or reviewing such changes.

Install
npx skills add 'https://github.com/sgl-project/sglang/tree/main/.claude/skills/mechanical-refactor-verify'
Download bundle ↓
main · a9fb1c3Scanned 2026-09-17

Contributors

GitHub-linked commit authors for this SKILL.md at the saved revision. Co-authors and history before file renames are not included.

File history ↗

guide-modify-skill.md

guide-modify-skill.mdBrowse 41 files
View on GitHub
← Back to SKILL.md

Modify this skill: the engine, the generator, or the spec

1. What has which bar

FileRoleBar
spec-reproduction-utils.mdnormative source of truth (SKILL.md §2): the clean-move property, the whitelist / not-allowed lists, each primitive's contract, the arbiterany behavior change lands here first
spec-reproduction-cli.mdnormative source of truth for the chain verifier: the word rule, the proof obligation, the report, the exit codesany behavior change lands here first
mechanical_refactor_reproduction_utils.pytrusted engine: the relocation primitives + the arbiterhighest — byte-faithfulness proven by tests
mechanical_refactor_proof_generator.pyconvenience: infers a recipe from a difflower — may report RESIDUAL/UNSUPPORTED without compromising trust, but still tested
mechanical_refactor_reproduction_cli.pygatekeeper: walks a chain, runs the proofs, reportshigh — a false chain PASS certifies an unproven commit; classification, resolution, and PASS-criterion behavior proven by tests
guide-*.md, SKILL.mdworkflow + file mapkept in sync, never describe behavior the code lacks

2. Cardinal rule — the spec leads, code follows

  • spec-reproduction-utils.md wins over every other file (SKILL.md §2). Code serves the spec, not the reverse.
  • A behavior change to a primitive, to §2.1/§2.2 (what counts as a clean move), or to the §4 arbiter must edit the spec in the same commit as the code. Code and spec never diverge across commits.
    • New primitive → add its contract to §3.
    • Changed clean-move boundary → edit §2.1 (allowed) / §2.2 (not allowed).
    • Changed reproduce/diff behavior → edit §4.
  • If you discover code and spec already disagree, the spec is authoritative: fix the code to match — or, if the spec itself is wrong, change it deliberately in one commit with the reasoning, not as a silent side effect.

3. The faithfulness invariant — never break this

  • Every primitive relocates original source bytes: AST-located, spliced as the source text that was there, never regenerated. A byte match after the formatter is the entire proof — the moment a primitive regenerates instead of splicing, the proof is worthless (it can no longer distinguish "moved" from "rewritten to look moved").
  • A new or changed primitive must:
    • locate its target through the AST, not by string search over source;
    • splice the original bytes (interiors of multi-line strings, comments, a magic trailing comma, semicolon-joined statements all survive verbatim);
    • preserve the file's newline style (CRLF round-trips) and UTF-8-byte-accurate columns.
  • Do not add a primitive that normalizes, reflows, or reformats. Formatting belongs to the pre-commit pass in the §4 arbiter, applied to both sides; primitives do relocation only.
  • When the generator cannot infer a move, the answer is a hand-written Repro (guide-construct-proof §2.3) — not loosening a primitive to make it fit.

4. Testing rules — the hard bar

  • The engine is trusted, so an untested change to it is not acceptable. "It ran once" is not a test.

  • Run the full suite and keep it green:

    cd scripts && uv run --with pytest --python 3.12 python -m pytest tests/ -q

    Baseline at the time of writing: 188 passed. Your change must leave the count at or above baseline — never delete a case to make the suite pass.

  • Layout mirrors the modules; put your test where it belongs:

    • tests/reproduction_utils/ — one test_<primitive>.py per engine primitive.
    • tests/proof_generator/ — the inference layer (test_infer_*, test_script_and_diff).
    • tests/reproduction_cli/ — the chain verifier (classification, proof discovery, chain walking, the report).
  • A new or changed primitive requires, in its test_<primitive>.py:

    • a byte-exact assertion on the resulting file (compare full bytes, not "contains");
    • at least one adversarial case where a regenerating implementation would differ from splicing — a comment mid-body, a magic trailing comma, odd indentation, a semicolon-joined import, non-ASCII text, or a CRLF file — asserting the original bytes survive;
    • the raise paths the spec promises: ambiguous anchor raises, missing anchor raises, wrong/absent from_class raises.
  • A change to the generator requires a tests/proof_generator/ case that runs it on a synthetic commit and asserts PASS, plus one non-move / bundled-change case that asserts RESIDUAL or UNSUPPORTED — so a future regression that makes it "pass" a dirty commit is caught.

  • A change to the chain verifier requires a tests/reproduction_cli/ case asserting a verified chain passes, plus one asserting the broken shape it guards (an unclassified commit, a missing proof, a failing proof) still fails — so a regression cannot silently green a dirty chain.

  • The engine stays self-contained: mechanical_refactor_reproduction_utils.py imports only git (via subprocess) and the standard library. Do not add a third-party dependency to it.

5. Before you commit — checklist

  • spec-reproduction-utils.md / spec-reproduction-cli.md edited in this same commit (if any behavior changed).
  • Full pytest suite green; case count ≥ prior baseline.
  • New/changed primitive has: byte-exact test + ≥1 adversarial (regeneration-would-differ) case + the raise-path tests.
  • Generator change has a PASS test and a RESIDUAL/UNSUPPORTED test.
  • SKILL.md §3 file map and the relevant guide-*.md updated for any new file or workflow change.
  • mechanical_refactor_reproduction_utils.py still imports only git + stdlib.
Referenced from SKILL.md