From 28caa9ba0a87e91c73c9aeda2c1f9495a37b1272 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 23:53:19 +0000 Subject: [PATCH 1/2] fix(id): upgrade v8 event stores in place; auto-GC the nix store when disk runs low The event store format went from v8 to v9 on Nov 13 (#7657) when the `bodies` table was added, but nothing upgraded existing stores. Every v8 store was refused as "not an event store: Table 'bodies' does not exist", which crash-looped id-primary on the amd laptop thousands of times. v9 is v8 plus an empty `bodies` table (an event with no body row is valid), so opening a v8 store now adds the table and rewrites the format row in one transaction. Every other format is still refused. Also set nix min-free/max-free: /nix (which holds /home, /var and /tmp under impermanence) filled to 100% with no GC configured, and every stateful service then failed on ENOSPC. The daemon now collects dead store paths during builds when free space drops under 64 GiB. Co-Authored-By: Claude Opus 5.5 --- nixos/nix/default.nix | 9 +++ .../crates/id-events/src/event_store/mod.rs | 68 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/nixos/nix/default.nix b/nixos/nix/default.nix index 52cf610fe4..f507bb4ee1 100644 --- a/nixos/nix/default.nix +++ b/nixos/nix/default.nix @@ -18,6 +18,15 @@ # package = pkgs.nixVersions.nix_2_24; package = pkgs.nixVersions.git; + # Keep the store from filling the disk. On Nov 21, 2026 /nix (which also + # holds /home, /var and /tmp under impermanence) reached 100% with no GC + # configured, and every service that writes state crash-looped on ENOSPC. + # When free space drops under min-free during a build, the daemon collects + # dead store paths until max-free is free. It only removes unreferenced + # paths, never a profile generation or anything a GC root still holds. + settings.min-free = 64 * 1024 * 1024 * 1024; # 64 GiB + settings.max-free = 256 * 1024 * 1024 * 1024; # 256 GiB + # Batch store deduplication, off the critical path. # # This is the GOOD knob. Do not re-enable `auto-optimise-store` in diff --git a/pkgs/id/crates/id-events/src/event_store/mod.rs b/pkgs/id/crates/id-events/src/event_store/mod.rs index 6838a9d3bc..0bc260f8a4 100644 --- a/pkgs/id/crates/id-events/src/event_store/mod.rs +++ b/pkgs/id/crates/id-events/src/event_store/mod.rs @@ -188,6 +188,12 @@ pub const TEMP_NAME: &str = "events.redb.tmp"; /// The format row in `meta`. A store with another format is refused. const FORMAT: &[u8] = b"id.events.store.v9"; const FORMAT_KEY: &[u8] = b"format"; +/// The format before the `bodies` table (#4837). A v8 store is a v9 store +/// with no `bodies` table and nothing else different, and v9 allows an event +/// with no body row. So a v8 store is upgraded in place when it is opened: +/// the empty table is added and the format row rewritten, in one +/// transaction. Every other format is still refused. +const FORMAT_V8: &[u8] = b"id.events.store.v8"; /// A one-time pass over retained event and waiting bodies rebuilds legacy device-op rows. const DEVICE_OPS_RECOVERED_KEY: &[u8] = b"device_ops_recovered"; @@ -447,6 +453,7 @@ impl EventStore { } else { create_fresh(storage)? }; + upgrade_from_v8(&db)?; verify_schema(&db)?; // The additive tables are opened without a format change. Recover device operations // from bodies retained by older sync/import paths where possible. @@ -1268,6 +1275,30 @@ fn initialize(db: &Database) -> Result<(), StoreError> { Ok(()) } +/// Upgrades a format-v8 store to this format in place (see [`FORMAT_V8`]). +/// A store whose format row names anything else is left untouched, for +/// [`verify_schema`] to accept or refuse. +fn upgrade_from_v8(db: &Database) -> Result<(), StoreError> { + let is_v8 = { + let txn = db.begin_read()?; + let Ok(meta) = txn.open_table(META) else { + return Ok(()); + }; + matches!(meta.get(FORMAT_KEY)?, Some(found) if found.value() == FORMAT_V8) + }; + if !is_v8 { + return Ok(()); + } + let txn = db.begin_write()?; + txn.open_table(BODIES)?; + let mut meta = txn.open_table(META)?; + meta.insert(FORMAT_KEY, FORMAT)?; + drop(meta); + txn.commit()?; + tracing::info!("upgraded the event store from format v8 to v9 (added the bodies table)"); + Ok(()) +} + /// Checks that every table exists and that the format row is this store's. fn verify_schema(db: &Database) -> Result<(), StoreError> { let txn = db.begin_read()?; @@ -1818,6 +1849,43 @@ mod tests { assert_eq!(found.value(), b"id.events.store.v2".as_slice()); } + #[test] + fn test_a_v8_store_is_upgraded_in_place_and_keeps_its_data() { + let dir = tempfile::tempdir().unwrap(); + // A v9 store, turned back into v8: no bodies table, the v8 format row. + let store = EventStore::open(dir.path(), WaitingLimits::default(), 0).unwrap(); + drop(store); + let db = Database::builder() + .open(dir.path().join(FILE_NAME)) + .unwrap(); + let txn = db.begin_write().unwrap(); + assert!(txn.delete_table(BODIES).unwrap()); + { + let mut meta = txn.open_table(META).unwrap(); + meta.insert(FORMAT_KEY, FORMAT_V8).unwrap(); + meta.insert(b"marker".as_slice(), b"kept".as_slice()) + .unwrap(); + } + txn.commit().unwrap(); + drop(db); + + let store = EventStore::open(dir.path(), WaitingLimits::default(), 0).unwrap(); + store.check_invariants().unwrap(); + drop(store); + + let db = Database::builder() + .open(dir.path().join(FILE_NAME)) + .unwrap(); + let txn = db.begin_read().unwrap(); + txn.open_table(BODIES).unwrap(); + let meta = txn.open_table(META).unwrap(); + assert_eq!(meta.get(FORMAT_KEY).unwrap().unwrap().value(), FORMAT); + assert_eq!( + meta.get(b"marker".as_slice()).unwrap().unwrap().value(), + b"kept".as_slice() + ); + } + fn block(kind: BlockKind, value: &[u8], expires_at_ms: Option) -> Block { Block { kind, From 6fc638b73157773160f8dacc9b601b511c4806aa Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 00:02:45 +0000 Subject: [PATCH 2/2] fix(id): refuse a v8-labelled store missing v8 tables before upgrading it Checks every v8 table in a read transaction before the upgrade writes, so NotAStore still leaves such a file unchanged. Adds a test for it. Co-Authored-By: Claude Opus 5.5 --- .../crates/id-events/src/event_store/mod.rs | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/pkgs/id/crates/id-events/src/event_store/mod.rs b/pkgs/id/crates/id-events/src/event_store/mod.rs index 0bc260f8a4..b6ed534d06 100644 --- a/pkgs/id/crates/id-events/src/event_store/mod.rs +++ b/pkgs/id/crates/id-events/src/event_store/mod.rs @@ -1289,6 +1289,9 @@ fn upgrade_from_v8(db: &Database) -> Result<(), StoreError> { if !is_v8 { return Ok(()); } + // A v8-labelled file missing a v8 table is not a store: refuse it before writing, so + // NotAStore still leaves the file unchanged. + verify_v8_tables(&db.begin_read()?)?; let txn = db.begin_write()?; txn.open_table(BODIES)?; let mut meta = txn.open_table(META)?; @@ -1299,9 +1302,8 @@ fn upgrade_from_v8(db: &Database) -> Result<(), StoreError> { Ok(()) } -/// Checks that every table exists and that the format row is this store's. -fn verify_schema(db: &Database) -> Result<(), StoreError> { - let txn = db.begin_read()?; +/// Checks that every table of format v8 exists. Format v9 adds only [`BODIES`]. +fn verify_v8_tables(txn: &redb::ReadTransaction) -> Result<(), StoreError> { let missing = |err: redb::TableError| StoreError::NotAStore(err.to_string()); txn.open_table(EVENTS).map_err(missing)?; txn.open_table(HASHES).map_err(missing)?; @@ -1314,6 +1316,14 @@ fn verify_schema(db: &Database) -> Result<(), StoreError> { txn.open_multimap_table(WAITING_BY_DEP).map_err(missing)?; txn.open_table(ACTIONS).map_err(missing)?; txn.open_table(APPLIED).map_err(missing)?; + Ok(()) +} + +/// Checks that every table exists and that the format row is this store's. +fn verify_schema(db: &Database) -> Result<(), StoreError> { + let txn = db.begin_read()?; + let missing = |err: redb::TableError| StoreError::NotAStore(err.to_string()); + verify_v8_tables(&txn)?; txn.open_table(BODIES).map_err(missing)?; let meta = txn.open_table(META).map_err(missing)?; match meta.get(FORMAT_KEY)? { @@ -1886,6 +1896,34 @@ mod tests { ); } + #[test] + fn test_a_v8_label_without_the_v8_tables_is_refused_and_left_at_v8() { + let dir = tempfile::tempdir().unwrap(); + let db = Database::builder() + .create(dir.path().join(FILE_NAME)) + .unwrap(); + let txn = db.begin_write().unwrap(); + txn.open_table(EVENTS).unwrap(); + { + let mut meta = txn.open_table(META).unwrap(); + meta.insert(FORMAT_KEY, FORMAT_V8).unwrap(); + } + txn.commit().unwrap(); + drop(db); + let err = EventStore::open(dir.path(), WaitingLimits::default(), 0).unwrap_err(); + assert!( + matches!(err, StoreError::NotAStore(_)), + "unexpected error: {err}" + ); + let db = Database::builder() + .open(dir.path().join(FILE_NAME)) + .unwrap(); + let txn = db.begin_read().unwrap(); + assert!(txn.open_table(BODIES).is_err(), "no bodies table was added"); + let meta = txn.open_table(META).unwrap(); + assert_eq!(meta.get(FORMAT_KEY).unwrap().unwrap().value(), FORMAT_V8); + } + fn block(kind: BlockKind, value: &[u8], expires_at_ms: Option) -> Block { Block { kind,