|
| 1 | +"""Native launch preserves the admitted binding through real CLI and stores.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import json |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from host_process_fixture import write_python_host_fixture |
| 11 | +from test_replan_successor_guard_reentry import AGENT, GOAL, _fixture |
| 12 | + |
| 13 | + |
| 14 | +def _host(root: Path, *, material: bool) -> Path: |
| 15 | + # This is a deterministic process transport, not a model or task evaluator. |
| 16 | + # The material arm writes a distinct artifact checked by the outer validator; |
| 17 | + # the replan arm ends the iteration and earns no writeback or debit. |
| 18 | + return write_python_host_fixture(root / "native-host", f'''#!/usr/bin/env python3 |
| 19 | +import json, pathlib, sys |
| 20 | +request = json.loads(sys.stdin.read().split("Turn request:\\n", 1)[1]) |
| 21 | +with pathlib.Path("host-requests.jsonl").open("a") as stream: |
| 22 | + stream.write(json.dumps(request) + "\\n") |
| 23 | +material = {material!r} |
| 24 | +if material: |
| 25 | + pathlib.Path("checked-source.txt").write_text("independently checked source\\n") |
| 26 | +print(json.dumps({{"type": "thread.started", "thread_id": "fixture-launch-session"}}), flush=True) |
| 27 | +result = {{ |
| 28 | + "schema_version": "loopx_turn_result_v0", "turn_key": request["turn_key"], |
| 29 | + "result_kind": "validated_progress" if material else "iteration_failed", |
| 30 | + "completed_phases": ["host_execute", "typed_result"], |
| 31 | + "classification": "source_checked" if material else "", |
| 32 | + "recommended_action": "Retain the original source criteria." if material else "", |
| 33 | + "next_action": "Continue independent source validation." if material else "", |
| 34 | + "delivery_batch_scale": "single_surface" if material else "", |
| 35 | + "delivery_outcome": "outcome_progress" if material else "", |
| 36 | + "path_delta_mode": "unchanged" if material else "", "agent_vision_json": "", |
| 37 | + "vision_unchanged_reason": "Original source criteria still apply." if material else "", |
| 38 | + "progress_observation_json": "", |
| 39 | + "summary": "Source checked." if material else "Source remains unverified.", |
| 40 | +}} |
| 41 | +if material: |
| 42 | + result.update(work_evidence=["artifact:checked-source"], |
| 43 | + validation_evidence=["check:independent-source-validator"]) |
| 44 | +args = sys.argv[1:] |
| 45 | +pathlib.Path(args[args.index("--output-last-message") + 1]).write_text(json.dumps(result)) |
| 46 | +''') |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize("provider", ["file", "sqlite"]) |
| 50 | +@pytest.mark.parametrize("binding", ["todo", "autonomous_replan"]) |
| 51 | +def test_native_cli_uses_original_binding_and_replays_without_new_effects( |
| 52 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, binding: str, |
| 53 | +) -> None: |
| 54 | + call, runtime, _ = _fixture(tmp_path, monkeypatch, provider, future_monitor=False) |
| 55 | + todo_id = None |
| 56 | + declared_text = None |
| 57 | + if binding == "todo": |
| 58 | + todo_id = call("todo", "add", "--goal-id", GOAL, "--role", "agent", |
| 59 | + "--claimed-by", AGENT, "--text", "Independently check the original source.", |
| 60 | + "--task-class", "advancement_task", "--action-kind", "validate")["todo_id"] |
| 61 | + declared_text = call("todo", "list", "--goal-id", GOAL, |
| 62 | + "--todo-id", todo_id)["todo"]["text"] |
| 63 | + project = tmp_path / "project" |
| 64 | + executable = _host(tmp_path, material=binding == "todo") |
| 65 | + decision = ["--goal-id", GOAL, "--agent-id", AGENT, "--host", "codex-cli", |
| 66 | + "--execution-mode", "isolated-headless", "--scheduler-owner", "outer_controller", |
| 67 | + "--iteration-context", "fresh", "--session-scope", "agent"] |
| 68 | + if todo_id: |
| 69 | + decision += ["--todo-id", todo_id] |
| 70 | + common = [*decision, "--project", str(project), "--codex-bin", str(executable), |
| 71 | + "--no-global-sync"] |
| 72 | + if todo_id: |
| 73 | + common += ["--validation-command-json", json.dumps([ |
| 74 | + sys.executable, "-c", "from pathlib import Path; " |
| 75 | + "assert Path('checked-source.txt').read_text() == 'independently checked source\\n'", |
| 76 | + ])] |
| 77 | + turn = "native-launch-binding" |
| 78 | + preview = call("turn", "plan", *decision, "--turn-instance-id", turn, |
| 79 | + "--include-transaction-detail") |
| 80 | + assert preview["route"]["kind"] == ("ready_for_host" if todo_id else "replan_required") |
| 81 | + obligation_id = None |
| 82 | + subject = todo_id |
| 83 | + if not todo_id: |
| 84 | + envelope = preview["turn_envelope"] |
| 85 | + obligation_id = envelope["replan_action_packet"]["obligation_id"] |
| 86 | + assert obligation_id.startswith("replan-") |
| 87 | + subject = f"autonomous_replan:{obligation_id}" |
| 88 | + expected_effect = f"{GOAL}:{AGENT}:{subject}:{turn}" |
| 89 | + assert not (project / "host-requests.jsonl").exists() |
| 90 | + |
| 91 | + result = call("turn", "run-once", *common, "--turn-instance-id", turn, "--execute") |
| 92 | + assert result["effects"]["host_invoked"] is True, result |
| 93 | + assert result["receipt"]["settlement_effect_id"] == expected_effect |
| 94 | + lineage = result["receipt"]["lineage"] |
| 95 | + assert lineage["todo_id"] == (todo_id or "") |
| 96 | + assert lineage.get("replan_obligation_id") == obligation_id |
| 97 | + assert result["effects"]["state_written"] is bool(todo_id) |
| 98 | + assert result["quota_slot_spend_count"] == int(bool(todo_id)) |
| 99 | + if todo_id: |
| 100 | + assert result["status"] == "committed" and result["validation"]["ok"] is True |
| 101 | + current = call("todo", "list", "--goal-id", GOAL, "--todo-id", todo_id) |
| 102 | + assert current["todo"]["text"] == declared_text |
| 103 | + else: |
| 104 | + assert result["status"] == "stopped" and result["result_kind"] == "iteration_failed" |
| 105 | + assert not (project / "checked-source.txt").exists() |
| 106 | + |
| 107 | + requests = (project / "host-requests.jsonl").read_bytes() |
| 108 | + sent = json.loads(requests) |
| 109 | + assert sent["turn_key"] == result["receipt"]["turn_key"] |
| 110 | + captures = {path: path.read_bytes() for path in runtime.rglob("decisions/*.json")} |
| 111 | + assert captures |
| 112 | + replay_args = list(common) |
| 113 | + if todo_id: |
| 114 | + rejected = call("turn", "run-once", *common, "--resume-turn-key", sent["turn_key"], |
| 115 | + "--execute", expected_code=1) |
| 116 | + assert "cannot retarget" in rejected["error"] and not any(rejected["effects"].values()) |
| 117 | + index = replay_args.index("--todo-id") |
| 118 | + del replay_args[index:index + 2] |
| 119 | + replay = call("turn", "run-once", *replay_args, "--resume-turn-key", sent["turn_key"], |
| 120 | + "--execute") |
| 121 | + assert replay["replayed"] is True |
| 122 | + assert not any(replay["effects"].values()) |
| 123 | + assert (project / "host-requests.jsonl").read_bytes() == requests |
| 124 | + assert all(path.read_bytes() == original for path, original in captures.items()) |
0 commit comments