#!/usr/bin/env python3 """Compare the incremental reservation bound to the uncached exact compiler. This oracle is deliberately outside timing and does not install a cached plan. The operational capacity sweep does not invoke the exact-plan query at all. """ import hashlib import importlib.util import json from pathlib import Path import random import sys import tempfile HERE = Path(__file__).resolve().parent ROOT = HERE.parent sys.dont_write_bytecode = True def load(path): spec = importlib.util.spec_from_file_location('reservation_bound_engine', path) module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module spec.loader.exec_module(module) return module def main(): engine_path = ROOT / 'pagefs.py' source_hash = hashlib.sha256(engine_path.read_bytes()).hexdigest() module = load(engine_path) rng = random.Random(701154) checks = 0 checkpoints = 0 reopens = 0 greatest_slack = 0 max_exact = 0 for case in range(4): with tempfile.TemporaryDirectory(prefix='nyarion-bound-oracle-') as tmp: path = Path(tmp) / 'oracle.img' expected = {f'/seed/{i:04d}': bytes([i]) * (128 if case < 2 else 257) for i in range(64)} store = module.PageStore.create(path, initial_files=expected, pool_blocks=4096) tokens = {} try: for i in range(90): keys = [f'/seed/{rng.randrange(80):04d}'] if i % 9 == 0: keys.append('/escaped/"control\t' + ('界' * 12)) changes = {} for key in keys: size = (0, 1, 128, 256, 257, 3992, 8192)[rng.randrange(7)] changes[key] = None if i % 13 == 0 else rng.randbytes(size) token = f'oracle-{case}-{i:04d}' epoch = store.transact(changes, token) tokens[token] = epoch for key, value in changes.items(): if value is None: expected.pop(key, None) else: expected[key] = value budget = store.admission_reservation() prior_cached = store._checkpoint_plan plan = store._prepare_checkpoint_plan() assert store._checkpoint_plan is prior_cached, 'oracle must not install a plan' exact = len(plan['dependency_writes']) bound = budget['reserved_upper_blocks'] assert exact <= bound, (case, i, exact, bound) assert exact <= len(store._free), 'acknowledged window must fit the exact compiler' max_exact = max(max_exact, exact) greatest_slack = max(greatest_slack, bound - exact) checks += 1 if i % 11 == 10: store.checkpoint() checkpoints += 1 if i % 30 == 29: store.close() store = module.PageStore(path) reopens += 1 assert store.files() == sorted(expected) for key, value in expected.items(): assert store.get(key) == value for token, epoch in tokens.items(): assert store.query(token)['epoch'] == epoch store.checkpoint() checkpoints += 1 finally: store.close() assert hashlib.sha256(engine_path.read_bytes()).hexdigest() == source_hash, 'engine changed during oracle checks' result = {'passed': True, 'engine_source_sha256': source_hash, 'exact_compiler_bound_checks': checks, 'checkpoints': checkpoints, 'reopens': reopens, 'maximum_exact_physical_blocks': max_exact, 'greatest_observed_bound_slack_blocks': greatest_slack, 'cached_plan_not_installed_by_oracle': True, 'scope': 'finite exact-compiler comparison after mixed overwrites/deletes/inline/blob changes; not a universal bound proof'} (HERE / 'reservation_bound_results.json').write_text(json.dumps(result, indent=2) + '\n') print(json.dumps(result, indent=2)) if __name__ == '__main__': main()