Architecture
How Thought Control is built, end to end. One Rust binary, an append-only log as the truth, a database you can delete, and a TUI whose every pixel comes from one serializable state.
This is the deep end of the docs: how the pieces of thc fit together, why they are shaped the
way they are, and where each one lives in the source. Every page links to the files it describes,
so you can read the code beside it.
The whole system
Read it from the edges in. Surfaces at the top are every way in: you at the CLI or in the
TUI, an agent running the same CLI (or steering the TUI through its socket), a file dropped into
drop/, a round trip through $EDITOR, the menu bar. None of them has behaviour of its own; they
all call thc-core.
A write is parsed once (due:fri becomes a date field and is never re-read from text), turned
into validated operations by the builder, checked against the actor’s policy and any
preconditions, then committed: lock, catch up, stamp each event with a hybrid logical clock,
append to log/<this-device>/, fsync, apply to SQLite. A read compiles a query into one SQL
WHERE over the store and renders rows, as JSON or for people.
At the bottom, the vault is a plain folder you sync with whatever you like. Only the log in
it is the truth. The store next to it is a cache in the local cache directory: delete it and
thc rebuild replays the log into an identical one.
The rules of the house
Every page in this section follows from a short list. When something in the code looks odd, it usually traces back to one of these.
The log is the truth. Every change is one JSON line, appended, never rewritten. The SQLite store, the Markdown export and every screen are derived from it.
Replay is a pure function of the events. Clocks, randomness, timezones and recurrence math live in the writer. Two devices with the same lines build the same state, byte for byte.
One writer per file. A device appends only to its own log directory, so a sync tool never sees two writers on one file and never makes a “conflicted copy”.
One primitive. A page, a day, a task, a tag and a saved view are all nodes. “Task” is a status, “reminder” is an alert. Fewer kinds, fewer edge cases.
Text at the edges, structure in the core. Capture syntax is parsed once, into typed fields. Stored text is never re-parsed to learn what something is.
The CLI is the API. People and agents use the same commands, with
—json, stable exit codes and attribution on every write.State is serializable, or it is a bug. The TUI is one
UiState, changed only by messages, drawn by a pure view. What isn’t in the state can’t be replayed, patched or tested.One source of truth, instantiated. A document lives in one place and every view holds a reference to it. The same component appears wherever that thing does; differences are data, not a second code path.
The crates
| Crate | What it holds | Size |
|---|---|---|
thc-core | The node model, the event log, HLC, merge and replay, the SQLite store, capture syntax, dates and recurrence, the query compiler, outline (documents ↔ ops), review, policy, vaults | ~16.6k lines |
thc-daemon | The optional background process: log watcher, socket server and subscriptions, alert scheduler, OS notifications, export, drop/ ingest | ~2k lines |
thc-tui | The ratatui app: UiState, Session, the pure update and view, the keymap, the sidebar, layers, and the editor built on caretline | ~43.9k lines |
thc | The binary: clap dispatch, one module per command in the registry, JSON output, schemas, setup and update, the team launcher | ~28.6k lines |
caretline | The editor engine, its own repository and site (caretline.app): text, selection, motion, undo, the layout map | external |
The life of a write
Follow one command all the way down:
$ THC_ACTOR=claude thc todo "Call the dentist due:fri #health !high"
- Parse.
capture.rssplits the text into words, findsdue:fri,#healthand!high, resolvesfriagainst the writer’s clock and timezone, and keeps the rest as text. - Build.
TxBuildermakes the operations: today’s journal day if it doesn’t exist yet (its id is keyed from the date, so every device makes the same one), anode.createfor the task with a fresh 12-character id, a fractional order key after the last sibling, and its status, due and priority as props, thehealthtag (keyed from its name), and anedge.addlinking the two. - Check. The actor
claudeis an agent, so its tier in the device config decides whether this verb is allowed, needs confirmation, or is refused.--expectand--if-matchwould be checked here too, under the lock. - Commit.
Vault::transacttakes the device write lock, catches up on any lines it hasn’t applied, stamps each op with the HLC, the device id, the actor and one shared transaction id, appends the lines, fsyncs, and applies them to SQLite in one transaction. - Notify. The daemon, if it runs, sees the append and pushes
changedto every subscriber. An open TUI flashes the new row, marked◆ claude. The transaction sits inthc reviewuntil a person accepts or reverts it.
The life of a read
$ thc q 'status:open due<=+3d #health sort:due' --json
The query compiler parses the terms into an AST (which --explain prints in plain words) and
one SQL WHERE over nodes, with relative dates resolved once. SQLite answers from partial
indexes on open tasks. The output layer renders node objects with a stable, additive JSON shape.
No daemon is involved: the CLI opens the store directly, because a round trip through a socket
would cost more than it saves.
Read on
| Page | Covers |
|---|---|
| The event log | The vault on disk, the envelope, every op, ids and keyed ids, the version rules |
| Replay and merge | Total order, per-field last-writer-wins, text conflicts, cycles, convergence |
| The write path | Capture, the builder, the lock, policy tiers, preconditions, undo and review |
| Reads and queries | The store schema, the query language and its compiler, views, vaults, time travel |
| The daemon | Watching, the socket protocol, push, alerts and notifications, export |
| The TUI as state | UiState, messages, the pure update, effects, traces, the thc ui socket |
| The editor | caretline inside thc: documents, views, saving, tasks on extension points |
| Agents and teams | Actors, tiers, review, claims, messages and the team board |
| How it’s tested | Convergence simulation, goldens, snapshots, flows, the sandbox |
| Decisions | The choices that shaped it, what each one costs, and what it rules out |