|
3 | 3 | import json |
4 | 4 | import os |
5 | 5 | from pathlib import Path |
| 6 | +import shlex |
6 | 7 | import subprocess |
7 | 8 | import sys |
8 | 9 |
|
9 | 10 | import pytest |
10 | 11 |
|
11 | 12 | from loopx import paths |
| 13 | +from loopx.cli_runtime import HOST_GLOBAL_REGISTRY_SELECTOR |
12 | 14 | from test_codex_context_lifecycle import project, setup |
13 | 15 |
|
14 | 16 |
|
@@ -93,3 +95,111 @@ def test_implicit_source_and_authority_commands_preserve_route_conflict(tmp_path |
93 | 95 | result = run_cli("loopx.entrypoint", tmp_path, *command) |
94 | 96 | assert result.returncode != 0 |
95 | 97 | assert "Both default LoopX runtime roots contain state" in result.stdout + result.stderr |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("provider", ["file", "sqlite"]) |
| 101 | +def test_replan_stages_read_original_authority_capture_without_registry( |
| 102 | + tmp_path, monkeypatch, conflicting_defaults, provider, |
| 103 | +): |
| 104 | + from canonical_authority_fixture import isolate_sqlite_runtime |
| 105 | + from loopx.control_plane.effect_runtime import restart_effect_runtime |
| 106 | + from test_replan_progress_native_cli import ( |
| 107 | + AGENT, GOAL, TODO, TURN, call_progress_cli, make_progress_cli_fixture, |
| 108 | + ) |
| 109 | + |
| 110 | + isolate_sqlite_runtime(tmp_path, monkeypatch) |
| 111 | + fixture = make_progress_cli_fixture(tmp_path, provider, "advanced") |
| 112 | + registry = json.loads(fixture["registry"].read_bytes()) |
| 113 | + registry["goals"][0]["explore_graph"] = {"enabled": True} |
| 114 | + fixture["registry"].write_text(json.dumps(registry)) |
| 115 | + document = tmp_path / "outcome.json" |
| 116 | + document.write_text(json.dumps({"outcome_id": "original-artifact", "owner_agent_id": AGENT, |
| 117 | + "objective": "Validate the original artifact", "todo_ids": [TODO], |
| 118 | + "requirements": [{"id": "retained-stop", "description": "Stop before publication."}], |
| 119 | + "source": {"ref": "message:original-request", "summary": "Original accepted boundary"}})) |
| 120 | + try: |
| 121 | + revision = call_progress_cli(fixture, "goal-acceptance", "inspect", "--goal-id", GOAL)["provider_revision"] |
| 122 | + call_progress_cli(fixture, "goal-acceptance", "refine", "--goal-id", GOAL, "--agent-id", AGENT, |
| 123 | + "--document", str(document), "--expected-provider-revision", revision, |
| 124 | + "--operation-id", "original-outcome", "--execute") |
| 125 | + guard = call_progress_cli(fixture, "quota", "should-run", "--codex-app", |
| 126 | + "--goal-id", GOAL, "--agent-id", AGENT, "--turn-instance-id", TURN, |
| 127 | + "--todo-id", TODO, "--decision-output-dir", str(tmp_path / "capture")) |
| 128 | + capture = tmp_path / "capture" / "decision.json" |
| 129 | + source = json.loads(capture.read_bytes()) |
| 130 | + identity = source["heartbeat_receipt"]["settlement_identity"] |
| 131 | + assert (identity["goal_id"], identity["agent_id"], identity["turn_instance_id"]) == (GOAL, AGENT, TURN) |
| 132 | + missing = tmp_path / "unrelated-missing-registry.json" |
| 133 | + |
| 134 | + def snapshot(): |
| 135 | + return {str(p.relative_to(tmp_path)): p.read_bytes() |
| 136 | + for root in (fixture["project"], fixture["runtime"], capture.parent) |
| 137 | + for p in root.rglob("*") if p.is_file()} | {"registry": fixture["registry"].read_bytes()} |
| 138 | + |
| 139 | + before = snapshot() |
| 140 | + for module in ("loopx.entrypoint", "loopx.cli"): |
| 141 | + for route in ((), ("--registry", str(missing)), ("--registry", HOST_GLOBAL_REGISTRY_SELECTOR)): |
| 142 | + def read(command): |
| 143 | + argv = shlex.split(command) |
| 144 | + result = run_cli(module, fixture["project"], *route, *argv[argv.index("quota"):]) |
| 145 | + assert result.returncode == 0, result.stdout + result.stderr |
| 146 | + value = json.loads(result.stdout) |
| 147 | + assert value["ok"] and (value["goal_id"], value["agent_id"]) == (GOAL, AGENT) |
| 148 | + assert value["obligation_id"] == source["replan_action_packet"]["obligation_id"] |
| 149 | + return value |
| 150 | + |
| 151 | + review = read(guard["replan_action_packet"]["review"]) |
| 152 | + writer = read(review["next"]["writeback"]) |
| 153 | + assert writer["context_id"] == review["context_id"] |
| 154 | + assert writer["source_hash"] == review["source_hash"] |
| 155 | + assert writer["writeback"]["settlement_plan"]["identity"] == identity |
| 156 | + index = review["acceptance_index"] |
| 157 | + assert len(index["outcomes"]) == 1 |
| 158 | + acceptance = read(index["read_action_template"].replace("{outcome_ref}", index["outcomes"][0]["outcome_ref"])) |
| 159 | + assert acceptance["outcome"]["requirements"][0]["description"] == "Stop before publication." |
| 160 | + explore = read(guard["replan_action_packet"]["review"].replace("--replan-stage review", "--replan-stage explore")) |
| 161 | + assert explore["explore_basis"] == source["autonomous_replan_obligation"]["replan_context"].get("explore_basis") |
| 162 | + assert snapshot() == before, "captured readers cannot admit, settle, repair routes or rewrite authority" |
| 163 | + assert not missing.exists() |
| 164 | + finally: |
| 165 | + restart_effect_runtime(wait_for_exit=True) |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.parametrize("module", ["loopx.entrypoint", "loopx.cli"]) |
| 169 | +def test_replan_capture_failures_belong_to_reader_not_default_route(tmp_path, conflicting_defaults, module): |
| 170 | + from loopx.control_plane.quota.turn_envelope import _turn_envelope_result |
| 171 | + from test_replan_host_context_projection import _quota_payload |
| 172 | + |
| 173 | + capture = tmp_path / "source.json" |
| 174 | + source = _quota_payload() |
| 175 | + source["replan_read_path"] = str(capture) |
| 176 | + |
| 177 | + def command(value, stage="review"): |
| 178 | + capture.write_text(json.dumps(value)) |
| 179 | + packet = _turn_envelope_result("replan_cli_view", payload=value)["replan_action_packet"] |
| 180 | + argv = shlex.split(packet[stage]) |
| 181 | + return argv[argv.index("quota"):] |
| 182 | + |
| 183 | + def rejected(argv, reason): |
| 184 | + result = run_cli(module, tmp_path, *argv) |
| 185 | + assert result.returncode == 1, result.stdout + result.stderr |
| 186 | + value = json.loads(result.stdout) |
| 187 | + assert not value["ok"] and reason in value["reason"], value |
| 188 | + |
| 189 | + argv = command(source) |
| 190 | + rejected(["quota", "replan-view"], "requires --decision-file and --source-hash") |
| 191 | + rejected(["quota", "replan-view", "--decision-file", str(capture)], "requires --decision-file and --source-hash") |
| 192 | + rejected(["quota", "replan-view", "--source-hash", "sha256:" + "0" * 64], "requires --decision-file and --source-hash") |
| 193 | + wrong = list(argv) |
| 194 | + wrong[wrong.index("--source-hash") + 1] = "sha256:" + "0" * 64 |
| 195 | + rejected(wrong, "source hash") |
| 196 | + capture.write_text("{") |
| 197 | + rejected(argv, "Expecting") |
| 198 | + capture.unlink() |
| 199 | + rejected(argv, "No such file") |
| 200 | + source["autonomous_replan_obligation"]["replan_context"]["obligation_id"] = "different-obligation" |
| 201 | + rejected(command(source), "matching decision context") |
| 202 | + source = _quota_payload() |
| 203 | + source["replan_read_path"] = str(capture) |
| 204 | + source["interaction_contract"]["schema_version"] = "unsupported-contract-version" |
| 205 | + rejected(command(source, "writeback"), "schema_version") |
0 commit comments