clipmem 0.5.4

macOS clipboard memory backed by SQLite and searchable from agent runtimes
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 std::path::{Path, PathBuf};
use std::process::Command as ProcessCommand;

use anyhow::{anyhow, Context, Result};

use super::doctor::render_agent_doctor_report;
use super::doctor::{AgentDoctorCheck, AgentDoctorStatus, HermesDoctorReport};
use super::package::{packaged_hermes_files, resolve_hermes_skill_dir, HERMES_SKILL_NAME};
use super::support::{
    binary_check, find_executable, frontmatter_value, parse_skill_frontmatter,
    required_frontmatter_value, validate_packaged_skill_file, validate_required_skill_references,
};
use crate::cli::schema::HermesDoctorArgs;

#[cfg(test)]
use super::package::packaged_hermes_skill;

pub(in crate::cli) fn hermes_doctor(args: &HermesDoctorArgs) -> Result<()> {
    let report = build_hermes_doctor_report(args)?;
    print!("{}", render_hermes_doctor_report(&report));

    if report
        .checks
        .iter()
        .any(|check| matches!(check.status, AgentDoctorStatus::Fail))
    {
        Err(anyhow!("Hermes Agent integration checks failed"))
    } else {
        Ok(())
    }
}

pub(in crate::cli) fn build_hermes_doctor_report(
    args: &HermesDoctorArgs,
) -> Result<HermesDoctorReport> {
    let target_dir = resolve_hermes_skill_dir(args.dest.as_deref())?;
    let mut checks = Vec::new();

    let clipmem_path = find_executable("clipmem");
    checks.push(binary_check(
        "Host clipmem on PATH",
        clipmem_path.as_ref(),
        &[
            "Install clipmem with `brew install tristanmanchester/tap/clipmem`.",
            "Or install it with `cargo install clipmem`.",
            "Then run `clipmem setup` to initialize the database and start background capture.",
        ],
    ));

    let hermes_path = find_executable("hermes");
    checks.push(hermes_binary_check(hermes_path.as_ref()));

    if target_dir.exists() {
        checks.push(AgentDoctorCheck {
            status: AgentDoctorStatus::Ok,
            label: "Installed skill directory".to_string(),
            detail: format!("Found {}", target_dir.display()),
            next_steps: Vec::new(),
        });
    } else {
        checks.push(AgentDoctorCheck {
            status: AgentDoctorStatus::Fail,
            label: "Installed skill directory".to_string(),
            detail: format!("Missing {}", target_dir.display()),
            next_steps: vec![
                "Install the skill with `clipmem agents hermes install-skill`.".to_string(),
            ],
        });
    }

    let skill_path = target_dir.join("SKILL.md");
    if skill_path.is_file() {
        match validate_hermes_skill_dir(&target_dir) {
            Ok(()) => checks.push(AgentDoctorCheck {
                status: AgentDoctorStatus::Ok,
                label: "SKILL.md metadata".to_string(),
                detail: format!(
                    "Validated {} and referenced package files under {}",
                    skill_path.display(),
                    target_dir.display()
                ),
                next_steps: Vec::new(),
            }),
            Err(error) => checks.push(AgentDoctorCheck {
                status: AgentDoctorStatus::Fail,
                label: "SKILL.md metadata".to_string(),
                detail: error.to_string(),
                next_steps: vec![
                    "Reinstall the packaged skill with `clipmem agents hermes install-skill --force`.".to_string(),
                    "Use `clipmem agents hermes print-skill` to inspect the packaged content.".to_string(),
                ],
            }),
        }
    } else {
        checks.push(AgentDoctorCheck {
            status: AgentDoctorStatus::Fail,
            label: "SKILL.md file".to_string(),
            detail: format!("Missing {}", skill_path.display()),
            next_steps: vec![
                "Install the packaged skill with `clipmem agents hermes install-skill`."
                    .to_string(),
            ],
        });
    }

    checks.push(hermes_skill_discovery_check(hermes_path.as_ref()));

    Ok(HermesDoctorReport { target_dir, checks })
}

