Skip to content

loom knowledge migrate-originals moves the originals held in ClickHouse to the bucket, hash-verified - #285

Merged
zlareb1 merged 51 commits into
mainfrom
feat/knowledge-migrate-originals
Nov 6, 2026
Merged

zlareb1 merged 51 commits into
mainfrom
feat/knowledge-migrate-originals

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Related: #157

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):

loom knowledge migrate-originals moves the document originals still stored in ClickHouse to the S3-compatible bucket. Each one is hash-verified before and after the copy, and only then switched to the object.

Why

Since #167, LOOM_DOCUMENT_OBJECT_STORE=s3 writes new originals to a bucket. Originals uploaded before the switch stayed in loom.document_blobs.data, with no way to move them. The issue's plan calls for moving those originals with a copy that is verified before each document is switched. After this command runs and the table merges (or OPTIMIZE TABLE loom.document_blobs FINAL), ClickHouse holds only the catalog rows and the originals nothing needs, which the sweep deletes, and the bytes are in the bucket.

Design

  • The walk: it goes one namespace at a time (blobs.namespaces, which takes an optional org), and through each namespace in key order (blobs.held_in_clickhouse). It moves only the originals the namespace still needs (blobs.needed, one read per page): those a live document names, as its original or its extraction artifact, or that an unexpired upload or an open ingestion holds. The rest are counted as left to the document_blob_gc sweep, which deletes each once it is older than document_blob_min_age_seconds (its unreferenced predicate); one that is needed again by then moves on a later run. Moving them would waste a transfer, and could re-create an original the sweep had just deleted along with its upload records, out of reach of its uploader's erasure until the next sweep. The per-page read narrows that window, but does not close it: a document deleted after the page's read, with the sweep taking the original in the instant before the move's row lands, leaves a switched row with no upload record until the sweep takes it once it is older than its min age, counted from its last upload (the switch keeps the moved row's age). blobs.needed reads the namespace's documents once per page, as the sweep's unreferenced does: measured on a namespace of 300,000 documents, a page takes 300,000 rows, 22.6 MiB and 36 ms. A whole walk is pages × documents, about 5,000 pages of 1,000,000 rows for a million originals and as many documents, which suits a one-off operator command.

    • A page first reads its last key, the key of the limit-th row after the cursor, in key order on one thread.
    • It then aggregates that key range alone and keeps the originals whose latest row (argMax(location, created_at)) has no location, which means the bytes are still in ClickHouse. It reads no data.
    • Measured on a private ClickHouse with system.query_log, a page of 200 in a namespace of 1,000,000 originals reads 15,104 rows for the bound and 256 for the range. An aggregate over the rest of the namespace with a LIMIT read all 1,000,000 rows and 137 MB of memory. A (org, namespace, sha256) > cursor across namespaces read the whole table on every page.
  • originals.move_to_bucket (in server/knowledge/app/originals.py, which holds the orchestration; the knowledge store keeps the statements it uses) moves one original:

    1. (Before any move of a namespace, the walk reads its fence: see below.)
    2. It reads the row's bytes (with sequential consistency) and refuses any that do not hash to their sha256, and any that do not fill their size while the row the walk read is still the latest; otherwise the move stops, as in step 5.
    3. It writes the bytes to a new object, <prefix>/<org>/<namespace>/<sha256>/<write id>, with the store's SHA-256 check, as an upload does.
    4. It reads the object back and hashes it.
    5. It reads the latest row again (blobs.latest, its location and version). If it is no longer the ClickHouse-held version the walk read (an erasure, the sweep or any re-upload changed it since the walk, before this read), the move deletes its own object, which no row will name, and stops.
    6. It checks the erasure ledger again. A covering erasure registered since step 1 ends the move: the move deletes its own object. If the original is still held in ClickHouse after that (the erasure has not reached it yet, or a user's erasure kept it), it is reported and moves on a later run if it is still there; only an original already gone counts as erased. An object gone before its read-back with no erasure behind it is an error, and the original stays.
    7. It refuses to write the row when more than DOCUMENT_UPLOAD_BUDGET_S has passed since the object write began (counted from the write, after the transfer slot is taken, as an upload's is). The bucket walk may delete an object no row names once it is older than document_blob_min_age_seconds, so a late row could name a deleted object. Past the budget, the move deletes its object and reports the original.
    8. It writes a new version of the catalog row naming the object. Its created_at is the read row's plus one millisecond, not the time it lands, so a write of the same bytes after the walk's read outranks it in every merge, and a take-back (by the location only the switching row names) can only remove the move's own version.
    9. It checks the ledger a last time (see below). With no erasure raced, the switch stands only while the latest row names its object (blobs.latest names it); a user's erasure that kept the original is judged by the same rule: a write landing between the re-read and the switch outranks it, and the move is counted as skipped, its object left to the bucket walk once the rows merge. A failed read there is reported as its own unconfirmed switch, naming the row to check. When that read finds no row holding the original (the sweep's age-bound delete took it with the switched row, which carries the original's age), the move deletes its own object at once, since no row can name it again; a sweep delete landing after that read leaves the object to the bucket walk, the same window as the document-deleted race above. An original nothing needs any more by now (its last document went as it moved) keeps its switched row, and the sweep deletes it once it is older than its min age, counted from its last upload since the switch keeps the moved row's age, as any original nothing names. A take-back here would be a delete with no age bound, and after a merge the moved row can be the only row left for a racing writer's original.

    Documents name an original by its sha, so that one row switches every document with those bytes. The ClickHouse copy stays until the ReplacingMergeTree merges the two versions. The upload records (loom.blob_uploads) are keyed by the sha and stay as they are, so a user erasure still finds a moved original.

  • The erasure fence: a move copies bytes an erasure may be deleting, so loom.erasure_requests (read with sequential consistency) fences it.

    • The ledger read is the erasure plane's own: server/erasure/store/ledger.py::unfinished_since, called through server/erasure/app/ledger_reads.py, so the knowledge plane runs no SQL on loom.erasure_requests.
    • An erasure covers an original when it is its namespace's (a namespace scope, or a namespace_list that holds it) or any user's of the org. A user's erasure finds that user's originals through their upload records and their documents, and any write can add to either, so the move does not guess whose an original is. Erasures are rare, and a fenced original moves on a later run. An earlier version read the original's uploaders. It missed authors known only from documents, and then a writer added after its read.
    • The namespace_list parser moves from server/erasure/api to server/erasure/domain/scopes.py, so the knowledge app (originals.py) reads scopes with the erasure plane's own parser; the erasure plane's imports change with it.
    • Step 1 reads the ledger once per namespace (originals.fence), since coverage depends on the namespace alone, and again after any original that was not switched cleanly (an erasure may have held it back, raced its switch or taken it). A refusal ends the namespace with one refused line, not one per original. Each move takes its own start on the ClickHouse clock for steps 6 and 9, so an erasure that ended more than the 60 s margin before its copy began is not taken for one that raced it; one that ended within the margin is reported as having overlapped the copy. The ledger read scans loom.erasure_requests FINAL for the org (keyed by id), measured at 200,000 ledger rows, 6.1 MiB and 6 ms, twice per moved original: small beside the transfer.
    • Step 1 refuses while the ledger holds a covering erasure that is unfinished or finished within the last 60 s (the clock margin between the server's and ClickHouse's clocks). A failed one counts as unfinished: the erasure recovery worker retries it, and one past its attempts finishes through loom erasure-re-verify --resume, as the message says.
    • Steps 1 and 6 cover every erasure registered before the last check. What is left is an erasure registered between step 6 and the row landing, the time of one insert, which may have deleted the original's rows first:
      • A namespace's erasure deletes every original of its namespace, so the move deletes its own row (by the location only it names) and its object at once. Waiting would gain nothing: the erasure's residue census could already have failed on the moved row, and the recovery worker re-runs that check once the row is gone. Only an erasure that certainly deletes the namespace's originals is taken for one: its namespace's, or a list that names it. An unreadable list or a scope this build does not know fences the move, but is waited for and decided by the moved object.
      • A user's erasure deletes every object under the original's prefix when it takes the original (_erase_user_objects), and the move's object is there. Every build that reads bucket rows erases this way: prefix deletion arrived with the bucket store itself (Document originals can be kept in an S3-compatible bucket #167). So the move waits for the erasure to finish, up to 600 s. If the object is gone (listed, not downloaded), the erasure took the original and the move deletes its row. If the object and its row are still there, the erasure kept the original and the switch stands. If only the row went, the move deletes the object: a user's erasure on a replica without the bucket set that read the scope's rows before the switch landed deletes rows only. The wait covers only the erasures that raced the switch; one registered later reads the row.
      • Precondition: the fence counts on every replica running with LOOM_DOCUMENT_OBJECT_STORE=s3, as the rollout already requires. A bucket-less replica's user erasure can also keep a moved row it never saw, which no check can tell from an erasure that kept the original; the user-scope census has no document_blobs check to catch it. The docs say so.
      • A user's erasure that has failed is waited for like any, since the recovery worker retries it.
      • A user's erasure that has not finished in time leaves the switched row to it, and the command names it and exits 1. So does a failed read in these checks after the row is switched, or a failed insert whose row may have landed (listed, counted as neither moved nor skipped), and the line says what to check. An erased original whose take-back failed is listed too, and counted as neither moved nor skipped. A failed delete of the switched row names the row to delete; a failed object delete while taking a row back is only logged, as no row names that object and the bucket walk takes it.
      • When both kinds raced, the namespace's rule applies.
    • The live tests caught a hazard in an earlier version of this fence: deleting the moved row after any racing erasure lost the original once the table had merged the two rows. A switching row stamped with the time it landed had the same hazard on a namespace erasure's take-back, when the same bytes were uploaded again between the erasure's step and the switch; the version in step 8 closes it.
  • knowledge.app.originals.migrate_originals reads the namespace's fence (the walk alone does; move_to_bucket does not), pages through the walk and moves each original. An original that fails is reported in errors; a namespace refused for the rest of the walk (by an erasure, or because its object keys would be too long for objects.MAX_KEY, checked before any read) is reported once in refused, and one switched but unconfirmed (left to an erasure, or a failed check) in unconfirmed, printed as switched, unconfirmed:; the walk goes on. A ClickHouse error stops the run, and the next run goes on from what is still held. A dry run counts the originals and their bytes, and reports each namespace a run would refuse (an erasure's fence, or object keys too long): it reads the ledger as a run does.

  • loom knowledge migrate-originals [--org] [--namespace] [--dry-run] refuses to move anything unless LOOM_DOCUMENT_OBJECT_STORE=s3 (a dry run, which only reads ClickHouse, sizes the move before the store is switched), which should be set on every replica first so no new original lands in ClickHouse behind the walk. It exits 1 when an original was not moved; a dry run exits 1 when it lists a namespace a run would refuse. It is idempotent, so running it again picks up after a stop or a failure. After a move it prints that the ClickHouse copies stay until a merge, and names OPTIMIZE TABLE loom.document_blobs FINAL.

  • Docs: "Where originals are stored" in docs/features/knowledge.md, a pointer from the originals step in docs/features/memory/retention-and-erasure.md, and the regenerated CLI reference.

Alternatives considered:

  • An INSERT … SELECT that rewrites the rows in one statement cannot write the objects. The bytes have to leave ClickHouse through the client either way.

  • A background worker that migrates on its own. The move is a one-off operator step that spends bucket transfers, so a command the operator runs and watches fits it better, and it records its own result.

  • Deleting the ClickHouse row's data with a mutation after the switch. The new row version supersedes it, and a merge (or OPTIMIZE … FINAL, which the operator chooses when to pay for) drops the old bytes without a mutation.

  • Taking back a move whose original stopped being needed as it moved. The delete would have no age bound, so the sweep's own bound handles it instead.

  • Undoing a move after any racing erasure, which was this PR's first version. It can drop an original a user's erasure kept for another writer once the rows have merged.

  • Naming a user erasure that raced the switch to "run again". A completed saga does not run again, and a new one finds nothing, since the first deleted the user's records and documents. The moved object's survival decides the case instead.

  • Broad excepts: four handlers (around the switching insert, the checks in _settle, the latest-row read in _stands, and the take-back) turn any failure after the object is written into a reported SwitchUnconfirmed, raised from the cause. Each carries its reason as a comment rather than # noqa: BLE001: ruff reports that noqa as unused (RUF100) on a handler that re-raises.

  • Reporting: the summary counts originals moved, skipped, left to the sweep, not moved, switched but unconfirmed, and namespaces refused (by an erasure, or because their object keys would be too long). A switched row is not walked again, so each unconfirmed switch is logged as it is caught, and the CLI prints everything the walk gathered even when a ClickHouse error stops the run.

Scope and risk

  • Operator command only; no op, setting, table or worker changes.
  • The command reads one original at a time, whole, into memory (an upload is at most LOOM_KNOWLEDGE_UPLOAD_MAX_BYTES), and writes through the process's bucket transfer slots.

Compatibility, rollout and rollback

  • Rolling upgrade: the rows it writes are the ones LOOM_DOCUMENT_OBJECT_STORE=s3 writes. Run it only once every replica runs a build that reads bucket rows, which is the same condition as switching the store (upgrades).
  • Rollback: a moved original stays in the bucket and downloads from there. No command moves it back.
  • Backups: once moved and merged, a ClickHouse backup or dump holds no bytes of those originals; the bucket needs its own backup, as docs/operations/backup-and-recovery.md describes, and the knowledge page now says so.

Tests

  • Integration, tests/integration/test_document_object_store_live.py (live ClickHouse and an S3-compatible store):
    • The move:
      • two originals written on the ClickHouse store, one with an uploader, plus a row whose bytes do not hash to its sha;
      • a dry run counts all three and their bytes and writes nothing;
      • the move switches the two good ones: each row names an object under its original's prefix, the object hashes to the sha, and the original reads back byte for byte;
      • the bad row is refused, reported and left in ClickHouse;
      • the uploader's record stays;
      • a second run moves nothing and reports the bad row again.
    • Refused bytes: bytes that do not fill their size are refused and reported, and nothing is written.
    • Races:
      • a row uploaded again on the bucket store after the walk read it is kept as the upload left it, and an original no row holds has nothing to move;
      • a row deleted after the object is written is not switched, and the move deletes its object;
      • the same bytes uploaded again after the walk's read, on the bucket store or on the ClickHouse store (a later version of a held row), are not switched, and the move deletes its object (the ClickHouse case fails with the re-read checking the location alone);
      • the same bytes uploaded again on the ClickHouse store between the re-read and the switching insert outrank the switch, and the move is not counted, its object left in the bucket for the bucket walk (the test fails with the switch taken to stand);
      • an object deleted before its read-back, with no erasure behind it, is an error, and the original stays;
      • a copy past the upload budget is not switched, and its object is deleted.
    • Needed originals: one named only as a document's extraction artifact, and one held only by an unexpired upload, both move; one no document names is counted, left to the sweep and not moved, in a dry run as in a move; one whose document goes as its row lands keeps its switch, and the sweep's unreferenced lists it under a 1 s age bound at once, since the switch keeps the original's age.
    • The fence:
      • erasures of another namespace and of a list of others let the move through, and then an unfinished erasure of the namespace holds the next original back, named by its id;
      • an unfinished erasure of any user of the org holds the move back, and so does one that finished just now; a failed one holds it with advice naming the recovery worker and loom erasure-re-verify --resume;
      • an erasure registered during the copy ends the move before its row and its object is deleted; an original that erasure kept is reported and stays in ClickHouse;
      • a namespace's erasure registered as the row lands, alone or with a user's, takes the move back at once;
      • a failed user erasure registered as the row lands is waited for, and past the wait the switched row is left to it, named;
      • the race tests run the erasure's own blob steps (erase_blobs_by_user, delete_document_blobs_by_namespace) after the move's last ledger read and before its switching insert, so the switched row lands alone and the take-back is what removes it (the tests fail with the take-back made a no-op);
      • a namespace's erasure whose step runs and the same bytes are uploaded again before the switching insert, with the table merged before the take-back: the re-upload's row and object survive (the test fails with the switch stamped now);
      • a user's erasure registered as the row lands is waited for: one that took the original takes the move back, one that kept it (a second writer's live document names the bytes) leaves the switch, one that took the row alone leaves no object, and one that does not finish in time is named and leaves the switched row to it.
    • The walk: pages advance on every page, and they find every held original both before the table merges and after OPTIMIZE TABLE … FINAL.
    • A failed check after the switch: it raises SwitchUnconfirmed and the switched row stands (the unit walk test checks that such an original is counted as moved, listed as unconfirmed, and the walk goes on).
    • The file passes twice in a row (26 tests).
  • Unit, tests/unit/test_blob_move_fence.py: which erasures fence a move: its namespace's; a namespace list naming it (a bare array and stripped names included; an unreadable one counts); any user's; and not another namespace's.
  • Unit, tests/unit/test_migrate_originals_walk.py: that the walk follows each page's cursor and stops on a short page; how the walk counts each original of a page (left to the sweep, moved, skipped, an error, or switched but unconfirmed); that a refused cleanup delete leaves the move's outcome as it is; that an erasure holding one back makes it read the fence again and a refusal then ends the namespace in one line; that a namespace refused up front is reported once and not walked; that a namespace whose object keys would be too long is refused before any read, in a run and in a dry run; that an erased original whose take-back failed is not counted as moved; and that the bytes are read with sequential consistency, as the checks after them are, so a lagging replica does not make a sound original read as corrupt; that missing bytes are corrupt only while the row the walk read is still the latest (a newer row, or none, skips the original); that a switch the sweep took, row and all, deletes its object at once; that a switch a later write outranked does not stand, with a failed read of the latest row reported on its own terms.
  • Unit, tests/unit/test_knowledge_migrate_originals_cli.py: the command refuses to move while new originals go to ClickHouse and refuses --namespace without --org, both before connecting; a dry run sizes the move before the store is switched, and exits 1 when it lists a refused namespace; and every unconfirmed switch is printed when an error stops the run.
  • CLI snapshots and the CLI reference are regenerated.
  • The full unit suite on the head rebased onto main (-n 8): 27,594 passed. The 4 failures are the four parametrized cases of test_the_delimiter_is_sniffed_in_bounded_time, a CSV timing bound that fails the same way on main on this loaded host (checked on a detached main checkout); this diff does not touch CSV extraction.
  • make format lint typecheck pass, and so do 2,626 unit tests for package contracts, public copy, doc links, CLI, blobs, object store, RMT write paths, mutations, erasure, alias shadowing and tunable constants. The full unit suite runs on the rebased head before the merge.

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. Checked: the move's order against how an upload writes an original (object first, then row), the race cases in the live test, and a review at every push.

  • 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 (apart from the known timing cases above); the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. (Not applicable: no migration.)
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version. (Not applicable: no plugin file changes.)
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ 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; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

@zlareb1
zlareb1 force-pushed the feat/knowledge-migrate-originals branch 2 times, most recently from 778f97e to b9ea889 Compare November 6, 2026 14:25
zlareb1 and others added 28 commits November 6, 2026 18:46
…se to the bucket

Each original whose latest catalog row holds its bytes is checked against its
sha256, written to its own object with the store's SHA-256 check, read back and
hashed, and then switched by a new catalog row naming the object.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… and refuse short bytes

A move does not start under an unfinished erasure of its namespace or an
uploader, ends before its row when one registers during the copy, and waits out
a namespace erasure that registers as the row lands; a user erasure there is
named to run again. Bytes that do not fill their size are refused, an object
gone before its read-back is not switched, and each page of the walk reads its
own key range. The help and docs say the ClickHouse copy stays until a merge.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… an erasure kept

A user erasure is matched by upload records and by the writers of documents that
name the original. An original an erasure kept during the copy is reported, not
counted as gone; a switch under an uploader's erasure is listed to run that
erasure again; the move deletes an object no row will name; the messages name
the erasure; an empty page is told by its row count. parse_namespace_list moves
to server/erasure/domain so the knowledge store can read the ledger's scopes.

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

A namespace erasure there takes the move back at once; a user erasure is waited
for and decided by whether the moved object survived it, the erasure deleting
every object of an original it takes. Uploaders are read once per page, the row
is written within the upload budget of the copy, an object gone before its
read-back is an error, and the ledger query no longer shadows status.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…witch is reported

