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 ascoach-vault-mcp, andapps/vault-view-server, run ascoach-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-serverserves MCP over stdio (PARA layout, PDF conversion, quote checks,Coach/sessions/,Plans/, preview-before-write);vault-view-serverserves the browser UI on port 4747. - The
coachCLI holds the learning rules, the conversations with Gemma and Gemini, and the confirm-before-write terminal flow. It startsnode <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:hexagonalfor backend domains (scope:coach,scope:vault):type:domain,type:application,type:adapter, andtype:app.architecture:layeredfor 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 hexagonaldomainandapplication.
| Project | Tags | Contains | May import |
|---|---|---|---|
libs/coach/domain | scope:coach, architecture:hexagonal, type:domain | The 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 errors | domain, scope:shared type:util; no npm package, no Node |
libs/coach/application | scope:coach, architecture:hexagonal, type:application | Use cases (Review, Filing, Sparring, SparringConversation, Judging, Studying, Synthesizing, Climbing, Generating, Elaborating, Grouping, Reading, Teaching), the ports they need, application errors, in-memory fakes for tests | domain, scope:shared type:util; no npm package, no Node |
libs/coach/adapter-vault-mcp | scope:coach, architecture:hexagonal, type:adapter, adapter:vault-mcp | McpVault, composing one adapter per port (McpNotesRepository, McpSessionLog, McpVaultFiling, McpLibrary) over VaultClient | core, scope:shared type:util; @modelcontextprotocol/sdk, zod, Node |
libs/coach/adapter-ollama | scope:coach, architecture:hexagonal, type:adapter, adapter:ollama | The seven Gemma partners (sparring, study, style, synthesis, elaboration, structure, teach) and OllamaClient: prompts, fencing, structured replies, the loopback-only guard | core, scope:shared type:util; zod, Node |
libs/coach/adapter-gemini | scope:coach, architecture:hexagonal, type:adapter, adapter:gemini | GeminiJudge: the blind claim-extraction protocol, fallback model, boundary re-check of the payload | core, scope:shared type:util; @google/genai, zod, Node |
libs/coach/adapter-local-state | scope:coach, architecture:hexagonal, type:adapter, adapter:local-state | FilePendingJudgements, SystemClock | core, scope:shared type:util; zod, Node |
apps/cli | scope:coach, architecture:hexagonal, type:app, app:cli | Terminal I/O, config, commands, and composeCoach, the only place a port meets its adapter | scope:coach libs, scope:shared type:util; zod, Node |
libs/shared/util-path | scope:shared, type:util | Pure posix path helpers that behave like node:path's | nothing |
libs/vault/domain | scope:vault, architecture:hexagonal, type:domain | PARA, 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 read | domain, scope:shared type:util; no npm package, no Node |
libs/vault/application | scope:vault, architecture:hexagonal, type:application | The 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 tests | domain, application, scope:shared type:util; no npm package, no Node |
libs/vault/adapter-fs | scope:vault, architecture:hexagonal, type:adapter, adapter:vault-fs | createFsNoteStore, vault root resolution, SHA-256 and random ids | core, scope:shared type:util; Node |
libs/vault/adapter-markdown | scope:vault, architecture:hexagonal, type:adapter, adapter:vault-markdown | parseMatter: YAML frontmatter with gray-matter | core; gray-matter |
libs/vault/adapter-search | scope:vault, architecture:hexagonal, type:adapter, adapter:vault-search | rankWithFlexSearch: full-text ranking | core; flexsearch |
libs/vault/adapter-pdf | scope:vault, architecture:hexagonal, type:adapter, adapter:vault-pdf | readPdf: a PDF's pages, metadata and outline | core; pdfjs-dist |
libs/vault/adapter-view | scope:vault, architecture:hexagonal, type:adapter, adapter:vault-view | The graph view's loopback HTTP server and its pages in assets/ | core; Node |
apps/vault-mcp-server | scope:vault, architecture:hexagonal, type:app, app:vault-mcp-server | Pure stdio MCP vault server (coach-vault-mcp serve): one tool per vault ability, and composeVault, where the vault's ports meet their adapters | scope:vault libs, scope:shared type:util; @modelcontextprotocol/sdk, zod, Node |
apps/vault-view-server | scope:vault, architecture:hexagonal, type:app, app:vault-view-server | Local 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.1 | scope:vault libs, scope:shared type:util; Node |
apps/vault-view | scope:vault-view, architecture:layered, type:app, app:vault-view | The Angular layered frontend for the vault view (including the climb drawing board), built as an Nx dependency of libs/vault/adapter-view | scope:vault-view layered libs, scope:shared type:util; Angular, ng-diagram |
libs/vault-view/data-access | scope:vault-view, architecture:layered, type:data-access | Frontend-owned DTOs, typed HTTP API services, browser adapters and signal stores | |
libs/vault-view/feat-graph | scope:vault-view, architecture:layered, type:feat | The link graph, filters, note reader and distillation (lazy route) | |
libs/vault-view/feat-plans | scope:vault-view, architecture:layered, type:feat | The goal chain, effort timeline and plan details (lazy route /plans) | |
libs/vault-view/feat-climb | scope:vault-view, architecture:layered, type:feat | The climb ladder, structure drawing board and sketch pad (lazy route) | |
libs/vault-view/ui | scope:vault-view, architecture:layered, type:ui | Presentational UI atoms, molecules and layout shells | |
libs/vault-view/util | scope:vault-view, architecture:layered, type:util | Pure TypeScript helpers: graph layout and camera math, markdown and distillation helpers | |
apps/docs | scope:docs, architecture:layered, type:app, app:docs | The Docusaurus documentation site | scope: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:
| Project | In one line |
|---|---|
libs/coach/domain | The rules of learning: rounds, verdicts, the review schedule. No I/O at all. |
libs/coach/application | The things you can do (spar, judge, study, synthesize) and the ports they need. |
libs/coach/adapter-ollama | The local sparring partner, powered by Gemma. |
libs/coach/adapter-gemini | The online judge, powered by Gemini. |
libs/coach/adapter-vault-mcp | Talks to your vault through the vault server. |
libs/coach/adapter-local-state | Remembers which sessions are waiting for the judge, and what day it is. |
apps/cli | The coach command you type. |
libs/shared/util-path | Path helpers any library may use. |
libs/vault/domain | The rules of the vault: notes, reading, the climb, planning. No I/O. |
libs/vault/application | What you can do with your vault, and the ports it needs. |
libs/vault/adapter-fs | Reads and writes the vault's files. |
libs/vault/adapter-markdown | Reads a note's YAML frontmatter. |
libs/vault/adapter-search | Ranks full-text search hits. |
libs/vault/adapter-pdf | Reads the text of a PDF book. |
libs/vault/adapter-view | Serves the graph view in your browser. |
apps/vault-mcp-server | Offers the vault as MCP tools to the coach and to AI agents. |
apps/vault-view-server | Serves the local vault-view web UI and its JSON/write APIs on 127.0.0.1. |
apps/vault-view | The Angular layered frontend for the vault view. |
libs/vault-view/data-access | The vault view's HTTP APIs, DTOs and signal stores. |
libs/vault-view/feat-graph | The vault view's graph page. |
libs/vault-view/feat-plans | The vault view's plans page. |
libs/vault-view/feat-climb | The vault view's climb page. |
libs/vault-view/ui | Shared presentational UI for the vault view. |
libs/vault-view/util | Pure helpers for the vault view. |
apps/docs | This documentation site, built from the repository's Markdown. |
Ports and who provides them
| Port | File | Adapter |
|---|---|---|
NotesRepository, SessionLog, VaultFiling | application/…/ports/vault.ts | adapter-vault-mcp |
Library | application/…/ports/library.ts | adapter-vault-mcp |
SparringPartner, StudyPartner, StyleClassifier, Judge | application/…/ports/models.ts | adapter-ollama, adapter-gemini (Judge) |
SynthesisPartner | application/…/ports/library.ts | adapter-ollama |
PendingJudgements, Clock | application/…/ports/state.ts | adapter-local-state |
The vault's ports
| Port | File | Adapter |
|---|---|---|
NoteStore | vault/application/…/ports/note-store.ts | adapter-fs (createFsNoteStore) |
FrontmatterParser | vault/domain/…/note/md.ts | adapter-markdown (parseMatter) |
SearchRanker | vault/application/…/notes/search.ts | adapter-search (rankWithFlexSearch) |
PdfReader | vault/application/…/reading/convert-units.ts | adapter-pdf (readPdf) |
Digest, randomHex | vault/application/…/shared/writes.ts, vault/types.ts | adapter-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
SparringRoundwill not accept a challenge that quotes the note, andtier2Payload()rejects any field other thanchallenge,answerand the coachingstyle(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 rawglobalThis.location/document, no god-stores).ui: pure presentational components (input()/output()only,ChangeDetectionStrategy.OnPush, zerodata-accessimports 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, injectingdata-accessstores) and composite components (composite/,*CompositeComponent, pureinput()/output()domain views) each in its own directory.util: pure, framework-free TypeScript helpers.- Components & files: explicit
ChangeDetectionStrategy.OnPush(enforced as an ESLint error), explicitpublic/privateaccess modifiers on all class members, no internal or inline CSS (styles:and staticstyle="..."are forbidden; all styles live in separate.cssfiles viastyleUrl), external.htmltemplates 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 andscope:shared.architecture:hexagonalandarchitecture:layeredisolate architectural styles, allowing each to depend only on its own style andscope:shared.onlyDependOnLibsWithTagskeeps hexagonal dependencies inward (domain←application←adapter←app) and enforces layered frontend rules (app→feat/ui/data-access/util;feat→ui/data-access/utilwith nofeat→feat;ui→ui/util;data-access→data-access/util;util→util).allowedExternalImportsis a whitelist per tag: empty fordomain,application, andscope:shared, and only its own SDK for each adapter and hexagonal app.no-restricted-importsbans Node built-ins inlibs/*/domain,libs/*/application, andlibs/shared/*, which the whitelist does not cover.- Circular project dependencies are errors.
Tests
| Level | Where | Against |
|---|---|---|
| Domain | domain | Plain functions and objects |
| Use cases | application | In-memory adapters in src/testing, including a check that a planted private line never reaches the judge |
| Adapters | adapter-ollama, adapter-gemini, adapter-local-state | A loopback HTTP server for Ollama, the real Gemini SDK with a fake client, a temporary directory |
| CLI | cli | A 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:
| File | Comes from kb-template's | What it is |
|---|---|---|
libs/vault/domain/src/lib/note/md.ts and libs/vault/adapter-markdown/src/lib/markdown.ts | core/src/kernel/md/frontmatter.ts, yaml.ts | reading a note's frontmatter and body (parseFrontmatter, extractBody) and yamlString, slightly simplified |
apps/vault-mcp-server/src/mcp/tool-types.ts | mcp/src/tool-types.ts | the shape of an MCP tool (ToolDef, defineTool), copied as is apart from formatting |
apps/vault-mcp-server/src/main.ts | cli/src/main.ts | the command dispatcher's shape; the serve command is new |
Everything else in this repository was written for ai-coach.