Repository navigation
feat(#350): backlink auto-generation — bidirectional memory edges - #380
Merged
Merged
Conversation
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
🔍 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.
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements #350 — automatic bidirectional links ("Iron Law of Back-Linking"). When memory A creates a forward edge to B, an inverse
referenced_byedge from B → A is now auto-inserted. Makes the graph navigable in both directions without O(n) scan.Changes
Core (
uteke-core):EDGE_REFERENCED_BYconst +backlink_type_for()helperStore::ensure_backlink()— idempotent inverse-edge insertionStore::add_memory_edge_with_backlink()— forward + backlink in one callStore::add_memory_edges_batch()now also ensures backlinks (covers the auto-wire path on everyremember())Store::rebuild_backlinks()— scan forward edges, repair missing inversesUteke::link_memories()— public explicit-edge API with auto backlinkUteke::rebuild_backlinks()— delegates to Store, logs countlib.rsnow exports edge types +EdgeList/MemoryEdge/backlink_type_forCLI (
uteke-cli):uteke rebuild-backlinks [--quiet]subcommanduteke edges <id>gains--direction <incoming|outgoing|both>Behavior
(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)All backlink operations are idempotent (UNIQUE constraint +
INSERT OR IGNORE).Tests
+11 new tests (198 → 209 core, all pass):
backlink_type_formapping (forward/inverse/unknown)ensure_backlink: creates inverse, idempotent, skips inverse+unknown types, skips self-loopsadd_memory_edge_with_backlink: creates both, idempotentrebuild_backlinks: from existing forward edges, idempotent, ignores non-forward typeswire_edgesend-to-end:remember()produces forward +referenced_bybacklinkstore_edge_dedupfor new backlink semanticscargo fmt,cargo clippy --all-targetsclean.Smoke test
Checklist from #350
ensure_backlink()function (idempotent)uteke rebuild-backlinksCLI command (scan all memories, rebuild)get_related()to query both directions via edge table (already done in feat: typed auto-edges — self-wiring knowledge graph on every remember() #346 vialist_memory_edgeswhich returns both directions;--directionflag added here)memory_edgestable — benchmark left for feat: salience + recency dual-axis recall ranking #352 recall-ranking work)Closes #350