Files
magpie/internal/awake/awake.go
T
yetone 56b770ec99 settings: keep awake can keep the display on too (#975, Hu9956: magpie's keep-awake held the system up, but the screen still went black and locked, and their agents record the screen to check their work, which a locked screen stops) — Settings › General's "Keep awake while agents work" row now has three segments, Off / On / Screen on too, saved as keepAwake and the new keepAwakeDisplay (default off, so On and every existing settings.json behave as before). awake.Keep takes a Level (Off, System, Display) in place of a bool; a level changed while held releases and takes the hold anew. On a Mac the display level runs caffeinate -i -d -w <pid> (pmset -g assertions on a caffeinate of the same flags shows PreventUserIdleDisplaySleep beside PreventUserIdleSystemSleep); on Windows SetThreadExecutionState gets ES_DISPLAY_REQUIRED too; on Linux logind's idle inhibitor (systemd-inhibit --what=idle:sleep, unchanged) is all there is, and whether the screen stays on is up to the desktop. keepAwakeDisplay is this computer's own (KeepOwn) and goes in prefsKeep, so another setting's save keeps it. The row's line says the display stays on when it's chosen; it keeps its English in data-en, so a language change re-translates it. zh/ja/de strings added. No TUI or CLI has keep-awake, so none changed. TestKeepDisplay (taken once, taken anew with the display, not again at the same level, back to System, released at Off), TestCaffeinateArgs, TestExecutionState (GOOS=windows test build) and TestKeepAwakeKeptAndOwn cover it; go test ./internal/awake ./internal/settings ./internal/gui ./internal/gateway pass, -race -count=5 on awake. keep-awake.test.cjs now runs en/zh/ja/de at 900px and 440px in Chromium and WebKit (display posts keepAwake+keepAwakeDisplay true, the line changes, another setting's save keeps it, segments fit the row, On and Off clear it, no click moves the page): 32/32 twice; gui-ja, gui-de, i18n-keys and click-scroll pass. go vet (darwin, windows, linux), darwin, linux (nogui) and windows builds pass. Not tried with an agent recording the screen; the reporter is asked to confirm.
2026-10-06 15:33:28 +08:00

105 lines
3.2 KiB
Go

// Package awake keeps the computer from going to sleep by itself while
// agents work through magpie's gateway (xiao_wang24004 on X: the computer
// went to sleep in the middle of an agent's task). With settings.KeepAwake
// on, the magpie serving the gateway holds the system's sleep off while a
// request is in flight, a Claude Code turn waits on its tool results, or a
// request ended less than Grace ago — an agent running a build or the tests
// between one request and the next — and lets go once it is idle. Only the
// system's sleep of its own is held off: the display still turns off, unless
// settings.KeepAwakeDisplay asks for it to stay on too (#975, Hu9956: an
// agent recording the screen to check its work found it locked), and
// closing a laptop's lid or choosing Sleep still sleeps.
//
// On a Mac it is `caffeinate -i` (-d with the display), on Windows
// SetThreadExecutionState with ES_SYSTEM_REQUIRED (and ES_DISPLAY_REQUIRED),
// on Linux `systemd-inhibit --what=idle:sleep`, whose idle inhibitor the
// desktop may or may not take for the screen too; where none is there it
// does nothing.
package awake
import (
"context"
"log"
"time"
)
// Grace is how long after the last request ended sleep is still held off.
var Grace = 10 * time.Minute
// tick is how often Keep looks again.
var tick = 5 * time.Second
// hold takes the system's sleep off, and with display the display's too,
// until the release it returns is called; a test stands in for it.
var hold = takeHold
// Level is how much Keep holds off.
type Level int
const (
Off Level = iota // nothing
System // the system's sleep
Display // the system's sleep and the display's
)
// State is what the gateway has in flight, as Keep needs to know it.
type State struct {
Busy bool // a request in flight or a turn waiting on tools
Last time.Time // when the last request ended
}
// want is whether sleep is held off at now for st.
func want(st State, now time.Time) bool {
return st.Busy || (!st.Last.IsZero() && now.Sub(st.Last) < Grace)
}
// Keep holds sleep off as far as level says while state says the agents are
// at work, until ctx ends. A level changed while held is taken anew.
func Keep(ctx context.Context, level func() Level, state func() State) {
var release func()
held := Off
let := func() {
if release != nil {
release()
release = nil
held = Off
log.Print("awake: the agents are idle; the computer may sleep again")
}
}
defer let()
failed := false
t := time.NewTicker(tick)
defer t.Stop()
for {
if l := level(); l != Off && want(state(), time.Now()) {
if release != nil && held != l {
release()
release = nil
}
if release == nil && !failed {
r, err := hold(l == Display)
if err != nil {
// not there (a Linux without systemd): said once
log.Print("awake: can't keep the computer awake: ", err)
failed = true
} else {
release, held = r, l
if l == Display {
log.Print("awake: agents at work; keeping the computer from sleeping and its display on")
} else {
log.Print("awake: agents at work; keeping the computer from sleeping")
}
}
}
} else {
let()
failed = false
}
select {
case <-ctx.Done():
return
case <-t.C:
}
}
}