Skip to main content

Architecture

ai-coach is a ports-and-adapters (hexagonal) application. The core knows nothing about MCP, Ollama, Gemini, the file system or the terminal. Those sit behind ports the core defines, and one composition root wires them together.

Why each part is the way it is lives in the decisions in reasonry/, a knowledge base that Reasonry (@push-based/reasonry) serves to agents. This page is the map; the decisions are the reasons.

The system around it​

┌───────────────────────── your machine ─────────────────────────┐
│ │
you ── terminal ──► coach CLI (this repo) ──stdio/MCP──► coach-vault serve (this repo) ──► Obsidian vault
│ │ ▲ │ (Markdown files)
│ └──HTTP, loopback only──► Ollama (Gemma) │
│ │ │
you ── AI agent ─────► .agents/skills (this repo) ──stdio/MCP───────┘ │
│ │
└────────────────────────────────────────────────────────────────┘
│
└──HTTPS, Tier 2 payload only──► Gemini (the judge)
  • The vault servers (apps/vault-mcp-server, run as coach-vault-mcp, and apps/vault-view-server, run as coach-vault-view) own the vault (DEC-AIC-0048, DEC-AIC-0061). They are the only programs that read or write vault files. The vault code was written for ai-coach in a branch of kb-template and moved here with its history; only three small files come from kb-template itself (see Code from kb-template). The reasons for its behaviour are the DEC-KI-0058 to DEC-KI-0071 written there, which moved here too (DEC-AIC-0049). vault-mcp-server serves MCP over stdio (PARA layout, PDF conversion, quote checks, Coach/sessions/, Plans/, preview-before-write); vault-view-server serves the browser UI on port 4747.
  • The coach CLI holds the learning rules, the conversations with Gemma and Gemini, and the confirm-before-write terminal flow. It starts node <docsMcpBin> serve --vault <vault> as a child process for each command, talks to it over MCP on stdio, and stops it when the command ends.
  • Ollama runs Gemma locally. The CLI refuses any Ollama host that is not on loopback.
  • The planning skills in .agents/skills/ are instructions for any AI coding agent that reads Agent Skills, not code; .claude/skills/ links to them for Claude Code. They reach the same server through the agent's MCP registration (the agent starts its own copy), and never touch vault files directly (DEC-AIC-0029, DEC-AIC-0030, DEC-AIC-0050).

Projects​

driving side ports, defined in application driven side

┌────────────────────────────────────┐
terminal │ vault NotesRepository │ ◄── adapter-vault-mcp MCP ─► vault-mcp-server ─► vault
│ │ SessionLog │
▼ │ VaultFiling │
cli │ Library │
(composition root, ├────────────────────────────────────┤
plugs adapters in) │ local model SparringPartner │ ◄── adapter-ollama Gemma, loopback only
│ │ StudyPartner │
▼ │ StyleClassifier │
application │ SynthesisPartner │
(use cases) ── uses ─►│ StructurePartner │
│ │ ElaborationPartner │
▼ │ TeachPartner │
domain ├────────────────────────────────────┤
(rules, no I/O) │ judge Judge │ ◄── adapter-gemini Gemini, Tier 2 payload only
├────────────────────────────────────┤
│ local state PendingJudgements │ ◄── adapter-local-state queue file, clock
│ Clock │
└────────────────────────────────────┘

The ports are the interfaces in libs/coach/application/src/lib/ports/: the vault ports in vault.ts and library.ts, the model ports in models.ts and library.ts, and the local state in state.ts. Each group has one adapter. McpVault implements all four vault ports, each local model port has its own Ollama… class, GeminiJudge is the judge, and FilePendingJudgements and SystemClock keep the local state. Only cli knows the adapters: composeCoach plugs them into the use cases.

All executable code lives in apps/ and libs/<domain>/<lib> (DEC-AIC-0051). Non-code repository content stays at the root (reasonry/, docs/, README.md, ARCHITECTURE.md, CONTRIBUTING.md). Two architectural styles coexist under @nx/enforce-module-boundaries:

  • architecture:hexagonal for backend domains (scope:coach, scope:vault): type:domain, type:application, type:adapter, and type:app.
  • architecture:layered for frontend applications (scope:vault-view, scope:docs, and future UIs): type:app, type:feat, type:ui, type:data-access, type:util.
  • scope:shared (type:util): pure TypeScript utilities (no external npm deps, no Node built-ins) importable by any scope, including hexagonal domain and application.
