Merged
Size
M
Change Breakdown
Bug Fix60%
Feature25%
Docs10%
Testing5%
#62506Tasks: allow `openclaw tasks cancel` for CLI runtime (#62419)

CLI tasks now cancellable via openclaw tasks cancel

The `openclaw tasks cancel` command now works for CLI-tracked tasks. Operators can finally clear stuck running tasks that were previously unkillable.

CLI-tracked tasks that got stuck in a running state had nowhere to go. When operators tried to cancel them with openclaw tasks cancel, the command returned "Task runtime does not support cancellation yet" — leaving orphaned rows with no escape hatch. The maintenance command didn't help either, doing nothing for these stale tasks.

The fix restructures cancelTaskById to recognize CLI runtime alongside ACP and subagent runtimes. Instead of attempting to kill a child session (CLI tasks run in the main session, so there's nothing separate to stop), the function now records the cancellation directly in the task registry. The task transitions to a cancelled terminal state and a delivery notification is sent when applicable.

The task listener also received a guardrail: terminal status tasks are now skipped during listener updates, preventing cancelled or completed tasks from being accidentally overwritten back to running.

In the task registry, the cancellation flow now branches by runtime type. ACP and subagent runtimes still kill their respective child sessions. CLI runtime skips the session kill entirely — it just records the cancellation in the registry and returns success.

Unit tests verify that CLI cancellation updates the task to cancelled, does not invoke ACP or subagent hooks, and delivers the expected message when delivery is pending.

View Original GitHub Description

Summary

  • Problem: openclaw tasks cancel returned "Task runtime does not support cancellation yet" for CLI-tracked tasks (for example gateway agent runs), so operators could not clear stuck running rows.
  • Why it matters: CLI tasks can remain running when follow-up bookkeeping never runs; without a working cancel path there was no supported way to mark them terminal.
  • What changed: cancelTaskById treats runtime === "cli" like other local cancellations: record cancelled in the task registry (CLI uses the main session as childSessionKey; there is no separate ACP/subagent session to stop). Docs and changelog updated; unit test covers the path.
  • What did NOT change: Cron and any other non-ACP/subagent/CLI runtimes still return the unsupported message. Maintenance/audit behavior for stale_running is unchanged (audit remains informational).

Change Type

  • Bug fix

Scope

  • Gateway / orchestration

Linked Issue/PR

  • Closes #62419
  • This PR fixes a bug or regression

Root Cause

  • Root cause: cancelTaskById only wired teardown for ACP and subagent runtimes; CLI fell through to the unsupported branch despite using a normal session key.
  • Missing detection / guardrail: Unit tests only covered ACP and subagent cancellation.

Regression Test Plan

  • Coverage: Unit test (src/tasks/task-registry.test.ts) asserts CLI cancel updates the task to cancelled, does not call ACP/subagent hooks, and delivers the expected terminal message when delivery is pending.

User-visible / Behavior Changes

  • openclaw tasks cancel succeeds for active CLI-tracked tasks and sets status to cancelled with operator error text.

Diagram

N/A

Security Impact

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No (registry state only for CLI cancel)
  • Data access scope changed? No

Repro + Verification

Environment

  • Unit test run: pnpm test src/tasks/task-registry.test.ts -t "cancels"

Steps

  1. Create a running CLI task record with childSessionKey set.
  2. Call cancelTaskById.
  3. Expect cancelled: true and no ACP/subagent control calls.

Expected

  • Task transitions to cancelled.

Actual

  • Matches test expectations.

Evidence

  • New unit test for CLI cancel path

Human Verification

  • Verified scenarios: Scoped Vitest for cancel tests in task-registry.test.ts.
  • Edge cases checked: Confirms ACP/subagent mocks are not invoked for CLI.
  • What I did not verify: Full pnpm check / pnpm build (local tree has unrelated TS/build failures on this checkout).

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Risks and Mitigations

  • Risk: Cancelling while a CLI run is still live does not abort the agent run; it only updates task registry state (same class of limitation as other operator-driven terminal updates if completion races).
    • Mitigation: Matches prior behavior for non-CLI paths that only had registry + session kill where applicable; documented in docs/automation/tasks.md.

Made with Cursor

© 2026 · via Gitpulse