roboticus-db 0.11.4

SQLite persistence layer with 28 tables, FTS5 search, WAL mode, and migration system
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
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
use crate::{Database, DbResultExt};
use roboticus_core::{Result, RoboticusError};
use rusqlite::OptionalExtension;

#[derive(Debug, Clone)]
pub struct SkillRecord {
    pub id: String,
    pub name: String,
    pub kind: String,
    pub description: Option<String>,
    pub source_path: String,
    pub content_hash: String,
    pub triggers_json: Option<String>,
    pub tool_chain_json: Option<String>,
    pub policy_overrides_json: Option<String>,
    pub script_path: Option<String>,
    pub risk_level: String,
    pub enabled: bool,
    pub usage_count: i64,
    pub last_used_at: Option<String>,
    pub last_loaded_at: Option<String>,
    pub created_at: String,
    pub version: String,
    pub author: String,
    pub registry_source: String,
}

#[allow(clippy::too_many_arguments)]
pub fn register_skill(
    db: &Database,
    name: &str,
    kind: &str,
    description: Option<&str>,
    source_path: &str,
    content_hash: &str,
    triggers_json: Option<&str>,
    tool_chain_json: Option<&str>,
    policy_overrides_json: Option<&str>,
    script_path: Option<&str>,
    risk_level: Option<&str>,
) -> Result<String> {
    register_skill_full(
        db,
        name,
        kind,
        description,
        source_path,
        content_hash,
        triggers_json,
        tool_chain_json,
        policy_overrides_json,
        script_path,
        risk_level.unwrap_or("Caution"),
    )
}

#[allow(clippy::too_many_arguments)]
pub fn register_skill_full(
    db: &Database,
    name: &str,
    kind: &str,
    description: Option<&str>,
    source_path: &str,
    content_hash: &str,
    triggers_json: Option<&str>,
    tool_chain_json: Option<&str>,
    policy_overrides_json: Option<&str>,
    script_path: Option<&str>,
    risk_level: &str,
) -> Result<String> {
    register_skill_with_provenance(
        db,
        name,
        kind,
        description,
        source_path,
        content_hash,
        triggers_json,
        tool_chain_json,
        policy_overrides_json,
        script_path,
        risk_level,
        "0.0.0",
        "local",
        "local",
    )
}

/// Register a skill with full provenance (version, author, registry_source).
///
/// Callers that already know the provenance fields (e.g., multi-registry sync,
/// learned-skill write-back) should call this directly.
#[allow(clippy::too_many_arguments)]
pub fn register_skill_with_provenance(
    db: &Database,
    name: &str,
    kind: &str,
    description: Option<&str>,
    source_path: &str,
    content_hash: &str,
    triggers_json: Option<&str>,
    tool_chain_json: Option<&str>,
    policy_overrides_json: Option<&str>,
    script_path: Option<&str>,
    risk_level: &str,
    version: &str,
    author: &str,
    registry_source: &str,
) -> Result<String> {
    let conn = db.conn();
    let id = uuid::Uuid::new_v4().to_string();
    let now = chrono::Utc::now().to_rfc3339();
    conn.execute(
        "INSERT INTO skills (id, name, kind, description, source_path, content_hash, \
         triggers_json, tool_chain_json, policy_overrides_json, script_path, risk_level, \
         usage_count, last_used_at, last_loaded_at, version, author, registry_source) \
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, 0, NULL, ?12, ?13, ?14, ?15)",
        rusqlite::params![
            id,
            name,
            kind,
            description,
            source_path,
            content_hash,
            triggers_json,
            tool_chain_json,
            policy_overrides_json,
            script_path,
            risk_level,
            now,
            version,
            author,
            registry_source,
        ],
    )
    .db_err()?;
    Ok(id)
}

pub fn get_skill(db: &Database, id: &str) -> Result<Option<SkillRecord>> {
    let conn = db.conn();
    conn.query_row(
        "SELECT id, name, kind, description, source_path, content_hash, \
         triggers_json, tool_chain_json, policy_overrides_json, script_path, risk_level, \
         enabled, usage_count, last_used_at, last_loaded_at, created_at, version, author, registry_source \
         FROM skills WHERE id = ?1",
        [id],
        row_to_skill,
    )
    .optional()
    .db_err()
}

