9fe9d27865
Consolidate next-stage docs, archive superseded narratives, and preserve frozen document inputs. Retain invalid P2 pairs, partial RootXY training, failed attempts and nonselectable checkpoints as explicit limitations. Include supervised evaluation/training, desktop GPU admission, bounded evidence and atomic-publication monitor repairs with CPU regressions. Ignore local dotenv configuration. Validation: 1403 passed, 6 skipped for contact-cycle and related contracts/CLI tests. Full-suite legacy ABI and registration tests require Python 3.12/Torch 2.10/pxr; no runtime checks relaxed. No new physical run or accepted skill.
192 lines
10 KiB
Python
192 lines
10 KiB
Python
"""CPU preparation tests; only a harmless stdlib RLIMIT child, never Kit."""
|
|
import importlib.util
|
|
import json
|
|
import threading
|
|
from pathlib import Path
|
|
from types import SimpleNamespace as NS
|
|
|
|
import pytest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
spec = importlib.util.spec_from_file_location(
|
|
'preparation_supervisor_test', ROOT / 'scripts/prepare_cycle_sim_binding.py')
|
|
prep = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(prep)
|
|
|
|
|
|
def test_native_scan_cannot_block_deadline_owner(monkeypatch):
|
|
entered, release = threading.Event(), threading.Event()
|
|
|
|
def blocked(*_):
|
|
entered.set()
|
|
release.wait(5)
|
|
raise RuntimeError('injected scan failure')
|
|
|
|
monkeypatch.setattr(prep.r, 'NativeMonitor', blocked)
|
|
monitor = prep.AsyncNativeMonitor({'check_interval_seconds': .1})
|
|
try:
|
|
assert entered.wait(1)
|
|
assert monitor.check() is None # immediate, despite scan being blocked
|
|
monitor.state = (prep.b.boottime() - 3, {}, None)
|
|
with pytest.raises(RuntimeError, match='scan_stale'):
|
|
monitor.check()
|
|
finally:
|
|
monitor.stop.set()
|
|
release.set()
|
|
monitor.thread.join(1)
|
|
with pytest.raises(RuntimeError, match='injected scan failure'):
|
|
monitor.check()
|
|
|
|
|
|
def test_supervisor_refuses_prior_preparation_without_spawning(tmp_path, monkeypatch):
|
|
(tmp_path / 'prep-p1').mkdir()
|
|
monkeypatch.setattr(prep.os, 'pidfd_open', lambda *_: None, raising=False)
|
|
monkeypatch.setattr(prep.signal, 'pidfd_send_signal', lambda *_: None, raising=False)
|
|
monkeypatch.setattr(prep.subprocess, 'Popen', lambda *_a, **_k: pytest.fail('no spawn'))
|
|
with pytest.raises(RuntimeError, match='fresh_campaign_no_retry'):
|
|
prep.supervise(tmp_path, tmp_path / 'policy', '0' * 64)
|
|
|
|
|
|
def test_output_cap_retains_reserves(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(prep.b, 'artifact_size', lambda *_a, **_kw:
|
|
prep.b.CAP_BYTES - prep.b.HEADROOM_BYTES - prep.b.STDOUT_ALLOWANCE)
|
|
with pytest.raises(RuntimeError, match='aggregate_output_cap'):
|
|
prep.write(tmp_path, 'report.json', {'extra': 1})
|
|
assert not (tmp_path / 'report.json').exists()
|
|
|
|
|
|
def test_blocked_baseline_stops_at_prepare_deadline_without_spawn(tmp_path, monkeypatch):
|
|
clock = NS(now=0)
|
|
monkeypatch.setattr(prep.b, 'boottime', lambda: clock.now)
|
|
monkeypatch.setattr(prep.time, 'sleep', lambda _: setattr(clock, 'now', clock.now + 1))
|
|
monkeypatch.setattr(prep.os, 'pidfd_open', lambda *_: None, raising=False)
|
|
monkeypatch.setattr(prep.signal, 'pidfd_send_signal', lambda *_: None, raising=False)
|
|
monkeypatch.setattr(prep.r, 'storage_policy_check', lambda *_: {'not_after_boottime': 1000})
|
|
monkeypatch.setattr(prep.r, 'OwnedProcesses', lambda: NS(
|
|
supervisor=(1, 2), discover=lambda: {}, unknown=False, known=set()))
|
|
monkeypatch.setattr(prep.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0))
|
|
monkeypatch.setattr(prep.b, 'digest', lambda _: '0' * 64)
|
|
stopped = []
|
|
monkeypatch.setattr(prep, 'AsyncNativeMonitor', lambda _: NS(
|
|
check=lambda: None, state=(None, None, None), stop=NS(set=lambda: stopped.append(True)),
|
|
thread=NS(is_alive=lambda: True)))
|
|
monkeypatch.setattr(prep.subprocess, 'Popen', lambda *_a, **_k: pytest.fail('no spawn before baseline'))
|
|
assert prep.supervise(tmp_path, tmp_path / 'policy', '0' * 64) == 1
|
|
report = json.loads((tmp_path / 'preparation-supervisor.json').read_bytes())
|
|
assert report['finished_boottime'] == 480 and report['deadline_boottime'] == 600
|
|
assert report['cleanup_clean'] and not report['kit_only_launch_started']
|
|
assert report['errors'] == ["RuntimeError('preparation_480s_deadline')"]
|
|
assert stopped == [True]
|
|
|
|
|
|
@pytest.mark.parametrize('baseline_failure', [False, True])
|
|
def test_async_native_monitor_retains_structured_failure_report(monkeypatch, baseline_failure):
|
|
report = {'coverage_complete': False, 'latest_scan_completed': False,
|
|
'failed_scan': {'logical_bytes': 5, 'skipped_count': 0,
|
|
'failure': {'path': '/synthetic/owned', 'errno': 13,
|
|
'metadata': {'uid': 123, 'mode': 0o40700}}}}
|
|
|
|
def failed(*_a, **_kw):
|
|
raise prep.r.NativeScanError(report)
|
|
|
|
if baseline_failure:
|
|
monkeypatch.setattr(prep.r, 'NativeMonitor', failed)
|
|
else:
|
|
monkeypatch.setattr(prep.r, 'NativeMonitor', lambda *_: NS(report=lambda: {}, check=failed))
|
|
monitor = prep.AsyncNativeMonitor({'check_interval_seconds': .001})
|
|
monitor.thread.join(1)
|
|
assert not monitor.thread.is_alive()
|
|
assert monitor.state[1] == report
|
|
assert (monitor.state[0] is None) == baseline_failure
|
|
with pytest.raises(RuntimeError, match='/synthetic/owned'):
|
|
monitor.check()
|
|
|
|
|
|
@pytest.mark.parametrize('protocol_index', [0, 2])
|
|
def test_preparation_environment_uses_exact_monitored_redirects_without_launch(tmp_path, monkeypatch, protocol_index):
|
|
monkeypatch.setattr(prep.b, 'EXTENDED_ROOT', tmp_path)
|
|
protocol = prep.r.PROTOCOLS[protocol_index]
|
|
monkeypatch.setattr(prep.os, 'pidfd_open', lambda *_: None, raising=False)
|
|
monkeypatch.setattr(prep.signal, 'pidfd_send_signal', lambda *_: None, raising=False)
|
|
monkeypatch.setattr(prep.r, 'storage_policy_check', lambda *_: {'not_after_boottime': prep.b.boottime() + 1000})
|
|
monkeypatch.setattr(prep.r, 'OwnedProcesses', lambda: NS(
|
|
supervisor=(1, 2), discover=lambda: {}, unknown=False, known=set()))
|
|
monkeypatch.setattr(prep.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0))
|
|
monkeypatch.setattr(prep.b, 'digest', lambda _: '0' * 64)
|
|
monkeypatch.setattr(prep, 'AsyncNativeMonitor', lambda _: NS(
|
|
check=lambda: {}, state=(0, {}, None), stop=NS(set=lambda: None), thread=NS(is_alive=lambda: False)))
|
|
observed = []
|
|
|
|
def command(arguments, environment):
|
|
assert arguments[arguments.index('--protocol') + 1] == protocol
|
|
assert arguments[arguments.index('--output') + 1] == str(tmp_path / prep.preparation_name(protocol))
|
|
expected = prep.r.native_redirects(tmp_path / 'native/preparation-support', preparation=True)
|
|
assert {key: environment[key] for key in expected} == expected
|
|
assert set(expected.values()) <= set(prep.r.task_owned_cache_paths(tmp_path))
|
|
assert all(Path(path).is_dir() for path in expected.values())
|
|
observed.append(expected)
|
|
raise RuntimeError('CPU stop before launcher')
|
|
|
|
monkeypatch.setattr(prep.r, 'launch_command', command)
|
|
monkeypatch.setattr(prep.subprocess, 'Popen', lambda *_a, **_k: pytest.fail('no Kit launch'))
|
|
assert prep.supervise(tmp_path, tmp_path / 'policy', '0' * 64, protocol) == 1
|
|
assert len(observed) == 1
|
|
report = json.loads((tmp_path / 'preparation-supervisor.json').read_text())
|
|
assert not report['kit_only_launch_started']
|
|
assert report['errors'] == ["RuntimeError('CPU stop before launcher')"]
|
|
|
|
|
|
def test_without_core_dumps_is_child_only_exec_setup():
|
|
# A harmless stdlib child, not Kit; parent's limits must remain unchanged.
|
|
import resource
|
|
import subprocess
|
|
import sys
|
|
|
|
before = resource.getrlimit(resource.RLIMIT_CORE)
|
|
command = prep.without_core_dumps([
|
|
sys.executable, '-c',
|
|
'import resource; print(resource.getrlimit(resource.RLIMIT_CORE))'])
|
|
result = subprocess.run(command, capture_output=True, text=True, timeout=5, check=True)
|
|
assert result.stdout.strip() == '(0, 0)'
|
|
assert resource.getrlimit(resource.RLIMIT_CORE) == before
|
|
|
|
|
|
@pytest.mark.parametrize('returncode,clean', [(0, True), (139, True), (0, False)])
|
|
def test_supervisor_postexit_and_finally_cleanup_own_final_pass(tmp_path, monkeypatch, returncode, clean):
|
|
monkeypatch.setattr(prep.os, 'pidfd_open', lambda *_: None, raising=False)
|
|
monkeypatch.setattr(prep.signal, 'pidfd_send_signal', lambda *_: None, raising=False)
|
|
monkeypatch.setattr(prep.r, 'storage_policy_check', lambda *_: {'not_after_boottime': prep.b.boottime() + 1000})
|
|
owned = NS(supervisor=(1, 2), discover=lambda: {}, unknown=False, known={(22, 33)}, attach=lambda _: None)
|
|
monkeypatch.setattr(prep.r, 'OwnedProcesses', lambda: owned)
|
|
monkeypatch.setattr(prep.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0))
|
|
monkeypatch.setattr(prep.b, 'digest', lambda _: '0' * 64)
|
|
monkeypatch.setattr(prep, 'AsyncNativeMonitor', lambda _: NS(
|
|
check=lambda: {}, state=(0, {}, None), stop=NS(set=lambda: None), thread=NS(is_alive=lambda: False)))
|
|
monkeypatch.setattr(prep.r, 'launch_command', lambda *_: ['CPU-fake-child'])
|
|
monkeypatch.setattr(prep.r, 'launch_identity', lambda: {'scope': 'CPU fixture'})
|
|
monkeypatch.setattr(prep.subprocess, 'Popen', lambda *_a, **_k: NS(
|
|
returncode=returncode, stdout=NS(close=lambda: None), poll=lambda: returncode))
|
|
monkeypatch.setattr(prep.r, 'StdoutCapture', lambda *_: NS(
|
|
eof=True, written=0, discarded=0, error=None, close=lambda: None))
|
|
events = []
|
|
monkeypatch.setattr(prep.r, 'cleanup_group', lambda *_: events.append('cleanup') or clean)
|
|
monkeypatch.setattr(prep, 'postexit_identity', lambda *_: events.append('postexit-files') or {'status': 'PASS'})
|
|
assert prep.supervise(tmp_path, tmp_path / 'policy', '0' * 64) == (0 if returncode == 0 and clean else 1)
|
|
report = json.loads((tmp_path / 'preparation-supervisor.json').read_bytes())
|
|
assert events == ['cleanup', 'postexit-files']
|
|
assert report['returncode'] == returncode and report['cleanup_clean'] == clean
|
|
assert report['owned_remaining'] == {} and not report['ownership_unknown']
|
|
if returncode:
|
|
assert 'preparation_child_exit_139' in report['errors'][0]
|
|
|
|
|
|
def test_postexit_rehash_cannot_block_total_deadline(tmp_path, monkeypatch):
|
|
release = threading.Event()
|
|
monkeypatch.setattr(prep, 'postexit_identity', lambda *_: release.wait(3))
|
|
errors = []
|
|
try:
|
|
assert prep.collect_postexit_identity(tmp_path, {}, NS(), errors, prep.b.boottime() - 1) is None
|
|
assert errors == ['preparation_postexit_identity_deadline']
|
|
finally:
|
|
release.set()
|