import 'dotenv/config'; import { describe, it, expect } from 'vitest'; import { randomUUID } from 'node:crypto'; import { prisma } from '@omniloq/db'; import { runAgentPersisted } from '../src/runtime'; import { seedGarageDemo, createInbound } from '../src/seed'; import { buildContext } from '../src/context-builder'; import { evaluateToolPolicy } from '../src/tool-policy'; import { detectLeak, guardFinalAnswer } from '../src/response-guard'; import { extractText } from '../src/text'; const TIMEOUT = 90000; let n = 0; function slug() { return `p15-${Date.now()}-${++n}`; } // ── Helpers (objets en mémoire pour les tests unitaires purs) ── function mkMessage(overrides: Partial = {}): any { return { id: overrides.id ?? randomUUID(), conversationId: 'c1', direction: 'inbound', senderType: 'contact', content: [{ type: 'text', text: 'message' }], createdAt: new Date(), ...overrides, }; } function mkRevision(overrides: Partial = {}): any { return { id: 'r1', agentId: 'a1', systemPrompt: 'system', provider: 'deepseek', model: 'deepseek-v4-flash', maxSteps: 6, temperature: 0.3, toolsetVersion: 'v1', ...overrides, }; } function mkTool(overrides: Partial = {}): any { return { id: 't1', agentId: 'a1', mcpServerId: 'm1', toolName: 'book_appointment', enabled: true, requiresConfirmation: false, riskLevel: 'low', timeoutMs: 15000, maxCallsPerRun: 3, idempotencyRequired: false, ...overrides, }; } function mkMcp(overrides: Partial = {}): any { return { id: 'm1', tenantId: 'x', name: 'mock', enabled: true, ...overrides }; } describe('Omniloq — Phase 1.5 (ContextBuilder + ToolPolicy + Confirmation + ResponseGuard)', () => { it('1. Context court correctement construit (tous les messages présents)', () => { const messages = [ mkMessage({ id: 'm1', content: [{ type: 'text', text: 'Bonjour' }], createdAt: new Date('2026-01-01T10:00:00Z') }), mkMessage({ id: 'm2', direction: 'outbound', senderType: 'agent', content: [{ type: 'text', text: 'Bonjour !' }], createdAt: new Date('2026-01-01T10:00:01Z') }), mkMessage({ id: 'm3', content: [{ type: 'text', text: 'Quel est mon solde ?' }], createdAt: new Date('2026-01-01T10:00:02Z') }), ]; const ctx = buildContext({ agentRevision: mkRevision(), variables: [], allMessages: messages, triggerMessage: messages[2], }); expect(ctx.includedMessageCount).toBe(3); expect(ctx.truncated).toBe(false); expect(ctx.prompt).toContain('Bonjour'); expect(ctx.prompt).toContain('Quel est mon solde ?'); }); it('2. Long historique tronqué selon budget (500 messages)', () => { const base = new Date('2026-01-01T00:00:00Z'); const messages = Array.from({ length: 500 }, (_, i) => mkMessage({ id: `m${i}`, direction: i % 2 === 0 ? 'inbound' : 'outbound', senderType: i % 2 === 0 ? 'contact' : 'agent', content: [{ type: 'text', text: `Message ${i} — un contenu volontairement long pour consommer le budget de tokens. ` }], createdAt: new Date(base.getTime() + i * 1000), }) ); const ctx = buildContext({ agentRevision: mkRevision(), variables: [], allMessages: messages, triggerMessage: messages[499], recentMessageCount: 20, maxContextTokens: 200, }); expect(ctx.truncated).toBe(true); expect(ctx.includedMessageCount).toBeLessThan(500); // jamais toute la conversation expect(ctx.includedMessageCount).toBeLessThan(20); // budget a réduit sous la fenêtre recent expect(ctx.totalTokens).toBeLessThanOrEqual(200); // budget respecté expect(ctx.prompt).toContain('Message 499'); // derniers messages conservés }); it('3. Variables agent présentes (substitution {{key}})', () => { const revision = mkRevision({ systemPrompt: 'Bienvenue au garage {{garage_name}}.' }); const variables = [{ id: 'v1', agentId: 'a1', key: 'garage_name', value: 'Garage Atlas' }]; const ctx = buildContext({ agentRevision: revision, variables, allMessages: [mkMessage()], triggerMessage: mkMessage(), }); expect(ctx.system).toContain('Garage Atlas'); expect(ctx.system).not.toContain('{{garage_name}}'); expect(ctx.variableNames).toEqual(['garage_name']); }); it('4. Tool autorisé → exécuté (ALLOW)', async () => { const { conversation } = await seedGarageDemo(slug()); await createInbound(conversation.id, 'Quel est mon solde ?'); const r = await runAgentPersisted(conversation.id); expect(r.toolCalls.some((t) => t.name === 'get_balance' && t.status === 'success')).toBe(true); expect(r.text).toMatch(/150|solde/i); }, TIMEOUT); it('5. Tool désactivé → refusé sans MCP (DENY)', async () => { // Unitaire : la policy refuse un tool désactivé const policy = evaluateToolPolicy({ agentTool: mkTool({ enabled: false }), mcpServer: mkMcp(), callsSoFarThisRun: 0, }); expect(policy.decision).toBe('DENY'); expect(policy.reason).toBe('tool_disabled'); // Intégration : le MCP n'est jamais exécuté pour ce tool const { conversation, agent } = await seedGarageDemo(slug()); await prisma.agentTool.updateMany({ where: { agentId: agent.id, toolName: 'get_balance' }, data: { enabled: false }, }); await createInbound(conversation.id, 'Quel est mon solde ?'); const r = await runAgentPersisted(conversation.id); const calls = await prisma.toolCall.findMany({ where: { runId: r.runId, toolName: 'get_balance' } }); // Aucun side effect MCP : jamais de status success ni de result expect(calls.every((t) => t.status !== 'success' && t.result === null)).toBe(true); }, TIMEOUT); it('6. ToolPolicy : maxCallsPerRun + décisions ALLOW/CONFIRM/DENY', () => { // ALLOW en dessous de la limite expect( evaluateToolPolicy({ agentTool: mkTool({ maxCallsPerRun: 3 }), mcpServer: mkMcp(), callsSoFarThisRun: 2 }).decision ).toBe('ALLOW'); // DENY à la limite (le LLM ne peut pas dépasser maxCallsPerRun) expect( evaluateToolPolicy({ agentTool: mkTool({ maxCallsPerRun: 3 }), mcpServer: mkMcp(), callsSoFarThisRun: 3 }).decision ).toBe('DENY'); // requiresConfirmation → CONFIRM expect( evaluateToolPolicy({ agentTool: mkTool({ requiresConfirmation: true }), mcpServer: mkMcp(), callsSoFarThisRun: 0 }).decision ).toBe('CONFIRM'); // riskLevel high → CONFIRM expect( evaluateToolPolicy({ agentTool: mkTool({ riskLevel: 'high' }), mcpServer: mkMcp(), callsSoFarThisRun: 0 }).decision ).toBe('CONFIRM'); // tool non enregistré pour l'agent → DENY expect(evaluateToolPolicy({ agentTool: null, mcpServer: mkMcp(), callsSoFarThisRun: 0 }).decision).toBe('DENY'); // MCP désactivé → DENY expect( evaluateToolPolicy({ agentTool: mkTool(), mcpServer: mkMcp({ enabled: false }), callsSoFarThisRun: 0 }).decision ).toBe('DENY'); }); it('7. Tool sensible → AWAITING_CONFIRMATION (aucune exécution)', async () => { const { conversation, agent } = await seedGarageDemo(slug()); await prisma.agentTool.updateMany({ where: { agentId: agent.id, toolName: 'book_appointment' }, data: { requiresConfirmation: true }, }); await createInbound(conversation.id, 'Réserve-moi un rendez-vous le 2026-09-23 à 10:00.'); const r = await runAgentPersisted(conversation.id); expect(r.awaitingConfirmation).toBe(true); expect(r.finishReason).toBe('awaiting_confirmation'); const pending = r.toolCalls.find((t) => t.status === 'awaiting_confirmation'); expect(pending).toBeTruthy(); const tc = await prisma.toolCall.findUnique({ where: { id: pending!.id } }); expect(tc?.status).toBe('awaiting_confirmation'); expect(tc?.result).toBeNull(); // pas exécuté expect(tc?.confirmationExpiresAt).toBeTruthy(); const outbound = await prisma.message.findFirst({ where: { conversationId: conversation.id, direction: 'outbound' }, orderBy: { createdAt: 'desc' }, }); expect(extractText(outbound?.content)).toContain('Confirmez'); }, TIMEOUT); it('8. Confirmation ultérieure → tool exécuté et nouveau run', async () => { const { conversation, agent } = await seedGarageDemo(slug()); await prisma.agentTool.updateMany({ where: { agentId: agent.id, toolName: 'book_appointment' }, data: { requiresConfirmation: true }, }); await createInbound(conversation.id, 'Réserve-moi un rendez-vous le 2026-09-23 à 14:00.'); const r1 = await runAgentPersisted(conversation.id); expect(r1.awaitingConfirmation).toBe(true); const pendingId = r1.toolCalls.find((t) => t.status === 'awaiting_confirmation')!.id; // Plus tard : « Oui » → résolution await createInbound(conversation.id, 'Oui'); const r2 = await runAgentPersisted(conversation.id); expect(r2.runId).not.toBe(r1.runId); // nouveau AgentRun const tc = await prisma.toolCall.findUnique({ where: { id: pendingId } }); expect(tc?.status).toBe('success'); // exécuté expect(tc?.confirmedAt).toBeTruthy(); expect(tc?.confirmationDecision).toBe('confirmed'); expect(tc?.result).toBeTruthy(); const runs = await prisma.agentRun.findMany({ where: { conversationId: conversation.id } }); expect(runs.length).toBe(2); }, TIMEOUT); it('9. Confirmation expirée → aucune action métier', async () => { const { conversation, agent } = await seedGarageDemo(slug()); await prisma.agentTool.updateMany({ where: { agentId: agent.id, toolName: 'book_appointment' }, data: { requiresConfirmation: true }, }); await createInbound(conversation.id, 'Réserve-moi un rendez-vous le 2026-09-23 à 14:00.'); const r1 = await runAgentPersisted(conversation.id); expect(r1.awaitingConfirmation).toBe(true); const pendingId = r1.toolCalls.find((t) => t.status === 'awaiting_confirmation')!.id; // Expirer la demande await prisma.toolCall.update({ where: { id: pendingId }, data: { confirmationExpiresAt: new Date(Date.now() - 1000) }, }); // « Oui » → ne doit PAS exécuter (demande expirée) await createInbound(conversation.id, 'Oui'); await runAgentPersisted(conversation.id); const tc = await prisma.toolCall.findUnique({ where: { id: pendingId } }); expect(tc?.status).toBe('awaiting_confirmation'); // toujours en attente expect(tc?.result).toBeNull(); // aucune action métier expect(tc?.confirmedAt).toBeNull(); }, TIMEOUT); it('10. Réponse suspecte de reasoning → jamais envoyée brute', async () => { // Détection expect(detectLeak('Bonjour, comment puis-je vous aider ?').safe).toBe(true); expect(detectLeak('Je dois vérifier le solde').safe).toBe(false); expect(detectLeak('Let me think through this problem step by step').safe).toBe(false); expect(detectLeak('Voici mon raisonnement : ...').safe).toBe(false); // Guard : SUSPECT → régénération (pas de mutilation regex) const leaked = 'analyse interne secrète'; const guarded = await guardFinalAnswer(leaked, async () => 'Réponse propre et directe au client.'); expect(guarded.regenerated).toBe(true); expect(guarded.text).toBe('Réponse propre et directe au client.'); // safe → pas de régénération let called = false; const safe = await guardFinalAnswer('Réponse normale.', async () => { called = true; return 'x'; }); expect(safe.regenerated).toBe(false); expect(called).toBe(false); expect(safe.text).toBe('Réponse normale.'); }); });