custom-skills.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import fs from 'node:fs'
  2. import os from 'node:os'
  3. import path from 'node:path'
  4. import pino from 'pino'
  5. import { strToU8, unzipSync, zipSync } from 'fflate'
  6. import { afterAll, beforeAll, describe, expect, it } from 'vitest'
  7. import { createApp, type App } from '../src/app'
  8. import { config, customSkillsDir, customSkillStoreDir } from '../src/config'
  9. import { LIMITS, normalizeRelPath } from '../src/custom-skills/store'
  10. import { resolveSkills } from '../src/providers/claude/skills'
  11. import { skillsToDisable } from '../src/providers/codex/skills'
  12. import { checkAccess, resolveAccess } from '../src/services/access-policy'
  13. let app: App
  14. const audits: string[] = []
  15. const live = (name: string, f = 'SKILL.md') => path.join(customSkillsDir(), 'skills', name, f)
  16. const md = (name: string, desc = `${name} 的说明:处理某类业务数据的流程与规范`) => `---\nname: ${name}\ndescription: ${desc}\n---\n# ${name}\n`
  17. /** 一个插件格式的技能源目录:skills/alpha、skills/beta(含指向 /etc/passwd 的符号链接) */
  18. function makeSourceDir(): string {
  19. const root = fs.mkdtempSync(path.join(os.tmpdir(), 'skill-src-'))
  20. for (const n of ['alpha', 'beta']) {
  21. fs.mkdirSync(path.join(root, 'skills', n, 'references'), { recursive: true })
  22. fs.writeFileSync(path.join(root, 'skills', n, 'SKILL.md'), md(n))
  23. fs.writeFileSync(path.join(root, 'skills', n, 'references', 'guide.md'), `${n} guide v1\nline2\n`)
  24. }
  25. fs.symlinkSync('/etc/passwd', path.join(root, 'skills', 'beta', 'passwd'))
  26. return root
  27. }
  28. beforeAll(() => {
  29. app = createApp(pino({ level: 'silent' }), { providers: {} })
  30. const orig = app.audit.log.bind(app.audit)
  31. app.audit.log = (e: Parameters<typeof orig>[0]) => { audits.push(e.summary); return orig(e) }
  32. })
  33. afterAll(() => app.skillSources.stop())
  34. describe('自定义技能:目录技能源导入与同步', () => {
  35. let srcDir: string
  36. let sourceId: string
  37. it('C-01 扫描出 2 个技能并导入;两个引擎的 ID 为 custom-skills:<name>;符号链接被跳过(C-03)', () => {
  38. srcDir = makeSourceDir()
  39. const s = app.skillSources.create({ name: '业务仓库', description: '', path: srcDir, syncMode: 'manual', intervalMin: 10 })
  40. sourceId = s.id
  41. const scan = app.skillSources.scan(sourceId)
  42. expect(scan.skills.map(x => [x.name, x.status, x.path, x.fileCount])).toEqual([['alpha', 'new', 'skills/alpha', 2], ['beta', 'new', 'skills/beta', 2]])
  43. expect(scan.skipped).toEqual(['skills/beta/passwd(符号链接)'])
  44. const r = app.skillSources.import(sourceId, ['alpha', 'beta'], scan.digest)
  45. expect(r.imported.map(x => [x.name, x.version, x.created])).toEqual([['alpha', 1, true], ['beta', 1, true]])
  46. expect(fs.readFileSync(live('alpha'), 'utf8')).toContain('name: alpha')
  47. expect(fs.existsSync(live('beta', 'passwd'))).toBe(false)
  48. expect(app.customSkills.list().map(x => [x.name, x.origin, x.publishedVersion, x.sourceName])).toEqual([['alpha', 'imported', 1, '业务仓库'], ['beta', 'imported', 1, '业务仓库']])
  49. expect(app.skillSources.list()[0]!.skillNames).toEqual(['alpha', 'beta'])
  50. expect(app.customSkills.pluginRef()!.skills.map(s => [s.id, s.optIn])).toEqual([['custom-skills:alpha', true], ['custom-skills:beta', true]])
  51. })
  52. it('C-02 修改来源中的一个参考文件:同步只显示该文件的变化,导入后生效目录更新', () => {
  53. fs.writeFileSync(path.join(srcDir, 'skills', 'alpha', 'references', 'guide.md'), 'alpha guide v2\nline2\n')
  54. const scan = app.skillSources.scan(sourceId)
  55. const alpha = scan.skills.find(s => s.name === 'alpha')!
  56. expect(alpha.status).toBe('changed')
  57. expect(alpha.changes).toEqual({ added: [], modified: ['references/guide.md'], removed: [] })
  58. expect(scan.skills.find(s => s.name === 'beta')!.status).toBe('unchanged')
  59. // 扫描后来源又变了:拒绝导入未审阅的内容
  60. fs.writeFileSync(path.join(srcDir, 'skills', 'alpha', 'extra.md'), 'x')
  61. expect(() => app.skillSources.import(sourceId, ['alpha'], scan.digest)).toThrow(/重新扫描/)
  62. fs.rmSync(path.join(srcDir, 'skills', 'alpha', 'extra.md'))
  63. const again = app.skillSources.scan(sourceId)
  64. expect(app.skillSources.import(sourceId, ['alpha'], again.digest).imported).toEqual([{ name: 'alpha', version: 2, created: false }])
  65. expect(fs.readFileSync(live('alpha', 'references/guide.md'), 'utf8')).toBe('alpha guide v2\nline2\n')
  66. const id = app.customSkills.list().find(s => s.name === 'alpha')!.id
  67. expect(app.customSkills.diff(id, 1, 2)[0]).toMatchObject({ path: 'references/guide.md', status: 'modified' })
  68. expect(app.customSkills.diff(id, 1, 2)[0]!.patch).toContain('-alpha guide v1')
  69. })
  70. it('来源中删除的技能只提示,不自动删除;同名冲突拒绝;导入的技能只读,转为可编辑后可改', () => {
  71. fs.rmSync(path.join(srcDir, 'skills', 'beta'), { recursive: true })
  72. expect(app.skillSources.scan(sourceId).missing).toEqual(['beta'])
  73. expect(fs.existsSync(live('beta'))).toBe(true)
  74. const other = fs.mkdtempSync(path.join(os.tmpdir(), 'skill-src2-'))
  75. fs.mkdirSync(path.join(other, 'alpha'))
  76. fs.writeFileSync(path.join(other, 'alpha', 'SKILL.md'), md('alpha'))
  77. const s2 = app.skillSources.create({ name: '另一个来源', description: '', path: other, syncMode: 'manual', intervalMin: 10 })
  78. expect(app.skillSources.scan(s2.id).skills[0]).toMatchObject({ name: 'alpha', status: 'conflict' })
  79. const alphaId = app.customSkills.list().find(s => s.name === 'alpha')!.id
  80. expect(() => app.customSkills.writeFile(alphaId, 'x.md', Buffer.from('x'))).toThrow(/只读/)
  81. expect(app.customSkills.detach(alphaId).origin).toBe('authored')
  82. })
  83. it('自动同步:到期后导入有变化的技能并记入审计', async () => {
  84. const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'skill-auto-'))
  85. fs.writeFileSync(path.join(dir, 'SKILL.md'), md('gamma'))
  86. const s = app.skillSources.create({ name: '自动', description: '', path: dir, syncMode: 'auto', intervalMin: 1 })
  87. await app.skillSources.runAutoSyncNow()
  88. expect(app.customSkills.list().find(x => x.name === 'gamma')).toMatchObject({ publishedVersion: 1, sourceId: s.id })
  89. expect(audits.some(a => a.startsWith('自动同步技能源 自动'))).toBe(true)
  90. })
  91. it('技能源路径校验:不能使用服务数据目录、不存在的 home 路径给出提示', () => {
  92. expect(() => app.skillSources.create({ name: 'x', description: '', path: config.dataDir, syncMode: 'manual', intervalMin: 10 })).toThrow(/服务数据目录/)
  93. expect(() => app.skillSources.create({ name: 'x', description: '', path: '/home/nobody/none', syncMode: 'manual', intervalMin: 10 })).toThrow(/ProtectHome/)
  94. })
  95. })
  96. describe('自定义技能:zip 上传', () => {
  97. it('上传 zip(带顶层目录)导入;不安全路径跳过并审计(C-03)', () => {
  98. const zip = zipSync({
  99. 'pack/delta/SKILL.md': strToU8(md('delta')),
  100. 'pack/delta/templates/report.pptx': new Uint8Array([80, 75, 3, 4, 1, 2, 3]),
  101. '../evil.md': strToU8('x'),
  102. '__MACOSX/pack/._delta': strToU8('x'),
  103. })
  104. const { source, scan } = app.skillSources.upload('pack.zip', Buffer.from(zip))
  105. expect(source.kind).toBe('upload')
  106. expect(scan.skills.map(s => [s.name, s.path, s.fileCount])).toEqual([['delta', 'pack/delta', 2]])
  107. expect(scan.skipped).toEqual(['../evil.md(路径不安全)'])
  108. expect(audits.some(a => a.includes('上传技能包 pack.zip') && a.includes('跳过 1 个'))).toBe(true)
  109. app.skillSources.import(source.id, ['delta'], scan.digest)
  110. expect([...fs.readFileSync(live('delta', 'templates/report.pptx'))]).toEqual([80, 75, 3, 4, 1, 2, 3])
  111. })
  112. it('按实际解压字节数限制大小(防压缩炸弹)', () => {
  113. const saved = LIMITS.unzipBytes
  114. LIMITS.unzipBytes = 1048576
  115. try {
  116. const bomb = zipSync({ 'big/SKILL.md': new Uint8Array(3 * 1048576) }, { level: 9 })
  117. expect(bomb.length).toBeLessThan(100_000)
  118. expect(() => app.skillSources.upload('bomb.zip', Buffer.from(bomb))).toThrow(/解压后超过/)
  119. } finally {
  120. LIMITS.unzipBytes = saved
  121. }
  122. expect(() => app.skillSources.upload('bad.zip', Buffer.from('not a zip'))).toThrow()
  123. })
  124. })
  125. describe('自定义技能:在线编写、草稿与发布、版本', () => {
  126. let id: string
  127. it('C-04 新建 → 上传模版并保存为草稿:不生效;发布后生效;恢复到上一版本', () => {
  128. const d = app.customSkills.create({ name: 'report-kit', description: '生成月度报告的规范与模版,涉及月报时使用' })
  129. id = d.id
  130. expect(d.versions.map(v => [v.version, v.reason])).toEqual([[1, 'create']])
  131. expect(d.publishedVersion).toBeNull()
  132. expect(fs.existsSync(live('report-kit'))).toBe(false)
  133. app.customSkills.writeFile(id, 'templates/a.pptx', Buffer.from([1, 2, 3]))
  134. expect(app.customSkills.detail(id).hasUnsavedChanges).toBe(true)
  135. const v2 = app.customSkills.save(id, { baseVersion: 1, publish: false })
  136. expect([v2.latestVersion, v2.publishedVersion, v2.hasUnsavedChanges]).toEqual([2, null, false])
  137. expect(fs.existsSync(live('report-kit'))).toBe(false)
  138. // 内容无变化不产生新版本;基于旧版本保存时冲突
  139. expect(app.customSkills.save(id, { baseVersion: 2, publish: false }).latestVersion).toBe(2)
  140. expect(() => app.customSkills.save(id, { baseVersion: 1, publish: false })).toThrow(/刷新/)
  141. expect(app.customSkills.publish(id, 2).publishedVersion).toBe(2)
  142. expect([...fs.readFileSync(live('report-kit', 'templates/a.pptx'))]).toEqual([1, 2, 3])
  143. app.customSkills.writeFile(id, 'templates/a.pptx', Buffer.from([9]))
  144. app.customSkills.writeFile(id, 'SKILL.md', Buffer.from(md('report-kit', '新版描述:生成月度报告的规范与模版,涉及月报时使用')))
  145. const v3 = app.customSkills.save(id, { baseVersion: 2, publish: true })
  146. expect([v3.latestVersion, v3.publishedVersion, v3.description]).toEqual([3, 3, '新版描述:生成月度报告的规范与模版,涉及月报时使用'])
  147. expect(v3.versions[0]!.changed).toEqual({ added: [], modified: ['SKILL.md', 'templates/a.pptx'], removed: [] })
  148. const v4 = app.customSkills.restore(id, 2)
  149. expect([v4.latestVersion, v4.publishedVersion, v4.versions[0]!.reason]).toEqual([4, 4, 'restore'])
  150. expect([...fs.readFileSync(live('report-kit', 'templates/a.pptx'))]).toEqual([1, 2, 3])
  151. })
  152. it('SKILL.md 校验、路径安全、重命名与删除、放弃改动、导出 zip', () => {
  153. app.customSkills.writeFile(id, 'SKILL.md', Buffer.from('---\nname: other\ndescription: x\n---\n'))
  154. expect(() => app.customSkills.save(id, { baseVersion: 4, publish: false })).toThrow(/不一致/)
  155. app.customSkills.discard(id)
  156. expect(() => app.customSkills.writeFile(id, '../escape.md', Buffer.from('x'))).toThrow(/路径不正确/)
  157. expect(() => normalizeRelPath('/abs')).toThrow()
  158. expect(() => app.customSkills.deleteFile(id, 'SKILL.md')).toThrow(/不能删除/)
  159. app.customSkills.writeFile(id, 'refs/a.md', Buffer.from('a'))
  160. expect(app.customSkills.move(id, 'refs', 'references').files.map(f => f.path)).toEqual(['SKILL.md', 'references/a.md', 'templates/a.pptx'])
  161. expect(app.customSkills.deleteFile(id, 'references').files.map(f => f.path)).toEqual(['SKILL.md', 'templates/a.pptx'])
  162. const z = app.customSkills.exportZip(id)
  163. expect(z.name).toBe('report-kit-v4.zip')
  164. expect(Object.keys(unzipSync(new Uint8Array(z.data))).sort()).toEqual(['report-kit/SKILL.md', 'report-kit/templates/a.pptx'])
  165. })
  166. })
  167. describe('自定义技能:使用、沙箱与恢复', () => {
  168. it('C-05 "全部技能"不包含自定义技能(可逐个设置包含);单独勾选后加载', () => {
  169. const plugins = [
  170. { name: 'agent-skills', path: '/a', skills: [{ id: 'agent-skills:r', loadId: 'agent-skills:r' }] },
  171. app.customSkills.pluginRef()!,
  172. ]
  173. expect(resolveSkills('all', plugins).skills).toEqual(['agent-skills:r'])
  174. expect(resolveSkills(['custom-skills:report-kit'], plugins).skills).toEqual(['custom-skills:report-kit'])
  175. const id = app.customSkills.list().find(s => s.name === 'report-kit')!.id
  176. app.customSkills.setFlags(id, { includeInAll: true })
  177. expect(resolveSkills('all', [plugins[0]!, app.customSkills.pluginRef()!]).skills).toContain('custom-skills:report-kit')
  178. expect(app.customSkills.excludedFromAll()).not.toContain('custom-skills:report-kit')
  179. // Codex:all 时禁用需要单独勾选的自定义技能
  180. const all = [
  181. { name: 'custom-skills:alpha', path: path.join(customSkillsDir(), 'skills/alpha/SKILL.md') },
  182. { name: 'custom-skills:Report Kit', path: path.join(customSkillsDir(), 'skills/report-kit/SKILL.md') },
  183. ].map(s => ({ ...s, scope: 'user' as const, description: '', enabled: true, pluginId: null }))
  184. expect(skillsToDisable(all, 'all', '/nohome', 'agent-skills', false, app.customSkills.excludedFromAll()).map(d => path.basename(path.dirname(d.path)))).toEqual(['alpha'])
  185. })
  186. it('停用后不加载,启用后恢复;指纹随之变化', () => {
  187. const id = app.customSkills.list().find(s => s.name === 'alpha')!.id
  188. const before = app.customSkills.fingerprint()
  189. app.customSkills.setFlags(id, { enabled: false })
  190. expect(fs.existsSync(live('alpha'))).toBe(false)
  191. expect(app.customSkills.skillInfos().map(s => s.name)).not.toContain('alpha')
  192. expect(app.customSkills.fingerprint()).not.toBe(before)
  193. app.customSkills.setFlags(id, { enabled: true })
  194. expect(fs.existsSync(live('alpha'))).toBe(true)
  195. })
  196. it('C-06 沙箱:生效目录只读可见,不能写;版本库不可读', () => {
  197. const settings = app.settings.get()
  198. const ws = path.join(config.dataDir, 'workspaces', 'ses_cs')
  199. fs.mkdirSync(ws, { recursive: true })
  200. const access = resolveAccess({ policy: { mode: 'restricted', read: [], write: [], deny: [] }, knowledgeRoots: [], selectedKnowledgeRootIds: [], workspaceDir: ws, settings })
  201. expect(checkAccess(access, live('alpha'), 'read').allowed).toBe(true)
  202. expect(checkAccess(access, live('alpha'), 'write').allowed).toBe(false)
  203. expect(checkAccess(access, path.join(customSkillStoreDir(), 'ab', 'x'), 'read').allowed).toBe(false)
  204. const broad = resolveAccess({ policy: { mode: 'broad', read: [], write: [], deny: [] }, knowledgeRoots: [], selectedKnowledgeRootIds: [], workspaceDir: ws, settings })
  205. expect(checkAccess(broad, path.join(customSkillStoreDir(), 'ab', 'x'), 'read').allowed).toBe(false)
  206. expect(checkAccess(broad, live('alpha'), 'read').allowed).toBe(true)
  207. })
  208. it('C-07 删除被两个 Profile 使用的技能:从两个 Profile 中移除', () => {
  209. const { id: _i, version: _v, updatedAt: _u, builtin: _b, ...input } = app.profiles.getBuiltin()
  210. const p1 = app.profiles.create({ ...input, name: 'p1', isDefault: false, skills: ['custom-skills:gamma', 'agent-skills:r'] })
  211. const p2 = app.profiles.create({ ...input, name: 'p2', isDefault: false, skills: ['custom-skills:gamma'] })
  212. const gamma = app.customSkills.list().find(s => s.name === 'gamma')!
  213. expect(gamma.profileNames.sort()).toEqual(['p1', 'p2'])
  214. app.customSkills.delete(gamma.id)
  215. expect(app.profiles.get(p1.id).skills).toEqual(['agent-skills:r'])
  216. expect(app.profiles.get(p2.id).skills).toEqual([])
  217. expect(fs.existsSync(live('gamma'))).toBe(false)
  218. })
  219. it('C-08 重启时按数据库重建:被删除或改动的生效目录恢复,多余目录移除', () => {
  220. fs.rmSync(path.join(customSkillsDir(), 'skills', 'alpha'), { recursive: true })
  221. fs.writeFileSync(live('report-kit'), 'tampered')
  222. fs.mkdirSync(path.join(customSkillsDir(), 'skills', 'stray'))
  223. const logs: string[] = []
  224. app.customSkills.reconcile(m => logs.push(m))
  225. expect(fs.readFileSync(live('alpha'), 'utf8')).toContain('name: alpha')
  226. expect(fs.existsSync(path.join(customSkillsDir(), 'skills', 'stray'))).toBe(false)
  227. expect(logs.some(l => l.includes('alpha'))).toBe(true)
  228. // 改动文件内容但标记未变:按标记判断不重新发布(运行时目录只能由服务写入)
  229. expect(logs.some(l => l.includes('stray'))).toBe(true)
  230. })
  231. })