Architecture
Grove cultivates git worktrees. manifest.toml declares desired state, git on disk is
actual state, and grove converges actual toward desired — additively, never destroying a
human's or an agent's work.
One Rust workspace, one shipped executable. grove serve is the daemon; every other
subcommand is the CLI. A UI lives outside this repo and reads the same state through
the HTTP API.
The four crates
crates/
├── grove-ops # the domain layer: manifest · git · env · roots · worktrees · pool
│ # · doctor · clock · wire · error · telemetry · testfix
├── grove-api # the shared HTTP contract: envelope, route bodies, event vocabulary
├── grove-daemon # tokio/axum: routes, boot state, guards, lanes, engines, watcher,
│ # event bus, log ring, the read surface
└── grove # the CLI + `grove serve` + process custody + setup/upgrade
Dependencies run one way: grove-ops ← grove-api ← grove-daemon ← grove. grove
also depends on grove-ops and grove-api directly, so the offline realizer and the
API client speak the same types the daemon serves. Edition 2024, resolver 3,
unsafe_code = "forbid" workspace-wide, clippy::pedantic warn-by-default with named
opt-outs (Cargo.toml).
grove-ops is a library only: the CLI, the daemon's lanes and the offline realizer
call it in-process.
Files are authoritative
There is no database. Everything the daemon holds in memory is a cache:
- a root's engine status (
unknown | missing | ready | degraded | cloning | unavailable) is re-derived from disk on every roots-changed event, and from scratch on restart; - the warm-pool count is a hint —
pool.fillre-observes disk before it mutates, so a stale hint costs at most one no-op fill; - the log ring is a bounded tail of what this process said, not a record.
A daemon may be killed and restarted with only the filesystem intact; an abandoned clone is re-derived from disk on the next boot.
Reading actual state means reading both of a root's directories, not the one entry the
realizer came for: roots/<slug>/ holds everything
grove owns — the bare, the warm pool, the in-flight-clone marker — and code/<slug>/, the
root's code dir, holds checkouts and only checkouts. A root carrying a superseded layout
(its bare inside the code dir) and a root whose code dir already holds somebody's files
both read as missing to the clone arm's probes, and cloning either would lay a second
root down beside what is there.
Realization refuses both instead, naming what it found: reconcile adds and never deletes,
so the four answers a declared-but-absent root can get are two refusals, a trunk recovery
and a clone (worktrees.html § Root realization). A refusal is a failed outcome, so
the daemon degrades that root and waits; grove doctor --fix migrates a legacy root in
place, and the engine re-derives it from disk.
The manifest is edited format-preservingly through toml_edit, under an exclusive
advisory flock on a sibling manifest.toml.lock — never on the manifest itself, which
a write replaces by rename, so a lock on its inode would not survive its own
publication — and canonicalized (roots sorted by slug, worktrees by name, one blank line
between tables) on both grove's own writes and a human's hand-edit. grove-ops::manifest
is its sole owner; the CLI edits it offline and the daemon edits it in-process.
One realizer, ever
Declaration is universal — the manifest is written by whoever runs the command. Realization is not: it belongs to a running daemon whenever one answers, and to the CLI only when none does.
Every command that delegates a state change probes GET /api/health first and reads
the answer three ways (grove::api::Reachability):
Up — a daemon answered | Busy — connected, no answer in time | Offline — connection refused | |
|---|---|---|---|
clone add · tree add | declare, then POST /api/roots/reconcile | declare only | realize in-process |
clone remove · tree remove | delegate to the API | refuse, exit 4 | remove in-process |
doctor | POST /api/doctor | error, exit 4 | run in-process |
sync | POST /api/roots/sync (accept-only) | refuse, exit 4 | roots::sync in-process |
tree list | the daemon's snapshot, else the disk | the disk | the disk |
apply | always local | always local | always local |
Only a connection-level failure means "no daemon". A timeout, or any other post-connect
error, means something is there and slow — realizing in-process against it would race
that daemon into a dual clone. A destructive op has no declare-only analogue, so Busy
refuses outright rather than guessing.
sync refuses on Busy for the same reason a remove does, arrived at from the other
side: it is a git write on the root (fetch, fast-forward, prune stranded warm slots),
so there is nothing to merely declare and fetching here would put a second writer in a
root whose daemon may be mid-reconcile. On Up it delegates without waiting — the
daemon's sync is accept-only — so the outcome is read from grove tree list or the event
stream rather than returned; offline it runs roots::sync here and prints the report the
delegating arm cannot produce. A diverged or dirty trunk is exit 0 either way: carried
law 9 makes it a report, not a failure, and the two arms must not disagree about a state
only one of them can see.
tree list is the row that never fails on the daemon's account: a read has no realizer
to race, so a snapshot that does not arrive — or whose row the daemon marks
unavailable — falls back to the disk rather than reporting the daemon's empty list as
an answer.
apply is the one exception: it probes nothing and realizes in this process whatever
the manifest declares — the offline realizer, exposed as a command. With a daemon up, grove apply
drives roots::reconcile_one on roots whose engine may be mid-reconcile, off any lane,
which is exactly the concurrent-caller hazard grove-ops warns about (lm:lane-is-callers,
worktrees.html § Serialization). It is carried as a hazard on crates/grove rather
than as a safe path; on a box with a daemon, grove clone add / POST /api/roots/reconcile
is the gated way to ask for the same convergence.
Landmarks: grove::commands (stele:landmark single-realizer) and
grove::api::ApiClient::reachable.
The daemon
grove serve installs a tracing subscriber with three sinks
($GROVE_HOME/serve.log, stderr unless it is a file, and the daemon's log ring) and a panic hook that logs
first, writes a start line with its version and pid, builds a multi-thread tokio runtime,
binds, and serves until the shutdown route or SIGTERM, SIGINT or SIGHUP drains the accept
loop; then it kills any git child still running, by process group, and writes an exit line
naming the reason. serve.log rotates to serve.log.1 at 10 MiB, one generation kept.
Health reads booting until the watcher's first read of the declared roots
lands.
Lanes — per-root serialization
grove-ops was written against a single-git-writer-per-root invariant and does not
enforce it; its flock serializes the manifest, deliberately not git. So every
git-writing op for a root — reconcile, sync, fill, promote, remove, worktree ops, doctor's
converge — goes through that root's lane, and only one runs at a time. Different roots run
in parallel. A lane is a tokio task; the work itself runs on spawn_blocking, because
grove-ops is synchronous.
Three queues, drained in order — except that waiting background work goes ahead of the next read after READ_DEPTH reads in a row — with no preemption of an op already running:
| tier | what | depth | shed behaviour |
|---|---|---|---|
Foreground | a mutation a user is waiting on: remove, promote, doctor converge | 256 | overflow → LaneError::Busy (HTTP 503 unavailable) |
Read | the git reads a snapshot row needs | 16 | overflow → Busy; a read whose caller has given up is dropped at the head of the lane |
Background | the engine's convergence: reconcile, sync, fill | unbounded | coalesced desired state — at most one outstanding per root per kind, so it cannot grow |
Reads earn a tier of their own because they arrive from polling — one lane job per root
per snapshot, on every GET /api/roots, every /api/events connect and every bus-lag
resync. Sharing the mutation queue let a dashboard shed a roots/remove with 503
unavailable. A read whose caller has already gone is abandoned; a mutation runs to
completion however bored its caller got.
Lanes spawn lazily and reap themselves after 60 s idle. The reap and a concurrent submit
both hold the registry lock across look-up-and-enqueue / re-check-and-unregister, so no
job is ever handed to a lane that has decided to die. Lanes::quiesced counts accepted-
and-unfinished jobs so a drain can ask whether any git write is still in flight.
Engine — one driver per declared root
One task per root, owning that root's status cache, its single background slot, and its
pool hint. At most one of reconcile / sync / fill is in flight, chosen in that priority
order, with every condition re-evaluated as level-triggered state on each completion. No
timers anywhere: a failure degrades one root, logs, and waits — the next event is the
retry. See engine.html.
engine::RootSet owns the map from declared slug to running engine. It subscribes to the
event bus and reconciles the map on every roots_changed, so anything that publishes that
event updates the engines for free.
Watcher — discover and announce
One task, three triggers, one job. The triggers are the HTTP nudge
(POST /api/roots/reconcile, and the poke a root removal sends after itself), an optional
notify watch on $GROVE_HOME/manifest.toml, and one announcement at boot. The job is
roots::adopt — declare any undeclared on-disk bare, never clone — then read the
declared set and publish roots_changed. Bursts are collapsed by a 150 ms debounce with
single-in-flight-plus-pending coalescing.
The nudge is the reliable path; the filesystem watch is opportunistic coverage for what
the CLI cannot tell the daemon about (a hand-edit, a git-synced manifest). It is off in
Config::new — every test drives the nudge — and on in Config::from_env
(GROVE_MANIFEST_FS_WATCH=0 turns it off in production). A watcher that fails to arm
logs a warning; it never fails the boot.
The watcher never clones, and an engine never reads the manifest for anyone but itself.
Events and logs — two channels, one wire
events::EventBus is a bounded (256) tokio broadcast carrying the four state events:
roots_changed, root_sync_changed, task_started, task_finished. Every one is
level-triggered — "something changed, re-read it" — so a subscriber that falls behind
loses freshness, never correctness: it is told it lagged and answers by re-reading the
world. EventBus::publish debug-asserts Event::is_state, so a snapshot or a log line
cannot reach the bus.
logs::LogRing is a separate channel: a bounded ring of the last 500 lines plus a
broadcast of each as it lands, fed by a tracing layer the process installs. A log line
is not level-triggered — a dropped one is gone — so sharing the bus would let a chatty
minute evict pending state events and leave a UI stale for the wrong reason. The layer
records a fixed shape (level, target, message) plus only the whitelisted fields grove's own
macros key on, each rendered through a redactor that strips URL userinfo; anything else is
dropped before it can reach a subscriber. Capture level is GROVE_LOG_RING (default
info), separate from GROVE_LOG, which is what the process writes to stderr.
stream is the read surface: snapshot() assembles the one Snapshot shape served both
as GET /api/roots and as the opening frame of GET /api/events, and pump produces the
stream — snapshot, then every state event, every log line, and a heartbeat comment every
15 s of silence, read off the clock seam.
Boot state and guards
booting → ready → stopping, with a sticky degraded(reason) beside it. The
watcher's roots read drives it: the first success lifts booting, a failure (a
manifest.toml that will not parse) degrades with roots unreadable: …, and a later
success clears that degrade and no other. mark_ready never clears a degrade: a fault
recorded while the process was coming up must survive the ready signal, or
grove upgrade's health smoke would accept the release that broke it.
mark_stopping does override a degrade — a daemon asked to drain is draining.
uptime_ms is wall-clock, so it counts the hours a laptop slept.
Three middleware layers — host, readiness, mutation — wrap every route
(api.html § Guards); with the loopback bind refusal in
config::guard_loopback they compensate for an unauthenticated API until served-mode auth lands.
Drain
POST /api/daemon/shutdown marks stopping before it answers, then schedules the accept
loop to stop after a 100 ms grace so the acknowledgement reaches the client; a stop signal
marks stopping and stops it at once. Daemon::serve
then spends one budget (drain_budget, default 5 s, armed off the clock seam at the
trigger) in two phases: open responses first, then lane work via Lanes::quiesced. What
does not land in either phase is logged by slug and abandoned.
An SSE response finishes only when its body stops being written,
so one peer that stopped reading would hold grove serve open forever and make every stop
end at SIGKILL. The default, plus the 2 s the runtime gets after it, sits under the CLI's own
10 s stop grace — pinned from both sides by
the_drain_budget_fits_inside_the_cli_stop_grace and
the_budgets_stay_in_their_intended_order — and under the server agent's
ExitTimeOut, which grove setup writes from that grace.
Process custody
Custody lives in the CLI (grove::server::ServerControl), not the daemon: grove on
launches the grove binary detached as <launcher> serve, tracks it by
$GROVE_HOME/grove.pid, and gates readiness on GET /api/health. grove off drains over
the API, then escalates SIGTERM → SIGKILL; the drain goes only to a listener that speaks
grove for this home, never to a stranger or to another home's daemon on the bind. A pid is re-verified before every escalation
signal — liveness plus a start-time identity token — so a recycled pid reads as stale
rather than as grove still running. A bind held with no pid file refuses the start rather
than adopting a stranger. grove reboot is off then on. When launchd has the
dev.gigantic.grove agent loaded for this home, launchd owns the process: grove off is
launchctl bootout and grove reboot one launchctl kickstart -k (a reload from the plist when a managed launch constraint would trip it), never a drain
raced against KeepAlive's respawn.
The client shakes hands on version: when the daemon's health names another
version than the CLI's, one stderr line names both and the fix
(grove reboot), and the command carries on. A
request that failed to connect is sent again, up to three sends 100 then 200 ms apart, so a
daemon mid-respawn answers it; a read is resent the same way on any failure short of a
timeout; a mutation that reached the daemon and got no answer is exit 7, outcome unknown,
and never resent.
The wire contract
grove-api is the one place the HTTP shape is written down, and both ends compile against
it. Three properties it exists to hold:
- One serializer.
Envelopeis the only thing that writes{ok, …}. - Discrimination on
ok. The envelope is tagged on its boolean, not on which ofdata/errorhappens to be present, and a body that disagrees with its own tag is a decode error. - Typed vocabularies. Error codes, root status, sync notes, boot status, event names
and the engine's policy curations are enums, declared through the
wire_enum!macro so the variant list, the serde spelling and theALLarray are one piece of text.
contracts/wire-vocab.json is the fixture: every vocabulary plus a key set per payload
shape, derived from the producing types. Each key set is one object deep, so a nested shape
carries its own group (git_status_keys, log_field_keys); one added under an
existing shape without a group is pinned by nothing. Details in
api.html § The vocabulary fixture.
The clock seam
grove_ops::clock is the one place Instant::now() is called. Every wall-clock budget is
a Deadline read off an injected Clock: production wires SystemClock, tests wire
TestClock and step time by hand, so a "60 s idle reap" test costs milliseconds and cannot
flake on a loaded machine. Deadline is absolute rather than a re-based Duration, so
handing it down a call chain cannot multiply the budget by the chain's depth.
harness_meta::every_wall_clock_read_goes_through_the_clock_seam fails the build when
an Instant::now() appears outside clock.rs.
tokio::time has a clock of its own, so the daemon's two timers do not use it directly:
wait::until / wait::within poll the injected clock on a 50 ms tick, which is what lets
a fake-clock advance() end a 60 s budget inside a test.
Telemetry
grove_ops::telemetry carries an OpenTelemetry span exporter behind GROVE_TELEMETRY,
with a redacting URL helper and the span attribute discipline (grove.op, grove.outcome,
grove.slug, redacted grove.url). It has no default destination: with GROVE_TELEMETRY on but
GROVE_OTLP_TRACES_ENDPOINT unset, init hands back the same no-op handle it hands back
when telemetry is off.
grove serve calls telemetry::init after its
subscriber and holds the handle until the runtime has drained, so the last batch is flushed. The exporter is a batch
processor posting OTLP/HTTP with a JSON body from its own thread, never from an op or a runtime thread, with an
Origin of the endpoint's own origin (floodgate refuses a write without one), under the service name
grove. The lane runs each named op through telemetry::in_op, a
grove-ops.<op> span with grove.slug timing the op alone, not its wait in the lane:
reconcile, sync and fill (the engine's background ops) and
remove_root, remove_worktree and doctor (the foreground ones). Snapshot reads emit none, and a
span's status is not set from an op's result.
Testing and gates
mise run check is format plus lint; mise run test is cargo nextest run --workspace
plus cargo test --doc --workspace. CI runs exactly those two, plus the
stele graph gate below.
Two tiers, by naming convention: a test whose name begins with slow_ is excluded by the
default nextest profile and included by NEXTEST_PROFILE=ci (.config/nextest.toml).
The architecture graph
The repo carries a stele graph: one node per
container and per crate, declared in that directory's AGENTS.md, each with a purpose, the
commands that prove it, and its invariants and hazards. Every claim is anchored to a
// stele:landmark <slug> comment (or a path#symbol) in the code it governs. A claim
proved by a separate test target names it in enforced_by:; a claim proved by the inline
mod tests beside its own anchor names nothing — enforced_by exempts a
claim from digest-staling, and the digest gate is the only thing watching a region whose
tests live inside it. .stele/graph.lock is the compiled
graph; the <!-- stele:begin router --> regions and .stele/index/ are projections of it.
.steleignore keeps plans/, .github/ and .claude/ out of the scan.
stele check # anchors resolve, dependencies match, every directory is routed,
# freshness holds, documented commands still resolve
stele emit --check # the committed projections still match the lock
stele build # recompile the lock and re-stamp each claim's `verified` mark
check and emit --check run in the hk pre-push hook and in CI. build runs in
neither — it re-stamps freshness, so a gate that ran it would launder staleness into a
green tick. When check reports a stale claim, re-read the anchored code, satisfy yourself
the claim still holds, then stele build by hand and commit the lock alongside the change
that staled it.
crates/grove-ops/tests/harness_meta.rs is the meta-check that keeps the harness honest:
every test file is reachable from mise run test, CI runs the same gate as local, the slow tier is configured and inhabited, every
wall-clock read goes through the seam, no scratch directory is parked in a static, and no
test hides behind #[ignore]. It reads the committed tree via git ls-files (through
grove_ops::git::git_command, so an inherited GIT_DIR cannot redirect it) and refuses an
inventory that does not contain itself.