Skip to content

test(gateway): wait for chat.send history run completion deterministically - #160479

Merged
VACInc merged 1 commit into
mainfrom
fix/chat-send-history-wait-budget
Sep 28, 2026
Merged

VACInc merged 1 commit into
mainfrom
fix/chat-send-history-wait-budget

Conversation

@VACInc

@VACInc VACInc commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Summary

High Level TLDR

Two gateway chat tests finished by calling agent.wait with a 1-second real-clock budget after starting a chat.send run. On loaded CI runners the run can take longer than 1s, so agent.wait returned {"status":"timeout"} and the test failed even though nothing was wrong. The tests now wait for the observed run to actually drain (waitForAgentRunDrained), then assert agent.wait with timeoutMs: 0 returns ok. No wall-clock budget left in these assertions.

What Problem This Solves

src/gateway/server.chat.gateway-server-chat.test.ts > "chat.send accepts the backing session id returned by chat.history" flakes on CI in PRs that do not touch the file:

→ {"runId":"idem-chat-send-history-session-id","status":"timeout"}: expected 'timeout' to be 'ok' // Object.is equality

Root Cause

  • The test starts a /context list run through chat.send, then calls waitForAgentRunOk(runId). That helper (line ~356) sends agent.wait with timeoutMs = 1_000 by default, so the assertion passes only if the run finishes inside 1s of real time.
  • Under a local 3-way concurrent run of this file, this test alone takes ~500ms, half the budget. A busier CI shard (checks-node-changed-compact-large-*, agentic-control-plane-agent-chat) pushes it over 1s, and agent.wait correctly reports timeout.
  • The same file already has a deterministic helper, waitForAgentRunDrained(runId). It joins the observed request plus its detached execution and effects via requestExecution.waitForCompletion(runId), asserts getActiveGatewayRootWorkCount() === 0, then calls agent.wait with timeoutMs: 0. The other started-run tests in this file (idem-img, idem-session-key-1, etc.) already use it. These two call sites were the only ones left on the 1s default.

Why This Change Was Made / exact change

  • Line ~1026 ("chat.send accepts the backing session id returned by chat.history"): waitForAgentRunOk(runId) -> waitForAgentRunDrained(runId).
  • Line ~2032 ("agent.wait ignores lifecycle completion while same-runId chat.send is active"): same pattern (a started chat.send run, then a 1s agent.wait after release), same change. The deliberate timeoutMs: 40 "still active" probe earlier in that test is untouched.
  • No timeouts raised, no production code changed.

Scope boundary

Only the two default-budget waitForAgentRunOk(runId) call sites. Tests that intentionally exercise timeout behavior (timeoutMs: 0 / 40 probes, expectAgentWaitTimeout) are unchanged.

User Impact

None. This is test-only. It removes a CI flake that fails unrelated PRs.

Real behavior proof

Reproduction gap: on clean main the original test passed 5/5 sequentially and 5/5 under 8-way concurrent pressure locally. The 1s overrun reproduces only on loaded CI runners (two independent CI failures above). The fix removes the wall-clock dependency, so it does not rely on reproducing the overrun.

After patch (local Linux, Node from the repo toolchain):

$ pnpm test src/gateway/server.chat.gateway-server-chat.test.ts --maxWorkers=1
 Test Files  1 passed (1)
      Tests  41 passed (41)
[test] passed 1 Vitest shard in 55.14s     (wall 55.56s, cold-ish compiler cache)

# 3 concurrent copies, --reporter=verbose
run 1 exit 0 / run 2 exit 0 / run 3 exit 0   (each: Tests 41 passed (41), ~27s)
✓ ... chat.send accepts the backing session id returned by chat.history 498ms
✓ ... agent.wait ignores lifecycle completion while same-runId chat.send is active 218ms / 226ms

What was not tested

  • I did not reproduce the original >1s overrun locally. The CI failures above are the before evidence.
  • CI was not rerun to get green.

Verification

  • pnpm test src/gateway/server.chat.gateway-server-chat.test.ts --maxWorkers=1: 41/41 pass, wall 55.6s. This includes compile. The change adds no new tests and no timers.
  • The same file run 3x concurrently: 3/3 pass, 41/41 each.
  • node scripts/check-changed.mjs: exit 0 (oxfmt, core test typecheck for the gateway-server graph, lint, dead-export and ratchet guards).

Evidence

The CI job links and local run output are above.

Flake validation

This follows the proof bar in docs/help/testing/writing-tests.md "Flake triage", run on PR head 4e71ce7a98 in a local Linux worktree. No timeout changes.

Root cause, classified per step 2: this is an ordering flake. The assertion ran before the owned completion signal. agent.wait got a 1s wall-clock budget instead of joining the observed request, detached execution, and effects. The fix waits on that owned signal (waitForAgentRunDrained).

20 standalone runs of the file: 20/20 pass (41/41 tests each)

for i in $(seq 1 20); do pnpm test src/gateway/server.chat.gateway-server-chat.test.ts --maxWorkers=1; done

Wall per run (s): 66.6, 34.9, 31.2, 30.9, 39.0, 39.6, 41.7, 78.8, 47.2, 42.1, 49.0, 39.2, 34.3, 32.0, 32.6, 33.4, 31.0, 31.4, 34.5, 40.5. The spread comes from shard runs executing at the same time as extra host load.

Original shard agentic-control-plane-agent-chat: 3 clean runs, in recorded plan order

I decoded the exact 59-file group, including its order, config (test/vitest/vitest.gateway-server.config.ts) and env (OPENCLAW_VITEST_MAX_WORKERS=2), from the failing job's OPENCLAW_NODE_TEST_GROUPS_GZIP_BASE64. Then I replayed it through the repo shard runner:

OPENCLAW_NODE_TEST_GROUPS_JSON='[<decoded agentic-control-plane-agent-chat group>]' \
OPENCLAW_NODE_TEST_PLAN_CONCURRENCY=1 OPENCLAW_VITEST_SHARD_NAME=agentic-control-plane-agent-chat \
node --import ./scripts/tsx.mjs scripts/ci-run-node-test-shard.mts
run result wall
1 58/59 files. The target file passed. src/gateway/server.sessions.preview-resolve.test.ts failed at suite startup (see below) 524s
2 59/59 files, 735/735 tests 574s
3 59/59 files, 735/735 tests 521s
4 59/59 files, 735/735 tests 473s

Unrelated failure in run 1: server.sessions.preview-resolve.test.ts failed Gateway startup with Startup failed: required secrets are unavailable. Error: Shared-state worker opening path changed (STATE_DATABASE_READ_ADMISSION_INVALIDATED, from assertOpenClawStateWorkerActorPath). This PR does not touch that file or the state-worker path, and the error did not recur in runs 2 to 4. I'm reporting it here and not hiding it. It needs its own triage.

Owner sibling tests: pass

pnpm test src/gateway/server.chat.gateway-server-chat-b.test.ts src/gateway/server.chat-abort-settlement.test.ts src/gateway/server.chat-history-cursor.test.ts --maxWorkers=1
# Test Files 3 passed (3), Tests 98 passed (98), wall 53s

The shard runs above also cover all 59 gateway-server sibling files in this lane.

@clawsweeper

clawsweeper Bot commented Sep 28, 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 28, 2026
@VACInc VACInc self-assigned this Sep 28, 2026
@VACInc
VACInc enabled auto-merge (squash) September 28, 2026 13:52
@VACInc
VACInc disabled auto-merge September 28, 2026 13:52
@clawsweeper

clawsweeper Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 28, 2026, 10:39 AM ET / 14:39 UTC (Revision 2).

ClawSweeper review

What this changes

Updates two Gateway WebSocket chat tests to wait for a started run to finish before checking its final status.

Merge readiness

✅ Ready for maintainer review

Current main still contains the one-second assertion race. The focused PR remains useful, and its updated validation addresses the prior review request. No introduced defect is evident.

Priority: P2
Reviewed head: 4e71ce7a98f30caecccac2a2c985241b80b1899c

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A small, well-targeted test repair now has the documented repeated-run and sibling validation, with its unrelated shard failure disclosed.
Proof confidence 🦞 diamond lobster (5/6) Not applicable: The contributor proof gate does not apply to this MEMBER-authored, test-only PR. Its reported Linux runs exercise the changed WebSocket Gateway test path after the fix: 20 standalone runs, three clean original-shard runs, and sibling tests passed; the earlier CI timeout is cited, though the baseline overrun was not reproduced locally. No stored-data contract changes.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The contributor proof gate does not apply to this MEMBER-authored, test-only PR. Its reported Linux runs exercise the changed WebSocket Gateway test path after the fix: 20 standalone runs, three clean original-shard runs, and sibling tests passed; the earlier CI timeout is cited, though the baseline overrun was not reproduced locally. No stored-data contract changes.
Evidence reviewed 9 items Introduced change: The pinned PR delta replaces the one-second wait at exactly two test call sites; it changes no production code or timeout probe.
Current main remains unfixed: Both corresponding call sites on fetched main still use the helper's one-second default, so the central change is not already implemented there.
Completion boundary: The existing helper joins the observed run, checks that Gateway root work drained, and then requests a zero-budget final snapshot.
Findings None None.
Security None None.

