Skip to main content

agentmux_srv_core/backend/
quota_mode.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//! Quota mode: a fleet setting instead of a broadcast (`SPEC_CHEAPER_FLEET_MESSAGING_2026_10_30.md` ยง5).
4//!
5//! `fleet:quota_mode` is `off` (the default) or `tight`. While tight, `Tasks claim` with no id takes only a
6//! task aimed at the claimer or with `priority >= fleet:quota_min_priority` (default 1); a claim by
7//! id still works, because the agent was told to. A routine `Message send` to an agent with no turn in
8//! flight is held too (`backend::cold_hold`, reason `quota`) until the agent next writes output or the mode
9//! is off. A change of mode posts a bulletin under the key `mode`, so
10//! no one broadcasts "slow down" again.
11
12use std::collections::HashMap;
13
14pub const SETTING_MODE: &str = "fleet:quota_mode";
15pub const SETTING_MIN_PRIORITY: &str = "fleet:quota_min_priority";
16/// The floor when the setting names none.
17pub const DEFAULT_MIN_PRIORITY: i64 = 1;
18
19/// Whether the mode is tight: a routine `Message send` to an agent with no turn in flight is held
20/// (`held_reason: "quota"`), and an unnamed claim is filtered.
21pub fn is_tight(extra: &HashMap<String, serde_json::Value>) -> bool {
22    claim_floor(extra).is_some()
23}
24
25/// The priority floor of an unnamed claim while the mode is tight, `None` while it is off.
26pub fn claim_floor(extra: &HashMap<String, serde_json::Value>) -> Option<i64> {
27    let tight = extra.get(SETTING_MODE).and_then(|v| v.as_str()).is_some_and(|m| m.trim().eq_ignore_ascii_case("tight"));
28    tight.then(|| extra.get(SETTING_MIN_PRIORITY).and_then(|v| v.as_i64()).unwrap_or(DEFAULT_MIN_PRIORITY))
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn the_floor_applies_only_while_the_mode_is_tight() {
37        let mut extra = HashMap::new();
38        assert_eq!(claim_floor(&extra), None, "off by default");
39        extra.insert(SETTING_MODE.to_string(), serde_json::json!("off"));
40        assert_eq!(claim_floor(&extra), None);
41        extra.insert(SETTING_MODE.to_string(), serde_json::json!(" Tight "));
42        assert_eq!(claim_floor(&extra), Some(DEFAULT_MIN_PRIORITY));
43        extra.insert(SETTING_MIN_PRIORITY.to_string(), serde_json::json!(3));
44        assert_eq!(claim_floor(&extra), Some(3));
45        extra.insert(SETTING_MODE.to_string(), serde_json::json!(true));
46        assert_eq!(claim_floor(&extra), None, "anything but the word tight is off");
47    }
48}