|
| 1 | +// Standalone verification: exercises the full skill discovery + mirror pipeline |
| 2 | +// against the real skills on this machine, without needing opencode or a Cursor |
| 3 | +// API key. Read-only against real skills; writes only to a temp dir. |
| 4 | +// |
| 5 | +// Run: npx tsx scripts/verify-skill-mirror.mjs |
| 6 | +import { mkdtempSync, rmSync, existsSync, readFileSync, readdirSync } from "node:fs"; |
| 7 | +import { tmpdir } from "node:os"; |
| 8 | +import { join } from "node:path"; |
| 9 | +import { homedir } from "node:os"; |
| 10 | + |
| 11 | +const { discoverSkills, resolveSkills, skillSetHash } = await import( |
| 12 | + "../src/plugin/skill-discovery.ts" |
| 13 | +); |
| 14 | +const { writeSkillMirror, removeSkillMirror, buildSkillsCatalogue } = await import( |
| 15 | + "../src/provider/skill-mirror.ts" |
| 16 | +); |
| 17 | + |
| 18 | +// Read skills.paths from the real opencode config. |
| 19 | +const configPath = join(homedir(), ".config", "opencode", "opencode.jsonc"); |
| 20 | +const configRaw = readFileSync(configPath, "utf8"); |
| 21 | +// Strip JSONC comments — but only // that aren't inside strings. |
| 22 | +// Simple approach: remove // comments and /* */ comments, but preserve |
| 23 | +// // inside quoted strings by tracking quote state. |
| 24 | +function stripJsonc(text) { |
| 25 | + let result = ""; |
| 26 | + let inString = false; |
| 27 | + let i = 0; |
| 28 | + while (i < text.length) { |
| 29 | + const ch = text[i]; |
| 30 | + if (inString) { |
| 31 | + result += ch; |
| 32 | + if (ch === "\\" && i + 1 < text.length) { |
| 33 | + result += text[i + 1]; |
| 34 | + i += 2; |
| 35 | + continue; |
| 36 | + } |
| 37 | + if (ch === '"') inString = false; |
| 38 | + i++; |
| 39 | + continue; |
| 40 | + } |
| 41 | + if (ch === '"') { |
| 42 | + inString = true; |
| 43 | + result += ch; |
| 44 | + i++; |
| 45 | + continue; |
| 46 | + } |
| 47 | + if (ch === "/" && text[i + 1] === "/") { |
| 48 | + // Line comment — skip to end of line. |
| 49 | + while (i < text.length && text[i] !== "\n") i++; |
| 50 | + continue; |
| 51 | + } |
| 52 | + if (ch === "/" && text[i + 1] === "*") { |
| 53 | + // Block comment — skip to closing. |
| 54 | + i += 2; |
| 55 | + while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i++; |
| 56 | + i += 2; |
| 57 | + continue; |
| 58 | + } |
| 59 | + result += ch; |
| 60 | + i++; |
| 61 | + } |
| 62 | + return result; |
| 63 | +} |
| 64 | +const config = JSON.parse(stripJsonc(configRaw)); |
| 65 | +const extraPaths = config.skills?.paths ?? []; |
| 66 | + |
| 67 | +console.log("=== Skill Mirror Verification ===\n"); |
| 68 | +console.log(`Config skills.paths: ${extraPaths.length} entries`); |
| 69 | +for (const p of extraPaths) console.log(` - ${p}`); |
| 70 | +console.log(); |
| 71 | + |
| 72 | +// Use the opencode config dir as cwd (where opencode actually runs). |
| 73 | +const cwd = join(homedir(), ".config", "opencode"); |
| 74 | +console.log(`Discovery cwd: ${cwd}`); |
| 75 | + |
| 76 | +// Layer 1: Discover skills (with extraPaths from config). |
| 77 | +const discovered = discoverSkills(cwd, extraPaths); |
| 78 | +console.log(`\nDiscovered: ${discovered.length} skills`); |
| 79 | +for (const s of discovered.sort((a, b) => a.id.localeCompare(b.id))) { |
| 80 | + console.log(` - ${s.id}: ${s.description.slice(0, 80)}${s.description.length > 80 ? "…" : ""}`); |
| 81 | +} |
| 82 | + |
| 83 | +// Layer 2: Filter through permissions. |
| 84 | +const resolved = resolveSkills(cwd, config, undefined); |
| 85 | +console.log(`\nAfter permission filtering: ${resolved.skills.length} permitted, ${resolved.withheld.length} withheld`); |
| 86 | +for (const w of resolved.withheld) { |
| 87 | + console.log(` WITHHELD: ${w.id} — ${w.reason}`); |
| 88 | +} |
| 89 | + |
| 90 | +// Layer 3: Materialise to a temp dir. |
| 91 | +const tmpDir = mkdtempSync(join(tmpdir(), "skill-mirror-verify-")); |
| 92 | +console.log(`\nMirror target: ${tmpDir}`); |
| 93 | +const warnings = []; |
| 94 | +const writeResult = writeSkillMirror(tmpDir, resolved.skills, (msg) => warnings.push(msg)); |
| 95 | +console.log(`Write result: ${writeResult}`); |
| 96 | +if (warnings.length > 0) { |
| 97 | + console.log(`Warnings (${warnings.length}):`); |
| 98 | + for (const w of warnings) console.log(` - ${w}`); |
| 99 | +} |
| 100 | + |
| 101 | +// Layer 4: Verify the mirror on disk. |
| 102 | +const mirrorDir = join(tmpDir, ".cursor", "skills"); |
| 103 | +const mirrored = existsSync(mirrorDir) ? readdirSync(mirrorDir, { withFileTypes: true }) |
| 104 | + .filter((e) => e.isDirectory()) |
| 105 | + .map((e) => e.name) : []; |
| 106 | +console.log(`\nMirrored directories: ${mirrored.length}`); |
| 107 | +for (const id of mirrored.sort()) { |
| 108 | + const skillMd = join(mirrorDir, id, "SKILL.md"); |
| 109 | + const content = readFileSync(skillMd, "utf8"); |
| 110 | + const hasSentinel = content.includes("generated: opencode-cursor"); |
| 111 | + console.log(` ${id}: sentinel=${hasSentinel}`); |
| 112 | +} |
| 113 | + |
| 114 | +// Layer 5: Verify .gitignore. |
| 115 | +const gitignorePath = join(mirrorDir, ".gitignore"); |
| 116 | +if (existsSync(gitignorePath)) { |
| 117 | + const gi = readFileSync(gitignorePath, "utf8"); |
| 118 | + console.log(`\n.gitignore entries: ${gi.split(/\r?\n/).filter(Boolean).length}`); |
| 119 | + console.log(` Contains .gitignore itself: ${gi.includes(".gitignore")}`); |
| 120 | +} |
| 121 | + |
| 122 | +// Layer 6: Verify the catalogue. |
| 123 | +const catalogue = buildSkillsCatalogue(resolved.skills); |
| 124 | +if (catalogue) { |
| 125 | + console.log(`\nCatalogue length: ${catalogue.length} chars`); |
| 126 | + console.log(` Contains <available_skills>: ${catalogue.includes("<available_skills>")}`); |
| 127 | + console.log(` Contains </available_skills>: ${catalogue.includes("</available_skills>")}`); |
| 128 | + const skillIdsInCatalogue = resolved.skills.filter((s) => catalogue.includes(`**${s.id}**`)); |
| 129 | + console.log(` Skills listed in catalogue: ${skillIdsInCatalogue.length} / ${resolved.skills.length}`); |
| 130 | +} |
| 131 | + |
| 132 | +// Layer 7: Verify idempotency. |
| 133 | +const hash1 = skillSetHash(resolved.skills); |
| 134 | +const hash2 = skillSetHash(resolved.skills); |
| 135 | +console.log(`\nIdempotency: hash stable = ${hash1 === hash2}`); |
| 136 | + |
| 137 | +// Layer 8: Verify dispose. |
| 138 | +removeSkillMirror(tmpDir); |
| 139 | +const afterDispose = existsSync(mirrorDir) ? readdirSync(mirrorDir, { withFileTypes: true }) |
| 140 | + .filter((e) => e.isDirectory()) |
| 141 | + .map((e) => e.name) : []; |
| 142 | +console.log(`\nAfter removeSkillMirror: ${afterDispose.length} dirs remain`); |
| 143 | + |
| 144 | +// Cleanup. |
| 145 | +rmSync(tmpDir, { recursive: true, force: true }); |
| 146 | + |
| 147 | +// Summary. |
| 148 | +console.log("\n=== Summary ==="); |
| 149 | +console.log(`Discovered: ${discovered.length}`); |
| 150 | +console.log(`Permitted: ${resolved.skills.length}`); |
| 151 | +console.log(`Withheld: ${resolved.withheld.length}`); |
| 152 | +console.log(`Mirrored: ${mirrored.length}`); |
| 153 | +console.log(`Warnings: ${warnings.length}`); |
| 154 | +console.log(`Dispose clean: ${afterDispose.length === 0}`); |
| 155 | + |
| 156 | +const allPass = discovered.length > 0 |
| 157 | + && resolved.skills.length === discovered.length |
| 158 | + && mirrored.length === resolved.skills.length |
| 159 | + && warnings.length === 0 |
| 160 | + && afterDispose.length === 0; |
| 161 | + |
| 162 | +console.log(`\nResult: ${allPass ? "PASS" : "FAIL"}`); |
| 163 | +if (!allPass) { |
| 164 | + console.log(" Discrepancies:"); |
| 165 | + if (discovered.length === 0) console.log(" - No skills discovered"); |
| 166 | + if (resolved.skills.length !== discovered.length) console.log(` - Permission filtering changed count: ${discovered.length} -> ${resolved.skills.length}`); |
| 167 | + if (mirrored.length !== resolved.skills.length) console.log(` - Mirror count mismatch: expected ${resolved.skills.length}, got ${mirrored.length}`); |
| 168 | + if (warnings.length > 0) console.log(` - ${warnings.length} warnings during mirror`); |
| 169 | + if (afterDispose.length > 0) console.log(` - Dispose left ${afterDispose.length} dirs`); |
| 170 | +} |
| 171 | +process.exit(allPass ? 0 : 1); |
0 commit comments