Skip to content

Stabilize AgentRunWorktreeStartTests production-capture selection race (#420)#430

Open
moonray wants to merge 1 commit into
repoprompt:mainfrom
moonray:fix/ci-flake-420
Open

Stabilize AgentRunWorktreeStartTests production-capture selection race (#420)#430
moonray wants to merge 1 commit into
repoprompt:mainfrom
moonray:fix/ci-flake-420

Conversation

@moonray

@moonray moonray commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the intermittent CI flake in AgentRunWorktreeStartTests/testProductionCapturePreservesBoundStoredSelectionInsteadOfCanonicalUI (app shard 4). Closes #420.

Root cause

captureAgentRunOracleReviewSource re-validates the canonical tab selection after its async work (requiredLookupContext + freezePromptGitReviewContext) via the agentRunOracleReviewLaunchSelectionStillMatches guard. This test deliberately keeps the active UI selection diverged (empty) from the canonical tab selection (storedSelection) to prove capture serves the stored selection rather than the UI.

The divergence is created with mirrorToUIIfActive: false, so no DeferredUISelectionFence protects canonical. A debounced TokenCountingViewModel recount — armed by the test's applyStoredSelection(StoredSelection()) — fires during the capture's await window and reads activeSelectionSnapshot(flushPendingUI: true), flushing the divergent empty UI into canonical. That bumps selectionRevisionForMCP and flips the canonical identity, falsely tripping the guard:

The launching selection changed while its frozen review capability was being created.

In normal production use the selection mirror suppresses these flushes during sensitive operations; the test bypassed the mirror and ran the capture outside that protection. This matches the precedent in 6f32dcf7 (Stabilize full-suite MCP worktree tests), which introduced the identity-based comparison for exactly this class of mirror/re-commit churn.

Fix

Test-only (+14/−4). Wrap the capture call in window.selectionCoordinator.withApplyingSelectionMirror { … } to hold the production flush suppression across the capture window, keeping canonical stable while the frozen review capability is built. No production code changed; assertions unchanged.

Validation

  • Repro on base (27064af8, an ancestor of current main): focused test failed 6/25 (24%) with the exact issue signature.
  • After fix: focused test 50/50 pass, 0 fail.
  • Full AgentRunWorktreeStartTests class: 38/38 pass, 0 fail.
  • SwiftFormat: 0 files need formatting. SwiftLint --strict: 0 violations.
  • Contribution preflight: commit and push modes both passed.
  • The 36 commits between this branch's base and current main do not touch the test, the capture guard, the selection coordinator, or token counting (only unrelated Sentry telemetry + formatting in MCPServerViewModel), so the fix applies unchanged.

Notes

  • Deliberately test-side: the production guard is correct for real usage; the race is an artifact of the test's artificial unsupervised UI↔canonical divergence. Happy to revisit as a production-side stabilization if maintainers prefer.

🤖 Generated with Claude Code

repoprompt#420)

The production source-capture drift guard re-validates the canonical selection
after its async work. testProductionCapturePreservesBoundStoredSelectionInsteadOfCanonicalUI
deliberately keeps the active UI selection diverged (empty) from canonical
(storedSelection) to prove capture serves the stored selection. A debounced
token-count recount flushed that divergent UI selection into canonical during
the capture's await window (requiredLookupContext / freezePromptGitReviewContext),
falsely tripping the guard ("launching selection changed while its frozen review
capability was being created"). In normal use the selection mirror suppresses
these flushes during sensitive operations; the test bypassed it via
mirrorToUIIfActive:false and ran the capture outside that protection.

Wrap the capture call in withApplyingSelectionMirror to hold the production
flush suppression across the capture window, keeping canonical stable while the
frozen review capability is built. Test-only; assertions unchanged.

Repro on base (27064af): focused test failed 6/25 (24%) with the exact issue
signature. After fix: focused 50/50 pass; full AgentRunWorktreeStartTests 38/38.

Closes repoprompt#420
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.

CI flake: AgentRunWorktreeStartTests/testProductionCapturePreservesBoundStoredSelectionInsteadOfCanonicalUI fails intermittently on shard 4

1 participant