From e93173cbdad4c08a4c58026495f73873c1082778 Mon Sep 17 00:00:00 2001 From: lane Date: Wed, 11 Nov 2026 22:40:39 -0400 Subject: [PATCH 1/2] tools: a check for shadowed top-level definitions, plus the f16 corpus named and fingerprinted `tools/hrx_f16_narrowing_check.py` defined `def self_test()` twice. The second wins, so the first one's scan arms were dead code: the file imported, the surviving self_test printed twelve `ok` lines, and three assertions could not fail because they never ran. Nobody noticed because nothing runs the tool in CI -- only by hand. Three things, in the order they matter: tools/ci/dupdef_check.py an AST walk for a top-level def/class defined more than once, reported as `name (lines N, M)`. It is scoped to tools/ and scripts/ and skips third_party/build/node_modules. Five self-test arms, including the exact shape it was written for and the control that a duplicated METHOD inside a class is not a top-level shadow. the dead copy removed its three scan arms (mul-updated / add-only / f32 control) already exist in the surviving self_test, so this deletes 22 lines and loses no assertion. the corpus is named `hrx_f16_narrowing_check.py ` now prints `CORPUS ` and `FINGERPRINT ( .loom files)`, hashing each file's relative path and bytes so a move changes it too. Four arms: the file count, the same corpus twice, a one-byte edit, and a rename. Measured on this tree before the fix and after: dupdef_check --check --root tools/hrx_f16_narrowing_check.py before rc 1 (self_test lines 163, 185) after rc 0 dupdef_check --check (whole tools/ + scripts/) rc 0, 0 shadowed definitions hrx_f16_narrowing_check.py --self-test 16 arms, rc 0 (was 12, all shadow-surviving) Both are registered as ctest rows so they run without a ci.yml edit. ctest, configured fresh: 1/2 dupdef_check Passed 0.47 s 2/2 hrx_f16_narrowing_selftest Passed 0.08 s 100% passed `ci_targets_check --check` still reports 9 baseline / none new, so the rows add no drift. --- CMakeLists.txt | 9 +++ tools/ci/dupdef_check.py | 120 +++++++++++++++++++++++++++++++ tools/hrx_f16_narrowing_check.py | 51 +++++++------ 3 files changed, 157 insertions(+), 23 deletions(-) create mode 100644 tools/ci/dupdef_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index bdc7dff26..663269550 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -209,6 +209,15 @@ add_dependencies(onebit npu_decode_note_test) # engine#2106: a shipped kernel set's own consistency -- manifest vs bytes, one form or none, name vs directory. The # refusal this pre-empts ("differ in size") needed private-repo archaeology to diagnose; the symptom is checkable here. add_test(NAME kernel_set_check COMMAND bash ${CMAKE_SOURCE_DIR}/tests/kernel_set_check_test.sh) + +# engine#2186/#2244: two checks that were only ever run by hand. `tools/ci/dupdef_check.py` is the one +# that found a shadowed `def self_test()` in tools/hrx_f16_narrowing_check.py -- the surviving copy +# passed and the shadowed one's arms could never fail. Both take no build input, so a ctest row is the +# route that needs no ci.yml line (the workflow is not editable from a lane). +add_test(NAME dupdef_check COMMAND python3 ${CMAKE_SOURCE_DIR}/tools/ci/dupdef_check.py --check + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) +add_test(NAME hrx_f16_narrowing_selftest COMMAND python3 ${CMAKE_SOURCE_DIR}/tools/hrx_f16_narrowing_check.py --self-test + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) add_test(NAME bump_hrx_report COMMAND bash ${CMAKE_SOURCE_DIR}/tests/bump_hrx_report_test.sh ${CMAKE_SOURCE_DIR}/scripts/bump-hrx-report.sh) add_test(NAME metrics_serve COMMAND ${CMAKE_SOURCE_DIR}/tests/metrics_serve.sh $) # serve --print-backend: the llama-server Lemonade starts itself, one hop (docs/serve.md): no GPU. diff --git a/tools/ci/dupdef_check.py b/tools/ci/dupdef_check.py new file mode 100644 index 000000000..1bb0607a5 --- /dev/null +++ b/tools/ci/dupdef_check.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +"""dupdef_check.py -- a shadowed top-level definition silently disables the one it shadows. + +A Python module may define `def self_test()` twice. The second wins, the first is dead, and nothing +warns: the file still imports, the surviving function still passes, and the arms the first one +carried can never fail because they never run. `tools/hrx_f16_narrowing_check.py` shipped exactly +that -- two `self_test()` bodies, the scan arms in the shadowed one -- which is a check that cannot +fail wearing the costume of two checks. + +Usage: + dupdef_check.py --check [--root DIR ...] + dupdef_check.py --self-test +""" + +import ast +import pathlib +import sys + +# Vendored and generated trees are not ours to police; a duplicate there is upstream's business. +SKIP_DIR_NAMES = {'.git', 'build', 'node_modules', '__pycache__', 'third_party', '.venv'} +DEFAULT_ROOTS = ('tools', 'scripts') + + +def duplicates(path): + """[(name, [lineno, ...])] for top-level def/class names defined more than once.""" + try: + tree = ast.parse(path.read_text(errors='replace')) + except (SyntaxError, OSError): + # A file we cannot parse is not this check's finding; whoever owns syntax reporting says so. + return [] + seen = {} + for node in tree.body: + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): + seen.setdefault((node.name, type(node).__name__), []).append(node.lineno) + return [(name, lines) for (name, _kind), lines in sorted(seen.items()) if len(lines) > 1] + + +def files_under(roots): + for root in roots: + p = pathlib.Path(root) + if p.is_file() and p.suffix == '.py': + yield p + continue + for f in sorted(p.rglob('*.py')): + if not any(part in SKIP_DIR_NAMES for part in f.parts): + yield f + + +def main(argv): + if '--self-test' in argv: + return self_test() + if '--check' not in argv: + print(__doc__.strip().splitlines()[-1].strip()) + print('usage: dupdef_check.py --check [--root DIR ...] | --self-test') + return 2 + roots = [] + for i, a in enumerate(argv): + if a == '--root' and i + 1 < len(argv): + roots.append(argv[i + 1]) + roots = roots or list(DEFAULT_ROOTS) + + bad = 0 + n = 0 + for f in files_under(roots): + if f.name == 'dupdef_check.py': + continue # its own fixtures define duplicates on purpose + n += 1 + for name, lines in duplicates(f): + bad += 1 + print(f"dupdef: {f}: `{name}` is defined {len(lines)} times (lines {', '.join(map(str, lines))}) " + f"-- everything but the last is dead, and its assertions never run") + if bad: + print(f"dupdef_check: {bad} shadowed definition(s) in {n} file(s)") + return 1 + print(f"dupdef_check: no shadowed top-level definition in {n} file(s)") + return 0 + + +def self_test(): + import tempfile + rc = 0 + + def chk(want, got, what): + nonlocal rc + if want == got: + print(f"ok {what}") + else: + print(f"FAIL {what}: want {want}, got {got}") + rc = 1 + + with tempfile.TemporaryDirectory() as d: + p = pathlib.Path(d, 'shadowed.py') + p.write_text("def self_test():\n return 1\n\ndef other():\n return 2\n\ndef self_test():\n return 3\n") + chk([('self_test', [1, 7])], duplicates(p), 'two top-level defs of one name are reported with both lines') + + q = pathlib.Path(d, 'indented.py') + q.write_text("class A:\n def f(self):\n return 1\n def f(self):\n return 2\n") + chk([], duplicates(q), 'a method defined twice inside a class is not a top-level shadow') + + r = pathlib.Path(d, 'once.py') + r.write_text("def a():\n pass\n\ndef b():\n pass\n") + chk([], duplicates(r), 'distinct names are not a finding') + + s = pathlib.Path(d, 'broken.py') + s.write_text("def a(:\n") + chk([], duplicates(s), 'an unparseable file is not this check\'s finding (absence of a result, not a pass)') + + # The defect this check was written for, reproduced: the surviving def passes, the shadowed one never runs. + t = pathlib.Path(d, 'hrx_shaped.py') + t.write_text("def self_test():\n check('scan arm')\n\ndef check(x):\n pass\n\ndef self_test():\n pass\n") + chk([('self_test', [1, 7])], duplicates(t), 'the hrx_f16_narrowing_check.py shape (a shadowed self_test) is caught') + + print(f"dupdef_check --self-test: {'ok' if rc == 0 else 'FAIL'}") + return rc + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/tools/hrx_f16_narrowing_check.py b/tools/hrx_f16_narrowing_check.py index 3b4a524f2..c1c4cad5e 100755 --- a/tools/hrx_f16_narrowing_check.py +++ b/tools/hrx_f16_narrowing_check.py @@ -38,7 +38,7 @@ # # usage: hrx_f16_narrowing_check.py [--self-test] # Exit: 0 always for a report (this is an inventory, not a gate); --self-test is the gate. -import re, sys, pathlib +import hashlib, re, sys, pathlib NARROW = re.compile(r'fptrunc\s+(%[A-Za-z0-9_]+)|fptrunc\s+%([A-Za-z0-9_]+)') DEF = re.compile(r'\s*(%[A-Za-z0-9_]+)\s*=\s*(.*)$') @@ -160,28 +160,6 @@ def scan(path, lines): return out -def self_test(): - rc = 0 - def chk(want, got, what): - nonlocal rc - if want == got: - print(f"ok {what}") - else: - print(f"FAIL {what}: want {want}, got {got}") - rc = 1 - mul_acc = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>", - " %next = vector.mulf %block_acc, %k : vector<16xf16>"] - chk('mul', scan('x', mul_acc)[0][3], 'a mul-updated f16 accumulator is flagged (the #2186 item-1 shape)') - add_acc = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>", - " %next = vector.addf %block_acc, %k : vector<16xf16>"] - chk('add', scan('x', add_acc)[0][3], 'an add-only f16 accumulator is reported separately') - f32 = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf32>"] - chk(0, len(scan('x', f32)), 'an f32 accumulator is not this defect') - print(f"self-test: {'ok' if rc == 0 else 'FAIL'}") - return rc - - - def self_test(): rc = 0 def chk(want, got, what): @@ -234,10 +212,33 @@ def chk(want, got, what): far = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>"] + [" // pad"] * 200 + \ [" %next = vector.mulf %block_acc, %k : vector<16xf16>"] chk('mul', scan('x', far)[0][3], 'class 3: an update 200 lines below the init is still found') + # The corpus is part of the result: same bytes -> same fingerprint, a changed file -> a different one. + import tempfile + with tempfile.TemporaryDirectory() as d: + r = pathlib.Path(d) + (r / 'a.loom').write_text('one\n'); (r / 'sub').mkdir(); (r / 'sub' / 'b.loom').write_text('two\n') + fp1 = fingerprint(r) + chk(True, fp1.endswith('(2 .loom file(s))'), 'the fingerprint counts the .loom files it read') + chk(fp1, fingerprint(r), 'the same corpus fingerprints the same way twice') + (r / 'sub' / 'b.loom').write_text('two changed\n') + chk(False, fp1 == fingerprint(r), 'a one-byte change to one file changes the fingerprint') + (r / 'a.loom').rename(r / 'sub' / 'a.loom') + chk(False, fp1 == fingerprint(r), 'a file moved to another path changes it too (the path is hashed)') print(f"self-test: {'ok' if rc == 0 else 'FAIL'}") return rc +def fingerprint(root): + """sha256 over the corpus: sorted (relative path, file digest), so a moved file changes it too.""" + h = hashlib.sha256() + n = 0 + for p in sorted(root.rglob('*.loom')): + h.update(str(p.relative_to(root)).encode()) + h.update(hashlib.sha256(p.read_bytes()).digest()) + n += 1 + return f"{h.hexdigest()[:16]} ({n} .loom file(s))" + + def main(argv): if '--self-test' in argv: return self_test() @@ -245,6 +246,8 @@ def main(argv): print("usage: hrx_f16_narrowing_check.py ") return 2 root = pathlib.Path(argv[1]) + # The corpus is part of the result. Two runs that disagree are only comparable if the tree they + # read is named and fingerprinted, and "it found 9" is unattributable without both. inputs, pubs, accs = [], [], [] for p in sorted(root.rglob('*.loom')): lines = p.read_text(errors='replace').splitlines() @@ -255,6 +258,8 @@ def main(argv): pubs.append(f"{p.relative_to(root)}:{ln} {operand}") for ln, acc, width, kind, upd in scan(p, lines): accs.append((kind, f"{p.relative_to(root)}:{ln} {acc} vector<{width}xf16> [{upd or '-'}]")) + print(f"CORPUS {root.resolve()}") + print(f"FINGERPRINT {fingerprint(root)}") for r in inputs: print(f"INPUT {r}") for r in pubs: From bbacf7b719ad3a4b2164770f4122293c9e40f60e Mon Sep 17 00:00:00 2001 From: lane Date: Wed, 11 Nov 2026 22:42:14 -0400 Subject: [PATCH 2/2] tools/ci/dupdef_check.py: the licence header in the form tools/copyright.py expects `tools/copyright.py --check` failed on the new file in CI (the build job runs it before anything else), so this is the `--fix` output: the full Apache paragraph rather than the short SPDX-only header I wrote. `--check` rc 0 after; dupdef_check --check and --self-test unchanged. --- tools/ci/dupdef_check.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/ci/dupdef_check.py b/tools/ci/dupdef_check.py index 1bb0607a5..d5fe7a4a0 100644 --- a/tools/ci/dupdef_check.py +++ b/tools/ci/dupdef_check.py @@ -1,6 +1,18 @@ #!/usr/bin/env python3 # Copyright 2026 bong-water-water-bong # SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. """dupdef_check.py -- a shadowed top-level definition silently disables the one it shadows. A Python module may define `def self_test()` twice. The second wins, the first is dead, and nothing