From 193ec41bcab9deb783ecebe21016a23773449e0f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 5 Nov 2026 22:38:52 +0000 Subject: [PATCH] cbz_drift: bind the declaration kind in reviewed LEAN-DRIFT exemptions A reviewed exemption keyed each location on repository, path and the SHA-256 of its interface. Theorem and axiom both keep only the header as their interface, so turning an exempted theorem into an axiom with the same header stayed exempt (reproduced by dot / OpenAI on #751, comment 6671147967). Drift itself already compares (kind, interface). Each exemption location now also names its declaration kind, which is required and part of the exact-match key. A missing or empty kind is a MALFORMED BLOCK. The shipped entry FEDDRIFT-C88-01 records "theorem" for both locations; its interface hashes are unchanged. Tests: the reproduced regression, missing- and empty-kind malformed cases, and two mutants (kind dropped from the match key; empty kind accepted), each killed by assertion. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DKKe26v3CSgdYch8kiJLt9 --- tests/test_cbz_drift.py | 24 ++++++++++++++++++++++-- tools/cbz_drift/crawl.py | 17 ++++++++++------- tools/cbz_drift/reviewed_exemptions.json | 4 +++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/tests/test_cbz_drift.py b/tests/test_cbz_drift.py index 3a7dc219b..d890f60a0 100644 --- a/tests/test_cbz_drift.py +++ b/tests/test_cbz_drift.py @@ -284,7 +284,7 @@ def doc(f, **kw): for loc in v['locations']: repo, rest = loc.split('@', 1) locs.append({'repository': repo, 'path': rest.split(':', 1)[1].rsplit(':', 1)[0], - 'interface_sha256': v['interface_sha256']}) + 'kind': v['kind'], 'interface_sha256': v['interface_sha256']}) ent = dict({'id': 'T-01', 'name': f['name'], 'rationale': 'reviewed in a test', 'locations': locs}, **kw) return {'schema': crawl.EXEMPTIONS_SCHEMA, 'exemptions': [ent]} @@ -312,6 +312,18 @@ def test_proof_and_comment_edits_stay_exempt(self): self.assertEqual(codes(rep, 'BLOCK'), []) self.assertIn('LEAN-EXEMPT', codes(rep, 'INFO')) + def test_changed_kind_invalidates_exemption(self): + # theorem and axiom keep the same header interface, so the kind must be bound too + doc = self.doc(self.drifted()) + edited = self.STALE.replace('theorem t (n : Nat) : 0 + n = n := by simp', 'axiom t (n : Nat) : 0 + n = n') + self.assertNotEqual(edited, self.STALE) + self.fed.pins['b'] = self.fed.commit('b', {'Copy/A.lean': edited}) + self.fed.write_seed() + self.assertEqual(codes(self.fed.crawl(), 'BLOCK'), ['LEAN-DRIFT']) + rep = self.fed.crawl(exemptions=doc) + self.assertEqual(codes(rep, 'BLOCK'), ['LEAN-DRIFT']) + self.assertIn('EXEMPT-UNUSED', codes(rep, 'INFO')) + def test_a_new_location_blocks_again(self): doc = self.doc(self.drifted()) self.fed.pins['b'] = self.fed.commit('b', {'Copy2/A.lean': self.STALE}) @@ -331,7 +343,11 @@ def test_malformed_exemptions_block(self): one['exemptions'][0]['locations'] = one['exemptions'][0]['locations'][:1] bad_hash = self.doc(f) bad_hash['exemptions'][0]['locations'][0]['interface_sha256'] = 'x' * 64 - for doc in ({'schema': 'other', 'exemptions': []}, {'error': 'not json'}, one, bad_hash): + no_kind = self.doc(f) + del no_kind['exemptions'][0]['locations'][0]['kind'] + empty_kind = self.doc(f) + empty_kind['exemptions'][0]['locations'][0]['kind'] = '' + for doc in ({'schema': 'other', 'exemptions': []}, {'error': 'not json'}, one, bad_hash, no_kind, empty_kind): rep = self.fed.crawl(exemptions=doc) self.assertIn('MALFORMED', codes(rep, 'BLOCK'), doc) self.assertIn('LEAN-DRIFT', codes(rep, 'BLOCK'), doc) @@ -808,6 +824,10 @@ def test_malformed_raise(self): # Each mutant: (label, exact source substring, replacement, file, designated test). MUTANTS = [ + ('exemption ignores kind', "key = frozenset((l['repo'], l['path'], l['kind'], hashlib", "key = frozenset((l['repo'], l['path'], 'theorem', hashlib", + 'crawl.py', 'ExemptionTests.test_changed_kind_invalidates_exemption'), + ('empty exemption kind accepted', "and isinstance(l.get('kind'), str) and l.get('kind')\n", "and isinstance(l.get('kind'), str)\n", + 'crawl.py', 'ExemptionTests.test_malformed_exemptions_block'), ('theorem statement ignored', "'interface': header if dkind in STATEMENT_ONLY else", "'interface': '' if dkind in STATEMENT_ONLY else", 'lean_decls.py', 'LeanDriftTests.test_changed_theorem_statement_blocks'), ('definition value ignored', "else ' '.join(hdr + body)", "else header", 'lean_decls.py', diff --git a/tools/cbz_drift/crawl.py b/tools/cbz_drift/crawl.py index 2710032ba..c675d96d6 100644 --- a/tools/cbz_drift/crawl.py +++ b/tools/cbz_drift/crawl.py @@ -33,7 +33,7 @@ LEAN-EVIDENCE Lean files under an evidence directory are frozen copies, not INFO compared (sealed checker inputs, snapshots, mutation controls) LEAN-EXEMPT a LEAN-DRIFT matched a reviewed exemption exactly (every INFO - location's repository, path and interface SHA-256) + location's repository, path, declaration kind and interface SHA-256) EXEMPT-UNUSED a reviewed exemption matched no drift this run (it is stale) INFO LAKE-DEP a lake dependency on a crawled repository pins another rev INFO PIN-DIVERGENT two reached repositories pin one repository differently INFO @@ -52,10 +52,11 @@ Reviewed exemptions. `reviewed_exemptions.json` beside this file (or `--exemptions FILE`) lists LEAN-DRIFT findings that a review has accepted. An entry names one declaration and -the complete set of its locations, each as repository, path and the SHA-256 of the -interface the crawl computes there. A drift is exempt only when its location set equals an -entry's exactly; any change to either interface, or a new or removed location, blocks -again. Comments, whitespace and proof bodies are outside the interface, so they do not. +the complete set of its locations, each as repository, path, declaration kind (theorem, +def, axiom, ...) and the SHA-256 of the interface the crawl computes there. A drift is exempt +only when its location set equals an entry's exactly; any change to either interface or +kind, or a new or removed location, blocks again. The kind is bound separately because +theorem and axiom both keep only the header as their interface. Comments, whitespace and proof bodies are outside the interface, so they do not. A malformed exemptions file is a BLOCK. The list lives with the tool, not in a crawled repository, so a crawled repository cannot exempt itself. @@ -238,12 +239,13 @@ def parse_exemptions(self, doc): ok = (isinstance(ent, dict) and all(isinstance(ent.get(k), str) and ent.get(k) for k in ('id', 'name', 'rationale')) and isinstance(locs, list) and len(locs) >= 2 and all(isinstance(l, dict) and isinstance(l.get('repository'), str) and isinstance(l.get('path'), str) + and isinstance(l.get('kind'), str) and l.get('kind') and re.fullmatch(r'[0-9a-f]{64}', str(l.get('interface_sha256', ''))) for l in locs)) if not ok or ent['id'] in seen: self.exemption_errors.append('exemption %r is malformed or duplicated' % (ent.get('id') if isinstance(ent, dict) else ent)) continue seen.add(ent['id']) - key = frozenset((repo_key(l['repository']), l['path'], l['interface_sha256']) for l in locs) + key = frozenset((repo_key(l['repository']), l['path'], l['kind'], l['interface_sha256']) for l in locs) if len(key) != len(locs): self.exemption_errors.append('exemption %r lists a location twice' % ent['id']) continue @@ -670,7 +672,8 @@ def check_lean(self): for l in locs: variants.setdefault((l['kind'], l['interface']), []).append(l) if len(variants) > 1: - key = frozenset((l['repo'], l['path'], hashlib.sha256(l['interface'].encode('utf-8')).hexdigest()) for l in locs) + key = frozenset((l['repo'], l['path'], l['kind'], hashlib.sha256(l['interface'].encode('utf-8')).hexdigest()) + for l in locs) ex = next((e for e in self.exemptions if e['name'] == name and e['key'] == key), None) detail = [{'kind': k, 'interface': i, 'interface_sha256': hashlib.sha256(i.encode('utf-8')).hexdigest(), 'locations': ['%s@%s:%s:%d' % (l['repo'], l['ref'][:12], l['path'], l['line']) for l in ls]} diff --git a/tools/cbz_drift/reviewed_exemptions.json b/tools/cbz_drift/reviewed_exemptions.json index 81feb2ed4..e2e92865f 100644 --- a/tools/cbz_drift/reviewed_exemptions.json +++ b/tools/cbz_drift/reviewed_exemptions.json @@ -4,16 +4,18 @@ { "id": "FEDDRIFT-C88-01", "name": "CapFirstExit.frontier_image_subset", - "rationale": "Math-#514 added a reused copy of this theorem whose context opens Set where the original opens Set unitInterval; the copy's single topology import does not provide unitInterval. The statement uses no name from the unitInterval namespace (no scoped I, no sigma, no unitInterval.* identifier), checked against the interface the crawl records at both locations, so the context difference does not change it. Only a change to either interface (the statement or its context: open, variable and the like) re-blocks; comments, whitespace and proof bodies are outside interface_sha256. Remove this entry if the #514 packet owner restores the original context in the copy. Routed at main#738 comments 6568570507 and 6569726362; design 6568753056; wording 6570035707.", + "rationale": "Math-#514 added a reused copy of this theorem whose context opens Set where the original opens Set unitInterval; the copy's single topology import does not provide unitInterval. The statement uses no name from the unitInterval namespace (no scoped I, no sigma, no unitInterval.* identifier), checked against the interface the crawl records at both locations, so the context difference does not change it. Only a change to either interface (the statement or its context: open, variable and the like) or to either declaration kind (both are theorem) re-blocks; comments, whitespace and proof bodies are outside interface_sha256. Remove this entry if the #514 packet owner restores the original context in the copy. Routed at main#738 comments 6568570507 and 6569726362; design 6568753056; wording 6570035707.", "locations": [ { "repository": "d6g8k5htny-coder/Math-", "path": "frontiers/cap_first_exit_lean_20261002/CapFirstExit.lean", + "kind": "theorem", "interface_sha256": "d0b4c10808199ba143e93ceb53999b618a000f7fc8b45b98720b605e0dc8c317" }, { "repository": "d6g8k5htny-coder/Math-", "path": "frontiers/source_derivative_gaussian_20261025/inputs/elder/ReusedCompactFrontier.lean", + "kind": "theorem", "interface_sha256": "460289c21db481a8af33b1910b066f50100fd64c2a5c9798318eed7522e4519c" } ]