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
15 changes: 13 additions & 2 deletions loopx/bootstrap_command_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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,
Expand Down
42 changes: 36 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,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"

Expand Down Expand Up @@ -101,17 +103,42 @@ 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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] 请登记新增直接 registry I/O。相同依赖下 semantic-vocabulary-drift-smoke 在 base 通过、此 head 因 _global_registration_projection::direct_json_read:read_json#1 未分类失败,standard premerge 因此阻断。请审核并补齐 project_registry_io_classifications_v1.json 与生成清单,再重跑检查。

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 +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 {
Expand All @@ -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"],
}


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

Expand Down Expand Up @@ -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"
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