mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
182 lines
8.4 KiB
Python
182 lines
8.4 KiB
Python
"""Real-process crash recovery using a gated synthetic job, never a model."""
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
import json
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from obliteratus.mcp_execution import ExecutionTools, ModelInput, RunResources
|
|
from obliteratus.mcp_policy import McpPolicy
|
|
from obliteratus.mcp_worker import atomic_json, cancel_guarded, load, locked, recover, worker_matches
|
|
from obliteratus.read_boundary import open_directory
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
pytestmark = pytest.mark.skipif(not Path('/proc/self/stat').is_file(), reason='Linux process identity required')
|
|
|
|
|
|
def wait_for(predicate):
|
|
deadline = time.monotonic() + 15
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
value = predicate()
|
|
if value:
|
|
return value
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
|
pass
|
|
time.sleep(.01)
|
|
pytest.fail('synthetic process transition timed out')
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def crashed_launcher(tmp_path):
|
|
archive, model, guard = (tmp_path / name for name in ('archive', 'model', 'guard'))
|
|
for path in (archive, model, guard):
|
|
path.mkdir(mode=0o700)
|
|
(model / 'config.json').write_text('{}')
|
|
(model / 'model.safetensors').write_bytes(b'synthetic')
|
|
marker, release = tmp_path / 'job.json', tmp_path / 'release'
|
|
job_code = (
|
|
'import json,os,sys,time\nfrom pathlib import Path\n'
|
|
'Path(sys.argv[1]).write_text(json.dumps({"pid":os.getpid()}))\n'
|
|
'while not Path(sys.argv[2]).exists(): time.sleep(.01)\n'
|
|
'raise SystemExit(1)\n'
|
|
)
|
|
(guard / 'sitecustomize.py').write_text(
|
|
'import subprocess\n'
|
|
'class SyntheticPopen(subprocess.Popen):\n'
|
|
' def __init__(self, command, **kwargs):\n'
|
|
" if command[0] == '/usr/bin/bwrap':\n"
|
|
' command = ' + repr([sys.executable, '-c', job_code, str(marker), str(release)]) + '\n'
|
|
' super().__init__(command, **kwargs)\n'
|
|
'subprocess.Popen = SyntheticPopen\n'
|
|
)
|
|
launcher_code = '''
|
|
import json,os,signal,subprocess,sys
|
|
from pathlib import Path
|
|
import obliteratus.mcp_execution as execution
|
|
import obliteratus.run_archive as archive_module
|
|
from obliteratus.mcp_policy import McpPolicy
|
|
execution.sandbox_identity=lambda:(1,2,3,4)
|
|
root=Path(sys.argv[1])
|
|
tools=execution.ExecutionTools(McpPolicy(archive_root=root/'archive',allowed_roots=(root/'model',),execution_enabled=True))
|
|
plan=tools.plan_run(execution.ModelInput('local',str(root/'model')),'basic',str(root/'archive'),execution.RunResources(1000,runtime_seconds=60))
|
|
(root/'plan.json').write_text(json.dumps({'run_id':plan.run_id}))
|
|
def crash_spawn(command,**kwargs):
|
|
kwargs['env']['PYTHONPATH']=str(root/'guard')+os.pathsep+sys.argv[2]
|
|
process=subprocess.Popen(command,**kwargs)
|
|
(root/'supervisor.json').write_text(json.dumps({'pid':process.pid}))
|
|
os.kill(os.getpid(),signal.SIGKILL)
|
|
archive_module._spawn_mcp_worker=crash_spawn
|
|
tools.launch_run(plan.token)
|
|
'''
|
|
supervisor = job = None
|
|
try:
|
|
launcher = subprocess.run([sys.executable, '-c', launcher_code, str(tmp_path), str(ROOT)],
|
|
cwd=ROOT, capture_output=True, text=True, timeout=20)
|
|
assert launcher.returncode == -signal.SIGKILL, launcher.stderr
|
|
supervisor = json.loads((tmp_path / 'supervisor.json').read_text())['pid']
|
|
run_id = json.loads((tmp_path / 'plan.json').read_text())['run_id']
|
|
job = wait_for(lambda: json.loads(marker.read_text()))['pid']
|
|
yield archive, model, run_id, supervisor, job
|
|
finally:
|
|
if job is None and marker.exists():
|
|
job = json.loads(marker.read_text())['pid']
|
|
if supervisor is None and (tmp_path / 'supervisor.json').exists():
|
|
supervisor = json.loads((tmp_path / 'supervisor.json').read_text())['pid']
|
|
for pid in (job, supervisor):
|
|
if pid:
|
|
with contextlib.suppress(ProcessLookupError):
|
|
os.killpg(pid, signal.SIGKILL)
|
|
|
|
|
|
def test_launcher_death_before_worker_save_preserves_live_admission(tmp_path, monkeypatch):
|
|
with crashed_launcher(tmp_path) as (archive, model, run_id, supervisor, job):
|
|
with open_directory(archive / run_id) as fd, locked(fd):
|
|
value = load(fd, run_id)
|
|
assert value['status'] == 'running'
|
|
assert value['worker']['pid'] == supervisor
|
|
assert worker_matches(fd, value) is True
|
|
value['launch_deadline'] = 0
|
|
atomic_json(fd, 'manifest.json', value)
|
|
assert recover(archive, run_id)['status'] == 'running'
|
|
os.kill(job, 0)
|
|
monkeypatch.setattr('obliteratus.mcp_execution.sandbox_identity', lambda: (1, 2, 3, 4))
|
|
policy = McpPolicy(archive_root=archive, allowed_roots=(model,), execution_enabled=True)
|
|
reconnect = ExecutionTools(policy)
|
|
plan = reconnect.plan_run(ModelInput('local', str(model)), 'basic', str(archive), RunResources(1000))
|
|
with pytest.raises(ValueError, match='concurrency'):
|
|
reconnect.launch_run(plan.token)
|
|
assert recover(archive, run_id)['worker']['pid'] == supervisor
|
|
|
|
|
|
def test_cancel_writer_death_after_save_is_retried_safely(tmp_path):
|
|
with crashed_launcher(tmp_path) as (archive, _model, run_id, supervisor, _job):
|
|
os.kill(supervisor, signal.SIGSTOP)
|
|
code = '''
|
|
import os,signal,sys
|
|
from pathlib import Path
|
|
import obliteratus.mcp_worker as worker
|
|
original=worker.save
|
|
def crash_after_save(fd,value,event):
|
|
original(fd,value,event)
|
|
if event=='cancellation_requested': os.kill(os.getpid(),signal.SIGKILL)
|
|
worker.save=crash_after_save
|
|
worker.cancel_guarded(Path(sys.argv[1]),sys.argv[2])
|
|
'''
|
|
writer = subprocess.run([sys.executable, '-c', code, str(archive), run_id],
|
|
cwd=ROOT, capture_output=True, text=True, timeout=15)
|
|
assert writer.returncode == -signal.SIGKILL, writer.stderr
|
|
for _ in range(2):
|
|
assert cancel_guarded(archive, run_id)['status'] == 'cancelling'
|
|
os.kill(supervisor, signal.SIGCONT)
|
|
wait_for(lambda: recover(archive, run_id)['status'] == 'cancelled')
|
|
assert cancel_guarded(archive, run_id)['status'] == 'cancelled'
|
|
|
|
|
|
def test_supervisor_consumes_cancel_without_signal_and_preserves_terminal_state(tmp_path):
|
|
with crashed_launcher(tmp_path) as (archive, _model, run_id, supervisor, _job):
|
|
with open_directory(archive / run_id) as fd, locked(fd):
|
|
value = load(fd, run_id)
|
|
value['status'] = 'cancelling'
|
|
value['phase'] = 'cancellation_requested'
|
|
atomic_json(fd, 'manifest.json', value)
|
|
wait_for(lambda: recover(archive, run_id)['status'] == 'cancelled')
|
|
assert recover(archive, run_id)['worker']['pid'] == supervisor
|
|
|
|
|
|
def test_terminal_recovery_is_never_overwritten_by_late_supervisor(tmp_path):
|
|
with crashed_launcher(tmp_path) as (archive, _model, run_id, supervisor, _job):
|
|
with open_directory(archive / run_id) as fd, locked(fd):
|
|
value = load(fd, run_id)
|
|
value['status'], value['phase'] = 'failed', 'committed_terminal'
|
|
atomic_json(fd, 'manifest.json', value)
|
|
wait_for(lambda: not Path(f'/proc/{supervisor}/stat').exists()
|
|
or Path(f'/proc/{supervisor}/stat').read_text().rsplit(')', 1)[1].split()[0] == 'Z')
|
|
assert recover(archive, run_id)['phase'] == 'committed_terminal'
|
|
assert recover(archive, run_id)['status'] == 'failed'
|
|
|
|
|
|
def test_forged_identity_cannot_signal_a_real_unrelated_process(tmp_path):
|
|
with crashed_launcher(tmp_path) as (archive, _model, run_id, supervisor, _job):
|
|
with subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'],
|
|
start_new_session=True) as unrelated:
|
|
try:
|
|
from obliteratus.run_archive import _process_start_ticks
|
|
with open_directory(archive / run_id) as fd, locked(fd):
|
|
value = load(fd, run_id)
|
|
value['worker'] = {'pid': unrelated.pid, 'uid': os.getuid(),
|
|
'start_ticks': _process_start_ticks(unrelated.pid)}
|
|
atomic_json(fd, 'manifest.json', value)
|
|
assert cancel_guarded(archive, run_id)['status'] == 'failed'
|
|
assert unrelated.poll() is None
|
|
finally:
|
|
unrelated.kill()
|
|
unrelated.wait(timeout=5)
|