DEC-AIC-0052: Split the vault into hexagonal domain, application and adapter libraries
Accepted
Status: accepted · 2026-11-20
Context
DEC-AIC-0051 moved the vault into libs/vault/core in stage 1 and tagged it type:vault, a transitional tag that lets it import Node built-ins, gray-matter, flexsearch and pdfjs-dist anywhere. The library is about 12,000 lines in 29 modules with no layers (observed: libs/vault/core/src @ 2026-11-20):
- 16 modules call
node:fsdirectly, some with synchronous calls (readFileSync,readdirSync) inside otherwise pure planning code. - Many modules mix pure rules with I/O in one file:
concept.tsholds both quote location and the write of a concept note. md.tsparses frontmatter withgray-matter;links,graph,marksandclimbneed onlynode:pathposixhelpers.vault.tsis a 990-line facade whosecreateVault(root)wires everything internally, soapps/vault-servercannot substitute any dependency.view.tsruns an HTTP server from inside the library.
Decision
libs/vault/coreis replaced by hexagonal libraries underlibs/vault/, and thetype:vaulttag is removed:domain(type:domain): pure rules and parsing of notes, PARA buckets, links, identity, habits, task lines, the link graph, sessions and mastery, the tier-2 payload, planning, andVaultTree, a snapshot of the vault's folders and note texts. No npm and no Node imports.application(type:application): theVaultfacade and the use cases (filing, conversion, concepts, distilling, records, planning writes, roll-over, review, harvest, note edits, the climb, the view graph), plus the ports they need:NoteStore(vault file system),SearchRanker,PdfReader,Digestand a random id source. No npm and no Node imports.adapter-fs: thenode:fsimplementation ofNoteStore, vault root resolution, and SHA-256 and random ids fromnode:crypto.adapter-markdown: theFrontmatterParserwithgray-matter.adapter-search: theSearchRankerwithflexsearch.adapter-pdf: thePdfReaderwithpdfjs-dist.adapter-view: the loopback HTTP server for graph, plans and climb pages, driving theVaultfacade, with its pages inassets/. Each adapter carries anadapter:vault-*tag whose external-import whitelist names only its own package or Node.
- Frontmatter parsing goes behind a
FrontmatterParserthat the domain takes as a parameter, rather than a hand-written YAML parser in the domain, so the domain stays pure without changing how notes are read. - Posix path helpers move to
libs/shared/util-path(scope:shared,type:util): purejoin,dirname,basename,extname,normalize,relative,resolveandisAbsolutethat behave exactly likenode:path'sposixfunctions. Vault paths are always posix, so the domain and application use it instead ofnode:path. NoteStoreis async only, and planners read a snapshot. Each operation loads aVaultTreethrough theNoteStoreonce; planners stay synchronous and read folders and note texts from it. Only the execute step goes back to theNoteStore, and it still refuses a note that changed or appeared since the preview.apps/vault-serveris the composition root.composeVaultbuilds the adapters and passes them tocreateVault(deps, opts); the MCP tools stay in the app as its driving adapter, and itsviewcommand composesadapter-viewthe same way. The tests of the composed vault, which need real files and real YAML parsing, move toapps/vault-server/src/vault/__tests__;domainandapplicationare tested in memory.
All invariants of DEC-AIC-0008 and DEC-AIC-0051 hold: dependencies point inward, adapters translate errors, composition is manual, and there are no circular dependencies.
Consequences
- The vault follows the same architecture as the coach, and
@nx/enforce-module-boundarieschecks it: domain and application code cannot touch the file system or an npm package. - Use cases can be tested with
createMemoryNoteStorefromapplication/src/testinginstead of a temporary directory; the existing tests that use a real directory keep running throughcomposeVaultinvault-server. - The package
@ai-coach/vaultand its@ai-coach/vault/*.jssubpath exports disappear.apps/vault-serverimports from@ai-coach/vault-domain,@ai-coach/vault-applicationand the adapter packages, reads its version from its ownpackage.json, and reaches note paths and search through the facade (vault.pathOf,vault.searchNotes). - A planner sees the vault as it was when the snapshot was loaded, and each operation reads every note once, as
scanVaultalready did. Reading one note (vault.note) lists paths without reading every note. libs/shared/util-pathis the firstscope:sharedlibrary.
Alternatives Considered
- Write a minimal YAML frontmatter parser in the domain. Rejected: notes are written by the user in Obsidian, and a hand-written parser risks reading valid YAML differently from today.
- Keep the posix helpers inside
libs/vault/domain. Rejected: path handling is not vault-specific, and the coach may need it; a pure shared util serves both scopes. - Make every planner async and query the
NoteStoredirectly. Rejected: about 50 existence and listing checks inside planners would each become an await, and planning would stop being pure; a snapshot keeps planners synchronous and testable without a store. - Keep
NoteStoreboth sync and async. Rejected: two variants of every port method doubles the adapter surface. - Leave the view server in
apps/vault-server. Rejected: it is a separate driving adapter with its own HTTP concerns and assets.