use super::{CompactedCheckpoint, ContextEntry, ContextError, SessionContextSnapshot};
use crate::{
memory::{
ActivatedMemory, MemoryActivationProvenance, MemoryActivationReason, MemoryActivationScore,
MemoryEvidence, MemoryId, MemoryScope,
},
text,
token_estimate::estimate_text_tokens,
};
use std::{
cmp::Ordering,
collections::{BTreeMap, btree_map::Entry},
};
fn format_memory_scope(scope: MemoryScope) -> &'static str {
match scope {
MemoryScope::Session => "session",
MemoryScope::Task => "task",
MemoryScope::Step => "step",
}
}
fn format_memory_scopes(scopes: &[MemoryScope]) -> String {
scopes
.iter()
.map(|scope| format_memory_scope(*scope))
.collect::<Vec<_>>()
.join(",")
}
fn canonicalize_memory_reason_text(value: &str) -> String {
text::collapse_whitespace(value).to_lowercase()
}
#[derive(Debug, Default)]
pub struct ContextCompiler;
impl ContextCompiler {
#[must_use]
pub fn new() -> Self {
Self
}
pub fn compile(
&self,
snapshot: &SessionContextSnapshot,
) -> Result<CompiledContext, ContextError> {
compile_entries(
snapshot.entries(),
snapshot.memories(),
snapshot.compacted_checkpoint(),
)
}
pub(crate) fn compile_without_compacted_checkpoint(
&self,
snapshot: &SessionContextSnapshot,
) -> Result<CompiledContext, ContextError> {
compile_entries(snapshot.entries(), snapshot.memories(), None)
}
}
fn compile_entries(
entries: &[ContextEntry],
memories: &[ActivatedMemory],
compacted_checkpoint: Option<&CompactedCheckpoint>,
) -> Result<CompiledContext, ContextError> {
let mut sections = Vec::with_capacity(entries.len());
for entry in entries {
match entry {
ContextEntry::Summary(summary) => {
sections.push(CompiledContextSection::Summary {
id: summary.id().to_owned(),
text: summary.text().to_owned(),
});
}
}
}
sections.sort_by(|left, right| left.sort_key().cmp(&right.sort_key()));
for memory in memories {
if memory.item().evidence().is_empty() {
return Err(ContextError::MemoryWithoutEvidence {
memory_id: memory.item().id().as_str().to_owned(),
});
}
}
let mut memory_projection = canonical_memory_projection(memories);
memory_projection.sort_by(|left, right| {
right
.score
.cmp(&left.score)
.then_with(|| left.id.cmp(&right.id))
});
Ok(CompiledContext {
sections,
memory_projection,
checkpoint: ContextCheckpointSegment::new(compacted_checkpoint.cloned()),
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompiledContext {
sections: Vec<CompiledContextSection>,
memory_projection: Vec<CompiledMemory>,
checkpoint: ContextCheckpointSegment,
}
impl CompiledContext {
#[must_use]
pub fn sections(&self) -> &[CompiledContextSection] {
&self.sections
}
#[must_use]
pub fn to_snapshot(&self) -> String {
let mut lines = Vec::new();
self.checkpoint.append_prompt_lines(&mut lines);
self.append_body_prompt_lines(&mut lines);
lines.join("\n")
}
pub(crate) fn checkpoint_snapshot(&self) -> String {
let mut lines = Vec::new();
self.checkpoint.append_prompt_lines(&mut lines);
lines.join("\n")
}
pub(crate) fn body_snapshot(&self) -> String {
let mut lines = Vec::new();
self.append_body_prompt_lines(&mut lines);
lines.join("\n")
}
fn append_body_prompt_lines(&self, lines: &mut Vec<String>) {
for section in &self.sections {
match section {
CompiledContextSection::Summary { id, text } => {
lines.push(format!("summary:{id}"));
lines.push(format!("text:{text}"));
}
}
}
for memory in &self.memory_projection {
lines.push(format!("memory:{}", memory.id));
lines.push(format!(
"memory-scope:{}",
format_memory_scope(memory.scope)
));
lines.push(format!("memory-text:{}", memory.text));
lines.push(format!(
"memory-activation-source-kind:{}",
memory.provenance.source_kind().as_str()
));
lines.push(format!(
"memory-activation-source-label:{}",
memory.provenance.source_label()
));
lines.push(format!(
"memory-activation-query:{}",
memory.provenance.canonical_query()
));
lines.push(format!(
"memory-activation-allowed-scopes:{}",
format_memory_scopes(memory.provenance.allowed_scopes())
));
for item in &memory.evidence {
lines.push(format!("memory-evidence:{}:{}", item.label, item.source));
}
for reason in &memory.reasons {
match reason {
CompiledMemoryReason::ScopeAllowed => {
lines.push("memory-reason:scope_allowed".to_owned());
}
CompiledMemoryReason::TriggerMatched(trigger) => {
lines.push(format!("memory-reason:trigger:{trigger}"));
}
CompiledMemoryReason::Ranked { score } => {
lines.push(format!(
"memory-reason:rank:matches={};priority={};confidence={:.3}",
score.trigger_matches(),
score.priority(),
score.confidence().as_f32()
));
}
CompiledMemoryReason::ConflictWinner { suppressed } => {
lines.push(format!(
"memory-reason:conflict_winner:suppressed={}",
suppressed
.iter()
.map(MemoryId::as_str)
.collect::<Vec<_>>()
.join(",")
));
}
}
}
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct ContextCheckpointSegment {
checkpoint: Option<CompactedCheckpoint>,
}
const COMPACTED_CHECKPOINT_HEADER: &str = "compacted-checkpoint:";
const COMPACTED_CHECKPOINT_GUIDANCE: &str = "guidance:This summary is the record of the turns that were removed. Continue from it, and carry it into the next compaction. Re-read workspace files before editing code. A compaction segment named in the summary is the verbatim transcript.";
const COMPACTED_CHECKPOINT_TEXT_PREFIX: &str = "text:";
pub(crate) fn compacted_checkpoint_wrapper_token_ceiling() -> u64 {
let wrapper = format!(
"{COMPACTED_CHECKPOINT_HEADER}\n{COMPACTED_CHECKPOINT_GUIDANCE}\n{COMPACTED_CHECKPOINT_TEXT_PREFIX}"
);
estimate_text_tokens(&wrapper).saturating_add(1)
}
impl ContextCheckpointSegment {
fn new(checkpoint: Option<CompactedCheckpoint>) -> Self {
Self { checkpoint }
}
fn append_prompt_lines(&self, lines: &mut Vec<String>) {
if let Some(checkpoint) = &self.checkpoint {
lines.push(COMPACTED_CHECKPOINT_HEADER.to_owned());
lines.push(COMPACTED_CHECKPOINT_GUIDANCE.to_owned());
lines.push(format!(
"{COMPACTED_CHECKPOINT_TEXT_PREFIX}{}",
checkpoint.text()
));
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompiledContextSection {
Summary {
id: String,
text: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct CompiledMemory {
id: MemoryId,
scope: MemoryScope,
text: String,
evidence: Vec<CompiledMemoryEvidence>,
score: MemoryActivationScore,
provenance: MemoryActivationProvenance,
reasons: Vec<CompiledMemoryReason>,
}
impl CompiledMemory {
fn from_activation(memory: &ActivatedMemory) -> Self {
let mut evidence = memory
.item()
.evidence()
.iter()
.map(CompiledMemoryEvidence::from_evidence)
.collect::<Vec<_>>();
evidence.sort();
evidence.dedup();
let mut reasons = memory
.reasons()
.iter()
.map(CompiledMemoryReason::from_reason)
.collect::<Vec<_>>();
reasons.sort();
reasons.dedup();
Self {
id: memory.item().id().clone(),
scope: memory.item().scope(),
text: memory.item().text().to_owned(),
evidence,
score: memory.score(),
provenance: memory.provenance().clone(),
reasons,
}
}
}
fn canonical_memory_projection(memories: &[ActivatedMemory]) -> Vec<CompiledMemory> {
let mut by_id = BTreeMap::<MemoryId, CompiledMemory>::new();
for memory in memories.iter().map(CompiledMemory::from_activation) {
match by_id.entry(memory.id.clone()) {
Entry::Occupied(mut entry) => {
if memory.canonical_key() < entry.get().canonical_key() {
entry.insert(memory);
}
}
Entry::Vacant(entry) => {
entry.insert(memory);
}
}
}
by_id.into_values().collect()
}
impl CompiledMemory {
fn canonical_key(&self) -> CompiledMemoryCanonicalKey<'_> {
(
std::cmp::Reverse(self.score),
self.scope,
self.text.as_str(),
self.evidence.as_slice(),
&self.provenance,
self.reasons.as_slice(),
)
}
}
type CompiledMemoryCanonicalKey<'a> = (
std::cmp::Reverse<MemoryActivationScore>,
MemoryScope,
&'a str,
&'a [CompiledMemoryEvidence],
&'a MemoryActivationProvenance,
&'a [CompiledMemoryReason],
);
#[derive(Debug, Clone, PartialEq, Eq)]
struct CompiledMemoryEvidence {
label: String,
source: String,
}
impl CompiledMemoryEvidence {
fn from_evidence(evidence: &MemoryEvidence) -> Self {
Self {
label: evidence.label().to_owned(),
source: evidence.source().to_owned(),
}
}
fn sort_key(&self) -> (&str, &str) {
(self.source.as_str(), self.label.as_str())
}
}
impl Ord for CompiledMemoryEvidence {
fn cmp(&self, other: &Self) -> Ordering {
self.sort_key().cmp(&other.sort_key())
}
}
impl PartialOrd for CompiledMemoryEvidence {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum CompiledMemoryReason {
ScopeAllowed,
TriggerMatched(String),
Ranked { score: MemoryActivationScore },
ConflictWinner { suppressed: Vec<MemoryId> },
}
impl CompiledMemoryReason {
fn from_reason(reason: &MemoryActivationReason) -> Self {
match reason {
MemoryActivationReason::ScopeAllowed => Self::ScopeAllowed,
MemoryActivationReason::TriggerMatched(trigger) => {
Self::TriggerMatched(canonicalize_memory_reason_text(trigger))
}
MemoryActivationReason::Ranked { score } => Self::Ranked { score: *score },
MemoryActivationReason::ConflictWinner { suppressed } => {
let mut suppressed = suppressed.clone();
suppressed.sort();
suppressed.dedup();
Self::ConflictWinner { suppressed }
}
}
}
}
impl CompiledContextSection {
fn sort_key(&self) -> (&str, &str) {
match self {
Self::Summary { id, .. } => ("summary", id.as_str()),
}
}
}