From 626cbb6f80cd97e3a11149c455a429db8c1fd3c0 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:17:44 +0800 Subject: [PATCH 01/14] fix(runtime): honor explicit Goal runtime with conflicting defaults Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../runtime/runtime_projection_route.py | 16 +++-- tests/test_chat_todo_detail.py | 67 +++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/loopx/control_plane/runtime/runtime_projection_route.py b/loopx/control_plane/runtime/runtime_projection_route.py index e0fe8eefe9..85876c2897 100644 --- a/loopx/control_plane/runtime/runtime_projection_route.py +++ b/loopx/control_plane/runtime/runtime_projection_route.py @@ -104,19 +104,21 @@ def resolve_goal_source_runtime_route( ``source_registry``, goal-scoped capabilities must use that registry's runtime rather than treating the shared projection as the source of truth. An explicit runtime override remains authoritative for diagnostics and - recovery commands. + recovery commands. It also supplies the invoked runtime for an unbound + registry. """ invoked_registry = registry_path.expanduser().resolve() registry_payload = registry if registry is not None else load_registry( invoked_registry, source_snapshots=source_snapshots, ) - invoked_runtime = _registry_runtime_root( - registry_payload, - registry_path=invoked_registry, - ) if runtime_root_override: runtime_root = Path(runtime_root_override).expanduser().resolve() + invoked_runtime = ( + _registry_runtime_root(registry_payload, registry_path=invoked_registry) + if registry_payload.get("common_runtime_root") + else runtime_root + ) return _goal_source_route_packet( status="explicit_override", declaration_source="cli.runtime_root_override", @@ -127,6 +129,10 @@ def resolve_goal_source_runtime_route( source_runtime=runtime_root, ) + invoked_runtime = _registry_runtime_root( + registry_payload, + registry_path=invoked_registry, + ) registry_is_global = bool(registry_payload.get("registry_role") == "global-local") or _same_path( invoked_registry, global_registry_path(invoked_runtime), diff --git a/tests/test_chat_todo_detail.py b/tests/test_chat_todo_detail.py index 14dafc2da5..dc97fb2707 100644 --- a/tests/test_chat_todo_detail.py +++ b/tests/test_chat_todo_detail.py @@ -251,6 +251,73 @@ def cli(*args, expected_code=0): worker.join() +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_explicit_task_read_with_conflicting_defaults(tmp_path, monkeypatch, capsys, provider): + from loopx import paths + from loopx.cli import main + from tests.control_plane.canonical_authority_fixture import ( + isolate_sqlite_runtime, + promoted_create_fixture, + ) + + isolate_sqlite_runtime(tmp_path, monkeypatch) + defaults = (tmp_path / "current", tmp_path / "legacy") + for root in defaults: + root.mkdir() + (root / "machine.json").write_text("{}", encoding="utf-8") + monkeypatch.setattr(paths, "DEFAULT_RUNTIME_ROOT", defaults[0]) + monkeypatch.setattr(paths, "LEGACY_RUNTIME_ROOT", defaults[1]) + + registry, runtime, state = promoted_create_fixture(tmp_path, provider=provider) + registration = json.loads(registry.read_text(encoding="utf-8")) + del registration["common_runtime_root"] + registry.write_text(json.dumps(registration), encoding="utf-8") + initial_state = state.read_bytes() + + def cli(*args, explicit=True): + override = ["--runtime-root", str(runtime)] if explicit else [] + code = main(["--registry", str(registry), *override, "--format", "json", + "todo", *args, "--goal-id", "goal-a"]) + return code, json.loads(capsys.readouterr().out) + + body = "[P1] Read the committed task when both machine roots contain state." + code, created = cli("add", "--role", "agent", "--text", body) + assert code == 0 and created["ok"], created + todo_id = created["todo_id"] + assert created["projection_delivery"] == "pending" + assert created["projection_outbox"]["retry_business_mutation"] is False + canonical = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") + + code, refused = cli("list", "--todo-id", todo_id, explicit=False) + assert code == 1 and not refused["ok"] + assert "Both default LoopX runtime roots contain state" in refused["error"] + + code, listed = cli("list", "--todo-id", todo_id) + assert code == 0 and listed["todo"]["text"] == body, listed + assert listed["authority_read"]["provider_revision"] == canonical["provider_revision"] + + with ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler) as server: + server.registry_path, server.runtime_root_override, server.verbose = registry, str(runtime), False + worker = threading.Thread(target=server.serve_forever, daemon=True) + worker.start() + url = (f"http://127.0.0.1:{server.server_port}/api/chat/todo/detail" + f"?goal_id=goal-a&todo_id={todo_id}") + try: + with urlopen(url, timeout=30) as response: + detail = json.load(response) + assert detail["todo_id"] == todo_id and detail["text"] == body + assert detail["review_basis"]["provider_revision"] == canonical["provider_revision"] + finally: + server.shutdown() + worker.join() + + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == canonical + assert state.read_bytes() == initial_state + for root in defaults: + assert list(root.iterdir()) == [root / "machine.json"] + assert (root / "machine.json").read_text(encoding="utf-8") == "{}" + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_source_bound_requirements_use_real_canonical_writer(tmp_path, monkeypatch, provider): monkeypatch.syspath_prepend(str(Path(__file__).parent / "control_plane")) From 89d4dbe32836e14fbb45706d638da8dbca6a99b2 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Thu, 12 Nov 2026 10:35:35 +0800 Subject: [PATCH 02/14] fix(chat): use the Task read route for Todo actions Use the serving coordination runtime for reviewed Todo and Monitor actions when no registered runtime owns the route. Keep the same provider for preview, apply and receipt replay, and retain registry precedence when a source is registered. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/chat/monitor_actions.py | 3 + loopx/chat/todo_actions.py | 11 +++- .../test_native_user_completion_update.py | 63 ++++++++++++++++--- 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/loopx/chat/monitor_actions.py b/loopx/chat/monitor_actions.py index c73ca78d02..dffa8057b2 100644 --- a/loopx/chat/monitor_actions.py +++ b/loopx/chat/monitor_actions.py @@ -20,9 +20,11 @@ def _run_monitor_update( goal_id = str(parameters["goal_id"]) operation = str(parameters["operation"]) + runtime_root_arg = self._todo_runtime_root_arg() if operation == "stop": result = complete_goal_todo( registry_path=self.registry_path, + runtime_root_arg=runtime_root_arg, goal_id=goal_id, todo_id=str(parameters["todo_id"]), evidence="Owner stopped the continuous monitor from typed Chat action.", @@ -36,6 +38,7 @@ def _run_monitor_update( else: kwargs: dict[str, Any] = { "registry_path": self.registry_path, + "runtime_root_arg": runtime_root_arg, "goal_id": goal_id, "todo_id": str(parameters["todo_id"]), "agent_id": str(parameters["agent_id"]), diff --git a/loopx/chat/todo_actions.py b/loopx/chat/todo_actions.py index 3d805121d3..ba46785d75 100644 --- a/loopx/chat/todo_actions.py +++ b/loopx/chat/todo_actions.py @@ -24,6 +24,11 @@ def _decision_readback(result: dict[str, Any]) -> dict[str, Any]: class ChatTodoActionMixin: """Keep Todo preview/apply parity separate from general orchestration.""" + def _todo_runtime_root_arg(self) -> str | None: + from .status_api import goal_read_runtime_root + + return goal_read_runtime_root(self) + def _run_todo_update( self, parameters: dict[str, Any], *, dry_run: bool, basis: dict[str, Any] | None = None, operation_id: str | None = None, @@ -37,10 +42,12 @@ def _run_todo_update( proof = {key: parameters[key] for key in ( "task_lease_idempotency_key", "task_lease_expected_version", ) if key in parameters} if operation == "complete" else {} + runtime_root_arg = self._todo_runtime_root_arg() if operation == "complete" and (basis is None or basis.get("schema_version") == "loopx_chat_canonical_terminal_basis_v0"): return complete_goal_todo( registry_path=self.registry_path, + runtime_root_arg=runtime_root_arg, goal_id=goal_id, todo_id=str(parameters["todo_id"]), note=parameters.get("note"), @@ -61,6 +68,7 @@ def _run_todo_update( status = "deferred" return update_goal_todo( registry_path=self.registry_path, + runtime_root_arg=runtime_root_arg, goal_id=goal_id, todo_id=str(parameters["todo_id"]), text=parameters.get("text"), @@ -97,6 +105,7 @@ def _run_gate_resolve( # the outcome and what it does to the dependent; the App only records it. return complete_goal_todo( registry_path=self.registry_path, + runtime_root_arg=self._todo_runtime_root_arg(), goal_id=str(parameters["goal_id"]), todo_id=str(parameters["todo_id"]), role="user", @@ -141,7 +150,7 @@ def _canonical_update_basis( ) -> dict[str, Any] | None: registry_sha256 = self._registry_fingerprint() authority = read_canonical_todos_if_promoted( - runtime_root=effective_runtime_root(self.registry_path, None), + runtime_root=effective_runtime_root(self.registry_path, self._todo_runtime_root_arg()), goal_id=goal_id, include_leases=True, ) if self._registry_fingerprint() != registry_sha256: diff --git a/tests/control_plane/test_native_user_completion_update.py b/tests/control_plane/test_native_user_completion_update.py index 0f050091d0..75b5a59675 100644 --- a/tests/control_plane/test_native_user_completion_update.py +++ b/tests/control_plane/test_native_user_completion_update.py @@ -69,6 +69,17 @@ def validation_command(source: str) -> str: return shlex.join([sys.executable, "-c", source]) +def chat_runtime_routes( + registry: Path, *, host_fallback: bool, +) -> tuple[str, str | None]: + if not host_fallback: + return str(registry.parent / "chat-history"), None + registration = json.loads(registry.read_text(encoding="utf-8")) + runtime = str(registration.pop("common_runtime_root")) + registry.write_text(json.dumps(registration), encoding="utf-8") + return runtime, runtime + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_validation_failure_preview_and_replay(tmp_path: Path, provider: str) -> None: marker = tmp_path / "calls" @@ -128,14 +139,26 @@ def test_invalid_input_and_permission_do_not_run_validation(tmp_path: Path, prov @pytest.mark.parametrize("provider", ["file", "sqlite"]) -@pytest.mark.parametrize("failure_boundary", ["display", "action_receipt"]) -def test_reviewed_chat_completion_recovers_projection(tmp_path: Path, provider: str, monkeypatch: pytest.MonkeyPatch, failure_boundary: str) -> None: +@pytest.mark.parametrize( + ("failure_boundary", "host_fallback"), + [("display", False), ("action_receipt", False), ("action_receipt", True)], + ids=["registered-display", "registered-receipt", "host-fallback-receipt"], +) +def test_reviewed_chat_completion_recovers_projection( + tmp_path: Path, provider: str, monkeypatch: pytest.MonkeyPatch, + failure_boundary: str, host_fallback: bool, +) -> None: from loopx.chat_action_store import ChatActionStore from loopx.chat_actions import ChatActionService from loopx.control_plane.todos import provider_projection registry, state, todo_id = fixture(tmp_path, provider) - service = ChatActionService(store=ChatActionStore(tmp_path / "actions"), registry_path=registry) + chat_root, todo_root = chat_runtime_routes(registry, host_fallback=host_fallback) + route = {"registry_path": registry, "goal_id": "goal-a", "runtime_root_arg": todo_root} + service = ChatActionService( + store=ChatActionStore(tmp_path / "actions"), registry_path=registry, + runtime_root_override=chat_root, + ) proposal = service.preview({"action_kind": "todo.update", "summary": "Record an observed outcome", "normalized_parameters": {"goal_id": "goal-a", "todo_id": todo_id, "agent_id": "agent-a", "operation": "complete", "note": "Reviewed observation"}, @@ -154,13 +177,16 @@ def lost_response(*args, **kwargs): patch.setattr(service.store, "apply", lost_response) with pytest.raises(ConnectionError): service.apply(proposal["proposal_id"]) - assert list_goal_todos(registry_path=registry, goal_id="goal-a")["todos"][0]["status"] == "done" + committed = list_goal_todos(**route) + assert committed["todos"][0]["status"] == "done" state.unlink() recovered = service.apply(proposal["proposal_id"])["proposal"] assert recovered["status"] == "applied" assert recovered["receipt"]["outcome"] == "todo_completed" assert state.exists() - dependent = next(row for row in list_goal_todos(registry_path=registry, goal_id="goal-a")["todos"] if row["role"] == "agent") + after = list_goal_todos(**route) + assert after["authority_read"]["provider_revision"] == committed["authority_read"]["provider_revision"] + dependent = next(row for row in after["todos"] if row["role"] == "agent") assert (dependent["status"], dependent["claimed_by"]) == ("open", "agent-a") @@ -183,7 +209,10 @@ def test_chat_user_validation_failure_never_creates_success_receipt(tmp_path: Pa @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("passed", [True, False]) -def test_packaged_chat_http_user_completion(tmp_path: Path, provider: str, passed: bool) -> None: +@pytest.mark.parametrize("host_fallback", [False, True], ids=["registered", "host-fallback"]) +def test_packaged_chat_http_user_completion( + tmp_path: Path, provider: str, passed: bool, host_fallback: bool, +) -> None: from http.client import HTTPConnection from threading import Thread from loopx.chat_action_store import ChatActionStore @@ -192,12 +221,20 @@ def test_packaged_chat_http_user_completion(tmp_path: Path, provider: str, passe registry, _state, todo_id = fixture(tmp_path, provider, validation=validation_command(f"raise SystemExit({0 if passed else 1})")) + chat_root, todo_root = chat_runtime_routes(registry, host_fallback=host_fallback) + route = {"registry_path": registry, "goal_id": "goal-a", "runtime_root_arg": todo_root} + before = list_goal_todos(**route) + dependent_id = next(row["todo_id"] for row in before["todos"] if row["role"] == "agent") store = ChatActionStore(tmp_path / "actions") server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler) server.verbose = False server.assets_dir = default_chat_assets_dir() + server.registry_path = registry + server.runtime_root_override = chat_root server.action_store = store - server.action_service = ChatActionService(store=store, registry_path=registry) + server.action_service = ChatActionService( + store=store, registry_path=registry, runtime_root_override=chat_root, + ) thread = Thread(target=server.serve_forever, daemon=True) thread.start() connection = HTTPConnection("127.0.0.1", server.server_address[1], timeout=45) @@ -206,6 +243,11 @@ def test_packaged_chat_http_user_completion(tmp_path: Path, provider: str, passe response = connection.getresponse() assert response.status == 200 assert b" Date: Thu, 12 Nov 2026 12:40:02 +0800 Subject: [PATCH 03/14] fix(chat): create work in the Goal source runtime Route reviewed Todo creation, assignment and Monitor creation through the same Goal source used for Task reads. Use the serving runtime only for an unbound project registry. A shared directory resolves its source registry before any write, and a Monitor confirmation becomes stale when that source registration changes. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/chat.py | 10 ++ loopx/chat/status_api.py | 2 +- loopx/chat/todo_actions.py | 25 ++++ loopx/chat_actions.py | 68 ++++++--- loopx/chat_server.py | 25 +--- .../test_chat_reviewed_create.py | 132 ++++++++++++++++++ 6 files changed, 223 insertions(+), 39 deletions(-) diff --git a/loopx/chat.py b/loopx/chat.py index b7dddbc829..23cb146d64 100644 --- a/loopx/chat.py +++ b/loopx/chat.py @@ -1106,6 +1106,7 @@ def _add_review_todo( *, registry_path: Path, goal_id: str, + runtime_root_arg: str | None, text: str, priority: str | None = None, dry_run: bool, @@ -1115,6 +1116,7 @@ def _add_review_todo( return add_goal_todo( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, role="agent", text=_normalize_todo_text(text), priority=priority, @@ -1134,12 +1136,14 @@ def _bound_todo_review_preview( *, registry_path: Path, goal_id: str, + runtime_root_arg: str | None, text: str, priority: str | None, ) -> dict[str, Any]: payload = _add_review_todo( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, dry_run=True, @@ -1153,6 +1157,7 @@ def _bound_todo_review_preview( return _add_review_todo( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, dry_run=True, @@ -1178,10 +1183,12 @@ def build_todo_review_preview( goal_id: str, text: str, priority: str | None = None, + runtime_root_arg: str | None = None, ) -> dict[str, Any]: payload = _bound_todo_review_preview( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, ) @@ -1197,10 +1204,12 @@ def apply_todo_review_preview( text: str, priority: str | None = None, preview_id: str, + runtime_root_arg: str | None = None, ) -> dict[str, Any]: current_preview = _bound_todo_review_preview( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, ) @@ -1216,6 +1225,7 @@ def apply_todo_review_preview( applied = _add_review_todo( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, dry_run=False, diff --git a/loopx/chat/status_api.py b/loopx/chat/status_api.py index 86e03e339b..c417063ea1 100644 --- a/loopx/chat/status_api.py +++ b/loopx/chat/status_api.py @@ -56,7 +56,7 @@ def goal_read_runtime_root(server: Any) -> str | None: """Keep registry source routing; Chat storage is only an unbound fallback.""" registry = _discovery_registry(server.registry_path) - if registry.get("common_runtime_root"): + if registry.get("common_runtime_root") or registry.get("registry_role") == "global-local": # Passing the coordinator's resolved root as an explicit override # suppresses the collector's per-Goal source_registry routing. A # global directory may contain Goals owned by different runtimes. diff --git a/loopx/chat/todo_actions.py b/loopx/chat/todo_actions.py index ba46785d75..d6d4154d9c 100644 --- a/loopx/chat/todo_actions.py +++ b/loopx/chat/todo_actions.py @@ -2,16 +2,41 @@ from __future__ import annotations +from pathlib import Path from typing import Any +from .status_api import goal_read_runtime_root +from ..chat import apply_todo_review_preview, build_todo_review_preview from ..control_plane.todos.mutation_api import complete_goal_todo, update_goal_todo from ..control_plane.coordination.local_authority import ( LocalCoordinationAuthorityUnavailable, read_canonical_todos_if_promoted, ) +from ..control_plane.runtime.runtime_projection_route import resolve_goal_source_runtime_route from ..paths import effective_runtime_root from ..control_plane.todos.provider_projection import projection_delivery_requires_ack +def todo_create_source(host: Any, goal_id: str) -> tuple[Path, str | None]: + """Use the Task read route before handing creation to the canonical writer.""" + override = goal_read_runtime_root(host) + if override is not None: + return host.registry_path, override + route = resolve_goal_source_runtime_route(registry_path=host.registry_path, goal_id=goal_id) + return Path(route["source_registry"]), None + + +def review_todo_create( + host: Any, *, goal_id: str, text: str, preview_id: str | None, +) -> dict[str, Any]: + """Route the standalone Todo review endpoint through the same source.""" + registry_path, runtime_root_arg = todo_create_source(host, goal_id) + if preview_id is None: + return build_todo_review_preview(registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text) + return apply_todo_review_preview(registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, preview_id=preview_id) + + def _decision_readback(result: dict[str, Any]) -> dict[str, Any]: """Carry the canonical outcome and dependent effect, never a local guess.""" resume = result.get("unblock_resume") diff --git a/loopx/chat_actions.py b/loopx/chat_actions.py index 0e2b1ba01d..6d0c50ad8c 100644 --- a/loopx/chat_actions.py +++ b/loopx/chat_actions.py @@ -8,7 +8,11 @@ import re from typing import Any, Mapping, Sequence -from .agent_registry import agent_profile_for_goal, registered_agent_ids_for_goal +from .agent_registry import ( + agent_profile_for_goal, + registered_agent_ids_for_goal, + registered_agent_ids_from_registry, +) from .control_plane.projects.identity import GoalCreationConflictError from .control_plane.coordination.new_goal_storage_transport import initialize_goal_storage_target from .chat import apply_todo_review_preview, build_todo_review_preview @@ -21,7 +25,7 @@ from .chat_goal_lifecycle_actions import ChatGoalLifecycleActionMixin from .chat.monitor_actions import ChatMonitorActionMixin from .chat_store import ChatSessionStore -from .chat.todo_actions import ChatTodoActionMixin +from .chat.todo_actions import ChatTodoActionMixin, todo_create_source from .control_plane.goals.acceptance_actions import GoalAcceptanceActionMixin from .control_plane.goals.goal_objective_actions import GoalObjectiveActionMixin from .configure_goal import configure_goal @@ -292,11 +296,12 @@ def project_team_plan_preview(self, preview: Mapping[str, Any]) -> dict[str, Any def _registry(self) -> dict[str, Any]: return load_registry(self.registry_path) - def _goal(self, goal_id: str) -> dict[str, Any]: + def _goal(self, goal_id: str, *, registry_path: Path | None = None) -> dict[str, Any]: + registry = self._registry() if registry_path is None else load_registry(registry_path) goal = next( ( goal - for goal in registry_goals(self._registry()) + for goal in registry_goals(registry) if str(goal.get("id") or "") == goal_id ), None, @@ -305,7 +310,7 @@ def _goal(self, goal_id: str) -> dict[str, Any]: raise ValueError("goal_id was not found in the active LoopX registry") return goal - def _registry_fingerprint(self) -> str: + def _registry_fingerprint(self, registry_path: Path | None = None) -> str: """Identity of the registry a proposal was previewed against. A fresh install has no registry until its first Goal is created. That @@ -315,13 +320,22 @@ def _registry_fingerprint(self) -> str: """ try: - content = self.registry_path.read_bytes() + content = (registry_path or self.registry_path).read_bytes() except FileNotFoundError: return "registry-absent" except OSError as exc: raise ValueError("the active LoopX registry is unavailable") from exc return hashlib.sha256(content).hexdigest() + def _monitor_create_fingerprint(self, source_registry: Path) -> str: + invoked = self._registry_fingerprint() + if source_registry == self.registry_path: + return invoked + # A directory can stay unchanged while the Goal moves to a different + # provider or changes its registered Agents in its source registry. + return _digest({"directory": invoked, "source": self._registry_fingerprint(source_registry), + "source_registry": str(source_registry)}) + def _creation_agent_eligibility( self, agent_id: str, *, project: Path | None = None ) -> dict[str, Any]: @@ -1093,15 +1107,16 @@ def _heartbeat_gate(self, parameters: dict[str, Any]) -> ProtectedActionGate: def _apply_monitor_create( self, proposal_id: str, proposal: dict[str, Any], parameters: dict[str, Any] ) -> dict[str, Any]: - current_fingerprint = self._registry_fingerprint() + goal_id = str(parameters["goal_id"]) + registry_path, runtime_root_arg = todo_create_source(self, goal_id) + current_fingerprint = self._monitor_create_fingerprint(registry_path) if current_fingerprint != proposal.get("expected_state_fingerprint"): stale = self.store.apply( proposal_id, current_state_fingerprint=current_fingerprint, receipt={} ) return {"proposal": stale, "turn": None} - goal_id = str(parameters["goal_id"]) agent_id = str(parameters["agent_id"]) - if agent_id not in registered_agent_ids_for_goal(self._goal(goal_id)): + if agent_id not in registered_agent_ids_from_registry(registry_path, goal_id): raise ValueError("monitor Agent must be registered for the Goal") stop_condition = str(parameters.get("stop_condition") or "").strip() resume_when = None @@ -1118,8 +1133,9 @@ def _apply_monitor_create( ): metadata["watch_only"] = "true" result = add_goal_todo( - registry_path=self.registry_path, + registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, role="agent", text=_monitor_text(parameters) or str(parameters["target"]), task_class="continuous_monitor", @@ -1300,15 +1316,22 @@ def preview(self, request: Mapping[str, Any]) -> dict[str, Any]: ] permission = "protected" elif action_kind == "todo.create": + registry_path, runtime_root_arg = todo_create_source(self, normalized["goal_id"]) canonical_preview = build_todo_review_preview( - registry_path=self.registry_path, + registry_path=registry_path, goal_id=normalized["goal_id"], + runtime_root_arg=runtime_root_arg, text=normalized["text"], priority=normalized.get("priority"), ) fingerprint = str(canonical_preview["preview_id"]) evidence = ["Canonical LoopX Todo dry-run validated the proposal."] permission = "durable_write" + elif action_kind == "monitor.create": + registry_path, _ = todo_create_source(self, normalized["goal_id"]) + fingerprint = self._monitor_create_fingerprint(registry_path) + evidence = ["Canonical LoopX contracts validated the bounded request shape."] + permission = "durable_write" elif action_kind == "run.correct": fingerprint = self._session_fingerprint( normalized["session_id"], normalized["goal_id"] @@ -1587,6 +1610,11 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No }, ) if action_kind == "todo.create": + goal_id = str(parameters["goal_id"]) + registry_path, runtime_root_arg = todo_create_source(self, goal_id) + agent_id = parameters.get("agent_id") + if agent_id and agent_id not in registered_agent_ids_from_registry(registry_path, goal_id): + raise ValueError("the selected Agent is no longer registered for this Goal") checkpoint = proposal.get("checkpoint") steps = checkpoint.get("steps") if isinstance(checkpoint, dict) else {} steps = steps if isinstance(steps, dict) else {} @@ -1601,8 +1629,9 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No ) else: current = build_todo_review_preview( - registry_path=self.registry_path, - goal_id=str(parameters["goal_id"]), + registry_path=registry_path, + goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=str(parameters["text"]), priority=parameters.get("priority"), ) @@ -1615,8 +1644,9 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No ) return {"proposal": stale, "turn": None} applied = apply_todo_review_preview( - registry_path=self.registry_path, - goal_id=str(parameters["goal_id"]), + registry_path=registry_path, + goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=str(parameters["text"]), priority=parameters.get("priority"), preview_id=current_fingerprint, @@ -1631,11 +1661,11 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No "canonical_receipt": canonical_receipt, }, ) - agent_id = parameters.get("agent_id") if agent_id and not isinstance(steps.get("todo_assigned"), dict): update_goal_todo( - registry_path=self.registry_path, - goal_id=str(parameters["goal_id"]), + registry_path=registry_path, + goal_id=goal_id, + runtime_root_arg=runtime_root_arg, todo_id=todo_id, claimed_by=str(agent_id), agent_id=str(agent_id), @@ -1675,7 +1705,7 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No turn_id = _opaque(execution_step.get("turn_id"), field="turn_id") created = False else: - goal = self._goal(str(parameters["goal_id"])) + goal = self._goal(goal_id, registry_path=registry_path) project = Path(str(goal.get("repo") or ".")).expanduser().resolve() if not project.is_dir(): raise ValueError("the Goal project root is unavailable") diff --git a/loopx/chat_server.py b/loopx/chat_server.py index 711818a2a4..b25576c856 100644 --- a/loopx/chat_server.py +++ b/loopx/chat_server.py @@ -20,12 +20,7 @@ from .capabilities.native_chat.transports import ChatConversationTransports from .attached_session_api import AttachedSessionRequestMixin from .zcode_goal_mode.api import ZCodeGoalRequestMixin -from .chat import ( - TodoReviewPreviewConflict, - apply_todo_review_preview, - build_todo_review_preview, - redact_local_paths, -) +from .chat import TodoReviewPreviewConflict, redact_local_paths from .chat_agent import CodexChatAgentError from .capabilities.native_chat.project_context import ChatProjectContexts from .chat_attachments import ( @@ -45,6 +40,7 @@ from .capabilities.native_chat.model_settings_api import ChatModelSettingsRequestMixin from .capabilities.native_chat.queue_api import ChatQueueRequestMixin from .chat.status_api import ChatStatusRequestMixin +from .chat.todo_actions import review_todo_create from .chat_runtime import ( STEERING_NOT_DELIVERED_CODES, ChatRuntimeController, @@ -1200,19 +1196,10 @@ def _todo(self, *, apply: bool) -> None: if not goal_id: raise ValueError("goal_id is required") self._registry_and_goal(goal_id) - if apply: - payload = apply_todo_review_preview( - registry_path=self.server.registry_path, - goal_id=goal_id, - text=text, - preview_id=str(body.get("preview_id") or ""), - ) - else: - payload = build_todo_review_preview( - registry_path=self.server.registry_path, - goal_id=goal_id, - text=text, - ) + payload = review_todo_create( + self.server, goal_id=goal_id, text=text, + preview_id=str(body.get("preview_id") or "") if apply else None, + ) except TodoReviewPreviewConflict as exc: self._send_error(str(exc), status=409, todo_receipt=exc.receipt) return diff --git a/tests/control_plane/test_chat_reviewed_create.py b/tests/control_plane/test_chat_reviewed_create.py index c29a70d3fa..cbffc08847 100644 --- a/tests/control_plane/test_chat_reviewed_create.py +++ b/tests/control_plane/test_chat_reviewed_create.py @@ -1,12 +1,144 @@ """Reviewed App creates must retain their revision through the actual write.""" +import json +from http.client import HTTPConnection +from threading import Thread + import pytest import loopx.chat as chat from canonical_authority_fixture import isolate_sqlite_runtime, promoted_create_fixture +from loopx.chat_action_store import ChatActionStore +from loopx.chat_actions import ChatActionService +from loopx.chat_server import ChatHTTPServer, ChatRequestHandler from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted from loopx.control_plane.todos.mutation_api import add_goal_todo +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("route", ["serving", "registered", "shared-directory", "shared-unbound"]) +@pytest.mark.parametrize("kind", ["todo.create", "monitor.create", "todo-review"]) +def test_http_create_uses_goal_runtime(tmp_path, monkeypatch, provider, route, kind): + isolate_sqlite_runtime(tmp_path, monkeypatch) + source_registry, runtime, state = promoted_create_fixture(tmp_path, provider=provider) + registry = source_registry + monkeypatch.setattr("loopx.paths.DEFAULT_RUNTIME_ROOT", tmp_path / "ambient-current") + monkeypatch.setattr("loopx.paths.LEGACY_RUNTIME_ROOT", tmp_path / "ambient-legacy") + serving_root = runtime + decoy = None + if route == "serving": + registration = json.loads(registry.read_text()) + del registration["common_runtime_root"] + registry.write_text(json.dumps(registration)) + else: + (tmp_path / "other").mkdir() + _, decoy_root, _ = promoted_create_fixture(tmp_path / "other", provider=provider) + decoy = read_canonical_todos_if_promoted(runtime_root=decoy_root, goal_id="goal-a") + serving_root = decoy_root if route == "registered" else tmp_path / "chat-storage" + if route.startswith("shared"): + registration = json.loads(registry.read_text()) + registration.update(registry_role="global-local", common_runtime_root=str(decoy_root)) + if route == "shared-unbound": + del registration["common_runtime_root"] + registration["goals"][0]["source_registry"] = str(source_registry) + registry = tmp_path / "directory.json" + registry.write_text(json.dumps(registration)) + before = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") + store = ChatActionStore(serving_root / "chat" / "actions") + server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler) + server.registry_path, server.runtime_root_override, server.verbose = registry, str(serving_root), False + server.action_store = store + server.action_service = ChatActionService(store=store, registry_path=registry, + runtime_root_override=str(serving_root)) + worker = Thread(target=server.serve_forever, daemon=True) + worker.start() + connection = HTTPConnection(*server.server_address, timeout=30) + + def request(method, path, body=None): + connection.request(method, path, None if body is None else json.dumps(body), + {"Content-Type": "application/json"}) + response = connection.getresponse() + return response.status, json.loads(response.read()) + + text = "Review the project index" + try: + if kind == "todo-review": + intent = {"goal_id": "goal-a", "text": text} + code, preview = request("POST", "/api/chat/todo/dry-run", intent) + assert code == 200, preview + apply_path, confirmation = "/api/chat/todo/apply", {**intent, "preview_id": preview["preview_id"]} + else: + parameters = {"goal_id": "goal-a", "agent_id": "agent-a"} + if kind == "monitor.create": + parameters.update(target=text, target_key="index-check", cadence="2h", + timezone="UTC", stop_condition="watch_only") + else: + parameters.update(text=text, priority="P2") + code, preview = request("POST", "/api/actions/preview", { + "action_kind": kind, "summary": text, "normalized_parameters": parameters, + "context": {"kind": "goal", "goal_id": "goal-a"}, "idempotency_key": "review-index", + }) + assert code == 201, preview + action_path = f"/api/actions/{preview['proposal']['proposal_id']}" + if kind == "todo.create" and route == "shared-directory": + original = source_registry.read_text() + changed = json.loads(original) + changed["goals"][0]["coordination"]["registered_agents"] = ["another-agent"] + source_registry.write_text(json.dumps(changed)) + refused_code, refused = request("POST", f"{action_path}/apply", {}) + assert refused_code == 400 and "no longer registered" in refused["error"], refused + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == before + source_registry.write_text(original) + if kind == "monitor.create" and route == "shared-directory": + original = source_registry.read_text() + source_registry.unlink() + unavailable_code, unavailable = request("POST", f"{action_path}/apply", {}) + assert unavailable_code == 400 and unavailable["error_code"] == "invalid_action_apply", unavailable + assert str(tmp_path) not in json.dumps(unavailable) + assert read_canonical_todos_if_promoted(runtime_root=decoy_root, goal_id="goal-a") == decoy + source_registry.write_text(json.dumps({**json.loads(original), + "common_runtime_root": str(decoy_root)})) + refused_code, refused = request("POST", f"{action_path}/apply", {}) + assert refused_code == 409 and refused["proposal"]["status"] == "stale", refused + assert read_canonical_todos_if_promoted(runtime_root=decoy_root, goal_id="goal-a") == decoy + source_registry.write_text(original) + code, preview = request("POST", f"{action_path}/regenerate", {}) + assert code == 201, preview + action_path = f"/api/actions/{preview['proposal']['proposal_id']}" + apply_path, confirmation = f"{action_path}/apply", {} + + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == before + assert text not in state.read_text() + code, applied = request("POST", apply_path, confirmation) + assert code == 200, applied + receipt = applied["receipt"] if kind == "todo-review" else applied["proposal"]["receipt"] + todo_id = receipt["todo_id"] if kind == "todo-review" else receipt["resource_ids"]["todo_id"] + saved = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") + assert len(saved["todos"]) == 1 + todo = saved["todos"][0] + assert todo["todo_id"] == todo_id and text in todo["text"] + assert text in state.read_text() + if kind != "todo-review": + assert todo["claimed_by"] == "agent-a" + if kind == "monitor.create": + assert (todo["task_class"], todo["cadence"], todo["target_key"]) == ( + "continuous_monitor", "2h", "index-check") + assert todo["next_due_at"] + else: + assert todo["priority"] == "P2" + assert request("POST", apply_path, confirmation) == (code, applied) + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == saved + detail_code, detail = request("GET", f"/api/chat/todo/detail?goal_id=goal-a&todo_id={todo_id}") + assert detail_code == 200, detail + assert detail["review_basis"]["provider_revision"] == saved["provider_revision"] + if decoy is not None: + assert read_canonical_todos_if_promoted(runtime_root=decoy_root, goal_id="goal-a") == decoy + finally: + connection.close() + server.shutdown() + server.server_close() + worker.join(timeout=5) + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("timing", ["before-confirmation", "between-preview-reads", "after-final-preview"]) def test_concurrent_create_refuses_reviewed_intent(tmp_path, monkeypatch, provider, timing): From a00b3dc0e1161f2bcce4f5e626ddc2597338de9a Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:50:12 +0800 Subject: [PATCH 04/14] fix(chat): recover committed Todo and Monitor confirmations Persist the reviewed provider, source and operation before a create. On retry, the canonical writer recovers the original receipt before checking freshness for a new write. Monitor retries retain the first due time and concurrent callers share the saved request. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- docs/project-agent-todo-contract.md | 11 + loopx/chat.py | 48 +++- loopx/chat/todo_actions.py | 53 +++- loopx/chat_action_store.py | 11 + loopx/chat_actions.py | 150 ++++++----- .../coordination/authority_source.ts | 7 + .../coordination/local_authority_runtime.ts | 19 +- .../control_plane/coordination/todo_create.ts | 9 +- loopx/control_plane/todos/mutation_api.py | 2 + loopx/control_plane/todos/provider_create.py | 14 +- .../test_chat_create_confirmation_recovery.py | 246 ++++++++++++++++++ .../todo_create_reviewed.test.ts | 23 ++ 12 files changed, 514 insertions(+), 79 deletions(-) create mode 100644 tests/control_plane/test_chat_create_confirmation_recovery.py diff --git a/docs/project-agent-todo-contract.md b/docs/project-agent-todo-contract.md index 0063f77c6c..d6ae0edcf7 100644 --- a/docs/project-agent-todo-contract.md +++ b/docs/project-agent-todo-contract.md @@ -90,6 +90,17 @@ same operation ID is a conflict. No provider promotion or receipt rewrite occurs ## Write Contract +Confirming a typed Chat Todo or Monitor prepares the provider revision and +source registry digest before writing. The proposal's operation ID and the +Monitor's first due time are reused on retry. After checking the current Goal +source and caller authority, retry returns the original receipt for a committed +create. An uncommitted create still requires the reviewed source and provider +revision. Older control planes reject the source-bound v3 request before writing. + +确认 Chat 的 Todo 或 Monitor 提案时,先记录源注册表摘要和权威存储修订号,再执行写入。 +重试沿用原操作 ID 和 Monitor 的首次到期时间。已提交的操作返回原回执,未提交的操作仍校验 +确认时的来源和修订号。来源绑定请求采用 v3 协议,旧版控制面会在写入前拒绝。 + For a caller-owned runtime that already registered its Goal/Agent, generate the model planning checkpoint before writing executable task Todos: diff --git a/loopx/chat.py b/loopx/chat.py index 23cb146d64..3a8f5a1d7d 100644 --- a/loopx/chat.py +++ b/loopx/chat.py @@ -1055,15 +1055,23 @@ def _todo_preview_fingerprint(payload: dict[str, Any]) -> str: ) +def todo_create_was_duplicate(payload: dict[str, Any]) -> bool: + """Classify the original create, even when this call only read its receipt.""" + if payload.get("decision_read_from_provider") is True: + return payload.get("status") == "no_change" + return bool(payload.get("already_exists")) + + def _todo_write_receipt( *, preview_id: str, - preview_payload: dict[str, Any], + preview_revision: str | None, applied_payload: dict[str, Any], ) -> dict[str, Any]: compact = _compact_todo_payload(applied_payload, applied=True) todo = compact["todo"] - outcome = "todo_already_exists" if compact["already_exists"] else "todo_added" + already_exists = todo_create_was_duplicate(applied_payload) + outcome = "todo_already_exists" if already_exists else "todo_added" receipt = { "schema_version": CHAT_TODO_RECEIPT_SCHEMA_VERSION, "preview_id": preview_id, @@ -1071,8 +1079,8 @@ def _todo_write_receipt( "todo_id": todo["todo_id"], "status": "applied", "outcome": outcome, - "already_exists": compact["already_exists"], - "preview_revision": _todo_revision(preview_payload), + "already_exists": already_exists, + "preview_revision": preview_revision, } receipt["receipt_id"] = _stable_digest(receipt) return receipt @@ -1111,6 +1119,7 @@ def _add_review_todo( priority: str | None = None, dry_run: bool, expected_provider_revision: str | None = None, + expected_registry_sha256: str | None = None, operation_id: str | None = None, ) -> dict[str, Any]: return add_goal_todo( @@ -1129,9 +1138,38 @@ def _add_review_todo( if expected_provider_revision is not None else {} ), + **({"expected_registry_sha256": expected_registry_sha256} + if expected_registry_sha256 is not None else {}), ) +def plan_reviewed_todo_create( + *, registry_path: Path, goal_id: str, runtime_root_arg: str, text: str, + priority: str | None, +) -> tuple[str, str]: + """Bind a typed proposal's existing preview to its provider revision.""" + preview = _add_review_todo(registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, dry_run=True) + revision = _todo_revision(preview) + if preview.get("decision_read_from_provider") is not True or not revision: + raise ValueError("Todo confirmation requires a canonical preview") + return _todo_preview_fingerprint(preview), revision + + +def apply_prepared_todo_review( + *, registry_path: Path, goal_id: str, runtime_root_arg: str, text: str, + priority: str | None, preview_id: str, operation_id: str, + expected_provider_revision: str, expected_registry_sha256: str, +) -> dict[str, Any]: + """Send the original reviewed request so the provider can recover it.""" + applied = _add_review_todo(registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, dry_run=False, + operation_id=operation_id, expected_provider_revision=expected_provider_revision, + expected_registry_sha256=expected_registry_sha256) + return _todo_write_receipt(preview_id=preview_id, preview_revision=expected_provider_revision, + applied_payload=applied) + + def _bound_todo_review_preview( *, registry_path: Path, @@ -1256,7 +1294,7 @@ def apply_todo_review_preview( compact = _compact_todo_payload(applied, applied=True) compact["receipt"] = _todo_write_receipt( preview_id=preview_id, - preview_payload=current_preview, + preview_revision=_todo_revision(current_preview), applied_payload=applied, ) return compact diff --git a/loopx/chat/todo_actions.py b/loopx/chat/todo_actions.py index d6d4154d9c..02c1960701 100644 --- a/loopx/chat/todo_actions.py +++ b/loopx/chat/todo_actions.py @@ -6,7 +6,8 @@ from typing import Any from .status_api import goal_read_runtime_root -from ..chat import apply_todo_review_preview, build_todo_review_preview +from ..chat import _stable_digest, apply_todo_review_preview, build_todo_review_preview +from ..control_plane.coordination.authority_source_capture import authority_registry_source from ..control_plane.todos.mutation_api import complete_goal_todo, update_goal_todo from ..control_plane.coordination.local_authority import ( LocalCoordinationAuthorityUnavailable, read_canonical_todos_if_promoted, @@ -37,6 +38,12 @@ def review_todo_create( runtime_root_arg=runtime_root_arg, text=text, preview_id=preview_id) +def create_checkpoint_steps(proposal: dict[str, Any]) -> dict[str, Any]: + checkpoint = proposal.get("checkpoint") + steps = checkpoint.get("steps") if isinstance(checkpoint, dict) else None + return steps if isinstance(steps, dict) else {} + + def _decision_readback(result: dict[str, Any]) -> dict[str, Any]: """Carry the canonical outcome and dependent effect, never a local guess.""" resume = result.get("unblock_resume") @@ -54,6 +61,50 @@ def _todo_runtime_root_arg(self) -> str | None: return goal_read_runtime_root(self) + def _confirmed_create_source(self, goal_id: str) -> tuple[Path, str, str, str]: + registry_path, override = todo_create_source(self, goal_id) + with authority_registry_source(registry_path) as witness: + root = str(effective_runtime_root(registry_path, override).resolve()) + source_binding = _stable_digest({"registry": witness["path"], "runtime": root}, length=64) + return registry_path, root, source_binding, witness["sha256"] + + def _save_create_plan( + self, proposal: dict[str, Any], fingerprint: str, plan: dict[str, Any], + ) -> dict[str, Any]: + proposal_id = proposal["proposal_id"] + if fingerprint != proposal["expected_state_fingerprint"]: + return self.store.apply(proposal_id, current_state_fingerprint=fingerprint, receipt={}, + stale_unless_checkpoint="todo_create_prepared") + return self.store.save_checkpoint(proposal_id, step="todo_create_prepared", + receipt={**plan, "operation_id": f"chat-create:{proposal_id}"}, once=True) + + @staticmethod + def _prepared_create_arguments(proposal: dict[str, Any], source_binding: str) -> dict[str, str]: + plan = create_checkpoint_steps(proposal).get("todo_create_prepared") + if not isinstance(plan, dict) or plan.get("operation_id") != f"chat-create:{proposal['proposal_id']}": + raise ValueError("Todo creation plan is missing or malformed") + if plan.get("source_binding") != source_binding: + raise ValueError("The Goal source changed since confirmation. Restore its original source before retrying.") + arguments = {key: plan.get(key) for key in ( + "operation_id", "expected_provider_revision", "expected_registry_sha256", + )} + if any(not isinstance(value, str) or not value for value in arguments.values()): + raise ValueError("Todo creation plan is missing its canonical write basis") + return arguments + + def _create_confirmation_conflict( + self, proposal: dict[str, Any], error: LocalCoordinationAuthorityUnavailable, + ) -> dict[str, Any]: + if error.code not in {"provider_revision_mismatch", "authority_source_changed"}: + raise error + # The canonical transaction checks the original receipt before either + # condition. These errors therefore reject a write that never landed. + stale = self.store.apply(proposal["proposal_id"], current_state_fingerprint=_stable_digest({ + "expected": proposal["expected_state_fingerprint"], "conflict": error.code, + "provider_revision": error.payload.get("current_provider_revision"), + }), receipt={}) + return {"proposal": stale, "turn": self._turn_from_receipt(stale.get("receipt"))} + def _run_todo_update( self, parameters: dict[str, Any], *, dry_run: bool, basis: dict[str, Any] | None = None, operation_id: str | None = None, diff --git a/loopx/chat_action_store.py b/loopx/chat_action_store.py index 3601c7a6f3..6fa63b037c 100644 --- a/loopx/chat_action_store.py +++ b/loopx/chat_action_store.py @@ -1398,6 +1398,7 @@ def save_checkpoint( *, step: str, receipt: Mapping[str, Any], + once: bool = False, ) -> dict[str, Any]: safe_step = _opaque_id(step, field="step") safe_receipt = _safe_json_value(dict(receipt), path="checkpoint.receipt") @@ -1413,6 +1414,8 @@ def save_checkpoint( proposal = payload["proposals"].get(token) if not isinstance(proposal, dict): raise KeyError("typed Chat action proposal was not found") + if once and proposal.get("status") == "applied": + return proposal if proposal.get("status") not in {"applying", "gated", "failed"}: raise ActionConflictError( f"proposal in {proposal.get('status')} state cannot save a checkpoint" @@ -1423,6 +1426,8 @@ def save_checkpoint( ) steps = checkpoint.get("steps") steps = dict(steps) if isinstance(steps, dict) else {} + if once and safe_step in steps: + return proposal steps[safe_step] = safe_receipt checkpoint["steps"] = steps checkpoint["last_completed_step"] = safe_step @@ -1511,6 +1516,7 @@ def apply( *, current_state_fingerprint: str, receipt: Mapping[str, Any], + stale_unless_checkpoint: str | None = None, ) -> dict[str, Any]: token = _opaque_id(proposal_id, field="proposal_id") current_fingerprint = _bounded_text( @@ -1537,6 +1543,11 @@ def apply( expected_fingerprint = str(proposal.get("expected_state_fingerprint") or "") now = _utc_now() if current_fingerprint != expected_fingerprint: + # A concurrent confirmer may already have bound the original + # write. Its durable plan must resolve commit vs stale. + checkpoint = proposal.get("checkpoint") or {} + if stale_unless_checkpoint and stale_unless_checkpoint in (checkpoint.get("steps") or {}): + return proposal proposal["status"] = "stale" proposal["stale"] = { "expected_state_fingerprint": expected_fingerprint, diff --git a/loopx/chat_actions.py b/loopx/chat_actions.py index 6d0c50ad8c..67cd1cbdfd 100644 --- a/loopx/chat_actions.py +++ b/loopx/chat_actions.py @@ -15,7 +15,12 @@ ) from .control_plane.projects.identity import GoalCreationConflictError from .control_plane.coordination.new_goal_storage_transport import initialize_goal_storage_target -from .chat import apply_todo_review_preview, build_todo_review_preview +from .chat import ( + apply_prepared_todo_review, + build_todo_review_preview, + plan_reviewed_todo_create, + todo_create_was_duplicate, +) from .chat_action_normalization import ChatActionNormalizationMixin from .chat_action_store import ( ACTION_KINDS as SUPPORTED_ACTION_KINDS, @@ -25,7 +30,7 @@ from .chat_goal_lifecycle_actions import ChatGoalLifecycleActionMixin from .chat.monitor_actions import ChatMonitorActionMixin from .chat_store import ChatSessionStore -from .chat.todo_actions import ChatTodoActionMixin, todo_create_source +from .chat.todo_actions import ChatTodoActionMixin, create_checkpoint_steps, todo_create_source from .control_plane.goals.acceptance_actions import GoalAcceptanceActionMixin from .control_plane.goals.goal_objective_actions import GoalObjectiveActionMixin from .configure_goal import configure_goal @@ -1108,31 +1113,9 @@ def _apply_monitor_create( self, proposal_id: str, proposal: dict[str, Any], parameters: dict[str, Any] ) -> dict[str, Any]: goal_id = str(parameters["goal_id"]) - registry_path, runtime_root_arg = todo_create_source(self, goal_id) - current_fingerprint = self._monitor_create_fingerprint(registry_path) - if current_fingerprint != proposal.get("expected_state_fingerprint"): - stale = self.store.apply( - proposal_id, current_state_fingerprint=current_fingerprint, receipt={} - ) - return {"proposal": stale, "turn": None} + registry_path, runtime_root_arg, source_binding, registry_sha = self._confirmed_create_source(goal_id) agent_id = str(parameters["agent_id"]) - if agent_id not in registered_agent_ids_from_registry(registry_path, goal_id): - raise ValueError("monitor Agent must be registered for the Goal") - stop_condition = str(parameters.get("stop_condition") or "").strip() - resume_when = None - if ( - stop_condition - and parse_timestamp(stop_condition) is None - and stop_condition.lower() - not in {"watch_only", "watch-only", "watch", "continuous", "never"} - ): - resume_when = stop_condition - metadata = _monitor_metadata(parameters) - if not ( - metadata.get("expires_at") or resume_when or metadata.get("watch_only") - ): - metadata["watch_only"] = "true" - result = add_goal_todo( + intent = dict( registry_path=registry_path, goal_id=goal_id, runtime_root_arg=runtime_root_arg, @@ -1142,17 +1125,50 @@ def _apply_monitor_create( action_kind="observe", claimed_by=agent_id, agent_id=agent_id, - resume_when=resume_when, - monitor_metadata=metadata, - dry_run=False, ) + if "todo_create_prepared" not in create_checkpoint_steps(proposal): + fingerprint = self._monitor_create_fingerprint(registry_path) + if fingerprint != proposal["expected_state_fingerprint"]: + proposal = self._save_create_plan(proposal, fingerprint, {}) + else: + metadata = _monitor_metadata(parameters) + stop = str(parameters.get("stop_condition") or "").strip() + resume_when = stop if stop and not (metadata.get("expires_at") or metadata.get("watch_only")) else None + if not (metadata.get("expires_at") or resume_when or metadata.get("watch_only")): + metadata["watch_only"] = "true" + try: + preview = add_goal_todo(**intent, resume_when=resume_when, + monitor_metadata=metadata, dry_run=True) + except LocalCoordinationAuthorityUnavailable: + # A concurrent confirmer can commit before this dry-run. + current = self.store.load(proposal_id) + if not current or "todo_create_prepared" not in create_checkpoint_steps(current): + raise + proposal = current + else: + proposal = self._save_create_plan(proposal, fingerprint, { + "source_binding": source_binding, "expected_registry_sha256": registry_sha, + "expected_provider_revision": preview["provider_revision"], + "resume_when": resume_when, "monitor_metadata": metadata, + }) + if proposal["status"] in {"stale", "applied"}: + return {"proposal": proposal, "turn": None} + if agent_id not in registered_agent_ids_from_registry(registry_path, goal_id): + raise ValueError("monitor Agent must be registered for the Goal") + arguments = self._prepared_create_arguments(proposal, source_binding) + plan = create_checkpoint_steps(proposal)["todo_create_prepared"] + try: + result = add_goal_todo(**intent, resume_when=plan["resume_when"], + monitor_metadata=plan["monitor_metadata"], dry_run=False, **arguments) + except LocalCoordinationAuthorityUnavailable as error: + return self._create_confirmation_conflict(proposal, error) todo_id = _opaque(result.get("todo_id"), field="todo_id") receipt = { "receipt_id": _digest( {"proposal_id": proposal_id, "goal_id": goal_id, "todo_id": todo_id} )[:32], "outcome": "monitor_already_exists" - if result.get("already_exists") + if todo_create_was_duplicate(result) else "monitor_created", "projection_verified": True, "resource_ids": { @@ -1162,7 +1178,7 @@ def _apply_monitor_create( }, } stored = self.store.apply( - proposal_id, current_state_fingerprint=current_fingerprint, receipt=receipt + proposal_id, current_state_fingerprint=proposal["expected_state_fingerprint"], receipt=receipt ) return {"proposal": stored, "turn": None} @@ -1566,6 +1582,8 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No }, ) proposal = self.store.start_apply(proposal_id) + if proposal["status"] == "applied": + return {"proposal": proposal, "turn": self._turn_from_receipt(proposal.get("receipt"))} action_kind = str(proposal.get("action_kind") or "") parameters = proposal.get("normalized_parameters") if not isinstance(parameters, dict): @@ -1611,56 +1629,54 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No ) if action_kind == "todo.create": goal_id = str(parameters["goal_id"]) - registry_path, runtime_root_arg = todo_create_source(self, goal_id) + registry_path, runtime_root_arg, source_binding, registry_sha = self._confirmed_create_source(goal_id) agent_id = parameters.get("agent_id") if agent_id and agent_id not in registered_agent_ids_from_registry(registry_path, goal_id): raise ValueError("the selected Agent is no longer registered for this Goal") - checkpoint = proposal.get("checkpoint") - steps = checkpoint.get("steps") if isinstance(checkpoint, dict) else {} - steps = steps if isinstance(steps, dict) else {} + steps = create_checkpoint_steps(proposal) + if "todo_create_prepared" not in steps and "todo_created" not in steps: + fingerprint, revision = plan_reviewed_todo_create( + registry_path=registry_path, goal_id=goal_id, runtime_root_arg=runtime_root_arg, + text=str(parameters["text"]), priority=parameters.get("priority"), + ) + proposal = self._save_create_plan(proposal, fingerprint, { + "source_binding": source_binding, "expected_registry_sha256": registry_sha, + "expected_provider_revision": revision, + }) + if proposal["status"] in {"stale", "applied"}: + return {"proposal": proposal, "turn": self._turn_from_receipt(proposal.get("receipt"))} + steps = create_checkpoint_steps(proposal) + arguments = (self._prepared_create_arguments(proposal, source_binding) + if "todo_create_prepared" in steps else {}) todo_step = steps.get("todo_created") + preview_id = proposal["expected_state_fingerprint"] if isinstance(todo_step, dict) and todo_step.get("todo_id"): todo_id = _opaque(todo_step.get("todo_id"), field="todo_id") canonical_receipt = todo_step.get("canonical_receipt") if not isinstance(canonical_receipt, dict): raise ValueError("Todo creation checkpoint is malformed") - current_fingerprint = str( - proposal.get("expected_state_fingerprint") or "" - ) else: - current = build_todo_review_preview( - registry_path=registry_path, - goal_id=goal_id, - runtime_root_arg=runtime_root_arg, - text=str(parameters["text"]), - priority=parameters.get("priority"), - ) - current_fingerprint = str(current["preview_id"]) - if current_fingerprint != proposal.get("expected_state_fingerprint"): - stale = self.store.apply( - proposal_id, - current_state_fingerprint=current_fingerprint, - receipt={}, + try: + canonical_receipt = apply_prepared_todo_review( + registry_path=registry_path, goal_id=goal_id, runtime_root_arg=runtime_root_arg, + text=str(parameters["text"]), priority=parameters.get("priority"), + preview_id=preview_id, **arguments, ) - return {"proposal": stale, "turn": None} - applied = apply_todo_review_preview( - registry_path=registry_path, - goal_id=goal_id, - runtime_root_arg=runtime_root_arg, - text=str(parameters["text"]), - priority=parameters.get("priority"), - preview_id=current_fingerprint, - ) - canonical_receipt = applied["receipt"] + except LocalCoordinationAuthorityUnavailable as error: + return self._create_confirmation_conflict(proposal, error) todo_id = _opaque(canonical_receipt["todo_id"], field="todo_id") - self.store.save_checkpoint( + proposal = self.store.save_checkpoint( proposal_id, step="todo_created", receipt={ "todo_id": todo_id, "canonical_receipt": canonical_receipt, }, + once=True, ) + if proposal["status"] == "applied": + return {"proposal": proposal, "turn": self._turn_from_receipt(proposal.get("receipt"))} + steps = create_checkpoint_steps(proposal) if agent_id and not isinstance(steps.get("todo_assigned"), dict): update_goal_todo( registry_path=registry_path, @@ -1670,13 +1686,18 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No claimed_by=str(agent_id), agent_id=str(agent_id), authority_reason="owner-confirmed typed Chat action", + update_operation_id=f"chat-assign:{proposal_id}", dry_run=False, ) - self.store.save_checkpoint( + proposal = self.store.save_checkpoint( proposal_id, step="todo_assigned", receipt={"todo_id": todo_id, "agent_id": str(agent_id)}, + once=True, ) + if proposal["status"] == "applied": + return {"proposal": proposal, "turn": self._turn_from_receipt(proposal.get("receipt"))} + steps = create_checkpoint_steps(proposal) receipt = { "receipt_id": str(canonical_receipt["receipt_id"]), "outcome": str(canonical_receipt["outcome"]), @@ -1732,6 +1753,7 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No proposal_id, step="execution_started", receipt={"session_id": session_id, "turn_id": turn_id}, + once=True, ) receipt["resource_ids"].update( {"session_id": session_id, "turn_id": turn_id} @@ -1745,7 +1767,7 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No } stored = self.store.apply( proposal_id, - current_state_fingerprint=current_fingerprint, + current_state_fingerprint=preview_id, receipt=receipt, ) return {"proposal": stored, "turn": turn_result} diff --git a/loopx/control_plane/coordination/authority_source.ts b/loopx/control_plane/coordination/authority_source.ts index 2779466d32..cf57a0447c 100644 --- a/loopx/control_plane/coordination/authority_source.ts +++ b/loopx/control_plane/coordination/authority_source.ts @@ -15,6 +15,13 @@ export const AUTHORITY_SOURCE_CHANGED = { reason: "Todo authority registration changed; review the current state before continuing", } as const; +export function requireReviewedRegistrySha256(value: unknown): string { + if (typeof value !== "string" || !BARE_SHA256_PATTERN.test(value)) { + throw new TypeError("expected_registry_sha256 must be a SHA-256 digest"); + } + return value; +} + export function registryAuthoritySourceCheck( input: JsonObject, witnessed: boolean, expectedDigest: unknown = null, ): AuthoritySourceCheck { diff --git a/loopx/control_plane/coordination/local_authority_runtime.ts b/loopx/control_plane/coordination/local_authority_runtime.ts index 4d4de11614..6d412ded63 100644 --- a/loopx/control_plane/coordination/local_authority_runtime.ts +++ b/loopx/control_plane/coordination/local_authority_runtime.ts @@ -2,7 +2,7 @@ import {requirePromotionRegisteredAgents} from "./shadow_registry_source.ts"; import {verifyReentrySettlement} from "./todo_terminal_reentry.ts"; import {readPromotionReceipt, commitPromotionAndReadBack} from './promotion_receipt.ts'; import {reviewedPromotionPlan, promotionPlanDigest, decodeReviewedPromotionOperation, REVIEWED_PROMOTION_OPERATION_RESULT_SCHEMA} from './reviewed_promotion_plan.ts'; -import {registryAuthoritySourceCheck} from "./authority_source.ts"; +import {registryAuthoritySourceCheck, requireReviewedRegistrySha256} from "./authority_source.ts"; import {ResearchTerminalEvidenceHost} from "../capabilities/explore_research_terminal.ts"; import {decodeTaskLeaseProof} from "./task_lease_proof.ts"; import type {ClaimWorkspaceObserver} from "./claim_execution.ts"; @@ -114,6 +114,8 @@ export const LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA = "loopx_local_coordination_todo_create_request_v0"; export const LOCAL_COORDINATION_TODO_CREATE_WITNESSED_REQUEST_SCHEMA = "loopx_local_coordination_todo_create_request_v1"; export const LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA = "loopx_local_coordination_todo_create_request_v2"; +export const LOCAL_COORDINATION_TODO_CREATE_SOURCE_BOUND_REQUEST_SCHEMA = + "loopx_local_coordination_todo_create_request_v3"; export const LOCAL_COORDINATION_TODO_TERMINAL_LIFECYCLE_REQUEST_SCHEMA = "loopx_local_coordination_todo_terminal_lifecycle_request_v3"; export const LOCAL_COORDINATION_TODO_ARCHIVE_REQUEST_SCHEMA = @@ -1146,17 +1148,21 @@ export async function createLocalCoordinationTodo( }; try { const input = requireJsonObject(value, "local coordination Todo create request"); - if (input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA && - input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_WITNESSED_REQUEST_SCHEMA && - input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA) { + const sourceBound = input.schema_version === LOCAL_COORDINATION_TODO_CREATE_SOURCE_BOUND_REQUEST_SCHEMA; + const reviewed = sourceBound || input.schema_version === LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA; + if (!reviewed && input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA && + input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_WITNESSED_REQUEST_SCHEMA) { throw new TypeError("local coordination Todo create request schema mismatch"); } - const reviewed = input.schema_version === LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA; if (!reviewed && Object.hasOwn(input, "expected_provider_revision")) { throw new TypeError("expected_provider_revision requires Todo create request v2"); } + if (!sourceBound && Object.hasOwn(input, "expected_registry_sha256")) { + throw new TypeError("expected_registry_sha256 requires Todo create request v3"); + } + const expectedRegistry = sourceBound ? requireReviewedRegistrySha256(input.expected_registry_sha256) : undefined; const authoritySourcesCurrent = registryAuthoritySourceCheck(input, - input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA); + input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA, expectedRegistry); const expectedRevision = reviewed ? requireAuthorityStoreId(input.expected_provider_revision, "expected provider revision") : undefined; if (typeof input.dry_run !== "boolean") { @@ -1183,6 +1189,7 @@ export async function createLocalCoordinationTodo( operation_id: requireAuthorityStoreId(input.operation_id, "operation id"), dry_run: input.dry_run === true, ...(expectedRevision === undefined ? {} : {expected_provider_revision: expectedRevision}), + ...(expectedRegistry === undefined ? {} : {expected_registry_sha256: expectedRegistry}), now: claimObservedAt(input.observed_at), }, authoritySourcesCurrent); return { diff --git a/loopx/control_plane/coordination/todo_create.ts b/loopx/control_plane/coordination/todo_create.ts index 45a62d7fc6..ee964a5435 100644 --- a/loopx/control_plane/coordination/todo_create.ts +++ b/loopx/control_plane/coordination/todo_create.ts @@ -1,5 +1,6 @@ import {planTodoCreationPriority} from "../todos/priority.ts"; -import {AUTHORITY_SOURCE_CHANGED, uncheckedAuthoritySource, type AuthoritySourceCheck} from "./authority_source.ts"; +import {AUTHORITY_SOURCE_CHANGED, requireReviewedRegistrySha256, + uncheckedAuthoritySource, type AuthoritySourceCheck} from "./authority_source.ts"; import type { JsonObject } from "../effect_program.ts"; import {CoordinationCommandReceipt} from "./command_receipt.ts"; import type { AuthorityStore } from "./authority_store.ts"; @@ -34,6 +35,7 @@ export interface CoordinationTodoCreateInput { readonly operation_id: string; readonly dry_run: boolean; readonly expected_provider_revision?: string; + readonly expected_registry_sha256?: string; readonly now: Date; } @@ -84,6 +86,9 @@ function normalizeCreateInput(rawInput: CoordinationTodoCreateInput, if (input.expected_provider_revision !== undefined) { requireAuthorityStoreId(input.expected_provider_revision, "expected provider revision"); } + if (input.expected_registry_sha256 !== undefined) { + requireReviewedRegistrySha256(input.expected_registry_sha256); + } if (todo.status === "done" || todo.archive_state !== "active") { throw new AuthorityStoreProtocolError( "Todo create requires an active record that is not already done", @@ -250,6 +255,8 @@ export async function executeCoordinationTodoCreate( dry_run: input.dry_run, ...(input.expected_provider_revision === undefined ? {} : {expected_provider_revision: input.expected_provider_revision}), + ...(input.expected_registry_sha256 === undefined ? {} : + {expected_registry_sha256: input.expected_registry_sha256}), }); const receipt = createReceipt(input, requestSha); const existing = await receipt.read(store); diff --git a/loopx/control_plane/todos/mutation_api.py b/loopx/control_plane/todos/mutation_api.py index 7299c0eac1..510db36020 100644 --- a/loopx/control_plane/todos/mutation_api.py +++ b/loopx/control_plane/todos/mutation_api.py @@ -77,6 +77,7 @@ def add_goal_todo( dry_run: bool = False, operation_id: str | None = None, expected_provider_revision: str | None = None, + expected_registry_sha256: str | None = None, ) -> dict[str, Any]: shadow_runtime_root = effective_runtime_root(registry_path, runtime_root_arg) if role not in TODO_SECTION_HEADINGS: @@ -151,6 +152,7 @@ def add_goal_todo( return create_canonical_todo_if_promoted( operation_id=operation_id, expected_provider_revision=expected_provider_revision, + expected_registry_sha256=expected_registry_sha256, registry_path=registry_path, runtime_root=shadow_runtime_root, goal_id=goal_id, diff --git a/loopx/control_plane/todos/provider_create.py b/loopx/control_plane/todos/provider_create.py index 8386b1b018..50edc90e40 100644 --- a/loopx/control_plane/todos/provider_create.py +++ b/loopx/control_plane/todos/provider_create.py @@ -47,11 +47,20 @@ def create_canonical_todo_if_promoted( project: Path | None = None, state_file: Path | None = None, operation_id: str | None = None, expected_provider_revision: str | None = None, + expected_registry_sha256: str | None = None, ) -> dict[str, Any]: # The native writer guard selects the route under the same maintenance # lock as provider open and commit. A separate source read grants nothing. if operation_id is not None and not re.fullmatch(r"[A-Za-z0-9_.:-]+", operation_id): raise ValueError("operation_id must be a non-empty public-safe token") + if expected_registry_sha256 is not None: + if expected_provider_revision is None: + raise ValueError("reviewed Todo source requires an expected provider revision") + request_schema = "loopx_local_coordination_todo_create_request_v3" + elif expected_provider_revision is not None: + request_schema = "loopx_local_coordination_todo_create_request_v2" + else: + request_schema = "loopx_local_coordination_todo_create_request_v1" operation_id = operation_id if operation_id is not None else f"todo-create:{uuid4().hex}" # Identity is independent of current Todo count: exact retries still address # the original operation after other creates, edits, completion or archive. @@ -99,10 +108,11 @@ def create_canonical_todo_if_promoted( result = effect_runtime_result( "coordination.local_authority.todo_create", { - "schema_version": ("loopx_local_coordination_todo_create_request_v2" - if expected_provider_revision is not None else "loopx_local_coordination_todo_create_request_v1"), + "schema_version": request_schema, **({"expected_provider_revision": expected_provider_revision} if expected_provider_revision is not None else {}), + **({"expected_registry_sha256": expected_registry_sha256} + if expected_registry_sha256 is not None else {}), "runtime_root": str(runtime_root.resolve()), "goal_id": goal_id, "todo": todo, diff --git a/tests/control_plane/test_chat_create_confirmation_recovery.py b/tests/control_plane/test_chat_create_confirmation_recovery.py new file mode 100644 index 0000000000..c67f4abaff --- /dev/null +++ b/tests/control_plane/test_chat_create_confirmation_recovery.py @@ -0,0 +1,246 @@ +"""Recover the original Chat confirmation against the real Todo providers.""" + +from concurrent.futures import ThreadPoolExecutor +from datetime import datetime, timedelta, timezone +import json +from threading import Barrier, Event + +import pytest + +from canonical_authority_fixture import isolate_sqlite_runtime, promoted_create_fixture +from loopx import chat, chat_actions +from loopx.chat_action_store import ChatActionStore +from loopx.chat_actions import ChatActionService +from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted +from loopx.control_plane.todos.mutation_api import add_goal_todo + + +@pytest.fixture(params=["file", "sqlite"]) +def confirmation(tmp_path, monkeypatch, request): + isolate_sqlite_runtime(tmp_path, monkeypatch) + registry, runtime, _ = promoted_create_fixture( + tmp_path, provider=request.param, registered_agents=["agent-a", "agent-b"], + ) + service = ChatActionService(store=ChatActionStore(runtime / "chat" / "actions"), + registry_path=registry) + return service, registry, runtime, request.param + + +def _preview(service, kind, key, *, agent=None): + parameters = {"goal_id": "goal-a"} + if kind == "monitor.create": + parameters.update(agent_id="agent-a", target="Review service availability", + target_key="service-health", cadence="2h", timezone="UTC", + stop_condition="watch_only") + else: + parameters.update(text="Review service availability", priority="P2") + if agent: + parameters["agent_id"] = agent + return service.preview({ + "action_kind": kind, "summary": "Review service availability", + "normalized_parameters": parameters, "context": {"kind": "goal", "goal_id": "goal-a"}, + "idempotency_key": key, + })["proposal_id"] + + +def _snapshot(runtime): + return read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") + + +def _interrupt_after_create(service, monkeypatch, kind): + lost = [] + if kind == "todo.create": + checkpoint = service.store.save_checkpoint + + def save(proposal_id, *, step, receipt, **kwargs): + if step == "todo_created" and not lost: + lost.append(receipt["canonical_receipt"]) + raise OSError("interrupted after provider commit") + return checkpoint(proposal_id, step=step, receipt=receipt, **kwargs) + + monkeypatch.setattr(service.store, "save_checkpoint", save) + else: + apply = service.store.apply + + def save(proposal_id, *, current_state_fingerprint, receipt): + if receipt.get("outcome") == "monitor_created" and not lost: + lost.append(receipt) + raise OSError("interrupted after provider commit") + return apply(proposal_id, current_state_fingerprint=current_state_fingerprint, receipt=receipt) + + monkeypatch.setattr(service.store, "apply", save) + return lost + + +@pytest.mark.parametrize("kind", ["todo.create", "monitor.create"]) +def test_lost_chat_confirmation_recovers_original_create(confirmation, monkeypatch, kind): + service, registry, runtime, _ = confirmation + proposal_id = _preview(service, kind, "original-confirmation") + lost = _interrupt_after_create(service, monkeypatch, kind) + confirmed_at = datetime.now(timezone.utc) + monkeypatch.setattr("loopx.chat_actions.now_utc", lambda: confirmed_at) + with pytest.raises(OSError, match="after provider commit"): + service.apply(proposal_id) + after_commit = _snapshot(runtime) + assert len(after_commit["todos"]) == 1 + todo = after_commit["todos"][0] + if kind == "monitor.create": + assert todo["next_due_at"] + + # Advance the time and registration revision without changing the source. + registry.write_text(registry.read_text() + "\n") + monkeypatch.setattr("loopx.chat_actions.now_utc", lambda: confirmed_at + timedelta(minutes=5)) + reopened = ChatActionService(store=ChatActionStore(runtime / "chat" / "actions"), + registry_path=registry) + recovered = reopened.apply(proposal_id)["proposal"] + assert recovered["status"] == "applied", recovered + assert recovered["receipt"]["receipt_id"] == lost[0]["receipt_id"] + assert recovered["receipt"]["outcome"] == lost[0]["outcome"] + assert recovered["receipt"]["resource_ids"]["todo_id"] == todo["todo_id"] + assert _snapshot(runtime) == after_commit + assert reopened.apply(proposal_id)["proposal"]["receipt"] == recovered["receipt"] + + +def test_separate_confirmation_cannot_claim_the_original_create(confirmation, monkeypatch): + service, _, runtime, _ = confirmation + first = _preview(service, "todo.create", "first", agent="agent-a") + competing = _preview(service, "todo.create", "competing", agent="agent-b") + _interrupt_after_create(service, monkeypatch, "todo.create") + with pytest.raises(OSError, match="after provider commit"): + service.apply(first) + saved = _snapshot(runtime) + assert service.apply(competing)["proposal"]["status"] == "stale" + assert _snapshot(runtime) == saved + assert service.apply(first)["proposal"]["status"] == "applied" + after_assignment = _snapshot(runtime) + assert len(after_assignment["todos"]) == 1 + assert after_assignment["todos"][0]["claimed_by"] == "agent-a" + assert service.apply(first)["proposal"]["status"] == "applied" + assert _snapshot(runtime) == after_assignment + + +def test_late_preparation_resumes_a_concurrent_commit(confirmation, monkeypatch): + service, _, runtime, _ = confirmation + proposal_id = _preview(service, "todo.create", "concurrent") + entered, resume = Event(), Event() + original_plan = chat_actions.plan_reviewed_todo_create + + def plan(**arguments): + if not entered.is_set(): + entered.set() + assert resume.wait(15), "the second confirmer did not finish" + return original_plan(**arguments) + + monkeypatch.setattr(chat_actions, "plan_reviewed_todo_create", plan) + lost = _interrupt_after_create(service, monkeypatch, "todo.create") + with ThreadPoolExecutor(max_workers=1) as pool: + late = pool.submit(service.apply, proposal_id) + try: + assert entered.wait(10) + with pytest.raises(OSError, match="after provider commit"): + service.apply(proposal_id) + finally: + resume.set() + recovered = late.result(timeout=30)["proposal"] + assert recovered["status"] == "applied" + assert recovered["receipt"]["canonical_receipt"] == lost[0] + assert len(_snapshot(runtime)["todos"]) == 1 + + +def test_saved_legacy_create_can_finish_assignment_once(confirmation, monkeypatch): + service, registry, runtime, _ = confirmation + proposal_id = _preview(service, "todo.create", "old-confirmation", agent="agent-a") + proposal = service.store.start_apply(proposal_id) + created = chat.apply_todo_review_preview( + registry_path=registry, goal_id="goal-a", text="Review service availability", + priority="P2", preview_id=proposal["expected_state_fingerprint"], + )["receipt"] + service.store.save_checkpoint(proposal_id, step="todo_created", receipt={ + "todo_id": created["todo_id"], "canonical_receipt": created, + }) + original_save = service.store.save_checkpoint + + def save(proposal_id, *, step, receipt, **kwargs): + if step == "todo_assigned": + raise OSError("interrupted after assignment") + return original_save(proposal_id, step=step, receipt=receipt, **kwargs) + + with monkeypatch.context() as faults: + faults.setattr(service.store, "save_checkpoint", save) + with pytest.raises(OSError, match="after assignment"): + service.apply(proposal_id) + assigned = _snapshot(runtime) + assert assigned["todos"][0]["claimed_by"] == "agent-a" + recovered = service.apply(proposal_id)["proposal"] + assert recovered["status"] == "applied" + assert recovered["receipt"]["canonical_receipt"] == created + assert _snapshot(runtime) == assigned + + +@pytest.mark.parametrize("kind", ["todo.create", "monitor.create"]) +def test_uncommitted_confirmation_rejects_changed_basis(confirmation, monkeypatch, kind): + service, registry, runtime, _ = confirmation + proposal_id = _preview(service, kind, "unfinished") + original_save = service.store.save_checkpoint + + def save(proposal_id, *, step, receipt, **kwargs): + saved = original_save(proposal_id, step=step, receipt=receipt, **kwargs) + if step == "todo_create_prepared": + raise OSError("interrupted before provider commit") + return saved + + with monkeypatch.context() as faults: + faults.setattr(service.store, "save_checkpoint", save) + with pytest.raises(OSError, match="before provider commit"): + service.apply(proposal_id) + assert not _snapshot(runtime)["todos"] + if kind == "todo.create": + add_goal_todo(registry_path=registry, goal_id="goal-a", role="agent", text="Independent Todo") + else: + registry.write_text(registry.read_text() + "\n") + before_retry = _snapshot(runtime) + assert service.apply(proposal_id)["proposal"]["status"] == "stale" + assert _snapshot(runtime) == before_retry + + +def test_concurrent_monitor_confirmation_uses_one_schedule(confirmation, monkeypatch): + service, _, runtime, _ = confirmation + proposal_id = _preview(service, "monitor.create", "concurrent") + now = datetime.now(timezone.utc) + clock = iter([now, now + timedelta(minutes=5)]) + monkeypatch.setattr("loopx.chat_actions.now_utc", lambda: next(clock)) + prepare = service.store.save_checkpoint + barrier = Barrier(2) + + def save(proposal_id, *, step, receipt, **kwargs): + if step == "todo_create_prepared": + barrier.wait(timeout=15) + return prepare(proposal_id, step=step, receipt=receipt, **kwargs) + + monkeypatch.setattr(service.store, "save_checkpoint", save) + with ThreadPoolExecutor(max_workers=2) as pool: + results = list(pool.map(service.apply, (proposal_id, proposal_id))) + assert results[0]["proposal"]["receipt"] == results[1]["proposal"]["receipt"] + todos = _snapshot(runtime)["todos"] + assert len(todos) == 1 and todos[0]["next_due_at"] + + +def test_retry_never_writes_a_replacement_runtime(confirmation, monkeypatch, tmp_path): + service, registry, runtime, provider = confirmation + proposal_id = _preview(service, "todo.create", "source-bound") + _interrupt_after_create(service, monkeypatch, "todo.create") + with pytest.raises(OSError, match="after provider commit"): + service.apply(proposal_id) + after_commit = _snapshot(runtime) + alternate = tmp_path / "alternate" + alternate.mkdir() + _, alternate_runtime, _ = promoted_create_fixture(alternate, provider=provider) + alternate_before = _snapshot(alternate_runtime) + source = registry.read_text() + registry.write_text(json.dumps({**json.loads(source), "common_runtime_root": str(alternate_runtime)})) + with pytest.raises(ValueError, match="source changed"): + service.apply(proposal_id) + assert _snapshot(alternate_runtime) == alternate_before + registry.write_text(source) + assert service.apply(proposal_id)["proposal"]["status"] == "applied" + assert _snapshot(runtime) == after_commit diff --git a/tests/control_plane_ts/todo_create_reviewed.test.ts b/tests/control_plane_ts/todo_create_reviewed.test.ts index d69128580a..7b106d85a7 100644 --- a/tests/control_plane_ts/todo_create_reviewed.test.ts +++ b/tests/control_plane_ts/todo_create_reviewed.test.ts @@ -100,3 +100,26 @@ test("reviewed create wire requires a revision before accessing a provider", asy assert.match(String(result.reason), /expected provider revision/); } }); + +test("source-bound create requires v3 and a valid registry digest before accessing a provider", async () => { + let opened = false; + const dependency = {createStore: () => { + opened = true; + throw new Error("must not access a provider"); + }}; + const v2 = await createLocalCoordinationTodo({ + schema_version: "loopx_local_coordination_todo_create_request_v2", + expected_provider_revision: "reviewed", expected_registry_sha256: "a".repeat(64), + }, dependency); + assert.equal(v2.status, "failed"); + assert.match(String(v2.reason), /expected_registry_sha256 requires Todo create request v3/); + for (const expected_registry_sha256 of [undefined, null, "short", 1]) { + const v3 = await createLocalCoordinationTodo({ + schema_version: "loopx_local_coordination_todo_create_request_v3", + expected_provider_revision: "reviewed", expected_registry_sha256, + }, dependency); + assert.equal(v3.status, "failed"); + assert.match(String(v3.reason), /expected_registry_sha256 must be a SHA-256 digest/); + } + assert.equal(opened, false); +}); From 20705491d40a17942b05a35011b21718055f2727 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:44:11 +0800 Subject: [PATCH 05/14] fix(chat): recover pending Todo and Monitor display delivery Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../goal-workspace-work.tsx | 12 ++- .../src/features/personal-workspace/i18n.tsx | 4 + .../personal-workspace-page.tsx | 15 ++- docs/project-agent-todo-contract.md | 5 + .../native_chat/external_conversations.py | 11 +-- .../native_chat/steward_commands.py | 18 +++- loopx/chat.py | 9 +- loopx/chat/todo_actions.py | 38 +++++++- loopx/chat_action_store.py | 11 ++- loopx/chat_actions.py | 41 +++++++- .../presentation/action_review_plan.ts | 27 +++++- .../test_chat_create_confirmation_recovery.py | 95 +++++++++++++++++++ .../action_review_plan.test.ts | 24 +++++ tests/test_chat_external_todo_confirmation.py | 35 +++++++ 14 files changed, 320 insertions(+), 25 deletions(-) diff --git a/apps/presentation/dashboard/src/features/personal-workspace/goal-workspace-work.tsx b/apps/presentation/dashboard/src/features/personal-workspace/goal-workspace-work.tsx index 794ca4ae2c..36e4cdd97f 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/goal-workspace-work.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/goal-workspace-work.tsx @@ -20,7 +20,7 @@ function AddTask({goal, proposals, onPreview, onSelect}: { onPreview: (request: WorkspaceActionPreviewRequest) => Promise; onSelect: (selection: WorkspaceDrawerSelection) => void; }) { - const {locale} = useWorkspaceI18n(); + const {locale, t} = useWorkspaceI18n(); const zh = locale === "zh-CN"; const [open, setOpen] = useState(false); const [text, setText] = useState(""); @@ -29,20 +29,22 @@ function AddTask({goal, proposals, onPreview, onSelect}: { const submitting = useRef(false); const [reviewed, setReviewed] = useState<{text: string; id: string} | null>(null); const proposal = reviewed ? proposals[reviewed.id] : undefined; + const pendingDisplay = proposal?.reviewPlan?.reason === "canonical_create_projection_pending" ? proposal : null; useEffect(() => { if (!reviewed || proposal) return; const successor = Object.values(proposals).find(item => item.regeneratedFrom === reviewed.id); if (successor) setReviewed({...reviewed, id: successor.previewId}); }, [reviewed, proposal, proposals]); useEffect(() => { - if (proposal?.status !== "applied") return; + if (proposal?.status !== "applied" && !pendingDisplay) return; setText(current => current === reviewed?.text ? "" : current); - setReviewed(null); - }, [proposal?.status, reviewed]); + if (proposal?.status === "applied") setReviewed(null); + }, [pendingDisplay, proposal?.status, reviewed]); const textLength = boundedLength(text); const tooLong = textLength > todoTextLimit; return
+ {pendingDisplay ? : null} {open ?
{ event.preventDefault(); if (submitting.current || textLength === 0 || tooLong) return; @@ -58,7 +60,7 @@ function AddTask({goal, proposals, onPreview, onSelect}: {