import type { AwilixContainer } from 'awilix' import type { AiAgentDefinition } from '../ai-agent-definition' import type { AiToolDefinition } from '../types' const generateObjectMock = jest.fn() const generateTextMock = jest.fn() const convertToModelMessagesMock = jest.fn((messages: unknown) => messages) const prepareMutationMock = jest.fn() jest.mock('ai', () => { const actual = jest.requireActual('ai') return { ...actual, generateObject: (...args: unknown[]) => generateObjectMock(...args), generateText: (...args: unknown[]) => generateTextMock(...args), convertToModelMessages: (...args: unknown[]) => convertToModelMessagesMock(...args), } }) jest.mock('@open-mercato/shared/lib/ai/llm-provider-registry', () => ({ llmProviderRegistry: { resolveFirstConfigured: () => ({ id: 'test-provider', defaultModel: 'provider-default-model', resolveApiKey: () => 'test-api-key', createModel: (options: { modelId: string }) => ({ id: options.modelId }), isConfigured: () => true, }), }, })) jest.mock('../prepare-mutation', () => { const actual = jest.requireActual('../prepare-mutation') return { ...actual, prepareMutation: (...args: unknown[]) => prepareMutationMock(...args), } }) import { z } from 'zod' import { resetAgentRegistryForTests, seedAgentRegistryForTests } from '../agent-registry' import { toolRegistry, registerMcpTool } from '../tool-registry' import { runAiAgentObject } from '../agent-runtime' const outputSchema = z.object({ verdict: z.string() }) const baseAuth = { tenantId: 'tenant-1', organizationId: 'org-1', userId: 'user-1', features: ['*'], isSuperAdmin: true, } const container = { resolve: (name: string) => { throw new Error(`unknown dependency ${name}`) }, } as unknown as AwilixContainer function seedMutationAgent(mutationPolicy: AiAgentDefinition['mutationPolicy'] = 'confirm-required'): void { const readTool: AiToolDefinition = { name: 'customers.read_deal', description: 'read', inputSchema: z.object({}).passthrough(), handler: async () => ({ ok: true }), } const updateTool: AiToolDefinition = { name: 'customers.update_deal', description: 'update', inputSchema: z.object({}).passthrough(), handler: async () => ({ shouldNeverRun: true }), isMutation: true, } registerMcpTool(readTool, { moduleId: 'customers' }) registerMcpTool(updateTool, { moduleId: 'customers' }) const agent: AiAgentDefinition = { id: 'customers.writer_agent', moduleId: 'customers', label: 'writer', description: 'writer', systemPrompt: 'System prompt base.', executionMode: 'object', readOnly: false, mutationPolicy, allowedTools: ['customers.read_deal', 'customers.update_deal'], output: { schemaName: 'Verdict', schema: outputSchema }, } seedAgentRegistryForTests([agent]) } type ExecutableTool = { execute: (args: unknown, options: unknown) => Promise } function mockGenerateTextCallingUpdate(): void { generateTextMock.mockImplementation(async (args: { tools: Record }) => { const update = args.tools['customers__update_deal'] if (update) { await update.execute({ dealId: 'deal-1', patch: { stage: 'won' } }, { toolCallId: 'call-1', messages: [] }) } return { output: { verdict: 'ok' }, finishReason: 'stop', usage: { inputTokens: 1, outputTokens: 1 }, } }) } describe('runAiAgentObject channel mutation policy (MCP parity ยง3.1)', () => { beforeEach(() => { jest.clearAllMocks() resetAgentRegistryForTests() toolRegistry.clear() prepareMutationMock.mockResolvedValue({ uiPart: { componentId: 'mutation-preview-card', props: {} }, pendingAction: { id: 'pending-1', expiresAt: new Date('2026-10-22T12:00:00Z') }, }) }) afterAll(() => { resetAgentRegistryForTests() toolRegistry.clear() }) it("forces read-only for channel 'mcp' in 'deny' mode", async () => { seedMutationAgent() mockGenerateTextCallingUpdate() const result = await runAiAgentObject({ agentId: 'customers.writer_agent', input: 'judge', authContext: baseAuth, enableTools: true, container, channel: 'mcp', mutationMode: 'deny', }) const callArg = generateTextMock.mock.calls[0][0] as { tools: Record; system: string } expect(Object.keys(callArg.tools)).toEqual(['customers__read_deal']) expect(callArg.system).toContain('Effective policy: read-only.') expect(prepareMutationMock).not.toHaveBeenCalled() if (result.mode !== 'generate') throw new Error('unreachable') expect(result.pendingActionIds).toBeUndefined() }) it("keeps the agent policy in 'approval' mode and returns the queued pending action ids", async () => { seedMutationAgent() mockGenerateTextCallingUpdate() const result = await runAiAgentObject({ agentId: 'customers.writer_agent', input: 'judge', authContext: baseAuth, enableTools: true, container, channel: 'mcp', mutationMode: 'approval', apiKeyId: 'key-1', correlationId: 'corr-1', }) const callArg = generateTextMock.mock.calls[0][0] as { tools: Record; system: string } expect(Object.keys(callArg.tools)).toEqual( expect.arrayContaining(['customers__read_deal', 'customers__update_deal']), ) expect(callArg.system).toContain('Effective policy: confirm-required.') expect(prepareMutationMock).toHaveBeenCalledTimes(1) const [prepareInput] = prepareMutationMock.mock.calls[0] as [Record] expect(prepareInput).toMatchObject({ channel: 'mcp', apiKeyId: 'key-1', correlationId: 'corr-1', }) if (result.mode !== 'generate') throw new Error('unreachable') expect(result.pendingActionIds).toEqual(['pending-1']) }) it("tells the model every write is queued for a destructive-confirm-required agent on 'mcp' in 'approval' mode", async () => { seedMutationAgent('destructive-confirm-required') mockGenerateTextCallingUpdate() const result = await runAiAgentObject({ agentId: 'customers.writer_agent', input: 'judge', authContext: baseAuth, enableTools: true, container, channel: 'mcp', mutationMode: 'approval', }) const callArg = generateTextMock.mock.calls[0][0] as { system: string } expect(callArg.system).toContain('Effective policy: destructive-confirm-required.') expect(callArg.system).toContain('Tools that REQUIRE APPROVAL under the effective policy: customers.update_deal.') expect(callArg.system).toContain('queued as a pending action') expect(callArg.system).not.toContain('WILL RUN DIRECTLY') expect(callArg.system).not.toContain('inline approval card') expect(prepareMutationMock).toHaveBeenCalledTimes(1) if (result.mode !== 'generate') throw new Error('unreachable') expect(result.pendingActionIds).toEqual(['pending-1']) }) it('keeps the direct-run prompt for a destructive-confirm-required agent on the agent channel', async () => { seedMutationAgent('destructive-confirm-required') generateTextMock.mockResolvedValue({ output: { verdict: 'ok' }, finishReason: 'stop', usage: { inputTokens: 1, outputTokens: 1 }, }) await runAiAgentObject({ agentId: 'customers.writer_agent', input: 'judge', authContext: baseAuth, enableTools: true, container, }) const callArg = generateTextMock.mock.calls[0][0] as { system: string } expect(callArg.system).toContain( 'Tools that WILL RUN DIRECTLY (no approval card, no pending action) under the effective policy: customers.update_deal.', ) expect(callArg.system).not.toContain('REQUIRE APPROVAL') expect(prepareMutationMock).not.toHaveBeenCalled() }) it('keeps default agent-channel behaviour when no channel is supplied', async () => { seedMutationAgent() mockGenerateTextCallingUpdate() const result = await runAiAgentObject({ agentId: 'customers.writer_agent', input: 'judge', authContext: baseAuth, enableTools: true, container, }) const [prepareInput] = prepareMutationMock.mock.calls[0] as [Record] expect(prepareInput.channel).toBe('agent') if (result.mode !== 'generate') throw new Error('unreachable') expect(result.pendingActionIds).toEqual(['pending-1']) }) })