diff --git a/loopx/control_plane/turn_driver/codex_execution_preparation.py b/loopx/control_plane/turn_driver/codex_execution_preparation.py index 8648adaefb..2594a73fcb 100644 --- a/loopx/control_plane/turn_driver/codex_execution_preparation.py +++ b/loopx/control_plane/turn_driver/codex_execution_preparation.py @@ -11,6 +11,7 @@ import subprocess from collections.abc import Callable, Mapping, Sequence from pathlib import Path +from time import monotonic from typing import Any, TYPE_CHECKING if TYPE_CHECKING: @@ -503,6 +504,7 @@ def execute_codex_execution_preparation( if request.get("claim_args") != expected_claim: raise ValueError("invalid Codex execution-preparation claim arguments") invoke = runner or _default_runner + deadline = monotonic() + timeout claimed = _run_json( expected_claim, registry_path=registry_path, @@ -524,12 +526,16 @@ def execute_codex_execution_preparation( "--todo-id", todo_id, "--turn-instance-id", turn_instance_id, "--decision-output-dir", str(capture_directory), "--prepared-capture-reference", ] + # Reserve time for readiness and give it the time the claim left unused. + remaining = deadline - monotonic() + if remaining <= 0: + raise TimeoutError("Native execution preparation deadline elapsed before the ready check") after = _run_json( guard, registry_path=registry_path, runtime_root=runtime_root, cwd=workspace, - timeout=timeout / 2, + timeout=remaining, runner=invoke, ) preparation = after.get("execution_preparation") diff --git a/tests/control_plane/test_codex_execution_preparation.py b/tests/control_plane/test_codex_execution_preparation.py index 9eac0acc58..83202c5c10 100644 --- a/tests/control_plane/test_codex_execution_preparation.py +++ b/tests/control_plane/test_codex_execution_preparation.py @@ -6,6 +6,7 @@ import pytest +from loopx.control_plane.turn_driver import codex_execution_preparation from loopx.control_plane.turn_driver.codex_execution_preparation import ( codex_execution_preparation_context, compact_execution_preparation_request, @@ -90,13 +91,17 @@ def _bound_request(tmp_path: Path) -> dict: @pytest.mark.parametrize("scenario", ["codex_app_heartbeat", "codex_app_ssh_goal"]) -def test_executor_claims_then_proves_same_turn_ready(tmp_path: Path, scenario: str) -> None: +def test_executor_claims_then_proves_same_turn_ready(tmp_path: Path, monkeypatch, scenario: str) -> None: request = _bound_request(tmp_path) capture, native = tmp_path / "ready", tmp_path / "native" calls: list[list[str]] = [] + timeouts = [] + clock = iter((100.0, 101.5)) + monkeypatch.setattr(codex_execution_preparation, "monotonic", lambda: next(clock)) def runner(command, *, cwd, timeout): calls.append(list(command)) + timeouts.append(timeout) if "should-run" not in command: return _completed({"ok": True, "claimed": True, "claim_operation_id": CLAIM}) @@ -133,6 +138,7 @@ def runner(command, *, cwd, timeout): assert receipt["capture_sha256"] == READY_DIGEST assert receipt["capture_path"] == str(capture / "decision.json") assert len(calls) == 2 + assert timeouts == [4.0, 6.5] for call in calls: assert call[call.index("--registry") + 1] == ( str((tmp_path / "loopx-project-a" / ".loopx" / "registry.json").resolve()) @@ -149,6 +155,21 @@ def runner(command, *, cwd, timeout): assert "do not repeat" in context and '"state":"ready"' in context +def test_executor_skips_ready_check_after_preparation_deadline(tmp_path: Path, monkeypatch) -> None: + clock = iter((100.0, 108.0)) + monkeypatch.setattr(codex_execution_preparation, "monotonic", lambda: next(clock)) + + def runner(command, *, cwd, timeout): + assert "claim" in command and timeout == 4.0 + return _completed({"ok": True, "claim_operation_id": CLAIM}) + + with pytest.raises(TimeoutError, match="deadline elapsed"): + execute_codex_execution_preparation( + _bound_request(tmp_path), cwd=tmp_path.resolve(), scenario="codex_app_heartbeat", + capture_directory=tmp_path / "ready", context_directory=tmp_path / "native", runner=runner, + ) + + @pytest.mark.parametrize("changed", ["goal", "agent", "todo", "turn"]) def test_executor_rejects_changed_todo_or_turn(tmp_path: Path, changed: str) -> None: request = _bound_request(tmp_path)