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 <noreply@anthropic.com>
This commit is contained in:
Jaakko Husso 2026-10-16 22:00:34 +03:00 committed by GitHub
parent 5b1964908e
commit edde04df32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 8 deletions

View File

@ -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,

View File

@ -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();