diff --git a/docs/architecture/code-index-architecture.md b/docs/architecture/code-index-architecture.md index 1f66d3bcb0..8d767d5137 100644 --- a/docs/architecture/code-index-architecture.md +++ b/docs/architecture/code-index-architecture.md @@ -354,12 +354,14 @@ remaining deadline. Presence queries and uncovered paths carry none; `code.enclosing` carries the same file lessons for resolved locations. Repeated locations share one file read; unresolved locations bring no lessons. The caller's readable memory namespaces, narrowed-token refusal, timeout and token cap apply -through the shared result enrichment. Empty lesson fields stay off the wire. +through the shared result enrichment. Resolved symbol names rank matching lessons +after constraints and pins; unresolved names contribute no query terms. Empty +lesson fields stay off the wire. Search and grep rank candidate lessons across all selected files by query-term overlap after constraints and pinned rows. English inflections share stems; equal matches keep path order, then the listing's confidence and recency order. -Other code reads retain path and listing order. +The remaining file-lesson reads retain path and listing order. This reorders the same readable candidates within the same response token budget; it adds no model call or storage read. Superseded rows and unrelated paths remain excluded. diff --git a/server/code/app/result_lessons.py b/server/code/app/result_lessons.py index dc66670453..1898ab2971 100644 --- a/server/code/app/result_lessons.py +++ b/server/code/app/result_lessons.py @@ -19,6 +19,7 @@ from typing import Any from server.code.api.models_grep import CodeGrepInput +from server.code.api.models_navigation import CodeEnclosingInput from server.code.api.models_search import CodeSearchInput from server.code.app import common, enrich, memory_links from server.code.app.search_legs import resolve_deadline_ms @@ -62,7 +63,9 @@ def _bound_s(input: Any, started: float) -> float: return bound -async def lessons_for(input: Any, paths: Iterable[str], started: float) -> path_lessons.Lessons: +async def lessons_for( + input: Any, paths: Iterable[str], started: float, *, query: str = "" +) -> path_lessons.Lessons: """The completed lessons for the answer's top `paths`; none when off, narrowed, past the call's deadline, or unable to read the scope. `started` is the call's monotonic start.""" @@ -103,7 +106,7 @@ async def scope() -> tuple[str, list[str], str]: if isinstance(input, CodeSearchInput) else input.pattern if isinstance(input, CodeGrepInput) - else "" + else query ), ) except Exception as e: # noqa: BLE001 — lesson enrichment must not fail the code answer @@ -123,7 +126,8 @@ def __init__(self, handler: Handler, paths_of: PathsOf) -> None: async def __call__(self, input: Any) -> Any: started = time.monotonic() out = await self.handler(input) - found = await lessons_for(input, self.paths_of(input, out), started) + query = _enclosing_query(out) if isinstance(input, CodeEnclosingInput) else "" + found = await lessons_for(input, self.paths_of(input, out), started, query=query) out.lessons, out.lessons_left_out = found.rows, found.left_out return out @@ -167,6 +171,17 @@ def enclosing_paths(_input: Any, out: Any) -> list[str]: return [str(row.get("path") or "") for row in out.results if row.get("start_line")] +def _enclosing_query(out: Any) -> str: + # Resolved names distinguish the code being read from other code in the same file. + return " ".join( + dict.fromkeys( + str(row.get("symbol") or row.get("qualified_name") or "") + for row in out.results + if row.get("start_line") + ) + ) + + def pack_paths(_input: Any, out: Any) -> list[str]: # a task briefing's files; the orientation map and a PR's stored pack name none return list(out._paths) diff --git a/tests/integration/test_code_result_lessons.py b/tests/integration/test_code_result_lessons.py index 0a3a4eb52d..3016ef472b 100644 --- a/tests/integration/test_code_result_lessons.py +++ b/tests/integration/test_code_result_lessons.py @@ -99,7 +99,8 @@ async def test_enclosing_attaches_only_authorized_resolved_file_lessons( ) -> None: org = unique_org("enclosing-lessons") writer = mint_test_token(org=org, namespaces=["*"], roles=["writer"]) - _set(org, control_plane_url, writer, "notes/allowed", "Keep callback order.", [PATH]) + _set(org, control_plane_url, writer, "notes/allowed", "Keep run callback order.", [PATH]) + _set(org, control_plane_url, writer, "notes/unrelated", "Storage configuration.", [PATH]) _set(org, control_plane_url, writer, "notes/missing", "Unresolved file note.", ["absent.py"]) ch_client.insert( "loom.code_symbols", @@ -136,7 +137,11 @@ async def test_enclosing_attaches_only_authorized_resolved_file_lessons( data = payload.get("data", payload) assert data["resolved"] == 2 and len(data["results"]) == 3 if include and "dev" in names: - assert [row["memory_key"] for row in data["lessons"]] == ["notes/allowed"] + assert data["lessons"][0]["memory_key"] == "notes/allowed" + assert {row["memory_key"] for row in data["lessons"]} == { + "notes/allowed", + "notes/unrelated", + } assert data["lessons"][0]["path"] == PATH else: assert "lessons" not in data diff --git a/tests/unit/test_code_result_lessons.py b/tests/unit/test_code_result_lessons.py index 31692e31d6..1a7dcdb98a 100644 --- a/tests/unit/test_code_result_lessons.py +++ b/tests/unit/test_code_result_lessons.py @@ -771,11 +771,27 @@ async def enclosing(inp: CodeEnclosingInput) -> CodeEnclosingOutput: resolved = skip != "unresolved" return CodeEnclosingOutput( results=[ - {"path": "src/a.py", "start_line": 1 if resolved else 0, "end_line": 5}, - {"path": "src/a.py", "start_line": 2 if resolved else 0, "end_line": 5}, - {"path": "missing.py", "start_line": 0, "end_line": 0}, + { + "path": "src/a.py", + "start_line": 1 if resolved else 0, + "end_line": 5, + "symbol": "cleanup", + }, + { + "path": "src/a.py", + "start_line": 2 if resolved else 0, + "end_line": 5, + "qualified_name": "pkg::finish", + }, + { + "path": "src/a.py", + "start_line": 1 if resolved else 0, + "end_line": 5, + "symbol": "cleanup", + }, + {"path": "missing.py", "start_line": 0, "end_line": 0, "symbol": "missing"}, ], - resolved=2 if resolved else 0, + resolved=3 if resolved else 0, paths_indexed=1, paths_total=2, took_ms=0, @@ -786,17 +802,18 @@ async def enclosing(inp: CodeEnclosingInput) -> CodeEnclosingOutput: CodeEnclosingInput( org="acme", namespace="code-repo", - locations=["src/a.py:2", "src/a.py:3", "missing.py:1"], + locations=["src/a.py:2", "src/a.py:3", "src/a.py:4", "missing.py:1"], lesson_namespaces=["mem-home", "denied"], include_lessons=skip != "argument", ) ) - assert len(out.results) == 3 + assert len(out.results) == 4 if skip != "none": assert calls == [] and "lessons" not in out.model_dump() else: assert calls[0]["paths"] == ["src/a.py"] assert calls[0]["nss"] == ["mem-home", "code-repo"] + assert calls[0]["query"] == "cleanup pkg::finish" assert out.lessons[0]["namespace"] == "mem-home"