sloop-daemon 0.5.0

Agentic coding scheduler — a daemon that runs background coding agents autonomously in isolated git worktrees
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
//! True-simultaneity races against the store's durable guards.
//!
//! In production the database really is written from several connections at
//! once: the dispatcher holds one, every run supervisor opens its own to
//! checkpoint exits (`scheduler.rs`), and crash recovery opens another. The
//! design documents say the *database engine* is the durable backstop — the
//! `leases` primary key and the conditional `UPDATE ... WHERE state = ...`
//! guards — with the supervisor-vs-recovery race decided by whoever
//! checkpoints first. These tests hold those guards under real thread-level
//! simultaneity: N threads, each with its own `Db` connection and local/run
//! facets, releasing from a barrier at the same instant.
//!
//! Time stays injected even here: a shared atomic counter hands every
//! operation a distinct logical timestamp.

use std::sync::Barrier;
use std::sync::atomic::{AtomicI64, Ordering};

use rusqlite::Connection;
use sloop::domain::ticket::TicketState;
use sloop::domain::trigger::TriggerKind;
use sloop::outcome::Outcome;
use sloop::run_store::{Exit, RunAdmission, RunExit, RunStart, Start};
use sloop::work_state::trigger::NewTrigger;
use tempfile::TempDir;

use crate::TestStore;
use crate::model::extra_invariants;

const THREADS: usize = 8;

struct Arena {
    _directory: TempDir,
    db_path: std::path::PathBuf,
    clock: AtomicI64,
}

impl Arena {
    fn new() -> Self {
        let directory = TempDir::new().expect("create tempdir");
        let db_path = directory.path().join("sloop.db");
        let store = TestStore::open(&db_path, 1_000);
        store
            .local
            .insert_local_project("default", "projects/default.md", "Default", 1_000)
            .expect("insert project");
        Self {
            _directory: directory,
            db_path,
            clock: AtomicI64::new(2_000),
        }
    }

    fn now(&self) -> i64 {
        self.clock.fetch_add(1, Ordering::Relaxed)
    }

    fn open(&self) -> TestStore {
        TestStore::open(&self.db_path, self.now())
    }

    fn add_ticket(&self, store: &TestStore, ticket: &str) {
        store
            .local
            .insert_local_ticket(
                ticket,
                "default",
                &format!("tickets/{ticket}.md"),
                &format!("Ticket {ticket}"),
                &[],
                &format!("sloop/{ticket}"),
                Some("opencode"),
                None,
                None,
                "default",
                TicketState::Ready,
                self.now(),
            )
            .expect("insert ticket");
    }

    fn add_trigger(&self, store: &TestStore, id: &str, ticket: &str) {
        store
            .local
            .insert_trigger(
                &NewTrigger {
                    id,
                    kind: TriggerKind::Immediate,
                    ticket_id: Some(ticket),
                    project_id: None,
                    eligible_at_ms: None,
                    interval_ms: None,
                },
                self.now(),
            )
            .expect("insert trigger");
    }

    fn check_invariants(&self) {
        let connection = Connection::open(&self.db_path).expect("open check connection");
        extra_invariants(&connection);
    }
}

fn run_admission<'a>(ticket: &'a str, run_id: &'a str, trigger_id: &'a str) -> RunAdmission<'a> {
    RunAdmission {
        ticket_id: ticket,
        run_id,
        trigger_id,
        flow_json: "{}",
        ticket_json: "{}",
    }
}

fn run_start(run_id: &str) -> RunStart<'_> {
    RunStart {
        run_id,
        branch: "sloop/branch",
        worktree_path: "/tmp/worktree",
        pid: 4_242,
        pid_start_time: Some(7),
        process_group_id: 4_242,
        worker_token: "token",
        worker_socket_path: "/tmp/worker.sock",
    }
}

fn run_exit(run_id: &str) -> RunExit<'_> {
    RunExit {
        run_id,
        attempt: 1,
        exit_code: Some(0),
        capture_complete: true,
        commits_json: "{}",
        vendor_error: None,
        cooldown_until_ms: None,
    }
}

