Skip to content

perf(platform): seal the audit chain off the write path - #4700

Merged
yannickmonney merged 10 commits into
mainfrom
perf/audit-async-sealing
Oct 10, 2026
Merged

yannickmonney merged 10 commits into
mainfrom
perf/audit-async-sealing

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Summary

Every audited write used to append to its organization's hash chain inside its own transaction: chain head locked FOR UPDATE, prior hash read, row hashed, head bumped. Under SERIALIZABLE that made every audited write of an organization one queue — about one write per transaction duration per organization, and the biggest single limit on the path to a million concurrent users. This moves the chain off the write path:

  • Writers insert the row unsealed (integrity_hash NULL, writer_xid stamped). No chain lock, no head row, no retry at the head.
  • A sealer in every worker (domains/audit_logs/sealer.ts) chains pending rows per organization under a try-lock, in batches, assigning chain_seq; the chain's order is the seal order. Same hash input as before, so existing rows verify unchanged. Rows sealed before this release keep chain_seq NULL and come first.
  • Migration 0194 adds chain_seq, writer_xid, the head's last_seq, and a trigger that positions an older image's inline append during a rolling deploy. The nightly walk is renamed audit.chain_check (the old name is retired) because an older worker would read seal order as a break.
  • Verification and export seal first; the admin panel says how many entries are still waiting to be sealed (EN/DE/FR). Retention cuts the oldest prefix in chain order.
  • Writers stop serializing on the chain: definition writers take their name lock first; the event dispatch stamps triggers in id order; task and wake writes take a per-project work key (lib/project-work-lock.ts) instead of the organization's chain key; comment retries queue on an org comment key that first attempts hold shared.
  • The deployment history orders by writer_xid, since ts no longer rises strictly per organization.
  • Spec AUDIT-R8, docs (EN/DE/FR), manual box GOV-F64.

Checklist

  • bun run check passes (format, lint, typecheck, all tests). Run per part: format check, conflict markers, links, platform oxlint (type-aware) and tsc, server + pii unit suites, UI suite.
  • bun run lint:sast passes (Opengrep) — 0 findings.
  • Translations updated in the affected messages/{en,de,fr}.yml catalogs — the integrity panel's waiting note (settings.yml).
  • Docs updated in docs/{en,de,fr}/ — audit log integrity (self-hosted) and the admin audit-log page.
  • README.md / README.de.md / README.fr.md — N/A, nothing they describe changes.

Test plan

  • Real-Postgres integration, full run against a throwaway database, compared with the same run on the merge base: 185 of 256 lanes ran on both before both stop at checkConversationApi (an API-key attribution assertion that fails on main today); every failure on this branch fails identically on main. The remaining lanes run filtered on both sides, with identical results.
  • New lanes: a burst of audited writes commits without retries and seals into a chain that verifies; a late-committing row is sealed after a later one and keeps its time; an older image's inline append during the roll takes its position from the head; racing sealers chain every row exactly once; comment bursts on one task and across an org's tasks each lose at most once; standing-role wake scenarios and trigger lock orders hold under the new keys.
  • Unit: sealer, verifier (legacy and positioned walks), retention prefix in chain order, the project work key, comment queue marks, writers' lock order.

WIP: writers insert unsealed rows; the worker's sealer chains them by position (chain_seq); the verifier and the retention sweep follow the chain's order; the nightly check is renamed so an older worker never walks the new order. Lock-site cleanup, integration lanes and docs follow.
Integration lanes for a burst of audited writes, a late commit, an older image's inline append during a roll and racing sealers; the verify panel shows entries not in the chain yet (EN/DE/FR); docs, AUDIT-R7 and GOV-F64.
Sealing moved off the write path, so an audited write no longer touches the
chain head, yet writers still took the organization's chain lock first and
kept every definition save, event dispatch, task write and wake write of an
organization in one line.

- Definition writers (save, deploy, bindings, trigger save and delete, the
  delete cascade), the event dispatch, the run delete and the retention sweep
  take no organization-wide lock: the name lock first, triggers stamped in id
  order, run rows before trigger rows.
- Task writes and wake writes take a per-project work key before the project
  row or the wake row (lib/project-work-lock.ts); the wake scan try-locks it
  per project, so one busy project never stalls the scan.
- The deployment history orders by the writing transaction's id (writer_xid),
  since ts no longer rises strictly per organization.
- The sealing migration is renumbered 0194; 0182 is claimed elsewhere.
- The standing-role wake lanes wait on the claim keys the writers now queue on.
A comment write used to end on the organization's chain head, so a loss
anywhere in it queued the retry on the chain key. The chain is sealed off the
write path now, but serializable isolation still aborts one of two comment
writes on different tasks over an index page both touch, and a retry queued
on its task alone could lose again to the rest of a burst.

Every comment write now holds the organization's comment key shared, after
its task's, so comment writes never wait for each other; a loss is marked
with that key too, and the queued retry holds it exclusively from before its
BEGIN. It waits for the writes already running, runs while later ones wait,
and a writer loses to the organization's other comment writes at most once.

The cross-task burst lane seals before it verifies and asserts that bound;
the retry-queue notes no longer use the chain head as their example.
The loop read a flag only its stop() closure changed, which the linter
cannot see; an AbortController's signal ends both the loop and its pause.
Main took AUDIT-R7 for key-made audit attribution while the sealing rule was
in review; the sealing rule and the tests that cite it move to AUDIT-R8.
The member and create-member doubles still answered the chain head's
statements and expected three audit writes; an audited write is now the one
row insert, and any chain-head statement would be an unexpected query.
The phase-2 retention lane inserted its ancient audit rows already sealed,
so migration 0194's trigger gave them the head's next chain positions,
after every row the organization had, and the prefix sweep rightly stopped
at the first row still inside the window. They are now sealed the way rows
written before chain positions are, which the chain walks first.
Only the sealer in the same module takes it now that the wake scan try-locks
a project's work key, so it is no longer exported (Knip), and its note names
the caller it has.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant