Skip to content

ADR-006: Pass-by-Reference Architecture

Accepted

When integrating rlm-rs with LLM tools like Claude Code, search results need to be communicated efficiently. The question is whether to:

  1. Return full chunk content in search results (pass-by-value)
  2. Return chunk IDs that can be dereferenced later (pass-by-reference)

This affects context window usage, response latency, and workflow flexibility.

  1. Context bloat: Embedding full content in search results consumes LLM context
  2. Wasted tokens: Search results may include irrelevant chunks that are never used
  3. Inflexibility: Cannot defer content retrieval to a later step
  1. Context efficiency: Minimize LLM context usage until content is actually needed
  2. Selective retrieval: Allow retrieving only chunks that will be used
  3. Integration flexibility: Support two-phase workflows (search then fetch)
  1. Performance: Smaller search responses are faster
  2. Debugging: Chunk IDs provide stable references for logging
  3. Caching: IDs enable caching strategies

Description: Search returns chunk IDs and metadata; content retrieved separately via chunk command.

Technical Characteristics:

  • Search returns: {id, score, buffer_name, byte_range, preview}
  • Separate chunk <id> command retrieves full content
  • Two-phase workflow: search → select → fetch

Advantages:

  • Minimal context usage in search phase
  • LLM can decide which chunks to retrieve
  • Smaller, faster search responses
  • Stable references for multi-step workflows

Disadvantages:

  • Requires additional command invocation
  • Slight complexity increase for simple use cases

Risk Assessment:

  • Technical Risk: Low. Simple ID lookup
  • Schedule Risk: Low. Straightforward implementation
  • Ecosystem Risk: Low. Pattern is well-understood

Description: Search returns complete chunk content in results.

Technical Characteristics:

  • Search returns: {id, score, content, ...metadata}
  • Single command for search and retrieval
  • All content immediately available

Advantages:

  • Simpler single-step workflow
  • No additional commands needed

Disadvantages:

  • Large responses consume context
  • Wasted tokens for unused chunks
  • Slower response times
  • Cannot scale to large result sets

Disqualifying Factor: Context inefficiency makes this unsuitable for LLM integration where context is precious.

Risk Assessment:

  • Technical Risk: Low. Simple to implement
  • Schedule Risk: Low. Less code
  • Ecosystem Risk: Medium. Poor scaling

Option 3: Hybrid with Configurable Expansion

Section titled “Option 3: Hybrid with Configurable Expansion”

Description: Return IDs by default, optionally expand content inline.

Technical Characteristics:

  • Default: ID-only results
  • --expand flag includes content
  • Best of both approaches

Advantages:

  • Flexibility for different use cases
  • Backwards compatible

Disadvantages:

  • More complex API
  • Users must choose mode

Risk Assessment:

  • Technical Risk: Low. Straightforward flag
  • Schedule Risk: Low. Minor addition
  • Ecosystem Risk: Low. Optional feature

Adopt pass-by-reference as the primary architecture with IDs for chunk retrieval.

The implementation will provide:

  • search command returning chunk IDs, scores, and previews (truncated content)
  • chunk command for retrieving full content by ID
  • Preview field with first ~100 characters for quick assessment
  • JSON output with structured chunk references
  1. Context efficiency: Search results use minimal context; content fetched on-demand
  2. LLM agency: LLM can intelligently select which chunks to retrieve
  3. Scalable results: Can return many search hits without context explosion
  4. Stable references: Chunk IDs persist across sessions for reproducible workflows
  1. Two-step workflow: Simple use cases require search + chunk commands
  2. Learning curve: Users must understand ID-based retrieval pattern
  1. Preview field: Provides quick content glimpse without full retrieval

Pass-by-reference enables efficient LLM integration by keeping search responses small and deferring content retrieval to when it’s needed. This pattern is essential for Claude Code hooks where context efficiency directly impacts quality.

Mitigations:

  • Include preview field in search results for quick assessment
  • Clear documentation on two-phase workflow
  • Consider future --expand flag for simple use cases
  • Date: 2025-01-15
  • Source: v1.0.0 release design decisions
  • Related ADRs: ADR-001, ADR-003, ADR-008

Status: Compliant

Findings:

FindingFilesLinesAssessment
Search returns IDssrc/cli/search.rs-compliant
Chunk command implementedsrc/cli/chunk.rs-compliant
Preview field in resultssrc/storage/search.rs-compliant
JSON output structuredsrc/cli/allcompliant

Summary: Pass-by-reference architecture fully implemented with search and chunk commands.

Action Required: None