From edde04df32d203b065b25fb6bd1be3eaaa43ff4a Mon Sep 17 00:00:00 2001 From: Jaakko Husso Date: Fri, 16 Oct 2026 22:00:34 +0300 Subject: [PATCH] refactor(core): Look up the queued agent once by message kind (no-changelog) enqueue now decides on the system kind once. The custom-agents setting applies to every other kind. A system message without a registered provider now fails with "Agent not found", also when the admin turned off custom agents. Co-Authored-By: Claude Opus 5.5 --- .../modules/agents/agent-message-queue.service.ts | 14 ++++++-------- .../agent-execution.repository.test.ts | 10 ++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/modules/agents/agent-message-queue.service.ts b/packages/cli/src/modules/agents/agent-message-queue.service.ts index 5aa5e90e8c8..76b73eb417a 100644 --- a/packages/cli/src/modules/agents/agent-message-queue.service.ts +++ b/packages/cli/src/modules/agents/agent-message-queue.service.ts @@ -94,20 +94,18 @@ export class AgentMessageQueueService { ): Promise<{ status: 'accepted'; item: AgentMessageQueue } | { status: 'duplicate' }> { const { payload } = input; // Only the system kind reaches a system agent. Project lookups never return instance agents. - const systemAgent = - payload.kind === 'system' ? this.systemAgents.get(input.agentId) : undefined; // System agents run even when the admin turns off custom agents. - if (!systemAgent) await this.settingsService.assertEnabled(); - const agent = - payload.kind === 'system' - ? systemAgent - : await this.agentRepository.findByIdAndProjectId(input.agentId, input.projectId); + const isSystem = payload.kind === 'system'; + if (!isSystem) await this.settingsService.assertEnabled(); + const agent = isSystem + ? this.systemAgents.get(input.agentId) + : await this.agentRepository.findByIdAndProjectId(input.agentId, input.projectId); if (!agent) throw new UserError('Agent not found'); const messageId = queuedMessageId(input.agentId, input.threadId, payload); let item: AgentMessageQueue; try { item = await this.txRunner.run({}, async (ctx) => { - if (!systemAgent) await this.settingsService.assertEnabled(ctx); + if (!isSystem) await this.settingsService.assertEnabled(ctx); await this.executionService.prepareThread( { ...input, diff --git a/packages/cli/test/integration/database/repositories/agent-execution.repository.test.ts b/packages/cli/test/integration/database/repositories/agent-execution.repository.test.ts index 4f11d64a8c7..0eb003209e3 100644 --- a/packages/cli/test/integration/database/repositories/agent-execution.repository.test.ts +++ b/packages/cli/test/integration/database/repositories/agent-execution.repository.test.ts @@ -2693,6 +2693,16 @@ describe('AgentExecutionRepository', () => { ); }); + it('refuses a system message without a provider while custom agents are turned off', async () => { + const services = recordingServices(); + await services.settingsService.setEnabled(false); + await agentRepo.ensureInstanceAgent(systemAgentId, 'Test System Agent'); + + await expect(services.queue.enqueue(systemInput(uuid(), 'Hello', 'new'))).rejects.toThrow( + 'Agent not found', + ); + }); + it('refuses a Preview message for a system agent', async () => { const services = await systemServices();