Skip to content

test(call_tool): make the auto-detach reproduction deterministic (fixes red main on macOS) - #1611

Merged
yvgude merged 1 commit into
mainfrom
fix/flaky-auto-detach
Aug 29, 2026
Merged

test(call_tool): make the auto-detach reproduction deterministic (fixes red main on macOS)#1611
yvgude merged 1 commit into
mainfrom
fix/flaky-auto-detach

Conversation

@yvgude

@yvgude yvgude commented Aug 29, 2026

Copy link
Copy Markdown
Owner

Main went red on ea0e5284bf (run 33248005217, Test (macos-latest)CI Green) with:

panicked at src/server/call_tool/tests.rs:196:13:
the reproduction must exercise the auto-detached path

No production code is involved. The failing assertion is a test-harness race that predates the merged content — the same commit was 29/29 green on PR #1603.

What raced

auto_detached_result drove run_foreground_or_detach with a 10 ms soft cap and a sleep 0.1 child, then asserted the result was Detached. That holds only if the child is spawned, scheduled and still running when the cap check fires. On a loaded macOS runner it isn't always: the child completes first, ForegroundResult::Finished comes back, and the let … else panics — with a message that says the premise broke and nothing about why.

The fix

Remove the timing assumption rather than widen it. The child now blocks on a barrier file until the test writes it, which only happens after the detach has been observed:

let guarded_command =
    format!("while [ ! -e \"$LCTX_TEST_RELEASE_PATH\" ]; do sleep 0.01; done; {command}");

It cannot finish inside the soft cap by construction, so the detach branch is guaranteed regardless of scheduling. auto_detached_pipeline_result already worked exactly this way; this brings the plain helper in line with it instead of leaving two different reliability levels in one file.

The four identical let … else sites become a match whose Finished arm prints the exit code and the captured output. If detachment ever fails for a real reason — a failed spawn, a missing sleep on some runner image — the failure will say what the child actually did.

Verification

  • cargo test --lib shell_outcome_tests → 31 passed, 0 failed
  • cargo test --lib → 10537 passed, 1 failed: tools::ctx_explore::tests::handle_output_is_byte_stable_across_runs, which passes in isolation and is unrelated to this file — a separate pre-existing flake, not something this PR introduces or claims to fix
  • cargo clippy --lib --all-features -- -D warnings → clean
  • cargo fmt --check → clean
  • scripts/loc-gate.sh → OK

🤖 Generated with Claude Code

`auto_detached_result` raced its own premise. It ran `sleep 0.1` against a
10 ms soft cap and assumed the child would still be running when the cap
expired, so `run_foreground_or_detach` would take the detach branch. On a
loaded macOS runner the helper can reach the cap check late enough that the
child has already exited, `ForegroundResult::Finished` comes back, and the
`let ... else` panics with "the reproduction must exercise the auto-detached
path" — a red build that says nothing about the code under test. This is what
turned main red on ea0e528 (run 33248005217, Test (macos-latest)), while
the same commit was 29/29 green on its PR.

The fix removes the timing assumption instead of widening it: the child now
blocks on a barrier file (`LCTX_TEST_RELEASE_PATH`) until the test itself
writes it, which is only after the detach has been observed. The child cannot
finish early by construction, so the soft cap is guaranteed to fire first.
`auto_detached_pipeline_result` already worked this way; this brings the plain
helper in line with it.

The four `let ... else` sites become a `match` whose `Finished` arm prints the
exit code and the captured output. If detachment ever fails again for a real
reason, the failure will say what the child actually did instead of only that
it did not detach.

No production code changes.

Verification: cargo test --lib shell_outcome_tests (31 passed), cargo test
--lib, cargo clippy --lib --all-features -D warnings clean, cargo fmt --check
clean, scripts/loc-gate.sh OK.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@yvgude
yvgude merged commit 9708b6c into main Aug 29, 2026
29 checks passed
@yvgude
yvgude deleted the fix/flaky-auto-detach branch August 29, 2026 12:08
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant