Skip to content

feat(#350): backlink auto-generation — bidirectional memory edges - #380

Merged
ajianaz merged 5 commits into
developfrom
feat/350-backlinks
Jun 18, 2026
Merged

ajianaz merged 5 commits into
developfrom
feat/350-backlinks

Conversation

@ajianaz

@ajianaz ajianaz commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

Implements #350 — automatic bidirectional links ("Iron Law of Back-Linking"). When memory A creates a forward edge to B, an inverse referenced_by edge from B → A is now auto-inserted. Makes the graph navigable in both directions without O(n) scan.

Changes

Core (uteke-core):

  • EDGE_REFERENCED_BY const + backlink_type_for() helper
  • Store::ensure_backlink() — idempotent inverse-edge insertion
  • Store::add_memory_edge_with_backlink() — forward + backlink in one call
  • Store::add_memory_edges_batch() now also ensures backlinks (covers the auto-wire path on every remember())
  • Store::rebuild_backlinks() — scan forward edges, repair missing inverses
  • Uteke::link_memories() — public explicit-edge API with auto backlink
  • Uteke::rebuild_backlinks() — delegates to Store, logs count
  • lib.rs now exports edge types + EdgeList/MemoryEdge/backlink_type_for

CLI (uteke-cli):

  • New uteke rebuild-backlinks [--quiet] subcommand
  • uteke edges <id> gains --direction <incoming|outgoing|both>

Behavior

Forward edge Backlink (auto)
(A → B, references) (B → A, referenced_by)
(A → B, tagged_as) (B → A, referenced_by)
(A → B, supersedes) (B → A, referenced_by)
(A → B, replies_to) (B → A, referenced_by)
(A → B, referenced_by) (none — inverse type is not backlinked)
Self-loop (skipped)

All backlink operations are idempotent (UNIQUE constraint + INSERT OR IGNORE).

Tests

+11 new tests (198 → 209 core, all pass):

  • backlink_type_for mapping (forward/inverse/unknown)
  • ensure_backlink: creates inverse, idempotent, skips inverse+unknown types, skips self-loops
  • add_memory_edge_with_backlink: creates both, idempotent
  • batch insert generates backlinks for all forward edges
  • rebuild_backlinks: from existing forward edges, idempotent, ignores non-forward types
  • wire_edges end-to-end: remember() produces forward + referenced_by backlink
  • Updated store_edge_dedup for new backlink semantics

cargo fmt, cargo clippy --all-targets clean.

Smoke test

$ uteke edges <source>
Edges for memory 68936ba6 (2 total):
→ outgoing (1):
  68936ba6 → 2b5e38ee   [supersedes]
← incoming (1):
  68936ba6 ← 2b5e38ee   [referenced_by]

$ uteke rebuild-backlinks --json
{"created":1,"edges_after":2,"edges_before":1}

Checklist from #350

  • 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 (already done in feat: typed auto-edges — self-wiring knowledge graph on every remember() #346 via list_memory_edges which returns both directions; --direction flag added here)
  • Tests: bidirectional creation, deduplication, rebuild idempotency
  • Benchmark: incoming edge lookup via SQL vs current O(n) scan (now O(log n) via indexed memory_edges table — benchmark left for feat: salience + recency dual-axis recall ranking #352 recall-ranking work)

Closes #350

Every forward edge (references, tagged_as, supersedes, replies_to) now
automatically receives an inverse  edge from target → source,
making the memory graph navigable in both directions without an O(n) scan.

Core (uteke-core):
  - EDGE_REFERENCED_BY const + backlink_type_for() helper
  - Store::ensure_backlink() — idempotent inverse-edge insertion
  - Store::add_memory_edge_with_backlink() — forward + backlink in one call
  - Store::add_memory_edges_batch() now also ensures backlinks (covers
    the auto-wire path on every )
  - Store::rebuild_backlinks() — scan forward edges, repair missing inverses
  - Uteke::link_memories() — public explicit-edge API with auto backlink
  - Uteke::rebuild_backlinks() — delegates to Store, logs count
  - lib.rs now exports edge types + EdgeList/MemoryEdge/backlink_type_for

CLI (uteke-cli):
  - New  subcommand
  -  gains

Tests (+11 new, 198 → 209 core tests, all pass):
  - backlink_type_for forward/inverse/unknown mapping
  - ensure_backlink: creates inverse, idempotent, skips inverse+unknown types,
    skips self-loops
  - add_memory_edge_with_backlink: creates both, idempotent
  - batch insert generates backlinks for all forward edges
  - rebuild_backlinks: from existing forward edges, idempotent, ignores
    non-forward types
  - wire_edges end-to-end: remember() produces forward + referenced_by backlink
  - Updated store_edge_dedup for new backlink semantics

Docs: CHANGELOG + cli-reference.md (rebuild-backlinks command, --direction)

Closes #350
@github-actions

github-actions Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-cli · BYOK · MIT

CI failure: wire_edges_generates_backlinks calls Uteke::open which
downloads the ONNX embedder from HuggingFace — unavailable in the CI
sandbox. Marked #[ignore] consistent with the other Uteke::open-based
tests in lib.rs (test_recall_threshold_*). Backlink behavior is fully
covered at the Store level by batch_insert_generates_backlinks,
add_memory_edge_with_backlink_*, and rebuild_backlinks_*.

Also addressed Cora review comment: rebuild_backlinks() previously used
.filter_map(|r| r.ok()) which silently dropped per-row rusqlite errors.
Replaced with .collect::<Result<Vec<_>, _>>()? so iteration failures
propagate instead of being swallowed.
Comment thread crates/uteke-core/src/edges.rs Fixed
ajianaz added 2 commits June 18, 2026 21:21
Addresses Cora + CodeCora review: backlink failures were silently
swallowed (tracing::warn only), which could leave the store with a
forward edge but missing inverse 'referenced_by' edge — contradicting
the documented 'automatic' backlink guarantee.

- Store::add_memory_edge_with_backlink() now propagates ensure_backlink
  errors via '?'. Callers who explicitly request 'with backlink' get
  transactional semantics.
- Store::add_memory_edges_batch() also propagates. The auto-wire path
  (Uteke::wire_edges) already wraps batch failures in a best-effort
  catch (tracing::warn), so remember() still never fails due to edges.
CodeCora flagged: 'add_memory_edge_with_backlink() inserts the forward
row first and only then calls ensure_backlink(). If backlink creation
fails, the method returns an error but leaves the DB in a partially
updated state with only the forward edge present.'

Wrap both insert paths in unchecked_transaction():
  - add_memory_edge_with_backlink: forward + inverse in single tx
  - add_memory_edges_batch: full batch + all inverses in single tx

On backlink failure the transaction is dropped (rollback) so no partial
edge state survives. Commits only when both forward and inverse are
applied. The 'no-backlink' edge types still commit (they have no inverse
to guarantee).
// BACKLINKED_EDGE_TYPES is fixed at 4 entries (references, tagged_as,
// supersedes, replies_to) — we bind them positionally.
debug_assert_eq!(
BACKLINKED_EDGE_TYPES.len(),
CodeCora flagged: rebuild_backlinks scans rows then inserts missing
backlinks one-by-one without a transaction. If one insert fails partway,
the repair is left in a partial state with some backlinks created and
others missing.

Wrap the repair pass in unchecked_transaction() so either all missing
backlinks are written or the whole rebuild rolls back atomically.
@ajianaz
ajianaz merged commit 48452b0 into develop Jun 18, 2026
8 checks passed
ajianaz added a commit that referenced this pull request Aug 13, 2026
* feat(#350): backlink auto-generation — bidirectional memory edges (#350)

Every forward edge (references, tagged_as, supersedes, replies_to) now
automatically receives an inverse  edge from target → source,
making the memory graph navigable in both directions without an O(n) scan.

Core (uteke-core):
  - EDGE_REFERENCED_BY const + backlink_type_for() helper
  - Store::ensure_backlink() — idempotent inverse-edge insertion
  - Store::add_memory_edge_with_backlink() — forward + backlink in one call
  - Store::add_memory_edges_batch() now also ensures backlinks (covers
    the auto-wire path on every )
  - Store::rebuild_backlinks() — scan forward edges, repair missing inverses
  - Uteke::link_memories() — public explicit-edge API with auto backlink
  - Uteke::rebuild_backlinks() — delegates to Store, logs count
  - lib.rs now exports edge types + EdgeList/MemoryEdge/backlink_type_for

CLI (uteke-cli):
  - New  subcommand
  -  gains

Tests (+11 new, 198 → 209 core tests, all pass):
  - backlink_type_for forward/inverse/unknown mapping
  - ensure_backlink: creates inverse, idempotent, skips inverse+unknown types,
    skips self-loops
  - add_memory_edge_with_backlink: creates both, idempotent
  - batch insert generates backlinks for all forward edges
  - rebuild_backlinks: from existing forward edges, idempotent, ignores
    non-forward types
  - wire_edges end-to-end: remember() produces forward + referenced_by backlink
  - Updated store_edge_dedup for new backlink semantics

Docs: CHANGELOG + cli-reference.md (rebuild-backlinks command, --direction)

Closes #350

* fix(#350): mark wire_edges e2e test #[ignore] + propagate rebuild errors

CI failure: wire_edges_generates_backlinks calls Uteke::open which
downloads the ONNX embedder from HuggingFace — unavailable in the CI
sandbox. Marked #[ignore] consistent with the other Uteke::open-based
tests in lib.rs (test_recall_threshold_*). Backlink behavior is fully
covered at the Store level by batch_insert_generates_backlinks,
add_memory_edge_with_backlink_*, and rebuild_backlinks_*.

Also addressed Cora review comment: rebuild_backlinks() previously used
.filter_map(|r| r.ok()) which silently dropped per-row rusqlite errors.
Replaced with .collect::<Result<Vec<_>, _>>()? so iteration failures
propagate instead of being swallowed.

* fix(#350): propagate backlink errors instead of silent warn

Addresses Cora + CodeCora review: backlink failures were silently
swallowed (tracing::warn only), which could leave the store with a
forward edge but missing inverse 'referenced_by' edge — contradicting
the documented 'automatic' backlink guarantee.

- Store::add_memory_edge_with_backlink() now propagates ensure_backlink
  errors via '?'. Callers who explicitly request 'with backlink' get
  transactional semantics.
- Store::add_memory_edges_batch() also propagates. The auto-wire path
  (Uteke::wire_edges) already wraps batch failures in a best-effort
  catch (tracing::warn), so remember() still never fails due to edges.

* fix(#350): atomic forward + backlink insertion via SQLite transaction

CodeCora flagged: 'add_memory_edge_with_backlink() inserts the forward
row first and only then calls ensure_backlink(). If backlink creation
fails, the method returns an error but leaves the DB in a partially
updated state with only the forward edge present.'

Wrap both insert paths in unchecked_transaction():
  - add_memory_edge_with_backlink: forward + inverse in single tx
  - add_memory_edges_batch: full batch + all inverses in single tx

On backlink failure the transaction is dropped (rollback) so no partial
edge state survives. Commits only when both forward and inverse are
applied. The 'no-backlink' edge types still commit (they have no inverse
to guarantee).

* fix(#350): wrap rebuild_backlinks in transaction for atomic repair

CodeCora flagged: rebuild_backlinks scans rows then inserts missing
backlinks one-by-one without a transaction. If one insert fails partway,
the repair is left in a partial state with some backlinks created and
others missing.

Wrap the repair pass in unchecked_transaction() so either all missing
backlinks are written or the whole rebuild rolls back atomically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants