ironclad-api 0.9.8

HTTP routes, WebSocket, auth, rate limiting, and dashboard for the Ironclad agent runtime
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
use axum::{
    Json,
    extract::{Path, Query, State},
    response::IntoResponse,
};
use serde::Deserialize;
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::collections::HashSet;
use std::path::Path as FsPath;
use std::sync::OnceLock;

use super::{AppState, JsonError, bad_request, internal_err, not_found};

#[derive(Debug, Clone, Deserialize)]
struct RegistryManifest {
    version: String,
    packs: RegistryPacks,
}

#[derive(Debug, Clone, Deserialize)]
struct RegistryPacks {
    skills: RegistrySkillPack,
}

#[derive(Debug, Clone, Deserialize)]
struct RegistrySkillPack {
    path: String,
    files: std::collections::HashMap<String, String>,
}

#[derive(Debug, Deserialize)]
pub struct CatalogQuery {
    #[serde(default)]
    pub q: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct CatalogInstallRequest {
    pub skills: Vec<String>,
    #[serde(default)]
    pub activate: bool,
}

#[derive(Debug, Deserialize)]
pub struct CatalogActivateRequest {
    pub skills: Vec<String>,
}

#[derive(Debug, Clone, Deserialize)]
struct BuiltinSkillRecord {
    name: String,
    description: String,
}

const BUILTIN_SKILLS_JSON: &str = include_str!(concat!(env!("OUT_DIR"), "/builtin-skills.json"));

fn builtin_skills() -> &'static Vec<BuiltinSkillRecord> {
    static BUILTIN_SKILLS: OnceLock<Vec<BuiltinSkillRecord>> = OnceLock::new();
    BUILTIN_SKILLS.get_or_init(|| match serde_json::from_str(BUILTIN_SKILLS_JSON) {
        Ok(skills) => skills,
        Err(e) => {
            tracing::error!(error = %e, "builtin skill registry JSON is invalid");
            Vec::new()
        }
    })
}

fn is_builtin_skill_name(name: &str) -> bool {
    builtin_skills()
        .iter()
        .any(|skill| skill.name.eq_ignore_ascii_case(name))
}

fn is_builtin_skill(s: &ironclad_db::skills::SkillRecord) -> bool {
    s.kind.eq_ignore_ascii_case("builtin") || is_builtin_skill_name(&s.name)
}

fn canonical_in_root(root: &FsPath, base: &FsPath, raw: &FsPath) -> Result<String, String> {
    let candidate = if raw.is_absolute() {
        raw.to_path_buf()
    } else {
        base.join(raw)
    };
    let canonical = std::fs::canonicalize(&candidate).map_err(|e| {
        format!(
            "script path '{}' cannot be resolved: {e}",
            candidate.display()
        )
    })?;
    if !canonical.starts_with(root) {
        return Err(format!(
            "script path '{}' escapes skills_dir '{}'",
            canonical.display(),
            root.display()
        ));
    }
    if !canonical.is_file() {
        return Err(format!(
            "script path '{}' is not a file",
            canonical.display()
        ));
    }
    Ok(canonical.to_string_lossy().to_string())
}

fn validate_policy_overrides(value: &serde_json::Value) -> Result<(), String> {
    let obj = value
        .as_object()
        .ok_or_else(|| "policy_overrides must be a JSON object".to_string())?;
    let allowed = ["require_creator", "deny_external", "disabled"];
    for (k, v) in obj {
        if !allowed.contains(&k.as_str()) {
            return Err(format!("unsupported policy_overrides key '{k}'"));
        }
        if !v.is_boolean() {
            return Err(format!(
                "policy_overrides key '{}' must be boolean, got {}",
                k, v
            ));
        }
    }
    Ok(())
}

fn normalize_risk_level(raw: &str) -> Result<&'static str, String> {
    match raw.to_ascii_lowercase().as_str() {
        "safe" => Ok("Safe"),
        "caution" => Ok("Caution"),
        "dangerous" => Ok("Dangerous"),
        "forbidden" => Ok("Forbidden"),
        _ => Err(format!("invalid risk_level '{raw}'")),
    }
}

