atelier-sdk 0.1.0

The atelier SDK and engine: workspaces, snapshots, sessions, gated landing, and the journal
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
//! Sessions and the landing gate: every legal transition lands and each
//! illegal one refuses by name — open, request, approve, reject, park,
//! abandon, and the races between them.
#![expect(
    clippy::too_many_lines,
    reason = "a test tells one story end to end; fragmenting it would hide the transition being pinned"
)]

use std::fs;
use std::path::Path;
use std::sync::{Mutex, MutexGuard, OnceLock};

use atelier_sdk::{
    Act, Actor, ActorKind, Error, GateOutcome, Instruction, RequestState, SessionState, Workspace,
};

/// Serialize tests: they all set the process-wide `ATELIER_CONFIG_HOME`.
fn env_lock() -> MutexGuard<'static, ()> {
    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
    LOCK.get_or_init(|| Mutex::new(()))
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

#[expect(unsafe_code, reason = "set_var wires the workspace to the test config")]
fn set_actor(config_home: &Path) {
    fs::create_dir_all(config_home).expect("create config home");
    fs::write(
        config_home.join("config.toml"),
        "[actor]\nname = \"test-actor\"\nkind = \"human\"\n",
    )
    .expect("write actor config");
    // SAFETY: every test holds `env_lock()` for its whole body, so no other
    // thread reads or writes the environment concurrently.
    unsafe {
        std::env::set_var("ATELIER_CONFIG_HOME", config_home);
    }
}

fn agent() -> Actor {
    Actor {
        name: "scribe".to_owned(),
        kind: ActorKind::Agent,
    }
}

fn human() -> Actor {
    Actor {
        name: "test-actor".to_owned(),
        kind: ActorKind::Human,
    }
}

fn instruction() -> Instruction {
    Instruction {
        summary: "redline the draft".to_owned(),
        run_ref: Some("bip:run/42".to_owned()),
        verbatim: Some("please redline the draft".to_owned()),
    }
}

#[test]
fn an_agent_session_lands_through_the_gate() {
    let _guard = env_lock();
    let config = tempfile::tempdir().unwrap();
    set_actor(config.path());
    let root = tempfile::tempdir().unwrap();

    let mut ws = Workspace::init(root.path()).unwrap();
    fs::write(root.path().join("notes.txt"), "shared draft\n").unwrap();

    let session = ws.open_session(&agent(), &instruction()).unwrap();
    assert_eq!(session.id.to_string(), "s1");
    assert_eq!(session.state, SessionState::Open);
    assert_eq!(session.actor, agent());
    assert!(session.working_copy.ends_with(".atelier/sessions/s1"));
    assert_eq!(
        fs::read_to_string(session.working_copy.join("notes.txt")).unwrap(),
        "shared draft\n"
    );

    // The session edits in isolation: the shared line must not see it.
    ws.session_write(session.id, "notes.txt", "agent draft\n")
        .unwrap();
    assert_eq!(
        fs::read_to_string(root.path().join("notes.txt")).unwrap(),
        "shared draft\n"
    );

    let diff = ws.session_diff(session.id).unwrap();
    assert_eq!(diff.deltas.len(), 1);
    assert_eq!(diff.deltas[0].address.as_str(), "notes.txt");
    assert!(
        diff.deltas[0]
            .lines
            .iter()
            .any(|line| line.text == "agent draft")
    );

    let request = ws.request_land(session.id).unwrap();
    assert_eq!(request.id.to_string(), "r1");
    assert_eq!(request.state, RequestState::Open);
    // Asking again returns the request already holding the gate.
    assert_eq!(ws.request_land(session.id).unwrap().id, request.id);

    let outcome = ws.approve(request.id, &agent()).unwrap();
    let GateOutcome::Landed { landings } = outcome else {
        panic!("self-approval under the default policy must land, got {outcome:?}");
    };
    assert_eq!(landings.len(), 1);
    assert_eq!(landings[0].source, None);
    let snapshot = landings[0].snapshot.clone();

    // The shared line advanced to the landed snapshot, attributed to the
    // agent, and the root working copy materialized the change.
    let log = ws.log(5).unwrap();
    assert_eq!(log[0].snapshot.id, snapshot);
    assert_eq!(log[0].snapshot.actor, "scribe");
    assert_eq!(
        fs::read_to_string(root.path().join("notes.txt")).unwrap(),
        "agent draft\n"
    );

    assert_eq!(ws.request(request.id).unwrap().state, RequestState::Landed);
    assert_eq!(ws.session(session.id).unwrap().state, SessionState::Landed);

    // The journal groups the session's acts under it, with the
    // instruction's summary and run reference.
    let entries = ws.journal(50).unwrap();
    let opened = entries
        .iter()
        .find(|entry| entry.act == Act::SessionOpen)
        .expect("session_open is journaled");
    assert_eq!(opened.session.as_deref(), Some("s1"));
    assert_eq!(
        opened.instruction_summary.as_deref(),
        Some("redline the draft")
    );
    assert_eq!(opened.instruction_run_ref.as_deref(), Some("bip:run/42"));
    // The default policy keeps the summary, never the verbatim prompt.
    assert_eq!(opened.instruction_verbatim, None);
    for act in [Act::LandRequest, Act::Approve, Act::Land] {
        let entry = entries
            .iter()
            .find(|entry| entry.act == act)
            .unwrap_or_else(|| panic!("{act} is journaled"));
        assert_eq!(entry.session.as_deref(), Some("s1"));
    }

    // A landed session is closed for work.
    let refused = ws.session_write(session.id, "notes.txt", "more\n");
    assert!(matches!(refused, Err(Error::SessionClosed { .. })));
}

