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.
PR #121 hardening + correctness โ implementation plan
One feature branch (feat/pr121-hardening-correctness), four cards driven one at a time in risk order, each a contiguous commit cluster with per-fix TDD (failing test first). Full suite green between cards. Each later card builds on the commits earlier cards landed โ do not duplicate or conflict with siblings.
Drive order (matches design.md Chosen approach):
t_f27d80feโ index-swap hardening (stabilizes the base; the forced reindex from C.2 will exercise this path).t_2ba5f0c9โ validation/output hardening (low-risk correctness).t_3ccb1512โ Han-bigram tokenizer (low-risk CJK correctness; its tokenization change is absorbed by the C.2 reindex).t_1224c740โ hot-path performance, ending with C.2 (the schema bump + version bump, the release-defining commit).
Task 1: Index-swap hardening โ fix reindex/crash-restore data-loss races (kanban t_f27d80fe, P4)
Rebase the validated local fix ec3bad27 (branch fix/index-swap-hardening) onto this release branch as the first commit cluster, then extend it with the INDEX_UNREADABLE self-heal as a separate follow-on commit.
Files:
src/core/search/store.tsโacquireReindexLock/isReindexInProgress(a dedicated lockfile, separate from the per-Store.openwriter lock);Store.opencrash-restore preamble (~293-306) probesisReindexInProgressnon-blocking and skips restoring while a reindex is actively running.src/core/search/indexer.tsโreindexVaultacquires the reindex lock BEFORE touchingnewPath(thetryUnlink(newPath)+ staging seed at ~659,664 move inside the lock); second concurrentreindexVaultfails fast with the existing typedINDEX_LOCKED.src/core/search/search.ts(NUL-byte sentinel โ usecommand grep -a) โopenReadOrSelfHeal(~210-226) self-heal set extended to includeINDEX_UNREADABLE.tests/core/search/โ the 3 regression tests fromec3bad27(secondreindexVaultrejected while one in progress;Store.opendoes not restore.bakmid-reindex; genuine crash-restore still works) + a new self-heal test assertingINDEX_UNREADABLEis recovered.
Acceptance (a passing test):
- Integration test that starts two overlapping
reindexVaultruns (the second entering during the first's build phase) and asserts the surviving index is complete and readable โ NOT an empty DB, NOTINDEX_UNREADABLE. Store.opendoes not restore.bakduring an in-flight reindex (interleaved open-during-swap test).- Genuine crash-restore path still green (the existing crash-restore test stays green).
INDEX_UNREADABLEis now self-healed byopenReadOrSelfHeal(new test).
Depends on: none (first card; stabilizes the base for the rest).
Task 2: Validation/output hardening โ four independent fixes (kanban t_2ba5f0c9, P4)
Land as up to four commits (one per fix), each TDD. B.1 is the contract-tightening gate (previously-accepted garbage โ INVALID_PARAMS) โ it is why the release is a minor.
B.1 โ created_at full-timestamp validation
- Files:
src/core/brain/continuity/store.ts(boundary ~42-45,176; lexical compare at 213,222-223); possibly a shared validator extracted fromnormalizeDemandTimestamp(DRY โ see B.3). - Acceptance: table-driven accept/reject at the store boundary โ
"2026-13-01T00:00:00Z"rejected (invalid month),"2026-07-06T15:00:00+03:00"rejected or canonicalized toZ(reject is simpler and matches the contract tightening); valid canonicalZtimestamps accepted; one MCP-level case per exposed tool (generation-tools.ts,host-memory-write.ts,session-summary.ts).
B.2 โ surrogate-safe card snippet
- Files:
src/core/search/search.ts(NUL file) โcardSnippet(~1121-1126,CARD_SNIPPET_CHARS=240) truncates on code points ([...collapsed].slice(0, N).join("")orIntl.Segmenter). - Acceptance: snippet fixture with an emoji astral char at the truncation boundary asserts no lone surrogate / no U+FFFD; the snippet ends on a complete code point followed by
....
B.3 โ since/until precision-consistent filtering
- Files:
src/core/brain/query-demand.tsโ filter bounds (238-239) run through448-453) before the lexical compare. Consider extracting a shared canonical-UTC normalizer reused by B.1 (DRY).normalizeDemandTimestamp( - Acceptance: boundary fixture โ a second-precision
--since 2026-07-01T00:00:00Zdoes NOT exclude ms-precision records in[00:00:00.000Z, 00:00:00.999Z].
B.4 โ secret-exec audit redaction
- Files:
src/core/brain/secrets/exec.ts(~88,96-108) โ the loggedcommand = argv.join(" ")is redacted (redactInfra: trueand/or a high-entropy-token pass) beforeappendAuditRecordpersists it. - Acceptance: audit fixture asserting a bare
sk--shaped token passed as a positional argument is masked inBrain/log/secret-custody/*.jsonl.
Depends on: Task 1 (build on the stabilized base).
Task 3: Han-bigram tokenizer โ restrict bigrams to contiguous Han spans (kanban t_3ccb1512, P3)
TDD-first per the card's acceptance. Its tokenization change is absorbed by the single C.2 reindex at release (no separate schema bump here).
Files:
src/core/surface/lexical-score.tsโtokenizeextracts bigrams only from maximal contiguous Han spans within each token, leaving non-Han segments untouched; the full token is still emitted as today.tests/core/surface/lexical-score.test.tsโ the four acceptance cases below.
Acceptance (a passing test):
tokenize("gbrainๅฎ็ฐ")emits the Han bigramๅฎ็ฐand the full token, and NO cross-boundary bigram (nๅฎ).tokenize("abๅฎ็ฐcd")emitsๅฎ็ฐ.- Pure-Han and pure-ASCII tokenization unchanged (existing tests stay green).
- No duplicate emission for a standalone 2-char Han token (
tokenize("ๅฎ็ฐ")stays["ๅฎ็ฐ"]).
Depends on: Task 2 (lands before the perf card; its tokenization change rides the C.2 reindex).
Task 4: Hot-path performance โ six items (kanban t_1224c740, P4)
Land the five contract-neutral items first (C.1, C.3, C.4, C.5, C.6), then C.2 as the release-defining commit (schema bump + version bump). Each item carries a before/after measurement on a synthetic large vault where the card calls for it.
C.1 โ entity-index mtime memo (biggest per-query win)
- Files:
src/core/brain/entities/index-builder.ts(NUL file, ~80-106); consumerssrc/core/search/entity-alias.ts:45andsrc/core/search/query-expansion.ts:106. - Acceptance: an invalidation test โ write a new entity โ the next query that names an entity sees it (mtime probe triggers rebuild). Existing entity suites green. Documented staleness-window trade-off at the call site.
C.2 โ documents.basename column (schema bump; release-defining commit)
- Files:
src/core/search/store.tsโ addbasenamecolumn + index (populated at index time);resolvedDocLinkPairs(~1064-1086) rewritten as equality joins (noSUBSTRfull scan); bumpLATEST_SCHEMA_VERSION;package.json+CHANGELOG.mdโ 1.24.0. - Acceptance: link-resolution suites green; a scale smoke test on a synthetic 5k-doc / multi-k-dangling-link fixture shows the suffix scans eliminated (equality joins). The single release reindex absorbs C.2 and the Task 3 tokenizer change.
C.3 โ buildBacklinkIndex cross-call memo
- Files:
src/core/brain/backlinks.ts(~268-272); callersquery-tools.ts:250,resources.ts:369,doctor.ts:930,explorer.ts:88, moc-audit, concept-cluster. - Acceptance: mtime/generation-keyed memo (or incremental log cursor) with a freshness test; full-history re-read eliminated on repeat calls within the same generation. "Recompute on demand" revisited deliberately, documented.
C.4 โ heal-enrich compile-once regex
- Files:
src/core/brain/heal-run.ts(76-88) +42-58). Sort once, compile once, post-filter self-matches per page; drop the duplicate frontmatter parse atsrc/core/brain/heal-enrich.ts(heal-run:80. - Acceptance: existing heal tests green; a test asserting the alternation regex is compiled once per run, not once per page (compile-count assertion or equivalent).
C.5 โ links normalize --mode short Map-based resolution
- Files:
src/cli/brain/verbs/links.ts(72-86) +60-90).src/core/brain/link-graph/format-wikilink.ts( - Acceptance: a characterization fixture captures current output BEFORE the rewrite; after the rewrite the output is byte-identical. Build a basenameโpaths Map once per run; resolve+disambiguate against it.
C.6 โ continuity reverse-chron early exit
- Files:
src/core/brain/continuity/store.ts(~190-215,readAllRecords); consumerssession-summary.ts:182,pre-compact-extract.ts:148, recall-telemetry/memory-cost/context-receipts. - Acceptance: a
since-aware file-skip test (shards outside the window are not parsed) and a latest-record reverse-chron early-exit test (stops at the first match, not after parsing all shards).
Depends on: Tasks 1โ3 (builds on the stabilized, correctness-fixed base; C.2's forced reindex exercises Task 1's race-safe reindex).