Skip to content

fix: gateway shutdown test flakes on unrelated PRs when the recap path is slow - #162117

Closed
azuretek wants to merge 3 commits into
openclaw:mainfrom
azuretek:fix/gateway-shutdown-await-recap-completion
Closed

azuretek wants to merge 3 commits into
openclaw:mainfrom
azuretek:fix/gateway-shutdown-await-recap-completion

Conversation

@azuretek

@azuretek azuretek commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #161803
Fixes #162114

What Problem This Solves

Fixes: src/gateway/server-runtime-subscriptions.shutdown.test.ts > "cancels auxiliary model work before inherited connection drain" fails intermittently on the compact-large Node shards, reddening unrelated pull requests whose diff never touches this file. The reported assertion is:

AssertionError: expected "defaultCompleteModel" to be called once, but got 0 times

User Impact

No user-visible change; the behavior under test and its assertion are kept. The impact is on contributors and maintainers: a flaky gateway test currently fails unrelated pull requests, so a red compact-large shard can no longer be read as a statement about the PR under test.

Why This Change Was Made

The case started subscriptions, requested a recap with ensure(target), then waited for the mocked defaultCompleteModel to have been called once:

await connectionWork.track(() => unsubs!.sessionActivitySummaries.ensure(target));
await vi.waitFor(() => expect(complete).toHaveBeenCalledOnce());

vi.waitFor polls under Vitest's default one-second budget. The completion call runs asynchronously behind a 70-message transcript read and model preparation, so on a loaded runner the first call can land after that budget and the poll reads zero calls: the wait measured host speed rather than the behavior under test.

The change gates the wait on the completion call itself. The mock resolves a deferred, and the test awaits it through withinTest, so there is no wall-clock budget: a healthy run that is merely slow cannot lose the race, and a genuine failure still fails at the Vitest test timeout. Both cases remain and production code is unchanged.

This mirrors the fix a maintainer already merged for the sibling recap file in #161824 (test(gateway): await recap completion instead of polling), which replaced the publication polls in src/gateway/session-activity-summaries.test.ts with awaits on the owner's publication. It also follows the repository's testing guidance, which rules out real timers, sleeps, and polling in tests (docs/help/testing/writing-tests.md); this change removes the last vi.waitFor in the file.

Relationship to CONTRIBUTING

CONTRIBUTING asks contributors not to open test-only PRs for failures already red on main CI. This change is test-only, and that is stated plainly: it removes the last live signature in #161803, whose other signature (the activity-recap store assertion) was already fixed on main by #161824. It is opened deliberately anyway, because the failure is intermittent rather than a standing red, it costs unrelated pull requests real red CI on compact-large, and the fix follows the pattern a maintainer already merged for its sibling file.

Fixes #162114 as well: that issue reports the same signature independently of #161803 - the same file, the same case, and the same defaultCompleteModel "called once, got 0 times" assertion - so merging this PR closes both.

The repository's own AGENTS.md asks for a flaky test seen red on main to be fixed by pushing directly to main rather than through a PR. That route is not available from a fork: the account opening this PR has pull access only (push: false), so the fix lands as this PR.

Evidence

Environment: Linux x64, Node v24.19.0, one file per invocation through the gateway-server config.

A/B against an injected slow recap path. A 1,500 ms delay was added to the recap run() in src/gateway/session-activity-summaries.ts to simulate a loaded runner (scratch, reverted, not in the diff). Same delay, same production code, only the test file differs:

Shutdown test file Result Detail
unmodified (poll under a 1 s budget) FAIL AssertionError: expected "defaultCompleteModel" to be called once, but got 0 times; Tests: 1 failed, 1 passed (2)
this change PASS Tests 2 passed (2)

The "before startup" case passes in both runs, which is the control: it must not depend on the delayed recap, and does not.

Repeats at the branch head (no injected delay).

Run Result
1 Tests 2 passed (2)
2 Tests 2 passed (2)
3 Tests 2 passed (2)

After rebasing onto current main (1bf0175f8c7) the same run passed again: Tests 2 passed (2), 26.6 s.

Measured test cost: 2 cases, one worker, 36.6 s wall on the fastest run here (72.9 s including a cold worker build). The change adds no timer, sleep, or poll and removes one.

Gap: the 1,500 ms delay simulates a loaded runner rather than the original hosted-CI job. The original failures and their job links are recorded in #161803.

The shutdown subscription test polled the mocked defaultCompleteModel with
vi.waitFor under Vitest's default one-second budget while the activity recap
ran asynchronously: a 70-message transcript read, model preparation, then the
completion call. On a loaded compact-large runner the first completion can
land after that budget, the poll reads zero calls, and the case fails on
unrelated PRs whose diff does not touch the file.

Gate the wait on the mocked completion being invoked instead: a deferred
resolved inside the completion mock, awaited through withinTest so a healthy
run that is merely slow cannot lose a race against a wall-clock deadline and
the wait fails only at the Vitest test timeout. Production code is unchanged.

Fixes openclaw#161803
@clawsweeper

clawsweeper Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS labels Sep 30, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 30, 2026
@clawsweeper

clawsweeper Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed September 30, 2026, 5:25 PM ET / 21:25 UTC (Revision 3).

ClawSweeper review

What this changes

The branch changes a Gateway shutdown test to await the activity recap’s model call directly instead of polling for it under a one-second deadline.

Merge readiness

⛔ Blocked before merge - 2 items remain

This is a focused repair for a Gateway test flake that remains on current main. The patch has no identified correctness defect, but the reported runs do not yet meet the repository’s flake-proof bar or resolve the prior review’s concrete proof request.

Priority: P2
Reviewed head: 8e371300627cd99271fc95e0144c49279b483fea

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The patch is narrow and preserves the shutdown contract, while flake validation remains short of the repository’s required proof bar.
Proof confidence 🦐 gold shrimp (3/6) Needs stronger real behavior proof before merge: The reported Linux/Node 24 test run exercised Gateway subscriptions and the recap path with a mocked model call: an injected 1,500 ms delay failed before this change and passed after it. That is relevant after-fix evidence, but the repository’s flake guide also requires 20 standalone passes, three original-shard-order passes, and sibling-test results, which are not supplied for this head. No stored-data contract changes. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Needs proof Needs stronger real behavior proof before merge: The reported Linux/Node 24 test run exercised Gateway subscriptions and the recap path with a mocked model call: an injected 1,500 ms delay failed before this change and passed after it. That is relevant after-fix evidence, but the repository’s flake guide also requires 20 standalone passes, three original-shard-order passes, and sibling-test results, which are not supplied for this head. No stored-data contract changes. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed 9 items Current-main failure point: The base revision still waits for the model-call assertion through vi.waitFor; the PR body reports that this one-second poll can expire while recap preparation continues.
Introduced test change: The pinned introduced patch resolves a deferred when the mocked completion starts, awaits it through withinTest, and retains the call-count and shutdown assertions.
Cancellation-aware wait: withinTest rejects when Vitest aborts the test context, allowing a suspended test to reach its finally cleanup.
Findings None None.
Security None None.

How this fits together

Gateway subscriptions start background activity recaps from session transcripts. The shutdown test checks that closing a connection aborts an in-flight recap before connection work finishes draining.

flowchart LR
  A[Session transcript] --> B[Gateway subscriptions]
  B --> C[Activity recap]
  C --> D[Model call signal]
  D --> E[Connection shutdown]
  E --> F[Abort and drain assertions]
Loading

