diff --git a/loopx/global_todos.py b/loopx/global_todos.py index 71103af9b1..11aa3d97f7 100644 --- a/loopx/global_todos.py +++ b/loopx/global_todos.py @@ -4,6 +4,10 @@ from pathlib import Path from typing import Any +from .control_plane.goals.contract_health import ( + CONTRACT_ERROR_DIAGNOSTIC_SCHEMA_VERSION, + contract_error_views, +) from .control_plane.projection_envelope_facts import ( render_projection_envelope_markdown, seal_projection_envelope, @@ -392,10 +396,50 @@ def _deduplicate_todos( return list(deduplicated.values()) +def _scoped_status_failure( + status_payload: dict[str, Any], +) -> tuple[set[str], set[str]] | None: + registry = as_dict(status_payload.get("global_registry")) + contract = as_dict(status_payload.get("contract")) + diagnostics = contract.get("error_diagnostics") + if ( + registry.get("available") is not True + or registry.get("ok") is not True + or contract.get("ok") is not False + or contract.get("global_errors") + or not isinstance(diagnostics, list) + or not diagnostics + or not all( + isinstance(row, dict) + and row.get("schema_version") == CONTRACT_ERROR_DIAGNOSTIC_SCHEMA_VERSION + for row in diagnostics + ) + ): + return None + views = contract_error_views(diagnostics) + if ( + views["global_errors"] + or views["errors"] != contract.get("errors") + or len(views["error_diagnostics"]) != len(diagnostics) + ): + return None + failed_goals = set(views["goal_errors"]) + registered_goals = { + row["id"] + for row in as_list(as_dict(status_payload.get("run_history")).get("goals")) + if isinstance(row, dict) and row.get("registry_member") is True + and isinstance(row.get("id"), str) and row["id"] + } + if not failed_goals or not failed_goals <= registered_goals: + return None + return registered_goals, failed_goals + + def _queue_goal_ids( status_payload: dict[str, Any], *, scan_limit: int, + eligible_goals: set[str] | None = None, ) -> tuple[list[str], int]: queue = as_dict(status_payload.get("attention_queue")) unique_goal_ids: list[str] = [] @@ -412,6 +456,12 @@ def _queue_goal_ids( reported_count = max(0, int(queue.get("item_count") or 0)) except (TypeError, ValueError): reported_count = 0 + # A degraded status queue can include a synthetic contract warning in + # place of the failed Goal. Only the observed registry can name read scopes. + if eligible_goals is not None: + eligible = [goal_id for goal_id in unique_goal_ids if goal_id in eligible_goals] + reported_count -= len(unique_goal_ids) - len(eligible) + unique_goal_ids = eligible available_count = max(len(unique_goal_ids), reported_count) return unique_goal_ids[:scan_limit], available_count @@ -426,6 +476,7 @@ def _todos_envelope( goal_scan_omitted: int, shown_count: int, available_count: int, + failed_status_goals: set[str] | None = None, ) -> dict[str, Any]: global_registry = as_dict(status_payload.get("global_registry")) status_envelope = as_dict(status_payload.get("projection_envelope")) @@ -467,7 +518,13 @@ def _todos_envelope( omitted.append( {"reason": "attention_goals_not_scanned", "count": goal_scan_omitted} ) - if status_payload and status_payload.get("ok") is not True: + if failed_status_goals: + omitted.append({ + "reason": "status_unavailable", + "count": len(failed_status_goals), + "refs": [_redact_text(goal_id, limit=120) for goal_id in sorted(failed_status_goals)[:8]], + }) + elif status_payload and status_payload.get("ok") is not True: omitted.append({"reason": "status_unavailable", "count": 1}) queue = status_payload.get("attention_queue") queue_read = isinstance(queue, dict) and isinstance(queue.get("items"), list) @@ -516,7 +573,7 @@ def _todos_envelope( "scope": "global", "expected_count": expected, "included_count": ( - max(0, expected - excluded - goal_scan_omitted) + max(0, expected - excluded - goal_scan_omitted - len(failed_status_goals or ())) if expected is not None else 0 ), @@ -554,21 +611,27 @@ def build_global_todos( limit=scan_limit, ) status_read_at = now_utc_iso() - if status_payload.get("ok") is not True: + status_ok = status_payload.get("ok") is True + scoped_failure = None if status_ok else _scoped_status_failure(status_payload) + registered_goals, failed_status_goals = scoped_failure or (set(), set()) + goal_ids, available_goal_count = _queue_goal_ids( + status_payload, + scan_limit=scan_limit, + eligible_goals=registered_goals - failed_status_goals if scoped_failure else None, + ) + if not status_ok and (scoped_failure is None or not goal_ids): return build_global_todos_error( "Global status source unavailable.", status_payload=status_payload, status_read_at=status_read_at, ) - - goal_ids, available_goal_count = _queue_goal_ids( - status_payload, - scan_limit=scan_limit, - ) - warnings: list[dict[str, Any]] = [] + warnings = [ + _warning("quota_unavailable", goal_id=goal_id, detail="The Goal's status contract check failed.") + for goal_id in sorted(failed_status_goals) + ] classified: list[dict[str, Any]] = [] quota_read_at = now_utc_iso() - quota_unavailable = 0 + quota_unavailable = len(failed_status_goals) for goal_id in goal_ids: quota_payloads, was_unavailable = _quotas_for_goal( status_payload, @@ -655,6 +718,7 @@ def build_global_todos( goal_scan_omitted=max(0, available_goal_count - len(goal_ids)), shown_count=returned_count, available_count=matched_count, + failed_status_goals=failed_status_goals, ), "omissions": [ ( diff --git a/tests/control_plane/test_unmigrated_goal_reads.py b/tests/control_plane/test_unmigrated_goal_reads.py index f7720c3b5f..6d4d5a52b0 100644 --- a/tests/control_plane/test_unmigrated_goal_reads.py +++ b/tests/control_plane/test_unmigrated_goal_reads.py @@ -11,6 +11,8 @@ from loopx.control_plane.coordination.legacy_writer_fence import ( legacy_coordination_writer_fence_path, ) +from loopx.control_plane.testing.canary_harness import run_json_cli_result +from canonical_authority_fixture import initialize_canonical_source_fixture, isolate_sqlite_runtime from test_cold_source_import_cli import canonical_todos, workspace @@ -66,6 +68,66 @@ def test_ordinary_cli_refuses_unmigrated_goal_without_importing_it( assert not (runtime / "authority").exists() +def test_global_todos_retains_healthy_lanes_with_a_stopped_unmigrated_goal(tmp_path, monkeypatch): + isolate_sqlite_runtime(tmp_path, monkeypatch) + runtime = tmp_path / "runtime" + runtime.mkdir() + registry = runtime / "registry.global.json" + goals = [] + state_by_goal = {} + for goal_id in ("ready", "stopped"): + project = tmp_path / goal_id + project.mkdir() + state = project / "ACTIVE_GOAL_STATE.md" + state.write_text( + "---\nstatus: active\nowner_mode: goal\n---\n\n" + "## Agent Todo\n\n- [ ] Verify the registered work.\n" + f" \n" + ) + goal = { + "id": goal_id, "repo": str(project), "state_file": state.name, "status": "active", + "domain": "software", "adapter": {"kind": "read_only_project_map_v0", "status": "connected-read-only"}, + "coordination": {"registered_agents": ["agent-a"]}, + } + if goal_id == "stopped": + goal["activation"] = { + "schema_version": "loopx_goal_activation_v1", "state": "stopped", + "updated_at": "2026-01-01T00:00:00Z", "actor_kind": "owner", "reason": "Stop this fixture.", + } + else: + initialize_canonical_source_fixture(runtime, goal_id, state_path=state, provider="sqlite") + goals.append(goal) + state_by_goal[goal_id] = state + registry.write_text(json.dumps({ + "schema_version": "0.1", "registry_role": "global-local", + "common_runtime_root": str(runtime), "goals": goals, + })) + original = registry.read_bytes(), state_by_goal["stopped"].read_bytes() + + code, status = run_json_cli_result("status", registry_path=registry) + assert code == 1 and status["ok"] is False + assert [(e["goal_id"], e["code"]) for e in status["contract"]["error_diagnostics"]] == [ + ("stopped", "canonical_authority_required"), + ] + assert any(row["goal_id"] == "loopx-contract" for row in status["attention_queue"]["items"]) + + code, observed = run_json_cli_result("global-todos", registry_path=registry) + assert code == 0 and observed["ok"] is True, observed + assert {(todo["goal_id"], todo["todo_id"]) for todo in observed["todos"]} == {("ready", "todo_ready")} + assert [(row["goal_id"], row["reason_code"]) for row in observed["source_warnings"]] == [ + ("stopped", "quota_unavailable"), + ] + envelope = observed["projection_envelope"] + assert envelope["complete"] is False + assert envelope["coverage"]["expected_count"] == 2 + assert envelope["coverage"]["included_count"] == 1 + quota = next(row for row in envelope["sources"] if row["source_id"] == "goal_quota") + assert (quota["read_status"], quota["item_count"], quota["unreadable_count"]) == ("read", 1, 1) + assert (registry.read_bytes(), state_by_goal["stopped"].read_bytes()) == original + assert not legacy_coordination_writer_fence_path(runtime_root=runtime, goal_id="stopped").exists() + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_explicit_import_retains_full_source_and_enables_ordinary_reads( tmp_path, monkeypatch, provider, diff --git a/tests/test_global_todos.py b/tests/test_global_todos.py index 1f2b578a63..a6d3c6bb6a 100644 --- a/tests/test_global_todos.py +++ b/tests/test_global_todos.py @@ -1,7 +1,13 @@ from pathlib import Path from typing import Any +import pytest + import loopx.global_todos as global_todos +from loopx.control_plane.goals.contract_health import ( + contract_error_diagnostic, + contract_error_views, +) def build_payload( @@ -137,6 +143,32 @@ def test_global_todos_error_redacts_local_paths(tmp_path) -> None: assert "" in payload["error"] +@pytest.mark.parametrize("failure", ["global_contract", "unknown_goal", "registry"]) +def test_global_todos_keeps_unverified_scope_unavailable(monkeypatch, tmp_path, failure) -> None: + diagnostic = contract_error_diagnostic(code="canonical_authority_required", + message="Unavailable canonical source.", goal_id="unknown" if failure == "unknown_goal" else "cold") + diagnostics = [diagnostic] + if failure == "global_contract": + diagnostics.append(contract_error_diagnostic(code="registry_contract", message="Global contract failed.")) + status = { + **status_for("ready"), + "ok": False, + "global_registry": {"available": True, "ok": failure != "registry"}, + "contract": {"ok": False, **contract_error_views(diagnostics)}, + "run_history": {"goals": [ + {"id": "ready", "registry_member": True}, + {"id": "cold", "registry_member": True}, + ]}, + } + calls = patch_manager_reads(monkeypatch, status=status, quotas={}) + + payload = build_payload(tmp_path) + + assert payload["ok"] is False + assert "todos" not in payload + assert calls == {"status": 1, "quota": []} + + def test_global_todos_error_survives_projection_runtime_failure(monkeypatch) -> None: monkeypatch.setattr( global_todos,