aft icon indicating copy to clipboard operation
aft copied to clipboard

bash and bash_watch waits misbehave when macOS sleeps: watch overruns by the sleep, and a waiting bash call fails at wake

Open null-axiom opened this issue 3 weeks ago • 2 comments

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.

  1. bash_watch runs 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.
  2. A waiting bash call fails the moment the Mac wakes. Five calls failed with Request "bash" (id=N) timed out after 280000ms (or 25000ms), each logged within about one second of a Wake or DarkWake in pmset -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 waitForBashStatus is startedAt + effectiveWaitMs, read from performance.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 setTimeout on wall time.

Reproduction

  1. On a Mac on battery, in an OpenCode subagent session: shell({ command: "sleep 1500", background: true }), then bash_watch({ taskId }) with no timeout, so the worker cap of 270 s applies.
  2. About three minutes in, let the Mac sleep (close the lid or pmset sleepnow) for a few minutes, then wake it.
  3. 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.

null-axiom avatar Nov 13 '26 04:11 null-axiom

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_watch limit is measured with performance.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 bash call fails at wake. The request deadline is a timer. In Bun on macOS timers run on CLOCK_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.

aft-alfonso[bot] avatar Nov 13 '26 06:11 aft-alfonso[bot]

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.

aft-alfonso[bot] avatar Nov 14 '26 10:11 aft-alfonso[bot]