fn registry_base_url(manifest_url: &str) -> String {
    if let Some(pos) = manifest_url.rfind('/') {
        manifest_url[..pos].to_string()
    } else {
        manifest_url.to_string()
    }
}

async fn fetch_catalog_manifest(state: &AppState) -> Result<(RegistryManifest, String), String> {
    let config = state.config.read().await;
    let registry_url = config.update.registry_url.clone();
    drop(config);

    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(15))
        .build()
        .map_err(|e| format!("catalog client init failed: {e}"))?;
    let resp = client
        .get(&registry_url)
        .send()
        .await
        .map_err(|e| format!("catalog manifest fetch failed: {e}"))?;
    if !resp.status().is_success() {
        return Err(format!(
            "catalog manifest endpoint returned HTTP {}",
            resp.status()
        ));
    }
    let manifest: RegistryManifest = resp
        .json()
        .await
        .map_err(|e| format!("invalid catalog manifest JSON: {e}"))?;
    Ok((manifest, registry_base_url(&registry_url)))
}

fn skill_item_matches_query(name: &str, query: Option<&str>) -> bool {
    if let Some(q) = query {
        return name.to_ascii_lowercase().contains(&q.to_ascii_lowercase());
    }
    true
}

async fn reload_skills_internal(state: &AppState) -> Result<Value, JsonError> {
    fn risk_level_str(r: ironclad_core::RiskLevel) -> &'static str {
        match r {
            ironclad_core::RiskLevel::Safe => "Safe",
            ironclad_core::RiskLevel::Caution => "Caution",
            ironclad_core::RiskLevel::Dangerous => "Dangerous",
            ironclad_core::RiskLevel::Forbidden => "Forbidden",
        }
    }
    let config = state.config.read().await;
    let skills_dir = std::fs::canonicalize(&config.skills.skills_dir).map_err(|e| {
        (
            axum::http::StatusCode::INTERNAL_SERVER_ERROR,
            format!(
                "failed to resolve skills_dir '{}': {e}",
                config.skills.skills_dir.display()
            ),
        )
    })?;
    drop(config);

    let loaded = ironclad_agent::skills::SkillLoader::load_from_dir(&skills_dir)
        .map_err(|e| internal_err(&e))?;

    let mut added = 0u32;
    let mut updated = 0u32;
    let mut rejected = 0u32;
    let mut issues: Vec<String> = Vec::new();

    let existing_by_name: std::collections::HashMap<String, ironclad_db::skills::SkillRecord> =
        ironclad_db::skills::list_skills(&state.db)
            .inspect_err(|e| tracing::error!(error = %e, "failed to list skills during sync"))
            .unwrap_or_default()
            .into_iter()
            .map(|s| (s.name.clone(), s))
            .collect();

    for skill in &loaded {
        let name = skill.name();
        let hash = skill.hash();
        let kind = match skill {
            ironclad_agent::skills::LoadedSkill::Structured(_, _, _) => "structured",
            ironclad_agent::skills::LoadedSkill::Instruction(_, _, _) => "instruction",
        };
        let triggers = serde_json::to_string(skill.triggers()).ok();
        let source = skill.source_path().to_string_lossy().to_string();
        let desc = skill.description();
        let (tool_chain_json, policy_overrides_json, script_path, risk_level) = if let Some(
            manifest,
        ) =
            skill.structured_manifest()
        {
            if manifest
                .tool_chain
                .as_ref()
                .is_some_and(|chain| !chain.is_empty())
            {
                rejected += 1;
                issues.push(format!(
                        "rejected skill '{}': tool_chain is not yet executable in runtime (remove it or keep empty)",
                        name
                    ));
                continue;
            }

            let tool_chain_json = manifest
                .tool_chain
                .as_ref()
                .and_then(|v| serde_json::to_string(v).ok());
            let policy_overrides_json = if let Some(v) = manifest.policy_overrides.as_ref() {
                if let Err(msg) = validate_policy_overrides(v) {
                    rejected += 1;
                    issues.push(format!("rejected skill '{}': {}", name, msg));
                    continue;
                }
                serde_json::to_string(v).ok()
            } else {
                None
            };
            let base = skill.source_path().parent().unwrap_or(skills_dir.as_path());
            let script_path = manifest
                .script_path
                .as_ref()
                .map(|p| canonical_in_root(&skills_dir, base, p))
                .transpose()
                .map_err(|msg| {
                    rejected += 1;
                    issues.push(format!("rejected skill '{}': {}", name, msg));
                    (
                        axum::http::StatusCode::BAD_REQUEST,
                        "invalid skill manifest".to_string(),
                    )
                })
                .ok()
                .flatten();
            if manifest.script_path.is_some() && script_path.is_none() {
                continue;
            }
            (
                tool_chain_json,
                policy_overrides_json,
                script_path,
                risk_level_str(manifest.risk_level).to_string(),
            )
        } else {
            (None, None, None, "Caution".to_string())
        };

        let existing = existing_by_name.get(name);

        let version = skill.version();
        let author = skill.author();

        if let Some(existing) = existing {
            if existing.content_hash != hash
                || existing.triggers_json.as_deref() != triggers.as_deref()
                || existing.tool_chain_json.as_deref() != tool_chain_json.as_deref()
                || existing.policy_overrides_json.as_deref() != policy_overrides_json.as_deref()
                || existing.script_path.as_deref() != script_path.as_deref()
                || existing.source_path != source
                || existing.risk_level != risk_level
                || existing.version != version
                || existing.author != author
            {
                match ironclad_db::skills::update_skill_with_provenance(
                    &state.db,
                    &existing.id,
                    hash,
                    triggers.as_deref(),
                    tool_chain_json.as_deref(),
                    policy_overrides_json.as_deref(),
                    script_path.as_deref(),
                    &source,
                    &risk_level,
                    Some(version),
                    Some(author),
                    None, // preserve existing registry_source
                ) {
                    Ok(_) => updated += 1,
                    Err(e) => {
                        tracing::warn!(error = %e, skill = name, "skill sync: update_skill failed");
                    }
                }
            }
        } else {
            match ironclad_db::skills::register_skill_with_provenance(
                &state.db,
                name,
                kind,
                desc,
                &source,
                hash,
                triggers.as_deref(),
                tool_chain_json.as_deref(),
                policy_overrides_json.as_deref(),
                script_path.as_deref(),
                &risk_level,
                version,
                author,
                "local", // locally loaded from disk
            ) {
                Ok(_) => added += 1,
                Err(e) => {
                    tracing::warn!(error = %e, skill = name, "skill sync: register_skill failed");
                }
            }
        }
    }

    Ok(serde_json::json!({
        "reloaded": true,
        "scanned": loaded.len(),
        "added": added,
        "updated": updated,
        "rejected": rejected,
        "issues": issues,
    }))
}

