Skip to content

ui-server: safety-history WS replay test races a poll against an unsettled write and is closed with gap under load #3723

Open

Description

@schlessera

Context

Hosted CI run 42483189496, job unit (4), on PR #3715 at head 12d7f1c6f, failed one test with Safety frame deadline: real WS replay and contiguous updates survive duplicate/reordered delivery without count reduction (test("real WS replay, packages/ui-server/tests/safety-history.test.ts:33). The error comes from the fixture's frame waiter (const until=, packages/ui-server/tests/helpers/safety-history-fixture.ts:62-64), an event-driven listener with a 3,000 ms failure maximum. The PR does not touch the safety-history path.

Reproduced and attributed at main fb999b8baeabbb3a9cb5756a19862eee45467ebf (triage 2026-11-09, Bun 1.4.4 38afee6c8, frozen install, throwaway worktree, one busy loop per core plus six parallel copies of the file):

  • Unmodified: 3 of 12 runs failed this test with error: Safety frame deadline at safety-history-fixture.ts:64:70, in about 3.7 s each. The other 13 tests in the file passed.
  • The frame is not late. It never comes. With the failure maximum raised to 30 s as a diagnostic only, 4 of 18 runs still failed after 30 s. At the 3 s mark the buffered frames were status and {"type":"safety_unavailable","version":1,"reason":"gap","requestId":"subscription"}, while the waiter was looking for m.type === "safety_update". A longer deadline cannot fix this test.
  • Tagging each gap site in the throwaway copy showed one source in every failing run, the equal-revision projection check in poll (if(update.toRevision===s.revision&&projectionHash!==s.projectionHash), packages/ui-server/src/governance/safety-history.ts:250-252). Its comment says this is intentional: "Equal durable revisions do not prove equal read state" (line 248).
  • The sequence in every failing run: the subscription anchors at revision 7 (unresolved 1, pending 0). A poll then delivers 7 → 10 with unresolvedCount: 2, pendingCount: 1. That read saw B's durable rows while B's local pending latch was still set. through(2) waits for pendingCount === 0, so it ignores that update (const through=, safety-history.test.ts:41). The next poll reads revision 10 again with pendingCount: 0. The revision is equal and the fingerprint differs, so the server deletes the subscription and sends gap. No further safety_update is sent on that subscription.
  • In passing runs the first delivered update was already 7 → 10 with pendingCount: 0, then 10 → 13.

The race is between a poll's readDelta (private async readDelta, safety-history.ts:190-205) and the test's await f.add("B");f.history.poll(); (safety-history.test.ts:42; const add=async, safety-history-fixture.ts:54-56). subscribe triggers a poll of its own (this.poll() at the end of subscribe, safety-history.ts:207-211). The diagnostics were discarded and no source was changed.

Scope

  • Make the test's subscription phase deterministic: no poll may observe a write between its durable commit and the clearing of its local pending latch. For example, let the subscribe-triggered poll settle before the next add, and make each explicit poll() run only after that write's local pending state has settled. Use an observable condition, not a sleep, and follow docs/process/test-waits.md.
  • Optionally, the waiter can fail at once with a clear message when a safety_unavailable frame arrives for the awaited requestId, instead of running out its deadline. That turns this race into a named failure.

Out of scope

  • Changing safety-history product semantics: the equal-revision/different-projection gap close, pending/loss latches, revision numbering or the WS protocol. If the implementer finds this window also reaches real clients in a way that needs a product change, file that as a separate issue with evidence.
  • Raising the 3,000 ms failure maximum as the fix, retries, skips or quarantining.

Acceptance criteria

  • The PR names the race (a poll observes B with pendingCount: 1 at the same revision a later poll sees with pendingCount: 0, so gap closes the subscription) and the ordering change that removes it.
  • Failing-first receipt under the load recipe below: before the fix, at least one run fails this test, with a safety_unavailable/gap frame recorded or with Safety frame deadline. After the fix, at least 18 runs under the same recipe produce zero failures of this test.
  • Mutation receipt: removing only the ordering change brings the failure back under the same recipe.
  • A mutation that suppresses the awaited safety_update still fails the test with the named deadline (or the new named safety_unavailable message).
  • packages/ui-server/src/governance/safety-history.ts and packages/ui-server/src/ws/handlers/safety.ts are unchanged.

Verification

for i in $(seq $(nproc)); do (while :; do :; done) & done   # kill afterwards
for r in 1 2 3; do for c in 1 2 3 4 5 6; do
  bun run test packages/ui-server/tests/safety-history.test.ts -t 'real WS replay' > run-$r-$c.log 2>&1 &
done; wait; done

Record pass/fail counts before and after, plus both mutation receipts. Also run the whole file once (bun run test packages/ui-server/tests/safety-history.test.ts).

Contract impact

None. Test/fixture ordering only.

Triage 2026-11-09 at main fb999b8ba: reproduced the hosted failure locally (3/12 unmodified loaded runs). A 30 s diagnostic deadline showed the frame never arrives: the server closes the subscription with gap after a poll observes B's transient pending latch. needs: repro is removed. The scope moved from "wait longer" to "remove the write/poll race". #736 is full, and #2753 covers only browser runs, so this stays standalone. P2: wanted, unscheduled.

Activity

  1. added
    type: test
    area: ui-server
    priority: p2
    needs: repro
    on Nov 8, 2026
  2. changed the title [-]ui-server: safety-history WS fixture's fixed 3 s frame deadline fails under CI load[/-] [+]ui-server: safety-history WS replay test races a poll against an unsettled write and is closed with gap under load[/+] on Nov 9, 2026
  3. added
    agent-ready
    and removed
    needs: repro
    on Nov 9, 2026
  4. schlessera commented on Nov 9, 2026

    @schlessera
    OwnerAuthor

    Triage 2026-11-09 at main fb999b8ba (Bun 1.4.4, busy loop per core plus six parallel copies):

    • Reproduced. 3 of 12 unmodified runs failed real WS replay… with Safety frame deadline, at the same fixture line as the hosted job.
    • A longer deadline does not help. With a 30 s diagnostic maximum, 4 of 18 runs still failed. The server had sent safety_unavailable with reason: "gap" for the subscription. Tagging the gap sites pinned it to the equal-revision/different-projection close in poll (safety-history.ts:250-252): one poll delivered 7→10 with pendingCount: 1 (B's local latch still set), the next read revision 10 with pendingCount: 0, and the subscription was closed.
    • Removed needs: repro. Rewrote the scope as a test-ordering fix (product gap semantics out of scope) with failing-first and mutation receipts, retitled the issue to the actual cause and added agent-ready. No parent: Epic: Keyless verification completes deterministically and isolates host state #736 is full, and Epic: browser verification is deterministic and attributable #2753 covers only browser runs.
  5. schlessera commented on Nov 10, 2026

    @schlessera
    OwnerAuthor

    Another hosted occurrence: CI run 42890776798, job unit (1), on the test-health batch PR #3897 (head a4b1b9854c64b772b4d92e7b0b931a2a5078a765, base epic/3855-test-health). real WS replay and contiguous updates survive duplicate/reordered delivery without count reduction failed after 15.3 s with Safety frame deadline at packages/ui-server/tests/helpers/safety-history-fixture.ts:69. That PR changes only packages/ui-backend-claude/tests files, none of them on this test's path. Not rerun.

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

    agent-readyarea: ui-serverpriority: p2type: test

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions