agentmux_srv/server/app_api/
agent_mute_quiet.rs1use axum::extract::State;
12use axum::http::StatusCode;
13use axum::response::{IntoResponse, Response};
14use axum::{Extension, Json};
15use serde::Deserialize;
16use serde_json::{json, Value};
17
18use super::agent_lifecycle::{actor_of, audit, refuse, target_of, Refusal};
19use crate::backend::quiet_watch;
20use crate::server::caller::Caller;
21use crate::server::state::AppState;
22
23#[derive(Debug, Deserialize)]
24pub(crate) struct MuteRequest {
25 pub(super) agent: String,
27 pub(super) minutes: u64,
29 #[serde(default)]
31 pub(super) reason: String,
32}
33
34async fn run(state: &AppState, caller: Option<&Caller>, req: MuteRequest) -> Result<Value, Refusal> {
35 let actor = actor_of(state, caller, "mute_quiet")?;
36 let blocking = state.clone();
37 let named = req.agent.clone();
38 let target = tokio::task::spawn_blocking(move || target_of(&blocking, &named))
39 .await
40 .map_err(|e| refuse(StatusCode::INTERNAL_SERVER_ERROR, format!("Agents mute_quiet: {e}")))??;
41 let block_id = target.block_id.clone().unwrap_or_default();
42 if target.agent_id == actor.uid() {
43 return Err(refuse(StatusCode::CONFLICT, "SELF_MUTE: that is you; Presence status with state waiting and until_minutes says you will be quiet"));
44 }
45 if !quiet_watch::leads(&state.mstore, actor.uid(), &target.agent_id) {
46 let why = format!("NOT_ITS_LEAD: only {}'s lead (its seat's lead, or the agent that spawned it) can mute it", target.name);
47 audit(state, &actor, "mute_quiet", &target.name, &block_id, false, Some(&why), "refused");
48 return Err(refuse(StatusCode::FORBIDDEN, why));
49 }
50 let now = agentmux_common::time::now_ms_u64();
51 if req.minutes == 0 {
52 let lifted = quiet_watch::unmute(&target.agent_id, now);
53 audit(state, &actor, "mute_quiet", &target.name, &block_id, true, None, "lifted");
54 return Ok(json!({ "agent": target.name, "agent_id": target.agent_id, "muted": false, "lifted": lifted }));
55 }
56 let muted = quiet_watch::mute(&target.agent_id, req.minutes, &req.reason, actor.slug(), now).map_err(|e| refuse(StatusCode::BAD_REQUEST, format!("INVALID_ARG: {e}")))?;
57 audit(state, &actor, "mute_quiet", &target.name, &block_id, true, None, &format!("{} minutes: {}", req.minutes, muted.reason));
58 Ok(json!({
59 "agent": target.name, "agent_id": target.agent_id, "muted": true, "until_ms": muted.until_ms, "reason": muted.reason,
60 "note": "the quiet watch neither nudges it nor tells anyone until then; it starts again from the end of the mute. minutes: 0 lifts it",
61 }))
62}
63
64pub(crate) async fn handle_agent_mute_quiet(State(state): State<AppState>, caller: Option<Extension<Caller>>, Json(req): Json<MuteRequest>) -> Response {
65 let caller = caller.map(|Extension(c)| c);
66 match run(&state, caller.as_ref(), req).await {
67 Ok(body) => Json(body).into_response(),
68 Err(r) => (r.status, Json(json!({ "error": r.message }))).into_response(),
69 }
70}
71
72#[cfg(test)]
73#[path = "tests/agent_mute_quiet_tests.rs"]
74mod tests;