#[test]
fn landing_into_a_moved_conflicting_line_parks_the_request() {
    let _guard = env_lock();
    let config = tempfile::tempdir().unwrap();
    set_actor(config.path());
    let root = tempfile::tempdir().unwrap();

    let mut ws = Workspace::init(root.path()).unwrap();
    fs::write(root.path().join("notes.txt"), "first line\n").unwrap();

    let session = ws.open_session(&agent(), &instruction()).unwrap();
    ws.session_write(session.id, "notes.txt", "agent version\n")
        .unwrap();

    // The shared line moves with a conflicting edit.
    fs::write(root.path().join("notes.txt"), "human version\n").unwrap();
    let head_before = ws.log(1).unwrap()[0].snapshot.id.clone();

    let request = ws.request_land(session.id).unwrap();
    let outcome = ws.approve(request.id, &agent()).unwrap();
    let GateOutcome::Parked {
        request: parked,
        landings,
        parked: parked_sources,
    } = outcome
    else {
        panic!("a conflicting apply must park, got {outcome:?}");
    };
    assert_eq!(parked.state, RequestState::Parked);
    // The root is the parked line; nothing landed under this request.
    assert_eq!(landings, Vec::new());
    assert_eq!(parked_sources, vec![None]);

    // The shared line did not move and never carries the conflict.
    assert_eq!(ws.log(1).unwrap()[0].snapshot.id, head_before);
    assert_eq!(
        fs::read_to_string(root.path().join("notes.txt")).unwrap(),
        "human version\n"
    );
    assert!(
        ws.journal(50)
            .unwrap()
            .iter()
            .any(|entry| entry.act == Act::LandParked)
    );

    // A parked request refuses approval; resolution is a new snapshot.
    let refused = ws.approve(request.id, &human());
    assert!(matches!(refused, Err(Error::RequestParked(_))));

    // Matching the shared line's content resolves the conflict; the new
    // snapshot re-opens the gate and the change lands.
    ws.session_write(session.id, "notes.txt", "human version\n")
        .unwrap();
    let reopened = ws.request_land(session.id).unwrap();
    assert_eq!(reopened.id, request.id);
    assert_eq!(reopened.state, RequestState::Open);
    let outcome = ws.approve(request.id, &agent()).unwrap();
    assert!(matches!(outcome, GateOutcome::Landed { .. }));
}

#[test]
fn a_new_snapshot_after_approval_dismisses_it_and_reopens_the_gate() {
    let _guard = env_lock();
    let config = tempfile::tempdir().unwrap();
    set_actor(config.path());
    let root = tempfile::tempdir().unwrap();

    let mut ws = Workspace::init(root.path()).unwrap();
    fs::write(
        root.path().join(".atelier/config.toml"),
        "schema = 1\n\n[workspace]\nname = \"gated\"\n\n[landing]\napprovals = 2\n",
    )
    .unwrap();

    let session = ws.open_session(&agent(), &instruction()).unwrap();
    ws.session_write(session.id, "notes.txt", "first draft\n")
        .unwrap();
    let request = ws.request_land(session.id).unwrap();

    let outcome = ws.approve(request.id, &human()).unwrap();
    let GateOutcome::Pending {
        request: pending,
        required,
    } = outcome
    else {
        panic!("one of two approvals must stay pending, got {outcome:?}");
    };
    assert_eq!((pending.approvals.len(), required), (1, 2));

    // A new snapshot on the change dismisses the approval.
    ws.session_write(session.id, "notes.txt", "second draft\n")
        .unwrap();
    let reopened = ws.request(request.id).unwrap();
    assert_eq!(reopened.state, RequestState::Open);
    assert_eq!(reopened.approvals.len(), 0);
    assert!(
        ws.journal(50)
            .unwrap()
            .iter()
            .any(|entry| entry.act == Act::ApprovalsDismissed)
    );

    // The gate starts over: two fresh approvals land the change.
    let outcome = ws.approve(request.id, &human()).unwrap();
    assert!(matches!(outcome, GateOutcome::Pending { .. }));
    let outcome = ws.approve(request.id, &agent()).unwrap();
    assert!(matches!(outcome, GateOutcome::Landed { .. }));
}

