use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum TaintLabel {
Untrusted,
Sanitized,
Quarantined,
Sensitive,
CrossAgent,
Replayed,
MixedProvenance,
IntegrityFailed,
}
impl fmt::Display for TaintLabel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TaintLabel::Untrusted => write!(f, "untrusted"),
TaintLabel::Sanitized => write!(f, "sanitized"),
TaintLabel::Quarantined => write!(f, "quarantined"),
TaintLabel::Sensitive => write!(f, "sensitive"),
TaintLabel::CrossAgent => write!(f, "cross_agent"),
TaintLabel::Replayed => write!(f, "replayed"),
TaintLabel::MixedProvenance => write!(f, "mixed_provenance"),
TaintLabel::IntegrityFailed => write!(f, "integrity_failed"),
}
}
}
pub const MAX_TAINT_LABELS: usize = 16;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct MemoryEntry {
pub id: String,
pub fingerprint: String,
pub preview: String,
pub recorded_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_accessed: Option<String>,
#[serde(default)]
pub access_count: u64,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub taint_labels: Vec<TaintLabel>,
#[serde(default = "default_trust_score")]
pub trust_score: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub provenance_id: Option<String>,
#[serde(default)]
pub quarantined: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub namespace: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub agent_id: Option<String>,
pub content_hash: String,
}
fn default_trust_score() -> f64 {
1.0
}
impl MemoryEntry {
pub const MAX_PREVIEW_LENGTH: usize = 100;
const MAX_ID_LEN: usize = 256;
const MAX_FINGERPRINT_LEN: usize = 128;
const MAX_CONTENT_HASH_LEN: usize = 128;
const MAX_TIMESTAMP_LEN: usize = 64;
const MAX_PROVENANCE_ID_LEN: usize = 256;
const MAX_NAMESPACE_LEN: usize = 256;
const MAX_SESSION_ID_LEN: usize = 256;
const MAX_AGENT_ID_LEN: usize = 256;
fn validate_no_control_chars(
type_name: &str,
field_name: &str,
value: &str,
) -> Result<(), String> {
if crate::core::has_dangerous_chars(value) {
return Err(format!(
"{type_name} {field_name} contains control or format characters"
));
}
Ok(())
}
pub fn validate(&self) -> Result<(), String> {
if self.id.len() > Self::MAX_ID_LEN {
return Err(format!(
"MemoryEntry id length {} exceeds max {}",
self.id.len(),
Self::MAX_ID_LEN
));
}
if self.fingerprint.len() > Self::MAX_FINGERPRINT_LEN {
return Err(format!(
"MemoryEntry '{}' fingerprint length {} exceeds max {}",
self.id,
self.fingerprint.len(),
Self::MAX_FINGERPRINT_LEN
));
}
if self.preview.len() > Self::MAX_PREVIEW_LENGTH + 3 {
return Err(format!(
"MemoryEntry '{}' preview length {} exceeds max {}",
self.id,
self.preview.len(),
Self::MAX_PREVIEW_LENGTH + 3
));
}
if self.recorded_at.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"MemoryEntry '{}' recorded_at length {} exceeds max {}",
self.id,
self.recorded_at.len(),
Self::MAX_TIMESTAMP_LEN
));
}
if let Some(ref la) = self.last_accessed {
if la.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"MemoryEntry '{}' last_accessed length {} exceeds max {}",
self.id,
la.len(),
Self::MAX_TIMESTAMP_LEN
));
}
}
if self.content_hash.len() > Self::MAX_CONTENT_HASH_LEN {
return Err(format!(
"MemoryEntry '{}' content_hash length {} exceeds max {}",
self.id,
self.content_hash.len(),
Self::MAX_CONTENT_HASH_LEN
));
}
if let Some(ref pid) = self.provenance_id {
if pid.len() > Self::MAX_PROVENANCE_ID_LEN {
return Err(format!(
"MemoryEntry '{}' provenance_id length {} exceeds max {}",
self.id,
pid.len(),
Self::MAX_PROVENANCE_ID_LEN
));
}
}
if let Some(ref ns) = self.namespace {
if ns.len() > Self::MAX_NAMESPACE_LEN {
return Err(format!(
"MemoryEntry '{}' namespace length {} exceeds max {}",
self.id,
ns.len(),
Self::MAX_NAMESPACE_LEN
));
}
}
if let Some(ref sid) = self.session_id {
if sid.len() > Self::MAX_SESSION_ID_LEN {
return Err(format!(
"MemoryEntry '{}' session_id length {} exceeds max {}",
self.id,
sid.len(),
Self::MAX_SESSION_ID_LEN
));
}
}
if let Some(ref aid) = self.agent_id {
if aid.len() > Self::MAX_AGENT_ID_LEN {
return Err(format!(
"MemoryEntry '{}' agent_id length {} exceeds max {}",
self.id,
aid.len(),
Self::MAX_AGENT_ID_LEN
));
}
}
Self::validate_no_control_chars("MemoryEntry", "id", &self.id)?;
Self::validate_no_control_chars("MemoryEntry", "fingerprint", &self.fingerprint)?;
Self::validate_no_control_chars("MemoryEntry", "recorded_at", &self.recorded_at)?;
Self::validate_no_control_chars("MemoryEntry", "content_hash", &self.content_hash)?;
if let Some(ref la) = self.last_accessed {
Self::validate_no_control_chars("MemoryEntry", "last_accessed", la)?;
}
if let Some(ref pid) = self.provenance_id {
Self::validate_no_control_chars("MemoryEntry", "provenance_id", pid)?;
}
if let Some(ref ns) = self.namespace {
Self::validate_no_control_chars("MemoryEntry", "namespace", ns)?;
}
if let Some(ref sid) = self.session_id {
Self::validate_no_control_chars("MemoryEntry", "session_id", sid)?;
}
if let Some(ref aid) = self.agent_id {
Self::validate_no_control_chars("MemoryEntry", "agent_id", aid)?;
}
if self.taint_labels.len() > MAX_TAINT_LABELS {
return Err(format!(
"MemoryEntry '{}' has {} taint_labels, max {}",
self.id,
self.taint_labels.len(),
MAX_TAINT_LABELS
));
}
if !self.trust_score.is_finite() {
return Err(format!(
"MemoryEntry '{}' trust_score is not finite: {}",
self.id, self.trust_score
));
}
if self.trust_score < 0.0 || self.trust_score > 1.0 {
return Err(format!(
"MemoryEntry '{}' trust_score must be in [0.0, 1.0], got {}",
self.id, self.trust_score
));
}
let has_security_taint = self.taint_labels.iter().any(|label| {
matches!(
label,
TaintLabel::Untrusted
| TaintLabel::Quarantined
| TaintLabel::IntegrityFailed
| TaintLabel::MixedProvenance
| TaintLabel::Replayed
)
});
if has_security_taint && self.trust_score >= 1.0 {
return Err(format!(
"MemoryEntry '{}' has security-relevant taint labels {:?} but trust_score is {} (must be < 1.0)",
self.id, self.taint_labels, self.trust_score
));
}
Ok(())
}
#[deprecated(
since = "4.0.1",
note = "use validate() which also checks taint/trust consistency"
)]
pub fn validate_finite(&self) -> Result<(), String> {
if !self.trust_score.is_finite() {
return Err(format!(
"MemoryEntry '{}' trust_score is not finite: {}",
self.id, self.trust_score
));
}
if self.trust_score < 0.0 || self.trust_score > 1.0 {
return Err(format!(
"MemoryEntry '{}' trust_score must be in [0.0, 1.0], got {}",
self.id, self.trust_score
));
}
Ok(())
}
pub fn new(
id: String,
fingerprint: String,
content: &str,
content_hash: String,
recorded_at: String,
) -> Self {
let preview = if content.len() > Self::MAX_PREVIEW_LENGTH {
let mut end = Self::MAX_PREVIEW_LENGTH;
while !content.is_char_boundary(end) && end > 0 {
end -= 1;
}
format!("{}...", &content[..end])
} else {
content.to_string()
};
Self {
id,
fingerprint,
preview,
recorded_at,
last_accessed: None,
access_count: 0,
taint_labels: vec![TaintLabel::Untrusted],
trust_score: 0.5,
provenance_id: None,
quarantined: false,
namespace: None,
session_id: None,
agent_id: None,
content_hash,
}
}
pub fn has_taint(&self, label: TaintLabel) -> bool {
self.taint_labels.contains(&label)
}
pub fn add_taint(&mut self, label: TaintLabel) -> bool {
if self.taint_labels.len() >= MAX_TAINT_LABELS {
return false;
}
if !self.taint_labels.contains(&label) {
self.taint_labels.push(label);
true
} else {
false
}
}
pub fn is_blocked(&self) -> bool {
self.quarantined || self.has_taint(TaintLabel::Quarantined)
}
pub fn decayed_trust_score(&self, decay_rate: f64, current_time: &str) -> f64 {
match Self::hours_since(&self.recorded_at, current_time) {
Some(age_hours) => self.trust_score * (-decay_rate * age_hours).exp(),
None => {
0.0
}
}
}
fn hours_since(start: &str, end: &str) -> Option<f64> {
let start_secs = Self::parse_timestamp(start)?;
let end_secs = Self::parse_timestamp(end)?;
if end_secs > start_secs {
Some((end_secs - start_secs) as f64 / 3600.0)
} else {
Some(0.0)
}
}
fn parse_timestamp(ts: &str) -> Option<u64> {
crate::time_util::parse_iso8601_secs(ts).ok()
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum ProvenanceEventType {
ToolResponse,
Notification,
Derivation,
Replay,
ExternalInput,
AgentGenerated,
CrossAgentReceive,
CrossAgentSend,
Restore,
Sanitization,
}
impl fmt::Display for ProvenanceEventType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProvenanceEventType::ToolResponse => write!(f, "tool_response"),
ProvenanceEventType::Notification => write!(f, "notification"),
ProvenanceEventType::Derivation => write!(f, "derivation"),
ProvenanceEventType::Replay => write!(f, "replay"),
ProvenanceEventType::ExternalInput => write!(f, "external_input"),
ProvenanceEventType::AgentGenerated => write!(f, "agent_generated"),
ProvenanceEventType::CrossAgentReceive => write!(f, "cross_agent_receive"),
ProvenanceEventType::CrossAgentSend => write!(f, "cross_agent_send"),
ProvenanceEventType::Restore => write!(f, "restore"),
ProvenanceEventType::Sanitization => write!(f, "sanitization"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ProvenanceNode {
pub id: String,
pub event_type: ProvenanceEventType,
pub timestamp: String,
pub source: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub parents: Vec<String>,
pub content_hash: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub entry_id: Option<String>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub metadata: HashMap<String, String>,
}
impl ProvenanceNode {
pub const MAX_PARENTS: usize = 64;
pub fn new(
id: String,
event_type: ProvenanceEventType,
source: String,
content_hash: String,
timestamp: String,
) -> Self {
Self {
id,
event_type,
timestamp,
source,
session_id: None,
parents: Vec::new(),
content_hash,
entry_id: None,
metadata: HashMap::new(),
}
}
pub const MAX_METADATA_ENTRIES: usize = 64;
pub const MAX_ID_LEN: usize = 256;
pub const MAX_SOURCE_LEN: usize = 1024;
pub const MAX_CONTENT_HASH_LEN: usize = 256;
pub fn validate(&self) -> Result<(), String> {
if self.id.len() > Self::MAX_ID_LEN {
return Err(format!(
"ProvenanceNode id length {} exceeds max {}",
self.id.len(),
Self::MAX_ID_LEN
));
}
if self.source.len() > Self::MAX_SOURCE_LEN {
return Err(format!(
"ProvenanceNode '{}' source length {} exceeds max {}",
self.id,
self.source.len(),
Self::MAX_SOURCE_LEN
));
}
if self.content_hash.len() > Self::MAX_CONTENT_HASH_LEN {
return Err(format!(
"ProvenanceNode '{}' content_hash length {} exceeds max {}",
self.id,
self.content_hash.len(),
Self::MAX_CONTENT_HASH_LEN
));
}
if self.parents.len() > Self::MAX_PARENTS {
return Err(format!(
"ProvenanceNode '{}' has {} parents (max {})",
self.id,
self.parents.len(),
Self::MAX_PARENTS
));
}
if self.metadata.len() > Self::MAX_METADATA_ENTRIES {
return Err(format!(
"ProvenanceNode '{}' has {} metadata entries (max {})",
self.id,
self.metadata.len(),
Self::MAX_METADATA_ENTRIES
));
}
for (field, value) in [
("id", self.id.as_str()),
("source", self.source.as_str()),
("content_hash", self.content_hash.as_str()),
("timestamp", self.timestamp.as_str()),
] {
if crate::core::has_dangerous_chars(value) {
return Err(format!(
"ProvenanceNode {field} contains control or format characters"
));
}
}
if let Some(ref sid) = self.session_id {
if crate::core::has_dangerous_chars(sid) {
return Err(
"ProvenanceNode session_id contains control or format characters".to_string(),
);
}
}
if let Some(ref eid) = self.entry_id {
if crate::core::has_dangerous_chars(eid) {
return Err(
"ProvenanceNode entry_id contains control or format characters".to_string(),
);
}
}
for parent in &self.parents {
if crate::core::has_dangerous_chars(parent) {
return Err(
"ProvenanceNode parent contains control or format characters".to_string(),
);
}
}
for (key, val) in &self.metadata {
if crate::core::has_dangerous_chars(key) || crate::core::has_dangerous_chars(val) {
return Err(
"ProvenanceNode metadata contains control or format characters".to_string(),
);
}
}
Ok(())
}
pub fn is_suspicious(&self) -> bool {
matches!(
self.event_type,
ProvenanceEventType::Replay | ProvenanceEventType::CrossAgentReceive
)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum QuarantineDetection {
InjectionPattern,
SuspiciousDataFlow,
LowTrust,
CrossSessionReplay,
NotificationReplay,
IntegrityFailure,
ManualQuarantine,
ExcessiveAccess,
SensitiveData,
}
impl fmt::Display for QuarantineDetection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
QuarantineDetection::InjectionPattern => write!(f, "injection_pattern"),
QuarantineDetection::SuspiciousDataFlow => write!(f, "suspicious_data_flow"),
QuarantineDetection::LowTrust => write!(f, "low_trust"),
QuarantineDetection::CrossSessionReplay => write!(f, "cross_session_replay"),
QuarantineDetection::NotificationReplay => write!(f, "notification_replay"),
QuarantineDetection::IntegrityFailure => write!(f, "integrity_failure"),
QuarantineDetection::ManualQuarantine => write!(f, "manual_quarantine"),
QuarantineDetection::ExcessiveAccess => write!(f, "excessive_access"),
QuarantineDetection::SensitiveData => write!(f, "sensitive_data"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct QuarantineEntry {
pub entry_id: String,
pub reason: QuarantineDetection,
pub quarantined_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub triggered_by: Option<String>,
#[serde(default)]
pub released: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub released_at: Option<String>,
}
impl QuarantineEntry {
const MAX_ENTRY_ID_LEN: usize = 256;
const MAX_TIMESTAMP_LEN: usize = 64;
const MAX_DESCRIPTION_LEN: usize = 4096;
const MAX_TRIGGERED_BY_LEN: usize = 256;
pub fn new(entry_id: String, reason: QuarantineDetection, quarantined_at: String) -> Self {
Self {
entry_id,
reason,
quarantined_at,
description: None,
triggered_by: None,
released: false,
released_at: None,
}
}
pub fn validate(&self) -> Result<(), String> {
if self.entry_id.len() > Self::MAX_ENTRY_ID_LEN {
return Err(format!(
"QuarantineEntry entry_id length {} exceeds max {}",
self.entry_id.len(),
Self::MAX_ENTRY_ID_LEN,
));
}
if crate::core::has_dangerous_chars(&self.entry_id) {
return Err(
"QuarantineEntry entry_id contains control or format characters".to_string(),
);
}
if self.quarantined_at.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"QuarantineEntry quarantined_at length {} exceeds max {}",
self.quarantined_at.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
if crate::core::has_dangerous_chars(&self.quarantined_at) {
return Err(
"QuarantineEntry quarantined_at contains control or format characters".to_string(),
);
}
if let Some(ref desc) = self.description {
if desc.len() > Self::MAX_DESCRIPTION_LEN {
return Err(format!(
"QuarantineEntry description length {} exceeds max {}",
desc.len(),
Self::MAX_DESCRIPTION_LEN,
));
}
if crate::core::has_dangerous_chars(desc) {
return Err(
"QuarantineEntry description contains control or format characters".to_string(),
);
}
}
if let Some(ref tb) = self.triggered_by {
if tb.len() > Self::MAX_TRIGGERED_BY_LEN {
return Err(format!(
"QuarantineEntry triggered_by length {} exceeds max {}",
tb.len(),
Self::MAX_TRIGGERED_BY_LEN,
));
}
if crate::core::has_dangerous_chars(tb) {
return Err(
"QuarantineEntry triggered_by contains control or format characters"
.to_string(),
);
}
}
if let Some(ref ra) = self.released_at {
if ra.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"QuarantineEntry released_at length {} exceeds max {}",
ra.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
if crate::core::has_dangerous_chars(ra) {
return Err(
"QuarantineEntry released_at contains control or format characters".to_string(),
);
}
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct MemoryNamespace {
pub id: String,
pub owner_agent: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub read_allowed: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub write_allowed: Vec<String>,
pub created_at: String,
#[serde(default)]
pub isolation: NamespaceIsolation,
#[serde(default)]
pub is_default: bool,
}
impl MemoryNamespace {
pub fn new(id: String, owner_agent: String, created_at: String) -> Self {
let read_allowed = vec![owner_agent.clone()];
let write_allowed = vec![owner_agent.clone()];
Self {
id,
owner_agent,
read_allowed,
write_allowed,
created_at,
isolation: NamespaceIsolation::default(),
is_default: false,
}
}
pub fn can_read(&self, agent_id: &str) -> bool {
self.owner_agent == agent_id || self.read_allowed.iter().any(|a| a == agent_id || a == "*")
}
pub fn can_write(&self, agent_id: &str) -> bool {
self.owner_agent == agent_id || self.write_allowed.iter().any(|a| a == agent_id || a == "*")
}
pub const MAX_ACL_ENTRIES: usize = 1000;
pub const MAX_FIELD_LEN: usize = 256;
pub fn validate(&self) -> Result<(), String> {
for (field, value) in [
("id", self.id.as_str()),
("owner_agent", self.owner_agent.as_str()),
("created_at", self.created_at.as_str()),
] {
if value.len() > Self::MAX_FIELD_LEN {
return Err(format!(
"MemoryNamespace {field} length {} exceeds max {}",
value.len(),
Self::MAX_FIELD_LEN
));
}
if crate::core::has_dangerous_chars(value) {
return Err(format!(
"MemoryNamespace {field} contains control or format characters"
));
}
}
if self.read_allowed.len() > Self::MAX_ACL_ENTRIES {
return Err(format!(
"MemoryNamespace '{}' has {} read_allowed entries (max {})",
self.id,
self.read_allowed.len(),
Self::MAX_ACL_ENTRIES
));
}
if self.write_allowed.len() > Self::MAX_ACL_ENTRIES {
return Err(format!(
"MemoryNamespace '{}' has {} write_allowed entries (max {})",
self.id,
self.write_allowed.len(),
Self::MAX_ACL_ENTRIES
));
}
for entry in self.read_allowed.iter().chain(self.write_allowed.iter()) {
if entry.len() > Self::MAX_FIELD_LEN {
return Err(format!(
"MemoryNamespace ACL entry length {} exceeds max {}",
entry.len(),
Self::MAX_FIELD_LEN
));
}
if crate::core::has_dangerous_chars(entry) {
return Err(
"MemoryNamespace ACL entry contains control or format characters".to_string(),
);
}
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum NamespaceIsolation {
#[default]
Session,
Agent,
Shared,
}
impl fmt::Display for NamespaceIsolation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NamespaceIsolation::Session => write!(f, "session"),
NamespaceIsolation::Agent => write!(f, "agent"),
NamespaceIsolation::Shared => write!(f, "shared"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MemoryAccessDecision {
Allow,
Deny { reason: String },
RequireApproval { reason: String },
}
impl fmt::Display for MemoryAccessDecision {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MemoryAccessDecision::Allow => write!(f, "allow"),
MemoryAccessDecision::Deny { reason } => write!(f, "deny: {reason}"),
MemoryAccessDecision::RequireApproval { reason } => {
write!(f, "require_approval: {reason}")
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct NamespaceSharingRequest {
pub namespace_id: String,
pub requester_agent: String,
pub access_type: NamespaceAccessType,
pub requested_at: String,
#[serde(default)]
pub approved: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub resolved_at: Option<String>,
}
impl NamespaceSharingRequest {
const MAX_NAMESPACE_ID_LEN: usize = 256;
const MAX_REQUESTER_AGENT_LEN: usize = 256;
const MAX_TIMESTAMP_LEN: usize = 64;
pub fn validate(&self) -> Result<(), String> {
if self.namespace_id.len() > Self::MAX_NAMESPACE_ID_LEN {
return Err(format!(
"NamespaceSharingRequest namespace_id length {} exceeds max {}",
self.namespace_id.len(),
Self::MAX_NAMESPACE_ID_LEN,
));
}
if self.requester_agent.len() > Self::MAX_REQUESTER_AGENT_LEN {
return Err(format!(
"NamespaceSharingRequest requester_agent length {} exceeds max {}",
self.requester_agent.len(),
Self::MAX_REQUESTER_AGENT_LEN,
));
}
if self.requested_at.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"NamespaceSharingRequest requested_at length {} exceeds max {}",
self.requested_at.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
if let Some(ref ra) = self.resolved_at {
if ra.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"NamespaceSharingRequest resolved_at length {} exceeds max {}",
ra.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
}
for c in self.namespace_id.chars() {
if c.is_control() || crate::core::is_unicode_format_char(c) {
return Err(format!(
"NamespaceSharingRequest namespace_id contains invalid character U+{:04X}",
c as u32,
));
}
}
for c in self.requester_agent.chars() {
if c.is_control() || crate::core::is_unicode_format_char(c) {
return Err(format!(
"NamespaceSharingRequest requester_agent contains invalid character U+{:04X}",
c as u32,
));
}
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum NamespaceAccessType {
Read,
Write,
Full,
}
impl fmt::Display for NamespaceAccessType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NamespaceAccessType::Read => write!(f, "read"),
NamespaceAccessType::Write => write!(f, "write"),
NamespaceAccessType::Full => write!(f, "full"),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct MemorySecurityStats {
pub total_entries: u64,
pub quarantined_entries: u64,
pub provenance_nodes: u64,
pub namespaces: u64,
pub injections_detected: u64,
pub cross_session_blocked: u64,
pub low_trust_denials: u64,
pub pending_shares: u64,
}
#[cfg(test)]
mod tests {
use super::*;
fn valid_entry() -> MemoryEntry {
MemoryEntry {
id: "entry-1".to_string(),
fingerprint: "abc123".to_string(),
preview: "hello world".to_string(),
recorded_at: "2025-01-15T10:00:00Z".to_string(),
last_accessed: None,
access_count: 0,
taint_labels: vec![],
trust_score: 0.8,
provenance_id: None,
quarantined: false,
namespace: None,
session_id: None,
agent_id: None,
content_hash: "deadbeef".to_string(),
}
}
#[test]
fn test_memory_entry_validate_valid_baseline_ok() {
assert!(valid_entry().validate().is_ok());
}
#[test]
fn test_memory_entry_validate_trust_score_nan_rejected() {
let mut e = valid_entry();
e.trust_score = f64::NAN;
let err = e.validate().unwrap_err();
assert!(err.contains("not finite"), "got: {err}");
}
#[test]
fn test_memory_entry_validate_trust_score_infinity_rejected() {
let mut e = valid_entry();
e.trust_score = f64::INFINITY;
let err = e.validate().unwrap_err();
assert!(err.contains("not finite"), "got: {err}");
}
#[test]
fn test_memory_entry_validate_trust_score_neg_infinity_rejected() {
let mut e = valid_entry();
e.trust_score = f64::NEG_INFINITY;
let err = e.validate().unwrap_err();
assert!(err.contains("not finite"), "got: {err}");
}
#[test]
fn test_memory_entry_validate_trust_score_negative_rejected() {
let mut e = valid_entry();
e.trust_score = -0.1;
let err = e.validate().unwrap_err();
assert!(err.contains("[0.0, 1.0]"), "got: {err}");
}
#[test]
fn test_memory_entry_validate_trust_score_above_one_rejected() {
let mut e = valid_entry();
e.trust_score = 1.01;
let err = e.validate().unwrap_err();
assert!(err.contains("[0.0, 1.0]"), "got: {err}");
}
#[test]
fn test_memory_entry_validate_trust_score_boundaries_ok() {
let mut e = valid_entry();
e.trust_score = 0.0;
assert!(e.validate().is_ok());
e.trust_score = 1.0;
assert!(e.validate().is_ok());
}
#[test]
fn test_memory_entry_validate_security_taint_with_perfect_trust_rejected() {
let mut e = valid_entry();
e.trust_score = 1.0;
e.taint_labels = vec![TaintLabel::Untrusted];
let err = e.validate().unwrap_err();
assert!(err.contains("security-relevant taint"), "got: {err}");
}
#[test]
fn test_memory_entry_validate_security_taint_with_low_trust_ok() {
let mut e = valid_entry();
e.trust_score = 0.5;
e.taint_labels = vec![TaintLabel::Untrusted];
assert!(e.validate().is_ok());
}
#[test]
fn test_memory_entry_validate_non_security_taint_with_perfect_trust_ok() {
let mut e = valid_entry();
e.trust_score = 1.0;
e.taint_labels = vec![TaintLabel::Sanitized, TaintLabel::Sensitive];
assert!(e.validate().is_ok());
}
#[test]
fn test_memory_entry_validate_all_security_taints_rejected_at_1() {
for label in &[
TaintLabel::Untrusted,
TaintLabel::Quarantined,
TaintLabel::IntegrityFailed,
TaintLabel::MixedProvenance,
TaintLabel::Replayed,
] {
let mut e = valid_entry();
e.trust_score = 1.0;
e.taint_labels = vec![*label];
assert!(e.validate().is_err(), "Expected error for {label:?}");
}
}
#[test]
fn test_memory_entry_validate_taint_labels_over_max_rejected() {
let mut e = valid_entry();
e.taint_labels = vec![TaintLabel::Sanitized; MAX_TAINT_LABELS + 1];
let err = e.validate().unwrap_err();
assert!(err.contains("taint_labels"), "got: {err}");
}
#[test]
fn test_memory_entry_validate_taint_labels_at_max_ok() {
let mut e = valid_entry();
e.taint_labels = vec![TaintLabel::Sanitized; MAX_TAINT_LABELS];
assert!(e.validate().is_ok());
}
#[test]
fn test_memory_entry_validate_id_too_long_rejected() {
let mut e = valid_entry();
e.id = "x".repeat(MemoryEntry::MAX_ID_LEN + 1);
assert!(e.validate().unwrap_err().contains("id length"));
}
#[test]
fn test_memory_entry_validate_fingerprint_too_long_rejected() {
let mut e = valid_entry();
e.fingerprint = "a".repeat(MemoryEntry::MAX_FINGERPRINT_LEN + 1);
assert!(e.validate().unwrap_err().contains("fingerprint length"));
}
#[test]
fn test_memory_entry_validate_content_hash_too_long_rejected() {
let mut e = valid_entry();
e.content_hash = "b".repeat(MemoryEntry::MAX_CONTENT_HASH_LEN + 1);
assert!(e.validate().unwrap_err().contains("content_hash length"));
}
#[test]
fn test_memory_entry_validate_preview_too_long_rejected() {
let mut e = valid_entry();
e.preview = "p".repeat(MemoryEntry::MAX_PREVIEW_LENGTH + 4);
assert!(e.validate().unwrap_err().contains("preview length"));
}
#[test]
fn test_memory_entry_validate_id_control_char_rejected() {
let mut e = valid_entry();
e.id = "entry\x00bad".to_string();
let err = e.validate().unwrap_err();
assert!(err.contains("control or format"), "got: {err}");
}
#[test]
fn test_memory_entry_validate_fingerprint_control_char_rejected() {
let mut e = valid_entry();
e.fingerprint = "fp\x07bell".to_string();
let err = e.validate().unwrap_err();
assert!(err.contains("control or format"), "got: {err}");
}
#[test]
fn test_memory_entry_validate_optional_fields_control_chars_rejected() {
let mut e = valid_entry();
e.provenance_id = Some("prov\x01id".to_string());
assert!(e.validate().unwrap_err().contains("control or format"));
let mut e2 = valid_entry();
e2.namespace = Some("ns\x08bad".to_string());
assert!(e2.validate().unwrap_err().contains("control or format"));
let mut e3 = valid_entry();
e3.session_id = Some("sid\x0Cff".to_string());
assert!(e3.validate().unwrap_err().contains("control or format"));
let mut e4 = valid_entry();
e4.agent_id = Some("aid\x7F del".to_string());
assert!(e4.validate().unwrap_err().contains("control or format"));
}
#[test]
fn test_memory_entry_new_short_content_no_truncation() {
let entry = MemoryEntry::new(
"id1".into(),
"fp".into(),
"short",
"hash".into(),
"2025-01-01T00:00:00Z".into(),
);
assert_eq!(entry.preview, "short");
assert_eq!(entry.trust_score, 0.5);
assert!(entry.has_taint(TaintLabel::Untrusted));
assert!(entry.validate().is_ok());
}
#[test]
fn test_memory_entry_new_long_content_truncated() {
let content = "a".repeat(200);
let entry = MemoryEntry::new(
"id2".into(),
"fp2".into(),
&content,
"hash2".into(),
"2025-06-01T12:00:00Z".into(),
);
assert!(entry.preview.ends_with("..."));
assert!(entry.preview.len() <= MemoryEntry::MAX_PREVIEW_LENGTH + 3);
}
#[test]
fn test_memory_entry_add_taint_duplicate_returns_false() {
let mut e = valid_entry();
e.taint_labels = vec![TaintLabel::Sensitive];
assert!(!e.add_taint(TaintLabel::Sensitive));
}
#[test]
fn test_memory_entry_add_taint_at_capacity_returns_false() {
let mut e = valid_entry();
e.taint_labels = vec![TaintLabel::Sanitized; MAX_TAINT_LABELS];
assert!(!e.add_taint(TaintLabel::Untrusted));
}
#[test]
fn test_memory_entry_is_blocked_quarantined_flag() {
let mut e = valid_entry();
e.quarantined = true;
assert!(e.is_blocked());
}
#[test]
fn test_memory_entry_is_blocked_quarantined_taint() {
let mut e = valid_entry();
e.taint_labels = vec![TaintLabel::Quarantined];
assert!(e.is_blocked());
}
#[test]
fn test_memory_entry_decayed_trust_score_fail_closed_on_bad_timestamp() {
let mut e = valid_entry();
e.recorded_at = "not-a-timestamp".to_string();
let score = e.decayed_trust_score(0.1, "2025-01-15T10:00:00Z");
assert_eq!(score, 0.0);
}
#[test]
fn test_provenance_node_validate_valid_ok() {
let node = ProvenanceNode::new(
"node-1".into(),
ProvenanceEventType::ToolResponse,
"my-tool".into(),
"abc123".into(),
"2025-01-01T00:00:00Z".into(),
);
assert!(node.validate().is_ok());
}
#[test]
fn test_provenance_node_validate_too_many_parents_rejected() {
let mut node = ProvenanceNode::new(
"n1".into(),
ProvenanceEventType::Derivation,
"src".into(),
"hash".into(),
"2025-01-01T00:00:00Z".into(),
);
node.parents = vec!["p".to_string(); ProvenanceNode::MAX_PARENTS + 1];
assert!(node.validate().unwrap_err().contains("parents"));
}
#[test]
fn test_provenance_node_validate_too_many_metadata_rejected() {
let mut node = ProvenanceNode::new(
"n2".into(),
ProvenanceEventType::ToolResponse,
"src".into(),
"hash".into(),
"2025-01-01T00:00:00Z".into(),
);
for i in 0..=ProvenanceNode::MAX_METADATA_ENTRIES {
node.metadata.insert(format!("key-{i}"), "val".into());
}
assert!(node.validate().unwrap_err().contains("metadata"));
}
#[test]
fn test_provenance_node_validate_id_too_long_rejected() {
let mut node = ProvenanceNode::new(
"x".repeat(ProvenanceNode::MAX_ID_LEN + 1),
ProvenanceEventType::ToolResponse,
"src".into(),
"hash".into(),
"2025-01-01T00:00:00Z".into(),
);
assert!(node.validate().unwrap_err().contains("id length"));
node.id = "ok".into();
node.source = "s".repeat(ProvenanceNode::MAX_SOURCE_LEN + 1);
assert!(node.validate().unwrap_err().contains("source length"));
}
#[test]
fn test_quarantine_entry_validate_valid_ok() {
let qe = QuarantineEntry::new(
"e1".into(),
QuarantineDetection::InjectionPattern,
"2025-01-01T00:00:00Z".into(),
);
assert!(qe.validate().is_ok());
}
#[test]
fn test_quarantine_entry_validate_entry_id_too_long_rejected() {
let qe = QuarantineEntry::new(
"x".repeat(257),
QuarantineDetection::LowTrust,
"2025-01-01T00:00:00Z".into(),
);
assert!(qe.validate().unwrap_err().contains("entry_id length"));
}
#[test]
fn test_quarantine_entry_validate_description_too_long_rejected() {
let mut qe = QuarantineEntry::new(
"e2".into(),
QuarantineDetection::ManualQuarantine,
"2025-01-01T00:00:00Z".into(),
);
qe.description = Some("d".repeat(4097));
assert!(qe.validate().unwrap_err().contains("description length"));
}
#[test]
fn test_memory_namespace_validate_valid_ok() {
let ns = MemoryNamespace::new(
"ns-1".into(),
"agent-a".into(),
"2025-01-01T00:00:00Z".into(),
);
assert!(ns.validate().is_ok());
assert!(ns.can_read("agent-a"));
assert!(ns.can_write("agent-a"));
assert!(!ns.can_read("agent-b"));
}
#[test]
fn test_memory_namespace_validate_read_allowed_overflow_rejected() {
let mut ns =
MemoryNamespace::new("ns-2".into(), "owner".into(), "2025-01-01T00:00:00Z".into());
ns.read_allowed = vec!["a".to_string(); MemoryNamespace::MAX_ACL_ENTRIES + 1];
assert!(ns.validate().unwrap_err().contains("read_allowed"));
}
#[test]
fn test_namespace_sharing_request_validate_control_chars_rejected() {
let req = NamespaceSharingRequest {
namespace_id: "ns\x00id".to_string(),
requester_agent: "agent".to_string(),
access_type: NamespaceAccessType::Read,
requested_at: "2025-01-01T00:00:00Z".to_string(),
approved: None,
resolved_at: None,
};
assert!(req.validate().unwrap_err().contains("invalid character"));
}
#[test]
fn test_namespace_sharing_request_validate_requester_agent_too_long_rejected() {
let req = NamespaceSharingRequest {
namespace_id: "ns-ok".to_string(),
requester_agent: "r".repeat(257),
access_type: NamespaceAccessType::Write,
requested_at: "2025-01-01T00:00:00Z".to_string(),
approved: None,
resolved_at: None,
};
assert!(req
.validate()
.unwrap_err()
.contains("requester_agent length"));
}
}