Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
refactor(cli): share backup size formatting
AkitaOnRailscdc56b4d5b1c
diff --git a/crates/ai-memory-cli/src/commands/backup.rs b/crates/ai-memory-cli/src/commands/backup.rsindex b5c7c9d5..459154ab 100644--- a/crates/ai-memory-cli/src/commands/backup.rs+++ b/crates/ai-memory-cli/src/commands/backup.rs@@ -38,7 +38,8 @@ pub async fn run(config: &Config, args: BackupArgs) -> Result<()> {Ok(())}-fn human_bytes(n: u64) -> String {+/// Render a backup size with binary units and two decimal places.+pub(crate) fn human_bytes(n: u64) -> String {const UNITS: &[&str] = &["B", "KiB", "MiB", "GiB"];if n < 1024 {return format!("{n} B");diff --git a/crates/ai-memory-cli/src/commands/backup_agents.rs b/crates/ai-memory-cli/src/commands/backup_agents.rsindex 6549e5b8..31d0f82a 100644--- a/crates/ai-memory-cli/src/commands/backup_agents.rs+++ b/crates/ai-memory-cli/src/commands/backup_agents.rs@@ -18,6 +18,7 @@ use tar::{Builder, Header};use tracing::info;use crate::cli::{AgentBackupScope, BackupAgentsArgs, McpClient};+use crate::commands::backup::human_bytes;use crate::commands::path_util::{claude_config_dir, home_dir};use crate::config::Config;@@ -989,25 +990,6 @@ fn portable_mode(path: &Path) -> Result<Option<u32>> {}}-fn human_bytes(n: u64) -> String {- const UNITS: &[&str] = &["B", "KiB", "MiB", "GiB"];- if n < 1024 {- return format!("{n} B");- }- let mut value = n as f64;- let mut unit = 0;- // Two decimal places: 1023.995 KiB renders as 1024.00. Carry while the- // formatted magnitude would still be 1024.00, the same rule `compact`- // uses at one decimal. Stopping at `value < 1024.0` leaves that sliver- // printing a size the unit cannot hold (1 MiB − 5 bytes came out as- // `1024.00 KiB`).- while (value * 100.0).round() >= 102_400.0 && unit + 1 < UNITS.len() {- value /= 1024.0;- unit += 1;- }- format!("{value:.2} {}", UNITS[unit])-}-/// Match an agent against a list of filter strings, handling kebab-case, snake_case, and aliases.pub(crate) fn matches_agent_filter(agent: AgentKind, filter_list: &[String]) -> bool {filter_list.iter().any(|f| {@@ -1021,22 +1003,3 @@ pub(crate) fn matches_agent_filter(agent: AgentKind, filter_list: &[String]) ->AgentKind::from_wire(&normalized) == agent})}--#[cfg(test)]-mod tests {- use super::human_bytes;-- /// Same two-decimal carry as `backup`: 1 MiB − 5 bytes must not render as- /// `1024.00 KiB` on the archive line.- #[test]- fn human_bytes_carries_a_value_that_would_round_up_to_the_next_unit() {- for n in 1_048_571..=1_048_575 {- assert_eq!(human_bytes(n), "1.00 MiB", "{n}");- }- assert_eq!(human_bytes(1024 * 1024 * 1024 - 6), "1.00 GiB");- assert_eq!(human_bytes(1_048_570), "1023.99 KiB");- assert_eq!(human_bytes(1023), "1023 B");- assert_eq!(human_bytes(1024), "1.00 KiB");- assert_eq!(human_bytes(1024 * 1024), "1.00 MiB");- }-}