/// Eight connections claim the same ready ticket at the same instant.
/// Exactly one may win; the conditional `UPDATE ... WHERE state='ready'` is
/// the only thing deciding it.
#[test]
fn simultaneous_claims_grant_exactly_one_winner() {
    let arena = Arena::new();
    let setup = arena.open();

    for round in 0..20 {
        let ticket = format!("T{round}");
        let trigger = format!("TR{round}");
        arena.add_ticket(&setup, &ticket);
        arena.add_trigger(&setup, &trigger, &ticket);

        let barrier = Barrier::new(THREADS);
        let grants: Vec<bool> = std::thread::scope(|scope| {
            let handles: Vec<_> = (0..THREADS)
                .map(|thread| {
                    let (arena, ticket, trigger, barrier) = (&arena, &ticket, &trigger, &barrier);
                    scope.spawn(move || {
                        let store = arena.open();
                        let run_id = format!("{ticket}-R{thread}");
                        barrier.wait();
                        crate::claim(
                            &store,
                            &run_admission(ticket, &run_id, trigger),
                            60_000,
                            arena.now(),
                        )
                        .is_some()
                    })
                })
                .collect();
            handles
                .into_iter()
                .map(|h| h.join().expect("join"))
                .collect()
        });

        let winners = grants.iter().filter(|granted| **granted).count();
        assert_eq!(winners, 1, "round {round}: ticket claimed {winners} times");
        arena.check_invariants();
    }
}

/// Eight connections race to checkpoint the same run's exit — the deliberate
/// driver-vs-recovery race, widened. Exactly one owns the walk.
#[test]
fn simultaneous_exit_checkpoints_grant_exactly_one_owner() {
    let arena = Arena::new();
    let setup = arena.open();

    for round in 0..20 {
        let ticket = format!("T{round}");
        let trigger = format!("TR{round}");
        let run_id = format!("{ticket}-R0");
        arena.add_ticket(&setup, &ticket);
        arena.add_trigger(&setup, &trigger, &ticket);
        assert!(
            crate::claim(
                &setup,
                &run_admission(&ticket, &run_id, &trigger),
                60_000,
                arena.now()
            )
            .is_some()
        );
        let started = setup
            .runs
            .start(&run_start(&run_id), arena.now())
            .expect("start");
        assert_eq!(started, Start::Granted);

        let barrier = Barrier::new(THREADS);
        let grants: Vec<bool> = std::thread::scope(|scope| {
            let handles: Vec<_> = (0..THREADS)
                .map(|_| {
                    let (arena, run_id, barrier) = (&arena, &run_id, &barrier);
                    scope.spawn(move || {
                        let store = arena.open();
                        barrier.wait();
                        let exit = store
                            .runs
                            .record_exit(&run_exit(run_id), arena.now())
                            .expect("record_exit must grant or deny, never fail");
                        matches!(exit, Exit::Granted)
                    })
                })
                .collect();
            handles
                .into_iter()
                .map(|h| h.join().expect("join"))
                .collect()
        });

        let owners = grants.iter().filter(|granted| **granted).count();
        assert_eq!(owners, 1, "round {round}: {owners} threads own the walk");
        arena.check_invariants();
    }
}

/// Eight connections race to settle the same run with *different* outcomes.
/// Exactly one settlement lands, and the database tells the winner's story —
/// never a blend of two.
#[test]
fn simultaneous_settlements_land_exactly_once() {
    const OUTCOMES: [Outcome; 4] = [
        Outcome::Merged,
        Outcome::Failed,
        Outcome::Cancelled,
        Outcome::RateLimited,
    ];

    let arena = Arena::new();
    let setup = arena.open();

    for round in 0..20 {
        let ticket = format!("T{round}");
        let trigger = format!("TR{round}");
        let run_id = format!("{ticket}-R0");
        arena.add_ticket(&setup, &ticket);
        arena.add_trigger(&setup, &trigger, &ticket);
        crate::claim(
            &setup,
            &run_admission(&ticket, &run_id, &trigger),
            60_000,
            arena.now(),
        )
        .expect("claim");
        setup
            .runs
            .start(&run_start(&run_id), arena.now())
            .expect("start");
        setup
            .runs
            .record_exit(&run_exit(&run_id), arena.now())
            .expect("record exit");

        let barrier = Barrier::new(THREADS);
        let landed: Vec<Option<Outcome>> = std::thread::scope(|scope| {
            let handles: Vec<_> = (0..THREADS)
                .map(|thread| {
                    let (arena, run_id, barrier) = (&arena, &run_id, &barrier);
                    scope.spawn(move || {
                        let store = arena.open();
                        let outcome = OUTCOMES[thread % OUTCOMES.len()];
                        barrier.wait();
                        let settled = crate::settle(&store, run_id, outcome, arena.now());
                        settled.then_some(outcome)
                    })
                })
                .collect();
            handles
                .into_iter()
                .map(|h| h.join().expect("join"))
                .collect()
        });

        let winners: Vec<Outcome> = landed.into_iter().flatten().collect();
        assert_eq!(winners.len(), 1, "round {round}: {winners:?} all landed");
        let winner = winners[0];

        let connection = Connection::open(&arena.db_path).expect("open check connection");
        let run_state: String = connection
            .query_row("SELECT state FROM runs WHERE id = ?1", [&run_id], |row| {
                row.get(0)
            })
            .expect("run row");
        assert_eq!(run_state, winner.as_str(), "run state is the winner's");
        let ticket_state: String = connection
            .query_row(
                "SELECT state FROM tickets WHERE id = ?1",
                [&ticket],
                |row| row.get(0),
            )
            .expect("ticket row");
        assert_eq!(
            ticket_state,
            TicketState::after_outcome(winner).as_str(),
            "ticket state is the winner's"
        );
        arena.check_invariants();
    }
}

