Skip to content

Commit b685891

Browse files
authored
fix(context): read captured replans without default registry routing (#7703)
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
1 parent 8171933 commit b685891

2 files changed

Lines changed: 114 additions & 4 deletions

File tree

‎loopx/cli_runtime.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ def resolve_cli_registry(
173173
registry_was_configured = user_supplied_registry(raw_argv) or bool(
174174
os.environ.get("LOOPX_REGISTRY")
175175
)
176-
if args.command == "quota" and getattr(args, "quota_command", None) == "context-read":
176+
if args.command == "quota" and getattr(args, "quota_command", None) in {"context-read", "replan-view"}:
177177
from .cli_commands.context_machine import has_explicit_context_read_source
178178

179-
# The reader owns capture integrity and native-session scope. It cannot
180-
# reach Goal authority, so default registry conflicts are unrelated.
181-
if has_explicit_context_read_source(args):
179+
# Captured readers own source validation (including missing references)
180+
# and native-session scope; neither reaches live Goal authority.
181+
if args.quota_command == "replan-view" or has_explicit_context_read_source(args):
182182
return registry_path, registry_was_configured
183183
if str(args.registry) == HOST_GLOBAL_REGISTRY_SELECTOR:
184184
try:

‎tests/control_plane/test_context_capture_registry_independence.py‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import json
44
import os
55
from pathlib import Path
6+
import shlex
67
import subprocess
78
import sys
89

910
import pytest
1011

1112
from loopx import paths
13+
from loopx.cli_runtime import HOST_GLOBAL_REGISTRY_SELECTOR
1214
from test_codex_context_lifecycle import project, setup
1315

1416

@@ -93,3 +95,111 @@ def test_implicit_source_and_authority_commands_preserve_route_conflict(tmp_path
9395
result = run_cli("loopx.entrypoint", tmp_path, *command)
9496
assert result.returncode != 0
9597
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

Comments
 (0)