Expand description
CPEX is a policy enforcement runtime for AI agents.
It is a deterministic reference monitor between an agent and every capability it invokes: tools, prompts, resources, inference providers, and A2A methods. Each operation runs through a policy-defined pipeline that can resolve identity, make an authorization decision (delegated to an engine like Cedar or CEL), exchange and reduce credentials before a downstream call, redact inputs and outputs, track information flow across calls, and audit. You write that policy declaratively in APL, the configuration that defines each operation’s pipeline; CPEX evaluates and enforces it at the boundary, against state the model cannot observe or forge.
- Guide and concepts: https://contextforge-org.github.io/cpex/
- Source and issues: https://github.com/contextforge-org/cpex
§This crate
cpex is the host facade: one dependency that re-exports the CPEX
runtime (cpex-core, apl-core, apl-cmf, apl-cpex), so a host depends
on this crate instead of pinning each of them separately.
By default it is the engine only: no builtin plugins are compiled in.
The bundled extension set lives in cpex-builtins and is
pulled in only when a builtins feature is enabled.
§Usage
Engine only (register your own factories):
use std::sync::Arc;
use cpex::PluginManager;
let mgr = Arc::new(PluginManager::default());
// ... register host factories, then `apl_cpex::register_apl(&mgr, opts)`.With the bundled builtins (enable the builtins or full feature):
use std::sync::Arc;
use cpex::PluginManager;
let mgr = Arc::new(PluginManager::default());
// Register every enabled builtin factory and install the APL config
// visitor (in-process defaults) in one call:
cpex::install_builtins(&mgr);
// ... then load a config that references the enabled `kind`s.§Features
No plugins are on by default (cpex = "0.2" is the engine alone).
builtins enables the common in-process set; full adds the Valkey
session store; or pick a granular subset (jwt, oauth, pii,
audit, cedar, cel, valkey). When any builtins feature is on, the
registration helpers and the concrete factory types are re-exported here
from cpex-builtins.
Re-exports§
pub use apl_cmf;pub use apl_core;pub use apl_cpex;pub use cpex_core;pub use cpex_builtins;
Structs§
- AplOptions
- Configuration for
register_apl. All runtime collaborators APL needs to do its work are funneled through here so the call site reads as a single block instead of a multi-step builder. - Audit
Logger Factory - Cedar
Direct PdpFactory - Factory for
CedarDirectResolver. Reportskind() = "cedar-direct"; builds resolvers from the unified-config block viaCedarDirectResolver::from_config. - CelPdp
Factory - Factory for
CelResolver. Reportskind() = "cel"; builds resolvers from the unified-config block viaCelResolver::from_config. - Dispatch
Cache - Host-owned dispatch cache. Construct once, share via
Arc<DispatchCache>across allCmfPluginInvoker::for_requestcalls so plans built for one request can be reused by the next. - JwtIdentity
Factory - Factory for
kind: identity/jwtplugins. Instantiates aJwtIdentityResolverfrom theconfig:block and registers it on theidentity.resolvehook. - Memory
Session Store - In-process
SessionStorebacked by aHashMapofHashSets. Suitable for tests, single-process deployments, and as the default when no distributed store is configured. Cloning the store viaArcshares state across all consumers. - OAuth
Delegator Factory - Factory for
kind: delegator/oauthplugins. Instantiates anOAuthDelegatorfrom theconfig:block and registers it on thetoken.delegatehook. - PiiScanner
Factory - Factory for
kind: validator/pii-scan. Instantiates aPiiScannerfrom theconfig:block and registers a handler for every CMF hook name listed incfg.hooks. Operators typically wire it oncmf.tool_pre_invoke/cmf.prompt_pre_invoke/cmf.resource_pre_fetchso it runs before any of those entity types reach the backend. - Plugin
Manager - Valkey
Config - Parsed
global.apl.session_storeconfig forkind: valkey. - Valkey
Session Store Factory - Factory the host registers via
AplOptions.session_store_factories.
Constants§
- AUDIT_
KIND kind:string operators write in CPEX YAML to declare an audit logger instance.- JWT_
KIND - The plugin
kind:string operators write in CPEX YAML to declare a JWT identity resolver. - OAUTH_
KIND - The plugin
kind:string operators write in CPEX YAML to declare an OAuth RFC 8693 token-exchange delegator. - PII_
KIND kind:string operators write in CPEX YAML to declare a PII scanner instance.- VALKEY_
KIND - The
kind:discriminator this factory builds. Part of the public surface — it is the string operators write in their config.
Traits§
- PdpFactory
- Build a
PdpResolverfrom a unified-config block. Implemented per PDP backend (cedar-direct, opa, …) and registered with the apl-cpex visitor so unified-config YAML can declare PDPs without the host pre-constructing them in code. - Session
Store - Pluggable session-state backend. Implementations must be
Send + Sync— the same store is shared across all concurrent requests. - Session
Store Factory - Factory the visitor consults when it encounters a
global.apl.session_storeblock in the unified config. Mirrorsapl_core::step::PdpFactory: each factory advertises akind()string matching the YAML block’skind:field, andbuildturns the block into a live store. Registered up front viacrate::AplOptions::session_store_factories; the visitor selects the active store from config during its global-config walk, before any route handler captures the store.
Functions§
- builtin_
pdp_ factories - The enabled PDP factories, ready to drop into
AplOptions::pdp_factories. A route’scedar:orcel:step selects which one runs. - builtin_
session_ store_ factories - The enabled session-store factories, ready to drop into
AplOptions::session_store_factories. Aglobal.apl.session_store: { kind: ... }config block selects one; absent that, the in-processMemorySessionStoredefault stays active. - install_
builtins - Register every enabled plugin factory and install the APL config visitor
on
mgrwith in-process defaults (aMemorySessionStoreand the default baseline capabilities). The enabled PDP and session-store factories are wired in, so a later config load can reference any of them bykind. - register_
apl - Build an
AplConfigVisitorfrom the supplied options and register it on the manager. Returns theArc<AplConfigVisitor>so the caller can stash it for later inspection (or callregister_pdpon it after the fact for late-bound resolvers) — but in the typical case the return value is dropped and the visitor lives inside the manager’s visitor list. - register_
builtin_ plugins - Register every enabled by-kind plugin factory on
mgr: identity (identity-jwt), delegators (delegator-oauth), validators (pii-scanner), and observers (audit-logger). Call before loading a config so the manager can instantiate plugins whose YAMLkind:matches.