Skip to main content

kintsugi_core/
memory.rs

1//! Decision-memory helpers.
2//!
3//! The mutable store itself lives on [`crate::EventLog`] (same SQLite file);
4//! this module holds the small pure pieces: how a command is hashed into a
5//! stable key for the per-repo always-allow / always-deny memory.
6
7use sha2::{Digest, Sha256};
8
9/// Stable hash of a raw command line, used as the memory key within a repo.
10///
11/// The exact command text is hashed (not the argv), so "always allow this exact
12/// command in this repo" means exactly that — a different command, even by a
13/// space, is a different key.
14pub fn command_hash(raw: &str) -> String {
15    let mut hasher = Sha256::new();
16    hasher.update(raw.trim().as_bytes());
17    hex::encode(hasher.finalize())
18}
19
20#[cfg(test)]
21mod tests {
22    use super::command_hash;
23
24    #[test]
25    fn stable_and_trim_insensitive() {
26        assert_eq!(command_hash("rm -rf x"), command_hash("  rm -rf x  "));
27    }
28
29    #[test]
30    fn distinguishes_different_commands() {
31        assert_ne!(command_hash("rm -rf x"), command_hash("rm -rf y"));
32        assert_eq!(command_hash("ls").len(), 64);
33    }
34}