mana-core 0.3.2

Core library for mana — task tracker for AI coding agents
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use std::path::{Path, PathBuf};
use std::process::Command as ShellCommand;

use anyhow::{anyhow, Context, Result};
use chrono::Utc;
use sha2::{Digest, Sha256};

use crate::config::resolve_identity;
use crate::discovery::find_unit_file;
use crate::index::Index;
use crate::resolve::resolve_unit;
use crate::unit::{AttemptOutcome, AttemptRecord, Status, Unit};

/// Parameters for claiming a unit.
pub struct ClaimParams {
    /// Who is claiming the unit. If None, resolved from config/git/env.
    pub by: Option<String>,
    /// Skip verify-on-claim check.
    pub force: bool,
}

/// Result of successfully claiming a unit.
#[derive(Debug)]
pub struct ClaimResult {
    pub unit: Unit,
    pub path: PathBuf,
    /// The resolved claimer identity.
    pub claimer: String,
    /// Whether the unit had no verify command (a GOAL, not a SPEC).
    pub is_goal: bool,
}

/// Result of releasing a claim on a unit.
pub struct ReleaseResult {
    pub unit: Unit,
    pub path: PathBuf,
}

/// Try to get the current git HEAD SHA. Returns None if not in a git repo.
fn git_head_sha(working_dir: &Path) -> Option<String> {
    ShellCommand::new("git")
        .args(["rev-parse", "HEAD"])
        .current_dir(working_dir)
        .output()
        .ok()
        .filter(|o| o.status.success())
        .and_then(|o| String::from_utf8(o.stdout).ok())
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
}

/// Run a verify command and return whether it passed (exit 0).
fn run_verify_check(verify_cmd: &str, project_root: &Path) -> Result<bool> {
    let output = ShellCommand::new("sh")
        .args(["-c", verify_cmd])
        .current_dir(project_root)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .with_context(|| format!("Failed to execute verify command: {}", verify_cmd))?;

    Ok(output.success())
}

/// Claim a unit for work.
///
/// Sets status to InProgress, records who claimed it and when.
/// The unit must be in Open status to be claimed.
///
/// If the unit has a verify command and `force` is false, the verify command
/// is run first for fail-first units. If it already passes, the claim is
/// rejected (nothing to do). The current git HEAD SHA is stored as
/// `checkpoint` for claimed dispatchable tasks so later review/diff/close flows
/// can compare against the attempt baseline.
pub fn claim(mana_dir: &Path, id: &str, params: ClaimParams) -> Result<ClaimResult> {
    let resolved = resolve_unit(mana_dir, id)?;
    let unit_path = resolved.path;
    let mut unit = resolved.unit;

    if unit.status != Status::Open {
        return Err(anyhow!(
            "Unit {} is {} -- only open units can be claimed",
            id,
            unit.status
        ));
    }

    let has_verify = unit.verify.as_ref().is_some_and(|v| !v.trim().is_empty());
    let is_goal = !unit.is_dispatchable_task();
    let project_root = mana_dir
        .parent()
        .ok_or_else(|| anyhow!("Cannot determine project root from units dir"))?;

    // Verify-on-claim: run verify before granting claim (TDD enforcement)
    // Skip when fail_first is false (unit created with -p / pass-ok)
    if has_verify && !params.force && unit.fail_first {
        let verify_cmd = unit.verify.as_ref().unwrap();

        let passed = run_verify_check(verify_cmd, project_root)?;

        if passed {
            return Err(anyhow!(
                "Cannot claim unit {}: verify already passes\n\n\
                 The verify command succeeded before any work was done.\n\
                 This means either the test is bogus or the work is already complete.\n\n\
                 Use --force to override.",
                id
            ));
        }

        // Verify failed — good, this proves the test is meaningful
        unit.fail_first = true;
    }

    if unit.kind.is_dispatchable_task() {
        unit.checkpoint = git_head_sha(project_root);
    }

    if has_verify {
        // Freeze the judge: hash the verify command so we can detect changes at close time.
        if let Some(ref verify_cmd) = unit.verify {
            let mut hasher = Sha256::new();
            hasher.update(verify_cmd.as_bytes());
            unit.verify_hash = Some(format!("{:x}", hasher.finalize()));
        }
    }

    // Resolve identity: explicit --by > resolved identity > "anonymous"
    let resolved_by = params.by.or_else(|| resolve_identity(mana_dir));
    let claimer = resolved_by
        .clone()
        .unwrap_or_else(|| "anonymous".to_string());

    let now = Utc::now();
    unit.status = Status::InProgress;
    unit.claimed_by = resolved_by.clone();
    unit.claimed_at = Some(now);
    unit.updated_at = now;

    // Start a new attempt in the attempt log (for memory system tracking)
    let attempt_num = unit.attempt_log.len() as u32 + 1;
    unit.attempt_log.push(AttemptRecord {
        num: attempt_num,
        outcome: AttemptOutcome::Abandoned, // default until close/release updates it
        notes: None,
        agent: resolved_by,
        started_at: Some(now),
        finished_at: None,
        autonomy_observation: None,
    });

    unit.to_file(&unit_path)
        .with_context(|| format!("Failed to save unit: {}", id))?;

    // Rebuild index
    let index = Index::build(mana_dir).with_context(|| "Failed to rebuild index")?;
    index
        .save(mana_dir)
        .with_context(|| "Failed to save index")?;

    Ok(ClaimResult {
        unit,
        path: unit_path,
        claimer,
        is_goal,
    })
}

/// Release a claim on a unit.
///
/// Clears claimed_by/claimed_at and sets status back to Open.
/// Marks the current attempt as abandoned.
pub fn release(mana_dir: &Path, id: &str) -> Result<ReleaseResult> {
    let unit_path = find_unit_file(mana_dir, id).map_err(|_| anyhow!("Unit not found: {}", id))?;
    let mut unit =
        Unit::from_file(&unit_path).with_context(|| format!("Failed to load unit: {}", id))?;

    let now = Utc::now();

    // Finalize the current attempt as abandoned (if one is in progress)
    if let Some(attempt) = unit.attempt_log.last_mut() {
        if attempt.finished_at.is_none() {
            attempt.outcome = AttemptOutcome::Abandoned;
            attempt.finished_at = Some(now);
        }
    }

    unit.claimed_by = None;
    unit.claimed_at = None;
    unit.status = Status::Open;
    unit.updated_at = now;

    unit.to_file(&unit_path)
        .with_context(|| format!("Failed to save unit: {}", id))?;

    // Rebuild index
    let index = Index::build(mana_dir).with_context(|| "Failed to rebuild index")?;
    index
        .save(mana_dir)
        .with_context(|| "Failed to save index")?;

    Ok(ReleaseResult {
        unit,
        path: unit_path,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use crate::ops::create::{self, tests::minimal_params};
    use std::fs;
    use tempfile::TempDir;

    fn setup() -> (TempDir, PathBuf) {
        let dir = TempDir::new().unwrap();
        let bd = dir.path().join(".mana");
        fs::create_dir(&bd).unwrap();
        Config {
            project: "test".to_string(),
            next_id: 1,
            auto_close_parent: true,
            run: None,
            plan: None,
            max_loops: 10,
            max_concurrent: 4,
            poll_interval: 30,
            extends: vec![],
            rules_file: None,
            file_locking: false,
            worktree: false,
            on_close: None,
            on_fail: None,
            verify_timeout: None,
            review: None,
            user: None,
            user_email: None,
            auto_commit: false,
            commit_template: None,
            research: None,
            run_model: None,
            plan_model: None,
            review_model: None,
            research_model: None,
            batch_verify: false,
            memory_reserve_mb: 0,
            notify: None,
        }
        .save(&bd)
        .unwrap();
        (dir, bd)
    }

    fn force_params(by: Option<&str>) -> ClaimParams {
        ClaimParams {
            by: by.map(String::from),
            force: true,
        }
    }

    fn strict_params(by: Option<&str>) -> ClaimParams {
        ClaimParams {
            by: by.map(String::from),
            force: false,
        }
    }

    #[test]
    fn claim_open_unit() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();

        let result = claim(&bd, "1", force_params(Some("alice"))).unwrap();
        assert_eq!(result.unit.status, Status::InProgress);
        assert_eq!(result.unit.claimed_by, Some("alice".to_string()));
        assert!(result.unit.claimed_at.is_some());
        assert_eq!(result.claimer, "alice");
    }

    #[test]
    fn claim_without_by() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();

        let result = claim(&bd, "1", force_params(None)).unwrap();
        assert_eq!(result.unit.status, Status::InProgress);
        assert!(result.unit.claimed_at.is_some());
    }

    #[test]
    fn claim_non_open_unit_fails() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();
        let bp = find_unit_file(&bd, "1").unwrap();
        let mut unit = Unit::from_file(&bp).unwrap();
        unit.status = Status::InProgress;
        unit.to_file(&bp).unwrap();

        assert!(claim(&bd, "1", force_params(Some("bob"))).is_err());
    }

    #[test]
    fn claim_closed_unit_fails() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();
        let bp = find_unit_file(&bd, "1").unwrap();
        let mut unit = Unit::from_file(&bp).unwrap();
        unit.status = Status::Closed;
        unit.to_file(&bp).unwrap();

        assert!(claim(&bd, "1", force_params(Some("bob"))).is_err());
    }

    #[test]
    fn claim_nonexistent_unit_fails() {
        let (_dir, bd) = setup();
        assert!(claim(&bd, "99", force_params(Some("alice"))).is_err());
    }

    #[test]
    fn release_claimed_unit() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();
        // First claim it
        claim(&bd, "1", force_params(Some("alice"))).unwrap();

        let result = release(&bd, "1").unwrap();
        assert_eq!(result.unit.status, Status::Open);
        assert_eq!(result.unit.claimed_by, None);
        assert_eq!(result.unit.claimed_at, None);
    }

    #[test]
    fn release_nonexistent_unit_fails() {
        let (_dir, bd) = setup();
        assert!(release(&bd, "99").is_err());
    }

    #[test]
    fn claim_rebuilds_index() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();

        claim(&bd, "1", force_params(Some("alice"))).unwrap();

        let index = Index::load(&bd).unwrap();
        assert_eq!(index.units.len(), 1);
        assert_eq!(index.units[0].status, Status::InProgress);
    }

    #[test]
    fn release_rebuilds_index() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();
        claim(&bd, "1", force_params(Some("alice"))).unwrap();

        release(&bd, "1").unwrap();

        let index = Index::load(&bd).unwrap();
        assert_eq!(index.units.len(), 1);
        assert_eq!(index.units[0].status, Status::Open);
    }

    #[test]
    fn claim_epic_is_goal_even_with_verify() {
        let (_dir, bd) = setup();
        let mut params = minimal_params("Epic");
        params.verify = Some("cargo test claim_epic_is_goal_even_with_verify".to_string());
        create::create(&bd, params).unwrap();

        let bp = find_unit_file(&bd, "1").unwrap();
        let mut unit = Unit::from_file(&bp).unwrap();
        unit.kind = crate::unit::UnitType::Epic;
        unit.to_file(&bp).unwrap();

        let result = claim(&bd, "1", force_params(Some("alice"))).unwrap();
        assert!(result.is_goal);
    }

    #[test]
    fn claim_unit_without_verify_is_goal() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();

        let result = claim(&bd, "1", force_params(Some("alice"))).unwrap();
        assert!(result.is_goal);
    }

    #[test]
    fn claim_unit_with_verify_is_not_goal() {
        let (_dir, bd) = setup();
        let mut params = minimal_params("Task");
        params.verify = Some("cargo test auth::login".to_string());
        create::create(&bd, params).unwrap();

        let result = claim(&bd, "1", force_params(Some("alice"))).unwrap();
        assert!(!result.is_goal);
    }

    #[test]
    fn claim_unit_with_empty_verify_is_goal() {
        let (_dir, bd) = setup();
        let mut params = minimal_params("Task");
        params.verify = Some("   ".to_string());
        params.force = true;
        create::create(&bd, params).unwrap();

        let result = claim(&bd, "1", force_params(Some("alice"))).unwrap();
        assert!(result.is_goal);
    }

    #[test]
    fn verify_on_claim_passing_verify_rejected() {
        let (_dir, bd) = setup();
        let mut params = minimal_params("Already done");
        params.verify = Some("grep -q 'project: test' .mana/config.yaml".to_string());
        params.fail_first = true;
        create::create(&bd, params).unwrap();

        let result = claim(&bd, "1", strict_params(Some("alice")));
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("verify already passes"));

        // Unit should still be open
        let bp = find_unit_file(&bd, "1").unwrap();
        let unit = Unit::from_file(&bp).unwrap();
        assert_eq!(unit.status, Status::Open);
    }

    #[test]
    fn verify_on_claim_failing_verify_succeeds() {
        let (_dir, bd) = setup();
        let mut params = minimal_params("Real work");
        params.verify = Some("false".to_string());
        params.fail_first = true;
        create::create(&bd, params).unwrap();

        let result = claim(&bd, "1", strict_params(Some("alice"))).unwrap();
        assert_eq!(result.unit.status, Status::InProgress);
        assert!(result.unit.fail_first);
    }

    #[test]
    fn verify_on_claim_force_overrides() {
        let (_dir, bd) = setup();
        let mut params = minimal_params("Force claim");
        params.verify = Some("grep -q 'project: test' .mana/config.yaml".to_string());
        create::create(&bd, params).unwrap();

        let result = claim(&bd, "1", force_params(Some("alice"))).unwrap();
        assert_eq!(result.unit.status, Status::InProgress);
    }

    #[test]
    fn verify_on_claim_checkpoint_sha_stored() {
        let (_dir, bd) = setup();
        let mut params = minimal_params("Checkpoint");
        params.verify = Some("false".to_string());
        params.fail_first = true;
        create::create(&bd, params).unwrap();

        // Initialize a git repo so we get a real SHA
        let project_root = bd.parent().unwrap();
        ShellCommand::new("git")
            .args(["init"])
            .current_dir(project_root)
            .output()
            .unwrap();
        ShellCommand::new("git")
            .args(["commit", "-m", "init", "--allow-empty"])
            .current_dir(project_root)
            .env("GIT_AUTHOR_NAME", "test")
            .env("GIT_AUTHOR_EMAIL", "test@test.com")
            .env("GIT_COMMITTER_NAME", "test")
            .env("GIT_COMMITTER_EMAIL", "test@test.com")
            .output()
            .unwrap();

        let result = claim(&bd, "1", strict_params(Some("alice"))).unwrap();
        assert!(result.unit.checkpoint.is_some());
        let sha = result.unit.checkpoint.unwrap();
        assert_eq!(sha.len(), 40);
        assert!(sha.chars().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn pass_ok_claim_also_stores_checkpoint_sha() {
        let (_dir, bd) = setup();
        let mut params = minimal_params("Checkpoint without fail-first");
        params.verify = Some("grep -q 'project: test' .mana/config.yaml".to_string());
        params.fail_first = false;
        create::create(&bd, params).unwrap();

        let project_root = bd.parent().unwrap();
        ShellCommand::new("git")
            .args(["init"])
            .current_dir(project_root)
            .output()
            .unwrap();
        ShellCommand::new("git")
            .args(["commit", "-m", "init", "--allow-empty"])
            .current_dir(project_root)
            .env("GIT_AUTHOR_NAME", "test")
            .env("GIT_AUTHOR_EMAIL", "test@test.com")
            .env("GIT_COMMITTER_NAME", "test")
            .env("GIT_COMMITTER_EMAIL", "test@test.com")
            .output()
            .unwrap();

        let result = claim(&bd, "1", strict_params(Some("alice"))).unwrap();
        assert!(result.unit.checkpoint.is_some());
        assert_eq!(result.unit.status, Status::InProgress);
    }

    #[test]
    fn unverified_task_claim_stores_checkpoint_sha_without_verify_hash() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Unverified checkpoint")).unwrap();

        let project_root = bd.parent().unwrap();
        ShellCommand::new("git")
            .args(["init"])
            .current_dir(project_root)
            .output()
            .unwrap();
        ShellCommand::new("git")
            .args(["commit", "-m", "init", "--allow-empty"])
            .current_dir(project_root)
            .env("GIT_AUTHOR_NAME", "test")
            .env("GIT_AUTHOR_EMAIL", "test@test.com")
            .env("GIT_COMMITTER_NAME", "test")
            .env("GIT_COMMITTER_EMAIL", "test@test.com")
            .output()
            .unwrap();

        let result = claim(&bd, "1", strict_params(Some("alice"))).unwrap();
        let sha = result
            .unit
            .checkpoint
            .expect("claim should store checkpoint");
        assert_eq!(sha.len(), 40);
        assert!(sha.chars().all(|c| c.is_ascii_hexdigit()));
        assert!(result.unit.verify_hash.is_none());
    }

    #[test]
    fn claim_without_git_repo_leaves_checkpoint_empty() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("No git checkpoint")).unwrap();

        let result = claim(&bd, "1", strict_params(Some("alice"))).unwrap();
        assert!(result.unit.checkpoint.is_none());
        assert_eq!(result.unit.status, Status::InProgress);
    }

    #[test]
    fn claim_starts_attempt() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();

        let result = claim(&bd, "1", force_params(Some("agent-1"))).unwrap();
        assert_eq!(result.unit.attempt_log.len(), 1);
        assert_eq!(result.unit.attempt_log[0].num, 1);
        assert_eq!(
            result.unit.attempt_log[0].agent,
            Some("agent-1".to_string())
        );
        assert!(result.unit.attempt_log[0].started_at.is_some());
        assert!(result.unit.attempt_log[0].finished_at.is_none());
    }

    #[test]
    fn release_marks_attempt_abandoned() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();
        claim(&bd, "1", force_params(Some("agent-1"))).unwrap();

        let result = release(&bd, "1").unwrap();
        assert_eq!(result.unit.attempt_log.len(), 1);
        assert_eq!(
            result.unit.attempt_log[0].outcome,
            AttemptOutcome::Abandoned
        );
        assert!(result.unit.attempt_log[0].finished_at.is_some());
    }

    #[test]
    fn multiple_claims_accumulate_attempts() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();

        claim(&bd, "1", force_params(Some("agent-1"))).unwrap();
        release(&bd, "1").unwrap();
        let result = claim(&bd, "1", force_params(Some("agent-2"))).unwrap();

        assert_eq!(result.unit.attempt_log.len(), 2);
        assert_eq!(result.unit.attempt_log[0].num, 1);
        assert_eq!(
            result.unit.attempt_log[0].outcome,
            AttemptOutcome::Abandoned
        );
        assert!(result.unit.attempt_log[0].finished_at.is_some());
        assert_eq!(result.unit.attempt_log[1].num, 2);
        assert_eq!(
            result.unit.attempt_log[1].agent,
            Some("agent-2".to_string())
        );
        assert!(result.unit.attempt_log[1].finished_at.is_none());
    }

    // -----------------------------------------------------------------------
    // Timeout / stuck-in-progress recovery tests
    // -----------------------------------------------------------------------

    /// When an agent times out, mana run calls release() to reset the unit back
    /// to Open so the next dispatch can claim it again without manual intervention.
    #[test]
    fn release_resets_timed_out_in_progress_unit_to_open() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();

        // Simulate agent claiming and then timing out (unit stuck in_progress)
        claim(&bd, "1", force_params(Some("agent-1"))).unwrap();
        // Verify unit is now in_progress (as it would be after a timeout)
        let bp = find_unit_file(&bd, "1").unwrap();
        let in_progress = Unit::from_file(&bp).unwrap();
        assert_eq!(in_progress.status, Status::InProgress);

        // mana run calls release() after a failed/timed-out agent
        let result = release(&bd, "1").unwrap();

        // Unit must be Open so the next mana run can claim it
        assert_eq!(result.unit.status, Status::Open);
        assert_eq!(result.unit.claimed_by, None);
        assert_eq!(result.unit.claimed_at, None);

        // Subsequent claim must succeed (the core fix: no manual intervention needed)
        let second = claim(&bd, "1", force_params(Some("agent-2"))).unwrap();
        assert_eq!(second.unit.status, Status::InProgress);
        assert_eq!(second.unit.claimed_by, Some("agent-2".to_string()));
    }

    /// Attempting to claim a unit that is still in_progress (e.g. if release was
    /// never called) must fail with a descriptive error.
    #[test]
    fn claim_stuck_in_progress_without_release_fails() {
        let (_dir, bd) = setup();
        create::create(&bd, minimal_params("Task")).unwrap();

        // First agent claims the unit
        claim(&bd, "1", force_params(Some("agent-1"))).unwrap();

        // Second agent tries to claim without release — must fail
        let result = claim(&bd, "1", force_params(Some("agent-2")));
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("in_progress") || err.contains("InProgress") || err.contains("only open"),
            "Error should explain the unit is not open: {}",
            err
        );
    }
}