Skip to main content

agentmux_srv/server/
jekt_ack.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//! The sender-side check that refuses a reply saying nothing (`backend::ack_policy`).
4
5use super::AppState;
6use crate::backend::ack_policy::{canon, remember_arrival};
7use crate::backend::reactive::types::InjectionRequest;
8
9/// Refuse an agent's reply to a message that asked for none, or a bare acknowledgement to an agent
10/// that wrote to it a moment ago (`backend::ack_policy`); otherwise remember the message, for the
11/// next reply's check. Only the sender's own srv refuses (a full-key, first-hop, host-tier
12/// request); a message arriving from another host is only remembered.
13pub(super) fn refuse_empty_reply(state: &AppState, auth_via: super::auth::ReactiveAuthVia, req: &InjectionRequest) -> Option<serde_json::Value> {
14    use crate::backend::ack_policy;
15    let source = req.source_agent.as_deref().filter(|s| !s.is_empty() && *s != "cron")?;
16    let from_here = auth_via == super::auth::ReactiveAuthVia::FullAuthKey
17        && req.forward_hops == 0
18        && req.delivery_tier.as_deref() == Some("host");
19    if from_here {
20        let extra = state.config_watcher.get_full_config().settings.extra.clone();
21        if let Err(why) = ack_policy::check(
22            &canon(&state.mstore, source),
23            &canon(&state.mstore, &req.target_agent),
24            &req.message,
25            req.in_reply_to.as_deref(),
26            // A caption on an attachment is not a bare acknowledgement: the files are the news.
27            ack_policy::refuses_bare_acks(&extra) && req.files.as_ref().is_none_or(|f| f.is_empty()),
28        ) {
29            tracing::info!(source, target = %req.target_agent, "reactive inject: refusing a reply that says nothing");
30            state.reactive_handler.log_ack_refused(source, &req.target_agent, req.request_id.as_deref().unwrap_or_default(), &why);
31            return Some(serde_json::json!({
32                "success": false,
33                "refused": true,
34                "error": format!("Not sent: {why}."),
35                "request_id": req.request_id.clone().unwrap_or_default(),
36            }));
37        }
38    }
39    remember_arrival(&state.mstore, req);
40    None
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use crate::backend::storage::store::Store;
47
48    fn arrival(source: Option<&str>, request_id: &str, reply: Option<&str>) -> InjectionRequest {
49        InjectionRequest {
50            target_agent: "jack-target".into(),
51            source_agent: source.map(str::to_string),
52            request_id: Some(request_id.into()),
53            reply: reply.map(str::to_string),
54            ..Default::default()
55        }
56    }
57
58    #[test]
59    fn an_arrival_that_asked_for_no_reply_is_remembered_for_the_next_check() {
60        let store = Store::open_in_memory().unwrap();
61        remember_arrival(&store, &arrival(Some("jack-sender"), "jack-msg-none", Some("none")));
62        remember_arrival(&store, &arrival(Some("jack-sender"), "jack-msg-reply", None));
63        assert!(crate::backend::ack_policy::asked_for_no_reply("jack-msg-none"));
64        assert!(!crate::backend::ack_policy::asked_for_no_reply("jack-msg-reply"));
65    }
66
67    #[test]
68    fn an_arrival_with_no_source_or_from_cron_is_not_remembered() {
69        let store = Store::open_in_memory().unwrap();
70        for source in [None, Some(""), Some("cron")] {
71            remember_arrival(&store, &arrival(source, "jack-msg-unsourced", Some("none")));
72        }
73        assert!(!crate::backend::ack_policy::asked_for_no_reply("jack-msg-unsourced"));
74    }
75}