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(task): anchor taskCallback on the topic's live leaf to avoid a hidden fork (#16293)

Arvin Xu0ac0a4f3f879
diff --git a/apps/server/src/services/taskResultBridge/index.test.ts b/apps/server/src/services/taskResultBridge/index.test.ts
index 96993abff1e..7b95cbd026b 100644
--- a/apps/server/src/services/taskResultBridge/index.test.ts
+++ b/apps/server/src/services/taskResultBridge/index.test.ts
@@ -1,6 +1,7 @@
// @vitest-environment node
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { MessageModel } from '@/database/models/message';
import { TaskModel } from '@/database/models/task';
import { TaskTopicModel } from '@/database/models/taskTopic';
@@ -9,10 +10,14 @@ import { TaskResultBridgeService } from './index';
// `MessageModel.create` is a class-field arrow (instance prop, not on the
// prototype) and `AiAgentService`'s constructor builds many sub-services β€” mock
// both modules so we observe the calls without standing up the real graph.
-const { createMsg, execAgent } = vi.hoisted(() => ({ createMsg: vi.fn(), execAgent: vi.fn() }));
+const { createMsg, execAgent, getLastLeaf } = vi.hoisted(() => ({
+ createMsg: vi.fn(),
+ execAgent: vi.fn(),
+ getLastLeaf: vi.fn(),
+}));
vi.mock('@/database/models/message', () => ({
- MessageModel: vi.fn(() => ({ create: createMsg })),
+ MessageModel: vi.fn(() => ({ create: createMsg, getLastMainThreadSpineMessageId: getLastLeaf })),
}));
vi.mock('../aiAgent', () => ({
@@ -46,6 +51,9 @@ describe('TaskResultBridgeService.deliver', () => {
beforeEach(() => {
createMsg.mockReset().mockResolvedValue({ id: 'task-cb-task-1-topic-done' } as any);
+ // The creator topic's current leaf at delivery time β€” the live tail of the
+ // conversation, NOT origin.messageId (the stale create-task message).
+ getLastLeaf.mockReset().mockResolvedValue('msg-current-leaf');
execAgent
.mockReset()
.mockResolvedValue({ operationId: 'op-new', topicId: 'topic-origin' } as any);
@@ -73,7 +81,8 @@ describe('TaskResultBridgeService.deliver', () => {
const [params, id] = createMsg.mock.calls[0] as [any, string];
expect(params).toMatchObject({
agentId: 'agent-creator',
- parentId: 'msg-anchor',
+ // anchored on the topic's live leaf, not origin.messageId ('msg-anchor')
+ parentId: 'msg-current-leaf',
role: 'taskCallback',
topicId: 'topic-origin',
});
@@ -97,6 +106,14 @@ describe('TaskResultBridgeService.deliver', () => {
});
});
+ it('scopes the MessageModel to the bridge workspace so workspace tasks find their leaf', async () => {
+ await new TaskResultBridgeService(db, TEST_USER, 'ws-1').deliver(baseParams);
+
+ // Personal-mode model (workspace_id IS NULL) would miss the team topic's
+ // leaf and create the callback parentless β€” the lookup must be ws-scoped.
+ expect(MessageModel).toHaveBeenCalledWith(db, TEST_USER, 'ws-1');
+ });
+
it('skips tasks with no origin (e.g. API-created)', async () => {
findById.mockResolvedValue({ context: {}, status: 'completed' } as any);
diff --git a/apps/server/src/services/taskResultBridge/index.ts b/apps/server/src/services/taskResultBridge/index.ts
index d23bd418e9a..b43814401a5 100644
--- a/apps/server/src/services/taskResultBridge/index.ts
+++ b/apps/server/src/services/taskResultBridge/index.ts
@@ -139,7 +139,20 @@ export class TaskResultBridgeService {
// can redeliver the `on-topic-complete` webhook (which drives this bridge) β€”
// the second create loses the PK race and we skip.
const messageId = `task-cb-${taskId}-${topicId ?? params.operationId}`;
- const messageModel = new MessageModel(this.db, this.userId);
+ // Pass workspaceId: a workspace-scoped task's origin topic lives under the
+ // team workspace, so the leaf lookup + create must use the matching
+ // ownership predicate β€” a personal-mode model (workspace_id IS NULL) finds
+ // no leaf and the callback would be created parentless (LOBE-10784).
+ const messageModel = new MessageModel(this.db, this.userId, this.workspaceId);
+
+ // Anchor the callback on the creator topic's CURRENT leaf at delivery time β€”
+ // NOT origin.messageId (the assistant turn that called createTask). A task
+ // runs for minutes while the creator agent keeps talking, so origin.messageId
+ // is a stale mid-conversation node; parenting there forks a hidden sibling
+ // branch the linear UI never follows, so the user never sees the result
+ // (LOBE-10784).
+ const parentId = await messageModel.getLastMainThreadSpineMessageId(origin.topicId);
+
try {
await messageModel.create(
{
@@ -148,7 +161,7 @@ export class TaskResultBridgeService {
metadata: {
taskCallback: { identifier: taskIdentifier, reason, taskId, topicId },
},
- parentId: origin.messageId,
+ parentId,
role: 'taskCallback',
topicId: origin.topicId,
},