Skip to content

feat: add get-related chunk similarity lookup (more-like-this)#397

Merged
RobertLD merged 3 commits intomainfrom
feat/get-related-395
Mar 16, 2026
Merged

feat: add get-related chunk similarity lookup (more-like-this)#397
RobertLD merged 3 commits intomainfrom
feat/get-related-395

Conversation

@RobertLD
Copy link
Copy Markdown
Owner

Closes #395

Summary

  • getRelatedChunks() in src/core/search.ts — new exported function that fetches a chunk's stored embedding from chunk_embeddings, runs an ANN vector search via sqlite-vec directly (no embedding provider call), excludes the source document by default, and optionally blends in explicitly linked documents at a boosted score
  • get-related MCP tool in src/mcp/server.ts — exposes getRelatedChunks as an MCP tool with schema fields for chunkId, limit, topic, library, tags, minScore, includeLinkedDocuments
  • related <chunkId> CLI command in src/cli/index.tslibscope related <chunkId> with --limit, --topic, --library, --min-score, --tags options; output matches search command style
  • tests/unit/get-related.test.ts — 9 unit tests covering: chunk-not-found error, no-embedding error, sourceChunk metadata, excludeDocumentId behavior, includeLinkedDocuments SQL logic, minScore filtering, limit

Key design decisions

  • getRelatedChunks is synchronous (matches all other DB functions in this codebase)
  • Uses direct SQL with embedding MATCH ? (sqlite-vec ANN) rather than delegating to searchDocuments, skipping the embedding provider entirely — fast, offline-capable, and guaranteed to use the same embedding space
  • Uses validateRow/validateRows + Zod schemas (consistent with existing codebase patterns)
  • includeLinkedDocuments fetches linked doc chunks via plain SQL and boosts their score to 0.6 so they surface even when below the vector similarity threshold

Test plan

  • All 9 unit tests pass (vitest run tests/unit/get-related.test.ts)
  • TypeScript type-checks cleanly (tsc --noEmit)
  • Manual: libscope related <chunkId> with an indexed workspace
  • Manual: MCP get-related tool via Claude Desktop / MCP client

🤖 Generated with Claude Code

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
libscope Ignored Ignored Mar 16, 2026 5:20pm

@RobertLD RobertLD force-pushed the feat/get-related-395 branch 2 times, most recently from d307a52 to 16d1ca7 Compare March 16, 2026 17:10
RobertLD and others added 3 commits March 16, 2026 17:20
Implements "more-like-this" functionality that seeds a vector similarity
search from an existing chunk's stored embedding, bypassing the embedding
provider entirely. Adds getRelatedChunks() to src/core/search.ts, a
get-related MCP tool, and a `libscope related <chunkId>` CLI command.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Remove unnecessary async from withErrorHandling callback (no await)
- Remove unused createTestDb import from test file
- Replace unused catch bindings (_err) with bare catch clauses

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@RobertLD RobertLD force-pushed the feat/get-related-395 branch from 16d1ca7 to 2f0288f Compare March 16, 2026 17:20
@RobertLD RobertLD merged commit 69b0bc7 into main Mar 16, 2026
9 checks passed
@RobertLD RobertLD deleted the feat/get-related-395 branch March 16, 2026 17:22
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.

Add get-related: chunk-seeded similarity lookup (more-like-this)

1 participant