Skip to content

Add deja-local: SQLite-backed vector memory for AI agents#9

Merged
acoyfellow merged 6 commits intomainfrom
claude/local-vector-memory-oyZal
Mar 22, 2026
Merged

Add deja-local: SQLite-backed vector memory for AI agents#9
acoyfellow merged 6 commits intomainfrom
claude/local-vector-memory-oyZal

Conversation

@acoyfellow
Copy link
Copy Markdown
Owner

Summary

Introduces deja-local, a new package providing durable, local vector memory for AI agents. The implementation includes a complete SQLite-backed memory store with semantic search, confidence scoring, conflict resolution, and audit logging.

Key Changes

  • Core memory store (src/index.ts): Implements createMemory() function that provides:

    • remember() - Store memories with automatic deduplication and conflict resolution
    • recall() - Semantic search with query decomposition for complex queries
    • confirm()/reject() - Feedback mechanism to boost/demote memory confidence
    • forget() - Permanent memory deletion
    • list() / recallLog() - Inspection of stored memories and audit trail
  • Embedding support:

    • Default embedding via HuggingFace Transformers (all-MiniLM-L6-v2)
    • Custom embedding function support for flexibility
  • Intelligent memory management:

    • Deduplication: Skips near-identical memories (≥0.95 similarity)
    • Conflict resolution: Supersedes contradictory memories (0.6-0.95 similarity range) by reducing old memory confidence
    • Confidence scoring: Memories start at 0.5, boosted by confirm() (+0.1) and decayed by reject() (-0.15), clamped to [0.01, 1.0]
    • Recall ranking blends relevance (70%) with confidence (30%)
  • Query decomposition: Complex queries are automatically split into keyword pairs and individual terms for broader recall coverage

  • Durability & audit:

    • SQLite with WAL mode for ACID guarantees
    • Every recall is logged with context, matched memory IDs, scores, and timestamp
    • Schema migration support for backward compatibility
  • Comprehensive test suite (test/index.test.ts): 544 lines covering:

    • Durability (process restart survival, disk persistence)
    • Consistency (zero-lag recall, immediate forget)
    • Deduplication (identical and near-identical text)
    • Auditability (recall logging, persistence)
    • Correctness (relevance ranking, threshold/limit options)
    • Confidence ratchet (boost/reject mechanics, persistence)
    • Conflict resolution (superseding, ranking impact)
    • Recall decomposition (complex query handling)
    • Backward compatibility (legacy schema migration)
  • Documentation (README.md): Complete API reference with examples and configuration guide

Notable Implementation Details

  • Uses deterministic cosine similarity for vector comparison
  • In-memory index loaded from SQLite at startup for fast recall
  • Prepared statements for all database operations
  • Stop-word filtering for query decomposition
  • Confidence clamping to 3 decimal places for stability
  • Backward compatibility layer for learn() alias and schema migrations

https://claude.ai/code/session_01Fb57JFi8VM36x8pCM1BFTw

claude added 6 commits March 18, 2026 21:34
…nsistency

Local vector brain for agents that need instant recall. No network,
no external services, no waiting for indexes to sync.

- Built-in character n-gram embedder (384 dims, ~0.1ms, zero deps)
- Pluggable embed function (swap in OpenAI/Anthropic/local model)
- Same learn/inject/query API shape as deja-client
- Optional JSON file persistence
- Brute-force cosine similarity (fast for <100k vectors)
- 14 tests passing

https://claude.ai/code/session_01Fb57JFi8VM36x8pCM1BFTw
Default embedder is now all-MiniLM-L6-v2 running locally via ONNX.
~23MB model cached after first download, ~5ms per embed after that.
No network needed for recall. Zero eventual consistency.

- embed: 'ngram' for zero-dep fallback
- embed: yourFn for custom (OpenAI, etc)
- model: option to swap ONNX model
- Extracted shared search() helper, removed duplication

https://claude.ai/code/session_01Fb57JFi8VM36x8pCM1BFTw
… else

Nuked all speculative complexity: trigger/learning split, scopes,
confidence, reason, source, recallCount, lastRecalledAt, Stats,
query vs inject distinction, LearnOptions, InjectOptions, QueryOptions.

What's left:
- learn(text) → store a memory
- recall(context) → get relevant memories back
- forget(id), list(), clear(), save(), load()
- Real embeddings via transformers.js (default)
- N-gram fallback for zero-dep mode
- Custom embed function support

API surface: createMemory() returns a MemoryStore.

https://claude.ai/code/session_01Fb57JFi8VM36x8pCM1BFTw
…gs only

This is a complete rewrite optimizing for reliability over simplicity:

- SQLite storage (bun:sqlite) — ACID durable, WAL mode
- Every learn() hits disk before returning
- Every recall() is logged to an audit table (context + matched IDs + scores)
- Deduplication: near-identical memories are not stored twice
- N-gram fallback removed — real embeddings only (transformers.js)
- In-memory vector index rebuilt from DB on startup
- path is required — no in-memory-only mode

Trust guarantees verified by 19 tests across 4 categories:
durability, consistency, deduplication, auditability

https://claude.ai/code/session_01Fb57JFi8VM36x8pCM1BFTw
- Rename learn() → remember(), add backward-compat alias
- Add confirm()/reject() for confidence scoring (the ratchet)
- Memories start at 0.5 confidence, confirm boosts +0.1, reject drops -0.15
- Recall ranks by blended score: 70% relevance + 30% confidence
- Conflict resolution: memories with 0.6-0.95 similarity are detected as
  conflicts — new memory supersedes old, old memory's confidence drops to 30%
- Recall decomposition: complex queries are split into keyword pairs and
  individual terms, each embedded independently for broader recall
- Schema migration for existing databases (adds confidence + supersedes columns)
- 33 tests covering all new features + backward compat
- Add README with full API documentation

https://claude.ai/code/session_01Fb57JFi8VM36x8pCM1BFTw
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7a34d2533a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

* ```
*/

import { Database } from 'bun:sqlite'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove the Bun-only bun:sqlite import from the published entrypoint

This package is advertised as npm install deja-local and ships standard import/require exports, but the first line of the runtime entrypoint imports bun:sqlite. Plain Node cannot resolve the bun: scheme, so any non-Bun consumer will fail at module load time before createMemory() can run. Unless this package is explicitly marked Bun-only, the published entrypoint needs a runtime-neutral SQLite binding.

Useful? React with 👍 / 👎.

// Open database, enable WAL mode, create schema
const db = new Database(opts.path)
db.exec('PRAGMA journal_mode = WAL')
db.exec('PRAGMA synchronous = NORMAL')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use crash-safe sync mode for committed memory writes

journal_mode=WAL combined with synchronous = NORMAL can still lose the most recent committed transactions if the host crashes or loses power immediately after remember(), confirm(), or reject() returns. For a library whose core promise is durable cross-session memory, that means users can silently lose the latest memories in the exact restart scenario they are trying to survive.

Useful? React with 👍 / 👎.

@acoyfellow acoyfellow merged commit 31b975a into main Mar 22, 2026
3 checks passed
acoyfellow pushed a commit that referenced this pull request Mar 23, 2026
1. Mark package as Bun-only (engines field + description) since bun:sqlite
   cannot be resolved by Node.js consumers.
2. Upgrade synchronous pragma from NORMAL to FULL to prevent data loss
   on host crash with WAL mode — honoring the durability promise.

https://claude.ai/code/session_01Fb57JFi8VM36x8pCM1BFTw
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.

2 participants