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
// Compatibility tests for `skills/ilo/SKILL.md`.
//
// These guard against regressions that would silently break the skill in
// Claude Code, Claude Desktop, Codex, or any other Agent Skills-compatible
// host. The full reference validator (`skills-ref validate`) is run as a
// separate CI step when available; these tests provide a hard, dependency-free
// regression layer that runs in `cargo test`.
//
// What we assert:
// 1. Frontmatter shape (name/description constraints, allowed-tools is a
// string per spec, no top-level `argument-hint`).
// 2. Body contains no `${CLAUDE_*}` env-var references (those are
// Claude-Code-only and break in Codex / other agents).
// 3. Every `scripts/<name>` path mentioned in the body resolves under
// `skills/ilo/scripts/`.
// 4. Body contains the canonical sections that Claude Code's loader and
// humans rely on — a regression test mirroring what an agent sees.
use std::path::{Path, PathBuf};
fn skill_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("skills/ilo")
}
fn skill_md_text() -> String {
let p = skill_dir().join("SKILL.md");
std::fs::read_to_string(&p).unwrap_or_else(|e| panic!("read {}: {e}", p.display()))
}
/// Split the file into (frontmatter, body). The frontmatter is the YAML
/// between the first two `---` lines; the body is everything after.
fn split_frontmatter(text: &str) -> (&str, &str) {
let rest = text
.strip_prefix("---\n")
.or_else(|| text.strip_prefix("---\r\n"))
.expect("SKILL.md must start with `---` frontmatter delimiter");
let end = rest
.find("\n---\n")
.or_else(|| rest.find("\n---\r\n"))
.expect("SKILL.md must close its frontmatter with a `---` line");
let fm = &rest[..end];
// Skip past `\n---\n` (5 bytes) or `\n---\r\n` (6 bytes).
let body_start = end
+ if rest[end..].starts_with("\n---\r\n") {
6
} else {
5
};
(fm, &rest[body_start..])
}
/// Extract a top-level scalar string value for `key:` from frontmatter.
/// Returns `None` if the key is missing or the value is a block (sequence /
/// mapping). Strips matching surrounding quotes.
fn top_level_scalar(fm: &str, key: &str) -> Option<String> {
let prefix = format!("{key}:");
for line in fm.lines() {
// Top-level keys have no leading whitespace.
if line.starts_with(&prefix) {
let rest = line[prefix.len()..].trim();
if rest.is_empty() {
return None; // block value
}
// Strip surrounding double or single quotes.
let unquoted = if (rest.starts_with('"') && rest.ends_with('"') && rest.len() >= 2)
|| (rest.starts_with('\'') && rest.ends_with('\'') && rest.len() >= 2)
{
&rest[1..rest.len() - 1]
} else {
rest
};
return Some(unquoted.to_string());
}
}
None
}
/// True if a top-level key exists at all (regardless of scalar/block).
fn has_top_level_key(fm: &str, key: &str) -> bool {
let prefix = format!("{key}:");
fm.lines().any(|l| l.starts_with(&prefix))
}
#[test]
fn skill_md_exists() {
let p = skill_dir().join("SKILL.md");
assert!(
p.is_file(),
"skills/ilo/SKILL.md missing at {}",
p.display()
);
}
#[test]
fn frontmatter_name_is_valid() {
let text = skill_md_text();
let (fm, _) = split_frontmatter(&text);
let name = top_level_scalar(fm, "name").expect("frontmatter must have `name`");
assert_eq!(name, "ilo", "skill name must be `ilo`");
assert!(name.len() <= 64, "name must be <= 64 chars");
// Pattern: ^[a-z][a-z0-9-]*[a-z0-9]$
let bytes = name.as_bytes();
assert!(
bytes.len() >= 2,
"name must be at least 2 chars to satisfy spec pattern"
);
assert!(
bytes[0].is_ascii_lowercase(),
"name must start with a lowercase letter"
);
let last = bytes[bytes.len() - 1];
assert!(
last.is_ascii_lowercase() || last.is_ascii_digit(),
"name must end with [a-z0-9]"
);
for &b in bytes {
assert!(
b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-',
"name must match ^[a-z][a-z0-9-]*[a-z0-9]$, got byte {b:#x}"
);
}
}
#[test]
fn frontmatter_description_is_valid() {
let text = skill_md_text();
let (fm, _) = split_frontmatter(&text);
let desc =
top_level_scalar(fm, "description").expect("frontmatter must have a scalar `description`");
assert!(!desc.is_empty(), "description must not be empty");
assert!(
desc.len() <= 1024,
"description must be <= 1024 chars (got {})",
desc.len()
);
}
#[test]
fn allowed_tools_is_string_form() {
// The Agent Skills spec defines `allowed-tools` as a space-separated
// string. Claude Code also accepts a YAML sequence, but Codex and other
// hosts only accept the string form. We pin to the spec.
let text = skill_md_text();
let (fm, _) = split_frontmatter(&text);
if !has_top_level_key(fm, "allowed-tools") {
return; // optional field
}
let val = top_level_scalar(fm, "allowed-tools")
.expect("`allowed-tools`, if present, must be a scalar string (not a YAML sequence)");
assert!(
!val.is_empty(),
"`allowed-tools` must not be the empty string"
);
// Each whitespace-separated token must look like a tool name.
for tok in val.split_whitespace() {
assert!(
tok.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'),
"unexpected token in allowed-tools: {tok:?}"
);
}
}
#[test]
fn no_top_level_argument_hint() {
// `argument-hint` is a Claude-only convenience; per Agent Skills spec it
// belongs under `metadata:`. Codex etc. ignore unknown top-level keys but
// this catches a regression where someone re-adds it at the top level.
let text = skill_md_text();
let (fm, _) = split_frontmatter(&text);
assert!(
!has_top_level_key(fm, "argument-hint"),
"`argument-hint` must live under `metadata:`, not at the top level"
);
}
#[test]
fn body_has_no_claude_env_vars() {
// ${CLAUDE_PROJECT_DIR} and friends are injected only by Claude Code and
// break when the skill runs under Codex or other Agent Skills hosts.
let text = skill_md_text();
let (_fm, body) = split_frontmatter(&text);
// Look for `${CLAUDE_` literally.
if let Some(idx) = body.find("${CLAUDE_") {
let snippet_end = (idx + 60).min(body.len());
panic!(
"SKILL.md body references a Claude-Code-only env var; this breaks portability.\n\
First match: {:?}",
&body[idx..snippet_end]
);
}
}
#[test]
fn referenced_scripts_exist() {
// Any `scripts/<name>` path mentioned in the body must resolve under
// `skills/ilo/scripts/` so other hosts can find them.
let text = skill_md_text();
let (_fm, body) = split_frontmatter(&text);
let scripts_dir = skill_dir().join("scripts");
let mut checked = 0usize;
let mut search_from = 0usize;
while let Some(rel) = body[search_from..].find("scripts/") {
let abs = search_from + rel;
// Walk forward gathering the path component until whitespace, backtick,
// or other delimiter.
let after = &body[abs + "scripts/".len()..];
let end = after
.find(|c: char| {
c.is_whitespace() || c == '`' || c == ')' || c == '"' || c == '\'' || c == ','
})
.unwrap_or(after.len());
let name = &after[..end];
if !name.is_empty() {
let candidate = scripts_dir.join(name);
assert!(
candidate.is_file(),
"SKILL.md references `scripts/{name}` but {} does not exist",
candidate.display()
);
checked += 1;
}
search_from = abs + "scripts/".len() + end;
}
assert!(
checked > 0,
"expected at least one `scripts/<name>` reference in SKILL.md (e.g. ensure-ilo.sh)"
);
}
#[test]
fn body_is_thin_bootstrap() {
// Phase 2 (PR #419): SKILL.md is a thin bootstrap pointer. The rich
// reference content has moved to the modular `ilo-*.md` files served by
// the binary via `ilo skill list/get/path/show`. This test guards the
// bootstrap shape so the file does not silently regrow into a monolith.
let text = skill_md_text();
let (_fm, body) = split_frontmatter(&text);
for required in [
"## Setup",
"## Bootstrap pointer",
"## Available skills",
"ilo skill list",
"ilo-edit-loop",
"ilo-examples",
] {
assert!(
body.contains(required),
"SKILL.md bootstrap missing required marker: {required}"
);
}
// Bootstrap cap: the file must stay short. The old monolith was ~50 KB;
// a healthy bootstrap is well under 5 KB. Trip if it bloats past 20 KB.
assert!(
body.len() < 20_000,
"SKILL.md body is {} bytes; bootstrap shape should stay well under 16 KB",
body.len()
);
}
#[test]
fn ensure_ilo_script_runs_without_claude_env_vars() {
// Portability check for the install script. We run it with a sanitised
// environment that strips any `CLAUDE_*` variables, with the working
// directory at the skill root, exactly as a non-Claude host (Codex etc.)
// would invoke it. The script must either succeed or exit with a clear
// error — it must not silently misbehave because of a missing
// Claude-injected env var.
//
// We run the script in `--check`-equivalent mode by piping it through
// `bash -n` (no-exec syntax check) first to ensure it parses; then we
// execute it in a sanitised env. For determinism in CI we don't require
// the install to actually succeed (network may be flaky), but the
// exit code must be 0 on success or non-zero with a useful message.
let script = skill_dir().join("scripts/ensure-ilo.sh");
assert!(
script.is_file(),
"ensure-ilo.sh missing at {}",
script.display()
);
// Syntax check: must parse under `sh -n`.
let syntax = std::process::Command::new("sh")
.arg("-n")
.arg(&script)
.output()
.expect("invoke sh -n");
assert!(
syntax.status.success(),
"ensure-ilo.sh failed `sh -n` syntax check: {}",
String::from_utf8_lossy(&syntax.stderr)
);
// Sanitised execution: strip CLAUDE_* env vars so we exercise the
// non-Claude-host code path. We don't require success (network may be
// unavailable in sandboxed CI), only that:
// - the process terminates,
// - it does not panic / crash with an interpreter error,
// - if it fails, stderr is non-empty (i.e. it explained itself).
let path = std::env::var("PATH").unwrap_or_default();
let home = std::env::var("HOME").unwrap_or_default();
let out = std::process::Command::new("sh")
.arg(skill_dir().join("scripts/ensure-ilo.sh"))
.env_clear()
.env("PATH", &path)
.env("HOME", &home)
.current_dir(skill_dir())
.output()
.expect("run ensure-ilo.sh");
if !out.status.success() {
let stderr = String::from_utf8_lossy(&out.stderr);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
!stderr.trim().is_empty() || !stdout.trim().is_empty(),
"ensure-ilo.sh failed silently with no output (exit {:?}); a non-Claude host \
would have no idea what went wrong",
out.status.code()
);
}
}
// Helper to keep clippy happy about unused std::path::Path in earlier drafts.
#[allow(dead_code)]
fn _unused(_: &Path) {}