Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions tests/control_plane/test_leased_host_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
from tests.control_plane.host_process_fixture import COUNTER_PROCESS_SOURCE


@pytest.fixture(params=["file", "sqlite"])
def canonical_execution(tmp_path, monkeypatch, request):
def _build_canonical_execution(tmp_path, monkeypatch, *, provider: str, ttl_seconds: int = 8):
isolate_sqlite_runtime(tmp_path, monkeypatch)
state, registry, runtime = tmp_path / "state.md", tmp_path / "registry.json", tmp_path / "runtime"
state.write_text("# Synthetic leased Host\n\n## Agent Todo\n")
Expand All @@ -37,8 +36,8 @@ def iso(value):
return value.isoformat().replace("+00:00", "Z")
lease = {"schema_version": "task_lease_v0", "goal_id": "host-goal", "todo_id": "todo_host",
"owner": "worker", "idempotency_key": "original-execution", "version": 1, "lease_epoch": 1,
"status": "active", "write_scopes": [], "acquire_ttl_seconds": 30,
"acquired_at": iso(now), "updated_at": iso(now), "expires_at": iso(now + timedelta(seconds=30))}
"status": "active", "write_scopes": [], "acquire_ttl_seconds": ttl_seconds,
"acquired_at": iso(now), "updated_at": iso(now), "expires_at": iso(now + timedelta(seconds=ttl_seconds))}
projection = build_todo_runtime_shadow_projection(goal_id="host-goal", handoff_mode="hard_lease",
leases=[lease], todos=[{"schema_version": "todo_item_v0", "todo_id": "todo_host", "role": "agent",
"status": "open", "done": False, "archive_state": "active", "text": "Execute synthetic work",
Expand All @@ -50,20 +49,35 @@ def iso(value):
projection["todo_read_model"] = {"schema_version": TODO_DOMAIN_READ_RECORD_SCHEMA_VERSION,
"contract_fields": list(TODO_DOMAIN_RECORD_FIELDS), "todo_count": 1,
"records_sha256": hashlib.sha256(canonical_bytes(projection["todos"])).hexdigest()}
initialize_canonical_authority(runtime, "host-goal", projection, state_path=state, provider=request.param)
initialize_canonical_authority(runtime, "host-goal", projection, state_path=state, provider=provider)
cli = [sys.executable, "-m", "loopx.cli", "--registry", str(registry), "--format", "json"]
selected = ["--goal-id", "host-goal", "--todo-id", "todo_host"]
context = {"lease": lease, "ttl_seconds": 30,
context = {"lease": lease, "ttl_seconds": ttl_seconds,
"renew_argv": [*cli, "task-lease", "renew", *selected, "--owner", "worker", "--idempotency-key", "original-execution"],
"read_argv": [*cli, "task-lease", "inspect", *selected]}
def command(*arguments):
result = subprocess.run([*cli, *arguments], capture_output=True, text=True, timeout=30, check=True)
return json.loads(result.stdout)
yield context, command, selected
return context, command, selected


def _shutdown_effect_runtime():
subprocess.run([sys.executable, "-c", "from loopx.control_plane.effect_runtime import effect_runtime_result;effect_runtime_result('runtime.shutdown',{},retry_safe=False)"],
capture_output=True, text=True, timeout=30, check=True)


@pytest.fixture
def canonical_execution(tmp_path, monkeypatch):
yield _build_canonical_execution(tmp_path, monkeypatch, provider="sqlite", ttl_seconds=8)
_shutdown_effect_runtime()


@pytest.fixture(params=["file", "sqlite"])
def canonical_execution_by_provider(tmp_path, monkeypatch, request):
yield _build_canonical_execution(tmp_path, monkeypatch, provider=request.param, ttl_seconds=8)
_shutdown_effect_runtime()


@pytest.mark.skipif(os.name == "nt", reason="POSIX owned process-group qualification")
@pytest.mark.parametrize("revoke", [False, True])
def test_long_ttl_checks_authority_without_rewriting_lease(canonical_execution, tmp_path, revoke):
Expand Down Expand Up @@ -119,7 +133,7 @@ def test_real_renewal_faults_keep_original_deadline_and_identity(canonical_execu
if fault == "rejected":
context["renew_argv"] = [sys.executable, "-c", "import json;print(json.dumps({'ok':False}))"]
elif fault == "hung":
context["renew_argv"] = [sys.executable, "-c", "import time;time.sleep(60)"]
context["renew_argv"] = [sys.executable, "-c", "import time;time.sleep(20)"]
else:
marker = tmp_path / "lost-reply"
trace = tmp_path / "lease-commands.jsonl"
Expand All @@ -136,7 +150,7 @@ def record(event,**values):
except ValueError: reply={}
record('returned',returncode=result.returncode,reply=reply)
if phase=='renew' and hold_until:
time.sleep(max(0,hold_until-time.time())+2)
time.sleep(max(0,hold_until-time.time())+1.5)
if phase=='read' or marker.exists(): print(result.stdout,end='')
else: marker.touch();record('reply_dropped')
sys.exit(result.returncode)
Expand All @@ -146,7 +160,7 @@ def record(event,**values):
for field, phase in (("renew_argv", "renew"), ("read_argv", "read")):
context[field] = [sys.executable, "-c", relay, str(marker), str(trace), phase,
str(held_deadline), *context[field]]
observed = run_host_process([sys.executable, "-c", "import time;time.sleep(35);print('finished')"],
observed = run_host_process([sys.executable, "-c", "import time;time.sleep(10);print('finished')"],
project=tmp_path, input_text="", timeout_seconds=None, delegated_lease=context)
evidence = {"original": context["lease"], "observed": observed,
"trace": trace.read_text() if fault in {"lost_reply", "late_reply"} and trace.exists() else ""}
Expand Down Expand Up @@ -214,8 +228,8 @@ def test_leased_supervisor_records_its_own_group_beside_the_owners_record(canoni


@pytest.mark.skipif(os.name == "nt", reason="POSIX owned process-group qualification")
def test_released_initial_proof_explains_refusal_before_host_launch(canonical_execution, tmp_path):
context, command, selected = canonical_execution
def test_released_initial_proof_explains_refusal_before_host_launch(canonical_execution_by_provider, tmp_path):
context, command, selected = canonical_execution_by_provider
command("task-lease", "release", *selected, "--owner", "worker",
"--idempotency-key", "original-execution", "--expected-version", "1")
marker = tmp_path / "must-not-launch"
Expand Down Expand Up @@ -264,7 +278,7 @@ def test_returned_host_uses_renewed_deadline_during_final_proof(canonical_execut
print('finished')
"""
observed = run_host_process([sys.executable, "-c", host, str(tmp_path)], project=tmp_path,
input_text="", timeout_seconds=45, delegated_lease=context)
input_text="", timeout_seconds=25, delegated_lease=context)
renewal = json.loads((tmp_path / "renewed").read_text())
evidence = {"observed": observed, "renewal": renewal, "original": context["lease"]}
record_property("final_proof", json.dumps(evidence))
Expand Down
Loading
Loading