feat(adk): implement cancel mechanism for ChatModelAgent#797
Merged
hi-pender merged 24 commits intofeat/agent_turn_loopfrom Feb 24, 2026
Merged
feat(adk): implement cancel mechanism for ChatModelAgent#797hi-pender merged 24 commits intofeat/agent_turn_loopfrom
hi-pender merged 24 commits intofeat/agent_turn_loopfrom
Conversation
- Add comments for exported functions (WithCancelMode, WithCancelTimeout, WithPreemptive, etc.) - Refactor cancelableTool to reduce cyclomatic complexity (43 -> smaller functions) - Fix file formatting (remove trailing blank lines in react.go)
Replace atomic.Int32 and atomic.Bool (Go 1.19+) with int32 and atomic functions for Go 1.18 compatibility: - atomic.Int32 -> int32 + atomic.AddInt32/LoadInt32 - atomic.Bool -> int32 + atomic.StoreInt32/LoadInt32
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## feat/agent_turn_loop #797 +/- ##
=======================================================
Coverage ? 79.52%
=======================================================
Files ? 148
Lines ? 16261
Branches ? 0
=======================================================
Hits ? 12931
Misses ? 2367
Partials ? 963 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Remove TestChatModelAgentImplementsCancellableRun (replaced by compile-time assertion) - Fix incorrect variable reference (slowTool -> st) - Remove incorrect assertion in CancelWithCheckpoint test
Cancel should produce Interrupted event, not Err event
- TestRunWithCancel_Streaming: test cancel with EnableStreaming=true - CancelImmediate_DuringModelStream - CancelAfterToolCall_Streaming - TestResumeWithCancel: test checkpoint + cancel workflow - RunWithCancel_ThenResumeWithCancel: first cancel, then resume normally - ResumeWithCancel_ThenCancel: first cancel, then resume and cancel again
Create new agent and runner instances for resume instead of modifying the original slowModel fields, which could be read by background goroutines.
Explain that when Cancel is triggered, the inner model's Generate/Stream continues in a background goroutine, so we must create new instances for Resume to avoid concurrent read/write on model fields.
- Run now calls runWithCancel(withCancel=false) - RunWithCancel calls runWithCancel(withCancel=true) - Resume now calls resumeWithCancel(withCancel=false) - ResumeWithCancel calls resumeWithCancel(withCancel=true) - Remove duplicate resume() method
meguminnnnnnnnn
approved these changes
Feb 24, 2026
hi-pender
added a commit
that referenced
this pull request
Feb 24, 2026
hi-pender
added a commit
that referenced
this pull request
Feb 27, 2026
meguminnnnnnnnn
pushed a commit
that referenced
this pull request
Feb 28, 2026
hi-pender
added a commit
that referenced
this pull request
Mar 3, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Mar 4, 2026
Closed
shentongmartin
pushed a commit
that referenced
this pull request
Mar 5, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Mar 19, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Mar 23, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Mar 30, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Apr 7, 2026
JonXSnow
pushed a commit
that referenced
this pull request
Apr 7, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Apr 8, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Apr 8, 2026
mrh997
pushed a commit
that referenced
this pull request
Apr 10, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Apr 13, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Apr 14, 2026
N3kox
pushed a commit
that referenced
this pull request
Apr 14, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Apr 14, 2026
shentongmartin
pushed a commit
that referenced
this pull request
Apr 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TurnLoop Cancel & Checkpoint Integration
Overview
This PR integrates the Cancel and Checkpoint mechanisms into TurnLoop, enabling:
WithCancelpatternWithTurnLoopResumeBackground
Business Scenarios
Design Goals
Architecture
Layer Structure
Signal Flow
Key Changes
1. TurnLoop Cancel Support
New API: WithCancel
Usage Example
2. TurnLoop Checkpoint Support
New Run Option: WithTurnLoopResume
TurnLoopConfig.Store
TurnLoopInterruptError
3. ChatModelAgent No-Tools Mode Fix
Issue: When ChatModelAgent has no tools configured,
buildNoToolsRunFuncwas ignoring thecancelSigparameter, causing Cancel mechanism to not work.Fix:
wrapModelForCancelablewhencancelSigis providedcompose.Interrupterror and convert to properAgentEventSignal Handling in TurnLoop
State Machine
Signal Priority in select
Usage Examples
1. Basic Cancel with Checkpoint
2. Resume from Checkpoint
3. Internal Tool Interrupt with Checkpoint
Test Coverage
New Test Cases
TestTurnLoop_ExternalCancel_WithStoreTestTurnLoop_InternalToolInterrupt_WithCheckpoint_ThenResumeTest Scenarios Covered
External Cancel Flow:
Internal Tool Interrupt Flow:
Design Decisions
WithCancelpatternAction.InterruptedMigration Notes
Breaking Changes
None. All changes are additive.
New Dependencies
TurnLoopConfig.Store: Optional field, no changes required for existing codeWithTurnLoopResume: New option, existing Run() calls unaffectedRelated Documents