Skip to content

fix(tests): give the picker's directive files a temp directory of their own, and make a PTY failure explain itself - #3619

Merged
max-sixty merged 14 commits into
mainfrom
fix-picker-execute-windows-flake
Jul 26, 2026
Merged

max-sixty merged 14 commits into
mainfrom
fix-picker-execute-windows-flake

Conversation

@max-sixty

@max-sixty max-sixty commented Jul 26, 2026 •

Copy link
Copy Markdown
Owner

Chasing the Windows-only failure of test_switch_picker_runs_execute_command that @max-sixty flagged after #3607 merged: it failed the advisory affected tests (windows) leg while the required test (windows) leg passed the same test on the same SHA in the same run.

The failure message was the whole problem

assertion `left == right` failed: Expected exit code 0 for picker switch with --execute
  left: 1
 right: 0

That is everything CI had. wt prints an error before exiting non-zero, so there was a message — it just never reached the captured frame, because of two harness gaps:

Both are fixed here: drain until the output goes quiet, and panic on a hang rather than killing silently, so every exit code a caller asserts on is one the child chose. The four directive tests now also dump the screen (and the EXEC file) in their exit-code assertions.

What reproducing it turned up

Loop-based reproduction of the reported symptom is hopeless by the numbers: across the last 38 PR runs, exactly one advisory job failed — this one — i.e. ~1 failure per 160,000 test executions. What did reproduce was a different Windows failure in the same five tests, at a rate 1000× higher, once the loop ran with realistic neighbours (the full suite running alongside, as the failing leg had at test 3111/4204):

thread '…switch_picker::test_switch_picker_emits_cd_directive_by_default' panicked at
tests\integration_tests\switch_picker.rs:2531:45:
failed to create cd temp file: … PathError { path: "D:\\tmp\\.tmp3XI9wb",
  err: Os { code: 5, kind: PermissionDenied, message: "Access is denied." } }

4 of 80 runs of the five directive tests. directive_files() called NamedTempFile::new() in the shared temp directory; tempfile retries a name collision only when it surfaces as AlreadyExists, and on Windows create_new against a name held by a directory — or by a file in delete-pending state — returns PermissionDenied, which it hands back to the caller. A full suite run fills that directory with .tmpXXXXXX entries (every TestRepo makes one), so the collision is routine rather than astronomical.

The helpers now take a TempDir and put fixed-named files inside it. TempDir::new isn't exposed to the same problem — a directory collision surfaces as AlreadyExists, which tempfile retries — and fixed names inside a private directory can't collide with anything. That covers all 75 call sites of directive_files() / legacy_directive_file(), not just the picker's five, and the switch_picker copy of the helper is gone.

The reported exit-1 is not proven fixed

Stated plainly: the panic above is a different symptom from the reported exit 1, so this PR does not prove that failure is gone. What the evidence does say, and what a reader should check first if it recurs:

  • The failing run took 3.34s where the same test passing on the required leg took 3.11s and the same switch without --execute takes 2.42s. The --execute tail — build_hook_context's git lookups, ~0.7s of the difference — had therefore run, so the failure was at the end of the pipeline, not in the picker or the switch.
  • After that tail, the only fallible step left is the EXEC directive append. Every other step is infallible or .ok()-swallowed.
  • That write's error was context-free, which is why nobody could place it. wt rendered ✗ No such file or directory (os error 2) — no file, no operation, and positioned right under ◎ Executing (--execute):, where it reads as if the command were missing. It now reads:
✗ Failed to write the command to the directive file /tmp/tmp.aBc
  No such file or directory (os error 2)

The message lives in append_line / write_cd_path rather than at their four call sites, so every directive write says which file and what didn't get written — including the legacy-protocol arms, which no test can drive to failure.

So the next occurrence prints its own diagnosis, in one look, instead of left: 1, right: 0.

Also here

