Skip to content

refactor: eliminate 4 Rust backend duplications (retrieval lock, cosine sim, DiscoverMode, note context)#77

Merged
WKJBryan merged 4 commits into
mainfrom
refactor/phase1-retrieval-helper
Jun 9, 2026
Merged

refactor: eliminate 4 Rust backend duplications (retrieval lock, cosine sim, DiscoverMode, note context)#77
WKJBryan merged 4 commits into
mainfrom
refactor/phase1-retrieval-helper

Conversation

@WKJBryan

@WKJBryan WKJBryan commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Summary

Removes four distinct instances of duplicated logic in the Rust backend, all with no behavior change:

  • run_retrieval helper — the 4-service read-lock pattern (search + graph + priority + retrieval) followed by retrieval.retrieve() was written out identically in commands/retrieval.rs, commands/memory.rs, and twice inline in commands/canvas.rs. Extracted to a single pub(crate) fn run_retrieval() in commands/mod.rs.

  • fetch_note_contexts helper — two places in canvas.rs both locked the knowledge store, iterated retrieval results, truncated content to 1500 chars, and built (id, title, content) tuples. Extracted to a private helper. The twin path's context_notes filtering is preserved via a HashSet of found IDs.

  • DiscoverMode enumcommands/zettelkasten.rs defined a private DiscoverMode with identical variants and parse() to services::link_discovery::DiscoverMode, then converted between them with to_service_mode(). Removed the local copy; import the service type directly (safe: IPC receives mode as Option<String>, no serialization boundary).

  • cosine_similarity — standalone pub fn cosine_similarity(HashMap, HashMap) -> f64 in link_discovery.rs was identical math to SparseTfVector::cosine_similarity() in similarity.rs. Added pub fn sparse_cosine() to similarity.rs; link_discovery.rs imports and uses it.

Test plan

  • All 178 Rust tests pass (cargo test)
  • Clippy error count unchanged (34 pre-existing errors, 0 new)
  • Lock precondition verified by code-reading: both canvas call sites use scoped {} blocks that drop all 4 locks before returning

🤖 Generated with Claude Code

WKJBryan and others added 4 commits June 9, 2026 16:55
…plication

The 4-service read-lock sequence (search + graph + priority + retrieval)
followed by retrieval.retrieve() was written out identically in
commands/retrieval.rs, commands/memory.rs, and twice inline in
commands/canvas.rs. Extract it into a pub(crate) helper in commands/mod.rs.

recall_relevant now calls the helper and maps RetrievalResult → RecallResult.
retrieve_relevant and the two canvas context resolvers call it directly.
No behavior change; all 178 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…e-access logic

Two places in canvas.rs both locked the knowledge_store, iterated retrieval
results, called store.get_note(), truncated content to 1500 chars, and built
(id, title, content) tuples — once in resolve_note_level_context and once in
the note-level fallback inside resolve_twin_prompt_context.

Extract the shared logic into fetch_note_contexts(). The twin fallback now
calls the helper and uses a HashSet of found IDs to filter context_notes,
preserving the existing behavior of excluding unfound notes from both vecs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
commands/zettelkasten.rs defined a private DiscoverMode enum with identical
variants and parse() logic to the canonical services::link_discovery::DiscoverMode,
then converted between them via to_service_mode(). Import the service type
directly, remove the local copy and the converter function. IPC receives mode
as a raw Option<String>, so no serialization contract is affected.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…cosine()

link_discovery.rs contained a standalone cosine_similarity() function
operating on HashMap<String, f64> term vectors, identical in math to
SparseTfVector::cosine_similarity() in similarity.rs. Add sparse_cosine()
as a free function in similarity.rs and have link_discovery.rs import and
use it. The local definition and its two call sites in link_discovery.rs are
updated accordingly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@WKJBryan WKJBryan force-pushed the refactor/phase1-retrieval-helper branch from fb18810 to d09cbc8 Compare June 9, 2026 08:55
@WKJBryan WKJBryan merged commit a37ca52 into main Jun 9, 2026
10 checks passed
@WKJBryan WKJBryan deleted the refactor/phase1-retrieval-helper branch June 9, 2026 09:08
@WKJBryan WKJBryan mentioned this pull request Jun 9, 2026
4 tasks
WKJBryan added a commit that referenced this pull request Jun 9, 2026
## Release v0.1.15

### Changes since v0.1.14

**Rust backend — deduplication refactor (PR #77)**
- Extract `run_retrieval()` helper in `commands/mod.rs` to eliminate 4x
duplicated lock-acquire pattern across `retrieve_relevant`,
`recall_relevant`, and both canvas context resolvers
- Extract `fetch_note_contexts()` helper in `commands/canvas.rs` to
deduplicate note content assembly
- Remove duplicate `DiscoverMode` enum from `commands/zettelkasten.rs` —
now uses `services::link_discovery::DiscoverMode` directly
- Consolidate `cosine_similarity` into `services/similarity.rs` as
`sparse_cosine()` free function

**Frontend — component refactors (PRs #78#80)**
- `BaseModal.vue`: shared modal shell replacing 7 hand-rolled overlay
implementations
- `useAsyncOperation` composable: eliminates repeated
loading/error/try-catch/finally store boilerplate
- `PanelHeader.vue` + `AsyncListState.vue`: shared panel header and
loading/empty tristate for 5 sidebar panels

## Checklist
- [x] `npm run release:prepare -- 0.1.15` passed (versions bumped,
Cargo.lock regenerated, 8 Cargo graphs validated)
- [ ] CI passes
- [ ] Merged to main
- [ ] `npm run release:tag -- 0.1.15` + `git push origin v0.1.15`

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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.

1 participant