Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix: cascade cancel running topics when task status transitions out of running

Arvin Xu7894a0a28ec5
diff --git a/src/server/routers/lambda/__tests__/integration/task.integration.test.ts b/src/server/routers/lambda/__tests__/integration/task.integration.test.ts
index 7ccc4e921fb..c8d1cae7b5a 100644
--- a/src/server/routers/lambda/__tests__/integration/task.integration.test.ts
+++ b/src/server/routers/lambda/__tests__/integration/task.integration.test.ts
@@ -356,6 +356,49 @@ describe('Task Router Integration', () => {
});
});
+ describe('updateStatus cascade cancels running topics', () => {
+ it('should cancel running topics when task transitions out of running', async () => {
+ const task = await caller.create({
+ assigneeAgentId: testAgentId,
+ instruction: 'Test cascade',
+ });
+
+ // Start running β€” creates a running topic
+ await caller.run({ id: task.data.id });
+
+ // Transition task from running β†’ paused via updateStatus
+ const result = await caller.updateStatus({
+ id: task.data.id,
+ status: 'paused',
+ });
+ expect(result.data.status).toBe('paused');
+
+ // The running topic should have been interrupted
+ expect(mockInterruptTask).toHaveBeenCalledWith({ operationId: 'op_test' });
+
+ // Running again should succeed (no CONFLICT) because the topic was canceled
+ mockExecAgent.mockResolvedValueOnce({
+ operationId: 'op_test_2',
+ success: true,
+ topicId: testTopicId,
+ });
+
+ // Need to set back to a runnable status first
+ await caller.updateStatus({ id: task.data.id, status: 'backlog' });
+ await expect(caller.run({ id: task.data.id })).resolves.toBeDefined();
+ });
+
+ it('should not interrupt topics when task is not currently running', async () => {
+ const task = await caller.create({
+ instruction: 'Test no cascade',
+ });
+
+ // Task is in backlog, transition to paused β€” no topics to cancel
+ await caller.updateStatus({ id: task.data.id, status: 'paused' });
+ expect(mockInterruptTask).not.toHaveBeenCalled();
+ });
+ });
+
describe('heartbeat timeout detection', () => {
it('should auto-detect timeout on detail and pause task', async () => {
const task = await caller.create({
diff --git a/src/server/routers/lambda/task.ts b/src/server/routers/lambda/task.ts
index a6ebd21ef22..fcbcf63ad82 100644
--- a/src/server/routers/lambda/task.ts
+++ b/src/server/routers/lambda/task.ts
@@ -1233,6 +1233,22 @@ export const taskRouter = router({
const model = ctx.taskModel;
const resolved = await resolveOrThrow(model, id);
+ // Cascade: when leaving `running`, cancel all running topics
+ if (resolved.status === 'running' && status !== 'running') {
+ const topics = await ctx.taskTopicModel.findByTaskId(resolved.id);
+ const aiAgentService = new AiAgentService(ctx.serverDB, ctx.userId);
+
+ for (const t of topics) {
+ if (t.status !== 'running') continue;
+ if (t.operationId) {
+ await aiAgentService.interruptTask({ operationId: t.operationId }).catch((err) => {
+ console.error('[task:updateStatus] failed to interrupt topic %s:', t.topicId, err);
+ });
+ }
+ await ctx.taskTopicModel.updateStatus(resolved.id, t.topicId, 'canceled');
+ }
+ }
+
const extra: Record<string, unknown> = {};
if (status === 'running') extra.startedAt = new Date();
if (status === 'completed' || status === 'failed' || status === 'canceled')