diff --git a/crates/core/src/commands/status.rs b/crates/core/src/commands/status.rs index af3130dc5..2676cd2b5 100644 --- a/crates/core/src/commands/status.rs +++ b/crates/core/src/commands/status.rs @@ -1,5 +1,5 @@ use crate::{ - AppError, PermissionReport, + AppError, ErrorCode, PermissionReport, adapter::PlatformAdapter, commands::permissions::{self, PermissionsArgs}, context::CommandContext, @@ -23,11 +23,19 @@ pub fn execute_with_report_with_context( let state_root = crate::storage::state_root::resolve_configured_state_root()?; let privacy = crate::storage::private_file_parent::ensure_private(&state_root); - let (snapshot_id, ref_count) = if privacy.is_ok() { + let (mut snapshot_id, ref_count) = if privacy.is_ok() { let store = RefStore::for_session(context.session_id())?; let snapshot_id = store.latest_snapshot_id()?; let ref_count = match snapshot_id.as_deref() { - Some(snapshot_id) => Some(store.load_snapshot(snapshot_id)?.len()), + Some(id) => match store.load_snapshot(id) { + Ok(refmap) => Some(refmap.len()), + // The latest pointer can outlive the snapshot it names when a + // caller deletes its own snapshots after finishing. A missing + // snapshot is not a status failure, so report it as absent; + // other load errors still fail the command. + Err(AppError::Adapter(error)) if error.code == ErrorCode::SnapshotNotFound => None, + Err(error) => return Err(error), + }, None => None, }; (snapshot_id, ref_count) @@ -41,6 +49,9 @@ pub fn execute_with_report_with_context( "error": crate::ErrorPayload::from_app_error(&AppError::Io(error)), }), }; + if ref_count.is_none() { + snapshot_id = None; + } let session_id = context.session_id().map(str::to_string); let tracing = context.trace_enabled(); let artifacts = session_id diff --git a/crates/core/src/commands/status_tests.rs b/crates/core/src/commands/status_tests.rs index 8d4bcc2ff..72062f91a 100644 --- a/crates/core/src/commands/status_tests.rs +++ b/crates/core/src/commands/status_tests.rs @@ -150,3 +150,45 @@ fn status_surfaces_artifacts_mode_for_active_session() { assert_eq!(value["artifacts"], "full"); } + +#[test] +fn status_reports_saved_latest_snapshot() { + let _guard = crate::refs::refs_test_support::HomeGuard::new(); + let store = RefStore::new().unwrap(); + store + .save_snapshot("ssvalid1", &crate::refs::RefMap::new()) + .unwrap(); + store.set_latest("ssvalid1").unwrap(); + let report = PermissionReport { + accessibility: PermissionState::Granted, + screen_recording: PermissionState::Granted, + automation: PermissionState::NotRequired, + }; + + let value = + execute_with_report_with_context(&DeniedAdapter, &report, &CommandContext::default()) + .unwrap(); + + assert_eq!(value["snapshot_id"], "ssvalid1"); + assert_eq!(value["ref_count"], 0); +} + +#[test] +fn status_reports_missing_latest_snapshot_as_absent() { + let _guard = crate::refs::refs_test_support::HomeGuard::new(); + let store = RefStore::new().unwrap(); + store.set_latest("ssgone1").unwrap(); + let report = PermissionReport { + accessibility: PermissionState::Granted, + screen_recording: PermissionState::Granted, + automation: PermissionState::NotRequired, + }; + + let value = + execute_with_report_with_context(&DeniedAdapter, &report, &CommandContext::default()) + .unwrap(); + + assert!(value["snapshot_id"].is_null()); + assert!(value["ref_count"].is_null()); + assert_eq!(value["platform"], std::env::consts::OS); +}