diff --git a/core/crates/layerfs-history/src/contract/catalog.rs b/core/crates/layerfs-history/src/contract/catalog.rs index 431c5563a7..fee7e3ffda 100644 --- a/core/crates/layerfs-history/src/contract/catalog.rs +++ b/core/crates/layerfs-history/src/contract/catalog.rs @@ -21,8 +21,8 @@ use crate::identity::{BranchId, CatalogId, CommitId, HistoryName, LayerId, Layer use crate::records::{ AddLayerOutcome, AddLayerRequest, BranchMergeRequest, BranchRecord, BranchSnapshot, CommitHistoryRequest, CommitOutcome, CommitRecord, CommitRequest, ForkRequest, - LayerHistoryRequest, LayerRecord, LayerStackRecord, Page, PageResult, PathRemovalOutcome, - PathRemovalRecord, PathRemovalRequest, Reservation, ReserveRequest, StackInitialization, + LayerHistoryRequest, LayerRecord, LayerStackRecord, Page, PageResult, Reservation, + ReserveRequest, StackInitialization, }; /// Checked inputs that establish one catalog binding. #[derive(Clone, Eq, PartialEq)] @@ -332,29 +332,6 @@ pub trait HistoryCatalog: Send + Sync { )) } - /// Applies one `project.remove_file` marker in one write transaction: - /// reads the Store high-water as the new frontier, returns `UpToDate` - /// when the path's marker already covers every project node, otherwise - /// advances the marker and the project epoch. The default refuses. - fn remove_path(&self, _request: &PathRemovalRequest) -> HistoryResult { - Err(crate::error::HistoryError::Unsupported("path removal")) - } - - /// Every `path_removed` marker of one Stack. The default refuses. - fn path_removals(&self, _stack: LayerStackId) -> HistoryResult> { - Err(crate::error::HistoryError::Unsupported( - "path removal markers", - )) - } - - /// The `history_serial` a Commit or Layer records, if it exists. The - /// default refuses. - fn node_history_serial(&self, _node: NodeId) -> HistoryResult> { - Err(crate::error::HistoryError::Unsupported( - "node history position", - )) - } - /// The retained history roots of one Stack. The default refuses. /// /// # Errors diff --git a/core/crates/layerfs-history/src/contract/error.rs b/core/crates/layerfs-history/src/contract/error.rs index e4b686b3d0..d13b476c1d 100644 --- a/core/crates/layerfs-history/src/contract/error.rs +++ b/core/crates/layerfs-history/src/contract/error.rs @@ -147,8 +147,6 @@ pub enum HistoryError { NameTaken(Box), /// A path is hidden by `remove_file`. Removed(Box), - /// A required marker or fence row is missing or contradicts the request. - Conflict(&'static str), /// The Store was replaced or moved under this handle. StaleStore, /// Immediate metadata admission was refused; the caller may retry later. @@ -210,7 +208,6 @@ impl std::fmt::Display for HistoryError { Self::Unsupported(what) => write!(formatter, "unsupported: {what}"), Self::NameTaken(state) => write!(formatter, "name taken: {:?}", state.holder), Self::Removed(state) => write!(formatter, "removed: {} paths", state.paths.len()), - Self::Conflict(what) => write!(formatter, "conflict: {what}"), Self::StaleStore => formatter.write_str("stale Store"), Self::Busy => formatter.write_str("busy"), Self::OwnershipUnavailable => formatter.write_str("ownership unavailable"), diff --git a/core/crates/layerfs-history/src/contract/mod.rs b/core/crates/layerfs-history/src/contract/mod.rs index 520aebbe72..37313bcbc2 100644 --- a/core/crates/layerfs-history/src/contract/mod.rs +++ b/core/crates/layerfs-history/src/contract/mod.rs @@ -2,6 +2,5 @@ pub mod catalog; pub mod error; pub mod identity; -pub mod path; pub mod query; pub mod records; diff --git a/core/crates/layerfs-history/src/contract/path.rs b/core/crates/layerfs-history/src/contract/path.rs deleted file mode 100644 index dc751b0403..0000000000 --- a/core/crates/layerfs-history/src/contract/path.rs +++ /dev/null @@ -1,154 +0,0 @@ -//! The `project.remove_file` path grammar and its keys. -//! -//! The call accepts an absolute POSIX path and stores it as the normalized -//! relative path (`/a` and `a` are the same). A path is refused when it is -//! empty, `/`, `//a`, has an empty, `.` or `..` component, a trailing `/`, a -//! `\` or NUL byte, is longer than 4096 bytes, or has more than 256 -//! components. The stored key is `BLAKE3(normalized path)`; the trace key is -//! keyed by the Store's `trace_salt` over the stack and the normalized path. - -use crate::identity::LayerStackId; - -/// The most bytes one normalized path may have. -pub const MAXIMUM_PATH_BYTES: usize = 4096; -/// The most components one normalized path may have. -pub const MAXIMUM_PATH_COMPONENTS: usize = 256; - -/// A checked, normalized relative path. -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct NormalizedPath { - relative: String, -} - -impl NormalizedPath { - /// Checks `text` and returns its normalized relative path. - /// - /// One leading `/` is stripped; everything after it is the relative path. - /// The returned value never starts or ends with `/` and never holds an - /// empty, `.` or `..` component. - pub fn new(text: &str) -> Result { - if text.is_empty() { - return Err("path is empty"); - } - if text.as_bytes().contains(&0) { - return Err("path contains a NUL byte"); - } - if text.contains('\\') { - return Err("path contains a backslash"); - } - let relative = text.strip_prefix('/').unwrap_or(text); - if relative.is_empty() { - return Err("path is the root"); - } - if relative.starts_with('/') { - return Err("path has an empty component"); - } - let mut components = 0usize; - for component in relative.split('/') { - if component.is_empty() { - return Err("path has an empty component"); - } - if component == "." || component == ".." { - return Err("path has a dot component"); - } - components += 1; - } - if components > MAXIMUM_PATH_COMPONENTS { - return Err("path has too many components"); - } - if relative.len() > MAXIMUM_PATH_BYTES { - return Err("path is too long"); - } - Ok(Self { - relative: relative.to_owned(), - }) - } - - /// The normalized relative path, without a leading `/`. - pub fn as_str(&self) -> &str { - &self.relative - } - - /// The normalized path bytes as stored in the marker. - pub fn as_bytes(&self) -> &[u8] { - self.relative.as_bytes() - } - - /// `BLAKE3(normalized path)`, the marker key. - pub fn key(&self) -> [u8; 32] { - path_key(self.as_bytes()) - } -} - -/// `BLAKE3(path)` over already normalized path bytes, the marker key. -pub fn path_key(path: &[u8]) -> [u8; 32] { - *blake3::hash(path).as_bytes() -} - -/// True when `path` is `marker` or lies below it at a `/` boundary -/// (`p == m OR p begins m ++ b"/"`), compared over bytes, never `||`/`LIKE`. -pub fn governs(marker: &[u8], path: &[u8]) -> bool { - path == marker - || (path.len() > marker.len() && path.starts_with(marker) && path[marker.len()] == b'/') -} - -/// The key a trace hashes for one stack and path: keyed by the Store's -/// 32-byte `trace_salt` over the stack identity and the normalized path. -pub fn trace_key(salt: &[u8; 32], stack: LayerStackId, path: &[u8]) -> [u8; 32] { - let mut hasher = blake3::Hasher::new_keyed(salt); - hasher.update(stack.as_slice()); - hasher.update(path); - *hasher.finalize().as_bytes() -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn accepts_one_leading_slash_and_relative_paths() { - assert_eq!(NormalizedPath::new("/a").unwrap().as_str(), "a"); - assert_eq!(NormalizedPath::new("a").unwrap().as_str(), "a"); - assert_eq!(NormalizedPath::new("/a/b").unwrap().as_str(), "a/b"); - assert_eq!(NormalizedPath::new("a-b_c.d").unwrap().as_str(), "a-b_c.d"); - } - - #[test] - fn refuses_the_bad_grammar() { - for text in [ - "", "/", "//a", "a//b", "a/", "./a", "a/./b", ".", "..", "/./a", "a\\b", "a\0b", - ] { - assert!(NormalizedPath::new(text).is_err(), "accepted {text:?}"); - } - } - - #[test] - fn refuses_long_paths_and_many_components() { - assert!(NormalizedPath::new(&"a".repeat(MAXIMUM_PATH_BYTES)).is_ok()); - assert!(NormalizedPath::new(&"a".repeat(MAXIMUM_PATH_BYTES + 1)).is_err()); - let many = vec!["a"; MAXIMUM_PATH_COMPONENTS].join("/"); - assert!(NormalizedPath::new(&many).is_ok()); - let more = vec!["a"; MAXIMUM_PATH_COMPONENTS + 1].join("/"); - assert!(NormalizedPath::new(&more).is_err()); - } - - #[test] - fn subtree_matches_only_at_a_slash_boundary() { - assert!(governs(b"a", b"a")); - assert!(governs(b"a", b"a/b")); - assert!(!governs(b"a", b"ab")); - assert!(!governs(b"ab", b"a")); - assert!(governs(b"a/b", b"a/b/c")); - assert!(!governs(b"a/b", b"a/bc")); - } - - #[test] - fn keys_are_stable_and_salted_traces_differ() { - let path = NormalizedPath::new("/a/b").unwrap(); - assert_eq!(path.key(), path_key(b"a/b")); - let stack = LayerStackId::from_authority([7; 16]); - let first = trace_key(&[1; 32], stack, path.as_bytes()); - let second = trace_key(&[2; 32], stack, path.as_bytes()); - assert_ne!(first, second); - } -} diff --git a/core/crates/layerfs-history/src/contract/records.rs b/core/crates/layerfs-history/src/contract/records.rs index 394ba3de9f..b840fdbf00 100644 --- a/core/crates/layerfs-history/src/contract/records.rs +++ b/core/crates/layerfs-history/src/contract/records.rs @@ -636,50 +636,3 @@ pub struct InitStatistics { /// Init's wall time in milliseconds, up to the first Branch. pub millis: u64, } - -/// One `path_removed` marker as stored for a project. -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct PathRemovalRecord { - /// The normalized relative path the marker names. - pub path: Vec, - /// The Store high-water when the marker last advanced. - pub frontier_serial: u64, - /// When the marker last advanced, in milliseconds since the epoch. - pub removed_ms: i64, -} - -/// Whether a `project.remove_file` call advanced the marker or found it current. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum PathRemovalKind { - /// The marker advanced to a new frontier. - Removed, - /// No project node is above the marker's frontier; the stored values are returned. - UpToDate, -} - -/// One `project.remove_file` request: the project, the normalized path and the -/// time the caller observed. -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct PathRemovalRequest { - /// Project (LayerStack) the path is removed in. - pub stack: LayerStackId, - /// The normalized relative path bytes; `BLAKE3` keys the marker. - pub path: Vec, - /// When the call was made, in milliseconds since the epoch. - pub removed_ms: i64, -} - -/// One `project.remove_file` result: the stored marker values. -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct PathRemovalOutcome { - /// Whether the call advanced the marker or found it current. - pub kind: PathRemovalKind, - /// Project (LayerStack) the marker belongs to. - pub stack: LayerStackId, - /// The stored normalized relative path. - pub path: Vec, - /// The stored frontier serial. - pub frontier_serial: u64, - /// The stored `removed_ms`. - pub removed_ms: i64, -} diff --git a/core/crates/layerfs-history/src/lib.rs b/core/crates/layerfs-history/src/lib.rs index 6f01165217..0134a0e65d 100644 --- a/core/crates/layerfs-history/src/lib.rs +++ b/core/crates/layerfs-history/src/lib.rs @@ -21,7 +21,6 @@ mod contract; pub use contract::catalog; pub use contract::error; pub use contract::identity; -pub use contract::path; pub use contract::query; pub use contract::records; @@ -35,7 +34,6 @@ pub use records::{ BranchSnapshot, CommitHistoryRequest, CommitMeta, CommitOutcome, CommitRecord, CommitRequest, CommitStatistics, CommitVia, FileDigest, ForkRequest, ForkSource, InitStatistics, LayerHistoryRequest, LayerRecord, LayerStackRecord, MergeCommit, MergeHead, MergeSide, Page, - PageResult, PathRemovalKind, PathRemovalOutcome, PathRemovalRecord, PathRemovalRequest, - RecordKind, Reservation, ReserveRequest, StackInitialization, StoreStatistics, + PageResult, RecordKind, Reservation, ReserveRequest, StackInitialization, StoreStatistics, FILE_DIGEST_WINDOW, MAXIMUM_MESSAGE_BYTES, MAXIMUM_PAGE_RECORDS, }; diff --git a/core/crates/layerfs-history/tests/contract_constructors.rs b/core/crates/layerfs-history/tests/contract_constructors.rs deleted file mode 100644 index 94625590a7..0000000000 --- a/core/crates/layerfs-history/tests/contract_constructors.rs +++ /dev/null @@ -1,70 +0,0 @@ -//! A provider outside this crate builds each refusal context through the -//! public constructors, and readers still reach the public fields. - -use layerfs_history::error::{ - HolderKind, HolderState, NameHolder, NameSource, NameTakenState, RemovedPath, RemovedState, -}; -use layerfs_history::HistoryError; - -#[test] -fn name_taken_context_is_constructed_and_read() { - let holder = NameHolder::new(HolderKind::Branch, "feature", HolderState::Retired); - assert_eq!(holder.kind, HolderKind::Branch); - assert_eq!(holder.name, "feature"); - assert_eq!(holder.state, HolderState::Retired); - - let state = NameTakenState::new(holder, NameSource::Home); - assert_eq!(state.source, NameSource::Home); - assert_eq!(state.holder.name, "feature"); - - let error = HistoryError::NameTaken(Box::new(state)); - match error.cause() { - HistoryError::NameTaken(state) => { - assert_eq!(state.holder.kind, HolderKind::Branch); - assert_eq!(state.holder.state, HolderState::Retired); - assert_eq!(state.source, NameSource::Home); - } - other => panic!("expected NameTaken, got {other:?}"), - } -} - -#[test] -fn removed_context_is_constructed_and_read() { - let path = RemovedPath::new("/gone.txt", 42); - assert_eq!(path.path, "/gone.txt"); - assert_eq!(path.frontier_serial, 42); - - let state = RemovedState::new(vec![path, RemovedPath::new("/other.txt", 7)]); - assert_eq!(state.paths.len(), 2); - assert_eq!(state.paths[1].path, "/other.txt"); - - let error = HistoryError::Removed(Box::new(state)); - match error.cause() { - HistoryError::Removed(state) => { - assert_eq!(state.paths[0].path, "/gone.txt"); - assert_eq!(state.paths[0].frontier_serial, 42); - assert_eq!(state.paths[1].frontier_serial, 7); - } - other => panic!("expected Removed, got {other:?}"), - } -} - -#[test] -fn every_holder_kind_and_state_builds() { - for kind in [HolderKind::Branch, HolderKind::Project] { - for state in [ - HolderState::Live, - HolderState::Retired, - HolderState::Deleted, - ] { - let holder = NameHolder::new(kind, "name", state); - assert_eq!(holder.kind, kind); - assert_eq!(holder.state, state); - } - } - for source in [NameSource::Store, NameSource::Home] { - let holder = NameHolder::new(HolderKind::Project, "project", HolderState::Live); - let state = NameTakenState::new(holder, source); - assert_eq!(state.source, source); - } -} diff --git a/core/docs/issues/project-41/gc-rollout.md b/core/docs/issues/project-41/gc-rollout.md deleted file mode 100644 index 5765c7f3c9..0000000000 --- a/core/docs/issues/project-41/gc-rollout.md +++ /dev/null @@ -1,119 +0,0 @@ -# GC rollout, rollback and dry run (T1.6) - -> Plan for the frozen `LayerFS-GC-spec.md` (2.0.0; the spec wins, `§N` cites it). This page adds no contract and no option: the four calls take only a resource -> identifier, with no confirm, force or dry run (§1), and are admin-profile only, refused under a folder scope (§10). - -## 1. Rollout order - -| Step | Content | Contract | -|---|---|---| -| T1.x | Additive schema: marker/intent tables, position state, `gc_op`/`gc_dead`, `trace_salt` | §4.1 | -| T3/T4/T5 | Removals `branch.remove`, `project.delete`, `project.remove_file`: marker rows only, no bytes freed | §4 | -| T6 | Admission: `gc_intent`, `MountsActive`/`Busy`/`Unresolved`, epoch fence | §5 | -| T7 | Mark: reachability closure at `G`, `none`/`rebuild` from wholly dead packs | §3, §6 | -| T8 | SQLite rebuild: seal, `visited` scratch, verify, rename install | §7 | -| T9 | PostgreSQL rebuild: Phase M mark, Phase A build, Phase B swap, Phase C cleanup | §8 | -| T10/T11 | Report (§11 fields) and API surface | §9–§11 | -| T12 | Acceptance (§12 cases) and measurement (§13) | §12 | - -Order is forced: a rebuild copies T1.x's tables and cleans T3–T5's marker rows. T8/T9 are independent per backend; no step enables `store.compact` by itself. - -## 2. Expand and contract - -- **Expand (T1.x, T3–T5).** The schema step only adds tables and columns and one idempotent backfill under the current schema identity; no column changes type or meaning, - none is dropped, and `history_meta.next_history_serial` is never lowered (§4.1). Removals add marker rows and bump `project_epoch.epoch` (§4, §5). Both are additive: a - Store that never removed anything and never compacts is a plain 2.0 Store. -- **Old readers.** SQLite takes the step as one more `add_*`; PostgreSQL's open check requires the exact table set and recorded definition, so the step rewrites that - definition for older Stores (§4.1). An older PG binary that predates the step refuses the upgraded Store, and the spec defines no down-migration: **downgrade is not - supported**. Roll forward, or restore a pre-upgrade backup. -- **Contract.** Two API contractions are version cuts, not toggles: `project.remove` leaves the call table (an old client gets `InvalidInput`, never a silent hard delete) - and `project.init` returns `NameTaken` instead of `Exists` (§4, §10). Both land with T1.x and are asserted in the error matrix. -- **Flags.** None are added. `store.compact` is opt-in by the caller invoking it: no automatic collection, no online sweep (§1), and the spec defines no global - enable/disable switch. Do not invent one; the only "enable" is the release cut plus §7, and an owner may simply not call it. - -## 3. Dry run - -`store.compact` has no dry-run option (§1). Two non-mutating paths exist and neither is a new call: - -1. **Copy rehearsal.** `backup.create`, `backup.restore` into a scratch Store/home, run `store.compact` against the *scratch* name, compare. §9 names `backup.create` - before compaction as the recovery path for a mark bug; a copy is the closest honest rehearsal. -2. **`none` outcome.** Below 10% wholly dead packs the path is `none`: mark only, nothing written, generation unchanged, `deadBytesRefined == deadBytesBound` (§3, §11). - It cannot be requested, but on a Store expected to be `none` it is a real non-mutating run. - -Compare, before → after (both backends; §11): - -| Observation | `none` | `rebuild` | -|---|---|---| -| `store.status` | no op before/after; phase visible during | same; phases `mark`/`build`/`verify`/`install` (SQLite) or `mark`/`build`/`swap`/`cleanup` (PG) | -| `generation {before, after}` | equal, `G` | `G → G + 1` | -| `deadBytesBound` ≥ `deadBytesRefined` | equal | refined ≤ bound, from the payload sweep | -| `logicalDiscarded` | 0 with a `zeroReason` | collected history `logical` bytes | -| `keptWaste` | measured, unchanged by the rehearsal | `{deadInRetainedPacksBytes, deltaBases, inodeBlocksBytes}` | -| `store_totals` | unchanged | `logical` unchanged; `canonical`/`stored`/`metadata` = old − `dead_*` (§7 step 4, §8 step 9) | -| `residue` | empty | empty after settle; nonzero is explained | - -Also compare `physicalShrink` and `backendReusable` (§11). A rehearsal that disagrees with the live Store is a fixture difference, not a different path: both obey the -same §3 rule. - -## 4. Rollback - -- **Schema is additive.** The T1.x step is the only schema change and re-running it is a no-op; there is no down-migration. Do not un-create the tables by hand: without - `gc_intent`/`gc_op` the Store cannot admit or recover an operation, and PostgreSQL's open check (exact table set, recorded definition) refuses it (§4.1). -- **Markers are not undone by dropping tables.** Dropping `branch_retired`, `layer_stack_retired` or `path_removed` would resurrect names and paths whose history a - rebuild may already have collected (a deleted project's rows are dropped by the rebuild, §7/§8), and §1 promises no undo. The only supported re-creation is a Commit - **after** the frontier that writes the path again: a post-frontier re-creation is visible, and the marker hides only inherited content (§4.3). A Branch or project name - is re-created after a rebuild frees it; the history is gone (§4.2, §12 "Names"). -- **A failed compaction keeps `G`.** A definite failure before the rename aborts and deletes the artifact set - `.compact{,-journal,-wal,-shm,-visited,-visited-journal,-mj*}` (SQLite) or the recorded `lfs_gc_` scratch schema (PostgreSQL); the old Store is unchanged - apart from step 1's checkpoint (§7 step 8, §8, §9). Keep `G` and re-run later. -- **Installed-but-uncertain.** A failure of the rename/fsync itself, or a lost `COMMIT` reply, is not pre-install: keep the new Store at `G + 1`, settle the op `unknown` - and report it; never delete the artifact as if nothing happened (§7 step 8, §8). On PostgreSQL resolve a lost reply by proving the backend ended (`pid`, - `backend_start`, `usename`), then read the incarnation (§8). -- **Artifacts belong to the next `store.compact`.** Only a compaction that names them from its op removes them; a staged name no record names is `Exists` and is not - deleted (§9). Leave crash leftovers alone by hand. - -## 5. Verification gates - -| Phase | Tests | `store.status` / report | Doctor | -|---|---|---|---| -| T1.x | converted 1.0/2.0 Stores open; backfill (`history_serial`, `project_epoch`, `trace_salt`, never-lowered serial); `tools/store_blocks.py` | unchanged, no op row | full, before and after | -| T3–T5 | marker transaction, §4.2 visibility guard, admission races, whiteout on merge/mount/publish | no op left; `Removed`/`NoSuch*` shapes | full | -| T6 | intent lifetime, `machine_stopped`, `Unresolved` ownership, `holders` | `busy`/`unresolved` only while owned | full | -| T7 | closure vs reference walk, `live_heads_digest`, "no live object lost" (§12) | phase `mark`; path chosen | full on fixtures | -| T8 | §7 failpoints (mark/build/verify/install/WAL switch/artifact removal/op settle), exact artifact set, `StaleStore` | both paths' §11 fields; `physicalShrink`, `backendReusable` | `validate` before rename; full after | -| T9 | §8 failpoints (mark/build/swap/cleanup/scratch drop), `ACCESS EXCLUSIVE` duration, standby, reply loss, privileges | PG zeros (`backendReusable = 0`); resume from `gc_op` | full after cleanup | -| T10/T11 | §11 snake_case keys, `zeroReason` vocabulary, capped `cap`, scope refusals (§10) | phase and `op` projection | full | -| T12 | whole §12 matrix; independent `diagnostic/scan.rs` walk; 3 runs per arm (§13) | every field with unit and sampling point | full on fixtures | - -`quick_check` and the full doctor stay separate commands, not part of install (§7 step 6). A gate is green only on both backends, or on the rows §12 assigns. - -## 6. Failure handling - -- **Crash.** The Store is wholly old or wholly new (§9). The next `store.compact` judges the recorded `G`: at `G` the artifact is removed (not installed); at `G + 1` the - outcome is recorded and Phase C resumes. A PG crash in Phase M/A leaves only the scratch schema, which the op names; Phase B is atomic. -- **`Busy`.** A live mount, long-lived client or operation holds the scope. The call does not wait, retry or replay; prepare inactivity and call once later (§1, §5). -- **`Unresolved`.** A dead, uncertain owner's op. A non-owning call observes it; only the owning call (the next `store.compact`) adopts or clears it (§5, §10). An - unreachable machine reports `unknown`, never clean (§9). -- **Lost reply.** The outcome stays uncertain: read `store.status`, the op and the generation before deciding; never replay blind, and never delete a sidecar or an - artifact the op names. -- **Standby.** PostgreSQL waits only when `synchronous_commit` is at least `remote_write` and `synchronous_standby_names` is non-empty; the check runs before Phase M and - again inside the swap. A missing standby abandons with `Busy` (standby list); a standby dying after the check blocks the `COMMIT` unbounded (§8). SQLite has no such - gate. -- **`Capacity` / disk full.** Aborts, deletes the artifact, leaves the old Store unchanged apart from the checkpoint; `Capacity` is the real refusal path, not a codec - bound (§7). -- **`StaleStore`.** A connection that bypassed admission is caught at its next transaction; an in-flight transaction at install is a documented limit (§7, §10). - -## 7. Go/no-go: enabling `store.compact` on a shared Store - -1. `backup.create` succeeds and a `backup.restore` drill of this Store was done (§9). -2. `store.status` is clean: no `holders`, `state` not `busy`/`unresolved`, no live `gc_intent` row, no existing `.compact*` set or `lfs_gc_` schema. -3. All mounts, daemons and long-lived clients of the Store are gone, and no Init/merge row names it (§5); one owner knows nobody may start one during the window. -4. The backend's T8 or T9 gate and the §12 rows touching it are green; the full doctor is clean on a fixture of the same schema layout (§7 step 6, §12). -5. Disk headroom covers peak temp: old Store + new Store + journal/WAL + `visited` (SQLite); old tables + new heap/TOAST + indexes + live-bytes WAL and `pg_wal` headroom - (PG) (§7 cost, §8). -6. PostgreSQL only: `CREATE` on the database, `TEMP`, `USAGE`/`CREATE` on the Store schema, table ownership, `pg_read_all_stats`, else `Unsupported`; a direct or - session-pooled connection (transaction pooling is `Unsupported`); standby replication at the intended level (§5.2, §8). -7. The expected path is known: wholly dead bytes below 10% means `none` and no shrink — decide in advance whether the run is still wanted (§3). -8. The call is made under the MCP admin profile, not a folder scope (§10). -9. Rollback is understood: keep `G`, let only the next `store.compact` remove named artifacts, restore from backup if a mark bug is suspected (§9). -10. One attempt, no replay: a refusal from §6 is inspected before any second call (§1, §9). diff --git a/engine/crates/layerfs-daemon/src/operator/workspace.rs b/engine/crates/layerfs-daemon/src/operator/workspace.rs index 19d5cb8a64..ad7218a6c2 100644 --- a/engine/crates/layerfs-daemon/src/operator/workspace.rs +++ b/engine/crates/layerfs-daemon/src/operator/workspace.rs @@ -155,27 +155,15 @@ fn scan( ) -> Result<()> { let binding = workspace.snapshot().map_err(args::failed)?; let root = binding.effective_root; - let node = binding.branch.head_commit.map_or( - layerfs_history::NodeId::Layer(binding.branch.base_layer), - layerfs_history::NodeId::Commit, - ); - // The base node `history_serial`, as every `Snap` carries it for the - // `remove_file` visibility rule; 0 is the same unknown fallback that - // `snapshot::resolve` takes. - let serial = match service - .store - .published(|history| history.node_history_serial(node)) - { - Ok(Some(serial)) => serial, - _ => 0, - }; let snap = Snap { kind: Kind::Commit, label: "Workspace base".into(), - node, + node: binding.branch.head_commit.map_or( + layerfs_history::NodeId::Layer(binding.branch.base_layer), + layerfs_history::NodeId::Commit, + ), root, tree: TreeTable::open(op.ports(), FilesystemRootId(root)).map_err(args::content)?, - serial, }; let pending = workspace .pending_conflicts() diff --git a/engine/crates/layerfs-persistence/sql/postgresql/queries/history/branch_snapshot.sql b/engine/crates/layerfs-persistence/sql/postgresql/queries/history/branch_snapshot.sql index 8a36f19368..bb079ef25c 100644 --- a/engine/crates/layerfs-persistence/sql/postgresql/queries/history/branch_snapshot.sql +++ b/engine/crates/layerfs-persistence/sql/postgresql/queries/history/branch_snapshot.sql @@ -1 +1 @@ -SELECT b.branch_id, b.layer_stack_id, b.name, b.base_layer_id, b.head_commit_id, b.forked_from_id, s.layer_stack_id, s.name, s.scope_id, s.profile_id, s.head_layer_id, l.root_id, c.commit_id, c.layer_stack_id, c.root_id, c.parent_commit_id, c.base_layer_id, c.second_parent_id, c.branch_id, c.via, c.message, c.merged_from_id, c.created_ms, c.logical, c.new_canonical, c.new_stored, c.new_metadata, c.lww, c.displaced_id FROM {s}.branch b LEFT JOIN {s}.layer_stack s ON s.layer_stack_id = b.layer_stack_id LEFT JOIN {s}.layer l ON l.layer_id = b.base_layer_id LEFT JOIN {s}."commit" c ON c.commit_id = b.head_commit_id WHERE b.branch_id = $1 AND NOT EXISTS (SELECT 1 FROM {s}.branch_retired r WHERE r.branch_id = b.branch_id) AND NOT EXISTS (SELECT 1 FROM {s}.layer_stack_retired d WHERE d.layer_stack_id = b.layer_stack_id); +SELECT b.branch_id, b.layer_stack_id, b.name, b.base_layer_id, b.head_commit_id, b.forked_from_id, s.layer_stack_id, s.name, s.scope_id, s.profile_id, s.head_layer_id, l.root_id, c.commit_id, c.layer_stack_id, c.root_id, c.parent_commit_id, c.base_layer_id, c.second_parent_id, c.branch_id, c.via, c.message, c.merged_from_id, c.created_ms, c.logical, c.new_canonical, c.new_stored, c.new_metadata, c.lww, c.displaced_id FROM {s}.branch b LEFT JOIN {s}.layer_stack s ON s.layer_stack_id = b.layer_stack_id LEFT JOIN {s}.layer l ON l.layer_id = b.base_layer_id LEFT JOIN {s}."commit" c ON c.commit_id = b.head_commit_id WHERE b.branch_id = $1; diff --git a/engine/crates/layerfs-persistence/sql/postgresql/queries/history/branch_snapshot_named.sql b/engine/crates/layerfs-persistence/sql/postgresql/queries/history/branch_snapshot_named.sql index 715134482e..8ff1a3808b 100644 --- a/engine/crates/layerfs-persistence/sql/postgresql/queries/history/branch_snapshot_named.sql +++ b/engine/crates/layerfs-persistence/sql/postgresql/queries/history/branch_snapshot_named.sql @@ -1 +1 @@ -SELECT b.branch_id, b.layer_stack_id, b.name, b.base_layer_id, b.head_commit_id, b.forked_from_id, s.layer_stack_id, s.name, s.scope_id, s.profile_id, s.head_layer_id, l.root_id, c.commit_id, c.layer_stack_id, c.root_id, c.parent_commit_id, c.base_layer_id, c.second_parent_id, c.branch_id, c.via, c.message, c.merged_from_id, c.created_ms, c.logical, c.new_canonical, c.new_stored, c.new_metadata, c.lww, c.displaced_id FROM {s}.layer_stack p LEFT JOIN {s}.branch b ON p.name = $1 AND b.layer_stack_id = p.layer_stack_id AND b.name = $2 AND NOT EXISTS (SELECT 1 FROM {s}.branch_retired r WHERE r.branch_id = b.branch_id) LEFT JOIN {s}.layer_stack s ON s.layer_stack_id = b.layer_stack_id LEFT JOIN {s}.layer l ON l.layer_id = b.base_layer_id LEFT JOIN {s}."commit" c ON c.commit_id = b.head_commit_id WHERE p.name = $1 AND NOT EXISTS (SELECT 1 FROM {s}.layer_stack_retired d WHERE d.layer_stack_id = p.layer_stack_id); +SELECT b.branch_id, b.layer_stack_id, b.name, b.base_layer_id, b.head_commit_id, b.forked_from_id, s.layer_stack_id, s.name, s.scope_id, s.profile_id, s.head_layer_id, l.root_id, c.commit_id, c.layer_stack_id, c.root_id, c.parent_commit_id, c.base_layer_id, c.second_parent_id, c.branch_id, c.via, c.message, c.merged_from_id, c.created_ms, c.logical, c.new_canonical, c.new_stored, c.new_metadata, c.lww, c.displaced_id FROM {s}.layer_stack p LEFT JOIN {s}.branch b ON p.name = $1 AND b.layer_stack_id = p.layer_stack_id AND b.name = $2 LEFT JOIN {s}.layer_stack s ON s.layer_stack_id = b.layer_stack_id LEFT JOIN {s}.layer l ON l.layer_id = b.base_layer_id LEFT JOIN {s}."commit" c ON c.commit_id = b.head_commit_id WHERE p.name = $1; diff --git a/engine/crates/layerfs-persistence/sql/postgresql/schema.sql b/engine/crates/layerfs-persistence/sql/postgresql/schema.sql index 8687c0371c..5b15c1bb69 100644 --- a/engine/crates/layerfs-persistence/sql/postgresql/schema.sql +++ b/engine/crates/layerfs-persistence/sql/postgresql/schema.sql @@ -70,7 +70,6 @@ CREATE TABLE history_meta ( binding_key bytea NOT NULL CHECK (octet_length(binding_key) BETWEEN 1 AND 128), identity_format bigint NOT NULL CHECK (identity_format = 3), schema_version bigint NOT NULL CHECK (schema_version = 2), - next_history_serial bigint NOT NULL DEFAULT 1, schema_source text NOT NULL, schema_definition text NOT NULL ); diff --git a/engine/crates/layerfs-persistence/sql/sqlite/history.sql b/engine/crates/layerfs-persistence/sql/sqlite/history.sql index 7c3ec88f1c..a1f6e85f6a 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/history.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/history.sql @@ -6,7 +6,6 @@ CREATE TABLE history_meta ( binding_key BLOB NOT NULL CHECK (length(binding_key) BETWEEN 1 AND 128), identity_format INTEGER NOT NULL CHECK (identity_format = 3), schema_version INTEGER NOT NULL CHECK(schema_version=1), - next_history_serial INTEGER NOT NULL DEFAULT 1, schema_source TEXT NOT NULL, schema_definition TEXT NOT NULL ) STRICT; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_id.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_id.sql index 1f9cc22f93..8bb8c308fd 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_id.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_id.sql @@ -1 +1 @@ -SELECT branch_id, layer_stack_id, name, base_layer_id, head_commit_id, forked_from_id FROM branch WHERE branch_id = $1 AND NOT EXISTS (SELECT 1 FROM branch_retired r WHERE r.branch_id = branch.branch_id) AND NOT EXISTS (SELECT 1 FROM layer_stack_retired s WHERE s.layer_stack_id = branch.layer_stack_id); +SELECT branch_id, layer_stack_id, name, base_layer_id, head_commit_id, forked_from_id FROM branch WHERE branch_id = $1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_id_any.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_id_any.sql deleted file mode 100644 index 8bb8c308fd..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_id_any.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT branch_id, layer_stack_id, name, base_layer_id, head_commit_id, forked_from_id FROM branch WHERE branch_id = $1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_name.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_name.sql index 0eadc60e31..f32a794a16 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_name.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_by_name.sql @@ -1 +1 @@ -SELECT branch_id, layer_stack_id, name, base_layer_id, head_commit_id, forked_from_id FROM branch WHERE layer_stack_id = $1 AND name = $2 AND NOT EXISTS (SELECT 1 FROM branch_retired r WHERE r.branch_id = branch.branch_id) AND NOT EXISTS (SELECT 1 FROM layer_stack_retired s WHERE s.layer_stack_id = branch.layer_stack_id); +SELECT branch_id, layer_stack_id, name, base_layer_id, head_commit_id, forked_from_id FROM branch WHERE layer_stack_id = $1 AND name = $2; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_first_page.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_first_page.sql index 948b07f14a..b31a7ac3cb 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_first_page.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_first_page.sql @@ -1 +1 @@ -SELECT branch_id, layer_stack_id, name, base_layer_id, head_commit_id, forked_from_id FROM branch WHERE layer_stack_id = $1 AND NOT EXISTS (SELECT 1 FROM branch_retired r WHERE r.branch_id = branch.branch_id) AND NOT EXISTS (SELECT 1 FROM layer_stack_retired s WHERE s.layer_stack_id = branch.layer_stack_id) ORDER BY name LIMIT $2; +SELECT branch_id, layer_stack_id, name, base_layer_id, head_commit_id, forked_from_id FROM branch WHERE layer_stack_id = $1 ORDER BY name LIMIT $2; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_name_taken.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_name_taken.sql index e0f54d1707..66493a918d 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_name_taken.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_name_taken.sql @@ -1 +1 @@ -SELECT b.name, CASE WHEN r.branch_id IS NULL THEN 0 ELSE 1 END FROM branch b LEFT JOIN branch_retired r ON r.branch_id = b.branch_id WHERE b.layer_stack_id = $1 AND b.name = $2; +SELECT 1 FROM branch WHERE layer_stack_id = $1 AND name = $2; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_next_page.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_next_page.sql index c37d2b136e..9530b4f910 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_next_page.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_branch_next_page.sql @@ -1 +1 @@ -SELECT branch_id, layer_stack_id, name, base_layer_id, head_commit_id, forked_from_id FROM branch WHERE layer_stack_id = $1 AND name > $2 AND NOT EXISTS (SELECT 1 FROM branch_retired r WHERE r.branch_id = branch.branch_id) AND NOT EXISTS (SELECT 1 FROM layer_stack_retired s WHERE s.layer_stack_id = branch.layer_stack_id) ORDER BY name LIMIT $3; +SELECT branch_id, layer_stack_id, name, base_layer_id, head_commit_id, forked_from_id FROM branch WHERE layer_stack_id = $1 AND name > $2 ORDER BY name LIMIT $3; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_retire_insert.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_retire_insert.sql deleted file mode 100644 index 700f5dd810..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_retire_insert.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO branch_retired (branch_id, removed_ms) VALUES ($1, $2) ON CONFLICT(branch_id) DO NOTHING RETURNING branch_id; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_snapshot.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_snapshot.sql index 08db9eb02a..146b8b8c6a 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_snapshot.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_snapshot.sql @@ -1 +1 @@ -SELECT b.branch_id, b.layer_stack_id, b.name, b.base_layer_id, b.head_commit_id, b.forked_from_id, s.layer_stack_id, s.name, s.scope_id, s.profile_id, s.head_layer_id, l.root_id, c.commit_id, c.layer_stack_id, c.root_id, c.parent_commit_id, c.base_layer_id, c.second_parent_id, c.branch_id, c.via, c.message, c.merged_from_id, c.created_ms, c.logical, c.new_canonical, c.new_stored, c.new_metadata, c.lww, c.displaced_id FROM branch b LEFT JOIN layer_stack s ON s.layer_stack_id = b.layer_stack_id LEFT JOIN layer l ON l.layer_id = b.base_layer_id LEFT JOIN "commit" c ON c.commit_id = b.head_commit_id WHERE b.branch_id = $1 AND NOT EXISTS (SELECT 1 FROM branch_retired r WHERE r.branch_id = b.branch_id) AND NOT EXISTS (SELECT 1 FROM layer_stack_retired d WHERE d.layer_stack_id = b.layer_stack_id); +SELECT b.branch_id, b.layer_stack_id, b.name, b.base_layer_id, b.head_commit_id, b.forked_from_id, s.layer_stack_id, s.name, s.scope_id, s.profile_id, s.head_layer_id, l.root_id, c.commit_id, c.layer_stack_id, c.root_id, c.parent_commit_id, c.base_layer_id, c.second_parent_id, c.branch_id, c.via, c.message, c.merged_from_id, c.created_ms, c.logical, c.new_canonical, c.new_stored, c.new_metadata, c.lww, c.displaced_id FROM branch b LEFT JOIN layer_stack s ON s.layer_stack_id = b.layer_stack_id LEFT JOIN layer l ON l.layer_id = b.base_layer_id LEFT JOIN "commit" c ON c.commit_id = b.head_commit_id WHERE b.branch_id = $1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_snapshot_named.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_snapshot_named.sql index beb7aa52ee..d44de96eba 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_snapshot_named.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/branch_snapshot_named.sql @@ -1 +1 @@ -SELECT b.branch_id, b.layer_stack_id, b.name, b.base_layer_id, b.head_commit_id, b.forked_from_id, s.layer_stack_id, s.name, s.scope_id, s.profile_id, s.head_layer_id, l.root_id, c.commit_id, c.layer_stack_id, c.root_id, c.parent_commit_id, c.base_layer_id, c.second_parent_id, c.branch_id, c.via, c.message, c.merged_from_id, c.created_ms, c.logical, c.new_canonical, c.new_stored, c.new_metadata, c.lww, c.displaced_id FROM layer_stack p LEFT JOIN branch b ON p.name = $1 AND b.layer_stack_id = p.layer_stack_id AND b.name = $2 AND NOT EXISTS (SELECT 1 FROM branch_retired r WHERE r.branch_id = b.branch_id) LEFT JOIN layer_stack s ON s.layer_stack_id = b.layer_stack_id LEFT JOIN layer l ON l.layer_id = b.base_layer_id LEFT JOIN "commit" c ON c.commit_id = b.head_commit_id WHERE p.name = $1 AND NOT EXISTS (SELECT 1 FROM layer_stack_retired d WHERE d.layer_stack_id = p.layer_stack_id); +SELECT b.branch_id, b.layer_stack_id, b.name, b.base_layer_id, b.head_commit_id, b.forked_from_id, s.layer_stack_id, s.name, s.scope_id, s.profile_id, s.head_layer_id, l.root_id, c.commit_id, c.layer_stack_id, c.root_id, c.parent_commit_id, c.base_layer_id, c.second_parent_id, c.branch_id, c.via, c.message, c.merged_from_id, c.created_ms, c.logical, c.new_canonical, c.new_stored, c.new_metadata, c.lww, c.displaced_id FROM layer_stack p LEFT JOIN branch b ON p.name = $1 AND b.layer_stack_id = p.layer_stack_id AND b.name = $2 LEFT JOIN layer_stack s ON s.layer_stack_id = b.layer_stack_id LEFT JOIN layer l ON l.layer_id = b.base_layer_id LEFT JOIN "commit" c ON c.commit_id = b.head_commit_id WHERE p.name = $1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/commit_insert_commit.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/commit_insert_commit.sql index 9af2640f05..48d2df4768 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/commit_insert_commit.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/commit_insert_commit.sql @@ -1 +1 @@ -INSERT INTO "commit" (commit_id, layer_stack_id, root_id, parent_commit_id, base_layer_id, second_parent_id, branch_id, via, message, merged_from_id, created_ms, logical, new_canonical, new_stored, new_metadata, lww, displaced_id, history_serial) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18) ON CONFLICT (commit_id) DO NOTHING RETURNING 1; +INSERT INTO "commit" (commit_id, layer_stack_id, root_id, parent_commit_id, base_layer_id, second_parent_id, branch_id, via, message, merged_from_id, created_ms, logical, new_canonical, new_stored, new_metadata, lww, displaced_id) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17) ON CONFLICT (commit_id) DO NOTHING RETURNING 1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/layerstack_insert_layer.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/layerstack_insert_layer.sql index 7d908b00d7..c483c0caf3 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/layerstack_insert_layer.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/layerstack_insert_layer.sql @@ -1 +1 @@ -INSERT INTO layer (layer_id,layer_stack_id,parent_layer_id,root_id,source_branch_id,source_commit_id,logical, overwrote, history_serial) VALUES ($1,$2,$3,$4,$5,$6,$7, $8, $9) RETURNING 1; +INSERT INTO layer (layer_id,layer_stack_id,parent_layer_id,root_id,source_branch_id,source_commit_id,logical, overwrote) VALUES ($1,$2,$3,$4,$5,$6,$7, $8) RETURNING 1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/layerstack_stack_name_taken.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/layerstack_stack_name_taken.sql index b1e577c213..875ddc4086 100644 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/layerstack_stack_name_taken.sql +++ b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/layerstack_stack_name_taken.sql @@ -1 +1 @@ -SELECT s.name, CASE WHEN r.layer_stack_id IS NULL THEN 0 ELSE 1 END FROM layer_stack s LEFT JOIN layer_stack_retired r ON r.layer_stack_id = s.layer_stack_id WHERE s.name = $1; +SELECT 1 FROM layer_stack WHERE name = $1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_advance_epoch.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_advance_epoch.sql deleted file mode 100644 index 15fa42d851..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_advance_epoch.sql +++ /dev/null @@ -1 +0,0 @@ -UPDATE project_epoch SET epoch=epoch+1 WHERE layer_stack_id=$1 RETURNING 1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_marker.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_marker.sql deleted file mode 100644 index 049788375c..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_marker.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT path,frontier_serial,removed_ms FROM path_removed WHERE layer_stack_id=$1 AND path_key=$2; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_markers.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_markers.sql deleted file mode 100644 index d3bd1e7c8a..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_markers.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT path,frontier_serial,removed_ms FROM path_removed WHERE layer_stack_id=$1 ORDER BY path; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_upsert.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_upsert.sql deleted file mode 100644 index f9171232e1..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/paths_upsert.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO path_removed(layer_stack_id,path_key,path,frontier_serial,removed_ms) VALUES($1,$2,$3,$4,$5) ON CONFLICT(layer_stack_id,path_key) DO UPDATE SET path=excluded.path, frontier_serial=excluded.frontier_serial, removed_ms=excluded.removed_ms; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_high_water.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_high_water.sql deleted file mode 100644 index c76bf13a1e..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_high_water.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT next_history_serial - 1 FROM history_meta WHERE id=1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_next_serial.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_next_serial.sql deleted file mode 100644 index a7f0c03ba9..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_next_serial.sql +++ /dev/null @@ -1 +0,0 @@ -UPDATE history_meta SET next_history_serial=next_history_serial+1 WHERE id=1 RETURNING next_history_serial-1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_node_serial.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_node_serial.sql deleted file mode 100644 index 5a3ff1e15c..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_node_serial.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT history_serial FROM "commit" WHERE commit_id=$1 UNION ALL SELECT history_serial FROM layer WHERE layer_id=$1 LIMIT 1; diff --git a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_stack_high_water.sql b/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_stack_high_water.sql deleted file mode 100644 index fab8fefa98..0000000000 --- a/engine/crates/layerfs-persistence/sql/sqlite/queries/history/position_stack_high_water.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT COALESCE(MAX(history_serial),0) FROM (SELECT history_serial FROM "commit" WHERE layer_stack_id=$1 UNION ALL SELECT history_serial FROM layer WHERE layer_stack_id=$1) AS serials; diff --git a/engine/crates/layerfs-persistence/src/backend/postgresql/schema.rs b/engine/crates/layerfs-persistence/src/backend/postgresql/schema.rs index fa3115e6ba..3fb0af5afa 100644 --- a/engine/crates/layerfs-persistence/src/backend/postgresql/schema.rs +++ b/engine/crates/layerfs-persistence/src/backend/postgresql/schema.rs @@ -33,9 +33,6 @@ pub(crate) const SCHEMA: &str = concat!( ); /// The Store tables a schema had before the GC tables ([`GC`]) were appended. const PRE_GC_SCHEMA: &str = include_str!("../../../sql/postgresql/schema.sql"); -/// The `history_meta` position column as `schema.sql` writes it; it sits on -/// its own line, so removing the line leaves the text an older Store recorded. -const NEXT_SERIAL: &str = " next_history_serial bigint NOT NULL DEFAULT 1,\n"; /// The GC marker, position and admission tables; a Store written before them /// gains them at open ([`add_gc`]). const GC: &str = include_str!("../../../sql/postgresql/gc.sql"); @@ -130,16 +127,6 @@ fn pre_gc_source(acquisition: bool) -> String { } } -/// The current source before [`NEXT_SERIAL`] was written. -fn source_without_next_serial(acquisition: bool) -> String { - source(acquisition).replacen(NEXT_SERIAL, "", 1) -} - -/// The pre-GC source before [`NEXT_SERIAL`] was written. -fn pre_gc_source_without_next_serial(acquisition: bool) -> String { - pre_gc_source(acquisition).replacen(NEXT_SERIAL, "", 1) -} - /// Creates the new schema `tx.schema` with the Store and acquisition tables, /// its policy row and its catalog row, inside the caller's transaction. A /// schema of that name that already exists is refused before anything is @@ -224,16 +211,10 @@ pub(crate) fn add_gc(session: &Session) -> Result<(), PersistenceError> { return Err(BackendError::Integrity.into()); }; let recorded_source: String = row.get(0)?; - let acquisition = if recorded_source == pre_gc_source(false) - || recorded_source == pre_gc_source_without_next_serial(false) - { - false - } else if recorded_source == pre_gc_source(true) - || recorded_source == pre_gc_source_without_next_serial(true) - { - true - } else { - return Err(BackendError::Integrity.into()); + let acquisition = match (recorded_source == pre_gc_source(false), recorded_source == pre_gc_source(true)) { + (true, _) => false, + (false, true) => true, + (false, false) => return Err(BackendError::Integrity.into()), }; let tables = tx.query(TABLE_NAMES, vec![])?; let actual: Vec = tables.iter().map(|r| r.get(0)).collect::>()?; @@ -258,71 +239,6 @@ pub(crate) fn add_gc(session: &Session) -> Result<(), PersistenceError> { }) } -/// Adds the position column `history_meta.next_history_serial` (T1.4) to a -/// Store written before it, then records the current schema: an additive -/// migration under the same conditions as [`add_gc`]. A schema that already -/// has the column is left alone; a recorded source or definition that is not -/// one of the earlier schemas is refused, never migrated. PostgreSQL has no -/// `commit`/`layer` serial columns yet, so only the column and one -/// `project_epoch` row per project are recorded here; the serial backfill is -/// SQLite's. -pub(crate) fn add_position_state(session: &Session) -> Result<(), PersistenceError> { - // A Store that already has the column is left alone: one statement, no - // transaction and no lane wait. - let present = session.run_as::<_, BackendError>(Mode::Statement, |tx| { - let sql = format!( - "SELECT 1 FROM information_schema.columns WHERE table_schema='{}' AND table_name='history_meta' AND column_name='next_history_serial'", - tx.schema - ); - Ok(!tx.query(&sql, vec![])?.is_empty()) - })?; - if present { - return Ok(()); - } - session.run_as(Mode::Write, |tx| -> Result<(), PersistenceError> { - // Re-check under the lane: another process may have migrated first. - let present = tx.query( - "SELECT 1 FROM information_schema.columns WHERE table_schema=current_schema() AND table_name='history_meta' AND column_name='next_history_serial'", - vec![], - )?; - if !present.is_empty() { - return Ok(()); - } - let rows = tx.query( - "SELECT schema_source,schema_definition FROM history_meta WHERE id=1", - vec![], - )?; - let [row] = rows.as_slice() else { - return Err(BackendError::Integrity.into()); - }; - let recorded_source: String = row.get(0)?; - let acquisition = if recorded_source == source(false) - || recorded_source == source_without_next_serial(false) - { - false - } else if recorded_source == source(true) - || recorded_source == source_without_next_serial(true) - { - true - } else { - return Err(BackendError::Integrity.into()); - }; - if row.get::(1)? != definition(tx)? { - return Err(BackendError::Integrity.into()); - } - tx.batch("ALTER TABLE history_meta ADD COLUMN next_history_serial bigint NOT NULL DEFAULT 1;")?; - tx.batch( - "INSERT INTO project_epoch(layer_stack_id, epoch) SELECT s.layer_stack_id, 0 FROM layer_stack s WHERE NOT EXISTS (SELECT 1 FROM project_epoch e WHERE e.layer_stack_id = s.layer_stack_id);", - )?; - let definition = definition(tx)?; - tx.query( - "UPDATE history_meta SET schema_source=$1,schema_definition=$2 WHERE id=1", - vec![Param::Text(source(acquisition)), Param::Text(definition)], - )?; - Ok(()) - }) -} - /// What an open's check read: the catalog's incarnation, whether the schema /// has the acquisition tables, the recorded binding, the frozen policy and /// the server's version. diff --git a/engine/crates/layerfs-persistence/src/backend/postgresql/schema_diagnostic.rs b/engine/crates/layerfs-persistence/src/backend/postgresql/schema_diagnostic.rs index dc4eb5ea1f..8f4a225948 100644 --- a/engine/crates/layerfs-persistence/src/backend/postgresql/schema_diagnostic.rs +++ b/engine/crates/layerfs-persistence/src/backend/postgresql/schema_diagnostic.rs @@ -89,7 +89,6 @@ history_meta catalog_incarnation bigint NO history_meta binding_key bytea NO history_meta identity_format bigint NO history_meta schema_version bigint NO -history_meta next_history_serial bigint NO history_meta schema_source text NO history_meta schema_definition text NO init_entry operation_id bigint NO diff --git a/engine/crates/layerfs-persistence/src/backend/sqlite/schema.rs b/engine/crates/layerfs-persistence/src/backend/sqlite/schema.rs index 188dcb1a9b..e9ce857540 100644 --- a/engine/crates/layerfs-persistence/src/backend/sqlite/schema.rs +++ b/engine/crates/layerfs-persistence/src/backend/sqlite/schema.rs @@ -64,17 +64,6 @@ pub(crate) fn definition(c: &Connection, w: &RefCell) -> Result &'static str { - let start = HISTORY - .find(" next_history_serial INTEGER NOT NULL DEFAULT 1") - .expect("history schema holds the next serial column"); - let end = start + HISTORY[start..].find('\n').expect("column line ends") + 1; - &HISTORY[start..end] -} - /// The `commit` and `layer` position column as `history.sql` writes it; it /// appears once in each of the two tables. fn history_serial_column() -> &'static str { @@ -94,16 +83,14 @@ fn trace_salt_column() -> &'static str { &METADATA[start..end] } -/// `source` as a Store written before the position columns recorded it: the -/// two `history_serial` columns, `store_policy.trace_salt` and -/// `history_meta.next_history_serial` removed. The table migrations compare -/// against this so a Store from before those columns still matches its -/// recorded source. -fn without_position_columns(source: String) -> String { +/// `source` as a Store written before the three position columns recorded it: +/// the two `history_serial` columns and `store_policy.trace_salt` removed. The +/// table migrations compare against this so a Store from before those columns +/// still matches its recorded source. +fn without_gc_columns(source: String) -> String { source .replacen(history_serial_column(), "", 2) .replacen(trace_salt_column(), "", 1) - .replacen(next_history_serial_column(), "", 1) } /// The ALTERs and salt backfill an old Store needs; existing rows keep serial @@ -127,93 +114,6 @@ fn add_position_columns(tx: &Transaction<'_>) -> Result<(), BackendError> { )?; Ok(()) } - -/// The serials and epochs a Store written before T1.4 needs: Commits take the -/// next Store serials in `(created_ms, commit_id)` order, then each project's -/// genesis Layer takes the next one in stack-id order; every other Layer -/// republishes its source Commit and carries that Commit's serial. Existing -/// serials are kept, `project_epoch` gets one zero row per project and -/// `next_history_serial` never falls below `MAX(serial) + 1`. -fn backfill_position_state(tx: &Transaction<'_>) -> Result<(), BackendError> { - let next: i64 = tx - .query( - "SELECT next_history_serial FROM history_meta WHERE id=1", - vec![], - )? - .first() - .ok_or(BackendError::Integrity)? - .get(0)?; - let commit_max: i64 = tx - .query( - "SELECT COALESCE(MAX(history_serial),0) FROM \"commit\"", - vec![], - )? - .first() - .ok_or(BackendError::Integrity)? - .get(0)?; - let layer_max: i64 = tx - .query("SELECT COALESCE(MAX(history_serial),0) FROM layer", vec![])? - .first() - .ok_or(BackendError::Integrity)? - .get(0)?; - let base = next.max(commit_max.max(layer_max) + 1); - tx.query( - &format!( - "WITH ordered(commit_id,serial) AS ( \ - SELECT commit_id,{base}+ROW_NUMBER() OVER (ORDER BY created_ms,commit_id)-1 \ - FROM \"commit\" WHERE history_serial=0) \ - UPDATE \"commit\" SET history_serial=( \ - SELECT ordered.serial FROM ordered \ - WHERE ordered.commit_id=\"commit\".commit_id) \ - WHERE history_serial=0" - ), - vec![], - )?; - let after_commits: i64 = tx - .query( - "SELECT COALESCE(MAX(history_serial),0)+1 FROM \"commit\"", - vec![], - )? - .first() - .ok_or(BackendError::Integrity)? - .get(0)?; - tx.query( - &format!( - "WITH ordered(layer_id,serial) AS ( \ - SELECT layer_id,{after_commits}+ROW_NUMBER() OVER ( \ - ORDER BY layer_stack_id,layer_id)-1 \ - FROM layer WHERE history_serial=0 AND parent_layer_id IS NULL) \ - UPDATE layer SET history_serial=( \ - SELECT ordered.serial FROM ordered \ - WHERE ordered.layer_id=layer.layer_id) \ - WHERE history_serial=0 AND parent_layer_id IS NULL" - ), - vec![], - )?; - tx.query( - "UPDATE layer SET history_serial=( \ - SELECT \"commit\".history_serial FROM \"commit\" \ - WHERE \"commit\".layer_stack_id=layer.layer_stack_id \ - AND \"commit\".commit_id=layer.source_commit_id) \ - WHERE history_serial=0 AND source_commit_id IS NOT NULL", - vec![], - )?; - tx.query( - "INSERT INTO project_epoch(layer_stack_id,epoch) \ - SELECT s.layer_stack_id,0 FROM layer_stack s \ - WHERE NOT EXISTS ( \ - SELECT 1 FROM project_epoch e WHERE e.layer_stack_id=s.layer_stack_id)", - vec![], - )?; - tx.query( - "UPDATE history_meta SET next_history_serial=MAX(next_history_serial,( \ - SELECT COALESCE(MAX(history_serial),0)+1 FROM ( \ - SELECT history_serial FROM \"commit\" \ - UNION ALL SELECT history_serial FROM layer))) WHERE id=1", - vec![], - )?; - Ok(()) -} pub(crate) fn check( c: &Connection, w: &RefCell, @@ -327,8 +227,8 @@ impl super::session::connection::Session { let Some(row) = rows.first() else { return Ok(()); }; - let current = without_position_columns(source((self.layout, self.acquisition))) - .replacen(GC, "", 1); + let current = + without_gc_columns(source((self.layout, self.acquisition))).replacen(GC, "", 1); if row.get::(0)? != current || row.get::(1)? != definition(tx.connection, tx.work)? { @@ -371,7 +271,7 @@ impl super::session::connection::Session { }; // The schema this migration records is the one before the // lineage and merge tables; later migrations add those. - let current = without_position_columns(source((self.layout, self.acquisition))) + let current = without_gc_columns(source((self.layout, self.acquisition))) .replacen(DIGESTS, "", 1) .replacen(SCRATCH, "", 1) .replacen(LINEAGE, "", 1) @@ -419,7 +319,7 @@ impl super::session::connection::Session { let Some(row) = rows.first() else { return Ok(()); }; - let current = without_position_columns(source((self.layout, self.acquisition))) + let current = without_gc_columns(source((self.layout, self.acquisition))) .replacen(DIGESTS, "", 1) .replacen(SCRATCH, "", 1) .replacen(GC, "", 1); @@ -457,7 +357,7 @@ impl super::session::connection::Session { let Some(row) = rows.first() else { return Ok(()); }; - let current = without_position_columns(source((self.layout, self.acquisition))) + let current = without_gc_columns(source((self.layout, self.acquisition))) .replacen(DIGESTS, "", 1) .replacen(GC, "", 1); if row.get::(0)? != current.replacen(SCRATCH, "", 1) @@ -494,8 +394,8 @@ impl super::session::connection::Session { let Some(row) = rows.first() else { return Ok(()); }; - let current = without_position_columns(source((self.layout, self.acquisition))) - .replacen(GC, "", 1); + let current = + without_gc_columns(source((self.layout, self.acquisition))).replacen(GC, "", 1); if row.get::(0)? != current.replacen(DIGESTS, "", 1) || row.get::(1)? != definition(tx.connection, tx.work)? { @@ -544,50 +444,6 @@ impl super::session::connection::Session { Ok(()) }) } - /// Adds the position state T1.4 introduced (`history_meta.next_history_serial`, - /// appended NOT NULL DEFAULT 1) and backfills the `commit`/`layer` serials - /// and `project_epoch` rows in the same write transaction: an additive - /// migration (REBUILD.md kind 2) under the same recorded-schema gate as - /// [`Self::add_gc_columns`]. `recorded == current` also matches a Store an - /// earlier step of this same open just brought forward, because those steps - /// record the current source before this column is appended. - pub(crate) fn add_position_state(&self) -> Result<(), BackendError> { - self.run::<_, BackendError>(true, |tx| { - let present = tx.query( - "SELECT 1 FROM pragma_table_info('history_meta') WHERE name='next_history_serial'", - vec![], - )?; - if !present.is_empty() { - return Ok(()); - } - let rows = tx.query( - "SELECT schema_source,schema_definition FROM history_meta WHERE id=1", - vec![], - )?; - let Some(row) = rows.first() else { - return Ok(()); - }; - let current = source((self.layout, self.acquisition)); - let before = current.replacen(next_history_serial_column(), "", 1); - let recorded = row.get::(0)?; - if (recorded != before && recorded != current) - || row.get::(1)? != definition(tx.connection, tx.work)? - { - return Ok(()); - } - tx.query( - "ALTER TABLE history_meta ADD COLUMN next_history_serial INTEGER NOT NULL DEFAULT 1", - vec![], - )?; - backfill_position_state(tx)?; - let definition = definition(tx.connection, tx.work)?; - tx.query( - "UPDATE history_meta SET schema_source=?1,schema_definition=?2 WHERE id=1", - vec![Param::Text(current), Param::Text(definition)], - )?; - Ok(()) - }) - } pub(crate) fn validate( &self, catalog_id: layerfs_history::CatalogId, diff --git a/engine/crates/layerfs-persistence/src/backend/sqlite/session/query.rs b/engine/crates/layerfs-persistence/src/backend/sqlite/session/query.rs index f82a0e5f91..5e0f2f7aed 100644 --- a/engine/crates/layerfs-persistence/src/backend/sqlite/session/query.rs +++ b/engine/crates/layerfs-persistence/src/backend/sqlite/session/query.rs @@ -45,12 +45,6 @@ pub(crate) fn history(key: &str) -> Result<&'static str, BackendError> { "branch_branch_by_id" => Ok(include_str!( "../../../../sql/sqlite/queries/history/branch_branch_by_id.sql" )), - "branch_branch_by_id_any" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/branch_branch_by_id_any.sql" - )), - "branch_retire_insert" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/branch_retire_insert.sql" - )), "branch_branch_by_name" => Ok(include_str!( "../../../../sql/sqlite/queries/history/branch_branch_by_name.sql" )), @@ -189,30 +183,6 @@ pub(crate) fn history(key: &str) -> Result<&'static str, BackendError> { "removal_stack" => Ok(include_str!( "../../../../sql/sqlite/queries/history/removal_stack.sql" )), - "position_high_water" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/position_high_water.sql" - )), - "position_stack_high_water" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/position_stack_high_water.sql" - )), - "position_next_serial" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/position_next_serial.sql" - )), - "position_node_serial" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/position_node_serial.sql" - )), - "paths_marker" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/paths_marker.sql" - )), - "paths_markers" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/paths_markers.sql" - )), - "paths_upsert" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/paths_upsert.sql" - )), - "paths_advance_epoch" => Ok(include_str!( - "../../../../sql/sqlite/queries/history/paths_advance_epoch.sql" - )), _ => Err(BackendError::Integrity), } } diff --git a/engine/crates/layerfs-persistence/src/diagnostic/history.rs b/engine/crates/layerfs-persistence/src/diagnostic/history.rs index efa0a74541..f15d464906 100644 --- a/engine/crates/layerfs-persistence/src/diagnostic/history.rs +++ b/engine/crates/layerfs-persistence/src/diagnostic/history.rs @@ -32,7 +32,7 @@ fn inspect( options: Options, observer: &mut dyn FnMut(Observation), ) -> Result<(), BackendError> { - scratch.0.execute_batch("CREATE TABLE nodes(id BLOB PRIMARY KEY,kind INTEGER NOT NULL,stack BLOB NOT NULL,root BLOB NOT NULL,parent BLOB,base BLOB,second BLOB,source BLOB,source_branch BLOB,done INTEGER NOT NULL DEFAULT 0,height INTEGER,depth INTEGER,newest INTEGER,run BLOB,overwrote INTEGER NOT NULL DEFAULT 0,branch BLOB) WITHOUT ROWID; CREATE TABLE edges(child BLOB NOT NULL,parent BLOB NOT NULL,PRIMARY KEY(child,parent)) WITHOUT ROWID; CREATE INDEX parent_edges ON edges(parent,child); CREATE TABLE stacks(id BLOB PRIMARY KEY,scope BLOB NOT NULL,profile BLOB NOT NULL,head BLOB NOT NULL) WITHOUT ROWID; CREATE TABLE branches(id BLOB PRIMARY KEY,stack BLOB NOT NULL,base BLOB NOT NULL,head BLOB,fork BLOB) WITHOUT ROWID; CREATE TABLE contexts(root BLOB,scope BLOB,profile BLOB,PRIMARY KEY(root,scope,profile)) WITHOUT ROWID;").map_err(sql)?; + scratch.0.execute_batch("CREATE TABLE nodes(id BLOB PRIMARY KEY,kind INTEGER NOT NULL,stack BLOB NOT NULL,root BLOB NOT NULL,parent BLOB,base BLOB,second BLOB,source BLOB,source_branch BLOB,done INTEGER NOT NULL DEFAULT 0,height INTEGER,depth INTEGER,newest INTEGER,run BLOB,overwrote INTEGER NOT NULL DEFAULT 0) WITHOUT ROWID; CREATE TABLE edges(child BLOB NOT NULL,parent BLOB NOT NULL,PRIMARY KEY(child,parent)) WITHOUT ROWID; CREATE INDEX parent_edges ON edges(parent,child); CREATE TABLE stacks(id BLOB PRIMARY KEY,scope BLOB NOT NULL,profile BLOB NOT NULL,head BLOB NOT NULL) WITHOUT ROWID; CREATE TABLE branches(id BLOB PRIMARY KEY,stack BLOB NOT NULL,base BLOB NOT NULL,head BLOB,fork BLOB) WITHOUT ROWID; CREATE TABLE contexts(root BLOB,scope BLOB,profile BLOB,PRIMARY KEY(root,scope,profile)) WITHOUT ROWID;").map_err(sql)?; super::facts::run(view, options, observer)?; let mut failures = 0u64; inventory(view, scratch, options, observer, &mut failures)?; @@ -43,7 +43,6 @@ fn inspect( ("store.history.branches","SELECT b.id,b.base FROM branches b LEFT JOIN nodes n ON n.id=b.base LEFT JOIN stacks s ON s.id=b.stack WHERE n.id IS NULL OR n.kind!=2 OR n.stack!=b.stack OR s.id IS NULL","branch base/stack invalid"), ("store.history.branches","SELECT b.id,b.head FROM branches b LEFT JOIN nodes n ON n.id=b.head WHERE b.head IS NOT NULL AND (n.id IS NULL OR n.kind!=1 OR n.stack!=b.stack)","branch head invalid"), ("store.history.branches","SELECT b.id,b.fork FROM branches b LEFT JOIN nodes n ON n.id=b.fork WHERE b.fork IS NOT NULL AND (n.id IS NULL OR n.stack!=b.stack)","branch fork provenance invalid"), - ("store.history.branches","SELECT n.id,n.branch FROM nodes n LEFT JOIN branches b ON b.id=n.branch WHERE n.kind=1 AND n.branch IS NOT NULL AND (b.id IS NULL OR b.stack!=n.stack)","Commit names absent or cross-stack Branch"), ("store.history.provenance","SELECT n.id,n.source FROM nodes n LEFT JOIN nodes c ON c.id=n.source LEFT JOIN branches b ON b.id=n.source_branch WHERE n.kind=2 AND n.parent IS NOT NULL AND (c.id IS NULL OR c.kind!=1 OR c.stack!=n.stack OR c.root!=n.root OR b.id IS NULL OR b.stack!=n.stack)","Layer source Commit/Branch/root mismatch"), ]{ let mut stmt=scratch.0.prepare(query).map_err(sql)?; @@ -143,7 +142,7 @@ fn inventory( 0=>{let s=crate::history::diagnostic_stack(&row).map_err(|e|e.to_string())?;scratch.0.execute("INSERT INTO stacks(id,scope,profile,head) VALUES(?1,?2,?3,?4)",params![s.id.as_slice(),s.scope.as_bytes().as_slice(),s.profile.as_bytes().as_slice(),s.head_layer.as_slice()]).map_err(|_|"diagnostic scratch".to_owned())?;}, 3=>{let b=crate::history::diagnostic_branch(&row).map_err(|e|e.to_string())?;scratch.0.execute("INSERT INTO branches(id,stack,base,head,fork) VALUES(?1,?2,?3,?4,?5)",params![b.id.as_slice(),b.stack.as_slice(),b.base_layer.as_slice(),b.head_commit.map(|v|v.as_slice().to_vec()),b.forked_from.map(|v|v.as_slice().to_vec())]).map_err(|_|"diagnostic scratch".to_owned())?;}, 2=>{let l=crate::history::diagnostic_layer(&row).map_err(|e|e.to_string())?;scratch.0.execute("INSERT INTO nodes(id,kind,stack,root,parent,source,source_branch,overwrote) VALUES(?1,2,?2,?3,?4,?5,?6,?7)",params![l.id.as_slice(),l.stack.as_slice(),l.root.as_bytes().as_slice(),l.parent.map(|v|v.as_slice().to_vec()),l.source_commit.map(|v|v.as_slice().to_vec()),l.source_branch.map(|v|v.as_slice().to_vec()),i64::try_from(l.overwrote_count).map_err(|_|"Layer overwrite count".to_owned())?]).map_err(|_|"diagnostic scratch".to_owned())?;if LayerId::derive(l.stack,l.parent,l.root)!=l.id{return Err("Layer identity mismatch".to_owned());}}, - _=>{let c=crate::history::diagnostic_commit_graph(&row).map_err(|e|e.to_string())?;scratch.0.execute("INSERT INTO nodes(id,kind,stack,root,parent,base,second,branch) VALUES(?1,1,?2,?3,?4,?5,?6,?7)",params![c.id.as_slice(),c.stack.as_slice(),c.root.as_bytes().as_slice(),c.parent.map(|v|v.as_slice().to_vec()),c.base_layer.as_slice(),c.second_parent.map(|v|v.as_slice().to_vec()),c.meta.branch.map(|v|v.as_slice().to_vec())]).map_err(|_|"diagnostic scratch".to_owned())?; + _=>{let c=crate::history::diagnostic_commit_graph(&row).map_err(|e|e.to_string())?;scratch.0.execute("INSERT INTO nodes(id,kind,stack,root,parent,base,second) VALUES(?1,1,?2,?3,?4,?5,?6)",params![c.id.as_slice(),c.stack.as_slice(),c.root.as_bytes().as_slice(),c.parent.map(|v|v.as_slice().to_vec()),c.base_layer.as_slice(),c.second_parent.map(|v|v.as_slice().to_vec())]).map_err(|_|"diagnostic scratch".to_owned())?; for edge in [c.meta.merged_from,c.meta.displaced.map(layerfs_history::NodeId::Commit)].into_iter().flatten(){scratch.0.execute("INSERT OR IGNORE INTO edges VALUES(?1,?2)",params![c.id.as_slice(),edge.as_slice()]).map_err(|_|"diagnostic scratch".to_owned())?;} if CommitId::derive(c.root,c.parent,c.base_layer,c.second_parent,&c.meta)!=c.id{return Err("Commit v3 identity mismatch".to_owned());} } diff --git a/engine/crates/layerfs-persistence/src/history/branch.rs b/engine/crates/layerfs-persistence/src/history/branch.rs index 9f0d64dd17..e174b8cdf2 100644 --- a/engine/crates/layerfs-persistence/src/history/branch.rs +++ b/engine/crates/layerfs-persistence/src/history/branch.rs @@ -7,10 +7,7 @@ use super::params; use super::transaction::Transaction; -use layerfs_history::error::{ - HistoryError, HistoryResult, HolderKind, HolderState, Missing, NameHolder, NameSource, - NameTakenState, -}; +use layerfs_history::error::{HistoryError, HistoryResult, Missing}; use layerfs_history::identity::{BranchId, CatalogId, HistoryName, LayerStackId}; use layerfs_history::records::{ BranchRecord, BranchSnapshot, ForkRequest, ForkSource, Page, PageResult, @@ -18,17 +15,13 @@ use layerfs_history::records::{ use layerfs_history::NamedSnapshot; use super::query::{self, Context, Cursor, Range}; -use super::rows::{branch_row, cell, commit_row, many, one, sql, stack_row}; +use super::rows::{branch_row, commit_row, many, one, sql, stack_row}; use crate::backend::records::{Record as Row, Value}; use layerfs_content::ObjectId; use layerfs_history::records::{CommitRecord, LayerStackRecord}; const BRANCH_BY_ID: &str = "branch_branch_by_id"; -/// The same row without the visibility filter: fork provenance and rendering -/// keep a retired Branch's name and stack without resolving it for a caller. -const BRANCH_BY_ID_ANY: &str = "branch_branch_by_id_any"; - const BRANCH_BY_NAME: &str = "branch_branch_by_name"; const BRANCH_FIRST_PAGE: &str = "branch_branch_first_page"; @@ -44,55 +37,6 @@ pub(crate) fn branch(tx: &Transaction<'_>, id: BranchId) -> HistoryResult