Files
aji/tests/hosted/shield_relations.py
dev e3fc896a0a wip: shield index relations, dev evaluation tasks, Q-063 audit notes
Verifier borrow/provenance work, new phase3 shield_index conformance cases,
dev-intervals/dev-lru/dev-shards tasks with evaluate.py harness, and agent skill/hook config.
2026-10-17 12:30:37 +03:00

91 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""Q-063: stable shield conflicts, handoff, slot reuse and cancellation."""
import json
from pathlib import Path
import re
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / 'zig-out/bin/aji'
CORPUS = REPO / 'tests/conformance'
checks = 0
def cli(*args, code=0):
global checks
p = subprocess.run([str(AJI), *map(str, args)], capture_output=True)
checks += 1
assert p.returncode == code and not p.stderr, (args, p.returncode, p.stdout, p.stderr)
return p.stdout
def location(path, text):
rows = [(i + 1, line.index(text) + 1) for i, line in enumerate(path.read_text().splitlines()) if text in line]
assert len(rows) == 1, rows
return rows[0]
with tempfile.TemporaryDirectory(prefix='aji-shield-relations-') as directory:
root = Path(directory)
first = CORPUS / 'phase3/reject/shield_index_first_conflict.air'
joined = CORPUS / 'phase3/reject/shield_index_join_priority.air'
for path in (first, joined):
output = cli('check', path, code=1)
ds = [json.loads(line) for line in output.splitlines()]
assert [d['code'] for d in ds] == (['E4305'] if path == first else ['E3002', 'E4305']), ds
conflict = ds[-1]
if path == first:
expected = ('%a', '%b', 'entry', 5)
site = 'task.shield.end %a'
else:
expected = ('%y', '%x', 'joined', 1)
site = 'task.shield.end %y'
assert ds[0]['resource'] == '%x' and (ds[0]['line'], ds[0]['col']) == location(path, 'joined(%x:')
assert (conflict['borrow'], conflict['inner'], conflict['block'], conflict['inst']) == expected, conflict
assert (conflict['line'], conflict['col']) == location(path, site), conflict
assert all(d['file'] == str(path) for d in ds), ds
for command in ('check', 'run', 'report', 'explore'):
assert cli(command, path, code=1) == output
source = CORPUS / 'source/layer8/shield_index_reuse.aji'
canonical = cli('lower', source)
formatted = root / source.name
formatted.write_bytes(cli('fmt', source))
assert cli('fmt', formatted) == formatted.read_bytes()
assert cli('lower', formatted) == canonical
lowered = root / 'shield_index_reuse.air'
lowered.write_bytes(canonical)
cli('check', lowered)
positives = [
CORPUS / 'phase3/run/shield_index_handoff.air',
CORPUS / 'phase3/run/shield_index_reuse.air',
source, lowered,
CORPUS / 'phase3/run/cancel_shield.air',
CORPUS / 'source/layer8/cancel_shield.aji',
]
for path in positives:
cli('check', path)
grants = re.findall(r'^[;/]+ grant: (.+)$', path.read_text(), re.M)
args = [argument for grant in grants for argument in ('--grant', grant)]
expected = 105 if path.stem == 'cancel_shield' else 42
for quantum in (1, 5):
for seed in (0, 7, 31):
for fuel in (0, 1, 4, 16, 64, 256, 10000):
p = subprocess.run([str(AJI), 'run', '--fuel', str(fuel), '--quantum', str(quantum),
'--schedule-seed', str(seed), *args, str(path)], capture_output=True)
checks += 1
assert p.returncode in (0, 5) and not p.stderr, (path, p)
record = json.loads(p.stdout)
assert record['status'] == ('returned' if p.returncode == 0 else 'limit_exceeded'), record
if p.returncode == 0:
assert record['value'] == expected, (path, record)
if fuel == 10000:
assert p.returncode == 0, (path, record)
result = root / 'hosted.json'
for seed in (0, 7, 31):
cli('run', '--profile', 'hosted', '--result-file', result, '--fuel', '10000',
'--quantum', '1', '--schedule-seed', str(seed), *args, path)
assert json.loads(result.read_text())['value'] == expected
print(f'shield relations passed ({checks} CLI checks)')