pub struct RedactionPolicy {
pub input_level: RedactionLevel,
pub output_level: RedactionLevel,
pub error_level: RedactionLevel,
pub sensitive_key_patterns: Vec<String>,
pub sensitive_value_prefixes: Vec<String>,
pub detector: Arc<dyn PiiDetector>,
}Expand description
Configurable redaction rules for tool audit records and observability payloads.
Each field category (input, output, error) has its own
RedactionLevel. At Baseline the
policy composes two layers:
- Structural —
sensitive_key_patternstriggers wholesale replacement of JSON object values by key name, andsensitive_value_prefixesdoes the same for strings that start with a known prefix. - Entity-level —
detectorscans every remaining string leaf for emails, PANs, CPFs, CNPJs, etc. and masks the spans it finds in place.
The detector is a runtime object not persisted across
serialisation; on deserialise it is rebound to the process-wide
BaselineDetector.
Fields§
§input_level: RedactionLevelRedaction level for tool input values.
output_level: RedactionLevelRedaction level for tool output values.
error_level: RedactionLevelRedaction level for error detail strings.
sensitive_key_patterns: Vec<String>Key substrings that trigger redaction at baseline level. Stored lowercase; matched case-insensitively.
sensitive_value_prefixes: Vec<String>String patterns in values that trigger redaction at baseline
level (e.g. "Bearer ", "sk-"). Case-sensitive prefix match.
detector: Arc<dyn PiiDetector>Entity-level PII detector applied at baseline. Defaults to
BaselineDetector; assign directly to plug in a custom
implementation.
Implementations§
Source§impl RedactionPolicy
impl RedactionPolicy
Sourcepub fn baseline() -> RedactionPolicy
pub fn baseline() -> RedactionPolicy
Baseline redaction policy suitable for production audit logs and observability exports.
Redacts JSON object keys that look like credentials and string values that look like tokens wholesale, and masks entity-level PII (emails, PANs, CPFs, CNPJs, Pix UUIDs, E.164 phones, IPs, JWTs) detected anywhere in remaining string leaves. Preserves non-sensitive structural data for debugging.
All three levels (input_level, output_level,
error_level) default to Baseline.
Error strings routinely embed user data in stack traces
(NotFound: user cpf=…), so masking them is the safer
default — callers that need raw errors can explicitly set
error_level: RedactionLevel::None on a baseline policy.
Sourcepub fn with_keys(keys: impl IntoIterator<Item = String>) -> RedactionPolicy
pub fn with_keys(keys: impl IntoIterator<Item = String>) -> RedactionPolicy
Baseline policy plus additional sensitive key patterns.
Custom keys augment the baseline list — they do not replace it. Patterns are normalised to lowercase to keep the case-insensitive matching contract intact.
use agent_sdk_foundation::privacy::RedactionPolicy;
let policy = RedactionPolicy::with_keys(["chave_pix".to_owned()]);
assert!(policy.sensitive_key_patterns.iter().any(|p| p == "password"));
assert!(policy.sensitive_key_patterns.iter().any(|p| p == "chave_pix"));Sourcepub fn extend(&mut self, keys: impl IntoIterator<Item = String>)
pub fn extend(&mut self, keys: impl IntoIterator<Item = String>)
Append additional sensitive key patterns to this policy.
Patterns are normalised to lowercase. Duplicates (relative to the existing list) are silently dropped.
Sourcepub fn none() -> RedactionPolicy
pub fn none() -> RedactionPolicy
No-redaction policy — stores all values as-is.
Suitable only for development and testing. Never use in production audit logs.
Sourcepub fn full() -> RedactionPolicy
pub fn full() -> RedactionPolicy
Full-redaction policy — replaces all input/output/error content.
Suitable for high-security environments where no tool data should be stored in audit logs.
Sourcepub fn redact(&self, value: &Value) -> Value
pub fn redact(&self, value: &Value) -> Value
Inherent shorthand for redact_value.
Returns a fresh serde_json::Value with the policy applied.
Use redact_in_place instead when
the caller already owns the value and wants to avoid the
clone.
Sourcepub fn redact_in_place(&self, value: &mut Value)
pub fn redact_in_place(&self, value: &mut Value)
Apply the policy’s input_level rules to value in place.
Mutates value directly: object/array contents are walked
and string leaves are replaced with masked strings without
cloning the entire tree.
Trait Implementations§
Source§impl Clone for RedactionPolicy
impl Clone for RedactionPolicy
Source§fn clone(&self) -> RedactionPolicy
fn clone(&self) -> RedactionPolicy
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RedactionPolicy
impl Debug for RedactionPolicy
Source§impl Default for RedactionPolicy
impl Default for RedactionPolicy
Source§fn default() -> RedactionPolicy
fn default() -> RedactionPolicy
Returns Self::baseline() — never an empty policy.
This is loud on purpose: code that derives Default on a
struct containing RedactionPolicy gets the baseline
(sensitive-key list + entity detector) automatically rather
than an empty pass-through that would silently leak PII.
Code that wants a genuinely empty policy must opt in via
RedactionPolicy::none.