How lecoeurdelest/memoir-canvas works
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: commands.ts Command layer · db.ts PGlite database · store.ts Application store · uiState.ts UI state
Components 18
Browser SPA
Local-first React application running entirely in the browser.
- Static assets · Vite output is served without an application server.
index.html - React entry
src/main.tsx
Domain integrity
Database-backed memory rules and shared mutation boundary.
- PGlite database · PostgreSQL WebAssembly persists its data in browser IndexedDB.
src/domain/db.ts - Memory graph schema · Constraints and deferred triggers enforce evidence, confirmation, and conflict rules.
src/domain/schema.sql - Domain contracts
src/domain/types.ts - Audit contracts
src/domain/audit.ts - Command layer · Validates intent, assumes roles, persists mutations, and records audit events.
src/domain/commands.ts - Seed archive loader · 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 · Adapts eight tool operations into graph reads or shared commands.
src/mcp/handlers.ts - Tool descriptors
src/mcp/descriptors.ts - Tool registry · Registers tools as a pure function of UI state.
src/mcp/registry.ts - Model context · 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
src/store/projection.ts - Application store · Refreshes read state after commands.
src/store/store.ts - UI state · Active conflict context controls resolve_claim availability.
src/store/uiState.ts - Memoir book canvas · Book, spread, page-turn, tear, constellation, and CSS fallback rendering.
src/view/Book.tsx - Evidence and review panels · Shows evidence, certainty, and audit history without deciding state.
src/panels/EvidencePanel.tsx - Manual tool panel
src/panels/ManualToolPanel.tsx
Connections 23
- 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