Skip to main content

Framework Reference

Output integrity

A result schema says an agent's output is well-formed. Output-integrity rules check that it is consistent with its evidence and with the run: a quote really appears in the input, a cited id really exists, a cited policy was really read. Rules run on the server after the schema passes and before anything is stored, returned or turned into a proposal.

What a passing rule does not prove. Integrity rules validate evidence and consistency, not conclusions. A quote that appears in the input, a reference that exists, a policy that was read, or a confidence score the model reported above a threshold does not show that the finding follows from that evidence. A passing run means the output survived these checks, not that it is correct. Keep a human review step (proposals, needsReview, held auto-approval) where a wrong conclusion matters.

Integrity rules are an Agent Orchestrator (enterprise) feature. You declare them on an agent with defineAgent({ integrity: [...] }), or in the integrity key of a file agent's GOVERNANCE.json. The working example is the support.policy_check agent in apps/mercato/src/modules/agent_examples (lib/policyCheckIntegrity.ts).

Declaring rules​

import { defineAgent } from '@open-mercato/enterprise/modules/agent_orchestrator/lib/sdk/defineAgent'

defineAgent({
id: 'support.policy_check',
// …
tools: ['agent_examples.get_support_policy'],
result: { kind: 'research', schema: policyCheckResult },
integrity: [
{ id: 'quotes', kind: 'evidence_quote', items: 'data.findings[*].evidence[*]', quote: 'quote',
offsets: { start: 'start', end: 'end' }, source: { input: 'reply' }, onViolation: 'drop' },
{ id: 'policy-ids', kind: 'reference_exists', ids: 'data.findings[*].policyId',
resolver: 'agent_examples.support_policy', version: { input: 'policySetVersion' }, onViolation: 'drop' },
{ id: 'policies-read', kind: 'tool_consulted', cites: 'data.findings[*].policyId',
tools: ['agent_examples.get_support_policy'], returned: ['policy.id'], onViolation: 'flag' },
{ id: 'confidence', kind: 'custom', checker: 'agent_examples.policy_finding_confidence',
config: { minConfidence: 0.5 }, onViolation: 'flag' },
],
})
  • Paths are relative to the result envelope ({ kind, data }), so they start with data.. [*] walks every array item.
  • Paths are validated against the result schema when the agent registers. A path that cannot exist is a startup error, not a silent no-op.
  • Rule ids must be unique within the agent. Rules run in declaration order; each rule sees the output left by the rules before it.

What a violation does​

Every rule has an onViolation:

ValueEffect
dropRemoves the offending array item from the output. The output is re-validated against the result schema; a drop that makes it invalid blocks the run.
repairOnly evidence_quote accepts it. A quote that cannot be found in the source cannot be repaired, so it is kept and flagged, as with flag. Quotes that are found are corrected whatever the onViolation (see evidence_quote).
flagKeeps the item and records a warn with holdsAutoApproval, which holds auto-approval of a proposal (auto_disposition_block = 'guardrail').
blockFails the run. Nothing is stored as the run output.

Each rule that runs writes one guardrail check row, visible in the run trace: pass, warn or block, with counts, item paths and reason codes. A rule that blocks ends the run, so the rules after it write no row. Quoted text and input text are never written to a check row. When rules changed the output, the model's original output is kept as an encrypted raw_output artifact.

Rule kinds​

evidence_quote​

Each quote at items[*].<quote> must appear in the input field named by source.input. Whitespace and typography are normalised by default; case is not folded. For a quote that is found, the platform recomputes the start/end offsets and writes them back (when offsets is declared), and replaces the quote with the exact source text (when repairQuote is on, the default). A quote that is not in the source, or is shorter than minChars (default 3), is a violation. Default onViolation: drop.

reference_exists​

Every id at ids is resolved in one batch by a reference resolver, registered in DI under agentReferenceResolver:<resolverId>. A resolver implements AgentReferenceResolver from @open-mercato/shared/lib/ai/referenceResolver and returns exists, not_found or version_mismatch per id. With version, every id must exist at that version. If no resolver is registered, onResolverMissing (default block) applies: an unverifiable reference is not a verified one. Default onViolation: drop.

A resolver over tenant data MUST filter by the tenantId and organizationId it receives and read through findWithDecryption.

tool_consulted​

