thcThought Control

The daemon

An optional background process that is a cache warmer, an event bus and an alarm clock, and never the only way to do anything.

thc daemon runs in the background, started at login by launchd or a systemd --user unit. It watches the vault, replays what other devices wrote, tells every open client what changed, fires reminders and keeps the Markdown export fresh. Stop it and nothing breaks: the CLI and the TUI read the store themselves, write under the same lock, and catch up on open. The daemon only makes things arrive sooner.

~120 msfrom an append to a pushed changed
100 msdebounce on file events
5 minsafety-net rescan
2 sfor a client to claim an alert before the OS shows it
1process for every registered vault

The loop

The daemon's loopAppends to log/, a five-minute rescan, files in drop/ and the next alert time wake the loop. One step debounces, catches up by replaying new lines, groups what changed by transaction, notes new conflicts, schedules the export and plans due alerts. Clients on the socket get changed, conflict and alert events; an alert no client claims within two seconds goes to the OS notifier.wakes itlog/ appendsFSEvents · inotifyrescanevery 5 mindrop/ files→ inbox notesthe clocknext fire_atone stepthe loopa thread per vaultdebounce 100 mscatch_up: replay new linesgroup news by txdiff open conflictsexport 2 s after quietingest drop/due alerts → planwho hearssocket subscriberschanged {ids, tx, actor}conflict {id, kind}alert.fire · alert.withdrawTUI · ThoughtBar · thc watcha client claims italert.delivered · 2 s graceOS fallbacknotifier · osascriptno claim
fig. 1One vault's loop. File events, the rescan, the drop folder and the clock wake it; one step replays, groups and pushes; alerts go to a client first and to the OS only if none claims them.

The watcher is notify (FSEvents on macOS, inotify on Linux) on log/, debounced 100 ms. A full rescan runs every five minutes anyway, because file events can be dropped (sleep, a sync tool writing behind the watcher’s back). Each step:

  1. Catch up. Vault::catch_up reads each device’s log file from its cursor and applies new lines in total order. A line that sorts before something already applied triggers a full re-materialization; at personal scale that is simple and fast.
  2. Take the news. Every event applied since the last step, from this device and others, not a watermark: another device’s events can arrive with older timestamps, and a rebuild renumbers rows. After a long time away the newest 500 stand for the rest.
  3. Push, per transaction. One changed event per transaction, naming its node ids, the transaction, the actor and the device. A TUI uses actor to flash an agent’s change as ◆ claude.
  4. Diff conflicts. New open conflicts go out as conflict with a kind, text or move.
  5. Schedule the export. The Markdown export regenerates two seconds after things go quiet.

Once a day, on a quiet rescan, it also compacts the store (VACUUM), and every six hours it runs the update check the person chose (notify, or install).

The socket

Clients talk to the daemon over a Unix socket, newline-delimited JSON. The socket lives at /tmp/thc-<uid>/<hash of the cache dir>.sock in a 0700 directory (macOS caps socket paths at 104 bytes), and clients find it through daemon.json in the vault’s cache. There is no TCP listener.

→ {"id":1,"method":"hello","params":{"client":"thoughtbar","topics":["changed","alerts"],"deliver":true}}
← {"id":1,"result":{"daemon":"0.10.1","proto":1,"device":"studio"}}
← {"event":"changed","data":{"ids":["k3f9a2mq7x1c"],"tx":"01K…","actor":"agent:claude","dev":"studio"}}
→ {"id":2,"method":"alert.delivered","params":{"alerts":["xr7pd…"]}}

Requests carry an id that the response echoes; pushed events have none. The methods are reads (status, query, today, search, page, journal, log, …) and a few writes (capture, complete, set, snooze, ack, undo, blocks.apply) for clients with no file access, such as the menu bar app. Those writes take the same device write lock as the CLI. The protocol is version 1 with additive minors; thc schema --proto prints it as JSON Schema, from which the menu bar app’s Swift types are generated.

DMN-1

Reads never go through the daemon

held

The CLI and the TUI open the store directly and write under the device file lock, whether or not a daemon runs (see D-11). The socket is for pushes and for clients that can’t touch files.

why
A direct SQLite read from the CLI takes about 2.3 ms in total, most of it process start. Routing it through a socket would save about half a millisecond and add a dependency on a running process.
cost
Every client links the store code. That's fine for one binary; the menu bar app, which can't, uses the socket's read methods instead.

Alerts: replicated state, local firing

A reminder is an alert object attached to a node. Two kinds of state meet here, and they are deliberately kept apart:

StateWhere it livesWhy
pending, snoozed, acked, fire_atthe log (alert.* ops), replicatedan ack or snooze on one device silences the others once it syncs
fired here, when, through whatalert_local in this device’s cache, never in the logfiring is a side effect of one device’s clock

An alert fires at most once per device per fire_at, and re-arms when fire_at changes (a snooze, a moved due date). alerts::plan is pure: given the due alerts and the time it groups those within a minute of each other into one delivery, summarizes ones missed while the machine slept, and marks anything over twelve hours late as fired without a sound.

Delivery prefers a client. If a subscriber said "deliver":true (the menu bar app), the daemon sends alert.fire and waits two seconds for alert.delivered. If no one claims it, it falls back to the OS:

PlatformChannelClicking it
macOS with terminal-notifiera banner, grouped by alertopens thc tui --focus <id> in your terminal
macOS otherwiseosascriptnothing
Linux with notify-senda desktop notificationnothing

When an alert is handled anywhere (acked here or on another device, its node done or deleted), the next step withdraws the notification and pushes alert.withdraw.

drop/ and export/

Two folders in the vault are for the world outside thc:

  • drop/ is an inbox for files. Anything .md or .txt put there (from a phone, a script, a share sheet) becomes inbox notes, one per list item or paragraph, in one transaction. The daemon claims the file for this device first, so two devices sharing the vault never ingest it twice, then archives it.
  • export/ is a read-only Markdown copy of the vault, regenerated two seconds after the last change. Obsidian can open it. Nothing ever reads it back.

Every vault, one process

The daemon serves every registered vault (vaults) on threads of one process. Each vault keeps its own socket, cache, watcher and alerts, so clients find it exactly as before, and reminders fire from every vault, not only the home one. The registry is re-read every half second: a vault added starts at once, one removed stops, one renamed restarts under its new name. A notification from a vault that isn’t home leads with its name (acme · Book the venue · due 17:00).

A daemon on an old binary

Installs and updates rename a new thc over the old one, which keeps the path and, for a rebuild, the version, so neither tells you a running daemon is out of date. The daemon records the file it started from as exe_id (device and inode) in its status. A daemon whose file has been replaced, or that runs a different binary than its login item names, is stale. thc setup, thc daemon install, thc daemon start and thc update restart a stale daemon through whatever runs it (launchd kickstart -k, systemctl --user restart, or a stop and a spawn), and wait for the new one to answer. thc daemon status --json carries stale and the reason; thc doctor lists it with the fix.

Without it

Without the daemonWhat happens instead
Readsunchanged: the CLI and TUI read the store directly
Writesunchanged: they take the device write lock themselves
Other devices’ changesapplied on the next open, or by the TUI’s own polling
Live updates in the TUIpolling; the TUI reconnects to the daemon every 10 s and switches back
thc watchannounces it is polling every second, and reconnects on its own
Remindersthey don’t fire; thc today still lists them