pub fn list_skills(db: &Database) -> Result<Vec<SkillRecord>> {
    let conn = db.conn();
    let mut stmt = conn
        .prepare(
            "SELECT id, name, kind, description, source_path, content_hash, \
             triggers_json, tool_chain_json, policy_overrides_json, script_path, risk_level, \
             enabled, usage_count, last_used_at, last_loaded_at, created_at, version, author, registry_source \
             FROM skills ORDER BY name ASC",
        )
        .db_err()?;

    let rows = stmt.query_map([], row_to_skill).db_err()?;

    rows.collect::<std::result::Result<Vec<_>, _>>().db_err()
}

pub fn update_skill(
    db: &Database,
    id: &str,
    content_hash: &str,
    triggers_json: Option<&str>,
    tool_chain_json: Option<&str>,
) -> Result<()> {
    let conn = db.conn();
    conn.execute(
        "UPDATE skills SET content_hash = ?1, triggers_json = ?2, tool_chain_json = ?3, \
         last_loaded_at = datetime('now') WHERE id = ?4",
        rusqlite::params![content_hash, triggers_json, tool_chain_json, id],
    )
    .db_err()?;
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub fn update_skill_full(
    db: &Database,
    id: &str,
    content_hash: &str,
    triggers_json: Option<&str>,
    tool_chain_json: Option<&str>,
    policy_overrides_json: Option<&str>,
    script_path: Option<&str>,
    source_path: &str,
    risk_level: &str,
) -> Result<()> {
    update_skill_with_provenance(
        db,
        id,
        content_hash,
        triggers_json,
        tool_chain_json,
        policy_overrides_json,
        script_path,
        source_path,
        risk_level,
        None,
        None,
        None,
    )
}

/// Update a skill with optional provenance (version, author, registry_source).
///
/// When provenance fields are `None`, the existing DB values are preserved.
#[allow(clippy::too_many_arguments)]
pub fn update_skill_with_provenance(
    db: &Database,
    id: &str,
    content_hash: &str,
    triggers_json: Option<&str>,
    tool_chain_json: Option<&str>,
    policy_overrides_json: Option<&str>,
    script_path: Option<&str>,
    source_path: &str,
    risk_level: &str,
    version: Option<&str>,
    author: Option<&str>,
    registry_source: Option<&str>,
) -> Result<()> {
    let conn = db.conn();
    // Build the SET clause dynamically based on which provenance fields are provided.
    let mut sql = String::from(
        "UPDATE skills SET content_hash = ?1, triggers_json = ?2, tool_chain_json = ?3, \
         policy_overrides_json = ?4, script_path = ?5, source_path = ?6, risk_level = ?7, \
         last_loaded_at = datetime('now')",
    );
    let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = vec![
        Box::new(content_hash.to_string()),
        Box::new(triggers_json.map(|s| s.to_string())),
        Box::new(tool_chain_json.map(|s| s.to_string())),
        Box::new(policy_overrides_json.map(|s| s.to_string())),
        Box::new(script_path.map(|s| s.to_string())),
        Box::new(source_path.to_string()),
        Box::new(risk_level.to_string()),
    ];
    let mut idx = 8;
    if let Some(v) = version {
        sql.push_str(&format!(", version = ?{idx}"));
        params.push(Box::new(v.to_string()));
        idx += 1;
    }
    if let Some(a) = author {
        sql.push_str(&format!(", author = ?{idx}"));
        params.push(Box::new(a.to_string()));
        idx += 1;
    }
    if let Some(r) = registry_source {
        sql.push_str(&format!(", registry_source = ?{idx}"));
        params.push(Box::new(r.to_string()));
        idx += 1;
    }
    sql.push_str(&format!(" WHERE id = ?{idx}"));
    params.push(Box::new(id.to_string()));

    let param_refs: Vec<&dyn rusqlite::types::ToSql> = params.iter().map(|p| p.as_ref()).collect();
    conn.execute(&sql, param_refs.as_slice()).db_err()?;
    Ok(())
}

pub fn delete_skill(db: &Database, id: &str) -> Result<()> {
    let conn = db.conn();
    conn.execute("DELETE FROM skills WHERE id = ?1", [id])
        .db_err()?;
    Ok(())
}

pub fn toggle_skill_enabled(db: &Database, id: &str) -> Result<Option<bool>> {
    let conn = db.conn();
    let current: Option<i32> = conn
        .query_row("SELECT enabled FROM skills WHERE id = ?1", [id], |row| {
            row.get(0)
        })
        .optional()
        .db_err()?;

    match current {
        Some(val) => {
            let new_val = if val != 0 { 0 } else { 1 };
            conn.execute(
                "UPDATE skills SET enabled = ?1 WHERE id = ?2",
                rusqlite::params![new_val, id],
            )
            .db_err()?;
            Ok(Some(new_val != 0))
        }
        None => Ok(None),
    }
}

/// Searches skills whose `triggers_json` contains the given keyword (case-insensitive).
pub fn find_by_trigger(db: &Database, keyword: &str) -> Result<Vec<SkillRecord>> {
    let conn = db.conn();
    let escaped = keyword.replace('%', "\\%").replace('_', "\\_");
    let pattern = format!("%{escaped}%");
    let mut stmt = conn
        .prepare(
            "SELECT id, name, kind, description, source_path, content_hash, \
             triggers_json, tool_chain_json, policy_overrides_json, script_path, risk_level, \
             enabled, usage_count, last_used_at, last_loaded_at, created_at, version, author, registry_source \
             FROM skills WHERE triggers_json LIKE ?1 ESCAPE '\\' AND enabled = 1",
        )
        .db_err()?;

    let rows = stmt.query_map([&pattern], row_to_skill).db_err()?;

    rows.collect::<std::result::Result<Vec<_>, _>>().db_err()
}

pub fn find_enabled_skill_by_script_path(
    db: &Database,
    script_path: &str,
) -> Result<Option<SkillRecord>> {
    let conn = db.conn();
    conn.query_row(
        "SELECT id, name, kind, description, source_path, content_hash, \
         triggers_json, tool_chain_json, policy_overrides_json, script_path, risk_level, \
         enabled, usage_count, last_used_at, last_loaded_at, created_at, version, author, registry_source \
         FROM skills WHERE script_path = ?1 AND enabled = 1 LIMIT 1",
        [script_path],
        row_to_skill,
    )
    .optional()
    .db_err()
}

pub fn find_skill_by_script_path(db: &Database, script_path: &str) -> Result<Option<SkillRecord>> {
    let conn = db.conn();
    let mut stmt = conn
        .prepare(
            "SELECT id, name, kind, description, source_path, content_hash, \
             triggers_json, tool_chain_json, policy_overrides_json, script_path, risk_level, \
             enabled, usage_count, last_used_at, last_loaded_at, created_at, version, author, registry_source \
             FROM skills WHERE script_path = ?1 ORDER BY created_at DESC",
        )
        .db_err()?;
    let rows = stmt.query_map([script_path], row_to_skill).db_err()?;
    let mut matches = rows.collect::<std::result::Result<Vec<_>, _>>().db_err()?;
    if matches.len() > 1 {
        return Err(RoboticusError::Database(format!(
            "ambiguous script_path '{}' matches {} skills",
            script_path,
            matches.len()
        )));
    }
    Ok(matches.pop())
}

fn row_to_skill(row: &rusqlite::Row<'_>) -> rusqlite::Result<SkillRecord> {
    Ok(SkillRecord {
        id: row.get(0)?,
        name: row.get(1)?,
        kind: row.get(2)?,
        description: row.get(3)?,
        source_path: row.get(4)?,
        content_hash: row.get(5)?,
        triggers_json: row.get(6)?,
        tool_chain_json: row.get(7)?,
        policy_overrides_json: row.get(8)?,
        script_path: row.get(9)?,
        risk_level: row.get(10)?,
        enabled: row.get::<_, i32>(11)? != 0,
        usage_count: row.get(12)?,
        last_used_at: row.get(13)?,
        last_loaded_at: row.get(14)?,
        created_at: row.get(15)?,
        version: row.get(16)?,
        author: row.get(17)?,
        registry_source: row.get(18)?,
    })
}

pub fn record_skill_usage(db: &Database, skill_id: &str) -> Result<()> {
    let conn = db.conn();
    conn.execute(
        "UPDATE skills
         SET usage_count = usage_count + 1,
             last_used_at = datetime('now')
         WHERE id = ?1",
        [skill_id],
    )
    .db_err()?;
    Ok(())
}

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

    fn test_db() -> Database {
        Database::new(":memory:").unwrap()
    }

    #[test]
    fn register_and_get_skill() {
        let db = test_db();
        let id = register_skill(
            &db,
            "git-commit",
            "structured",
            Some("Auto-commit helper"),
            "/skills/git-commit.toml",
            "abc123",
            Some(r#"{"keywords":["commit","git"]}"#),
            None,
            None,
            None,
            None,
        )
        .unwrap();

        let skill = get_skill(&db, &id).unwrap().expect("skill should exist");
        assert_eq!(skill.name, "git-commit");
        assert_eq!(skill.kind, "structured");
        assert!(skill.enabled);
    }

    #[test]
    fn list_and_delete_skills() {
        let db = test_db();
        let id = register_skill(
            &db,
            "s1",
            "instruction",
            None,
            "/s1.toml",
            "h1",
            None,
            None,
            None,
            None,
            None,
        )
        .unwrap();
        register_skill(
            &db,
            "s2",
            "structured",
            None,
            "/s2.toml",
            "h2",
            None,
            None,
            None,
            None,
            None,
        )
        .unwrap();

        assert_eq!(list_skills(&db).unwrap().len(), 2);

        delete_skill(&db, &id).unwrap();
        assert_eq!(list_skills(&db).unwrap().len(), 1);
    }

    #[test]
    fn update_skill_changes_hash() {
        let db = test_db();
        let id = register_skill(
            &db,
            "s1",
            "structured",
            None,
            "/s1.toml",
            "old-hash",
            None,
            None,
            None,
            None,
            None,
        )
        .unwrap();

        update_skill(
            &db,
            &id,
            "new-hash",
            Some(r#"{"keywords":["deploy"]}"#),
            None,
        )
        .unwrap();
        let skill = get_skill(&db, &id).unwrap().unwrap();
        assert_eq!(skill.content_hash, "new-hash");
        assert!(skill.triggers_json.unwrap().contains("deploy"));
    }

    #[test]
    fn toggle_skill_enabled_flips_value() {
        let db = test_db();
        let id = register_skill(
            &db,
            "s1",
            "structured",
            None,
            "/s1.toml",
            "h1",
            None,
            None,
            None,
            None,
            None,
        )
        .unwrap();

        let skill = get_skill(&db, &id).unwrap().unwrap();
        assert!(skill.enabled);

        let new_val = toggle_skill_enabled(&db, &id).unwrap();
        assert_eq!(new_val, Some(false));

        let new_val = toggle_skill_enabled(&db, &id).unwrap();
        assert_eq!(new_val, Some(true));

        assert_eq!(toggle_skill_enabled(&db, "nonexistent").unwrap(), None);
    }

    #[test]
    fn find_by_trigger_keyword() {
        let db = test_db();
        register_skill(
            &db,
            "deploy",
            "structured",
            None,
            "/d.toml",
            "h",
            Some(r#"{"keywords":["deploy","ship"]}"#),
            None,
            None,
            None,
            None,
        )
        .unwrap();
        register_skill(
            &db,
            "test",
            "structured",
            None,
            "/t.toml",
            "h",
            Some(r#"{"keywords":["test","ci"]}"#),
            None,
            None,
            None,
            None,
        )
        .unwrap();

        let results = find_by_trigger(&db, "deploy").unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "deploy");
    }

    #[test]
    fn get_skill_nonexistent_returns_none() {
        let db = test_db();
        assert!(get_skill(&db, "no-such-id").unwrap().is_none());
    }

    #[test]
    fn list_skills_empty_db() {
        let db = test_db();
        let skills = list_skills(&db).unwrap();
        assert!(skills.is_empty());
    }

    #[test]
    fn find_by_trigger_no_matches() {
        let db = test_db();
        register_skill(
            &db,
            "s1",
            "structured",
            None,
            "/s1.toml",
            "h",
            Some(r#"{"keywords":["deploy"]}"#),
            None,
            None,
            None,
            None,
        )
        .unwrap();
        let results = find_by_trigger(&db, "nonexistent-keyword").unwrap();
        assert!(results.is_empty());
    }

    #[test]
    fn find_by_trigger_excludes_disabled() {
        let db = test_db();
        let id = register_skill(
            &db,
            "deploy",
            "structured",
            None,
            "/d.toml",
            "h",
            Some(r#"{"keywords":["deploy"]}"#),
            None,
            None,
            None,
            None,
        )
        .unwrap();

        let results = find_by_trigger(&db, "deploy").unwrap();
        assert_eq!(results.len(), 1);

        toggle_skill_enabled(&db, &id).unwrap();
        let results = find_by_trigger(&db, "deploy").unwrap();
        assert!(results.is_empty());
    }

    #[test]
    fn delete_skill_nonexistent_is_noop() {
        let db = test_db();
        delete_skill(&db, "no-such-id").unwrap();
    }

    #[test]
    fn register_skill_all_optional_fields() {
        let db = test_db();
        let id = register_skill(
            &db,
            "full-skill",
            "scripted",
            Some("A fully populated skill"),
            "/skills/full.toml",
            "deadbeef",
            Some(r#"{"keywords":["full","test"]}"#),
            Some(r#"["tool_a","tool_b"]"#),
            Some(r#"{"allow_web":true}"#),
            Some("/scripts/full.sh"),
            None,
        )
        .unwrap();

        let skill = get_skill(&db, &id).unwrap().unwrap();
        assert_eq!(skill.name, "full-skill");
        assert_eq!(skill.kind, "scripted");
        assert_eq!(
            skill.description.as_deref(),
            Some("A fully populated skill")
        );
        assert!(skill.triggers_json.unwrap().contains("full"));
        assert!(skill.tool_chain_json.unwrap().contains("tool_a"));
        assert_eq!(skill.script_path.as_deref(), Some("/scripts/full.sh"));
    }

    #[test]
    fn update_skill_sets_last_loaded_at() {
        let db = test_db();
        let id = register_skill(
            &db,
            "s1",
            "structured",
            None,
            "/s1.toml",
            "old",
            None,
            None,
            None,
            None,
            None,
        )
        .unwrap();
        let skill = get_skill(&db, &id).unwrap().unwrap();
        // last_loaded_at is set during registration
        assert!(skill.last_loaded_at.is_some(), "set on register");

        update_skill(&db, &id, "new", None, None).unwrap();
        let skill = get_skill(&db, &id).unwrap().unwrap();
        assert!(skill.last_loaded_at.is_some(), "still set after update");
        assert_eq!(skill.content_hash, "new");
    }

    #[test]
    fn register_skill_full_persists_risk_level() {
        let db = test_db();
        let id = register_skill_full(
            &db,
            "high-risk",
            "structured",
            Some("dangerous operation"),
            "/skills/high-risk.toml",
            "h-risk",
            Some(r#"{"keywords":["danger"]}"#),
            None,
            Some(r#"{"require_creator":true}"#),
            Some("/skills/bin/high-risk.sh"),
            "Dangerous",
        )
        .unwrap();
        let skill = get_skill(&db, &id).unwrap().unwrap();
        assert_eq!(skill.risk_level, "Dangerous");
    }

    #[test]
    fn find_skill_by_script_path_rejects_ambiguous_duplicates() {
        let db = test_db();
        register_skill_full(
            &db,
            "dup-a",
            "structured",
            None,
            "/skills/dup-a.toml",
            "h-a",
            None,
            None,
            None,
            Some("/skills/bin/dup.sh"),
            "Caution",
        )
        .unwrap();
        register_skill_full(
            &db,
            "dup-b",
            "structured",
            None,
            "/skills/dup-b.toml",
            "h-b",
            None,
            None,
            None,
            Some("/skills/bin/dup.sh"),
            "Caution",
        )
        .unwrap();

        let err = find_skill_by_script_path(&db, "/skills/bin/dup.sh")
            .expect_err("duplicate script paths must fail closed");
        assert!(err.to_string().contains("ambiguous script_path"));
    }

    #[test]
    fn update_skill_full_updates_all_fields() {
        let db = test_db();
        let id = register_skill(
            &db,
            "updatable",
            "structured",
            Some("original"),
            "/old/path.toml",
            "old-hash",
            None,
            None,
            None,
            None,
            None,
        )
        .unwrap();

        update_skill_full(
            &db,
            &id,
            "new-hash",
            Some(r#"{"keywords":["updated"]}"#),
            Some(r#"["new_tool"]"#),
            Some(r#"{"policy":"strict"}"#),
            Some("/scripts/updated.sh"),
            "/new/path.toml",
            "Dangerous",
        )
        .unwrap();

        let skill = get_skill(&db, &id).unwrap().unwrap();
        assert_eq!(skill.content_hash, "new-hash");
        assert!(skill.triggers_json.unwrap().contains("updated"));
        assert!(skill.tool_chain_json.unwrap().contains("new_tool"));
        assert!(skill.policy_overrides_json.unwrap().contains("strict"));
        assert_eq!(skill.script_path.as_deref(), Some("/scripts/updated.sh"));
        assert_eq!(skill.source_path, "/new/path.toml");
        assert_eq!(skill.risk_level, "Dangerous");
        assert!(
            skill.last_loaded_at.is_some(),
            "last_loaded_at should be set"
        );
    }

    #[test]
    fn update_skill_full_clears_optional_fields() {
        let db = test_db();
        let id = register_skill(
            &db,
            "clearable",
            "scripted",
            Some("has everything"),
            "/path.toml",
            "hash",
            Some(r#"{"keywords":["test"]}"#),
            Some(r#"["tool"]"#),
            Some(r#"{"p":true}"#),
            Some("/scripts/test.sh"),
            None,
        )
        .unwrap();

        update_skill_full(
            &db,
            &id,
            "hash2",
            None,
            None,
            None,
            None,
            "/path.toml",
            "Safe",
        )
        .unwrap();

        let skill = get_skill(&db, &id).unwrap().unwrap();
        assert!(skill.triggers_json.is_none());
        assert!(skill.tool_chain_json.is_none());
        assert!(skill.policy_overrides_json.is_none());
        assert!(skill.script_path.is_none());
        assert_eq!(skill.risk_level, "Safe");
    }

    #[test]
    fn find_enabled_skill_by_script_path_found() {
        let db = test_db();
        register_skill_full(
            &db,
            "scripted-skill",
            "scripted",
            None,
            "/skills/scripted.toml",
            "hash",
            None,
            None,
            None,
            Some("/scripts/my_script.sh"),
            "Caution",
        )
        .unwrap();

        let found = find_enabled_skill_by_script_path(&db, "/scripts/my_script.sh")
            .unwrap()
            .unwrap();
        assert_eq!(found.name, "scripted-skill");
    }

    #[test]
    fn find_enabled_skill_by_script_path_not_found() {
        let db = test_db();
        let result = find_enabled_skill_by_script_path(&db, "/no/such/path.sh").unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn find_enabled_skill_by_script_path_excludes_disabled() {
        let db = test_db();
        let id = register_skill_full(
            &db,
            "disabled-scripted",
            "scripted",
            None,
            "/skills/disabled.toml",
            "hash",
            None,
            None,
            None,
            Some("/scripts/disabled.sh"),
            "Caution",
        )
        .unwrap();

        // Disable the skill
        toggle_skill_enabled(&db, &id).unwrap();

        let result = find_enabled_skill_by_script_path(&db, "/scripts/disabled.sh").unwrap();
        assert!(result.is_none(), "disabled skill should not be found");
    }

    #[test]
    fn find_skill_by_script_path_single_match() {
        let db = test_db();
        register_skill_full(
            &db,
            "unique-script",
            "scripted",
            None,
            "/skills/unique.toml",
            "hash",
            None,
            None,
            None,
            Some("/scripts/unique.sh"),
            "Caution",
        )
        .unwrap();

        let found = find_skill_by_script_path(&db, "/scripts/unique.sh")
            .unwrap()
            .unwrap();
        assert_eq!(found.name, "unique-script");
    }

    #[test]
    fn find_skill_by_script_path_no_match() {
        let db = test_db();
        let result = find_skill_by_script_path(&db, "/no/script.sh").unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn find_by_trigger_escapes_special_like_chars() {
        let db = test_db();
        register_skill(
            &db,
            "special-trigger",
            "structured",
            None,
            "/s.toml",
            "h",
            Some(r#"{"keywords":["100%_match","under_score"]}"#),
            None,
            None,
            None,
            None,
        )
        .unwrap();

        // Search for literal "%" -- should find the skill with "100%_match"
        let results = find_by_trigger(&db, "100%").unwrap();
        assert_eq!(results.len(), 1);

        // Search for literal "_" -- should find the skill with "under_score"
        let results = find_by_trigger(&db, "under_score").unwrap();
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn register_skill_defaults_to_caution_risk() {
        let db = test_db();
        let id = register_skill(
            &db,
            "default-risk",
            "structured",
            None,
            "/s.toml",
            "h",
            None,
            None,
            None,
            None,
            None,
        )
        .unwrap();
        let skill = get_skill(&db, &id).unwrap().unwrap();
        assert_eq!(skill.risk_level, "Caution");
    }
}