ProjectTagsContainsMay import
libs/coach/domainscope:coach, architecture:hexagonal, type:domainThe sparring round aggregate, sparring rules, coaching styles, buzzword and verbatim checks, the review ladder, the judge's verdict rule, the Tier 2 payload invariant, study rounds, synthesis checks, the climb's steps, domain errorsdomain, scope:shared type:util; no npm package, no Node
libs/coach/applicationscope:coach, architecture:hexagonal, type:applicationUse cases (Review, Filing, Sparring, SparringConversation, Judging, Studying, Synthesizing, Climbing, Generating, Elaborating, Grouping, Reading, Teaching), the ports they need, application errors, in-memory fakes for testsdomain, scope:shared type:util; no npm package, no Node
libs/coach/adapter-vault-mcpscope:coach, architecture:hexagonal, type:adapter, adapter:vault-mcpMcpVault, composing one adapter per port (McpNotesRepository, McpSessionLog, McpVaultFiling, McpLibrary) over VaultClientcore, scope:shared type:util; @modelcontextprotocol/sdk, zod, Node
libs/coach/adapter-ollamascope:coach, architecture:hexagonal, type:adapter, adapter:ollamaThe seven Gemma partners (sparring, study, style, synthesis, elaboration, structure, teach) and OllamaClient: prompts, fencing, structured replies, the loopback-only guardcore, scope:shared type:util; zod, Node
libs/coach/adapter-geminiscope:coach, architecture:hexagonal, type:adapter, adapter:geminiGeminiJudge: the blind claim-extraction protocol, fallback model, boundary re-check of the payloadcore, scope:shared type:util; @google/genai, zod, Node
libs/coach/adapter-local-statescope:coach, architecture:hexagonal, type:adapter, adapter:local-stateFilePendingJudgements, SystemClockcore, scope:shared type:util; zod, Node
apps/cliscope:coach, architecture:hexagonal, type:app, app:cliTerminal I/O, config, commands, and composeCoach, the only place a port meets its adapterscope:coach libs, scope:shared type:util; zod, Node
libs/shared/util-pathscope:shared, type:utilPure posix path helpers that behave like node:path'snothing
libs/vault/domainscope:vault, architecture:hexagonal, type:domainPARA, notes and links, the link graph, reading rules (marks, passages, concept notes), the climb, sessions and mastery, the Tier 2 payload, task lines, habits, planning and periods, and VaultTree, the snapshot planners readdomain, scope:shared type:util; no npm package, no Node
libs/vault/applicationscope:vault, architecture:hexagonal, type:applicationThe Vault facade and its use cases (filing, conversion, concepts, distilling, records, the climb, planning writes, roll-over, review, harvest, the view graph), the NoteStore port and the other ports, an in-memory NoteStore for testsdomain, application, scope:shared type:util; no npm package, no Node
libs/vault/adapter-fsscope:vault, architecture:hexagonal, type:adapter, adapter:vault-fscreateFsNoteStore, vault root resolution, SHA-256 and random idscore, scope:shared type:util; Node
libs/vault/adapter-markdownscope:vault, architecture:hexagonal, type:adapter, adapter:vault-markdownparseMatter: YAML frontmatter with gray-mattercore; gray-matter
libs/vault/adapter-searchscope:vault, architecture:hexagonal, type:adapter, adapter:vault-searchrankWithFlexSearch: full-text rankingcore; flexsearch
libs/vault/adapter-pdfscope:vault, architecture:hexagonal, type:adapter, adapter:vault-pdfreadPdf: a PDF's pages, metadata and outlinecore; pdfjs-dist
libs/vault/adapter-viewscope:vault, architecture:hexagonal, type:adapter, adapter:vault-viewThe graph view's loopback HTTP server and its pages in assets/core; Node
apps/vault-mcp-serverscope:vault, architecture:hexagonal, type:app, app:vault-mcp-serverPure stdio MCP vault server (coach-vault-mcp serve): one tool per vault ability, and composeVault, where the vault's ports meet their adaptersscope:vault libs, scope:shared type:util; @modelcontextprotocol/sdk, zod, Node
apps/vault-view-serverscope:vault, architecture:hexagonal, type:app, app:vault-view-serverLocal loopback HTTP backend (coach-vault-view) serving @ai-coach/vault-adapter-view and the built Angular @ai-coach/vault-view UI on 127.0.0.1scope:vault libs, scope:shared type:util; Node
apps/vault-viewscope:vault-view, architecture:layered, type:app, app:vault-viewThe Angular layered frontend for the vault view (including the climb drawing board), built as an Nx dependency of libs/vault/adapter-viewscope:vault-view layered libs, scope:shared type:util; Angular, ng-diagram
libs/vault-view/data-accessscope:vault-view, architecture:layered, type:data-accessFrontend-owned DTOs, typed HTTP API services, browser adapters and signal stores
libs/vault-view/feat-graphscope:vault-view, architecture:layered, type:featThe link graph, filters, note reader and distillation (lazy route)
libs/vault-view/feat-plansscope:vault-view, architecture:layered, type:featThe goal chain, effort timeline and plan details (lazy route /plans)
libs/vault-view/feat-climbscope:vault-view, architecture:layered, type:featThe climb ladder, structure drawing board and sketch pad (lazy route)
libs/vault-view/uiscope:vault-view, architecture:layered, type:uiPresentational UI atoms, molecules and layout shells
libs/vault-view/utilscope:vault-view, architecture:layered, type:utilPure TypeScript helpers: graph layout and camera math, markdown and distillation helpers
apps/docsscope:docs, architecture:layered, type:app, app:docsThe Docusaurus documentation sitescope:docs layered libs, scope:shared type:util; Docusaurus