pub async fn list_skills(State(state): State<AppState>) -> impl IntoResponse {
    match ironclad_db::skills::list_skills(&state.db) {
        Ok(skills) => {
            let mut items: Vec<Value> = skills
                .into_iter()
                .map(|s| {
                    let built_in = is_builtin_skill(&s);
                    serde_json::json!({
                        "id": s.id,
                        "name": s.name,
                        "kind": s.kind,
                        "description": s.description,
                        "risk_level": s.risk_level,
                        "enabled": s.enabled || built_in,
                        "built_in": built_in,
                        "last_loaded_at": s.last_loaded_at,
                        "created_at": s.created_at,
                        "version": s.version,
                        "author": s.author,
                        "registry_source": s.registry_source,
                    })
                })
                .collect();
            let seen: HashSet<String> = items
                .iter()
                .filter_map(|item| item.get("name").and_then(|v| v.as_str()))
                .map(|name| name.to_ascii_lowercase())
                .collect();
            for built_in in builtin_skills() {
                if seen.contains(&built_in.name.to_ascii_lowercase()) {
                    continue;
                }
                items.push(serde_json::json!({
                    "id": format!("builtin:{}", built_in.name),
                    "name": built_in.name,
                    "kind": "builtin",
                    "description": built_in.description,
                    "risk_level": "Caution",
                    "enabled": true,
                    "built_in": true,
                    "last_loaded_at": Value::Null,
                    "created_at": Value::Null,
                }));
            }
            Ok(axum::Json(serde_json::json!({ "skills": items })))
        }
        Err(e) => Err(internal_err(&e)),
    }
}

