feat(7834): issues linked to the file being written arrive before anything found by meaning
CI & Build / TypeScript typecheck (push) Successful in 37s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Python tests (push) Successful in 2m53s
CI & Build / integration (push) Successful in 2m7s
CI & Build / Build & push image (push) Successful in 30s

Before a write, the write path now looks up issues by structure:
- an issue whose body or a log names the file (any status: a closed one
  is the record of what was already tried there);
- an open issue tagged to an area whose path patterns cover the file.

At most two, listed first, withheld from the search by meaning, which
fills only what is left and only past the gate. Measured under
write_path_linked. The lookups follow the kind x moment table: no issue
lookup when issues are off at work.change, no nearby-snippet lookup when
snippets are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 18:58:59 -05:00
co-authored by Claude Opus 5.5
parent 1363936ed7
commit a2e474f401
7 changed files with 328 additions and 8 deletions
+17
View File
@@ -388,6 +388,23 @@ def _default_record_kinds(request):
yield
@pytest.fixture(autouse=True)
def _no_linked_issues(request):
"""Stub the write path's structural issue lookup to find nothing.
It reads notes and task logs, which a unit test of the write path has
not written; a test about linked issues patches it with its own answer.
The integration lane runs the real query.
"""
if request.node.get_closest_marker("integration"):
yield
return
from grimoire.services import record_links
with patch.object(record_links, "issues_for_path", AsyncMock(return_value=[])):
yield
@pytest.fixture(autouse=True)
def _ungated_doors(request):
"""Leave the plugin doors' cue-swap gate off unless a test asks for it.
+80
View File
@@ -0,0 +1,80 @@
"""Real-Postgres tests for `record_links.issues_for_path`: an issue naming the
file in its body or a log comes first; an open issue in the file's area comes
next; a closed one there, another project's and a plain task are
left out, as is everything for a reader without access; and a path is
matched literally."""
import pytest
from grimoire.models import async_session
from grimoire.models.note import Note
from grimoire.models.project import Project
from grimoire.models.system import RecordSystem, System
from grimoire.models.task_log import TaskLog
from grimoire.services import record_links
from tests.helpers import ensure_user
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
PATH = "src/app/lock_guard.py"
async def _world():
async with async_session() as s:
owner = await ensure_user(s, "record_links_owner")
stranger = await ensure_user(s, "record_links_stranger")
project = Project(user_id=owner.id, title="record links project")
other = Project(user_id=owner.id, title="record links other")
s.add_all([project, other])
await s.flush()
area = System(user_id=owner.id, project_id=project.id, name="locks",
path_patterns=["src/app/**"])
s.add(area)
await s.flush()
def issue(title, status="done", body="", pid=project.id, uid=owner.id, kind="issue"):
return Note(user_id=uid, project_id=pid, title=title, status=status,
task_kind=kind, body=body)
made = {
"in_body": issue("names it in its body", body=f"fixed in {PATH}"),
"in_log": issue("names it in a log"),
"open_area": issue("open in the area", status="todo"),
"closed_area": issue("closed in the area"),
"other_project": issue("other project", body=f"see {PATH}", pid=other.id),
"work_task": issue("a work task naming it", body=f"edit {PATH}", kind="work"),
# `_` is a single-character wildcard to LIKE; matched literally it
# must not stand in for the `_` in the path.
"near_miss": issue("a near miss", body="src/app/lockXguard.py"),
}
s.add_all(made.values())
await s.flush()
s.add(TaskLog(task_id=made["in_log"].id, user_id=owner.id, content=f"touched {PATH}"))
s.add_all(RecordSystem(note_id=made[k].id, system_id=area.id)
for k in ("open_area", "closed_area"))
await s.commit()
ids = {k: n.id for k, n in made.items()}
return owner.id, project.id, ids, stranger.id
async def test_issues_naming_the_file_come_first_then_open_ones_in_its_area():
uid, pid, ids, _stranger = await _world()
found = await record_links.issues_for_path(uid, pid, PATH, limit=5)
got = [(link, int(n.id)) for link, n in found]
named = {i for link, i in got if link == record_links.NAMES_FILE}
assert named == {ids["in_body"], ids["in_log"]}
assert got[:2] == [(link, i) for link, i in got if link == record_links.NAMES_FILE]
assert (record_links.OPEN_IN_AREA, ids["open_area"]) in got
left_out = {"closed_area", "other_project", "work_task", "near_miss"}
assert not {ids[k] for k in left_out} & {i for _l, i in got}
async def test_the_limit_and_the_exclusions_hold():
uid, pid, ids, stranger = await _world()
assert len(await record_links.issues_for_path(uid, pid, PATH, limit=1)) == 1
found = await record_links.issues_for_path(
uid, pid, PATH, limit=5, exclude={ids["in_body"], ids["in_log"]},
)
assert [int(n.id) for _l, n in found] == [ids["open_area"]]
assert await record_links.issues_for_path(uid, 0, PATH, limit=5) == []
# A reader who cannot read the project is shown none of its issues.
assert await record_links.issues_for_path(stranger, pid, PATH, limit=5) == []
+72
View File
@@ -0,0 +1,72 @@
"""The write path's structural issue arm: an issue that names the file being
written, or is open in its area, is listed before anything found by meaning,
withheld from that search, measured under its own source, and not looked for
at all when the user's table turns issues off at this door."""
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from grimoire.services import record_kinds, record_links
from tests.helpers import fake_note, fake_task
from tests.test_write_path_trigger import REAL_CODE, _cfg
async def _hint(linked, *, semantic=(), kinds=None, project_id=2):
from grimoire.services import plugin_context as pc
search = AsyncMock(return_value=list(semantic))
surfaced = MagicMock()
lookup = AsyncMock(return_value=linked)
patches = [
patch.object(pc, "get_writepath_config", AsyncMock(return_value=_cfg())),
patch.object(pc.snippets_svc, "list_snippets", AsyncMock(return_value=([], 0))),
patch.object(pc, "semantic_search_notes", search),
patch.object(pc, "record_retrieval", MagicMock()),
patch.object(pc, "record_surfaced", surfaced),
patch.object(pc, "owner_names_for", AsyncMock(return_value={})),
patch.object(record_links, "issues_for_path", lookup),
]
if kinds is not None:
patches.append(patch.object(record_kinds, "kinds_for", AsyncMock(return_value=kinds)))
for p in patches:
p.start()
try:
out = await pc.build_write_path_hint(1, "src/x.py", code=REAL_CODE, project_id=project_id)
finally:
for p in reversed(patches):
p.stop()
return out, search, surfaced, lookup
@pytest.mark.asyncio
async def test_a_linked_issue_is_listed_before_meaning_and_withheld_from_it():
issue = fake_task(id=41, title="the lock leaked in x.py", task_kind="issue", status="done",
user_id=1, note_type="note", data=None)
similar = (0.90, fake_note(id=7, title="a snippet like it", note_type="snippet", user_id=1))
out, search, surfaced, lookup = await _hint(
[(record_links.NAMES_FILE, issue)], semantic=[similar],
)
lines = [ln for ln in out["context"].splitlines() if ln.startswith("> - #")]
assert lines[0].startswith('> - #41 [names this file · issue (done)]')
assert lookup.await_args.args[:3] == (1, 2, "src/x.py")
# The search by meaning is told the linked issue is already listed.
assert 41 in search.await_args.kwargs["exclude_ids"]
by_source = {c.kwargs["source"]: c.kwargs["note_ids"] for c in surfaced.call_args_list}
assert by_source["write_path_linked"] == [41]
assert "write_path_place" not in by_source
@pytest.mark.asyncio
async def test_no_issue_is_looked_up_when_the_door_turned_issues_off():
_out, _search, _surfaced, lookup = await _hint(
[], kinds=frozenset({"snippet", "note", "lesson"}),
)
lookup.assert_not_awaited()
@pytest.mark.asyncio
async def test_no_issue_is_looked_up_without_a_project():
_out, _search, _surfaced, lookup = await _hint([], project_id=0)
lookup.assert_not_awaited()