Whose an original is can change with any write, so a move no longer reads its
uploaders: every unfinished, or just finished, user erasure of the org holds it
back. A failure after the row is switched lands in left_to_erasure rather than
stopping the walk; the clock is _cutoff_ms's; the docs name the bucket walk's
min-age setting.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ed erasure's fix

An original no live document, upload or ingestion names is counted and left
to the sweep, so a move cannot re-create one the sweep just took. A failed
erasure's fence points at loom erasure-re-verify --resume; a namespace erasure
that overtook the copy counts as erased; only the reads after the switch are
caught, and a failed object delete when taking a row back says what is left.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…, not a stop

An erasure that started during the copy is reported as started, one that just
finished asks for a minute, and a switch it could not confirm is listed as
unconfirmed. A key too long for the bucket is a per-original error, so the walk
goes on. Originals nothing needs are counted as left to the sweep after its
min age; _settle reads in one pass; the docs are reflowed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A failed erasure, else an unfinished one, else one that just finished names the
refusal, at the start and after the copy alike; the help lists both reasons a
switched original exits 1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ce once per page

After its row lands, a move whose original nothing needs any more is taken
back, so it cannot outlive the upload records the sweep took. A failed erasure
that raced the switch is not waited out. The upload budget starts with the
write, as an upload's does. The clock and the first ledger read are one per page
(blobs.fence), coverage depending on the namespace alone.

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

Taking it back deleted a row with no age bound; after a merge that row can be
the only one left for a racing writer's original. The sweep's own age bound
takes it instead. A namespace erasure that overtook the copy while the original
is still held is reported, an erasure that held one back makes the walk read the
fence again for the rest of the page, and a failed take-back is reported.

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

The walk reads the fence per namespace and, after an erasure held an original
back or raced its switch, again; a refusal ends the namespace with one line.
Each move takes its own start for the ledger checks, so an erasure that ended
before its copy is not reported as racing it. A failed take-back names the row
to delete, and a failed object delete there is left to the bucket walk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e fence is a string

The advice names the worker before the re-verify, an erasure that finished just
now reads right in either phrase, and the docs say a namespace is not started
under an open erasure while one registered later ends a move after its copy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…arted in it

move_to_bucket takes fenced=True from the walk instead of a refusal string, the
wait constant is private like its siblings, the module and walk docstrings name
the needed-only rule and the namespace-wide refusal, and a test loop reads
plainly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The fenced switch had one caller, which always set it. The live tests that
checked a refusal now go through the walk and its one error line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A namespace erasure that took a move back, or one that took the original during
its copy, came back as a plain skip, so the next original was copied before the
fence stopped it. A raced user erasure's outcome is read by listing the moved
object, not downloading it; the ClickHouse clock read has its own name; the test
stubs and phrases match the code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…es its namespace

