diff --git a/atomic-cli/src/commands/diagnostics/metrics.rs b/atomic-cli/src/commands/diagnostics/metrics.rs index 0a960bda..e0025cd0 100644 --- a/atomic-cli/src/commands/diagnostics/metrics.rs +++ b/atomic-cli/src/commands/diagnostics/metrics.rs @@ -2,6 +2,9 @@ use std::path::{Path, PathBuf}; +use atomic_core::pristine::build_metadata::{ + read_version, CREATED_WITH_KEY, LAST_WRITABLE_OPEN_WITH_KEY, MIGRATED_WITH_KEY, +}; use atomic_core::pristine::tables::{ node_type, CHANGE_META, GRAPH, NODE_TYPES, VAULT_ENTRIES, VAULT_MANIFEST, VIEWS, }; @@ -44,11 +47,36 @@ pub(crate) enum RepositoryMetrics { views: Observation, intents: Observation, current_view: Observation, + #[serde(flatten)] + build_versions: Box, tables: Vec, last_compacted: Observation, }, } +#[derive(Debug, Serialize)] +pub(crate) struct BuildVersions { + pub created_with: Observation, + pub migrated_with: Observation, + pub last_writable_open_with: Observation, +} + +fn build_version(txn: &redb::ReadTransaction, key: &str) -> Observation { + match read_version(txn, key) { + Ok(Some(value)) + if !value.is_empty() + && value.len() <= 128 + && value + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b" .()-_+".contains(&b)) => + { + Observation::available(value) + } + Ok(None) => Observation::unavailable("not recorded"), + _ => Observation::unavailable("build metadata unreadable or invalid"), + } +} + #[derive(Debug, Default, Serialize)] pub(crate) struct IntentCounts { pub total: u64, @@ -261,6 +289,11 @@ pub(crate) fn collect(workspace: &Result, SourceError>) -> Repos views: view_count, intents: intents(&txn), current_view: current_view(root), + build_versions: Box::new(BuildVersions { + created_with: build_version(&txn, CREATED_WITH_KEY), + migrated_with: build_version(&txn, MIGRATED_WITH_KEY), + last_writable_open_with: build_version(&txn, LAST_WRITABLE_OPEN_WITH_KEY), + }), tables: vec![ table_metric("change_meta", changes), table_metric("views", views), diff --git a/atomic-cli/src/commands/diagnostics/render.rs b/atomic-cli/src/commands/diagnostics/render.rs index b0fa0d8f..9b5879d5 100644 --- a/atomic-cli/src/commands/diagnostics/render.rs +++ b/atomic-cli/src/commands/diagnostics/render.rs @@ -75,6 +75,7 @@ fn render_repository(o: &mut String, repository: &RepositoryMetrics) { views, intents, current_view, + build_versions, tables, last_compacted, } => { @@ -89,6 +90,21 @@ fn render_repository(o: &mut String, repository: &RepositoryMetrics) { &display_number(registered_changes, ""), ); field(o, "Views", &display_number(views, "")); + for (label, version) in [ + ("Created with", &build_versions.created_with), + ("Last migrated with", &build_versions.migrated_with), + ( + "Last writable initialization open with", + &build_versions.last_writable_open_with, + ), + ] { + match version { + Observation::Available { value } => field(o, label, value), + Observation::Unavailable { reason } => { + field(o, label, &format!("unknown — {reason}")) + } + } + } match current_view { Observation::Available { value } => field( o, diff --git a/atomic-cli/tests/it/diagnostics_integration_test.rs b/atomic-cli/tests/it/diagnostics_integration_test.rs index 2d71f345..c32fa63d 100644 --- a/atomic-cli/tests/it/diagnostics_integration_test.rs +++ b/atomic-cli/tests/it/diagnostics_integration_test.rs @@ -252,14 +252,63 @@ fn repository_metrics_report_real_repository_without_modifying_database() { >= current["membership"].as_u64().unwrap() ); assert_eq!(repo["last_compacted"]["status"], "unavailable"); + assert_eq!(repo["created_with"]["status"], "available", "{repo}"); + assert_eq!( + repo["last_writable_open_with"]["status"], "available", + "{repo}" + ); + let version = fixture.ok(&["--version"]); + assert_eq!( + repo["created_with"]["value"], + version.trim().trim_start_matches("atomic ") + ); + assert_eq!(repo["migrated_with"]["status"], "unavailable", "{repo}"); assert!(repo["database_bytes"]["value"].as_u64().unwrap() > 0); assert_eq!(std::fs::read(&path).unwrap(), before); let text = fixture.ok(&["diagnostics"]); assert!(text.contains("Repository and redb")); + assert!(text.contains("Created with:")); + assert!(text.contains("Last migrated with:")); assert!(text.contains("Last compacted: unknown")); assert_eq!(std::fs::read(path).unwrap(), before); } +#[test] +fn repository_build_metadata_reports_unknown_and_rejects_untrusted_values() { + use atomic_core::pristine::build_metadata::{CREATED_WITH_KEY, LAST_WRITABLE_OPEN_WITH_KEY}; + use atomic_core::pristine::tables::ATOMIC_META; + + let fixture = Fixture::new(); + fixture.ok(&["init", "--no-vault"]); + let path = fixture.work.path().join(".atomic/atomic.redb"); + let database = redb::Database::open(&path).unwrap(); + let write = database.begin_write().unwrap(); + { + let mut meta = write.open_table(ATOMIC_META).unwrap(); + meta.remove(CREATED_WITH_KEY).unwrap(); + meta.insert( + LAST_WRITABLE_OPEN_WITH_KEY, + b"SYNTHETIC_SECRET\nforged: value".as_slice(), + ) + .unwrap(); + } + write.commit().unwrap(); + drop(database); + let before = std::fs::read(&path).unwrap(); + let json = fixture.ok(&["diagnostics", "--json"]); + let report: Value = serde_json::from_str(&json).unwrap(); + assert_eq!( + report["repository"]["created_with"]["reason"], + "not recorded" + ); + assert_eq!( + report["repository"]["last_writable_open_with"]["reason"], + "build metadata unreadable or invalid" + ); + assert!(!json.contains("SYNTHETIC_SECRET")); + assert_eq!(std::fs::read(&path).unwrap(), before); +} + #[test] fn repository_metrics_track_changes_and_do_not_reveal_content() { let fixture = Fixture::new(); diff --git a/atomic-core/build.rs b/atomic-core/build.rs new file mode 100644 index 00000000..6eb38c9f --- /dev/null +++ b/atomic-core/build.rs @@ -0,0 +1,78 @@ +use std::env; +use std::path::{Path, PathBuf}; +use std::process::Command; + +fn git(dir: &Path, args: &[&str]) -> Option { + let output = Command::new("git") + .arg("-C") + .arg(dir) + .args(args) + .output() + .ok()?; + output + .status + .success() + .then(|| String::from_utf8(output.stdout).ok()) + .flatten() +} + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-env-changed=ATOMIC_BUILD_CHANNEL"); + println!("cargo:rerun-if-env-changed=ATOMIC_BUILD_COMMIT"); + let version = env::var("CARGO_PKG_VERSION").expect("Cargo supplies the package version"); + let channel = env::var("ATOMIC_BUILD_CHANNEL").unwrap_or_else(|_| "dev".to_string()); + if channel == "release" { + println!("cargo:rustc-env=ATOMIC_BUILD_VERSION={version}"); + return; + } + assert_eq!( + channel, "dev", + "ATOMIC_BUILD_CHANNEL must be 'dev' or 'release'" + ); + let dir = PathBuf::from( + env::var_os("CARGO_MANIFEST_DIR").expect("Cargo supplies the manifest directory"), + ); + let root = git(&dir, &["rev-parse", "--show-toplevel"]).map(|path| PathBuf::from(path.trim())); + let mut dirty = false; + if let Some(root) = &root { + for name in ["HEAD", "index", "packed-refs", "refs/heads"] { + if let Some(path) = git(root, &["rev-parse", "--git-path", name]) { + let path = root.join(path.trim()); + if path.exists() { + println!("cargo:rerun-if-changed={}", path.display()); + } + } + } + if let Some(files) = git(root, &["ls-files", "-z"]) { + for file in files.split('\0').filter(|file| !file.is_empty()) { + println!("cargo:rerun-if-changed={}", root.join(file).display()); + } + } + dirty = git(root, &["status", "--porcelain", "--untracked-files=no"]) + .is_some_and(|status| !status.trim().is_empty()); + } + let supplied_commit = env::var("ATOMIC_BUILD_COMMIT").ok(); + let commit = supplied_commit + .clone() + .or_else(|| { + root.as_deref() + .and_then(|root| git(root, &["rev-parse", "--verify", "HEAD"])) + }) + .unwrap_or_else(|| "unknown".to_string()); + let commit = commit.trim(); + let commit = if commit.len() >= 12 + && commit.len() <= 64 + && commit.bytes().all(|b| b.is_ascii_hexdigit()) + { + &commit[..12] + } else { + assert!( + supplied_commit.is_none(), + "ATOMIC_BUILD_COMMIT must contain a 12 to 64 character hexadecimal commit" + ); + "unknown" + }; + let dirty = if dirty { "-dirty" } else { "" }; + println!("cargo:rustc-env=ATOMIC_BUILD_VERSION={version} (dev {commit}{dirty})"); +} diff --git a/atomic-core/src/pristine/build_metadata.rs b/atomic-core/src/pristine/build_metadata.rs new file mode 100644 index 00000000..1be084a8 --- /dev/null +++ b/atomic-core/src/pristine/build_metadata.rs @@ -0,0 +1,161 @@ +//! Build identities recorded when a repository database is created or migrated. + +use redb::{ReadableTable, TableError, WriteTransaction}; + +use super::{tables::ATOMIC_META, PristineResult}; + +/// The build that created this database, if recorded at creation time. +pub const CREATED_WITH_KEY: &str = "created_with"; +/// The build that last completed a recorded database migration. +pub const MIGRATED_WITH_KEY: &str = "migrated_with"; +/// The build that last ran the normal writable table-initialization open. +pub const LAST_WRITABLE_OPEN_WITH_KEY: &str = "last_writable_open_with"; + +/// Identity of the build containing the repository implementation. +pub const BUILD_VERSION: &str = env!("ATOMIC_BUILD_VERSION"); + +pub(crate) fn record_open(txn: &WriteTransaction, is_new: bool) -> PristineResult<()> { + let mut meta = txn.open_table(ATOMIC_META)?; + if is_new && meta.get(CREATED_WITH_KEY)?.is_none() { + meta.insert(CREATED_WITH_KEY, BUILD_VERSION.as_bytes())?; + } + meta.insert(LAST_WRITABLE_OPEN_WITH_KEY, BUILD_VERSION.as_bytes())?; + Ok(()) +} + +/// Record a completed migration in the same transaction as its schema changes. +pub fn record_migration(txn: &WriteTransaction) -> PristineResult<()> { + let mut meta = txn.open_table(ATOMIC_META)?; + meta.insert(MIGRATED_WITH_KEY, BUILD_VERSION.as_bytes())?; + Ok(()) +} + +/// Read a recorded build identity without creating metadata in legacy databases. +pub fn read_version(txn: &redb::ReadTransaction, key: &str) -> PristineResult> { + let table = match txn.open_table(ATOMIC_META) { + Ok(table) => table, + Err(TableError::TableDoesNotExist(_)) => return Ok(None), + Err(error) => return Err(error.into()), + }; + let Some(value) = table.get(key)? else { + return Ok(None); + }; + let value = + std::str::from_utf8(value.value()).map_err(|_| super::PristineError::Inconsistent { + message: format!("repository build metadata {key} is not UTF-8"), + })?; + Ok(Some(value.to_owned())) +} + +#[cfg(test)] +mod tests { + use super::*; + use redb::ReadableDatabase; + + #[test] + fn creation_is_immutable_and_writable_open_updates_only_its_field() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("atomic.redb"); + drop(super::super::Pristine::open(&path).unwrap()); + let db = redb::ReadOnlyDatabase::open(&path).unwrap(); + let read = db.begin_read().unwrap(); + assert_eq!( + read_version(&read, CREATED_WITH_KEY).unwrap().as_deref(), + Some(BUILD_VERSION) + ); + assert_eq!( + read_version(&read, LAST_WRITABLE_OPEN_WITH_KEY) + .unwrap() + .as_deref(), + Some(BUILD_VERSION) + ); + drop(read); + drop(db); + let db = redb::Database::open(&path).unwrap(); + let write = db.begin_write().unwrap(); + { + let mut table = write.open_table(ATOMIC_META).unwrap(); + table + .insert(CREATED_WITH_KEY, b"old-creator".as_slice()) + .unwrap(); + table + .insert(LAST_WRITABLE_OPEN_WITH_KEY, b"old-writer".as_slice()) + .unwrap(); + } + write.commit().unwrap(); + drop(db); + + drop(super::super::Pristine::open_readonly(&path).unwrap()); + drop(super::super::Pristine::open_existing(&path).unwrap()); + let db = redb::ReadOnlyDatabase::open(&path).unwrap(); + let read = db.begin_read().unwrap(); + assert_eq!( + read_version(&read, LAST_WRITABLE_OPEN_WITH_KEY) + .unwrap() + .as_deref(), + Some("old-writer") + ); + drop(read); + drop(db); + + drop(super::super::Pristine::open(&path).unwrap()); + let db = redb::ReadOnlyDatabase::open(&path).unwrap(); + let read = db.begin_read().unwrap(); + assert_eq!( + read_version(&read, CREATED_WITH_KEY).unwrap().as_deref(), + Some("old-creator") + ); + assert_eq!( + read_version(&read, LAST_WRITABLE_OPEN_WITH_KEY) + .unwrap() + .as_deref(), + Some(BUILD_VERSION) + ); + assert_eq!(read_version(&read, MIGRATED_WITH_KEY).unwrap(), None); + } + + #[test] + fn legacy_unknown_creator_is_not_invented_and_failed_migration_is_not_recorded() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("atomic.redb"); + drop(super::super::Pristine::open(&path).unwrap()); + let db = redb::Database::open(&path).unwrap(); + let write = db.begin_write().unwrap(); + write + .open_table(ATOMIC_META) + .unwrap() + .remove(CREATED_WITH_KEY) + .unwrap(); + write.commit().unwrap(); + let write = db.begin_write().unwrap(); + record_migration(&write).unwrap(); + write.abort().unwrap(); + drop(db); + drop(super::super::Pristine::open(&path).unwrap()); + let db = redb::ReadOnlyDatabase::open(&path).unwrap(); + let read = db.begin_read().unwrap(); + assert_eq!(read_version(&read, CREATED_WITH_KEY).unwrap(), None); + assert_eq!(read_version(&read, MIGRATED_WITH_KEY).unwrap(), None); + } + + #[test] + fn creating_a_missing_additive_table_records_the_migrating_build() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("atomic.redb"); + drop(super::super::Pristine::open(&path).unwrap()); + let db = redb::Database::open(&path).unwrap(); + let write = db.begin_write().unwrap(); + write + .delete_table(super::super::tables::PENDING_UNTRACKS) + .unwrap(); + write.commit().unwrap(); + drop(db); + drop(super::super::Pristine::open(&path).unwrap()); + let db = redb::ReadOnlyDatabase::open(&path).unwrap(); + let read = db.begin_read().unwrap(); + assert_eq!( + read_version(&read, MIGRATED_WITH_KEY).unwrap().as_deref(), + Some(BUILD_VERSION) + ); + } +} diff --git a/atomic-core/src/pristine/merge.rs b/atomic-core/src/pristine/merge.rs index 138544b3..9da8dfc4 100644 --- a/atomic-core/src/pristine/merge.rs +++ b/atomic-core/src/pristine/merge.rs @@ -153,6 +153,7 @@ impl LegacyDatabases { meta.insert(key.as_str(), digest.as_slice())?; } } + super::build_metadata::record_migration(&write)?; write.commit()?; Ok(report) } diff --git a/atomic-core/src/pristine/mod.rs b/atomic-core/src/pristine/mod.rs index 57b2865c..476d7fa9 100644 --- a/atomic-core/src/pristine/mod.rs +++ b/atomic-core/src/pristine/mod.rs @@ -152,6 +152,7 @@ mod applied_changes; mod attribute; mod bindings; +pub mod build_metadata; mod capability; mod error; pub(crate) mod inode_graph; diff --git a/atomic-core/src/pristine/tables.rs b/atomic-core/src/pristine/tables.rs index 030cea0f..57c4c724 100644 --- a/atomic-core/src/pristine/tables.rs +++ b/atomic-core/src/pristine/tables.rs @@ -874,7 +874,8 @@ pub const EMBEDDINGS: TableDefinition<&str, &[u8]> = TableDefinition::new("embed /// Holds the schema version (`SCHEMA_VERSION_KEY`, little-endian u64) and, for /// databases merged from the legacy `pristine.redb` + `changes.redb` layout, /// the `.atomic/legacy/` directory the old files were moved to -/// (`LEGACY_DIR_KEY`, UTF-8). +/// (`LEGACY_DIR_KEY`, UTF-8). Build identities are UTF-8 values under the +/// keys in [`super::build_metadata`]; absent historical values remain unknown. pub const ATOMIC_META: TableDefinition<&str, &[u8]> = TableDefinition::new("atomic_meta"); // KG Edge Key / FTS Helpers diff --git a/atomic-core/src/pristine/txn/pristine.rs b/atomic-core/src/pristine/txn/pristine.rs index f4487687..b82a6e2e 100644 --- a/atomic-core/src/pristine/txn/pristine.rs +++ b/atomic-core/src/pristine/txn/pristine.rs @@ -283,6 +283,11 @@ impl Pristine { // Initialize all tables. Re-check in the write transaction so a // concurrently raised requirement cannot race additive initialization. let write_txn = db.begin_write()?; + let previous_table_count = if is_new_database { + 0 + } else { + write_txn.list_tables()?.count() + write_txn.list_multimap_tables()?.count() + }; { let metadata = write_txn.open_table(PRISTINE_META)?; let mut requirements = Vec::new(); @@ -398,6 +403,13 @@ impl Pristine { } stamp_schema_version(&write_txn)?; + if !is_new_database + && write_txn.list_tables()?.count() + write_txn.list_multimap_tables()?.count() + > previous_table_count + { + crate::pristine::build_metadata::record_migration(&write_txn)?; + } + crate::pristine::build_metadata::record_open(&write_txn, is_new_database)?; } write_txn.commit()?; diff --git a/atomic-core/tests/merge_test.rs b/atomic-core/tests/merge_test.rs index 0768becc..782f1805 100644 --- a/atomic-core/tests/merge_test.rs +++ b/atomic-core/tests/merge_test.rs @@ -3,6 +3,7 @@ use std::path::Path; use atomic_core::crdt::tables as crdt; +use atomic_core::pristine::build_metadata::{BUILD_VERSION, CREATED_WITH_KEY, MIGRATED_WITH_KEY}; use atomic_core::pristine::merge::{merged_legacy_dir, LegacyDatabases}; use atomic_core::pristine::schema::{LEGACY_DIR_KEY, SCHEMA_VERSION, SCHEMA_VERSION_KEY}; use atomic_core::pristine::tables::{ @@ -140,6 +141,14 @@ fn merge_copies_both_databases_and_records_the_legacy_dir() { meta.get(LEGACY_DIR_KEY).unwrap().unwrap().value(), b"1700000000".as_slice() ); + assert_eq!( + meta.get(CREATED_WITH_KEY).unwrap().unwrap().value(), + BUILD_VERSION.as_bytes() + ); + assert_eq!( + meta.get(MIGRATED_WITH_KEY).unwrap().unwrap().value(), + BUILD_VERSION.as_bytes() + ); let external = txn.open_table(EXTERNAL).unwrap(); assert_eq!(external.len().unwrap(), 1); } diff --git a/atomic-repository/src/repository/migration.rs b/atomic-repository/src/repository/migration.rs index bb775df7..c64fdeac 100644 --- a/atomic-repository/src/repository/migration.rs +++ b/atomic-repository/src/repository/migration.rs @@ -561,6 +561,8 @@ fn commit_migration(path: &Path, plan: MigrationPlan) -> Result<(), RepositoryEr .insert(PATH_CLAIM_SCHEMA_KEY, PATH_CLAIM_SCHEMA_VERSION) .map_err(|error| RepositoryError::Database(error.to_string()))?; } + atomic_core::pristine::build_metadata::record_migration(&write) + .map_err(RepositoryError::from)?; write .commit() .map_err(|error| RepositoryError::Database(error.to_string())) diff --git a/atomic-repository/src/repository/tests/path_claim_migration_tests.rs b/atomic-repository/src/repository/tests/path_claim_migration_tests.rs index a1386cd0..4fe202ed 100644 --- a/atomic-repository/src/repository/tests/path_claim_migration_tests.rs +++ b/atomic-repository/src/repository/tests/path_claim_migration_tests.rs @@ -90,6 +90,17 @@ fn writable_open_atomically_backfills_claims_and_removes_reverse_only_rows() { drop(repo); Repository::open_readonly(temp.path()).unwrap(); + let db = redb::ReadOnlyDatabase::open(&database_path).unwrap(); + let read = redb::ReadableDatabase::begin_read(&db).unwrap(); + assert_eq!( + atomic_core::pristine::build_metadata::read_version( + &read, + atomic_core::pristine::build_metadata::MIGRATED_WITH_KEY + ) + .unwrap() + .as_deref(), + Some(atomic_core::pristine::build_metadata::BUILD_VERSION), + ); } #[test] diff --git a/docs/diagnostics.md b/docs/diagnostics.md index bed8c4fd..bebb1a70 100644 --- a/docs/diagnostics.md +++ b/docs/diagnostics.md @@ -20,6 +20,7 @@ invocation appends to. |---|---| | Atomic version, install source, platform | The running binary (`atomic --version`); install source uses the same detection as `atomic update` | | Workspace | The same upward search other commands use | +| Repository build metadata | Builds recorded at database creation, last completed table initialization, legacy-layout or path-claim migration, and last normal writable table-initialization open | | Agent integrations (installed packages) | Install receipts in `~/.atomic/integrations//receipt.json`, plus last recorded hook dispatch from the current repository's hook log | | Skills | The cached `atomic-skills` package manifest and each installed skill's `metadata.version` and `metadata.updatedAt` frontmatter | | Enabled agents | Whether supported settings contain recognizable active Atomic hooks, with the evidence and inspection limitations | @@ -49,6 +50,13 @@ not searched. An absent observation does not prove the hook never ran. If another process holds the repository database for writing, the timestamp is reported as unknown rather than opening or modifying the database. +Repository build metadata is recorded only from the introduction of this feature; +older creation and migration builds remain unknown. The last writable initialization +open is not the last access: read-only commands and fast writable opens do not update +it. Diagnostics itself does not update these fields. The migration field covers the +additive table initialization, legacy split-database merge and path-claim backfill, +not redb's internal format upgrade. + ## Privacy The report contains versions, timestamps, paths, integration sources, and