Skip to main content

agentmux_srv/server/
browser_handoff_reason.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//! The reason of a Browser hand-off: carried whole to the person, or refused with the limit.
4
5/// The most characters a hand-off's reason may have. The person reads all of it on the card, so it is never cut: an
6/// over-long one is refused with the limit, and the agent shortens it.
7pub(crate) const HANDOFF_REASON_MAX: usize = 600;
8
9/// The reason for a hand-off, whole, or why it is refused.
10pub(crate) fn handoff_reason(reason: &str) -> Result<String, String> {
11    let n = reason.chars().count();
12    if n == 0 {
13        return Err("say what the user should do (`reason`)".to_string());
14    }
15    if n > HANDOFF_REASON_MAX {
16        return Err(format!(
17            "the reason is {n} characters and the most a hand-off carries is {HANDOFF_REASON_MAX}: the person reads all of it on the card, so it is not cut. Shorten it to what they must do"
18        ));
19    }
20    Ok(reason.to_string())
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    /// A hand-off's reason reaches the person whole: a long one is refused with the limit, never cut.
28    #[test]
29    fn a_handoff_reason_is_carried_whole_or_refused_with_the_limit() {
30        let long_but_ok = "Upload the file on the form, then press Done. ".repeat(12);
31        assert!(long_but_ok.chars().count() > 300 && long_but_ok.chars().count() <= HANDOFF_REASON_MAX);
32        assert_eq!(handoff_reason(&long_but_ok).unwrap(), long_but_ok, "past the old 300-character cut, carried whole");
33        let exactly = "x".repeat(HANDOFF_REASON_MAX);
34        assert_eq!(handoff_reason(&exactly).unwrap(), exactly);
35        let over = "x".repeat(HANDOFF_REASON_MAX + 1);
36        let why = handoff_reason(&over).unwrap_err();
37        assert!(why.contains(&(HANDOFF_REASON_MAX + 1).to_string()) && why.contains(&HANDOFF_REASON_MAX.to_string()) && why.contains("not cut"), "{why}");
38        assert!(handoff_reason("").unwrap_err().contains("what the user should do"));
39        // Characters, not bytes: a reason in another script is not refused early.
40        assert!(handoff_reason(&"é".repeat(HANDOFF_REASON_MAX)).is_ok());
41    }
42}