pub async fn get_skill(State(state): State<AppState>, Path(id): Path<String>) -> impl IntoResponse {
    match ironclad_db::skills::get_skill(&state.db, &id) {
        Ok(Some(s)) => {
            let built_in = is_builtin_skill(&s);
            Ok(axum::Json(serde_json::json!({
                "id": s.id,
                "name": s.name,
                "kind": s.kind,
                "description": s.description,
                "content_hash": s.content_hash,
                "triggers_json": s.triggers_json,
                "tool_chain_json": s.tool_chain_json,
                "policy_overrides_json": s.policy_overrides_json,
                "risk_level": s.risk_level,
                "enabled": s.enabled || built_in,
                "built_in": built_in,
                "last_loaded_at": s.last_loaded_at,
                "created_at": s.created_at,
                "version": s.version,
                "author": s.author,
                "registry_source": s.registry_source,
            })))
        }
        Ok(None) => Err(not_found(format!("skill {id} not found"))),
        Err(e) => Err(internal_err(&e)),
    }
}

pub async fn reload_skills(State(state): State<AppState>) -> Result<impl IntoResponse, JsonError> {
    let payload = reload_skills_internal(&state).await?;
    Ok(axum::Json(payload))
}

pub async fn catalog_list(
    State(state): State<AppState>,
    Query(query): Query<CatalogQuery>,
) -> impl IntoResponse {
    let q = query.q.as_deref();
    let mut items = Vec::<Value>::new();
    let builtin_names: HashSet<String> = builtin_skills()
        .iter()
        .map(|skill| skill.name.to_ascii_lowercase())
        .collect();
    for built_in in builtin_skills() {
        if skill_item_matches_query(&built_in.name, q) {
            items.push(serde_json::json!({
                "name": built_in.name,
                "kind": "builtin",
                "description": built_in.description,
                "source": "builtin",
            }));
        }
    }

    if let Ok((manifest, _base_url)) = fetch_catalog_manifest(&state).await {
        for (filename, sha256) in &manifest.packs.skills.files {
            let name = filename.strip_suffix(".md").unwrap_or(filename).to_string();
            if builtin_names.contains(&name.to_ascii_lowercase()) {
                continue;
            }
            if skill_item_matches_query(&name, q) {
                items.push(serde_json::json!({
                    "name": name,
                    "kind": "instruction",
                    "source": "registry",
                    "filename": filename,
                    "sha256": sha256,
                    "version": manifest.version,
                }));
            }
        }
    }

    axum::Json(serde_json::json!({ "items": items }))
}

