Repository navigation
AI-4796: fix flaky test that read the wrong reply - #3054
Conversation
…writes The test wrote her "and the price?" and Bob's message back to back. Both were only queued, and the queue routes messages side by side, so Bob's was sometimes routed first: it retired her session and her message went to the worker instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EZsRxWUwySyG1JAn9T7GpE
🎉 Out of draft, Kanat! Want a second pair of eyes on this?I'm Jeffery, Stream's review agent. I look for bugs, regressions, missing tests and slow paths, and I only post findings I've checked against the code.
Or comment |
kanat
left a comment
There was a problem hiding this comment.
Text review: 2 unclear of 2
[Nit] T1.1 PR opening. The second sentence has a colon clause where "it" could mean the queue or Bob's message, and "retired her session" and "the worker" are not explained. A suggested opening (not changed by me, the PR-body fixer can paste it):
TestAnotherUsersMessageLetsThePersonsSessionFinishItsRepliesfailed now and then because Bob's message could be processed before Anya's second one. The chat endpoint answers 200 once a message is queued, and the queue handles several messages at once, so two quick writes can be processed in either order. When Bob's came first, it ended Anya's session and her message went to the worker instead; the test now waits until her session has taken her message before Bob writes, and the router does not change.
The ## How it works chart and the tables stay as they are.
| // A 200 only means the message is queued, and the queue routes messages side by side, so Bob's | ||
| // could be routed before her second and hand it to the worker. We wait until her session owes | ||
| // the reply to her second, after it no longer owes the one to her first. |
There was a problem hiding this comment.
[Nit] T1.2 "owes" is not explained, and the last sentence is hard to follow.
| // A 200 only means the message is queued, and the queue routes messages side by side, so Bob's | |
| // could be routed before her second and hand it to the worker. We wait until her session owes | |
| // the reply to her second, after it no longer owes the one to her first. | |
| // A 200 only means the message is queued, and queued messages are handled in parallel, so Bob's | |
| // could be processed before her second one and send that one to the worker. So we wait until her | |
| // session has finished her first reply, then until it has taken her second, before Bob writes. |
There was a problem hiding this comment.
Fixed in 593ee20: the comment at chat_episodes_test.go now says why the test waits (commit only, no code change; gofmt and go vet -tags integration ./internal/api are clean).
kanat
left a comment
There was a problem hiding this comment.
VERDICT: GO
Reviewed 9200306 (it sits on top of the current main, 0b2eda6). The diff is 7 test lines; the router is unchanged.
Cause. Her "and the price?" and Bob's message were written back to back. A 200 only means queued, and the events queue routes up to 64 at once (config.go:939), so Bob's could be routed first, retire her session and send hers to the worker. The new waits make the router count her second message as owed (messagehooks.go:414) before Bob writes. After that retire waits for it (chat_episodes.go:187), so the order the test assumed now holds.
Checked.
- Base test with a 300 ms delay before her message is routed, under 10 busy loops,
-cpu 4: 0/5 pass, allno model was asked to answer "and the price? ...". - Head under the same delay and load: 20/20 at
-cpu 4, 20/20 at-cpu 1. Head under load without the delay,-cpu 1: 10/10. The wholeTestChatEpisodesSuitepasses;gofmt,go build,go vet -tags integrationare clean. - The author's 6 mutants re-run: 5 killed, the
!oweswait survives (see the Nit). I added one: dropping the second wait is killed 3/3 under the delay. - No assertion is weakened and no timeout is raised. The second wait cannot miss its window:
followUpMessageloops while her first reply runs, so her second stays owed for about 6 s. - Sweep of the other tests in the suite: only this one writes two messages back to back and then writes as someone else. The others wait for the first message to be handed on or asked before the next write (
:87/:100,:378-:394,:725/:726/:731,:858-:878).
CI: a draft runs no integration job, so it runs at gh pr ready. Details: reviewer-1.md in the ship-task scratchpad.
FINDINGS
- [Nit]
chat_episodes_test.go:772— the PR body and the author report say no mutant can show the!oweswait. One can: delayowed()by 300 ms afterFollowUpreturns for her first message, plus the routing delay. Head passes 3/3 and the mutant without the wait fails 3/3 (Bob routed first). The wait is needed; only the claim is wrong. Fix (optional): drop the "no mutant" sentence from the body. - [Question] Two messages in one channel are routed side by side, so one person's two quick messages can be answered out of order. Not changed here, as the body says. File a ticket for the product decision (order per channel, or leave).
MUTATIONS: 8 checked, 1 survived (the !owes wait under the plain test; killed by the delayed-owed() probe above).
| // the reply to her second, after it no longer owes the one to her first. | ||
| hers := session.Key{CustomerID: s.customerID(), ID: anyas.Id} | ||
| owes := func() bool { return s.router.owing.owes(hers) } | ||
| s.Require().Eventually(func() bool { return !owes() }, settleFor, 10*time.Millisecond, "her first question never began") |
There was a problem hiding this comment.
[Nit] R1.1 This wait survives the plain test and the routing delay (5/5), so the PR body says no mutant can show it. A delay of 300 ms before owed() is released for her first message, plus the routing delay, does: with this line the test passes 3/3, without it 0/3. Keep the line; the "no mutant" claim in the body is the only thing to change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EZsRxWUwySyG1JAn9T7GpE
TestAnotherUsersMessageLetsThePersonsSessionFinishItsRepliesfailed now and then because Bob's message could be processed before Anya's second one. The chat endpoint answers 200 once a message is queued, and the queue handles several messages at once, so two quick writes can be processed in either order. When Bob's came first, it ended Anya's session and her message went to the worker instead; the test now waits until her session has taken her message before Bob writes, and the router does not change.How it works
sequenceDiagram participant Test participant Queue as events queue participant Router as routeArrivingMessage participant Session as Anya's session participant Worker Test->>Queue: "and the price?" (200 = queued) Test->>Queue: Bob's message (200 = queued) Note over Queue,Router: before: both run side by side Queue->>Router: Bob's message first Router->>Session: retire (another person wrote) Queue->>Router: "and the price?" Router->>Worker: handed out, since her session is retiring Note over Test,Worker: CI: worker got "and the price?" where the test expected "one more thing"After the fix the test checks the router's count of follow-ups her session owes (
owing.owes) between the two writes:owed()by 300 ms after her firstFollowUpreturns, plus the routing delay: base fails, head passes (review 6206486728, 3/3 each)-cpu 4-cpu 1, and-race -cpu 1)Under load the base failed the same way CI did (run 47567243034):
:776got "and the price?" where it expected "one more thing", and:946waited for a model to be asked "and the price?". The delay probe forces that order on every run.Not changed
The router still routes two messages of one channel side by side. A person who sends two messages within a few milliseconds can get the answers in either order. Whether they should keep their order is a separate decision, not part of this fix.
Mutations and commands
Each mutant was applied, the test run once, and the file restored (
scripts/mutate.sh <worktree> <db>):owed()delay plus the routing delay (review 6206486728)retirecloses the session at once (the defect the test was written for)retirewaits only while busy, not for owed follow-upsCommands, from
acceleration/:The delay probe adds
if strings.HasPrefix(event.Message.Text, "and the price? ") { time.Sleep(300 * time.Millisecond) }beforeliveSessionOninrouteArrivingMessage, only in a scratch run.🤖 Generated with Claude Code
https://claude.ai/code/session_01EZsRxWUwySyG1JAn9T7GpE