Repository navigation
fix(go-agent): wire task cancellation to runner - #17406
Harry-Hz-Zhang wants to merge 0 commit into
Conversation
📝 WalkthroughWalkthroughCanvas task IDs now use the canvas ID, and an authenticated task-cancellation endpoint forwards cancellation through the agent service to the runner. Per-canvas cancellation state tracking is removed, with coverage added for task ID assignment. ChangesTask cancellation flow
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant AgentHandler
participant AgentService
participant Runner
Client->>AgentHandler: POST /api/v1/tasks/:task_id/cancel
AgentHandler->>AgentService: CancelAgent(context, user.ID, task_id)
AgentService->>Runner: Cancel(canvasID)
Runner-->>AgentService: cancellation result
AgentService-->>AgentHandler: service result
AgentHandler-->>Client: success response
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
internal/service/agent.go (1)
1950-1957: 🩺 Stability & Availability | 🔵 TrivialVerify process-local cancellation before multi-replica deployment.
Runner.Cancelonly closes the in-memory cancellation channel on the process that owns the run. If the cancel request reaches another replica, this method returns success while the original run continues. Confirm sticky routing or add shared cancellation signaling before relying on this endpoint across replicas.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/service/agent.go` around lines 1950 - 1957, Update CancelAgent and the cancellation flow around Runner.Cancel to ensure cancellation reaches the process owning the run when requests may hit different replicas. Verify and enforce sticky routing, or introduce a shared cancellation signal that the owning runner observes before returning success; do not rely solely on the process-local cancellation channel.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/agent/canvas/runner_task_test.go`:
- Around line 26-33: Update the test around the callback passed to Runner.Run so
a runner failure cannot leave the taskIDs receive blocked. Ensure the callback
communicates completion or failure through a mechanism that is closed or
signaled when the callback panics, while preserving validation that the captured
__task_id__ equals "canvas-1".
---
Nitpick comments:
In `@internal/service/agent.go`:
- Around line 1950-1957: Update CancelAgent and the cancellation flow around
Runner.Cancel to ensure cancellation reaches the process owning the run when
requests may hit different replicas. Verify and enforce sticky routing, or
introduce a shared cancellation signal that the owning runner observes before
returning success; do not rely solely on the process-local cancellation channel.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 56182fb7-2dfa-4d27-863b-be50fa58ff68
📒 Files selected for processing (5)
internal/agent/canvas/runner.gointernal/agent/canvas/runner_task_test.gointernal/handler/agent.gointernal/router/router.gointernal/service/agent.go
| taskIDs := make(chan string, 1) | ||
| events := runner.Run(context.Background(), func(_ context.Context, root map[string]any) (*CanvasState, error) { | ||
| taskIDs <- root["__task_id__"].(string) | ||
| return nil, nil | ||
| }, "canvas-1", "session-1", nil, map[string]any{"version_id": "version-1"}) | ||
|
|
||
| if taskID := <-taskIDs; taskID != "canvas-1" { | ||
| t.Fatalf("task_id = %q, want canvas-1", taskID) |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Prevent this test from hanging on runner failures.
If the type assertion in the runner goroutine panics, Runner.Run recovers and closes events, but taskIDs is never written; line 32 then blocks indefinitely.
Proposed fix
import (
"context"
"testing"
+ "time"
)
- if taskID := <-taskIDs; taskID != "canvas-1" {
- t.Fatalf("task_id = %q, want canvas-1", taskID)
+ select {
+ case taskID := <-taskIDs:
+ if taskID != "canvas-1" {
+ t.Fatalf("task_id = %q, want canvas-1", taskID)
+ }
+ case <-time.After(time.Second):
+ t.Fatal("runner did not invoke RunFunc")
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| taskIDs := make(chan string, 1) | |
| events := runner.Run(context.Background(), func(_ context.Context, root map[string]any) (*CanvasState, error) { | |
| taskIDs <- root["__task_id__"].(string) | |
| return nil, nil | |
| }, "canvas-1", "session-1", nil, map[string]any{"version_id": "version-1"}) | |
| if taskID := <-taskIDs; taskID != "canvas-1" { | |
| t.Fatalf("task_id = %q, want canvas-1", taskID) | |
| import ( | |
| "context" | |
| "testing" | |
| "time" | |
| ) | |
| taskIDs := make(chan string, 1) | |
| events := runner.Run(context.Background(), func(_ context.Context, root map[string]any) (*CanvasState, error) { | |
| taskIDs <- root["__task_id__"].(string) | |
| return nil, nil | |
| }, "canvas-1", "session-1", nil, map[string]any{"version_id": "version-1"}) | |
| select { | |
| case taskID := <-taskIDs: | |
| if taskID != "canvas-1" { | |
| t.Fatalf("task_id = %q, want canvas-1", taskID) | |
| } | |
| case <-time.After(time.Second): | |
| t.Fatal("runner did not invoke RunFunc") | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/agent/canvas/runner_task_test.go` around lines 26 - 33, Update the
test around the callback passed to Runner.Run so a runner failure cannot leave
the taskIDs receive blocked. Ensure the callback communicates completion or
failure through a mechanism that is closed or signaled when the callback panics,
while preserving validation that the captured __task_id__ equals "canvas-1".
87ed5e8 to
cd846cc
Compare
Summary
Testing
bash build.sh --go