Skip to content

ADFA-7249 | AI-Core: fix the races in three ChatViewModel test classes - #176

Open
davidschachterADFA wants to merge 3 commits into
mainfrom
fix/ADFA-7249-chat-test-races
Open

davidschachterADFA wants to merge 3 commits into
mainfrom
fix/ADFA-7249-chat-test-races

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Jira: ADFA-7249

What: three AI-Core test classes failed now and then, more often under CPU load, because a test went on before the work it checks had finished. This changes only the tests: no production code, and the .cgp is unchanged.

Why they failed

  • ChatViewModelCompactionTest: a failed run sets AgentState.Error, then adds its error message, and clears its in-flight flag last, in a finally (ChatViewModel.kt, the end of the run). The send() and retry() helpers stopped waiting at AgentState.Error. The next step could then read before the message existed, or call retryLastRun() while the run was still in flight, which ignores it, and the next wait timed out.
  • ChatViewModelPromptEditTest: a run sets AgentState.Idle before its finally clears the in-flight flag. sendAndAwait and the other waits stopped at Idle. editPrompt could then return BUSY instead of STARTED, a fork never started and the next wait timed out, or sendMessage returned false.
  • ChatViewModelTitleQueueTest: the title request runs on Dispatchers.IO. Two tests checked that generateCompletion was called without waiting for it.

The fix

  • ChatViewModelCompactionTest: both helpers wait on canChangePrompts, which turns true only after the in-flight flag is cleared. AgentState.Error doesn't count as running, so a failed run still ends the wait.
  • ChatViewModelPromptEditTest: the waits use agentState == Idle && canChangePrompts. These hunks are line for line the ones in ADFA-6767 | Put prompt actions beside the bubble and add a full-screen chat #154 (ADFA-6767), so the two PRs merge cleanly in either order.
  • ChatViewModelTitleQueueTest: the two checks use verify(timeout = WAIT_MS).

Tests

Each fix was proved by widening its race on purpose, with a 300 ms sleep added to ChatViewModel for the proof only, never committed:

  • ChatViewModelCompactionTest and ChatViewModelPromptEditTest, with a sleep before the in-flight flag is cleared: at main, 12 of the 30 tests in the two classes fail; with this PR, none.
  • ChatViewModelTitleQueueTest, with a delay at the start of the title request: at main, both tests fail; with this PR, neither.

Without any added sleep:

  • The first two classes, pinned to 2 cores (taskset -c 0,1): main failed in 3 of 3 runs, and this PR passed 5 of 5.
  • The full AI-Core testDebugUnitTest at the head: 10 runs in a row, 1,342 tests each, 0 failures, 5 of the runs pinned to 2 cores. Before the title fix, 3 of the 5 pinned runs failed one title test; that is how the third race was found.
  • assemblePlugin and addons check pass.

Version: no bump. Only tests change, and the .cgp is the same; #152 (ADFA-6795), a test-only fix to these classes, merged without one.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn

davidschachterADFA and others added 3 commits November 21, 2026 22:51
…mpactionTest

A failed run sets AgentState.Error, adds its error message, and clears its
in-flight flag last. send() and retry() stopped waiting at the error state,
so the next step could read before the message existed, or retry while the
run was still in flight, which ignores the retry. Both now wait on
canChangePrompts, which turns true only after the flag is cleared.

With a 300 ms sleep added before the flag is cleared (proof only), the old
helpers fail and the new ones pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
…ditTest

A run sets AgentState.Idle before it clears its in-flight flag. The waits
stopped at Idle, so editPrompt could return BUSY, a fork never started, or
sendMessage returned false. They now also wait on canChangePrompts. The
hunks are the same as in #154 (ADFA-6767), so both merge cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
…eueTest

The title request runs on Dispatchers.IO. Two tests verified the
generateCompletion call without waiting for it; they now verify with a
timeout. With a 300 ms delay at the start of the request (proof only), the
old checks fail and the new ones pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

  • Test-only change. Three files, +33/-15 lines. No production code changes.
  • CompactionTest: helpers wait for canChangePrompts after the run unwinds, not on AgentState.Error.
  • PromptEditTest: waits require agentState == Idle and canChangePrompts == true.
  • TitleQueueTest: two checks use verify(timeout = WAIT_MS) for the Dispatchers.IO request.
  • Risk: timeout waits depend on WAIT_MS. Slow CI may still fail.
  • Risk: the author's stress-test and pass/fail results are not verified here.
  • Risk: the PromptEdit change matches PR #154. Coordinate merge order if conflicts appear.

AI-Core view-model tests now wait for runs to finish before continuing prompt operations. Title queue tests wait for asynchronous generateCompletion calls before checking their arguments.

Changes: View-model test synchronization

Layer / File(s) Summary
Run completion waits
plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModelCompactionTest.kt, plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModelPromptEditTest.kt
The tests use canChangePrompts with agent idleness to determine when a run has finished. Compaction tests wait for runs to unwind after retry and send operations.
Title call verification
plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModelTitleQueueTest.kt
Title queue tests wait up to WAIT_MS for matching generateCompletion calls and verify the expected source text.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: jatezzz

Merge Risk: Minimal · up to 022cc

This PR only changes test synchronization to remove intermittent failures in three ChatViewModel test classes. Production code is unchanged. No concrete defect was found, and the reported repeated passing runs support the fix, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the ticket and the main change: it fixes races in three ChatViewModel test classes in AI-Core. The changeset edits only those three test classes, and the summaries show the waits now u…
Description check Passed The description explains the failing tests, the root cause (tests stopped waiting before the in-flight flag was cleared or before the title request finished), the fix in each of the three test classes…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the ticket and the main change: it fixes races in three ChatViewModel test classes in AI-Core. The changeset edits only those three test classes, and the summaries show the waits now use canChangePrompts, runFinished-style conditions, and verify(timeout). The title matches the main change.

Full details: Description check

Explanation

The description explains the failing tests, the root cause (tests stopped waiting before the in-flight flag was cleared or before the title request finished), the fix in each of the three test classes, and the verification performed. It matches the changeset closely.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit checks the run is done,
Then waits for titles, one by one.
No idle state can fool its ear,
It waits until the path is clear.
It hops away with tests in tune.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 48214618-cdbd-4e11-8f3e-8947934e6737
Commits

Reviewing files that changed from the base of the PR and between 13c36ee and 022ccc2.

Files selected for processing: 3

  • plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModelCompactionTest.kt
  • plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModelPromptEditTest.kt
  • plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModelTitleQueueTest.kt

Included review availability: This review used your included allowance. 1 included review remains after this review. Your included PR review attempts over the past 7 days set your current allowance at 2 reviews per hour.

@davidschachterADFA

Copy link
Copy Markdown
Contributor Author

Thanks. On the three risks in the summary:

  • WAIT_MS: the new waits use the same 5 s bound as every other wait in these three classes. A wait returns as soon as its condition holds, so a passing run is no slower. On 2 cores, the slowest setup tried, the full suite passed 5 of 5 runs.
  • Results not verified here: this repository's CI runs only the toolchain check, not the AI-Core unit tests, so the runs were local (summarized in the description). To repeat them, run taskset -c 0,1 ../../gradlew testDebugUnitTest in plugins/AI-Core. At main, that fails now and then in these classes; at 022ccc2, it passed every run.
  • ADFA-6767 | Put prompt actions beside the bubble and add a full-screen chat #154: the ChatViewModelPromptEditTest lines match ADFA-6767 | Put prompt actions beside the bubble and add a full-screen chat #154's exactly, and git merges identical changes without a conflict. Whichever PR merges first, that file's change in the other becomes a no-op.

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