Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion tests/test_cbz_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,39 @@ def test_changed_variable_in_scope_blocks(self):
rep = self.drift(LEAN_A.replace('namespace Fed\n', 'namespace Fed\nvariable (k : Nat)\n'))
self.assertEqual(sorted(f['name'] for f in rep['findings'] if f['code'] == 'LEAN-DRIFT'), ['Fed.S', 'Fed.f', 'Fed.t'])

def write_ack(self, entries):
path = pathlib.Path(self.tmp.name) / 'ack.json'
path.write_text(json.dumps({'schema': 'cbz.drift.ack.v1', 'acknowledged': entries}))
return str(path)

def test_acknowledged_drift_is_info_and_any_change_blocks_again(self):
rep = self.drift(LEAN_A.replace('n + 0 = n', '0 + n = n'))
drift = [f for f in rep['findings'] if f['code'] == 'LEAN-DRIFT'][0]
ack = self.write_ack([{'name': 'Fed.t', 'interfaces_sha256': [v['sha256'] for v in drift['variants']],
'reason': 'reviewed'}])
rep = self.fed.crawl(ack_path=ack)
self.assertEqual(codes(rep, 'BLOCK'), [])
self.assertEqual([(f['name'], f['level'], f['acknowledged']) for f in rep['findings']
if f['code'] == 'LEAN-DRIFT-ACK'], [('Fed.t', 'INFO', 'reviewed')])
# any further change to either copy no longer matches the reviewed set
self.drift(LEAN_A.replace('n + 0 = n', 'n * 1 = n'))
self.assertEqual(codes(self.fed.crawl(ack_path=ack), 'BLOCK'), ['LEAN-DRIFT'])

def test_acknowledgement_for_another_name_does_not_apply(self):
rep = self.drift(LEAN_A.replace('n + 0 = n', '0 + n = n'))
drift = [f for f in rep['findings'] if f['code'] == 'LEAN-DRIFT'][0]
ack = self.write_ack([{'name': 'Fed.other', 'interfaces_sha256': [v['sha256'] for v in drift['variants']],
'reason': 'reviewed'}])
self.assertEqual(codes(self.fed.crawl(ack_path=ack), 'BLOCK'), ['LEAN-DRIFT'])

def test_malformed_acknowledgements_block(self):
self.fed.build()
for entries in ([{'name': 'Fed.t', 'interfaces_sha256': ['x' * 64, 'y' * 64], 'reason': 'r'}],
[{'name': 'Fed.t', 'interfaces_sha256': ['a' * 64, 'b' * 64], 'reason': ' '}],
[{'name': 'Fed.t', 'interfaces_sha256': ['a' * 64], 'reason': 'r'}]):
with self.subTest(entries=entries):
self.assertIn('MALFORMED', codes(self.fed.crawl(ack_path=self.write_ack(entries)), 'BLOCK'))

