Commit Graph
6 Commits
Author SHA1 Message Date
yetone e8efe05b22 awake: TestKeepOffWaitsForTheSetting keeps its 50ms clear of a beat (#3687)
Turned off, Keep looks at the setting at each beat, the multiples of
filememo.OthersEvery (5s) on the clock. The test counted Keep's looks
over a fixed 50ms, so a beat inside that window made a second look and
the test red, about one run in 100. That happened in the full suite on
mote at e9bde2520 ("looked at the setting 2 times in 50ms"). The test
now waits out a beat that is under a second away before it starts Keep,
as TestKeepOffSeesAWriteDuringTheLook does. Keep's code is unchanged,
as #3687 found.

Run (mote): go test -count=400 -run 'TestKeepOffWaitsForTheSetting$'
./internal/awake ok (26.7s); go test ./internal/awake ./internal/beat
ok; GOOS=linux and GOOS=windows go vet ./internal/awake ok; gofmt clean.
Broken by hand: the window put 30ms before a beat (sleep to it) without
the new wait, red 3 of 3 with the line above. With the wait, ok 3 of 3.

Not done here: #3687's comment also asks for a write-while-off check in
this test and a deferred cancel in TestKeep and TestKeepDisplay. Those
stay open on #3687.
2026-11-07 19:42:47 +08:00
JoeyWu-tech 8ff90bee3d awake: Keep awake turned on just as the setting is read takes hold at once, not up to a minute later (#3686)
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/main ef0a03e1, 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 on 098ac969 (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.
2026-10-27 16:00:46 +08:00
yetone 250adf5ec5 awake: turned off, the setting is waited for, not looked at every 5s (#3185)
Keep awake is off unless the user turns it on, and with it off Keep still
read settings.json on every 5s beat to see whether it had been. Only a
settings write can turn it on, and every one goes through settings.save,
which closes filememo.Written. Off, Keep now waits on that, and looks
again once a minute (OthersEvery) for another process's write the watch
missed. On, it keeps its 5s look, since a request starting is not a write.

Run at this head:
- go test -count=3 ./internal/awake ok. TestKeepOffWaitsForTheSetting is
  red on the old loop (52 looks in 50ms) and green here; TestKeep and
  TestKeepDisplay now write the setting as settings.save does.
- gofmt clean; GOOS=linux / windows go vet -tags nogui ./internal/awake ok.
- Real gateway, sandbox HOME, idle, 120s after 90s warm-up: with the
  watch alone 1.8 wakeups/s, with this too 1.3 (v0.1.1561: 2.1 to 2.7).
2026-10-26 11:22:20 +08:00
yetone 466cafa0cf idle: the loops that look every few seconds wake together on the clock's beat (#3185)
An idle magpie serve still woke ~6 times a second with nothing to do.
Its loops each kept a ticker of their own, started at their own instant:
awake.Keep (5s), the plugin config's KeepFresh (5s), the middleware
observer (5s), OTel's session watch (5s idle), the Codex daemon check
(15s), dsh's wiring (30s), the model lists' look (1m). Every one woke
the process apart, a few threads each time.

internal/beat's Next(every) fires at the next multiple of every on the
clock, so the 5s loops come due at one instant, the 15s and 30s ones on
every third and sixth of it, and the runtime runs timers due together
in one wakeup. Each loop now waits on beat.Next instead of its ticker.
No channel is shared between callers (a synctest bubble's channel
reached from another crashes the process, #3632).

Run at this head (05ea29ac8 + this):
- Sandbox HOME serve, idle, 90s warm-up then 120s of proc_pid_rusage
  (pkg_idle_wkups + interrupt_wkups), old (origin/main 19253f67b) and new
  taking turns: old 6.1, 5.3, 6.2 wakeups/s; new 2.7, 2.6 (an earlier
  shared-timer form of beat), 3.0 (this one). CPU 0.20-0.36 ms/s old,
  0.11-0.22 new.
- TestIntervalsMeet (5s/15s/30s/1m beats from 09:00:03.2 and on a beat),
  TestNextFires; go test ./internal/beat ./internal/awake
  ./internal/middleware ./internal/otlp -count=3, ./internal/pluginrt,
  ./internal/agent -run 'Dsh|CodexDaemon', ./internal/provider -skip
  TestUnreadLoginsWriteErrorsKeepCredentials (#3632): ok.
  ./internal/pluginrt -count=3 fails TestKeepFreshFollowsPluginsYml
  ("two plugins fresh-test") on origin/main too; -count=1 is ok.
- gofmt; go vet darwin, GOOS=linux and GOOS=windows -tags nogui.
Not tried: a GUI build's wakeups (the tray process has AppKit's own).
2026-10-26 10:27:01 +08:00
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
yetone ae23ad83f7 awake, gateway, gui: Settings › General › Keep awake while agents work keeps the computer from sleeping by itself while agents work through magpie (xiao_wang24004 on X) — the computer went to sleep in the middle of an agent's task. With the new setting on (off by default, this computer's own: KeepOwn), the magpie serving the gateway holds the system's idle sleep off while a request is in flight or a Claude Code turn waits on its tool results, and for ten minutes after the last request ended (an agent running a build or the tests between requests), looking every 5 s, and lets go once idle or turned off. On a Mac it runs caffeinate -i -w <magpie's pid> (PreventUserIdleSystemSleep, gone with magpie), on Windows SetThreadExecutionState(ES_CONTINUOUS|ES_SYSTEM_REQUIRED) from a thread of its own, on Linux systemd-inhibit --what=idle:sleep (Pdeathsig; said once in the log and nothing else where it is missing). The display may still turn off, and closing the lid still sleeps. en/zh/ja/de. Tested with a real DeepSeek account through magpie web --gateway in a sandbox HOME: a streamed request had caffeinate asserting on behalf of magpie's pid in pmset -g assertions, and turning the setting off through /api/settings released it within a tick. TestKeep (held once while busy and within the grace, let go past it, when off and when Keep ends; -race) and TestKeepAwakeKeptAndOwn are new; keep-awake.test.cjs (WebKit and Chromium, en and zh: off by default, On/Off post keepAwake, another setting's save keeps it, no scroll, no left border, shown on a browser's page) fails without the change; lightweight and click-scroll pass in WebKit. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass. 2026-10-05 19:46:02 +08:00