/// Eight connections hammer a small shared ticket pool through whole run
/// lifecycles at once, with no coordination between threads beyond the
/// database itself. Whatever interleaving happens, every operation must
/// resolve to a grant or a denial — never a structural error — and the
/// cross-table invariants must hold at the end.
#[test]
fn uncoordinated_lifecycle_hammer_preserves_invariants() {
    const POOL: [&str; 4] = ["P0", "P1", "P2", "P3"];
    const ITERATIONS: usize = 60;

    let arena = Arena::new();
    let setup = arena.open();
    for ticket in POOL {
        arena.add_ticket(&setup, ticket);
    }

    let barrier = Barrier::new(THREADS);
    let completed: Vec<usize> = std::thread::scope(|scope| {
        let handles: Vec<_> = (0..THREADS)
            .map(|thread| {
                let (arena, barrier) = (&arena, &barrier);
                scope.spawn(move || {
                    let store = arena.open();
                    let mut completed = 0;
                    barrier.wait();
                    for iteration in 0..ITERATIONS {
                        let ticket = POOL[(thread + iteration) % POOL.len()];
                        let trigger = format!("{ticket}-{thread}-{iteration}");
                        let run_id = format!("{ticket}-{thread}-{iteration}-run");
                        arena.add_trigger(&store, &trigger, ticket);

                        if crate::claim(
                            &store,
                            &run_admission(ticket, &run_id, &trigger),
                            60_000,
                            arena.now(),
                        )
                        .is_none()
                        {
                            continue;
                        }
                        // Walk the whole lifecycle; every step must be
                        // granted, because this thread owns the run.
                        assert_eq!(
                            store
                                .runs
                                .start(&run_start(&run_id), arena.now())
                                .expect("start"),
                            Start::Granted
                        );
                        assert_eq!(
                            store
                                .runs
                                .record_exit(&run_exit(&run_id), arena.now())
                                .expect("record exit"),
                            Exit::Granted
                        );
                        let outcome = if iteration % 2 == 0 {
                            Outcome::Cancelled
                        } else {
                            Outcome::Merged
                        };
                        assert!(
                            crate::settle(&store, &run_id, outcome, arena.now()),
                            "the owner's settlement must land"
                        );
                        completed += 1;
                    }
                    completed
                })
            })
            .collect();
        handles
            .into_iter()
            .map(|h| h.join().expect("join"))
            .collect()
    });

    let total: usize = completed.iter().sum();
    assert!(total > 0, "contention must not starve every thread");
    arena.check_invariants();

    // Every granted claim left a settled run behind; nothing leaked.
    let connection = Connection::open(&arena.db_path).expect("open check connection");
    let live_runs: i64 = connection
        .query_row(
            "SELECT COUNT(*) FROM runs WHERE exited_at_ms IS NULL",
            [],
            |row| row.get(0),
        )
        .expect("count live runs");
    assert_eq!(live_runs, 0, "the hammer settles every run it starts");
    let leases: i64 = connection
        .query_row("SELECT COUNT(*) FROM leases", [], |row| row.get(0))
        .expect("count leases");
    assert_eq!(leases, 0, "no lease survives its settled run");
}