use super::ContextError;
use crate::memory::ActivatedMemory;
const FNV_OFFSET_BASIS: u64 = 0xcbf29ce484222325;
const FNV_PRIME: u64 = 0x100000001b3;
fn validate_non_blank(field: &'static str, value: &str) -> Result<(), ContextError> {
if value.trim().is_empty() {
return Err(ContextError::BlankField { field });
}
Ok(())
}
fn sanitize_context_text(field: &'static str, value: String) -> Result<String, ContextError> {
let cleaned = match crate::user_input::sanitize_freeform_text(&value) {
std::borrow::Cow::Borrowed(_) => value,
std::borrow::Cow::Owned(cleaned) => cleaned,
};
validate_non_blank(field, &cleaned)?;
Ok(cleaned)
}
fn validate_no_control_characters(field: &'static str, value: &str) -> Result<(), ContextError> {
if value
.chars()
.any(|character| character.is_control() && character != '\n' && character != '\t')
{
return Err(ContextError::InvalidControlCharacter { field });
}
Ok(())
}
pub(crate) fn stable_content_hash(bytes: &[u8]) -> String {
let mut hash = FNV_OFFSET_BASIS;
for byte in bytes {
hash = (hash ^ u64::from(*byte)).wrapping_mul(FNV_PRIME);
}
format!("fnv1a64:{hash:016x}")
}
#[derive(Debug, Clone)]
pub struct SessionContextSnapshot {
entries: Vec<ContextEntry>,
memories: Vec<ActivatedMemory>,
compacted_checkpoint: Option<CompactedCheckpoint>,
}
impl SessionContextSnapshot {
pub(crate) fn new(
entries: Vec<ContextEntry>,
memories: Vec<ActivatedMemory>,
compacted_checkpoint: Option<CompactedCheckpoint>,
) -> Self {
Self {
entries,
memories,
compacted_checkpoint,
}
}
pub(super) fn entries(&self) -> &[ContextEntry] {
&self.entries
}
pub(super) fn memories(&self) -> &[ActivatedMemory] {
&self.memories
}
pub(super) fn compacted_checkpoint(&self) -> Option<&CompactedCheckpoint> {
self.compacted_checkpoint.as_ref()
}
#[cfg(test)]
pub(crate) fn compacted_checkpoint_for_tests(&self) -> Option<&CompactedCheckpoint> {
self.compacted_checkpoint.as_ref()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContextEntry {
Summary(ContextSummary),
}
impl ContextEntry {
#[must_use]
pub fn summary(summary: ContextSummary) -> Self {
Self::Summary(summary)
}
pub(crate) fn as_summary(&self) -> &ContextSummary {
match self {
Self::Summary(summary) => summary,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContextSummary {
id: String,
text: String,
}
impl ContextSummary {
pub fn new(id: impl Into<String>, text: impl Into<String>) -> Result<Self, ContextError> {
let id = id.into();
validate_non_blank("context summary id", &id)?;
let text = text.into();
validate_non_blank("context summary text", &text)?;
Ok(Self { id, text })
}
#[must_use]
pub fn id(&self) -> &str {
&self.id
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectRules {
source_path: String,
text: String,
content_hash: String,
}
impl ProjectRules {
pub fn new(
source_path: impl Into<String>,
text: impl Into<String>,
) -> Result<Self, ContextError> {
let source_path = source_path.into();
validate_non_blank("project rules source path", &source_path)?;
validate_no_control_characters("project rules source path", &source_path)?;
let text = sanitize_context_text("project rules text", text.into())?;
let content_hash = stable_content_hash(text.as_bytes());
Ok(Self {
source_path,
text,
content_hash,
})
}
#[must_use]
pub fn source_path(&self) -> &str {
&self.source_path
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[must_use]
pub fn content_hash(&self) -> &str {
&self.content_hash
}
#[must_use]
pub fn to_stable_prefix_message_text(&self) -> String {
format!(
"project-rules-source:{}\nproject-rules-content-hash:{}\n{}",
self.source_path, self.content_hash, self.text
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaskAnchor {
objective: String,
}
impl TaskAnchor {
pub fn new(objective: impl Into<String>) -> Result<Self, ContextError> {
let objective = sanitize_context_text("task anchor objective", objective.into())?;
Ok(Self { objective })
}
#[must_use]
pub fn objective(&self) -> &str {
&self.objective
}
pub(crate) fn to_dynamic_control_message_text(&self) -> String {
format!("task-anchor:\n{}", self.objective)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompactedCheckpoint {
text: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CompactedCheckpointSummary;
impl CompactedCheckpoint {
pub fn new(text: impl Into<String>) -> Result<Self, ContextError> {
let text = sanitize_context_text("compacted checkpoint text", text.into())?;
Ok(Self { text })
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[must_use]
pub fn summary(&self) -> CompactedCheckpointSummary {
CompactedCheckpointSummary
}
}