Skip to main content

a2a_protocol_server/steering/
config.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! What a server that takes steering messages is set to: how many wait for a
5//! running turn, and how large one may be.
6
7/// How many steering messages may wait in one running turn's inbox, unless
8/// [`SteeringConfig::with_inbox_capacity`] says otherwise.
9///
10/// Sixteen is room for a person, or a supervising agent, to correct course a
11/// few times while a turn reads between steps, and small enough that a turn
12/// which never reads its inbox holds at most sixteen messages of at most
13/// [`DEFAULT_MAX_MESSAGE_SIZE`] bytes, about a mebibyte, for as long as it
14/// runs.
15pub const DEFAULT_INBOX_CAPACITY: usize = 16;
16
17/// The largest steering message, in bytes of its JSON, unless
18/// [`SteeringConfig::with_max_message_size`] says otherwise.
19///
20/// 64 KiB holds a long instruction and a few data parts. A steering message
21/// is read by a model between steps, so one past this is a document, and a
22/// document belongs in a task of its own. It is also what a message costs in
23/// the store when it is handed to another replica.
24pub const DEFAULT_MAX_MESSAGE_SIZE: usize = 64 * 1024;
25
26/// Turns steering on, and sets its two bounds. See
27/// [`RequestHandlerBuilder::with_steering`](crate::RequestHandlerBuilder::with_steering).
28///
29/// ```rust
30/// use a2a_protocol_server::SteeringConfig;
31///
32/// let config = SteeringConfig::new()
33///     .with_inbox_capacity(4)
34///     .with_max_message_size(8 * 1024);
35/// assert_eq!(config.inbox_capacity(), 4);
36/// assert_eq!(config.max_message_size(), 8 * 1024);
37/// ```
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39#[non_exhaustive]
40pub struct SteeringConfig {
41    inbox_capacity: usize,
42    max_message_size: usize,
43    notifications: bool,
44}
45
46impl Default for SteeringConfig {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52impl SteeringConfig {
53    /// The defaults: [`DEFAULT_INBOX_CAPACITY`] messages of at most
54    /// [`DEFAULT_MAX_MESSAGE_SIZE`] bytes.
55    #[must_use]
56    pub const fn new() -> Self {
57        Self {
58            inbox_capacity: DEFAULT_INBOX_CAPACITY,
59            max_message_size: DEFAULT_MAX_MESSAGE_SIZE,
60            notifications: false,
61        }
62    }
63
64    /// How many messages may wait in one turn's inbox. A message that finds
65    /// the inbox full is refused with `503` and `Retry-After`, never dropped
66    /// and never waited for. `build` refuses `0`: an inbox that holds nothing
67    /// refuses every message.
68    #[must_use]
69    pub const fn with_inbox_capacity(mut self, capacity: usize) -> Self {
70        self.inbox_capacity = capacity;
71        self
72    }
73
74    /// The largest message accepted, in bytes of its JSON; one byte over is
75    /// refused as too large. `build` refuses `0`.
76    #[must_use]
77    pub const fn with_max_message_size(mut self, bytes: usize) -> Self {
78        self.max_message_size = bytes;
79        self
80    }
81
82    /// Whether a replica that handed a steering message to another one
83    /// through the store also tells it so at once, with PostgreSQL's
84    /// `NOTIFY`, instead of leaving it to the other replica's next look at
85    /// the store (every turn-claim renewal interval, one second by default).
86    /// Off by default, and only the PostgreSQL claim store sends one.
87    ///
88    /// It holds a `LISTEN` session on a pooled connection, which breaks
89    /// behind a connection pooler in transaction mode (`PgBouncer`'s usual
90    /// setting): the pooler lends the session's connection to another client,
91    /// and the next notification arrives in the middle of that client's
92    /// statement. Measured against `PgBouncer` 1.26.1 on 2026-10-30, for the
93    /// halt store, which listens the same way. Turn it on only with a direct
94    /// connection to PostgreSQL. Nothing is lost by leaving it off: the
95    /// look every renewal interval delivers every message.
96    #[must_use]
97    pub const fn with_notifications(mut self, on: bool) -> Self {
98        self.notifications = on;
99        self
100    }
101
102    /// How many messages may wait in one turn's inbox.
103    #[must_use]
104    pub const fn inbox_capacity(&self) -> usize {
105        self.inbox_capacity
106    }
107
108    /// The largest message accepted, in bytes.
109    #[must_use]
110    pub const fn max_message_size(&self) -> usize {
111        self.max_message_size
112    }
113
114    /// Whether a handed-off message is announced with `NOTIFY`.
115    #[must_use]
116    pub const fn notifications(&self) -> bool {
117        self.notifications
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    #[test]
126    fn the_defaults_are_the_documented_ones() {
127        let config = SteeringConfig::new();
128        assert_eq!(config.inbox_capacity(), 16);
129        assert_eq!(config.max_message_size(), 65_536);
130        assert_eq!(config.inbox_capacity(), DEFAULT_INBOX_CAPACITY);
131        assert_eq!(config.max_message_size(), DEFAULT_MAX_MESSAGE_SIZE);
132        assert!(!config.notifications(), "off until asked for");
133    }
134
135    #[test]
136    fn each_setter_sets_its_own_bound_only() {
137        let capacity = SteeringConfig::new().with_inbox_capacity(3);
138        assert_eq!(capacity.inbox_capacity(), 3);
139        assert_eq!(capacity.max_message_size(), DEFAULT_MAX_MESSAGE_SIZE);
140        let size = SteeringConfig::new().with_max_message_size(7);
141        assert_eq!(size.max_message_size(), 7);
142        assert_eq!(size.inbox_capacity(), DEFAULT_INBOX_CAPACITY);
143        let on = SteeringConfig::new().with_notifications(true);
144        assert!(on.notifications());
145        assert_eq!(on.inbox_capacity(), DEFAULT_INBOX_CAPACITY);
146        assert_eq!(on.max_message_size(), DEFAULT_MAX_MESSAGE_SIZE);
147        assert!(!on.with_notifications(false).notifications());
148    }
149}