pub async fn catalog_install(
    State(state): State<AppState>,
    Json(req): Json<CatalogInstallRequest>,
) -> Result<impl IntoResponse, JsonError> {
    if req.skills.is_empty() {
        return Err(bad_request("skills list is required"));
    }
    let (manifest, base_url) = fetch_catalog_manifest(&state).await.map_err(|e| {
        (
            axum::http::StatusCode::BAD_GATEWAY,
            format!("failed to fetch catalog: {e}"),
        )
    })?;

    let config = state.config.read().await;
    let skills_dir = config.skills.skills_dir.clone();
    drop(config);
    std::fs::create_dir_all(&skills_dir)
        .map_err(|e| internal_err(&format!("failed to create skills dir: {e}")))?;

    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(20))
        .build()
        .map_err(|e| internal_err(&format!("catalog client init failed: {e}")))?;

    let selected: Vec<(&String, &String)> = manifest
        .packs
        .skills
        .files
        .iter()
        .filter(|(filename, _)| {
            let name = filename.strip_suffix(".md").unwrap_or(filename.as_str());
            req.skills.iter().any(|s| s == name || s == *filename)
        })
        .collect();
    if selected.is_empty() {
        return Err(not_found("no matching catalog skills found"));
    }
    let selected_names: HashSet<String> = selected
        .iter()
        .map(|(filename, _)| {
            filename
                .strip_suffix(".md")
                .unwrap_or(filename.as_str())
                .to_ascii_lowercase()
        })
        .collect();
    let builtin_collisions: Vec<String> = builtin_skills()
        .iter()
        .filter(|skill| selected_names.contains(&skill.name.to_ascii_lowercase()))
        .map(|skill| skill.name.clone())
        .collect();
    if !builtin_collisions.is_empty() {
        return Err(bad_request(format!(
            "cannot install skills that are built-in: {}",
            builtin_collisions.join(", ")
        )));
    }

    let mut rollback_existing: Vec<(std::path::PathBuf, Vec<u8>)> = Vec::new();
    let mut rollback_new: Vec<std::path::PathBuf> = Vec::new();
    let mut installed: Vec<String> = Vec::new();

    for (filename, expected_hash) in selected {
        let url = format!("{}/{}{}", base_url, manifest.packs.skills.path, filename);
        let bytes = client
            .get(&url)
            .send()
            .await
            .map_err(|e| internal_err(&format!("download failed for {filename}: {e}")))?
            .bytes()
            .await
            .map_err(|e| internal_err(&format!("download body failed for {filename}: {e}")))?;
        let actual_hash = hex::encode(Sha256::digest(&bytes));
        if actual_hash != *expected_hash {
            // Roll back any files already touched.
            for (path, old) in rollback_existing.drain(..) {
                if let Err(e) = std::fs::write(&path, old) {
                    tracing::warn!(error = %e, path = %path.display(), "failed to restore file during skill install rollback");
                }
            }
            for path in rollback_new.drain(..) {
                if let Err(e) = std::fs::remove_file(&path) {
                    tracing::warn!(error = %e, path = %path.display(), "failed to remove file during skill install rollback");
                }
            }
            return Err(bad_request(format!("checksum mismatch for {filename}")));
        }

        // Path traversal guard: reject filenames containing path separators or
        // parent-directory components so that a malicious registry manifest
        // cannot write outside skills_dir.
        if filename.contains('/')
            || filename.contains('\\')
            || filename.contains("..")
            || filename.starts_with('.')
        {
            // Roll back any files already touched.
            for (path, old) in rollback_existing.drain(..) {
                if let Err(e) = std::fs::write(&path, old) {
                    tracing::warn!(error = %e, path = %path.display(), "failed to restore file during skill install rollback");
                }
            }
            for path in rollback_new.drain(..) {
                if let Err(e) = std::fs::remove_file(&path) {
                    tracing::warn!(error = %e, path = %path.display(), "failed to remove file during skill install rollback");
                }
            }
            return Err(bad_request(format!(
                "invalid filename rejected: {filename}"
            )));
        }

        let target = skills_dir.join(filename);
        // Canonicalize and verify the resolved path stays inside skills_dir.
        // We check the parent directory (which must exist) since the file
        // itself may not exist yet.
        let target_parent = target.parent().unwrap_or(&skills_dir);
        let canonical_parent = std::fs::canonicalize(target_parent)
            .map_err(|e| internal_err(&format!("failed to resolve target directory: {e}")))?;
        let canonical_skills_dir = std::fs::canonicalize(&skills_dir)
            .map_err(|e| internal_err(&format!("failed to resolve skills_dir: {e}")))?;
        if !canonical_parent.starts_with(&canonical_skills_dir) {
            return Err(bad_request(format!(
                "filename escapes skills directory: {filename}"
            )));
        }

        if target.exists() {
            let old = std::fs::read(&target)
                .map_err(|e| internal_err(&format!("failed to backup {filename}: {e}")))?;
            rollback_existing.push((target.clone(), old));
        } else {
            rollback_new.push(target.clone());
        }

        let tmp = skills_dir.join(format!(".{}.tmp", filename));
        std::fs::write(&tmp, &bytes)
            .map_err(|e| internal_err(&format!("failed to write temp {filename}: {e}")))?;
        std::fs::rename(&tmp, &target)
            .map_err(|e| internal_err(&format!("failed to install {filename}: {e}")))?;
        installed.push(filename.clone());
    }

    let mut activation = None;
    if req.activate {
        match reload_skills_internal(&state).await {
            Ok(payload) => activation = Some(payload),
            Err(e) => {
                // Roll back all file writes if activation failed.
                for (path, old) in rollback_existing {
                    if let Err(e) = std::fs::write(&path, old) {
                        tracing::error!(error = %e, path = %path.display(), "rollback: failed to restore file");
                    }
                }
                for path in rollback_new {
                    if let Err(e) = std::fs::remove_file(&path) {
                        tracing::error!(error = %e, path = %path.display(), "rollback: failed to remove file");
                    }
                }
                return Err(e);
            }
        }
    }

    Ok(axum::Json(serde_json::json!({
        "ok": true,
        "version": manifest.version,
        "installed": installed,
        "activated": req.activate,
        "activation": activation,
    })))
}