#[test]
fn abandon_closes_the_session_and_its_request() {
    let _guard = env_lock();
    let config = tempfile::tempdir().unwrap();
    set_actor(config.path());
    let root = tempfile::tempdir().unwrap();

    let mut ws = Workspace::init(root.path()).unwrap();
    let session = ws.open_session(&agent(), &instruction()).unwrap();
    ws.session_write(session.id, "notes.txt", "draft\n")
        .unwrap();
    let request = ws.request_land(session.id).unwrap();

    let abandoned = ws.abandon(session.id).unwrap();
    assert_eq!(abandoned.state, SessionState::Abandoned);
    assert_eq!(
        ws.request(request.id).unwrap().state,
        RequestState::Abandoned
    );
    // The work stays on disk; the session is closed for new work.
    assert!(abandoned.working_copy.join("notes.txt").is_file());
    let refused = ws.session_write(session.id, "notes.txt", "more\n");
    assert!(matches!(refused, Err(Error::SessionClosed { .. })));
    assert!(
        ws.journal(50)
            .unwrap()
            .iter()
            .any(|entry| entry.act == Act::SessionAbandon)
    );
}

#[test]
fn a_rejected_request_closes_and_a_new_one_can_open() {
    let _guard = env_lock();
    let config = tempfile::tempdir().unwrap();
    set_actor(config.path());
    let root = tempfile::tempdir().unwrap();

    let mut ws = Workspace::init(root.path()).unwrap();
    let session = ws.open_session(&agent(), &instruction()).unwrap();
    ws.session_write(session.id, "notes.txt", "draft\n")
        .unwrap();
    let request = ws.request_land(session.id).unwrap();

    let rejected = ws
        .reject(request.id, &human(), Some("needs a second pass"))
        .unwrap();
    assert_eq!(rejected.state, RequestState::Rejected);
    let refused = ws.approve(request.id, &human());
    assert!(matches!(refused, Err(Error::RequestClosed { .. })));

    // The session stays open; asking to land opens a fresh request.
    let second = ws.request_land(session.id).unwrap();
    assert_ne!(second.id, request.id);
    assert_eq!(second.state, RequestState::Open);
}

#[test]
fn session_paths_never_leave_the_working_copy() {
    let _guard = env_lock();
    let config = tempfile::tempdir().unwrap();
    set_actor(config.path());
    let root = tempfile::tempdir().unwrap();

    let mut ws = Workspace::init(root.path()).unwrap();
    let session = ws.open_session(&agent(), &instruction()).unwrap();

    let climb = ws.session_write(session.id, "../escape.txt", "out\n");
    assert!(matches!(climb, Err(Error::PathOutsideWorkingCopy(_))));
    let absolute = ws.session_read(session.id, "/etc/hosts", 0, None);
    assert!(matches!(absolute, Err(Error::PathOutsideWorkingCopy(_))));
}

