beck 0.2.0

Local skills router CLI for AI agents. Your agent's skills, at its beck and call.
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
//! `beck sync`: two personalities behind one subcommand.
//!
//! 1. Default path (`beck sync`, no `--from`): the Phase 1 behavior.
//!    Walk configured skill roots, rebuild the SQLite FTS5 index.
//!    Untouched by v0.2.
//!
//! 2. Reverse ingest (`beck sync --from <agent> [--write] [--force]`):
//!    new in Phase 6. Pulls skills from an agent's native directory
//!    into the canonical `~/beck/skills/` tree so beck takes over
//!    as the source of record. Dry-run by default. Conflict-aware.
//!
//! The split is intentional. The v0.1 sync surface is load-bearing for
//! existing users; we do not want to change its defaults. The `--from`
//! flag is the one knob that flips the mode.

use std::fs;
use std::path::{Path, PathBuf};

use serde::Serialize;
use serde_json::json;

use beck::agents::adapter::Adapter;
use beck::agents::paths::skills_home;
use beck::agents::registry;
use beck::agents::skill::Skill;
use beck::db::Db;
use beck::error::{CliError, Result};
use beck::paths;
use beck::sync as core_sync;

pub async fn handle(
    _force: bool,
    json_out: bool,
    from: Option<String>,
    write: bool,
) -> Result<()> {
    if let Some(agent) = from {
        return handle_ingest(&agent, write, json_out).await;
    }

    // Default v0.1 path: rebuild the index from configured roots.
    let roots = paths::default_roots();
    if roots.is_empty() {
        return Err(CliError::Validation(
            "no skill roots found. Expected ~/.hermes/skills or ~/.claude/skills.".into(),
        ));
    }
    let db_path = paths::db_path()?;
    let db = Db::open(&db_path).map_err(CliError::Other)?;
    db.clear().map_err(CliError::Other)?;

    let mut total = 0usize;
    let mut per_root: Vec<(String, usize)> = Vec::new();
    for root in &roots {
        let n = core_sync::sync_root(&db, root).map_err(CliError::Other)?;
        per_root.push((root.display().to_string(), n));
        total += n;
    }

    if json_out {
        let payload = json!({
            "indexed": total,
            "db": db_path.display().to_string(),
            "roots": per_root.iter().map(|(p, n)| json!({"path": p, "indexed": n})).collect::<Vec<_>>(),
        });
        println!(
            "{}",
            serde_json::to_string_pretty(&payload).unwrap_or_default()
        );
    } else {
        println!("indexed {total} skills into {}", db_path.display());
        for (path, n) in &per_root {
            println!("  {n:>4}  {path}");
        }
    }
    Ok(())
}

async fn handle_ingest(agent: &str, write: bool, json_out: bool) -> Result<()> {
    let adapter = registry::find_adapter(agent).ok_or_else(|| {
        CliError::Validation(format!(
            "unknown agent {agent:?}, known: {}",
            registry::known_agent_names()
        ))
    })?;

    if !adapter.detect() {
        return Err(CliError::Validation(format!(
            "agent {agent} not detected on this machine"
        )));
    }

    let skills_root = skills_home()?;
    let report = run_ingest(adapter.as_ref(), &skills_root, write, false)?;

    if json_out {
        println!(
            "{}",
            serde_json::to_string_pretty(&report).unwrap_or_default()
        );
    } else {
        print_ingest_report(&report);
    }

    if write && report.conflicts > 0 {
        return Err(CliError::Validation(
            "one or more conflicts, pass --force to overwrite canonical sources".into(),
        ));
    }
    Ok(())
}

#[derive(Debug, Default, Serialize, PartialEq)]
pub struct IngestReport {
    pub agent: String,
    pub plans: Vec<IngestPlan>,
    pub created: u32,
    pub skipped: u32,
    pub conflicts: u32,
    pub written: bool,
}