An unreadable list or an unknown scope fences a move but is waited for and
decided by the moved object, not guessed. A failed insert is reported as an
unconfirmed switch, and an erased original whose take-back failed counts as
skipped, not moved.

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

When a user erasure that raced the switch leaves the moved object but no row
names it (a replica without the bucket set deletes rows only), the object goes
and the original counts as erased. _covers and deletes_namespace share one
namespace helper, a mid-namespace refusal speaks of the rest of its originals,
and the walk's comment names every case that reads the fence again.

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

_settle reads its three outcomes in one guarded pass, so a failed row check or
object delete there is reported rather than stopping the walk. A failed insert
of the switching row no longer counts as moved. SwitchUnconfirmed and
Migrated.unconfirmed name what they hold, as the CLI line already did.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…chable branch goes

A row-only take by a user erasure cannot happen: a replica without the bucket
fails its erasure closed. The wait no longer holds a move for an unrelated
erasure registered after the row landed; a failed read-back deletes the object
it wrote; an unconfirmed switch whose row may not stand counts as neither moved
nor skipped; the docs say the bucket then needs its own backup.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…the run stops

A switched row is not walked again, so its line is its only record: it is
logged when caught, and the CLI prints what the walk gathered before an error
leaves it. Refused namespaces and unconfirmed switches have their own counts; a
dry run sizes the move before the store is switched; a failed cleanup after a
failed read-back no longer hides the read's error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ts run the real erasure

