Skip to content

test(switch): deflake comments-tab preview wait by re-issuing the tab key - #3238

Merged
max-sixty merged 1 commit into
mainfrom
deflake-comments-tab-diff-wait
Jun 25, 2026
Merged

max-sixty merged 1 commit into
mainfrom
deflake-comments-tab-diff-wait

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Deflake test_switch_picker_worktree_row_comments_tab_shows_thread

This PTY/TUI test intermittently failed on test (windows) with Timed out after 30s waiting for expected content "diff --git", while Linux and macOS stayed green and a plain re-run passed. It presented as a slow-runner timing flake, but the timeout is structural — bumping it would never help.

Root cause

The picker's preview pane is served from a DashMap cache populated by background COLLECT_POOL workers (preview_orchestrator.rs just does cache.insert(...), with no poke back to skim). skim 4.8 re-reads that cache only inside run_preview, which fires only on Event::RunPreview — a cursor move or a tab switch. On a cache miss it synchronously paints a "Loading working-tree diff. Press alt-1 again to refresh." placeholder. The other events don't help: Event::Render (sent by every background on_update — CI status, row pipeline, the comments fetch) only redraws the current state, and the heartbeat re-runs the preview only inside skim's 50ms debounce window, which two keystrokes 500ms apart never hit.

So a single Alt-1 keystroke produces a single RunPreview that reads the cache once. If feature's WorkingTree precompute (a real git diff HEAD) hasn't landed by that instant, the placeholder is painted and never refreshed — the finished diff lands in the cache with no event to surface it. That race is what loses on a contended Windows runner: this test seeds an open PR and mocks gh pr view, so the WorkingTree precompute competes with an extra gh subprocess (plus 16-way CI parallelism). A re-run shifts the timing and the precompute wins.

Fix

Re-issue the (idempotent) Alt-<digit> tab keystroke every second while the expected preview content is still absent — exactly what the placeholder's own "Press alt-N again to refresh" instruction tells a user to do. Each re-issue forces a fresh RunPreview against the now-warm cache, so the diff surfaces as soon as the precompute lands. The change lives entirely in the shared PTY drivers (tests/integration_tests/switch_picker.rs); the wait keeps its existing 30s ceiling and its loud panic diagnostics.

Only Alt-<digit> tab switches are re-issued. They jump to a fixed tab, so repeating one is a no-op. Tab / Shift-Tab cycle, typed filter text accumulates, and Enter commits — none may be repeated — and list-content waits (rows streaming in, filter matches) repaint on every Event::Render and never strand, so all of those keep the plain wait.

Verification

With a temporary env-gated 3s delay injected into the WorkingTree precompute (forcing the cold cache deterministically):

  • reverting the re-issue reproduces the exact Timed out after 30s waiting for expected content "diff --git" panic;
  • with the re-issue active the test passes in ~5.7s, once the delayed precompute lands.

All temporary instrumentation was removed. The full switch_picker suite (34 tests, incl. the Alt-digit, Tab/Shift-Tab cycle, and snapshot-asserting siblings) passes; the snapshot tests are unaffected because re-issue stops the instant content appears, so the captured screen is identical. cargo fmt --check and cargo clippy --tests --features shell-integration-tests are clean.

This was written by Claude Code on behalf of max

… key

The picker preview cache is re-read only on skim's Event::RunPreview, so a
single Alt-digit keystroke reads it exactly once. A WorkingTree precompute
that lands after that read strands the "Loading…" placeholder with no event
to refresh it — a Windows-CI timeout under load, worsened by this test's
extra gh subprocess competing on the preview pool. Re-issue the idempotent
Alt-<digit> tab keystroke while the expected preview content is absent, the
remedy the placeholder itself names ("Press alt-N again to refresh").

