250adf5e(#3185) has Keep, turned off, wait on filememo.Written for the setting, but it took the channel after the look (level, which is settings.Load in the gateway), not before it. A save in between, the user turning Keep awake on, had Forget close the channel from before the look, and Keep waited on the one made after it until its beat, filememo.OthersEvery(): 5s polled, a minute while Watch runs (where the watch's event for the same save still woke it, if that came after Written). A write from the CLI or a second magpie, which only the watch's wake tells of, had the same window. The window is narrow, from Load's read of settings.json (its unlock, for a save in this process, which waits for that) to Written, and wider only while let() lets go of a hold. It came up in review of #3632. Keep now takes Written before the look, as middleware's observer, OTel's session watch, pluginrt.KeepFresh and the agent's Codex daemon check do, and waits on that channel when the look found it off. On, it waits for its tick alone, as before: the channel is taken on every pass but waited on only after a look that found it off. What was run (tree e8e23dae, on origin/mainef0a03e1, go1.26.8, linux/amd64): - TestKeepOffSeesAWriteDuringTheLook (new): the level func turns the setting on and writes it (filememo.Forget) right after its read, at least 2s before the next beat, and Keep must ask it again before that beat. It times its own level func's second call, not a hold: a Keep that another test's failure leaves running holds through the same stand-in. ok at -count=5, 0.00s each. - Broken by hand, each put back after, -count=3 of the new test alone and of the whole package: - ef0a03e1's awake.go (Written taken after the look): red 3 of 3 and 3 of 3, the new test only, "turned on during the look, the setting was looked at again 4.999540055s after it was written, at the beat (00:22:45), not at once", 4.46s to 5.00s across the six. - Written taken right after level(): the same, 4.44s to 5.00s. - Written taken before the look but not waited on when off: the new test red 3 of 3 and 3 of 3, 4.43s to 5.00s; in the whole package TestKeep and TestKeepOffWaitsForTheSetting go red beside it. - Written taken once, above the loop: every test passes, the new one too. After the first write Keep would spin while off, and no test writes and leaves it off; noted on #3687, with the other test fixes this file needs. - The new test started 1.5s before a beat, so that it waits that beat out before the write (a scratch wrapper, not committed): ok 3 of 3, and red 3 of 3 with ef0a03e1's awake.go, 5.00s each. - Outside graftcp, ./internal/awake with -race -count=20, -count=20, and -race -shuffle=on -count=20: 100 of 100 ok each. The new test took 0.00s in 19 of each 20, and 1.87s, 1.91s and 1.90s in the one that started under 2s before a beat. TestKeepOffWaitsForTheSetting, unchanged here, is red about once in 100 runs on098ac969(4 of 400) when a 5s beat lands in its 50ms window: #3687. It didn't come up in these runs. - gofmt -l clean; GOOS=linux and darwin go vet -tags nogui ./..., GOOS=windows go vet ./..., GOOS=linux and darwin go build -tags nogui ./... and GOOS=windows go build ./...: exit 0 each. go vet and go build ./... without the tag on linux stop at pkg-config here (no libsoup-3.0 or gio-unix-2.0), exit 1. - Full suite outside graftcp (systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI, -timeout 45m, log kept): 66 ok, awake, beat, filememo and settings among them, and 6 red, none in what this changes: - agent TestInstalledVersion, TestHowInstalled, TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir: #3141; TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout: #3641. - edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram: this box's umask 077, #3640. - gateway TestPluginToldWhereTheAgentWorks: this box's /tmp/.git, #3675. - gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie: #3649. - provider: "fatal error: close of synctest channel from outside bubble", main's CI red, #3632. Not tried: a real gateway with Keep awake turned on inside the window (it is too narrow to hit by hand; the test puts the write there), and macOS and Windows, vet and build only (nothing here depends on the OS). Reviewed on macOS arm64, go1.26.8: internal/awake -race -count=20 ok. Broken by hand: main's awake.go turned TestKeepOffSeesAWriteDuringTheLook red twice of two ("looked at again 4.09s / 5.00s after it was written, at the beat"). The other loops waiting on filememo.Written (middleware points, otlp sessions, pluginrt config, codex daemon) already take it before their look. gofmt clean; vet ok for linux nogui, windows, darwin.
117 lines
3.7 KiB
Go
117 lines
3.7 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"
|
|
|
|
"github.com/yetone/magpie/internal/beat"
|
|
"github.com/yetone/magpie/internal/filememo"
|
|
)
|
|
|
|
// 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
|
|
for {
|
|
// taken before the look, so a write made during it isn't missed
|
|
written := filememo.Written()
|
|
l := level()
|
|
if 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
|
|
}
|
|
// turned off (as it is unless the user turns it on), only a change
|
|
// to settings can turn it on: waited for, not looked for each few
|
|
// seconds (#3185: these looks woke an idle magpie)
|
|
every, changed := tick, (<-chan struct{})(nil)
|
|
if l == Off {
|
|
every, changed = filememo.OthersEvery(), written
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-changed:
|
|
case <-beat.Next(every):
|
|
}
|
|
}
|
|
}
|