Files
ARCADIA-Studio/harness/experiments/resize-port-policy-20260912/stable-ranking/run-stable-ranking-gates.py
T

322 lines
16 KiB
Python

"""Serialized identified 6203b5c gates. Requires a coordinator lease; no auto-run.
Usage: python .local/run-stable-ranking-gates.py MODE UNIQUE-LABEL
MODE: build, comparison, original-threshold, original-exit, stable-threshold,
stable-exit, allocator, anchors, summary
Expected criterion failures retain process exit101 and never mean adoption.
"""
from datetime import datetime, timezone
from pathlib import Path
import hashlib
import json
import os
import re
import shutil
import subprocess
import sys
import time
WORK = Path(__file__).resolve().parent.parent
ROOT = WORK.parent.parent
SOURCE = '6203b5c4c0cc8bd736196aa4744dccfc0e139a2a'
ORIGINAL_THRESHOLD = 'a_point_one_height_increase_does_not_needlessly_flip_twelve_of_twenty_ports'
ORIGINAL_EXIT = 'capacity_spillover_does_not_add_a_blocked_mandatory_stub_above_the_owner'
STABLE_THRESHOLD = 'stable_fallback_ranking_bounds_threshold_churn_across_three_identity_sets_and_reverse'
STABLE_EXIT = 'stable_fallback_ranking_still_needs_real_exit_clearance_including_near_corner'
COMPARISONS = [
'experimental_current_is_the_unchanged_default_projector',
'alternatives_realize_the_three_previously_forbidden_rectangles',
'all_eight_outward_handles_have_exact_fixed_edges_without_authored_inactive_changes',
'capacity_uses_available_sides_where_spacing_only_overlaps_fourteen_unit_glyphs',
'manual_anchor_edits_preserve_owner_geometry_and_unrelated_automatic_sides',
'exact_crowded_manual_fractions_survive_every_alternative_and_reopen',
'narrow_peer_gap_keeps_clear_sides_and_actual_routed_port_endpoints',
'candidate_geometry_is_deterministic_and_independent_of_peer_motion_and_names',
]
FALSIFIERS = [ORIGINAL_THRESHOLD, ORIGINAL_EXIT, STABLE_THRESHOLD, STABLE_EXIT]
ALLOCATORS = ['projection::port_layout::capacity_experiment::tests::' + name for name in [
'reassignment_recovers_scarce_clear_capacity',
'clear_overflow_does_not_choose_a_blocked_empty_side',
'small_clear_masks_match_an_exhaustive_assignment_oracle',
]]
GATES = {
'comparison': ('experimental_port_policy', None, COMPARISONS, None, False),
'original-threshold': ('experimental_port_policy_falsifiers', ORIGINAL_THRESHOLD, [ORIGINAL_THRESHOLD],
'Prospective resize-stability criterion failed:', True),
'original-exit': ('experimental_port_policy_falsifiers', ORIGINAL_EXIT, [ORIGINAL_EXIT],
'Prospective exit-clearance criterion failed:', True),
'stable-threshold': ('experimental_port_policy_falsifiers', STABLE_THRESHOLD, [STABLE_THRESHOLD],
'Prospective stable-ranking threshold criterion failed:', False),
'stable-exit': ('experimental_port_policy_falsifiers', STABLE_EXIT, [STABLE_EXIT],
'Prospective stable-ranking real-exit criterion failed:', True),
'allocator': ('oa_core', 'projection::port_layout::capacity_experiment::tests', ALLOCATORS, None, False),
}
def utc():
return datetime.now(timezone.utc).isoformat()
def sha(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def read(path):
return json.loads(path.read_bytes())
def write(path, value):
with path.open('x', encoding='utf-8', newline='\n') as stream:
stream.write(json.dumps(value, indent=2) + '\n')
def checked(args):
return subprocess.check_output(args, cwd=WORK, stderr=subprocess.STDOUT).decode().strip()
def stamp(paths):
return {str(p): {'bytes': p.stat().st_size, 'sha256': sha(p)} for p in sorted(set(paths))}
def changed(files):
return [name for name, item in files.items() if not Path(name).is_file()
or sha(Path(name)) != item['sha256']]
def source_ok():
assert checked(['git', 'rev-parse', 'HEAD']) == SOURCE
assert not checked(['git', 'status', '--porcelain', '--untracked-files=no'])
def committed_inputs():
# Status can hide assume-unchanged/skip-worktree flags or checkout
# transformations. Compare actual bytes with every blob in the exact tree.
names = subprocess.check_output(['git', 'ls-tree', '-r', '--name-only', '-z', SOURCE], cwd=WORK).decode().split('\0')
names = [name for name in names if name]
assert all('\n' not in name and '\r' not in name for name in names)
requests = ''.join(SOURCE + ':' + name + '\n' for name in names).encode()
batch = subprocess.run(['git', 'cat-file', '--batch'], input=requests, cwd=WORK,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True).stdout
cursor, verified = 0, {}
for name in names:
end = batch.index(b'\n', cursor)
header = batch[cursor:end].split()
assert len(header) == 3 and header[1] == b'blob', (name, header)
length = int(header[2])
cursor = end + 1
blob = batch[cursor:cursor + length]
assert len(blob) == length and (WORK / name).read_bytes() == blob, ('Committed bytes differ', name)
verified[str(WORK / name)] = {'bytes': length, 'sha256': hashlib.sha256(blob).hexdigest()}
cursor += length
assert batch[cursor:cursor + 1] == b'\n'
cursor += 1
assert cursor == len(batch)
return verified
def invoke(command, stem, env):
log = OUT / (stem + '.log')
start, clock = utc(), time.monotonic()
print('START', stem, command, flush=True)
with log.open('xb') as stream:
process = subprocess.run(command, cwd=WORK, env=env, stdout=stream, stderr=subprocess.STDOUT)
return {'source': SOURCE, 'command': command, 'start': start, 'end': utc(),
'seconds': round(time.monotonic() - clock, 3), 'exit': process.returncode,
'log': log.name, 'log_sha256': sha(log)}
def environment(manifest):
env = os.environ.copy()
for key, value in manifest['environment'].items():
env[key] = value
return env
def integrity(manifest):
source_ok()
return changed(manifest['files'])
def compile_targets(manifest, targets, stem):
assert not integrity(manifest)
command = [manifest['toolchain']['cargo']['path'], 'test', '--locked', '--jobs', '1',
'-p', 'oa-core', *targets, '--no-run', '--message-format=json']
result = invoke(command, stem, environment(manifest))
artifacts, binaries, core = [], {}, []
for line in (OUT / result['log']).read_text(encoding='utf-8', errors='replace').splitlines():
try:
message = json.loads(line)
except json.JSONDecodeError:
continue
if message.get('reason') != 'compiler-artifact':
continue
artifacts.extend(Path(name) for name in message['filenames'] if Path(name).is_file())
if message.get('executable') and message['target']['name'] in [
'experimental_port_policy', 'experimental_port_policy_falsifiers', 'oa_core', 'port_anchors']:
binaries[message['target']['name']] = message['executable']
if message['target']['name'] == 'oa_core' and not message['profile']['test']:
core.extend(Path(name) for name in message['filenames'] if name.endswith('.rlib'))
assert all(p.resolve().is_relative_to(TARGET) for p in artifacts), 'Unexpected artifact outside isolated target'
result.update({'artifacts': stamp(artifacts), 'binaries': binaries, 'core_rlibs': stamp(core),
'changed_inputs': integrity(manifest)})
result['build_accepted'] = result['exit'] == 0 and not result['changed_inputs']
write(OUT / (stem + '.json'), result)
return result
def build():
source_ok()
# Verify exact committed bytes before stamping all tracked workspace inputs,
# including source, fixtures, manifests and configuration.
committed = committed_inputs()
paths = [Path(p) for p in committed]
tools = {}
for name in ['cargo', 'rustc']:
path = Path(checked(['rustup', 'which', name]))
tools[name] = {'path': str(path), **stamp([path])[str(path)]}
linker = Path(shutil.which('clang.exe'))
tools['linker'] = {'path': str(linker), **stamp([linker])[str(linker)]}
tools['cargo_version'] = checked([tools['cargo']['path'], '-V'])
tools['rustc_version'] = checked([tools['rustc']['path'], '-Vv'])
tools['active_toolchain'] = checked(['rustup', 'show', 'active-toolchain'])
assert os.environ.get('RUSTFLAGS', '') in ['', '-Dwarnings'], 'Review nonstandard RUSTFLAGS first'
assert not os.environ.get('CARGO_ENCODED_RUSTFLAGS'), 'Review encoded flags first'
env_file = ROOT / '.local/toolchain/env.ps1'
assert Path(os.environ['CARGO_HOME']).resolve() == ROOT / '.local/toolchain/cargo'
assert Path(os.environ['RUSTUP_HOME']).resolve() == ROOT / '.local/toolchain/rustup'
paths += [env_file, Path(__file__).resolve(), ROOT / '.local/resize-stable-ranking-independent-review-20260912.md']
paths += [Path(tools[name]['path']) for name in ['cargo', 'rustc', 'linker']]
# Read-only protection of both prior experiment and current root core files.
# No cargo invocation points at either of their target directories.
for folder in [ROOT / 'target/debug/deps', ROOT / '.local/resize-port-policy-20260912/.local/target-7e25b3d-01/debug/deps']:
paths += list(folder.glob('liboa_core-*.rlib'))
files = stamp(paths)
assert all(files[name] == record for name, record in committed.items()), 'Input changed after blob verification'
manifest = {'source': SOURCE, 'start': utc(), 'worktree': str(WORK), 'target': str(TARGET),
'committed_blob_verification': {'source': SOURCE, 'count': len(committed),
'method': 'git cat-file --batch; exact blob/working-file byte equality before stamping'},
'toolchain': tools, 'files': files, 'policy_accepted': False,
'environment': {'CARGO_HOME': os.environ['CARGO_HOME'], 'RUSTUP_HOME': os.environ['RUSTUP_HOME'],
'CARGO_TARGET_DIR': str(TARGET), 'CARGO_BUILD_JOBS': '1', 'RUSTFLAGS': '-Dwarnings',
'RUSTC': tools['rustc']['path'], 'CARGO_TARGET_X86_64_PC_WINDOWS_GNULLVM_LINKER': str(linker)}}
write(OUT / 'inputs.json', manifest)
result = compile_targets(manifest, ['--test', 'experimental_port_policy', '--test', 'experimental_port_policy_falsifiers'], 'build')
required = {'experimental_port_policy', 'experimental_port_policy_falsifiers'}
assert result['build_accepted'] and required <= result['binaries'].keys() and len(result['core_rlibs']) == 1
return 0
def listing(binary, stem, env, expected, selected=None, exact=False):
args = [binary, '--list', '--format', 'terse']
if selected:
args.append(selected)
if exact:
args.append('--exact')
result = invoke(args, stem, env)
names = [line.removesuffix(': test') for line in (OUT / result['log']).read_text().splitlines()
if line.endswith(': test')]
result.update({'selected_names': names, 'expected_names': sorted(expected),
'selection_accepted': result['exit'] == 0 and sorted(names) == sorted(expected)})
write(OUT / (stem + '.json'), result)
assert result['selection_accepted'], (stem, names)
def gate(mode):
manifest, built = read(OUT / 'inputs.json'), read(OUT / 'build.json')
assert built['build_accepted'] and not integrity(manifest)
assert not changed(built['artifacts'])
env = environment(manifest)
if mode in ['allocator', 'anchors']:
targets = ['--lib'] if mode == 'allocator' else ['--test', 'port_anchors']
auxiliary = compile_targets(manifest, targets, mode + '-build')
assert auxiliary['build_accepted']
built = auxiliary
if mode == 'anchors':
source_text = (WORK / 'crates/oa-core/tests/port_anchors.rs').read_text()
expected = re.findall(r'#\[test\]\s*fn\s+(\w+)\s*\(', source_text)
assert len(expected) == 13
target, selected, marker, planned_failure = 'port_anchors', None, None, False
else:
target, selected, expected, marker, planned_failure = GATES[mode]
binary = built['binaries'][target]
frozen = stamp([Path(binary), *[Path(p) for p in built['core_rlibs']]])
# Prove the complete integration binary identity and nonempty selection.
if target == 'experimental_port_policy_falsifiers':
listing(binary, mode + '-inventory', env, FALSIFIERS)
exact = selected is not None and mode != 'allocator'
listing(binary, mode + '-list', env, expected, selected, exact)
geometry = OUT / (mode + '-geometry')
assert not geometry.exists()
env['OA_PORT_POLICY_RECEIPTS'] = str(geometry)
command = [binary, '--nocapture', '--test-threads=1']
if selected:
command.append(selected)
if exact:
command.append('--exact')
result = invoke(command, mode, env)
log = (OUT / result['log']).read_text(encoding='utf-8', errors='replace')
match = re.search(r'test result: (ok|FAILED)\. (\d+) passed; (\d+) failed; (\d+) ignored;', log)
counts = tuple(map(int, match.groups()[1:])) if match else None
passed = result['exit'] == 0 and counts == (len(expected), 0, 0)
designated = bool(result['exit'] == 101 and counts == (0, 1, 0) and marker and marker in log)
historical = None
if mode == 'original-threshold':
historical = all(f'threshold {policy}: adjacent side changes={values};' in log for policy, values in [
('Current', '[0, 0]'), ('SameSidesAdaptSpacing', '[0, 0]'), ('CapacityAware', '[12, 0]')])
elif mode == 'original-exit':
historical = all(f'obstacle {policy}: independently blocked=[], NoClearPath=[];' in log
for policy in ['Current', 'SameSidesAdaptSpacing']) and all(text in log for text in [
'obstacle CapacityAware: independently blocked=[(956a2026-0912-4000-8000-00000000006a, 956a2026-0912-4000-8000-00000000002a)]',
'NoClearPath=[956a2026-0912-4000-8000-0000000003ee]', 'all_NoClearPath=true'])
elif mode == 'stable-threshold':
rows = re.findall(r'stable threshold set=(\d+) policy=(\w+) changes=(\[[^\]]*\]);', log)
historical = len(rows) == 6 and all((str(i), 'CapacityAware', '[12, 0, 0, 12]') in rows for i in range(3))
elif mode == 'stable-exit':
historical = bool(re.search(r'^stable-obstacle-cornertrue-CapacityAwareStableRanking: actual Right midpoint .*actual blocked=\[\(', log, re.MULTILINE))
issues = integrity(manifest) + changed(frozen)
result.update({'target': target, 'expected_names': expected, 'counts_passed_failed_ignored': counts,
'outcome': 'test_passed' if passed else ('designated_criterion_failure' if designated else 'unexpected_failure'),
'planned_failure': planned_failure, 'designated_marker': marker,
'historical_or_completion_checks': historical, 'changed_inputs_or_binary': issues,
'compiled_core_identity': read(OUT / 'build.json')['core_rlibs'], 'binary_identity': frozen,
'geometry': stamp(p for p in geometry.rglob('*') if p.is_file()) if geometry.exists() else {},
'policy_accepted': False})
write(OUT / (mode + '.json'), result)
print(json.dumps({k: v for k, v in result.items() if k not in ['geometry', 'binary_identity']}), flush=True)
if issues or historical is False or not (passed or designated):
return 1
# A designated prospective failure remains a failed test, not an auto-pass.
return result['exit']
def summarize():
modes = [*GATES, 'anchors']
results = {mode: read(OUT / (mode + '.json')) for mode in modes if (OUT / (mode + '.json')).exists()}
record = {'source': SOURCE, 'utc': utc(), 'policy_accepted': False,
'results': {mode: {key: r[key] for key in ['exit', 'outcome', 'planned_failure',
'historical_or_completion_checks', 'changed_inputs_or_binary']} for mode, r in results.items()},
'missing_modes': [mode for mode in modes if mode not in results]}
write(OUT / 'summary.json', record)
print(json.dumps(record, indent=2))
return 0
if __name__ == '__main__':
MODE, LABEL = sys.argv[1:]
assert MODE in ['build', *GATES, 'anchors', 'summary'] and LABEL.replace('-', '').isalnum()
OUT = WORK / '.local' / ('gate-stable-6203b5c-' + LABEL)
TARGET = WORK / '.local' / ('target-stable-6203b5c-' + LABEL)
assert TARGET.resolve().is_relative_to(WORK / '.local')
if MODE == 'build':
assert not OUT.exists() and not TARGET.exists(), 'Use a fresh receipt AND target label'
OUT.mkdir()
else:
assert OUT.is_dir() and not (OUT / (MODE + '.json')).exists(), 'Never overwrite a mode receipt'
try:
code = build() if MODE == 'build' else (summarize() if MODE == 'summary' else gate(MODE))
except Exception as error:
write(OUT / (MODE + '-exception.json'), {'utc': utc(), 'source': SOURCE,
'type': type(error).__name__, 'error': str(error), 'policy_accepted': False})
raise
raise SystemExit(code)