Skip to content

Add Cancelled phase for task cancellation#1274

Open
j-bennet wants to merge 3 commits into
kelos-dev:mainfrom
datagravity-ai:feat/task-cancel
Open

Add Cancelled phase for task cancellation#1274
j-bennet wants to merge 3 commits into
kelos-dev:mainfrom
datagravity-ai:feat/task-cancel

Conversation

@j-bennet

@j-bennet j-bennet commented Jun 3, 2026

Copy link
Copy Markdown

What type of PR is this?

/kind feature

What this PR does / why we need it:

Adds a Cancelled terminal phase to Tasks, enabling clean cancellation of running or pending tasks without losing audit trail (unlike deletion).

Changes:

  • New TaskPhaseCancelled phase constant and CancelledBy field in TaskStatus
  • Task controller: deletes Job on cancellation, releases branch locks, handles TTL, fails dependent tasks
  • kelos cancel task <name> CLI command
  • All terminal-phase checks updated: spawner, taskspawner controller, CLI (get/logs), reporting (GitHub comments, Check Runs, Slack)

Which issue(s) this PR is related to:

Ref #765

This implements the foundation (Cancelled phase, controller handling, CLI) from #765. The spawner-driven cancellationPolicy (onSourceRemoved, onRetrigger, gracePeriodSeconds) is deferred to a follow-up PR.

Special notes for your reviewer:

  • The metric taskCompletedTotal fires when the Job is deleted (first reconcile) or when no Job was ever created (task cancelled while Waiting). Subsequent reconciles of the same Cancelled task do not re-increment.
  • Session runner cancellation awareness (for persistent execution mode) is not included — it will come in a follow-up once persistent mode lands upstream.

Does this PR introduce a user-facing change?

Add Cancelled terminal phase for Tasks and `kelos cancel task` CLI command, enabling clean cancellation with audit trail via the new CancelledBy status field.

Summary by cubic

Adds a Cancelled terminal phase to Tasks and a kelos cancel task <name> command so users can stop running or pending work without deleting it, with an audit trail via status.cancelledBy. Controllers, spawners, CLI, and reporting now treat Cancelled as terminal and clean up resources.

  • New Features

    • New Cancelled phase and status.cancelledBy.
    • kelos cancel task <name> sets phase/message/completion time, retries on conflict, and exits early if already terminal to avoid misleading output.
    • Controller deletes the Job, releases branch locks, applies TTL cleanup, and fails dependents if a dependency is Cancelled.
    • Spawner/taskspawner exclude Cancelled from active-task counts and retrigger logic.
    • CLI get --phase includes Cancelled; logs treat it as terminal.
    • Reporting adds GitHub comments, Check Runs, and Slack messages for Cancelled; caches clear on terminal.
    • Metrics record Cancelled once per task.
  • Migration

Written for commit 7513929. Summary will update on new commits.

Review in cubic

Add TaskPhaseCancelled as a terminal phase and CancelledBy field in
TaskStatus. When a task is set to Cancelled, the controller deletes its
Job, releases branch locks, and handles TTL cleanup. Dependent tasks
that reference a Cancelled dependency are failed immediately.

Add `kelos cancel task <name>` CLI command that patches a task's status
to Cancelled with cancelledBy=user.

Update all terminal-phase checks across spawner, taskspawner controller,
CLI (get --phase, logs), and reporting (GitHub comments, Check Runs,
Slack) to include the new Cancelled phase.

Ref: kelos-dev#765

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

3 issues found across 15 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="internal/cli/cancel.go">

<violation number="1" location="internal/cli/cancel.go:68">
P1: CLI `cancel task` writes to status subresource but no end-user RBAC grants `tasks/status` update</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread internal/cli/cancel.go Outdated
task.Status.Message = "Cancelled by user"
task.Status.CancelledBy = "user"
task.Status.CompletionTime = &now
if err := cl.Status().Update(ctx, &task); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1: CLI cancel task writes to status subresource but no end-user RBAC grants tasks/status update

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At internal/cli/cancel.go, line 68:

<comment>CLI `cancel task` writes to status subresource but no end-user RBAC grants `tasks/status` update</comment>

<file context>
@@ -0,0 +1,78 @@
+			task.Status.Message = "Cancelled by user"
+			task.Status.CancelledBy = "user"
+			task.Status.CompletionTime = &now
+			if err := cl.Status().Update(ctx, &task); err != nil {
+				return fmt.Errorf("cancelling task %s: %w", taskName, err)
+			}
</file context>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 48f6592 — wrapped in retry.RetryOnConflict.

Comment thread internal/controller/cancel_test.go
Comment thread internal/cli/cancel.go Outdated
- Use apierrors.IsNotFound for strict Job deletion assertion in test
- Add retry.RetryOnConflict to CLI cancel status update for conflict
  resilience

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="internal/cli/cancel.go">

<violation number="1" location="internal/cli/cancel.go:68">
P1: CLI `cancel task` writes to status subresource but no end-user RBAC grants `tasks/status` update</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread internal/cli/cancel.go
Move the already-terminal check before the retry loop so it returns
early without printing the misleading "cancelled" success message.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature Categorizes issue or PR as related to a new feature needs-actor needs-priority needs-triage release-note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant