Skip to content

fix(go-agent): wire task cancellation to runner - #17406

Closed
Harry-Hz-Zhang wants to merge 0 commit into
infiniflow:mainfrom
Harry-Hz-Zhang:fix/agent-cancel-task
Closed

Harry-Hz-Zhang wants to merge 0 commit into
infiniflow:mainfrom
Harry-Hz-Zhang:fix/agent-cancel-task

Conversation

@Harry-Hz-Zhang

Copy link
Copy Markdown
Contributor

Summary

  • Add the agent task cancellation route and handler.
  • Route agent cancellation through the canvas runner.
  • Use the canvas ID as the active cancellation task ID.

Testing

  • bash build.sh --go

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jul 27, 2026
@Harry-Hz-Zhang Harry-Hz-Zhang added 🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration labels Jul 27, 2026
@coderabbitai

coderabbitai Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Canvas 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.

Changes

Task cancellation flow

Layer / File(s) Summary
Canvas task identity
internal/agent/canvas/runner.go, internal/agent/canvas/runner_task_test.go
Runner.Run assigns the canvas ID as the task ID, and a test verifies the injected __task_id__ value.
Task cancellation API
internal/service/agent.go, internal/handler/agent.go, internal/router/router.go
Agent cancellation tracking is removed; the authenticated cancel endpoint calls CancelAgent, which invokes runner.Cancel.

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
Loading

Suggested reviewers: jinhai-cn, haruko386, qinling0210

Poem

A bunny hops through task IDs bright,
Canvas names now guide the flight.
A cancel button calls the hare,
The runner stops with careful care.
Tests twitch noses: all is right!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: task cancellation is wired through the runner.
Description check ✅ Passed The description matches the required Summary section and includes useful testing notes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
internal/service/agent.go (1)

1950-1957: 🩺 Stability & Availability | 🔵 Trivial

Verify process-local cancellation before multi-replica deployment.

Runner.Cancel only 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

📥 Commits

Reviewing files that changed from the base of the PR and between 1571abd and 87ed5e8.

📒 Files selected for processing (5)
  • internal/agent/canvas/runner.go
  • internal/agent/canvas/runner_task_test.go
  • internal/handler/agent.go
  • internal/router/router.go
  • internal/service/agent.go

Comment on lines +26 to +33
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 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.

Suggested change
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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant