3"""Source-only loader proofs for image-supervisor runtime mutations."""
5from __future__
import annotations
10from pathlib
import Path
12import hil_convergence_safety_runtime_loader_harness
as loader_harness
13import hil_convergence_safety_runtime_mutations
as runtime_mutations
14from hil_convergence_safety_runtime_mutations
import (
28 main_path: Path, process_path: Path, cases_path: Path
29) -> subprocess.CompletedProcess[bytes]:
30 """Load each private API, then require the cases globals to reject re-exec."""
31 return loader_harness.run(
32 main_path, process_path, cases_path, runtime_mutations.RESIDUE_TIMEOUT_SECONDS
36def _one_shot_case(root: Path, sources: SourceBundle) -> tuple[bool, frozenset[str]]:
37 """Require base refusal and prove deleting consumption makes the negative fire."""
38 supervisor, process_source, cases = sources
39 main_path, process_path, cases_path = _write_sources(root, supervisor, process_source, cases)
40 baseline = frozenset(path.name
for path
in root.iterdir())
41 base = _one_shot_result(main_path, process_path, cases_path)
42 source_guard =
'globals().get("_RA8_SUPERVISOR_CASES_VERSION")'
43 if cases.count(source_guard) != 1:
44 message =
"supervisor cases one-shot source guard is not unique"
45 raise RuntimeMutationError(message)
46 mutant_main, mutant_process, mutant_cases = _write_sources(
50 cases.replace(source_guard,
"CASES_LOAD_VERSION"),
52 mutation = _one_shot_result(mutant_main, mutant_process, mutant_cases)
55 and base.stdout == base.stderr == b
""
56 and mutation.returncode == runtime_mutations.ONE_SHOT_MUTATION_STATUS
57 and mutation.stdout == mutation.stderr == b
""
59 return exact, baseline
62def _source_only_cases(sources: SourceBundle) -> list[tuple[str, bool]]:
63 """Prove the exact source-only diagnostic and loader sentinel."""
64 supervisor, process_source, cases = sources
65 root, identity = _create_root()
67 _main_path, _process_path, cases_path = _write_sources(
68 root, supervisor, process_source, cases
70 descriptor = os.open(cases_path, os.O_RDONLY | os.O_NOFOLLOW)
72 direct = subprocess.run(
73 (sys.executable,
"-B",
"-I",
"-S", f
"/proc/self/fd/{descriptor}"),
74 pass_fds=(descriptor,),
76 timeout=runtime_mutations.RESIDUE_TIMEOUT_SECONDS,
81 one_shot, baseline = _one_shot_case(root, sources)
83 direct.returncode == 1
84 and direct.stderr.count(b
"RuntimeError: supervisor cases module is source-only") == 1
86 sentinel =
' "_RA8_SUPERVISOR_CASES_VERSION": 1,\n'
87 if supervisor.count(sentinel) != 1:
88 message =
"supervisor cases load sentinel is not unique"
89 raise RuntimeMutationError(message)
90 mutant_main, mutant_process, mutant_cases = _write_sources(
91 root, supervisor.replace(sentinel,
""), process_source, cases
93 names = {path.name
for path
in root.iterdir()}
94 status, clean, _stderr = _run_supervisor(
98 (
"--selftest-missing-entry", str(root), _identity_text(root)),
101 status == runtime_mutations.PUBLIC_REFUSAL_STATUS
103 and {path.name
for path
in root.iterdir()} == names
106 (
"supervisor cases source-only diagnostic is exact", source_exact),
108 "supervisor cases grant is consumed before exact re-exec refusal",
109 one_shot
and frozenset(path.name
for path
in root.iterdir()) == baseline,
111 (
"supervisor cases load sentinel removal refuses before effects", sentinel_refused),
112 (
"source-only runtime leaves no process or descriptor residue", _no_residue((root,))),
115 _remove_root(root, identity)
118def cases(inputs: dict[str, str]) -> list[tuple[str, bool]]:
119 """Run source-only, loader collision, removal, and repeat-load proofs."""
121 inputs[
"devcontainer_image_selftest_supervisor"],
122 inputs[
"devcontainer_image_selftest_process"],
123 inputs[
"devcontainer_image_selftest_supervisor_cases"],
125 with _owned_root_scope():
126 return _source_only_cases(sources)