Skip to content
Merged
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
164 changes: 87 additions & 77 deletions tests/control_plane/test_turn_envelope_capability_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@
import pytest

from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection
from loopx.control_plane.effect_runtime import EffectRuntimeRemoteError
from loopx.control_plane.quota.turn_envelope import (
assert_complete_turn_decision,
bind_turn_decision_capture,
build_turn_envelope,
)
from loopx.control_plane.turn_driver.codex_cli import _prompt
from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
from loopx.control_plane.turn_driver.executor import (
build_loopx_turn_host_request,
validate_loopx_turn_host_result,
)
from tests.control_plane.canonical_authority_fixture import (
initialize_canonical_authority, isolate_sqlite_runtime, read_fixture_todos,
)
from tests.control_plane.host_process_fixture import assert_host_task_context
from tests.control_plane.reward_memory_host_fixture import enable_live_memory
from tests.control_plane.test_quota_settlement_cli import (
AGENT_ID, GOAL_ID, TODO_ID, _bind_selected_replan_guard, _run_cli, _write_fixture,
Expand Down Expand Up @@ -56,31 +68,29 @@ def test_real_captured_guard_preserves_required_and_missing_capabilities(
"--turn-envelope", "--decision-output-dir", str(capture), cwd=project)
assert code == (0 if network_available else 1), envelope
full = json.loads((capture / "decision.json").read_text())
# This ordinary quota producer retains its recorded v0 contract; the
# current Turn producer is qualified independently in the cutover tests.
assert envelope["schema_version"] == "loopx_turn_envelope_v0"
assert envelope == full and full["schema_version"] == "loopx_turn_envelope_v1"
gate = full["capability_gate"]
assert "network" in gate["required"]
assert ("network" in gate["missing"]) is (not network_available)
assert full["interaction_contract"]["agent_channel"]["delivery_allowed"] is network_available
compact = envelope["boundary"]["capability_gate"]
assert compact["required"] == gate["required"]
assert compact["missing"] == gate["missing"]
assert compact["action"] == gate["action"]
assert envelope["action"]["delivery_allowed"] is network_available
assert envelope["action_signature"]["matches"] is True
# The source remains one full observation, not another admission evaluation.
from loopx.control_plane.quota.turn_envelope import (
build_turn_envelope, turn_envelope_action_signature_document,
)
# Both readers must preserve the same admitted or refused facts without
# re-entering quota or writing another receipt.
before = {p: p.read_bytes() for p in runtime.rglob("*") if p.is_file()}
assert build_turn_envelope(full)["boundary"]["capability_gate"] == compact
assert {p: p.read_bytes() for p in runtime.rglob("*") if p.is_file()} == before
changed = json.loads(json.dumps(envelope))
changed["boundary"]["capability_gate"]["missing"] = ["different-capability"]
assert turn_envelope_action_signature_document(changed) != turn_envelope_action_signature_document(envelope)
assert_complete_turn_decision(full)
legacy = build_turn_envelope(full)
compact = legacy["boundary"]["capability_gate"]
for field in ("required", "missing", "action"):
assert compact[field] == gate[field]
assert legacy["action"]["delivery_allowed"] is network_available
assert legacy["action_signature"]["matches"] is True

changed = deepcopy(full)
changed["capability_gate"]["missing"] = ["different-capability"]
with pytest.raises(EffectRuntimeRemoteError, match="content integrity mismatch"):
assert_complete_turn_decision(changed)
if mode in {"off", "stale"}:
assert "reward_memory" not in envelope["boundary"].get("capabilities", {})
assert "reward_memory" not in legacy["boundary"].get("capabilities", {})
assert {p: p.read_bytes() for p in runtime.rglob("*") if p.is_file()} == before


@pytest.mark.parametrize("provider", ["file", "sqlite"])
Expand Down Expand Up @@ -132,44 +142,25 @@ def test_captured_vision_contract_preserves_closeout_and_original_observation(
"--available-capability", "filesystem_read", "--turn-envelope",
"--decision-output-dir", str(capture), cwd=project)
assert code == 0, envelope
full = json.loads((capture / "decision.json").read_text())
assert envelope["schema_version"] == "loopx_turn_envelope_v0"
saved = capture / "decision.json"
original = saved.read_bytes()
full = json.loads(original)
assert envelope == full and full["schema_version"] == "loopx_turn_envelope_v1"
audit = full["vision_continuation_audit"]
assert audit["required"] and audit["required_before_closeout"]
assert audit["vision_gap_judge"]["done"] is False
assert full["goal_frontier_projection"]["replan_required"] is replan
projected = envelope["contract_capsule"]["vision_continuation_audit"]
before = {p: p.read_bytes() for p in runtime.rglob("*") if p.is_file()}
legacy = build_turn_envelope(full, captured_decision_path=str(saved))
projected = legacy["contract_capsule"]["vision_continuation_audit"]
for field in ("required_before_closeout", "recommended_action", "vision_gap_judge",
"authoritative_evidence_kinds", "not_satisfied_by"):
assert projected[field] == audit[field]
assert envelope["action_signature"]["matches"] is True
assert "reward_memory" not in envelope["boundary"].get("capabilities", {})

from loopx.control_plane.quota.turn_envelope import (
build_turn_envelope, turn_envelope_action_signature_document,
)
assert legacy["action_signature"]["matches"] is True
assert "reward_memory" not in legacy["boundary"].get("capabilities", {})

# Consume the saved source without reentering quota or appending a receipt.
before = {p: p.read_bytes() for p in runtime.rglob("*") if p.is_file()}
replay = build_turn_envelope(full, captured_decision_path=str(capture / "decision.json"))
assert replay["contract_capsule"]["vision_continuation_audit"] == projected
assert {p: p.read_bytes() for p in runtime.rglob("*") if p.is_file()} == before
changed = json.loads(json.dumps(envelope))
changed["contract_capsule"]["vision_continuation_audit"]["required_before_closeout"].pop()
assert turn_envelope_action_signature_document(changed) != turn_envelope_action_signature_document(envelope)
from loopx.control_plane.turn_driver.host_candidate import extract_turn_authority
from loopx.control_plane.turn_driver.codex_cli import _prompt

assert extract_turn_authority({"turn_envelope": envelope})["primary_action"]
assert json.dumps(projected, ensure_ascii=False, sort_keys=True, separators=(",", ":")) in _prompt(
{"turn_envelope": envelope, "turn_key": "vision-contract-turn"}
)
with pytest.raises(ValueError, match="action signature"):
extract_turn_authority({"turn_envelope": changed})

# Exercise the shared host's real stdio boundary, rather than only proving
# that the envelope and Codex's full-request prompt retain the audit.
original = (capture / "decision.json").read_bytes()
# Exercise the current Host request and DSH's stdio boundary using the
# same immutable observation the quota CLI saved.
runner = tmp_path / "runner.py"
runner.write_text(
"import json\nfrom pathlib import Path\n"
Expand All @@ -190,13 +181,23 @@ def test_captured_vision_contract_preserves_closeout_and_original_observation(
)
command = [sys.executable, "-m", "loopx.dsh_goal_mode", "--workspace", str(project),
"--dsh-home", str(tmp_path / "host-home"), "--dsh-runner", str(runner)]
plan = build_loopx_turn_plan(envelope, host="dsh", execution_mode="isolated-headless",
turn_instance_id="vision-contract-turn")
plan = build_loopx_turn_plan(full, host="dsh", execution_mode="isolated-headless",
turn_instance_id="vision-contract-turn")
assert plan["route"]["would_invoke_host"] is True
# A replan route permits planning, not ordinary delivery.
assert envelope["action"]["delivery_allowed"] is (not replan)
request = {"schema_version": "loopx_turn_host_request_v0", "turn_key": plan["transaction"]["turn_key"],
"route": "replan_required" if replan else "primary_delivery", "turn_envelope": envelope}
assert full["interaction_contract"]["agent_channel"]["delivery_allowed"] is (not replan)
assert plan["transaction"]["settlement_plan"]["identity"] == (
full["interaction_contract"]["cli_channel"]["settlement_plan"]["identity"]
)
request = build_loopx_turn_host_request(plan)
assert request["route"] == ("replan_required" if replan else "ready_for_host")
assert request["work_contract"] == full
request.update(bind_turn_decision_capture(
full, captured_decision_path=str(saved), turn_instance_id="vision-contract-turn",
turn_key=request["turn_key"],
complete_host_request={
key: request[key] for key in ("schema_version", "turn_key", "source_decision_hash")
},
))
result = subprocess.run(command, input=json.dumps(request), text=True, capture_output=True, timeout=30)
assert result.returncode == 0, result.stdout + result.stderr
host_result = json.loads(result.stdout)
Expand All @@ -207,10 +208,6 @@ def test_captured_vision_contract_preserves_closeout_and_original_observation(
assert host_result["validation_evidence"] == [
"check:profile-targeted-validation"
]
from loopx.control_plane.turn_driver.executor import (
validate_loopx_turn_host_result,
)

verdict = validate_loopx_turn_host_result(plan, host_result)
assert verdict["ok"], verdict["errors"]
assert verdict["result"]["delivery_batch_scale"] == "implementation"
Expand All @@ -219,44 +216,57 @@ def test_captured_vision_contract_preserves_closeout_and_original_observation(
assert verdict["result"]["validation_evidence"] == host_result["validation_evidence"]
prompt_path = project / "received.txt"
prompt = prompt_path.read_text()
authority = json.loads(prompt.split("Turn authority JSON:\n", 1)[1].split("\n\nWhen finished", 1)[0])
assert authority["vision_continuation_audit"] == projected
# The native work-size and outcome rules must reach the provider too;
# retaining an audit alone cannot tell it what qualifies as useful work.
assert authority["execution_profile"] == full["execution_profile"]
assert authority["vision_continuation_audit"]["vision_gap_judge"]["done"] is False
assert authority["action_signature"] == envelope["action_signature"]
assert authority["detail_ref"] == envelope["detail_ref"]
detail = authority["detail_ref"]
decoder = json.JSONDecoder()
model_input, _ = decoder.raw_decode(prompt.split("Turn request:\n", 1)[1])
codex_input, _ = decoder.raw_decode(_prompt(request).split("Turn request:\n", 1)[1])
assert model_input == codex_input
expected_source = full
if replan:
# The Host resolves the original authoring reference before hiding
# executor-owned commands from the model.
expected_source = deepcopy(full)
writer = expected_source["replan_action_packet"]["writeback_contract"]
steps = full["interaction_contract"]["cli_channel"]["settlement_plan"]["ordered_steps"]
[(index, writeback)] = [
(index, step) for index, step in enumerate(steps) if step["kind"] == "durable_writeback"
]
assert writer.pop("vision_authoring_ref") == (
f"$.interaction_contract.cli_channel.settlement_plan.ordered_steps[{index}].vision_authoring"
)
writer["vision_authoring"] = writeback["vision_authoring"]
task = assert_host_task_context(model_input, expected_source)
assert task["vision_continuation_audit"] == audit
assert task["execution_profile"] == full["execution_profile"]
assert model_input["detail_ref"] == request["detail_ref"]
detail = model_input["detail_ref"]
captured = detail["captured_decision"]
assert (captured["goal_id"], captured["agent_id"], captured["turn_instance_id"]) == (
GOAL_ID, AGENT_ID, "vision-contract-turn",
)
read = subprocess.run(shlex.split(detail["full_decision"]), capture_output=True, check=True).stdout
assert read == original
assert build_turn_envelope(json.loads(read))["action_signature"]["source_decision_hash"] == authority["action_signature"]["source_decision_hash"]
assert "writeback" not in authority
assert authority["scheduler"] == envelope["scheduler"]
assert "Do not execute its state or scheduler commands" in prompt
assert_complete_turn_decision(json.loads(read))
assert model_input["source_decision_hash"] == full["contract_binding"]["content_hash"]
assert "Do not write LoopX state" in prompt
for field, value in (
("required_before_closeout", list(reversed(projected["required_before_closeout"]))),
("vision_gap_judge", {**projected["vision_gap_judge"], "done": True}),
("not_satisfied_by", []),
):
invalid = deepcopy(request)
invalid["turn_envelope"]["contract_capsule"]["vision_continuation_audit"][field] = value
invalid["work_contract"]["vision_continuation_audit"][field] = value
refused = subprocess.run(command, input=json.dumps(invalid), text=True, capture_output=True, timeout=30)
assert refused.returncode != 0 and "action signature" in refused.stderr
assert refused.returncode != 0 and "complete Turn contract rejected" in refused.stderr
assert prompt_path.read_text() == prompt
for field, value in (
("minimum_scale", "any_operation"),
("must_include", []),
("outcome_floor", {"configured": False, "must_advance": []}),
):
invalid = deepcopy(request)
invalid["turn_envelope"]["contract_capsule"]["execution_profile"][field] = value
invalid["work_contract"]["execution_profile"][field] = value
refused = subprocess.run(command, input=json.dumps(invalid), text=True, capture_output=True, timeout=30)
assert refused.returncode != 0 and "action signature" in refused.stderr
assert refused.returncode != 0 and "complete Turn contract rejected" in refused.stderr
assert prompt_path.read_text() == prompt
assert (capture / "decision.json").read_bytes() == original
assert saved.read_bytes() == original
assert {p: p.read_bytes() for p in runtime.rglob("*") if p.is_file()} == before
Loading