bones-cli 0.24.0

CLI binary for bones issue tracker
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! `bn triage dep` — manage dependency links between bones.
//!
//! Subcommands:
//! - `bn triage dep add <from> --blocks <to>` — emit `item.link` with type "blocks"
//! - `bn triage dep add <from> --relates <to>` — emit `item.link` with type "`related_to`"
//! - `bn triage dep rm <from> <to>` — emit `item.unlink` removing the dependency

use std::collections::BTreeMap;
use std::path::Path;
use std::time::Duration;

use clap::{Args, Subcommand};
use serde::Serialize;
#[cfg(test)]
use serde_json::json;

use bones_core::db::query::{item_exists, try_open_projection};
use bones_core::event::data::{EventData, LinkData, UnlinkData};
use bones_core::event::writer::write_event;
use bones_core::event::{Event, EventType};
use bones_core::model::item_id::ItemId;
use bones_core::shard::ShardManager;

use crate::agent;
use crate::itc_state::assign_next_itc;
use crate::output::{CliError, OutputMode, render_error, render_mode};
use crate::validate;

// ---------------------------------------------------------------------------
// Clap types
// ---------------------------------------------------------------------------

#[derive(Args, Debug)]
pub struct DepArgs {
    #[command(subcommand)]
    pub command: DepCommand,
}

#[derive(Subcommand, Debug)]
pub enum DepCommand {
    #[command(
        about = "Add a dependency link between two bones",
        after_help = "EXAMPLES:\n    # A blocks B\n    bn triage dep add bn-abc --blocks bn-def\n\n    # A relates to B (informational)\n    bn triage dep add bn-abc --relates bn-def"
    )]
    Add(DepAddArgs),

    #[command(
        about = "Remove a dependency link between two bones",
        after_help = "EXAMPLES:\n    # Remove the link: bn-abc blocks bn-def\n    bn triage dep rm bn-abc bn-def"
    )]
    Rm(DepRmArgs),
}

/// Arguments for `bn triage dep add`.
#[derive(Args, Debug)]
pub struct DepAddArgs {
    /// Source bone ID (the blocker / origin of the link).
    pub from: String,

    /// Target bone: <from> blocks <to> (creates a hard blocking dependency).
    #[arg(long, group = "link_target", value_name = "TO")]
    pub blocks: Option<String>,

    /// Target bone: <from> relates to <to> (informational, no triage impact).
    #[arg(long, group = "link_target", value_name = "TO")]
    pub relates: Option<String>,
}

/// Arguments for `bn triage dep rm`.
#[derive(Args, Debug)]
pub struct DepRmArgs {
    /// Source bone ID (the blocker).
    pub from: String,

    /// Target bone ID (the bone that was blocked).
    pub to: String,
}

// ---------------------------------------------------------------------------
// Output types
// ---------------------------------------------------------------------------

#[derive(Debug, Serialize)]
struct DepAddOutput {
    ok: bool,
    from: String,
    to: String,
    link_type: String,
    event_hash: String,
}

#[derive(Debug, Serialize)]
struct DepRmOutput {
    ok: bool,
    from: String,
    to: String,
    event_hash: String,
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn find_bones_dir(start: &Path) -> Option<std::path::PathBuf> {
    let mut current = start.to_path_buf();
    loop {
        let candidate = current.join(".bones");
        if candidate.is_dir() {
            return Some(candidate);
        }
        if !current.pop() {
            return None;
        }
    }
}

/// Emit an `item.link` or `item.unlink` event.
fn emit_event(
    bones_dir: &Path,
    agent: &str,
    event_type: EventType,
    item_id: &ItemId,
    data: EventData,
) -> anyhow::Result<String> {
    let shard_mgr = ShardManager::new(bones_dir);

    let mut event = Event {
        wall_ts_us: 0,
        agent: agent.to_string(),
        itc: String::new(),
        parents: vec![],
        event_type,
        item_id: item_id.clone(),
        data,
        event_hash: String::new(),
    };

    let project_root = bones_dir.parent().unwrap_or(bones_dir);

    {
        use bones_core::lock::ShardLock;
        let lock_path = shard_mgr.lock_path();
        let _lock = ShardLock::acquire(&lock_path, Duration::from_secs(5))
            .map_err(|e| anyhow::anyhow!("failed to acquire lock: {e}"))?;

        let (year, month) = shard_mgr
            .rotate_if_needed()
            .map_err(|e| anyhow::anyhow!("failed to rotate shards: {e}"))?;

        event.wall_ts_us = shard_mgr
            .next_timestamp()
            .map_err(|e| anyhow::anyhow!("timestamp error: {e}"))?;

        assign_next_itc(project_root, &mut event)?;

        let line = write_event(&mut event).map_err(|e| anyhow::anyhow!("serialize event: {e}"))?;

        shard_mgr
            .append_raw(year, month, &line)
            .map_err(|e| anyhow::anyhow!("write event: {e}"))?;
    }

    // Best-effort projection
    let db_path = bones_dir.join("bones.db");
    if let Ok(conn) = bones_core::db::open_projection(&db_path) {
        let _ = bones_core::db::project::ensure_tracking_table(&conn);
        let projector = bones_core::db::project::Projector::new(&conn);
        if let Err(e) = projector.project_event(&event) {
            tracing::warn!("projection failed (will be fixed on rebuild): {e}");
        }
    }

    Ok(event.event_hash)
}

// ---------------------------------------------------------------------------
// Command runners
// ---------------------------------------------------------------------------

#[tracing::instrument(skip_all, name = "cmd.dep")]
pub fn run_dep(
    args: &DepArgs,
    agent_flag: Option<&str>,
    output: OutputMode,
    project_root: &Path,
) -> anyhow::Result<()> {
    match &args.command {
        DepCommand::Add(a) => run_dep_add(a, agent_flag, output, project_root),
        DepCommand::Rm(a) => run_dep_rm(a, agent_flag, output, project_root),
    }
}

fn run_dep_add(
    args: &DepAddArgs,
    agent_flag: Option<&str>,
    output: OutputMode,
    project_root: &Path,
) -> anyhow::Result<()> {
    // 1. Resolve agent
    let agent = match agent::require_agent(agent_flag) {
        Ok(a) => a,
        Err(e) => {
            render_error(
                output,
                &CliError::with_details(&e.message, "Set --agent, BONES_AGENT, or AGENT", e.code),
            )?;
            anyhow::bail!("{}", e.message);
        }
    };
    if let Err(e) = validate::validate_agent(&agent) {
        render_error(output, &e.to_cli_error())?;
        anyhow::bail!("{}", e.reason);
    }

    // 2. Determine link target and type
    let (to_raw, link_type) = if let Some(ref to) = args.blocks {
        (to.clone(), "blocks")
    } else if let Some(ref to) = args.relates {
        (to.clone(), "related_to")
    } else {
        let msg = "must provide --blocks <to> or --relates <to>";
        render_error(output, &CliError::new(msg))?;
        anyhow::bail!("{msg}");
    };

    // 3. Validate item IDs
    if let Err(e) = validate::validate_item_id(&args.from) {
        render_error(output, &e.to_cli_error())?;
        anyhow::bail!("{}", e.reason);
    }
    if let Err(e) = validate::validate_item_id(&to_raw) {
        render_error(output, &e.to_cli_error())?;
        anyhow::bail!("{}", e.reason);
    }

    let from_id = ItemId::parse(&args.from)
        .map_err(|e| anyhow::anyhow!("invalid item ID '{}': {}", args.from, e))?;
    let to_id =
        ItemId::parse(&to_raw).map_err(|e| anyhow::anyhow!("invalid item ID '{to_raw}': {e}"))?;

    if from_id == to_id {
        let msg = "cannot link an item to itself";
        render_error(output, &CliError::new(msg))?;
        anyhow::bail!("{msg}");
    }

    // 4. Find .bones dir and DB
    let bones_dir = find_bones_dir(project_root).ok_or_else(|| {
        let msg = "Not a bones project: .bones directory not found";
        render_error(
            output,
            &CliError::with_details(
                msg,
                "Run 'bn init' to create a new project",
                "not_a_project",
            ),
        )
        .ok();
        anyhow::anyhow!("{msg}")
    })?;

    let db_path = bones_dir.join("bones.db");

    // 5. Verify both items exist
    if let Some(conn) = try_open_projection(&db_path)? {
        if !item_exists(&conn, from_id.as_str())? {
            let msg = format!("item not found: {from_id}");
            render_error(output, &CliError::new(&msg))?;
            anyhow::bail!("{msg}");
        }
        if !item_exists(&conn, to_id.as_str())? {
            let msg = format!("item not found: {to_id}");
            render_error(output, &CliError::new(&msg))?;
            anyhow::bail!("{msg}");
        }

        // 6. Cycle check for blocking links
        if link_type == "blocks"
            && let Err(cycle_msg) =
                check_would_create_cycle(&conn, from_id.as_str(), to_id.as_str())
        {
            render_error(output, &CliError::new(&cycle_msg))?;
            anyhow::bail!("{cycle_msg}");
        }
    } else {
        let msg = "projection database not found; run `bn admin rebuild` first";
        render_error(output, &CliError::new(msg))?;
        anyhow::bail!("{msg}");
    }

    // 7. Emit event: item_id = to (blocked), target = from (blocker)
    let event_hash = emit_event(
        &bones_dir,
        &agent,
        EventType::Link,
        &to_id,
        EventData::Link(LinkData {
            target: from_id.as_str().to_string(),
            link_type: link_type.to_string(),
            extra: BTreeMap::new(),
        }),
    )?;

    // 8. Output
    let result = DepAddOutput {
        ok: true,
        from: from_id.as_str().to_string(),
        to: to_id.as_str().to_string(),
        link_type: link_type.to_string(),
        event_hash,
    };

    render_mode(
        output,
        &result,
        |r, w| {
            writeln!(
                w,
                "ok=true  from={}  to={}  link_type={}",
                r.from, r.to, r.link_type
            )
        },
        |r, w| {
            let arrow = if r.link_type == "blocks" {
                "blocks"
            } else {
                "relates to"
            };
            writeln!(w, "{} {} {}", r.from, arrow, r.to)
        },
    )?;

    Ok(())
}

/// Check whether adding a blocking edge `from → to` would create a cycle.
///
/// Returns `Ok(())` if safe, `Err(msg)` with cycle description if not.
fn check_would_create_cycle(
    conn: &rusqlite::Connection,
    from: &str,
    to: &str,
) -> Result<(), String> {
    use bones_triage::graph::{RawGraph, would_create_cycle};

    let raw =
        RawGraph::from_sqlite(conn).map_err(|e| format!("failed to load dependency graph: {e}"))?;

    // Get or insert node indices for from and to
    let from_idx = match raw.node_index(from) {
        Some(idx) => idx,
        None => return Ok(()), // Item not in graph yet — no cycle possible
    };
    let to_idx = match raw.node_index(to) {
        Some(idx) => idx,
        None => return Ok(()), // Item not in graph yet — no cycle possible
    };

    if let Some(cycle_path) = would_create_cycle(&raw.graph, from_idx, to_idx) {
        let path_str = cycle_path.join("");
        Err(format!(
            "adding this dependency would create a cycle: {path_str}"
        ))
    } else {
        Ok(())
    }
}

fn run_dep_rm(
    args: &DepRmArgs,
    agent_flag: Option<&str>,
    output: OutputMode,
    project_root: &Path,
) -> anyhow::Result<()> {
    // 1. Resolve agent
    let agent = match agent::require_agent(agent_flag) {
        Ok(a) => a,
        Err(e) => {
            render_error(
                output,
                &CliError::with_details(&e.message, "Set --agent, BONES_AGENT, or AGENT", e.code),
            )?;
            anyhow::bail!("{}", e.message);
        }
    };
    if let Err(e) = validate::validate_agent(&agent) {
        render_error(output, &e.to_cli_error())?;
        anyhow::bail!("{}", e.reason);
    }

    // 2. Validate item IDs
    if let Err(e) = validate::validate_item_id(&args.from) {
        render_error(output, &e.to_cli_error())?;
        anyhow::bail!("{}", e.reason);
    }
    if let Err(e) = validate::validate_item_id(&args.to) {
        render_error(output, &e.to_cli_error())?;
        anyhow::bail!("{}", e.reason);
    }

    let from_id = ItemId::parse(&args.from)
        .map_err(|e| anyhow::anyhow!("invalid item ID '{}': {}", args.from, e))?;
    let to_id = ItemId::parse(&args.to)
        .map_err(|e| anyhow::anyhow!("invalid item ID '{}': {}", args.to, e))?;

    // 3. Find .bones dir
    let bones_dir = find_bones_dir(project_root).ok_or_else(|| {
        let msg = "Not a bones project: .bones directory not found";
        render_error(
            output,
            &CliError::with_details(
                msg,
                "Run 'bn init' to create a new project",
                "not_a_project",
            ),
        )
        .ok();
        anyhow::anyhow!("{msg}")
    })?;

    // 4. Emit unlink event: item_id = to (blocked), target = from (blocker)
    let event_hash = emit_event(
        &bones_dir,
        &agent,
        EventType::Unlink,
        &to_id,
        EventData::Unlink(UnlinkData {
            target: from_id.as_str().to_string(),
            link_type: None, // remove any link between the two items
            extra: BTreeMap::new(),
        }),
    )?;

    // 5. Output
    let result = DepRmOutput {
        ok: true,
        from: from_id.as_str().to_string(),
        to: to_id.as_str().to_string(),
        event_hash,
    };

    render_mode(
        output,
        &result,
        |r, w| writeln!(w, "ok=true  removed_link={}{}", r.from, r.to),
        |r, w| writeln!(w, "✓ removed link: {}{}", r.from, r.to),
    )?;

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dep_add_args_blocks() {
        use clap::Parser;

        #[derive(Parser)]
        struct Wrapper {
            #[command(subcommand)]
            cmd: DepCommand,
        }

        let w = Wrapper::parse_from(["test", "add", "bn-abc", "--blocks", "bn-def"]);
        if let DepCommand::Add(a) = w.cmd {
            assert_eq!(a.from, "bn-abc");
            assert_eq!(a.blocks.as_deref(), Some("bn-def"));
            assert!(a.relates.is_none());
        } else {
            panic!("expected Add");
        }
    }

    #[test]
    fn dep_add_args_relates() {
        use clap::Parser;

        #[derive(Parser)]
        struct Wrapper {
            #[command(subcommand)]
            cmd: DepCommand,
        }

        let w = Wrapper::parse_from(["test", "add", "bn-abc", "--relates", "bn-xyz"]);
        if let DepCommand::Add(a) = w.cmd {
            assert_eq!(a.from, "bn-abc");
            assert!(a.blocks.is_none());
            assert_eq!(a.relates.as_deref(), Some("bn-xyz"));
        } else {
            panic!("expected Add");
        }
    }

    #[test]
    fn dep_rm_args() {
        use clap::Parser;

        #[derive(Parser)]
        struct Wrapper {
            #[command(subcommand)]
            cmd: DepCommand,
        }

        let w = Wrapper::parse_from(["test", "rm", "bn-aaa", "bn-bbb"]);
        if let DepCommand::Rm(a) = w.cmd {
            assert_eq!(a.from, "bn-aaa");
            assert_eq!(a.to, "bn-bbb");
        } else {
            panic!("expected Rm");
        }
    }

    #[test]
    fn dep_add_cannot_have_both_blocks_and_relates() {
        use clap::Parser;

        #[derive(Parser)]
        struct Wrapper {
            #[command(subcommand)]
            cmd: DepCommand,
        }

        // clap's `group` enforcement: --blocks and --relates conflict
        let result =
            Wrapper::try_parse_from(["test", "add", "bn-abc", "--blocks", "x", "--relates", "y"]);
        assert!(result.is_err(), "conflicting flags should be rejected");
    }

    /// Integration test: add and remove a blocking dependency.
    #[test]
    fn dep_add_and_rm_end_to_end() {
        use bones_core::db::rebuild;
        use bones_core::event::data::CreateData;
        use bones_core::event::writer::write_event;
        use bones_core::event::{Event, EventData, EventType};
        use bones_core::model::item::Kind;
        use bones_core::model::item_id::ItemId;
        use bones_core::shard::ShardManager;
        use std::time::Duration;

        let dir = tempfile::tempdir().expect("create temp dir");
        let root = dir.path().to_path_buf();
        let bones_dir = root.join(".bones");
        let shard_mgr = ShardManager::new(&bones_dir);
        shard_mgr.ensure_dirs().expect("ensure dirs");
        shard_mgr.init().expect("init shard");

        // Create two items
        for item_id in ["bn-aaa", "bn-bbb"] {
            let ts = shard_mgr.next_timestamp().expect("ts");
            let mut evt = Event {
                wall_ts_us: ts,
                agent: "test-agent".into(),
                itc: "itc:AQ".into(),
                parents: vec![],
                event_type: EventType::Create,
                item_id: ItemId::new_unchecked(item_id),
                data: EventData::Create(CreateData {
                    title: item_id.into(),
                    kind: Kind::Task,
                    size: None,
                    urgency: bones_core::model::item::Urgency::Default,
                    labels: vec![],
                    parent: None,
                    causation: None,
                    description: None,
                    extra: BTreeMap::new(),
                }),
                event_hash: String::new(),
            };
            let line = write_event(&mut evt).expect("write");
            shard_mgr
                .append(&line, false, Duration::from_secs(5))
                .expect("append");
        }

        // Rebuild projection
        let events_dir = bones_dir.join("events");
        let db_path = bones_dir.join("bones.db");
        rebuild::rebuild(&events_dir, &db_path).expect("rebuild");

        // dep add bn-aaa --blocks bn-bbb
        let add_args = DepAddArgs {
            from: "bn-aaa".into(),
            blocks: Some("bn-bbb".into()),
            relates: None,
        };
        run_dep_add(&add_args, Some("test-agent"), OutputMode::Pretty, &root)
            .expect("dep add should succeed");

        // Rebuild and verify dependency exists
        rebuild::rebuild(&events_dir, &db_path).expect("rebuild");
        let conn = bones_core::db::query::try_open_projection(&db_path)
            .expect("open db")
            .expect("db exists");
        let deps = bones_core::db::query::get_dependencies(&conn, "bn-bbb").expect("get deps");
        assert_eq!(deps.len(), 1, "should have one dep");
        assert_eq!(deps[0].depends_on_item_id, "bn-aaa");
        assert_eq!(deps[0].link_type, "blocks");

        // dep rm bn-aaa bn-bbb
        let rm_args = DepRmArgs {
            from: "bn-aaa".into(),
            to: "bn-bbb".into(),
        };
        run_dep_rm(&rm_args, Some("test-agent"), OutputMode::Pretty, &root)
            .expect("dep rm should succeed");

        // Rebuild and verify dependency removed
        rebuild::rebuild(&events_dir, &db_path).expect("rebuild");
        let conn = bones_core::db::query::try_open_projection(&db_path)
            .expect("open db")
            .expect("db exists");
        let deps_after =
            bones_core::db::query::get_dependencies(&conn, "bn-bbb").expect("get deps");
        assert!(deps_after.is_empty(), "dep should be removed");
    }

    #[test]
    fn dep_add_rejects_cycle() {
        use bones_core::db::rebuild;
        use bones_core::event::data::CreateData;
        use bones_core::event::writer::write_event;
        use bones_core::event::{Event, EventData, EventType};
        use bones_core::model::item::Kind;
        use bones_core::model::item_id::ItemId;
        use bones_core::shard::ShardManager;
        use std::time::Duration;

        let dir = tempfile::tempdir().expect("create temp dir");
        let root = dir.path().to_path_buf();
        let bones_dir = root.join(".bones");
        let shard_mgr = ShardManager::new(&bones_dir);
        shard_mgr.ensure_dirs().expect("ensure dirs");
        shard_mgr.init().expect("init shard");

        // Create three items: A, B, C
        for item_id in ["bn-ca1", "bn-ca2", "bn-ca3"] {
            let ts = shard_mgr.next_timestamp().expect("ts");
            let mut evt = Event {
                wall_ts_us: ts,
                agent: "test-agent".into(),
                itc: "itc:AQ".into(),
                parents: vec![],
                event_type: EventType::Create,
                item_id: ItemId::new_unchecked(item_id),
                data: EventData::Create(CreateData {
                    title: item_id.into(),
                    kind: Kind::Task,
                    size: None,
                    urgency: bones_core::model::item::Urgency::Default,
                    labels: vec![],
                    parent: None,
                    causation: None,
                    description: None,
                    extra: BTreeMap::new(),
                }),
                event_hash: String::new(),
            };
            let line = write_event(&mut evt).expect("write");
            shard_mgr
                .append(&line, false, Duration::from_secs(5))
                .expect("append");
        }

        // Add A blocks B, B blocks C
        let events_dir = bones_dir.join("events");
        let db_path = bones_dir.join("bones.db");
        rebuild::rebuild(&events_dir, &db_path).expect("rebuild");

        for (from, to) in [("bn-ca1", "bn-ca2"), ("bn-ca2", "bn-ca3")] {
            let args = DepAddArgs {
                from: from.into(),
                blocks: Some(to.into()),
                relates: None,
            };
            run_dep_add(&args, Some("test-agent"), OutputMode::Pretty, &root)
                .expect("initial dep add should succeed");
            rebuild::rebuild(&events_dir, &db_path).expect("rebuild");
        }

        // Now try C blocks A — should fail with cycle error
        let cycle_args = DepAddArgs {
            from: "bn-ca3".into(),
            blocks: Some("bn-ca1".into()),
            relates: None,
        };
        let result = run_dep_add(&cycle_args, Some("test-agent"), OutputMode::Pretty, &root);
        assert!(result.is_err(), "cycle should be rejected");
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("cycle"),
            "error should mention cycle: {err_str}"
        );
    }

    #[test]
    fn dep_add_self_link_rejected() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let root = dir.path();

        let args = DepAddArgs {
            from: "bn-abc".into(),
            blocks: Some("bn-abc".into()),
            relates: None,
        };
        let result = run_dep_add(&args, Some("test-agent"), OutputMode::Pretty, root);
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("itself"), "should mention self-link: {msg}");
    }

    /// Verify that the output struct serializes correctly.
    #[test]
    fn dep_add_output_serialization() {
        let out = DepAddOutput {
            ok: true,
            from: "bn-abc".into(),
            to: "bn-def".into(),
            link_type: "blocks".into(),
            event_hash: "blake3:abc123".into(),
        };
        let json = serde_json::to_value(&out).expect("serialize");
        assert_eq!(json["ok"], json!(true));
        assert_eq!(json["from"], json!("bn-abc"));
        assert_eq!(json["to"], json!("bn-def"));
        assert_eq!(json["link_type"], json!("blocks"));
    }
}