返回 CodeWhale
catalog_matrix.rs
根目录 / crates / skills / src / catalog_matrix.rs
1 //! Provider-free contract tests for the bundled starter pack (#4698).
2 //!
3 //! Scope, stated precisely so these assertions are not over-read:
4 //!
5 //! * They validate **deterministic Codewhale behavior** — which skills install,
6 //! which parse, which become ambient catalogue entries, which stay explicitly
7 //! loadable, how aliases resolve, and how much prompt budget the catalogue
8 //! costs.
9 //! * They validate **nothing about semantic routing**. Whether a model picks
10 //! `debug` for a stack trace is a live-provider question and is deliberately
11 //! out of scope here (see `docs/LIVE_SMOKE.md` for the opt-in harness).
12 //!
13 //! The expectation table lives in `assets/skills-catalog-matrix.json` and is
14 //! authored, not generated. The bijection assertion means a shipped-catalog
15 //! change cannot land without an explicit fixture update.
16
17 use std::collections::{BTreeSet, HashMap};
18 use std::path::Path;
19
20 use tempfile::TempDir;
21
22 use super::system::{bundled_skill_generation, bundled_skill_names, install_system_skills};
23 use super::{
24 BundledSkillTier, DEFAULT_SKILL_DESCRIPTION_CHARS, MAX_AVAILABLE_SKILLS_CHARS, Skill,
25 SkillInvocation, SkillRegistry, bundled_skill_tier, render_skills_block,
26 };
27
28 const MATRIX_JSON: &str = include_str!("../assets/skills-catalog-matrix.json");
29
30 #[derive(Debug, serde::Deserialize)]
31 struct MatrixFixture {
32 generation: String,
33 skills: Vec<MatrixEntry>,
34 }
35
36 #[derive(Debug, serde::Deserialize)]
37 struct MatrixEntry {
38 name: String,
39 tier: String,
40 invocation: String,
41 #[serde(default)]
42 aliases: Vec<String>,
43 in_model_catalogue: bool,
44 #[serde(default)]
45 shadowed_aliases: Vec<String>,
46 }
47
48 fn fixture() -> MatrixFixture {
49 serde_json::from_str(MATRIX_JSON).expect("skills-catalog-matrix.json must be valid JSON")
50 }
51
52 /// Install the shipped pack into a temp dir and discover it, exactly as a
53 /// fresh user install would. No network, no provider, no ambient home.
54 fn installed_registry() -> (TempDir, SkillRegistry) {
55 let tmp = TempDir::new().expect("temp dir");
56 install_system_skills(tmp.path(), &Default::default()).expect("bundled install");
57 let registry = SkillRegistry::discover(&tmp.path().join(".system"));
58 assert!(
59 registry.warnings().is_empty(),
60 "bundled pack must parse without warnings: {:?}",
61 registry.warnings()
62 );
63 (tmp, registry)
64 }
65
66 fn rendered_catalogue(registry: &SkillRegistry, locale: &str, workspace: &Path) -> String {
67 render_skills_block(registry, locale, workspace).expect("non-empty registry renders a block")
68 }
69
70 /// Canonical names with or without descriptions that appear under
71 /// `### Available skills` (and only there — the warnings section also uses
72 /// `- ` bullets).
73 fn catalogue_entry_names(block: &str) -> Vec<String> {
74 let mut names = Vec::new();
75 let mut inside = false;
76 for line in block.lines() {
77 if line.starts_with("###") {
78 inside = line.trim() == "### Available skills";
79 continue;
80 }
81 if !inside {
82 continue;
83 }
84 if let Some(rest) = line.strip_prefix("- ")
85 && !rest.starts_with("...")
86 {
87 let name = rest.split_once(':').map_or(rest, |(name, _)| name);
88 if !name.contains(char::is_whitespace) {
89 names.push(name.to_string());
90 }
91 }
92 }
93 names
94 }
95
96 fn by_name(registry: &SkillRegistry) -> HashMap<&str, &Skill> {
97 registry
98 .list()
99 .iter()
100 .map(|skill| (skill.name.as_str(), skill))
101 .collect()
102 }
103
104 // ── fixture ↔ bundle bijection ──────────────────────────────────────────────
105
106 #[test]
107 fn fixture_matrix_covers_exactly_the_shipped_bundle() {
108 let fixture = fixture();
109 let fixture_names: BTreeSet<&str> = fixture.skills.iter().map(|e| e.name.as_str()).collect();
110 let shipped_names: BTreeSet<&str> = bundled_skill_names().into_iter().collect();
111
112 assert_eq!(
113 fixture_names, shipped_names,
114 "assets/skills-catalog-matrix.json must be updated whenever the bundled \
115 starter pack changes; left = fixture, right = BUNDLED_SKILLS"
116 );
117 assert_eq!(
118 fixture.skills.len(),
119 fixture_names.len(),
120 "fixture must not list the same skill twice"
121 );
122 assert_eq!(
123 fixture.generation,
124 bundled_skill_generation(),
125 "fixture generation must track BUNDLED_SKILL_VERSION"
126 );
127 }
128
129 #[test]
130 fn fixture_matrix_matches_parsed_frontmatter_for_every_bundled_skill() {
131 let (_tmp, registry) = installed_registry();
132 let skills = by_name(&registry);
133
134 for entry in fixture().skills {
135 let skill = skills
136 .get(entry.name.as_str())
137 .unwrap_or_else(|| panic!("{} must install and parse", entry.name));
138
139 let expected_invocation = match entry.invocation.as_str() {
140 "explicit-only" => SkillInvocation::ExplicitOnly,
141 "model+user" => SkillInvocation::ModelAndUser,
142 other => panic!("{}: unknown fixture invocation {other}", entry.name),
143 };
144 assert_eq!(
145 skill.invocation, expected_invocation,
146 "{} invocation drifted from the fixture",
147 entry.name
148 );
149
150 let actual_aliases: BTreeSet<&str> = skill.aliases.iter().map(String::as_str).collect();
151 let expected_aliases: BTreeSet<&str> = entry.aliases.iter().map(String::as_str).collect();
152 assert_eq!(
153 actual_aliases, expected_aliases,
154 "{} aliases drifted from the fixture",
155 entry.name
156 );
157
158 let expected_tier = match entry.tier.as_str() {
159 "core" => BundledSkillTier::CoreAgentic,
160 "tools" => BundledSkillTier::FormatTooling,
161 other => panic!("{}: unknown fixture tier {other}", entry.name),
162 };
163 assert_eq!(
164 bundled_skill_tier(&entry.name),
165 Some(expected_tier),
166 "{} tier drifted from the fixture",
167 entry.name
168 );
169
170 assert!(
171 !skill.description.trim().is_empty(),
172 "{} must ship a routing description",
173 entry.name
174 );
175 }
176 }
177
178 // ── positive: eligibility and explicit load ─────────────────────────────────
179
180 #[test]
181 fn every_bundled_skill_is_explicitly_loadable_including_explicit_only() {
182 let (_tmp, registry) = installed_registry();
183 for name in bundled_skill_names() {
184 let resolved = registry
185 .get(name)
186 .unwrap_or_else(|| panic!("{name} must resolve by canonical name"));
187 assert_eq!(resolved.name, name);
188 }
189 }
190
191 #[test]
192 fn ambient_catalogue_is_a_progressive_subset_of_eligible_bundled_skills() {
193 let (tmp, registry) = installed_registry();
194 let block = rendered_catalogue(&registry, "en", tmp.path());
195 let rendered: BTreeSet<String> = catalogue_entry_names(&block).into_iter().collect();
196
197 let expected: BTreeSet<String> = fixture()
198 .skills
199 .into_iter()
200 .filter(|entry| entry.in_model_catalogue)
201 .map(|entry| entry.name)
202 .collect();
203
204 assert!(
205 !rendered.is_empty(),
206 "the ambient routing page must not be empty"
207 );
208 assert!(
209 rendered.is_subset(&expected),
210 "the ambient page may contain only model+user bundled skills"
211 );
212 if rendered.len() < expected.len() {
213 assert!(
214 block.contains("`load_skill` with `name=\"list\""),
215 "a truncated ambient page must point to complete discovery"
216 );
217 }
218 }
219
220 // ── negative: non-activation and explicit-only exclusion ────────────────────
221
222 #[test]
223 fn explicit_only_skills_are_absent_from_the_ambient_catalogue() {
224 let (tmp, registry) = installed_registry();
225 let block = rendered_catalogue(&registry, "en", tmp.path());
226
227 for entry in fixture()
228 .skills
229 .into_iter()
230 .filter(|e| !e.in_model_catalogue)
231 {
232 assert!(
233 !catalogue_entry_names(&block).contains(&entry.name),
234 "{} is explicit-only and must not become ambient context",
235 entry.name
236 );
237 assert!(
238 registry.get(&entry.name).is_some(),
239 "{} must still be loadable by explicit name",
240 entry.name
241 );
242 }
243 }
244
245 #[test]
246 fn unrelated_and_unshipped_names_do_not_activate_a_bundled_skill() {
247 let (_tmp, registry) = installed_registry();
248 for name in [
249 "",
250 " ",
251 "imagine",
252 "image-generation",
253 "codereview",
254 "checkwork",
255 "check-work",
256 "verify",
257 "gh-file-issue",
258 "codew-release-qa-sweep",
259 "totally-unknown-skill",
260 ] {
261 assert!(
262 registry.get(name).is_none(),
263 "{name:?} must not resolve to a bundled skill"
264 );
265 }
266
267 // Lookup normalization is punctuation-insensitive by design, so an alias
268 // typed with a space still lands on its canonical skill. Pin that so the
269 // negative cases above stay meaningful rather than accidental.
270 assert_eq!(
271 registry.get("code review").map(|skill| skill.name.as_str()),
272 Some("review")
273 );
274 }
275
276 // ── alias behavior ──────────────────────────────────────────────────────────
277
278 #[test]
279 fn aliases_resolve_to_their_canonical_skill_and_add_no_catalogue_entries() {
280 let (tmp, registry) = installed_registry();
281 let block = rendered_catalogue(&registry, "en", tmp.path());
282 let rendered = catalogue_entry_names(&block);
283
284 for entry in fixture().skills {
285 for alias in &entry.aliases {
286 let resolved = registry
287 .get(alias)
288 .unwrap_or_else(|| panic!("alias {alias} must resolve"));
289 let expected = if entry.shadowed_aliases.contains(alias) {
290 // A canonical bundled name always wins over another skill's
291 // alias, so `docx` resolves to `docx`, never to `documents`.
292 alias.as_str()
293 } else {
294 entry.name.as_str()
295 };
296 assert_eq!(
297 resolved.name, expected,
298 "alias {alias} resolved to the wrong canonical skill"
299 );
300
301 if !entry.shadowed_aliases.contains(alias) {
302 assert!(
303 !rendered.iter().any(|name| name == alias),
304 "alias {alias} must not become a second catalogue entry"
305 );
306 }
307 }
308 }
309 }
310
311 #[test]
312 fn grok_compatibility_aliases_map_to_shipped_codewhale_workflows() {
313 let (_tmp, registry) = installed_registry();
314 for (alias, canonical) in [("code-review", "review"), ("create-skill", "skill-creator")] {
315 let resolved = registry
316 .get(alias)
317 .unwrap_or_else(|| panic!("{alias} must resolve"));
318 assert_eq!(resolved.name, canonical, "{alias} must map to {canonical}");
319 }
320 }
321
322 #[test]
323 fn no_two_bundled_skills_claim_the_same_alias() {
324 let mut owners: HashMap<String, String> = HashMap::new();
325 for entry in fixture().skills {
326 for alias in entry.aliases {
327 if let Some(previous) = owners.insert(alias.clone(), entry.name.clone()) {
328 panic!(
329 "alias {alias} is claimed by both {previous} and {}",
330 entry.name
331 );
332 }
333 }
334 }
335 }
336
337 // ── prompt-budget invariants ────────────────────────────────────────────────
338
339 #[test]
340 fn catalogue_has_unique_entries_and_the_complete_block_fits_the_prompt_budget() {
341 let (tmp, registry) = installed_registry();
342 let block = rendered_catalogue(&registry, "en", tmp.path());
343 let rendered = catalogue_entry_names(&block);
344
345 let unique: BTreeSet<&String> = rendered.iter().collect();
346 assert_eq!(
347 rendered.len(),
348 unique.len(),
349 "metadata must never duplicate a prompt entry: {rendered:?}"
350 );
351
352 assert!(
353 block.chars().count() <= MAX_AVAILABLE_SKILLS_CHARS,
354 "complete ambient block is {} chars, over the {MAX_AVAILABLE_SKILLS_CHARS} budget",
355 block.chars().count()
356 );
357 if block.contains("additional skills omitted") {
358 assert!(
359 block.contains("`load_skill` with `name=\"list\""),
360 "budget overflow must advertise complete on-demand discovery"
361 );
362 }
363 // Budget headroom (docs/SKILLS.md): the shipped pack alone must render
364 // every eligible skill with no omission line, so user skills are never
365 // silently displaced by the bundle.
366 assert!(
367 !block.contains("additional skills omitted"),
368 "shipped pack alone must not truncate the ambient catalogue"
369 );
370
371 // No entry may smuggle newlines or an oversized description into the
372 // prompt prefix — that is how a catalogue line would poison context.
373 for line in block.lines() {
374 assert!(
375 line.chars().count() <= DEFAULT_SKILL_DESCRIPTION_CHARS + 200,
376 "catalogue line is too long for a routing hint: {line}"
377 );
378 }
379 for skill in registry.list() {
380 assert!(
381 !skill.description.contains('\n'),
382 "{} description must stay single-line",
383 skill.name
384 );
385 }
386 }
387
388 // ── locale-aware routing metadata ───────────────────────────────────────────
389
390 #[test]
391 fn every_shipped_locale_falls_back_to_canonical_english_routing_descriptions() {
392 // The bundled pack ships no `description_<tag>` frontmatter. Rather than
393 // fabricate translations, the contract is an explicit, tested fallback:
394 // every shipped locale sees the canonical English routing description.
395 let (_tmp, registry) = installed_registry();
396 for skill in registry.list() {
397 assert!(
398 skill.localized_descriptions.is_empty(),
399 "{} ships localized routing metadata; add source-backed coverage \
400 to this test instead of relying on the English-fallback contract",
401 skill.name
402 );
403 for locale in codewhale_localization::Locale::shipped() {
404 assert_eq!(
405 skill.description_for_locale(locale.tag()),
406 skill.description,
407 "{} must fall back to canonical English for {}",
408 skill.name,
409 locale.tag()
410 );
411 }
412 }
413 }
414
415 #[test]
416 fn rendered_catalogue_is_identical_across_every_shipped_locale() {
417 let (tmp, registry) = installed_registry();
418 let english = rendered_catalogue(&registry, "en", tmp.path());
419 for locale in codewhale_localization::Locale::shipped() {
420 let localized = rendered_catalogue(&registry, locale.tag(), tmp.path());
421 assert_eq!(
422 localized,
423 english,
424 "{} catalogue must match English parity while no localized \
425 routing descriptions are shipped",
426 locale.tag()
427 );
428 }
429 }
430
431 #[test]
432 fn locale_resolution_covers_exact_primary_tag_and_english_fallback() {
433 // Synthetic fixture: exercises the three resolution paths the bundled pack
434 // does not currently reach, for every shipped locale tag.
435 let content = "---\n\
436 name: locale-probe\n\
437 description: English routing description\n\
438 description_ja: 日本語のルーティング説明\n\
439 description_pt: Descrição de roteamento\n\
440 description_zh-hant: 繁體路由說明\n\
441 ---\n\n# body\n";
442 let skill = SkillRegistry::parse_skill(Path::new("SKILL.md"), content).expect("parses");
443
444 // Exact tag match (lowercased key lookup).
445 assert_eq!(
446 skill.description_for_locale("ja"),
447 "日本語のルーティング説明"
448 );
449 assert_eq!(skill.description_for_locale("zh-Hant"), "繁體路由說明");
450 // Primary-subtag fallback: pt-BR → description_pt.
451 assert_eq!(
452 skill.description_for_locale("pt-BR"),
453 "Descrição de roteamento"
454 );
455 // English fallback for shipped locales with no authored variant.
456 for tag in ["en", "ko", "vi", "es-419", "zh-Hans"] {
457 assert_eq!(
458 skill.description_for_locale(tag),
459 "English routing description",
460 "{tag} must fall back to the canonical description"
461 );
462 }
463
464 // Every shipped locale tag must resolve to *some* non-empty description.
465 for locale in codewhale_localization::Locale::shipped() {
466 assert!(
467 !skill.description_for_locale(locale.tag()).is_empty(),
468 "{} must resolve to a non-empty routing description",
469 locale.tag()
470 );
471 }
472 }
473
474 // ── starter-pack boundaries ─────────────────────────────────────────────────
475
476 #[test]
477 fn repository_maintenance_helpers_stay_out_of_the_end_user_starter_pack() {
478 for name in [
479 "gh-file-issue",
480 "gh-compile-issues",
481 "gh-assign-issues",
482 "gh-find-prs",
483 "gh-treasure-hunt",
484 "gh-close-issues",
485 "gh-credit-harvest",
486 "codew-release-qa-sweep",
487 ] {
488 assert!(
489 !bundled_skill_names().contains(&name),
490 "{name} is a maintainer helper and must not ship in the starter pack"
491 );
492 }
493 }
494
495 #[test]
496 fn starter_pack_does_not_advertise_capabilities_the_runtime_lacks() {
497 // `imagine` stays out of scope while no image-generation/edit tool exists.
498 let names = bundled_skill_names();
499 for name in ["imagine", "image", "image-gen"] {
500 assert!(
501 !names.contains(&name),
502 "{name} must not ship without a real image-generation tool"
503 );
504 }
505 }
506
507 #[test]
508 fn help_is_a_bounded_explicit_only_router() {
509 let (_tmp, registry) = installed_registry();
510 let help = registry.get("help").expect("help must be installed");
511 assert_eq!(help.invocation, SkillInvocation::ExplicitOnly);
512 // Bounded: a routing card, not an embedded manual.
513 assert!(
514 help.body.lines().count() < 80,
515 "help must stay a router, not a manual: {} lines",
516 help.body.lines().count()
517 );
518 for surface in ["/help", "/skills", "/config", "doctor"] {
519 assert!(
520 help.body.contains(surface),
521 "help must route to the installed {surface} surface"
522 );
523 }
524 }
525
525 lines RUST