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(heteroFinish): trigger task lifecycle on cloud sandbox agent completion (#14681)

LiJiana5299696de6d
diff --git a/src/server/routers/lambda/aiAgent.ts b/src/server/routers/lambda/aiAgent.ts
index 2d6b818004e..82b12694f7f 100644
--- a/src/server/routers/lambda/aiAgent.ts
+++ b/src/server/routers/lambda/aiAgent.ts
@@ -9,6 +9,8 @@ import pMap from 'p-map';
import { z } from 'zod';
import { MessageModel } from '@/database/models/message';
+import { TaskModel } from '@/database/models/task';
+import { TaskTopicModel } from '@/database/models/taskTopic';
import { ThreadModel } from '@/database/models/thread';
import { TopicModel } from '@/database/models/topic';
import { authedProcedure, heteroAuthedProcedure, router } from '@/libs/trpc/lambda';
@@ -17,6 +19,7 @@ import { AgentRuntimeService } from '@/server/services/agentRuntime';
import { AiAgentService } from '@/server/services/aiAgent';
import { AiChatService } from '@/server/services/aiChat';
import { HeterogeneousAgentService } from '@/server/services/heterogeneousAgent';
+import { TaskLifecycleService } from '@/server/services/taskLifecycle';
import { nanoid } from '@/utils/uuid';
const log = debug('lobe-server:ai-agent-router');
@@ -1293,6 +1296,45 @@ export const aiAgentRouter = router({
sessionId,
topicId,
});
+
+ // Trigger task lifecycle transition — mirrors the onComplete hook that the
+ // normal LLM execAgent path dispatches after AgentRuntimeService finishes.
+ // The hetero path spawns the sandbox fire-and-forget and returns early, so
+ // the hook is never registered or dispatched; we must call onTopicComplete
+ // explicitly here when the CLI signals process exit.
+ //
+ // Guard: heteroFinish can be called more than once for the same operation
+ // (signal path sends cancelled, normal exit sends the real result, and
+ // transient transport failures can replay). onTopicComplete is NOT
+ // idempotent (reason='error' creates briefs), so skip the call when the
+ // topic is already in a terminal state.
+ const TERMINAL_TOPIC_STATUSES = new Set(['canceled', 'completed', 'failed', 'timeout']);
+ try {
+ const taskTopicModel = new TaskTopicModel(ctx.serverDB, ctx.userId);
+ const taskTopic = await taskTopicModel.findByTopicId(topicId);
+ if (taskTopic && !TERMINAL_TOPIC_STATUSES.has(taskTopic.status)) {
+ const taskModel = new TaskModel(ctx.serverDB, ctx.userId);
+ const task = await taskModel.findById(taskTopic.taskId);
+ if (task) {
+ const reason =
+ result === 'success' ? 'done' : result === 'cancelled' ? 'interrupted' : 'error';
+ const taskLifecycle = new TaskLifecycleService(ctx.serverDB, ctx.userId);
+ await taskLifecycle.onTopicComplete({
+ errorMessage: error?.message,
+ operationId,
+ reason,
+ taskId: task.id,
+ taskIdentifier: task.identifier,
+ topicId,
+ });
+ }
+ }
+ } catch (lifecycleErr: any) {
+ // Non-fatal: log but do not fail the heteroFinish ack. The CLI has
+ // already finished; failing here would cause it to retry unnecessarily.
+ log('heteroFinish: task lifecycle update failed (non-fatal): %s', lifecycleErr?.message);
+ }
+
return { ack: true as const };
} catch (err: any) {
log('heteroFinish failed: %s', err?.message);