fix(7736): #N in a body links to the record it names, not to a tag
CI & Build / TypeScript typecheck (push) Successful in 35s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / Python tests (push) Failing after 1m37s
CI & Build / integration (push) Successful in 2m11s
CI & Build / Build & push image (push) Skipped

A purely numeric #N is a record reference everywhere now. The tag
patterns (frontend utils/tags.ts, the editor decoration, the first-line
tag strip, tag autocomplete, backend utils/tags.py) leave digits alone,
so #3127 is no longer drawn as a tag chip linking to /notes?tag=3127.

- utils/recordRefs.ts links #N, milestone N and rule N in rendered
  markdown, with the same patterns and other-numbering word list as
  services/record_refs.py. A drift test reads the TS source and fails
  when the two differ.
- v-record-refs (directives/recordRefs.ts) asks POST
  /api/notes/resolve-refs once per body for what each id is. A link
  gets the route for its kind (task, lesson, snippet, note, milestone,
  rule) and its kind and title on hover. One the reader cannot open is
  drawn as a dead reference, and missing vs not shared read the same.
  Applied on every surface that renders markdown.
- /milestones/:id finds the project and opens its board at that
  milestone (?milestone= expands it, scrolls to it and focuses it).
- Backlinks include records that cite #N in a body or a work log,
  through readable_notes_clause instead of an owner-only filter, and
  route by kind. The graph draws #N citations as dashed reference edges.
- create_note, create_task and add_task_log say which forms link.

There were no numeric tags to clean up: tags are stored only from the
explicit field, and none of the 200 most used is numeric.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 14:15:15 -05:00
co-authored by Claude Opus 5.5
parent 6be44013bb
commit 38bc25a2b7
24 changed files with 846 additions and 48 deletions
+81 -18
View File
@@ -986,33 +986,80 @@ async def get_notes_by_ids(user_id: int, note_ids: list[int]) -> dict[int, Note]
async def get_backlinks(user_id: int, note_id: int) -> list[dict]:
note = await get_note(user_id, note_id)
if note is None:
return []
"""The records that link to this one, among those the caller may read.
title = note.title
if not title:
Two ways to link: a `[[title]]` wikilink, and a `#N` reference in a body or
in a task's work log (most cross-references agents write are the second).
Each row says which one it was in `via`. A record reached both ways is
listed once, as a wikilink.
"""
from grimoire.models.task_log import TaskLog
from grimoire.services.access import readable_notes_clause
from grimoire.services.record_refs import referenced
found = await get_note_for_user(user_id, note_id)
if found is None:
return []
note, _ = found
readable = readable_notes_clause(user_id)
rows: dict[int, dict] = {}
def _row(n: Note, via: str) -> dict:
return {
"type": "task" if n.is_task else "note",
"id": n.id,
"title": n.title,
"is_task": n.is_task,
"note_type": n.note_type or "note",
"via": via,
}
async with async_session() as session:
escaped = title.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
pattern = f"%[[{escaped}]]%"
pattern_alias = f"%[[{escaped}|%"
if note.title:
escaped = note.title.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
linking = (await session.execute(
select(Note).where(
readable,
Note.id != note_id,
Note.deleted_at.is_(None),
or_(Note.body.like(f"%[[{escaped}]]%"),
Note.body.like(f"%[[{escaped}|%")),
)
)).scalars().all()
for n in linking:
rows[n.id] = _row(n, "wikilink")
results = await session.execute(
select(Note.id, Note.title, Note.status).where(
Note.user_id == user_id,
# LIKE narrows to bodies holding the digits after a `#`; `referenced`
# then reads each one the way the renderer does, so `#12` does not
# count as a link to 1, and "PR #12" is not a link at all.
needle = f"%#{note_id}%"
citing = (await session.execute(
select(Note).where(
readable,
Note.id != note_id,
Note.deleted_at.is_(None),
or_(Note.body.like(pattern), Note.body.like(pattern_alias)),
Note.body.like(needle),
)
)
backlinks: list[dict] = []
for row in results.fetchall():
link_type = "task" if row[2] is not None else "note"
backlinks.append({"type": link_type, "id": row[0], "title": row[1]})
)).scalars().all()
for n in citing:
if n.id not in rows and note_id in referenced(n.body)["notes"]:
rows[n.id] = _row(n, "reference")
return backlinks
logged = (await session.execute(
select(Note, TaskLog.content)
.join(TaskLog, TaskLog.task_id == Note.id)
.where(
readable,
Note.id != note_id,
Note.deleted_at.is_(None),
TaskLog.content.like(needle),
)
)).all()
for n, content in logged:
if n.id not in rows and note_id in referenced(content)["notes"]:
rows[n.id] = _row(n, "reference")
return list(rows.values())
_WIKILINK_RE = re.compile(r'\[\[([^\]|]+)(?:\|[^\]]+)?\]\]')
@@ -1071,6 +1118,22 @@ async def build_note_graph(
edge_set.add(pair)
edges.append({"source": n.id, "target": target_id, "type": "wikilink"})
# `#N` reference edges, read the way the renderer links them. Only between
# records already on the graph: a reference to something outside it (another
# project, a record the caller cannot read) has no node to end at.
from grimoire.services.record_refs import referenced
on_graph = {n.id for n in notes}
for n in notes:
if not n.body:
continue
for target_id in sorted(referenced(n.body)["notes"]):
if target_id in on_graph and target_id != n.id:
pair = (n.id, target_id)
if pair not in edge_set:
edge_set.add(pair)
edges.append({"source": n.id, "target": target_id, "type": "reference"})
# Build tag nodes + note→tag edges
if include_shared_tags:
tag_to_ids: dict[str, list[int]] = {}