← Back to Pull requests

fix(scheduling): make ScheduledAgentTask start/stop atomic state transitions (issue #215) #370

Closed
unknown closed this pull request without merging on Nov 22, 2026 13:56 UTC
View on GitHub
unknown unknown commented on Nov 15, 2026 03:34 UTC
Author

Describe what this PR does / why we need it

Issue #215 reports that ScheduledAgentTask.start/stop compose their lifecycle from two independent volatile flags with no atomic transition. This PR replaces the flag pair with a single CAS-able lifecycle state and closes all four windows it exposes, with deterministic latch-driven regression tests.

Root cause

Four concrete defects fall out of the flag pair. Two concurrent starts can both pass the started guard and each publish a schedule, leaving two live schedulings for one task. A stop racing with an in-flight start can finish before the future is published, so the late future is never cancelled and keeps triggering. A stop followed by a start succeeds: stopped plays no role in the start guard, resurrecting a stopped task. And a start whose scheduling throws leaves the task registered forever, since the registration happens in the constructor but nothing releases it on failure.

The fix

The task now holds one AtomicReference over a package-private ScheduleState (NEW, STARTING, RUNNING, STOPPING, STOPPED, FAILED). start() claims NEW → STARTING up front, so a concurrent or repeated start is rejected while the first is still publishing the schedule. After the schedule is published, STARTING → RUNNING must win; if stop() took the state during the window, the just-published future is cancelled instead of left live and the start fails. A scheduling failure moves to FAILED, releases the registration, and propagates the original exception, so a failed start strands nothing. stop() funnels through a single-winner transition to STOPPING — concurrent and repeated stops are no-ops — then cancels the future, unregisters, notifies, and lands on STOPPED. The stop semantics are unchanged: cancel(false) only, so in-flight executions are still not interrupted. No new listener events and no public API growth; isStarted/isStopped keep their previous observable semantics (started history is retained after stop).

Testing

The new ScheduledAgentTaskAtomicStartStopTest gates the scheduler publication with latches for deterministic interleaving: a first start blocked inside the publication window makes a concurrent second start fail with exactly one schedule published (red before: two schedules, no exception); a start after stop is rejected with no new schedule (red before: it scheduled again); a start whose scheduling throws unregisters and propagates (red before: registration left behind); a stop during the STARTING window cancels the late-published future and ends STOPPED (red before: the future stayed live and start succeeded); and the full state walk NEW → STARTING → RUNNING → STOPPING → STOPPED is asserted through a package-private observation point. The scheduling package is 19/19 green with the #214/#216 regressions untouched, checkstyle and spring-javaformat are clean. The module's 8 pre-existing failures on this baseline (skills prompt template, file store Windows path, prompt template line endings) are unrelated and unchanged.

Fixes #215

Diff: 383 lines (+287 / -96) across 4 files Reviewer: yuluo-yx Review duration: 10701m
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.43) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 383 lines diff across 4 files
0.43
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
56%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.356
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #370"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 56.8% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 43.0% (MEDIUM) · Higher = larger blast radius
43.0%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 56.8% · Lower = rushed or shallow review
56.8%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 18.6% · Safe to merge
18.6%