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(acceptance): resolve the origin agent from the topic when the round recorded none (#19953)

Arvin Xu6f47a06ad48a
diff --git a/apps/server/src/services/verify/__tests__/acceptanceOrigin.test.ts b/apps/server/src/services/verify/__tests__/acceptanceOrigin.test.ts
new file mode 100644
index 00000000000..84430201a56
--- /dev/null
+++ b/apps/server/src/services/verify/__tests__/acceptanceOrigin.test.ts
@@ -0,0 +1,86 @@
+// @vitest-environment node
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { AcceptanceService } from '../acceptanceService';
+
+const getAgentAvatarsByIds = vi.fn();
+const findTopicById = vi.fn();
+
+vi.mock('@/database/models/agent', () => ({
+ AgentModel: vi.fn().mockImplementation(function () {
+ return { getAgentAvatarsByIds };
+ }),
+}));
+
+vi.mock('@/database/models/topic', () => ({
+ TopicModel: vi.fn().mockImplementation(function () {
+ return { findById: findTopicById };
+ }),
+}));
+
+const agentRow = (id: string) => ({ avatar: '🦊', backgroundColor: null, id, title: 'Fox' });
+const runWithOrigin = (origin: Record<string, string>) => ({ metadata: { origin } }) as never;
+
+/**
+ * Rounds published from a dispatched run (task / goal / device) carry only the
+ * topic β€” the connector strips the ambient agent id. The topic row still names
+ * its agent, so the origin must resolve to it instead of hiding the
+ * conversation panel and the agent chip.
+ */
+describe('AcceptanceService.resolveOrigin', () => {
+ const service = new AcceptanceService({} as never, 'user', 'ws');
+
+ beforeEach(() => {
+ getAgentAvatarsByIds.mockReset();
+ findTopicById.mockReset();
+ });
+
+ it("falls back to the topic's agent when the round recorded none", async () => {
+ findTopicById.mockResolvedValue({ agentId: 'agt_topic', id: 'tpc_1', title: 'Harbor' });
+ getAgentAvatarsByIds.mockResolvedValue([agentRow('agt_topic')]);
+
+ const origin = await service.resolveOrigin([runWithOrigin({ topicId: 'tpc_1' })]);
+
+ expect(getAgentAvatarsByIds).toHaveBeenCalledWith(['agt_topic']);
+ expect(origin).toEqual({
+ agent: agentRow('agt_topic'),
+ topic: { id: 'tpc_1', title: 'Harbor' },
+ });
+ });
+
+ it('keeps the recorded agent over the topic owner', async () => {
+ findTopicById.mockResolvedValue({ agentId: 'agt_topic', id: 'tpc_1', title: 'Harbor' });
+ getAgentAvatarsByIds.mockResolvedValue([agentRow('agt_recorded')]);
+
+ const origin = await service.resolveOrigin([
+ runWithOrigin({ agentId: 'agt_recorded', topicId: 'tpc_1' }),
+ ]);
+
+ expect(getAgentAvatarsByIds).toHaveBeenCalledWith(['agt_recorded']);
+ expect(origin?.agent?.id).toBe('agt_recorded');
+ });
+
+ it('looks up a recorded agent without waiting on the topic read', async () => {
+ let resolveTopic!: (row: unknown) => void;
+ findTopicById.mockReturnValue(new Promise((resolve) => (resolveTopic = resolve)));
+ getAgentAvatarsByIds.mockResolvedValue([agentRow('agt_recorded')]);
+
+ const pending = service.resolveOrigin([
+ runWithOrigin({ agentId: 'agt_recorded', topicId: 'tpc_1' }),
+ ]);
+ await Promise.resolve();
+
+ expect(getAgentAvatarsByIds).toHaveBeenCalledWith(['agt_recorded']);
+ resolveTopic({ agentId: 'agt_topic', id: 'tpc_1', title: 'Harbor' });
+ expect((await pending)?.agent?.id).toBe('agt_recorded');
+ });
+
+ it('leaves the agent empty when neither the round nor the topic names one', async () => {
+ findTopicById.mockResolvedValue({ agentId: null, id: 'tpc_1', title: 'Harbor' });
+
+ const origin = await service.resolveOrigin([runWithOrigin({ topicId: 'tpc_1' })]);
+
+ expect(getAgentAvatarsByIds).not.toHaveBeenCalled();
+ expect(origin).toEqual({ agent: null, topic: { id: 'tpc_1', title: 'Harbor' } });
+ });
+});
diff --git a/apps/server/src/services/verify/acceptanceService.ts b/apps/server/src/services/verify/acceptanceService.ts
index cd6d8bcbe9b..56473280084 100644
--- a/apps/server/src/services/verify/acceptanceService.ts
+++ b/apps/server/src/services/verify/acceptanceService.ts
@@ -1407,20 +1407,26 @@ export class AcceptanceService {
const origin = [...runs].reverse().find((run) => run.metadata?.origin)?.metadata?.origin;
if (!origin?.agentId && !origin?.topicId) return null;
- const [agent, topic] = await Promise.all([
- origin.agentId
+ const topicRowPromise = origin.topicId
+ ? new TopicModel(this.db, this.userId, this.workspaceId)
+ .findById(origin.topicId)
+ .catch(() => null)
+ : Promise.resolve(null);
+ // Dispatched runs (task / goal / device) record only the topic β€” the
+ // connector strips the ambient agent id β€” so the topic's own agent stands in.
+ // A recorded agent does not wait on the topic read.
+ const agentPromise = (
+ origin.agentId ? Promise.resolve(origin.agentId) : topicRowPromise.then((row) => row?.agentId)
+ ).then((agentId) =>
+ agentId
? new AgentModel(this.db, this.userId, this.workspaceId)
- .getAgentAvatarsByIds([origin.agentId])
+ .getAgentAvatarsByIds([agentId])
.then((rows) => rows[0] ?? null)
.catch(() => null)
: null,
- origin.topicId
- ? new TopicModel(this.db, this.userId, this.workspaceId)
- .findById(origin.topicId)
- .then((row) => (row ? { id: row.id, title: row.title ?? null } : null))
- .catch(() => null)
- : null,
- ]);
+ );
+ const [topicRow, agent] = await Promise.all([topicRowPromise, agentPromise]);
+ const topic = topicRow ? { id: topicRow.id, title: topicRow.title ?? null } : null;
if (!agent && !topic) return null;
return { agent, topic };
};