Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions atomic-cli/src/commands/diagnostics/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -44,11 +47,36 @@ pub(crate) enum RepositoryMetrics {
views: Observation<u64>,
intents: Observation<IntentCounts>,
current_view: Observation<CurrentViewCounts>,
#[serde(flatten)]
build_versions: Box<BuildVersions>,
tables: Vec<TableMetric>,
last_compacted: Observation<String>,
},
}

#[derive(Debug, Serialize)]
pub(crate) struct BuildVersions {
pub created_with: Observation<String>,
pub migrated_with: Observation<String>,
pub last_writable_open_with: Observation<String>,
}

fn build_version(txn: &redb::ReadTransaction, key: &str) -> Observation<String> {
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,
Expand Down Expand Up @@ -261,6 +289,11 @@ pub(crate) fn collect(workspace: &Result<Option<PathBuf>, 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),
Expand Down
16 changes: 16 additions & 0 deletions atomic-cli/src/commands/diagnostics/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fn render_repository(o: &mut String, repository: &RepositoryMetrics) {
views,
intents,
current_view,
build_versions,
tables,
last_compacted,
} => {
Expand All @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions atomic-cli/tests/it/diagnostics_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
78 changes: 78 additions & 0 deletions atomic-core/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;

fn git(dir: &Path, args: &[&str]) -> Option<String> {
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})");
}
161 changes: 161 additions & 0 deletions atomic-core/src/pristine/build_metadata.rs
Original file line number Diff line number Diff line change
@@ -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<Option<String>> {
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)
);
}
}
Loading
Loading