#[test]
fn every_closed_gate_refuses_each_transition_by_name() {
    let _guard = env_lock();
    let config = tempfile::tempdir().unwrap();
    set_actor(config.path());
    let root = tempfile::tempdir().unwrap();
    let mut ws = Workspace::init(root.path()).unwrap();
    fs::write(root.path().join("notes.txt"), "shared\n").unwrap();

    // Landed: approve, reject, and a fresh approval all refuse by name.
    let session = ws.open_session(&agent(), &instruction()).unwrap();
    ws.session_write(session.id, "notes.txt", "landed work\n")
        .unwrap();
    let request = ws.request_land(session.id).unwrap();
    let outcome = ws.approve(request.id, &agent()).unwrap();
    assert!(matches!(outcome, GateOutcome::Landed { .. }));
    for refused in [
        ws.approve(request.id, &human()).map(|_| ()).err(),
        ws.reject(request.id, &human(), None).map(|_| ()).err(),
    ] {
        let error = refused.expect("a landed gate is closed");
        assert!(
            matches!(&error, Error::RequestClosed { state, .. } if state == "landed"),
            "unexpected refusal: {error:?}"
        );
    }
    let error = ws
        .abandon(session.id)
        .expect_err("a landed session is closed");
    assert!(matches!(&error, Error::SessionClosed { state, .. } if state == "landed"));

    // Rejected: the gate stays closed to approval and re-rejection.
    let session = ws.open_session(&agent(), &instruction()).unwrap();
    ws.session_write(session.id, "notes.txt", "rejected work\n")
        .unwrap();
    let request = ws.request_land(session.id).unwrap();
    ws.reject(request.id, &human(), Some("not yet")).unwrap();
    for refused in [
        ws.approve(request.id, &human()).map(|_| ()).err(),
        ws.reject(request.id, &human(), None).map(|_| ()).err(),
    ] {
        let error = refused.expect("a rejected gate is closed");
        assert!(
            matches!(&error, Error::RequestClosed { state, .. } if state == "rejected"),
            "unexpected refusal: {error:?}"
        );
    }

    // Abandoned: same story, session and request both closed.
    let request = ws.request_land(session.id).unwrap();
    ws.abandon(session.id).unwrap();
    let error = ws
        .approve(request.id, &human())
        .expect_err("an abandoned gate is closed");
    assert!(matches!(&error, Error::RequestClosed { state, .. } if state == "abandoned"));
    let error = ws
        .abandon(session.id)
        .expect_err("an abandoned session is closed");
    assert!(matches!(&error, Error::SessionClosed { state, .. } if state == "abandoned"));
}

#[expect(
    unsafe_code,
    reason = "set_var arms the land-hold test seam in a locked test"
)]
fn hold_applies(ms: &str) {
    // SAFETY: as in set_actor; guarded by `env_lock()`.
    unsafe {
        std::env::set_var("ATELIER_LAND_HOLD_MS", ms);
    }
}

#[expect(
    unsafe_code,
    reason = "remove_var disarms the land-hold test seam in a locked test"
)]
fn release_hold() {
    // SAFETY: as in set_actor; guarded by `env_lock()`.
    unsafe {
        std::env::remove_var("ATELIER_LAND_HOLD_MS");
    }
}

#[test]
fn a_stale_apply_cannot_overwrite_a_concurrent_abandonment() {
    // The apply holds the landing lease and sleeps (the test seam);
    // another handle abandons the session meanwhile. The engine landing
    // is history and journals, but the apply's request and session
    // transitions arrive stale — the abandonment must stand.
    let _guard = env_lock();
    let config = tempfile::tempdir().unwrap();
    set_actor(config.path());
    let root = tempfile::tempdir().unwrap();
    let mut ws = Workspace::init(root.path()).unwrap();
    fs::write(root.path().join("notes.txt"), "shared\n").unwrap();

    let session = ws.open_session(&agent(), &instruction()).unwrap();
    ws.session_write(session.id, "notes.txt", "agent draft\n")
        .unwrap();
    let request = ws.request_land(session.id).unwrap();

    hold_applies("1500");
    let mut applier = Workspace::open(root.path()).unwrap();
    let apply = std::thread::spawn(move || applier.approve(request.id, &agent()));

    // Wait for the applier to pass the gate and enter its held apply.
    let mut ws_b = Workspace::open(root.path()).unwrap();
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
    while ws_b.request(request.id).unwrap().state != RequestState::Approved {
        assert!(
            std::time::Instant::now() < deadline,
            "the applier never approved"
        );
        std::thread::sleep(std::time::Duration::from_millis(20));
    }
    ws_b.abandon(session.id).unwrap();

    let outcome = apply.join().expect("the applier thread joins").unwrap();
    release_hold();

    // The engine landed the approved tip — that is history and journaled —
    // but the store rows keep the abandonment: nothing was overwritten.
    let GateOutcome::Landed { landings } = outcome else {
        panic!("the held apply landed the approved tip, got {outcome:?}");
    };
    let snapshot = landings[0].snapshot.clone();
    assert_eq!(ws_b.log(2).unwrap()[0].snapshot.id, snapshot);
    assert_eq!(
        ws_b.request(request.id).unwrap().state,
        RequestState::Abandoned
    );
    assert_eq!(
        ws_b.session(session.id).unwrap().state,
        SessionState::Abandoned
    );
    let entries = ws_b.journal(50).unwrap();
    for act in [Act::Land, Act::SessionAbandon] {
        assert!(
            entries.iter().any(|entry| entry.act == act),
            "{act} must be journaled"
        );
    }
}