Adapters never import each other, and nothing imports apps/*. Nothing in scope:coach imports scope:vault: the coach reaches the vault only over MCP, through adapter-vault-mcp (DEC-AIC-0048). Each project's README says what it does in plain words:

ProjectIn one line
libs/coach/domainThe rules of learning: rounds, verdicts, the review schedule. No I/O at all.
libs/coach/applicationThe things you can do (spar, judge, study, synthesize) and the ports they need.
libs/coach/adapter-ollamaThe local sparring partner, powered by Gemma.
libs/coach/adapter-geminiThe online judge, powered by Gemini.
libs/coach/adapter-vault-mcpTalks to your vault through the vault server.
libs/coach/adapter-local-stateRemembers which sessions are waiting for the judge, and what day it is.
apps/cliThe coach command you type.
libs/shared/util-pathPath helpers any library may use.
libs/vault/domainThe rules of the vault: notes, reading, the climb, planning. No I/O.
libs/vault/applicationWhat you can do with your vault, and the ports it needs.
libs/vault/adapter-fsReads and writes the vault's files.
libs/vault/adapter-markdownReads a note's YAML frontmatter.
libs/vault/adapter-searchRanks full-text search hits.
libs/vault/adapter-pdfReads the text of a PDF book.
libs/vault/adapter-viewServes the graph view in your browser.
apps/vault-mcp-serverOffers the vault as MCP tools to the coach and to AI agents.
apps/vault-view-serverServes the local vault-view web UI and its JSON/write APIs on 127.0.0.1.
apps/vault-viewThe Angular layered frontend for the vault view.
libs/vault-view/data-accessThe vault view's HTTP APIs, DTOs and signal stores.
libs/vault-view/feat-graphThe vault view's graph page.
libs/vault-view/feat-plansThe vault view's plans page.
libs/vault-view/feat-climbThe vault view's climb page.
libs/vault-view/uiShared presentational UI for the vault view.
libs/vault-view/utilPure helpers for the vault view.
apps/docsThis documentation site, built from the repository's Markdown.

Ports and who provides them​

PortFileAdapter
NotesRepository, SessionLog, VaultFilingapplication/…/ports/vault.tsadapter-vault-mcp
Libraryapplication/…/ports/library.tsadapter-vault-mcp
SparringPartner, StudyPartner, StyleClassifier, Judgeapplication/…/ports/models.tsadapter-ollama, adapter-gemini (Judge)
SynthesisPartnerapplication/…/ports/library.tsadapter-ollama
PendingJudgements, Clockapplication/…/ports/state.tsadapter-local-state

The vault's ports​

PortFileAdapter
NoteStorevault/application/…/ports/note-store.tsadapter-fs (createFsNoteStore)
FrontmatterParservault/domain/…/note/md.tsadapter-markdown (parseMatter)
SearchRankervault/application/…/notes/search.tsadapter-search (rankWithFlexSearch)
PdfReadervault/application/…/reading/convert-units.tsadapter-pdf (readPdf)
Digest, randomHexvault/application/…/shared/writes.ts, vault/types.tsadapter-fs (sha256Hex, randomHex)

createVault takes these as VaultDeps, and composeVault in apps/vault-mcp-server and apps/vault-view-server supplies them (DEC-AIC-0052, DEC-AIC-0061). Planners never touch the disk: they read a VaultTree snapshot loaded through the NoteStore, and only the execute step writes.

Rules​

  • Dependencies point inward. Ports are shaped by what the use cases need, not by a tool's API.
  • Business rules live in the domain, in objects that guard their own invariants: a SparringRound will not accept a challenge that quotes the note, and tier2Payload() rejects any field other than challenge, answer and the coaching style (plus core assertions when allowed, DEC-AIC-0025).
  • Use cases orchestrate: they load through ports, let the domain decide, and save through ports.
  • Every write to the vault is a preview first and an execute only after the user confirms (DEC-AIC-0009).
  • Adapters translate technology failures into application errors (UnavailableError, UnusableReplyError, VaultRefusedError); the CLI turns those into messages and exit codes.
  • The only data that may leave the machine is a Tier2Payload. The Gemini adapter accepts nothing else and re-checks it.
  • Source code carries no comments (DEC-AIC-0011). Names and tests carry the meaning.

Frontend standards (vault-view)​

apps/vault-view and libs/vault-view/* follow the Angular layered standards in DEC-AIC-0062 and the typed component taxonomy in DEC-AIC-0063:

  • data-access: pure HTTP API services and focused signal stores/facades (no raw globalThis.location/document, no god-stores).
  • ui: pure presentational components (input()/output() only, ChangeDetectionStrategy.OnPush, zero data-access imports or injection), organized into atoms (atoms/, *AtomComponent), molecules (molecules/, *MoleculeComponent), and shell frames (shell/, *ShellComponent), all using standard *.component.{ts,html,css} filenames.
  • feat-*: lazy-loaded feature libraries exporting only their route-level feature component (src/lib/<domain>.component.ts / *FeatureComponent), with private smart components (smart/, *SmartComponent, injecting data-access stores) and composite components (composite/, *CompositeComponent, pure input()/output() domain views) each in its own directory.
  • util: pure, framework-free TypeScript helpers.
  • Components & files: explicit ChangeDetectionStrategy.OnPush (enforced as an ESLint error), explicit public/private access modifiers on all class members, no internal or inline CSS (styles: and static style="..." are forbidden; all styles live in separate .css files via styleUrl), external .html templates once inline templates exceed ~40 lines, and a ~250 LOC soft cap per file.

Enforcement​

npm exec nx run-many -- -t lint runs @nx/enforce-module-boundaries with the tags above (DEC-AIC-0051) and angular-eslint across apps/vault-view and libs/vault-view/* (DEC-AIC-0062):

  • scope:* isolates domains (scope:coach, scope:vault, scope:vault-view, scope:docs), allowing each to depend only on itself and scope:shared.
  • architecture:hexagonal and architecture:layered isolate architectural styles, allowing each to depend only on its own style and scope:shared.
  • onlyDependOnLibsWithTags keeps hexagonal dependencies inward (domain ← application ← adapter ← app) and enforces layered frontend rules (app → feat/ui/data-access/util; feat → ui/data-access/util with no feat → feat; ui → ui/util; data-access → data-access/util; util → util).
  • allowedExternalImports is a whitelist per tag: empty for domain, application, and scope:shared, and only its own SDK for each adapter and hexagonal app.
  • no-restricted-imports bans Node built-ins in libs/*/domain, libs/*/application, and libs/shared/*, which the whitelist does not cover.
  • Circular project dependencies are errors.

Tests​

LevelWhereAgainst
DomaindomainPlain functions and objects
Use casesapplicationIn-memory adapters in src/testing, including a check that a planted private line never reaches the judge
Adaptersadapter-ollama, adapter-gemini, adapter-local-stateA loopback HTTP server for Ollama, the real Gemini SDK with a fake client, a temporary directory
CLIcliA scripted terminal and stubbed use cases

Tests that start the vault server (the MCP adapter and end-to-end CLI tests) are dropped for now (DEC-AIC-0010), so adapter-vault-mcp has no tests and passes with none. The vault's domain and application are tested in memory, with an in-memory NoteStore in application/src/testing. Each vault adapter has its own tests; the tests of the composed vault and MCP tools run in vault-mcp-server against temporary vaults, and the loopback HTTP view server tests run in vault-view-server.

Code from kb-template​

The vault server (libs/vault/*, apps/vault-mcp-server, apps/vault-view-server, and apps/vault-view) was first written in kb-template, a separate private project (now publicly released as Reasonry), and moved here with its history (DEC-AIC-0048). It was written for ai-coach. Only these three small pieces hold code that was already in kb-template before that work began:

FileComes from kb-template'sWhat it is
libs/vault/domain/src/lib/note/md.ts and libs/vault/adapter-markdown/src/lib/markdown.tscore/src/kernel/md/frontmatter.ts, yaml.tsreading a note's frontmatter and body (parseFrontmatter, extractBody) and yamlString, slightly simplified
apps/vault-mcp-server/src/mcp/tool-types.tsmcp/src/tool-types.tsthe shape of an MCP tool (ToolDef, defineTool), copied as is apart from formatting
apps/vault-mcp-server/src/main.tscli/src/main.tsthe command dispatcher's shape; the serve command is new

Everything else in this repository was written for ai-coach.