---
title: Finance audit trail and gapless numbers
description: Every change in Petawork Finance lands in a tamper-evident event chain. What is recorded, how the hash chain works, how to verify it, what is masked, and how document numbers stay gapless.
section: finance
order: 30
audience: [user, operator, agent]
sources: [services/nodejs-cloud/lib/finance/core/events-pure.ts, services/nodejs-cloud/lib/finance/core/events.ts, services/nodejs-cloud/lib/finance/core/events-mcp.ts, services/nodejs-cloud/lib/finance/core/sequences.ts, services/nodejs-cloud/lib/finance/core/chain-sweep.ts, services/nodejs-cloud/components/finance/Events.tsx, services/nodejs-cloud/app/(finance)/finance/settings/events, services/nodejs-cloud/migrations/0425_fin_events.sql]
features: [cloud.page-finance]
---

Petawork's general audit log records who did what, but anyone who can reach the database could edit it without a
trace. Finance keeps its own record on top of it: an **event chain**, one event per change, where each event carries a
hash of the event before it. Change, remove or reorder an event and the chain breaks at that event, and a check names
it. Documents that need numbers (bills, invoices, payment runs, as the modules arrive) get them from a counter that
never skips one.

## What is recorded {#recorded}

Every change made in Finance appends one event, in the same database transaction as the change itself. If the change
is refused or fails, there is no event; if the event cannot be written, the change does not happen either. Today that is
every change to the [reference data](/docs/finance/reference-data.md) (entities, dimensions and their values, accounts, tax codes,
exchange rates, period status), every [role](/docs/finance/roles.md) grant and revocation, and every change to a
[party](/docs/finance/parties.md) or its bank accounts, including each reveal of an account number. Each module adds its own as it arrives.

Each event has:

| Field | What it holds |
|---|---|
| `seq` | Its number in the organisation's chain: 1, 2, 3 and so on, with no gaps. |
| `at` | When it was recorded, to the millisecond (UTC). |
| `actor` | Who made the change: a person (`user:<id>`), an agent (`agent:<id>`) or Petawork itself (`system:<what>`). |
| `entity_id` | The entity the change belongs to, or empty for something every entity shares (a dimension, a tax code, a rate). |
| `subject_kind`, `subject_id` | The record that changed: `entity`, `dimension`, `account`, `tax_code`, `fx_rate`, `period`, `role_grant`, `account_import`, `taxcode_import`, `party` or `party_bank_account`, and its id. |
| `action` | What happened, such as `finance.account.updated` or `finance.period.locked`. |
| `detail` | The record before and after the change, and `via`, the credential used (`key:<id>`, an OAuth grant or the console). |
| `agent` | When an agent acts under a Finance job: the job, the job's version and the rule that allowed it. Empty otherwise. |
| `prev_hash`, `hash` | The previous event's hash, and this event's own. |

A CSV import is **one event per file**, not one per row: it holds the counts (rows, created, changed) and every row's
code, so a 5,000-line chart of accounts does not become 5,000 events. A dimension saved together with its values is
one event too, naming the values it touched.

The organisation's general audit log in the console still gets its own row for each change, as before.

## How the hash chain works {#chain}

An event's hash is SHA-256, written as 64 lowercase hex characters, over the event's fields in a fixed text form
(canonical JSON):

```json
{"action":"finance.account.created","actor":"user:abc","agent":null,"at":"2026-11-23T10:00:00.000Z","detail":{"after":{"code":"4000","name":"Rooms"},"before":null},"entity_id":null,"prev_hash":"0000000000000000000000000000000000000000000000000000000000000000","seq":"1","subject_id":"acc-1","subject_kind":"account","tenant_id":"00000000-0000-0000-0000-000000000001","v":1}
```

That text hashes to `6a9cb77cf94e08caaf7a8e3e4a462893da602df75017f9233423fe6b8eaecc0b`. The rules that make the text
the same every time:

- Object keys are sorted at every level; arrays keep their order; there is no whitespace.
- Numbers are whole numbers only (money is always in minor units, such as cents). Anything else, such as an exchange
  rate, is stored as text.
- `seq` is written as text, and times as ISO 8601 with milliseconds and `Z`.
- `prev_hash` is the previous event's `hash`. An organisation's first event uses 64 zeros.
- `v` is the version of this form. It is 1.

Because each hash covers the one before it, changing any field of event 40 changes its hash, so event 40 no longer
matches. Rewriting event 40's hash to hide that makes event 41's `prev_hash` wrong. Deleting an event leaves a gap in
`seq`. Numbers are given out under a lock per organisation, so two changes at the same moment never get the same
`seq` or split the chain.

The database refuses to change or delete an event, whoever asks (only deleting the whole organisation removes its
events), and Petawork's own code has a test that fails if anything tries.

## How to verify {#verify}

- **In the app.** Finance > Settings > Events (`/finance/settings/events`) lists the organisation's events, newest first, with what changed. Press
  **Verify chain** to recompute every hash. It says the chain checks out, or names the first event that does not and
  why: the event was changed (`hash_mismatch`), an event before it was changed or removed (`prev_mismatch`), or an event
  is missing (`gap`).
- **Over MCP or REST.** `finance_event_verify` (`GET /api/v1/finance/events/verify`) does the same, from `from` to `to`
  if you give them. One call checks up to 50,000 events; when `complete` is false, call again from `last_seq + 1`.
  `finance_event_list` (`GET /api/v1/finance/events`) lists events, for one record with `subject_kind` and
  `subject_id`, for one entity with `entity_id`, or one kind of change with `action`.
- **On your own.** Export the events with `finance_event_list` and recompute each hash with any SHA-256 tool from the
  form above. You do not need Petawork to check Petawork.
- **Every day, automatically.** A background check recomputes each Finance organisation's new events since its last
  run. A break is recorded and lands on the [exceptions queue](/docs/finance/exceptions.md#chain-break) as one critical
  `chain_break` exception, which stays the one open exception while the chain stays broken.

Who may read events: anyone whose [Finance role](/docs/finance/roles.md) includes the audit trail, which is a controller or an
auditor. Someone whose role covers only some entities sees those entities' events and the shared ones. Verifying reads
every entity's events, so it needs the role on every entity. Owners and admins are controllers everywhere.

## What is masked {#masked}

Before an event is written and hashed, values under these field names are replaced with `[redacted]`, at any depth:

- any name ending in `_sealed` (values Petawork keeps encrypted),
- `account_number`, or any name ending in `_account_number`,
- any name containing `token`, `secret`, `password` or `passphrase`.

The field stays, so you can see that a value was there, but its content never enters the chain.

## Gapless numbers {#numbers}

Some documents must be numbered with no gaps: an auditor asks where invoice 1043 went. Finance gives each entity one
counter per document type (`fin_sequences`). A number is taken at the moment a document is issued, inside the same
transaction that issues it, while the counter is locked. If the issue fails or is cancelled before it commits, the
number is not used and the next document gets it. So numbers are consecutive and never repeat, even when many
documents are issued at once.

A number is the document type's prefix followed by the count, padded to six digits: `INV-000042`. The modules that
issue documents (payables, billing, payments) use this as they arrive.
