Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(compact): roll a size up a unit instead of printing 1024.0 KiB

Rudy Mizrahi Celekli1a89b2e8eb3d
diff --git a/crates/ai-memory-cli/src/commands/compact.rs b/crates/ai-memory-cli/src/commands/compact.rs
index 68041f71..6d08ca7b 100644
--- a/crates/ai-memory-cli/src/commands/compact.rs
+++ b/crates/ai-memory-cli/src/commands/compact.rs
@@ -70,7 +70,11 @@ pub(crate) fn human_bytes(bytes: u64) -> String {
}
let mut value = bytes as f64;
let mut unit = 0;
- while value >= 1024.0 && unit < UNITS.len() - 1 {
+ // Carry while the value would still *render* as 1024.0 or more at one
+ // decimal. Stopping at `value < 1024.0` leaves 1023.95..1024.0 to round
+ // up at format time, printing a magnitude that belongs to the next unit
+ // (1 MiB − 6 bytes came out as `1024.0 KiB`).
+ while (value * 10.0).round() >= 10240.0 && unit < UNITS.len() - 1 {
value /= 1024.0;
unit += 1;
}
@@ -90,4 +94,16 @@ mod tests {
assert_eq!(human_bytes(1024 * 1024), "1.0 MiB");
assert_eq!(human_bytes(3 * 1024 * 1024 * 1024), "3.0 GiB");
}
+
+ /// A size just under a unit boundary rounds up at one decimal, so it must
+ /// carry to the next unit rather than print a magnitude that unit cannot
+ /// hold. `status` reported `1024.0 KiB on disk` for a 1 MiB − 6 database.
+ #[test]
+ fn human_bytes_carries_a_value_that_would_round_up_to_the_next_unit() {
+ assert_eq!(human_bytes(1024 * 1024 - 6), "1.0 MiB");
+ assert_eq!(human_bytes(1024 * 1024 * 1024 - 24), "1.0 GiB");
+ // Controls: a value that does not reach the boundary stays put.
+ assert_eq!(human_bytes(1024 * 1024 - 600), "1023.4 KiB");
+ assert_eq!(human_bytes(1023), "1023 B");
+ }
}