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(agent-share): count Workspace agents' own runs in share stats (#23473)

YuTengjingbdea230ad8d2
diff --git a/packages/database/src/models/__tests__/agentShareProfile.test.ts b/packages/database/src/models/__tests__/agentShareProfile.test.ts
index b20b74e7feb..2de2a7c5a37 100644
--- a/packages/database/src/models/__tests__/agentShareProfile.test.ts
+++ b/packages/database/src/models/__tests__/agentShareProfile.test.ts
@@ -346,6 +346,81 @@ describe('AgentShareProfileModel', () => {
expect((await model.listFeaturedWorks(agent, ['unsafe-url']))[0].url).toBeNull();
});
+ it('measures a Workspace agent from creator rows in that same Workspace', async () => {
+ const workspaceId = 'profile-workspace';
+ await db.insert(workspaces).values({
+ id: 'other-workspace',
+ name: 'Other',
+ slug: 'other-workspace',
+ primaryOwnerId: owner,
+ });
+ await db.update(agents).set({ workspaceId }).where(eq(agents.id, agent));
+ await db.update(topics).set({ workspaceId }).where(eq(topics.id, creatorTopic));
+ await db.update(topics).set({ workspaceId }).where(eq(topics.id, visitorTopic));
+ await db.insert(topics).values([
+ { id: 'personal-topic', userId: owner, agentId: agent },
+ {
+ id: 'other-workspace-topic',
+ userId: owner,
+ agentId: agent,
+ workspaceId: 'other-workspace',
+ },
+ ]);
+
+ await addWork('workspace-work', { workspaceId });
+ const latest = await addVersion('workspace-work', 2, 4, 'operation-2');
+ await addWork('personal-work');
+ await addWork(
+ 'other-workspace-work',
+ { originTopicId: 'other-workspace-topic', workspaceId: 'other-workspace' },
+ 'other-workspace-topic',
+ );
+ await addWork('visitor-work', { originTopicId: visitorTopic, workspaceId }, visitorTopic);
+ await addWork('mixed-work', { workspaceId });
+ await addVersion('mixed-work', 2, 3, 'personal-operation', 'personal-topic');
+
+ const startedAt = new Date('2026-01-01T00:00:00Z');
+ const operation = (
+ id: string,
+ seconds: number,
+ overrides: Partial<typeof agentOperations.$inferInsert> = {},
+ ) => ({
+ id,
+ userId: owner,
+ agentId: agent,
+ topicId: creatorTopic,
+ workspaceId,
+ status: 'done' as const,
+ startedAt,
+ completedAt: new Date(startedAt.getTime() + seconds * 1000),
+ ...overrides,
+ });
+ await db.insert(agentOperations).values([
+ operation('workspace-first', 10),
+ operation('workspace-second', 30),
+ operation('visitor', 500, { topicId: visitorTopic }),
+ operation('personal', 500, { topicId: 'personal-topic', workspaceId: null }),
+ operation('other-workspace', 500, {
+ topicId: 'other-workspace-topic',
+ workspaceId: 'other-workspace',
+ }),
+ ]);
+
+ expect(await model.getStats(agent)).toEqual({
+ averageOperationDurationSeconds: 20,
+ averageWorkCost: 6,
+ lastDeliveredAt: latest.createdAt,
+ workCount: 1,
+ });
+ expect((await model.listEligibleWorks(agent)).items.map(({ id }) => id)).toEqual([
+ 'workspace-work',
+ ]);
+ await expect(model.validateFeaturedWorks(agent, ['workspace-work'])).resolves.toBeUndefined();
+ await expect(model.validateFeaturedWorks(agent, ['personal-work'])).rejects.toMatchObject({
+ code: 'BAD_REQUEST',
+ });
+ });
+
it('stops exposing profile data for a trashed agent', async () => {
await addWork('first');
await db.update(agents).set({ isDeleted: true }).where(eq(agents.id, agent));
diff --git a/packages/database/src/models/agentShareProfile.ts b/packages/database/src/models/agentShareProfile.ts
index 2ca5c3e0802..a0bf084bfc5 100644
--- a/packages/database/src/models/agentShareProfile.ts
+++ b/packages/database/src/models/agentShareProfile.ts
@@ -2,6 +2,7 @@ import type { SharedAgentDeliveryStats, SharedAgentWork } from '@lobechat/types'
import { TRPCError } from '@trpc/server';
import {
and,
+ type AnyColumn,
avg,
count,
desc,
@@ -29,6 +30,19 @@ const currentTopics = alias(topics, 'share_work_current_topics');
const historyVersions = alias(workVersions, 'share_work_history_versions');
const historyTopics = alias(topics, 'share_work_history_topics');
+/**
+ * A profile row belongs to the shared agent's own scope: personal rows for a
+ * personal agent, rows of the agent's Workspace for a Workspace agent. The
+ * agent row is the single source of that scope, so a Work or run left in the
+ * other scope (or an agent moved after the fact) never counts. Callers must
+ * join `agents` on the shared agent id; subqueries reference it as an outer
+ * column.
+ */
+const inAgentScope = (workspaceId: AnyColumn) =>
+ sql`${workspaceId} is not distinct from ${agents.workspaceId}`;
+const outsideAgentScope = (workspaceId: AnyColumn) =>
+ sql`${workspaceId} is distinct from ${agents.workspaceId}`;
+
/** Owner-scoped source for explicitly curated share profiles, not a general resource grant. */
export class AgentShareProfileModel {
constructor(
@@ -71,18 +85,17 @@ export class AgentShareProfileModel {
eq(works.userId, this.userId),
eq(works.originAgentId, agentId),
eq(agents.userId, this.userId),
- isNull(agents.workspaceId),
sql`${agents.isDeleted} is not true`,
- isNull(works.workspaceId),
+ inAgentScope(works.workspaceId),
isNull(works.deletedAt),
sql`${works.isDeleted} is not true`,
eq(originTopics.userId, this.userId),
- isNull(originTopics.workspaceId),
+ inAgentScope(originTopics.workspaceId),
isNull(originTopics.senderId),
isNull(originTopics.deletedAt),
sql`${originTopics.isDeleted} is not true`,
eq(currentTopics.userId, this.userId),
- isNull(currentTopics.workspaceId),
+ inAgentScope(currentTopics.workspaceId),
isNull(currentTopics.senderId),
isNull(currentTopics.deletedAt),
sql`${currentTopics.isDeleted} is not true`,
@@ -99,7 +112,7 @@ export class AgentShareProfileModel {
isNull(historyTopics.id),
isNotNull(historyTopics.senderId),
ne(historyTopics.userId, this.userId),
- isNotNull(historyTopics.workspaceId),
+ outsideAgentScope(historyTopics.workspaceId),
),
),
),
@@ -117,7 +130,7 @@ export class AgentShareProfileModel {
and(
eq(documents.id, works.resourceId),
eq(documents.userId, this.userId),
- isNull(documents.workspaceId),
+ inAgentScope(documents.workspaceId),
isNull(documents.deletedAt),
sql`${documents.isDeleted} is not true`,
),
@@ -134,7 +147,7 @@ export class AgentShareProfileModel {
and(
eq(tasks.id, works.resourceId),
eq(tasks.createdByUserId, this.userId),
- isNull(tasks.workspaceId),
+ inAgentScope(tasks.workspaceId),
isNull(tasks.deletedAt),
sql`${tasks.isDeleted} is not true`,
),
@@ -281,11 +294,10 @@ export class AgentShareProfileModel {
eq(agentOperations.userId, this.userId),
eq(agentOperations.agentId, agentId),
eq(agents.userId, this.userId),
- isNull(agents.workspaceId),
sql`${agents.isDeleted} is not true`,
- isNull(agentOperations.workspaceId),
+ inAgentScope(agentOperations.workspaceId),
eq(topics.userId, this.userId),
- isNull(topics.workspaceId),
+ inAgentScope(topics.workspaceId),
isNull(topics.senderId),
isNull(topics.deletedAt),
sql`${topics.isDeleted} is not true`,