pub(in crate::cli) fn hermes_binary_check(path: Option<&PathBuf>) -> AgentDoctorCheck {
    match path {
        Some(path) => AgentDoctorCheck {
            status: AgentDoctorStatus::Ok,
            label: "Host hermes on PATH".to_string(),
            detail: format!("Found {}", path.display()),
            next_steps: Vec::new(),
        },
        None => AgentDoctorCheck {
            status: AgentDoctorStatus::Warn,
            label: "Host hermes on PATH".to_string(),
            detail: "hermes is not available; skipping live Hermes skill discovery checks."
                .to_string(),
            next_steps: vec![
                "Install Hermes Agent if you want to verify live skill discovery.".to_string(),
            ],
        },
    }
}

pub(in crate::cli) fn hermes_skill_discovery_check(
    hermes_path: Option<&PathBuf>,
) -> AgentDoctorCheck {
    let Some(hermes_path) = hermes_path else {
        return AgentDoctorCheck {
            status: AgentDoctorStatus::Warn,
            label: "Hermes skill discovery".to_string(),
            detail: "Skipped live discovery because `hermes` is not available.".to_string(),
            next_steps: vec![
                "Install Hermes Agent, then run `clipmem agents hermes doctor` again.".to_string(),
            ],
        };
    };

    let output = ProcessCommand::new(hermes_path)
        .args(["skills", "list"])
        .output();

    match output {
        Ok(output) if output.status.success() => {
            let stdout = String::from_utf8_lossy(&output.stdout).to_string();
            if stdout.contains(HERMES_SKILL_NAME) {
                AgentDoctorCheck {
                    status: AgentDoctorStatus::Ok,
                    label: "Hermes skill discovery".to_string(),
                    detail: format!("`hermes skills list` includes {HERMES_SKILL_NAME}."),
                    next_steps: Vec::new(),
                }
            } else {
                AgentDoctorCheck {
                    status: AgentDoctorStatus::Warn,
                    label: "Hermes skill discovery".to_string(),
                    detail: format!(
                        "`hermes skills list` did not show {HERMES_SKILL_NAME}."
                    ),
                    next_steps: vec![
                        "Restart Hermes or open a fresh session so it rescans skills.".to_string(),
                        "If you installed to a custom path, add that directory under `skills.external_dirs` in ~/.hermes/config.yaml.".to_string(),
                    ],
                }
            }
        }
        Ok(output) => AgentDoctorCheck {
            status: AgentDoctorStatus::Warn,
            label: "Hermes skill discovery".to_string(),
            detail: format!(
                "Could not list Hermes skills (`hermes skills list` exited with {}).",
                output.status
            ),
            next_steps: vec![
                "Run `hermes skills list` manually and verify whether the skill appears."
                    .to_string(),
            ],
        },
        Err(error) => AgentDoctorCheck {
            status: AgentDoctorStatus::Warn,
            label: "Hermes skill discovery".to_string(),
            detail: format!("Could not list Hermes skills: {error}"),
            next_steps: vec![
                "Run `hermes skills list` manually and verify whether the skill appears."
                    .to_string(),
            ],
        },
    }
}

pub(in crate::cli) fn validate_hermes_skill_file(path: &Path) -> Result<()> {
    let content = std::fs::read_to_string(path)
        .with_context(|| format!("failed to read {}", path.display()))?;
    validate_hermes_skill_content(&content)
}

pub(in crate::cli) fn validate_hermes_skill_dir(path: &Path) -> Result<()> {
    let skill_path = path.join("SKILL.md");
    validate_hermes_skill_file(&skill_path)?;

    for file in packaged_hermes_files() {
        let installed_path = path.join(file.relative_path);
        validate_packaged_skill_file(&installed_path, file.relative_path)?;
    }

    Ok(())
}

pub(in crate::cli) fn validate_hermes_skill_content(content: &str) -> Result<()> {
    let frontmatter_lines = parse_skill_frontmatter(content)?;

    let name = frontmatter_value(&frontmatter_lines, "name").filter(|value| !value.is_empty());
    if name != Some(HERMES_SKILL_NAME) {
        return Err(anyhow!(
            "skill frontmatter must include `name: {HERMES_SKILL_NAME}`"
        ));
    }

    required_frontmatter_value(&frontmatter_lines, "description")?;
    required_frontmatter_value(&frontmatter_lines, "version")?;

    let platforms = frontmatter_value(&frontmatter_lines, "platforms")
        .ok_or_else(|| anyhow!("skill frontmatter must include `platforms`"))?;
    if !inline_yaml_list_contains(platforms, "macos") {
        return Err(anyhow!(
            "skill frontmatter `platforms` must include `macos`"
        ));
    }

    if !frontmatter_lines
        .iter()
        .any(|line| line.trim() == "metadata:")
    {
        return Err(anyhow!("skill frontmatter must include `metadata`"));
    }
    if !frontmatter_lines
        .iter()
        .any(|line| line.trim() == "hermes:")
    {
        return Err(anyhow!("metadata must include `hermes`"));
    }
    let category = frontmatter_lines
        .iter()
        .find_map(|line| line.trim().strip_prefix("category:").map(str::trim));
    if category != Some("productivity") {
        return Err(anyhow!("metadata.hermes.category must be `productivity`"));
    }

    let tags = frontmatter_lines
        .iter()
        .find_map(|line| line.trim().strip_prefix("tags:").map(str::trim))
        .ok_or_else(|| anyhow!("metadata.hermes.tags must be present"))?;
    for tag in [
        "clipboard",
        "memory",
        "macos",
        "local-first",
        "cli",
        "retrieval",
    ] {
        if !inline_yaml_list_contains(tags, tag) {
            return Err(anyhow!("metadata.hermes.tags must include `{tag}`"));
        }
    }

    validate_required_skill_references(
        content,
        &["references/commands.md", "references/troubleshooting.md"],
    )?;

    Ok(())
}

fn inline_yaml_list_contains(value: &str, expected: &str) -> bool {
    value
        .trim()
        .trim_start_matches('[')
        .trim_end_matches(']')
        .split(',')
        .map(|item| item.trim().trim_matches(['"', '\'']))
        .any(|item| item == expected)
}

pub(in crate::cli) fn render_hermes_doctor_report(report: &HermesDoctorReport) -> String {
    render_agent_doctor_report(
        "Hermes Agent integration target",
        &report.target_dir,
        &report.checks,
    )
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::super::support::referenced_markdown_files;
    use super::{
        inline_yaml_list_contains, packaged_hermes_files, packaged_hermes_skill,
        validate_hermes_skill_content,
    };

    #[test]
    fn packaged_hermes_skill_includes_required_metadata() {
        let content = packaged_hermes_skill();

        validate_hermes_skill_content(&content).expect("packaged Hermes skill should validate");
        assert!(content.contains("platforms: [macos]"));
        assert!(content.contains("metadata:"));
        assert!(content.contains("hermes:"));
        assert!(content.contains("category: productivity"));
        assert!(content.contains("license:"));
        assert!(content.contains("clipmem recall"));
        assert!(content.contains("clipmem timeline"));
        assert!(content.contains("clipmem export"));
        assert!(content.contains("references/commands.md"));
        assert!(content.contains("references/json-schema.md"));
        assert!(content.contains("references/examples.md"));
        assert!(content.contains("references/setup-check.md"));
        assert!(content.contains("references/troubleshooting.md"));
        assert!(content.contains("scripts/check-setup.sh"));
        assert!(content.contains("what was that command I copied?"));
        assert!(content.contains("toon"));
        assert!(content.contains("--cursor"));
        assert!(content.contains("schema_version"));
    }

    #[test]
    fn packaged_hermes_package_embeds_reference_files() {
        let files = packaged_hermes_files();
        assert_eq!(files.len(), 7);
        assert!(files.iter().any(|file| file.relative_path == "SKILL.md"));
        assert!(files
            .iter()
            .any(|file| file.relative_path == "references/commands.md"));
        assert!(files
            .iter()
            .any(|file| file.relative_path == "references/troubleshooting.md"));
        assert!(files
            .iter()
            .any(|file| file.relative_path == "references/json-schema.md"));
        assert!(files
            .iter()
            .any(|file| file.relative_path == "references/examples.md"));
        assert!(files
            .iter()
            .any(|file| file.relative_path == "references/setup-check.md"));
        assert!(files
            .iter()
            .any(|file| file.relative_path == "scripts/check-setup.sh"));
    }

    #[test]
    fn packaged_hermes_skill_references_relative_markdown_files() {
        let references = referenced_markdown_files(&packaged_hermes_skill());
        assert!(references
            .iter()
            .any(|path| path == &PathBuf::from("references/commands.md")));
        assert!(references
            .iter()
            .any(|path| path == &PathBuf::from("references/troubleshooting.md")));
        assert!(references
            .iter()
            .any(|path| path == &PathBuf::from("references/json-schema.md")));
        assert!(references
            .iter()
            .any(|path| path == &PathBuf::from("references/examples.md")));
        assert!(references
            .iter()
            .any(|path| path == &PathBuf::from("references/setup-check.md")));
    }

    #[test]
    fn hermes_skill_validation_rejects_missing_required_frontmatter() {
        let valid = packaged_hermes_skill();

        let missing_name = valid.replacen("name: clipboard-memory\n", "", 1);
        assert!(validate_hermes_skill_content(&missing_name)
            .unwrap_err()
            .to_string()
            .contains("name: clipboard-memory"));

        let wrong_name = valid.replacen("name: clipboard-memory", "name: other", 1);
        assert!(validate_hermes_skill_content(&wrong_name)
            .unwrap_err()
            .to_string()
            .contains("name: clipboard-memory"));

        let missing_description = valid.replacen("description:", "summary:", 1);
        assert!(validate_hermes_skill_content(&missing_description)
            .unwrap_err()
            .to_string()
            .contains("description"));

        let missing_platforms = valid.replacen("platforms: [macos]\n", "", 1);
        assert!(validate_hermes_skill_content(&missing_platforms)
            .unwrap_err()
            .to_string()
            .contains("platforms"));

        let wrong_platforms = valid.replacen("platforms: [macos]", "platforms: [macos-intel]", 1);
        assert!(validate_hermes_skill_content(&wrong_platforms)
            .unwrap_err()
            .to_string()
            .contains("platforms"));

        let missing_metadata = valid.replacen("  hermes:", "  runtime:", 1);
        assert!(validate_hermes_skill_content(&missing_metadata)
            .unwrap_err()
            .to_string()
            .contains("metadata must include `hermes`"));

        let missing_references = valid.replace("references/commands.md", "references/missing.md");
        assert!(validate_hermes_skill_content(&missing_references)
            .unwrap_err()
            .to_string()
            .contains("references/commands.md"));
    }

    #[test]
    fn hermes_skill_validation_requires_exact_inline_list_items() {
        assert!(inline_yaml_list_contains(
            "[clipboard, 'memory', \"macos\"]",
            "memory"
        ));
        assert!(!inline_yaml_list_contains(
            "[clipboard-memory]",
            "clipboard"
        ));

        let valid = packaged_hermes_skill();
        let wrong_tags = valid.replacen(
            "tags: [clipboard, memory, macos, local-first, cli, retrieval]",
            "tags: [clipboard-memory, macos, local-first, cli, retrieval]",
            1,
        );
        assert!(validate_hermes_skill_content(&wrong_tags)
            .unwrap_err()
            .to_string()
            .contains("metadata.hermes.tags must include `clipboard`"));
    }
}