Skip to content

Under Postgres, loom reconcile runs again in a restored store the erasures and redactions the other records past the restore point - #460

Merged
zlareb1 merged 6 commits into
mainfrom
feat/pg-erasure-ledger
Nov 10, 2026
Merged

zlareb1 merged 6 commits into
mainfrom
feat/pg-erasure-ledger

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • New Feature

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

Under the Postgres authority, loom reconcile --restored clickhouse|postgres --point <time> repairs a deployment after one of its stores is restored from a backup: the erasures and redactions the other store records past the restore point run again, and after a ClickHouse restore every revision and knowledge document committed since is published again, so nothing erased or redacted comes back. Knowledge chunks live in ClickHouse alone: the command names each source to re-chunk with loom rechunk (past 20 in a namespace, the namespace).

Why

Under the Postgres authority a deployment has two stores, and a restore of either brings back an older state. Before this change:

  • after a ClickHouse restore, ClickHouse lacked every revision published after the backup, and it served again what was erased or redacted after it. The saga ledger (loom.erasure_requests) is in ClickHouse, so the erasures themselves were forgotten, including what they removed from tables keyed by user or namespace;
  • after a Postgres point-in-time restore, erasures and redactions made after the recovery point came back as live heads and revisions.

A restore loses what was written after its point; that is what restoring means. It must not bring back what was forgotten after it.

