errand-bot 0.1.0

Run a coding agent from a chat channel, in a sandbox it cannot escape.
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
488
489
490
491
492
//! Bounds concurrent turns and live sessions, and absorbs provider backoff.
//!
//! Two independent caps, because the costs are independent. A session with a
//! turn in flight costs provider load; a session that merely exists costs
//! memory. One number would force a bad trade in both directions.
//!
//! A slot covers a whole turn rather than an individual model request, because
//! from outside the agent a request inside a tool loop is not visible. A turn
//! is the coarsest unit that can be observed and the finest that can be
//! controlled, so it is the unit.

use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::config::schema::LimitsConfig;

/// What a fired timer asks the scheduler to do.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Timer {
    /// The provider backoff has run out; try the queue again.
    Backoff,
    /// Sweep the queue for prompts that waited too long.
    Sweep,
}

/// Where time comes from. Injected so tests drive it rather than wait for it.
pub trait Clock: Send + Sync + 'static {
    /// Milliseconds since the epoch.
    fn now(&self) -> i64;
    /// Asks to be told when `ms` have passed.
    fn set_timeout(&self, action: Timer, ms: i64) -> u64;
    /// Cancels a timer that has not fired.
    fn clear_timeout(&self, handle: u64);
}

/// The clock the daemon runs on, whose timers run on the async runtime.
#[derive(Clone)]
pub struct SystemClock {
    on_fire: Arc<dyn Fn(Timer) + Send + Sync>,
}

impl SystemClock {
    /// A clock whose fired timers hand the action to `on_fire`, which is where
    /// the daemon wires the scheduler back in.
    pub fn new(on_fire: impl Fn(Timer) + Send + Sync + 'static) -> Self {
        Self {
            on_fire: Arc::new(on_fire),
        }
    }
}

impl Clock for SystemClock {
    fn now(&self) -> i64 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |since| {
                i64::try_from(since.as_millis()).unwrap_or(i64::MAX)
            })
    }

    fn set_timeout(&self, action: Timer, ms: i64) -> u64 {
        let on_fire = Arc::clone(&self.on_fire);
        tokio::spawn(async move {
            let ms = u64::try_from(ms).unwrap_or(0);
            tokio::time::sleep(Duration::from_millis(ms)).await;
            on_fire(action);
        });
        0
    }

    fn clear_timeout(&self, _handle: u64) {
        // A spawned sleep cannot be cancelled through this trait, and does not
        // need to be: a fired timer asks, and an empty scheduler answers by
        // doing nothing.
    }
}

/// Held for the duration of one turn, and released exactly once.
#[derive(Debug)]
pub struct Ticket {
    released_at: Mutex<Option<i64>>,
}

impl Ticket {
    /// Marks the ticket spent. Returns false on a second release.
    fn mark_released(&self, at: i64) -> bool {
        let mut released = self.released_at.lock().expect("a ticket lock");
        if released.is_some() {
            return false;
        }
        *released = Some(at);
        true
    }
}

/// How a submitted prompt was disposed of.
#[derive(Debug)]
pub enum SubmitOutcome {
    /// A slot was free and is now held.
    Admitted { ticket: Ticket },
    /// The prompt waits, at the given position.
    Queued { position: usize },
    /// The queue was full, with words worth posting back.
    Rejected { reason: String },
}

/// A prompt waiting for a turn slot.
pub struct QueueEntry {
    /// The session the prompt belongs to.
    pub session_id: String,
    /// Called when a slot frees and the prompt should be sent.
    pub on_admitted: Box<dyn FnOnce(Ticket) + Send>,
    /// Called when the prompt waited longer than the configured maximum.
    pub on_expired: Box<dyn FnOnce() + Send>,
    /// Called when the prompt's place in the queue changes.
    pub on_position_changed: Option<Box<dyn Fn(usize) + Send>>,
}

struct PendingEntry {
    entry: QueueEntry,
    enqueued_at: i64,
    last_reported_position: usize,
}

struct SchedulerState {
    in_flight: usize,
    live_sessions: usize,
    queue: Vec<PendingEntry>,
    backoff_until: i64,
    backoff_ms: i64,
    backoff_timer: Option<u64>,
    last_start_at: i64,
    sweep_timer: Option<u64>,
}

/// Why the scheduler is currently refusing to admit work.
pub const PAUSED_REASON: &str = "provider backoff";

/// Bounds turns in flight, live sessions, and the queue between them.
pub struct Scheduler {
    limits: LimitsConfig,
    clock: Arc<dyn Clock>,
    base_backoff_ms: i64,
    max_backoff_ms: i64,
    start_interval_ms: i64,
    state: Mutex<SchedulerState>,
}

impl Scheduler {
    /// A scheduler over `limits`, with its timers on `clock`.
    ///
    /// Returns it behind an arc, because the timers it schedules call back
    /// into it when they fire.
    pub fn start(
        limits: LimitsConfig,
        clock: Arc<dyn Clock>,
        base_backoff_ms: i64,
        max_backoff_ms: i64,
        start_interval_ms: i64,
    ) -> Arc<Self> {
        Arc::new(Self {
            limits,
            clock,
            base_backoff_ms,
            max_backoff_ms,
            start_interval_ms,
            state: Mutex::new(SchedulerState {
                in_flight: 0,
                live_sessions: 0,
                queue: Vec::new(),
                backoff_until: 0,
                backoff_ms: base_backoff_ms,
                backoff_timer: None,
                last_start_at: 0,
                sweep_timer: None,
            }),
        })
    }

    /// Sessions currently holding a slot for a running turn.
    pub fn turns_in_flight(&self) -> usize {
        self.state.lock().expect("the scheduler lock").in_flight
    }

    /// Prompts currently waiting for a slot.
    pub fn queue_length(&self) -> usize {
        self.state.lock().expect("the scheduler lock").queue.len()
    }

    /// Sessions that exist, running a turn or not.
    #[allow(
        dead_code,
        reason = "read by this module's tests, which assert on state the daemon never asks for"
    )]
    pub fn sessions(&self) -> usize {
        self.state.lock().expect("the scheduler lock").live_sessions
    }

    /// Set while the scheduler is refusing to admit work, with the reason.
    pub fn paused_because(&self) -> Option<&'static str> {
        let state = self.state.lock().expect("the scheduler lock");
        (self.clock.now() < state.backoff_until).then_some(PAUSED_REASON)
    }

    /// How long the current backoff will last, for reporting.
    #[allow(
        dead_code,
        reason = "read by this module's tests, which assert on state the daemon never asks for"
    )]
    pub fn backoff_remaining_ms(&self) -> i64 {
        let state = self.state.lock().expect("the scheduler lock");
        (state.backoff_until - self.clock.now()).max(0)
    }

    /// Reserves capacity for a new session, returning how long to wait before
    /// starting, or nothing when the cap is reached.
    pub fn reserve_session(&self) -> Option<i64> {
        let mut state = self.state.lock().expect("the scheduler lock");
        if state.live_sessions >= self.limits.max_live_sessions as usize {
            return None;
        }
        state.live_sessions += 1;

        let now = self.clock.now();
        let earliest = state.last_start_at + self.start_interval_ms;
        let delay = (earliest - now).max(0);
        state.last_start_at = now.max(earliest);
        Some(delay)
    }

    /// Releases a session's reservation when it ends.
    pub fn release_session(&self) {
        let mut state = self.state.lock().expect("the scheduler lock");
        if state.live_sessions > 0 {
            state.live_sessions -= 1;
        }
    }

    /// Why a session was refused, in words worth posting back to the channel.
    pub fn session_refused_reason(&self) -> String {
        format!(
            "the session limit of {} is reached, so this message did not start one",
            self.limits.max_live_sessions
        )
    }

    /// Takes a slot if one is free now, and does not queue when none is.
    ///
    /// For work that is worth doing only immediately: a delegated subtask
    /// waiting behind a queue would stall the turn it was meant to make
    /// cheaper, so it is given up on instead.
    pub fn try_admit(self: &Arc<Self>) -> Option<Ticket> {
        if !self.can_admit_now() {
            return None;
        }
        self.state.lock().expect("the scheduler lock").in_flight += 1;
        Some(Ticket {
            released_at: Mutex::new(None),
        })
    }

    /// Submits a prompt, admitting it now or queueing it behind the cap.
    pub fn submit(self: &Arc<Self>, entry: QueueEntry) -> SubmitOutcome {
        if let Some(ticket) = self.try_admit() {
            return SubmitOutcome::Admitted { ticket };
        }

        let mut state = self.state.lock().expect("the scheduler lock");
        if state.queue.len() >= self.limits.max_queue_length as usize {
            return SubmitOutcome::Rejected {
                reason: format!(
                    "the queue is full at {} waiting prompts, so this message was not accepted",
                    self.limits.max_queue_length
                ),
            };
        }

        let position = state.queue.len() + 1;
        state.queue.push(PendingEntry {
            entry,
            enqueued_at: self.clock.now(),
            last_reported_position: position,
        });
        drop(state);
        self.schedule_sweep();
        SubmitOutcome::Queued { position }
    }

    /// Releases a turn slot. Every exit path calls this: completion, abort,
    /// error, sandbox death, and session termination.
    ///
    /// Returns false when the ticket was already released, which is a bug in
    /// the caller rather than something to absorb silently.
    pub fn release(self: &Arc<Self>, ticket: &Ticket) -> bool {
        if !ticket.mark_released(self.clock.now()) {
            return false;
        }
        {
            let mut state = self.state.lock().expect("the scheduler lock");
            if state.in_flight > 0 {
                state.in_flight -= 1;
            }
        }
        self.pump();
        true
    }

    /// Drops a session's queued prompts when the session ends.
    pub fn cancel_session(&self, session_id: &str) -> usize {
        let mut state = self.state.lock().expect("the scheduler lock");
        let before = state.queue.len();
        state
            .queue
            .retain(|pending| pending.entry.session_id != session_id);
        let removed = before - state.queue.len();
        if removed > 0 {
            drop(state);
            self.report_positions();
        }
        removed
    }

    /// Records that the provider signalled rate limiting.
    ///
    /// Admission stops globally, not just for the session that noticed. The
    /// limit is per account, so a limit one session hit is information about
    /// all of them, and backing off only the one that noticed leaves the rest
    /// pushing into the same wall.
    pub fn note_rate_limit(self: &Arc<Self>) {
        let backoff_ms = {
            let mut state = self.state.lock().expect("the scheduler lock");
            let now = self.clock.now();
            if now < state.backoff_until {
                state.backoff_ms = (state.backoff_ms * 2).min(self.max_backoff_ms);
            }
            state.backoff_until = now + state.backoff_ms;
            state.backoff_ms
        };

        let previous = {
            let mut state = self.state.lock().expect("the scheduler lock");
            state
                .backoff_timer
                .replace(self.clock.set_timeout(Timer::Backoff, backoff_ms))
        };
        if let Some(handle) = previous {
            self.clock.clear_timeout(handle);
        }
    }

    /// Records a turn that completed without rate limiting, decaying the pause.
    pub fn note_success(&self) {
        let mut state = self.state.lock().expect("the scheduler lock");
        if self.clock.now() < state.backoff_until {
            return;
        }
        state.backoff_ms = self.base_backoff_ms.max(state.backoff_ms / 2);
    }

    /// Drops prompts that have waited longer than the configured maximum.
    pub fn expire_stale(&self) -> usize {
        let expired: Vec<PendingEntry>;
        {
            let mut state = self.state.lock().expect("the scheduler lock");
            #[expect(clippy::cast_possible_wrap)]
            let wait = self.limits.max_queue_wait_ms as i64;
            let cutoff = self.clock.now() - wait;
            let due: Vec<usize> = state
                .queue
                .iter()
                .enumerate()
                .filter(|(_, pending)| pending.enqueued_at <= cutoff)
                .map(|(index, _)| index)
                .collect();
            // Removed newest first, which is the order they are told about it.
            expired = due
                .into_iter()
                .rev()
                .map(|index| state.queue.remove(index))
                .collect();
            if expired.is_empty() {
                return 0;
            }
        }
        let count = expired.len();
        for pending in expired {
            (pending.entry.on_expired)();
        }
        self.report_positions();
        count
    }

    /// Stops every timer, so the daemon can exit.
    pub fn shutdown(&self) {
        let mut state = self.state.lock().expect("the scheduler lock");
        if let Some(handle) = state.backoff_timer.take() {
            self.clock.clear_timeout(handle);
        }
        if let Some(handle) = state.sweep_timer.take() {
            self.clock.clear_timeout(handle);
        }
        state.queue.clear();
    }

    /// The timer the backoff asked for has run out; the queue may go again.
    pub fn timer_fired(self: &Arc<Self>, action: Timer) {
        match action {
            Timer::Backoff => {
                {
                    let mut state = self.state.lock().expect("the scheduler lock");
                    state.backoff_timer = None;
                }
                self.pump();
            }
            Timer::Sweep => {
                {
                    let mut state = self.state.lock().expect("the scheduler lock");
                    state.sweep_timer = None;
                }
                self.expire_stale();
                let waiting = self.state.lock().expect("the scheduler lock").queue.len();
                if waiting > 0 {
                    self.schedule_sweep();
                }
            }
        }
    }

    fn can_admit_now(&self) -> bool {
        // One lock: the backoff check reads the same state the cap reads.
        let state = self.state.lock().expect("the scheduler lock");
        state.in_flight < self.limits.max_concurrent_turns as usize
            && self.clock.now() >= state.backoff_until
    }

    fn pump(self: &Arc<Self>) {
        loop {
            let next = {
                let mut state = self.state.lock().expect("the scheduler lock");
                let backoff_over = self.clock.now() >= state.backoff_until;
                if state.queue.is_empty()
                    || state.in_flight >= self.limits.max_concurrent_turns as usize
                    || !backoff_over
                {
                    break;
                }
                let next = state.queue.remove(0);
                state.in_flight += 1;
                next
            };
            (next.entry.on_admitted)(Ticket {
                released_at: Mutex::new(None),
            });
        }
        self.report_positions();
    }

    /// Tells each waiting prompt its position, but only when it changed, so a
    /// thread gets one message updated rather than a message per shuffle.
    fn report_positions(&self) {
        let mut state = self.state.lock().expect("the scheduler lock");
        for (index, pending) in state.queue.iter_mut().enumerate() {
            let position = index + 1;
            if pending.last_reported_position == position {
                continue;
            }
            pending.last_reported_position = position;
            if let Some(report) = &mut pending.entry.on_position_changed {
                report(position);
            }
        }
    }

    fn schedule_sweep(self: &Arc<Self>) {
        let mut state = self.state.lock().expect("the scheduler lock");
        if state.sweep_timer.is_some() {
            return;
        }
        state.sweep_timer = Some(self.clock.set_timeout(Timer::Sweep, self.sweep_interval()));
    }

    fn sweep_interval(&self) -> i64 {
        {
            #[expect(clippy::cast_possible_wrap)]
            let interval = (self.limits.max_queue_wait_ms / 4) as i64;
            interval.max(1)
        }
    }
}

#[cfg(test)]
mod tests;