|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The published skills' eval cases, read from a skills root: the workspace's |
| 5 | + * `skills/` tree, or the `dist/skills/` of a packed `@objectstack/skills` |
| 6 | + * version. Each bundle is one directory holding a `SKILL.md`; its cases are the |
| 7 | + * `evals/*.json` files beside it, in the shape every one of them already uses: |
| 8 | + * |
| 9 | + * { "skill_name": BUNDLE, "evals": [ { "id", "prompt", "expected_output", |
| 10 | + * "files": [], "assertions": { "must_contain": [..], "must_not_contain": [..] } } ] } |
| 11 | + * |
| 12 | + * The reader is strict, never lenient: a file that does not parse, a case |
| 13 | + * whose prompt is not a string, an assertion list that is not a list of |
| 14 | + * strings, or a case with no assertion at all is a located refusal — never a |
| 15 | + * case graded as a pass because nothing was asked of it. The cases themselves |
| 16 | + * are not this runner's to edit. |
| 17 | + */ |
| 18 | + |
| 19 | +import { createHash } from 'node:crypto'; |
| 20 | +import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'; |
| 21 | +import { join } from 'node:path'; |
| 22 | + |
| 23 | +/** One eval case, located. */ |
| 24 | +export interface SkillEvalCase { |
| 25 | + /** The bundle directory name (`objectstack-query`). */ |
| 26 | + bundle: string; |
| 27 | + /** The eval file's name inside `evals/` (`filters-pagination-search.json`). */ |
| 28 | + file: string; |
| 29 | + /** The case's own `id` inside its file. */ |
| 30 | + id: number | string; |
| 31 | + /** `BUNDLE/FILE_STEM#ID` — unique across a catalog. */ |
| 32 | + key: string; |
| 33 | + prompt: string; |
| 34 | + expectedOutput?: string; |
| 35 | + mustContain: string[]; |
| 36 | + mustNotContain: string[]; |
| 37 | +} |
| 38 | + |
| 39 | +/** A file or case the reader refused, with where. */ |
| 40 | +export interface SkillEvalRefusal { |
| 41 | + path: string; |
| 42 | + message: string; |
| 43 | +} |
| 44 | + |
| 45 | +export interface SkillEvalCatalog { |
| 46 | + root: string; |
| 47 | + /** Every bundle directory under the root (a `SKILL.md` beside it), sorted. */ |
| 48 | + bundles: string[]; |
| 49 | + /** |
| 50 | + * Bundles whose `SKILL.md` frontmatter says `user-invocable: false`. Claude |
| 51 | + * Code leaves such a skill out of the init line's `skills` list while the |
| 52 | + * model can still launch it (measured on 2.1.326: `objectui`, absent from |
| 53 | + * every with-skill init line of a round, launched by 33 of its 34 turns), so |
| 54 | + * the init line cannot show that it was installed. |
| 55 | + */ |
| 56 | + unlistedBundles: string[]; |
| 57 | + cases: SkillEvalCase[]; |
| 58 | + refusals: SkillEvalRefusal[]; |
| 59 | +} |
| 60 | + |
| 61 | +const isStringList = (v: unknown): v is string[] => Array.isArray(v) && v.every((x) => typeof x === 'string'); |
| 62 | + |
| 63 | +/** `BUNDLE/FILE_STEM#ID`. */ |
| 64 | +export const caseKey = (bundle: string, file: string, id: number | string): string => `${bundle}/${file.replace(/\.json$/, '')}#${id}`; |
| 65 | + |
| 66 | +/** |
| 67 | + * The case as a turn meets it: a hash of the prompt the turn is asked and the |
| 68 | + * two token lists its answer is graded on (`expected_output` is neither sent |
| 69 | + * nor graded, so it is left out). Every run record carries it, so `--regrade` |
| 70 | + * can tell a case reworded since the round from the case that ran. Pure. |
| 71 | + */ |
| 72 | +export const hashCase = (c: Pick<SkillEvalCase, 'prompt' | 'mustContain' | 'mustNotContain'>): string => |
| 73 | + `sha256:${createHash('sha256').update(JSON.stringify([c.prompt, c.mustContain, c.mustNotContain])).digest('hex')}`; |
| 74 | + |
| 75 | +/** Read one eval file's text into cases, or refusals. Pure over its inputs. */ |
| 76 | +export function parseEvalFile(bundle: string, file: string, text: string): { cases: SkillEvalCase[]; refusals: SkillEvalRefusal[] } { |
| 77 | + const path = `${bundle}/evals/${file}`; |
| 78 | + const cases: SkillEvalCase[] = []; |
| 79 | + const refusals: SkillEvalRefusal[] = []; |
| 80 | + let doc: unknown; |
| 81 | + try { |
| 82 | + doc = JSON.parse(text); |
| 83 | + } catch (e) { |
| 84 | + return { cases, refusals: [{ path, message: `does not parse: ${e instanceof Error ? e.message : String(e)}` }] }; |
| 85 | + } |
| 86 | + const evals = typeof doc === 'object' && doc !== null && !Array.isArray(doc) ? (doc as Record<string, unknown>).evals : undefined; |
| 87 | + if (!Array.isArray(evals)) return { cases, refusals: [{ path, message: 'has no `evals` list' }] }; |
| 88 | + const seen = new Set<string>(); |
| 89 | + evals.forEach((raw, i) => { |
| 90 | + const at = `${path}: evals[${i}]`; |
| 91 | + if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) { |
| 92 | + refusals.push({ path: at, message: 'is not an object' }); |
| 93 | + return; |
| 94 | + } |
| 95 | + const e = raw as Record<string, unknown>; |
| 96 | + const id = e.id; |
| 97 | + if (typeof id !== 'number' && typeof id !== 'string') { |
| 98 | + refusals.push({ path: at, message: '`id` is neither a number nor a string' }); |
| 99 | + return; |
| 100 | + } |
| 101 | + if (typeof e.prompt !== 'string' || e.prompt.trim() === '') { |
| 102 | + refusals.push({ path: at, message: '`prompt` is not a non-empty string' }); |
| 103 | + return; |
| 104 | + } |
| 105 | + const a = e.assertions; |
| 106 | + if (typeof a !== 'object' || a === null || Array.isArray(a)) { |
| 107 | + refusals.push({ path: at, message: '`assertions` is not an object' }); |
| 108 | + return; |
| 109 | + } |
| 110 | + const { must_contain: mc = [], must_not_contain: mnc = [] } = a as Record<string, unknown>; |
| 111 | + if (!isStringList(mc) || !isStringList(mnc)) { |
| 112 | + refusals.push({ path: at, message: '`assertions.must_contain` / `must_not_contain` must each be a list of strings' }); |
| 113 | + return; |
| 114 | + } |
| 115 | + if (mc.length + mnc.length === 0) { |
| 116 | + refusals.push({ path: at, message: 'asserts nothing: a case with no `must_contain` and no `must_not_contain` would pass every answer' }); |
| 117 | + return; |
| 118 | + } |
| 119 | + const key = caseKey(bundle, file, id); |
| 120 | + if (seen.has(key)) { |
| 121 | + refusals.push({ path: at, message: `repeats the id \`${String(id)}\`` }); |
| 122 | + return; |
| 123 | + } |
| 124 | + seen.add(key); |
| 125 | + cases.push({ |
| 126 | + bundle, |
| 127 | + file, |
| 128 | + id, |
| 129 | + key, |
| 130 | + prompt: e.prompt, |
| 131 | + ...(typeof e.expected_output === 'string' ? { expectedOutput: e.expected_output } : {}), |
| 132 | + mustContain: mc, |
| 133 | + mustNotContain: mnc, |
| 134 | + }); |
| 135 | + }); |
| 136 | + return { cases, refusals }; |
| 137 | +} |
| 138 | + |
| 139 | +/** Every bundle under `root` and every case its `evals/*.json` hold. */ |
| 140 | +export function loadSkillEvalCatalog(root: string): SkillEvalCatalog { |
| 141 | + if (!existsSync(root) || !statSync(root).isDirectory()) { |
| 142 | + return { root, bundles: [], unlistedBundles: [], cases: [], refusals: [{ path: root, message: 'is not a directory of skill bundles' }] }; |
| 143 | + } |
| 144 | + const bundles = readdirSync(root, { withFileTypes: true }) |
| 145 | + .filter((d) => d.isDirectory() && existsSync(join(root, d.name, 'SKILL.md'))) |
| 146 | + .map((d) => d.name) |
| 147 | + .sort(); |
| 148 | + const cases: SkillEvalCase[] = []; |
| 149 | + const refusals: SkillEvalRefusal[] = []; |
| 150 | + for (const bundle of bundles) { |
| 151 | + const dir = join(root, bundle, 'evals'); |
| 152 | + if (!existsSync(dir)) continue; |
| 153 | + const files = readdirSync(dir, { withFileTypes: true }) |
| 154 | + .filter((d) => d.isFile() && d.name.endsWith('.json')) |
| 155 | + .map((d) => d.name) |
| 156 | + .sort(); |
| 157 | + for (const file of files) { |
| 158 | + const r = parseEvalFile(bundle, file, readFileSync(join(dir, file), 'utf8')); |
| 159 | + cases.push(...r.cases); |
| 160 | + refusals.push(...r.refusals); |
| 161 | + } |
| 162 | + } |
| 163 | + const unlistedBundles = bundles.filter((b) => notUserInvocable(readFileSync(join(root, b, 'SKILL.md'), 'utf8'))); |
| 164 | + return { root, bundles, unlistedBundles, cases, refusals }; |
| 165 | +} |
| 166 | + |
| 167 | +/** The `SKILL.md` frontmatter sets `user-invocable: false`. Pure over its input. */ |
| 168 | +export function notUserInvocable(skillMd: string): boolean { |
| 169 | + const fm = /^---\r?\n([\s\S]*?)\r?\n---/.exec(skillMd); |
| 170 | + return fm !== null && /^user-invocable:\s*false\s*$/m.test(fm[1]!); |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * Narrow a catalog to the named bundles (`--skills a,b`). A name that is not a |
| 175 | + * bundle of the catalog is refused, never dropped: a misspelt touched skill |
| 176 | + * would otherwise run nothing and read as a clean round. |
| 177 | + */ |
| 178 | +export function selectBundles(catalog: SkillEvalCatalog, names: readonly string[] | undefined): SkillEvalCase[] { |
| 179 | + if (!names || names.length === 0) return catalog.cases; |
| 180 | + const unknown = names.filter((n) => !catalog.bundles.includes(n)); |
| 181 | + if (unknown.length) throw new Error(`--skills names ${unknown.map((n) => `\`${n}\``).join(', ')}, not a bundle under ${catalog.root} (bundles: ${catalog.bundles.join(', ')})`); |
| 182 | + const noCases = names.filter((n) => !catalog.cases.some((c) => c.bundle === n)); |
| 183 | + if (noCases.length) throw new Error(`--skills names ${noCases.map((n) => `\`${n}\``).join(', ')}, a bundle with no eval case under ${catalog.root}`); |
| 184 | + const set = new Set(names); |
| 185 | + return catalog.cases.filter((c) => set.has(c.bundle)); |
| 186 | +} |
0 commit comments