Repository navigation
knowledge.restore brings a deleted document and its chunks back, and never what an erasure reached - #309
Merged
Conversation
zlareb1
force-pushed
the
feat/knowledge-restore
branch
2 times, most recently
from
November 8, 2026 01:20
d9d1f67 to
a6c8328
Compare
…never what an erasure reached Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
zlareb1
force-pushed
the
feat/knowledge-restore
branch
from
November 8, 2026 04:27
c148a30 to
4b61c8b
Compare
This was referenced Nov 8, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
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):
knowledge.restoreundoes aknowledge.deleteorknowledge.bulk_delete: the document's last version and its chunks are searchable again. It never brings back what an erasure retired, and it says whether the stored original could be kept and, when it restores nothing, why.Why
A document delete is already a soft delete: the document row and its chunks stay, marked superseded. Memory has
memory.restorefor the same mistake, but a deleted document had no way back short of adding it again, which loses its id and revision and needs the content at hand. An agent that deletes the wrong document, or a source sync that retired one by mistake, needs one call to undo it.Design
The op is Revise-shaped, on the knowledge plane, with the preconditions of
memory.restore:org,namespace,source_uri, andif_revision, which restores only when the deleted document is at that revision.restoredwithchunks_restoredandoriginal(kept,gone,none), or areason:not_found,live(withdoc_idandrevisionnaming the live version, which may be a later write),revision_mismatch,not_restorable, orexpired.How it works:
superseded_at) and the supersession rows it writes for the document's live ingestion (loom.chunk_supersessions). Restore takes the ingestions of the row's revision (chunks record it inchunk_metadata) listed as retired at exactly that time, removes those rows (a lightweightDELETE, every embedding model, nothing another retirement wrote), and writes the document row back live with a new version. The chunks read as live again through the sharedLIVE_CHUNK_SQL, on N-1 and N alike.doc_superseded_aton its chunks without writing supersession rows, so its documents have no signature and arenot_restorable.blob_erased_at), refuses the restore.server/erasure/store/steps_memory.py,supersede_chunks_by_namespace/_by_user; the mutation itself is unchanged, only the count that gates it), so the stamp outlives the ledger row. Restore also reads the ledger (ledger_reads.unfinished_reaching, a read ofloom.erasure_requestsnarrowed in SQL to the erasures that may reach the namespace, with the clock margin the originals fence uses) and refuses when an erasure that may reach the document had not finished when it was written: its namespace's, a namespace list naming it (one it cannot read covers), or the user who wrote the restored revision. A user's purge also supersedes in place another writer's version that the user's replaced, but that version has no delete signature, so it is refused already; for a cut-off restore's live leftovers, which no signature ties to a writer, any user's erasure covers. The ledger rows carrypurge(server/erasure/store/ledger.py). The scope rule lives inserver/erasure/domain/scopes.py(may_cover) and the clock margin inserver/erasure/app/ledger_reads.py(LEDGER_CLOCK_MARGIN_S), both shared with the originals move. The ledger keeps a request 90 days, so a restore reaches deletes of the last 89 (expiredafter); a unit test pins that window inside the TTL inschema.sql.DELETEof its own row (by its version, so the purge finds no copy of the content left), otherwise the row written back retired (a merge may already have folded the delete's row into the restored one, so deleting it could leave no row); then its ingestions listed as retired again at the delete's own time, so the delete's signature is what it was. Any other failure once the writes begin takes it back the same way: it reads the ledger again, as the restore read it (a user's purge counts only for the writer of a signed revision), and deletes its row under a purge. Otherwise, and when the ledger cannot be read, it writes the row back retired (a delete could leave the source with no row, since a merge may fold the delete's row into the restored one at any moment); when a later write's row won meanwhile, the undo leaves it as it is and retires only the restore's own ingestions. Text writes take no claim, so one that started before the restore stores its row under a lower version and the restored row hides it, as a delete's row hides one that started before the delete; the docs say so and say to write it again. The row the undo writes back is built from the newest row, so ablob_erased_atorsuperseded_atan erasure stamped on the restored row in place stays; when no row is left (a purge took them all), it writes nothing. Leftover live chunks of a cut-off restore that an erasure covers are retired.not_found). A document with no chunks has nothing a delete listed and isnot_restorable; adding it again restores it.doc_version, keeps an ingest's unpublished chunks (written before it takes the claim, and listed by a delete that raced it) out of the restore.claim_source), as an ingest's publish and a metadata edit do, and re-reads the row there; a row that changed in between isPRECONDITION_FAILED, and a second call is safe. Text writes and deletes take no claim: a text write landing on the same source during a restore can leave both versions' chunks searchable until the next write of that source, as it can during a delete today, and a delete landing during one can leave the document live with no searchable chunks until a second delete. The docs say both.max(now_ns, version + 1)), so a replica with a clock behind the deleting one still wins underReplacingMergeTree.document_blob_gcsweep had removed it, the restored row names no original and the answer saysgone; an extraction artifact the sweep removed is cleared from the row the same way. The sweep reads unreferenced originals in batches, so one it read just before the restore may still go; the download then fails as it does for any missing original. Keeping originals for a restore window would hold bucket bytes for every delete; reporting it costs nothing.Alternatives:
Writing a "restored" row into
loom.chunk_supersessionsinstead of deleting rows would changeLIVE_CHUNK_SQLfor every reader, and an N-1 reader would still see the chunks as retired.Re-inserting the chunk rows would copy every vector.
Trusting the ledger alone misses an erasure whose row has expired; the stamps cover it. Trusting the stamps alone misses an erasure that has not run yet, or ran on a build that stamped only live chunks; the ledger and the 89-day window cover those.
frontend/src/views/Documents.tsx,frontend/src/components/Toast.tsx,frontend/src/api/knowledge.ts: the Library's delete notice carries Undo, which callsknowledge.restorefor the namespace the delete ran in and says why when nothing came back. A notice with an action keeps no clock, so a keyboard or screen-reader user can reach it, and is keyed by the document, so a later delete of it replaces the notice and its Undo. Undo hands focus back to the search field. Neither delete confirmation says the delete is irreversible any more: the single one points at Undo, and the bulk one says a bulk delete has no Undo in the dashboard and each document can be restored through the API; both say the uploaded file may not come back. This is the dashboard control a new person-facing op ships with (the op reach census).Scope and risk
chunks.supersedecount includes them, and itsALTER UPDATEnow runs for a scope that holds only retired chunks, where it used to be skipped. It stamps more, never less; the residue census and the purge path are unchanged.DELETEs on a request path: the supersession rows one delete wrote, and the one document row the restore wrote (by its version), only when it takes itself back under a purge that may cover the document.knowledge.restore(writer, audited, experimental, idempotent), reachable over HTTP, the SDKs andloom_op. It is not in the default MCP listing.knowledge.delete; the leakage walk skips it with the other knowledge writes.Compatibility, rollout and rollback
knowledge.restoreand PythonAgent.knowledge.restore(source_uri, if_revision=None); generated SDK tables regenerated.Restore's ordering against erasures, other writes and the original sweep, and what it cannot take back, is on
docs/architecture/failure-and-recovery.md(Knowledge restore); the feature page keeps the contract and the reasons. One window is named there and not closed: the sweep deletes an unreferenced original by age alone, so a sweep pass that read the namespace while the document was deleted can delete the original after restore found it, and the restored row names an original that is gone. Every write that names an existing original again shares it; closing it means a reference re-check in the sweep's delete, its own change.The ledger read returns the rows that may reach the document (its namespace's, the writer's, every user's for a cut-off restore's leftovers, and namespace lists to parse), bounded by the ledger's 90 days. An undo deletes its row only when the erasure purges as its saga reads it (a user's from the column, a namespace list's from its scope); a namespace erasure only supersedes in place, so it never does. A failed write is taken back, and so is the restore of a request that is cancelled (nobody hears of it and no audit row would record it). The claimed part of a restore, from the re-read to the write back and its undo, runs as one shielded task (
server.core.shielded, which now returns the coroutine's result), so a cancelled request neither cuts the undo short nor gives up the source's claim before it ends (unit-tested with a raised error, a raised cancellation from the claim's own timeout, and a cancelled anyio task group with and without an erasure at the second ledger read: the restore is taken back and the claim released after its last write); the chunk count runs with the writes, before the second ledger read, so a failed count is taken back like any failed write. A caller that goes away after the restore answered, while the transport writes its audit row, can leave it standing with no audit row, as any audited write can (failure-and-recovery.md). The dashboard's Undo restores the document's last deleted revision, whichever delete retired it (docs/features/dashboard.md).Tests
tests/unit/test_knowledge_restore.py: every branch (restored, not found, live, a revision precondition, an erasure by chunks and by request, no delete signature, a swept or erased original, a row that changed under the claim, a held claim).tests/integration/test_knowledge_restore.py, against a private ClickHouse and server (LOOM_CH_*andLOOM_BASE_URLexported from the worktree's.env, thenpytest tests/integration/test_knowledge_restore.py), on the server built from the head, rebased on main: 20 passed in three runs in a row, the first straight after the server started (logs kept). Three of them run the ledger statement on real ClickHouse while it returns covering rows: a queued namespace erasure and a queued erasure of the writer each keep the document deleted, and a direct read returns the namespace's, the writer's and a namespace list's rows, leaves out another user's unless every user's is asked for, and keeps a completed erasure only when it finished after the document was written.versionandsuperseded_atmatches what it was, the chunk count matches, and search finds it again; a second restore answerslivewith the samedoc_id;not_found;erasure.request, run to completion) on a document deleted before it and on one live when it ran: bothnot_restorable, chunks still retired;not_restorableon the chunk stamp alone; one of another user: the document restores;namespace_listrequest in its object form keeps a deleted document retired;bulk_deleteleaves the other retired;system.query_logon that run: each restore read finished in 3-9 ms reading at most 14 rows, and the lightweightDELETEin 18 ms, inside the op's 200 ms budget.frontend/src/views/Documents.restore.test.tsx: Undo restores into the namespace the delete ran in, says why a refusal left the document deleted, returns focus to the search, a second delete of the document replaces its notice, axe passes with the notice up, and the notice outlives the toast clock. The frontend suite (2,074 passed), build and bundle budget pass.make lint,make typecheck, and the unit slice covering ops, reach, MCP, knowledge, SDK, audit, erasure, mutations and docs (7,741 passed; the SDK surface snapshot updated for the new method).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 SQL was run against a real ClickHouse through the integration tests, and the erasure paths were read to confirm which rows they keep.
I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make format-check,make lint,make typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.git commit -s).🤖 Generated with Claude Code