Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(backup): carry a size that rounds up to the next unit
Rudy Celeklif5a4086d06b4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 82e19d3e..511acbd9 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,6 +24,10 @@ version in [`docs/changelog/`](docs/changelog/):### Fixed+- `ai-memory backup` and `ai-memory backup-agents` now carry a size that+ would round to `1024.00` of the current unit into the next one. A backup+ of 1 MiB − 5 bytes was printed as `1024.00 KiB`.+- Codex's own memory-writing agent no longer appears as captured sessions.Codex runs a background "memory writing agent" from `$CODEX_HOME/memories`(the default `~/.codex/memories`) to consolidate its memory; the hooksdiff --git a/crates/ai-memory-cli/src/commands/backup.rs b/crates/ai-memory-cli/src/commands/backup.rsindex 088bb2e0..b5c7c9d5 100644--- a/crates/ai-memory-cli/src/commands/backup.rs+++ b/crates/ai-memory-cli/src/commands/backup.rs@@ -40,15 +40,45 @@ pub async fn run(config: &Config, args: BackupArgs) -> Result<()> {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;- while value >= 1024.0 && unit + 1 < UNITS.len() {+ // 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;}- if unit == 0 {- format!("{n} {}", UNITS[0])- } else {- format!("{value:.2} {}", UNITS[unit])+ format!("{value:.2} {}", UNITS[unit])+}++#[cfg(test)]+mod tests {+ use super::human_bytes;++ /// A size just under a unit boundary rounds up at two decimals, so it must+ /// carry to the next unit rather than print a magnitude that unit cannot+ /// hold. `backup` reported `1024.00 KiB` for 1 MiB − 5 bytes.+ #[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");+ // Controls: a value that does not reach the boundary stays put, and+ // exact boundaries keep the scale they already had.+ assert_eq!(human_bytes(1_048_570), "1023.99 KiB");+ assert_eq!(human_bytes(1_047_976), "1023.41 KiB");+ assert_eq!(human_bytes(0), "0 B");+ assert_eq!(human_bytes(1023), "1023 B");+ assert_eq!(human_bytes(1024), "1.00 KiB");+ assert_eq!(human_bytes(1536), "1.50 KiB");+ assert_eq!(human_bytes(1024 * 1024), "1.00 MiB");+ assert_eq!(human_bytes(3 * 1024 * 1024 * 1024), "3.00 GiB");}}diff --git a/crates/ai-memory-cli/src/commands/backup_agents.rs b/crates/ai-memory-cli/src/commands/backup_agents.rsindex 49ae0fd6..6549e5b8 100644--- a/crates/ai-memory-cli/src/commands/backup_agents.rs+++ b/crates/ai-memory-cli/src/commands/backup_agents.rs@@ -991,17 +991,21 @@ 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;- while value >= 1024.0 && unit + 1 < UNITS.len() {+ // 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;}- if unit == 0 {- format!("{n} {}", UNITS[0])- } else {- format!("{value:.2} {}", UNITS[unit])- }+ format!("{value:.2} {}", UNITS[unit])}/// Match an agent against a list of filter strings, handling kebab-case, snake_case, and aliases.@@ -1017,3 +1021,22 @@ 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");+ }+}