init() once at process startup, then decorate any function with @rubric.tool — every call routes through your policy bundle automatically.
For multi-agent processes or tests where you need explicit lifecycle control, use the Governance class directly. The two coexist; the singleton wraps a Governance under the hood.
init()
Governance singleton. Idempotent — call it once at startup and forget it. All arguments fall back to the same env vars as Governance.bootstrap().
Registers an atexit hook that flushes audit events on normal process exit. Hard kills (SIGKILL, OOM) skip the hook — by design.
Returns the underlying Governance instance if you need it (rarely).
@tool
evaluate() before invoking the function. If the policy denies the call:
- The underlying function is not invoked.
- A
GovernanceDeniedErroris raised. It’s aPermissionErrorsubclass; catch either.
metadata.args; keyword arguments in metadata.kwargs. Match those in your policies:
session()
@rubric.tool calls inside the block. Falls back to default_session_id (default "default") outside any scope.
ContextVars under the hood — propagates across await points within the same task. Safe in async code and threadpools.
trace()
TraceContext for tool calls in scope. Every governed call uploads the running transcript so the dashboard’s drawer has the conversation behind each audit row.
evaluate()
For ad-hoc evaluations outside a@rubric.tool-decorated function:
gov.evaluate(). Reads the singleton, the session ContextVar, and the trace ContextVar — pass explicit overrides only if you need to.
shutdown()
atexit hook on normal exit. Safe to call manually for explicit cleanup (tests, child-process forks).
Errors
| Error | When |
|---|---|
rubric.GovernanceNotInitializedError | @tool or evaluate() called before init(). |
rubric.GovernanceDeniedError | A policy denied a @tool-wrapped call. Has .tool_name and .result attributes. |
rubric.GovernanceProblemError | Rubric API returned an RFC 9457 problem response (e.g. enrollment token revoked). |
When NOT to use the singleton
- Multiple agents in one process. Use
Governance.bootstrap()directly with a fresh instance per agent. The singleton supports exactly one identity at a time. - Tests. Pass an explicit
Governance(token_store=...)so each test starts fresh. - Library code. A library shouldn’t claim the process-wide singleton on its consumer’s behalf. Accept a
Governanceinstance via dependency injection instead.