From 3f5ef285036110c78210d69f07037a90152c0b45 Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 16 Nov 2026 13:01:30 +0100 Subject: [PATCH] docs: document phase 2 daemon Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 7 ++-- docs/phase2.md | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 docs/phase2.md diff --git a/CLAUDE.md b/CLAUDE.md index b377ac4..284ca5c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -4,21 +4,22 @@ Linux tool that keeps chosen git repos continuously in sync across machines, including uncommitted work, via snapshot refs (`refs/sync//`) on a primary remote. Planned as a Rust workspace (core, providers, daemon, CLI, Tauri GUI). Currently in -**Phase 0 done, Phase 1 implemented and validated against the real Gitea with two simulated machines (real two-PC run still pending)**: `grove-core` + `grove` CLI (`status`, `snapshot`, `add`, `sync`, `doctor`); daemon, GUI and providers do not exist yet. Do not start a +**Phase 0 done, Phase 1 implemented and validated against the real Gitea with two simulated machines (real two-PC run still pending)**: `grove-core` + `grove` CLI (`status`, `snapshot`, `add`, `sync`, `doctor`); daemon (`groved`, Phase 2) implemented (not yet run on the real two PCs); GUI and providers do not exist yet. Do not start a new phase before its predecessor's results are written to `docs/`. ## Commands - Check shell scripts: `for f in spikes/*.sh; do bash -n "$f"; done` - Throwaway Forgejo (dev only, Podman): `spikes/forgejo-up.sh`, then `spikes/phase0.sh` - Rust: `cargo fmt --check`, `cargo clippy --all-targets -- -D warnings`, `cargo test` +- Daemon: `cargo run -p grove-daemon --bin groved -- --config PATH`; client `grove daemon status` ## Architecture -See `docs/` (`phase0-spike.md`, `phase1.md`, `DECISIONS.md`). Git is the storage layer; shell out to +See `docs/` (`phase0-spike.md`, `phase1.md`, `phase2.md`, `DECISIONS.md`). Git is the storage layer; shell out to system `git`; snapshots use a temp `GIT_INDEX_FILE` + `commit-tree`, never touching the user's branch or index. ## Conventions -- Dependencies: serde+toml (config), clap+anyhow (CLI), approved. Ask before adding more. +- Dependencies: serde+toml (config), clap+anyhow (CLI), notify+serde_json (daemon), approved. Ask before adding more. - Everything in the repo is English. Open questions go to `docs/DECISIONS.md`. - Never destroy local work; divergence is surfaced, never auto-merged. diff --git a/docs/phase2.md b/docs/phase2.md new file mode 100644 index 0000000..cff2069 --- /dev/null +++ b/docs/phase2.md @@ -0,0 +1,89 @@ +# Phase 2: daemon + +`groved` (crate `grove-daemon`) is a per-user daemon. It adds no sync logic of its own: every pass +is `grove_core::sync::sync_repo`, the same code the CLI runs. The daemon only decides *when*. + +## Behaviour + +- **Repos**: every directory `root/` or `root//` that has a `.bare` or `.git` + (dot-directories skipped). Rediscovered at every full pass, so new repos are picked up. +- **Watching**: inotify (crate `notify`), recursive per repo. Changes under `.git`, `.bare`, + `.grove-stage` and anything matching `sync.ignore_extra` (exact component name or `*suffix`) are + ignored. If a watch cannot be set (typically `ENOSPC`, raise `fs.inotify.max_user_watches`) it is + logged and the periodic pass still covers that repo. +- **Debounce**: each event pushes the repo's next pass to `now + snapshot_debounce_ms`. +- **Periodic pass**: every `fetch_interval_s` all repos get a full pass (fetch, receive, apply + snapshots, materialize/archive). Also what catches missed events. +- **One worker thread**: passes never overlap. Events that arrive during a pass queue another pass; + it finds nothing to do and ends the chain. +- **Failures** (network, auth, git errors) back off exponentially per repo: 10 s, 20 s, ... capped at + 5 min, and are logged as `error`. `sync` over IPC clears the backoff. +- **Locks**: grove never takes the user's index (snapshots use a temp index) so git's own locks are + not contended; a git command that fails because the user holds a lock is just a failed pass that + is retried. +- **Pause**: `pause` stops all passes (events keep accumulating); `resume` runs a full pass. +- **Crash safety**: nothing durable lives in the daemon. Killing it at any point leaves the working + tree and index untouched; the next start rediscovers and resyncs (covered by a kill -9 test). + There is no signal handler: SIGTERM ends the process, which is safe for the same reason. + +## IPC (protocol version 1) + +Unix socket `$XDG_RUNTIME_DIR/grove/grove.sock` (directory mode 0700; falls back to the state dir). +Line-delimited JSON, one request per line: + +```json +{"v":1,"cmd":"status"} +{"v":1,"ok":true,"data":{"machine":"pca","paused":false,"repos":[{"path":"...","phase":"idle", + "last_sync":1790000000,"last_error":null,"failures":0, + "worktrees":[{"branch":"main","state":"synced"}]}]}} +``` + +| `cmd` | Extra fields | Result | +|---|---|---| +| `ping` | | `"pong"` | +| `status` | | machine, paused flag, repos with phase (`idle`/`pending`/`syncing`/`error`) and worktree states | +| `pause`, `resume` | | `{"paused": bool}` | +| `sync` | `repo` (optional path) | `"queued"` | +| `log` | `limit` (default 50) | recent activity events | +| `subscribe` | | `"subscribed"`, then a stream of `{"v":1,"event":{...}}` until the client closes | + +Errors: `{"v":1,"ok":false,"error":"..."}`. A different `v` is rejected. A second daemon on the same +socket is refused; a stale socket file left by a crash is replaced. + +## Activity log + +JSON lines in `/activity.jsonl` (rotated at 5 MiB to `.jsonl.1`), the last 500 events in +memory, and live via `subscribe`. Kinds: `start`, `stop`, `snapshot`, `fast_forward`, `applied`, +`declined`, `state` (worktree state changed), `materialized`, `archived`, `error`, `watch_error`, +`pause`, `resume`. + +## CLI + +`grove daemon status | pause | resume | sync [REPO] | log [--limit N]`. + +## Install (systemd user service) + +```bash +install -Dm755 target/release/groved ~/.local/bin/groved +install -Dm644 contrib/groved.service ~/.config/systemd/user/groved.service +systemctl --user daemon-reload +systemctl --user enable --now groved +journalctl --user -u groved -f +``` + +Socket activation was considered and not done: the daemon must run anyway to watch files. + +## Found by the daemon tests + +A worktree that had another machine's snapshot applied stayed `BehindDirty` forever once the author +committed and pushed that work. `receive` now adopts the upstream commit with a mixed reset when the +worktree's content is byte-identical to the upstream tree (`receive::adopt_if_identical`); any +other dirty worktree is still left alone. + +## Not done + +- GUI/tray (Phase 3), providers and wizards (Phase 4). +- Remote-change notifications (webhooks); only polling at `fetch_interval_s`. +- Config reload: restart the daemon after editing `config.toml`. +- Watching skips no heavy ignored directories (`node_modules`, `target`) at watch time; add them to + `sync.ignore_extra` to avoid triggering passes, and raise the inotify limit if needed.