pub async fn catalog_activate(
    State(state): State<AppState>,
    Json(req): Json<CatalogActivateRequest>,
) -> Result<impl IntoResponse, JsonError> {
    let payload = reload_skills_internal(&state).await?;
    Ok(axum::Json(serde_json::json!({
        "ok": true,
        "requested_skills": req.skills,
        "activation": payload,
    })))
}

pub async fn audit_skills(State(state): State<AppState>) -> Result<impl IntoResponse, JsonError> {
    let config = state.config.read().await;
    let skills_dir = std::fs::canonicalize(&config.skills.skills_dir).map_err(|e| {
        (
            axum::http::StatusCode::INTERNAL_SERVER_ERROR,
            format!(
                "failed to resolve skills_dir '{}': {e}",
                config.skills.skills_dir.display()
            ),
        )
    })?;
    drop(config);

    let loaded = ironclad_agent::skills::SkillLoader::load_from_dir(&skills_dir)
        .map_err(|e| internal_err(&e))?;
    let loaded_by_name: std::collections::HashMap<
        String,
        (&ironclad_agent::skills::LoadedSkill, String),
    > = loaded
        .iter()
        .map(|s| (s.name().to_string(), (s, s.hash().to_string())))
        .collect();

    let db_skills = ironclad_db::skills::list_skills(&state.db).map_err(|e| internal_err(&e))?;
    let mut drifted = 0usize;
    let mut skills_report = Vec::new();

    for s in &db_skills {
        let (drift_status, drift_reason) = if let Err(msg) = normalize_risk_level(&s.risk_level) {
            drifted += 1;
            ("invalid_metadata", msg)
        } else if let Some((loaded_skill, loaded_hash)) = loaded_by_name.get(&s.name) {
            if &s.content_hash != loaded_hash {
                drifted += 1;
                (
                    "drifted",
                    format!("hash mismatch (db={} disk={})", s.content_hash, loaded_hash),
                )
            } else {
                let mut issues = Vec::new();
                if let Some(manifest) = loaded_skill.structured_manifest() {
                    if manifest.tool_chain.as_ref().is_some_and(|c| !c.is_empty()) {
                        issues.push(
                            "tool_chain present but runtime does not execute skill tool chains"
                                .to_string(),
                        );
                    }
                    if let Some(v) = manifest.policy_overrides.as_ref()
                        && let Err(msg) = validate_policy_overrides(v)
                    {
                        issues.push(msg);
                    }
                    if let Some(script) = manifest.script_path.as_ref() {
                        let base = loaded_skill
                            .source_path()
                            .parent()
                            .unwrap_or(skills_dir.as_path());
                        if let Err(msg) = canonical_in_root(&skills_dir, base, script) {
                            issues.push(msg);
                        }
                    }
                }
                if issues.is_empty() {
                    ("in_sync", String::new())
                } else {
                    drifted += 1;
                    ("invalid_metadata", issues.join("; "))
                }
            }
        } else {
            drifted += 1;
            (
                "missing_on_disk",
                "present in DB but not found in skills_dir scan".to_string(),
            )
        };

        skills_report.push(serde_json::json!({
            "id": s.id,
            "name": s.name,
            "enabled": s.enabled,
            "risk_level": s.risk_level,
            "drift_status": drift_status,
            "drift_reason": drift_reason,
        }));
    }

    let tool_names: Vec<String> = state
        .tools
        .list()
        .into_iter()
        .map(|t| t.name().to_string())
        .collect();
    let key_tools = [
        ("run_script", serde_json::json!({"path":"sample.sh"})),
        ("read_file", serde_json::json!({"path":"README.md"})),
        (
            "write_file",
            serde_json::json!({"path":"tmp/audit.txt","content":"x"}),
        ),
        (
            "edit_file",
            serde_json::json!({"path":"tmp/audit.txt","old":"x","new":"y"}),
        ),
        ("list_directory", serde_json::json!({"path":"."})),
        ("glob_files", serde_json::json!({"pattern":"*.md"})),
        ("search_files", serde_json::json!({"query":"TODO"})),
    ];
    let mut capability_rows = Vec::new();
    for (tool_name, sample_params) in key_tools {
        let Some(tool) = state.tools.get(tool_name) else {
            capability_rows.push(serde_json::json!({
                "tool_name": tool_name,
                "present": false,
            }));
            continue;
        };
        let normal_tier = ironclad_core::SurvivalTier::Normal;
        let creator_allowed = super::agent::check_tool_policy(
            &state.policy_engine,
            tool_name,
            &sample_params,
            ironclad_core::InputAuthority::Creator,
            normal_tier,
            tool.risk_level(),
        )
        .is_ok();
        let external_allowed = super::agent::check_tool_policy(
            &state.policy_engine,
            tool_name,
            &sample_params,
            ironclad_core::InputAuthority::External,
            normal_tier,
            tool.risk_level(),
        )
        .is_ok();
        let approval_classification = state
            .approvals
            .check_tool(tool_name)
            .map(|c| format!("{c:?}"))
            .unwrap_or_else(|e| format!("error:{e}"));
        capability_rows.push(serde_json::json!({
            "tool_name": tool_name,
            "present": true,
            "risk_level": format!("{:?}", tool.risk_level()),
            "creator_allowed": creator_allowed,
            "external_allowed": external_allowed,
            "approval_classification": approval_classification,
        }));
    }

    Ok(axum::Json(serde_json::json!({
        "skills_dir": skills_dir,
        "summary": {
            "db_skills": db_skills.len(),
            "disk_skills": loaded.len(),
            "drifted_skills": drifted,
        },
        "runtime": {
            "registered_tools": tool_names,
            "capabilities": capability_rows,
        },
        "skills": skills_report,
    })))
}

