Repository navigation
feat(cli): discover registered Goals with scoped search and paging #9431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
huangruiteng
merged 3 commits into
loopx-project:main
from
steven-kid:roadmap-auto/20261125-registry-goal-discovery
Nov 26, 2026
+334
−2
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """Optional display selection over one complete registry inspection.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| import unicodedata | ||
|
|
||
|
|
||
| def _search_text(value: object) -> str: | ||
| return unicodedata.normalize("NFKC", str(value or "")).casefold() | ||
|
|
||
|
|
||
| def select_registry_goals( | ||
| payload: dict[str, object], | ||
| *, | ||
| goal_id: str | None, | ||
| query: str | None, | ||
| limit: int | None, | ||
| offset: int | None, | ||
| ) -> dict[str, object]: | ||
| if goal_id is None and query is None and limit is None and offset is None: | ||
| return payload | ||
| page_size = 20 if limit is None else limit | ||
| start = 0 if offset is None else offset | ||
| if page_size < 1 or start < 0: | ||
| raise ValueError("--limit must be positive and --offset must be non-negative") | ||
| if goal_id is not None and not goal_id.strip(): | ||
| raise ValueError("--goal-id must not be empty") | ||
| term = _search_text(query).strip() if query is not None else None | ||
| if term is not None and not term: | ||
| raise ValueError("--query must contain non-whitespace text") | ||
| # An unavailable source must retain its original failure, not become an | ||
| # apparently successful empty search or an invented missing-Goal error. | ||
| if "goals" not in payload: | ||
| return payload | ||
| raw_goals = payload.get("goals") | ||
| goals = ( | ||
| [row for row in raw_goals if isinstance(row, Mapping)] | ||
| if isinstance(raw_goals, list) | ||
| else [] | ||
| ) | ||
| if goal_id is not None and not any(row.get("id") == goal_id for row in goals): | ||
| raise ValueError(f"Goal is not present in this registry: {goal_id}") | ||
| matches = [ | ||
| row | ||
| for row in goals | ||
| if (goal_id is None or row.get("id") == goal_id) | ||
| and ( | ||
| term is None | ||
| or any( | ||
| term in _search_text(row.get(field)) | ||
| for field in ("id", "repo", "domain", "status", "adapter_kind") | ||
| ) | ||
| ) | ||
| ] | ||
| selected = matches[start : start + page_size] | ||
| end = start + len(selected) | ||
| # Keep global health/provenance and collision diagnostics intact. Selection | ||
| # narrows the displayed Goal rows, never the inspection or its authority. | ||
| return { | ||
| **payload, | ||
| "goals": selected, | ||
| "selection": { | ||
| "goal_id": goal_id, | ||
| "query": query, | ||
| "offset": start, | ||
| "limit": page_size, | ||
| "matched_goal_count": len(matches), | ||
| "returned_goal_count": len(selected), | ||
| "next_offset": end if end < len(matches) else None, | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| """Real CLI discovery selects after full inspection without hiding diagnostics.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import os | ||
| from pathlib import Path | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| from loopx.registry import inspect_registry | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def registry_path(tmp_path: Path) -> Path: | ||
| goals = [] | ||
| for index in range(31): | ||
| repo = tmp_path / f"project-{index:02d}" | ||
| repo.mkdir() | ||
| (repo / "STATE.md").write_text("# Synthetic Goal\n", encoding="utf-8") | ||
| goals.append( | ||
| { | ||
| "id": f"{'target' if index in (3, 17, 28) else 'other'}-{index:02d}", | ||
| "repo": str(repo), | ||
| "state_file": "STATE.md", | ||
| "domain": "software", | ||
| "status": "active", | ||
| "adapter": {"kind": "read_only_project_map_v0"}, | ||
| } | ||
| ) | ||
| path = tmp_path / "registry.json" | ||
| path.write_text( | ||
| json.dumps( | ||
| { | ||
| "schema_version": "0.1", | ||
| "goals": goals, | ||
| "common_runtime_root": str(tmp_path / "runtime"), | ||
| } | ||
| ), | ||
| encoding="utf-8", | ||
| ) | ||
| return path | ||
|
|
||
|
|
||
| def read_registry(path, *arguments, markdown=False): | ||
| before = {p: p.read_bytes() for p in path.parent.rglob("*") if p.is_file()} | ||
| result = subprocess.run( | ||
| [ | ||
| sys.executable, | ||
| "-m", | ||
| "loopx.entrypoint", | ||
| "--registry", | ||
| str(path), | ||
| "--format", | ||
| "markdown" if markdown else "json", | ||
| "registry", | ||
| *arguments, | ||
| ], | ||
| text=True, | ||
| capture_output=True, | ||
| timeout=60, | ||
| env={**os.environ, "LOOPX_USAGE_PING": "0"}, | ||
| ) | ||
| assert {p: p.read_bytes() for p in path.parent.rglob("*") if p.is_file()} == before | ||
| return result, result.stdout if markdown else json.loads(result.stdout) | ||
|
|
||
|
|
||
| def test_query_filters_before_paging_and_defaults_remain_complete(registry_path): | ||
| result, full = read_registry(registry_path) | ||
| assert result.returncode == 0, result.stderr | ||
| assert full == inspect_registry(registry_path) | ||
| assert len(full["goals"]) == 31 and "selection" not in full | ||
| result, first = read_registry(registry_path, "--query", "TARGET", "--limit", "2") | ||
| assert result.returncode == 0 | ||
| assert [g["id"] for g in first["goals"]] == ["target-03", "target-17"] | ||
| assert first["goal_count"] == 31 | ||
| assert first["selection"] == { | ||
| "goal_id": None, | ||
| "query": "TARGET", | ||
| "offset": 0, | ||
| "limit": 2, | ||
| "matched_goal_count": 3, | ||
| "returned_goal_count": 2, | ||
| "next_offset": 2, | ||
| } | ||
| _, second = read_registry( | ||
| registry_path, "--query", "TARGET", "--limit", "2", "--offset", "2" | ||
| ) | ||
| assert [g["id"] for g in second["goals"]] == ["target-28"] | ||
| assert second["selection"]["next_offset"] is None | ||
| _, beyond = read_registry(registry_path, "--query", "TARGET", "--offset", "50") | ||
| assert beyond["goals"] == [] and beyond["selection"]["matched_goal_count"] == 3 | ||
|
|
||
|
|
||
| def test_exact_goal_and_query_compose_without_prefix_match(registry_path): | ||
| _, payload = read_registry( | ||
| registry_path, "--goal-id", "target-17", "--query", "PROJECT-17" | ||
| ) | ||
| assert [g["id"] for g in payload["goals"]] == ["target-17"] | ||
| _, empty = read_registry( | ||
| registry_path, "--goal-id", "target-17", "--query", "project-03" | ||
| ) | ||
| assert empty["ok"] and empty["goals"] == [] | ||
| result, missing = read_registry(registry_path, "--goal-id", "target-1") | ||
| assert result.returncode == 1 and missing["ok"] is False | ||
|
|
||
|
|
||
| def test_selection_keeps_unrelated_registry_problems_visible(registry_path): | ||
| raw = json.loads(registry_path.read_text()) | ||
| raw["goals"][0].pop("state_file") | ||
| registry_path.write_text(json.dumps(raw)) | ||
| _, full = read_registry(registry_path) | ||
| result, selected = read_registry(registry_path, "--goal-id", "target-17") | ||
| assert result.returncode == 1 and selected["ok"] is False | ||
| assert [g["id"] for g in selected["goals"]] == ["target-17"] | ||
| for field in ("problems", "problem_diagnostics", "status_counts", "goal_count"): | ||
| assert selected[field] == full[field] | ||
| result, markdown = read_registry( | ||
| registry_path, "--goal-id", "target-17", markdown=True | ||
| ) | ||
| assert result.returncode == 1 | ||
| assert "entire registry" in markdown and "missing state_file" in markdown | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "arguments", | ||
| [ | ||
| ["--query", " "], | ||
| ["--goal-id", ""], | ||
| ["--limit", "0"], | ||
| ["--limit", "-1"], | ||
| ["--offset", "-1"], | ||
| ], | ||
| ) | ||
| def test_invalid_selection_reports_failure(registry_path, arguments): | ||
| result, payload = read_registry(registry_path, *arguments) | ||
| assert result.returncode == 1 and payload["ok"] is False and payload["error"] | ||
|
|
||
|
|
||
| def test_unicode_and_literal_search_and_default_page_size(registry_path): | ||
| raw = json.loads(registry_path.read_text()) | ||
| raw["goals"][17]["domain"] = "研究[候选]" | ||
| registry_path.write_text(json.dumps(raw, ensure_ascii=False)) | ||
| _, payload = read_registry(registry_path, "--query", "研究[") | ||
| assert [g["id"] for g in payload["goals"]] == ["target-17"] | ||
| _, normalized = read_registry(registry_path, "--query", "TARGET") | ||
| assert normalized["selection"]["matched_goal_count"] == 3 | ||
| _, default_page = read_registry(registry_path, "--offset", "0") | ||
| assert len(default_page["goals"]) == 20 | ||
| assert default_page["selection"]["next_offset"] == 20 | ||
|
|
||
|
|
||
| def test_unavailable_registry_keeps_original_error(tmp_path): | ||
| result, payload = read_registry(tmp_path / "absent.json", "--query", "target") | ||
| assert result.returncode == 1 and payload["error"] == "registry file does not exist" | ||
| assert "selection" not in payload | ||
|
|
||
|
|
||
| def test_unknown_goal_keeps_global_diagnostics_in_both_formats(registry_path): | ||
| raw = json.loads(registry_path.read_text()) | ||
| raw["goals"][0].pop("state_file") | ||
| registry_path.write_text(json.dumps(raw)) | ||
| _, full = read_registry(registry_path) | ||
| result, selected = read_registry(registry_path, "--goal-id", "unknown-goal") | ||
| assert result.returncode == 1 and selected["ok"] is False | ||
| assert selected["error"] == "Goal is not present in this registry: unknown-goal" | ||
| assert selected["goals"] == [] | ||
| for field in ("problems", "problem_diagnostics", "status_counts", "goal_count"): | ||
| assert selected[field] == full[field] | ||
| result, markdown = read_registry( | ||
| registry_path, "--goal-id", "unknown-goal", markdown=True | ||
| ) | ||
| assert result.returncode == 1 | ||
| assert selected["error"] in markdown | ||
| assert "- goals: `31`" in markdown | ||
| assert "missing state_file" in markdown | ||
|
|
||
|
|
||
| def test_unreadable_registry_selection_keeps_short_original_error(tmp_path): | ||
| path = tmp_path / "registry.json" | ||
| _, original = read_registry(path) | ||
| result, selected = read_registry(path, "--goal-id", "unknown-goal") | ||
| assert result.returncode == 1 and selected == original | ||
| result, markdown = read_registry(path, "--goal-id", "unknown-goal", markdown=True) | ||
| assert result.returncode == 1 | ||
| assert original["error"] in markdown | ||
| assert "Goal is not present" not in markdown | ||
| assert "- goals:" not in markdown and "## Problems" not in markdown | ||
|
|
||
|
|
||
| def test_global_registry_selection_does_not_follow_source_and_keeps_duplicate_diagnostics( | ||
| registry_path, | ||
| ): | ||
| raw = json.loads(registry_path.read_text()) | ||
| raw["registry_role"] = "global-local" | ||
| source = registry_path.parent / "source.json" | ||
| source.write_text("not a registry") | ||
| raw["goals"][17]["source_registry"] = str(source) | ||
| raw["goals"].append(dict(raw["goals"][17])) | ||
| registry_path.write_text(json.dumps(raw)) | ||
| _, full = read_registry(registry_path) | ||
| result, selected = read_registry( | ||
| registry_path, "--goal-id", "target-17", "--limit", "1" | ||
| ) | ||
| assert result.returncode == 1 | ||
| assert selected["problems"] == full["problems"] | ||
| assert selected["selection"]["matched_goal_count"] == 2 | ||
| assert selected["selection"]["next_offset"] == 1 | ||
| assert len(selected["goals"]) == 1 | ||
| assert selected["goal_count"] == 32 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.