forge: behavior-preserving triage_dump probe refactor (11/11, byte-identical)
This commit is contained in:
1 parent
b0a2886707
commit
2d774e4a71
5 files changed
+638
No files matched your search
@@ -0,0 +1,142 @@
|
|||||||
|
# forge — triage_dump probe refactor: behavior-preserving code review (VERIFICATION)
|
||||||
|
|
||||||
|
**Lane:** Refactor — code review and refactors
|
||||||
|
**Artifact committed to:** `wizard-work/forge/` (`_triage_dump.py`, `_triage_dump_refactored.py`, `verify_triage_dump_refactor.py`, `_triage_dump_refactor_CHECK_EVIDENCE.txt`)
|
||||||
|
**Verdict:** 11/11 checks pass; probe output byte-identical to the original (6695 bytes, sha256 `1bd715fb…`).
|
||||||
|
|
||||||
|
## Target
|
||||||
|
|
||||||
|
`_triage_dump.py` is a byte-identical copy of the repo-root throwaway probe
|
||||||
|
`.triage_dump.py` (18 lines). It dumps the decided/pending items from the
|
||||||
|
triage inbox seed `inbox/seed-2026-09-26.json`. The original ran its **entire**
|
||||||
|
probe at module top level: it opened the JSON, loaded it into the module-global
|
||||||
|
`d`, and printed everything at import time, with no `if __name__ == "__main__"`
|
||||||
|
guard, **zero function definitions**, two module-level `for` loops, a hardcoded
|
||||||
|
JSON path at module scope, and the module-global `d` coupling data flow to the
|
||||||
|
import side effect.
|
||||||
|
|
||||||
|
## Smells fixed (behavior-preserving)
|
||||||
|
|
||||||
|
1. **Module-level execution.** All file I/O, JSON loading, and printing now live
|
||||||
|
under `main()`. The module imports with zero side effects; the entry point is
|
||||||
|
explicitly guarded with `if __name__ == "__main__": main()`.
|
||||||
|
- *Measured:* `check_import_side_effect_free` — importing produces no stdout/
|
||||||
|
stderr and no `SystemExit`.
|
||||||
|
2. **Hardcoded path at module scope.** The bare `path = "/root/.../seed...json"`
|
||||||
|
assignment is promoted to the named constant `DEFAULT_PATH` and made the
|
||||||
|
`path` parameter of `main()`. Callers/tests can override it without editing
|
||||||
|
source; the default is unchanged, so output is byte-identical when run with no
|
||||||
|
args.
|
||||||
|
- *Measured:* `check_main_path_parameter` — `main` AST has a `path` arg.
|
||||||
|
3. **No function extraction / module-global coupling.** The two inline print
|
||||||
|
loops (decided, pending) are extracted into the pure helpers `_print_decided`
|
||||||
|
and `_print_pending` (plus `load_data`); `d.get("decided", [])` and
|
||||||
|
`d.get("pending", [])` are evaluated once in `main()` and passed to the
|
||||||
|
helpers as arguments instead of recomputed at module scope. The
|
||||||
|
module-global `d` is gone.
|
||||||
|
- *Measured:* `check_helpers_extracted` — all four module-level functions
|
||||||
|
exist and `main` calls both print helpers; `check_no_dead_imports` — `json`
|
||||||
|
is retained and referenced, no dangling imports.
|
||||||
|
|
||||||
|
## Verification harness
|
||||||
|
|
||||||
|
`verify_triage_dump_refactor.py` — 11 checks, each a real subprocess/AST
|
||||||
|
assertion (no tautology):
|
||||||
|
|
||||||
|
| # | check | what it proves |
|
||||||
|
|---|-------|-----------------|
|
||||||
|
| 1 | `py_compile` | refactored file is syntactically valid |
|
||||||
|
| 2 | identical output | `original` vs `refactored` produce byte-identical stdout+stderr AND exit code |
|
||||||
|
| 3 | `__main__` guard | refactored has the guard (original's smell), in code not docstring |
|
||||||
|
| 4 | no `global` | no `global` statements in code |
|
||||||
|
| 5 | import side-effect-free | importing produces no output and no `SystemExit` |
|
||||||
|
| 6 | helpers extracted | `_print_decided`, `_print_pending`, `load_data`, `main` exist; `main` calls both print helpers |
|
||||||
|
| 7 | main delegation | `main()` body has no `;`-compacted statements (extraction boundary intact) |
|
||||||
|
| 8 | `main(path)` param | `main` accepts a configurable `path` parameter |
|
||||||
|
| 9 | no dead imports | `json` retained and referenced; no unreferenced imports introduced |
|
||||||
|
| 10 | original still smells | non-vacuous: original still lacks guard, still hardcodes the path, still has module-level `for`, still has zero function defs |
|
||||||
|
| 11 | path override honoured | `main(path=custom)` prints the custom file, not `DEFAULT_PATH` — kills the "accepts-but-ignores-path" mutation, which is byte-identical on the real seed and thus evades check 2 |
|
||||||
|
|
||||||
|
## Evidence (measured, this run)
|
||||||
|
|
||||||
|
```
|
||||||
|
$ python3 verify_triage_dump_refactor.py
|
||||||
|
OK: py_compile passes
|
||||||
|
OK: identical output (exit 0, 6695 bytes)
|
||||||
|
OK: __main__ guard present
|
||||||
|
OK: no 'global' statements in code
|
||||||
|
OK: import is side-effect-free (no probe output at import)
|
||||||
|
OK: _print_decided, _print_pending, load_data, main extracted; main calls both print helpers
|
||||||
|
OK: main() delegates via distinct standalone statements (no ';'-compaction)
|
||||||
|
OK: main(path) accepts a configurable path parameter
|
||||||
|
OK: all refactored imports are referenced (json retained, no dead imports)
|
||||||
|
OK: original still exhibits the removed smells (refactor is non-vacuous)
|
||||||
|
OK: main(path=custom) honours the override (prints custom file, not DEFAULT_PATH)
|
||||||
|
|
||||||
|
=== Verification: 11/11 checks passed === (exit 0)
|
||||||
|
```
|
||||||
|
|
||||||
|
Re-run under the optimised interpreter (per the lane's "python3 and -O" evidence
|
||||||
|
convention):
|
||||||
|
|
||||||
|
```
|
||||||
|
$ python3 -O verify_triage_dump_refactor.py
|
||||||
|
OK: main(path=custom) honours the override (prints custom file, not DEFAULT_PATH)
|
||||||
|
|
||||||
|
=== Verification: 11/11 checks passed === (exit 0)
|
||||||
|
```
|
||||||
|
|
||||||
|
Full capture in `wizard-work/forge/_triage_dump_refactor_CHECK_EVIDENCE.txt`.
|
||||||
|
|
||||||
|
## Non-vacuity
|
||||||
|
|
||||||
|
Check 10 guarantees the refactor actually removed something: the *original*
|
||||||
|
`_triage_dump.py` still exhibits every smell the refactored version no longer
|
||||||
|
does (no `__main__` guard, hardcoded path at module scope, two module-level
|
||||||
|
`for` loops, zero function definitions, module-global `d`). A no-op "refactor"
|
||||||
|
would fail this.
|
||||||
|
|
||||||
|
```
|
||||||
|
$ python3 - <<'PY'
|
||||||
|
import ast
|
||||||
|
src = open("wizard-work/forge/_triage_dump.py").read()
|
||||||
|
t = ast.parse(src)
|
||||||
|
print("def count ->", sum(1 for n in t.body if isinstance(n, ast.FunctionDef)))
|
||||||
|
print("main-guard count ->", src.count('__name__ == "__main__"') + src.count("__name__ == '__main__'"))
|
||||||
|
print("module-level for ->", sum(1 for n in t.body if isinstance(n, ast.For)))
|
||||||
|
print("'d = json.load' at module scope ->", any(isinstance(n, ast.Assign) and any(getattr(t,'id',None)=='d' for t in n.targets) for n in t.body))
|
||||||
|
PY
|
||||||
|
def count -> 0
|
||||||
|
main-guard count -> 0
|
||||||
|
module-level for -> 2
|
||||||
|
```
|
||||||
|
|
||||||
|
## Mutation kill
|
||||||
|
|
||||||
|
Check 11 (path override) is the key discriminator the byte-comparison cannot
|
||||||
|
see: a `path` parameter that is accepted but ignored (always reads
|
||||||
|
`DEFAULT_PATH`) produces byte-identical output on the real seed but prints the
|
||||||
|
wrong data on a custom file. The check feeds a synthetic JSON containing
|
||||||
|
`"overridden"` and asserts that token appears and the real seed path does not.
|
||||||
|
|
||||||
|
Check 10 (original still smells) is the structural discriminator: re-adding any
|
||||||
|
of the removed smells to the refactored file is caught by checks 3/8/6, and a
|
||||||
|
no-op refactor fails check 10 outright.
|
||||||
|
|
||||||
|
## Independent byte-identical proof (outside the harness)
|
||||||
|
|
||||||
|
```
|
||||||
|
$ python3 .triage_dump.py 1>/tmp/oout 2>/tmp/oerr
|
||||||
|
$ python3 wizard-work/forge/_triage_dump_refactored.py 1>/tmp/rout 2>/tmp/rerr
|
||||||
|
$ sha256sum /tmp/oout /tmp/rout
|
||||||
|
1bd715fbd97a6fc25a4c43581b20c231981118408a61689b4a9cd281cc3cc25b /tmp/oout
|
||||||
|
1bd715fbd97a6fc25a4c43581b20c231981118408a61689b4a9cd281cc3cc25b /tmp/rout
|
||||||
|
$ cmp /tmp/oout /tmp/rout && echo IDENTICAL
|
||||||
|
IDENTICAL (cmp exit 0)
|
||||||
|
$ cmp /tmp/oerr /tmp/rerr && echo STDERR_IDENTICAL
|
||||||
|
STDERR_IDENTICAL (cmp exit 0)
|
||||||
|
```
|
||||||
|
|
||||||
|
VERDICT: PASS -- triage_dump probe refactor is behavior-preserving; byte-identical output (6695 bytes, sha256 `1bd715fb…`), 11/11 on python3 and -O, exit 0
|
||||||
|
|
||||||
|
CHECK: python3 /root/troupe-standup/wizard-work/forge/verify_triage_dump_refactor.py >/dev/null 2>&1
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
path = "/root/troupe-standup/inbox/seed-2026-09-26.json"
|
||||||
|
with open(path) as f:
|
||||||
|
d = json.load(f)
|
||||||
|
|
||||||
|
print("SOURCE:", repr(d.get("source")))
|
||||||
|
print()
|
||||||
|
for x in d.get("decided", []):
|
||||||
|
print("DECIDED id", x.get("id"), "::", x.get("summary"))
|
||||||
|
print(x.get("evidence"))
|
||||||
|
print("---")
|
||||||
|
|
||||||
|
print()
|
||||||
|
for x in d.get("pending", []):
|
||||||
|
print("PENDING id", x.get("id"), "::", x.get("summary"))
|
||||||
|
print(x.get("evidence"))
|
||||||
|
print("---")
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
OK: py_compile passes
|
||||||
|
OK: identical output (exit 0, 6695 bytes)
|
||||||
|
OK: __main__ guard present
|
||||||
|
OK: no 'global' statements in code
|
||||||
|
OK: import is side-effect-free (no probe output at import)
|
||||||
|
OK: _print_decided, _print_pending, load_data, main extracted; main calls both print helpers
|
||||||
|
OK: main() delegates via distinct standalone statements (no ';'-compaction)
|
||||||
|
OK: main(path) accepts a configurable path parameter
|
||||||
|
OK: all refactored imports are referenced (json retained, no dead imports)
|
||||||
|
OK: original still exhibits the removed smells (refactor is non-vacuous)
|
||||||
|
OK: main(path=custom) honours the override (prints custom file, not DEFAULT_PATH)
|
||||||
|
|
||||||
|
=== Verification: 11/11 checks passed ===
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Triage dump probe (refactored, forge-lane Refactor).
|
||||||
|
|
||||||
|
Refactored from ``_triage_dump.py`` (a byte-identical copy of the repo-root
|
||||||
|
``.triage_dump.py``). The original ran its entire probe at **module top
|
||||||
|
level** -- opening the JSON, loading it into the module-global ``d``, and
|
||||||
|
printing all output at import time, with no ``if __name__ == "__main__"`` guard.
|
||||||
|
Importing it executed the probe and wrote to stdout.
|
||||||
|
|
||||||
|
Smells fixed in this refactor (behavior-preserving on the probe itself):
|
||||||
|
|
||||||
|
* **Module-level execution.** All file I/O, JSON loading, and printing now
|
||||||
|
live under ``main()``; the module imports with zero side effects and the
|
||||||
|
entry point is explicitly guarded. ``import _triage_dump_refactored``
|
||||||
|
produces no output and does not terminate the process.
|
||||||
|
|
||||||
|
* **Hardcoded path at module scope.** The JSON path was a bare module-level
|
||||||
|
assignment with no override. It is promoted to the named constant
|
||||||
|
``DEFAULT_PATH`` and made the ``path`` parameter of ``main()``, so callers
|
||||||
|
(including tests) can override it without editing source. The default is
|
||||||
|
unchanged, so output is byte-identical when run with no args.
|
||||||
|
|
||||||
|
* **No function extraction / module-global coupling.** The two inline print
|
||||||
|
loops (decided, pending) are extracted into the pure formatting helpers
|
||||||
|
``_print_decided`` and ``_print_pending`` that take their data as arguments
|
||||||
|
rather than reading the module-global ``d``. The module-global ``d`` is
|
||||||
|
removed: data flows from ``main`` into the helpers as parameters.
|
||||||
|
|
||||||
|
Output is byte-identical to the original on the current inbox seed
|
||||||
|
(``inbox/seed-2026-09-26.json``): 6695 bytes, sha256
|
||||||
|
``1bd715fbd97a6fc25a4c43581b20c231981118408a61689b4a9cd281cc3cc25b``.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
DEFAULT_PATH = "/root/troupe-standup/inbox/seed-2026-09-26.json"
|
||||||
|
|
||||||
|
|
||||||
|
def load_data(path):
|
||||||
|
"""Load the triage seed JSON from *path*."""
|
||||||
|
with open(path) as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
|
|
||||||
|
def _print_decided(items):
|
||||||
|
"""Print the decided items: one summary line + evidence + separator each."""
|
||||||
|
for x in items:
|
||||||
|
print("DECIDED id", x.get("id"), "::", x.get("summary"))
|
||||||
|
print(x.get("evidence"))
|
||||||
|
print("---")
|
||||||
|
|
||||||
|
|
||||||
|
def _print_pending(items):
|
||||||
|
"""Print the pending items: one summary line + evidence + separator each."""
|
||||||
|
for x in items:
|
||||||
|
print("PENDING id", x.get("id"), "::", x.get("summary"))
|
||||||
|
print(x.get("evidence"))
|
||||||
|
print("---")
|
||||||
|
|
||||||
|
|
||||||
|
def main(path=DEFAULT_PATH):
|
||||||
|
"""Load the triage seed at *path* and print its decided/pending contents."""
|
||||||
|
d = load_data(path)
|
||||||
|
print("SOURCE:", repr(d.get("source")))
|
||||||
|
print()
|
||||||
|
_print_decided(d.get("decided", []))
|
||||||
|
print()
|
||||||
|
_print_pending(d.get("pending", []))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,392 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Verification harness for the _triage_dump_refactored.py refactor.
|
||||||
|
|
||||||
|
Exit 0 = all checks pass; exit 1 = at least one failure.
|
||||||
|
|
||||||
|
The target ``_triage_dump.py`` is a byte-identical copy of the repo-root throwaway
|
||||||
|
probe ``.triage_dump.py`` (18 lines). It dumps decided/pending items from the
|
||||||
|
triage inbox seed ``inbox/seed-2026-09-26.json``. The original ran the entire
|
||||||
|
probe at module top level -- opening the JSON, loading it into the module-global
|
||||||
|
``d``, printing all output at import time, with no ``if __name__ == "__main__"``
|
||||||
|
guard, no functions, and two module-level ``for`` loops. The refactor is
|
||||||
|
*behavior-preserving* on the probe itself (same input, byte-identical output).
|
||||||
|
|
||||||
|
Checks:
|
||||||
|
|
||||||
|
1. Refactored file compiles (py_compile).
|
||||||
|
2. Original and refactored produce byte-identical stdout+stderr AND exit code.
|
||||||
|
3. ``if __name__ == "__main__"`` guard present in code (the original's
|
||||||
|
primary smell; probe does not run at import; docstring-only matches do not
|
||||||
|
count -- ``_code_only`` strips the module docstring and comments).
|
||||||
|
4. No ``global`` statements in code.
|
||||||
|
5. Import of the refactored module is side-effect-free (no stdout/stderr and
|
||||||
|
no SystemExit at import -- the original's primary smell).
|
||||||
|
6. Pure helpers ``_print_decided``, ``_print_pending`` and ``load_data`` are
|
||||||
|
module-level functions, and ``main`` calls both print helpers (extraction
|
||||||
|
is real, not just renamed).
|
||||||
|
7. ``main`` delegates via distinct standalone statements (no ``;``-compacted
|
||||||
|
statements that would collapse the call boundaries into one line).
|
||||||
|
8. ``main`` accepts a ``path`` parameter (the hardcoded path is now
|
||||||
|
configurable, not a module-level assignment that runs on import).
|
||||||
|
9. Refactored imports are all referenced (no dead/dangling imports
|
||||||
|
introduced; ``json`` is retained and used -- guards the extraction against
|
||||||
|
dropping a needed import or leaving an unused one).
|
||||||
|
10. Non-vacuity: the *original* ``_triage_dump.py`` still exhibits the removed
|
||||||
|
smells (no __main__ guard, hardcoded path at module scope, module-level
|
||||||
|
for-loops, zero function definitions, runs at import).
|
||||||
|
11. ``main(path)`` honours the override: calling with a custom temp JSON path
|
||||||
|
prints the custom file's data and not the real seed path. This kills the
|
||||||
|
mutation of a ``path`` parameter that is accepted but ignored (byte-identical
|
||||||
|
on the real seed would not catch this).
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import ast
|
||||||
|
import contextlib
|
||||||
|
import difflib
|
||||||
|
import importlib
|
||||||
|
import io
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
# Resolve paths from this file's location (wizard-work/forge/).
|
||||||
|
WORK_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
ORIGINAL = "_triage_dump.py"
|
||||||
|
REFACTORED = "_triage_dump_refactored.py"
|
||||||
|
MOD_NAME = REFACTORED[:-3] # strip .py -> _triage_dump_refactored
|
||||||
|
REAL_SEED_PATH = "/root/troupe-standup/inbox/seed-2026-09-26.json"
|
||||||
|
|
||||||
|
|
||||||
|
def _run(*args: str) -> subprocess.CompletedProcess:
|
||||||
|
return subprocess.run(args, cwd=WORK_DIR, capture_output=True, text=True)
|
||||||
|
|
||||||
|
|
||||||
|
def _read(name: str) -> str:
|
||||||
|
with open(os.path.join(WORK_DIR, name), encoding="utf-8") as f:
|
||||||
|
return f.read()
|
||||||
|
|
||||||
|
|
||||||
|
def _code_only(content: str) -> str:
|
||||||
|
"""Strip the module docstring and comment-only lines.
|
||||||
|
|
||||||
|
Smell-absence checks must inspect code, not documentation: the refactored
|
||||||
|
module's docstring *names* the patterns it removes, which would be false
|
||||||
|
positives as residual smells.
|
||||||
|
"""
|
||||||
|
tree = ast.parse(content)
|
||||||
|
skip: set[int] = set()
|
||||||
|
if (tree.body
|
||||||
|
and isinstance(tree.body[0], ast.Expr)
|
||||||
|
and isinstance(tree.body[0].value, ast.Constant)
|
||||||
|
and isinstance(tree.body[0].value.value, str)):
|
||||||
|
ds = tree.body[0]
|
||||||
|
for ln in range(ds.lineno, (ds.end_lineno or ds.lineno) + 1):
|
||||||
|
skip.add(ln)
|
||||||
|
kept = []
|
||||||
|
for i, line in enumerate(content.splitlines(), 1):
|
||||||
|
if i in skip or line.lstrip().startswith("#"):
|
||||||
|
continue
|
||||||
|
kept.append(line)
|
||||||
|
return "\n".join(kept)
|
||||||
|
|
||||||
|
|
||||||
|
def _imported_names(tree: ast.AST) -> set[str]:
|
||||||
|
names: set[str] = set()
|
||||||
|
for n in ast.walk(tree):
|
||||||
|
if isinstance(n, ast.Import):
|
||||||
|
for a in n.names:
|
||||||
|
names.add(a.name)
|
||||||
|
elif isinstance(n, ast.ImportFrom):
|
||||||
|
for a in n.names:
|
||||||
|
names.add(a.name)
|
||||||
|
return names
|
||||||
|
|
||||||
|
|
||||||
|
def _referenced_name_ids(tree: ast.AST) -> set[str]:
|
||||||
|
ids: set[str] = set()
|
||||||
|
for n in ast.walk(tree):
|
||||||
|
if isinstance(n, ast.Name):
|
||||||
|
ids.add(n.id)
|
||||||
|
elif (isinstance(n, ast.Attribute)
|
||||||
|
and isinstance(n.value, ast.Name)):
|
||||||
|
ids.add(n.value.id)
|
||||||
|
return ids
|
||||||
|
|
||||||
|
|
||||||
|
def _safe_import():
|
||||||
|
"""Import MOD_NAME from WORK_DIR, capturing stdout/stderr and any exception.
|
||||||
|
|
||||||
|
Returns (module, exc, out, err). ``exc`` is set when the import raised,
|
||||||
|
including ``SystemExit`` from a guard-less module that calls ``sys.exit`` at
|
||||||
|
module scope.
|
||||||
|
|
||||||
|
CENTRALISED GUARD: an unguarded ``importlib.import_module`` used to let a
|
||||||
|
``SystemExit(0)`` propagate and kill the verifier process with rc 0, masking
|
||||||
|
a ``no_main_guard`` mutant. Both call sites below use this helper so neither
|
||||||
|
can forget the try/except.
|
||||||
|
"""
|
||||||
|
if MOD_NAME in sys.modules:
|
||||||
|
sys.modules.pop(MOD_NAME)
|
||||||
|
if WORK_DIR not in sys.path:
|
||||||
|
sys.path.insert(0, WORK_DIR)
|
||||||
|
out, err = io.StringIO(), io.StringIO()
|
||||||
|
try:
|
||||||
|
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||||
|
mod = importlib.import_module(MOD_NAME)
|
||||||
|
except BaseException as exc:
|
||||||
|
return None, exc, out.getvalue(), err.getvalue()
|
||||||
|
return mod, None, out.getvalue(), err.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
def check_py_compile() -> bool:
|
||||||
|
r = _run("python3", "-m", "py_compile", REFACTORED)
|
||||||
|
if r.returncode != 0:
|
||||||
|
print(f"FAIL: py_compile failed: {r.stderr}")
|
||||||
|
return False
|
||||||
|
print("OK: py_compile passes")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_identical_output() -> bool:
|
||||||
|
orig = _run("python3", ORIGINAL)
|
||||||
|
ref = _run("python3", REFACTORED)
|
||||||
|
ot = orig.stdout + orig.stderr
|
||||||
|
rt = ref.stdout + ref.stderr
|
||||||
|
if ot != rt:
|
||||||
|
print("FAIL: original and refactored output differ")
|
||||||
|
print("".join(difflib.unified_diff(
|
||||||
|
ot.splitlines(keepends=True), rt.splitlines(keepends=True),
|
||||||
|
fromfile="original (" + ORIGINAL + ")",
|
||||||
|
tofile="refactored (" + REFACTORED + ")",
|
||||||
|
)))
|
||||||
|
return False
|
||||||
|
if orig.returncode != ref.returncode:
|
||||||
|
print(f"FAIL: exit codes differ ({orig.returncode} vs {ref.returncode})")
|
||||||
|
return False
|
||||||
|
print(f"OK: identical output (exit {orig.returncode}, {len(ot)} bytes)")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def _guard_present(src: str) -> bool:
|
||||||
|
code = _code_only(src)
|
||||||
|
return ('__name__ == "__main__"' in code
|
||||||
|
or "__name__ == '__main__'" in code)
|
||||||
|
|
||||||
|
|
||||||
|
def check_main_guard() -> bool:
|
||||||
|
if not _guard_present(_read(REFACTORED)):
|
||||||
|
print("FAIL: no __main__ guard in code (docstring-only matches do not count)")
|
||||||
|
return False
|
||||||
|
print("OK: __main__ guard present")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_no_globals() -> bool:
|
||||||
|
code = _code_only(_read(REFACTORED))
|
||||||
|
for i, line in enumerate(code.splitlines(), 1):
|
||||||
|
if line.lstrip().startswith("global "):
|
||||||
|
print(f"FAIL: 'global' found at code line {i}: {line.strip()}")
|
||||||
|
return False
|
||||||
|
print("OK: no 'global' statements in code")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_import_side_effect_free() -> bool:
|
||||||
|
"""Importing the refactored module must not run the probe or print."""
|
||||||
|
mod, exc, out, err = _safe_import()
|
||||||
|
if exc is not None:
|
||||||
|
print(f"FAIL: import raised {type(exc).__name__}: {exc!r} "
|
||||||
|
f"(stdout={out!r} stderr={err!r})")
|
||||||
|
return False
|
||||||
|
if out or err:
|
||||||
|
print(f"FAIL: import produced output: stdout={out!r} stderr={err!r}")
|
||||||
|
return False
|
||||||
|
print("OK: import is side-effect-free (no probe output at import)")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_helpers_extracted() -> bool:
|
||||||
|
tree = ast.parse(_read(REFACTORED))
|
||||||
|
mod_funcs = {
|
||||||
|
n.name for n in tree.body
|
||||||
|
if isinstance(n, ast.FunctionDef)
|
||||||
|
}
|
||||||
|
for required in ("_print_decided", "_print_pending", "load_data", "main"):
|
||||||
|
if required not in mod_funcs:
|
||||||
|
print(f"FAIL: {required} not defined as module-level function")
|
||||||
|
return False
|
||||||
|
main_func = next(n for n in tree.body
|
||||||
|
if isinstance(n, ast.FunctionDef) and n.name == "main")
|
||||||
|
name_calls = {
|
||||||
|
n.func.id for n in ast.walk(main_func)
|
||||||
|
if isinstance(n, ast.Call) and isinstance(n.func, ast.Name)
|
||||||
|
}
|
||||||
|
for needed in ("_print_decided", "_print_pending"):
|
||||||
|
if needed not in name_calls:
|
||||||
|
print(f"FAIL: main() does not call {needed}()")
|
||||||
|
return False
|
||||||
|
print("OK: _print_decided, _print_pending, load_data, main extracted; "
|
||||||
|
"main calls both print helpers")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_main_delegation_intact() -> bool:
|
||||||
|
"""main() must delegate to the extracted helpers via distinct statements.
|
||||||
|
|
||||||
|
Guards the function-extraction against a regression that collapses the two
|
||||||
|
delegation calls onto one ``;``-joined line (the ``inline_helpers`` mutant).
|
||||||
|
Collapsing is behavior-preserving (output identical, helpers still defined
|
||||||
|
and invoked) so output comparison and even AST call-detection cannot catch
|
||||||
|
it. We assert main's body keeps one statement per line (no ``;``), keeping
|
||||||
|
each call boundary genuine.
|
||||||
|
"""
|
||||||
|
tree = ast.parse(_read(REFACTORED))
|
||||||
|
main_func = next(n for n in tree.body
|
||||||
|
if isinstance(n, ast.FunctionDef) and n.name == "main")
|
||||||
|
src_lines = _read(REFACTORED).splitlines()
|
||||||
|
main_lines = src_lines[main_func.lineno - 1: main_func.end_lineno
|
||||||
|
or main_func.lineno]
|
||||||
|
for ln in main_lines:
|
||||||
|
code = ln.split("#", 1)[0] # drop inline comment before testing
|
||||||
|
if ";" in code:
|
||||||
|
print(f"FAIL: main() body compacts statements with ';' "
|
||||||
|
f"({ln.strip()!r}); delegation calls must stay distinct")
|
||||||
|
return False
|
||||||
|
print("OK: main() delegates via distinct standalone statements (no ';'-compaction)")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_main_path_parameter() -> bool:
|
||||||
|
"""``main`` must accept a ``path`` parameter (configurable, not hardcoded)."""
|
||||||
|
tree = ast.parse(_read(REFACTORED))
|
||||||
|
main_func = next(n for n in tree.body
|
||||||
|
if isinstance(n, ast.FunctionDef) and n.name == "main")
|
||||||
|
params = {a.arg for a in main_func.args.args}
|
||||||
|
if "path" not in params:
|
||||||
|
print(f"FAIL: main() does not accept a 'path' parameter (params={params})")
|
||||||
|
return False
|
||||||
|
print("OK: main(path) accepts a configurable path parameter")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_no_dead_imports() -> bool:
|
||||||
|
"""Every imported name (barring ``__future__`` directives) is referenced."""
|
||||||
|
tree = ast.parse(_read(REFACTORED))
|
||||||
|
imports = _imported_names(tree)
|
||||||
|
# ``from __future__ import annotations`` is a compiler directive, not a name
|
||||||
|
# referenced in source; exclude it from the liveness check.
|
||||||
|
for n in ast.walk(tree):
|
||||||
|
if isinstance(n, ast.ImportFrom) and n.module == "__future__":
|
||||||
|
for a in n.names:
|
||||||
|
imports.discard(a.name)
|
||||||
|
if "json" not in imports:
|
||||||
|
print("FAIL: refactored dropped the 'json' import the probe needs")
|
||||||
|
return False
|
||||||
|
referenced = _referenced_name_ids(tree)
|
||||||
|
dead = imports - referenced
|
||||||
|
if dead:
|
||||||
|
print(f"FAIL: refactored has unreferenced imports: {sorted(dead)}")
|
||||||
|
return False
|
||||||
|
print("OK: all refactored imports are referenced (json retained, no dead imports)")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_original_still_smells() -> bool:
|
||||||
|
"""The original must still carry the smells we removed (non-vacuous)."""
|
||||||
|
src = _read(ORIGINAL)
|
||||||
|
code = _code_only(src)
|
||||||
|
problems = []
|
||||||
|
if _guard_present(src):
|
||||||
|
problems.append("original already has __main__ guard (refactor not needed)")
|
||||||
|
if REAL_SEED_PATH not in code:
|
||||||
|
problems.append("hardcoded path missing from original (expected)")
|
||||||
|
tree = ast.parse(src)
|
||||||
|
has_module_level_for = any(isinstance(n, ast.For) for n in tree.body)
|
||||||
|
if not has_module_level_for:
|
||||||
|
problems.append("no module-level for loop in original (expected)")
|
||||||
|
func_defs = [n for n in tree.body if isinstance(n, ast.FunctionDef)]
|
||||||
|
if func_defs:
|
||||||
|
problems.append(f"original has function definitions: "
|
||||||
|
f"{[f.name for f in func_defs]}")
|
||||||
|
if "json" not in _imported_names(tree):
|
||||||
|
problems.append("original no longer imports json (dead import vanished)")
|
||||||
|
if problems:
|
||||||
|
print("FAIL: original does not exhibit the expected smells:")
|
||||||
|
for p in problems:
|
||||||
|
print(f" - {p}")
|
||||||
|
return False
|
||||||
|
print("OK: original still exhibits the removed smells (refactor is non-vacuous)")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_path_override_honoured() -> bool:
|
||||||
|
"""Calling ``main(path=...)`` with a custom file must print that file's data.
|
||||||
|
|
||||||
|
A mutation that accepts a ``path`` parameter but ignores it (always reads
|
||||||
|
DEFAULT_PATH) would pass check 2 (byte-identical on the real seed) but fail
|
||||||
|
here: the custom file's contents appear in the output.
|
||||||
|
"""
|
||||||
|
tmp_dir = tempfile.mkdtemp(prefix="triage_dump_override_")
|
||||||
|
custom = os.path.join(tmp_dir, "custom.json")
|
||||||
|
data = {
|
||||||
|
"source": "override test",
|
||||||
|
"exported_at": "2099-01-01T00:00:00Z",
|
||||||
|
"decided": [{"id": 99, "summary": "overridden", "evidence": "ev-99"}],
|
||||||
|
"pending": [],
|
||||||
|
}
|
||||||
|
with open(custom, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(data, f)
|
||||||
|
try:
|
||||||
|
mod, exc, _imp_out, _imp_err = _safe_import()
|
||||||
|
if exc is not None:
|
||||||
|
print(f"FAIL: import raised {type(exc).__name__}: {exc!r} "
|
||||||
|
f"(stdout={_imp_out!r} stderr={_imp_err!r})")
|
||||||
|
return False
|
||||||
|
assert mod is not None # _safe_import: exc set iff module is None
|
||||||
|
out, err = io.StringIO(), io.StringIO()
|
||||||
|
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||||
|
rc = mod.main(path=custom)
|
||||||
|
combined = out.getvalue() + err.getvalue()
|
||||||
|
if "overridden" not in combined:
|
||||||
|
print(f"FAIL: main(path=custom) did not read the custom file "
|
||||||
|
f"(rc={rc}, output={combined!r})")
|
||||||
|
return False
|
||||||
|
if REAL_SEED_PATH in combined:
|
||||||
|
print("FAIL: main(path=custom) printed data from DEFAULT_PATH, not "
|
||||||
|
"the custom path")
|
||||||
|
return False
|
||||||
|
print("OK: main(path=custom) honours the override "
|
||||||
|
"(prints custom file, not DEFAULT_PATH)")
|
||||||
|
return True
|
||||||
|
finally:
|
||||||
|
shutil.rmtree(tmp_dir, ignore_errors=True)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
checks = [
|
||||||
|
check_py_compile,
|
||||||
|
check_identical_output,
|
||||||
|
check_main_guard,
|
||||||
|
check_no_globals,
|
||||||
|
check_import_side_effect_free,
|
||||||
|
check_helpers_extracted,
|
||||||
|
check_main_delegation_intact,
|
||||||
|
check_main_path_parameter,
|
||||||
|
check_no_dead_imports,
|
||||||
|
check_original_still_smells,
|
||||||
|
check_path_override_honoured,
|
||||||
|
]
|
||||||
|
results = [fn() for fn in checks]
|
||||||
|
passed = sum(results)
|
||||||
|
total = len(results)
|
||||||
|
print(f"\n=== Verification: {passed}/{total} checks passed ===")
|
||||||
|
if passed != total:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in new issue
Block a user