Eval Lab & AI Guardrails
Every tool invocation, A2A task, and agent workflow step in @latticedx/plugin-dxos-ai passes through runtime governance checks (ai-governance.js) before and after interacting with an LLM or database repository. The subsystem combines a 9-rule Vietnamese PII and secret redaction pipeline, PostgreSQL-backed daily quota enforcement, structured audit logging in latticeAiAuditLogs, and the built-in Eval Lab (ai-eval-lab.js).
All governance collections (latticeAiAuditLogs, latticeAiQuotaPolicies, latticeAiQuotaUsage, latticeAiTraces) enforce tenant isolation via PostgreSQL FORCE ROW LEVEL SECURITY and reject unmediated client writes.
Eval Lab and Waterfall Trace Inspector
Eval Lab lets administrators benchmark the deterministic Vietnamese natural-language intent router or a configured live LLM against capability and security suites, while the trace inspector merges OpenTelemetry GenAI spans with audit records into per-traceId waterfall trees.
/api/dxosAi:listEvalSuites/api/dxosAi:runEvalBenchmark/api/dxosAi:listAgentTraces/api/dxosAi:getAgentTraceDetailBuilt-in Evaluation Suites (listEvalSuites)
| Suite ID | Scope | Supported Modes |
|---|---|---|
vietnamese-nl-agent | Full Vietnamese natural-language routing suite spanning RAG, task inbox, radar, GraphRAG, workflow architect, App Builder, BI charts, memory, A2A, approvals, omnichannel notifications, public forms, and security refusals. | deterministic, llm |
wave1-3-capabilities | Focused capability suite covering App Builder plan/patch/rollback, DxosBiChartBlockModel, ReAct loops and agent memory, A2A delegation, workflow publishing, multi-level approvals, and notification channel testing. | deterministic, llm |
security-red-team | SECURITY_RED_TEAM_CASES testing prompt injection refusal, .env/AGENT_API_KEY exfiltration, root privilege escalation, SSRF to 169.254.169.254, unauthenticated public-form credential leaks, and M-of-N self-approval bypasses. | deterministic, llm |
custom | Evaluates administrator-supplied customCases payloads (up to 200 cases per run). | deterministic, llm |
| Parameter | Type | Default | Description |
|---|---|---|---|
suiteId | string | vietnamese-nl-agent | Target suite: vietnamese-nl-agent, wave1-3-capabilities, security-red-team, or custom. |
mode | string | deterministic | Either deterministic (local parseVietnameseNaturalIntent router) or llm (live configured LLM). |
modelName | string | — | Optional model override when executing in llm mode. |
limit | integer | — | Caps total executed test cases between 1 and 500. |
customCases | EvalCase[] | — | Array of up to 200 custom cases with prompt, isAdmin, portalSlug, expectedTool, expectedIntent, refused, and expectedKeywords. |
Waterfall Trace Aggregation (latticeAiTraces)
gatherTenantSpans merges spans from latticeAiTraces, the in-process otel-exporter buffer (strictly filtering norm.organizationId === organizationId), and latticeAiAuditLogs fallback entries. Direct REST or Chart access to latticeAiTraces is rejected with DXOS_TRACES_DIRECT_CRUD_FORBIDDEN so all trace queries pass through sanitizeTraceAttributes and tenant RLS checks.
Vietnamese PII and Secret Redaction (9 Rules)
Before payloads leave to an external LLM (redactPiiForLlm), an A2A peer (redactPiiDeep), or audit log storage, ai-governance.js runs three layers of scrubbing:
- Key-name masking (
SENSITIVE_KEY_RE): replaces values with[REDACTED]for 17 sensitive key patterns (token,secret,password,apikey,api_key,authorization,cookie,credential,jwt,bearer,privatekey,private_key,sessionid,session_id,passphrase,access_key,client_secret). - Inline key-value scrubbing (
SECRET_KV_RE): redacts inlinekey=valueandkey: valuesecret assignments inside strings to[REDACTED]. - Nine ordered regex rules inside
redactPiiText: masks international credentials and Vietnamese personal/tax identifiers.
| # | Detected Pattern | Matching Scope | Replacement Marker |
|---|---|---|---|
| 1 | JSON Web Token | Three-segment Base64URL string prefixed with eyJ... | [REDACTED_JWT] |
| 2 | Bearer Token | Bearer prefix followed by an 8+ character token | Bearer [REDACTED] |
| 3 | sk- API Key | Prefix sk- followed by 6 or more alphanumeric/_/- characters | [REDACTED_KEY] |
| 4 | Email Address | Standard <local>@<domain>.<tld> pattern | [REDACTED_EMAIL] |
| 5 | Bank Account / IBAN | Keywords stk, số tài khoản, tài khoản ngân hàng, iban followed by account digits or IBAN | $1[REDACTED_BANK] |
| 6 | Labeled CCCD / CMND | Keywords cccd, cmnd, căn cước công dân followed by 9 to 12 digits | $1[REDACTED_CCCD] |
| 7 | Unlabeled 12-Digit CCCD | Standalone 12-digit identifier starting with 0 (0\d{11}) | [REDACTED_CCCD] |
| 8 | Vietnamese Mobile Phone | `(+84 | 0)prefix followed by carrier prefixes3, 5, 7, 8, 9` and 8 digits |
| 9 | Vietnamese Tax ID (MST) | Keywords mst, mã số thuế followed by 10 digits and optional -3 branch suffix | $1[REDACTED_MST] |
Token/Cost Quotas and Audit Logging
Every tool or model call executes checkAndConsumeAiQuotaPersistent prior to dispatch. On PostgreSQL, the check runs inside withTenantRlsContext using SELECT ... FOR UPDATE against latticeAiQuotaUsage to enforce four daily thresholds (keyed by UTC YYYY-MM-DD). Exceeding any threshold returns HTTP 429 (AI_QUOTA_EXCEEDED).
/api/dxosAi:quotaSummary/api/dxosAi:saveQuotaPolicy/api/dxosAi:auditLogsDefault Daily Quotas and Model Pricing
| Parameter | Type | Default | Description |
|---|---|---|---|
DXOS_AI_DAILY_ORG_CALL_LIMIT | integer | 5000 | Daily organization-wide AI invocation limit (orgDailyCalls). |
DXOS_AI_DAILY_USER_CALL_LIMIT | integer | 500 | Daily per-user AI invocation limit (userDailyCalls). |
DXOS_AI_DAILY_ORG_TOKEN_LIMIT | bigInt | 5000000 | Daily organization total token ceiling (orgDailyTokens). |
DXOS_AI_DAILY_ORG_USD_LIMIT | double | 50 | Daily organization estimated cost ceiling in USD (orgDailyUsd). |
DXOS_AI_USD_PER_1M_INPUT / OUTPUT | number | — | Overrides built-in per-1M token input and output pricing. |
Built-in MODEL_PRICING_USD_PER_1M rates (USD per 1 million tokens) used to compute estimatedCostUsd when environment overrides are unset:
| Model Family | Input ($ / 1M tokens) | Output ($ / 1M tokens) |
|---|---|---|
ag/gemini-3.8-flash, gemini-2.5-flash, gpt-4o-mini | 0.15 | 0.60 |
gemini-2.0-flash | 0.10 | 0.40 |
gemini-2.5-pro | 1.25 | 10.00 |
gpt-4o | 2.50 | 10.00 |
claude-sonnet-4, claude-3-5-sonnet, claude-sonnet | 3.00 | 15.00 |
ollama / local/* | 0.00 | 0.00 |
combo (fallback pricing) | 0.35 | 1.05 |
latticeAiAuditLogs Record Schema
All tool executions across chat, workflows, MCP Gateway, A2A Gateway, and Eval Lab emit an audit entry in latticeAiAuditLogs. Key columns include actorUserId, actorUsername, organizationId, channel, aiEmployee, toolName, argsSummary (scrubbed via sanitizeAuditSummary), status (allowed | degraded | denied | error), durationMs, promptTokens, completionTokens, totalTokens, tokenSource (provider | estimated), estimatedCostUsd, piiRedactedCount, errorCode, and resultSummary.
curl -s -X POST https://lattice.example.com/api/dxosAi:runEvalBenchmark \
-H "Authorization: Bearer $ADMIN_JWT" \
-H "Content-Type: application/json" \
-d '{
"suiteId": "security-red-team",
"mode": "deterministic"
}'Related
- AI Overview & Profiles: Role locking and LLM service routing.
- 20 Unified Tools:
createAuditedToolwrapper enforcing quotas and emittinglatticeAiAuditLogs. - A2A Protocol: Outbound PII redaction prior to external peer delegation.
- Observability: OTLP span export and Prometheus metrics.
- Multi-Tenancy & PostgreSQL RLS:
FORCE ROW LEVEL SECURITYenforcement on AI governance tables.