pub async fn toggle_skill(
    State(state): State<AppState>,
    Path(id): Path<String>,
) -> Result<impl IntoResponse, JsonError> {
    let existing = ironclad_db::skills::get_skill(&state.db, &id).map_err(|e| internal_err(&e))?;
    if let Some(s) = existing.as_ref()
        && is_builtin_skill(s)
    {
        return Err(JsonError(
            axum::http::StatusCode::FORBIDDEN,
            format!("skill {} is built-in and cannot be disabled", s.name),
        ));
    }
    match ironclad_db::skills::toggle_skill_enabled(&state.db, &id) {
        Ok(Some(new_enabled)) => Ok(axum::Json(serde_json::json!({
            "id": id,
            "enabled": new_enabled,
        }))),
        Ok(None) => Err(not_found(format!("skill {id} not found"))),
        Err(e) => Err(internal_err(&e)),
    }
}

pub async fn delete_skill(
    State(state): State<AppState>,
    Path(id): Path<String>,
) -> Result<impl IntoResponse, JsonError> {
    match ironclad_db::skills::get_skill(&state.db, &id) {
        Ok(Some(skill)) => {
            if is_builtin_skill(&skill) {
                return Err(JsonError(
                    axum::http::StatusCode::FORBIDDEN,
                    format!("skill {} is built-in and cannot be deleted", skill.name),
                ));
            }
            ironclad_db::skills::delete_skill(&state.db, &id).map_err(|e| internal_err(&e))?;
            Ok(axum::Json(serde_json::json!({
                "id": id,
                "name": skill.name,
                "deleted": true,
            })))
        }
        Ok(None) => Err(not_found(format!("skill {id} not found"))),
        Err(e) => Err(internal_err(&e)),
    }
}

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

    fn sample_record(name: &str, kind: &str) -> ironclad_db::skills::SkillRecord {
        ironclad_db::skills::SkillRecord {
            id: "s1".into(),
            name: name.into(),
            kind: kind.into(),
            description: Some("desc".into()),
            source_path: "/tmp/skill.md".into(),
            content_hash: "abc".into(),
            triggers_json: None,
            tool_chain_json: None,
            policy_overrides_json: None,
            script_path: None,
            risk_level: "Caution".into(),
            enabled: true,
            last_loaded_at: None,
            created_at: "now".into(),
            version: "0.0.0".into(),
            author: "local".into(),
            registry_source: "local".into(),
        }
    }

    #[test]
    fn builtin_catalog_loads_and_has_unique_names() {
        let catalog = builtin_skills();
        assert!(!catalog.is_empty());
        let mut seen = std::collections::HashSet::new();
        for skill in catalog {
            assert!(!skill.name.trim().is_empty());
            assert!(!skill.description.trim().is_empty());
            assert!(
                seen.insert(skill.name.to_ascii_lowercase()),
                "duplicate builtin skill name: {}",
                skill.name
            );
        }
    }

    #[test]
    fn builtin_skill_detection_is_case_insensitive() {
        assert!(is_builtin_skill_name("SELF-DIAGNOSTICS"));
        assert!(!is_builtin_skill_name("scope-cli"));
        assert!(is_builtin_skill(&sample_record(
            "self-diagnostics",
            "instruction"
        )));
        assert!(is_builtin_skill(&sample_record("custom", "builtin")));
        assert!(!is_builtin_skill(&sample_record("custom", "instruction")));
    }

    #[test]
    fn canonical_in_root_accepts_files_and_rejects_escape() {
        let root = tempfile::tempdir().unwrap();
        let nested = root.path().join("scripts");
        std::fs::create_dir_all(&nested).unwrap();
        let script = nested.join("run.sh");
        std::fs::write(&script, "echo ok\n").unwrap();
        let canonical_root = std::fs::canonicalize(root.path()).unwrap();

        let canonical = canonical_in_root(
            &canonical_root,
            &canonical_root,
            FsPath::new("scripts/run.sh"),
        )
        .expect("path inside root should resolve");
        assert!(canonical.ends_with("run.sh"));

        let outside = tempfile::NamedTempFile::new().unwrap();
        let escaped = canonical_in_root(&canonical_root, &canonical_root, outside.path());
        assert!(escaped.is_err());
    }

    #[test]
    fn validate_policy_overrides_accepts_allowed_boolean_keys() {
        let ok = serde_json::json!({
            "require_creator": true,
            "deny_external": false,
            "disabled": false
        });
        assert!(validate_policy_overrides(&ok).is_ok());

        let bad_key = serde_json::json!({"nope": true});
        assert!(validate_policy_overrides(&bad_key).is_err());

        let bad_type = serde_json::json!({"disabled": "yes"});
        assert!(validate_policy_overrides(&bad_type).is_err());
    }

    #[test]
    fn normalize_risk_level_canonicalizes_supported_values() {
        assert_eq!(normalize_risk_level("safe").unwrap(), "Safe");
        assert_eq!(normalize_risk_level("CAUTION").unwrap(), "Caution");
        assert_eq!(normalize_risk_level("Dangerous").unwrap(), "Dangerous");
        assert_eq!(normalize_risk_level("forbidden").unwrap(), "Forbidden");
        assert!(normalize_risk_level("unknown").is_err());
    }

    #[test]
    fn registry_base_url_and_query_matching_behave() {
        assert_eq!(
            registry_base_url("https://example.com/catalog/manifest.json"),
            "https://example.com/catalog"
        );
        assert_eq!(registry_base_url("manifest.json"), "manifest.json");

        assert!(skill_item_matches_query("self-diagnostics", None));
        assert!(skill_item_matches_query("self-diagnostics", Some("Diag")));
        assert!(!skill_item_matches_query(
            "self-diagnostics",
            Some("wallet")
        ));
    }

    #[test]
    fn validate_policy_overrides_rejects_non_object() {
        assert!(validate_policy_overrides(&serde_json::json!("not-object")).is_err());
    }

    #[test]
    fn canonical_in_root_rejects_directory_targets() {
        let root = tempfile::tempdir().unwrap();
        let canonical_root = std::fs::canonicalize(root.path()).unwrap();
        std::fs::create_dir_all(canonical_root.join("scripts")).unwrap();
        let result = canonical_in_root(&canonical_root, &canonical_root, FsPath::new("scripts"));
        assert!(result.is_err());
    }
}