Clara Docs
All docs

Architecture

How Clara is built6 min readEdit on GitHub

On this page

How Clara's code is laid out, and how one change travels from a door to the files. The rules here come from SPEC.md C3 and C4; tests enforce most of them.

The idea in one paragraph

Everything Clara knows is in the library. The library defines every operation once, in a registry. The command line, the MCP server, the REST API and the SDK are doors into that registry: none of them has rules of its own. An operation runs on a transaction against a storage, records one event per change, and commits the files and the log line together. The storage is a folder on disk, GitHub's API, or memory, behind one interface.

Layers

flowchart TD
  person([Person]) --> cli[cli/ the clara command]
  person --> web[web/ Preact app]
  agent([Coding agent]) --> mcp[mcp/ MCP server]
  app([Your code]) --> sdk[clara-ipm SDK]
  web -->|HTTP| server[server/ REST and live events]
  cli --> engine
  mcp --> engine
  server --> engine
  sdk --> engine
  subgraph library[library/]
    engine[ops/ registry and engine] --> rules[rules/ query/ log/ model/ format/]
    engine --> storage[storage/ interface]
    conductor[conductor/ runs agents] --> engine
  end
  cli --> conductor
  storage --> disk[(disk: .clara/ folder)]
  storage --> github[(GitHub API)]
  storage --> memory[(memory)]

library/

The core and the SDK. No UI and no argument parsing.

Folder What it holds
model/ zod schemas and types: tickets, sprints, config, agents, events, runs, views; defaults.
format/ Reading and writing the files: front matter, deterministic output, migrations.
ops/ The operations (define.ts), the registry (registry.ts), the engine (engine.ts) and transactions (tx.ts).
rules/ The board rules of SPEC D2: moves, claims, dependencies, ranks, epics, sprints, review, readiness, mentions. Pure functions.
query/ The query language (QUERY.md): parser and evaluator.
log/ The activity log: reading it, undo-aware, and replaying it to any moment.
insights/ Throughput, cycle time, forecasts, scorecards, standups.
context/ Context packs for agents, the working agreement, and fencing of board text.
doctor/ Finding and fixing what a merge or a hand edit broke.
storage/ The Storage interface (types.ts) and its adapters: memory.ts, fs.ts (disk), github.ts.
git/ git and worktree helpers. Node only.
conductor/ Running agents: routing, worktrees, adapters, checks, review, landing. Node only.
node/ What needs Node: hashing, CLARA_HOME, workspaces.
import/, export/, scan/, templates/ Importers, exports and the badge, clara scan, project templates.

library/index.ts is the public API, published as clara-ipm. It runs anywhere: Node, a browser, a worker, a GitHub Action. library/node.ts adds disk storage, published as clara-ipm/node. The list of exported names is pinned in test/fixtures/api/clara.txt.

The doors

Folder Door What it is
cli/ CLI The clara command. cli/commands.ts is the command table: most commands name one operation, and take their flags from its input schema.
mcp/ MCP Clara's MCP server: 19 tools, 3 resources and 4 prompts (MCP.md). clara mcp serves it on stdio; the hosted board serves it at /api/mcp.
server/ REST A Hono app: every REST operation at POST /api/ops/<name>, live changes over Server-Sent Events, and the web app's files (REST.md). clara serve runs it on 127.0.0.1; server/hosted.ts runs it for the hosted board (HOSTED.md).
library/ SDK The operations and execute, imported from clara-ipm.
web/ (through REST) The Preact web board. It talks to the server through a client interface and has no rules of its own.

Other folders: action/ is the GitHub Action, plugin/ the Claude Code plugin, api/ the Vercel entry, templates/ the init, workflow and demo templates, scripts/ the build, gates and docs generation, and test/ every test.

How one change flows

Take ticket.comment, defined in library/ops/workflow.ts. It declares its name, a title and description, zod schemas for input and output, who may call it (people and agents), and its doors (all four). Then:

  • CLI. clara comment 12 "Use the existing mailer" finds the comment entry in cli/commands.ts, which names ticket.comment. The CLI parses the arguments into the operation's input, works out the actor (--as, CLARA_ACTOR, git's user.name), opens disk storage on .clara/, and calls execute.
  • MCP. An agent calls the clara_comment tool. mcp/tools.ts maps the tool to the operation, names the actor from the connection, never from the model's arguments, and calls execute.
  • REST. The web board posts to /api/ops/ticket.comment. server/app.ts checks the request (Host, origin, key), acts as the person who started clara serve (always a person; agents come through MCP), and calls execute.
  • SDK. Your code calls execute(ticketComment, session, 'ada', { id: 'WEB-12', text }).

From there every door is the same. execute in library/ops/engine.ts:

  1. Checks that this actor may call the operation and that the input fits its schema.
  2. Reads the board at one storage version.
  3. Runs the operation on a transaction (tx.ts). The operation reads tickets, applies the rules and emits events. Nothing is written yet.
  4. Adds the changes Clara makes on its own as system (derive.ts), such as approving under gates or moving an epic to done.
  5. Plans the files of every record that changed and one log line per event, and commits them all at once, naming the version it read.
  6. If another writer committed first, reads the board again and runs the operation again, up to five times. So two agents claiming one ticket end with one holder and one CLAIM_HELD.

A refusal at any step is an error with a code, a message and a hint, and nothing is written.

Storage

library/storage/types.ts is the one contract: read, list, version, commit and an optional watch. A commit is a set of changes applied together or not at all.

  • Disk (fs.ts) takes a lock, writes a journal of the changes, applies them by writing temporary files and renaming them, and removes the journal. A process that stops half way is finished by the next one. Many processes on one folder are safe.
  • GitHub (github.ts) makes one commit per change from blobs, a tree and a commit, and moves the branch only from the commit it read.
  • Memory (memory.ts) is for tests and embedding.

The same conformance suite (test/conformance/storage.ts) runs against all three. .clara/.local/ (runs, undo records, plans) is never seen through this interface.

The rules (SPEC C4)

  1. One registry. Every capability is an operation. A test checks each door against it.
  2. One storage interface, with one conformance suite.
  3. The library runs anywhere. Only storage/fs, git, conductor, node and parts of doctor may use Node. The portable gate and test/unit/core/boundaries.test.ts enforce it.
  4. Deterministic core. The clock, ids and randomness are injected. Rules are pure.
  5. One event per change. A change that alters nothing records nothing. Replaying the log reproduces every ticket's status, assignee and sprint, and a test checks it.
  6. Errors teach. fail('CODE', message, hint). No stack traces for users.
  7. Round-trip safe files. Fixed key order; keys Clara does not know are kept.
  8. Versioned format. config.json has a version, and clara migrate upgrades.
  9. Embeddable. No singletons, a configurable base path, every colour a CSS variable.
  10. Import boundaries. library/ never imports from cli/, mcp/, server/ or web/.

Generated files

npm run docs writes CLI.md, MCP.md, REST.md and the Claude Code plugin's files from the same sources the commands use. The docs gate fails when any of them is out of date.

Budgets

The web board's first load stays under 100 KB gzipped, clara list on 1,000 tickets under 200 ms, and more. BENCHMARKS.md has the measured numbers.