Skip to content

Fix flaky watch-nodes e2e test under the pre-push gate's cold-build load #2601

Closed

Description

@malibio

Overview

packages/desktop-app/src/tests/e2e/watch-nodes.e2e.ts's "delivers nodeCreated event when a node is written" test fails intermittently when run as part of the pre-push gate (scripts/test-gate.ts, ADR-047), even though it passes reliably when run standalone.

Problem Statement

waitForEvent (same file) uses a single 5000ms timeout that covers both opening the SSE connection and waiting for the expected event. Under the pre-push gate — which runs test:all and a cargo build --bin nodespaced immediately before test:e2e — the machine is already under load from that prior work, so the daemon's connection setup alone eats into the 5s budget. Observed failures were at 5002-5003ms against the 5000ms limit: the timeout aborts moments after connecting, before the event has a chance to arrive.

Reproduced twice in a row under the full pre-push gate while working on #2563 / PR #2600. Both times, running bun run --cwd packages/desktop-app test:e2e standalone immediately afterward passed cleanly — confirming this is a load-sensitivity issue specific to running after test:all + a cold(ish) cargo build, not a broken SSE stream.

Prior attempt already on main

Commit 0f4f9b03 ("Give the SSE watch helper a timeout that survives a loaded machine") diagnosed this exact failure mode and raised the timeout from 5000ms to 25000ms (deliberately kept under vitest's 30s testTimeout so this helper's own abort error surfaces instead of a bare vitest timeout). That commit was reverted shortly after by f35c6e34 ("Revert "Give the SSE watch helper a timeout that survives a loaded machine""), whose message gives no reason beyond naming the commit it reverts — the rationale for reverting isn't recorded anywhere this issue's author could find. That's worth resolving before repeating the same fix: there may have been a real justification (e.g. it masked a separate hang, or 25s made some other workflow too slow) that changes what the right fix here is.

Proposed Solution

  1. Re-diagnose why 0f4f9b03's fix was reverted — check for related discussion, a follow-up incident, or a reason the 25s timeout itself caused a problem, before assuming the revert was simply a mistake.
  2. Land a durable fix. Two candidate directions, to be chosen based on what's learned in step 1:
    • Re-apply a longer timeout (as 0f4f9b03 did), if no real objection to it turns up.
    • Restructure waitForEvent to track the connect phase and the wait-for-event phase as two separate budgets (e.g. an onConnected callback already exists in the helper's options — the connect phase could reset or extend the deadline once the connection is confirmed live), so a slow connection under load doesn't eat into the 5s meant for the event itself. This addresses the root cause more precisely than uniformly raising one shared timeout.

Acceptance Criteria

  • Root cause of the f35c6e34 revert is understood and documented in the PR (either "no real objection found, safe to redo" or "found X, so the fix needs to account for it")
  • watch-nodes.e2e.ts's "delivers nodeCreated event when a node is written" test passes reliably when run as part of the full pre-push gate (scripts/test-gate.ts), not just standalone — verified by running the gate (or bun run test:e2e immediately after a test:all + cargo build --bin nodespaced run) multiple times in a row
  • No regression to the helper's existing behavior for a genuinely broken/hung SSE stream — it must still fail within a bounded, reasonable time, not silently hang
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • packages/desktop-app/src/tests/e2e/watch-nodes.e2e.ts: the waitForEvent helper (currently ~line 29-40) and the failing test itself
  • scripts/test-gate.ts: the pre-push gate script — runs test:all, then cargo build --bin nodespaced, then test:e2e, in that order
  • packages/desktop-app/src/tests/e2e/daemon-harness.ts: how the e2e harness spawns the real headless daemon this test connects to

Git history to review

  • 0f4f9b03 — the prior fix (5000ms → 25000ms, with a detailed root-cause writeup in the commit message)
  • f35c6e34 — the revert of 0f4f9b03, with no stated reason

Related Issues

Activity

  1. added
    bug
    on Sep 13, 2026
  2. malibio commented on Sep 16, 2026

    @malibio
    CollaboratorAuthor

    Investigation: already fixed by #2587 — closing

    Investigated before implementing. The flake is gone, and both proposed directions in this issue would be counterproductive. Details below so this doesn't get re-opened and the timeout bump re-applied a third time.

    1. The revert's rationale is recorded — in #2584

    This issue says the reason for f35c6e34 "isn't recorded anywhere this issue's author could find." It's documented in detail in #2584. The 25s timeout wasn't reverted arbitrarily — it was reverted because it disproved its own hypothesis:

    × WatchNodes SSE stream > delivers nodeCreated event when a node is written 25010ms
      → This operation was aborted
    

    The test consumed the entire 25s budget. The event was never delivered at all, so the connection was not merely slow. A larger timeout only made failures take 5× longer while masking a genuine server-side race.

    2. The actual bug was a race, fixed in #2587

    96aee026 ("Gate SSE : connected on the daemon watch bridge actually being attached", merged 2026-09-13 13:34) fixed the real cause: the dev-proxy's HTTP server came up before startWatchBridge()'s async gRPC connect finished, so a client could be told : connected with no upstream listener attached to relay the write it was racing. The fix gates : connected behind await bridgeAttached, reset on every reconnect.

    3. Why this issue still saw failures

    PR #2600's branch (issue-2563-playbook-agent-interface) was cut from f35c6e34 (09-12 23:38) and never contained 96aee026. The fix merged to main at 09-13 13:34 but was never pulled into that branch. The flakes reported here were observed on code that predated the fix — they would have failed on any machine.

    4. Verification on current main

    Ran the real gate sequence from scripts/test-gate.ts — test:all → cargo build --bin nodespaced → test:e2e, back-to-back with NODESPACED_BINARY set — then three more cycles with a forced daemon rebuild before each.

    Run delivers nodeCreated Budget
    After full test:all + cargo build 38ms 5000ms
    Cycle 1 (rebuild) 22ms 5000ms
    Cycle 2 (rebuild) 75ms 5000ms
    Cycle 3 (rebuild) 14ms 5000ms

    27/27 tests passed, 4/4 gate cycles clean.

    The gate load is real and measurable — the watch-nodes file goes from 675ms standalone to 2786ms post-build (2–4× slower). But the specific test lands at 14–75ms against 5000ms, a 60–350× margin. It isn't squeaking by under load; it finishes instantly.

    5. What the timings retro-explain

    The original 5002–5012ms failures were never "the event arrived just past the deadline" — they were the event never arriving, with 5000ms being merely when the test gave up. That's why raising the budget to 25s produced a 25010ms failure rather than a pass. The failure mode was binary, not gradual. Post-#2587 the event arrives in tens of milliseconds.

    This also rules out the slower-machine theory: a machine would have to be ~100× slower on this path to threaten the budget, and hardware speed was never the variable in a never-delivered event.

    6. Why not to implement either proposed fix

    Closing as already fixed by #2587. If this flake reappears on a branch that does contain 96aee026, that's a new bug — please capture the per-test timing, since a slow-but-arriving event and a never-arriving one point at completely different causes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtesting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions