Repository navigation
feat: auto-wired memory edges (#346) - #375
Merged
Merged
Conversation
Self-wiring knowledge graph: every remember() extracts entity references via pure string parsing (no LLM) and writes typed edges to a new memory_edges SQLite table. get_related() now uses indexed SQL instead of O(n) JSON scan. Schema v7 -> v8: - New memory_edges table (source_id, target_id, edge_type, created_at) with indices on source, target, type; UNIQUE(source, target, type). - New optional memories.slug column (partial index) for [[slug]] linking. - Migration v7->v8 backfills existing metadata.relationships JSON entries into memory_edges rows. Extraction patterns (zero LLM): - [[slug]] -> references edge (via memories.slug lookup) - @tag -> tagged_as edge (most recent memory with that tag) - ^<uuid> -> supersedes edge (direct UUID) - ><uuid> -> replies_to edge (direct UUID) - rel:<t>:<id> -> <t> edge (legacy --meta form, backward compat) New CLI: uteke edges <id> # list direct edges (both directions) uteke edges <id> --deep N # BFS across edge table, N hops uteke edges <id> --json Core API (uteke-core): - Uteke::wire_edges() (pub(crate)) called from remember_precomputed - Uteke::edges_for(id) -> EdgeList { outgoing, incoming } - Uteke::related_via_edges(id, depth) -> Vec<Memory> (replaces O(n) scan) - Uteke::count_edges() -> usize - Store::add_memory_edge/add_memory_edges_batch/resolve_slug/ resolve_tag_to_memory/list_memory_edges/edge_bfs/count_memory_edges Rewrites: - get_related() prefers edge table; falls back to legacy JSON scan only if no edges exist for the memory (backward compat for pre-v8 stores that never auto-wired). Cleanup: - Fixed pre-existing clippy warnings in commands/graph.rs (3x else-if collapse) so cargo clippy --workspace -- -D warnings passes cleanly. Tests: +20 unit tests covering extraction patterns (slug/tag/uuid/reply/ legacy meta), edge roundtrip, deduplication, BFS traversal, cycle safety, and slug/tag resolution. All 193 tests pass. Docs: CHANGELOG entry, new 'uteke edges' section in cli-reference.md. Closes #346.
🔍 Cora AI Code Review❌ Blocked — critical issues found. 🔴 Error (5)
Review powered by cora-cli · BYOK · MIT |
Three valid findings from security/semantic review: 1. Migration FK abort (finding #131): backfill INSERT now uses 'INSERT ... SELECT ... WHERE EXISTS (SELECT 1 FROM memories WHERE id = ?2)' so a legacy metadata.relationships entry pointing to a deleted or nonexistent memory is skipped instead of aborting the v7->v8 schema migration (which would prevent the database from opening). 2. get_related() semantic change (finding #132): union edge-table results with legacy metadata.relationships scan instead of early-returning when any edge exists. The previous code could silently drop incoming relations encoded only in metadata. Now both sources are merged via a shared visited set. 3. Slug uniqueness documentation (finding #130): the SCHEMA comment and Store::resolve_slug doc now accurately describe slug resolution as 'most recently updated wins' rather than claiming uniqueness is enforced (the partial index is non-unique). Users who need deterministic links are told to keep slugs unique per namespace. Plus: - Removed stray double-semicolon in schema.rs (clippy warning). - Added 2 regression tests for findings #131 and #132. The 'Hardcoded password in variable' finding on edges.rs:146 was a false positive (variable named 'token' in UUID extraction code) — no actual secret, will dismiss the alert. All 195 tests pass. Clippy clean.
edge_bfs() previously called self.conn.prepare(...) inside the while loop, recompiling the same SQL once per visited node. Move the prepare above the loop and reuse the statement across hops. Also adds migration_dispatcher_reaches_v8 regression test that proves the v7->v8 migration is invoked on upgrade (refutes CodeCora finding #136, which misread the dispatcher logic). A fresh Store now must report schema_version=8 and a queryable memory_edges table. All 172 tests pass.
CodeCora found that ExtractedRef::Uuid (^<uuid>, ><uuid>) and ExtractedRef::Rel (rel:<type>:<id>) resolved targets via store.get_by_id without namespace filtering. This leaked relationships across isolated namespaces — inconsistent with slug (@[[slug]]) and tag (@tag) resolution which are already namespace-scoped. Fix: add Store::get_by_id_in_namespace(id, ns) and use it in wire_edges for Uuid + Rel branches. A UUID that exists in another namespace is now treated as unresolved (same as a missing UUID). +1 regression test (uuid_edges_respect_namespace_isolation) covering both cross-namespace rejection and same-namespace success. 173 tests pass. Clippy clean. Finding #139 (wire_edges non-atomic vs store.insert) dismissed as an accepted design trade-off: edge rows CASCADE-delete on forget, so no permanent corruption. Atomic buffering would require restructuring remember() and is deferred until we have usage signal. Closes #138.
ajianaz
added a commit
that referenced
this pull request
Aug 13, 2026
* feat: auto-wired memory edges (#346) Self-wiring knowledge graph: every remember() extracts entity references via pure string parsing (no LLM) and writes typed edges to a new memory_edges SQLite table. get_related() now uses indexed SQL instead of O(n) JSON scan. Schema v7 -> v8: - New memory_edges table (source_id, target_id, edge_type, created_at) with indices on source, target, type; UNIQUE(source, target, type). - New optional memories.slug column (partial index) for [[slug]] linking. - Migration v7->v8 backfills existing metadata.relationships JSON entries into memory_edges rows. Extraction patterns (zero LLM): - [[slug]] -> references edge (via memories.slug lookup) - @tag -> tagged_as edge (most recent memory with that tag) - ^<uuid> -> supersedes edge (direct UUID) - ><uuid> -> replies_to edge (direct UUID) - rel:<t>:<id> -> <t> edge (legacy --meta form, backward compat) New CLI: uteke edges <id> # list direct edges (both directions) uteke edges <id> --deep N # BFS across edge table, N hops uteke edges <id> --json Core API (uteke-core): - Uteke::wire_edges() (pub(crate)) called from remember_precomputed - Uteke::edges_for(id) -> EdgeList { outgoing, incoming } - Uteke::related_via_edges(id, depth) -> Vec<Memory> (replaces O(n) scan) - Uteke::count_edges() -> usize - Store::add_memory_edge/add_memory_edges_batch/resolve_slug/ resolve_tag_to_memory/list_memory_edges/edge_bfs/count_memory_edges Rewrites: - get_related() prefers edge table; falls back to legacy JSON scan only if no edges exist for the memory (backward compat for pre-v8 stores that never auto-wired). Cleanup: - Fixed pre-existing clippy warnings in commands/graph.rs (3x else-if collapse) so cargo clippy --workspace -- -D warnings passes cleanly. Tests: +20 unit tests covering extraction patterns (slug/tag/uuid/reply/ legacy meta), edge roundtrip, deduplication, BFS traversal, cycle safety, and slug/tag resolution. All 193 tests pass. Docs: CHANGELOG entry, new 'uteke edges' section in cli-reference.md. Closes #346. * fix(edges): address CodeCora review findings Three valid findings from security/semantic review: 1. Migration FK abort (finding #131): backfill INSERT now uses 'INSERT ... SELECT ... WHERE EXISTS (SELECT 1 FROM memories WHERE id = ?2)' so a legacy metadata.relationships entry pointing to a deleted or nonexistent memory is skipped instead of aborting the v7->v8 schema migration (which would prevent the database from opening). 2. get_related() semantic change (finding #132): union edge-table results with legacy metadata.relationships scan instead of early-returning when any edge exists. The previous code could silently drop incoming relations encoded only in metadata. Now both sources are merged via a shared visited set. 3. Slug uniqueness documentation (finding #130): the SCHEMA comment and Store::resolve_slug doc now accurately describe slug resolution as 'most recently updated wins' rather than claiming uniqueness is enforced (the partial index is non-unique). Users who need deterministic links are told to keep slugs unique per namespace. Plus: - Removed stray double-semicolon in schema.rs (clippy warning). - Added 2 regression tests for findings #131 and #132. The 'Hardcoded password in variable' finding on edges.rs:146 was a false positive (variable named 'token' in UUID extraction code) — no actual secret, will dismiss the alert. All 195 tests pass. Clippy clean. * style: cargo fmt edges.rs (Format CI) * perf(edges): hoist BFS prepare statement out of loop (CodeCora #134) edge_bfs() previously called self.conn.prepare(...) inside the while loop, recompiling the same SQL once per visited node. Move the prepare above the loop and reuse the statement across hops. Also adds migration_dispatcher_reaches_v8 regression test that proves the v7->v8 migration is invoked on upgrade (refutes CodeCora finding #136, which misread the dispatcher logic). A fresh Store now must report schema_version=8 and a queryable memory_edges table. All 172 tests pass. * fix(edges): enforce namespace isolation on UUID/rel edges (#138) CodeCora found that ExtractedRef::Uuid (^<uuid>, ><uuid>) and ExtractedRef::Rel (rel:<type>:<id>) resolved targets via store.get_by_id without namespace filtering. This leaked relationships across isolated namespaces — inconsistent with slug (@[[slug]]) and tag (@tag) resolution which are already namespace-scoped. Fix: add Store::get_by_id_in_namespace(id, ns) and use it in wire_edges for Uuid + Rel branches. A UUID that exists in another namespace is now treated as unresolved (same as a missing UUID). +1 regression test (uuid_edges_respect_namespace_isolation) covering both cross-namespace rejection and same-namespace success. 173 tests pass. Clippy clean. Finding #139 (wire_edges non-atomic vs store.insert) dismissed as an accepted design trade-off: edge rows CASCADE-delete on forget, so no permanent corruption. Atomic buffering would require restructuring remember() and is deferred until we have usage signal. Closes #138. * style: cargo fmt
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 #346 (priority: high, next-up). Every
remember()now auto-extracts entity references via pure string parsing (zero LLM) and writes typed edges to a newmemory_edgesSQLite table.get_related()rewrites from O(n) JSON scan to indexed SQL.Schema (v7 → v8)
memory_edgestable —source_id, target_id, edge_type, created_atwith indices on source/target/type andUNIQUE(source, target, type).memories.slugcolumn (optional) — for[[slug]]Wikilink-style references. Partial indexWHERE slug IS NOT NULL.metadata.relationshipsJSON entries intomemory_edgesrows (zero data loss).Extraction patterns
[[slug]]referencesmemories.sluglookup@tagtagged_as^<uuid>supersedes><uuid>replies_torel:<type>:<uuid>in--meta<type>Resolution is namespace-scoped. Self-edges skipped. Dangling targets skipped (no orphan edges). Failures are logged and never fail the
remember()call — auto-wiring is best-effort.New CLI
Core API (uteke-core)
Uteke::wire_edges()(pub(crate)) — called fromremember_precomputedUteke::edges_for(id)→EdgeList { outgoing, incoming }Uteke::related_via_edges(id, depth)→Vec<Memory>— replaces O(n) scanUteke::count_edges()→usizeadd_memory_edge,add_memory_edges_batch,resolve_slug,resolve_tag_to_memory,list_memory_edges,edge_bfs,count_memory_edgesRewrites
get_related()now prefers the edge table (indexed SQL). Falls back to legacy JSON metadata scan only if no edges exist for the memory — preserves backward compat for pre-v8 stores that never auto-wired.Cleanup bonus
commands/graph.rs(else { if .. }collapse) socargo clippy --workspace --all-targets -- -D warningsnow passes cleanly.Tests
edges.rscovering: extraction patterns (slug/multiple/reject-invalid/@tag/caret-uuid/arrow-uuid/short-id-reject/legacy-metadata/flat-meta-pairs/mixed),is_valid_slug,looks_like_uuid, edge roundtrip, deduplication, BFS 1-hop + 2-hop, BFS cycle safety, slug/tag resolution.Checklist (from issue #346)
memory_edgesSQLite table + migration (schema v8)remember()flowget_related()to use SQL instead of JSON scanRewrite— left as-is for now; it already callsrecall_related()BFSget_related()path indirectly and the metadata-basedparse_relationshipsis still valid for legacy data. Edge-table-backedrecall_relatedcan be layered on top in a follow-up once we have usage signal.uteke edgesCLI subcommandAdd— already exists from feat: SQLite graph storage — optional knowledge graph mode #317 (uteke graphCLI subcommand (summary stats)uteke graph stats)metadata.relationships→memory_edgesrows (inline in v7→v8 migration)Verification
cargo build --workspace— cleancargo clippy --workspace --all-targets -- -D warnings— cleancargo test --workspace— 193 pass, 0 failNon-goals (per issue)
Closes #346