The four directive tests select their row by cursor, not by a typed query. They typed target and waited for target-branch's preview pane before Enter. That gate can only assert what skim painted: the filtered item list is swapped into ItemList during a render, while Accept reads item_list.selected() — the cursor's slot in whatever list is current at that moment. The list-pane > pointer comes from the same render state the accept reads, which is why this file's own wait_for_cursor_on_row calls it the race-free signal. One Down reaches target-branch (the picker sorts the current worktree first) and the arrow is re-issued until the pointer settles, so no query — and no matcher — is in the accept path at all. It also drops the wait on the preview pane rendering.

Testing

  • Reproducer and confirmation both on Windows CI, via a temporary workflow (deleted in the last commit): 80 runs of the five directive tests with the full suite running alongside. 4 failures before the fix, 0 after. The concurrent full suite passed 4208/4208 in both.
  • Local: switch_picker (68), plus every suite that calls these helpers — directives, switch, remove, security, bare_repository, step_alias, user_hooks, step_relocate (861 tests) — green, plus clippy -Dwarnings on all targets with --features shell-integration-tests.
  • The two directive-write messages are pinned by tests of their own (test_exec_directive_write_failure_names_the_file, test_cd_directive_write_failure_names_the_file): an unwritable directive path fails the same way on every platform, so the paths that produce these messages are covered rather than checked by hand.

Follow-ups, not in here

  • build_hook_context is called with referenced: None on the --execute path, so a -x 'echo hi' with no template variables still runs the default_branch, primary_worktree, commit, short_commit, remote, and upstream lookups — roughly doubling the command's subprocess count (30ms → 60ms locally, 2.42s → 3.11s in the Windows test timings). The alias path already passes its referenced set; a --execute command receives no JSON context, so the reason hooks pass None doesn't apply to it.
  • cancel_background_commands() is a no-op on Windows (signal_background_pid), so the picker's "the picker's exit cancels the unfinished request" promise isn't kept there: preview git children keep reading .git/config while the accept path writes worktrunk.history and worktrunk.default-branch to it. Issue Flaky: test_prune_skips_unmerged on Windows — Permission denied reading .git/config from background subprocess #2564 is the same shape of concurrent-config-access failure on Windows.
  • assert_valid_create_exit_code's Windows tolerance (test(switch_picker): tolerate Windows self-exit-1 on successful picker-create #3427) and assert_valid_abort_exit_code's acceptance of 1 both rest on the kill-vs-self-exit ambiguity this PR removes; they can be tightened once a Windows run confirms nothing else produces a stray 1.

This was written by Claude Code on behalf of Maximilian Roos

max-sixty added 14 commits July 26, 2026 13:44
The Windows flake in `test_switch_picker_runs_execute_command` reported only
`left: 1, right: 0` — wt's own error line, which explains why it exited 1, was
never in the captured frame. Two harness gaps hid it:

- The post-exit drain was a single non-blocking sweep taken the moment
  `try_wait` reported the child reaped, so the child's final writes could still
  be in the PTY. Drain until the output goes quiet instead.
- `child.kill()` ran unconditionally after the exit poll. On Windows that is
  `TerminateProcess(proc, 1)`, so a hung picker and a picker that failed on its
  own both report 1. A hang now panics as a hang, and every exit code a caller
  asserts on is one the child chose.

Also dumps the screen in the four directive tests' exit-code assertions, the
one place they asserted an exit code without showing what was on screen.

Includes a temporary Windows stress workflow to reproduce the flake; it goes
before merge.
Whether wt got as far as writing the EXEC directive splits the failure space
in two: an empty file means it failed before `execute_user_command`, a
populated one means the write landed and something after it failed.

Also folds `PtyResult::screen`'s body into a free `screen_text` so the
hang-path panic can render a frame it doesn't own, and points the stress
workflow at realistic test neighbours (a full-suite run alongside the loop)
rather than CPU starvation — the observed failure ran at normal speed.
`change_directory` and `execute` propagated the raw `io::Error` from their
directive-file write, so a failure rendered as

    ✗ No such file or directory (os error 2)

with nothing to attach it to. On the `--execute` path it lands *after* the
switch has already happened and after `◎ Executing (--execute):`, so it reads
as if the command were missing — the last thing on screen it could refer to.
Both writes now say which file and which write:

    ✗ Failed to write the command to the exec directive file /tmp/tmp.aBc
      No such file or directory (os error 2)

