import { createLogger } from '@open-mercato/shared/lib/logger' import { createHash } from 'node:crypto' import type { AwilixContainer } from 'awilix' import type { EntityManager } from '@mikro-orm/postgresql' import type { AiAgentDefinition, AiAgentMutationPolicy } from './ai-agent-definition' import type { AiChatRequestContext, AiUiPart } from './attachment-bridge-types' import type { AiToolDefinition, AiToolFieldDiffDisplayHints, AiToolLoadBeforeRecord, AiToolLoadBeforeSingleRecord, McpToolContext, } from './types' import { resolveEffectiveMutationPolicy } from './agent-policy' import { AiPendingActionRepository } from '../data/repositories/AiPendingActionRepository' import type { AiPendingAction } from '../data/entities' import { AI_RAW_TOOL_AGENT_ID, resolveMcpPendingActionTtlSeconds, type AiPendingActionChannel, type AiPendingActionFieldDiff, type AiPendingActionRecordDiff, } from './pending-action-types' import { notifyPendingActionAwaitingApproval } from './pending-action-notify' const logger = createLogger('ai_assistant') /** * Structured error raised by {@link prepareMutation}. Callers (today the * agent-runtime tool wrapper installed by `resolveAiAgentTools`) turn this * into a tool-call failure that the model surfaces back to the user without * leaking internals. The runtime NEVER reaches this helper when the agent * is declared read-only — the policy gate rejects the tool call upstream — * but we keep the fail-closed check as a defensive guard. */ export class AiMutationPreparationError extends Error { constructor( public readonly code: | 'not_a_mutation_tool' | 'read_only_agent' | 'tenant_scope_missing' | 'container_missing' | 'em_missing' | 'requester_unresolved', message: string, ) { super(message) this.name = 'AiMutationPreparationError' } } export interface PrepareMutationInput { agent: AiAgentDefinition tool: AiToolDefinition toolCallArgs: Record conversationId?: string | null /** * Optional downgrade the caller already resolved (mirror of * `resolveAiAgentTools({ mutationPolicyOverride })`). When omitted, the * agent's code-declared policy stands alone. */ mutationPolicyOverride?: AiAgentMutationPolicy | null /** * Surface the agent run was invoked through. Defaults to `'agent'`; an * agent run on behalf of an MCP / tools-api caller stamps its rows with * that channel so approvers can see where the write came from. */ channel?: AiPendingActionChannel apiKeyId?: string | null correlationId?: string | null /** * Deterministic clock hook for tests. Defaults to `new Date()`. */ now?: Date } export interface PrepareMutationContext extends AiChatRequestContext { container: AwilixContainer } export interface PrepareMutationResult { uiPart: AiUiPart pendingAction: AiPendingAction /** * True when `pendingAction` is an existing row the repository returned * as-is (still `pending`, or `confirmed`/`executing` because the * mutation already ran) instead of a freshly inserted row. */ reused: boolean } const MUTATION_PREVIEW_CARD_COMPONENT_ID = 'mutation-preview-card' const NO_RESOLVER_SIDE_EFFECTS_MESSAGE = 'Tool did not declare a field-diff resolver; action will proceed without a preview.' function assertTenantScope(ctx: { tenantId: string | null }): string { if (!ctx.tenantId) { throw new AiMutationPreparationError( 'tenant_scope_missing', 'prepareMutation requires a tenant-scoped request context.', ) } return ctx.tenantId } function resolveEm(container: AwilixContainer): EntityManager { if (!container) { throw new AiMutationPreparationError( 'container_missing', 'prepareMutation requires an Awilix container to resolve the EntityManager.', ) } let em: EntityManager | null = null try { em = container.resolve('em') } catch { em = null } if (!em) { throw new AiMutationPreparationError( 'em_missing', 'prepareMutation could not resolve "em" from the container.', ) } return em } function toolHandlerContext(ctx: PrepareMutationContext): McpToolContext { return { tenantId: ctx.tenantId, organizationId: ctx.organizationId, userId: ctx.userId, container: ctx.container, userFeatures: ctx.features, isSuperAdmin: ctx.isSuperAdmin, } } function safeStringify(value: unknown): string { const seen = new WeakSet() return JSON.stringify(value, (_key, raw) => { if (raw && typeof raw === 'object') { if (seen.has(raw as object)) return '[Circular]' seen.add(raw as object) const entries = Object.entries(raw as Record) entries.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)) return entries.reduce>((acc, [k, v]) => { acc[k] = v return acc }, {}) } return raw }) } /** * Hashes `(tenantId, orgId, agentId, conversationId, toolName, normalizedInput)` * into a stable SHA-256 digest so that retries of the same tool call with the * same payload collapse to a single `AiPendingAction` row inside the TTL * window. The input is normalized through `safeStringify` to make object key * order irrelevant (spec §8 rule `idempotencyKey prevents double-submission`). * Attachments are NOT included — the attachment set is captured separately on * the pending row so that re-uploading the same file set with a different * tool-call object never accidentally collides. * * When `userId` is supplied it joins the hash, so two users proposing the * same call in a shared conversation get separate rows (each resolvable only * by its creator). Omitting it keeps the previous digest. */ export function computeMutationIdempotencyKey(input: { tenantId: string organizationId: string | null agentId: string conversationId: string | null toolName: string normalizedInput: Record userId?: string | null }): string { const canonical = safeStringify({ tenant: input.tenantId, org: input.organizationId ?? null, agent: input.agentId, conversation: input.conversationId ?? null, tool: input.toolName, input: input.normalizedInput ?? {}, ...(input.userId ? { user: input.userId } : {}), }) return createHash('sha256').update(canonical).digest('hex') } function computeFieldDiff( before: Record, after: Record, display?: AiToolFieldDiffDisplayHints, options?: { keys?: Iterable }, ): AiPendingActionFieldDiff[] { const diff: AiPendingActionFieldDiff[] = [] const keys = options?.keys ? new Set(options.keys) : new Set([ ...Object.keys(before ?? {}), ...Object.keys(after ?? {}), ]) for (const field of keys) { const beforeValue = before ? before[field] : undefined const afterValue = after ? after[field] : undefined if (!Object.is(beforeValue, afterValue) && safeStringify(beforeValue) !== safeStringify(afterValue)) { const fieldLabel = display?.fieldLabels?.[field] const beforeDisplay = display?.before?.[field] const afterDisplay = display?.after?.[field] diff.push({ field, ...(fieldLabel !== undefined ? { fieldLabel } : {}), before: beforeValue, after: afterValue, ...(beforeDisplay !== undefined ? { beforeDisplay } : {}), ...(afterDisplay !== undefined ? { afterDisplay } : {}), }) } } return diff } function extractPatchFromArgs( args: Record, ): Record { const raw = args?.patch if (raw && typeof raw === 'object' && !Array.isArray(raw)) { return raw as Record } // Fall back: treat the whole args object (minus well-known envelope keys) // as the patch. This preserves compatibility with tools whose schema is // flat (`{ productId, name }`) rather than nested (`{ productId, patch }`). const envelope = new Set([ 'id', 'recordId', 'productId', 'mediaId', 'operation', 'commentId', 'activityId', 'personId', 'companyId', 'records', 'attachmentIds', '_sessionToken', ]) const reduced: Record = {} for (const [key, value] of Object.entries(args ?? {})) { if (envelope.has(key)) continue reduced[key] = value } return reduced } function matchBatchPatch( args: Record, recordId: string, ): Record { const rawList = args?.records if (Array.isArray(rawList)) { const match = rawList.find((entry) => { if (!entry || typeof entry !== 'object') return false const candidate = entry as Record return candidate.recordId === recordId || candidate.id === recordId }) if (match && typeof match === 'object') { const patch = (match as Record).patch if (patch && typeof patch === 'object' && !Array.isArray(patch)) { return patch as Record } const envelope = new Set(['recordId', 'id']) const reduced: Record = {} for (const [key, value] of Object.entries(match as Record)) { if (envelope.has(key)) continue reduced[key] = value } return reduced } } return {} } function normalizeAttachmentIds(args: Record): string[] { const raw = args?.attachmentIds if (!Array.isArray(raw)) return [] return raw.filter((value): value is string => typeof value === 'string' && value.length > 0) } async function buildSingleRecordDiff( tool: AiToolDefinition, toolCallArgs: Record, handlerContext: McpToolContext, ): Promise<{ fieldDiff: AiPendingActionFieldDiff[] targetEntityType: string | null targetRecordId: string | null recordVersion: string | null sideEffectsSummary: string | null }> { const resolver = tool.loadBeforeRecord if (!resolver) { logger.warn('prepareMutation: tool declared isMutation=true but no loadBeforeRecord resolver; shipping empty fieldDiff', { toolName: tool.name }) return { fieldDiff: [], targetEntityType: null, targetRecordId: null, recordVersion: null, sideEffectsSummary: NO_RESOLVER_SIDE_EFFECTS_MESSAGE, } } const before: AiToolLoadBeforeSingleRecord | null = await resolver( toolCallArgs as never, handlerContext, ) if (!before) { return { fieldDiff: [], targetEntityType: null, targetRecordId: null, recordVersion: null, sideEffectsSummary: null, } } const hasExplicitAfter = before.after != null const patch = before.after ?? extractPatchFromArgs(toolCallArgs) const fieldDiff = computeFieldDiff( before.before, patch, before.display, hasExplicitAfter ? undefined : { keys: Object.keys(patch) }, ) return { fieldDiff, targetEntityType: before.entityType, targetRecordId: before.recordId, recordVersion: before.recordVersion, sideEffectsSummary: null, } } async function buildBatchRecords( tool: AiToolDefinition, toolCallArgs: Record, handlerContext: McpToolContext, ): Promise<{ records: AiPendingActionRecordDiff[] | null targetEntityType: string | null sideEffectsSummary: string | null }> { const resolver = tool.loadBeforeRecords if (!resolver) { logger.warn('prepareMutation: bulk tool declared isMutation=true but no loadBeforeRecords resolver; shipping empty records[]', { toolName: tool.name }) return { records: null, targetEntityType: null, sideEffectsSummary: NO_RESOLVER_SIDE_EFFECTS_MESSAGE, } } const rows: AiToolLoadBeforeRecord[] = await resolver( toolCallArgs as never, handlerContext, ) if (!Array.isArray(rows) || rows.length === 0) { return { records: null, targetEntityType: null, sideEffectsSummary: null, } } const diffs: AiPendingActionRecordDiff[] = rows.map((row) => { const hasExplicitAfter = row.after != null const patch = row.after ?? matchBatchPatch(toolCallArgs, row.recordId) return { recordId: row.recordId, entityType: row.entityType, label: row.label, fieldDiff: computeFieldDiff( row.before, patch, row.display, hasExplicitAfter ? undefined : { keys: Object.keys(patch) }, ), recordVersion: row.recordVersion ?? null, } }) const [firstEntity] = rows return { records: diffs, targetEntityType: firstEntity ? firstEntity.entityType : null, sideEffectsSummary: null, } } type MutationPreview = { fieldDiff: AiPendingActionFieldDiff[] records: AiPendingActionRecordDiff[] | null targetEntityType: string | null targetRecordId: string | null recordVersion: string | null sideEffectsSummary: string | null } async function buildMutationPreview( tool: AiToolDefinition, toolCallArgs: Record, handlerContext: McpToolContext, ): Promise { if (tool.isBulk === true) { const batch = await buildBatchRecords(tool, toolCallArgs, handlerContext) return { fieldDiff: [], records: batch.records, targetEntityType: batch.targetEntityType, targetRecordId: null, recordVersion: null, sideEffectsSummary: batch.sideEffectsSummary, } } const single = await buildSingleRecordDiff(tool, toolCallArgs, handlerContext) return { fieldDiff: single.fieldDiff, records: null, targetEntityType: single.targetEntityType, targetRecordId: single.targetRecordId, recordVersion: single.recordVersion, sideEffectsSummary: single.sideEffectsSummary, } } /** * Intercepts a mutation tool call and turns it into an `AiPendingAction` + * `mutation-preview-card` UI part (spec Phase 3 WS-C §9). The caller MUST * have already confirmed the agent's effective `mutationPolicy` is NOT * `read-only`; this helper repeats the check defensively because skipping it * would be a policy-bypass. * * The tool handler is NEVER invoked by this function — the write is * short-circuited and only runs from the Step 5.8 confirm route. See the * unit test `does not call the tool handler` for the guard. */ export async function prepareMutation( input: PrepareMutationInput, ctx: PrepareMutationContext, ): Promise { const { agent, tool } = input if (tool.isMutation !== true) { throw new AiMutationPreparationError( 'not_a_mutation_tool', `Tool "${tool.name}" is not a mutation tool; prepareMutation should not be invoked.`, ) } const effectivePolicy = resolveEffectiveMutationPolicy( agent.mutationPolicy, input.mutationPolicyOverride ?? null, agent.id, ) if (effectivePolicy === 'read-only') { throw new AiMutationPreparationError( 'read_only_agent', `Agent "${agent.id}" has effective mutationPolicy=read-only; mutation tool "${tool.name}" cannot be prepared.`, ) } const tenantId = assertTenantScope(ctx) const em = resolveEm(ctx.container) const repo = new AiPendingActionRepository(em) const { fieldDiff, records, targetEntityType, targetRecordId, recordVersion, sideEffectsSummary, } = await buildMutationPreview(tool, input.toolCallArgs, toolHandlerContext(ctx)) const normalizedInput = input.toolCallArgs ?? {} const conversationId = input.conversationId ?? null const idempotencyKey = computeMutationIdempotencyKey({ tenantId, organizationId: ctx.organizationId ?? null, agentId: agent.id, conversationId, toolName: tool.name, normalizedInput, userId: ctx.userId, }) const { row: pendingAction, created } = await repo.createOrReuse( { agentId: agent.id, toolName: tool.name, idempotencyKey, createdByUserId: ctx.userId, normalizedInput, conversationId, targetEntityType, targetRecordId, fieldDiff, records, sideEffectsSummary, recordVersion, attachmentIds: normalizeAttachmentIds(normalizedInput), channel: input.channel ?? 'agent', apiKeyId: input.apiKeyId ?? null, correlationId: input.correlationId ?? null, now: input.now, }, { tenantId, organizationId: ctx.organizationId ?? null, userId: ctx.userId, }, ) if (created) { await notifyPendingActionAwaitingApproval(ctx.container, pendingAction, { tenantId, organizationId: ctx.organizationId ?? null, }) } const uiPart: AiUiPart = { componentId: MUTATION_PREVIEW_CARD_COMPONENT_ID, props: { pendingActionId: pendingAction.id, expiresAt: pendingAction.expiresAt.toISOString(), ...(records ? { records } : { fieldDiff }), ...(sideEffectsSummary ? { sideEffectsSummary } : {}), }, } return { uiPart, pendingAction, reused: !created } } export const MUTATION_PREVIEW_CARD_COMPONENT = MUTATION_PREVIEW_CARD_COMPONENT_ID export type RawToolMutationChannel = 'mcp' | 'tools-api' export interface PrepareRawToolMutationInput { tool: AiToolDefinition toolCallArgs: Record channel: RawToolMutationChannel requesterUserId: string | null apiKeyId: string | null correlationId: string | null now?: Date } export interface RawToolMutationSummary { toolName: string targetEntityType: string | null targetRecordId: string | null recordCount: number } export interface PrepareRawToolMutationResult { pendingAction: AiPendingAction summary: RawToolMutationSummary } const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i function resolveRequesterUserId(candidate: string | null): string { if (typeof candidate === 'string' && UUID_PATTERN.test(candidate)) return candidate throw new AiMutationPreparationError( 'requester_unresolved', 'No user could be resolved as the requester of this mutation; refusing to queue it for approval.', ) } /** * Hashes `(tenantId, orgId, channel, requester, apiKeyId, toolName, normalizedInput)` so * repeating the identical raw tool call while its pending row is still * `pending` collapses onto that row instead of queueing a duplicate. */ export function computeRawToolIdempotencyKey(input: { tenantId: string organizationId: string | null channel: RawToolMutationChannel requesterUserId: string apiKeyId: string | null toolName: string normalizedInput: Record }): string { const canonical = safeStringify({ tenant: input.tenantId, org: input.organizationId ?? null, agent: AI_RAW_TOOL_AGENT_ID, channel: input.channel, requester: input.requesterUserId, apiKey: input.apiKeyId ?? null, tool: input.toolName, input: input.normalizedInput ?? {}, }) return createHash('sha256').update(canonical).digest('hex') } /** * Queues a raw MCP / `/tools/execute` mutation call as an `AiPendingAction` * (agent id {@link AI_RAW_TOOL_AGENT_ID}) instead of running it. The tool * handler is NEVER invoked here; the write only runs after an approver * confirms the row. Fails closed with `requester_unresolved` when no user id * can be attributed to the call. */ export async function prepareRawToolMutation( input: PrepareRawToolMutationInput, ctx: McpToolContext, ): Promise { const { tool } = input if (tool.isMutation !== true) { throw new AiMutationPreparationError( 'not_a_mutation_tool', `Tool "${tool.name}" is not a mutation tool; prepareRawToolMutation should not be invoked.`, ) } const requesterUserId = resolveRequesterUserId(input.requesterUserId) const tenantId = assertTenantScope(ctx) const em = resolveEm(ctx.container) const repo = new AiPendingActionRepository(em) const organizationId = ctx.organizationId ?? null const normalizedInput = input.toolCallArgs ?? {} const preview = await buildMutationPreview(tool, normalizedInput, ctx) const idempotencyKey = computeRawToolIdempotencyKey({ tenantId, organizationId, channel: input.channel, requesterUserId, apiKeyId: input.apiKeyId ?? null, toolName: tool.name, normalizedInput, }) const { row: pendingAction, created } = await repo.createOrReuse( { agentId: AI_RAW_TOOL_AGENT_ID, toolName: tool.name, idempotencyKey, createdByUserId: requesterUserId, normalizedInput, conversationId: null, targetEntityType: preview.targetEntityType, targetRecordId: preview.targetRecordId, fieldDiff: preview.fieldDiff, records: preview.records, sideEffectsSummary: preview.sideEffectsSummary, recordVersion: preview.recordVersion, attachmentIds: normalizeAttachmentIds(normalizedInput), channel: input.channel, apiKeyId: input.apiKeyId ?? null, correlationId: input.correlationId ?? null, ttlSeconds: resolveMcpPendingActionTtlSeconds(), now: input.now, }, { tenantId, organizationId, userId: requesterUserId, }, ) if (created) { await notifyPendingActionAwaitingApproval(ctx.container, pendingAction, { tenantId, organizationId, }) } const pendingRecords = Array.isArray(pendingAction.records) ? pendingAction.records : null return { pendingAction, summary: { toolName: tool.name, targetEntityType: pendingAction.targetEntityType ?? null, targetRecordId: pendingAction.targetRecordId ?? null, recordCount: pendingRecords ? pendingRecords.length : 1, }, } }