# lecoeurdelest/memoir-canvas architecture

> Architecture diagram of the GitHub repository lecoeurdelest/memoir-canvas, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lecoeurdelest/memoir-canvas
- Repository: https://github.com/lecoeurdelest/memoir-canvas

## Overview

#### Purpose and runtime
Hồi Ký is a local-first family-memory canvas: people and an AI collaboratively build a sourced memory graph while database rules prevent an agent from promoting guesses to facts or resolving conflicts. It is a Vite/React TypeScript SPA, entered through `src/main.tsx`, with PostgreSQL compiled to WebAssembly via PGlite and persisted in browser IndexedDB. It deploys as static assets (`index.html`, `public/_headers`); there is no application server.

#### Domain database and integrity boundary
`src/domain/db.ts` exclusively owns PGlite initialization, SQL access, roles, and seed/bootstrap execution. `src/domain/schema.sql` defines the memory graph and enforces epistemic rules in database constraints and deferred constraint triggers: human confirmation for confirmed claims, evidence for document-supported claims, and human-only conflict resolution. Domain records and command contracts live in `src/domain/types.ts` and `src/domain/audit.ts`. This is the hard security/integrity boundary rather than model prompts or UI logic.

#### Single command/write layer
All mutations pass through `src/domain/commands.ts`; the UI and MCP handlers share these command functions. Commands validate domain intent, assume the appropriate database role, persist changes, and write audit events. This prevents separate agent and human mutation paths and preserves database-enforced invariants regardless of call order or prompt manipulation.

#### Agent-facing WebMCP integration
`src/mcp/handlers.ts` is the sole agent entry point and adapts eight WebMCP operations into domain commands or graph reads. `src/mcp/descriptors.ts` defines tool contracts, while `src/mcp/registry.ts` computes the registered tool set as a pure function of UI state. `src/mcp/modelContext.ts` supplies that context to the host/model. Tool arguments and results are the deliberate external-data boundary: they travel to the hosted model session, unlike the local archive.

#### Projection and UI state
The store is read-oriented: `src/store/projection.ts` derives UI-ready graph/book data from persisted domain state, and `src/store/store.ts` coordinates loading/refresh after commands. `src/store/uiState.ts` captures navigation and focused conflict state. In particular, the active conflict controls whether `resolve_claim` is registered, so closing that context removes the agent capability rather than merely hiding a control.

#### Memory-canvas rendering
The read-only React presentation layer renders the graph as an interactive memoir book. `src/view/Book.tsx`, `src/view/Spread.tsx`, and `src/view/PageTurn.tsx` implement the book/navigation composition; `src/view/Tear.tsx` visualizes unresolved contradictory claims as a page that cannot close; `src/view/Constellation.tsx` exposes graph-like relationships. `src/view/CssBook.tsx` provides a CSS-based rendering fallback.

#### Evidence, review, and seeded archive
Panels present—not decide—epistemic state: `src/panels/EvidencePanel.tsx`, `src/panels/CertaintyBadge.tsx`, and `src/panels/AuditTrail.tsx` reveal sources, confidence, and mutation history. `src/panels/ManualToolPanel.tsx` provides human command access through the same command layer. A demonstrative archive is loaded from `src/seed/family.sql` by `src/seed/loadSeed.ts`, exercising graph, source, conflict, and audit flows.