Design (reviewed adversarially before coding; the rounds cut it to this):

  • Each store records the erasures for the other. loom.erasure_ledger (Postgres migration 006_erasure_ledger.sql) gets each saga's row from begin_erasure, before its first ClickHouse checkpoint, then completed_at at its end or abandoned_at when the recovery worker gives up on it. The erasure recovery pass copies in what only ClickHouse records (erasures of a build before this one, or one a process of the previous build ran during a rollout) and the terminal states this ledger missed, a page at a time from a cursor that walks ClickHouse's ledger and starts over at its end, and sweeps rows past LOOM_ERASURE_LEDGER_DAYS (live, default and maximum 89, the days ClickHouse's ledger reads keep inside its 90-day TTL).
  • Redactions need no ledger. Each store records them in its own rows: Postgres in memory_revisions.redacted_at, ClickHouse in memory_versions.redacted_at.
  • The restore is declared, not guessed. The operator stops the deployment, restores, and runs loom reconcile --restored clickhouse|postgres --point <the backup's time>. A dry run is the default.
  • Which erasures run again.
    • Only those the other store records as completed after the point itself are replayed (one completed before it is in the restored store, and running it again would delete what the user wrote after it). The replay runs the restored store's steps directly, each under its step name, with no census: the other store holds what the user wrote after the erasure, which a census would count and a resumed saga would delete. After a ClickHouse restore its ClickHouse row reads failed with "replay after a restore pending: run loom reconcile again" while the steps run (a row the recovery worker and loom erasure-re-verify --resume skip), then the saga's completed row and an erasure.completed audit row once all ran. After a Postgres restore the loom.erasure_ledger row is completed once the Postgres steps ran. A step that fails is reported by name, and the reconcile is run again whole.
    • An erasure with no terminal state is requeued for the recovery worker: after a ClickHouse restore its checkpoint is written again with every step to run but the Postgres ones the ledger records as committed (or, when the restored row shows it completed, the Postgres ledger row is completed instead); after a Postgres restore its ledger row, and its checkpoint with the Postgres steps the restore undid reopened.
    • An abandoned one is not run again; loom erasure-re-verify owns it.
  • After a ClickHouse restore:
    1. Every Postgres revision and knowledge head committed after the point (less a writer's lease and an insert's bound) is owed again as a delivery held parked (lease_until infinity, fenced_until now). An erasure waits nothing for it, and no claim takes it.
    2. Every Postgres head that ClickHouse holds at an older version, or not at all, is held the same way. These are revisions committed before the point but published after it, compared a page at a time.
    3. The completed erasures run again without their Postgres steps (they hold, and running them again would delete what the user wrote after the erasure); external backends are not restored, so their steps are skipped.
    4. The held deliveries are released and published through the delivery path, under the durability gate. A republished head owes its search document, or for a delete the retirement of the vectors ClickHouse restored below it, plus a deleted decision's record.
    5. Every redaction Postgres records after the point runs memory.redact again. That scrubs the restored rows and the cached answers and precedents quoting them; Postgres keeps its first redaction.
  • After a Postgres restore: the completed erasures run their Postgres steps and the replay answers' step (a cache with a Postgres copy) only, on the restored copy, and are written into the Postgres ledger. Every redaction ClickHouse records past the point is applied to the restored copy; one whose version is the copy's live head again (the write that replaced it was lost) is scrubbed in place. What was written after the point is lost, as with any point-in-time restore. Search does not serve ClickHouse's copies of it, since no Postgres head matches them.
  • Refusals, before anything is written: a point later than now; --restored both (erasures after both points are in neither store; reconcile names the window to issue again from the audit export); a point older than the ledger keeps; a worker that reported a pass within two workers-lease terms (LOOM_SUPERVISOR_LEASE_TTL_S, a minute by default; --allow-live overrides).
  • RPO and RTO for both stores are documented in docs/operations/backup-and-recovery.md, "Under the Postgres authority".

Rejected alternatives:

  • A watermark replay by seq or version. ClickHouse rows carry no seq, and copy writers draw versions before their reads.
  • An erasure count as the restore signal. Sweeps and rows from the previous build give false verdicts, and it misses restores with no erasure in the gap.
  • Running a saga again in full. Its Postgres steps would delete what the user wrote after the erasure.
  • Deleting search documents by version. ClickHouse-only retirements carry versions of their own.

Restore detection and a quarantine refused at the Postgres commit are the follow-up. Until then the runbook says to stop the deployment and always reconcile after a restore.

Scope and risk

Touches the erasure saga (one Postgres row written before its first checkpoint, one completion), the recovery pass (backfill and sweep, Postgres only, isolated from the sagas' recovery), and a new CLI command with the memory, knowledge and erasure functions it calls. Under the ClickHouse authority nothing changes.

A replay's ClickHouse rows and its audit row carry the original erasure's id and the actor loom-reconcile, whose rows a later reconcile trusts whatever their time. A requeued erasure's rows, and those the recovery worker writes as it resumes it, name the recovery worker, which a later reconcile trusts only before its point. The replay answers' step drops the org's whole replay cache in both stores, as in any erasure.

Interaction with the payload clearing in progress (#453): once delivered payloads are cleared, a non-head revision whose payload is gone cannot be republished (the window pass selects row IS NOT NULL; heads keep their payload). A follow-up refuses a ClickHouse backup older than #453's payload_clearing.cleared_through, once that exists.

Compatibility, rollout and rollback

  • Wire: none.
  • Settings: LOOM_ERASURE_LEDGER_DAYS (live, 1 to 89, default 89).
  • Migration: schemas/pg_migrations/006_erasure_ledger.sql, additive (one table, one maintenance index).
  • Rolling upgrade: N-1 writes no ledger rows and marks no Postgres steps. The recovery pass of N copies the rows in from ClickHouse within the ledger's days, and a saga N runs or resumes records its row first. A Postgres step an N-1 process committed is unmarked, so a reconcile after a Postgres restore runs it again.
  • Operator steps: the restore runbook in docs/operations/backup-and-recovery.md.
  • Rollback: the table stays, unread by the previous build. Finish a pending replay with loom reconcile first: the previous build's recovery worker does not skip the "replay after a restore pending" row and would resume it with a census.

Tests

  • tests/unit/test_restore_reconcile.py:

    • which saga steps run in each direction (every ClickHouse step and no census after a ClickHouse restore; only the Postgres steps after a Postgres restore), the completion written before any step, a failed step leaving nothing for the recovery worker, erasures read from the point and revisions from a margin before it, and a refusal when the code registry cannot be read;
    • the republish targets (a live head's document, a delete's retirement, a deleted decision's record);
    • the refusals (both, an old point, a live worker);
    • a point in the future refuses;
    • the order: hold (window, then heads behind ClickHouse), erase, requeue the open ones, publish, redact;
    • a dry run writes nothing;
    • the ledger row precedes the ClickHouse checkpoint;
    • a replay skips the steps the restored copy shows ran (the checkpoint's, less what its census reopened; the Postgres steps the ledger marked in their own transactions), and a resumed saga records its ledger row and unmarks what its census reopens;
    • an erasure completed in the point's second is replayed (clickhouse-local);
    • a requeue keeps the Postgres steps the authority records, and marks the Postgres ledger row of an erasure the restored row shows completed or abandoned instead of running it;
    • the recovery worker's reads and still_open never return a pending replay (clickhouse-local), and loom erasure-re-verify neither counts nor resumes one;
    • the knowledge sources to re-chunk are printed, one by one or by namespace;
    • the recovery pass copies and sweeps the ledger, and notes a failure of either, or of an abandonment's ledger write, without stopping;
    • a requeue's checkpoint names the recovery worker, and the restored ledger is trusted only from rows checkpointed before the point or written by a reconcile, a requeued saga's later rows included (clickhouse-local);
    • a failed request's first checkpoint deletes its open ledger row, and a second reconcile run replays nothing it completed.
  • tests/integration/test_restore_reconcile.py, in the Postgres authority job. One org's rows are copied aside, more is written (a new memory, an update and a redaction, a user's purge erasure), the copy is put back, and the reconcile runs:

    • a ClickHouse restore: the erased user's text is back in ClickHouse after the restore and gone after the reconcile; memory.search returns the same keys, order and scores as before the restore, for four queries; a memory the erased user wrote after the erasure has its row and search document back in ClickHouse (the reconcile deletes no later write);
    • a Postgres restore: the erased user's memory is a live head again after the restore and gone after the reconcile; a version written before the point and redacted after it is the restored head, and after the reconcile memory.get serves no secret; search serves neither.
    • the ledger copy against the real Postgres: a row only ClickHouse records is copied in, a completion the walk passed is copied by the open-row check, a row this ledger ended is kept, the cursor pages on and starts over, and the sweep removes a row past the ledger's days.
    • a memory delivery parked out of attempts, and a knowledge one through both knowledge holds, count their attempts from none once held;
    • pg_ledger.forget deletes an open ledger row and keeps a completed one.
    • All six pass locally against Postgres 16 with a synchronous standby and ClickHouse. The rest of the integration suite runs in this PR's CI.
  • Local unit runs: the new file, the settings reference, the CLI snapshot (regenerated: only loom reconcile added), public copy, doc links and the Postgres table classification pass; the erasure-related unit files pass (7203 tests); tests/unit/test_restore_reconcile.py has 40.

  • CI: tests/integration/test_restore_reconcile.py runs as its own step, last in the Postgres authority job (-p no:randomly): loom reconcile acts on the whole database, so it must not run before other tests that read the delivery queues.

  • tests/analyzer/shapes.txt is regenerated. Its 10 changed shapes come from main (erasure.status returns the erasure's receipt: every substrate its latest census counted, and the purge it applied #462's erasure_requests columns and the schema dump), which landed after the file was last written; this PR's own ClickHouse reads (ledger.completed_since, trusted_row, still_open, redaction.redacted_since, the ledger copy, the heads-behind comparisons, the live-worker read) have no analyzer case yet.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. The replay's step sets were checked against the saga's step lists and its resume path; both drills were run against real stores.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed. Locally: format, lint, the affected unit files and the new integration file; the full suites run in CI.
  • Generated files are regenerated in this PR, not by hand: docs/reference/configuration.md, loom.example.yaml, schemas/loom-settings.schema.json, llms.txt, the CLI snapshots.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version: no plugin file.
  • The frontend builds, if frontend/ changed: not changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s).

🤖 Generated with Claude Code

zlareb1 and others added 3 commits November 10, 2026 06:48
…sures and redactions the other records past the restore point

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…urnal stores

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… values to restore, and the Postgres flag

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/pg-erasure-ledger branch from f380bfa to 91a00ab Compare November 10, 2026 06:53
…orts the knowledge plane once

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
zlareb1 and others added 2 commits November 10, 2026 08:12
…s follow the ledger reads

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…open helper is named for what it does

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 1065897 into main Nov 10, 2026
38 checks passed
@zlareb1
zlareb1 deleted the feat/pg-erasure-ledger branch November 10, 2026 09:41
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
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