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
3 changed files with 155 additions and 22 deletions

View file

@ -18,11 +18,15 @@ use crate::routes::AppState;
/// The commands the swarm UI offers every agent: the name without the slash,
/// and what it does.
const COMMANDS: [(&str, &str); 2] = [
const COMMANDS: [(&str, &str); 3] = [
(
"compact",
"Compact this agent's context after the current turn",
),
(
"clear",
"Start the next turn on a fresh session, dropping this one's context",
),
("clear-todos", "Mark all of this agent's todos done"),
];
@ -164,7 +168,7 @@ mod tests {
#[test]
fn the_swarms_commands_come_first_and_always() {
let list = build(None);
assert_eq!(names(&list), ["compact", "clear-todos"]);
assert_eq!(names(&list), ["compact", "clear", "clear-todos"]);
assert!(list.iter().all(|e| e.kind == CommandKind::Command));
}
@ -173,8 +177,11 @@ mod tests {
let list = build(Some(Skills {
skills: vec![skill("zeta", "z"), skill("alpha", "a")],
}));
assert_eq!(names(&list), ["compact", "clear-todos", "alpha", "zeta"]);
assert_eq!(list[2].kind, CommandKind::Skill);
assert_eq!(
names(&list),
["compact", "clear", "clear-todos", "alpha", "zeta"]
);
assert_eq!(list[3].kind, CommandKind::Skill);
}
/// What the agent publishes is untrusted: a skill cannot take a command's
@ -184,15 +191,16 @@ mod tests {
let list = build(Some(Skills {
skills: vec![
skill("compact", "pretends to be the command"),
skill("clear", "so does this"),
skill("good", "one"),
skill("good", "two"),
skill("two words", "not a word"),
skill("", "empty"),
],
}));
assert_eq!(names(&list), ["compact", "clear-todos", "good"]);
assert_eq!(names(&list), ["compact", "clear", "clear-todos", "good"]);
assert_eq!(list[0].kind, CommandKind::Command);
assert_eq!(list[2].description, "one");
assert_eq!(list[3].description, "one");
}
#[test]

View file

@ -112,6 +112,50 @@ pub(crate) async fn post_agent_cancel(
send(&client, &agent, &from, AgentControl::Cancel).await
}
/// Start an agent's next turn on a fresh claude session.
#[utoipa::path(
post,
path = "/api/agents/{name}/new-session",
params(("name" = String, Path, description = "agent whose next turn starts a new session")),
responses(
(status = 200, description = "the agent answered and queued the reset, which takes \
effect at its next turn; `acted` is true", body = CancelResponse),
(status = 400, description = "the agent name is not shaped like an identifier \
(problem+json)", body = String),
(status = 401, description = "the request carries no `Remote-User` (problem+json)",
body = String),
(status = 503, description = "the agent's harness is not subscribed - not running, \
or not connected to the swarm queue - or no swarm queue is configured on this \
host or it could not be reached (problem+json)", body = String),
(status = 504, description = "the harness held the command and did not answer in \
time, so whether it took effect is unknown (problem+json)", body = String),
(status = 500, description = "the queue refused or failed the request (problem+json)",
body = String),
),
tag = "agents"
)]
pub(crate) async fn post_agent_new_session(
State(state): State<AppState>,
Path(agent): Path<String>,
headers: HeaderMap,
) -> Result<Json<CancelResponse>, ProblemDetails> {
let (agent, from) = target(&headers, &agent).map_err(|(s, d)| error_problem(s, d))?;
let Some(status) = state.status.as_ref() else {
return Err(error_problem(
StatusCode::SERVICE_UNAVAILABLE,
"no swarm queue is configured on this host",
));
};
let client = status.queue_client();
swarm_queue_client::ensure_connected(&client).map_err(|e| {
error_problem(
StatusCode::SERVICE_UNAVAILABLE,
&swarm_queue_client::chain(&e),
)
})?;
send(&client, &agent, &from, AgentControl::NewSession).await
}
/// The agent and who is asking, or the refusal. Everything decided before the
/// queue is touched.
pub(crate) fn target(
@ -160,7 +204,7 @@ async fn send(
Err(error_problem(
StatusCode::GATEWAY_TIMEOUT,
&format!(
"{agent} did not answer in time: whether anything was cancelled is unknown"
"{agent} did not answer in time: whether the command took effect is unknown"
),
))
}

View file

@ -674,6 +674,7 @@ pub(crate) fn build_app(state: AppState) -> axum::Router {
.routes(routes!(agent_input::post_agent_input))
.routes(routes!(agent_input::post_agents_broadcast))
.routes(routes!(agent_control::post_agent_cancel))
.routes(routes!(agent_control::post_agent_new_session))
.routes(routes!(agent_custom::post_agent_custom))
.routes(routes!(schedules::list_schedules, schedules::add_schedule))
.routes(routes!(