Every value cited at cites must have been returned by a successful call to one of tools in the same run, at one of the returned paths of the tool result. includeSubRuns also counts delegated sub-runs. enforce: 'prepare_step' forces the model to call one of the tools before it can answer. Default onViolation: flag.

abstain_or_review​

Turns signals from the run into a structured outcome instead of a failure. notApplicable and needsReview each list when signals (output_empty, tool_returned_empty, retrieval_empty, confidence_below, violations_at_least, output_flag, integrity_dropped_all) and the fields to set in the output. needsReview with route: 'flag' holds auto-approval; route: 'review' also emits agent_orchestrator.run.review_requested, which notifies a human for non-proposal runs. It must be the last integrity rule.

custom​

A domain rule written in code, for checks the built-in kinds cannot express. The rule names a checker by id; your module registers the function under that id with registerIntegrityChecker.

import { z } from 'zod'
import {
getIntegrityChecker,
registerIntegrityChecker,
type IntegrityCheckerFn,
} from '@open-mercato/enterprise/modules/agent_orchestrator/lib/sdk/integrityCheckers'

const scoredFindings = z.object({
data: z.object({ findings: z.array(z.object({ confidence: z.number() }).passthrough()) }),
})

export const policyFindingConfidenceChecker: IntegrityCheckerFn = ({ output, config }) => {
const minConfidence = typeof config?.minConfidence === 'number' ? config.minConfidence : 0.5
const { findings } = scoredFindings.parse(output).data
return {
violations: findings.flatMap((finding, index) =>
finding.confidence < minConfidence
? [{ itemPath: `data.findings[${index}]`, reason: 'low_confidence' }]
: [],
),
}
}

export function registerPolicyFindingConfidenceChecker(): void {
if (getIntegrityChecker('agent_examples.policy_finding_confidence')) return
registerIntegrityChecker('agent_examples.policy_finding_confidence', policyFindingConfidenceChecker)
}

Parse the output with parse, not safeParse: an output the checker does not understand must throw, so reusing the checker id on an agent with an incompatible schema blocks the run with checker_error instead of passing every output. The full version, which also validates config with zod, is apps/mercato/src/modules/agent_examples/lib/policyFindingConfidence.ts.

Checker id. <module>.<name>, lowercase (^[a-z][a-z0-9_]*\.[a-z0-9][a-z0-9_.-]*$, at most 200 characters). Registering the same id twice throws, so guard the call as above.

Where to register. Call the registration in your module's ai-agents.ts, before the defineAgent call that uses it. The checker is then loaded wherever the agent is. The checker only has to exist when the agent runs, not when it is defined.

Input. The checker receives:

FieldContent
outputA private copy of the current result envelope, after earlier rules
inputA private copy of the run input
toolCallsThe run's tool calls, in call order
citableSourcesCitable sources assembled for the run's context (empty when none)
scope{ tenantId, organizationId } of the run
containerThe DI container of the run, for reading tenant data
configThe rule's declared config, when any

Changing output or input inside the checker has no effect on the run. A checker that reads tenant data through container MUST filter by scope and read through findWithDecryption.

Result. { violations: [{ itemPath, reason }], repairedOutput? }:

  • itemPath is a concrete path into the result (data.findings[2], no [*]). With drop, the nearest enclosing array item is removed; a path that cannot be addressed is flagged instead.
  • reason is a stable code matching ^[a-z0-9_.:-]{1,64}$. Anything else is recorded as invalid_reason, so free text never reaches a check row.
  • repairedOutput, when returned, replaces the output and is re-validated against the result schema. Violations are then applied to it.

custom rules accept drop, flag and block; onViolation is required.

Failures block. A checker that is not registered when the agent runs blocks the run with checker_missing. A checker that throws or returns a malformed result blocks it with checker_error, whatever onViolation says, and the error is logged and reported. A misconfigured rule fails closed.

Shadow mode​

Set OM_AGENT_INTEGRITY=off to roll rules out safely. Rules still run and record their check rows, but nothing is dropped, repaired or blocked, and auto-approval is not held: a would-be block is recorded as warn with wouldBlock. The default is on.

OpenCode agents​

Agents that run on OpenCode check only the submitted outcome: the declared integrity rules, or the schema and tool scope when none are declared. Grounding and untrusted-input screening do not run there.

  • Agents — defining agents and result schemas.
  • Citations — how read tools cite their sources.
  • Agent trace capture — where check rows and the raw output artifact show up.