bash and bash_watch waits misbehave when macOS sleeps: watch overruns by the sleep, and a waiting bash call fails at wake
Version: observed on 0.63.5 (OpenCode 2.2.7, macOS, Apple silicon, on battery). I have not reproduced it on 0.64.0, but the 0.64.0 bundle still has both clocks described below.
What happens
When the Mac sleeps while a bash call or a bash_watch is waiting, the wait misbehaves in one of two ways, depending on which deadline is hit first.
bash_watchruns long by the length of the sleep. A watch with the 270 s worker cap returned after 441 s, 705 s, 1018 s and 2384 s of wall time. In each case the Mac was asleep for the difference, and the awake time inside the call was 248-268 s. So the watch deadline counts awake time only.- A waiting
bashcall fails the moment the Mac wakes. Five calls failed withRequest "bash" (id=N) timed out after 280000ms(or25000ms), each logged within about one second of a Wake or DarkWake inpmset -g log. Three of the five errors said it was unknown whether the command had started; two said the task had started and its reply arrived after the deadline. So the request deadline counts wall time, sleep included, and it fires before the reply that is already on its way.
The hang watchdog fires at the same instant with a large silent_ms, probes the child, and keeps it, so nothing is killed.
Why it matters
Case 2 tells the agent a command timed out, sometimes with "unknown whether started", when the command did start and is fine. An agent can then run it a second time. For a command with side effects that is a real risk.
Case 1 only costs time, but a watch that was asked to wait 270 s can hold a worker for 40 minutes.
Where it seems to come from
- The watch deadline in
waitForBashStatusisstartedAt + effectiveWaitMs, read fromperformance.now(). I believe that clock does not advance while macOS sleeps, which matches the awake-time numbers above. I did not test Bun's behaviour directly. - The per-request deadline in the bridge is a plain
setTimeouton wall time.
Reproduction
- On a Mac on battery, in an OpenCode subagent session:
shell({ command: "sleep 1500", background: true }), thenbash_watch({ taskId })with no timeout, so the worker cap of 270 s applies. - About three minutes in, let the Mac sleep (close the lid or
pmset sleepnow) for a few minutes, then wake it. - Expected: the watch returns after about 270 s of wall time, or at wake if that is later. Observed: it returns after 270 s of awake time.
Variant for case 2: shell({ command: "sleep 400", wait: true, timeout: 400000 }), and sleep the Mac past the 280 s mark. At wake the call fails with Request "bash" (id=N) timed out after 280000ms.
Suggestion
Use one clock for both deadlines. If a wake is detected (wall time jumped far more than the monotonic clock), re-arm the request deadline instead of failing, so the late reply can still be matched to its call.
Thanks for the careful measurements. Your diagnosis holds on current main, and 0.64.0 doesn't change it.
- The watch runs long by the sleep. The
bash_watchlimit is measured withperformance.now(). In Bun on macOS that clock comes from an uptime clock that stops while the Mac sleeps, so the watch counts awake time only. - A waiting
bashcall fails at wake. The request deadline is a timer. In Bun on macOS timers run onCLOCK_MONOTONIC, which keeps counting during sleep. So it isn't wall time as such, but the effect is the one you describe. After a long sleep the timer is due the moment the Mac wakes, while AFT's own wait, measured on a clock that paused, still has time left. The bridge then waits 10 seconds for a late reply. Even a reply that names the running task is still reported as a timeout, and with no reply in that window the call is reported as unknown. That is the wrong answer for a command AFT already started.
The fix we plan:
- Every wait and request deadline is measured on one clock that counts time asleep.
- A request that expires across a sleep gets one short extra window, in which a late reply completes the original call normally.
- A command AFT knows it started is never reported as unknown.
No reply text changes. We'll post here when it ships.
Fixed in v0.65.0, released today: Shell waits and request deadlines now count time the computer was asleep, and a command that started is never reported as unknown after a sleep. Thanks for the report. If you still see it after updating, reply here and we'll reopen.