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
7 changes: 6 additions & 1 deletion loopx/control_plane/collaboration/delegation_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import heapq
from typing import TYPE_CHECKING

from ..effect_runtime import EffectRuntimeRemoteError, effect_runtime_result
from ..effect_runtime import (
EffectRuntimeRemoteError,
effect_runtime_request_scope,
effect_runtime_result,
)
from .inbox import _read
from .peers import _goal, require_operation_id
from ..content_digest import BARE_SHA256_PATTERN
Expand All @@ -20,6 +24,7 @@
DELEGATION_RECORD_SIDECAR_SUFFIXES = (DELEGATION_STOP_RECEIPT_SUFFIX, DELEGATION_HOST_PROCESS_SUFFIX)


@effect_runtime_request_scope()
def read_delegation_inventory(service: Delegations, *, limit: int = 20,
cursor: str | None = None) -> dict:
query = effect_runtime_result("collaboration.delegation.inventory_query",
Expand Down
7 changes: 6 additions & 1 deletion loopx/control_plane/goals/goal_amendment_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@
from ...file_lock import exclusive_file_lock
from ...history import load_index, load_registry
from ...runtime import validate_goal_id_path_segment
from ..effect_runtime import EffectRuntimeRejected, effect_runtime_result
from ..effect_runtime import (
EffectRuntimeRejected,
effect_runtime_request_scope,
effect_runtime_result,
)
from ..runtime.time import chronology_key
from ..status.autonomous_replan_projection import (
autonomous_replan_obligation_from_runs,
Expand Down Expand Up @@ -144,6 +148,7 @@ def read_goal_amendment_proposal_journal(
)


@effect_runtime_request_scope()
def admit_goal_amendment_proposal(
*,
proposal: Mapping[str, Any],
Expand Down
59 changes: 49 additions & 10 deletions tests/capabilities/test_research_composition_gate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Real CLI admission, successor and writeback paths with synthetic evidence."""
from __future__ import annotations

from contextlib import redirect_stderr, redirect_stdout
import io
import json
import os
import shlex
Expand All @@ -10,6 +12,8 @@

import pytest

from loopx.cli import main as cli_main
from loopx.control_plane import effect_runtime
from loopx.capabilities.explore.research_evidence import append_research_observation
from loopx.capabilities.explore.result_log import (
append_explore_result_event, build_explore_edge_event, build_explore_node_event, explore_result_log_path,
Expand All @@ -23,6 +27,8 @@
from tests.control_plane.canonical_authority_fixture import initialize_canonical_source_fixture, isolate_sqlite_runtime
from tests.control_plane.test_quota_settlement_cli import _run_generated_cli

REPO = Path(__file__).resolve().parents[2]

GOAL = "research-gate-fixture"
AGENT = "fixture-agent"

Expand All @@ -41,7 +47,8 @@ def observation(node: str, target: str | None = None) -> dict:

@pytest.fixture(params=["file", "sqlite"])
def fixture(tmp_path: Path, request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch):
isolate_sqlite_runtime(tmp_path, monkeypatch)
if request.param == "sqlite":
isolate_sqlite_runtime(tmp_path, monkeypatch)
monkeypatch.setenv("LOOPX_USAGE_PING", "0")
state = tmp_path / "ACTIVE_GOAL_STATE.md"
state.write_text('---\nstatus: active\nowner_mode: goal\nobjective: "Test the explicit research boundary."\n'
Expand All @@ -68,19 +75,51 @@ def fixture(tmp_path: Path, request: pytest.FixtureRequest, monkeypatch: pytest.
append_explore_result_event(log, build_explore_edge_event(goal_id=GOAL, from_node="joint", to_node=name, edge_type="depends_on"))
initialize_canonical_source_fixture(runtime, GOAL, state_path=state, provider=request.param)

def cli(*args: str, success: bool = True) -> dict:
result = subprocess.run([sys.executable, "-m", "loopx.cli", "--format", "json", "--registry", str(registry),
"--runtime-root", str(runtime), *args], capture_output=True, text=True, check=False,
cwd=Path(__file__).resolve().parents[2])
assert (result.returncode == 0) is success, result.stdout + result.stderr
def cli(*args: str, success: bool = True, via_subprocess: bool = False) -> dict:
argv = [
"--format", "json", "--registry", str(registry), "--runtime-root", str(runtime),
*(str(arg) for arg in args),
]
if via_subprocess:
result = subprocess.run(
[sys.executable, "-m", "loopx.cli", *argv],
capture_output=True, text=True, check=False, cwd=REPO,
)
assert (result.returncode == 0) is success, result.stdout + result.stderr
stdout_text = result.stdout
else:
stdout_buf = io.StringIO()
stderr_buf = io.StringIO()
prev_cwd = os.getcwd()
try:
os.chdir(REPO)
with redirect_stdout(stdout_buf), redirect_stderr(stderr_buf):
try:
code = cli_main(argv)
except SystemExit as exc:
if exc.code is None:
code = 0
elif isinstance(exc.code, int):
code = exc.code
else:
stderr_buf.write(str(exc.code))
code = 1
finally:
os.chdir(prev_cwd)
stdout_text = stdout_buf.getvalue()
assert (code == 0) is success, stdout_text + stderr_buf.getvalue()
if args[:2] == ("quota", "should-run"):
return QuotaToolContext.from_output(result.stdout).packet
return json.loads(result.stdout)
return QuotaToolContext.from_output(stdout_text).packet
return json.loads(stdout_text)

try:
yield cli, log, runtime, registry, state
finally:
restart_effect_runtime(wait_for_exit=True)
if request.param == "sqlite":
scoped_dir = effect_runtime._runtime_dir()
if any(scoped_dir.glob("runtime-*.json")):
restart_effect_runtime(wait_for_exit=True)
effect_runtime.close_stale_cleanup_sweeps(runtime_dir=scoped_dir)


def activate(cli) -> None:
Expand All @@ -102,7 +141,7 @@ def test_real_guard_and_successor_share_the_current_gap(fixture) -> None:
cli, log, runtime, registry, state = fixture
old = cli("quota", "should-run", "--goal-id", GOAL, "--agent-id", AGENT, "--turn-instance-id", "fixture-off")
assert old.get("effective_action") != "autonomous_replan_required"
assert "research_execution_frontier" not in cli("explore", "summary", "--goal-id", GOAL)
assert "research_execution_frontier" not in cli("explore", "summary", "--goal-id", GOAL, via_subprocess=True)
off_status = cli("status", "--goal-id", GOAL, "--agent-id", AGENT)
assert all("bounded_research_frontier" not in item.get("project_asset", {})
for item in off_status["attention_queue"]["items"])
Expand Down
49 changes: 49 additions & 0 deletions tests/control_plane/test_goal_amendment_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,3 +1229,52 @@ def test_retention_does_not_advance_the_derived_head(tmp_path: Path) -> None:
assert second["admission"] == "admitted"
assert second["admission_facts"] == ["base_source_basis_unverifiable"]
assert _canonical_tree_snapshot(paths) == before_tree


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

paths = _write_fixture(tmp_path, provider="file")
proposal = _proposal(paths)
failing_proposal = _proposal(paths, {"proposal_id": "gap_stage2_scoped_fail"})

fingerprint_calls = 0
original_fp = effect_runtime._runtime_fingerprint

def counting_fingerprint() -> str:
nonlocal fingerprint_calls
fingerprint_calls += 1
return original_fp()

rpc_calls = 0
original_req = effect_runtime.effect_runtime_request

def counting_req(method: str, params: dict, **kwargs):
nonlocal rpc_calls
rpc_calls += 1
return original_req(method, params, **kwargs)

monkeypatch.setattr(effect_runtime, "_runtime_fingerprint", counting_fingerprint)
monkeypatch.setattr(effect_runtime, "effect_runtime_request", counting_req)

record = admit_goal_amendment_proposal(
proposal=proposal,
project=paths["project"],
)
assert record["admission"] == "admitted"
assert rpc_calls >= 2
assert fingerprint_calls == 1
assert effect_runtime._REQUEST_RUNTIME_REVISION.get() is None

def failing_req(*_args, **_kwargs):
raise RuntimeError("synthetic proposal admission failure")

monkeypatch.setattr(effect_runtime, "effect_runtime_request", failing_req)
with pytest.raises(RuntimeError, match="synthetic proposal admission failure"):
admit_goal_amendment_proposal(
proposal=failing_proposal,
project=paths["project"],
)
assert effect_runtime._REQUEST_RUNTIME_REVISION.get() is None
Loading
Loading