fix: distinguish archived automation runs from cancelled ones (#1860)

This commit is contained in:
Clément Fiere
2026-07-24 10:50:36 +02:00
committed by GitHub
parent 93c6987bf5
commit 62919efbdc
12 changed files with 576 additions and 63 deletions
@@ -0,0 +1,76 @@
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { AutomationRun } from "@/lib/types/automation";
const mockUseAutomationRuns = vi.fn();
vi.mock("@/hooks/domains/settings/use-automation-runs", () => ({
useAutomationRuns: (...args: unknown[]) => mockUseAutomationRuns(...args),
}));
const mockPush = vi.fn();
vi.mock("@/lib/routing/client-router", () => ({
useRouter: () => ({ push: mockPush }),
}));
import { RunsSection } from "./runs-section";
function mkRun(overrides: Partial<AutomationRun> = {}): AutomationRun {
return {
id: "run-1",
automation_id: "auto-1",
trigger_id: "trig-1",
trigger_type: "scheduled",
task_id: "task-1",
status: "task_created",
dedup_key: "",
trigger_data: {},
error_message: "",
created_at: new Date().toISOString(),
...overrides,
};
}
function setup(runs: AutomationRun[]) {
mockUseAutomationRuns.mockReturnValue({
runs,
loading: false,
refresh: vi.fn(),
deleteRun: vi.fn(),
deleteAllRuns: vi.fn(),
});
render(<RunsSection automationId="auto-1" executionMode="task" workspaceId="ws-1" />);
// The runs table only renders once the "Recent Runs" disclosure is expanded.
fireEvent.click(screen.getByText(/Recent Runs/));
}
describe("RunsSection status badges", () => {
afterEach(() => {
cleanup();
vi.clearAllMocks();
});
it("labels an archived-task run as Archived, not Cancelled", () => {
setup([mkRun({ id: "run-archived", status: "archived" })]);
expect(screen.getByText("Archived")).toBeTruthy();
expect(screen.queryByText("Cancelled")).toBeNull();
});
it("labels a genuinely cancelled-task run as Cancelled", () => {
setup([mkRun({ id: "run-cancelled", status: "cancelled" })]);
expect(screen.getByText("Cancelled")).toBeTruthy();
expect(screen.queryByText("Archived")).toBeNull();
});
it("distinguishes archived and cancelled runs side by side", () => {
setup([
mkRun({ id: "run-archived", status: "archived" }),
mkRun({ id: "run-cancelled", status: "cancelled" }),
]);
expect(screen.getByText("Archived")).toBeTruthy();
expect(screen.getByText("Cancelled")).toBeTruthy();
});
});
@@ -37,9 +37,14 @@ const STATUS_BADGE: Record<
succeeded: { variant: "default", label: "Succeeded" },
failed: { variant: "destructive", label: "Failed" },
skipped: { variant: "outline", label: "Skipped" },
// The generating task was archived or no longer exists — its outcome is
// unknown, so this is deliberately distinct from succeeded/failed rather
// than guessing one. See internal/automation.RunStatusCancelled.
// The generating task was archived — via the UI or by the agent itself
// (e.g. an "archive this task" instruction). Distinct from a genuine
// user cancellation: archiving just closes the task out, it doesn't
// mean the run's work was rejected. See internal/automation.RunStatusArchived.
archived: { variant: "outline", label: "Archived" },
// The generating task no longer exists, or its current primary session
// is CANCELLED — a real cancellation, distinct from archived.
// See internal/automation.RunStatusCancelled.
cancelled: { variant: "outline", label: "Cancelled" },
};
+20
View File
@@ -2446,6 +2446,26 @@ export class ApiClient {
task_id: taskId ?? "",
});
}
/**
* Seed a task_sessions row directly via the E2E HTTP endpoint —
* deterministic state seeding, not a substitute for driving a real
* agent stop/resume cycle. Used to assert on session-state-derived
* behavior (e.g. the automation Recent Runs "Cancelled" status, which
* is keyed off the task's *primary* session state, not the task's own
* state). Only works when KANDEV_MOCK_AGENT is active.
*/
async seedAutomationTaskSession(
taskId: string,
state: string,
isPrimary = true,
): Promise<{ id: string; task_id: string; status: string }> {
return this.request("POST", "/api/v1/e2e/task-sessions", {
task_id: taskId,
state,
is_primary: isPrimary,
});
}
}
// --- Jira / Linear mock payload types ---
@@ -358,17 +358,21 @@ test.describe("Automations settings page", () => {
await expect(testPage.getByText("No runs yet")).toBeVisible({ timeout: 5_000 });
});
test("archived task's run shows Cancelled instead of Running", async ({
test("archived task's run shows Archived, cancelled task's run shows Cancelled", async ({
testPage,
seedData,
apiClient,
}) => {
// Regression test: an automation-generated task that gets archived
// (manually, via auto-archive, or via cascade) before its run is
// otherwise finalized used to leave the run stuck at "task_created"
// forever, displayed as "Running" and permanently pinned against
// max_concurrent_runs. See internal/automation.Store.CountActiveRuns /
// ListRuns.
// (manually, via auto-archive, via cascade, or by the agent itself)
// before its run is otherwise finalized used to leave the run stuck at
// "task_created" forever, displayed as "Running" and permanently
// pinned against max_concurrent_runs. It also used to be
// indistinguishable from a genuine user cancellation (the task's
// primary session state going CANCELLED — the signal a real Stop
// leaves behind; the task's own state is untouched by a stop), both
// showing as "Cancelled". See internal/automation.Store.CountActiveRuns
// / ListRuns.
const automation = await apiClient.seedAutomation({
workspaceId: seedData.workspaceId,
name: "Archived Task Run Test",
@@ -381,13 +385,20 @@ test.describe("Automations settings page", () => {
"Archived Automation Task",
{ workflow_id: seedData.workflowId, workflow_step_id: seedData.startStepId },
);
const cancelledTask = await apiClient.createTask(
seedData.workspaceId,
"Cancelled Automation Task",
{ workflow_id: seedData.workflowId, workflow_step_id: seedData.startStepId },
);
const openTask = await apiClient.createTask(seedData.workspaceId, "Open Automation Task", {
workflow_id: seedData.workflowId,
workflow_step_id: seedData.startStepId,
});
await apiClient.seedAutomationRun(automation.id, "task_created", archivedTask.id);
await apiClient.seedAutomationRun(automation.id, "task_created", cancelledTask.id);
await apiClient.seedAutomationRun(automation.id, "task_created", openTask.id);
await apiClient.archiveTask(archivedTask.id);
await apiClient.seedAutomationTaskSession(cancelledTask.id, "CANCELLED");
await testPage.goto(`/settings/workspace/${seedData.workspaceId}/automations/${automation.id}`);
await testPage.getByTestId("automation-editor").waitFor({ state: "visible", timeout: 15_000 });
@@ -401,13 +412,22 @@ test.describe("Automations settings page", () => {
const tbody = testPage.locator("table tbody");
await tbody.waitFor({ state: "visible", timeout: 5_000 });
await expect(tbody.locator("tr")).toHaveCount(2, { timeout: 10_000 });
await expect(tbody.locator("tr")).toHaveCount(3, { timeout: 10_000 });
// The archived task's run is no longer outstanding work.
// The archived task's run is no longer outstanding work, and reads
// "Archived" rather than being conflated with a real cancellation.
const archivedRow = testPage.locator("table tbody tr", {
hasText: archivedTask.id.slice(0, 8),
});
await expect(archivedRow.getByText("Cancelled", { exact: true })).toBeVisible();
await expect(archivedRow.getByText("Archived", { exact: true })).toBeVisible();
// A task whose current (primary) session was genuinely cancelled —
// the signal a real Stop leaves behind — reads "Cancelled", distinct
// from "Archived".
const cancelledRow = testPage.locator("table tbody tr", {
hasText: cancelledTask.id.slice(0, 8),
});
await expect(cancelledRow.getByText("Cancelled", { exact: true })).toBeVisible();
// The still-open task's run is unaffected.
const openRow = testPage.locator("table tbody tr", { hasText: openTask.id.slice(0, 8) });
+1
View File
@@ -8,6 +8,7 @@ export type RunStatus =
| "succeeded"
| "failed"
| "skipped"
| "archived"
| "cancelled";
// ExecutionMode controls whether an automation firing creates a visible