A user erasure on a replica without the bucket that read the scope's rows before
the switch landed deletes the row and leaves the object: the move deletes it.
The race tests now run erase_blobs_by_user and delete_document_blobs_by_namespace
themselves, kept by a second writer's document where it should be. The docs name
every replica on s3 as part of the fence; the move's docstring is its contract.

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

The move builds its location before reading the original, and the walk checks
the key length once per namespace, refusing it in one line. The copy-time test
runs the namespace erasure's own blob step.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…each the take-back

The fence, copy, settle and wait move to server/knowledge/app/originals.py; the
knowledge store keeps its statements (the clock, the switching insert, the row
check, the take-back), and the ledger read moves to server/erasure/store/ledger.py
behind server/erasure/app/ledger_reads.py, so no plane runs SQL on another's
table. The race hooks register the erasure and run its blob step before the
switching insert, so the switched row lands alone and the take-back is what
removes it. The refusal test names the covering erasure and shows the others let
the move through; a failed delete on the copy-time path no longer hides the
refusal; the CLI help points at the docs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A refused delete there is logged and left to the bucket walk, so the move's
outcome stands: a replaced row still reads as skipped, a corrupt read-back still
as corrupt. The per-original key-length case is gone, as the walk refuses such a
namespace whole before any read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
blobs.unwrite deletes the row alone and _settle drops the object, as every other
cleanup does. The summary counts refused namespaces whatever refused them, the
ledger docstring names the statuses that exist, and move_to_bucket's client is
typed and its reach stated.

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
zlareb1 and others added 20 commits November 6, 2026 18:46
…bucket walk

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A stop during the wait would otherwise leave no line naming a switched row the
walk never reads again. The write id's length is a named constant, and _one
says it returns False for what an erasure may explain.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A document's extraction artifact and an original an unexpired upload holds
both move. The store's docstring says what the move deletes, a test is named
for what it checks, and _covers reads plainly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A take-back after a merge could delete the only row of a same-bytes
re-upload that landed between an erasure and the switch: the switch,
stamped now, outranked it. The switch now carries the read row's
created_at plus one millisecond, so any later write wins the merge.
A dry run that lists a refused namespace exits 1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The re-read before the switch compares the latest row's version with the
walk's, so a later ClickHouse-store write of the same sha is not counted
as moved. The docs say a moved original keeps its age for the sweep, and
a live test checks it under a 1 s bound. The race tests finish their
erasures only after the switch registered them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The race tests' finisher waits for the switching insert itself, and the
replaced-row test covers a re-upload on the ClickHouse store, which only
the version check catches. The command's doc paragraph is rewrapped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
With no erasure raced, the switch stands only while the latest row names
its object; a write between the re-read and the switching insert
outranks it, and the move counts as skipped, its object left to the
bucket walk once the rows merge.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The check that a later write outranked the switch has its own
unconfirmed message, the settle summary and the docs describe the case,
and the tests pin the object left behind and the settle branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The kept branch judges the switch by the same latest-row rule as the
unraced one, and one store read (blobs.latest, with its location and
version under sequential consistency) serves the re-read and that rule.
The settle tests check an outranked switch deletes nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The two remaining checks after a failed read and an overtaken copy read
the latest row through blobs.latest as the re-read does, and the
docstring names both cases SwitchUnconfirmed covers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A replica behind the walk's could answer the byte read with nothing while
the sequential check after it found the row held, reporting a sound
original as corrupt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
When the byte read finds nothing, the move reports the original corrupt
only while the latest row is still the version the walk read; a newer
one was written since and the move is skipped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
held_in_clickhouse answers after='' when its page reached the
namespace's last key, as its field says, so the walk no longer reads one
empty page per namespace.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/knowledge-migrate-originals branch from 5f438a4 to c2a676c Compare November 6, 2026 18:49
zlareb1 and others added 3 commits November 6, 2026 18:55
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The switched row carries the original's age, so the sweep's age-bound
delete can take it as it lands. With no row left, the move deletes its
own object, which no row can name again, rather than leave bytes no
erasure finds until the bucket walk's age.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit fb8efef into main Nov 6, 2026
28 of 32 checks passed
@zlareb1
zlareb1 deleted the feat/knowledge-migrate-originals branch November 6, 2026 19:08
@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