service

package
v0.22.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func NewPlan(exe, dataDir string, port int, hiveDir string) (Plan, error)

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

func (p Plan) Args() []string

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

func (p Plan) Installed() (present, current bool, path string, err error)

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

func (p Plan) Load(path string) error

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

func (p Plan) LogPath() string

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) Mechanism

func (p Plan) Mechanism() string

Mechanism names what is used on this platform, for the status output.

func (Plan) OS

func (p Plan) OS() string

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) Path

func (p Plan) Path() (string, error)

Path is where this platform's definition file lives.

func (Plan) PreflightErr

func (p Plan) PreflightErr() error

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

func (p Plan) Registered() bool

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

func (p Plan) Remove() (removed bool, path string, err error)

Remove deletes the definition file. A missing file is not an error — that is the clean no-op uninstall promises.

func (Plan) Render

func (p Plan) Render() ([]byte, os.FileMode, error)

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

func (p Plan) Supported() bool

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

func (p Plan) Unload() error

Unload deregisters the service. It never fails because the service was not registered — that is the clean no-op uninstall promises.

func (Plan) Write

func (p Plan) Write() (string, error)

Write creates the definition file (and its directory), replacing any previous version. Writing the same plan twice leaves exactly one file with the same contents — the whole of install's idempotency, since the load step below is itself idempotent per platform.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL