agent-hub-runtime 0.6.0

AgentHub desktop and companion CLI runtime for Tauri applications
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
use super::*;
use crate::core::{content_hash, skill_store::ScenarioRecord};

fn deploy(repo: &TestRepo, central: &Path, root: &Path, tool: &str, mode: &str) -> PathBuf {
    let target = root.join("old-skill");
    let actual = sync_engine::sync_skill(
        central,
        &target,
        sync_engine::sync_mode_for_tool(tool, Some(mode)),
        sync_engine::ReplacePolicy::NoClobber,
    )
    .unwrap();
    repo.store
        .insert_target(&SkillTargetRecord {
            id: tool.into(),
            skill_id: "skill-id".into(),
            tool: tool.into(),
            target_path: target.to_string_lossy().into_owned(),
            mode: match actual {
                sync_engine::SyncMode::Copy => "copy",
                sync_engine::SyncMode::Symlink => "symlink",
            }
            .into(),
            status: "ok".into(),
            synced_at: Some(1),
            last_error: None,
            source_hash: None,
        })
        .unwrap();
    target
}

#[test]
fn rename_preserves_identity_memberships_and_updates_copies_and_self_source() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    fs::write(central.join("notes.txt"), "retained").unwrap();
    let mut skill = sample_skill("skill-id", "old-skill", &central);
    skill.enabled = false;
    repo.store.insert_skill(&skill).unwrap();
    repo.store
        .set_tags_for_skill(&skill.id, &["team".into()])
        .unwrap();
    repo.store
        .insert_scenario(&ScenarioRecord {
            id: "group".into(),
            name: "Group".into(),
            description: None,
            icon: None,
            sort_order: 0,
            created_at: 1,
            updated_at: 1,
        })
        .unwrap();
    repo.store
        .add_skill_to_scenario("group", &skill.id)
        .unwrap();
    let targets = tempdir().unwrap();
    let old_target = deploy(&repo, &central, targets.path(), "codex", "copy");

    rename_skill_internal(&repo.store, &skill.id, "new-skill").unwrap();

    let new = central_repo::skills_dir().join("new-skill");
    assert!(!central.exists());
    assert!(!old_target.exists());
    assert_eq!(
        fs::read_to_string(new.join("notes.txt")).unwrap(),
        "retained"
    );
    assert_eq!(
        skill_metadata::parse_skill_md(&new).name.as_deref(),
        Some("new-skill")
    );
    let copy = targets.path().join("new-skill");
    assert_eq!(
        skill_metadata::parse_skill_md(&copy).name.as_deref(),
        Some("new-skill")
    );
    let updated = repo.store.get_skill_by_id(&skill.id).unwrap().unwrap();
    assert_eq!(repo.store.get_all_skills().unwrap().len(), 1);
    assert_eq!(updated.created_at, skill.created_at);
    assert!(!updated.enabled);
    assert_eq!(updated.source_ref.as_deref(), new.to_str());
    assert_eq!(
        updated.content_hash.as_deref(),
        Some(content_hash::hash_directory(&new).unwrap().as_str())
    );
    assert_eq!(
        repo.store.get_scenarios_for_skill(&skill.id).unwrap(),
        vec!["group"]
    );
    assert_eq!(repo.store.get_tags_map().unwrap()[&skill.id], vec!["team"]);
    let records = repo.store.get_targets_for_skill(&skill.id).unwrap();
    assert_eq!(records[0].target_path, copy.to_string_lossy());
    assert_eq!(records[0].source_hash, updated.content_hash);
    assert!(!central_repo::base_dir().join("local-sources").exists());
    assert_eq!(fs::read_dir(targets.path()).unwrap().count(), 1);
}

#[test]
fn rename_preserves_external_source_and_retargets_links() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    let external = tempdir().unwrap();
    fs::write(external.path().join("SKILL.md"), "original source").unwrap();
    let mut skill = sample_skill("skill-id", "old-skill", &central);
    skill.source_ref = Some(external.path().to_string_lossy().into_owned());
    repo.store.insert_skill(&skill).unwrap();
    let targets = tempdir().unwrap();
    let old_target = deploy(&repo, &central, targets.path(), "codex", "symlink");

    rename_skill_internal(&repo.store, &skill.id, "new-skill").unwrap();

    let new = central_repo::skills_dir().join("new-skill");
    assert!(fs::symlink_metadata(old_target).is_err());
    let target = targets.path().join("new-skill");
    let record = &repo.store.get_targets_for_skill(&skill.id).unwrap()[0];
    if record.mode == "symlink" {
        assert_eq!(target.canonicalize().unwrap(), new.canonicalize().unwrap());
    } else {
        // Windows may fall back to a copy when link creation is unavailable.
        assert_eq!(
            skill_metadata::parse_skill_md(&target).name.as_deref(),
            Some("new-skill")
        );
    }
    assert_eq!(
        repo.store
            .get_skill_by_id(&skill.id)
            .unwrap()
            .unwrap()
            .source_ref,
        skill.source_ref
    );
    assert_eq!(
        fs::read_to_string(external.path().join("SKILL.md")).unwrap(),
        "original source"
    );
}

#[test]
fn rename_conflicts_preserve_original_library_and_all_deployments() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    repo.store
        .insert_skill(&sample_skill("skill-id", "old-skill", &central))
        .unwrap();
    let targets = tempdir().unwrap();
    let old_target = deploy(&repo, &central, targets.path(), "codex", "copy");
    let conflict = targets.path().join("new-skill");
    fs::create_dir(&conflict).unwrap();
    fs::write(conflict.join("keep.txt"), "user content").unwrap();

    assert!(rename_skill_internal(&repo.store, "skill-id", "new-skill").is_err());

    assert!(central.exists());
    assert!(old_target.exists());
    assert!(!central_repo::skills_dir().join("new-skill").exists());
    assert_eq!(
        fs::read_to_string(conflict.join("keep.txt")).unwrap(),
        "user content"
    );
    assert_eq!(
        repo.store
            .get_skill_by_id("skill-id")
            .unwrap()
            .unwrap()
            .name,
        "old-skill"
    );
}

#[test]
fn rename_rolls_back_files_and_deployments_when_database_publication_fails() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    repo.store
        .insert_skill(&sample_skill("skill-id", "old-skill", &central))
        .unwrap();
    let targets = tempdir().unwrap();
    let old_target = deploy(&repo, &central, targets.path(), "codex", "copy");
    let conn = rusqlite::Connection::open(central_repo::base_dir().join("test.db")).unwrap();
    conn.execute_batch(
        "CREATE TRIGGER reject_rename BEFORE UPDATE OF name ON skills
        BEGIN SELECT RAISE(ABORT, 'injected rename failure'); END;",
    )
    .unwrap();

    let error = rename_skill_internal(&repo.store, "skill-id", "new-skill").unwrap_err();

    assert!(error.message.contains("injected rename failure"));
    assert_eq!(
        skill_metadata::parse_skill_md(&central).name.as_deref(),
        Some("old-skill")
    );
    assert_eq!(
        skill_metadata::parse_skill_md(&old_target).name.as_deref(),
        Some("old-skill")
    );
    assert!(!central_repo::skills_dir().join("new-skill").exists());
    assert!(!targets.path().join("new-skill").exists());
    assert_eq!(fs::read_dir(targets.path()).unwrap().count(), 1);
    let skill = repo.store.get_skill_by_id("skill-id").unwrap().unwrap();
    assert_eq!(skill.name, "old-skill");
    assert_eq!(skill.central_path, central.to_string_lossy());
}

#[test]
fn rename_preserves_a_directory_that_replaced_a_recorded_link() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    repo.store
        .insert_skill(&sample_skill("skill-id", "old-skill", &central))
        .unwrap();
    let targets = tempdir().unwrap();
    let old_target = deploy(&repo, &central, targets.path(), "codex", "symlink");
    if repo.store.get_targets_for_skill("skill-id").unwrap()[0].mode != "symlink" {
        return; // This host cannot create links; copy deployments are covered separately.
    }
    sync_engine::remove_target(&old_target).unwrap();
    fs::create_dir(&old_target).unwrap();
    fs::write(old_target.join("keep.txt"), "user content").unwrap();

    assert!(rename_skill_internal(&repo.store, "skill-id", "new-skill").is_err());
    assert!(central.exists());
    assert_eq!(
        fs::read_to_string(old_target.join("keep.txt")).unwrap(),
        "user content"
    );
}

#[test]
fn detach_source_keeps_identity_content_and_deployments_without_deleting_source() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    let source = tempdir().unwrap();
    fs::write(source.path().join("SKILL.md"), "source").unwrap();
    let mut skill = sample_skill("skill-id", "old-skill", &central);
    skill.source_ref = Some(source.path().to_string_lossy().into_owned());
    repo.store.insert_skill(&skill).unwrap();
    let targets = tempdir().unwrap();
    let deployed = deploy(&repo, &central, targets.path(), "codex", "copy");

    detach_local_skill_source_internal(&repo.store, &skill.id).unwrap();

    let updated = repo.store.get_skill_by_id(&skill.id).unwrap().unwrap();
    assert!(updated.source_ref.is_none());
    assert_eq!(updated.update_status, "local_only");
    assert_eq!(updated.central_path, skill.central_path);
    assert!(central.join("SKILL.md").exists());
    assert!(deployed.join("SKILL.md").exists());
    assert_eq!(
        fs::read_to_string(source.path().join("SKILL.md")).unwrap(),
        "source"
    );
}

#[test]
fn rename_updates_a_direct_library_target_without_copying_or_removing_the_library() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    repo.store
        .insert_skill(&sample_skill("skill-id", "old-skill", &central))
        .unwrap();
    repo.store
        .insert_target(&SkillTargetRecord {
            id: "direct".into(),
            skill_id: "skill-id".into(),
            tool: "shared".into(),
            target_path: central.to_string_lossy().into_owned(),
            mode: "copy".into(),
            status: "ok".into(),
            synced_at: Some(1),
            last_error: None,
            source_hash: None,
        })
        .unwrap();

    rename_skill_internal(&repo.store, "skill-id", "new-skill").unwrap();

    let new = central_repo::skills_dir().join("new-skill");
    assert_eq!(
        skill_metadata::parse_skill_md(&new).name.as_deref(),
        Some("new-skill")
    );
    assert!(!central.exists());
    assert_eq!(
        repo.store.get_targets_for_skill("skill-id").unwrap()[0].target_path,
        new.to_string_lossy()
    );
}

#[test]
fn rename_quotes_yaml_names_and_refuses_control_characters() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    repo.store
        .insert_skill(&sample_skill("skill-id", "old-skill", &central))
        .unwrap();
    let invalid = rename_skill_internal(&repo.store, "skill-id", "new\nname").unwrap_err();
    assert_eq!(invalid.kind, crate::core::error::ErrorKind::InvalidInput);
    let missing = rename_skill_internal(&repo.store, "missing-id", "valid-name").unwrap_err();
    assert_eq!(missing.kind, crate::core::error::ErrorKind::NotFound);
    assert!(central.exists());

    rename_skill_internal(&repo.store, "skill-id", "New: # skill").unwrap();

    let skill = repo.store.get_skill_by_id("skill-id").unwrap().unwrap();
    assert_eq!(
        skill_metadata::parse_skill_md(Path::new(&skill.central_path))
            .name
            .as_deref(),
        Some("New: # skill")
    );
}

#[test]
fn rename_refuses_an_existing_central_directory_before_touching_deployments() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    let other = write_skill_dir("new-skill");
    repo.store
        .insert_skill(&sample_skill("skill-id", "old-skill", &central))
        .unwrap();
    let targets = tempdir().unwrap();
    let deployed = deploy(&repo, &central, targets.path(), "codex", "copy");

    assert!(rename_skill_internal(&repo.store, "skill-id", "new-skill").is_err());

    assert_eq!(
        skill_metadata::parse_skill_md(&central).name.as_deref(),
        Some("old-skill")
    );
    assert_eq!(
        skill_metadata::parse_skill_md(&deployed).name.as_deref(),
        Some("old-skill")
    );
    assert_eq!(
        skill_metadata::parse_skill_md(&other).name.as_deref(),
        Some("new-skill")
    );
}

#[cfg(unix)]
#[test]
fn rename_keeps_a_source_alias_to_the_library_from_becoming_a_missing_source() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    repo.store
        .insert_skill(&sample_skill("skill-id", "old-skill", &central))
        .unwrap();
    let targets = tempdir().unwrap();
    let alias = deploy(&repo, &central, targets.path(), "codex", "symlink");
    repo.store
        .update_skill_source_ref("skill-id", &alias.to_string_lossy())
        .unwrap();

    rename_skill_internal(&repo.store, "skill-id", "new-skill").unwrap();

    let skill = repo.store.get_skill_by_id("skill-id").unwrap().unwrap();
    assert_eq!(
        skill.source_ref.as_deref(),
        Some(skill.central_path.as_str())
    );
    assert!(!alias.exists());
}

#[cfg(unix)]
#[test]
fn rename_preserves_multiple_direct_readers_and_a_linked_library_directory() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    repo.store
        .insert_skill(&sample_skill("skill-id", "old-skill", &central))
        .unwrap();
    let shared = tempdir().unwrap();
    let alias = shared.path().join("skills");
    std::os::unix::fs::symlink(central_repo::skills_dir(), &alias).unwrap();
    for (tool, path) in [
        ("first", central.clone()),
        ("second", central.clone()),
        ("linked", alias.join("old-skill")),
    ] {
        repo.store
            .insert_target(&SkillTargetRecord {
                id: tool.into(),
                skill_id: "skill-id".into(),
                tool: tool.into(),
                target_path: path.to_string_lossy().into_owned(),
                mode: "copy".into(),
                status: "ok".into(),
                synced_at: Some(1),
                last_error: None,
                source_hash: None,
            })
            .unwrap();
    }

    rename_skill_internal(&repo.store, "skill-id", "new-skill").unwrap();

    let new = central_repo::skills_dir().join("new-skill");
    let targets = repo.store.get_targets_for_skill("skill-id").unwrap();
    assert_eq!(targets.len(), 3);
    assert!(targets.iter().all(
        |record| Path::new(&record.target_path).canonicalize().unwrap()
            == new.canonicalize().unwrap()
    ));
    assert!(targets
        .iter()
        .any(|record| record.target_path == alias.join("new-skill").to_string_lossy()));
    assert!(fs::symlink_metadata(&alias)
        .unwrap()
        .file_type()
        .is_symlink());
    assert!(!central.exists());
    assert_eq!(repo.store.get_all_skills().unwrap().len(), 1);
}

#[test]
fn rename_reports_the_preserved_id_when_metadata_needs_a_retry() {
    let repo = test_repo();
    let central = write_skill_dir("old-skill");
    repo.store
        .insert_skill(&sample_skill("skill-id", "old-skill", &central))
        .unwrap();
    let blocked = sync_metadata::metadata_dir().join("schema.json");
    fs::create_dir_all(&blocked).unwrap();

    let error = rename_skill_internal(&repo.store, "skill-id", "new-skill").unwrap_err();

    assert!(error.message.contains("技能已改名"));
    assert!(error.message.contains("skill-id"));
    assert_eq!(
        repo.store
            .get_skill_by_id("skill-id")
            .unwrap()
            .unwrap()
            .name,
        "new-skill"
    );
    fs::remove_dir(&blocked).unwrap();
    rename_skill_internal(&repo.store, "skill-id", "new-skill").unwrap();
    assert!(!central.exists());
    assert!(sync_metadata::metadata_dir()
        .join("skills/skill-id.json")
        .exists());
}