#[derive(Debug, Serialize, PartialEq, Clone)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum IngestPlan {
    Create { skill: String, target: PathBuf },
    Skip { skill: String, reason: String },
    Conflict { skill: String, target: PathBuf },
}

/// Core ingest logic. Takes the adapter to walk, the canonical skills
/// root, and the write/force flags. Never touches stdout.
pub fn run_ingest(
    adapter: &dyn Adapter,
    skills_root: &Path,
    write: bool,
    force: bool,
) -> Result<IngestReport> {
    let ingested = adapter.ingest()?;
    let mut report = IngestReport {
        agent: adapter.name().to_string(),
        written: write,
        ..Default::default()
    };

    for skill in ingested {
        let target_dir = skills_root.join(&skill.name);
        let target = target_dir.join("SKILL.md");

        let plan = classify(&skill, &target)?;
        report.plans.push(plan.clone());

        match plan {
            IngestPlan::Create { .. } => {
                if write {
                    fs::create_dir_all(&target_dir).map_err(|e| {
                        CliError::Validation(format!(
                            "mkdir -p {} failed: {e}",
                            target_dir.display()
                        ))
                    })?;
                    atomic_write(&target, &skill.source_path)?;
                }
                report.created += 1;
            }
            IngestPlan::Skip { .. } => {
                report.skipped += 1;
            }
            IngestPlan::Conflict { target, .. } => {
                if write && force {
                    fs::create_dir_all(&target_dir).map_err(|e| {
                        CliError::Validation(format!(
                            "mkdir -p {} failed: {e}",
                            target_dir.display()
                        ))
                    })?;
                    atomic_write(&target, &skill.source_path)?;
                    // Force counts as create, not conflict.
                    report.created += 1;
                    // Remove the conflict plan we pushed a second ago
                    // and replace it with Create so the report is
                    // consistent with what actually ran.
                    if let Some(last) = report.plans.last_mut() {
                        *last = IngestPlan::Create {
                            skill: skill.name.clone(),
                            target: target.clone(),
                        };
                    }
                } else {
                    report.conflicts += 1;
                }
            }
        }
    }

    Ok(report)
}

fn classify(skill: &Skill, target: &Path) -> Result<IngestPlan> {
    if !target.exists() {
        return Ok(IngestPlan::Create {
            skill: skill.name.clone(),
            target: target.to_path_buf(),
        });
    }

    // Compare byte-for-byte against the source we just loaded. If the
    // on-disk canonical source is identical, nothing to do.
    let existing = fs::read(target).map_err(|e| {
        CliError::Validation(format!(
            "cannot read existing {} for hash compare: {e}",
            target.display()
        ))
    })?;
    let source = fs::read(&skill.source_path).map_err(|e| {
        CliError::Validation(format!(
            "cannot read source {} for hash compare: {e}",
            skill.source_path.display()
        ))
    })?;

    if existing == source {
        Ok(IngestPlan::Skip {
            skill: skill.name.clone(),
            reason: "target already matches source byte-for-byte".into(),
        })
    } else {
        Ok(IngestPlan::Conflict {
            skill: skill.name.clone(),
            target: target.to_path_buf(),
        })
    }
}

fn atomic_write(target: &Path, source: &Path) -> Result<()> {
    let bytes = fs::read(source).map_err(|e| {
        CliError::Validation(format!(
            "failed to read source {}: {e}",
            source.display()
        ))
    })?;
    let tmp = {
        let mut os = target.as_os_str().to_os_string();
        os.push(".tmp");
        PathBuf::from(os)
    };
    fs::write(&tmp, &bytes).map_err(|e| {
        CliError::Validation(format!(
            "write to tmp {} failed: {e}",
            tmp.display()
        ))
    })?;
    fs::rename(&tmp, target).map_err(|e| {
        CliError::Validation(format!(
            "rename {} -> {} failed: {e}",
            tmp.display(),
            target.display()
        ))
    })?;
    Ok(())
}

