Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(util): add redact module for PII redaction in log output

Steven Enamakel2a28cea8abe9
diff --git a/src/openhuman/util/mod.rs b/src/openhuman/util/mod.rs
index 811e8643ed..f7ca7d4646 100644
--- a/src/openhuman/util/mod.rs
+++ b/src/openhuman/util/mod.rs
@@ -14,6 +14,9 @@
//! `openhuman::util::<fn>` paths (including the `truncate_with_ellipsis`
//! doctest) still resolve.
+/// PII redaction for log output. See the module docs for why this is here and
+/// not taken from the memory engine.
+pub mod redact;
pub mod retry;
pub mod sanitize;
pub mod text;
diff --git a/src/openhuman/util/redact.rs b/src/openhuman/util/redact.rs
new file mode 100644
index 0000000000..e05798fba0
--- /dev/null
+++ b/src/openhuman/util/redact.rs
@@ -0,0 +1,75 @@
+//! PII redaction for log output.
+//!
+//! Per `CLAUDE.md`: "Never log secrets or full PII." Source ids, entity ids and
+//! content paths can embed full email addresses, so any log line printing one
+//! has to redact it first.
+//!
+//! # Why this is OpenHuman's and not the memory crate's
+//!
+//! An identical helper lives at `tinymemory_core::util::redact`, and this host
+//! used to call it. That is an engine dependency taken on for the sake of six
+//! lines of SHA-256 — and the whole point of the memory-module port is that the
+//! host reaches memory over the bus and does not link the engine. A log
+//! formatter is not contract vocabulary either, so widening `tinymemory-api` to
+//! carry it would have meant adding a dependency to a crate whose manifest
+//! documents that it stays dependency-light.
+//!
+//! The engine keeps its copy for its own log lines. The two are independent by
+//! design: neither reads the other's output, and the hash is a grep key for a
+//! human debugging with the raw value to hand, not a wire value that has to
+//! agree across a boundary.
+
+use sha2::{Digest, Sha256};
+
+/// Redact a string by hashing it to 8 hex chars.
+///
+/// Stable across runs for the same input, so it can be grepped for in logs when
+/// debugging with the raw value available externally.
+///
+/// Use for source ids, entity ids, content paths and similar PII-bearing
+/// strings in log output.
+#[must_use]
+pub fn redact(s: &str) -> String {
+ let mut hasher = Sha256::new();
+ hasher.update(s.as_bytes());
+ let digest = hasher.finalize();
+ format!(
+ "{:08x}",
+ u32::from_be_bytes([digest[0], digest[1], digest[2], digest[3]])
+ )
+}
+
+#[cfg(test)]
+mod tests {
+ use super::redact;
+
+ #[test]
+ fn redaction_is_stable_for_the_same_input() {
+ assert_eq!(redact("[email protected]"), redact("[email protected]"));
+ }
+
+ #[test]
+ fn redaction_distinguishes_different_inputs() {
+ assert_ne!(redact("[email protected]"), redact("[email protected]"));
+ }
+
+ #[test]
+ fn redaction_never_echoes_the_input() {
+ let out = redact("[email protected]");
+ assert!(!out.contains("alice"), "{out}");
+ assert_eq!(out.len(), 8, "expected 8 hex chars, got {out:?}");
+ assert!(out.chars().all(|c| c.is_ascii_hexdigit()), "{out}");
+ }
+
+ /// Pins byte-parity with `tinymemory_core::util::redact::redact`, which this
+ /// replaced at five call sites. The two are independent going forward, but
+ /// a silent change on day one would make old and new log lines for the same
+ /// id fail to match while both looked correct.
+ #[test]
+ fn matches_the_engine_helper_it_replaced() {
+ assert_eq!(
+ redact("gmail:[email protected]"),
+ tinymemory_core::util::redact::redact("gmail:[email protected]")
+ );
+ }
+}