def test_lean_syntax_error_blocks(self):
rep = self.drift(LEAN_A + '\n/- never closed\n')
self.assertIn('LEAN-SYNTAX', codes(rep, 'BLOCK'))
Expand Down Expand Up @@ -737,7 +770,7 @@ def test_malformed_raise(self):
'LeanDriftTests.test_changed_definition_value_blocks'),
('kind ignored', "variants.setdefault((l['kind'], l['interface']), [])", "variants.setdefault(('', l['interface']), [])",
'crawl.py', 'LeanDriftTests.test_changed_kind_blocks'),
('drift downgraded', "self.add(BLOCK, 'LEAN-DRIFT'", "self.add(INFO, 'LEAN-DRIFT'", 'crawl.py',
('drift downgraded', "self.add(INFO if acked else BLOCK,", "self.add(INFO if acked else INFO,", 'crawl.py',
'LeanDriftTests.test_changed_theorem_statement_blocks'),
('private compared', " if d['private']:\n continue\n", "", 'crawl.py',
'LeanDriftTests.test_private_names_are_not_compared'),
Expand Down Expand Up @@ -859,6 +892,12 @@ def test_malformed_raise(self):
('case-sensitive identity', " return ('%s/%s' % (m.group(1), m.group(2))).lower()", " return '%s/%s' % (m.group(1), m.group(2))", 'crawl.py',
'TopologyTests.test_identity_normalization'),
('exit ignores blocks', " return 1 if rep['blocking'] else 0", " return 0", 'crawl.py', 'CleanFederationTests.test_cli_exit_status'),
('ack matched by name only', "acked = ack is not None and ack[0] == frozenset(interface_digest(k, i) for k, i in variants)",
"acked = ack is not None", 'crawl.py', 'LeanDriftTests.test_acknowledged_drift_is_info_and_any_change_blocks_again'),
('ack ignored', "self.add(INFO if acked else BLOCK, 'LEAN-DRIFT-ACK' if acked else 'LEAN-DRIFT',",
"self.add(BLOCK, 'LEAN-DRIFT',", 'crawl.py', 'LeanDriftTests.test_acknowledged_drift_is_info_and_any_change_blocks_again'),
('malformed ack accepted', " raise ValueError('malformed acknowledgement %.160r' % (entry,))", " continue",
'crawl.py', 'LeanDriftTests.test_malformed_acknowledgements_block'),
('evidence copies compared', " if EVIDENCE_SEGMENT.search(path):", " if False:", 'crawl.py',
'LeanDriftTests.test_evidence_copies_are_counted_not_compared'),
('every evidence-like name skipped', "EVIDENCE_SEGMENT = re.compile(r'(^|/)([^/]*-)?evidence(/|$)')",
Expand Down
1 change: 1 addition & 0 deletions tools/cbz_drift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ lag a moving tip (INFO).**
| `LEAN-DRIFT` | BLOCK | one fully qualified Lean declaration has different interfaces in two crawled locations |
| `LEAN-SYNTAX` | BLOCK | a Lean file cannot be lexed, or its `namespace`/`section`/`end` do not balance |
| `LEAN-EVIDENCE` | INFO | Lean files under an evidence directory (a path segment `evidence` or `*-evidence`) are frozen copies — sealed checker inputs, snapshots of earlier commits, mutation controls — and are counted but not compared |
| `LEAN-DRIFT-ACK` | INFO | a `LEAN-DRIFT` whose exact set of interfaces (SHA-256 of `kind\0interface`) is a reviewed entry in `acknowledged_drift.json`, with its reason and evidence. Any other change to either copy no longer matches and blocks again; a malformed file is `MALFORMED` (BLOCK) |
| `LAKE-DEP` | INFO | a lake dependency on a crawled repository pins a present, different full commit id (any other rev, such as an absent, empty, short or floating one like a branch or tag, is `PIN-MISSING`; a git package with no repository URL, a package that is neither git nor path, and a `packages` value that is not a list are `MALFORMED`) |
| `PIN-DIVERGENT` | INFO | two reached repositories pin one repository differently; the first in breadth-first order is read |
| `FETCH-FAILED` | BLOCK | `--fetch` could not refresh a repository, so its tips are not current |
Expand Down
19 changes: 19 additions & 0 deletions tools/cbz_drift/acknowledged_drift.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"schema": "cbz.drift.ack.v1",
"acknowledged": [
{
"name": "CapFirstExit.frontier_image_subset",
"interfaces_sha256": [
"f5c08a64f33954585a9ffb261b3178f7364101b8166802fc1141af080f1bb6a6",
"cf341f363a8153d09902f2be07070c5c0078318789791837b2922b46c03c8a2d"
],
"reason": "Deliberate single-theorem extraction. The copy in Math- frontiers/source_derivative_gaussian_20261025/inputs/elder/ReusedCompactFrontier.lean keeps the statement and proof byte-identical to frontiers/cap_first_exit_lean_20261002/CapFirstExit.lean lines 359-369 and drops only the unused `open unitInterval` (its narrow import, Mathlib.Topology.Separation.Hausdorff, does not provide that namespace). Recorded as successor_context_delta in COMPACT_FRONTIER_INPUT_BINDING.json, with a strict compile receipt. Lexical equality of the statement only; elaborated equality is not established here (B-006).",
"evidence": [
"d6g8k5htny-coder/Math-@ce4b43c5:frontiers/source_derivative_gaussian_20261025/COMPACT_FRONTIER_INPUT_BINDING.json",
"d6g8k5htny-coder/Math-@ce4b43c5:frontiers/source_derivative_gaussian_20261025/compact-frontier-input-evidence/strict-01.json",
"https://github.com/d6g8k5htny-coder/main/issues/738#issuecomment-6669319293"
],
"added": "2026-11-05"
}
]
}
55 changes: 49 additions & 6 deletions tools/cbz_drift/crawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
INPUT-MISSING a declared canonical input is absent at its selected ref BLOCK
LEAN-DRIFT one fully qualified Lean declaration has different interfaces BLOCK
in two crawled locations (see lean_decls for the exact domain)
LEAN-DRIFT-ACK the same, but the exact set of interfaces is a reviewed entry in INFO
acknowledged_drift.json; any other change to either copy blocks
LEAN-SYNTAX a Lean file could not be lexed or its scopes do not balance BLOCK
LEAN-EVIDENCE Lean files under an evidence directory are frozen copies, not INFO
compared (sealed checker inputs, snapshots, mutation controls)
Expand Down Expand Up @@ -69,6 +71,35 @@
# formal-evidence/) holds frozen copies: sealed checker inputs, snapshots of earlier commits and mutation
# controls. They are deliberately different from the live sources, so they are not compared.
EVIDENCE_SEGMENT = re.compile(r'(^|/)([^/]*-)?evidence(/|$)')
# Reviewed drift acknowledgements. Each entry names one declaration and the exact set of its interfaces,
# as SHA-256 of "kind\0interface". A drift whose variants are exactly that set is reported as INFO
# LEAN-DRIFT-ACK with the recorded reason; any other change to either copy no longer matches and blocks.
ACK_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'acknowledged_drift.json')