Before merge

  • Add real behavior proof - Needs stronger real behavior proof before merge: The reported Linux/Node 24 test run exercised Gateway subscriptions and the recap path with a mocked model call: an injected 1,500 ms delay failed before this change and passed after it. That is relevant after-fix evidence, but the repository’s flake guide also requires 20 standalone passes, three original-shard-order passes, and sibling-test results, which are not supplied for this head. No stored-data contract changes. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Complete next step (P2) - Add the flake guide’s 20 standalone runs, three runs in the original shard order, and recap sibling-test result for this head; include comparable timing and the cost rationale. Redact private details from any logs or screenshots, then update the PR body to trigger re-review, or ask a maintainer to comment @clawsweeper re-review if it does not.
Agent review details

Security

None.

PR surface

Tests +13. Total +13 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 1 27 14 +13
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 1 27 14 +13

Review metrics

Metric Value Why it matters
Reported single-worker test cost 36.6 s for 2 cases The reported fastest run exceeds the repository’s 30-second threshold for explaining why a cheaper test cannot prove the contract.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #161803
Summary: The PR targets the shutdown assertion reported in the broader Gateway flake issue; the separately merged recap-publication repair addressed its other signature.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Technical review

Best possible solution:

Keep the completion-signal wait and shutdown assertions, then establish flake resistance in the original shard order and document why this Gateway-level test needs its measured runtime.

Do we have a high-confidence way to reproduce the issue?

Yes, the current-main one-second poll and the reported controlled-delay before/after run give a focused way to reproduce the flake. I did not execute the test in this read-only review.

Is this the best way to solve the issue?

Yes, awaiting the model-call signal addresses the test’s timing race without changing production behavior or weakening its shutdown assertions; the repository’s flake-proof runs remain outstanding.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 8c39234ebfff.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: The intermittent Gateway test failure disrupts unrelated PR validation, with no user-visible runtime change.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The reported Linux/Node 24 test run exercised Gateway subscriptions and the recap path with a mocked model call: an injected 1,500 ms delay failed before this change and passed after it. That is relevant after-fix evidence, but the repository’s flake guide also requires 20 standalone passes, three original-shard-order passes, and sibling-test results, which are not supplied for this head. No stored-data contract changes. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Evidence

What I checked:

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Record 20 clean standalone file runs, three runs in the original shard order, and the relevant recap sibling-test result for this head.
  • Add comparable before-and-after file timing and explain why the shutdown contract needs the measured test cost.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (2 earlier review cycles)
  • reviewed 2026-09-30T20:49:00.848Z sha 0c851c2 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-30T21:10:12.894Z sha af7be69 :: needs real behavior proof before merge. :: none

@azuretek

Copy link
Copy Markdown
Contributor Author

Superseded by 22d7946296b (fix: shutdown test fails before recap model starts, #162133), merged to main on 2026-09-30.

Both changes correct the same assertion in the same case. Diffing this branch and the merged commit against the shared base blob 4d09067edd0 of src/gateway/server-runtime-subscriptions.shutdown.test.ts:

  • This branch replaces await vi.waitFor(() => expect(complete).toHaveBeenCalledOnce()); with await withinTest(completeStarted.promise, testSignal); expect(complete).toHaveBeenCalledOnce();, resolving completeStarted at the top of the defaultCompleteModel mock.
  • 22d7946296b replaces the same line with await withinTest(started.promise, signal); expect(complete).toHaveBeenCalledOnce();, resolving started as the first statement of the callback passed to trackAsyncWork. That helper runs run() synchronously inside scope.track(run), so the deferred resolves at the same instant the mock body does.

The only remaining delta is how the per-test abort signal is obtained: here a beforeEach(({ signal }) => ...) with it.each; in the merged commit it.for with { signal } from the test context. The gate on model start, the deferred itself, the withinTest helper and the removal of the wall-clock poll are identical, and main no longer polls that assertion. Rebasing would leave an empty diff against main, so this PR is closed as superseded rather than pushed.

Issues: #162114 (this signature reported on its own) and #161803 (both signatures, the activity-recap store assertion fixed by #161824 with merge 5bfb545bfc3, and this shutdown assertion fixed by 22d7946296b) are closed as fixed on main.

@azuretek azuretek closed this Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: XS status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

1 participant