diff --git a/loopx/bootstrap_command_pack.py b/loopx/bootstrap_command_pack.py index 8451873ba5..a355c0e4dd 100644 --- a/loopx/bootstrap_command_pack.py +++ b/loopx/bootstrap_command_pack.py @@ -50,7 +50,7 @@ render_refresh_state_command, shell_arg_for_host, ) -from .paths import resolve_runtime_root +from .paths import global_registry_path, resolve_runtime_root from .registry import registry_goals, resolve_state_file from .slash_commands import build_slash_command_catalog from .thread_agent_binding import normalize_thread_id, resolve_thread_agent_binding @@ -608,6 +608,7 @@ def inspect_bootstrap_connection( def _inspect_bootstrap_connection( project: Path, *, goal_id: str | None, resolve_linked_worktree_alias: bool, + runtime_root_arg: str | None = None, ) -> _BootstrapConnectionSnapshot: """Capture the connection and the registry used to build it together.""" input_project = _resolve_project(project) @@ -638,9 +639,14 @@ def _inspect_bootstrap_connection( "registry": str(registry_path), } + shared_registry = global_registry_path(resolve_runtime_root( + registry or {}, override=runtime_root_arg, registry_path=registry_path, + )) + if registry_error: return absent_goal_connection( base_connection=base_connection, + shared_registry=shared_registry, goal_id=inferred_goal_id, state_file=state_file, registry_exists=registry_exists, @@ -651,6 +657,7 @@ def _inspect_bootstrap_connection( if not registry: return absent_goal_connection( base_connection=base_connection, + shared_registry=shared_registry, goal_id=inferred_goal_id, state_file=state_file, registry_exists=False, @@ -672,6 +679,7 @@ def _inspect_bootstrap_connection( if selected_goal is None: return absent_goal_connection( base_connection=base_connection, + shared_registry=shared_registry, goal_id=resolved_goal_id, state_file=state_file, registry_exists=True, @@ -818,6 +826,7 @@ def build_loopx_bootstrap_command_pack( ) -> dict[str, Any]: inspection, registry_payload = _connection_snapshot or _inspect_bootstrap_connection( project, goal_id=goal_id, resolve_linked_worktree_alias=resolve_linked_worktree_alias, + runtime_root_arg=runtime_root_arg, ) resolved_project = str(inspection["project"]) resolved_goal_id = str(inspection["goal_id"]) @@ -1449,7 +1458,9 @@ def build_start_goal_guided_packet( display_name: str | None = None, runtime_root_arg: str | None = None, ) -> dict[str, Any]: - snapshot = _inspect_bootstrap_connection(project, goal_id=goal_id, resolve_linked_worktree_alias=False) + snapshot = _inspect_bootstrap_connection( + project, goal_id=goal_id, resolve_linked_worktree_alias=False, runtime_root_arg=runtime_root_arg, + ) if goal_id is None: selection_packet = _build_multi_goal_start_selection_packet( connection_snapshot=snapshot, diff --git a/loopx/control_plane/goals/orphaned_goal_state.py b/loopx/control_plane/goals/orphaned_goal_state.py index 3a09dc83e0..3ee3a19092 100644 --- a/loopx/control_plane/goals/orphaned_goal_state.py +++ b/loopx/control_plane/goals/orphaned_goal_state.py @@ -1,4 +1,4 @@ -"""Guard the goal-start flow when Goal state outlives its registry entry. +"""Guard goal start when Goal state or its shared route outlives its registry entry. A project ``ACTIVE_GOAL_STATE.md`` for a goal id the project registry no longer declares is not the ordinary "nothing to connect yet" case: continuing would hand @@ -18,7 +18,9 @@ from pathlib import Path from typing import Any +from ...global_registry import same_source_registry from ...project_prompt import shell_arg +from ...registry import read_json, registry_goals ACTIVE_GOAL_STATE_FILENAME = "ACTIVE_GOAL_STATE.md" @@ -101,9 +103,34 @@ def orphaned_goal_state_projection(project: Path, goal_id: str) -> dict[str, Any } +def _global_registration_projection( + shared_registry: Path, source_registry: str, goal_id: str, +) -> dict[str, Any] | None: + try: + global_payload = read_json(shared_registry) + except (OSError, ValueError): + return None + source = {"source_registry": source_registry} + if not any( + item.get("id") == goal_id and same_source_registry(item, source) + for item in registry_goals(global_payload) + ): + return None + return { + "schema_version": ORPHANED_GOAL_STATE_GATE_SCHEMA_VERSION, + "goal_id": goal_id, + "state_file_routes": [], + "reason": ( + "the shared registry still records this Goal for this project. " + "Restore the original project registry entry before retrying." + ), + } + + def absent_goal_connection( *, base_connection: dict[str, Any], + shared_registry: Path, goal_id: str, state_file: Path, registry_exists: bool, @@ -111,7 +138,7 @@ def absent_goal_connection( absence_reason: str, known_goal_ids: list[str] | None = None, ) -> dict[str, Any]: - """Project "no registry entry carries this goal", fenced if its state survives. + """Project a missing registration, fenced if its state or shared route survives. This is the only way the goal-start flow states an absence, so an unparseable registry file, a missing or empty one, and a readable registry with no @@ -137,6 +164,8 @@ def absent_goal_connection( # working directory, which is not a project this packet may speak for. project = str(base_connection.get("project") or "") projection = orphaned_goal_state_projection(Path(project), goal_id) if project else None + if projection is None: + projection = _global_registration_projection(shared_registry, str(connection["registry"]), goal_id) if projection is None: return connection return { @@ -148,7 +177,7 @@ def absent_goal_connection( "absent_connection_state": absence_connection, "absent_reason": absence_reason, "bootstrap_continuation_allowed": False, - "reason": ORPHANED_GOAL_STATE_REASON, + "reason": projection["reason"], } @@ -221,7 +250,7 @@ def fence_command_pack(command_pack: dict[str, Any], *, command_prefix: str) -> command_pack["recommended_next_step"] = { "kind": "resolve_orphaned_goal_state", "requires_user_confirmation": True, - "summary": ORPHANED_GOAL_STATE_REASON, + "summary": gate["reason"], "orphaned_goal_state_gate": gate, } @@ -291,11 +320,12 @@ def render_guided_lines(transaction: dict[str, Any]) -> str: if not isinstance(gate, dict): return "" routes = _preview_route_lines(gate) + state_files = ', '.join(f'`{route}`' for route in gate.get('state_file_routes') or []) return ( "\n## Orphaned Goal State Gate\n\n" f"{gate.get('reason')}\n\n" - f"- orphaned state: {', '.join(f'`{route}`' for route in gate.get('state_file_routes') or [])}\n" - f"- blocked until resolved: {', '.join(f'`{name}`' for name in gate.get('forbidden_until_resolved') or [])}\n" + + (f"- orphaned state: {state_files}\n" if state_files else "") + + f"- blocked until resolved: {', '.join(f'`{name}`' for name in gate.get('forbidden_until_resolved') or [])}\n" f"- {gate.get('execution_boundary')}\n\n" + routes + "\n" diff --git a/tests/control_plane/test_start_goal_orphaned_goal_state.py b/tests/control_plane/test_start_goal_orphaned_goal_state.py index 99f9552530..041cc0f7a6 100644 --- a/tests/control_plane/test_start_goal_orphaned_goal_state.py +++ b/tests/control_plane/test_start_goal_orphaned_goal_state.py @@ -217,6 +217,43 @@ def test_guided_packet_offers_only_previews_over_orphaned_state( assert contract["mutation_commands_are_previewed"] is True +@pytest.mark.parametrize("registry", ["missing", "registered"]) +@pytest.mark.parametrize("same_source", [True, False]) +def test_guided_connect_checks_retained_global_registration( + tmp_path: Path, registry: str, same_source: bool, +) -> None: + project = _project(tmp_path, registry=registry) + runtime_root = tmp_path / "shared" + runtime_root.mkdir() + global_registry = runtime_root / "registry.global.json" + source_project = project if same_source else tmp_path / "other" + recorded = json.dumps({"goals": [{ + "id": ORPHANED_GOAL_ID, + "repo": str(source_project), + "source_registry": str(source_project / ".loopx/registry.json"), + }]}) + global_registry.write_text(recorded, encoding="utf-8") + + payload = build_start_goal_guided_packet( + project=project, + goal_id=ORPHANED_GOAL_ID, + agent_id=None, + cli_bin="loopx", + host_surface="codex-app", + goal_text=GOAL_TEXT, + runtime_root_arg=str(runtime_root), + ) + + steps = payload["guided_transaction"]["ordered_steps"] + if same_source: + assert [step["id"] for step in steps] == ["inspect_connection", "resolve_orphaned_goal_state"] + assert "Restore the original project registry entry" in payload["message"] + assert payload["command_pack"]["commands"]["goal_start_connect_if_needed"] is None + else: + assert any(step["id"] == "connect_if_needed" for step in steps) + assert global_registry.read_text(encoding="utf-8") == recorded + + def test_guided_packet_carries_no_bootstrap_or_todo_authoring_continuation( tmp_path: Path, ) -> None: