Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(ui): settle cancelled swarm cards

Zigen Han31e0c08256b1
diff --git a/frontend/src/lib/__tests__/swarmStatus.test.ts b/frontend/src/lib/__tests__/swarmStatus.test.ts
index 2037be092..c8450eb2c 100644
--- a/frontend/src/lib/__tests__/swarmStatus.test.ts
+++ b/frontend/src/lib/__tests__/swarmStatus.test.ts
@@ -2,6 +2,7 @@ import {
applySwarmEvent,
buildSwarmStatusFromStarted,
buildSwarmStatusFromToolResultPreview,
+ stopSwarmRun,
} from "../swarmStatus";
describe("swarmStatus server timestamps", () => {
@@ -118,3 +119,51 @@ describe("swarmStatus cancellation events", () => {
expect(cancelled.agents[0].status).toBe("cancelled");
});
});
+
+describe("swarmStatus user stop", () => {
+ const runningRun = () => {
+ const initial = buildSwarmStatusFromStarted({
+ run_id: "run-stop",
+ status: "running",
+ agents: [{ id: "analyst" }, { id: "trader" }],
+ tasks: [
+ { id: "task-1", agent_id: "analyst", status: "in_progress" },
+ { id: "task-2", agent_id: "trader", status: "completed" },
+ ],
+ })!;
+ return initial;
+ };
+
+ it("settles the run and its in-flight workers as cancelled", () => {
+ const stopped = stopSwarmRun(runningRun(), 1_700_000_005_000);
+
+ expect(stopped.status).toBe("cancelled");
+ expect(stopped.completedAt).toBe(1_700_000_005_000);
+ expect(stopped.agents.map((agent) => agent.status)).toEqual([
+ "cancelled",
+ "done",
+ ]);
+ });
+
+ it("ignores events that were already in flight when the user pressed stop", () => {
+ const stopped = stopSwarmRun(runningRun(), 1_700_000_005_000);
+ const late = applySwarmEvent(stopped, {
+ type: "run_completed",
+ timestamp: "2026-08-22T09:00:09Z",
+ data: { status: "completed" },
+ });
+
+ // A late completion must not repaint a stopped card as a finished run.
+ expect(late.status).toBe("cancelled");
+ expect(late.completedAt).toBe(1_700_000_005_000);
+ });
+
+ it("still lets a cancelled run report its own cancellation event", () => {
+ const stopped = stopSwarmRun(runningRun(), 1_700_000_005_000);
+ const echoed = applySwarmEvent(stopped, {
+ type: "run_completed",
+ data: { status: "cancelled" },
+ });
+ expect(echoed.status).toBe("cancelled");
+ });
+});
diff --git a/frontend/src/lib/swarmStatus.ts b/frontend/src/lib/swarmStatus.ts
index f08561923..fc8179265 100644
--- a/frontend/src/lib/swarmStatus.ts
+++ b/frontend/src/lib/swarmStatus.ts
@@ -188,7 +188,31 @@ export function buildSwarmStatusFromStarted(data: AnyRecord): SwarmRunStatus | n
};
}
+/**
+ * Mark a run as stopped by the user. The cancel request reaches the runtime
+ * asynchronously, so the card is settled locally first — otherwise it keeps
+ * showing "running" until the last worker notices the flag.
+ */
+export function stopSwarmRun(status: SwarmRunStatus, at = Date.now()): SwarmRunStatus {
+ return {
+ ...status,
+ status: "cancelled",
+ completedAt: status.completedAt ?? at,
+ agents: status.agents.map((agent) => (
+ agent.status === "running" || agent.status === "waiting"
+ ? { ...agent, status: "cancelled" }
+ : agent
+ )),
+ };
+}
+
export function applySwarmEvent(current: SwarmRunStatus, rawEvent: unknown): SwarmRunStatus {
+ // A stopped run is terminal. Events already in flight when the user pressed
+ // stop (a worker finishing its iteration, the runtime's own run_completed)
+ // arrive afterwards and would repaint the card as running or completed, so
+ // they are dropped rather than applied.
+ if (current.status === "cancelled") return current;
+
const event = asRecord(rawEvent);
const data = asRecord(event.data);
const type = asString(event.type);
diff --git a/frontend/src/pages/Agent.tsx b/frontend/src/pages/Agent.tsx
index 6ab9f10b1..cef7c8cd7 100644
--- a/frontend/src/pages/Agent.tsx
+++ b/frontend/src/pages/Agent.tsx
@@ -46,6 +46,7 @@ import {
buildSwarmStatusFromStarted,
buildSwarmStatusFromToolResultPreview,
mergeSwarmRunUsage,
+ stopSwarmRun,
} from "@/lib/swarmStatus";
import { buildToolTimelineMessages } from "@/pages/agentToolTimeline";
@@ -1561,6 +1562,20 @@ export function Agent() {
}
try {
await api.cancelSession(sessionId);
+ // Cancelling the session only stops the ReAct loop; a swarm tool call that
+ // already returned keeps its workers running. Tell the swarm endpoint too
+ // and settle the cards locally, so the user sees "cancelled" immediately
+ // instead of a card that stays "running" until the last worker notices.
+ const activeSwarmRuns = Object.values(swarmRuns).filter(
+ (run) => run.status === "pending" || run.status === "running",
+ );
+ for (const run of activeSwarmRuns) {
+ act().updateSwarmStatus(run.runId, stopSwarmRun);
+ void api.cancelSwarmRun(run.runId).catch(() => {
+ // Already finished or unknown to this runtime — the local status is
+ // what the card shows either way.
+ });
+ }
flushPendingStreamUpdate();
const partialAnswer = act().streamingText;
const stoppedActivity = archiveActivity("stopped");
@@ -1578,6 +1593,7 @@ export function Agent() {
flushPendingStreamUpdate,
persistPartialAnswer,
sessionId,
+ swarmRuns,
t,
]);