def interface_digest(kind, interface):
return hashlib.sha256(('%s\0%s' % (kind, interface)).encode('utf-8')).hexdigest()


def load_acknowledgements(path):
"""Return {name: (frozenset of interface digests, reason)}. Raise ValueError on a malformed file."""
if not os.path.exists(path):
return {}
with open(path, encoding='utf-8') as fh:
doc = json.load(fh)
if not isinstance(doc, dict) or doc.get('schema') != 'cbz.drift.ack.v1' or not isinstance(doc.get('acknowledged'), list):
raise ValueError('expected schema cbz.drift.ack.v1 with an acknowledged list')
acks = {}
for entry in doc['acknowledged']:
name, digests, reason = (entry.get('name'), entry.get('interfaces_sha256'), entry.get('reason')) \
if isinstance(entry, dict) else (None, None, None)
if not (isinstance(name, str) and isinstance(reason, str) and reason.strip() and isinstance(digests, list)
and len(set(digests)) >= 2 and all(isinstance(d, str) and re.fullmatch(r'[0-9a-f]{64}', d) for d in digests)):
raise ValueError('malformed acknowledgement %.160r' % (entry,))
if name in acks:
raise ValueError('two acknowledgements for %s' % name)
acks[name] = (frozenset(digests), reason)
return acks


class CrawlError(RuntimeError):
Expand Down Expand Up @@ -196,8 +227,9 @@ def parse_gitmodules(text):


class Crawl:
def __init__(self, local, at='pins', overrides=None, fetch=False, clone_into=None):
def __init__(self, local, at='pins', overrides=None, fetch=False, clone_into=None, ack_path=ACK_PATH):
self.local, self.at, self.overrides, self.fetch = local, at, dict(overrides or {}), fetch
self.ack_path = ack_path
self.clone_into, self.urls = clone_into, {}
self.findings, self.repos, self.refs = [], {}, {}
self.pins, self.optional, self.edges = {}, set(), []
Expand Down Expand Up @@ -582,6 +614,11 @@ def crawled(self):
yield key, self.repos[key], self.ref_for(key, self.repos[key])

def check_lean(self):
try:
acks = load_acknowledgements(self.ack_path)
except (OSError, ValueError) as exc:
self.add(BLOCK, 'MALFORMED', 'drift acknowledgements %s: %s' % (self.ack_path, exc))
acks = {}
decls = collections.defaultdict(list)
packages = {}
for key, repo, ref in list(self.crawled()):
Expand Down Expand Up @@ -628,9 +665,15 @@ def check_lean(self):
for l in locs:
variants.setdefault((l['kind'], l['interface']), []).append(l)
if len(variants) > 1:
self.add(BLOCK, 'LEAN-DRIFT', '%s has %d different interfaces across %d locations' % (name, len(variants), len(locs)),
name=name, variants=[{'kind': k, 'interface': i, 'locations': ['%s@%s:%s:%d' % (
l['repo'], l['ref'][:12], l['path'], l['line']) for l in ls]} for (k, i), ls in variants.items()])
ack = acks.get(name)
acked = ack is not None and ack[0] == frozenset(interface_digest(k, i) for k, i in variants)
detail = {'acknowledged': ack[1]} if acked else {}
self.add(INFO if acked else BLOCK, 'LEAN-DRIFT-ACK' if acked else 'LEAN-DRIFT',
'%s has %d different interfaces across %d locations' % (name, len(variants), len(locs)),
name=name, variants=[{'kind': k, 'interface': i, 'sha256': interface_digest(k, i),
'locations': ['%s@%s:%s:%d' % (
l['repo'], l['ref'][:12], l['path'], l['line']) for l in ls]} for (k, i), ls in variants.items()],
**detail)
self.stats['lean_shared_names'] = shared
for (a, b) in sorted({tuple(sorted((x, y))) for x, ys in coupled.items() for y in ys}):
if packages.get(a) != packages.get(b):
Expand All @@ -651,9 +694,9 @@ def report(self):
}


def run(seed, search, at='pins', seed_ref=None, overrides=None, fetch=False, clone_into=None):
def run(seed, search, at='pins', seed_ref=None, overrides=None, fetch=False, clone_into=None, ack_path=ACK_PATH):
local = find_local_repos(search)
c = Crawl(local, at=at, overrides=overrides, fetch=fetch, clone_into=clone_into)
c = Crawl(local, at=at, overrides=overrides, fetch=fetch, clone_into=clone_into, ack_path=ack_path)
c.seed(repo_key(seed) if '/' in seed else _match_name(seed, local), seed_ref)
c.close_graph()
c.check_pins()
Expand Down
Loading