🤯 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
♻️ refactor(server): split the 1510-line task service into cohesive modules (#21151)
Arvin Xu2d1d85b219f8
diff --git a/apps/server/src/services/task/deps.ts b/apps/server/src/services/task/deps.tsnew file mode 100644index 00000000000..d9a577cbbe3--- /dev/null+++ b/apps/server/src/services/task/deps.ts@@ -0,0 +1,23 @@+import type { AgentModel } from '@/database/models/agent';+import type { TaskModel } from '@/database/models/task';+import type { TaskTopicModel } from '@/database/models/taskTopic';+import type { LobeChatDatabase } from '@/database/type';++/**+ * What the `TaskService` sub-domain modules read from the service.+ *+ * Use when:+ * - A function under `task/` needs the caller scope or one of the models the+ * service instance owns+ *+ * Expects:+ * - The service builds it on every call, so it always reflects the instance+ */+export interface TaskServiceDeps {+ agentModel: AgentModel;+ db: LobeChatDatabase;+ taskModel: TaskModel;+ taskTopicModel: TaskTopicModel;+ userId: string;+ workspaceId?: string;+}diff --git a/apps/server/src/services/task/index.detailState.test.ts b/apps/server/src/services/task/index.detailState.test.tsnew file mode 100644index 00000000000..c6eb3400286--- /dev/null+++ b/apps/server/src/services/task/index.detailState.test.ts@@ -0,0 +1,184 @@+// @vitest-environment node+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { TaskService } from './index';++/**+ * Task detail reads that change state or decorate rows: the heartbeat-timeout+ * auto-pause, clearing its stale error, and the verify verdict on run rows.+ */+const mocks = vi.hoisted(() => ({+ findByOperations: vi.fn(),+ findWithHandoff: vi.fn(),+ resolve: vi.fn(),+ timeoutRunning: vi.fn(),+ update: vi.fn(),+ updateStatus: vi.fn(),+}));++vi.mock('@/database/models/agent', () => ({+ AgentModel: vi.fn(function () {+ return { getAgentAvatarsByIds: vi.fn().mockResolvedValue([]) };+ }),+}));+vi.mock('@/database/models/project', () => ({ ProjectModel: vi.fn() }));+vi.mock('@/database/models/topic', () => ({ TopicModel: vi.fn() }));+vi.mock('@/database/models/user', () => ({+ UserModel: { findByIds: vi.fn().mockResolvedValue([]) },+}));+vi.mock('@/database/models/goalGraph', () => ({+ GoalGraphModel: vi.fn(function () {+ return { findGoalByTaskId: vi.fn().mockResolvedValue(undefined) };+ }),+}));+vi.mock('@/database/models/verifyRun', () => ({+ VerifyRunModel: vi.fn(function () {+ return { findByOperations: mocks.findByOperations };+ }),+}));+vi.mock('@/database/models/taskTopic', () => ({+ TaskTopicModel: vi.fn(function () {+ return {+ findWithHandoff: mocks.findWithHandoff,+ timeoutRunning: mocks.timeoutRunning,+ };+ }),+}));+vi.mock('@/database/models/task', () => ({+ taskActivityActor: vi.fn(),+ TaskModel: vi.fn(function () {+ return {+ findAllDescendants: vi.fn().mockResolvedValue([]),+ findByIds: vi.fn().mockResolvedValue([]),+ getActivities: vi.fn().mockResolvedValue([]),+ getCheckpointConfig: vi.fn().mockReturnValue({}),+ getComments: vi.fn().mockResolvedValue([]),+ getDependencies: vi.fn().mockResolvedValue([]),+ getTreePinnedDocuments: vi.fn().mockResolvedValue({ nodeMap: {}, tree: [] }),+ getVerifyConfig: vi.fn().mockReturnValue(undefined),+ resolve: mocks.resolve,+ update: mocks.update,+ updateStatus: mocks.updateStatus,+ };+ }),+}));+vi.mock('@/server/services/verify/taskAcceptance', () => ({+ resolveTaskAcceptance: vi.fn().mockResolvedValue(undefined),+}));+vi.mock('@/server/services/aiAgent', () => ({ AiAgentService: vi.fn() }));+vi.mock('@/server/services/file/resolveAttachments', () => ({+ resolveAttachmentMetadata: vi.fn().mockResolvedValue([]),+}));+vi.mock('@/server/services/file/extractFileIdsFromEditorData', () => ({+ extractFileIdsFromEditorData: vi.fn().mockResolvedValue([]),+}));++const baseTask = {+ assigneeAgentId: null,+ createdAt: null,+ error: null,+ id: 'task-1',+ identifier: 'T-1',+ parentTaskId: null,+ status: 'todo',+};++const service = () => new TaskService({} as any, 'user-1');++describe('TaskService.getTaskDetail state', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ mocks.findWithHandoff.mockResolvedValue([]);+ mocks.findByOperations.mockResolvedValue(new Map());+ });++ it('pauses a running task whose heartbeat timed out, then re-reads it', async () => {+ const stale = {+ ...baseTask,+ heartbeatTimeout: 60,+ lastHeartbeatAt: new Date(Date.now() - 120_000),+ status: 'running',+ };+ mocks.resolve+ .mockResolvedValueOnce(stale)+ .mockResolvedValueOnce({ ...stale, error: 'Heartbeat timeout', status: 'paused' });++ const detail = await service().getTaskDetail('T-1');++ expect(mocks.updateStatus).toHaveBeenCalledWith('task-1', 'paused', {+ error: 'Heartbeat timeout',+ });+ expect(mocks.timeoutRunning).toHaveBeenCalledWith('task-1');+ expect(mocks.resolve).toHaveBeenCalledTimes(2);+ // The re-read task is no longer running, so its timeout error is cleared.+ expect(mocks.update).toHaveBeenCalledWith('task-1', { error: null });+ expect(detail).toMatchObject({ error: null, status: 'paused' });+ });++ it('keeps a running task whose heartbeat is still fresh', async () => {+ mocks.resolve.mockResolvedValue({+ ...baseTask,+ heartbeatTimeout: 600,+ lastHeartbeatAt: new Date(),+ status: 'running',+ });++ await service().getTaskDetail('T-1');++ expect(mocks.updateStatus).not.toHaveBeenCalled();+ expect(mocks.update).not.toHaveBeenCalled();+ });++ it("decorates each run row with its verify round's verdict", async () => {+ mocks.resolve.mockResolvedValue(baseTask);+ mocks.findWithHandoff.mockResolvedValue([+ {+ createdAt: new Date('2024-01-02T00:00:00Z'),+ operationId: 'op-1',+ seq: 1,+ status: 'completed',+ topicId: 'topic-1',+ },+ {+ createdAt: new Date('2024-01-03T00:00:00Z'),+ operationId: null,+ seq: 2,+ status: 'running',+ topicId: 'topic-2',+ },+ ]);+ mocks.findByOperations.mockResolvedValue(+ new Map([+ [+ 'op-1',+ {+ acceptanceId: 'acc-1',+ id: 'run-1',+ passed: 3,+ roundIndex: 2,+ status: 'passed',+ total: 4,+ },+ ],+ ]),+ );++ const detail = await service().getTaskDetail('T-1');++ expect(mocks.findByOperations).toHaveBeenCalledWith(['op-1']);+ expect(detail?.activities?.map((activity) => [activity.id, activity.verify])).toEqual([+ [+ 'topic-1',+ {+ acceptanceId: 'acc-1',+ passed: 3,+ roundIndex: 2,+ runId: 'run-1',+ status: 'passed',+ total: 4,+ },+ ],+ ['topic-2', null],+ ]);+ });+});diff --git a/apps/server/src/services/task/index.statusCompletion.test.ts b/apps/server/src/services/task/index.statusCompletion.test.tsnew file mode 100644index 00000000000..dc7932d3201--- /dev/null+++ b/apps/server/src/services/task/index.statusCompletion.test.ts@@ -0,0 +1,118 @@+// @vitest-environment node+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { TaskService } from './index';++/**+ * Completing a task: the parent checkpoint pause, the all-siblings-done+ * signal, and the dependency cascade the runner starts.+ */+const mocks = vi.hoisted(() => ({+ areAllSubtasksCompleted: vi.fn(),+ cascadeOnCompletion: vi.fn(),+ findById: vi.fn(),+ resolve: vi.fn(),+ shouldPauseAfterComplete: vi.fn(),+ updateStatus: vi.fn(),+}));++vi.mock('@/database/models/agent', () => ({ AgentModel: vi.fn() }));+vi.mock('@/database/models/project', () => ({ ProjectModel: vi.fn() }));+vi.mock('@/database/models/topic', () => ({ TopicModel: vi.fn() }));+vi.mock('@/database/models/taskTopic', () => ({+ TaskTopicModel: vi.fn(function () {+ return { findByTaskId: vi.fn().mockResolvedValue([]) };+ }),+}));+vi.mock('@/database/models/task', () => ({+ taskActivityActor: vi.fn(),+ TaskModel: vi.fn(function () {+ return {+ areAllSubtasksCompleted: mocks.areAllSubtasksCompleted,+ findById: mocks.findById,+ resolve: mocks.resolve,+ shouldPauseAfterComplete: mocks.shouldPauseAfterComplete,+ updateStatus: mocks.updateStatus,+ };+ }),+}));+vi.mock('@/server/services/aiAgent', () => ({ AiAgentService: vi.fn() }));+vi.mock('@/server/services/taskRunner', () => ({+ TaskRunnerService: vi.fn(function () {+ return { cascadeOnCompletion: mocks.cascadeOnCompletion };+ }),+}));++const db = { transaction: vi.fn(async (fn: (tx: unknown) => unknown) => fn(db)) };++const subtask = {+ id: 'task-2',+ identifier: 'T-2',+ parentTaskId: 'task-1',+ status: 'running',+};++describe('TaskService.updateStatus completion', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ mocks.resolve.mockResolvedValue({ ...subtask, status: 'todo' });+ mocks.updateStatus.mockImplementation(async (id: string, status: string) => ({+ ...subtask,+ id,+ status,+ }));+ mocks.findById.mockResolvedValue({ id: 'task-1', identifier: 'T-1' });+ mocks.cascadeOnCompletion.mockResolvedValue({ paused: ['T-9'], started: ['T-3'] });+ });++ it('pauses the parent at its checkpoint and reports the finished family', async () => {+ mocks.shouldPauseAfterComplete.mockReturnValue(true);+ mocks.areAllSubtasksCompleted.mockResolvedValue(true);++ const result = await new TaskService(db as any, 'user-1').updateStatus({+ id: 'T-2',+ status: 'completed',+ });++ expect(mocks.shouldPauseAfterComplete).toHaveBeenCalledWith(+ { id: 'task-1', identifier: 'T-1' },+ 'T-2',+ );+ expect(mocks.updateStatus).toHaveBeenCalledWith('task-1', 'paused');+ expect(mocks.areAllSubtasksCompleted).toHaveBeenCalledWith('task-1');+ expect(mocks.cascadeOnCompletion).toHaveBeenCalledWith('task-2');+ expect(result).toMatchObject({+ allSubtasksDone: true,+ checkpointTriggered: true,+ parentTaskId: 'task-1',+ paused: ['T-9'],+ unlocked: ['T-3'],+ });+ });++ it('omits the checkpoint and family flags when neither applies', async () => {+ mocks.shouldPauseAfterComplete.mockReturnValue(false);+ mocks.areAllSubtasksCompleted.mockResolvedValue(false);++ const result = await new TaskService(db as any, 'user-1').updateStatus({+ id: 'T-2',+ status: 'completed',+ });++ expect(mocks.updateStatus).not.toHaveBeenCalledWith('task-1', 'paused');+ expect(result).not.toHaveProperty('checkpointTriggered');+ expect(result).not.toHaveProperty('allSubtasksDone');+ expect(result).toMatchObject({ paused: ['T-9'], unlocked: ['T-3'] });+ });++ it('rejects an error message on a non-failed status before reading the task', async () => {+ await expect(+ new TaskService(db as any, 'user-1').updateStatus({+ error: 'boom',+ id: 'T-2',+ status: 'completed',+ }),+ ).rejects.toThrow('Task error can only be provided when status is failed.');+ expect(mocks.resolve).not.toHaveBeenCalled();+ });+});diff --git a/apps/server/src/services/task/index.ts b/apps/server/src/services/task/index.tsindex ec0744e3094..665a8225c47 100644--- a/apps/server/src/services/task/index.ts+++ b/apps/server/src/services/task/index.ts@@ -1,24 +1,12 @@-import { randomUUID } from 'node:crypto';--import { UNFINISHED_TASK_STATUSES } from '@lobechat/builtin-tool-task';import { TASK_ASSIGNEE_PERMISSION_CODES } from '@lobechat/const/rbac';-import type {- TaskContext,- TaskDetailData,- TaskItem,- TaskSchedulerContext,- TaskStatus,-} from '@lobechat/types';+import type { TaskContext, TaskDetailData, TaskItem } from '@lobechat/types';import { TRPCError } from '@trpc/server';+import pMap from 'p-map';import { AgentModel } from '@/database/models/agent';import { ProjectModel } from '@/database/models/project';import { RbacModel } from '@/database/models/rbac';-import {- isTaskIdentifierUniqueViolation,- taskActivityActor,- TaskModel,-} from '@/database/models/task';+import { isTaskIdentifierUniqueViolation, TaskModel } from '@/database/models/task';import { TaskTopicModel } from '@/database/models/taskTopic';import { TopicModel } from '@/database/models/topic';import { WorkspaceMemberModel } from '@/database/models/workspaceMember';@@ -28,10 +16,22 @@ import { AiAgentService } from '../aiAgent';import { type SubtaskGraphPlan, TaskGraphService } from '../taskGraph';import { type ReviewResult, TaskReviewService } from '../taskReview';import { TaskRunnerService } from '../taskRunner';-import { createTaskSchedulerModule } from '../taskScheduler';-import { assertArchivedTaskStaysIdle, writeKeepingArchivedIdle } from './archiveGuard';+import { writeKeepingArchivedIdle } from './archiveGuard';+import type { TaskServiceDeps } from './deps';import { TaskDetailService } from './detail';import { resolveMissingTaskModelConfig } from './modelSnapshot';+import {+ updateStatus,+ updateStatusCascade,+ type UpdateStatusCascadeResult,+ type UpdateStatusResult,+} from './statusTransitions';+import { interruptTaskOperation, resolveTaskOrThrow } from './utils';++export type { UpdateStatusCascadeResult, UpdateStatusResult } from './statusTransitions';++/** Subtask runs started at once when kicking off a ready layer. */+const SUBTASK_KICKOFF_CONCURRENCY = 5;/** How long after a run starts a task may have no recorded operation yet. */const TASK_RUN_STARTING_GRACE_MS = 2 * 60 * 1000;@@ -62,24 +62,6 @@ export interface CreateTaskInput {visibility?: 'private' | 'public';}-export interface UpdateStatusResult {- allSubtasksDone?: boolean;- checkpointTriggered?: boolean;- /** Completion was requested by the task's own run and lands when it ends. */- completionDeferred?: boolean;- parentTaskId?: string | null;- paused: string[];- task: TaskItem;- unlocked: string[];-}--export interface UpdateStatusCascadeResult {- paused: string[];- task: TaskItem;- unlocked: string[];- updatedSubtasks: string[];-}-export interface RunReadySubtasksResult {failed: { error: string; identifier: string }[];kickedOff: string[];@@ -109,6 +91,18 @@ export class TaskService {this.topicModel = new TopicModel(db, userId, workspaceId);}+ /** Collaborators the extracted sub-domain functions read — resolved on every call. */+ private get deps(): TaskServiceDeps {+ return {+ agentModel: this.agentModel,+ db: this.db,+ taskModel: this.taskModel,+ taskTopicModel: this.taskTopicModel,+ userId: this.userId,+ workspaceId: this.workspaceId,+ };+ }+/*** Create a task. Validates the assignee belongs to the user, resolves* `parentTaskId` if it's an identifier, and snapshots the assignee agent's@@ -267,16 +261,7 @@ export class TaskService {});}- private interruptTaskOperation = async (service: AiAgentService, operationId: string) => {- const result = await service.interruptTask({ operationId });- if (!result.success || result.deviceCancellationConfirmed === false) {- throw new TRPCError({- code: 'CONFLICT',- message:- 'Task interruption was not confirmed. The execution remains active; retry stopping it before starting another attempt.',- });- }- };+ private interruptTaskOperation = interruptTaskOperation;/*** Cancel a running topic: interrupt the remote operation (if any), then@@ -493,194 +478,9 @@ export class TaskService {* completions, kick off any newly-unlocked downstream tasks.*/async updateStatus(- input: {- error?: string;- id: string;- /**- * The caller's own run, when an agent changes the task it is executing.- * Completing that task is a delivery request: it is recorded and- * finalized by onTopicComplete once the run ends, instead of- * interrupting the run mid-tool-call.- */- operationId?: string;- status: TaskStatus;- },- /**- * Present only for a change a person or their agent made; its absence is- * how system transitions (runner / lifecycle / watchdog) opt out of the- * activity feed.- */- actor?: { agentId?: string | null; userId?: string | null },+ ...args: Parameters<typeof updateStatus> extends [unknown, ...infer A] ? A : never): Promise<UpdateStatusResult> {- const { id, operationId, status, error: errorMsg } = input;-- if (errorMsg && status !== 'failed') {- throw new TRPCError({- code: 'BAD_REQUEST',- message: 'Task error can only be provided when status is failed.',- });- }-- const resolved = await this.resolveOrThrow(id);- assertArchivedTaskStaysIdle(resolved, { status });-- if (resolved.status === 'running' && status !== 'running') {- const topics = await this.taskTopicModel.findByTaskId(resolved.id);-- if (- status === 'completed' &&- operationId &&- topics.some((t) => t.status === 'running' && t.operationId === operationId)- ) {- await this.taskModel.updateContext(resolved.id, {- completion: { requestedByOperationId: operationId },- });- return { completionDeferred: true, paused: [], task: resolved, unlocked: [] };- }-- const aiAgentService = new AiAgentService(this.db, this.userId, {- workspaceId: this.workspaceId,- });-- for (const t of topics) {- if (t.status !== 'running' || !t.topicId) continue;-- // Interrupt the remote operation first; if it fails, skip cancellation- // to avoid desynchronizing DB state from a still-running operation.- if (t.operationId) {- try {- await this.interruptTaskOperation(aiAgentService, t.operationId);- } catch (err) {- console.error(- '[TaskService.updateStatus] failed to interrupt topic %s:',- t.topicId,- err,- );- throw err;- }- }-- await this.taskTopicModel.cancelIfRunning(resolved.id, t.topicId);- }- }-- const extra: Record<string, unknown> = {};- if (status === 'running') extra.startedAt = new Date();- if (status === 'completed' || status === 'failed' || status === 'canceled')- extra.completedAt = new Date();- if (errorMsg) extra.error = errorMsg;-- const task = await writeKeepingArchivedIdle(this.db, (tx) => {- const model = new TaskModel(tx, this.userId, this.workspaceId);- return actor- ? model.updateWithLog(resolved.id, { status, ...extra }, actor)- : model.updateStatus(resolved.id, status, extra);- });- if (!task) throw new TRPCError({ code: 'NOT_FOUND', message: 'Task not found' });-- // Stamp the schedule run-count window each time the user (re)starts a- // scheduled task. The cron dispatcher itself flips a task running →- // scheduled on every tick, so we exclude that natural cycle by only- // resetting when the previous status was NOT 'running'. This lets- // `runScheduleTick` enforce `config.schedule.maxExecutions` by counting- // task_topics created since this timestamp.- if (- status === 'scheduled' &&- task.automationMode === 'schedule' &&- resolved.status !== 'running'- ) {- await this.taskModel.updateContext(task.id, {- scheduler: { scheduleStartedAt: new Date().toISOString() },- });- }-- // Heartbeat ticks are one-shot delayed messages that re-arm themselves- // after each run. Seed the very first message when a user starts/restarts- // the schedule; otherwise a fresh heartbeat task has no event capable of- // reaching the lifecycle re-arm path.- if (- status === 'scheduled' &&- task.automationMode === 'heartbeat' &&- task.heartbeatInterval &&- task.heartbeatInterval > 0 &&- resolved.status !== 'running' &&- resolved.status !== 'scheduled'- ) {- const scheduler = createTaskSchedulerModule();- const schedulerContext = (resolved.context as TaskContext | null)?.scheduler as- TaskSchedulerContext | undefined;- const previousTickMessageId = schedulerContext?.tickMessageId;- const tickToken = randomUUID();- let tickMessageId: string | undefined;-- try {- // Invalidate the previous generation before publishing. This closes- // the race where an old QStash delivery arrives while the replacement- // message is being created.- await this.taskModel.updateContext(task.id, { scheduler: { tickToken } });- tickMessageId = await scheduler.scheduleNextTopic({- delay: task.heartbeatInterval,- taskId: task.id,- tickToken,- userId: this.userId,- });-- await this.taskModel.updateContext(task.id, {- scheduler: {- consecutiveFailures: schedulerContext?.consecutiveFailures ?? 0,- scheduledAt: new Date().toISOString(),- tickMessageId,- tickToken,- },- });- } catch (error) {- // Never expose a scheduled status without a persisted tick. Restore- // the user-visible state and cancel a newly published message when- // persistence was the failing step.- if (tickMessageId) await scheduler.cancelScheduled(tickMessageId).catch(() => undefined);- await this.taskModel.update(task.id, { context: resolved.context });- // The logged transition really happened and is now being undone, so- // the undo is logged too (same actor, back to the same value): the- // audit trail stays truthful and the feed folds the pair away.- if (actor) await this.taskModel.updateWithLog(task.id, { status: resolved.status }, actor);- else await this.taskModel.updateStatus(task.id, resolved.status);- throw error;- }-- if (previousTickMessageId) {- await scheduler.cancelScheduled(previousTickMessageId).catch(() => undefined);- }- }-- const unlocked: string[] = [];- const paused: string[] = [];- let allSubtasksDone = false;- let checkpointTriggered = false;-- if (status === 'completed') {- if (task.parentTaskId) {- const parentTask = await this.taskModel.findById(task.parentTaskId);- if (parentTask && this.taskModel.shouldPauseAfterComplete(parentTask, task.identifier)) {- await this.taskModel.updateStatus(parentTask.id, 'paused');- checkpointTriggered = true;- }- allSubtasksDone = await this.taskModel.areAllSubtasksCompleted(task.parentTaskId);- }-- // Unlock blocked tasks and actually kick them off via the runner.- const runner = new TaskRunnerService(this.db, this.userId, this.workspaceId);- const cascade = await runner.cascadeOnCompletion(task.id);- unlocked.push(...cascade.started);- paused.push(...cascade.paused);- }-- return {- paused,- task,- unlocked,- ...(checkpointTriggered && { checkpointTriggered: true }),- ...(allSubtasksDone && { allSubtasksDone: true, parentTaskId: task.parentTaskId }),- };+ return updateStatus(this.deps, ...args);}/**@@ -690,113 +490,9 @@ export class TaskService {* sibling in the middle of the cascade.*/async updateStatusCascade(- input: {- id: string;- status: 'canceled' | 'completed';- },- /** The person confirming "include subtasks"; absent for system callers. */- actor?: { agentId?: string | null; userId?: string | null },+ ...args: Parameters<typeof updateStatusCascade> extends [unknown, ...infer A] ? A : never): Promise<UpdateStatusCascadeResult> {- const resolved = await this.resolveOrThrow(input.id);- const subtasks = await this.taskModel.findSubtasks(resolved.id);- const unfinishedStatuses = new Set<string>(UNFINISHED_TASK_STATUSES);- const openSubtasks = subtasks.filter((task) => unfinishedStatuses.has(task.status));- // Freeze the cascade to this snapshot: both the interrupt pass and the- // status update operate on the same id set, so a subtask created or- // transitioned after the confirmation dialog is never rewritten.- const targetTasks = [resolved, ...openSubtasks];- const targetIds = targetTasks.map((task) => task.id);-- const aiAgentService = new AiAgentService(this.db, this.userId, {- workspaceId: this.workspaceId,- });-- const runningTopics = await this.taskTopicModel.findRunningByTaskIds(targetIds);- if (runningTopics.length > 0) {- const settled = await Promise.allSettled(- runningTopics.map(async (topic) => {- if (topic.operationId) {- await this.interruptTaskOperation(aiAgentService, topic.operationId);- }- }),- );- const failure = settled.find((result) => result.status === 'rejected');- if (failure) {- // Persist the interrupts that did succeed before surfacing the error,- // so an actually-stopped operation is not left recorded as running.- for (const [index, topic] of runningTopics.entries()) {- if (settled[index].status !== 'fulfilled' || !topic.topicId) continue;- await this.taskTopicModel- .cancelIfRunning(topic.taskId, topic.topicId)- .catch(() => undefined);- }- throw failure.reason;- }- }-- const completedAt = new Date();- let updatedTasks: TaskItem[] = [];- let canceledTopics: Awaited<ReturnType<TaskTopicModel['cancelRunningByTaskIds']>> = [];- await this.db.transaction(async (tx) => {- const taskModel = new TaskModel(tx, this.userId, this.workspaceId);- const taskTopicModel = new TaskTopicModel(tx, this.userId, this.workspaceId);-- // Cancel by the frozen id set rather than the pre-read topic list, so a- // topic that started between the snapshot and this transaction is still- // closed together with the status update.- canceledTopics = await taskTopicModel.cancelRunningByTaskIds(targetIds);- // The pre-transaction snapshot only chose *which* tasks; what each one- // is leaving is read under the lock, so a collaborator's edit between- // the dialog and this write is logged as it really was.- const locked = actor ? await taskModel.lockForStatusChange(targetIds) : [];- updatedTasks = await taskModel.updateStatusForIds(targetIds, input.status, { completedAt });-- // A person confirmed this for the whole family, so every member that- // moved gets its own row — one INSERT, not one per task.- if (actor) {- const { actorKind, ...actorColumns } = taskActivityActor(actor);- await taskModel.addActivities(- locked- .filter((before) => before.status !== input.status)- .map((before) => ({- ...actorColumns,- payload: { actorKind, from: before.status, to: input.status },- taskId: before.id,- type: 'status' as const,- visibility: before.visibility,- })),- );- }- });-- // Best-effort: stop any operation discovered only inside the transaction.- const interruptedOperationIds = new Set(runningTopics.map((topic) => topic.operationId));- await Promise.allSettled(- canceledTopics- .filter((topic) => topic.operationId && !interruptedOperationIds.has(topic.operationId))- .map((topic) => aiAgentService.interruptTask({ operationId: topic.operationId! })),- );-- const task = updatedTasks.find(({ id }) => id === resolved.id);- if (!task) throw new TRPCError({ code: 'NOT_FOUND', message: 'Task not found' });-- const unlocked: string[] = [];- const paused: string[] = [];- if (input.status === 'completed') {- const runner = new TaskRunnerService(this.db, this.userId, this.workspaceId);- const cascade = await runner.cascadeOnCompletionMany(updatedTasks.map(({ id }) => id));- unlocked.push(...cascade.started);- paused.push(...cascade.paused);- }-- return {- paused,- task,- unlocked,- updatedSubtasks: updatedTasks- .filter(({ parentTaskId }) => parentTaskId === resolved.id)- .map(({ identifier }) => identifier),- };+ return updateStatusCascade(this.deps, ...args);}/**@@ -836,13 +532,19 @@ export class TaskService {const kickedOff: string[] = [];const failed: { error: string; identifier: string }[] = [];- const settled = await Promise.allSettled(- firstLayer.map(async (identifier) => {- const id = identifierToId.get(identifier);- if (!id) throw new Error(`Subtask ${identifier} not found`);- await runner.runTask({ taskId: id });- return identifier;- }),+ const settled = await pMap(+ firstLayer,+ async (identifier): Promise<PromiseSettledResult<string>> => {+ try {+ const id = identifierToId.get(identifier);+ if (!id) throw new Error(`Subtask ${identifier} not found`);+ await runner.runTask({ taskId: id });+ return { status: 'fulfilled', value: identifier };+ } catch (reason) {+ return { reason, status: 'rejected' };+ }+ },+ { concurrency: SUBTASK_KICKOFF_CONCURRENCY },);for (const [index, result] of settled.entries()) {@@ -980,9 +682,7 @@ export class TaskService {}private async resolveOrThrow(idOrIdentifier: string): Promise<TaskItem> {- const task = await this.taskModel.resolve(idOrIdentifier);- if (!task) throw new TRPCError({ code: 'NOT_FOUND', message: 'Task not found' });- return task;+ return resolveTaskOrThrow(this.deps, idOrIdentifier);}async getTaskDetail(taskIdOrIdentifier: string): Promise<TaskDetailData | null> {diff --git a/apps/server/src/services/task/statusTransitions.ts b/apps/server/src/services/task/statusTransitions.tsnew file mode 100644index 00000000000..75d2db7a134--- /dev/null+++ b/apps/server/src/services/task/statusTransitions.ts@@ -0,0 +1,362 @@+import { randomUUID } from 'node:crypto';++import { UNFINISHED_TASK_STATUSES } from '@lobechat/builtin-tool-task';+import type { TaskContext, TaskItem, TaskSchedulerContext, TaskStatus } from '@lobechat/types';+import { TRPCError } from '@trpc/server';+import pMap from 'p-map';++import { taskActivityActor, TaskModel } from '@/database/models/task';+import { TaskTopicModel } from '@/database/models/taskTopic';++import { AiAgentService } from '../aiAgent';+import { TaskRunnerService } from '../taskRunner';+import { createTaskSchedulerModule } from '../taskScheduler';+import { assertArchivedTaskStaysIdle, writeKeepingArchivedIdle } from './archiveGuard';+import type { TaskServiceDeps } from './deps';+import { interruptTaskOperation, resolveTaskOrThrow } from './utils';++/** Concurrent agent-operation interrupts issued by one status cascade. */+const INTERRUPT_CONCURRENCY = 5;++export interface UpdateStatusResult {+ allSubtasksDone?: boolean;+ checkpointTriggered?: boolean;+ /** Completion was requested by the task's own run and lands when it ends. */+ completionDeferred?: boolean;+ parentTaskId?: string | null;+ paused: string[];+ task: TaskItem;+ unlocked: string[];+}++export interface UpdateStatusCascadeResult {+ paused: string[];+ task: TaskItem;+ unlocked: string[];+ updatedSubtasks: string[];+}++/**+ * Transition a task to a new status, cascading the side effects:+ * - leaving `running`: interrupt + cancel still-running topics+ * - entering `completed`: check parent checkpoint, count sibling+ * completions, kick off any newly-unlocked downstream tasks.+ */+export async function updateStatus(+ deps: TaskServiceDeps,+ input: {+ error?: string;+ id: string;+ /**+ * The caller's own run, when an agent changes the task it is executing.+ * Completing that task is a delivery request: it is recorded and+ * finalized by onTopicComplete once the run ends, instead of+ * interrupting the run mid-tool-call.+ */+ operationId?: string;+ status: TaskStatus;+ },+ /**+ * Present only for a change a person or their agent made; its absence is+ * how system transitions (runner / lifecycle / watchdog) opt out of the+ * activity feed.+ */+ actor?: { agentId?: string | null; userId?: string | null },+): Promise<UpdateStatusResult> {+ const { id, operationId, status, error: errorMsg } = input;++ if (errorMsg && status !== 'failed') {+ throw new TRPCError({+ code: 'BAD_REQUEST',+ message: 'Task error can only be provided when status is failed.',+ });+ }++ const resolved = await resolveTaskOrThrow(deps, id);+ assertArchivedTaskStaysIdle(resolved, { status });++ if (resolved.status === 'running' && status !== 'running') {+ const topics = await deps.taskTopicModel.findByTaskId(resolved.id);++ if (+ status === 'completed' &&+ operationId &&+ topics.some((t) => t.status === 'running' && t.operationId === operationId)+ ) {+ await deps.taskModel.updateContext(resolved.id, {+ completion: { requestedByOperationId: operationId },+ });+ return { completionDeferred: true, paused: [], task: resolved, unlocked: [] };+ }++ const aiAgentService = new AiAgentService(deps.db, deps.userId, {+ workspaceId: deps.workspaceId,+ });++ for (const t of topics) {+ if (t.status !== 'running' || !t.topicId) continue;++ // Interrupt the remote operation first; if it fails, skip cancellation+ // to avoid desynchronizing DB state from a still-running operation.+ if (t.operationId) {+ try {+ await interruptTaskOperation(aiAgentService, t.operationId);+ } catch (err) {+ console.error('[TaskService.updateStatus] failed to interrupt topic %s:', t.topicId, err);+ throw err;+ }+ }++ await deps.taskTopicModel.cancelIfRunning(resolved.id, t.topicId);+ }+ }++ const extra: Record<string, unknown> = {};+ if (status === 'running') extra.startedAt = new Date();+ if (status === 'completed' || status === 'failed' || status === 'canceled')+ extra.completedAt = new Date();+ if (errorMsg) extra.error = errorMsg;++ const task = await writeKeepingArchivedIdle(deps.db, (tx) => {+ const model = new TaskModel(tx, deps.userId, deps.workspaceId);+ return actor+ ? model.updateWithLog(resolved.id, { status, ...extra }, actor)+ : model.updateStatus(resolved.id, status, extra);+ });+ if (!task) throw new TRPCError({ code: 'NOT_FOUND', message: 'Task not found' });++ // Stamp the schedule run-count window each time the user (re)starts a+ // scheduled task. The cron dispatcher itself flips a task running →+ // scheduled on every tick, so we exclude that natural cycle by only+ // resetting when the previous status was NOT 'running'. This lets+ // `runScheduleTick` enforce `config.schedule.maxExecutions` by counting+ // task_topics created since this timestamp.+ if (+ status === 'scheduled' &&+ task.automationMode === 'schedule' &&+ resolved.status !== 'running'+ ) {+ await deps.taskModel.updateContext(task.id, {+ scheduler: { scheduleStartedAt: new Date().toISOString() },+ });+ }++ // Heartbeat ticks are one-shot delayed messages that re-arm themselves+ // after each run. Seed the very first message when a user starts/restarts+ // the schedule; otherwise a fresh heartbeat task has no event capable of+ // reaching the lifecycle re-arm path.+ if (+ status === 'scheduled' &&+ task.automationMode === 'heartbeat' &&+ task.heartbeatInterval &&+ task.heartbeatInterval > 0 &&+ resolved.status !== 'running' &&+ resolved.status !== 'scheduled'+ ) {+ const scheduler = createTaskSchedulerModule();+ const schedulerContext = (resolved.context as TaskContext | null)?.scheduler as+ TaskSchedulerContext | undefined;+ const previousTickMessageId = schedulerContext?.tickMessageId;+ const tickToken = randomUUID();+ let tickMessageId: string | undefined;++ try {+ // Invalidate the previous generation before publishing. This closes+ // the race where an old QStash delivery arrives while the replacement+ // message is being created.+ await deps.taskModel.updateContext(task.id, { scheduler: { tickToken } });+ tickMessageId = await scheduler.scheduleNextTopic({+ delay: task.heartbeatInterval,+ taskId: task.id,+ tickToken,+ userId: deps.userId,+ });++ await deps.taskModel.updateContext(task.id, {+ scheduler: {+ consecutiveFailures: schedulerContext?.consecutiveFailures ?? 0,+ scheduledAt: new Date().toISOString(),+ tickMessageId,+ tickToken,+ },+ });+ } catch (error) {+ // Never expose a scheduled status without a persisted tick. Restore+ // the user-visible state and cancel a newly published message when+ // persistence was the failing step.+ if (tickMessageId) await scheduler.cancelScheduled(tickMessageId).catch(() => undefined);+ await deps.taskModel.update(task.id, { context: resolved.context });+ // The logged transition really happened and is now being undone, so+ // the undo is logged too (same actor, back to the same value): the+ // audit trail stays truthful and the feed folds the pair away.+ if (actor) await deps.taskModel.updateWithLog(task.id, { status: resolved.status }, actor);+ else await deps.taskModel.updateStatus(task.id, resolved.status);+ throw error;+ }++ if (previousTickMessageId) {+ await scheduler.cancelScheduled(previousTickMessageId).catch(() => undefined);+ }+ }++ const unlocked: string[] = [];+ const paused: string[] = [];+ let allSubtasksDone = false;+ let checkpointTriggered = false;++ if (status === 'completed') {+ if (task.parentTaskId) {+ const parentTask = await deps.taskModel.findById(task.parentTaskId);+ if (parentTask && deps.taskModel.shouldPauseAfterComplete(parentTask, task.identifier)) {+ await deps.taskModel.updateStatus(parentTask.id, 'paused');+ checkpointTriggered = true;+ }+ allSubtasksDone = await deps.taskModel.areAllSubtasksCompleted(task.parentTaskId);+ }++ // Unlock blocked tasks and actually kick them off via the runner.+ const runner = new TaskRunnerService(deps.db, deps.userId, deps.workspaceId);+ const cascade = await runner.cascadeOnCompletion(task.id);+ unlocked.push(...cascade.started);+ paused.push(...cascade.paused);+ }++ return {+ paused,+ task,+ unlocked,+ ...(checkpointTriggered && { checkpointTriggered: true }),+ ...(allSubtasksDone && { allSubtasksDone: true, parentTaskId: task.parentTaskId }),+ };+}++/**+ * Transition a parent and every currently unfinished direct subtask as one+ * database transaction. Completion side effects run only after the whole+ * family has reached the target status, so dependency edges cannot start a+ * sibling in the middle of the cascade.+ */+export async function updateStatusCascade(+ deps: TaskServiceDeps,+ input: {+ id: string;+ status: 'canceled' | 'completed';+ },+ /** The person confirming "include subtasks"; absent for system callers. */+ actor?: { agentId?: string | null; userId?: string | null },+): Promise<UpdateStatusCascadeResult> {+ const resolved = await resolveTaskOrThrow(deps, input.id);+ const subtasks = await deps.taskModel.findSubtasks(resolved.id);+ const unfinishedStatuses = new Set<string>(UNFINISHED_TASK_STATUSES);+ const openSubtasks = subtasks.filter((task) => unfinishedStatuses.has(task.status));+ // Freeze the cascade to this snapshot: both the interrupt pass and the+ // status update operate on the same id set, so a subtask created or+ // transitioned after the confirmation dialog is never rewritten.+ const targetTasks = [resolved, ...openSubtasks];+ const targetIds = targetTasks.map((task) => task.id);++ const aiAgentService = new AiAgentService(deps.db, deps.userId, {+ workspaceId: deps.workspaceId,+ });++ const runningTopics = await deps.taskTopicModel.findRunningByTaskIds(targetIds);+ if (runningTopics.length > 0) {+ // Settled-shaped results, in input order, so a failed interrupt never+ // hides which of the others actually stopped.+ const settled = await pMap(+ runningTopics,+ async (topic): Promise<PromiseSettledResult<void>> => {+ try {+ if (topic.operationId) {+ await interruptTaskOperation(aiAgentService, topic.operationId);+ }+ return { status: 'fulfilled', value: undefined };+ } catch (reason) {+ return { reason, status: 'rejected' };+ }+ },+ { concurrency: INTERRUPT_CONCURRENCY },+ );+ const failure = settled.find(+ (result): result is PromiseRejectedResult => result.status === 'rejected',+ );+ if (failure) {+ // Persist the interrupts that did succeed before surfacing the error,+ // so an actually-stopped operation is not left recorded as running.+ for (const [index, topic] of runningTopics.entries()) {+ if (settled[index].status !== 'fulfilled' || !topic.topicId) continue;+ await deps.taskTopicModel+ .cancelIfRunning(topic.taskId, topic.topicId)+ .catch(() => undefined);+ }+ throw failure.reason;+ }+ }++ const completedAt = new Date();+ let updatedTasks: TaskItem[] = [];+ let canceledTopics: Awaited<ReturnType<TaskTopicModel['cancelRunningByTaskIds']>> = [];+ await deps.db.transaction(async (tx) => {+ const taskModel = new TaskModel(tx, deps.userId, deps.workspaceId);+ const taskTopicModel = new TaskTopicModel(tx, deps.userId, deps.workspaceId);++ // Cancel by the frozen id set rather than the pre-read topic list, so a+ // topic that started between the snapshot and this transaction is still+ // closed together with the status update.+ canceledTopics = await taskTopicModel.cancelRunningByTaskIds(targetIds);+ // The pre-transaction snapshot only chose *which* tasks; what each one+ // is leaving is read under the lock, so a collaborator's edit between+ // the dialog and this write is logged as it really was.+ const locked = actor ? await taskModel.lockForStatusChange(targetIds) : [];+ updatedTasks = await taskModel.updateStatusForIds(targetIds, input.status, { completedAt });++ // A person confirmed this for the whole family, so every member that+ // moved gets its own row — one INSERT, not one per task.+ if (actor) {+ const { actorKind, ...actorColumns } = taskActivityActor(actor);+ await taskModel.addActivities(+ locked+ .filter((before) => before.status !== input.status)+ .map((before) => ({+ ...actorColumns,+ payload: { actorKind, from: before.status, to: input.status },+ taskId: before.id,+ type: 'status' as const,+ visibility: before.visibility,+ })),+ );+ }+ });++ // Best-effort: stop any operation discovered only inside the transaction.+ const interruptedOperationIds = new Set(runningTopics.map((topic) => topic.operationId));+ await pMap(+ canceledTopics.filter(+ (topic) => topic.operationId && !interruptedOperationIds.has(topic.operationId),+ ),+ (topic) =>+ aiAgentService.interruptTask({ operationId: topic.operationId! }).catch(() => undefined),+ { concurrency: INTERRUPT_CONCURRENCY },+ );++ const task = updatedTasks.find(({ id }) => id === resolved.id);+ if (!task) throw new TRPCError({ code: 'NOT_FOUND', message: 'Task not found' });++ const unlocked: string[] = [];+ const paused: string[] = [];+ if (input.status === 'completed') {+ const runner = new TaskRunnerService(deps.db, deps.userId, deps.workspaceId);+ const cascade = await runner.cascadeOnCompletionMany(updatedTasks.map(({ id }) => id));+ unlocked.push(...cascade.started);+ paused.push(...cascade.paused);+ }++ return {+ paused,+ task,+ unlocked,+ updatedSubtasks: updatedTasks+ .filter(({ parentTaskId }) => parentTaskId === resolved.id)+ .map(({ identifier }) => identifier),+ };+}diff --git a/apps/server/src/services/task/utils.ts b/apps/server/src/services/task/utils.tsnew file mode 100644index 00000000000..c0bde0f5826--- /dev/null+++ b/apps/server/src/services/task/utils.ts@@ -0,0 +1,25 @@+import type { TaskItem } from '@lobechat/types';+import { TRPCError } from '@trpc/server';++import type { AiAgentService } from '../aiAgent';+import type { TaskServiceDeps } from './deps';++export const interruptTaskOperation = async (service: AiAgentService, operationId: string) => {+ const result = await service.interruptTask({ operationId });+ if (!result.success || result.deviceCancellationConfirmed === false) {+ throw new TRPCError({+ code: 'CONFLICT',+ message:+ 'Task interruption was not confirmed. The execution remains active; retry stopping it before starting another attempt.',+ });+ }+};++export async function resolveTaskOrThrow(+ deps: Pick<TaskServiceDeps, 'taskModel'>,+ idOrIdentifier: string,+): Promise<TaskItem> {+ const task = await deps.taskModel.resolve(idOrIdentifier);+ if (!task) throw new TRPCError({ code: 'NOT_FOUND', message: 'Task not found' });+ return task;+}