Skip to content

test(chat): make durable-cron scheduled-wake tests wait robustly#847

Merged
arul28 merged 1 commit into
mainfrom
fix/agentchat-cron-test-flake
Jul 17, 2026
Merged

test(chat): make durable-cron scheduled-wake tests wait robustly#847
arul28 merged 1 commit into
mainfrom
fix/agentchat-cron-test-flake

Conversation

@arul28

@arul28 arul28 commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Problem

test-desktop (8) was failing deterministically on main (blocking the v1.2.30 release gate, which requires a green ci-pass on the tagged commit). Two agentChatService.test.ts cron tests under hasActiveWorkloads failed:

  • does not create task-id scheduled rows for ambiguous parentless cron runs
  • claims a native cron when unrelated idle output already opened the turn

Root cause

Both tests polled for an async scheduled-wake user_message by advancing fake timers only 100 × 1ms. The durable-scheduling pipeline (#844) needs more timer+microtask cycles than that to emit the event, so the tests passed only on fast/unloaded runners. They began failing deterministically once an unrelated merge (#846) reshuffled the 8-way shard split onto a slower shard. Verified: running the whole file in isolation reproduced 2 failed / 671 passed every time; same test+impl was green on main before the shard reshuffle.

Fix

Replace the fragile fixed-iteration loops with a waitForFakeTimerCondition helper that advances fake time up to 10s while flushing microtasks until the predicate holds. Test-only — no product code changed, no assertions weakened or skipped.

Verification

  • Full file run 10× in a row: 673 passed (673) every time.
  • npm --prefix apps/desktop run typecheck: pass.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Improved reliability of chat agent tests involving cron scheduling, idle activity, and foreground sends.
    • Added deterministic waiting utilities for asynchronous state changes when using simulated timers.
    • Updated tests to verify scheduled wake events and agent responses without relying on repeated manual timer advances.

Greptile Summary

This PR makes durable-cron scheduled-wake tests wait more reliably. The main changes are:

  • Adds a fake-timer condition helper with a bounded 10s timeout.
  • Adds a helper for waiting on promises while advancing fake timers.
  • Updates scheduled-work tests to wait for specific emitted events instead of fixed 100ms polling loops.

Confidence Score: 5/5

Safe to merge with minimal risk.

The change is confined to tests and preserves the existing assertions while replacing fragile fixed polling with bounded fake-timer waits.

No files require special attention.

T-Rex T-Rex Logs

What T-Rex did

  • The cron-focused Vitest run completed, showing that 1 file passed and 2 targeted tests passed.
  • The desktop TypeScript type-check step was executed but was killed with exit code 137, so type-check completion could not be proven by a TypeScript diagnostic.
  • The results indicate the relevant changed test behavior executed successfully despite the kill signal during type-checking, as reflected by the Vitest results.

View all artifacts

T-Rex Ran code and verified through T-Rex

Important Files Changed

Filename Overview
apps/desktop/src/main/services/chat/agentChatService.test.ts Adds fake-timer wait helpers and updates durable-cron tests to wait for scheduled-wake events without fixed short polling loops; no issues found.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
participant Test as Durable-cron test
participant Helper as waitForFakeTimerCondition
participant Timers as Vitest fake timers
participant Service as AgentChatService events

Test->>Service: start session turn / release cron gate
Test->>Helper: wait for expected event predicate
loop until predicate true or 10s fake time
    Helper->>Service: evaluate emitted events
    alt event not emitted yet
        Helper->>Timers: advanceTimersByTimeAsync(1)
        Helper->>Helper: flush microtasks
    else event emitted
        Helper-->>Test: return
    end
end
Test->>Service: assert scheduled-work / scheduled-wake behavior
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
participant Test as Durable-cron test
participant Helper as waitForFakeTimerCondition
participant Timers as Vitest fake timers
participant Service as AgentChatService events

Test->>Service: start session turn / release cron gate
Test->>Helper: wait for expected event predicate
loop until predicate true or 10s fake time
    Helper->>Service: evaluate emitted events
    alt event not emitted yet
        Helper->>Timers: advanceTimersByTimeAsync(1)
        Helper->>Helper: flush microtasks
    else event emitted
        Helper-->>Test: return
    end
end
Test->>Service: assert scheduled-work / scheduled-wake behavior
Loading

Reviews (1): Last reviewed commit: "test(chat): make durable-cron scheduled-..." | Re-trigger Greptile

Two hasActiveWorkloads cron tests polled for an async scheduled-wake
user_message by advancing fake timers only 100×1ms. The durable-
scheduling pipeline needs more timer+microtask cycles than that to emit
the event, so the tests passed only on fast/unloaded runners and failed
deterministically elsewhere (surfaced when an unrelated merge reshuffled
the 8-way shard split). Replace the fragile fixed-iteration loops with a
waitForFakeTimerCondition helper that advances fake time up to 10s while
flushing microtasks until the predicate holds. Test-only; no assertions
weakened, no product code changed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vercel

vercel Bot commented Jul 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
ade Ignored Ignored Jul 17, 2026 4:58pm

@arul28

arul28 commented Jul 17, 2026

Copy link
Copy Markdown
Owner Author

@copilot review but do not make fixes

Test-only fix unblocking the v1.2.30 release gate (green ci-pass on the tagged commit). Robustifies two fake-timer cron-scheduling waits; verified 10× green + typecheck.

@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ee3bb8b1-dc3a-4fad-bbf0-fc5f2048973b

📥 Commits

Reviewing files that changed from the base of the PR and between 5e08bcd and 98452ef.

📒 Files selected for processing (1)
  • apps/desktop/src/main/services/chat/agentChatService.test.ts

📝 Walkthrough

Walkthrough

Changes

The test suite adds deterministic fake-timer helpers and applies them to Claude foreground sends, ambiguous cron handling, idle activity, and scheduled-wake assertions.

Agent chat test timing

Layer / File(s) Summary
Fake-timer wait helpers
apps/desktop/src/main/services/chat/agentChatService.test.ts
Adds helpers for polling fake-timer conditions and waiting for promise settlement.
Foreground send synchronization
apps/desktop/src/main/services/chat/agentChatService.test.ts
Replaces manual timer polling while waiting for the Claude send mock.
Cron and idle event synchronization
apps/desktop/src/main/services/chat/agentChatService.test.ts
Initializes fake timers, coordinates setup turns, and waits for matching cron, idle activity, and scheduled-wake events.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • arul28/ADE#763: Updates the same chat service tests around Claude lifecycle, wakeups, cron, and subagent behavior.
  • arul28/ADE#844: Introduces scheduled and idle behavior covered by these deterministic test waits.

Suggested labels: desktop

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the test-only durable-cron scheduled-wake robustness changes in agentChatService.test.ts.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/agentchat-cron-test-flake

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@arul28
arul28 merged commit 2d7a812 into main Jul 17, 2026
34 checks passed
@arul28
arul28 deleted the fix/agentchat-cron-test-flake branch July 17, 2026 17:12
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