🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
✨ feat(task): add an onTaskRunCompleted business slot (#21355)
AmAzing-27c19c055221
diff --git a/apps/server/src/services/taskLifecycle/index.ts b/apps/server/src/services/taskLifecycle/index.tsindex 67fc780cf10..8d3453783d8 100644--- a/apps/server/src/services/taskLifecycle/index.ts+++ b/apps/server/src/services/taskLifecycle/index.ts@@ -33,6 +33,7 @@ import {notifyScheduledTaskFailed,} from '@/business/server/task/notifyScheduledTaskResult';import { notifyTaskRunFailed } from '@/business/server/task/notifyTaskRunFailed';+import { onTaskRunCompleted } from '@/business/server/task/onTaskRunCompleted';import { BriefModel } from '@/database/models/brief';import { GoalModel } from '@/database/models/goal';import { TaskModel } from '@/database/models/task';@@ -331,6 +332,29 @@ export class TaskLifecycleService {),);}++ // 7. Run-completed follow-up through the `@/business` slot (default impl+ // is a no-op), after the post-run transition above. Awaited so it+ // finishes inside this callback, which in production is a queued+ // workflow request; a failure is logged and never fails the lifecycle.+ try {+ await onTaskRunCompleted({+ agentId: currentTask?.assigneeAgentId ?? undefined,+ lastAssistantContent,+ operationId: params.operationId,+ taskId,+ taskIdentifier,+ topicId,+ userId: this.userId,+ workspaceId: this.workspaceId,+ });+ } catch (error) {+ console.error(+ '[TaskLifecycle] run-completed follow-up failed for task=%s (non-fatal):',+ taskIdentifier,+ error,+ );+ }} else if (reason === 'error') {if (topicId)await this.taskTopicModel.updateStatus(diff --git a/apps/server/src/services/taskLifecycle/onTopicComplete.test.ts b/apps/server/src/services/taskLifecycle/onTopicComplete.test.tsindex 5134e443615..a563ff8bad7 100644--- a/apps/server/src/services/taskLifecycle/onTopicComplete.test.ts+++ b/apps/server/src/services/taskLifecycle/onTopicComplete.test.ts@@ -54,6 +54,10 @@ const notifyRunFailed = vi.fn().mockResolvedValue(undefined);vi.mock('@/business/server/task/notifyTaskRunFailed', () => ({notifyTaskRunFailed: (...args: unknown[]) => notifyRunFailed(...args),}));+const runCompleted = vi.fn().mockResolvedValue(undefined);+vi.mock('@/business/server/task/onTaskRunCompleted', () => ({+ onTaskRunCompleted: (...args: unknown[]) => runCompleted(...args),+}));const baseTask = (overrides: Partial<TaskItem> = {}): TaskItem =>({@@ -88,6 +92,7 @@ describe('TaskLifecycleService.onTopicComplete', () => {notifyCompleted.mockReset().mockResolvedValue(undefined);notifyFailed.mockReset().mockResolvedValue(undefined);notifyRunFailed.mockReset().mockResolvedValue(undefined);+ runCompleted.mockReset().mockResolvedValue(undefined);cascadeOnCompletion.mockReset().mockResolvedValue({ failed: [], paused: [], started: [] });service = new TaskLifecycleService({} as any, 'user-1');@@ -883,6 +888,72 @@ describe('TaskLifecycleService.onTopicComplete', () => {});});+ describe('run-completed follow-up', () => {+ it('runs after the status has settled, with the run identity', async () => {+ findById.mockResolvedValue(+ baseTask({ assigneeAgentId: 'agent-1', automationMode: null } as Partial<TaskItem>),+ );++ await service.onTopicComplete({+ lastAssistantContent: 'final reply',+ operationId: 'op-1',+ reason: 'done',+ taskId: 'task-1',+ taskIdentifier: 'TASK-1',+ topicId: 'topic-1',+ });++ expect(runCompleted).toHaveBeenCalledWith({+ agentId: 'agent-1',+ lastAssistantContent: 'final reply',+ operationId: 'op-1',+ taskId: 'task-1',+ taskIdentifier: 'TASK-1',+ topicId: 'topic-1',+ userId: 'user-1',+ workspaceId: undefined,+ });+ expect(updateStatus).toHaveBeenCalledWith('task-1', 'paused', { error: null });+ expect(updateStatus.mock.invocationCallOrder[0]).toBeLessThan(+ runCompleted.mock.invocationCallOrder[0],+ );+ });++ it('a failing follow-up does not fail the lifecycle', async () => {+ findById.mockResolvedValue(baseTask({ automationMode: null } as Partial<TaskItem>));+ runCompleted.mockRejectedValue(new Error('boom'));+ const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});++ await expect(+ service.onTopicComplete({+ operationId: 'op-1',+ reason: 'done',+ taskId: 'task-1',+ taskIdentifier: 'TASK-1',+ topicId: 'topic-1',+ }),+ ).resolves.toBeUndefined();+ expect(updateStatus).toHaveBeenCalledWith('task-1', 'paused', { error: null });+ // Logged where production sees it: the follow-up can change task state.+ expect(consoleError).toHaveBeenCalled();+ });++ it('is not called when the run failed', async () => {+ findById.mockResolvedValue(baseTask({ automationMode: null } as Partial<TaskItem>));++ await service.onTopicComplete({+ errorMessage: 'upstream 500',+ operationId: 'op-1',+ reason: 'error',+ taskId: 'task-1',+ taskIdentifier: 'TASK-1',+ topicId: 'topic-1',+ });++ expect(runCompleted).not.toHaveBeenCalled();+ });+ });+describe('scheduled-task result notifications', () => {it('successful scheduled tick → completed notification with task identity', async () => {const task = baseTask({ automationMode: 'schedule', name: 'Daily digest' });diff --git a/packages/business-server/src/task/onTaskRunCompleted.ts b/packages/business-server/src/task/onTaskRunCompleted.tsnew file mode 100644index 00000000000..978842b8ad4--- /dev/null+++ b/packages/business-server/src/task/onTaskRunCompleted.ts@@ -0,0 +1,20 @@+export interface OnTaskRunCompletedParams {+ /** Agent that executed the run. */+ agentId?: string;+ /** Final assistant reply of the run. */+ lastAssistantContent?: string;+ operationId: string;+ taskId: string;+ /** Workspace-level task identifier (e.g. 'T-1'). */+ taskIdentifier: string;+ topicId?: string;+ userId: string;+ workspaceId?: string;+}++/**+ * Follow-up for a task run that finished normally, called after the+ * lifecycle's post-run status transition. A run whose delivery Verify still+ * decides leaves the task `running`. The default does nothing.+ */+export async function onTaskRunCompleted(_params: OnTaskRunCompletedParams): Promise<void> {}