keepawake

package
v0.117.4 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 12 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 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

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

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

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

Modes lists the valid modes in the order a screen offers them.

Functions

func Normalize

func Normalize(m string) string

Normalize maps the stored value to a mode: empty (never chosen) and anything unknown are the default.

func ValidMode

func ValidMode(m string) bool

ValidMode reports whether m is one of Modes.

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

func (*Keeper) Check

func (k *Keeper) Check(ctx context.Context)

Check decides once whether to hold, and takes or releases accordingly.

func (*Keeper) Run

func (k *Keeper) Run(ctx context.Context, every time.Duration)

Run checks every `every` until ctx ends, then releases.

func (*Keeper) Status

func (k *Keeper) Status() Status

Status reports the keeper's state for the API.

func (*Keeper) Wake

func (k *Keeper) Wake()

Wake asks Run for a check now, for a setting just changed.

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

Jump to

Keyboard shortcuts

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