marver 0.0.27

A TUI workspace for AI agent sessions: tmux orchestration, git worktree management, and repo control in one place.
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Deciding which queued tasks may start.
//!
//! One global cap. The subtlety is which states count against it:
//!
//! | State | Holds a slot | Why |
//! |---|---|---|
//! | `running` | yes | obviously working |
//! | `blocked` | **yes** | resumes the instant the user answers, so freeing its slot would let answering two prompts put you over the cap |
//! | `awaiting-review` | **no** | the turn is finished; counting it would let unreviewed work starve the queue |
//! | `paused` | **no** | releasing the slot is the point of pausing |
//! | `queued`, terminal | no | nothing is running |
//!
//! A paused task is not `queued`, so it is never picked — which is why `paused`
//! is a state rather than a flag this has to remember to consult.
//!
//! [`Scheduler::plan`] is a pure query over the store, so the policy is testable
//! without worktrees or tmux; [`Scheduler::tick`] adds the effects through
//! [`Launch`].

use chrono::{DateTime, Utc};

use crate::domain::{Task, TaskState};
use crate::store::{Store, Transition};

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    Store(#[from] crate::store::Error),
}

pub type Result<T> = std::result::Result<T, Error>;

/// States that consume a concurrency slot.
pub const OCCUPYING: &[TaskState] = &[TaskState::Running, TaskState::Blocked];

/// How many agents may be live at once when nothing says otherwise.
pub const DEFAULT_CAP: usize = 3;

/// Starts a task for real: worktrees, tmux session, agent.
pub trait Launch {
    /// Bring `task` to life. The error is recorded as the task's failure
    /// reason, so it should read as an explanation rather than a code.
    fn launch(&self, store: &mut Store, task: &Task) -> std::result::Result<(), String>;
}

/// What one pass of the scheduler did.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Tick {
    /// Tasks now running.
    pub started: Vec<i64>,
    /// Tasks that could not be started, with the reason. Each was moved to
    /// `failed`, not left queued.
    pub failed: Vec<(i64, String)>,
    /// Tasks that left `queued` while they were being started, with where they
    /// went. Their agents are live and their state is not `running`.
    pub abandoned: Vec<(i64, TaskState)>,
    /// Slots still free after this pass.
    pub remaining: usize,
}

pub struct Scheduler {
    cap: usize,
}

impl Scheduler {
    pub fn new(cap: usize) -> Self {
        Self { cap }
    }

    pub fn cap(&self) -> usize {
        self.cap
    }

    /// How many slots are in use.
    pub fn occupied(&self, store: &Store) -> Result<usize> {
        let mut total = 0;
        for state in OCCUPYING {
            total += store.list_tasks_in_state(*state)?.len();
        }
        Ok(total)
    }

    /// How many tasks could start right now.
    pub fn available(&self, store: &Store) -> Result<usize> {
        Ok(self.cap.saturating_sub(self.occupied(store)?))
    }

    /// Which queued tasks should start, oldest first.
    pub fn plan(&self, store: &Store) -> Result<Vec<Task>> {
        let free = self.available(store)?;
        if free == 0 {
            return Ok(Vec::new());
        }
        let mut queued = store.list_tasks_in_state(TaskState::Queued)?;
        // Oldest first: ids ascend with creation, and list_tasks_in_state
        // already orders by id, but say so rather than rely on it.
        queued.sort_by_key(|task| task.id);
        queued.truncate(free);
        Ok(queued)
    }

    /// Plan, launch, and record the results.
    pub fn tick(
        &self,
        store: &mut Store,
        launcher: &impl Launch,
        now: DateTime<Utc>,
    ) -> Result<Tick> {
        let mut tick = Tick::default();

        for task in self.plan(store)? {
            match launcher.launch(store, &task) {
                Ok(()) => {
                    // `transition_from`, naming the state this decision was
                    // made in.
                    match store.transition_from(
                        task.id,
                        TaskState::Queued,
                        TaskState::Running,
                        Transition::Plain,
                        now,
                    ) {
                        Ok(_) => tick.started.push(task.id),
                        Err(crate::store::Error::IllegalTransition { from, .. }) => {
                            tick.abandoned.push((task.id, from));
                        }
                        Err(err) => return Err(err.into()),
                    }
                }
                Err(reason) => {
                    store.transition(
                        task.id,
                        TaskState::Failed,
                        Transition::Failed(reason.clone()),
                        now,
                    )?;
                    tick.failed.push((task.id, reason));
                }
            }
        }

        tick.remaining = self.available(store)?;
        Ok(tick)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::BlockedKind;
    use crate::store::BlockedInfo;
    use std::cell::RefCell;
    use std::path::Path;

    fn at(secs: i64) -> DateTime<Utc> {
        DateTime::from_timestamp(secs, 0).expect("valid timestamp")
    }

    /// Records what it was asked to start; optionally refuses named tasks.
    #[derive(Default)]
    struct Recorder {
        seen: RefCell<Vec<i64>>,
        refuse: Vec<i64>,
    }

    impl Recorder {
        fn refusing(ids: &[i64]) -> Self {
            Self {
                seen: RefCell::new(Vec::new()),
                refuse: ids.to_vec(),
            }
        }

        fn launched(&self) -> Vec<i64> {
            self.seen.borrow().clone()
        }
    }

    impl Launch for Recorder {
        fn launch(&self, _store: &mut Store, task: &Task) -> std::result::Result<(), String> {
            self.seen.borrow_mut().push(task.id);
            if self.refuse.contains(&task.id) {
                return Err(format!("no worktree for task {}", task.id));
            }
            Ok(())
        }
    }

    fn store() -> Store {
        Store::open_in_memory().expect("store")
    }

    fn queue(store: &mut Store, count: usize) -> Vec<i64> {
        (0..count)
            .map(|i| {
                store
                    .create_task(
                        &format!("task {i}"),
                        "do it",
                        Path::new("/tmp/tasks"),
                        &[],
                        at(0),
                    )
                    .unwrap()
                    .id
            })
            .collect()
    }

    fn run(store: &mut Store, id: i64) {
        store
            .transition(id, TaskState::Running, Transition::Plain, at(1))
            .unwrap();
    }

    fn block(store: &mut Store, id: i64) {
        run(store, id);
        store
            .transition(
                id,
                TaskState::Blocked,
                Transition::Blocked(BlockedInfo::new(BlockedKind::Question)),
                at(2),
            )
            .unwrap();
    }

    fn review(store: &mut Store, id: i64) {
        run(store, id);
        store
            .transition(id, TaskState::AwaitingReview, Transition::Plain, at(2))
            .unwrap();
    }

    fn hold(store: &mut Store, id: i64) {
        store
            .transition(id, TaskState::Paused, Transition::Plain, at(2))
            .unwrap();
    }

    #[test]
    fn a_paused_task_is_passed_over_for_the_next_one() {
        // Pausing before a task starts is only useful if the queue moves on
        // without it.
        let mut store = store();
        let ids = queue(&mut store, 2);
        hold(&mut store, ids[0]);

        let scheduler = Scheduler::new(3);
        let launcher = Recorder::default();
        let pass = scheduler.tick(&mut store, &launcher, at(3)).unwrap();

        assert_eq!(pass.started, [ids[1]]);
        assert_eq!(
            store.get_task(ids[0]).unwrap().state,
            TaskState::Paused,
            "a held task stays held"
        );
    }

    #[test]
    fn pausing_a_working_task_frees_its_slot() {
        // The reason pausing a running agent is worth having at all: the cap
        // is full, and setting one aside lets the next task start.
        let mut store = store();
        let ids = queue(&mut store, 3);
        run(&mut store, ids[0]);
        run(&mut store, ids[1]);

        let scheduler = Scheduler::new(2);
        assert_eq!(scheduler.available(&store).unwrap(), 0);

        hold(&mut store, ids[0]);

        assert_eq!(scheduler.available(&store).unwrap(), 1);
        let pass = scheduler
            .tick(&mut store, &Recorder::default(), at(3))
            .unwrap();
        assert_eq!(pass.started, [ids[2]]);
    }

    #[test]
    fn an_empty_queue_starts_nothing() {
        let store = store();
        let tick = Scheduler::new(3).plan(&store).unwrap();
        assert!(tick.is_empty());
    }

    #[test]
    fn starts_up_to_the_cap_oldest_first() {
        let mut store = store();
        let ids = queue(&mut store, 5);
        let launcher = Recorder::default();

        let tick = Scheduler::new(2)
            .tick(&mut store, &launcher, at(5))
            .unwrap();

        assert_eq!(tick.started, ids[..2]);
        assert_eq!(launcher.launched(), ids[..2], "oldest queued go first");
        assert_eq!(tick.remaining, 0);
        assert_eq!(
            store.list_tasks_in_state(TaskState::Queued).unwrap().len(),
            3
        );
    }

    #[test]
    fn a_cap_larger_than_the_queue_starts_everything() {
        let mut store = store();
        let ids = queue(&mut store, 2);
        let tick = Scheduler::new(10)
            .tick(&mut store, &Recorder::default(), at(5))
            .unwrap();
        assert_eq!(tick.started, ids);
        assert_eq!(tick.remaining, 8);
    }

    #[test]
    fn a_cap_of_zero_starts_nothing() {
        let mut store = store();
        queue(&mut store, 3);
        let launcher = Recorder::default();
        let tick = Scheduler::new(0)
            .tick(&mut store, &launcher, at(5))
            .unwrap();
        assert!(tick.started.is_empty());
        assert!(launcher.launched().is_empty(), "nothing should be touched");
    }

    #[test]
    fn a_blocked_task_still_holds_its_slot() {
        let mut store = store();
        let ids = queue(&mut store, 3);
        block(&mut store, ids[0]);

        let scheduler = Scheduler::new(1);
        assert_eq!(scheduler.occupied(&store).unwrap(), 1);
        assert!(
            scheduler.plan(&store).unwrap().is_empty(),
            "a blocked agent resumes the moment it is answered, so its slot is \
             not free"
        );
    }

    #[test]
    fn a_task_awaiting_review_frees_its_slot() {
        let mut store = store();
        let ids = queue(&mut store, 3);
        review(&mut store, ids[0]);

        let scheduler = Scheduler::new(1);
        assert_eq!(scheduler.occupied(&store).unwrap(), 0);
        let planned = scheduler.plan(&store).unwrap();
        assert_eq!(
            planned.iter().map(|t| t.id).collect::<Vec<_>>(),
            [ids[1]],
            "unreviewed work must not starve the queue"
        );
    }

    #[test]
    fn terminal_tasks_never_hold_a_slot() {
        let mut store = store();
        let ids = queue(&mut store, 4);
        review(&mut store, ids[0]);
        store
            .transition(ids[0], TaskState::Committed, Transition::Plain, at(3))
            .unwrap();
        store
            .transition(ids[1], TaskState::Cancelled, Transition::Plain, at(3))
            .unwrap();
        run(&mut store, ids[2]);
        store
            .transition(
                ids[2],
                TaskState::Failed,
                Transition::Failed("died".into()),
                at(3),
            )
            .unwrap();

        assert_eq!(Scheduler::new(2).occupied(&store).unwrap(), 0);
    }

    #[test]
    fn a_full_scheduler_is_a_no_op() {
        let mut store = store();
        let ids = queue(&mut store, 3);
        run(&mut store, ids[0]);
        run(&mut store, ids[1]);

        let launcher = Recorder::default();
        let tick = Scheduler::new(2)
            .tick(&mut store, &launcher, at(5))
            .unwrap();

        assert!(tick.started.is_empty());
        assert!(launcher.launched().is_empty());
        assert_eq!(store.get_task(ids[2]).unwrap().state, TaskState::Queued);
    }

    #[test]
    fn lowering_the_cap_below_what_is_live_does_not_underflow() {
        let mut store = store();
        let ids = queue(&mut store, 3);
        run(&mut store, ids[0]);
        run(&mut store, ids[1]);

        let scheduler = Scheduler::new(1);
        assert_eq!(scheduler.available(&store).unwrap(), 0);
        assert!(
            scheduler.plan(&store).unwrap().is_empty(),
            "over-capacity must not start more, nor panic"
        );
        // Nothing already running is disturbed.
        assert_eq!(store.get_task(ids[0]).unwrap().state, TaskState::Running);
    }

    #[test]
    fn a_task_that_cannot_start_fails_rather_than_requeueing() {
        let mut store = store();
        let ids = queue(&mut store, 2);
        let launcher = Recorder::refusing(&[ids[0]]);

        let tick = Scheduler::new(2)
            .tick(&mut store, &launcher, at(5))
            .unwrap();

        assert_eq!(tick.started, [ids[1]]);
        assert_eq!(tick.failed.len(), 1);
        assert_eq!(tick.failed[0].0, ids[0]);

        let failed = store.get_task(ids[0]).unwrap();
        assert_eq!(failed.state, TaskState::Failed);
        assert_eq!(
            failed.failure_reason.as_deref(),
            Some(format!("no worktree for task {}", ids[0]).as_str()),
            "the launcher's explanation must survive"
        );
    }

    #[test]
    fn a_failed_launch_frees_its_slot_for_the_next_pass() {
        let mut store = store();
        let ids = queue(&mut store, 3);

        // First pass: the only slot goes to a task that cannot start.
        let scheduler = Scheduler::new(1);
        scheduler
            .tick(&mut store, &Recorder::refusing(&[ids[0]]), at(5))
            .unwrap();
        assert_eq!(store.get_task(ids[0]).unwrap().state, TaskState::Failed);

        // Second pass: the slot is free again, so the queue advances rather
        // than being wedged behind the broken task.
        let launcher = Recorder::default();
        let tick = scheduler.tick(&mut store, &launcher, at(6)).unwrap();
        assert_eq!(tick.started, [ids[1]]);
    }

    #[test]
    fn repeated_ticks_do_not_double_start() {
        let mut store = store();
        queue(&mut store, 5);
        let scheduler = Scheduler::new(2);
        let launcher = Recorder::default();

        let first = scheduler.tick(&mut store, &launcher, at(5)).unwrap();
        let second = scheduler.tick(&mut store, &launcher, at(6)).unwrap();

        assert_eq!(first.started.len(), 2);
        assert!(second.started.is_empty());
        assert_eq!(launcher.launched().len(), 2, "no task launched twice");
    }

    #[test]
    fn finishing_a_task_lets_the_queue_advance() {
        let mut store = store();
        let ids = queue(&mut store, 3);
        let scheduler = Scheduler::new(1);
        let launcher = Recorder::default();

        scheduler.tick(&mut store, &launcher, at(5)).unwrap();
        assert_eq!(launcher.launched(), [ids[0]]);

        // The agent finishes; its slot is released.
        store
            .transition(ids[0], TaskState::AwaitingReview, Transition::Plain, at(6))
            .unwrap();
        let tick = scheduler.tick(&mut store, &launcher, at(7)).unwrap();

        assert_eq!(tick.started, [ids[1]]);
    }

    #[test]
    fn answering_a_blocked_task_does_not_exceed_the_cap() {
        let mut store = store();
        let ids = queue(&mut store, 4);
        let scheduler = Scheduler::new(2);
        let launcher = Recorder::default();

        scheduler.tick(&mut store, &launcher, at(5)).unwrap();
        // Both live agents get stuck on a prompt.
        for id in &ids[..2] {
            store
                .transition(
                    *id,
                    TaskState::Blocked,
                    Transition::Blocked(BlockedInfo::new(BlockedKind::PermissionPrompt)),
                    at(6),
                )
                .unwrap();
        }

        let tick = scheduler.tick(&mut store, &launcher, at(7)).unwrap();
        assert!(
            tick.started.is_empty(),
            "starting here would mean four live agents once both prompts are \
             answered, with a cap of two"
        );
    }
}

#[cfg(test)]
mod race_tests {
    use super::*;
    use std::cell::RefCell;
    use std::path::Path;

    fn at(secs: i64) -> DateTime<Utc> {
        DateTime::from_timestamp(secs, 0).expect("valid timestamp")
    }

    /// Stands in for the user pausing a task during the window where the
    /// launcher is making worktrees and starting tmux.
    struct PausesWhileLaunching {
        seen: RefCell<Vec<i64>>,
    }

    impl Launch for PausesWhileLaunching {
        fn launch(&self, store: &mut Store, task: &Task) -> std::result::Result<(), String> {
            self.seen.borrow_mut().push(task.id);
            store
                .transition(task.id, TaskState::Paused, Transition::Plain, at(9))
                .expect("the user can pause a queued task at any moment");
            Ok(())
        }
    }

    #[test]
    fn a_task_paused_during_its_launch_stays_paused() {
        let mut store = Store::open_in_memory().unwrap();
        let id = store
            .create_task("held", "do it", Path::new("/tmp/tasks"), &[], at(0))
            .unwrap()
            .id;

        let launcher = PausesWhileLaunching {
            seen: RefCell::new(Vec::new()),
        };
        let _ = Scheduler::new(3).tick(&mut store, &launcher, at(10));

        assert_eq!(
            store.get_task(id).unwrap().state,
            TaskState::Paused,
            "the scheduler must not undo a pause that landed while it was starting the task"
        );
    }
}