Files
pad/cmd
xarmian 35ac7552eb feat(backlinks): UI panel + mention badge + CLI + MCP (Phase 3) (#623)
* feat(backlinks): UI panel + mention badge + CLI + MCP (Phase 3)

Phase 3 of PLAN-1593 (TASK-1596). Surfaces the backlinks index
shipped in Phases 1/2a/2b across every place users live: web UI,
CLI, MCP.

What changed

Web UI
- New BacklinksPanel.svelte at web/src/lib/components/. Fetches via
  api.items.backlinks (new method) and renders inbound `[[...]]`
  references grouped by source collection. Per-row: collection icon,
  ref + title, snippet, relative timestamp, optional `(displayed as)`
  override, faint workspace badge on cross-ws rows. Pagination via
  "Show older" when the page is full. Collapses entirely when the
  count is zero — no header, no whitespace, no empty surface for
  items with no inbound links.
- Mention badge ("📎 N") in the item-page action bar next to the
  Timeline button. Hidden when N=0; smooth-scrolls to the panel.
  Wired via onCountChange callback so badge + panel stay in sync.
- New Backlink TypeScript type at web/src/lib/types/index.ts mirroring
  internal/models/backlink.go; new api.items.backlinks(ws, slug, opts)
  client method.

CLI
- `pad item show <ref>` enriched with inline top-5 "Mentioned in"
  section in TTY mode (skipped when empty), and a backlinks_top
  array in JSON output. Hint at the dedicated `pad item backlinks`
  command when the inline list hits the 5-row cap.

MCP
- New `pad_item.action: backlinks` — passes through to
  `pad item backlinks <ref>` with optional `limit` (default 50,
  max 300) + `offset` params. Bumps ToolSurfaceVersion 0.5 → 0.6
  with a backwards-compatible additive note in version.go. Updated
  the catalog_readonly_test fixtures so the cmdhelp drift check
  passes.

Test plan
- [x] go build ./... + go test ./internal/mcp/ green
- [x] make check (lint + Go + web) green
- [x] make install + restart
- [x] pad item show TASK-1596 shows --- Mentioned in --- inline
- [x] pad item show TASK-1596 --format json includes backlinks_top
- [x] svelte-check 0 errors
- [ ] /codex review --loop → CLEAN

Out of scope (filed as separate ideas if anyone asks)
- Force-directed graph visualization of the link network
- Broken-links report (target_item_id IS NULL feeds it but it's its
  own feature)

PLAN-1593 / TASK-1596.

* fix(backlinks): unique each-block key for multi-occurrence rows (Codex round 1)

Codex round 1 P1: BacklinksPanel keyed each row by source_item_id,
but the server preserves multiplicity — a source body that mentions
the target three times produces three Backlink rows (Phase 1 design
decision, covered by TestWikiLinks_RepeatedRefStoresMultipleRows).
Duplicate keys in Svelte's #each are rejected at dev time and
silently reuse DOM in prod, so the panel would render only one of
the N rows from a multi-mention source.

Fix: compose a unique key per row via new rowKey(bl, index) helper:
`${source_item_id}|${snippet}|${index}`. The snippet usually
differs across positions (centered on the bracket byte offset);
the index suffix is the unconditional tie-breaker.

PLAN-1593 / TASK-1596.
2026-05-24 15:02:06 -04:00
..