Skip to content
Merged
8 changes: 4 additions & 4 deletions AGENTS.md

Large diffs are not rendered by default.

491 changes: 438 additions & 53 deletions crates/agent/src/crew.rs

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions crates/agent/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ pub enum Event {
/// Whether it gives more than the sandbox holds: the network, a
/// file outside the workspace, where code runs later.
widens: bool,
/// What a file written or edited becomes, as a unified diff of its
/// lines, when the agent may read the file: the person sees the
/// change before they allow it.
preview: Option<String>,
},
/// The person answered, or nobody did in time.
Answered {
Expand Down Expand Up @@ -283,6 +287,12 @@ pub enum Event {
/// What.
text: String,
},
/// The person's skills open to the agents of a chat, until the turn
/// they serve ends, or shut.
Skills {
/// The chat, while they are open.
chat: Option<String>,
},
}

impl Event {
Expand Down Expand Up @@ -341,6 +351,11 @@ impl Event {
renamed(agent);
on.iter_mut().for_each(renamed);
}
Event::Skills { chat } => {
if let Some(chat) = chat {
renamed(chat);
}
}
}
}
}
107 changes: 106 additions & 1 deletion crates/agent/src/keep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ use std::path::PathBuf;
use iq_core::{Effort, Message, Usd};
use serde::{Deserialize, Serialize};

use crate::PERSON;

/// How a chat came to run on the model the settings name for the person's
/// chats: chosen by none, it follows them as they change.
pub const SETTINGS_MODEL: &str = "the settings' model";

/// An agent and its conversation, as kept.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Saved {
Expand Down Expand Up @@ -57,6 +63,55 @@ pub struct Saved {
pub set_apart: Vec<String>,
/// Its conversation, the standing instructions first.
pub messages: Vec<Message>,
/// The messages its compactions replaced, whole and in order, without
/// the summaries they left: its thread comes back from them as it was
/// shown.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub compacted_away: Vec<Message>,
/// How many of its messages, after its standing instructions, its last
/// compaction wrote: its summary, and its last answer again.
#[serde(default, skip_serializing_if = "is_zero")]
pub compacted_head: usize,
}

fn is_zero(count: &usize) -> bool {
*count == 0
}

impl Saved {
/// Whether the chat runs on the model the settings name, as the person
/// started it: they chose none for it, and it follows the settings as
/// they change.
#[must_use]
pub fn follows_settings(&self) -> bool {
self.by == PERSON
&& self
.models_chosen
.last()
.is_none_or(|choice| choice.how == SETTINGS_MODEL)
}

/// Its conversation as it went: what its compactions replaced, in
/// place of the summaries they left. What was set aside stands among
/// these messages.
#[must_use]
pub fn history(&self) -> Vec<Message> {
history(&self.messages, &self.compacted_away, self.compacted_head)
}

/// Moves the chat to `model`, which `by` chose as `how` says at `at`,
/// from its next turn: its answers keep their text, not the reasoning
/// their provider signed, which another provider cannot check.
pub fn move_to(&mut self, model: &str, by: &str, how: &str, at: u64) {
forget_reasoning(&mut self.messages);
model.clone_into(&mut self.model);
self.models_chosen.push(ModelChoice {
model: model.to_owned(),
by: by.to_owned(),
how: how.to_owned(),
at,
});
}
}

/// One thing the person was shown of an agent beside its conversation,
Expand All @@ -69,7 +124,8 @@ pub struct Aside {
/// The turn of the agent's it came in: 0 before its first.
pub turn: u64,
/// How many messages its conversation held then: where it stands
/// among them, to a step.
/// among them, to a step, those its compactions replaced counted
/// ([`Saved::history`]).
#[serde(default)]
pub after: usize,
/// What it was.
Expand All @@ -86,6 +142,11 @@ pub enum Shown {
/// What.
text: String,
},
/// The model its turns answer on from there.
Model {
/// Which.
model: String,
},
/// A question the agent asked the person, with its choices, and their
/// answer once given.
Question {
Expand Down Expand Up @@ -157,6 +218,50 @@ pub struct ModelChoice {
pub at: u64,
}

/// A conversation, `messages`, as it went: its standing instructions,
/// what its compactions replaced (`away`), then what it holds after the
/// `head` messages its last compaction wrote.
pub(crate) fn history(messages: &[Message], away: &[Message], head: usize) -> Vec<Message> {
if away.is_empty() {
return messages.to_vec();
}
let rest = messages.get(1 + head..).unwrap_or_default();
messages
.iter()
.take(1)
.chain(away)
.chain(rest)
.cloned()
.collect()
}

/// Where the place `at` among a conversation's messages stands in its
/// [`history`], `away` messages replaced by the `head` its last compaction
/// wrote: within those, just after what they replaced.
pub(crate) fn placed(at: usize, away: usize, head: usize) -> usize {
if away == 0 {
return at;
}
(at.max(1 + head) + away).saturating_sub(head)
}

/// Takes off what the answers of `messages` carry of their model's
/// reasoning, as its provider signed it: their text stays. Returns whether
/// any carried it.
pub(crate) fn forget_reasoning(messages: &mut [Message]) -> bool {
let mut forgot = false;
for message in messages {
if let Message::Assistant {
reasoning_signature,
..
} = message
{
forgot |= reasoning_signature.take().is_some();
}
}
forgot
}

/// Keeps the chats between sessions.
pub trait Keeper: Send + Sync {
/// Keeps `chat` as it stands now, in place of what was kept of it.
Expand Down
3 changes: 2 additions & 1 deletion crates/agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod diff;
mod event;
mod extension;
mod keep;
mod look_back;
mod make;
mod once;
mod place;
Expand All @@ -36,7 +37,7 @@ pub use crew::{
};
pub use event::{Event, PERSON};
pub use extension::{Called, Extension};
pub use keep::{Aside, Keeper, ModelChoice, Saved, Shown};
pub use keep::{Aside, Keeper, ModelChoice, SETTINGS_MODEL, Saved, Shown};
pub use once::{Asked, ask_once};
pub use place::{Placed, Placer};
pub use rules::{RulesFile, with_imports};
Expand Down
178 changes: 178 additions & 0 deletions crates/agent/src/look_back.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//! What an agent's compactions replaced, found again: the messages they
//! took out of its conversation, one entry each, numbered, by the words
//! they hold or on from a number, cut short so that reading them back
//! never swells the conversation for long.

use iq_core::Message;

/// The most characters of one entry given.
const ENTRY_MOST: usize = 4_000;

/// The most characters given in all.
const ALL_MOST: usize = 24_000;

/// `message` as one entry: what it was said to the agent, what the agent
/// said and called, what a tool returned. Its standing instructions are
/// none.
fn entry(message: &Message) -> Option<String> {
Some(match message {
Message::System(_) => return None,
Message::User(text) => format!("to you: {text}"),
Message::Assistant {
content,
tool_calls,
..
} => {
let mut said = format!("you: {}", content.as_deref().unwrap_or("").trim());
for call in tool_calls {
said.push_str(&format!(
"\n(you called {} {})",
call.name,
crate::tools::tool_input(&call.arguments)
));
}
said
}
Message::Tool { content, .. } => format!("result: {content}"),
_ => return None,
})
}

/// What `look_back` answers from `away`, the messages compactions
/// replaced: the entries holding all the words of `query`, or those on
/// from the number `from`, or both, each cut past [`ENTRY_MOST`]
/// characters and [`ALL_MOST`] in all, saying what was left out and how
/// to ask for it.
pub(crate) fn found(away: &[Message], query: Option<&str>, from: Option<usize>) -> String {
let entries: Vec<String> = away.iter().filter_map(entry).collect();
if entries.is_empty() {
return "Nothing of your conversation was compacted away: all of it is still there.".into();
}
let words: Vec<String> = query
.unwrap_or("")
.split_whitespace()
.map(str::to_lowercase)
.collect();
let from = from.unwrap_or(1).max(1);
let chosen: Vec<(usize, &String)> = entries
.iter()
.enumerate()
.map(|(at, text)| (at + 1, text))
.filter(|(number, _)| *number >= from)
.filter(|(_, text)| {
let lower = text.to_lowercase();
words.iter().all(|word| lower.contains(word.as_str()))
})
.collect();
let asked = match query {
Some(query) if !words.is_empty() => format!(" holding all of \"{}\"", query.trim()),
_ => String::new(),
};
if chosen.is_empty() {
return format!(
"No entry{asked} from {from} on, of the {} your compactions replaced: `from` reads \
them on from a number, `query` finds them by their words.",
entries.len()
);
}
let mut out = format!(
"{} entries{asked} of the {} your compactions replaced, oldest first:",
chosen.len(),
entries.len()
);
for (given, (number, text)) in chosen.iter().enumerate() {
let cut = cut(text);
if given > 0 && out.chars().count() + cut.chars().count() > ALL_MOST {
let again = if asked.is_empty() {
format!("`look_back` with from {number}")
} else {
format!("`look_back` with the same query and from {number}")
};
out.push_str(&format!(
"\n\n({} more left out: {again} reads on.)",
chosen.len() - given
));
return out;
}
out.push_str(&format!("\n\n#{number} {cut}"));
}
out
}

/// `text`, or its first [`ENTRY_MOST`] characters and how many it leaves.
fn cut(text: &str) -> String {
match text.char_indices().nth(ENTRY_MOST) {
Some((at, _)) => format!(
"{}… ({} more characters)",
&text[..at],
text[at..].chars().count()
),
None => text.to_owned(),
}
}

#[cfg(test)]
mod tests {
use super::*;
use iq_core::ToolCall;

fn away() -> Vec<Message> {
vec![
Message::system("be brief"),
Message::user("rename the parser"),
Message::Assistant {
content: Some("Reading it.".into()),
tool_calls: vec![ToolCall {
id: "1".into(),
name: "read_file".into(),
arguments: r#"{"path": "src/parser.rs"}"#.into(),
}],
reasoning_signature: None,
},
Message::Tool {
call_id: "1".into(),
content: "fn parse() {}".into(),
},
Message::Assistant {
content: Some("Renamed the Parser to Reader.".into()),
tool_calls: Vec::new(),
reasoning_signature: None,
},
]
}

#[test]
fn entries_are_found_by_their_words_or_read_on_from_a_number() {
let said = found(&away(), Some("parser RENAMED"), None);
assert_eq!(
said,
"1 entries holding all of \"parser RENAMED\" of the 4 your compactions replaced, \
oldest first:\n\n#4 you: Renamed the Parser to Reader."
);
let said = found(&away(), None, Some(2));
assert!(
said.contains("#2 you: Reading it.\n(you called read_file src/parser.rs)"),
"{said}"
);
assert!(said.contains("#3 result: fn parse() {}"), "{said}");
assert!(!said.contains("#1 "), "{said}");
assert!(found(&away(), Some("lexer"), None).starts_with("No entry holding all of"));
assert!(found(&[], None, None).starts_with("Nothing of your conversation"));
}

#[test]
fn a_long_entry_is_cut_and_what_passes_the_most_is_left_for_later() {
let long: Vec<Message> = (0..12)
.map(|n| Message::user(format!("{n} {}", "x".repeat(ENTRY_MOST * 2))))
.collect();
let said = found(&long, None, None);
// "to you: 0 " and twice the most, less the most.
assert!(said.contains(&format!("… ({} more characters)", ENTRY_MOST + 10)));
assert!(said.chars().count() <= ALL_MOST + 200, "{}", said.len());
assert!(
said.ends_with("more left out: `look_back` with from 6 reads on.)"),
"{}",
&said[said.len() - 80..]
);
}
}
Loading
Loading