Core Functions
API reference for createGovernance, register, enforce, recordOutcome, audit, integrityChain, score, and scoreFleet.
The primary API surface of governance-sdk — create an instance, register agents, enforce policies, record outcomes, and inspect state.
createGovernance(config)
Creates a governance instance that holds your policy rules, registered agents, and audit trail. Export as a singleton so all agents share the same policy set.
GovernanceConfig fields (all optional):
| Field | Type | Default | Description |
|---|---|---|---|
rules | PolicyRule[] | [] | Policy rules evaluated on every enforce(). User priorities are clamped to ≤998. |
storage | GovernanceStorage | in-memory | Storage adapter — swap for createPostgresStorage(pool) in production. |
defaultOutcome | "allow" | "block" | "allow" | Returned when no rule matches. |
serverUrl | string | — | When set, enforce() / register() POST to this URL instead of running locally. |
apiKey | string | — | Bearer token for remote calls. Required when serverUrl is set. |
timeout | number | 30000 | Remote call timeout (ms). |
maxRetries | number | 3 | Remote retry attempts on transient failure. |
fallbackMode | "allow" | "block" | "allow" | What to do when the remote API is unreachable after retries. |
onAuditError | (err) => void | noop | Called when a fire-and-forget audit write fails. |
integrityAudit | { signingKey; onFailure? } | off | Enables HMAC-SHA256 hash chaining of EVERY audit event. See Audit Trail. |
Note: Rules are evaluated in priority order (descending). User rules with
priority >= 999are clamped to 998 so the kill switch wins unconditionally.
gov.register(agent)
Registers an agent with the governance instance. Computes a 7-dimension governance score instantly and assigns a level (L0 through L4).
Warning: Self-reported booleans (
hasAuth, etc.) are accepted at face value. Cross-check callers' claims againstscanRepoContents()fromgovernance-sdk/repo-patterns. See Governance Scoring for the pattern.
gov.enforce(ctx)
Evaluates all matching policy rules before a tool call executes. Returns an EnforcementDecision. Every call is automatically recorded in the audit trail (and HMAC-chained when integrityAudit is set).
Notable EnforcementContext fields:
| Field | Populated by | Read by |
|---|---|---|
agentId, action, tool, input | always | every condition |
recentActionCount | host | rateLimit |
sessionTokensUsed | host | tokenBudget |
identityVerified, identityCapabilityMatch | host (after Ed25519 verify) | requireSignedIdentity |
mlInjectionScore, mlInjectionCategories | host (after ML classifier) | mlInjectionGuard |
outputText, outputTokenCount, executionDurationMs | host (postprocess) | output rules |
Also available: gov.enforcePreprocess(ctx) and gov.enforcePostprocess(ctx) for stage-scoped evaluation.
gov.recordOutcome(outcome)
Closes the decision → outcome loop. Call after the tool / LLM returns so the audit chain covers what actually happened, not just the permission check.
For most callers, the one-line helper is easier:
gov.audit
gov.integrityChain (opt-in)
Populated only when integrityAudit: { signingKey } was passed. Exports the HMAC-chained events for offline verification.
gov.score / gov.scoreFleet
gov.addRule / gov.removeRule
Mutate the policy set at runtime. User rules with priority >= 999 are clamped to 998 to preserve the kill-switch invariant.
gov.eval (in-memory)
Submit results from your adversarial harness (inspect-ai, PyRIT, Garak). Durable eval storage lives in Lua Governance Cloud.