Skip to content
Merged
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
7 changes: 6 additions & 1 deletion loopx/capabilities/explore/research_evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from pathlib import Path
from typing import Any

from ...control_plane.effect_runtime import EffectRuntimeRemoteError, effect_runtime_result
from ...control_plane.effect_runtime import (
EffectRuntimeRemoteError,
effect_runtime_request_scope,
effect_runtime_result,
)
from ...control_plane.runtime.public_safety import validate_public_safe_value
from ...control_plane.work_items.progress_observation import normalize_progress_observation
from ...control_plane.work_items.progress_result import PROGRESS_OBSERVATION_SCHEMA_VERSION
Expand Down Expand Up @@ -88,6 +92,7 @@ def validate_research_append(
})


@effect_runtime_request_scope()
def append_research_observation(
path: Path, *, goal_id: str, observation: Mapping[str, Any], agent_id: str | None = None,
registry_path: Path | None = None, runtime_root: Path | None = None,
Expand Down
3 changes: 2 additions & 1 deletion loopx/control_plane/turn_driver/settlement.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
require_turn_provider_step_kind,
settlement_result_payload,
)
from ..effect_runtime import effect_runtime_result
from ..effect_runtime import effect_runtime_request_scope, effect_runtime_result
from ..goals.source_session_turn_effects import (
SOURCE_TURN_EFFECT_HOLD_SCHEMA_VERSION,
)
Expand Down Expand Up @@ -434,6 +434,7 @@ def _read_prepared_effect(
}


@effect_runtime_request_scope()
def execute_turn_driver_settlement(
transaction_plan: Mapping[str, Any],
*,
Expand Down
53 changes: 53 additions & 0 deletions tests/capabilities/test_explore_research_evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,56 @@ def test_cli_execution_requires_actor_and_preserves_lineage_in_readback(tmp_path
joint = next(row for row in readback["nodes"] if row["node_id"] == "joint")
assert joint["research_observation"] == result["observation"]
assert readback["research_frontier"]["observed_count"] == 1


def test_append_research_observation_shares_one_effect_runtime_scope(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
) -> None:
from loopx.control_plane import effect_runtime

ok_dir = tmp_path / "ok"
fail_dir = tmp_path / "fail"
ok_dir.mkdir()
fail_dir.mkdir()
registry, runtime, path, raw = execution_fixture(ok_dir)
fail_registry, fail_runtime, fail_path, fail_raw = execution_fixture(fail_dir)
assert effect_runtime.effect_runtime_result("runtime.ping", {})["ready"] is True

real_fp = effect_runtime._runtime_fingerprint
real_req_fp = effect_runtime._runtime_fingerprint_for_request
counts = {"fp": 0, "rpc": 0}
fail_on_rpc: int | None = None

def spy_fp() -> str:
counts["fp"] += 1
return real_fp()

def spy_req_fp() -> str:
counts["rpc"] += 1
if fail_on_rpc is not None and counts["rpc"] == fail_on_rpc:
raise RuntimeError("injected rpc failure")
return real_req_fp()

monkeypatch.setattr(effect_runtime, "_runtime_fingerprint", spy_fp)
monkeypatch.setattr(effect_runtime, "_runtime_fingerprint_for_request", spy_req_fp)

result = append_research_observation(
path, goal_id=GOAL, observation=raw, agent_id="fixture-agent",
registry_path=registry, runtime_root=runtime,
)
assert result["written"] is True
assert counts["rpc"] >= 3
assert counts["fp"] == 1
assert effect_runtime._REQUEST_RUNTIME_REVISION.get() is None

counts["fp"] = 0
counts["rpc"] = 0
fail_on_rpc = 2
with pytest.raises(RuntimeError, match="injected rpc failure"):
append_research_observation(
fail_path, goal_id=GOAL, observation=fail_raw, agent_id="fixture-agent",
registry_path=fail_registry, runtime_root=fail_runtime,
)
assert counts["rpc"] == 2
assert counts["fp"] == 1
assert effect_runtime._REQUEST_RUNTIME_REVISION.get() is None
61 changes: 61 additions & 0 deletions tests/control_plane/test_settlement_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,64 @@ def test_decode_settlement_result_rejects_invalid_union_states(
) -> None:
with pytest.raises(RuntimeError, match=message):
decode_settlement_result(payload)


def test_turn_driver_settlement_shares_one_effect_runtime_scope(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from loopx.control_plane import effect_runtime

assert effect_runtime.effect_runtime_result("runtime.ping", {})["ready"] is True

real_fp = effect_runtime._runtime_fingerprint
real_req_fp = effect_runtime._runtime_fingerprint_for_request
counts = {"fp": 0, "rpc": 0}

def spy_fp() -> str:
counts["fp"] += 1
return real_fp()

def spy_req_fp() -> str:
counts["rpc"] += 1
return real_req_fp()

monkeypatch.setattr(effect_runtime, "_runtime_fingerprint", spy_fp)
monkeypatch.setattr(effect_runtime, "_runtime_fingerprint_for_request", spy_req_fp)

result = execute_turn_driver_settlement(
TRANSACTION,
transaction_phases=PHASES,
completed_phases=PHASES[:3],
writeback_payload=None,
quota_spend_payload=None,
writeback=lambda: {"ok": True, "appended": True},
spend=lambda: {"ok": True, "appended": True},
checkpoint=lambda _step, _payload, _phases: None,
prepare=lambda _step, _effect_ref: None,
)
assert result.failure is None
assert counts["rpc"] >= 5
assert counts["fp"] == 1
assert effect_runtime._REQUEST_RUNTIME_REVISION.get() is None

counts["fp"] = 0
counts["rpc"] = 0

def failing_writeback() -> dict[str, object]:
raise RuntimeError("injected writeback failure")

with pytest.raises(RuntimeError, match="injected writeback failure"):
execute_turn_driver_settlement(
TRANSACTION,
transaction_phases=PHASES,
completed_phases=PHASES[:3],
writeback_payload=None,
quota_spend_payload=None,
writeback=failing_writeback,
spend=lambda: {"ok": True, "appended": True},
checkpoint=lambda _step, _payload, _phases: None,
prepare=lambda _step, _effect_ref: None,
)
assert counts["rpc"] >= 1
assert counts["fp"] == 1
assert effect_runtime._REQUEST_RUNTIME_REVISION.get() is None
Loading
Loading