aft icon indicating copy to clipboard operation
aft copied to clipboard

Foreground bash pays a fixed ~100 ms idle wait per call (completion detected on a ~100 ms tick, not on SIGCHLD/exit pipe)

Open gaojunran opened this issue 2 weeks ago • 2 comments

Summary

Every foreground bash tool call pays a fixed ~100 ms of idle wait on top of the command, independent of the command's runtime, output size, exit status, or working directory. For a small true/date command the tool call takes ~110–125 ms end to end while the command itself takes ~1 ms and the daemon burns ~6 ms of CPU.

On a session that runs 50–100 shell commands this is 5–12 s of pure wall clock, and it is the dominant per-command cost of the AFT bash tool (the whole plugin-side overhead is ~3 ms).

The wait happens inside the daemon's bash RPC, after the child has already exited, and its length tracks a ~100 ms quantization (it shrinks as the command gets longer), which points at timer/poll-driven completion detection rather than a signal/EOF-driven one.

Environment

  • AFT 0.65.0 (@cortexkit/aft-opencode), Linux x64, ext4
  • OpenCode 2.2.7 (also reproduced against an isolated harness: OPENCODE_CONFIG_DIR + a local OpenAI-compatible stub with ~1 ms TTFT)
  • Reproduced both in a real session and in a throwaway harness, so this is not session/state specific

Measurements

1. End-to-end, from the recorded tool part

session_message.data.content[].time gives created → ran → completed. The command itself stamps Date.now() into a file so we can split pre from post:

phase measurement
ran → command start 11 ms
command (date) ~1 ms
command exit → completed ~103 ms
total ran → completed 113 / 114 / 116 ms (three consecutive calls)

Ten consecutive true calls: 124, 112, 111, 109, 111, 113, 109, 111, 110, 109 ms — very tight, i.e. a fixed delay, not a load-dependent one.

2. The daemon is idle, not working

Sampling /proc/<daemon>/stat (utime+stime) around 8 calls:

8 calls: wall 3769ms, daemon CPU 50ms (6.2ms/call)

So ~100 ms of the ~110 ms is waiting, not computing. No fsync storm, no compression work, no serialization cost.

3. The wait shrinks as the command gets longer

command tool call overhead over the command
sleep 0.01 108 ms +98 ms
sleep 0.06 138 ms +78 ms
sleep 0.12 176 ms +56 ms
sleep 0.29 311 ms +21 ms

A fixed "settle window" would be constant; a fixed amount of work would be constant. The overhead decreasing with the command's own duration is the signature of completion being observed on a periodic tick (the remainder of the current tick).

4. strace: the child is gone, the daemon is still waiting

strace -f over the whole tree (one true call):

21:50:38.859887  clone(...) = 2107384              # wrapper spawns bash
21:50:38.865540  write(1, "0", 1) = 1              # wrapper reports exit status on the pipe
21:50:38.865850  +++ exited with 0 +++             # command AND wrapper are done
                 ... 100 ms with no syscall on the daemon thread that is waiting for it
21:50:38.965355  wait4(2107383, ...) = 2107383     # "discovers" the exit 99.5 ms later

The wrapper writes its exit status to the pipe immediately (write(1, "0", 1) at .865540) and exits at .865850; nothing in the daemon reacts to that for ~100 ms.

What it is not (all measured)

hypothesis test result
fsync / disk fsync p50 on the AFT store = 1.45 ms ruled out
output compression / size true vs seq 1 20000 (≈140 KB) 108 vs 113 ms — size-independent
waiting for grandchildren ("drain") sleep 2 & echo bg-detached 117 ms — does not wait
cgroup / linux_scope project config bash.linux_scope: false 112 ms — no change
changed_files (git diff) bash.changed_files: "off" 119 ms — no change
compress bash.compress: false no change
plugin / opencode overhead instrumented plugin (__plog around execute and callBridge) 2 ms before the bridge, 1 ms after
CPU-bound work in the daemon /proc sampling 6.2 ms/call

Plugin instrumention of one call (ms since tool entry):

bash-enter
bash-after-subagent   +1
bash-after-shellenv   +2
bash-before-bridge    +2
bridge-enter bash
bash-after-bridge     +119     <-- the entire cost is this RPC
bash-after-metadata   +120

Where the time goes (hypothesis)

The foreground bash RPC waits for the completion of the task on a ~100 ms poll/timer instead of waking on SIGCHLD / the exit-status pipe. Every command therefore pays up to one full tick, and short commands always pay the whole remainder.

If there is a deliberate settle window behind it (e.g. "let late output arrive before composing the frame"), it is currently ~100 ms — that is a lot for a foreground call that already has the child's exit status, and it is invisible to the caller.

Suggested fix

  1. Wake the foreground wait on the exit-status pipe / SIGCHLD (the wrapper already reports the status synchronously), and use the timer only as a guard for descendants that outlive the child (e.g. timeout = 10–20 ms, configurable), not as the primary detection path.
  2. If a settle window must stay, expose it (e.g. bash.completion_settle_ms) and default it small — today there is no user-side knob that changes this at all.
  3. Guard against regressions with a benchmark: bash("true") p50 should be ≲ 20 ms warm.

Reproduction

Simplest: run true ten times through the agent and compare with a direct loop (time for i in {1..10}; do bash -c true; done ≈ 20 ms total). Per-call overhead is visible directly in the tool part's time.ran/time.completed, or with a timestamp written from inside the command:

# in the agent shell
date +%s%3N

and compare with content[0].time.ran / .time.completed in the session DB.

Happy to provide the full harness (isolated OPENCODE_CONFIG_DIR + stub provider + instrumented plugin + strace) if it helps.

gaojunran avatar Nov 15 '26 14:11 gaojunran

Thanks for the careful measurements. The strace and the table where the overhead shrinks as the command gets longer pointed straight at the cause.

You were right: a foreground call noticed its command's exit on a 100 ms polling tick. Foreground calls now wait on the operating system's own exit notification for the command (pidfd on Linux, kqueue on macOS, the process handle on Windows). The 100 ms check stays only as a safety net.

Measured through the daemon's real bash call path on Linux (median of 20 alternating runs, unoptimized build):

command before after
true 117.7 ms 12.7 ms
sleep 0.01 119.3 ms 23.8 ms
sleep 0.06 119.8 ms 74.6 ms
sleep 0.29 323.7 ms 304.4 ms

The added time is now a flat 13–15 ms, instead of whatever is left of a 100 ms tick. The fix is in our next release batch. We'll post here when a release that includes it is published.

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

Fixed in v0.66.0: a foreground shell call returns as soon as the local command exits, instead of waiting for the next check. In our measurements the overhead per call dropped to about 12–15 ms on Linux and about 20 ms on macOS. Thanks for measuring it, @gaojunran.

aft-alfonso[bot] avatar Nov 17 '26 09:11 aft-alfonso[bot]