add agent-facing compact tool gated on context usage

This commit is contained in:
damocles 2026-07-23 10:08:34 +02:00 committed by mara
commit e9df5def02
6 changed files with 133 additions and 16 deletions

View file

@ -587,6 +587,29 @@ impl AgentServer {
.await
}
#[tool(
description = "Compact the current session's context, mirroring the operator's \
dashboard `/compact` button. Gated: only honoured when this agent's last \
completed turn used more than 66% of the effective context window below \
that the call is refused with an explanation and has no effect. On a pass, \
queues compaction for the end of the current turn (same deferred mechanism \
the dashboard button uses, so it never races a live claude process); the \
usual pre-compaction notes-checkpoint turn still fires first. No args."
)]
async fn compact(&self) -> String {
run_tool_envelope("compact", String::new(), async move {
match dial_agent_socket(&hive_agent_sock::Request::Compact).await {
Some(hive_agent_sock::Response::Ok) => {
"compact queued — will run at the end of the current turn".to_owned()
}
Some(hive_agent_sock::Response::Err { message }) => message,
Some(other) => format!("compact: unexpected response: {other:?}"),
None => "compact failed: in-agent socket unavailable".to_owned(),
}
})
.await
}
// IMPORTANT: this tool is only available when the `lifecycle` tool group
// is granted to this agent. hive-c0re enforces the topology check
// server-side: the call is rejected unless `name` is a direct child.