Skip to content

feat: backlink auto-generation — bidirectional links on memory references #350

Closed

Description

@ajianaz

Motivation

GBrain enforces "Iron Law of Back-Linking": every mention of an entity creates a bidirectional link. If memory A references memory B, then B should know it's referenced by A. This makes the graph navigable in both directions.

Uteke currently has no automatic backlinking. When recall_related finds edges, it only follows outgoing relationships — incoming edges require an O(n) scan of all memories.

Proposal

When a memory A creates an edge to memory B, automatically ensure a backlink edge exists from B to A (type: referenced_by).

Depends on: #346 (typed auto-edges — edge table must exist first)

Backlink Edge Type

pub const REL_REFERENCED_BY: &str = "referenced_by";

When remember() or explicit edge creation adds edge (A → B, type: references):

  1. Also insert edge (B → A, type: referenced_by) if not exists
  2. Idempotent — won't duplicate if backlink already present

Backlink Maintenance

# Rebuild all backlinks (one-time migration + maintenance)
uteke rebuild-backlinks

# Show backlinks for a memory
uteke edges <memory-id> --direction incoming

Integration with Auto-Edges (#346)

When the pattern parser in #346 detects [[some-memory]] in content:

  1. Create edge (this → some-memory, references)
  2. Auto-create backlink (some-memory → this, referenced_by)
  3. Both stored in memory_edges table — instant lookup, no scan

Deduplication

Backlinks are deduplicated by (source_id, target_id, edge_type) — inserting the same backlink twice is a no-op.

Scope

  • Implement ensure_backlink() function (idempotent)
  • Hook into edge creation path (both manual + auto-extracted)
  • Add uteke rebuild-backlinks CLI command (scan all memories, rebuild)
  • Update get_related() to query both directions via edge table
  • Tests: bidirectional creation, deduplication, rebuild idempotency
  • Benchmark: incoming edge lookup via SQL vs current O(n) scan

Non-Goals

  • Transitive backlink computation (only direct backlinks)
  • Backlink depth limits (all direct backlinks stored)

Dependencies

References

  • GBrain: skills/conventions/quality.md — "Iron Law: Back-Linking (MANDATORY)"
  • GBrain: skills/brain-ops/SKILL.md — "An unlinked mention is a broken brain"
  • Current: graph.rs get_related() — O(n) scan for incoming edges

Activity

  1. added this to the milestone on Jun 14, 2026
  2. added
    enhancement
    scope:core
    type:feature
    epic:memory-engine
    status:planning
    on Jun 14, 2026
  3. modified the milestones: , v0.2.1 — DX & Ecosystem on Jun 14, 2026
  4. added 4 commits that reference this issue on Jun 18, 2026
  5. added
    status:in-progress
    and removed
    status:planning
    on Jun 18, 2026
  6. added 3 commits that reference this issue on Jun 18, 2026
  7. added a commit that references this issue on Jul 2, 2026
  8. added 3 commits that reference this issue on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementepic:memory-enginepriority:mediumscope:corestatus:in-progresstype:feature

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions