Open MercatoDocsSearch docsAPI reference

Framework

Input Moderation & Safety Identifiers

Decide how the application should respond to unsafe input and to an unavailable moderation provider. Those are separate outcomes. Configure the policy, identify the originating user where supported, and inspect the audit record when a request is blocked.

The AI assistant runtime ships two provider-aware content guardrails that protect the instance owner's LLM-provider organization from enforcement (warning → suspension → termination) triggered by abusive end users on untrusted surfaces (customer portals, public widgets):

  1. End-user safety identifiers – a hashed, non-reversible per-user identifier attached to every model call so the provider can act on a single abuser instead of banning the whole API-key organization.
  2. Input pre-moderation – an opt-in, fail-closed gate that screens the current user turn through the provider's moderation endpoint before the model call.

Both are additive: existing tenants see no behavior change until an agent declares untrustedInput or a tenant/env enables moderation.

Safety identifiers

For every chat turn the runtime computes:

endUserIdentifier = HMAC-SHA256(derivedSecret, `${tenantId}:${userId}`)

Identifiers are advisory provider-side metadata, not an in-platform security control.

Note · Secret rotation

Rotating JWT_SECRET changes every derived identifier, so the provider sees "new" users and loses per-user abuse-history continuity. This is accepted – rotation is rare and a never-rotating dedicated secret would be worse hygiene.

Input pre-moderation

Before the model call, the runtime runs a moderation check for the current user turn only (prior history was already screened on its own turns):

runAiAgentText
  ├─ resolve model + provider
  ├─ [gate] resolve moderation policy
  │     ├─ skip when the resolved provider has no moderation endpoint
  │     ├─ flagged ──► persist AiModerationFlag (categories + scores, no content)
  │     │              emit ai_assistant.moderation_flag.created
  │     │              throw AiModerationBlockedError → SSE { code: 'moderation_blocked' }
  │     └─ not flagged ──► continue
  └─ streamText(...)

Moderation runs only when the resolved chat provider supports it (initially the OpenAI adapter, via OpenAI's free /v1/moderations endpoint). Tenants on Anthropic/Google rely on those providers' own server-side filtering.

Policy precedence

The effective policy for a turn is resolved first-match-wins:

  1. agentDef.untrustedInput === true → enforced (tenant cannot disable).
  2. Per-agent tenant override (input_moderation on the agent-scoped settings row) → on / off.
  3. Tenant-wide override (input_moderation on the tenant-wide row) → on / off.
  4. Env default OM_AI_INPUT_MODERATION → on / off.
  5. Default off.

Fail-closed vs fail-open

Audit trail

Each blocked input appends one immutable ai_moderation_flags row storing category flags and scores only – never the prompt content or any PII. Review them at /backend/config/ai-assistant/moderation-flags (requires ai_assistant.settings.manage) to quantify false-positive rates and identify repeat abusers before widening enforcement.

Configuration

VariableDefaultPurpose
OM_AI_INPUT_MODERATIONunset (off)Env default for step 4 of the policy precedence (parsed with parseBooleanWithDefault).
OM_AI_MODERATION_MODELomni-moderation-latestModeration model id passed to /v1/moderations.

The safety-identifier HMAC secret is derived from JWT_SECRET – there is no dedicated env var, and it is never logged.

Marking an agent untrusted

A module developer shipping a portal-facing agent marks it as accepting untrusted input so moderation is enforced regardless of tenant settings:

import { defineAiAgent } from '@open-mercato/ai-assistant'

export const aiAgents = [
  defineAiAgent({
    id: 'support.portal_assistant',
    moduleId: 'support',
    label: 'Portal assistant',
    description: 'Customer-facing support assistant.',
    systemPrompt: '…',
    allowedTools: ['search.hybrid_search'],
    untrustedInput: true, // enforces input moderation where the provider supports it
  }),
]

Operator rejection UX

Scope notes

See the spec .ai/specs/2026-06-04-ai-input-moderation-and-safety-identifiers.md for the full design, risk register, and compliance report.

Previous: MCP Server (Claude Code & external clients) · Next: Mutation Approvals