Documentation
¶
Overview ¶
Package keepawake keeps the computer from idle-sleeping while agents work (ADR-049). The daemon asks the operating system not to put the machine to sleep for inactivity — the system, not the display: the screen still dims and locks — and lets go the moment nothing is working, on exit, and with the daemon if it dies.
The OS call sits behind Holder, so the rules here are tested without one: no caffeinate, no power assertion, no dialog in a test run. Every failure is logged and the feature degrades to off; nothing here may stop the daemon or a session.
Index ¶
Constants ¶
const ( // ModeOff never holds the machine awake. ModeOff = "off" // ModeWorking holds it while any agent session is working, the default. ModeWorking = "working" // ModeAlways holds it for as long as the daemon runs. ModeAlways = "always" )
The three modes, as stored in config.json and carried by the API.
const DefaultMode = ModeWorking
DefaultMode is what an install that never chose gets. See ADR-049 for why it is on: the assertion only stops idle sleep, it lets go within a check of the last agent stopping, and a laptop's lid still sleeps it.
const Silence = 30 * time.Minute
Silence is how long a session that has said nothing is still believed to be working. A tool call in flight sends nothing until it returns, so a twenty-minute build reads as silence; the bound is what keeps a session whose process died mid-turn from holding the machine awake all night. The same half hour bounds a silent subagent (store.LiveSubagentWindow).
Variables ¶
var Modes = []string{ModeOff, ModeWorking, ModeAlways}
Modes lists the valid modes in the order a screen offers them.
Functions ¶
Types ¶
type Holder ¶
type Holder interface {
// Available reports whether this machine offers an assertion; when it
// does not, reason says why in a line a person can act on.
Available() (ok bool, reason string)
// Hold takes the assertion. Holding twice is one hold.
Hold() error
// Release lets it go. Releasing what is not held is a no-op.
Release()
// Held reports whether the assertion is in force now: a helper process
// that died on its own is not holding anything.
Held() bool
}
Holder takes and releases the operating system's "do not idle-sleep" assertion. Implementations are per OS (holder_*.go).
func NewOS ¶
func NewOS() Holder
NewOS returns this machine's holder: systemd-inhibit taking an idle and sleep lock for as long as the command it runs lives. That command is a shell loop that ends once the daemon's pid is gone (kill -0 sends no signal; it only asks whether the pid exists), so a crash lets go within ten seconds; Release stops the whole process group at once.
Without systemd-inhibit (no systemd, a container) there is nothing to ask, and the feature says so instead of pretending.
type Keeper ¶
type Keeper struct {
Holder Holder
// Mode returns the stored mode; it is normalized here.
Mode func() string
// Busy returns how many sessions are working now. Asked only in
// ModeWorking; an error counts as none, so a broken query lets go rather
// than holding forever.
Busy func(ctx context.Context) (int, error)
Log *slog.Logger
Now func() time.Time
// contains filtered or unexported fields
}
Keeper decides when to hold. Mode and Busy are read on every check, so a changed setting takes effect at the next one (Wake makes that immediate).
type Status ¶
type Status struct {
Mode string `json:"mode"`
// Holding is true while the assertion is in force.
Holding bool `json:"holding"`
// Since is when the current hold began, Unix ms; 0 when not holding.
Since int64 `json:"since,omitempty"`
// Working is how many sessions counted as working at the last check.
Working int `json:"working"`
// Available is false where the machine offers no assertion (a Linux
// without systemd-inhibit); Reason says why.
Available bool `json:"available"`
Reason string `json:"reason,omitempty"`
// Error is why the last attempt to hold failed, empty when it did not.
Error string `json:"error,omitempty"`
}
Status is GET /v1/keepawake (03-contracts.md § Keep awake).