agent86-core 1.2.2

The agent behind agent86: a safe agent runtime you can embed
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Turn limits changed while agent86 runs: a deliberate addition, since Python
//! reads `[limits] max_steps`, `[limits] max_output_tokens`, the provider's
//! `max_tokens`, and `[agents] max_steps` only from config.
//!
//! Three limits can be overridden for the rest of the process, by `/limits`, by
//! `run`'s `--max-steps`, `--max-output-tokens`, and `--max-agent-steps`, or by
//! answering the interface's offer when a turn reaches one:
//!
//! - `steps`: the turn's step budget (`[limits] max_steps`)
//! - `agent_steps`: each sub-agent's step budget (`[agents] max_steps`), never
//!   above `steps`, as Python's `CircuitBreaker` clamps it
//! - `output`: the output cap of every model call, the main loop's and the
//!   sub-agents'. It replaces whichever setting Python's `max_output_tokens_for`
//!   would pick (the provider's `max_tokens`, else `[limits] max_output_tokens`),
//!   so a raised cap always takes effect.
//!
//! An override outlives `/model`, `/clear`, and `/resume`. The offer is a hook,
//! like the approval prompt: the interface installs it, and without it (`run`, the
//! plain loop) a limit reached ends the turn or the sub-agent as before.

use crate::agent::Harness;
use crate::config::Config;
use crate::config_writer::Change;
use serde_json::json;
use std::sync::{Mutex, PoisonError};

/// One limit `/limits` can change.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LimitKind {
    Steps,
    AgentSteps,
    Output,
}

impl LimitKind {
    pub const ALL: [Self; 3] = [Self::Steps, Self::AgentSteps, Self::Output];

    /// The name `/limits` takes.
    pub fn name(self) -> &'static str {
        match self {
            Self::Steps => "steps",
            Self::AgentSteps => "agent_steps",
            Self::Output => "output",
        }
    }

    pub fn parse(name: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|kind| kind.name() == name)
    }

    /// What the limit counts.
    pub fn unit(self) -> &'static str {
        match self {
            Self::Steps | Self::AgentSteps => "steps",
            Self::Output => "tokens",
        }
    }
}

/// The overridden limits; `None` leaves a limit to the config.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct LimitOverrides {
    pub steps: Option<u32>,
    pub agent_steps: Option<u32>,
    pub output: Option<u32>,
}

impl LimitOverrides {
    pub fn get(&self, kind: LimitKind) -> Option<u32> {
        match kind {
            LimitKind::Steps => self.steps,
            LimitKind::AgentSteps => self.agent_steps,
            LimitKind::Output => self.output,
        }
    }

    fn slot(&mut self, kind: LimitKind) -> &mut Option<u32> {
        match kind {
            LimitKind::Steps => &mut self.steps,
            LimitKind::AgentSteps => &mut self.agent_steps,
            LimitKind::Output => &mut self.output,
        }
    }

    pub fn is_empty(&self) -> bool {
        *self == Self::default()
    }
}

/// A limit a running turn reached, put to the user.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LimitHit {
    pub kind: LimitKind,
    /// The sub-agent's role, or `None` for the main loop.
    pub agent: Option<String>,
    /// The limit that was reached.
    pub current: u32,
    /// The values offered, ascending.
    pub offers: Vec<u32>,
    /// Beyond Python's settings: the raise lasts for this run of a specialist
    /// sub-agent only, not the session, since it is that specialty's own budget or
    /// model.
    pub run_only: bool,
}

impl LimitHit {
    /// What happened, in one line.
    pub fn headline(&self) -> String {
        let current = self.current;
        match (self.kind, &self.agent) {
            (LimitKind::Steps | LimitKind::AgentSteps, None) => {
                format!("Step budget reached ({current} steps)")
            }
            (LimitKind::Steps | LimitKind::AgentSteps, Some(role)) => {
                format!("Sub-agent '{role}' reached its step budget ({current} steps)")
            }
            (LimitKind::Output, None) => {
                format!("The reply reached the output limit ({current} tokens) before any text")
            }
            (LimitKind::Output, Some(role)) => format!(
                "Sub-agent '{role}' reached the output limit ({current} tokens) before any text"
            ),
        }
    }

    /// What accepting does.
    pub fn question(&self) -> &'static str {
        match (self.kind, self.run_only) {
            (LimitKind::Steps | LimitKind::AgentSteps, false) => {
                "Raise it for this session and go on?"
            }
            (LimitKind::Output, false) => "Raise it for this session and ask again?",
            (LimitKind::Steps | LimitKind::AgentSteps, true) => "Raise it for this run and go on?",
            (LimitKind::Output, true) => "Raise it for this run and ask again?",
        }
    }
}

/// Asks whether to raise a limit that was reached: the new value, or `None` to stop.
pub type LimitPrompt = Box<dyn Fn(&LimitHit) -> Option<u32> + Send + Sync>;

/// The overrides, behind a lock so a turn holding the harness can raise one, and
/// the hook that offers to.
#[derive(Default)]
pub struct SessionLimits {
    overrides: Mutex<LimitOverrides>,
    pub prompt: Option<LimitPrompt>,
    /// The `[table] key` the configured output cap comes from, for messages.
    configured_output: String,
}

impl SessionLimits {
    pub fn overrides(&self) -> LimitOverrides {
        *self
            .overrides
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
    }

    pub fn get(&self, kind: LimitKind) -> Option<u32> {
        self.overrides().get(kind)
    }

    pub fn set(&self, kind: LimitKind, value: Option<u32>) {
        let mut overrides = self
            .overrides
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        *overrides.slot(kind) = value;
    }

    pub fn clear(&self) {
        *self
            .overrides
            .lock()
            .unwrap_or_else(PoisonError::into_inner) = LimitOverrides::default();
    }

    /// What to raise for a larger output cap: the session's override when there is
    /// one, else the setting the configured cap comes from.
    pub fn output_setting(&self) -> String {
        if self.get(LimitKind::Output).is_some() {
            "the session's output limit (/limits output)".to_owned()
        } else if self.configured_output.is_empty() {
            "[limits] max_output_tokens".to_owned()
        } else {
            self.configured_output.clone()
        }
    }
}

/// What to offer for a limit at `current`: double and quadruple it, kept at or
/// under `ceiling` and above `current`.
pub fn offers(current: u32, ceiling: Option<u32>) -> Vec<u32> {
    let mut values: Vec<u32> = [current.saturating_mul(2), current.saturating_mul(4)]
        .into_iter()
        .map(|value| ceiling.map_or(value, |ceiling| value.min(ceiling)))
        .filter(|&value| value > current)
        .collect();
    values.dedup();
    values
}

/// The setting the output cap comes from in `config`, as Python's
/// `max_output_tokens_for` chooses it, for the provider section `provider`.
pub fn output_setting(config: &Config, provider: &str) -> (Vec<String>, String) {
    let per_provider = config
        .providers
        .get(provider)
        .and_then(|section| section.max_tokens)
        .is_some_and(|cap| cap > 0);
    if per_provider {
        (
            vec!["providers".into(), provider.into(), "max_tokens".into()],
            format!("[providers.{provider}] max_tokens"),
        )
    } else {
        (
            vec!["limits".into(), "max_output_tokens".into()],
            "[limits] max_output_tokens".into(),
        )
    }
}

/// The config key path and its `[table] key` label for `kind`.
pub fn setting(kind: LimitKind, config: &Config, provider: &str) -> (Vec<String>, String) {
    match kind {
        LimitKind::Steps => (
            vec!["limits".into(), "max_steps".into()],
            "[limits] max_steps".into(),
        ),
        LimitKind::AgentSteps => (
            vec!["agents".into(), "max_steps".into()],
            "[agents] max_steps".into(),
        ),
        LimitKind::Output => output_setting(config, provider),
    }
}

/// What `config` sets `kind` to for the provider section `provider`.
pub fn configured(kind: LimitKind, config: &Config, provider: &str) -> u32 {
    match kind {
        LimitKind::Steps => config.limits.max_steps,
        LimitKind::AgentSteps => config.agents.max_steps,
        LimitKind::Output => config.max_output_tokens_for(provider),
    }
}

/// The config changes that keep `overrides`: each one written to the setting it
/// replaces, so the saved value is the one that takes effect. The output cap goes
/// to the provider's `max_tokens` when that section sets one, else to
/// `[limits] max_output_tokens`.
pub fn save_changes(overrides: LimitOverrides, config: &Config, provider: &str) -> Vec<Change> {
    LimitKind::ALL
        .into_iter()
        .filter_map(|kind| {
            let value = overrides.get(kind)?;
            let value = toml_edit::Value::from(i64::from(value));
            Some((setting(kind, config, provider).0, Some(value)))
        })
        .collect()
}

impl Harness {
    /// The turn's step budget.
    pub fn max_steps(&self) -> u32 {
        self.session_limits
            .get(LimitKind::Steps)
            .unwrap_or(self.limits.max_steps)
    }

    /// A sub-agent's step budget, never above the turn's.
    pub fn agent_max_steps(&self) -> u32 {
        self.session_limits
            .get(LimitKind::AgentSteps)
            .unwrap_or(self.agents.max_steps)
            .min(self.max_steps())
    }

    /// The output cap sent with every model call.
    pub fn output_cap(&self) -> u32 {
        self.session_limits
            .get(LimitKind::Output)
            .unwrap_or(self.configured_output())
    }

    /// What to raise for a larger output cap, for messages: the session's override,
    /// else the setting the active model's cap comes from.
    pub fn output_setting(&self) -> String {
        match self.configured_output_setting() {
            Some(setting) if self.session_limits.get(LimitKind::Output).is_none() => setting,
            _ => self.session_limits.output_setting(),
        }
    }

    /// The effective value of `kind`.
    pub fn limit(&self, kind: LimitKind) -> u32 {
        match kind {
            LimitKind::Steps => self.max_steps(),
            LimitKind::AgentSteps => self.agent_max_steps(),
            LimitKind::Output => self.output_cap(),
        }
    }

    /// Take the configured output cap for the provider section `provider`, as
    /// Python's `max_output_tokens_for`: at start and on every model switch.
    pub fn configure_output(&mut self, config: &Config, provider: &str) {
        self.max_tokens = config.max_output_tokens_for(provider);
        self.session_limits.configured_output = output_setting(config, provider).1;
    }

    /// Take the limits from a freshly loaded `config` for the provider section
    /// `provider`, dropping the overrides it now agrees with (after a save).
    pub fn reload_limits(&mut self, config: &Config, provider: &str) {
        self.limits = config.limits.clone();
        self.agents = config.agents.clone();
        self.configure_output(config, provider);
        for kind in LimitKind::ALL {
            if self.session_limits.get(kind) == Some(configured(kind, config, provider)) {
                self.session_limits.set(kind, None);
            }
        }
    }

    /// Offer to raise `kind`, reached at `current`, and keep the new value for the
    /// session, or with `run_only` hand it back for the caller's run alone (a
    /// specialist sub-agent's own budget or model). `None` when there is no hook,
    /// nothing to offer, or the user stops.
    pub(crate) fn raise_limit(
        &self,
        kind: LimitKind,
        agent: Option<&str>,
        sid: &str,
        current: u32,
        run_only: bool,
    ) -> Option<u32> {
        let prompt = self.session_limits.prompt.as_ref()?;
        // A sub-agent's budget stays under the turn's.
        let ceiling = (kind == LimitKind::AgentSteps).then(|| self.max_steps());
        let hit = LimitHit {
            kind,
            agent: agent.map(str::to_owned),
            current,
            offers: offers(current, ceiling),
            run_only,
        };
        if hit.offers.is_empty() {
            return None;
        }
        // One question at a time, and none once the turn was cancelled.
        let raised = self
            .gate
            .queue
            .ask(
                crate::approval::Question {
                    agent: hit.agent.clone(),
                    subject: Some(kind.name().to_owned()),
                    ..crate::approval::Question::new(crate::approval::QuestionKind::Limit)
                },
                || prompt(&hit),
            )
            .flatten()
            .filter(|value| hit.offers.contains(value))?;
        if !run_only {
            self.session_limits.set(kind, Some(raised));
        }
        let mut fields = vec![("limit", json!(kind.name()))];
        if let Some(agent) = agent {
            fields.push(("agent", json!(agent)));
        }
        fields.push(("from", json!(current)));
        fields.push(("to", json!(raised)));
        self.recorder.event(sid, "limit_raised", fields);
        Some(raised)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{load_config, ConfigPaths, Overrides};

    fn config(name: &str, text: &str) -> Config {
        let root =
            std::env::temp_dir().join(format!("agent86-limits-{name}-{}", std::process::id()));
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(root.join("config.toml"), text).unwrap();
        let paths = ConfigPaths {
            user: None,
            project: root.join("config.toml"),
            project_trust: crate::config::ProjectTrust::Trusted,
        };
        let config = load_config(&paths, &Overrides::default()).unwrap();
        let _ = std::fs::remove_dir_all(&root);
        config
    }

    #[test]
    fn offers_double_and_quadruple_under_the_ceiling() {
        assert_eq!(offers(40, None), vec![80, 160]);
        assert_eq!(offers(8192, None), vec![16384, 32768]);
        assert_eq!(offers(8, Some(20)), vec![16, 20]);
        assert_eq!(offers(8, Some(10)), vec![10]);
        assert_eq!(offers(40, Some(40)), Vec::<u32>::new());
        assert_eq!(offers(u32::MAX, None), Vec::<u32>::new());
    }

    #[test]
    fn names_parse_back() {
        for kind in LimitKind::ALL {
            assert_eq!(LimitKind::parse(kind.name()), Some(kind));
        }
        assert_eq!(LimitKind::parse("max_steps"), None);
    }

    #[test]
    fn the_output_cap_is_saved_where_it_takes_effect() {
        let overrides = LimitOverrides {
            steps: Some(80),
            agent_steps: None,
            output: Some(32000),
        };
        let written = |changes: Vec<Change>| -> Vec<(String, Option<i64>)> {
            changes
                .into_iter()
                .map(|(path, value)| (path.join("."), value.and_then(|value| value.as_integer())))
                .collect()
        };
        let plain = config("plain", "[model]\ndefault = \"openrouter:a\"\n");
        assert_eq!(
            written(save_changes(overrides, &plain, "openrouter")),
            vec![
                ("limits.max_steps".to_owned(), Some(80)),
                ("limits.max_output_tokens".to_owned(), Some(32000)),
            ]
        );
        // The provider's own cap wins over `[limits]`, so that is the one to change.
        let per_provider = config(
            "provider",
            "[model]\ndefault = \"openrouter:a\"\n[providers.openrouter]\nmax_tokens = 4096\n",
        );
        assert_eq!(
            written(save_changes(overrides, &per_provider, "openrouter"))[1],
            ("providers.openrouter.max_tokens".to_owned(), Some(32000))
        );
        assert_eq!(
            setting(LimitKind::Output, &per_provider, "openrouter").1,
            "[providers.openrouter] max_tokens"
        );
    }

    #[test]
    fn the_notice_names_the_setting_to_raise() {
        let mut limits = SessionLimits::default();
        assert_eq!(limits.output_setting(), "[limits] max_output_tokens");
        limits.configured_output = "[providers.anthropic] max_tokens".into();
        assert_eq!(limits.output_setting(), "[providers.anthropic] max_tokens");
        limits.set(LimitKind::Output, Some(32000));
        assert_eq!(
            limits.output_setting(),
            "the session's output limit (/limits output)"
        );
    }

    #[test]
    fn hits_say_what_was_reached() {
        let hit = |kind, agent: Option<&str>, current| LimitHit {
            kind,
            agent: agent.map(str::to_owned),
            current,
            offers: Vec::new(),
            run_only: false,
        };
        assert_eq!(
            hit(LimitKind::Steps, None, 40).headline(),
            "Step budget reached (40 steps)"
        );
        assert_eq!(
            hit(LimitKind::AgentSteps, Some("reviewer"), 8).headline(),
            "Sub-agent 'reviewer' reached its step budget (8 steps)"
        );
        assert_eq!(
            hit(LimitKind::Output, Some("reviewer"), 8192).headline(),
            "Sub-agent 'reviewer' reached the output limit (8192 tokens) before any text"
        );
    }
}