fn print_ingest_report(report: &IngestReport) {
    println!(
        "agent: {}, dry_run: {}",
        report.agent,
        !report.written
    );
    println!(
        "  created: {}, skipped: {}, conflicts: {}",
        report.created, report.skipped, report.conflicts
    );
    for plan in &report.plans {
        match plan {
            IngestPlan::Create { skill, target } => {
                println!("  create {skill} -> {}", target.display())
            }
            IngestPlan::Skip { skill, reason } => {
                println!("  skip {skill}: {reason}")
            }
            IngestPlan::Conflict { skill, target } => {
                println!("  conflict {skill} at {}", target.display())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::RefCell;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use beck::agents::adapter::InstallPlan;
    use beck::agents::manifest::{Entry, InstallMode};

    static COUNTER: AtomicUsize = AtomicUsize::new(0);

    fn tempdir(name: &str) -> PathBuf {
        let base = std::env::temp_dir().join(format!(
            "beck-ingest-tests-{name}-{}-{}",
            std::process::id(),
            COUNTER.fetch_add(1, Ordering::SeqCst)
        ));
        let _ = fs::remove_dir_all(&base);
        fs::create_dir_all(&base).unwrap();
        base
    }

    struct StubAdapter {
        name: &'static str,
        skills: RefCell<Vec<Skill>>,
    }

    unsafe impl Send for StubAdapter {}
    unsafe impl Sync for StubAdapter {}

    impl Adapter for StubAdapter {
        fn name(&self) -> &'static str {
            self.name
        }
        fn detect(&self) -> bool {
            true
        }
        fn target_root(&self) -> Result<PathBuf> {
            Ok(PathBuf::from("/tmp/stub"))
        }
        fn plan(&self, _skill: &Skill) -> Result<InstallPlan> {
            unreachable!("plan not used")
        }
        fn install(&self, _plan: &InstallPlan) -> Result<Entry> {
            unreachable!("install not used")
        }
        fn uninstall(&self, _entry: &Entry) -> Result<()> {
            Ok(())
        }
        fn ingest(&self) -> Result<Vec<Skill>> {
            Ok(self.skills.borrow().clone())
        }
    }

    fn write_source_file(root: &Path, name: &str, body: &str) -> Skill {
        let dir = root.join(name);
        fs::create_dir_all(&dir).unwrap();
        let path = dir.join("SKILL.md");
        fs::write(&path, body).unwrap();
        Skill::from_path(&path).unwrap()
    }

    #[test]
    fn classify_absent_is_create() {
        let src_root = tempdir("classify-absent-src");
        let dst_root = tempdir("classify-absent-dst");
        let skill = write_source_file(&src_root, "alpha", "body\n");

        let target = dst_root.join("alpha").join("SKILL.md");
        let plan = classify(&skill, &target).unwrap();
        match plan {
            IngestPlan::Create { skill: n, .. } => assert_eq!(n, "alpha"),
            other => panic!("expected Create, got {other:?}"),
        }
    }

    #[test]
    fn classify_matching_target_is_skip() {
        let src_root = tempdir("classify-skip-src");
        let dst_root = tempdir("classify-skip-dst");
        let skill = write_source_file(&src_root, "alpha", "matching body\n");

        let target_dir = dst_root.join("alpha");
        fs::create_dir_all(&target_dir).unwrap();
        let target = target_dir.join("SKILL.md");
        fs::write(&target, "matching body\n").unwrap();

        let plan = classify(&skill, &target).unwrap();
        assert!(matches!(plan, IngestPlan::Skip { .. }));
    }

    #[test]
    fn classify_drifted_target_is_conflict() {
        let src_root = tempdir("classify-conflict-src");
        let dst_root = tempdir("classify-conflict-dst");
        let skill = write_source_file(&src_root, "alpha", "source body\n");

        let target_dir = dst_root.join("alpha");
        fs::create_dir_all(&target_dir).unwrap();
        let target = target_dir.join("SKILL.md");
        fs::write(&target, "different body\n").unwrap();

        let plan = classify(&skill, &target).unwrap();
        assert!(matches!(plan, IngestPlan::Conflict { .. }));
    }

    #[test]
    fn run_ingest_dry_run_writes_nothing() {
        let src_root = tempdir("ingest-dry-src");
        let dst_root = tempdir("ingest-dry-dst");
        let skill = write_source_file(&src_root, "alpha", "body\n");

        let adapter = StubAdapter {
            name: "stub",
            skills: RefCell::new(vec![skill]),
        };
        let report = run_ingest(&adapter, &dst_root, false, false).unwrap();

        assert_eq!(report.agent, "stub");
        assert!(!report.written);
        assert_eq!(report.created, 1);
        assert!(!dst_root.join("alpha").join("SKILL.md").exists());
    }

    #[test]
    fn run_ingest_write_creates_file_atomically() {
        let src_root = tempdir("ingest-write-src");
        let dst_root = tempdir("ingest-write-dst");
        let skill = write_source_file(&src_root, "alpha", "body\n");

        let adapter = StubAdapter {
            name: "stub",
            skills: RefCell::new(vec![skill]),
        };
        let report = run_ingest(&adapter, &dst_root, true, false).unwrap();

        assert_eq!(report.created, 1);
        assert!(report.written);
        let target = dst_root.join("alpha").join("SKILL.md");
        assert!(target.exists());
        assert_eq!(fs::read_to_string(&target).unwrap(), "body\n");

        // No leftover .tmp file.
        let tmp = dst_root.join("alpha").join("SKILL.md.tmp");
        assert!(!tmp.exists());
    }

    #[test]
    fn run_ingest_conflict_without_force_is_recorded_not_written() {
        let src_root = tempdir("ingest-conflict-src");
        let dst_root = tempdir("ingest-conflict-dst");
        let skill = write_source_file(&src_root, "alpha", "source body\n");

        // Pre-existing canonical source with different contents.
        fs::create_dir_all(dst_root.join("alpha")).unwrap();
        let target = dst_root.join("alpha").join("SKILL.md");
        fs::write(&target, "pre-existing body\n").unwrap();

        let adapter = StubAdapter {
            name: "stub",
            skills: RefCell::new(vec![skill]),
        };
        let report = run_ingest(&adapter, &dst_root, true, false).unwrap();

        assert_eq!(report.conflicts, 1);
        assert_eq!(report.created, 0);
        // File on disk is unchanged.
        assert_eq!(
            fs::read_to_string(&target).unwrap(),
            "pre-existing body\n"
        );
    }

    #[test]
    fn run_ingest_conflict_with_force_overwrites() {
        let src_root = tempdir("ingest-force-src");
        let dst_root = tempdir("ingest-force-dst");
        let skill = write_source_file(&src_root, "alpha", "source body\n");

        fs::create_dir_all(dst_root.join("alpha")).unwrap();
        let target = dst_root.join("alpha").join("SKILL.md");
        fs::write(&target, "pre-existing body\n").unwrap();

        let adapter = StubAdapter {
            name: "stub",
            skills: RefCell::new(vec![skill]),
        };
        let report = run_ingest(&adapter, &dst_root, true, true).unwrap();

        assert_eq!(report.conflicts, 0);
        assert_eq!(report.created, 1);
        assert_eq!(fs::read_to_string(&target).unwrap(), "source body\n");
    }

    #[test]
    fn ingest_report_serializes_cleanly() {
        let report = IngestReport {
            agent: "claude-code".into(),
            plans: vec![IngestPlan::Create {
                skill: "alpha".into(),
                target: PathBuf::from("/tmp/alpha/SKILL.md"),
            }],
            created: 1,
            skipped: 0,
            conflicts: 0,
            written: true,
        };
        let json = serde_json::to_string(&report).unwrap();
        assert!(json.contains("\"agent\":\"claude-code\""));
        assert!(json.contains("\"kind\":\"create\""));
        assert!(json.contains("\"created\":1"));
    }
}