The files belong to the shell wrapper, so a reader who sees this knows to look
at the wrapper's temp files rather than at their own arguments. `.context()` on
wrapped I/O is what CLAUDE.md's Error Handling section asks for; these two were
the exceptions.
The four directive tests typed `target` and waited for `target-branch`'s
preview pane before pressing Enter. That gate can only assert what skim
*painted*: the filtered item list is swapped into `ItemList` during a render,
while `Accept` reads `item_list.selected()` — the cursor's slot in whatever
list is current at that moment. The list-pane `>` pointer comes from the same
render state the accept reads, which is why this file's `wait_for_cursor_on_row`
already calls it the race-free signal.

So navigate instead of filtering: the picker sorts the current worktree first,
so one Down lands on `target-branch`, and the arrow is re-issued until the
pointer settles there (absorbing an async row refresh that resets the cursor).
No query means no matcher in the accept path at all. It's also less to wait on
— the tests no longer block on the preview pane rendering.
`directive_files()` and `legacy_directive_file()` called
`NamedTempFile::new()` in the shared temp directory. `tempfile` retries a name
collision only when it surfaces as `AlreadyExists`, and on Windows a
`create_new` against a name already held by a *directory* — or by a file in
delete-pending state — comes back `PermissionDenied`, which it hands straight to
the caller. A full suite run leaves the temp directory full of `.tmpXXXXXX`
entries (every `TestRepo` makes one), and under that load the call panics:

    thread '…switch_picker::test_switch_picker_emits_cd_directive_by_default'
    panicked at tests\integration_tests\switch_picker.rs:2531:45:
    failed to create cd temp file: … PathError { path: "D:\\tmp\\.tmp3XI9wb",
      err: Os { code: 5, kind: PermissionDenied, message: "Access is denied." } }

Reproduced on Windows CI in 4 of 80 runs of the five directive tests while the
full suite ran alongside — a 1% chance per test that any of the ~65 callers of
these helpers fails for reasons that have nothing to do with what it asserts.

Both helpers now take a `TempDir` and put fixed-named files inside it.
`TempDir::new` isn't exposed to the same problem (a directory collision
surfaces as `AlreadyExists`, which tempfile retries), and fixed names inside a
private directory can't collide with anything. The switch_picker copy of the
helper is gone — it existed only to duplicate this.
The file had three copies of "poll for exit, kill on timeout, drain once".
`wait_for_exit` replaces them, so the invariant holds everywhere: a hang panics
with its frame instead of being killed into an exit code, and the final output is
drained to quiet before any caller reads it.
The rule that fell out of the picker flake: a file a test needs by name goes in
a `TempDir`, not in the shared temp directory under a random name, because
`NamedTempFile::new()` surfaces the collision as an unretried
`PermissionDenied` on Windows.
Neighbours, not starvation: loop the suspect tests on a Windows runner with a
full suite running alongside. Starving the CPU models the wrong thing whenever
the failing run's own timing was normal, and the duration is in the log.
It did its job: 4 failures in 80 runs of the five directive tests before the
temp-directory fix, 0 after, both with a full suite running alongside. The
recipe is recorded in tests/CLAUDE.md; the workflow itself doesn't need to live
in the repo.
Two beliefs, one test each: an unwritable EXEC file fails the command and names
the write, and so does an unwritable CD file. Both point the reader at the shell
wrapper's temp file, which is the whole reason the context was added — and these
are the paths a real failure takes, so they were worth a test rather than a
manual check.
… file

The context was at the four call sites, so two of the four closures (the legacy
arms) had no test that could reach them, and each site repeated the wording.
`append_line` and `write_cd_path` now own the message and return
`anyhow::Result`, with `append_line` taking the payload's name — one closure per
helper, both on paths the new tests exercise, and the legacy arms get the same
message rather than the bare `io::Error`.
@max-sixty
max-sixty marked this pull request as ready for review July 26, 2026 22:34
@max-sixty
max-sixty merged commit 951dac7 into main Jul 26, 2026
40 checks passed
@max-sixty
max-sixty deleted the fix-picker-execute-windows-flake branch July 26, 2026 22:41
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.

2 participants