Non-idempotent inputs (Tab cycling, filter text, Enter) and list-content
waits are left on the plain wait.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@max-sixty
max-sixty enabled auto-merge (squash) June 25, 2026 09:02
@max-sixty
max-sixty merged commit 523814e into main Jun 25, 2026
37 checks passed
@max-sixty
max-sixty deleted the deflake-comments-tab-diff-wait branch June 25, 2026 09:10
worktrunk-bot added a commit that referenced this pull request Jun 25, 2026
…n feature

`test_switch_picker_worktree_row_comments_tab_shows_thread` drove the
picker with a single blind `Down` (`("\x1b[B", None)`) to move the cursor
onto `feature`, then Alt-1 and waited for `feature`'s `diff --git`. On
Windows CI it timed out after 30s — the captured screen showed the `>`
pointer stranded on `main` ("○ main has no uncommitted changes" in the
HEAD± preview), so the awaited `diff --git` never appeared.

Root cause: `feature`'s row decorates asynchronously (primed CI status
resolves to "has PR" in the background). When that resolution lands it
refreshes skim's item list, which resets the cursor to the top — undoing
the `Down` and leaving the pointer on the primary worktree. The existing
preview-reissue nudge then re-ran Alt-1 against `main` forever.

Make `Down` cursor-confirmed: when a Up/Down arrow step carries expected
content, treat it as the target row name and re-issue the (idempotent)
arrow via `wait_for_stable_until`'s nudge until the `>` pointer lands on
that row and the screen settles — by which point the list has stopped
refreshing. The comments-tab step becomes `("\x1b[B", Some("feature"))`.

This mirrors the alt-digit preview re-issue added in #3238 and reuses
`wait_for_cursor_on_row`'s pointer predicate (factored out as
`cursor_points_at`). Arrow steps that pass `None` (every other test) are
unaffected — they still fall through to a plain stability wait.

