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.
TypeScript
Honest Query Embed and Safe Upgrades - implementation plan
Every task is TDD: write the failing test, watch it fail for the expected reason, implement, refactor green. Run the formatter and linter before every commit (bun run fmt, bun run lint, bun run typecheck). One conventional commit per task.
Tasks
Task 1: Effective input window and the query prefix the backend sends
- Files:
src/core/search/types.ts(inputWindowTokens?onResolvedEmbeddingConfig),src/core/search/index.ts(keyembedding_input_window_tokens/ envOPEN_SECOND_BRAIN_EMBEDDING_INPUT_WINDOW_TOKENS,parseInteger(raw, 0, key, { min: 1 }), and post-merge validation invalidateResolvedConfig),src/core/search/embeddings/presets.ts(effectiveInputWindowTokens(semantic): operator key, thendeclaredInputWindowTokens(model), then null;queryPrefixSentByProvider(provider, configuredQueryPrefix)as an exhaustive switch),src/core/search/indexer.ts(censusChunkWindowreadseffectiveInputWindowTokens),tests/core/search/embeddings.input-window.test.ts. - Acceptance:
- absent key: the field stays absent and the
Object.keys(semantic)pin inembeddings.batch-tokens.test.tsis unchanged; - config and env both set the key;
- 0, a fraction and a blank value are refused with
INVALID_INPUTnaming the key, both from the string and from an override; - the operator key beats the preset window, the preset window applies when the key is absent, and an unknown model with no key resolves to null;
queryPrefixSentByProviderreturns the configured query prefix only foropenai-compat;- the census reports an over-window chunk for an uncurated model once the key is set.
- absent key: the field stays absent and the
- Depends on: none
Task 2: Query fit to the window
- Files:
src/core/search/embeddings/query-embed.ts(fitQueryToWindow(query, prefix, windowTokens | null)returning{ text, truncated, sentTokens }),tests/core/search/query-embed.test.ts. - Acceptance:
- a null window returns the query unchanged with
truncated: false; - an ASCII query over the window is cut so that
tokenEstimateCeiling(prefix + text) <= window, and one more code point would exceed it; - a Han query is cut under the ceiling bound, never the floor;
- a cut never splits a surrogate pair (an astral-plane character at the boundary);
- a prefix that alone fills the window gives an empty text with
truncated: true, and the caller treats that as a degradation, not an embed; sentTokenscounts the prefix.
- a null window returns the query unchanged with
- Depends on: Task 1
Task 3: The query-embed gateway (reach and price gate)
- Files:
src/core/search/embeddings/query-embed.ts(prepareQueryEmbed(config, query, reach)returningrefused | ready, usingactiveSpendQuote,COST_GATE_KEY,EMBEDDING_PRICE_SOURCE,TRANSPORT_REACH),tests/core/search/query-embed.test.ts. - Acceptance:
- remote + positive gate + unpriced:
refused, carrying the model; - local + positive gate + unpriced:
ready; - remote + gate 0:
ready; - remote + positive gate + operator-priced:
ready; - the local provider:
ready(builtin price 0); - the
readyresult carries the fitted text from Task 2 and the quote; - no provider is constructed by the gateway (it is pure over config).
- remote + positive gate + unpriced:
- Depends on: Task 2
Task 4: Reach threaded into the semantic lane, gated and fitted
- Files:
src/core/search/search.ts(pass the resolved reach intosemanticLaneInput),src/core/search/pipeline/semantic-lane.ts(SemanticLaneInput.transportReach, forwarded),src/core/search/semantic-phase.ts(reach in opts,prepareQueryEmbedafter the capability guard, the explicit arm throwsEMBEDDING_COST_UNPRICEDnaming the model, the gate key and the price pair, the implicit arm warns plussemanticCostUnpriced, a cut warns plussemanticQueryTruncated { windowTokens }, the fitted text is embedded),src/core/search/retrieval-trail.ts(two members, the closed list, the sentence switch),src/core/search/types.ts(fix the stale reach docblock at:904-907),tests/core/search/semantic-phase-spend-gate.test.ts. - Acceptance, using
startFakeHttpandcallCount():- remote implicit search under a positive gate and an unpriced model sends no embed, returns keyword results, and the trail carries
semantic-cost-unpricedandhybrid-degraded; - remote explicit search throws
EMBEDDING_COST_UNPRICEDwithcallCount() === 0; - local search embeds once;
- an MCP
brain_searchoverMCPServer({..},{ reach: "remote" })refuses before the embed, and a local server embeds once; - an over-window query with a declared window embeds the cut text (the captured
inputlength is checked) and the trail carriessemantic-query-truncatedwithouthybrid-degraded; - the existing
semantic-degrade-quota,semantic-explicit-blocked,capability-tierandhybrid-deadlinetests stay green.
- remote implicit search under a positive gate and an unpriced model sends no embed, returns keyword results, and the trail carries
- Depends on: Task 3
Task 5: The semantic belief order adopts the gateway
- Files:
src/core/brain/belief-semantic.ts(replace the inline gate withprepareQueryEmbed, resolve an omitted reach withresolvedTransportReach, embed the fitted text,queryTokensfrom the gateway, a cut warning inwarnings, fix the docblocks at:85-86and:257-260),tests/core/brain/belief-semantic.test.ts,tests/mcp/context-pack-tool.test.ts. - Acceptance:
- the existing remote refusal (
:449) and local embed (:464) tests stay green; - a call with no reach under a positive gate and an unpriced model is now refused;
- the disclosed
query_tokensincludes the instruction prefix for an e5 model; - an over-window query is embedded cut, with one warning;
- the wire test
:875stays green.
- the existing remote refusal (
- Depends on: Task 3
Task 6: Recall feedback discloses the gated re-run
- Files:
src/core/search/feedback.ts(the outcome gainsdegraded: ReadonlyArray<RetrievalDegradationCode>from the re-run trail),src/mcp/search-tools.ts(additivedegradedin thebrain_recall_feedbackresponse),tests/mcp/recall-feedback-tool.test.ts. - Acceptance:
- a remote feedback call under a positive gate and an unpriced model sends no embed, records the event, and returns
degradedcontainingsemantic-cost-unpriced; - a local call embeds once and returns an empty
degraded; - the CLI twin
learned-weightsis unchanged.
- a remote feedback call under a positive gate and an unpriced model sends no embed, records the event, and returns
- Depends on: Task 4
Task 7: Hook refusals become visible
- Files:
src/core/brain/recall-inject.ts(RecallResultSet.degraded, filled fromoutcome.retrievalTrail?.degradedindefaultRecallRetriever; the codes are carried ontorecallInjectAuditDetails),src/core/brain/gaps/gap-recall.ts(the same codes ongapScopedRecallRetriever's result),tests/core/brain/recall-inject*.test.ts,tests/core/brain/gaps/*.test.ts. - Acceptance:
- the recall-inject retriever under a positive gate and an unpriced model sends no embed, returns keyword candidates, and its result carries
semantic-cost-unpriced; - the local audit details carry the codes;
- the synced telemetry metadata (
recallInjectTelemetryMetadata) is unchanged; - no hook passes local reach (the reserved-page filter is unchanged);
- existing recall-inject and gap-promote tests stay green.
- the recall-inject retriever under a positive gate and an unpriced model sends no embed, returns keyword candidates, and its result carries
- Depends on: Task 4
Task 8: Extra request body for OpenAI-compatible endpoints
- Files:
src/core/search/types.ts(extraBody?: Readonly<Record<string, unknown>>),src/core/search/index.ts(keyembedding_extra_body/ envOPEN_SECOND_BRAIN_EMBEDDING_EXTRA_BODY, JSON object parse,RESERVED_EMBEDDING_BODY_KEYSwith normalised matching,INVALID_INPUTnaming the key or env variable and the refused spellings),src/core/search/embeddings/openai-compat.ts(spreadextraBodybefore the owned fields),tests/core/search/embeddings.extra-body.test.ts. - Acceptance:
- absent key: the request body is exactly
{ model, input, encoding_format }(a new pin via the fake-http handler body); - a declared
{ "dimensions": 256, "user": "x" }arrives in the body next to the owned fields; - each of
model,Input,encoding-formatis refused, naming the spelling; - invalid JSON, an array, a scalar and a blank value are each refused, naming the key;
- an env value beats config;
- the embedding identity is unchanged by the key (no reindex is required);
- a response width that differs from the stored index surfaces as a named error, not an empty result (if not, refuse a
dimensionsthat disagrees withembedding_dimensionat resolution and pin that instead).
- absent key: the request body is exactly
- Depends on: none
Task 9: Compare-before-write primitive
- Files:
src/core/fs-atomic.ts(AtomicWriteOptions.expectBefore?: string | null,FileDriftError,isFileDrift),tests/core/fs-atomic.test.ts. - Acceptance:
- matching bytes write;
- different bytes throw
FileDriftErrornaming the path and leave the target untouched; nullwith an absent target writes, andnullwith a present target throws;- a string expectation with an absent target throws;
- the option composes with
skipIfUnchanged; - no temp file is left after a refusal.
- Depends on: none
Task 10: Upgrades apply the shown plan and refuse drift
- Files:
src/core/brain/upgrade.ts(before: string | null,applyUpgrade(vault, { plan?, agent?, now? }), a pre-snapshot drift pass,expectBeforeper write,BrainUpgradeError.drifted),src/cli/brain/upgrade-render.ts(null-safebefore_sizeand diff),src/cli/brain/verbs/upgrade.ts(pass the shown plan),src/core/maintenance/self-heal-upgrade.ts(pass its plan; drop the double plan),tests/core/brain/upgrade-drift.test.ts,tests/core/brain/upgrade.test.ts,tests/core/maintenance/self-heal-upgrade.test.ts,tests/cli/brain-upgrade.test.ts. - Acceptance:
- a missing
_brain.yamlplansbefore: null, and an empty one plansbefore: ""; - a hand edit made after the plan and before the apply refuses with
driftednaming the path, no snapshot is created, and the edit survives; - an edit injected between the snapshot and the write (via a spy on
createSnapshot) refuses at that row with the rollback command, and the edit survives; - a clean apply is unchanged (snapshot, log row, idempotent re-run);
- the CLI
--apply --yesapplies the plan it printed; - the worker calls
planUpgradeonce per attempt; - all existing upgrade, self-heal and CLI tests stay green.
- a missing
- Depends on: Task 9
Task 11: Docs, CHANGELOG and version 1.73.0 (phase 5, in osb-pr-prepare)
- Files:
CHANGELOG.md(## [1.73.0] - YYYY-MM-DDwith the day the PR is prepared and the compare link),README.md("What is new"),docs/cli-reference.md(both keys, the query gate, the egress sentence),docs/how-it-works.md(the gateway bullet, plus a correction to the 1.72.0 "as for search" sentence in the current-behaviour text),docs/mcp.md(thebrain_searchspend refusal,brain_recall_feedback.degraded, the two degradation codes),docs/updating.md("Upgrading to 1.73.0": hooks under a positive gate, declare a price for a self-hosted model),skills/embeddings-setup/SKILL.md, thenbun run sync-plugin-mirrors, thepackage.jsonbump plusbun run scripts/sync-version.ts. - Acceptance:
bun run sync-version:checkandbun run sync-plugin-mirrors:checkpass; every key, code and command named in the docs exists in the source. - Depends on: Tasks 1-10