Skip to content

fix(harness): Reap a dropped Session off-thread - #664

Merged
omesser merged 2 commits into
mainfrom
fix/634-macos-harness-popup-off
Sep 14, 2026
Merged

omesser merged 2 commits into
mainfrom
fix/634-macos-harness-popup-off

Conversation

@omesser

@omesser omesser commented Sep 13, 2026 •

Copy link
Copy Markdown
Owner

Why

On macOS Settings, switching the AI source popup back to Off did not take. ax-settings pick <pid> Harness Off exited 0, the dump still read claude, and the next pick Harness claude died with the Harness popup has no option claude.

The popup-rebuild theory in the issue is wrong. form::harness_options() is static and the items are never reordered — that guard was built, disproved against the live window, and removed.

The cause is a main-thread stall. harness::retarget reaps the dropped Session on the calling thread, and its only caller is SettingsSession::apply, which on macOS runs inside the popup's own action — on the UI thread, inside NSMenu tracking. Session::shutdown waits REAP (2s) for the child to go, so picking a new source froze the window for two seconds. During that freeze the AX dump read the popup from before the click and the menu was still on screen; the next pick pressed a popup whose menu was already down against a stalled app, its AXPress timed out, the fallback dismissed the menu, and the option search found nothing. Both symptoms, one cause.

What changed

The first attempt moved the reap onto a detached thread. Review found that traded the freeze for something worse, because wire.shutdown() does not kill — it posts Msg::Shutdown, and turn() handled that exactly like Cancel: cancel, end the turn, and loop. The message was swallowed, and the kill waited for the second Shutdown that Drop for Wire posts. So a detached reap could lose its race with process exit and orphan an npx tree that outlives the app.

The fix is a rung lower, and it is the whole change:

  • src-tauri/src/acp_wire.rs — turn() now returns Err(TurnError::Lost) on Msg::Shutdown. serve breaks on Lost, and that break is the only road to kill_harness_tree in run. The kill genuinely leaves at once. The in-flight reply is lost by design: every sender of Shutdown is killing the child a line later.
  • src-tauri/src/harness.rs — retarget reaps on the calling thread, as it always did. No detached thread. That is affordable now precisely because wait_for_exit returns as fast as the kill does. Fixing the swallow removed the reason for the workaround instead of moving the cost somewhere it could escape.
  • scripts/ax-settings.swift — pick waits for the popup to read the picked title with its menu gone, so pick means "the row took it" rather than "the press returned".
  • scripts/verify-settings-macos.sh — the two source-switch pick failures are now fail, not skip. skip() does not increment failures, so the script used to print "all checks passed" and exit 0 on exactly the bug it exists to catch. The skips further down are genuine environment skips and stay.

This is shared code, so all three renderers get it. GTK and Win32 are cfg'd out on this Mac and were not built here.

How to verify

cargo test -p ai-buddy
cargo clippy --workspace --all-targets -- -D warnings

411 tests pass, clippy clean.

retarget_does_not_wait_out_a_turn_in_flight is the regression lock. It puts a prompt on the wire with the slow fake, installs the session, and times retarget(Some(""), false). It asserts three things, not just the timing: that the call returns under 500ms, that the Off pick took, and that the child was really killed — the turn's caller must come back Err, and the session must not read alive.

Mutation, run both ways just now: revert turn() so Msg::Shutdown falls back into the Cancel arm and the test fails with

retarget waited 2.00723675s on the dropped session

against 0.04s with the fix. Four orders of magnitude, so the assert is not wall-clock flaky.

Live, on macOS 26.6.2:

cargo build -p ai-buddy
AI_BUDDY_VERIFY_HARNESS=grok ./scripts/verify-settings-macos.sh

AI_BUDDY_VERIFY_HARNESS=claude does not reproduce — claude attaches and the reap is masked. grok (spawns but never answers ACP, so its wire is live) reproduces exactly, including the no option death. After: off-again.txt reads AXPopUpButton||Model API with the full eight-option list intact.

Notes

  • The filed reproduction may not be this bug. macOS Settings Off pick does not leave the Harness popup #634 used claude "set but not running". If that Harness never spawned, Session::wire is None and Session::shutdown returns at its first guard — no wait, no freeze, so the stall cannot explain it. The live reproduction used grok, which did spawn and simply never answered ACP, leaving a live wire and the full 2s. These are plausibly two different preconditions, and the ax-settings.swift pick guard may be what actually fixes the symptom as filed. Flagging rather than closing over it — an issue that closes on the wrong explanation comes back.
  • Unverified without a live window: whether an open NSPopUpButton menu is a descendant of the AXPopUpButton in the AX tree. If it is not, the find(target, where: role == "AXMenu") == nil half of the new pick guard is vacuously true and only the AXValue comparison carries weight.
  • The skip→fail change means verify-settings-macos.sh can now fail where it previously exited 0. That is the point, but it may surface other latent failures on first run.

Closes #634

— Claude Code agent, on @omesser's behalf.

🤖 Generated with Claude Code

omesser and others added 2 commits September 13, 2026 20:49
Picking Model API froze the window for the two seconds `shutdown` waits
on the child, so the menu was still down when the next pick arrived.
`ax-settings pick` now waits for the row to take it. #634

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
`turn` handled Shutdown exactly like Cancel - cancel, end the turn, loop -
so the message was swallowed and the break came only on the second one
`Drop for Wire` posts. `kill_harness_tree` sits after `serve` returns, so
`Session::shutdown` paid the full `REAP` whenever a turn was in flight,
and `retarget` paid it on the UI thread. Shutdown now returns `Lost`,
which `serve` already breaks on, so the kill leaves at once and the
dropped Session is reaped inline again.

`verify-settings-macos.sh` counted a pick that did not take as a skip,
and `skip` does not touch `failures` - so #634 returning printed "all
checks passed" and exited 0.

#634

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@omesser
omesser force-pushed the fix/634-macos-harness-popup-off branch from b252e87 to e6e7401 Compare September 13, 2026 17:50
@omesser
omesser marked this pull request as ready for review September 13, 2026 17:50
@omesser
omesser merged commit 3331af0 into main Sep 14, 2026
5 checks passed
@omesser
omesser deleted the fix/634-macos-harness-popup-off branch September 14, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS Settings Off pick does not leave the Harness popup

1 participant