Skip to content
Open
Show file tree
Hide file tree
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
23 changes: 18 additions & 5 deletions tests/control_plane/test_turn_todoless_settlement.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)


def _fixture(tmp_path, monkeypatch, provider, *, replan=True):
def _fixture(tmp_path, monkeypatch, provider, *, replan=True, capture_dir=None):
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry = _write_fixture(tmp_path)
if replan:
Expand All @@ -47,9 +47,10 @@ def _fixture(tmp_path, monkeypatch, provider, *, replan=True):
assert code == 0, listed
original = listed["todos"]
turn = "empty-frontier-planning-turn"
capture_args = ("--decision-output-dir", str(capture_dir)) if capture_dir is not None else ()
code, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app",
"--goal-id", GOAL_ID, "--agent-id", AGENT_ID, "--turn-instance-id", turn,
cwd=project)
*capture_args, cwd=project)
assert code == 0 and guard["decision"] == ("autonomous_replan_required" if replan else "run"), guard
assert guard.get("selected_todo") is None
return project, runtime, registry, turn, guard, original
Expand Down Expand Up @@ -183,9 +184,21 @@ def test_bound_todoless_planning_executes_and_settles_once(tmp_path, monkeypatch
@pytest.mark.parametrize("provider", ["file", "sqlite"])
@pytest.mark.parametrize("captured", [False, True])
def test_no_todo_requires_the_original_signed_scoped_binding(tmp_path, monkeypatch, provider, captured):
_project, runtime, _registry, turn, guard, _original = _fixture(tmp_path, monkeypatch, provider)
if captured:
guard["replan_read_path"] = str(tmp_path / "original-decision.json")
capture_dir = tmp_path / "decision-capture" if captured else None
_project, runtime, _registry, turn, guard, _original = _fixture(
tmp_path, monkeypatch, provider, capture_dir=capture_dir,
)
if capture_dir is not None:
capture_path = capture_dir / "decision.json"
assert guard["replan_read_path"] == str(capture_path)
assert json.loads(capture_path.read_bytes()) == guard

plan = build_loopx_turn_plan(guard, host="codex-cli", execution_mode="isolated-headless")
assert plan["route"]["would_invoke_host"] is True
assert plan["work_contract"] == guard
assert plan["transaction"]["settlement_plan"]["identity"] == guard["heartbeat_receipt"]["settlement_identity"]

# The historical adapter must preserve the same binding and reject mismatches.
envelope = build_turn_envelope(guard)
binding = (lambda value: value["writeback"]["settlement_identity"]) if captured else (
lambda value: value["writeback"]["settlement_plan"]["identity"])
Expand Down
47 changes: 29 additions & 18 deletions tests/control_plane/test_work_context_host_delivery.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
"""Authored requirements must reach the Host, not just agree after projection."""
import copy
import json
import os
import shlex
import subprocess
from urllib.parse import quote

import pytest

from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection
from loopx.control_plane.effect_runtime import EffectRuntimeRejected
from loopx.control_plane.quota.turn_envelope import 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
from loopx.control_plane.turn_driver.host_candidate import extract_turn_authority, render_prompt
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.test_quota_settlement_cli import (
AGENT_ID, GOAL_ID, TODO_ID, TURN_ID, _configure_read_only_todo, _configure_selectable_alternative,
_heartbeat_receipt_count, _run_cli, _spend_run_count, _write_fixture,
Expand Down Expand Up @@ -101,15 +105,33 @@ def test_authored_requirements_survive_capture_projection_and_host(tmp_path, mon
'--todo-id', TODO_ID, '--turn-instance-id', TURN_ID, '--codex-app',
'--available-capability', 'shell', '--available-capability', 'filesystem_read']
capture = tmp_path / 'original-context'
code, envelope = _run_cli(registry, runtime, *args, '--turn-envelope',
'--decision-output-dir', str(capture))
assert code == 0, envelope
code, emitted = _run_cli(registry, runtime, *args, '--turn-envelope',
'--decision-output-dir', str(capture))
assert code == 0, emitted
saved = capture / 'decision.json'
original_bytes = saved.read_bytes()
full = json.loads(original_bytes)
assert emitted == full
source_context = full['interaction_contract']['agent_channel']['work_context']
assert source_context['complete'] is True
assert source_context['instruction'] # Non-vacuous instruction transport.

plan = build_loopx_turn_plan(full, host='codex-cli', execution_mode='isolated-headless')
request = build_loopx_turn_host_request(plan)
model = json.loads(_prompt(request).split('Turn request:\n', 1)[1])
model_context = assert_host_task_context(model, full)
model_channel = model_context['interaction_contract']['agent_channel']
model_task = next(source['content']['todo'] for source in model_channel['work_context']['sources']
if source['source'] == 'selected_todo')
assert model_task.get('text', model_context['selected_todo']['text']) == source_task['text']
assert model_task['note'] == note
model_goal = next(source['content'] for source in model_channel['work_context']['sources']
if source['source'] == 'goal_state')
assert model_goal['full_source'] is True and goal in model_goal['text']
assert all(item['source'] != 'goal_state' for item in model_channel['required_reads'])

# Keep the historical reader exercised with an explicit legacy projection.
envelope = build_turn_envelope(full, captured_decision_path=str(saved))
authority = extract_turn_authority({'turn_envelope': envelope})
# The raw heartbeat and the structured Host have different effect owners,
# but ordinary execution must not lose notification, bypass or scheduler
Expand Down Expand Up @@ -139,16 +161,8 @@ def test_authored_requirements_survive_capture_projection_and_host(tmp_path, mon
assert task_detail['note'] == note
delivered_task = task_detail.get('text', authority['selected_todo']['text'])
assert delivered_task == source_task['text']
# This fixture has an unregistered Goal narrative: pending read must remain
# explicit even when all available inline sources were loaded successfully.
goal_read = next(item for item in authority['required_reads'] if item['source'] == 'goal_state')
assert goal_read['ordering'] == 'before_work'
if os.name != 'nt':
readback = subprocess.run(shlex.split(goal_read['command']), capture_output=True,
text=True, check=True)
assert goal in readback.stdout
else:
assert goal in state.read_text(encoding='utf-8')
assert goal in source_by_kind['goal_state']['text']
assert all(item['source'] != 'goal_state' for item in authority['required_reads'])
# The actual Host prompt must contain the independent source requirements,
# not merely match an action signature computed by the same projector.
prompt = render_prompt(authority)
Expand All @@ -173,9 +187,6 @@ def test_authored_requirements_survive_capture_projection_and_host(tmp_path, mon
extract_turn_authority({'turn_envelope': altered})
# A matching v0 digest does not authorize interpreting a future wire version.
# Version declarations are outside the historical action digest document.
from loopx.control_plane.effect_runtime import EffectRuntimeRejected
from loopx.control_plane.turn_driver.codex_cli import _prompt
from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
for path in (('schema_version',), ('action_signature', 'schema_version'),
('action_signature', 'coverage')):
for unsupported in ('future_required_semantics', None):
Expand Down
Loading