How this fits together

Gateway chat requests acknowledge a started run before its detached work finishes. These tests send WebSocket requests to a test Gateway, then check run status and session events.

flowchart LR
A[WebSocket chat test] --> B[Chat send request]
B --> C[Started acknowledgment]
C --> D[Detached Gateway run]
D --> E[Observed run completion]
E --> F[Agent wait snapshot]
F --> G[Test assertion]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Tests 0. Total 0 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 1 2 2 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 1 2 2 0

Review metrics

Metric Value Why it matters
Flake validation 20/20 standalone; 3 clean of 4 original-shard runs; 98/98 sibling tests The reported runs cover the repository's documented flake-validation bar and disclose the separate failed shard run.

Technical review

Best possible solution:

Use the existing observed-run completion signal for final-status assertions while retaining the deliberate active-run timeout probe.

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

No deterministic local reproduction is recorded. Current main has the one-second wait, and the PR cites two CI timeout failures, while its local baseline runs passed.

Is this the best way to solve the issue?

Yes. The existing observer joins the work whose completion the tests assert, without changing production behavior or the intentional timeout probe.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 9fe93a204e3b.

Labels

Label changes:

  • add P2: This bounded test flake can fail unrelated PR checks but does not change user-facing runtime behavior.
  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The contributor proof gate does not apply to this MEMBER-authored, test-only PR. Its reported Linux runs exercise the changed WebSocket Gateway test path after the fix: 20 standalone runs, three clean original-shard runs, and sibling tests passed; the earlier CI timeout is cited, though the baseline overrun was not reproduced locally. No stored-data contract changes.

Label justifications:

  • P2: This bounded test flake can fail unrelated PR checks but does not change user-facing runtime behavior.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The contributor proof gate does not apply to this MEMBER-authored, test-only PR. Its reported Linux runs exercise the changed WebSocket Gateway test path after the fix: 20 standalone runs, three clean original-shard runs, and sibling tests passed; the earlier CI timeout is cited, though the baseline overrun was not reproduced locally. No stored-data contract changes.

Evidence

What I checked:

Likely related people:

  • unknown: The claimed source-line change could not be verified from bounded local history. (role: source history unknown; confidence: low)
  • RileyJJY: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

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 (1 earlier review cycle)
  • reviewed 2026-09-28T13:52:34.452Z sha 4e71ce7 :: blocked before merge. :: none

@VACInc

VACInc commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

@clawsweeper re-review — flake-validation tallies (20/20 standalone, 3 clean shard replays, siblings) are in the new "Flake validation" section of the PR body.

@clawsweeper

clawsweeper Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 28, 2026
@VACInc
VACInc merged commit d2c789b into main Sep 28, 2026
112 checks passed
@VACInc
VACInc deleted the fix/chat-send-history-wait-budget branch September 28, 2026 14:41
MarMarLabs added a commit to MarMarLabs/openclaw that referenced this pull request Sep 28, 2026
31 upstream commits, including the deterministic wait for the gateway
chat.send history test (openclaw#160479), which failed on the previous merge in a
file this branch does not touch.

No conflicts. No file changed on both sides, and upstream left the
generated config baseline, the channel metadata and the lockfile unchanged
in this range.

build:docker ran in a separate detached checkout of this commit outside the
nested node_modules install. Regenerating there changed nothing, so the
counts stay at core 2464, channel 3788, plugin 4374 with the same hashes.
The three generated-file checks pass there.

Upstream deleted seven test files in this range. None of them were added
or changed by this branch.

Feishu: 106 files and 1853 tests in the extension project, 17 files and
312 tests in the database-workers project. The forwarder test and the
routing guards pass, 1981 tests. tsgo:core, tsgo:extensions,
tsgo:extensions:test, tsgo:test:root, the line-cap and assertion-safety
ratchets against upstream/main, oxlint and oxfmt on the 43 changed code
files all pass.

check:max-lines-ratchet reports its line counts OK but fails the
OPENCLAW_* environment variable budget at 484 against 483. Clean upstream
741c9cb fails it with the same count, and this branch adds no new
OPENCLAW_* name.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 29, 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: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant