shepherd-cli 6.4.5

The canonical shepherd command-line interface over the per-project registry, run artifacts, and sprint pipeline.
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
/*
    Appellation: run_store <integration tests>
    Created At: 2026.08.14
    Contrib: @FL03
*/

use std::fs::OpenOptions;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use shepherd_cli::{RunStore, RunStoreError, RunStoreResult};

fn fixture_dir(label: &str) -> PathBuf {
    let nonce = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("clock is after epoch")
        .as_nanos();
    let path = std::env::temp_dir().join(format!(
        "shepherd-run-store-{label}-{}-{nonce:x}",
        std::process::id()
    ));
    std::fs::create_dir_all(&path).expect("create isolated run-store fixture");
    std::fs::canonicalize(path).expect("canonical isolated run-store fixture")
}

fn cleanup(path: &Path) {
    std::fs::remove_dir_all(path).expect("remove isolated run-store fixture");
}

fn state(run: &str) -> shepherd::RunState {
    serde_json::from_value(serde_json::json!({
        "run": run,
        "status": "executing",
        "future_top_level": {"owned_by": "another implementation"}
    }))
    .expect("fixture state parses")
}

fn wait_for_file(path: &Path) {
    let deadline = Instant::now() + Duration::from_secs(5);
    while !path.is_file() {
        assert!(
            Instant::now() < deadline,
            "timed out waiting for {}",
            path.display()
        );
        std::thread::sleep(Duration::from_millis(10));
    }
}

#[test]
fn update_holds_one_lock_across_load_mutation_and_atomic_store() {
    let dir = fixture_dir("roundtrip");
    let path = dir.join("v645/run.json");
    let store = RunStore::new(&path);
    store.initialize(&state("v645")).expect("initialize run");

    store
        .update(|run| {
            run.updated_at = 42;
            run.extra
                .insert("new_writer_field".into(), serde_json::json!([1, 2, 3]));
            Ok(())
        })
        .expect("update run");

    let loaded = store.load().expect("load updated run");
    assert_eq!(loaded.updated_at, 42);
    assert_eq!(
        loaded.extra.get("future_top_level"),
        Some(&serde_json::json!({"owned_by": "another implementation"}))
    );
    assert_eq!(
        loaded.extra.get("new_writer_field"),
        Some(&serde_json::json!([1, 2, 3]))
    );
    assert_eq!(
        std::fs::read_to_string(&path).expect("read canonical bytes"),
        format!("{}\n", loaded.to_canonical_json())
    );
    assert_eq!(store.lock_path(), dir.join("v645/run.lock"));
    assert!(store.lock_path().is_file());

    cleanup(&dir);
}

#[test]
fn failed_mutation_and_panic_leave_the_previous_document_intact_and_release_the_lock() {
    let dir = fixture_dir("failed-mutation");
    let path = dir.join("v645/run.json");
    let store = RunStore::new(&path);
    store.initialize(&state("v645")).expect("initialize run");
    let before = std::fs::read(&path).expect("read baseline bytes");

    let failed: RunStoreResult<()> = store.update(|run| {
        run.status = "closed".into();
        Err(RunStoreError::mutation("refuse this transition"))
    });
    assert!(matches!(
        failed,
        Err(RunStoreError::Mutation(message)) if message == "refuse this transition"
    ));
    assert_eq!(std::fs::read(&path).expect("read after error"), before);

    let panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let _: RunStoreResult<()> = store.update(|_| panic!("simulated mutator panic"));
    }));
    assert!(panicked.is_err());
    assert_eq!(std::fs::read(&path).expect("read after panic"), before);

    store
        .update(|run| {
            run.updated_at = 9;
            Ok(())
        })
        .expect("lock is released during unwind");

    cleanup(&dir);
}

#[test]
fn invalid_or_foreign_state_is_never_published() {
    let dir = fixture_dir("validation");
    let path = dir.join("v645/run.json");
    let store = RunStore::new(&path);

    let mut mismatched = state("other-run");
    assert!(matches!(
        store.initialize(&mismatched),
        Err(RunStoreError::Validation(_))
    ));
    assert!(!path.exists());

    mismatched.run = "v645".into();
    store
        .initialize(&mismatched)
        .expect("valid state initializes");
    let before = std::fs::read(&path).expect("read baseline");

    let invalid = store.update(|run| {
        run.status = "made-up-state".into();
        run.lanes.push(
            serde_json::from_value(serde_json::json!({"id": "../escape"}))
                .expect("lane fixture parses"),
        );
        Ok(())
    });
    assert!(matches!(invalid, Err(RunStoreError::Validation(_))));
    assert_eq!(std::fs::read(&path).expect("invalid write refused"), before);

    let schema_ahead = store.update(|run| {
        run.schema_version += 1;
        Ok(())
    });
    assert!(matches!(schema_ahead, Err(RunStoreError::SchemaAhead(2))));
    assert_eq!(
        std::fs::read(&path).expect("foreign schema refused"),
        before
    );

    cleanup(&dir);
}

#[test]
fn schema_ahead_state_is_readable_for_resume_but_never_overwritten() {
    let dir = fixture_dir("schema-ahead-read");
    let path = dir.join("v645/run.json");
    let mut future = state("v645");
    future.schema_version = 2;
    future.status = "paused-by-future-host".into();
    future.lanes.push(
        serde_json::from_value(serde_json::json!({
            "id": "l1-future",
            "state": "waiting-on-future-host",
            "future_lane_field": {"preserve": true}
        }))
        .expect("future lane parses through the tolerant state schema"),
    );
    future
        .store(&path)
        .expect("a future implementation publishes its state");
    let before = std::fs::read(&path).expect("read future bytes");

    let store = RunStore::new(&path);
    let loaded = store
        .load()
        .expect("schema-ahead state remains available to read-only resume and inspection");
    assert_eq!(loaded.schema_version, 2);
    assert_eq!(loaded.status, "paused-by-future-host");
    assert_eq!(loaded.lanes[0].state, "waiting-on-future-host");

    let mut called = false;
    let update: RunStoreResult<()> = store.update(|run| {
        called = true;
        run.updated_at += 1;
        Ok(())
    });
    assert!(matches!(update, Err(RunStoreError::SchemaAhead(2))));
    assert!(
        !called,
        "a schema-ahead document must be rejected before mutation"
    );
    assert_eq!(std::fs::read(&path).expect("read preserved bytes"), before);

    cleanup(&dir);
}

#[test]
fn raw_rewrite_holds_the_lock_and_refuses_to_publish_invalid_migrations() {
    let dir = fixture_dir("raw-rewrite");
    let path = dir.join("v645/run.json");
    let store = RunStore::new(&path);
    store.initialize(&state("v645")).expect("initialize run");
    let before = std::fs::read(&path).expect("read baseline");

    let invalid = store.rewrite_from_raw(|bytes| {
        assert_eq!(
            bytes,
            before.as_slice(),
            "transform receives current bytes under lock"
        );
        let mut state = state("v645");
        state.status = "not-a-status".into();
        Ok((state, ()))
    });
    assert!(matches!(invalid, Err(RunStoreError::Validation(_))));
    assert_eq!(
        std::fs::read(&path).expect("invalid migration preserves bytes"),
        before
    );

    let migrated = store
        .rewrite_from_raw(|bytes| {
            let mut migrated: shepherd::RunState = serde_json::from_slice(bytes)
                .expect("fixture document remains decodable during transform");
            migrated.status = "planned".into();
            Ok((migrated, "run_id -> run"))
        })
        .expect("valid migration writes atomically");
    assert_eq!(migrated, "run_id -> run");
    assert_eq!(
        store.load().expect("load migrated document").status,
        "planned"
    );

    cleanup(&dir);
}

#[test]
fn reading_or_updating_a_missing_run_never_scaffolds_an_orphan_directory() {
    let dir = fixture_dir("missing-run");
    let run_dir = dir.join("absent");
    let store = RunStore::new(run_dir.join("run.json"));

    assert!(store.load().is_err());
    let update: RunStoreResult<()> = store.update(|_| Ok(()));
    assert!(update.is_err());
    assert!(
        !run_dir.exists(),
        "a read path must not create {} or a stray run.lock",
        run_dir.display(),
    );

    cleanup(&dir);
}

#[test]
fn lock_contention_has_a_real_upper_bound_for_reads_and_writes() {
    let dir = fixture_dir("timeout");
    let path = dir.join("v645/run.json");
    let seed_store = RunStore::new(&path);
    seed_store
        .initialize(&state("v645"))
        .expect("initialize run");

    let lock = OpenOptions::new()
        .read(true)
        .write(true)
        .open(seed_store.lock_path())
        .expect("open sidecar lock");
    lock.lock().expect("hold competing lock");

    let store = RunStore::with_timeout(&path, Duration::from_millis(40));
    let started = Instant::now();
    assert!(matches!(
        store.load(),
        Err(RunStoreError::LockTimeout { .. })
    ));
    let read_elapsed = started.elapsed();
    assert!(read_elapsed >= Duration::from_millis(35));
    assert!(read_elapsed < Duration::from_millis(500));

    let started = Instant::now();
    let result: RunStoreResult<()> = store.update(|_| Ok(()));
    assert!(matches!(result, Err(RunStoreError::LockTimeout { .. })));
    let write_elapsed = started.elapsed();
    assert!(write_elapsed >= Duration::from_millis(35));
    assert!(write_elapsed < Duration::from_millis(500));

    lock.unlock().expect("release competing lock");
    cleanup(&dir);
}

#[test]
fn two_os_processes_cannot_lose_each_others_lane_update() {
    let dir = fixture_dir("process-race");
    let path = dir.join("v645/run.json");
    RunStore::new(&path)
        .initialize(&state("v645"))
        .expect("initialize run");

    let go = dir.join("go");
    let ready_a = dir.join("ready-a");
    let ready_b = dir.join("ready-b");
    let spawn = |lane: &str, ready: &Path| {
        Command::new(std::env::current_exe().expect("current test binary"))
            .args(["--ignored", "--exact", "run_store_process_helper"])
            .env("RUN_STORE_HELPER_MODE", "update")
            .env("RUN_STORE_PATH", &path)
            .env("RUN_STORE_LANE", lane)
            .env("RUN_STORE_READY", ready)
            .env("RUN_STORE_GO", &go)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("spawn update helper")
    };

    let child_a = spawn("l1-alpha", &ready_a);
    let child_b = spawn("l2-beta", &ready_b);
    wait_for_file(&ready_a);
    wait_for_file(&ready_b);
    std::fs::write(&go, b"go\n").expect("release process barrier");

    for child in [child_a, child_b] {
        let output = child.wait_with_output().expect("wait for update helper");
        assert!(
            output.status.success(),
            "helper failed: stdout={} stderr={}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }

    let loaded = RunStore::new(&path).load().expect("load final state");
    let lane_ids: Vec<_> = loaded.lanes.iter().map(|lane| lane.id.as_str()).collect();
    assert_eq!(lane_ids.len(), 2, "both updates survive: {lane_ids:?}");
    assert!(lane_ids.contains(&"l1-alpha"));
    assert!(lane_ids.contains(&"l2-beta"));
    assert_eq!(loaded.updated_at, 2);

    cleanup(&dir);
}

#[test]
fn an_aborted_process_releases_its_os_lock() {
    let dir = fixture_dir("crash-release");
    let path = dir.join("v645/run.json");
    let store = RunStore::new(&path);
    store.initialize(&state("v645")).expect("initialize run");
    let ready = dir.join("crash-ready");

    let output = Command::new(std::env::current_exe().expect("current test binary"))
        .args(["--ignored", "--exact", "run_store_process_helper"])
        .env("RUN_STORE_HELPER_MODE", "crash")
        .env("RUN_STORE_PATH", &path)
        .env("RUN_STORE_READY", &ready)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .expect("run crashing lock holder");
    assert!(
        !output.status.success(),
        "helper must abort, not exit cleanly"
    );
    assert!(ready.is_file(), "helper acquired the lock before aborting");

    store
        .update(|run| {
            run.updated_at = 7;
            Ok(())
        })
        .expect("OS releases the crashed process lock");

    cleanup(&dir);
}

#[test]
#[ignore = "subprocess-only helper"]
fn run_store_process_helper() {
    let mode = std::env::var("RUN_STORE_HELPER_MODE").expect("helper mode");
    let path = PathBuf::from(std::env::var_os("RUN_STORE_PATH").expect("run path"));
    let ready = PathBuf::from(std::env::var_os("RUN_STORE_READY").expect("ready path"));

    if mode == "crash" {
        let lock_path = path.with_file_name("run.lock");
        let lock = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(lock_path)
            .expect("open crash lock");
        lock.lock().expect("acquire crash lock");
        std::fs::write(ready, b"locked\n").expect("publish crash readiness");
        std::process::abort();
    }

    assert_eq!(mode, "update");
    let lane = std::env::var("RUN_STORE_LANE").expect("lane id");
    let go = PathBuf::from(std::env::var_os("RUN_STORE_GO").expect("barrier path"));
    std::fs::write(&ready, b"ready\n").expect("publish update readiness");
    wait_for_file(&go);

    RunStore::with_timeout(path, Duration::from_secs(3))
        .update(|run| {
            std::thread::sleep(Duration::from_millis(150));
            run.lanes.push(
                serde_json::from_value(serde_json::json!({"id": lane}))
                    .expect("lane helper state parses"),
            );
            run.updated_at += 1;
            Ok(())
        })
        .expect("helper update succeeds");
}