Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(cli): make task cancel report unknown and finished tasks instead of claiming success (#4331)

kevincc7e76513ae8
diff --git a/v3/@claude-flow/cli/__tests__/task-cancel.test.ts b/v3/@claude-flow/cli/__tests__/task-cancel.test.ts
new file mode 100644
index 0000000000..832edc0f0a
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/task-cancel.test.ts
@@ -0,0 +1,102 @@
+/**
+ * task_cancel overwrote a finished task's result and completedAt, never
+ * returned the previous status the CLI prints, and `task cancel` treated the
+ * tool's `success: false` answer as a successful cancel (exit 0).
+ */
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { taskTools } from '../src/mcp-tools/task-tools.js';
+import { taskCommand } from '../src/commands/task.js';
+import { output } from '../src/output.js';
+import type { CommandContext } from '../src/types.js';
+
+const tool = (name: string) => {
+ const t = taskTools.find(t => t.name === name);
+ if (!t) throw new Error(`MCP tool not registered: ${name}`);
+ return t;
+};
+
+const cancelCommand = taskCommand.subcommands!.find(c => c.name === 'cancel')!;
+
+type CancelResult = { success: boolean; status?: string; previousStatus?: string; error?: string };
+type StatusResult = { status: string; result: unknown; completedAt: string | null };
+
+describe('task cancel', () => {
+ let dir: string;
+ let prevCwd: string | undefined;
+
+ beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'ruflo-task-cancel-'));
+ prevCwd = process.env.CLAUDE_FLOW_CWD;
+ process.env.CLAUDE_FLOW_CWD = dir;
+ vi.spyOn(output, 'writeln').mockImplementation(() => {});
+ vi.spyOn(output, 'printSuccess').mockImplementation(() => {});
+ vi.spyOn(output, 'printInfo').mockImplementation(() => {});
+ vi.spyOn(output, 'printJson').mockImplementation(() => {});
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ if (prevCwd === undefined) delete process.env.CLAUDE_FLOW_CWD;
+ else process.env.CLAUDE_FLOW_CWD = prevCwd;
+ rmSync(dir, { recursive: true, force: true });
+ });
+
+ const create = async () =>
+ (await tool('task_create').handler({ type: 'feature', description: 'work' }) as { taskId: string }).taskId;
+
+ it('task_cancel returns the status the task had before it was cancelled', async () => {
+ const taskId = await create();
+ const result = await tool('task_cancel').handler({ taskId }) as CancelResult;
+ expect(result.success).toBe(true);
+ expect(result.status).toBe('cancelled');
+ expect(result.previousStatus).toBe('pending');
+ });
+
+ it('task_cancel leaves a completed task and its result alone', async () => {
+ const taskId = await create();
+ await tool('task_complete').handler({ taskId, result: { answer: 42 } });
+ const before = await tool('task_status').handler({ taskId }) as StatusResult;
+
+ const result = await tool('task_cancel').handler({ taskId }) as CancelResult;
+ expect(result.success).toBe(false);
+ expect(result.previousStatus).toBe('completed');
+ expect(result.error).toContain('already completed');
+
+ const after = await tool('task_status').handler({ taskId }) as StatusResult;
+ expect(after.status).toBe('completed');
+ expect(after.result).toEqual({ answer: 42 });
+ expect(after.completedAt).toBe(before.completedAt);
+ });
+
+ it('task cancel prints the previous status', async () => {
+ const taskId = await create();
+ const printInfo = vi.mocked(output.printInfo);
+ const ctx = { args: [taskId], flags: { force: true }, cwd: dir, interactive: false } as unknown as CommandContext;
+ const result = await cancelCommand.action!(ctx);
+ expect(result?.success).toBe(true);
+ expect(printInfo).toHaveBeenCalledWith('Previous status: pending');
+ });
+
+ it('task cancel exits 1 for an unknown task id', async () => {
+ const printError = vi.spyOn(output, 'printError').mockImplementation(() => {});
+ const ctx = { args: ['task-missing'], flags: { force: true }, cwd: dir, interactive: false } as unknown as CommandContext;
+ const result = await cancelCommand.action!(ctx);
+ expect(result?.success).toBe(false);
+ expect(result?.exitCode).toBe(1);
+ expect(printError).toHaveBeenCalledWith('Failed to cancel task task-missing: Task not found');
+ expect(output.printSuccess).not.toHaveBeenCalled();
+ });
+
+ it('task cancel exits 1 for a task that already finished', async () => {
+ const taskId = await create();
+ await tool('task_complete').handler({ taskId });
+ vi.spyOn(output, 'printError').mockImplementation(() => {});
+ const ctx = { args: [taskId], flags: { force: true }, cwd: dir, interactive: false } as unknown as CommandContext;
+ const result = await cancelCommand.action!(ctx);
+ expect(result?.success).toBe(false);
+ expect(result?.exitCode).toBe(1);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/commands/task.ts b/v3/@claude-flow/cli/src/commands/task.ts
index ccdf546e15..29cadd7db3 100644
--- a/v3/@claude-flow/cli/src/commands/task.ts
+++ b/v3/@claude-flow/cli/src/commands/task.ts
@@ -568,15 +568,29 @@ const cancelCommand: Command = {
try {
const result = await callMCPTool<{
+ success: boolean;
taskId: string;
- cancelled: boolean;
- previousStatus: string;
- cancelledAt: string;
+ status?: string;
+ previousStatus?: string;
+ cancelledAt?: string;
+ error?: string;
}>('task_cancel', {
taskId,
reason: reason || 'Cancelled by user via CLI'
});
+ // task_cancel answers an unknown or already-finished task with
+ // success: false instead of throwing; report it instead of printing
+ // "Task ... cancelled" and exiting 0.
+ if (result.success === false) {
+ if (ctx.flags.format === 'json') {
+ output.printJson(result);
+ } else {
+ output.printError(`Failed to cancel task ${taskId}: ${result.error || 'unknown error'}`);
+ }
+ return { success: false, exitCode: 1, data: result };
+ }
+
output.writeln();
output.printSuccess(`Task ${taskId} cancelled`);
output.printInfo(`Previous status: ${result.previousStatus}`);
diff --git a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
index 8e577f952e..4c0c6d6b12 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
@@ -549,6 +549,19 @@ export const taskTools: MCPTool[] = [
const task = store.tasks[taskId];
if (task) {
+ const previousStatus = task.status;
+ // A finished task keeps its outcome: cancelling it used to overwrite a
+ // completed task's result and completedAt with a cancel reason.
+ if (previousStatus === 'completed' || previousStatus === 'failed' || previousStatus === 'cancelled') {
+ return {
+ success: false,
+ taskId,
+ status: previousStatus,
+ previousStatus,
+ error: `Task is already ${previousStatus}`,
+ };
+ }
+
task.status = 'cancelled';
task.completedAt = new Date().toISOString();
task.result = { cancelReason: input.reason || 'Cancelled by user' };
@@ -559,6 +572,7 @@ export const taskTools: MCPTool[] = [
success: true,
taskId: task.taskId,
status: task.status,
+ previousStatus,
cancelledAt: task.completedAt,
};
}