Documentation
¶
Overview ¶
Package service registers the Caprock daemon with the operating system's own login-time supervisor, so it comes back after a reboot without the user running `caprock up` by hand.
Why this exists: a monitoring tool you have to restart manually is a tool you stop trusting. The daemon already survives a closed terminal (`caprock up` detaches), but nothing brings it back after a reboot.
Three mechanisms, one per OS, all of them user-level — nothing here needs root, an installer, or a system-wide daemon, and nothing is written outside the user's own config/home locations (never `~/.claude/`):
- macOS — a LaunchAgent plist in ~/Library/LaunchAgents, loaded with `launchctl bootstrap gui/<uid>`.
- Linux — a systemd *user* unit in ~/.config/systemd/user, enabled with `systemctl --user enable --now`.
- Windows — a .cmd script in the per-user Startup folder. See windowsStartupDir for why not a Scheduled Task.
The generated file always runs the daemon in the *foreground*: every one of these supervisors tracks the process it started, so the double-fork that `caprock up` normally does would make the supervisor think the daemon exited immediately and restart it forever.
Layering: this package generates and inspects the definition files (pure functions over a Plan, testable on every OS) and shells out to the platform tool only in Load/Unload/Running. The command layer in cmd/caprock owns the printing.
Index ¶
- Constants
- type Plan
- func (p Plan) Args() []string
- func (p Plan) Installed() (present, current bool, path string, err error)
- func (p Plan) Load(path string) error
- func (p Plan) LogPath() string
- func (p Plan) Mechanism() string
- func (p Plan) OS() string
- func (p Plan) Path() (string, error)
- func (p Plan) PreflightErr() error
- func (p Plan) Registered() bool
- func (p Plan) Remove() (removed bool, path string, err error)
- func (p Plan) Render() ([]byte, os.FileMode, error)
- func (p Plan) Supported() bool
- func (p Plan) Unload() error
- func (p Plan) Write() (string, error)
Constants ¶
const Label = "dev.caprock.daemon"
Label is the reverse-DNS identifier used by launchd and, as a stem, by the systemd unit and the Windows startup script. Changing it orphans existing installs, so it is a constant, not a setting.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Plan ¶
type Plan struct {
// Exe is the absolute path of the caprock binary to run. Always
// os.Executable() in production — a bare "caprock" would depend on a PATH
// that login agents do not reliably have.
Exe string
// DataDir is the resolved Caprock data directory. It is passed to the
// daemon through CAPROCK_DATA_DIR so a user who runs with a custom data
// dir keeps it after a reboot.
DataDir string
// Port is the port the service listens on, taken from the user's
// config.json (or --port at install time).
Port int
// HiveDir, when non-empty, enables Phase 2 orchestration at login.
HiveDir string
// Home is the user's home directory. Injected rather than looked up so
// tests never touch the real home.
Home string
// GOOS selects the mechanism. Defaults to runtime.GOOS; tests set it to
// generate every platform's file from one machine.
GOOS string
}
Plan is everything the generated definition needs. It is resolved once by NewPlan and then passed to the pure generator functions, which is what makes the file contents testable on an OS other than the one they target.
func NewPlan ¶
NewPlan resolves a plan from the running process. exe is normally os.Executable(); it is a parameter so tests do not depend on the test binary's own path.
func (Plan) Args ¶
Args is the argument vector the installed service runs, after the binary itself. --foreground because the supervisor does the supervising; --no-open because nobody wants a browser tab at every login; --no-hooks because hook and statusline registration is a consent decision that belongs to an interactive `caprock up`, not to a login agent.
func (Plan) Installed ¶
Installed reports whether a definition file written by Caprock is present, and whether its contents already match what this plan would write. A file that exists but differs (an upgrade moved the binary, the port changed) is "installed but stale" — install rewrites it, which is what makes install idempotent without duplicating anything.
func (Plan) Load ¶
Load registers the written definition with the platform supervisor and starts it. It is idempotent: re-loading an already-loaded service replaces the registration rather than duplicating it.
On Windows there is nothing to load — the file in the Startup folder *is* the registration — so this is a no-op and the caller says so.
func (Plan) LogPath ¶
LogPath is where the supervisor's own stdout/stderr for the daemon goes. It sits in the data dir next to caprock.log, so `caprock service` never writes outside the user's Caprock directory.
func (Plan) OS ¶
OS is the target OS this plan generates for. Exported so the command layer can tailor its output (the Windows caveat) without duplicating the default.
func (Plan) PreflightErr ¶
PreflightErr returns a non-nil, actionable error when the platform mechanism exists in principle but cannot be used on this machine — today only the missing-systemd case. Checked before anything is written, so a machine without a systemd user session does not end up with a unit file that nothing will ever read.
func (Plan) Registered ¶
Registered asks the supervisor — not the filesystem — whether the service will start at the next login. The two can disagree: a plist deleted by hand stays loaded until logout, and a unit file can exist without ever being enabled. `service status` reports both, which is what makes a half-installed state visible instead of confusing.
func (Plan) Remove ¶
Remove deletes the definition file. A missing file is not an error — that is the clean no-op uninstall promises.
func (Plan) Render ¶
Render produces the exact bytes of the definition file for this platform, plus the mode to write it with. It is a pure function of the plan — no filesystem, no subprocess — which is what lets one machine's tests assert all three platforms' output.
func (Plan) Supported ¶
Supported reports whether this OS has a mechanism at all, so callers can fail with the actionable message instead of writing a file nothing will read.
func (Plan) Unload ¶
Unload deregisters the service. It never fails because the service was not registered — that is the clean no-op uninstall promises.