"""Records linked to the work by structure: no score, no bar. WHY THIS EXISTS A search by meaning asks "what resembles the words of this edit", and the answer is often a record about something else that happens to read the same. A record that NAMES the file being edited, or is open in the area the file belongs to, needs no resemblance to be relevant: the link is the reason. So these lookups run first, their lines take the kind's place in the payload, and the search by meaning only fills what they left. THE TWO LINKS, strongest first - **Names this file** — an issue whose body, or one of whose logs, carries the file's repo-relative path. That is how a session records what it touched, so an issue that fixed something in this file says so in its own words. Any status: a closed issue is the record of what was already tried here. - **Open in this area** — an issue still open, tagged to an area (System) whose path patterns cover the file. A finished issue in the area is history the file may not share; an open one is live work on it. Both are scoped to the project and to what the reader may read. """ from __future__ import annotations import logging from sqlalchemy import exists, or_, select from grimoire.models import async_session from grimoire.models.note import Note from grimoire.models.system import RecordSystem from grimoire.models.task_log import TaskLog from grimoire.services.access import readable_notes_clause logger = logging.getLogger(__name__) NAMES_FILE = "names this file" OPEN_IN_AREA = "open in this area" _CLOSED = ("done", "cancelled") def _issue(user_id: int, project_id: int): return ( select(Note) .where( Note.project_id == project_id, Note.status.isnot(None), Note.task_kind == "issue", Note.deleted_at.is_(None), readable_notes_clause(user_id), ) ) async def issues_for_path( user_id: int, project_id: int, path: str, *, limit: int, exclude: set[int] | frozenset[int] = frozenset(), ) -> list[tuple[str, Note]]: """Issues linked to the file at `path`, each with the link that found it: those naming the file first, then those open in its area, most recently touched first within each, at most `limit` in all.""" path = (path or "").strip() if not project_id or not path or limit <= 0: return [] from grimoire.services.systems import systems_for_paths out: list[tuple[str, Note]] = [] taken = set(exclude) try: async with async_session() as session: # A literal path, not a pattern: `_` and `%` are common in file # names and would otherwise match any character. named = (await session.execute( _issue(user_id, project_id) .where(or_( Note.body.contains(path, autoescape=True), exists().where( TaskLog.task_id == Note.id, TaskLog.content.contains(path, autoescape=True), ), )) .order_by(Note.updated_at.desc(), Note.id.desc()) .limit(limit + len(taken)) )).scalars().all() for note in named: if int(note.id) not in taken and len(out) < limit: out.append((NAMES_FILE, note)) taken.add(int(note.id)) if len(out) >= limit: return out areas = [s.id for s, _hit in await systems_for_paths(user_id, project_id, [path])] if not areas: return out open_here = (await session.execute( _issue(user_id, project_id) .where( Note.status.not_in(_CLOSED), exists().where( RecordSystem.note_id == Note.id, RecordSystem.system_id.in_(areas), ), ) .order_by(Note.updated_at.desc(), Note.id.desc()) .limit(limit + len(taken)) )).scalars().all() for note in open_here: if int(note.id) not in taken and len(out) < limit: out.append((OPEN_IN_AREA, note)) taken.add(int(note.id)) except Exception: # noqa: BLE001 - a recall aid never breaks its act logger.warning("linked-issue lookup failed", exc_info=True) return out