Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions AGENTS.md

Large diffs are not rendered by default.

155 changes: 139 additions & 16 deletions crates/agent/src/crew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ pub const BUSY_WAIT: Duration = Duration::from_secs(10);
/// The longest one wait lasts, whatever the provider asks.
pub const BUSY_MOST: Duration = Duration::from_secs(300);

/// How long a call to a model with a backup left waits for its answer,
/// unless the settings say: past it, the call goes to the backup.
pub const BACKUP_AFTER: Duration = Duration::from_secs(180);

/// How long a command may write nothing before it is stopped, unless the
/// settings say: one waiting for a login in a browser, or for an answer at
/// a terminal it does not have, would hold its agent until its time ran
Expand Down Expand Up @@ -193,6 +197,13 @@ pub struct Settings {
pub busy_wait: Duration,
/// The longest one wait lasts, whatever the provider asks.
pub busy_most: Duration,
/// The models a call to a model goes to in turn when it gets no answer
/// in time or its provider is out of capacity, by that model: best the
/// same model in another region, so that the cache and the reasoning
/// are lost once at most. A refusal goes to none.
pub backups: HashMap<ModelId, Vec<ModelId>>,
/// How long a call to a model with a backup left waits for its answer.
pub backup_after: Duration,
/// The model of the provider that names the person's chats after what
/// their work is about: none, and they keep the name they started
/// with.
Expand Down Expand Up @@ -229,6 +240,8 @@ impl Default for Settings {
busy_tries: BUSY_TRIES,
busy_wait: BUSY_WAIT,
busy_most: BUSY_MOST,
backups: HashMap::new(),
backup_after: BACKUP_AFTER,
namer: None,
turn_ask: None,
light_from: LIGHT_FROM,
Expand Down Expand Up @@ -501,6 +514,9 @@ struct Failure {
/// Whether the provider stayed out of capacity through every try,
/// which `said` tells whole.
busy: bool,
/// Whether the call may go to its model's next backup: it got no
/// answer in time, or its provider is out of capacity.
hand_over: bool,
}

/// The wakes an agent asked for: how many wait, and when it asked those of
Expand Down Expand Up @@ -758,6 +774,9 @@ struct Member {
forget_reasoning: bool,
/// Whether it was told the person's skills are open to it.
skills_told: bool,
/// The names of the tools its last call offered: offered others, its
/// answers' reasoning, signed with those it was offered, goes.
offered: Vec<String>,
/// The messages its compactions replaced, in order.
compacted_away: Vec<Message>,
/// How many of its messages, after its standing instructions, its last
Expand Down Expand Up @@ -1004,7 +1023,15 @@ where
Message::system(self.instructions(&name, &saved.by, saved.role.as_deref(), &place));
let mut messages = saved.messages;
match messages.first_mut() {
Some(first @ Message::System(_)) => *first = instructions,
Some(first @ Message::System(_)) => {
// Written otherwise (the person's rules, a skill, its
// place), they unseal the reasoning its answers carry,
// which a provider checks against all that came before.
if *first != instructions {
forget_reasoning(&mut messages);
}
messages[0] = instructions;
}
_ => messages.insert(0, instructions),
}
if messages.len() == 1 {
Expand Down Expand Up @@ -1048,6 +1075,7 @@ where
models_chosen: saved.models_chosen,
forget_reasoning: false,
skills_told: false,
offered: Vec::new(),
compacted_away: saved.compacted_away,
compacted_head: saved.compacted_head,
},
Expand Down Expand Up @@ -1590,6 +1618,7 @@ where
}],
forget_reasoning: false,
skills_told: false,
offered: Vec::new(),
compacted_away: Vec::new(),
compacted_head: 0,
},
Expand Down Expand Up @@ -2888,6 +2917,7 @@ where
.collect();
let older = &results[..results.len().saturating_sub(LIGHT_KEPT)];
let (mut count, mut chars) = (0, 0);
let mut first = None;
for &at in older {
let Message::Tool { call_id, content } = &messages[at] else {
continue;
Expand Down Expand Up @@ -2917,10 +2947,16 @@ where
);
count += 1;
chars += size;
first.get_or_insert(at);
if let Message::Tool { content, .. } = &mut messages[at] {
*content = line;
}
}
// The answers after the first result changed carry a reasoning
// signed with what it was: a provider would refuse it.
if let Some(first) = first {
forget_reasoning(&mut messages[first..]);
}
if count > 0 {
member.lightened = member.context;
}
Expand Down Expand Up @@ -2976,13 +3012,35 @@ where
&self,
agent: &str,
request: &ChatRequest,
backup: bool,
) -> Result<iq_core::ChatResponse, Failure> {
let settings = &self.inner.settings;
let started = Instant::now();
let mut wait = settings.busy_wait;
let mut tries = 0;
loop {
let error = match self.inner.provider.complete(request).await {
// With a backup left, a call waits only so long.
let answer = if backup {
let after = settings.backup_after;
match tokio::time::timeout(after, self.inner.provider.complete(request)).await {
Ok(answer) => answer,
Err(_) => {
return Err(Failure {
said: format!(
"{} gave no answer within {}",
request.model,
lasted(after)
),
refusal: None,
busy: false,
hand_over: true,
});
}
}
} else {
self.inner.provider.complete(request).await
};
let error = match answer {
Ok(response) => {
if tries > 0 {
self.report(Event::Notice {
Expand All @@ -2999,14 +3057,26 @@ where
said: chain(&error),
refusal: self.inner.provider.refusal(&error),
busy: false,
hand_over: false,
});
};
let said = cut_to(&chain(&error), BUSY_SAID);
// Out of capacity, it goes to its backup at once rather than
// waiting through the tries.
if backup {
return Err(Failure {
said: format!("{} is out of capacity ({said})", request.model),
refusal: self.inner.provider.refusal(&error),
busy: true,
hand_over: true,
});
}
if tries >= settings.busy_tries {
let refusal = self.inner.provider.refusal(&error);
return Err(Failure {
refusal,
busy: true,
hand_over: false,
said: format!(
"the provider of {} stayed out of capacity for {}, through {} tries ({said}). \
The conversation is kept: write to the agent to try again later, or give \
Expand Down Expand Up @@ -3064,13 +3134,32 @@ where
// The turns before this one, whose reasoning may be another
// model's.
let earlier = messages.len();
let mut forgotten = false;
// How far a refused signature took the reasoning off: the earlier
// turns', then all of it.
let mut forgotten = 0;
// The model each call goes to: the agent's, then its backups in
// turn, for the rest of the turn.
let backups = self
.inner
.settings
.backups
.get(model)
.cloned()
.unwrap_or_default();
let mut next_backup = 0;
let mut current = model.clone();
messages.push(Message::user(prompt));
if let Some(member) = lock(&self.inner.board).members.get_mut(name) {
member.reason = None;
}
let mut urged = false;
for step in 0..self.inner.settings.steps {
let model = &current;
// The next backup the person allows, when one is left.
let backup = backups[next_backup..]
.iter()
.position(|backup| self.refused_model(backup).is_none())
.map(|at| next_backup + at);
if let Some(news) = self.unread(name) {
messages.push(Message::user(news));
}
Expand Down Expand Up @@ -3100,6 +3189,15 @@ where
// agent asked for it.
let mut tools = tools::work();
tools.extend(self.tools_for(name));
// Other tools offered, the reasoning signed with the others
// goes: a provider checks it against them.
let names: Vec<String> = tools.iter().map(|tool| tool.name.clone()).collect();
if let Some(member) = lock(&self.inner.board).members.get_mut(name) {
if !member.offered.is_empty() && member.offered != names {
forget_reasoning(&mut messages);
}
member.offered = names;
}
let request = ChatRequest {
model: model.clone(),
messages: messages.clone(),
Expand All @@ -3111,25 +3209,49 @@ where
return (messages, Err(stopped));
}
self.measure(name, model).await;
let response = match self.ask_model(name, &request).await {
let response = match self.ask_model(name, &request, backup.is_some()).await {
Ok(response) => response,
// A signature its provider cannot check, written as another
// model answered: the call goes again once without them.
// A signature its provider cannot check, for a change iq
// could not foresee: the call goes again without the
// reasoning of the earlier turns, then without any.
Err(failed)
if !forgotten
&& signature_refused(&failed)
&& forget_reasoning(&mut messages[..earlier]) =>
if forgotten < 2 && signature_refused(&failed) && {
let mut forgot = false;
while forgotten < 2 && !forgot {
let upto = if forgotten == 0 {
earlier
} else {
messages.len()
};
forgotten += 1;
forgot = forget_reasoning(&mut messages[..upto]);
}
forgot
} =>
{
forgotten = true;
self.report(Event::Notice {
agent: name.to_owned(),
text: "its provider refused the reasoning its earlier answers carry, \
signed for another model: it is called again without it"
text: "its provider could not check the reasoning its earlier answers \
carry: they go on without it"
.into(),
});
continue;
}
Err(failed) => {
if failed.hand_over
&& let Some(at) = backup
{
next_backup = at + 1;
let to = backups[at].clone();
self.report(Event::Notice {
agent: name.to_owned(),
text: format!("{}: the call goes to {to}, its backup", failed.said),
});
// Another model reads none of its reasoning.
forget_reasoning(&mut messages);
current = to;
continue;
}
let said = self.failed(name, model, failed, &messages[counted..]);
return (messages, Err(said));
}
Expand Down Expand Up @@ -4395,7 +4517,7 @@ where
effort: Some(self.inner.settings.reviewer_effort),
};
let response = self
.ask_model(agent, &request)
.ask_model(agent, &request, false)
.await
.map_err(|failed| failed.said)?;
let cost = match response.cost {
Expand Down Expand Up @@ -5113,9 +5235,10 @@ const TITLE_CHARS: usize = 24;
const NAMING: &str = "You name a chat of a coding harness after what its work is about, from the \
first request the person wrote in it, which follows. Answer with two or three words in the \
request's own language, lower case, joined by dashes, that name its subject (the feature, the \
module, the bug), not the way it is asked: for \"could you code the payment part of the \
invoice module so that a refund can be made?\", payment-refunds. The name alone, nothing \
else.";
module, the bug), not the way it is asked: never a verb of asking (can, could, want, need) \
nor a politeness (please, thanks, hello), in any language. For \"could you code the payment \
part of the invoice module so that a refund can be made?\", payment-refunds; for \"hi, can \
you please review pull request 482?\", pr-482-review. The name alone, nothing else.";

/// The name a namer's `answer` gives a chat, as the board writes names;
/// none when it gives none.
Expand Down
12 changes: 4 additions & 8 deletions crates/agent/src/keep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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.
/// chats, as it started or came back.
pub const SETTINGS_MODEL: &str = "the settings' model";

/// An agent and its conversation, as kept.
Expand Down Expand Up @@ -79,16 +79,12 @@ fn is_zero(count: &usize) -> bool {
}

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.
/// Whether the chat comes back on the settings' lead and effort: the
/// person leads it. A model or an effort they chose for it lasts until
/// iq starts again.
#[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
Expand Down
4 changes: 2 additions & 2 deletions crates/agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ mod team;
mod tools;

pub use crew::{
ALL, BUSY_MOST, BUSY_TRIES, BUSY_WAIT, COMMAND_QUIET, COMMAND_TIMEOUT, CONTEXT_EVERY, Crew,
LIGHT_FROM, Listed, Posted, REFUSALS, Report, SCRATCH_MOST, Settings,
ALL, BACKUP_AFTER, BUSY_MOST, BUSY_TRIES, BUSY_WAIT, COMMAND_QUIET, COMMAND_TIMEOUT,
CONTEXT_EVERY, Crew, LIGHT_FROM, Listed, Posted, REFUSALS, Report, SCRATCH_MOST, Settings,
};
pub use event::{Event, PERSON};
pub use extension::{Called, Extension};
Expand Down
Loading
Loading