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
4 changes: 2 additions & 2 deletions AGENTS.md

Large diffs are not rendered by default.

94 changes: 85 additions & 9 deletions crates/agent/src/crew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ const PEER_SPAN: Duration = Duration::from_secs(10 * 60);
/// started still work.
const WAIT_LOOK: Duration = Duration::from_secs(1);

/// How long the board stays quiet before an agent waiting for all those it
/// started is given their answers: one rests just before its answer is
/// posted.
const WAIT_QUIET: Duration = Duration::from_millis(300);

/// How many crews this program made: each numbers its questions apart
/// from the others', so that an answer reaches the crew that asked when
/// one window shows several.
Expand Down Expand Up @@ -2491,6 +2496,54 @@ where
}
}

/// Waits until none of the agents `agent` started works, or one asks
/// it something it has not read, ten minutes at most, then gives all
/// that was written to it at once: one call, where waking at each
/// message would read its whole conversation again each time.
async fn wait_all(&self, agent: &str) -> String {
let mut posted = self.inner.posted.subscribe();
let on = self.at_work_by(agent);
if !on.is_empty() {
self.report(Event::Waiting {
agent: agent.to_owned(),
on,
});
}
let most = tokio::time::Instant::now() + Duration::from_secs(tools::WAIT_MOST);
loop {
posted.borrow_and_update();
let asked = lock(&self.inner.raised)
.get(agent)
.is_some_and(|raised| !raised.is_empty())
&& self.has_unread(agent);
let now = tokio::time::Instant::now();
if asked || !self.started_at_work(agent) || now >= most {
break;
}
let _ = tokio::time::timeout_at(most.min(now + WAIT_LOOK), posted.changed()).await;
}
// The answers of those who just rested, posted a moment after.
while tokio::time::Instant::now() < most
&& matches!(
tokio::time::timeout(WAIT_QUIET, posted.changed()).await,
Ok(Ok(()))
)
{}
let working = self.at_work_by(agent);
match (self.unread(agent), working.is_empty()) {
(Some(news), _) => news,
(None, true) => {
"The agents you started are done, and wrote nothing more to you.".to_owned()
}
(None, false) => format!(
"No message came in {} seconds, and {} still work. End your turn: their answers \
will wake you.",
tools::WAIT_MOST,
working.join(", ")
),
}
}

/// Whether an agent `agent` started works.
fn started_at_work(&self, agent: &str) -> bool {
!self.at_work_by(agent).is_empty()
Expand Down Expand Up @@ -2654,8 +2707,8 @@ where
}
self.retire_done(&name);
}
// What the person wrote after its last look at the board.
if self.person_waits(&name)
// What was written to it after its last look at the board.
if self.woken_by_unread(&name)
&& let Some(news) = self.unread(&name)
{
self.wake(&name, news);
Expand Down Expand Up @@ -3108,14 +3161,34 @@ where
}
}

/// Whether the person wrote to `agent` since it last read the board.
fn person_waits(&self, agent: &str) -> bool {
let board = lock(&self.inner.board);
board.members.get(agent).is_some_and(|member| {
board.messages[member.read..]
/// Whether what was written to `agent` since it last read the board
/// wakes it as its turn ends: the person's message, iq's, or that of an
/// agent it started or one retired since, at once; a peer's at work
/// while the two do not wake each other too often. Posted during its
/// last call, it would wait unread until something else woke it, and
/// be lost with the board were iq started again.
fn woken_by_unread(&self, agent: &str) -> bool {
let (direct, peer) = {
let board = lock(&self.inner.board);
let Some(member) = board.members.get(agent) else {
return false;
};
let unread: Vec<&Posted> = board.messages[member.read..]
.iter()
.any(|m| m.from == PERSON && (m.to == agent || m.to == ALL))
})
.filter(|m| (m.to == agent || m.to == ALL) && m.from != agent)
.collect();
// A writer gone from the board wakes nobody in turn.
let peer_of = |m: &Posted| {
board
.members
.get(&m.from)
.is_some_and(|writer| writer.by != agent)
};
let direct = unread.iter().any(|m| !peer_of(m));
let peer = unread.iter().find(|m| peer_of(m)).map(|m| m.from.clone());
(direct, peer)
};
direct || peer.is_some_and(|from| self.peer_may_wake(&from, agent))
}

/// A turn of one of iq's agents: its model called with its tools until
Expand Down Expand Up @@ -3561,6 +3634,9 @@ where
Ok("Sent.".into())
}
"wait_for_messages" => {
if args.flag("until_done") {
return Ok(self.wait_all(agent).await);
}
let seconds = args
.number("seconds")
.unwrap_or(tools::WAIT_SECONDS)
Expand Down
10 changes: 7 additions & 3 deletions crates/agent/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,18 @@ pub fn board() -> Vec<ToolSpec> {
name: "wait_for_messages".into(),
description: "Gives the messages written to you since you last read them, waiting \
up to `seconds` (30 by default) for one when there is none, and while \
agents you started work, until one of them writes. Ending your turn \
is the cheaper wait: an agent you started wakes you with its answer, \
and each call made to wait again reads your whole conversation."
agents you started work, until one of them writes. With `until_done`, \
waits until none of the agents you started works, or one asks you \
something, ten minutes at most, and gives all they wrote at once: the \
wait for several agents. Ending your turn is the cheaper wait for one: \
it wakes you with its answer, and each call made to wait again reads \
your whole conversation."
.into(),
parameters: json!({
"type": "object",
"properties": {
"seconds": {"type": "integer"},
"until_done": {"type": "boolean", "description": "Wait until none of the agents you started works."},
},
}),
},
Expand Down
93 changes: 93 additions & 0 deletions crates/agent/tests/crew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2079,6 +2079,99 @@ async fn a_model_too_slow_hands_the_call_to_its_backup() {
assert_eq!(g.model.models_of("lead"), ["lab/main"]);
}

#[tokio::test]
async fn waiting_until_done_gives_every_agent_s_answer_in_one_call() {
let start = |role: &str, model: &str| {
calls(
"start_agent",
serde_json::json!({"role": role, "model": model, "task": "help"}),
)
};
let f = fixture(
&[
(
"lead",
vec![
start("first", "lab/quick"),
start("second", "lab/slower"),
calls("wait_for_messages", serde_json::json!({"until_done": true})),
say("Both answered."),
],
),
("lead/first", vec![say("First done.")]),
("lead/second", vec![say("Second done.")]),
],
Settings::default(),
);
let slow = [
("lab/quick", Duration::from_millis(50)),
("lab/slower", Duration::from_millis(400)),
];
f.model
.slow_models
.lock()
.unwrap()
.extend(slow.map(|(model, time)| (model.to_owned(), time)));
f.crew.start("lead", &model("lab/m"), "go".into(), PERSON);
f.settled().await;
// One wait, both answers in it.
let requests = f.model.requests_of("lead");
assert_eq!(requests.len(), 4, "{:#?}", f.events());
let after = format!("{:?}", requests[3].messages);
assert!(
after.contains("First done.") && after.contains("Second done."),
"{after}"
);
assert!(f.events().contains(&Event::Done {
agent: "lead".into(),
outcome: Ok("Both answered.".into())
}));
}

#[tokio::test]
async fn a_message_sent_during_an_agent_s_last_call_wakes_it_once_its_turn_ends() {
let f = fixture(
&[
(
"lead",
vec![
calls(
"start_agent",
serde_json::json!({"role": "helper", "model": "lab/medium", "task": "help"}),
),
say("Started."),
say("Got its answer."),
],
),
("lead/helper", vec![say("Done helping.")]),
],
Settings::default(),
);
// The lead's calls take longer than its helper's: the helper answers
// during the lead's last call.
let slow = [
("lab/slow", Duration::from_millis(400)),
("lab/medium", Duration::from_millis(100)),
];
f.model
.slow_models
.lock()
.unwrap()
.extend(slow.map(|(model, time)| (model.to_owned(), time)));
f.crew
.start("lead", &model("lab/slow"), "go".into(), PERSON);
f.settled().await;
let seen = f.model.seen_by("lead");
assert!(
seen.contains("Message from lead/helper: Done helping."),
"{seen}"
);
assert!(f.events().contains(&Event::Done {
agent: "lead".into(),
outcome: Ok("Got its answer.".into())
}));
}

#[tokio::test]
async fn an_agent_names_a_model_whole_as_the_provider_lists_it() {
let f = fixture(
Expand Down
Loading
Loading