> ## 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.

# Fail-Closed Audit Ledger

> Control 5 ("No Log, No Action"): PostgreSQL append-only triggers, SHA-256 hash chaining, and hard abort on sink outage.

Control 5 guarantees that no tool action executes without an immutable audit trail committed **first**.

Inside `GovernanceGateway.execute()` (`src/gateway.ts`):

1. After identity, OWASP scanning, budget reservation, policy evaluation, and approval checks succeed, the gateway calls `this.config.auditLedger.append({ phase: 'pre_execution', ... })`.
2. **Only after `append()` resolves** does the gateway call `await executor(request)`.
3. If `auditLedger.append()` throws `AuditLedgerUnavailableError` (for example, PostgreSQL is unreachable or readonly), `executor(request)` is never invoked and the reserved workflow budget is released cleanly in the `catch` block.

## Database Engine Immutability (`001_audit_ledger.sql`)

The schema migration at [`db/migrations/001_audit_ledger.sql`](https://github.com/trulyagentic/truly-agentic-governance-kit/blob/main/db/migrations/001_audit_ledger.sql) enforces append-only semantics directly in PostgreSQL:

* Strict `CHECK` constraints on `phase` (`pre_execution`, `post_execution`, `blocked`, `approval_transition`, `kill_switch`), `action_tier` (`read`, `propose`, `write`), and `decision` (`allow`, `require_approval`, `deny`, `executed`, `failed`).
* Trigger `trg_audit_ledger_immutable` (`BEFORE UPDATE OR DELETE ON audit_ledger`) invokes `reject_audit_ledger_mutation()`, raising a PostgreSQL exception (`RAISE EXCEPTION 'audit_ledger is append-only: % operations are prohibited', TG_OP;`).
* `PostgresAuditLedger.append()` runs inside a transaction with `LOCK TABLE audit_ledger IN SHARE ROW EXCLUSIVE MODE` so sequence numbers (`seq`) and `prev_hash -> entry_hash` chains remain strictly linear under concurrent workers.

## Hash Chain Computation & Verification

Every record computes its `entry_hash` from `GENESIS_HASH` (`'0'.repeat(64)`) forward via `computeAuditEntryHash` (`src/audit-ledger.ts`):

```ts theme={null}
import { computeAuditEntryHash, verifyAuditChainEntries } from '@trulyagentic/governance-kit';

const entries = await postgresLedger.list();
const verification = verifyAuditChainEntries(entries);

if (!verification.valid) {
  console.error(`Chain tampered at seq=${verification.brokenAtSeq}: ${verification.reason}`);
}
```

For long-term regulatory retention and SIEM export, the companion [AWS & GCP Terraform modules](https://docs.trulyagentic.io/deploy/terraform-aws-gcp.md) provision WORM buckets (`S3 Object Lock` and `GCS Retention Lock`) with write-only runtime IAM permissions.


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