Skip to content

Commit capsules to a journal and make sled its index - #129

Merged
saworbit merged 4 commits into
mainfrom
format/capsule-journal
Nov 12, 2026
Merged

saworbit merged 4 commits into
mainfrom
format/capsule-journal

Conversation

@saworbit

Copy link
Copy Markdown
Owner

Description

Part of #97.

Stacked on #128, which is stacked on #127. Merge those first. Until then this diff also shows their commits; this PR's change is ae9eb03.

This implements the capsule-journal half of on-disk format 1 (spec §5–§7; PR 2 in §14). Two problems motivate it:

  • Capsule records lived only in sled, so losing space.db lost the capsule list.

  • A write that crashed after storing a segment left that segment's hash in the content index. Once compaction removed the segment, a later write could dedup against a hash pointing at nothing.

  • Journal record codecs (crates/common/src/format.rs):

    • CAPSULE manifests carry the capsule's segment entries (plain length and content hash each), a BLAKE3 digest of the capsule, dedupe and requires-encryption flags, and the write-time policy.
    • DROP records a delete; BATCH groups records that apply together.
    • Capsule gains a digest field.
  • Shared scanner (crates/nvram-sim/src/record_file.rs): the open, scan and torn-tail logic from Store segments as framed records and rebuild the log by scanning #128, now used by both the data log and the journal.

  • The journal (crates/capsule-registry/src/journal.rs): space.journal sits next to space.db. A write commits by appending and fsyncing a CAPSULE record, a delete by a DROP.

  • sled is an index of the journal:

    • Each record is applied in one sled transaction together with the journal position.
    • On open, records past that position are replayed, and a missing or mismatched index is rebuilt from the journal.
    • A space.db written before the journal existed is refused.
    • sled now keeps per-segment reference counts, plus which segment each indexed hash belongs to, so a DROP can remove a hash when its last reference goes.
  • Pending index: new segments' hashes wait in memory, where concurrent writes can dedup against them. They reach the durable content index only when their capsule commits.

  • Pipeline:

    • Each write builds the manifest and digest as it goes.
    • A delete commits before releasing segments.
    • read_capsule checks the digest.
  • Store pairing: opening a pipeline pairs the journal with the data log's store id. A new journal adopts it; a journal with capsules and a mismatched id is refused. The segment allocator is kept above every id in the log, and the log's in-memory ref counts are set from sled.

What a reviewer should know

  • S3 buckets and keys still live only in sled. Losing space.db loses them until they move onto the journal (PR 3).
  • The sled index's encoding changed: capsules now carry a digest, so the pinned-encoding test is updated. The journal position now records an index version, so a future encoding change rebuilds the index instead of needing a migration.
  • The commit lock is held across the sled apply, so sled applies records in journal order. If the apply fails after the fsync, the write is still committed and the next open replays it, but the call returns an error.
  • Where the code differs from the spec (§14 lists these too):
    • The segment allocator stays a sled counter, raised on open, not derived in memory.
    • A damaged journal is refused rather than opened read-only.
    • Only read_capsule checks the digest; streaming and range reads, scrub and the S3 ETag don't yet.
    • BATCH is implemented but nothing writes one yet.
  • Two integration tests moved to unique temp folders. They used fixed paths in the crate folder, and the sled data they left behind was never cleaned up between runs; the new store-pairing check rightly refused it.
  • Removed: the registry methods that bypassed the pipeline (put_capsule, create_capsule_with_segments, add_segment, add_deduped_bytes), and the capsule and content operations in MetadataOp. Nothing outside the registry used them.

Tests added:

  • Journal codec round trips and layouts, a size-mismatch rejection, and a check that BATCH records can't nest.
  • Registry level, in crates/capsule-registry/tests/journal_test.rs:
    • deleting space.db rebuilds identical capsules, ref counts and content index;
    • an older space.db catches up by replay;
    • a torn journal tail is dropped;
    • a crashed write leaves no entry in the content index;
    • mismatched store ids are refused and new journals adopt the log's;
    • the digest equals BLAKE3 of the content, including for an empty capsule.

With replay disabled, the replay test fails. In a real spacectl run, deleting space.db and running list/read brought the capsule back from the journal.

Type

  • Bug fix
  • New feature
  • Breaking change
  • Refactor / cleanup
  • Docs / comments only

Checklist

Quality gates

  • cargo fmt --all
  • cargo clippy --all-targets -- -D warnings
  • Tests pass (cargo test --workspace): 316 passed
  • cargo xtask audit (if touching crates, deps, or features): not available in this tree. No dependency changes.
  • Docs compile: RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps

Documentation

  • I updated relevant docs (or no user-facing changes)
  • CHANGELOG.md updated (for features, fixes, breaking changes)

Security

  • No hardcoded secrets, tokens, or keys introduced
  • External inputs are validated (if applicable): journal records are checksummed, size-checked before allocation, and must decode exactly; manifests whose size disagrees with their entries are rejected
  • No new unsafe blocks (or they are justified and documented)

Add docs/on-disk-format.md, a draft spec (not implemented) for making
every durable fact rebuildable from append-only files:

- A 48-byte checksummed frame on every record.
- SEGMENT records that carry their write-once descriptor next to the
  stored bytes, replacing the JSON sidecar.
- A separate capsule journal (CAPSULE manifests with a logical digest,
  DROP, NAMESPACE, BIND, UNBIND, atomic BATCH), with sled demoted to a
  rebuildable index and ref counts derived from live manifests.
- Torn-tail truncation, read-only open on mid-file corruption, and
  compaction by generation swap.

Targets #96, #97 and #108. Link it from docs/README.md as a draft.
Implement the data-log half of on-disk format 1. The NVRAM log no longer
needs a sidecar: losing space.nvram.segments made every capsule
unreadable (#96), and the file was rewritten whole on every append and
ref-count change without an fsync.

- common::format: codec for the 48-byte record frame (BLAKE3 head and
  body checksums), the FILE record and the SEGMENT descriptor, with
  byte layouts defined field by field.
- nvram-sim: the log starts with a FILE record and every append writes
  one framed record per segment. open() scans the log to rebuild the
  segment map, truncates a record cut short at the end, and opens
  read-only when invalid bytes are followed by valid records. Stores
  from before format 1 are refused with a message saying so.
- Ref counts are no longer stored. Opening a registry pipeline recounts
  them from the capsules in space.db, so an interrupted dedup write
  can't leave an inflated count (part of #97). Compaction refuses to
  run until counts have been recounted.
- Deletes and GC now change only the in-memory map; a removed segment's
  record comes back with no references on reopen until compaction
  drops it. integration_test is updated for that.
- Tests: codec layouts and checksums, truncation of the last record at
  every byte offset, mid-log damage, compaction after a recount, and
  registry-level reopen, dedup-crash and torn-append cases.
- Docs: capsule.md, the S3 quick start, dedup and encryption notes,
  SECURITY.md, nvram-sim.md and CHANGELOG describe the new log. The spec
  now records that only the last body is checksummed on open, that an
  all-zero content hash means none, a forward-search limitation, and
  how this stands in until the journal exists.

Breaking: existing space.nvram stores can't be opened.

Closes #96.
Implement the capsule-journal half of on-disk format 1. Capsule records
lived only in sled, so losing space.db lost the capsule list, and a
crashed write could leave a content-index entry for a segment that
compaction later removed.

- common::format: codecs for CAPSULE manifests (segment entries with
  plain length and content hash, BLAKE3 digest, dedupe and
  requires-encryption flags, write-time policy), DROP and BATCH.
  Capsule gains a digest field.
- nvram_sim::record_file: the open, scan and torn-tail logic from the
  data log, now shared with the journal.
- capsule-registry: space.journal next to space.db. A write commits by
  appending and fsyncing a CAPSULE record, a delete by a DROP. Each
  record is applied to sled in one transaction with the journal
  position. On open, records past that position are replayed; a missing
  or mismatched index is rebuilt; a space.db from before the journal is
  refused. sled keeps per-segment reference counts and which segment
  each indexed hash belongs to.
- New segments' hashes wait in an in-memory pending index and reach the
  durable content index only when their capsule commits.
- The pipeline builds the manifest and digest as it writes, commits a
  delete before releasing segments, and read_capsule checks the digest.
- Pipeline opens pair the journal with the data log's store id (a new
  journal adopts it; a mismatch is refused), keep the segment allocator
  above every id in the log, and set the log's ref counts from sled.
- Tests: journal codecs; index rebuilt after deleting space.db; records
  an older index missed replayed; torn journal tail; crashed write
  leaves no index entry; store id mismatch and adoption; digest. Two
  integration tests move from fixed paths in the crate folder to unique
  temp directories.
- Docs: capsule.md, the S3 quick start, dedup notes and CHANGELOG
  describe the journal; the spec records PR 2 as done and where the code
  differs from it.

S3 buckets and keys still live only in sled.

Part of #97.
@saworbit
saworbit merged commit 0795a25 into main Nov 12, 2026
2 checks passed
@saworbit
saworbit deleted the format/capsule-journal branch November 12, 2026 20:48
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