forge: DRY refactor queue_append_audit _grade_error + shutil.rmtree (verified byte-identical)

This commit is contained in:
forge committed 2026-10-22 05:59:38 +00:00
1 parent 009cd8525f
commit 2cd7e2cf50
3 files changed
+337 -48

No files matched your search

+21 -48
View File
@@ -70,6 +70,7 @@ import argparse
import io import io
import json import json
import os import os
import shutil
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
@@ -222,21 +223,7 @@ def audit_commit(repo, rev, queue, index, timeout):
row["verdict"] = "ERROR" row["verdict"] = "ERROR"
return row return row
finally: finally:
for root, dirs, files in os.walk(td, topdown=False): shutil.rmtree(td, ignore_errors=True)
for f in files:
try:
os.unlink(os.path.join(root, f))
except OSError:
pass
for d in dirs:
try:
os.rmdir(os.path.join(root, d))
except OSError:
pass
try:
os.rmdir(td)
except OSError:
pass
# ------------------------------------------------------------------ the range # ------------------------------------------------------------------ the range
@@ -268,28 +255,32 @@ def audit(repo, queue, index, rev_from, rev_to, guard=False, timeout=30,
"from": None, "to": None, "guard": bool(guard), "from": None, "to": None, "guard": bool(guard),
"auto_from": False, "pre_gate": 0, "auto_from": False, "pre_gate": 0,
"rows": [], "counts": {}, "graded": 0, "rc": None} "rows": [], "counts": {}, "graded": 0, "rc": None}
def _grade_error(msg):
"""Attach a grade error to the payload and return the (2, payload) verdict.
Every precondition failure in this function is the same three-statement
shape: set the message, set rc=2, return. Six inline copies were the
smell; this helper is the single source of that shape.
"""
payload["error"] = msg
payload["rc"] = 2
return 2, payload
if not os.path.isdir(repo): if not os.path.isdir(repo):
payload["error"] = "not a directory: %s" % repo return _grade_error("not a directory: %s" % repo)
payload["rc"] = 2
return 2, payload
if not is_repo(repo): if not is_repo(repo):
payload["error"] = "not a git repository: %s" % repo return _grade_error("not a git repository: %s" % repo)
payload["rc"] = 2
return 2, payload
to_sha = resolve(repo, rev_to) to_sha = resolve(repo, rev_to)
if to_sha is None: if to_sha is None:
payload["error"] = "unknown revision: %s" % rev_to return _grade_error("unknown revision: %s" % rev_to)
payload["rc"] = 2
return 2, payload
payload["to"] = to_sha payload["to"] = to_sha
from_sha = None from_sha = None
if rev_from: if rev_from:
from_sha = resolve(repo, rev_from) from_sha = resolve(repo, rev_from)
if from_sha is None: if from_sha is None:
payload["error"] = "unknown revision: %s" % rev_from return _grade_error("unknown revision: %s" % rev_from)
payload["rc"] = 2
return 2, payload
elif not all_history: elif not all_history:
rc, out, _ = git(repo, "log", "--diff-filter=A", "--format=%H", "-1", rc, out, _ = git(repo, "log", "--diff-filter=A", "--format=%H", "-1",
to_sha, "--", index) to_sha, "--", index)
@@ -304,17 +295,13 @@ def audit(repo, queue, index, rev_from, rev_to, guard=False, timeout=30,
rc, revs, err = queue_revs(repo, queue, from_sha, to_sha) rc, revs, err = queue_revs(repo, queue, from_sha, to_sha)
if rc != 0: if rc != 0:
payload["error"] = "git rev-list failed: %s" % ((err.strip().splitlines() or [""])[0]) return _grade_error("git rev-list failed: %s" % ((err.strip().splitlines() or [""])[0]))
payload["rc"] = 2
return 2, payload
if guard: if guard:
revs = revs[-1:] revs = revs[-1:]
if not revs: if not revs:
where = ("%s..%s" % (rev_from, rev_to) if rev_from else rev_to) where = ("%s..%s" % (rev_from, rev_to) if rev_from else rev_to)
payload["error"] = ("no commit in %s touches %s, so there is no index " return _grade_error("no commit in %s touches %s, so there is no index "
"currency to grade" % (where, queue)) "currency to grade" % (where, queue))
payload["rc"] = 2
return 2, payload
if payload["auto_from"] and not guard: if payload["auto_from"] and not guard:
rc_n, out_n, _ = git(repo, "rev-list", "--count", to_sha, "--", queue) rc_n, out_n, _ = git(repo, "rev-list", "--count", to_sha, "--", queue)
@@ -509,21 +496,7 @@ def selftest():
check("a report with a fault never prints a clean verdict", check("a report with a fault never prints a clean verdict",
"every graded commit" in buf.getvalue(), False) "every graded commit" in buf.getvalue(), False)
finally: finally:
for root, dirs, files in os.walk(base, topdown=False): shutil.rmtree(base, ignore_errors=True)
for f in files:
try:
os.unlink(os.path.join(root, f))
except OSError:
pass
for dd in dirs:
try:
os.rmdir(os.path.join(root, dd))
except OSError:
pass
try:
os.rmdir(base)
except OSError:
pass
bad = [c for c in cells if not c[1]] bad = [c for c in cells if not c[1]]
for name, ok, got, want in bad: for name, ok, got, want in bad:
@@ -0,0 +1,120 @@
# QUEUE_APPEND_AUDIT_DRY_REFACTOR_VERIFICATION
Tool: `tools/queue_append_audit.py` (forge lane: Refactor)
Original: `git show HEAD~1:tools/queue_append_audit.py` — byte-identical at HEAD and
HEAD~1 (both sha1 `a1b744949417cccc4bdbc78af2d061d5475034ea`); the refactor is
unstaged in the working tree, so the verifier reads a genuine original both before
the refactor lands and after it is committed (the `--selftest` commit 9f7bc3b only
ran ruff and changed nothing material).
Refactored: working tree (`tools/queue_append_audit.py`)
## The smell
Two families of repetition, both pure refactor targets:
1. **Manual temp-dir teardown, twice.** `audit_commit`'s `finally` and `selftest`'s
`finally` each ended in the same 9-line `os.walk(td, topdown=False)` loop that
`os.unlink`s every file, `os.rmdir`s every dir, and `os.rmdir`s the temp root,
each swallowed in a `try/except OSError: pass`. Two copies of a pattern the
stdlib already owns as `shutil.rmtree(..., ignore_errors=True)`. Teardown runs in
a `finally` *after* the graded `row` dict is built, so it can affect neither the row
nor any verdict.
2. **Six inline grade-error blocks, one shape.** `audit()` returned a `(2, payload)`
precondition failure six times, each spelling out:
```python
payload["error"] = "..."
payload["rc"] = 2
return 2, payload
```
Identical three-statement shape, differing only in the message string. The literal
`payload["error"] = msg; payload["rc"] = 2; return 2, payload` sequence is the unit
that was collapsed.
## The refactor
- Added `import shutil`.
- Replaced both `os.walk`/`unlink`/`rmdir` teardown blocks with
`shutil.rmtree(<dir>, ignore_errors=True)`.
- Replaced the six inline blocks in `audit()` with a single `_grade_error(msg)`
helper that sets `payload["error"] = msg`, `payload["rc"] = 2`, `return 2, payload`
-- the same fields, same order, same return -- and calls the helper at each site.
`git diff` for the change: two hunks for the teardowns, one hunk for the
`audit()` error-shape collapse, plus the `import shutil` line. No logic in the
grading path (`audit_commit` verdict derivation, `numsum`, the range rules) was
touched.
### Equivalence argument
- **Teardown is post-grading.** `shutil.rmtree` runs in the same `finally` slot the
`os.walk` loop occupied. The `row` dict is fully built before `finally` executes,
so the teardown change is invisible to the payload.
- **The error helper is a textual closure.** Each of the six call sites becomes
`return _grade_error("...")`; the helper sets exactly `payload["error"]`,
`payload["rc"]`, and returns `2, payload` -- the same three statements in the same
order, with the message threaded through. No field is added, dropped, reordered, or
retyped. `audit()` therefore returns the same `(rc, payload)` tuple for every input.
## Evidence (measured)
The proof lives in `wizard-work/forge/verify_queue_append_audit_dry_refactor.py`.
It materializes the `HEAD~1` blob as `queue_append_audit.py` in a temp dir and drives
the ORIGINAL module's `audit()` alongside the REFACTORED working-tree module on a
5-commit synthetic history (UNGRADABLE / PASS / STALE / PASS / unrelated) built by the
tool's own `_fixture`. Run output (captured 2026-10-22, this host):
```
[1] end-to-end audit() drift rc=1 rows=4 sha_orig=9d9292b55411 sha_ref=9d9292b55411 PASS
[2] --selftest drift guard rc=0 PASS
[3] redundancy (code lines) _grade_error def=1 calls=6 shutil.rmtree=2 os.walk=0 inline return-2-payload=1 (orig: os.walk=1 inline=6) PASS
VERDICT: ALL GREEN -- queue_append_audit audit() byte-identical to original, --selftest 25/25, 6 inline error blocks -> _grade_error + shutil.rmtree
```
> Note on the payload sha: the absolute value (`9d9292b55411` above) is
> **run-dependent**. The fixture commits git snapshots with live timestamps, so
> `audit()`'s payload (which records each commit's hash and author date) differs per
> run. What is verified is the **invariant** `sha_orig == sha_ref` on every run; the
> CHECK exits non-zero on any mismatch. The captured run shows both sides equal.
### Verdict breakdown
- **[1] END-TO-END audit() DRIFT.** The original (HEAD~1) and refactored working-tree
modules both `audit()` the SAME fixture repo in full-history mode. The payloads are
byte-identical -- `json.dumps(payload, sort_keys=True)` sha256 matches on both sides
(the absolute value is run-dependent; the invariant is orig == ref), and both exit
rc=1. The match is non-vacuous: the fixture grades exactly 4 queue-touching commits
with counts UNGRADABLE=1, PASS=2, STALE=1, so rc=1 is a real STALE verdict, not a
shared error/0.
- **[2] SELFTEST DRIFT GUARD.** `python3 tools/queue_append_audit.py --selftest` exits
0 on the refactored worktree -- the tool's own 25-cell contract
(UNGRADABLE/PASS/STALE/PASS selection, `--guard` on clean head and on the stale
commit, default-range auto-from, pre-gate count, empty-range rc=2, missing-repo and
unknown-rev rc=2, unknown-queue rc=2, `--json` round-trip) still holds 25/25.
- **[3] REDUNDANCY COLLAPSED.** Measured on code lines (comments/docstrings excluded):
| pattern (code lines) | original (HEAD~1) | refactored (tree) |
|-----------------------------------|-------------------|-------------------|
| `def _grade_error(` | 0 | 1 |
| `_grade_error(` call sites | 0 | 6 |
| `shutil.rmtree(` | 0 | 2 |
| `os.walk(td, topdown=False)` | 1 | 0 |
| `return 2, payload` (inline block)| 6 | 1 (helper only) |
The smell removed from the working tree is still present in the original baseline
(1 `os.walk` teardown shape, 6 inline `return 2, payload` blocks), confirming the
diff is non-trivial and the assertions are non-vacuous.
## Verdict
Behavior-preserving refactor: the repeated manual teardowns collapsed to
`shutil.rmtree` and the six inline grade-error returns collapsed to a single
`_grade_error` helper. `audit()` renders a byte-identical `(rc, payload)` on a 5-commit
fixture (sha256 matching on both sides, rc=1 STALE), the tool's own 25-cell
`--selftest` stays green, and the smell is gone from code lines while still present in
the original.
VERDICT: PASS -- `queue_append_audit.py` refactor is behavior-preserving; audit()
output byte-identical to original, --selftest 25/25, 6 inline error blocks + 2 manual
teardowns collapsed into `_grade_error` and `shutil.rmtree`.
@@ -0,0 +1,196 @@
#!/usr/bin/env python3
"""verify_queue_append_audit_dry_refactor.py
Equivalence proof for the behavior-preserving refactor in
tools/queue_append_audit.py (forge lane: Refactor).
Two behavior-preserving collapses were applied to the working tree:
(R1) CLEANUP -- the manual temp-dir teardown in `audit_commit` (an
`os.walk(td, topdown=False)` loop that `os.unlink`s every file and
`os.rmdir`s every dir, then `os.rmdir`s `td` itself, plus a second
copy of the same dance in `selftest`) collapsed to
`shutil.rmtree(td, ignore_errors=True)` at BOTH sites. Teardown runs
in a `finally` AFTER the graded row is built, so it changes neither the
row dict nor any verdict.
(R2) ERROR-HELPER -- six inline blocks of identical shape
`payload["error"] = msg; payload["rc"] = 2; return 2, payload` (the
three-stmt precondition-failure shape repeated six times in `audit()`)
collapsed into one `_grade_error(msg)` helper that sets the SAME fields
in the SAME order and returns the SAME `(2, payload)`. Payload and
return value are unchanged.
The verdict is proven on three legs:
1. END-TO-END audit() DRIFT -- a 5-commit synthetic history (UNGRADABLE /
PASS / STALE / PASS / unrelated) is graded by the ORIGINAL (git HEAD~1)
and the REFACTORED (working tree) `audit()` on the SAME repo; the full
`(rc, payload)` is byte-identical (sha256 match). Non-vacuous: the fixture
grades a STALE row, so both versions must exit 1, not 0/2.
2. SELFTEST DRIFT GUARD -- `python3 tools/queue_append_audit.py --selftest`
exits 0 on the refactored worktree (the tool's own 25-cell contract).
3. REDUNDANCY COLLAPSED -- the worktree carries ONE `_grade_error` def with
six call sites, TWO `shutil.rmtree` cleanup sites, and ZERO of the old
`os.walk(td, topdown=False)` teardown / `return 2, payload` inline blocks
(the original, read from git, still carried all six).
Exit code is the verdict: 0 = all green, 1 = any mismatch.
"""
import hashlib
import importlib.util
import json
import os
import shutil
import subprocess
import sys
import tempfile
REPO = "/root/troupe-standup"
DCC = os.path.join(REPO, "tools", "queue_append_audit.py")
INDEX_DEFAULT = "docs/queue-index.md"
class Verdict(Exception):
pass
def sha(text):
return hashlib.sha256(text.encode()).hexdigest()
def materialize_original():
"""Write the PRE-refactor source (git HEAD~1) under its real name in a
temp dir; return (dir, path).
queue_append_audit.py is byte-identical at HEAD and HEAD~1 (the ruff
commit 9f7bc3b only removed an unused `import re` from a sibling lineage);
the refactor is unstaged in the working tree. So HEAD~1 reads a genuine
original both before the refactor lands and after it is committed -- the
same "reads a genuine original both before and after" guarantee the other
forge verifiers rely on.
"""
res = subprocess.run(
["git", "-C", REPO, "show", "HEAD~1:tools/queue_append_audit.py"],
capture_output=True, text=True)
if not res.stdout:
raise Verdict("could not read HEAD~1:tools/queue_append_audit.py: "
+ res.stderr.strip())
d = tempfile.mkdtemp(prefix="qaa_orig_")
p = os.path.join(d, "queue_append_audit.py")
with open(p, "w", encoding="utf-8") as fh:
fh.write(res.stdout)
return d, p
def load_module(path, name):
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def main():
ok = True
orig_dir, orig_path = materialize_original()
base = None
try:
tm_orig = load_module(orig_path, "qaa_orig")
tm_ref = load_module(DCC, "qaa_ref")
# Build a 5-commit history once, using the refactored module's own
# fixture builder (HERE resolves to the real tools/ dir, so it copies
# the real queue_index.py into the fixture). Both audits grade the
# SAME repo, so any behavioral drift would show as a payload diff.
base = tempfile.mkdtemp(prefix="qaa_fix_")
repo, revs = tm_ref._fixture(base)
a, b, c, d, e = revs
rc_o, p_o = tm_orig.audit(repo, "WORK-QUEUE.md", INDEX_DEFAULT,
None, "HEAD", all_history=True)
rc_r, p_r = tm_ref.audit(repo, "WORK-QUEUE.md", INDEX_DEFAULT,
None, "HEAD", all_history=True)
jo = json.dumps(p_o, sort_keys=True)
jr = json.dumps(p_r, sort_keys=True)
# Non-vacuity: the fixture grades exactly the 4 queue-touching commits,
# commit C is STALE, so both versions must exit 1.
nonvac = (len(p_o["rows"]) == 4 and rc_o == 1
and p_o["counts"].get("STALE") == 1
and p_o["counts"].get("PASS") == 2
and p_o["counts"].get("UNGRADABLE") == 1)
drift_ok = nonvac and rc_o == rc_r and jo == jr
print("[1] end-to-end audit() drift rc=%d rows=%d sha_orig=%s sha_ref=%s %s"
% (rc_o, len(p_o["rows"]), sha(jo)[:12], sha(jr)[:12],
"PASS" if drift_ok else "FAIL"))
if not drift_ok:
ok = False
print(" non-vacuity (4 rows, rc==1, STALE=1, PASS=2, UNGRADABLE=1): %s" % nonvac)
print(" rc_o=%s rc_r=%s equal=%s" % (rc_o, rc_r, jo == jr))
co = json.dumps(p_o, sort_keys=True, indent=1)
cr = json.dumps(p_r, sort_keys=True, indent=1)
for i, (x, y) in enumerate(zip(co, cr)):
if x != y:
print(" first diff at char %d: %r vs %r" % (i, x, y))
break
if len(co) != len(cr):
print(" length differs: %d vs %d" % (len(co), len(cr)))
# [2] the tool's own 25-cell selftest on the refactored worktree.
rc = subprocess.run([sys.executable, DCC, "--selftest"],
capture_output=True, text=True)
guard_ok = rc.returncode == 0
print("[2] --selftest drift guard rc=%d %s"
% (rc.returncode, "PASS" if guard_ok else "FAIL"))
if not guard_ok:
ok = False
print(" " + (rc.stdout + rc.stderr).strip()[:400].replace("\n", "\n "))
# [3] redundancy collapsed -- code lines only (comments/docstrings excluded).
text = open(DCC, encoding="utf-8").read()
orig_text = open(orig_path, encoding="utf-8").read()
code = "\n".join(ln for ln in text.splitlines()
if not ln.lstrip().startswith("#")
and not ln.lstrip().startswith('"""'))
code_o = "\n".join(ln for ln in orig_text.splitlines()
if not ln.lstrip().startswith("#")
and not ln.lstrip().startswith('"""'))
defs = text.count("def _grade_error(")
calls = code.count("_grade_error(") - code.count("def _grade_error(")
shutil_count = code.count("shutil.rmtree(")
walk_gone = code.count("os.walk(td, topdown=False)")
inline_returns = code.count("return 2, payload")
# The original must still carry the smell we claim to have removed.
orig_walks = code_o.count("os.walk(td, topdown=False)")
orig_returns = code_o.count("return 2, payload")
red_ok = (defs == 1 and calls == 6 and shutil_count == 2
and walk_gone == 0 and inline_returns == 1
and orig_walks == 1 and orig_returns == 6)
print("[3] redundancy (code lines) _grade_error def=%d calls=%d "
"shutil.rmtree=%d os.walk=%d inline return-2-payload=%d "
"(orig: os.walk=%d inline=%d) %s"
% (defs, calls, shutil_count, walk_gone, inline_returns,
orig_walks, orig_returns, "PASS" if red_ok else "FAIL"))
if not red_ok:
ok = False
finally:
shutil.rmtree(orig_dir, ignore_errors=True)
if base:
shutil.rmtree(base, ignore_errors=True)
print("\nVERDICT:",
"ALL GREEN -- queue_append_audit audit() byte-identical to original, "
"--selftest 25/25, 6 inline error blocks -> _grade_error + shutil.rmtree"
if ok else "MISMATCH -- behavior NOT preserved")
sys.exit(0 if ok else 1)
if __name__ == "__main__":
try:
main()
except Verdict as e:
print("\nVERDICT: MISMATCH -- %s" % e)
sys.exit(1)