diff --git a/packages/agent/src/activities/agent.test.ts b/packages/agent/src/activities/agent.test.ts index 11f44904..55ed2716 100644 --- a/packages/agent/src/activities/agent.test.ts +++ b/packages/agent/src/activities/agent.test.ts @@ -597,7 +597,7 @@ describe('Agent Database Activities Integration', () => { // Retrieve main session entries (contains top-level comments mainComment1 and mainComment2) const mainEntries = await prisma.agentSessionEntry.findMany({ where: { sessionId: mainSession!.id }, - orderBy: { createdAt: 'asc' }, + orderBy: { id: 'asc' }, }) // Main session contains mainComment1 and mainComment2, but NOT threadReply1 @@ -878,6 +878,37 @@ describe('Agent Database Activities Integration', () => { ).resolves.not.toThrow() }) + it('should not tag Thread ID when only __CHAT__ placeholder comment exists under root comment', async () => { + const rootComment = await prisma.assetComment.create({ + data: { assetId: asset.id, message: 'Root comment asking agent', creatorId: user.id }, + }) + // Create placeholder comment under rootComment + await prisma.assetComment.create({ + data: { + assetId: asset.id, + message: '__CHAT__', + creatorId: user.id, + replyToId: rootComment.id, + }, + }) + + const sessionId = await initializeAgentSessionActivity({ + teamId: team.id, + agentId: 'test-agent-id', + userCommentId: rootComment.id, + userId: user.id, + }) + + const storage = new DatabaseSessionStorage(sessionId) + const pathEntries = await storage.getPathToRoot(rootComment.id) + const rootEntry = pathEntries.find((e) => e.id === rootComment.id) + + expect(rootEntry).toBeDefined() + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- entry data structure parsing + const content = (rootEntry as any)?.message?.content?.[0]?.text || '' + expect(content).not.toContain(`[Thread ID: ${rootComment.id}]`) + }) + it('should return user name and role for team member', async () => { const info = await getUserTeamInfoActivity({ userId: user.id, diff --git a/packages/agent/src/activities/agent.ts b/packages/agent/src/activities/agent.ts index 344c8f63..decc7675 100644 --- a/packages/agent/src/activities/agent.ts +++ b/packages/agent/src/activities/agent.ts @@ -547,14 +547,14 @@ export async function initializeAgentSessionActivity(params: { }) } - // 1. Fetch top-level comments up to rootComment.createdAt + // 1. Fetch top-level comments up to rootComment.createdAt sorted by id const topLevelComments = await prisma.assetComment.findMany({ where: { assetId: userComment.assetId, replyToId: null, createdAt: { lte: rootComment.createdAt }, }, - orderBy: { createdAt: 'asc' }, + orderBy: { id: 'asc' }, include: { creator: true }, }) @@ -566,7 +566,7 @@ export async function initializeAgentSessionActivity(params: { createdAt: { lte: userComment.createdAt }, id: { not: userComment.id }, }, - orderBy: { createdAt: 'asc' }, + orderBy: { id: 'asc' }, include: { creator: true }, }) : [] diff --git a/packages/agent/src/database-session-storage.ts b/packages/agent/src/database-session-storage.ts index a60ddeda..ac131168 100644 --- a/packages/agent/src/database-session-storage.ts +++ b/packages/agent/src/database-session-storage.ts @@ -265,6 +265,7 @@ export class DatabaseSessionStorage implements SessionStorage() diff --git a/packages/agent/src/tools/read-thread.ts b/packages/agent/src/tools/read-thread.ts index 98438c57..eec963e9 100644 --- a/packages/agent/src/tools/read-thread.ts +++ b/packages/agent/src/tools/read-thread.ts @@ -28,8 +28,11 @@ export const createReadThreadTool = (): AgentTool< } const replies = await prisma.assetComment.findMany({ - where: { replyToId: rootComment.id }, - orderBy: { createdAt: 'asc' }, + where: { + replyToId: rootComment.id, + message: { not: '__CHAT__' }, + }, + orderBy: { id: 'asc' }, include: { creator: true }, })