Skip to content

AI-4796: fix flaky test that read the wrong reply - #3054

Merged
kanat merged 2 commits into
mainfrom
kanat/ai-4796-flaky-chat-episodes
Dec 6, 2026
Merged

kanat merged 2 commits into
mainfrom
kanat/ai-4796-flaky-chat-episodes

Conversation

@kanat

@kanat kanat commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

TestAnotherUsersMessageLetsThePersonsSessionFinishItsReplies failed 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"
Loading

After the fix the test checks the router's count of follow-ups her session owes (owing.owes) between the two writes:

write "when is my cleaning?" -> model asked -> wait: her session owes nothing
write "and the price?"                      -> wait: her session owes a reply
write Bob's message                         -> retires her session, which still
                                               answers "and the price?" first
Change Result Proof
Wait until her session owes the reply to "and the price?" before Bob writes Bob's message can no longer be routed first With a 300 ms delay injected into routing "and the price?": base fails 10/10, head passes 10/10
Wait first until it owes nothing, after "when is my cleaning?" began The second wait cannot pass on the first question's count Delay owed() by 300 ms after her first FollowUp returns, plus the routing delay: base fails, head passes (review 6206486728, 3/3 each)
Runs Base Head
CPU load, 10 busy loops, -cpu 4 24/25 pass: the one failure is CI's, line for line 50/50 pass
No load (-cpu 1, and -race -cpu 1) 35/35 pass not run
300 ms delay before "and the price?" is routed 0/10 pass 10/10 pass

Under load the base failed the same way CI did (run 47567243034): :776 got "and the price?" where it expected "one more thing", and :946 waited 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>):

Mutant Result
Test never writes "and the price?" killed: "her second question is never handed to her session"
Drop the wait for the first question's follow-up to begin survived under the plain test; killed by the 300 ms owed() delay plus the routing delay (review 6206486728)
retire closes the session at once (the defect the test was written for) killed: "no model was asked to answer "and the price? ...""
retire waits only while busy, not for owed follow-ups killed: same
A follow-up handed to a running session is not counted as owed killed: "her second question is never handed to her session"
A message by another person does not retire the session killed: "the worker was handed nothing"

Commands, from acceleration/:

go test -tags integration -p 1 -timeout 10m -count=1 -run 'TestChatEpisodesSuite/TestAnotherUsersMessageLetsThePersonsSessionFinishItsReplies$' ./internal/api
go test -tags integration -p 1 -timeout 10m -count=1 -run 'TestChatEpisodesSuite' ./internal/api

The delay probe adds if strings.HasPrefix(event.Message.Text, "and the price? ") { time.Sleep(300 * time.Millisecond) } before liveSessionOn in routeArrivingMessage, only in a scratch run.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EZsRxWUwySyG1JAn9T7GpE

…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
@getstream-metis

getstream-metis Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

🎉 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.

  • Review this PR

Or comment /jeffery review. I'll stay quiet unless you ask.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

TestAnotherUsersMessageLetsThePersonsSessionFinishItsReplies failed 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.

Comment on lines +767 to +769
// 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Nit] T1.2 "owes" is not explained, and the last sentence is hard to follow.

Suggested change
// 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, all no 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 whole TestChatEpisodesSuite passes; gofmt, go build, go vet -tags integration are clean.
  • The author's 6 mutants re-run: 5 killed, the !owes wait 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: followUpMessage loops 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 !owes wait. One can: delay owed() by 300 ms after FollowUp returns 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")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
@kanat
kanat marked this pull request as ready for review December 5, 2026 22:23
@kanat
kanat merged commit 00ba69c into main Dec 6, 2026
20 checks passed
@kanat
kanat deleted the kanat/ai-4796-flaky-chat-episodes branch December 6, 2026 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant