Watch
0
0
Fork
You've already forked hyperhive
0

swarm: /clear starts an agent's next turn on a fresh session

This commit is contained in:
iris 2026-11-16 01:01:31 +01:00
commit fb9d4b5040

View file

@ -81,6 +81,13 @@ async fn run_control(
let Some(answer) = act(
&msg.payload,
|| crate::web_ui::cancel_turn(&bus, &interrupted, "swarm UI: cancel"),
|| {
bus.request_session_reset();
bus.emit(crate::events::LiveEvent::Note {
text: "operator: session reset queued (/clear) — takes effect at the next turn"
.into(),
});
},
crate::swarm_agents_md::handle,
)
.await
@ -93,12 +100,13 @@ async fn run_control(
}
}
/// The encoded answer to a command payload: `cancel` runs for a cancel and
/// `apply` for an `AGENTS.md` edit. `None` (logged) when the payload is not a
/// The encoded answer to a command payload: `cancel` runs for a cancel,
/// `new_session` for a session reset and `apply` for an `AGENTS.md` edit. `None` (logged) when the payload is not a
/// command, which gets no answer.
async fn act<F>(
payload: &[u8],
cancel: impl FnOnce() -> bool,
new_session: impl FnOnce(),
apply: impl FnOnce(swarm_queue_client::agents_md::Apply) -> F,
) -> Option<Vec<u8>>
where
@ -110,6 +118,10 @@ where
Ok(AgentControl::Cancel) => {
serde_json::to_vec(&swarm_queue_client::AgentControlReply { acted: cancel() })
}
Ok(AgentControl::NewSession) => {
new_session();
serde_json::to_vec(&swarm_queue_client::AgentControlReply { acted: true })
}
Ok(AgentControl::ApplyAgentsMd(edit)) => serde_json::to_vec(&apply(edit).await),
Err(e) => {
tracing::warn!(error = %e, "swarm control: undecodable command dropped");
@ -321,6 +333,7 @@ mod tests {
let got = act(
br#""cancel""#,
|| acted,
|| panic!("reset the session for a cancel"),
|_| async { panic!("applied an edit for a cancel") },
)
.await
@ -341,6 +354,7 @@ mod tests {
let got = act(
&payload,
|| panic!("cancelled for an edit"),
|| panic!("reset the session for an edit"),
|got| async move {
assert_eq!(got, edit);
ApplyReply::Applied {
@ -364,9 +378,26 @@ mod tests {
let got = act(
b"pause",
|| panic!("ran a command that was not one"),
|| panic!("ran a command that was not one"),
|_| async { panic!("ran a command that was not one") },
)
.await;
assert_eq!(got, None);
}
#[tokio::test]
async fn a_new_session_resets_once_and_is_answered_as_acted() {
let resets = std::cell::Cell::new(0);
let got = act(
br#""new_session""#,
|| panic!("cancelled for a new session"),
|| resets.set(resets.get() + 1),
|_| async { panic!("applied an edit for a new session") },
)
.await
.expect("answered");
let got: AgentControlReply = serde_json::from_slice(&got).expect("a reply");
assert_eq!(got, AgentControlReply { acted: true });
assert_eq!(resets.get(), 1);
}
}