Framework Reference
Reference checks
A model can cite a rule, a record or a document section that does not exist. The Agent Orchestrator's reference_exists output-integrity rule catches that: before the run's result is used, every id at a declared path is looked up through a reference resolver, and references that do not resolve are dropped, flagged or block the run.
The rule is part of the enterprise Agent Orchestrator (integrity on an agent definition). Resolvers are plain objects registered in DI by the module that owns the data, so OSS and app modules can provide them without depending on the enterprise package.
Declaring the rule
import type { IntegrityRuleInput } from '@open-mercato/enterprise/modules/agent_orchestrator/data/validators'
export const contractReviewIntegrity: IntegrityRuleInput[] = [
{
id: 'cited-sections',
kind: 'reference_exists',
ids: 'data.findings[*].citations[*]',
resolver: 'attachments.document_section',
onViolation: 'drop',
},
]
Add the array to the agent definition as integrity: contractReviewIntegrity.
| Option | Meaning |
|---|---|
ids | Path (relative to the result envelope { kind, data }) to the references. Items may be strings or objects. |
idPath / versionPath | For object items: where the id (default id) and the claimed version are. |
versionSeparator | For string items: splits id@version (default @). |
version | Pin every reference to one version: { const }, { input: '<path in the run input>' } or { path: '<path in the item>' }. |
onViolation | drop (default) removes the nearest enclosing array item; flag keeps it and holds auto-approval; block fails the run. |
onResolverMissing | block (default) or flag when the resolver is not registered, throws, or the run has no organization. An unverifiable reference is never treated as verified. |
Every rule records one guardrail check on the run (pass, warn or block). The evidence lists item paths and reason codes (not_found, version_mismatch, id_missing, version_missing) — never the id values. OM_AGENT_INTEGRITY=off switches integrity to shadow mode: checks are recorded but the output is not changed.
Shipped resolvers
| Resolver id | Module | Verifies |
|---|---|---|
attachments.document_section | attachments | Document-section citations <attachmentId>#<anchorKey> — the ref that attachments.get_document_sections emits. |
agent_examples.support_policy | agent_examples (example app module) | Ids of the example's static, global support-policy catalogue. Demo only: it ignores the organization scope because its data is not tenant data. |
attachments.document_section
A reference exists when the attachment belongs to the run's tenant and organization and the section's anchor is present in the attachment's live (newest non-deleted) extraction. A section that only existed in an older extraction, a ref of another organization, or a malformed ref is not_found.
The version is the section's text hash (textSha256). A reference pinned to a hash that no longer matches the current text reports version_mismatch, which tells you the document changed since the agent read it.
The attachment must also be readable by the run's principal — the identity the agent's tools act as (the run user, or for OpenCode runs the per-run session key acting for that user). The resolver applies the same read check as the document-structure read path (partition and owner access rules, for example a private documents:document). A reference the principal cannot read is not_found, with no version, so the rule reveals neither that the section exists nor its hash. A run without a user principal can only cite attachments that carry no owner access rule. The resolver returns only a status and a hash, never section content.
Writing a resolver
Implement AgentReferenceResolver from @open-mercato/shared/lib/ai/referenceResolver and register it in your module's di.ts under agentReferenceResolverDiKey('<module>.<reference>').
// src/modules/compliance/lib/ruleResolver.ts
import type { EntityManager } from '@mikro-orm/postgresql'
import { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'
import type { AgentReferenceResolver, ReferenceResolution } from '@open-mercato/shared/lib/ai/referenceResolver'
import { ComplianceRule } from '../data/entities'
export function createComplianceRuleResolver(em: EntityManager): AgentReferenceResolver {
return {
id: 'compliance.rule',
contractVersion: 1,
async resolve(refs, scope) {
const filter = { tenantId: scope.tenantId, organizationId: scope.organizationId }
const rows = await findWithDecryption(
em,
ComplianceRule,
{ ...filter, code: { $in: refs.map((ref) => ref.id) }, deletedAt: null },
{ fields: ['code', 'version'] },
filter,
)
const versions = new Map(rows.map((row) => [row.code, row.version]))
const result = new Map<string, ReferenceResolution>()
for (const ref of refs) {
const current = versions.get(ref.id)
if (current === undefined) result.set(ref.id, { status: 'not_found' })
else if (ref.version && ref.version !== current) result.set(ref.id, { status: 'version_mismatch', currentVersion: current })
else result.set(ref.id, { status: 'exists', version: current })
}
return result
},
}
}
// src/modules/compliance/di.ts
import { asFunction } from 'awilix'
import type { EntityManager } from '@mikro-orm/postgresql'
import type { AppContainer } from '@open-mercato/shared/lib/di/container'
import { agentReferenceResolverDiKey } from '@open-mercato/shared/lib/ai/referenceResolver'
import { createComplianceRuleResolver } from './lib/ruleResolver'
export function register(container: AppContainer) {
container.register({
[agentReferenceResolverDiKey('compliance.rule')]: asFunction(({ em }: { em: EntityManager }) =>
createComplianceRuleResolver(em),
)
.scoped()
.proxy(),
})
}
Resolver contract:
- Read-only. Never write, enqueue or emit events.
- Scoped. Filter every query by
scope.tenantIdandscope.organizationId, and read throughfindWithDecryption. - Access-aware.
scope.principal(optional) carries theuserIdthe run's tools act as, plusapiKeyIdwhen they act through an API key (OpenCode runs use a per-run session key). If your records have per-user access rules, evaluate them for that principal and returnnot_found(never a version) for records it cannot read. Without a principal, treat the lookup as a service call with no user. - Batched. One
resolvecall receives every unique reference of the rule; answer it with a fixed number of indexed queries, not one query per id. - Keyed by id. Return a
Mapkeyed byReferenceLookup.id. An id missing from the map counts asnot_found. - Exact ids. Compare ids exactly; if your resolver canonicalises them (case, prefixes), document it.
- Fail loudly. Throw on infrastructure errors; the rule then applies
onResolverMissinginstead of treating references as verified.
Related
- Citations — how tools emit the refs that reference checks verify.
- Spec:
.ai/specs/enterprise/agent-orchestrator/2026-10-21-output-integrity-guardrails.md.