Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion loopx/bootstrap_command_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,15 @@ def _inspect_bootstrap_connection(
"registry": str(registry_path),
}

def shared_registry_path() -> Path:
return global_registry_path(resolve_runtime_root(
registry or {}, override=runtime_root, registry_path=registry_path,
))

if registry_error:
return absent_goal_connection(
base_connection=base_connection,
shared_registry=shared_registry_path(),
goal_id=inferred_goal_id,
state_file=state_file,
registry_exists=registry_exists,
Expand All @@ -731,6 +737,7 @@ def _inspect_bootstrap_connection(
if not registry:
return absent_goal_connection(
base_connection=base_connection,
shared_registry=shared_registry_path(),
goal_id=inferred_goal_id,
state_file=state_file,
registry_exists=False,
Expand All @@ -752,6 +759,7 @@ def _inspect_bootstrap_connection(
if selected_goal is None:
return absent_goal_connection(
base_connection=base_connection,
shared_registry=shared_registry_path(),
goal_id=resolved_goal_id,
state_file=state_file,
registry_exists=True,
Expand Down Expand Up @@ -1519,7 +1527,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 = resolve_bootstrap_project_route(
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,
Expand Down
44 changes: 38 additions & 6 deletions loopx/control_plane/goals/orphaned_goal_state.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,7 +18,10 @@
from pathlib import Path
from typing import Any

from ...global_registry import same_source_registry
from ...project_prompt import shell_arg
from ...registry import registry_goals
from ..projects.registry_codec import load_registry

ACTIVE_GOAL_STATE_FILENAME = "ACTIVE_GOAL_STATE.md"

Expand Down Expand Up @@ -101,17 +104,43 @@ 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:
"""Project a matching route when the shared registry is readable."""
try:
global_payload = load_registry(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,
absence_connection: str,
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
Expand All @@ -137,6 +166,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 {
Expand All @@ -148,7 +179,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"],
}


Expand Down Expand Up @@ -221,7 +252,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,
}

Expand Down Expand Up @@ -291,11 +322,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"
Expand Down
37 changes: 37 additions & 0 deletions tests/control_plane/test_start_goal_orphaned_goal_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading