> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trulyagentic.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Five Runtime Controls

> Architecture and TypeScript API reference for Controls 1–5 in GovernanceGateway.

Every tool invocation passes sequentially through `GovernanceGateway.execute()` (`src/gateway.ts`) before reaching any downstream ERP, CRM, or cloud SDK.

```mermaid theme={null}
flowchart LR
  A["ToolCallRequest"] --> B["Control 1 & 4: Registry & Kill-Switch Check"]
  B --> C["ASI04: scanMcpToolDefinition (Manifest Hash)"]
  C --> D["ASI01 / ASI06: scanUntrustedPayload"]
  D --> E["Control 4: reserveBudget"]
  E --> F["Control 2: PolicyEngine.evaluate (L1–L5 & Schema)"]
  F --> G["Control 3: ApprovalGate (Named usr_* + Idempotency)"]
  G --> H["Control 5: auditLedger.append (pre_execution SHA-256)"]
  H -->|Committed| I["Execute Downstream Tool"]
  H -.->|Sink Offline / Error| J["AuditLedgerUnavailableError (Hard Abort)"]
```

## Control Inventory

| # | Control | Primary Module & Exports | Enforcement Guarantee |
| - | - | - | - |
| **1** | **Company-Provisioned Service Identities** | `src/registry.ts` (`AgentRegistry`, `isValidServiceAccount`) | Rejects personal emails (`@`), `root`, `personal-*`, `user-*`, `shared-admin`, and `superadmin`; binds each agent to `svc-agent-*` and an explicit `allowedTools` allowlist. |
| **2** | **Read / Propose / Write Separation** | `src/policy-engine.ts` (`PolicyEngine`) | Enforces minimum `AgencyLevel` (`L1`–`L5`) and validates monetary ceilings, currencies, IBAN country prefixes, and vendor email domains in deterministic code. |
| **3** | **Named Individual Approver Sign-Off** | `src/approval-gate.ts`, `src/slack-bot.ts` (`ApprovalGate`, `isIndividualHumanId`) | Write tools require an `idempotencyKey` and pause on a ticket bound to `(agentId, toolName, inputSha256, idempotencyKey)`, rejecting shared group aliases (`team-*`, `finance-*`, `@here`, `@channel`). |
| **4** | **Central Registry, Budget Caps & Kill Switch** | `src/registry.ts`, `src/gateway.ts` (`reserveBudget`, `activateKillSwitch`) | Reserves `estimatedCostUsd` pre-flight against `budgetUsdCap` (expiring uncompleted reservations after 10 minutes) and suspends active agent leases immediately on kill-switch trigger. |
| **5** | **Fail-Closed "No Log, No Action" Ledger** | `src/audit-ledger.ts`, `db/migrations/001_audit_ledger.sql` | Writes a SHA-256 hash-chained `pre_execution` record **before** calling `executor(request)`. If persistence throws (`AuditLedgerUnavailableError`), downstream execution never runs. |

## Gateway Invocation Flow

```ts theme={null}
import {
  AgentRegistry,
  ApprovalGate,
  GovernanceGateway,
  InMemoryAuditLedger,
  PolicyEngine,
} from '@trulyagentic/governance-kit';

const registry = new AgentRegistry([
  {
    agentId: 'ap-invoice-agent',
    purpose: 'Accounts payable invoice extraction and controlled batch payment prep',
    ownerId: 'usr_anil_finance_lead',
    serviceAccountId: 'svc-agent-ap-worker',
    systems: ['netsuite-sandbox'],
    version: '0.1.0',
    maxAgencyLevel: 3,
    allowedTools: ['erp.get_invoice', 'erp.post_payment_batch'],
    budgetUsdCap: 25,
  },
]);

const gateway = new GovernanceGateway({
  registry,
  policyEngine: new PolicyEngine([/* ToolPolicy entries */]),
  auditLedger: new InMemoryAuditLedger(),
  approvalGate: new ApprovalGate(),
});

const result = await gateway.execute(request, async (req) => {
  // Executed ONLY after Controls 1-5 and OWASP scanners succeed
  return await invokeErpTool(req);
});
```

## Instant Containment (`activateKillSwitch`)

Calling `gateway.activateKillSwitch(agentId, operatorId, reason)` suspends the registered agent immediately (`state.suspended = true`) and commits an immutable `kill_switch` audit entry tagged with `ASI02`. Any subsequent tool call targeting that `agentId` is blocked in step 1 with zero tool execution.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.