← Back to Pull requests

fix(agent): cancel late model stream subscriptions after the run stopped (issue #276) #338

Closed
unknown closed this pull request without merging on Oct 30, 2026 15:45 UTC
View on GitHub
unknown unknown commented on Oct 30, 2026 10:29 UTC
Author

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_VALUE from 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): onLoss fires while the slot is still empty, and the late onSubscribe used to request anyway.

Does this pull request fix one issue?

Fixes #276

Describe how you did it

  • onSubscribe now checks cancelled.get() || future.isCancelled() after winning the slot CAS; when the run is over it returns the slot and cancels the subscription without requesting. The future.isCancelled() disjunct covers the control-stopped path, where the run-level flag is not set.

  • cancel() and the onLoss callback now take the subscription with getAndSet(null), so ownership transfer is atomic: the subscription is cancelled exactly once and a later slot cleanup cannot erase it.

  • No sentinel subscription: the monotone cancelled flag answers "may this run start?", the slot reference answers "who cancels?", and the cleanup keeps its existing shape.

  • The remaining interleaving (guard read wins, cancellation lands before the cancel write) may still issue one request; this is allowed by the reactive-streams contract (RS 3.6 is the publisher's responsibility) and is not locked away.
  • How was it tested / verified?

    New offline ReActStreamSubscriptionCancellationTest with 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, one Finished, 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() before onSubscribe → no request, one cancel.

    Red before the fix: Tests run: 5, Failures: 3. Green after: 5/5; argi-agent-core 103/103; checkstyle passes; downstream argi-agent-framework regression shows no new failures.

    Additional notes

  • No public API, event, configuration or persistence format changes.

  • Scope is the standalone ReAct model stream only; tool-operation cancellation and the A2A transport cancellation (#277) are untouched.
  • Diff: 323 lines (+242 / -81) across 4 files Reviewer: yuluo-yx Review duration: 315m
    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
    0.42
    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%
    60%
    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.346
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #338"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 42.0% (MEDIUM) · Higher = larger blast radius
    42.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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 16.6% · Safe to merge
    16.6%