test-skill-registration.mjs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import fs from 'fs';
  2. import path from 'path';
  3. import { pathToFileURL } from 'url';
  4. // Verifies the V2 skill registration payload matches OpenCode 2.0.4's
  5. // Skill.Info contract (packages/schema/src/skill.ts):
  6. // { id, name, description?, autoinvoke?, path, content }
  7. // Upstream commit 199aabe9e2 (first released in v2.0.4) renamed the required
  8. // file field `location` -> `path`. A wrong field name makes draft.add()
  9. // throw inside the host's transform rebuild, which asynchronously disables
  10. // the whole plugin ("Plugin disabled after skill.transform failed") and
  11. // takes the bootstrap hook down with it — see PR #2106 review by 80avin.
  12. const [, , inputPath] = process.argv;
  13. if (!inputPath) {
  14. console.error('Usage: node test-skill-registration.mjs PLUGIN_PATH');
  15. process.exit(2);
  16. }
  17. const pluginPath = fs.realpathSync(inputPath);
  18. const skillsDir = path.resolve(path.dirname(pluginPath), '../../skills');
  19. const mod = await import(pathToFileURL(pluginPath).href);
  20. const failures = [];
  21. // --- Run 1: passive capture of every draft.add payload -------------------
  22. const added = [];
  23. await mod.default.setup(makeCtx({ add: (skill) => added.push(skill) }));
  24. const expectedIds = fs.existsSync(skillsDir)
  25. ? fs.readdirSync(skillsDir, { withFileTypes: true })
  26. .filter((e) => e.isDirectory() && !e.name.startsWith('.'))
  27. .filter((e) => fs.existsSync(path.join(skillsDir, e.name, 'SKILL.md')))
  28. .map((e) => e.name)
  29. .sort()
  30. : [];
  31. if (added.length === 0) {
  32. failures.push('expected setup() to register at least one skill via draft.add()');
  33. }
  34. if (JSON.stringify(added.map((s) => s.id).sort()) !== JSON.stringify(expectedIds)) {
  35. failures.push(`expected draft.add() ids to match skills dir contents, got ${JSON.stringify(added.map((s) => s.id))}`);
  36. }
  37. for (const skill of added) {
  38. if (typeof skill.path !== 'string' || !path.isAbsolute(skill.path)) {
  39. failures.push(`skill "${skill.id}": expected required absolute Skill.Info field "path", got ${JSON.stringify(skill.path)}`);
  40. } else if (skill.path !== path.join(skillsDir, skill.id, 'SKILL.md')) {
  41. failures.push(`skill "${skill.id}": expected path ${path.join(skillsDir, skill.id, 'SKILL.md')}, got ${skill.path}`);
  42. } else if (!fs.existsSync(skill.path)) {
  43. failures.push(`skill "${skill.id}": path does not exist on disk: ${skill.path}`);
  44. }
  45. // Stale 2.0.3-era fields must not leak into the payload: the host strips
  46. // unknown keys, but keeping them would silently mask a future regression
  47. // to a schema that no longer accepts `path`.
  48. if ('location' in skill) {
  49. failures.push(`skill "${skill.id}": payload still carries the pre-2.0.4 field "location"`);
  50. }
  51. if ('slash' in skill) {
  52. failures.push(`skill "${skill.id}": payload carries "slash", removed from Skill.Info in 2.0.4`);
  53. }
  54. if (typeof skill.id !== 'string' || skill.id.length === 0) failures.push(`skill payload missing non-empty "id"`);
  55. if (typeof skill.name !== 'string' || skill.name.length === 0) failures.push(`skill "${skill.id}" missing non-empty "name"`);
  56. if (typeof skill.content !== 'string' || !skill.content.trim()) failures.push(`skill "${skill.id}" missing non-empty "content"`);
  57. if ('description' in skill && typeof skill.description !== 'string') {
  58. failures.push(`skill "${skill.id}": "description" must be a string when present`);
  59. }
  60. }
  61. // --- Run 2: hostile draft.add must not abort the remaining registrations --
  62. // The real host swallows a throw escaping the transform callback and then
  63. // hard-disables the plugin asynchronously. Locally we can only observe the
  64. // synchronous half of that contract: when draft.add() rejects one skill, the
  65. // plugin must keep registering the rest instead of aborting the loop.
  66. const hostileId = added.length > 1 ? added[Math.floor(added.length / 2)].id : null;
  67. const survived = [];
  68. let setupThrew = null;
  69. let survivingContextHook;
  70. try {
  71. await mod.default.setup(makeCtx({
  72. add: (skill) => {
  73. if (skill.id === hostileId) throw new Error('Simulated Skill.Info decode failure');
  74. survived.push(skill.id);
  75. },
  76. onHook: (name, callback) => {
  77. if (name === 'context') survivingContextHook = callback;
  78. },
  79. }));
  80. } catch (err) {
  81. setupThrew = err;
  82. }
  83. if (setupThrew) {
  84. failures.push(`expected setup() to contain draft.add() failures, but it threw: ${setupThrew.message}`);
  85. } else if (hostileId) {
  86. const expectedSurvivors = added.map((s) => s.id).filter((id) => id !== hostileId);
  87. if (JSON.stringify(survived.sort()) !== JSON.stringify(expectedSurvivors.sort())) {
  88. failures.push(`expected all non-rejected skills to still register when one draft.add() throws, got ${JSON.stringify(survived)}`);
  89. }
  90. }
  91. if (typeof survivingContextHook !== 'function') {
  92. failures.push('expected bootstrap hook to survive a rejected skill');
  93. } else {
  94. const event = {
  95. sessionID: 'registration-survival-root',
  96. messages: [{ role: 'user', content: [{ type: 'text', text: 'Continue' }] }],
  97. };
  98. await survivingContextHook(event);
  99. const count = event.messages.flatMap((message) => message.content).filter(
  100. (part) => part.type === 'text' && part.text.startsWith('<EXTREMELY_IMPORTANT>\nYou have superpowers.')
  101. ).length;
  102. if (count !== 1) failures.push(`expected surviving bootstrap once, got ${count}`);
  103. }
  104. const result = {
  105. registered: added.length,
  106. ids: added.map((s) => s.id),
  107. allPathsValid: added.every((s) => s.path === path.join(skillsDir, s.id, 'SKILL.md') && fs.existsSync(s.path)),
  108. staleLocationField: added.some((s) => 'location' in s),
  109. hostileRejectedId: hostileId,
  110. survivedHostileAdd: JSON.stringify(survived.sort()) === JSON.stringify(added.map((s) => s.id).filter((id) => id !== hostileId).sort()),
  111. };
  112. if (failures.length > 0) {
  113. console.error(JSON.stringify(result, null, 2));
  114. for (const failure of failures) {
  115. console.error(`FAIL: ${failure}`);
  116. }
  117. process.exit(1);
  118. }
  119. console.log(JSON.stringify(result, null, 2));
  120. function makeCtx({ add, onHook = () => {} }) {
  121. return {
  122. skill: {
  123. transform: async (fn) => {
  124. await fn({ list: () => [], get: () => undefined, add, update: () => {}, remove: () => {} });
  125. },
  126. },
  127. session: {
  128. hook: async (name, callback) => onHook(name, callback),
  129. get: async ({ sessionID }) => ({ id: sessionID }), // top-level: no parentID
  130. },
  131. };
  132. }