diff --git a/packages/cli/src/modules/agents/__tests__/agent-execution.service.test.ts b/packages/cli/src/modules/agents/__tests__/agent-execution.service.test.ts index c3f63b6fdd8..a6a9a423fe1 100644 --- a/packages/cli/src/modules/agents/__tests__/agent-execution.service.test.ts +++ b/packages/cli/src/modules/agents/__tests__/agent-execution.service.test.ts @@ -102,7 +102,7 @@ describe('AgentExecutionService', () => { async function recordExecution(params: RecordMessageParams): Promise { const { record, ...startParams } = params; const executionId = await service.startExecutionRecording( - { ...startParams, agentName: 'Agent' }, + { ...startParams, agentName: startParams.agentName ?? 'Agent' }, new Date(record.startTime), ); return await service.finalizeExecution(executionId, params); diff --git a/packages/cli/src/modules/agents/__tests__/test-utils/turn-queue.ts b/packages/cli/src/modules/agents/__tests__/test-utils/turn-queue.ts index dc74f97f92a..5faea844c38 100644 --- a/packages/cli/src/modules/agents/__tests__/test-utils/turn-queue.ts +++ b/packages/cli/src/modules/agents/__tests__/test-utils/turn-queue.ts @@ -127,13 +127,7 @@ export function createTestTurnQueue(user: User = { id: 'user-1', disabled: false : null; }); const agentRepository = mock(); - agentRepository.findSummariesByIds.mockImplementation(async ([id]) => [ - { - id, - name: 'Agent', - projectId: rows.find((row) => row.agentId === id)?.projectId ?? 'project-1', - }, - ]); + agentRepository.findNameByIdAndProjectId.mockResolvedValue('Agent'); const userRepository = mock(); userRepository.findByIdWithRole.mockImplementation(async (id) => id === user.id ? user : ({ id, disabled: true } as User), diff --git a/packages/cli/src/modules/agents/agent-turn-queue.service.ts b/packages/cli/src/modules/agents/agent-turn-queue.service.ts index 0853559dedc..2ad98f52999 100644 --- a/packages/cli/src/modules/agents/agent-turn-queue.service.ts +++ b/packages/cli/src/modules/agents/agent-turn-queue.service.ts @@ -337,11 +337,12 @@ export class AgentTurnQueueService { private async withAgentName( turn: AgentTurnSubmission, ): Promise { - const [agent] = await this.agentRepository.findSummariesByIds([turn.agentId]); - if (!agent || agent.projectId !== turn.projectId) { - throw new UserError(`Agent "${turn.agentId}" not found`); - } - return { ...turn, agentName: agent.name, resourceId: turn.resourceId ?? null }; + const agentName = await this.agentRepository.findNameByIdAndProjectId( + turn.agentId, + turn.projectId, + ); + if (agentName === null) throw new UserError(`Agent "${turn.agentId}" not found`); + return { ...turn, agentName, resourceId: turn.resourceId ?? null }; } /** The sender must still be able to run the agent; the drain has no request that checked that. */ diff --git a/packages/cli/src/modules/agents/repositories/agent.repository.ts b/packages/cli/src/modules/agents/repositories/agent.repository.ts index 047ec8cba81..b1350cc9da1 100644 --- a/packages/cli/src/modules/agents/repositories/agent.repository.ts +++ b/packages/cli/src/modules/agents/repositories/agent.repository.ts @@ -210,6 +210,12 @@ export class AgentRepository extends Repository { return await this.exists({ where: { id, projectId } }); } + /** Scoped to the project so ownership is enforced by the query; `null` when the agent is not in it. */ + async findNameByIdAndProjectId(id: string, projectId: string): Promise { + const agent = await this.findOne({ select: ['id', 'name'], where: { id, projectId } }); + return agent?.name ?? null; + } + async findByIdsAndProjectId( ids: string[], projectId: string,