Local-first 🧠memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.
Recall & Ranking Suite - sharpen the retrieval, recall-budget, and pre-injection layers
Status: draft Author: claude-vps-agent (via feature-release-playbook) Audience: implementation
Problem statement
Open Second Brain's retrieval layer fuses FTS5, semantic vectors, MMR diversity,
link traversal, entity boost, and tier weighting (v0.13.0), but recency is a
hardcoded step function, every query is treated identically regardless of what it
is asking for, identical queries are recomputed from scratch, and a query whose
wording differs from the stored wording under-recalls. Separately, the
context-injection surface (brain_context_pack) budgets only by token count - a
single oversized preference can still crowd out the rest - and there is no
read-only bundle an external runtime can inject just before a compression event.
Each gap is something a memory product is expected to address; together they are
the recall-quality and context-economy core of the product.
Scope
Eight features across seven atomic units, one pull request, separate commits, in dependency order. All are individually disable-able and bounded.
- F1 - Weibull recency decay. Replace the stepwise
recencyBoostinranker.tswith a configurable Weibull curve (shape + scale). Default parameters approximate today's curve; bounded and deterministic (injectablenowMs). - F2 - Query-intent classification. A pure
QueryPlanstage classifies the query into a structural intent and emits aweightProfilethat nudges the keyword/semantic/entity/recency mix. Structural signals only - no language tables. - F3 - Synonym / query expansion.
QueryPlan.expandedTermsaugment the FTS candidate set, language-agnostically (vault-derived term co-occurrence; optional embedding-neighbour expansion when the embedding provider is configured). No-op (bit-identical) when no expansion signal exists. - F4 - Per-memory character cap. A shared
recall-budgetprimitive trims individual oversized entries by score order before they consume the bundle. Consumed bycontext-pack.ts. - F5 - Total recall character cap. A second budget axis alongside
max_tokens, discarding lowest-scored overflow. Same shared primitive. - F6 - Persistent query cache + corpus-generation invalidation. A SQLite-backed
query_cachetable (additive migration v4) keyed by(normalizedQuery, scope, planHash, corpusGeneration)with a TTL. ThecorpusGenerationfingerprint (embedding model + dimension + schema version + monotonic index revision) is derived fromindex_state; rows whose generation differs from current are never served and are swept. A cache hit is byte-identical to a fresh compute. The index-revision component is what makes a content reindex (which changes the index DBsearch()reads from, but not the embedding model/dimension/schema) invalidate the cache. - F7 -
brain_pre_compress_packMCP tool. Read-only tool returning a budgeted bundle of top-K highest-confidence preferences plus the head ofactive.md, formatted as a system-prompt addendum. Reuses the F4/F5 budget primitive. OSB ships only the tool; host-runtime wiring is an out-of-scope recipe.
(F6 bundles the cache and its corpus-generation invalidation gate into one atomic unit because the gate is meaningless without the cache it guards - the "smaller first cut" the source task calls for.)
Out of scope
- In-memory-only caching (rejected: the CLI runs a fresh process per call).
- Per-page corpus-generation hashing (the gbrain full model); generation is tracked at embedding-model/dimension/schema granularity - the documented smaller first cut.
- LLM-based intent classification or LLM query rewriting (non-deterministic, language-coupled).
- Any per-language keyword / synonym / stopword list (forbidden by the language-agnostic invariant).
- Host-runtime (Hermes) pre-compress wiring - shipped separately as a recipe.
- Changing the meaning of existing
BrainSearchResultfields.
Chosen approach
Variant 2 (dedicated query-analysis layer), per variants.md. A new pure module
query-plan.ts runs one structural pass over the query and returns
QueryPlan { intent, weightProfile, expandedTerms, planHash }. search.ts
consumes it: expandedTerms augment FTS candidates; weightProfile is threaded
into rankResults as per-query weight multipliers. Weibull recency stays a pure
config-driven curve inside ranker.ts (not routed through the plan). A persistent,
generation-gated query_cache wraps search(). A shared recall-budget primitive
backs both context-pack.ts and the new brain_pre_compress_pack tool.
Pipeline after the change (search.ts):
buildQueryPlan(query) [F2 intent, F3 terms, planHash]
-> query-cache lookup (normQuery, scope, planHash, generation) -- hit? return [F6]
-> fts candidates (+ expandedTerms augmentation) [F3]
-> semantic candidates -> hydrate
-> rankResults(... weightProfile, Weibull recency ...) [F1, F2]
-> traversal -> MMR -> property filter -> visibility -> slice (unchanged v0.13.0)
-> query-cache store (with current generation, TTL) [F6]
Graceful degradation, per feature:
- F1: defaults approximate the prior curve; a config flag can restore exact prior steps if needed. Pure, deterministic.
- F2:
weightProfiledefaults to neutral (all multipliers 1.0) for an unclassifiable query -> bit-identical ranking. - F3: empty
expandedTerms(no co-occurrence signal, no embeddings) -> identical candidate set. - F4/F5: zero/unset caps -> no trimming (today's behaviour).
- F6: cache disabled by config, or empty on first query, or generation mismatch -> always falls through to a fresh compute that equals the uncached path.
- F7: additive new tool; no existing surface changes.
Design decisions
QueryPlanis the single audit seam for the language-agnostic invariant. Both intent classification and synonym expansion derive exclusively from structural signals computed once here:extractEntities(already structural), quoted-phrase spans, FTS wildcard/boolean operators, wikilink-shaped tokens, digit/date tokens, token count. No natural-language word list appears anywhere.- Intent is structural, not semantic. Intents are derived from query shape (e.g. high entity share -> entity-leaning profile; a quoted exact phrase -> keyword-leaning, expansion suppressed; long natural-language run with no entities -> semantic/recency-leaning). The mapping is a small fixed table from structural features to weight multipliers - never from words to meaning.
- Weibull stays pure and config-driven, outside the plan.
recencyBoost(age)becomesweibullDecay(ageDays, shape, scale, amplitude); recency does not depend on the query, so threading it throughQueryPlanwould be artificial coupling. - Synonym expansion is vault-derived, never a bundled dictionary. Primary source: co-occurrence statistics already implicit in the index (terms frequently sharing chunks). Optional: nearest-neighbour terms via the existing embedding provider when configured. Both are language-agnostic by construction and degrade to no-op without data.
- The query cache is persistent and generation-gated. New
query_cachetable (migration v4, additive, reindex-safe), columns include the serialized result,corpus_generation, andcreated_atfor TTL. Lookups filter on the current generation, so an embedding-model/dimension/schema change silently invalidates the cache. A cache hit is asserted byte-identical to a fresh compute.corpusGenerationis a pure function of(embedding_model, embedding_dimension, LATEST_SCHEMA_VERSION, index_revision)read fromindex_state.index_revisionis a monotonic counter the indexer bumps whenever it actually mutates the index (added + updated + deleted > 0), so a content reindex invalidates the cache even though the embedding model/dimension/schema are unchanged -search()reads only from the index DB, so the cache stays exactly as fresh as that DB. - One shared budget primitive.
recall-budget.tsexposes a pureapplyBudget(entries, { maxTokens?, maxCharsPerEntry?, maxTotalChars? })that trims per-entry then enforces the total, discarding lowest-scored overflow.context-pack.tsandbrain_pre_compress_packboth consume it - no duplicated trimming logic. brain_pre_compress_packreuses ranking + budget, adds no new storage. It selects top-K confirmed preferences by confidence/recency, prepends the head ofactive.md, and renders a compact system-prompt addendum under the same budget primitive. Read-only; ToolScope consistent with other read tools.
File changes
New:
src/core/search/query-plan.ts- purebuildQueryPlan(query)->{ intent, weightProfile, expandedTerms, planHash }.src/core/search/recency.ts- pureweibullDecay(ageDays, opts)(or folded intoranker.tsif cleaner).src/core/search/synonyms.ts- pure expansion given a co-occurrence/embedding-neighbour source.src/core/search/query-cache.ts- persistent cache get/put + generation gate + TTL sweep (I/O via Store).src/core/search/corpus-generation.ts- pure generation fingerprint from index_state values.src/core/brain/recall-budget.ts- shared pure budget primitive.src/core/brain/pre-compress-pack.ts- core builder for the pre-compress bundle.- Test files under
tests/core/search/andtests/core/brain/for each new module, plus integration tests in the search/context-pack suites.
Modified:
src/core/search/ranker.ts- Weibull recency; accept per-queryweightProfile.src/core/search/types.ts-QueryPlan,weightProfile, recall-config fields (weibull shape/scale/amplitude, intent toggle, expansion toggle, cache TTL/enable);SearchOptionsopt-outs.src/core/search/index.ts- resolve + validate the new config fields andDEFAULTS.src/core/search/search.ts- build plan, augment candidates, thread weightProfile, wrap with cache.src/core/search/store.ts-query_cacheread/write/sweep;corpusGenerationaccessor; co-occurrence accessor for synonyms.src/core/search/schema.ts- migration v4 (query_cachetable); bumpLATEST_SCHEMA_VERSIONto 4.src/core/brain/context-pack.ts- consumerecall-budget(per-memory + total char caps).src/mcp/brain-tools.ts-brain_context_packnew cap args; registerbrain_pre_compress_pack.src/mcp/instructions.ts- document the new tool + cap args.src/cli/brain/verbs/*- surface new flags where a CLI verb maps (context-pack / search), if applicable.README.md,CHANGELOG.md,docs/mcp.md,docs/cli-reference.md,docs/how-it-works.md.
Risks and open questions
- Migration v4 correctness. Adding
query_cacheis additive, but the migration must be reindex-safe and a newer-than-supported DB must still raiseSCHEMA_MISMATCH. Mitigation: a migration test that opens a v3 fixture, migrates, and asserts the table exists and old data is intact. - Cache transparency. A hit must equal a fresh compute. Mitigation: a test that runs a query twice and asserts deep-equality of results, and a test that mutates generation and asserts the stale row is not served.
- Intent over-steering. A misclassified intent must never inject irrelevant docs - only re-weight an already-relevant set. Mitigation: bound weight multipliers to a narrow band and keep the neutral profile bit-identical.
- Synonym precision. Vault-derived expansion can add noise. Mitigation: cap the number of expansion terms, require a minimum co-occurrence threshold, and keep expansion off for quoted exact-phrase queries.
- Determinism. All new ranking/plan/budget math is pure with injectable time;
no
Date.now()/Math.random()inside pure modules.