# WORM tier (/docs/worm)



The `worm` tier makes the event log and the call-row table write-once at the
database. While enabled, triggers refuse `UPDATE`, `DELETE` and `TRUNCATE` for
every role. Rows leave only through two destroy functions, one per table, which
check legal holds first. Every outcome is written to an append-only disposal
record.

A table owner or superuser can still disable the triggers, so the tier
constrains application roles. For that reason we don't grade it as the
non-rewriteable storage format that SEC recordkeeping rules describe. See the
[SEC and FINRA mapping](/docs/compliance/sec-finra) and
[What you provide](#what-you-provide-alongside-it).

The [hash chain](/docs/hash-chain) detects an edit after the fact. WORM refuses
the edit when it's attempted. The two answer different questions, so run both.

**Subpath:** `@pleach/core/schema`

## Applying it [#applying-it]

The tier is opt-in, Postgres only, and not part of the default apply. It covers
the event log and the call-row table. To remove records past a retention period
while honouring legal holds, schedule `runChainRetentionPass` from
`@pleach/core/eventLog` for event-log chains and `runCallLedgerRetentionPass`
from `@pleach/core/audit` for call-row sessions. Neither has a default period.
Name the tier:

```bash
npx @pleach/core schema --apply --with worm
```

`--apply` copies the SQL into your migrations directory; your migration tool
runs it. See [Schema → Applying the bundle](/docs/schema#applying-the-bundle)
for targets and clash handling.

Re-apply the whole tier, not a single file. Re-applying one file can put back an
older disposal function that writes no disposal record. `runChainRetentionPass`
destroys nothing while the live function does not record.

`runChainRetentionPass` also refuses by default when the disposal check is
missing. It scans and destroys nothing and reports `disposal_check_absent`,
dry runs included. So apply the disposal-record SQL together with the destroy
function: a destroy function without it makes every scheduled pass refuse.
Pass `requireDisposalCheck: false` to run without the check.
`runCallLedgerRetentionPass` has no such option.

Requirements:

* Apply after the base tier.
* Apply as a role that can `CREATE ROLE`: a superuser, or a role with
  `CREATEROLE` such as Supabase's `postgres`.
* `--with all` includes `worm`, and this tier refuses every `UPDATE` on the
  event log and the call-row table. Name tiers one by one if you don't want
  it. A run without `--apply` is a dry run and prints the plan.

## What it refuses [#what-it-refuses]

The event log refuses every row `UPDATE`, `DELETE` and `TRUNCATE`, except a
`DELETE` from inside the destroy function. A refused statement fails with an
error naming the operation and the role. The refusal covers `service_role`,
`authenticated`, `anon`, and the role that applied the schema.

Row-level security can't do this on its own: `service_role` and table owners
are exempt from un-forced RLS, and RLS never checks `TRUNCATE`.

There is no `UPDATE` allow-path. Core's writers only insert, so a host process
that updates event-log rows will start failing once the tier is applied.

The call-row table behaves the same way. A host process that updates or deletes
call rows directly, for example to correct a price or purge old sessions, will
start failing. Record a correction as a new row in a table of your own, and
remove rows through the destroy function below.

### Foreign keys from your tables [#foreign-keys-from-your-tables]

If your host adds a foreign key to the event log with `ON DELETE SET NULL` or
`CASCADE`, a parent delete becomes a change to event-log rows, and the guard
refuses it. The key then behaves like `RESTRICT` for any parent that still has
event rows. That is deliberate, because blanking an attribution column rewrites
evidence. Soft-delete the parent, or declare the key `ON DELETE RESTRICT`.

## Disposal: the event-log destroy function [#disposal-the-event-log-destroy-function]

Rows leave only through this function. It takes exactly one target: a whole
chat, or unchained rows by id. You also pass the tenant, a reason and the
actor.

| Returns                      | Meaning                                           |
| ---------------------------- | ------------------------------------------------- |
| `destroyed` with a row count | That many rows were deleted                       |
| `held` with the hold id      | An active hold covers the target; nothing deleted |

A hold is **returned**, not raised, so you can count holds separately from
errors.

These fail with a clear error and delete nothing:

* A blank reason or actor, or not exactly one target.
* A row id that is chained. Destroy the whole chat instead.
* A transaction isolation level other than `READ COMMITTED`.
* A `RESTRICTIVE` RLS policy on the log or the holds that applies to the disposer.

`EXECUTE` is granted to `service_role` only. Keep disposal transactions
short: a hold placed during a disposal waits for it to finish.

The install refuses unsafe role grants. If it refuses, adjust the grants or
policies it names and re-apply.

## Disposal: the call-row destroy function [#disposal-the-call-row-destroy-function]

Call rows leave only through this function. It takes exactly one target: a
whole session, or unchained rows by id. You also pass the tenant, a reason, the
actor, and optionally a cutoff time.

| Returns                      | Meaning                                                        |
| ---------------------------- | -------------------------------------------------------------- |
| `destroyed` with a row count | That many rows were deleted                                    |
| `retained`                   | A cutoff is set and the session's newest row is at or after it |
| `held` with the hold id      | An active hold covers the target; nothing deleted              |

Its errors, isolation rule and `service_role`-only `EXECUTE` match the
event-log function. A `chat` or `chain` hold covers call rows when its ref is
the session id, or a chat tied to that session. A hold on a chat therefore also
holds that chat's model calls.

Destroying a chained session leaves its chain head in place as the record that
the chain existed. Don't write new call rows under a destroyed session id.

## Disposal records [#disposal-records]

The disposal record holds one row for every outcome a destroy function
returns: `destroyed`, `held` or `retained`. Each row names the target, the
reason and actor you passed, the database login that called the function, and
the outcome. A call that raises writes no record and deletes nothing.

The records are append-only for every role, and `service_role` can only read
them. Nothing prunes them: a retention pass that meets the same held chain every
run adds a `held` row each time, so size the pass frequency with that in mind.

## Legal holds [#legal-holds]

The destroy functions consult the legal-hold register. A hold is active until
it is released.

| Scope     | Ref                           | Blocks                           |
| --------- | ----------------------------- | -------------------------------- |
| `tenant`  | None                          | Every disposal in the tenant     |
| `chat`    | Chat id (lowercase UUID text) | Disposal of that chat            |
| `chain`   | Chain id (today, the chat id) | Disposal of that chat            |
| `subject` | Opaque host subject token     | **Every** disposal in the tenant |

A `subject` hold fails closed: event-log rows carry no subject field, so the
function can't tell which rows the hold covers.

Holds are insert-only:

* `service_role` can read and insert hold rows, and nothing else.
* `DELETE`, `TRUNCATE` and every `UPDATE` except one release are refused for
  every role.
* A hold placed during a disposal waits for it to finish.

You place a hold by inserting a row with the tenant, scope, ref, a reason and
who created it.

The one permitted change is a single, attributed release, made through a
release function that takes the hold id, the actor and a reason. No application
role can execute it by default, `service_role` included. Grant it deliberately
to an operator role.

The register records holds and releases. A retention floor or a signed hold
record, where your procedure calls for one, is yours to add.

## What you provide alongside it [#what-you-provide-alongside-it]

The tier constrains the **application** roles: `service_role`,
`authenticated` and `anon`. Database administration stays with you.

### Your duties [#your-duties]

* **Table ownership and superuser access.** Limit who holds them under
  your access procedures. On Supabase the table owner is `postgres`.

### Setup [#setup]

* **Disposer role membership.** Keep both disposer roles free of members.
* **Backups and restores**, and everything below SQL, under your
  infrastructure controls.
* **Scheduled anchoring.** Pair the tier with the hash chain and [scheduled
  anchoring](/docs/hash-chain#verifying-the-anchors), so the record stays
  verifiable against an anchor held outside the database.

## GDPR erasure [#gdpr-erasure]

For a worked case where an erasure request meets an active hold, see
[the litigation-hold example](/docs/compliance/gdpr-erasure#example-an-erasure-request-during-a-litigation-hold).

WORM doesn't conflict with crypto-shredding, which destroys the subject key and
never touches an event-log row.

Row deletion with a chain rebuild does conflict: every rewrite is refused, and a
chained row can't be removed by id. Under WORM the deletion route is the
destroy function on a whole chat. See [GDPR erasure](/docs/compliance/gdpr-erasure)
for why crypto-shredding is the recommended approach.

## Where to go next [#where-to-go-next]

<Cards>
  <Card title="Tamper-evident hash chain" href="/docs/hash-chain" description="Detects an edit after the fact; WORM refuses it at the database." />

  <Card title="Schema" href="/docs/schema" description="The base tier, every extension tier, and how --with applies them." />

  <Card title="GDPR erasure" href="/docs/compliance/gdpr-erasure" description="Crypto-shredding vs row deletion against an append-only log." />
</Cards>