Refs #3241

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
max-sixty added a commit that referenced this pull request Jun 25, 2026
…nds (#3247)

## Problem

The `wt switch` picker's preview pane is served from a `DashMap` cache
filled by background workers on `COLLECT_POOL` (a `git diff HEAD`, a
`git log`, a forge `gh pr view`). skim 4.8 re-reads that cache **only**
inside `run_preview`, which fires only on `Event::RunPreview` — produced
by a selection change or a preview-tab keystroke. The cache-insert path
didn't poke skim, so a compute that finished *after* the single
`RunPreview` the keystroke produced sat in the cache with no event to
surface it: the pane stayed on its `Loading…` placeholder until the user
pressed a key again. That `Press alt-N again to refresh` text was the
manual workaround for exactly this gap, and it was a Windows-CI flake
(PR #3238 papered over it test-side by re-issuing the tab keystroke).

## The skim mechanism this uses

skim 4.8 hands the embedder its event sender at TUI init:
`Skim::event_sender()` returns the `tokio::sync::mpsc::Sender<Event>`
that drives the loop. The picker already captures it (as `render_tx`, a
shared `Arc<OnceLock<…>>`) and pushes `Event::Render` through it for
in-place row repaints. **Pushing `Event::RunPreview` through the same
channel forces `run_preview` to re-read the cache for the
currently-selected row + current mode** — the external injection point
the orchestrator needed. The channel is `1024*1024`-capacity, so the
`try_send` poke is never dropped.

## Approach

New `PreviewNotifier` (`src/commands/picker/preview_notify.rs`) closes
the producer → consumer loop:

- **Consumer side:** every `*SkimItem::preview()` records the selected
row's awaited `(row-key, mode)` via `note_awaiting` — *before* it reads
the cache. That ordering makes the hand-off race-free: if the read
misses, the fill that satisfies it necessarily lands after the read, so
it observes the awaited key already set.
- **Producer side:** the orchestrator routes **every** cache fill
through a single `PreviewOrchestrator::fill` / `fill_external` path,
which calls `notify_filled(key)`. That injects `Event::RunPreview`
**iff** the filled key matches what the selected row is awaiting. A fill
for an off-screen row or a tab the user isn't on matches nothing and
injects nothing — so background pre-compute never thrashes the visible
preview.

`preview()` is only ever called for the selected row, so the single
shared `awaiting` slot always reflects what's on screen; when the
selection changes, the next `RunPreview` updates it.

Two producers feed the panes, both now covered:
- **Orchestrator cache fills** (diff / log / summary / the `--prs`
comments & log fetch) → `notify_filled(key)`, exact-key match.
- **The collect handler's `on_update`** mirrors a row's live `pr_status`
(the `pr` / `comments` panes) and `local_content` (the diff tabs' dim
state) — not cache fills → `notify_row_changed(row_key)`, which re-runs
the selected row's preview on *any* tab when that row's data lands. This
is what flips the `pr` tab from "Fetching PR status…" to the resolved PR
on its own.

Wiring: `render_tx` is constructed before the orchestrator in
`handle_picker` and handed in; it's still published once, inside
`run_skim` after `init_tui`. `generate_and_cache_summary` became
`generate_summary_for_item` (returns the pane; the orchestrator inserts
via `fill`).

## User-visible change

The placeholders drop the now-obsolete "press alt-N … to refresh"
wording — the pane fills in on its own:

```
○ Loading working-tree diff…       (was: ○ Loading working-tree diff. Press alt-1 again to refresh.)
○ Generating summary…
○ Fetching PR status for feature…  (was: … press alt-6 to refresh)
ⓘ Loading comments…                (--prs deferred tabs; was: … press alt-2 again to refresh)
```

## Testing

- `test_switch_picker_preview_auto_refreshes_when_compute_lands` (PTY):
mocks `gh pr view --json comments` behind a 3 s delay, opens a `--prs`
row's comments tab mid-fetch — the comment surfaces **with no further
input** (the orchestrator-fill path).
- `test_switch_picker_pr_tab_auto_resolves_from_fetching` (PTY): the
per-row CI fetch (`gh pr list --head`) is delayed 3 s and unseeded, so
the `pr` tab opens on "Fetching PR status…" and resolves to the live PR
on its own (the `on_update` path).
- Both verified to **time out (stay stranded) when the poke is
disabled**, so they genuinely exercise the mechanism rather than passing
on a cache hit.
- `fill_notifies_only_awaited_key` /
`notify_row_changed_pokes_only_the_selected_row` (unit): the poke fires
for the visible row+mode (resp. row, any mode) and nothing for
off-screen / other-tab keys — the no-thrash guarantee.
- The PTY driver's keystroke re-issue (`nudge` / `is_alt_digit_tab` /
`PREVIEW_REISSUE_INTERVAL`, PR #3238) is removed — the product now
auto-refreshes, so the tests genuinely verify it rather than papering
over a strand. All 37 `switch_picker` PTY tests pass; full `cargo run --
hook pre-merge --yes` green.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
worktrunk-bot added a commit that referenced this pull request Jun 26, 2026
Resolves the conflict in tests/integration_tests/switch_picker.rs. main
(#3247) added PreviewNotifier, which auto-repaints a late preview when its
background compute lands, and removed the test-side Alt-<digit> keystroke
re-issue that #3238 used to paper over stranded "Loading…" placeholders.

The two de-flakes in this PR are orthogonal to that:

- Flake #1 (alt-x sticky exit code) — a separate region, auto-merged cleanly.
- Flake #2 (comments-tab cursor reset) — kept. PreviewNotifier re-runs only
  the *preview* for the selected row (notify_row_changed); it does not stop
  on_update's request_render from resetting skim's cursor to the top after an
  async row decoration lands. So the cursor-confirmed Down is still needed.

Resolution keeps the cursor-arrow re-issue (restored wait_for_stable_until's
nudge param, now scoped to cursor recovery and renamed PREVIEW_REISSUE_INTERVAL
-> CURSOR_REISSUE_INTERVAL) and drops the obsolete Alt-<digit> re-issue branch
(is_alt_digit_tab deleted) in favor of main's PreviewNotifier path. Full
switch_picker suite passes locally on Linux.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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