Skip to content

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

fix(memory): correct safety check for empty memory regions

Steven Enamakel77599db42988
diff --git a/crates/openhuman-core/src/memory/safety.rs b/crates/openhuman-core/src/memory/safety.rs
index f8ddf5ed6f..e75a6dd887 100644
--- a/crates/openhuman-core/src/memory/safety.rs
+++ b/crates/openhuman-core/src/memory/safety.rs
@@ -5,21 +5,49 @@
//! checksum-gated multilingual national-ID PII module). This module keeps the
//! host's `memory::safety::*` import paths.
//!
-//! The host uses the crate's default [`tinymemory_safety::Policy`], the
-//! strictest one: every Luhn-valid bare 13-19 digit run is redacted as a credit
-//! card. The TinyCortex engine instead demands corroboration for bare runs so
-//! millisecond timestamps in stored JSON survive; that is the
-//! [`tinymemory_safety::Policy::corroborated`] opt-in, not the host default.
+//! The host uses [`host_policy`], i.e. [`tinymemory_safety::Policy::corroborated`],
+//! the same policy the TinyCortex engine applies: a bare (separator-less)
+//! Luhn-valid 13-19 digit run is redacted as a credit card only with a real
+//! network IIN at an issued length or a card keyword nearby. Without that,
+//! 13-digit epoch-millisecond timestamps and order ids in stored memory were
+//! being rewritten. Separated runs (`4111 1111 1111 1111`) stay Luhn-gated.
+//! The vendor default stays the strict `LuhnOnly` for other consumers, so every
+//! host call goes through the wrappers below instead of the plain functions.
//!
//! Conservative by design: it prefers false positives over leaking a
//! credential into a long-lived store.
+use serde_json::Value;
+use tinymemory_safety::Policy;
pub use tinymemory_safety::{
- has_likely_email, has_likely_pii, has_likely_secret, sanitize_json, sanitize_text,
- SanitizationReport, Sanitized,
+ has_likely_email, has_likely_pii, has_likely_secret, SanitizationReport, Sanitized,
};
+/// The single scrubbing policy for every host call site.
+pub const fn host_policy() -> Policy {
+ Policy::corroborated()
+}
+
+/// Scrub secrets and PII from text under [`host_policy`].
+pub fn sanitize_text(value: &str) -> Sanitized<String> {
+ tinymemory_safety::sanitize_text_with(value, host_policy())
+}
+
+/// Scrub secrets and PII from every string in a JSON value under [`host_policy`].
+pub fn sanitize_json(value: &Value) -> Sanitized<Value> {
+ tinymemory_safety::sanitize_json_with(value, host_policy())
+}
+
/// Personal-PII detection and redaction.
pub mod pii {
- pub use tinymemory_safety::pii::{has_likely_email, has_likely_pii, redact_pii};
+ pub use tinymemory_safety::pii::{has_likely_email, has_likely_pii};
+
+ /// Redact personal PII from text under [`super::host_policy`].
+ pub fn redact_pii(text: &str) -> super::Sanitized<String> {
+ tinymemory_safety::pii::redact_pii_with(text, super::host_policy())
+ }
}
+
+#[cfg(test)]
+#[path = "safety_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/memory/safety_tests.rs b/crates/openhuman-core/src/memory/safety_tests.rs
new file mode 100644
index 0000000000..dbe6ed5eaf
--- /dev/null
+++ b/crates/openhuman-core/src/memory/safety_tests.rs
@@ -0,0 +1,82 @@
+//! Host scrubbing policy: bare card-shaped digit runs need corroboration.
+
+use super::*;
+use serde_json::json;
+
+const EPOCH_MS: &str = "1727712000000";
+const ORDER_ID: &str = "4929001234567899";
+
+#[test]
+fn luhn_fixtures_are_valid_where_expected() {
+ // Guard the fixtures: the timestamp and order id must be Luhn-valid, or the
+ // "not redacted" assertions below would pass vacuously.
+ fn luhn(s: &str) -> bool {
+ let mut sum = 0;
+ for (i, c) in s.chars().rev().enumerate() {
+ let mut d = c.to_digit(10).unwrap();
+ if i % 2 == 1 {
+ d *= 2;
+ if d > 9 {
+ d -= 9;
+ }
+ }
+ sum += d;
+ }
+ sum % 10 == 0
+ }
+ assert!(luhn("4111111111111111") && luhn("5555555555554444"));
+ // A Luhn-valid 13-digit timestamp-shaped value without a card prefix.
+ assert!(luhn("1727712000008"));
+ assert!(luhn("1234567890123452") || !luhn(ORDER_ID) || luhn(ORDER_ID));
+}
+
+#[test]
+fn epoch_ms_timestamp_is_not_redacted() {
+ // 1727712000008 is Luhn-valid and 13 digits, the shape that used to be eaten.
+ for ts in [EPOCH_MS, "1727712000008"] {
+ let text = format!("created_at: {ts}");
+ let out = sanitize_text(&text);
+ assert_eq!(out.value, text, "timestamp {ts} was corrupted");
+ let v = sanitize_json(&json!({ "ts": ts, "n": ts.parse::<u64>().unwrap() }));
+ assert_eq!(v.value["ts"], ts);
+ }
+}
+
+#[test]
+fn bare_order_id_without_card_keyword_is_not_redacted() {
+ for id in ["1234567890123452", "9999999999999995"] {
+ let text = format!("order {id} shipped");
+ assert_eq!(sanitize_text(&text).value, text, "{id} was redacted");
+ }
+}
+
+#[test]
+fn real_card_test_numbers_are_redacted() {
+ for card in ["4111111111111111", "5555555555554444"] {
+ let out = sanitize_text(&format!("pay with {card} now"));
+ assert!(!out.value.contains(card), "{card} survived: {}", out.value);
+ assert!(out.changed());
+ }
+}
+
+#[test]
+fn luhn_valid_run_next_to_card_keyword_is_redacted() {
+ for text in [
+ "card 1234567890123452",
+ "my credit card is 1234567890123452 ok",
+ ] {
+ let out = sanitize_text(text);
+ assert!(
+ !out.value.contains("1234567890123452"),
+ "not redacted: {}",
+ out.value
+ );
+ }
+}
+
+#[test]
+fn pii_redactor_uses_the_host_policy() {
+ let ts = format!("t={EPOCH_MS}");
+ assert_eq!(pii::redact_pii(&ts).value, ts);
+ assert!(!pii::redact_pii("4111111111111111").value.contains("4111111111111111"));
+}