use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use aither_core::llm::{Classify, Event, Failure, LLMRequest, Message, Role};
use aither_core::{LanguageModel, llm::GenerateError};
use askama::Template;
use futures_lite::StreamExt as _;
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sync_wrapper::SyncWrapper;
pub trait HandoffSchema: JsonSchema + DeserializeOwned + Send + 'static {
fn instruction(prompt: &HandoffPrompt<'_>) -> String;
fn retry(reason: &str) -> String;
fn validate(&self) -> Result<(), InvalidHandoff> {
Ok(())
}
fn render(&self, file_index: &[String]) -> String;
fn into_restoration(self) -> Restoration;
}
#[derive(Debug, Clone, Copy)]
pub struct HandoffPrompt<'a> {
pub focus: Option<&'a str>,
pub skills: &'a [SkillCatalogEntry],
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Restoration {
pub context_files: Vec<ContextFile>,
pub skills: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{reason}")]
pub struct InvalidHandoff {
pub reason: String,
}
impl InvalidHandoff {
pub fn new(reason: impl Into<String>) -> Self {
Self {
reason: reason.into(),
}
}
}
impl From<InvalidHandoff> for CompactionError {
fn from(error: InvalidHandoff) -> Self {
Self::Invalid {
reason: error.reason,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct HandoffDocument {
pub current_task: String,
pub key_decisions: Vec<String>,
#[serde(default)]
pub context_files: Vec<ContextFile>,
pub errors_fixes: Vec<String>,
pub pending_work: Vec<String>,
pub recovery_hints: Vec<String>,
#[serde(default)]
pub skills: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct ContextFile {
pub path: String,
pub lines: Option<LineRange>,
pub why: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct LineRange {
pub start: u32,
pub end: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SkillCatalogEntry {
pub name: String,
pub description: String,
pub instructions: String,
}
#[derive(Template)]
#[template(path = "handoff_document.md", escape = "none")]
struct HandoffDocumentTemplate<'a> {
handoff: &'a HandoffDocument,
file_index: &'a [String],
}
#[derive(Template)]
#[template(path = "compaction_instruction.txt", escape = "none")]
struct CompactionInstruction<'a> {
focus: Option<&'a str>,
skills: &'a [SkillCatalogEntry],
}
#[derive(Template)]
#[template(path = "compaction_retry.txt", escape = "none")]
struct CompactionRetry<'a> {
reason: &'a str,
}
impl HandoffSchema for HandoffDocument {
fn instruction(prompt: &HandoffPrompt<'_>) -> String {
CompactionInstruction {
focus: prompt.focus,
skills: prompt.skills,
}
.render()
.expect("compaction instruction template must render")
}
fn retry(reason: &str) -> String {
CompactionRetry { reason }
.render()
.expect("compaction retry template must render")
}
fn validate(&self) -> Result<(), InvalidHandoff> {
if self.current_task.trim().is_empty() {
return Err(InvalidHandoff::new("current_task must not be empty"));
}
Ok(())
}
fn render(&self, file_index: &[String]) -> String {
HandoffDocumentTemplate {
handoff: self,
file_index,
}
.render()
.expect("handoff document template must render")
}
fn into_restoration(self) -> Restoration {
Restoration {
context_files: self.context_files,
skills: self.skills,
}
}
}
#[derive(Debug, Clone, thiserror::Error)]
pub enum CompactionError {
#[error("compaction request failed: {message}")]
Provider {
message: String,
failure: Failure,
},
#[error("compaction produced an invalid handoff document: {reason}")]
Invalid {
reason: String,
},
#[error("compaction found no conversation to summarize")]
Empty,
}
const INDEX_SEPARATOR: &str = " — ";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Handoff {
text: String,
file_index: Vec<String>,
}
impl Handoff {
#[must_use]
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
file_index: Vec::new(),
}
}
#[must_use]
pub const fn with_file_index(text: String, file_index: Vec<String>) -> Self {
Self { text, file_index }
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[must_use]
pub fn file_index(&self) -> &[String] {
&self.file_index
}
}
#[must_use]
pub fn build_file_index(
previous: &[String],
messages: &[Message],
outputs_dir: Option<&std::path::Path>,
) -> Vec<String> {
use std::collections::HashMap;
let mut call_description: HashMap<&str, &str> = HashMap::new();
for message in messages {
for call in message.tool_calls() {
if let Some(description) = call.arguments.get("description").and_then(|v| v.as_str()) {
call_description.insert(call.id.as_str(), description);
}
}
}
let mut index: indexmap::IndexMap<String, Option<String>> = indexmap::IndexMap::new();
for entry in previous {
match entry.split_once(INDEX_SEPARATOR) {
Some((path, description)) => {
index.insert(path.to_string(), Some(description.to_string()));
}
None => {
index.insert(entry.clone(), None);
}
}
}
for message in messages {
let Message::Tool {
content,
tool_call_id,
} = message
else {
continue;
};
let description = call_description
.get(tool_call_id.as_str())
.map(|description| (*description).to_string());
for url in crate::output_urls::extract_referenced_urls(content, outputs_dir) {
index.insert(url, description.clone());
}
for word in content.split_whitespace() {
let word = word.trim_matches(|c: char| {
c == '"' || c == '\'' || c == '`' || c == ',' || c == ')' || c == ']' || c == ';'
});
if word.starts_with("artifacts/") && word.len() > "artifacts/".len() {
index.insert(word.to_string(), description.clone());
}
}
}
index
.into_iter()
.map(|(path, description)| match description {
Some(description) => format!("{path}{INDEX_SEPARATOR}{description}"),
None => path,
})
.collect()
}
pub struct CompactionFork<D> {
fork_point: u64,
previous_file_index: Vec<String>,
outputs_dir: Option<std::path::PathBuf>,
request: LLMRequest,
schema: PhantomData<fn() -> D>,
}
impl<D> std::fmt::Debug for CompactionFork<D> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("CompactionFork")
.field("fork_point", &self.fork_point)
.field("schema", &std::any::type_name::<D>())
.finish_non_exhaustive()
}
}
impl<D: HandoffSchema> CompactionFork<D> {
pub fn new(
request: &LLMRequest,
fork_point: u64,
previous_file_index: &[String],
focus: Option<&str>,
skills: &[SkillCatalogEntry],
) -> Result<Self, CompactionError> {
if request
.messages()
.iter()
.all(|message| message.role() == Role::System)
{
return Err(CompactionError::Empty);
}
let focus = focus.map(str::trim).filter(|focus| !focus.is_empty());
let instruction = D::instruction(&HandoffPrompt { focus, skills });
let mut messages = request.messages().to_vec();
messages.push(Message::user(instruction));
Ok(Self {
fork_point,
previous_file_index: previous_file_index.to_vec(),
outputs_dir: None,
request: LLMRequest::new(messages)
.with_parameters(request.parameters().clone())
.with_tool_definitions(request.tool_definitions().to_vec()),
schema: PhantomData,
})
}
#[must_use]
pub fn with_outputs_dir(mut self, outputs_dir: Option<std::path::PathBuf>) -> Self {
self.outputs_dir = outputs_dir;
self
}
#[must_use]
pub const fn fork_point(&self) -> u64 {
self.fork_point
}
#[must_use]
pub const fn request(&self) -> &LLMRequest {
&self.request
}
pub async fn run<M: LanguageModel>(
self,
model: M,
) -> Result<CompletedCompaction, CompactionError> {
let Self {
fork_point,
request,
previous_file_index,
outputs_dir,
..
} = self;
let file_index = build_file_index(
&previous_file_index,
request.messages(),
outputs_dir.as_deref(),
);
let reason = match model.generate::<D>(request.clone()).await {
Ok(document) => match document.validate() {
Ok(()) => return Ok(CompletedCompaction::new(fork_point, document, file_index)),
Err(error) => error.reason,
},
Err(GenerateError::Provider(error)) => {
return Err(CompactionError::Provider {
message: error.to_string(),
failure: error.failure(),
});
}
Err(error @ GenerateError::Parse { .. }) => error.to_string(),
};
tracing::warn!(reason = %reason, "retrying compaction with a corrective prompt");
let (mut messages, parameters, definitions) = request.into_parts();
messages.push(Message::user(D::retry(&reason)));
let retry_request = LLMRequest::new(messages)
.with_parameters(parameters)
.with_tool_definitions(definitions);
let response = collect_text(&model, retry_request).await?;
let document = parse_handoff::<D>(&response)?;
document.validate()?;
Ok(CompletedCompaction::new(fork_point, document, file_index))
}
}
impl CompletedCompaction {
pub(crate) fn new<D: HandoffSchema>(
fork_point: u64,
document: D,
file_index: Vec<String>,
) -> Self {
let text = document.render(&file_index);
Self {
fork_point,
handoff: Handoff::with_file_index(text, file_index),
restoration: document.into_restoration(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompletedCompaction {
pub fork_point: u64,
pub handoff: Handoff,
pub restoration: Restoration,
}
type CompactionFuture =
Pin<Box<dyn Future<Output = Result<CompletedCompaction, CompactionError>> + Send>>;
pub struct PendingCompaction {
fork_point: u64,
future: SyncWrapper<CompactionFuture>,
}
impl std::fmt::Debug for PendingCompaction {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("PendingCompaction")
.field("fork_point", &self.fork_point)
.finish_non_exhaustive()
}
}
impl PendingCompaction {
#[must_use]
pub fn start<D: HandoffSchema, M: LanguageModel + 'static>(
fork: CompactionFork<D>,
model: M,
) -> Self {
let fork_point = fork.fork_point();
Self {
fork_point,
future: SyncWrapper::new(Box::pin(fork.run(model))),
}
}
#[must_use]
pub const fn fork_point(&self) -> u64 {
self.fork_point
}
pub fn wait(
&mut self,
) -> impl Future<Output = Result<CompletedCompaction, CompactionError>> + '_ {
self.future.get_mut()
}
}
fn parse_handoff<D: DeserializeOwned>(response: &str) -> Result<D, CompactionError> {
let trimmed = response.trim();
if trimmed.is_empty() {
return Err(CompactionError::Invalid {
reason: "the model returned an empty response".to_string(),
});
}
let json = match (trimmed.find('{'), trimmed.rfind('}')) {
(Some(start), Some(end)) if end > start => &trimmed[start..=end],
_ => trimmed,
};
serde_json::from_str(json).map_err(|error| CompactionError::Invalid {
reason: error.to_string(),
})
}
async fn collect_text<M: LanguageModel>(
model: &M,
request: LLMRequest,
) -> Result<String, CompactionError> {
let stream = model.respond(request);
futures_lite::pin!(stream);
let mut text = String::new();
while let Some(event) = stream.next().await {
match event {
Ok(Event::Text(chunk)) => text.push_str(&chunk),
Ok(_) => {}
Err(error) => {
return Err(CompactionError::Provider {
message: error.to_string(),
failure: error.failure(),
});
}
}
}
Ok(text)
}
#[cfg(test)]
mod tests {
use super::*;
use aither_core::llm::ToolCall;
#[test]
fn build_file_index_pairs_outputs_with_call_descriptions() {
let messages = vec![
Message::assistant_with_tool_calls(
"",
vec![ToolCall::new(
"call_1",
"terminal",
serde_json::json!({
"description": "Fetch WebGPU status pages",
"script": "webfetch https://caniuse.com"
}),
)],
),
Message::tool(
"call_1",
"Output saved to outputs/amber-oak-swift-river.txt (100 lines, 5000 bytes)",
),
Message::assistant_with_tool_calls(
"",
vec![ToolCall::new(
"call_2",
"terminal",
serde_json::json!({"description": "Write deck", "script": "tee artifacts/slides.md"}),
)],
),
Message::tool("call_2", "wrote artifacts/slides.md"),
];
let index = build_file_index(&[], &messages, None);
assert_eq!(index.len(), 2);
assert!(
index[0].contains("outputs/amber-oak-swift-river.txt")
&& index[0].contains("Fetch WebGPU status pages"),
"index entry should pair path with description: {index:?}"
);
assert!(index[1].contains("artifacts/slides.md") && index[1].contains("Write deck"));
}
#[test]
fn build_file_index_finds_the_absolute_path_a_saved_output_notice_prints() {
let dir = std::path::Path::new("/work/session/outputs");
let path = dir.join("amber-oak-swift-river.txt").display().to_string();
let path = path.as_str();
let messages = vec![
Message::assistant_with_tool_calls(
"",
vec![ToolCall::new(
"call_1",
"terminal",
serde_json::json!({"description": "Run the suite"}),
)],
),
Message::tool(
"call_1",
aither_sandbox::output_saved_text("head", path, 100, 5000),
),
];
let index = build_file_index(&[], &messages, Some(dir));
assert_eq!(index.len(), 1, "{index:?}");
assert!(index[0].contains(path) && index[0].contains("Run the suite"));
}
#[test]
fn build_file_index_keeps_latest_description_per_path() {
let messages = vec![
Message::assistant_with_tool_calls(
"",
vec![ToolCall::new(
"call_1",
"terminal",
serde_json::json!({"description": "first pass"}),
)],
),
Message::tool("call_1", "see artifacts/report.md"),
Message::assistant_with_tool_calls(
"",
vec![ToolCall::new(
"call_2",
"terminal",
serde_json::json!({"description": "final revision"}),
)],
),
Message::tool("call_2", "updated artifacts/report.md"),
];
let index = build_file_index(&[], &messages, None);
assert_eq!(index.len(), 1);
assert!(index[0].contains("final revision"));
}
fn read_file_messages(id: &str, description: &str, path: &str) -> Vec<Message> {
vec![
Message::assistant_with_tool_calls(
"",
vec![ToolCall::new(
id,
"terminal",
serde_json::json!({"description": description}),
)],
),
Message::tool(id, format!("saved {path}")),
]
}
fn compact(context: &mut crate::context::Context) -> Vec<String> {
let file_index = build_file_index(
context.handoff_file_index(),
&context.build_request(&[]),
None,
);
context.apply_handoff(
Handoff::with_file_index("task".to_string(), file_index.clone()),
u64::MAX,
0,
);
file_index
}
#[test]
fn the_second_handoff_keeps_files_indexed_by_the_first() {
let mut context = crate::context::Context::new();
for message in read_file_messages("call_1", "Fetch notes", "artifacts/notes.md") {
context.push(message);
}
let first = compact(&mut context);
assert_eq!(first, ["artifacts/notes.md — Fetch notes"]);
for message in read_file_messages("call_2", "Write deck", "artifacts/slides.md") {
context.push(message);
}
let second = compact(&mut context);
assert_eq!(
second,
[
"artifacts/notes.md — Fetch notes",
"artifacts/slides.md — Write deck"
]
);
for message in read_file_messages("call_3", "Revised notes", "artifacts/notes.md") {
context.push(message);
}
let third = compact(&mut context);
assert_eq!(
third,
[
"artifacts/notes.md — Revised notes",
"artifacts/slides.md — Write deck"
]
);
}
#[test]
fn handoff_markdown_renders_file_index_section() {
let handoff = HandoffDocument {
current_task: "task".to_string(),
..HandoffDocument::default()
};
let markdown = handoff.render(&["outputs/a-b-c-d.txt — research notes".to_string()]);
assert!(markdown.contains("Materials Already On Disk"));
assert!(markdown.contains("outputs/a-b-c-d.txt — research notes"));
assert!(markdown.contains("Read the file instead of redoing the work"));
}
#[test]
fn a_handoff_without_a_current_task_is_rejected() {
let error = HandoffDocument::default()
.validate()
.expect_err("empty current_task must not validate");
assert!(error.reason.contains("current_task"));
}
#[test]
fn handoff_parsing_tolerates_fences_and_commentary() {
let response = "Sure! Here you go:\n```json\n{\"current_task\": \"ship it\", \
\"key_decisions\": [], \"files_paths\": [], \"errors_fixes\": [], \
\"pending_work\": [], \"recovery_hints\": []}\n```\nHope that helps.";
let document = parse_handoff::<HandoffDocument>(response).expect("handoff parses");
assert_eq!(document.current_task, "ship it");
}
#[test]
fn empty_responses_are_reported_as_invalid() {
let error =
parse_handoff::<HandoffDocument>(" ").expect_err("empty response must not parse");
assert!(matches!(error, CompactionError::Invalid { .. }));
}
#[test]
fn forking_an_empty_context_is_an_error() {
let request = LLMRequest::new(vec![Message::system("only the prefix")]);
assert!(matches!(
CompactionFork::<HandoffDocument>::new(&request, 0, &[], None, &[]),
Err(CompactionError::Empty)
));
}
#[test]
fn the_fork_reproduces_the_main_request_and_appends_the_instruction() {
use aither_core::llm::model::Parameters;
let parameters = Parameters {
temperature: Some(0.3),
..Parameters::default()
};
let main = LLMRequest::new(vec![
Message::system("be useful"),
Message::system("<tool-outputs>tail block</tool-outputs>"),
Message::user("do the thing"),
])
.with_parameters(parameters);
let skills = [SkillCatalogEntry {
name: "pdf".to_string(),
description: "read PDFs".to_string(),
instructions: "how to read PDFs".to_string(),
}];
let fork = CompactionFork::<HandoffDocument>::new(
&main,
7,
&[],
Some("the failing test"),
&skills,
)
.expect("fork");
assert_eq!(fork.fork_point(), 7);
assert_eq!(
fork.request().parameters().temperature,
main.parameters().temperature,
"the compaction request reuses the main parameters"
);
assert_eq!(
fork.request().tool_definitions().len(),
main.tool_definitions().len(),
"the compaction request carries the same tools"
);
assert_eq!(fork.request().messages().len(), main.messages().len() + 1);
for (index, message) in main.messages().iter().enumerate() {
assert_eq!(
message.content(),
fork.request().messages()[index].content(),
"fork perturbed prefix message {index}"
);
}
let instruction = fork
.request()
.messages()
.last()
.expect("instruction")
.content();
assert!(instruction.contains("Focus the handoff on: the failing test"));
assert!(instruction.contains("pdf"), "the skill catalog is offered");
}
#[derive(Debug)]
struct NoError;
impl std::fmt::Display for NoError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("never fails")
}
}
impl std::error::Error for NoError {}
impl aither_core::llm::Classify for NoError {
fn failure(&self) -> aither_core::llm::Failure {
aither_core::llm::Failure::permanent()
}
}
struct CapturingModel {
seen: std::sync::Mutex<Vec<LLMRequest>>,
}
impl LanguageModel for CapturingModel {
type Error = NoError;
fn respond(
&self,
request: LLMRequest,
) -> impl futures_lite::Stream<Item = Result<Event, Self::Error>> + Send {
self.seen.lock().unwrap().push(request);
futures_lite::stream::iter([Ok(Event::Text(
r#"{"current_task":"keep going","key_decisions":[],"context_files":[],"errors_fixes":[],"pending_work":["continue"],"recovery_hints":[],"skills":[]}"#
.to_string(),
))])
}
fn profile(
&self,
) -> impl std::future::Future<
Output = Result<aither_core::llm::model::Profile, Self::Error>,
> + Send {
std::future::ready(Ok(aither_core::llm::model::Profile::new(
"capture",
"test",
"capture",
"capturing model",
1_000_000,
)))
}
}
#[tokio::test]
async fn the_compaction_request_keeps_the_cache_controls_beside_structured_outputs() {
use aither_core::llm::model::{
ClaudeCacheBreakpointTarget, ClaudePromptCacheStrategy, OpenAIPromptCacheRetention,
Parameters,
};
let messages = vec![
Message::system("prefix"),
Message::user("hello"),
Message::assistant("hi"),
Message::system("live tail"),
];
let parameters = crate::context::PromptCache::new("session-1").apply(
Parameters::default(),
&messages,
false,
false,
);
let main = LLMRequest::new(messages).with_parameters(parameters);
let fork = CompactionFork::<HandoffDocument>::new(&main, 0, &[], None, &[]).expect("fork");
let model = CapturingModel {
seen: std::sync::Mutex::default(),
};
fork.run(&model).await.expect("compaction");
let request = model.seen.lock().unwrap().remove(0);
let parameters = request.parameters();
assert!(parameters.structured_outputs);
let openai = parameters.cache.openai.as_ref().expect("openai cache");
assert_eq!(openai.key.as_deref(), Some("session-1:0"));
assert_eq!(openai.retention, Some(OpenAIPromptCacheRetention::Hours24));
let ClaudePromptCacheStrategy::Explicit(breakpoints) =
parameters.cache.claude.expect("claude cache").strategy
else {
panic!("expected explicit claude breakpoints");
};
assert_eq!(
breakpoints.first.target,
ClaudeCacheBreakpointTarget::LastSystem
);
}
}