use std::fmt::Write as _;
use crate::agent::AgentName;
pub const MAX_MEMORY: usize = 4_096;
#[derive(Debug, Clone, Copy)]
pub struct Run<'a> {
pub agent: &'a AgentName,
pub instructions: &'a str,
pub memory: Option<&'a str>,
pub brief: &'a str,
pub handover: Option<&'a str>,
pub body: &'a str,
}
#[must_use]
pub fn compose(run: &Run<'_>) -> String {
let mut out = String::with_capacity(
run.instructions.len() + run.brief.len() + run.body.len() + MAX_MEMORY + 256,
);
write_identity(&mut out, run.agent);
write_section(&mut out, run.instructions);
if let Some(memory) = run.memory {
write_memory(&mut out, memory);
}
write_section(&mut out, run.brief);
if let Some(handover) = run.handover {
write_section(&mut out, handover);
}
write_body(&mut out, run.body);
out
}
fn write_identity(out: &mut String, agent: &AgentName) {
let _ = writeln!(out, "You are `{agent}`.\n");
}
fn write_section(out: &mut String, text: &str) {
let text = text.trim();
if text.is_empty() {
return;
}
out.push_str(text);
out.push_str("\n\n");
}
fn write_memory(out: &mut String, memory: &str) {
let memory = memory.trim();
if memory.is_empty() {
return;
}
out.push_str("== WHAT YOU WROTE DOWN LAST TIME ==\n");
out.push_str(
"Runs are a clean slate, so this is everything you remember. It is what earlier runs of \
you chose to record, and nothing else carried over.\n\n",
);
if memory.len() <= MAX_MEMORY {
out.push_str(memory);
out.push_str("\n\n");
return;
}
let mut start = memory.len() - MAX_MEMORY;
while start < memory.len() && !memory.is_char_boundary(start) {
start += 1;
}
out.push_str(
"[This is the most recent part of your memory. It is longer than fits here — call \
`layover_memory_read` for the whole file.]\n\n",
);
out.push_str(memory[start..].trim_start());
out.push_str("\n\n");
}
fn write_body(out: &mut String, body: &str) {
out.push_str("== WHAT YOU HAVE BEEN ASKED TO DO ==\n\n");
out.push_str(body.trim());
out.push('\n');
}
#[cfg(test)]
mod tests {
use super::*;
fn agent() -> AgentName {
AgentName::new("tester")
}
fn minimal<'a>(agent: &'a AgentName, body: &'a str) -> Run<'a> {
Run {
agent,
instructions: "Run the suite and report what failed.",
memory: None,
brief: "",
handover: None,
body,
}
}
#[test]
fn the_sections_arrive_in_the_settled_order() {
let name = agent();
let run = Run {
agent: &name,
instructions: "INSTRUCTIONS",
memory: Some("MEMORY"),
brief: "BRIEF",
handover: Some("HANDOVER"),
body: "BODY",
};
let text = compose(&run);
let at = |needle: &str| {
text.find(needle)
.unwrap_or_else(|| panic!("missing {needle}"))
};
assert!(at("You are `tester`") < at("INSTRUCTIONS"));
assert!(at("INSTRUCTIONS") < at("MEMORY"));
assert!(at("MEMORY") < at("BRIEF"));
assert!(
at("BRIEF") < at("HANDOVER"),
"a recovery instruction must not be buried under accumulated advice"
);
assert!(
at("HANDOVER") < at("BODY"),
"the message that woke the agent reads as the current instruction, so it goes last"
);
}
#[test]
fn the_body_is_the_last_thing_in_the_payload() {
let name = agent();
let text = compose(&minimal(&name, "Fix the retry policy."));
assert!(text.trim_end().ends_with("Fix the retry policy."), "{text}");
}
#[test]
fn identity_comes_from_the_tower_and_is_always_present() {
let name = agent();
assert!(compose(&minimal(&name, "go")).starts_with("You are `tester`."));
}
#[test]
fn absent_sections_leave_no_hole() {
let name = agent();
let text = compose(&minimal(&name, "go"));
assert!(!text.contains("WHAT YOU WROTE DOWN"), "{text}");
assert!(
!text.contains("\n\n\n"),
"blank lines should not stack: {text:?}"
);
}
#[test]
fn memory_shorter_than_the_cap_arrives_whole_and_says_nothing_about_cutting() {
let name = agent();
let run = Run {
memory: Some("The e2e suite needs VPN. Ask before assuming a failure is real."),
..minimal(&name, "go")
};
let text = compose(&run);
assert!(text.contains("needs VPN"), "{text}");
assert!(!text.contains("most recent part"), "{text}");
}
#[test]
fn over_long_memory_keeps_the_end_not_the_beginning() {
let name = agent();
let memory = format!("OLDEST{}NEWEST", "x".repeat(MAX_MEMORY * 2));
let run = Run {
memory: Some(&memory),
..minimal(&name, "go")
};
let text = compose(&run);
assert!(text.contains("NEWEST"), "the recent end was dropped");
assert!(!text.contains("OLDEST"), "the old end was kept");
}
#[test]
fn a_cut_memory_says_so_and_says_where_the_rest_is() {
let name = agent();
let memory = "y".repeat(MAX_MEMORY * 2);
let run = Run {
memory: Some(&memory),
..minimal(&name, "go")
};
let text = compose(&run);
assert!(text.contains("longer than fits here"), "{text}");
assert!(
text.contains("layover_memory_read"),
"an agent told it has more should be told how to get it: {text}"
);
}
#[test]
fn cutting_memory_never_splits_a_character() {
let name = agent();
let memory = "é".repeat(MAX_MEMORY);
let run = Run {
memory: Some(&memory),
..minimal(&name, "go")
};
assert!(compose(&run).contains('é'));
}
#[test]
fn an_ordinary_dispatch_carries_no_handover() {
let name = agent();
let text = compose(&minimal(&name, "go"));
assert!(!text.contains("continuing"), "{text}");
}
#[test]
fn empty_memory_is_the_same_as_no_memory() {
let name = agent();
let blank = Run {
memory: Some(" \n "),
..minimal(&name, "go")
};
assert_eq!(compose(&blank), compose(&minimal(&name, "go")));
}
}