fix(agent): cancel late model stream subscriptions after the run stopped (issue #276) #338
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 323 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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.346
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #338"
general
weight: 0.35
yuluo-yx
Describe what this PR does / why we need it
Fixes #276: the standalone ReAct agent registered a model stream subscription and immediately requested
Long.MAX_VALUEfrom it. Cancellation only reached a subscription that was already parked in the slot, so a subscription that arrived after the run was cancelled still started the upstream model call — a stopped run kept spending tokens and holding a connection. The same window exists when the execution control is stopped before the stream is created (deadline/ownership loss):onLossfires while the slot is still empty, and the lateonSubscribeused to request anyway.Does this pull request fix one issue?
Fixes #276
Describe how you did it
onSubscribenow checkscancelled.get() || future.isCancelled()after winning the slot CAS; when the run is over it returns the slot and cancels the subscription without requesting. Thefuture.isCancelled()disjunct covers the control-stopped path, where the run-level flag is not set.cancel()and theonLosscallback now take the subscription withgetAndSet(null), so ownership transfer is atomic: the subscription is cancelled exactly once and a later slot cleanup cannot erase it.cancelledflag answers "may this run start?", the slot reference answers "who cancels?", and the cleanup keeps its existing shape.How was it tested / verified?
New offline
ReActStreamSubscriptionCancellationTestwith a deferred publisher and a counting subscription:1. Cancel before
onSubscribe→requests=0, cancels=1(the issue's repro);2. Normal streaming completion →
requests=1, cancels=0, oneFinished,onComplete;3. Two streaming turns → one request per turn, no cancels (guards the slot cleanup);
4. Cancel during the second turn → only the active subscription is cancelled;
5.
context.control().cancel()beforeonSubscribe→ no request, one cancel.Red before the fix:
Tests run: 5, Failures: 3. Green after:5/5;argi-agent-core103/103; checkstyle passes; downstreamargi-agent-frameworkregression shows no new failures.Additional notes