#[cfg(feature = "skills")]
use std::future::Future;
use std::path::PathBuf;
use askama::Template;
use super::source::{Block, ContextSource};
use crate::working_docs::{self, WorkingDocsSnapshot};
#[derive(Template)]
#[template(path = "tasks_document.txt", escape = "none")]
struct TasksDocumentTemplate {
tasks: Option<String>,
recent_changes: Option<String>,
}
#[derive(Template)]
#[template(path = "tasks_stale.txt", escape = "none")]
struct TasksStaleTemplate {
content: String,
}
#[derive(Template)]
#[template(path = "system_reminder.txt", escape = "none")]
struct SystemReminderTemplate {
content: String,
}
fn block(tag: &str, template: &impl Template) -> Block {
Block::new(
tag,
template
.render()
.expect("context block template must render"),
)
}
#[derive(Debug)]
pub struct TasksDocument {
sandbox_dir: PathBuf,
stale_after_turns: usize,
rendered: Option<WorkingDocsSnapshot>,
last_content: Option<String>,
seen: bool,
stale_turns: usize,
alerted: bool,
}
impl TasksDocument {
#[must_use]
pub fn new(sandbox_dir: impl Into<PathBuf>, stale_after_turns: usize) -> Self {
Self {
sandbox_dir: sandbox_dir.into(),
stale_after_turns,
rendered: None,
last_content: None,
seen: false,
stale_turns: 0,
alerted: false,
}
}
}
impl ContextSource for TasksDocument {
async fn render(&self) -> Option<Block> {
let docs = working_docs::read_snapshot(&self.sandbox_dir).await;
let recent_changes = self
.rendered
.as_ref()
.and_then(|previous| working_docs::tasks_md_diff(previous, &docs));
let tasks = docs.tasks_md;
if tasks.is_none() && recent_changes.is_none() {
return None;
}
Some(block(
"tasks_document",
&TasksDocumentTemplate {
tasks,
recent_changes,
},
))
}
async fn commit(&mut self, _block: &Block) {
self.rendered = Some(working_docs::read_snapshot(&self.sandbox_dir).await);
}
async fn on_tool_turn_end(&mut self) -> Option<Block> {
let docs = working_docs::read_snapshot(&self.sandbox_dir).await;
let threshold = self.stale_after_turns;
if !self.seen {
self.seen = true;
self.last_content = docs.tasks_md;
return None;
}
if self.last_content != docs.tasks_md {
self.last_content = docs.tasks_md;
self.stale_turns = 0;
self.alerted = false;
return None;
}
let missing = match &docs.tasks_md {
Some(content) if working_docs::has_unchecked_markdown_tasks(content) => false,
Some(_) => {
self.stale_turns = 0;
self.alerted = false;
return None;
}
None => true,
};
self.stale_turns += 1;
if self.stale_turns < threshold || self.alerted {
return None;
}
self.alerted = true;
let content = if missing {
format!(
"{threshold} tool turns have run without a tasks.md. For multi-step work, create tasks.md in the sandbox root with a checklist of the plan — it is the canonical task document and survives context compaction."
)
} else {
format!(
"tasks.md has unchecked items but has not been updated for {threshold} tool turns. Mark finished items and keep the checklist in sync with actual progress."
)
};
Some(block("tasks_stale", &TasksStaleTemplate { content }))
}
async fn on_final_turn(&mut self) -> Option<Block> {
let docs = working_docs::read_snapshot(&self.sandbox_dir).await;
if !docs.has_unchecked_items() {
return None;
}
Some(block(
"system_reminder",
&SystemReminderTemplate {
content: "tasks.md still has unchecked items. Continue working through the checklist. If user input is required, call ask_user and then proceed.".to_string(),
},
))
}
}
#[derive(Debug)]
pub struct BackgroundJobs {
registry: aither_sandbox::JobRegistry,
}
impl BackgroundJobs {
#[must_use]
pub const fn new(registry: aither_sandbox::JobRegistry) -> Self {
Self { registry }
}
}
impl ContextSource for BackgroundJobs {
async fn render(&self) -> Option<Block> {
let running = self.registry.format_running_jobs().await;
if running.is_empty() {
return None;
}
Some(block(
"system_reminder",
&SystemReminderTemplate {
content: format!(
"Running background terminals:\n{running}Use terminal_read for incremental output, read redirected output files via terminal commands like head/tail/grep/cat when needed, terminal_input for stdin, and terminal_kill to stop tasks."
),
},
))
}
}
#[cfg(feature = "skills")]
#[derive(Template)]
#[template(path = "skill_instruction.txt", escape = "none")]
struct SkillInstructionTemplate<'a> {
name: &'a str,
description: &'a str,
instructions: &'a str,
allowed_tools: Option<&'a [String]>,
resource_paths: Option<&'a [String]>,
}
#[cfg(feature = "skills")]
#[derive(Debug, Default)]
pub struct ActiveSkills {
skills: Vec<aither_skills::Skill>,
}
#[cfg(feature = "skills")]
impl ActiveSkills {
#[must_use]
pub const fn new() -> Self {
Self { skills: Vec::new() }
}
pub fn set(&mut self, skills: Vec<aither_skills::Skill>) {
self.skills = skills;
}
#[must_use]
pub fn skills(&self) -> &[aither_skills::Skill] {
&self.skills
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.skills.is_empty()
}
}
#[cfg(feature = "skills")]
impl ContextSource for ActiveSkills {
fn render(&self) -> impl Future<Output = Option<Block>> + Send {
std::future::ready(self.render_skills())
}
}
#[cfg(feature = "skills")]
impl ActiveSkills {
fn render_skills(&self) -> Option<Block> {
if self.skills.is_empty() {
return None;
}
let mut rendered = String::new();
for skill in &self.skills {
let mut resource_paths = skill.resources.keys().cloned().collect::<Vec<_>>();
resource_paths.sort();
let instruction = SkillInstructionTemplate {
name: &skill.name,
description: &skill.description,
instructions: &skill.instructions,
allowed_tools: skill.allowed_tools.as_deref(),
resource_paths: (!resource_paths.is_empty()).then_some(resource_paths.as_slice()),
}
.render()
.expect("skill instruction template must render");
if !rendered.is_empty() {
rendered.push('\n');
}
rendered.push_str(&instruction);
}
Some(Block::new("skill_instructions", rendered))
}
}
#[cfg(test)]
mod tests {
use super::*;
async fn write_tasks(root: &std::path::Path, content: &str) {
async_fs::write(root.join("tasks.md"), content)
.await
.expect("write tasks.md");
}
#[tokio::test]
async fn an_absent_tasks_document_renders_nothing() {
let root = tempfile::tempdir().expect("tempdir");
let source = TasksDocument::new(root.path(), 4);
assert!(source.render().await.is_none());
}
#[tokio::test]
async fn the_rendered_block_carries_the_document_and_its_changes() {
let root = tempfile::tempdir().expect("tempdir");
write_tasks(root.path(), "- [ ] first\n").await;
let mut source = TasksDocument::new(root.path(), 4);
let block = source.render().await.expect("renders the document");
assert!(block.content().contains("- [ ] first"));
source.commit(&block).await;
write_tasks(root.path(), "- [x] first\n- [ ] second\n").await;
let block = source.render().await.expect("renders the document");
assert!(
block.content().contains("Changes to tasks.md"),
"a changed document must report its diff: {}",
block.content()
);
}
#[tokio::test]
async fn staleness_alerts_once_per_episode_and_resets_on_change() {
let root = tempfile::tempdir().expect("tempdir");
write_tasks(root.path(), "- [ ] work\n").await;
let mut source = TasksDocument::new(root.path(), 2);
assert!(source.on_tool_turn_end().await.is_none());
assert!(source.on_tool_turn_end().await.is_none());
let alert = source.on_tool_turn_end().await.expect("alert fires");
assert!(alert.content().contains("has unchecked items"));
assert!(
source.on_tool_turn_end().await.is_none(),
"the alert must not repeat within one episode"
);
write_tasks(root.path(), "- [x] work\n- [ ] more\n").await;
assert!(source.on_tool_turn_end().await.is_none());
assert!(source.on_tool_turn_end().await.is_none());
assert!(
source.on_tool_turn_end().await.is_some(),
"a new stale episode must alert again"
);
}
#[tokio::test]
async fn a_complete_checklist_is_never_stale() {
let root = tempfile::tempdir().expect("tempdir");
write_tasks(root.path(), "- [x] done\n").await;
let mut source = TasksDocument::new(root.path(), 1);
assert!(source.on_tool_turn_end().await.is_none());
for _ in 0..5 {
assert!(source.on_tool_turn_end().await.is_none());
}
}
#[tokio::test]
async fn a_missing_document_is_reported_differently() {
let root = tempfile::tempdir().expect("tempdir");
let mut source = TasksDocument::new(root.path(), 1);
assert!(source.on_tool_turn_end().await.is_none());
let alert = source.on_tool_turn_end().await.expect("alert fires");
assert!(alert.content().contains("without a tasks.md"));
}
#[tokio::test]
async fn unchecked_items_block_the_final_turn() {
let root = tempfile::tempdir().expect("tempdir");
write_tasks(root.path(), "- [ ] work\n").await;
let mut source = TasksDocument::new(root.path(), 4);
assert!(source.on_final_turn().await.is_some());
write_tasks(root.path(), "- [x] work\n").await;
assert!(source.on_final_turn().await.is_none());
}
}