Main entry points: [`src/domain/commands.ts`](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/commands.ts) (Command layer), [`src/domain/db.ts`](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/db.ts) (PGlite database), [`src/store/store.ts`](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/store/store.ts) (Application store), [`src/store/uiState.ts`](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/store/uiState.ts) (UI state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Browser SPA"]
  node_static["Static assets<br/>deployment<br/>[index.html]"]
  node_entry["React entry<br/>SPA bootstrap<br/>[main.tsx]"]
end

subgraph group_domain["Domain integrity"]
  node_db[("PGlite database<br/>local persistence<br/>[db.ts]")]
  node_schema["Memory graph schema<br/>SQL integrity rules<br/>[schema.sql]"]
  node_contracts["Domain contracts<br/>records and audit types<br/>[types.ts]"]
  node_audit["Audit contracts<br/>audit model<br/>[audit.ts]"]
  node_commands{{"Command layer<br/>shared writes<br/>[commands.ts]"}}
  node_seed["Seed archive loader<br/>demo bootstrap<br/>[loadSeed.ts]"]
end

subgraph group_agent["Agent integration"]
  node_mcp_handlers{{"MCP handlers<br/>agent entry point<br/>[handlers.ts]"}}
  node_mcp_descriptors["Tool descriptors<br/>tool contracts<br/>[descriptors.ts]"]
  node_mcp_registry["Tool registry<br/>capability selection<br/>[registry.ts]"]
  node_model_context["Model context<br/>host context bridge<br/>[modelContext.ts]"]
end

subgraph group_ui["Projection and UI"]
  node_projection["Read projection<br/>[projection.ts]"]
  node_store["Application store<br/>load and refresh coordinator<br/>[store.ts]"]
  node_ui_state["UI state<br/>navigation and conflict focus<br/>[uiState.ts]"]
  node_book["Memoir book canvas<br/>React presentation<br/>[Book.tsx]"]
  node_review["Evidence and review panels<br/>epistemic presentation<br/>[EvidencePanel.tsx]"]
  node_manual["Manual tool panel<br/>human command UI"]
end

node_static -->|"loads"| node_entry
node_entry -->|"initializes local archive"| node_db
node_db -->|"applies"| node_schema
node_db -->|"bootstraps with"| node_seed
node_seed -->|"loads family.sql"| node_db
node_commands -->|"role-scoped writes"| node_db
node_commands -->|"validates"| node_contracts
node_commands -->|"records events"| node_audit
node_projection -->|"reads persisted graph"| node_db
node_store -->|"loads and refreshes"| node_projection
node_commands -.->|"triggers refresh"| node_store
node_manual -->|"human mutations"| node_commands
node_mcp_handlers -->|"agent mutations"| node_commands
node_mcp_handlers -->|"graph reads"| node_projection
node_mcp_descriptors -->|"defines candidate tools"| node_mcp_registry
node_ui_state -->|"gates resolve_claim"| node_mcp_registry
node_mcp_registry -->|"registered tool set"| node_model_context
node_model_context -->|"host invokes tools"| node_mcp_handlers
node_entry -->|"mounts application state"| node_store
node_store -->|"supplies projected memoir"| node_book
node_ui_state -->|"drives navigation"| node_book
node_store -->|"supplies evidence and history"| node_review
node_ui_state -->|"focuses conflict"| node_review

click node_static "https://github.com/lecoeurdelest/memoir-canvas/blob/main/index.html"
click node_entry "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/main.tsx"
click node_db "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/db.ts"
click node_schema "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/schema.sql"
click node_contracts "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/types.ts"
click node_audit "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/audit.ts"
click node_commands "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/commands.ts"
click node_seed "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/seed/loadSeed.ts"
click node_mcp_handlers "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/mcp/handlers.ts"
click node_mcp_descriptors "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/mcp/descriptors.ts"
click node_mcp_registry "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/mcp/registry.ts"
click node_model_context "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/mcp/modelContext.ts"
click node_projection "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/store/projection.ts"
click node_store "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/store/store.ts"
click node_ui_state "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/store/uiState.ts"
click node_book "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/view/Book.tsx"
click node_review "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/panels/EvidencePanel.tsx"
click node_manual "https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/panels/ManualToolPanel.tsx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_static,node_entry toneBlue
class node_db,node_schema,node_contracts,node_audit,node_commands,node_seed toneAmber
class node_mcp_handlers,node_mcp_descriptors,node_mcp_registry,node_model_context toneMint
class node_projection,node_store,node_ui_state,node_book,node_review,node_manual toneRose
```

## Components

### Browser SPA

Local-first React application running entirely in the browser.

- [Static assets](https://github.com/lecoeurdelest/memoir-canvas/blob/main/index.html): deployment · Vite output is served without an application server. · `index.html`
- [React entry](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/main.tsx): SPA bootstrap · `src/main.tsx`

### Domain integrity

Database-backed memory rules and shared mutation boundary.

- [PGlite database](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/db.ts): local persistence · PostgreSQL WebAssembly persists its data in browser IndexedDB. · `src/domain/db.ts`
- [Memory graph schema](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/schema.sql): SQL integrity rules · Constraints and deferred triggers enforce evidence, confirmation, and conflict rules. · `src/domain/schema.sql`
- [Domain contracts](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/types.ts): records and audit types · `src/domain/types.ts`
- [Audit contracts](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/audit.ts): audit model · `src/domain/audit.ts`
- [Command layer](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/domain/commands.ts): shared writes · Validates intent, assumes roles, persists mutations, and records audit events. · `src/domain/commands.ts`
- [Seed archive loader](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/seed/loadSeed.ts): demo bootstrap · Loads a demonstrative family archive with sources, conflicts, and history. · `src/seed/loadSeed.ts`

### Agent integration

WebMCP exposes constrained archive operations to a hosted model.

- [MCP handlers](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/mcp/handlers.ts): agent entry point · Adapts eight tool operations into graph reads or shared commands. · `src/mcp/handlers.ts`
- [Tool descriptors](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/mcp/descriptors.ts): tool contracts · `src/mcp/descriptors.ts`
- [Tool registry](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/mcp/registry.ts): capability selection · Registers tools as a pure function of UI state. · `src/mcp/registry.ts`
- [Model context](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/mcp/modelContext.ts): host context bridge · Supplies registered tools and context to the hosted model session. · `src/mcp/modelContext.ts`

### Projection and UI

Read models, interaction state, and memoir-book presentation.

- [Read projection](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/store/projection.ts): derived graph and book data · `src/store/projection.ts`
- [Application store](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/store/store.ts): load and refresh coordinator · Refreshes read state after commands. · `src/store/store.ts`
- [UI state](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/store/uiState.ts): navigation and conflict focus · Active conflict context controls resolve_claim availability. · `src/store/uiState.ts`
- [Memoir book canvas](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/view/Book.tsx): React presentation · Book, spread, page-turn, tear, constellation, and CSS fallback rendering. · `src/view/Book.tsx`
- [Evidence and review panels](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/panels/EvidencePanel.tsx): epistemic presentation · Shows evidence, certainty, and audit history without deciding state. · `src/panels/EvidencePanel.tsx`
- [Manual tool panel](https://github.com/lecoeurdelest/memoir-canvas/blob/main/src/panels/ManualToolPanel.tsx): human command UI · `src/panels/ManualToolPanel.tsx`

## Connections

- Static assets → React entry: loads
- React entry → PGlite database: initializes local archive
- PGlite database → Memory graph schema: applies
- PGlite database → Seed archive loader: bootstraps with
- Seed archive loader → PGlite database: loads family.sql
- Command layer → PGlite database: role-scoped writes
- Command layer → Domain contracts: validates
- Command layer → Audit contracts: records events
- Read projection → PGlite database: reads persisted graph
- Application store → Read projection: loads and refreshes
- Command layer → Application store: triggers refresh
- Manual tool panel → Command layer: human mutations
- MCP handlers → Command layer: agent mutations
- MCP handlers → Read projection: graph reads
- Tool descriptors → Tool registry: defines candidate tools
- UI state → Tool registry: gates resolve_claim
- Tool registry → Model context: registered tool set
- Model context → MCP handlers: host invokes tools · Tool arguments and results cross the hosted-model boundary.
- React entry → Application store: mounts application state
- Application store → Memoir book canvas: supplies projected memoir
- UI state → Memoir book canvas: drives navigation
- Application store → Evidence and review panels: supplies evidence and history
- UI state → Evidence and review panels: focuses conflict
