skill-filesystem.spec.ts 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. import { describe, expect, it } from 'vitest'
  2. import { mkdir, readdir, readFile, rename, rm, stat, symlink, writeFile } from 'node:fs/promises'
  3. import { dirname, join } from 'node:path'
  4. import { tmpdir } from 'node:os'
  5. import { Context } from '@deepseek-ai/cordis'
  6. import SkillRegistry from '@deepseek-ai/dsh-skill'
  7. import { FileSystem, FsError, FsVersion, type FsDirEntry, type FsEditOutcome, type FsEditRequest, type FsInfo, type FsPathInfo, type FsTarget, type FsWriteOutcome } from '@deepseek-ai/dsh-fs'
  8. import * as SkillFileSystem from '../src/index.ts'
  9. async function tempDir(name: string): Promise<string> {
  10. return await import('node:fs/promises').then(fs => fs.mkdtemp(join(tmpdir(), `dsh-${name}-`)))
  11. }
  12. async function writeSkill(root: string, name: string, description: string, body = 'Use the skill.'): Promise<void> {
  13. const dir = join(root, name)
  14. await mkdir(dir, { recursive: true })
  15. await writeFile(join(dir, 'SKILL.md'), `---\nname: ${name}\ndescription: ${description}\n---\n\n${body}\n`)
  16. }
  17. async function writeFlatSkill(root: string, name: string, description: string, body = 'Flat body.'): Promise<void> {
  18. await mkdir(root, { recursive: true })
  19. await writeFile(join(root, `${name}.md`), `---\nname: ${name}\ndescription: ${description}\n---\n\n${body}\n`)
  20. }
  21. class TestFileSystem extends FileSystem {
  22. listDirCalls = 0
  23. failResolvePaths = new Set<string>()
  24. failStatPaths = new Set<string>()
  25. failListDirPaths = new Set<string>()
  26. errorResolvePaths = new Set<string>()
  27. errorStatPaths = new Set<string>()
  28. errorReadPaths = new Set<string>()
  29. missingReadPaths = new Set<string>()
  30. statOverrides = new Map<string, FsInfo | undefined>()
  31. statSignals: Array<AbortSignal | undefined> = []
  32. readTextSignals: Array<AbortSignal | undefined> = []
  33. readTextOverride?: (target: FsTarget, signal?: AbortSignal) => Promise<string>
  34. override async resolve(path: string): Promise<FsTarget> {
  35. if (this.failResolvePaths.has(path)) throw new FsError('resolve failed', 'FS_NOT_FOUND')
  36. if (this.errorResolvePaths.has(path)) throw new Error('resolve temporarily failed')
  37. return { targetKey: path as never, displayPath: path }
  38. }
  39. override processPath(target: FsTarget): string { return String(target.targetKey) }
  40. override fileUrl(target: FsTarget): string { return `file://${target.targetKey}` }
  41. override contains(parent: FsTarget, child: FsTarget): boolean {
  42. return child.targetKey === parent.targetKey || String(child.targetKey).startsWith(`${parent.targetKey}/`)
  43. }
  44. override async stat(target: FsTarget, signal?: AbortSignal): Promise<FsInfo | undefined> {
  45. this.statSignals.push(signal)
  46. if (this.failStatPaths.has(target.displayPath)) throw new FsError('stat failed', 'FS_NOT_FOUND')
  47. if (this.errorStatPaths.has(target.displayPath)) throw new Error('stat temporarily failed')
  48. if (this.statOverrides.has(target.displayPath)) return this.statOverrides.get(target.displayPath)
  49. try {
  50. const fs = await import('node:fs/promises')
  51. const info = await fs.stat(target.displayPath)
  52. return {
  53. version: FsVersion(String(info.mtimeMs)),
  54. type: info.isFile() ? 'file' : info.isDirectory() ? 'directory' : 'other',
  55. size: info.size,
  56. }
  57. } catch {
  58. return undefined
  59. }
  60. }
  61. override async lstat(path: string): Promise<FsPathInfo | undefined> {
  62. try {
  63. const fs = await import('node:fs/promises')
  64. const info = await fs.lstat(path)
  65. return {
  66. version: FsVersion(String(info.mtimeMs)),
  67. type: info.isSymbolicLink() ? 'symlink' : info.isFile() ? 'file' : info.isDirectory() ? 'directory' : 'other',
  68. size: info.size,
  69. }
  70. } catch {
  71. return undefined
  72. }
  73. }
  74. override async readText(target: FsTarget, signal?: AbortSignal): Promise<string> {
  75. this.readTextSignals.push(signal)
  76. if (this.readTextOverride !== undefined) return await this.readTextOverride(target, signal)
  77. if (this.missingReadPaths.has(target.displayPath)) throw new FsError('read failed', 'FS_NOT_FOUND')
  78. if (this.errorReadPaths.has(target.displayPath)) throw new Error('read temporarily failed')
  79. const text = await readFile(target.displayPath, 'utf8')
  80. if (text.includes('\uFFFD')) throw new FsError('not text', 'FS_NOT_TEXT')
  81. return text
  82. }
  83. override async streamText(_target: FsTarget): Promise<AsyncIterable<string>> {
  84. throw new Error('not needed in skill tests')
  85. }
  86. override async readBytes(_target: FsTarget, _signal: AbortSignal | undefined, _maxBytes: number): Promise<Uint8Array> {
  87. throw new Error('not needed in skill tests')
  88. }
  89. override async listDir(target: FsTarget): Promise<FsDirEntry[]> {
  90. this.listDirCalls += 1
  91. if (this.failListDirPaths.has(target.displayPath)) throw new Error('list temporarily failed')
  92. const entries = await readdir(target.displayPath, { withFileTypes: true, encoding: 'utf8' })
  93. const result: FsDirEntry[] = []
  94. for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
  95. const childPath = join(target.displayPath, entry.name)
  96. let type: FsInfo['type'] = 'other'
  97. let size: number | undefined
  98. try {
  99. const info = await stat(childPath)
  100. type = info.isFile() ? 'file' : info.isDirectory() ? 'directory' : 'other'
  101. size = info.isFile() ? info.size : undefined
  102. } catch {
  103. type = 'other'
  104. }
  105. result.push({
  106. name: entry.name,
  107. type,
  108. target: { targetKey: childPath as never, displayPath: childPath },
  109. version: FsVersion('test'),
  110. ...(size !== undefined ? { size } : {}),
  111. })
  112. }
  113. return result
  114. }
  115. override async writeText(target: FsTarget, content: string): Promise<FsWriteOutcome> {
  116. await mkdir(dirname(target.displayPath), { recursive: true })
  117. await writeFile(target.displayPath, content)
  118. return { operation: 'create', version: FsVersion('test'), before: null, after: content }
  119. }
  120. override async editText(_target: FsTarget, _request: FsEditRequest): Promise<FsEditOutcome> {
  121. throw new Error('not needed in skill tests')
  122. }
  123. }
  124. async function setupLocal(home: string, config: Partial<SkillFileSystem.Config> = {}): Promise<Context> {
  125. const ctx = new Context()
  126. await ctx.plugin(SkillRegistry)
  127. await ctx.plugin(SkillFileSystem, {
  128. dshHome: join(home, '.dsh'),
  129. agentsHome: join(home, '.agents'),
  130. watch: false,
  131. ...config,
  132. })
  133. return ctx
  134. }
  135. async function waitFor<T>(read: () => Promise<T>, accept: (value: T) => boolean): Promise<T> {
  136. const deadline = Date.now() + 5000
  137. while (true) {
  138. const value = await read()
  139. if (accept(value)) return value
  140. if (Date.now() >= deadline) throw new Error('timed out waiting for watcher state')
  141. await new Promise(resolve => setTimeout(resolve, 20))
  142. }
  143. }
  144. describe('dsh-skill-filesystem plugin exports', () => {
  145. it('declares stable plugin metadata', () => {
  146. expect(SkillFileSystem.name).toBe('skill-filesystem')
  147. expect(SkillFileSystem.inject).toEqual(['skills'])
  148. })
  149. })
  150. describe('FileSystemSkillProvider', () => {
  151. it('discovers project, custom, user, and agents skill roots in priority order', async () => {
  152. const home = await tempDir('skill-home')
  153. const project = await tempDir('skill-project')
  154. const custom = await tempDir('skill-custom')
  155. await mkdir(join(project, '.git'), { recursive: true })
  156. await writeSkill(join(home, '.agents/skills'), 'same', 'user agents skill')
  157. await writeSkill(join(home, '.dsh/skills'), 'same', 'user dsh skill')
  158. await writeSkill(custom, 'same', 'custom skill')
  159. await writeSkill(join(project, '.agents/skills'), 'same', 'project agents skill')
  160. await writeSkill(join(project, '.dsh/skills'), 'same', 'project dsh skill')
  161. await writeSkill(custom, 'custom-only', 'custom only')
  162. await writeSkill(join(home, '.dsh/skills/.system'), 'hidden-system', 'hidden system')
  163. const bundled = await tempDir('skill-bundled')
  164. await writeSkill(bundled, 'bundled-only', 'bundled skill')
  165. await writeSkill(bundled, 'same', 'bundled skill')
  166. const ctx = await setupLocal(home, { customSkillDirs: [custom], bundledSkillDir: bundled })
  167. const skills = await ctx.skills.list({ cwd: join(project, 'src') })
  168. expect(skills.map(skill => skill.name)).toEqual([
  169. 'bundled-only',
  170. 'custom-only',
  171. 'same',
  172. ])
  173. expect(skills.find(skill => skill.name === 'custom-only')?.description).toBe('custom only')
  174. expect(skills.find(skill => skill.name === 'same')?.description).toBe('project dsh skill')
  175. expect(skills.find(skill => skill.name === 'same')?.source).toBe('project-dsh')
  176. expect(skills.find(skill => skill.name === 'hidden-system')).toBeUndefined()
  177. expect(skills.find(skill => skill.name === 'bundled-only')).toMatchObject({ source: 'bundled' })
  178. expect((await ctx.skills.get('bundled-only'))?.content).toBe('Use the skill.')
  179. const noGit = await tempDir('skill-no-git')
  180. await writeSkill(join(noGit, '.dsh/skills'), 'fallback-root', 'Fallback root')
  181. expect((await ctx.skills.list({ cwd: noGit })).map(skill => skill.name)).toContain('fallback-root')
  182. })
  183. it('lets project skills override runtime while runtime overrides custom and user skills', async () => {
  184. const home = await tempDir('skill-runtime-priority')
  185. const project = await tempDir('skill-runtime-project')
  186. const custom = await tempDir('skill-runtime-custom')
  187. await mkdir(join(project, '.git'), { recursive: true })
  188. await writeSkill(join(project, '.dsh/skills'), 'project-name', 'Project wins')
  189. await writeSkill(custom, 'runtime-name', 'Custom loses')
  190. await writeSkill(join(home, '.dsh/skills'), 'runtime-name', 'User loses')
  191. const ctx = await setupLocal(home, { customSkillDirs: [custom] })
  192. ctx.skills.register({
  193. name: 'project-name',
  194. description: 'Runtime loses to project',
  195. content: 'Runtime body.',
  196. source: 'runtime',
  197. })
  198. ctx.skills.register({
  199. name: 'runtime-name',
  200. description: 'Runtime wins',
  201. content: 'Runtime body.',
  202. source: 'runtime',
  203. })
  204. expect((await ctx.skills.get('project-name', { cwd: project }))?.description).toBe('Project wins')
  205. expect((await ctx.skills.get('runtime-name', { cwd: project }))?.description).toBe('Runtime wins')
  206. })
  207. it('parses flat skills and filters invalid skills from the invocation-neutral listing', async () => {
  208. const home = await tempDir('skill-flat')
  209. const root = join(home, '.dsh/skills')
  210. await writeFlatSkill(root, 'flat-skill', 'flat description', 'Flat instructions.')
  211. await writeFile(join(root, 'rich-skill.md'), [
  212. '---',
  213. 'name: rich-skill',
  214. 'description: rich description',
  215. 'whenToUse: For richer local parsing',
  216. 'disable-model-invocation: off',
  217. 'user-invocable: YES',
  218. 'metadata:',
  219. ' owner: tests',
  220. '---',
  221. '',
  222. 'Rich body.',
  223. ].join('\n'))
  224. await writeFile(join(root, 'bad.md'), '---\nname: Bad_Name\ndescription: bad\n---\n\nbad')
  225. await writeFile(join(root, 'missing-description.md'), '---\nname: missing-description\n---\n\nbad')
  226. await writeFile(join(root, 'no-frontmatter.md'), 'No frontmatter.')
  227. await writeFile(join(root, 'plain-markdown.md'), '# Notes\nNot a skill.')
  228. await writeFile(join(root, 'open-frontmatter.md'), '---\nname: open-frontmatter')
  229. await writeFile(join(root, 'non-object.md'), '---\n[]\n---\n\nbad')
  230. await writeFile(join(root, 'no-trailing-body.md'), '---\nname: no-trailing-body\ndescription: No trailing body\n---')
  231. await writeFile(join(root, 'notes.txt'), 'ignored')
  232. await mkdir(join(root, 'not-a-skill'), { recursive: true })
  233. await writeSkill(root, 'user-only-skill', 'user-only description', 'User-only.')
  234. await writeFile(join(root, 'user-only-skill/SKILL.md'), '---\nname: user-only-skill\ndescription: user-only description\ndisable-model-invocation: true\n---\n\nUser-only.\n')
  235. await writeSkill(root, 'model-only-skill', 'model-only description', 'Model-only.')
  236. await writeFile(join(root, 'model-only-skill/SKILL.md'), '---\nname: model-only-skill\ndescription: model-only description\nuser-invocable: false\n---\n\nModel-only.\n')
  237. const ctx = await setupLocal(home)
  238. const listedBeforeDelete = await ctx.skills.list()
  239. const flatSummary = listedBeforeDelete.find(skill => skill.name === 'flat-skill')
  240. if (flatSummary === undefined) throw new Error('expected flat-skill')
  241. await rm(join(root, 'flat-skill.md'))
  242. expect(listedBeforeDelete.map(skill => skill.name)).toEqual([
  243. 'flat-skill',
  244. 'model-only-skill',
  245. 'no-trailing-body',
  246. 'rich-skill',
  247. 'user-only-skill',
  248. ])
  249. expect(flatSummary.invocation).toEqual({ modelInvocable: true, userInvocable: true })
  250. expect(await ctx.skills.get('flat-skill')).toBeUndefined()
  251. expect(await ctx.skills.get('no-trailing-body')).toMatchObject({
  252. invocation: { modelInvocable: true, userInvocable: true },
  253. })
  254. expect(await ctx.skills.get('user-only-skill')).toMatchObject({
  255. invocation: { modelInvocable: false, userInvocable: true },
  256. content: 'User-only.',
  257. })
  258. expect(await ctx.skills.get('model-only-skill')).toMatchObject({
  259. invocation: { modelInvocable: true, userInvocable: false },
  260. content: 'Model-only.',
  261. })
  262. expect(await ctx.skills.get('rich-skill')).toMatchObject({
  263. whenToUse: 'For richer local parsing',
  264. invocation: { modelInvocable: true, userInvocable: true },
  265. metadata: { owner: 'tests' },
  266. })
  267. expect(await ctx.skills.get('Bad_Name')).toBeUndefined()
  268. })
  269. it('accepts the documented boolean spellings for invocation frontmatter', async () => {
  270. const home = await tempDir('skill-invocation-booleans')
  271. const root = join(home, '.dsh/skills')
  272. await mkdir(root, { recursive: true })
  273. const truthy = ['true', 'TRUE', '"true"', 'yes', 'ON', '1', '"1"']
  274. const falsy = ['false', 'FALSE', '"false"', 'no', 'OFF', '0', '"0"']
  275. for (const [index, value] of truthy.entries()) {
  276. await writeFile(join(root, `truthy-${index}.md`), [
  277. '---',
  278. `name: truthy-${index}`,
  279. `description: Truthy ${index}`,
  280. `disable-model-invocation: ${value}`,
  281. '---',
  282. '',
  283. 'Truthy.',
  284. ].join('\n'))
  285. }
  286. for (const [index, value] of falsy.entries()) {
  287. await writeFile(join(root, `falsy-${index}.md`), [
  288. '---',
  289. `name: falsy-${index}`,
  290. `description: Falsy ${index}`,
  291. `user-invocable: ${value}`,
  292. '---',
  293. '',
  294. 'Falsy.',
  295. ].join('\n'))
  296. }
  297. const ctx = await setupLocal(home)
  298. for (const [index] of truthy.entries()) {
  299. expect((await ctx.skills.get(`truthy-${index}`))?.invocation).toEqual({
  300. modelInvocable: false,
  301. userInvocable: true,
  302. })
  303. }
  304. for (const [index] of falsy.entries()) {
  305. expect((await ctx.skills.get(`falsy-${index}`))?.invocation).toEqual({
  306. modelInvocable: true,
  307. userInvocable: false,
  308. })
  309. }
  310. })
  311. it('rejects legacy and invalid invocation frontmatter without hiding valid siblings', async () => {
  312. const home = await tempDir('skill-invalid-invocation')
  313. const root = join(home, '.dsh/skills')
  314. await writeSkill(root, 'good-skill', 'Good skill')
  315. const invalid = [
  316. ['legacy-model', 'disableModelInvocation: true'],
  317. ['legacy-positive-model', 'modelInvocable: false'],
  318. ['legacy-user', 'userInvocable: false'],
  319. ['bad-string', 'disable-model-invocation: maybe'],
  320. ['bad-value', 'user-invocable: null'],
  321. ] as const
  322. for (const [name, field] of invalid) {
  323. await writeFile(join(root, `${name}.md`), `---\nname: ${name}\ndescription: ${name}\n${field}\n---\n\nBad.\n`)
  324. }
  325. const ctx = await setupLocal(home)
  326. expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['good-skill'])
  327. })
  328. it('supports CRLF frontmatter and ignores delimiter-looking text inside YAML values', async () => {
  329. const home = await tempDir('skill-frontmatter-crlf')
  330. const root = join(home, '.dsh/skills')
  331. await mkdir(root, { recursive: true })
  332. await writeFile(join(root, 'crlf-skill.md'), [
  333. '---',
  334. 'name: crlf-skill',
  335. 'description: CRLF skill',
  336. 'metadata:',
  337. ' marker: "----"',
  338. '---',
  339. '',
  340. 'CRLF body.',
  341. ].join('\r\n'))
  342. await writeFile(join(root, 'block-skill.md'), [
  343. '---',
  344. 'name: block-skill',
  345. 'description: |',
  346. ' Includes a ---- marker that is not a delimiter.',
  347. '---',
  348. '',
  349. 'Block body.',
  350. ].join('\n'))
  351. const ctx = await setupLocal(home)
  352. expect((await ctx.skills.get('crlf-skill'))?.content).toBe('CRLF body.')
  353. expect((await ctx.skills.get('crlf-skill'))?.metadata).toEqual({ marker: '----' })
  354. expect((await ctx.skills.get('block-skill'))?.description).toBe('Includes a ---- marker that is not a delimiter.\n')
  355. expect((await ctx.skills.get('block-skill'))?.content).toBe('Block body.')
  356. })
  357. it('skips invalid YAML skill files without hiding valid siblings', async () => {
  358. const home = await tempDir('skill-invalid-yaml')
  359. const root = join(home, '.dsh/skills')
  360. await writeSkill(root, 'good-skill', 'Good skill')
  361. await writeFile(join(root, 'bad-yaml.md'), '---\nname: bad-yaml\ndescription: [unclosed\n---\n\nBad body.\n')
  362. const ctx = await setupLocal(home)
  363. expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['good-skill'])
  364. })
  365. it('discovers symlinked skill directories and flat files', async () => {
  366. const home = await tempDir('skill-symlink-home')
  367. const external = await tempDir('skill-symlink-external')
  368. await writeSkill(external, 'linked-dir', 'Linked directory')
  369. await writeFlatSkill(external, 'linked-flat', 'Linked flat')
  370. await mkdir(join(home, '.dsh/skills'), { recursive: true })
  371. await symlink(join(external, 'linked-dir'), join(home, '.dsh/skills/linked-dir'))
  372. await symlink(join(external, 'linked-flat.md'), join(home, '.dsh/skills/linked-flat.md'))
  373. await symlink(join(external, 'missing'), join(home, '.dsh/skills/broken-link'))
  374. await symlink('/dev/null', join(home, '.dsh/skills/device-link'))
  375. const ctx = await setupLocal(home)
  376. expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['linked-dir', 'linked-flat'])
  377. })
  378. it('uses the filesystem service for discovery, reads, and project-root lookup', async () => {
  379. const home = await tempDir('skill-read-fs')
  380. const project = await tempDir('skill-project-root-backend')
  381. const nestedCwd = join(project, 'packages/app')
  382. const root = join(home, '.dsh/skills')
  383. await mkdir(nestedCwd, { recursive: true })
  384. await writeFlatSkill(root, 'text-skill', 'Text skill', 'Text body.')
  385. await writeFlatSkill(root, 'resolve-fail', 'Resolve fail', 'Resolve body.')
  386. await writeFlatSkill(root, 'stat-fail', 'Stat fail', 'Stat body.')
  387. await mkdir(join(root, 'empty-dir'), { recursive: true })
  388. await mkdir(join(root, 'directory-skill/SKILL.md'), { recursive: true })
  389. await writeFile(join(root, 'binary-skill.md'), Buffer.concat([
  390. Buffer.from('---\nname: binary-skill\ndescription: Binary skill\n---\n\n'),
  391. Buffer.from([0xff]),
  392. Buffer.from('\n'),
  393. ]))
  394. await writeSkill(join(project, '.agents/skills'), 'backend-root', 'Backend root skill')
  395. const ctx = new Context()
  396. await ctx.plugin(TestFileSystem)
  397. const fs = ctx.fs as TestFileSystem
  398. fs.failResolvePaths.add(join(root, 'resolve-fail.md'))
  399. fs.failStatPaths.add(join(root, 'stat-fail.md'))
  400. fs.failResolvePaths.add(join(nestedCwd, '.git'))
  401. fs.failStatPaths.add(join(project, 'packages/.git'))
  402. fs.statOverrides.set(join(project, '.git'), {
  403. version: FsVersion('virtual-git'),
  404. type: 'directory',
  405. size: 0,
  406. })
  407. await ctx.plugin(SkillRegistry)
  408. await ctx.plugin(SkillFileSystem, { dshHome: join(home, '.dsh'), agentsHome: join(home, '.agents'), watch: false })
  409. expect((await ctx.skills.list({ cwd: nestedCwd })).map(skill => [skill.name, skill.source])).toEqual([
  410. ['backend-root', 'project-agents'],
  411. ['text-skill', 'user-dsh'],
  412. ])
  413. expect(fs.listDirCalls).toBeGreaterThan(0)
  414. expect(await ctx.skills.get('binary-skill')).toBeUndefined()
  415. const bundled = await tempDir('skill-backend-bundled')
  416. await writeSkill(bundled, 'bundled-host', 'Bundled host skill')
  417. const bundledCtx = new Context()
  418. await bundledCtx.plugin(TestFileSystem)
  419. const bundledFs = bundledCtx.fs as TestFileSystem
  420. bundledFs.failResolvePaths.add(bundled)
  421. await bundledCtx.plugin(SkillRegistry)
  422. await bundledCtx.plugin(SkillFileSystem, {
  423. dshHome: join(home, '.dsh'),
  424. agentsHome: join(home, '.agents'),
  425. bundledSkillDir: bundled,
  426. })
  427. expect((await bundledCtx.skills.get('bundled-host'))?.source).toBe('bundled')
  428. })
  429. it('reports transient root reads as incomplete without caching an empty catalog', async () => {
  430. const home = await tempDir('skill-transient-root')
  431. const root = join(home, '.agents/skills')
  432. await writeSkill(root, 'stable-skill', 'Stable skill')
  433. const ctx = new Context()
  434. await ctx.plugin(TestFileSystem)
  435. const fs = ctx.fs as TestFileSystem
  436. await ctx.plugin(SkillRegistry)
  437. await ctx.plugin(SkillFileSystem, {
  438. dshHome: join(home, '.dsh'),
  439. agentsHome: join(home, '.agents'),
  440. watch: false,
  441. })
  442. expect(await ctx.skills.snapshot()).toMatchObject({
  443. skills: [{ name: 'stable-skill' }],
  444. complete: true,
  445. })
  446. fs.failListDirPaths.add(root)
  447. const path = join(root, 'stable-skill/SKILL.md')
  448. ctx.emit(
  449. 'fs/observed',
  450. { targetKey: path as never, displayPath: path },
  451. { kind: 'present', version: FsVersion('failed-read') },
  452. { name: 'edit' },
  453. )
  454. expect(await ctx.skills.snapshot()).toEqual({ skills: [], complete: false })
  455. fs.failListDirPaths.clear()
  456. expect(await ctx.skills.snapshot()).toMatchObject({
  457. skills: [{ name: 'stable-skill' }],
  458. complete: true,
  459. })
  460. })
  461. it('distinguishes transient filesystem entry failures from confirmed disappearance', async () => {
  462. const home = await tempDir('skill-transient-entry')
  463. const root = join(home, '.agents/skills')
  464. const path = join(root, 'stable-skill/SKILL.md')
  465. await writeSkill(root, 'stable-skill', 'Stable skill')
  466. const ctx = new Context()
  467. await ctx.plugin(TestFileSystem)
  468. const fs = ctx.fs as TestFileSystem
  469. await ctx.plugin(SkillRegistry)
  470. await ctx.plugin(SkillFileSystem, {
  471. dshHome: join(home, '.dsh'),
  472. agentsHome: join(home, '.agents'),
  473. watch: false,
  474. })
  475. const invalidate = (): void => {
  476. ctx.emit(
  477. 'fs/observed',
  478. { targetKey: path as never, displayPath: path },
  479. { kind: 'present', version: FsVersion('entry-failure') },
  480. { name: 'write' },
  481. )
  482. }
  483. expect((await ctx.skills.snapshot()).complete).toBe(true)
  484. for (const failures of [fs.errorResolvePaths, fs.errorStatPaths, fs.errorReadPaths]) {
  485. failures.add(path)
  486. invalidate()
  487. expect((await ctx.skills.snapshot()).complete).toBe(false)
  488. failures.clear()
  489. }
  490. fs.missingReadPaths.add(path)
  491. invalidate()
  492. expect(await ctx.skills.snapshot()).toEqual({ skills: [], complete: true })
  493. fs.missingReadPaths.clear()
  494. invalidate()
  495. expect(await ctx.skills.snapshot()).toMatchObject({
  496. skills: [{ name: 'stable-skill' }],
  497. complete: true,
  498. })
  499. })
  500. it('marks an unexpected native skill-file read failure incomplete', async () => {
  501. const home = await tempDir('skill-native-read-failure')
  502. const root = join(home, '.agents/skills')
  503. await mkdir(join(root, 'broken-skill/SKILL.md'), { recursive: true })
  504. const ctx = await setupLocal(home)
  505. expect(await ctx.skills.snapshot()).toEqual({ skills: [], complete: false })
  506. })
  507. it('forwards cancellation to filesystem reads while loading a skill', async () => {
  508. const home = await tempDir('skill-read-abort')
  509. await writeSkill(join(home, '.dsh/skills'), 'abortable-skill', 'Abortable skill')
  510. const ctx = new Context()
  511. await ctx.plugin(TestFileSystem)
  512. const fs = ctx.fs as TestFileSystem
  513. await ctx.plugin(SkillRegistry)
  514. await ctx.plugin(SkillFileSystem, { dshHome: join(home, '.dsh'), agentsHome: join(home, '.agents'), watch: false })
  515. expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['abortable-skill'])
  516. fs.statSignals = []
  517. fs.readTextSignals = []
  518. const started = Promise.withResolvers<undefined>()
  519. fs.readTextOverride = async (_target, signal) => {
  520. if (signal === undefined) throw new Error('expected the skill lookup signal')
  521. started.resolve(undefined)
  522. return await new Promise<string>((_resolve, reject) => {
  523. signal.addEventListener('abort', () => {
  524. const abortReason = signal.reason as unknown
  525. reject(abortReason instanceof Error ? abortReason : new Error(String(abortReason)))
  526. }, { once: true })
  527. })
  528. }
  529. const controller = new AbortController()
  530. const reason = new Error('turn cancelled')
  531. const loading = ctx.skills.get('abortable-skill', { signal: controller.signal })
  532. await started.promise
  533. controller.abort(reason)
  534. await expect(loading).rejects.toBe(reason)
  535. expect(fs.statSignals).toEqual([controller.signal])
  536. expect(fs.readTextSignals).toEqual([controller.signal])
  537. })
  538. it('refreshes additions, metadata changes, deletions, and a recreated missing root', { timeout: 20000 }, async () => {
  539. const home = await tempDir('skill-watch-home')
  540. const agentsRoot = join(home, '.agents/skills')
  541. const ctx = new Context()
  542. await ctx.plugin(SkillRegistry)
  543. const fiber = await ctx.plugin(SkillFileSystem, {
  544. dshHome: join(home, '.dsh'),
  545. agentsHome: join(home, '.agents'),
  546. watch: true,
  547. watchStabilityThresholdMs: 20,
  548. watchPollIntervalMs: 10,
  549. })
  550. try {
  551. expect(await ctx.skills.list()).toEqual([])
  552. await writeSkill(agentsRoot, 'watched-skill', 'First description', 'First body.')
  553. const added = await waitFor(
  554. async () => await ctx.skills.list(),
  555. skills => skills.some(skill => skill.name === 'watched-skill'),
  556. )
  557. expect(added.find(skill => skill.name === 'watched-skill')?.description).toBe('First description')
  558. await writeSkill(agentsRoot, 'watched-skill', 'Second description', 'Second body.')
  559. const changed = await waitFor(
  560. async () => await ctx.skills.list(),
  561. skills => skills.find(skill => skill.name === 'watched-skill')?.description === 'Second description',
  562. )
  563. expect(changed).toHaveLength(1)
  564. expect((await ctx.skills.get('watched-skill'))?.content).toBe('Second body.')
  565. await writeFlatSkill(agentsRoot, 'flat-added', 'Flat added')
  566. expect(await waitFor(
  567. async () => (await ctx.skills.list()).map(skill => skill.name),
  568. names => names.includes('flat-added'),
  569. )).toEqual(['flat-added', 'watched-skill'])
  570. await rename(join(agentsRoot, 'watched-skill'), join(agentsRoot, 'renamed-skill'))
  571. await writeSkill(agentsRoot, 'renamed-skill', 'Renamed skill')
  572. expect(await waitFor(
  573. async () => (await ctx.skills.list()).map(skill => skill.name),
  574. names => names.includes('renamed-skill') && !names.includes('watched-skill'),
  575. )).toEqual(['flat-added', 'renamed-skill'])
  576. await rm(join(agentsRoot, 'renamed-skill'), { recursive: true })
  577. expect(await waitFor(
  578. async () => (await ctx.skills.list()).map(skill => skill.name),
  579. names => !names.includes('renamed-skill'),
  580. )).toEqual(['flat-added'])
  581. await rm(join(home, '.agents'), { recursive: true })
  582. expect(await waitFor(
  583. async () => await ctx.skills.list(),
  584. skills => skills.length === 0,
  585. )).toEqual([])
  586. await writeSkill(agentsRoot, 'recreated-skill', 'Recreated')
  587. expect(await waitFor(
  588. async () => (await ctx.skills.list()).map(skill => skill.name),
  589. names => names.includes('recreated-skill'),
  590. )).toEqual(['recreated-skill'])
  591. } finally {
  592. await fiber.dispose()
  593. }
  594. })
  595. it('uses fs/observed as a synchronous first-party invalidation path without a watcher', async () => {
  596. const home = await tempDir('skill-observed-home')
  597. const root = join(home, '.agents/skills')
  598. const ctx = await setupLocal(home)
  599. expect(await ctx.skills.list()).toEqual([])
  600. let invalidations = 0
  601. ctx.on('skills/change', () => { invalidations += 1 })
  602. await writeSkill(root, 'observed-skill', 'Observed skill')
  603. const path = join(root, 'observed-skill/SKILL.md')
  604. const emitObserved = (displayPath: string, actor?: object): void => {
  605. ctx.emit(
  606. 'fs/observed',
  607. { targetKey: displayPath as never, displayPath },
  608. { kind: 'present', version: FsVersion('observed') },
  609. actor,
  610. )
  611. }
  612. emitObserved(path)
  613. emitObserved(path, {})
  614. emitObserved(path, { name: 'read' })
  615. emitObserved(join(home, 'outside.md'), { name: 'write' })
  616. emitObserved(root, { name: 'write' })
  617. emitObserved(join(root, 'observed-skill/references/notes.md'), { name: 'write' })
  618. emitObserved(join(home, '.dsh/skills/.system/SKILL.md'), { name: 'write' })
  619. emitObserved(join(root, 'flat-skill.md'), { name: 'write' })
  620. ctx.emit(
  621. 'fs/observed',
  622. { targetKey: path as never, displayPath: path },
  623. { kind: 'present', version: FsVersion('observed') },
  624. { name: 'edit' },
  625. )
  626. expect(invalidations).toBe(2)
  627. expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['observed-skill'])
  628. })
  629. it('bounds project watchers and re-observes an evicted project on its next lookup', async () => {
  630. const home = await tempDir('skill-watch-lru-home')
  631. const first = await tempDir('skill-watch-lru-first')
  632. const second = await tempDir('skill-watch-lru-second')
  633. await mkdir(join(first, '.git'), { recursive: true })
  634. await mkdir(join(second, '.git'), { recursive: true })
  635. await writeSkill(join(first, '.agents/skills'), 'first-project', 'First project')
  636. await writeSkill(join(second, '.agents/skills'), 'second-project', 'Second project')
  637. const ctx = new Context()
  638. await ctx.plugin(SkillRegistry)
  639. const fiber = await ctx.plugin(SkillFileSystem, {
  640. dshHome: join(home, '.dsh'),
  641. agentsHome: join(home, '.agents'),
  642. customSkillDirs: [join(first, '.agents/skills')],
  643. watch: true,
  644. watchMaxProjects: 1,
  645. watchStabilityThresholdMs: 20,
  646. watchPollIntervalMs: 10,
  647. })
  648. try {
  649. expect((await ctx.skills.list({ cwd: first })).map(skill => skill.name)).toContain('first-project')
  650. expect((await ctx.skills.list({ cwd: second })).map(skill => skill.name)).toContain('second-project')
  651. await writeSkill(join(first, '.agents/skills'), 'first-project', 'First project refreshed')
  652. expect((await ctx.skills.list({ cwd: first })).find(skill => skill.name === 'first-project')?.description)
  653. .toBe('First project refreshed')
  654. } finally {
  655. await fiber.dispose()
  656. }
  657. const noWatch = new Context()
  658. await noWatch.plugin(SkillRegistry)
  659. await noWatch.plugin(SkillFileSystem, {
  660. dshHome: join(home, '.dsh'),
  661. agentsHome: join(home, '.agents'),
  662. watch: false,
  663. watchMaxProjects: 1,
  664. })
  665. await noWatch.skills.list({ cwd: first })
  666. await noWatch.skills.list({ cwd: second })
  667. })
  668. it('contains repeated disposal and late first-party observations', async () => {
  669. const home = await tempDir('skill-watch-dispose')
  670. const nonDirectoryRoot = join(home, 'not-a-directory')
  671. await writeFile(nonDirectoryRoot, 'not a skill root')
  672. await writeSkill(join(home, '.agents/skills'), 'disposed-skill', 'Disposed skill')
  673. const ctx = new Context()
  674. await ctx.plugin(SkillRegistry)
  675. let provider!: SkillFileSystem.FileSystemSkillProvider
  676. const disposeProvider = ctx.skills.registerProvider((control) => {
  677. provider = new SkillFileSystem.FileSystemSkillProvider(ctx, control, {
  678. dshHome: join(home, '.dsh'),
  679. agentsHome: join(home, '.agents'),
  680. customSkillDirs: [nonDirectoryRoot],
  681. watch: true,
  682. watchStabilityThresholdMs: 20,
  683. watchPollIntervalMs: 10,
  684. })
  685. return provider
  686. })
  687. const beforeDisposal = await provider.list({})
  688. expect((Array.isArray(beforeDisposal) ? beforeDisposal : beforeDisposal.candidates).map(skill => skill.name))
  689. .toEqual(['disposed-skill'])
  690. await provider.dispose()
  691. await provider.dispose()
  692. provider.observeHostMutation(join(home, '.agents/skills/disposed-skill/SKILL.md'))
  693. const afterDisposal = await provider.list({})
  694. expect((Array.isArray(afterDisposal) ? afterDisposal : afterDisposal.candidates).map(skill => skill.name))
  695. .toEqual(['disposed-skill'])
  696. disposeProvider()
  697. })
  698. it('refreshes frontmatter through a followed skill symlink', { timeout: 10000 }, async () => {
  699. const home = await tempDir('skill-watch-symlink-home')
  700. const external = await tempDir('skill-watch-symlink-external')
  701. const root = join(home, '.dsh/skills')
  702. await writeSkill(external, 'linked-skill', 'First linked description')
  703. await mkdir(root, { recursive: true })
  704. await symlink(join(external, 'linked-skill'), join(root, 'linked-skill'))
  705. const ctx = new Context()
  706. await ctx.plugin(SkillRegistry)
  707. const fiber = await ctx.plugin(SkillFileSystem, {
  708. dshHome: join(home, '.dsh'),
  709. agentsHome: join(home, '.agents'),
  710. watch: true,
  711. watchFollowSymlinks: true,
  712. watchStabilityThresholdMs: 20,
  713. watchPollIntervalMs: 10,
  714. })
  715. try {
  716. expect((await ctx.skills.list())[0]?.description).toBe('First linked description')
  717. await writeSkill(external, 'linked-skill', 'Second linked description')
  718. const refreshed = await waitFor(
  719. async () => await ctx.skills.list(),
  720. skills => skills[0]?.description === 'Second linked description',
  721. )
  722. expect(refreshed[0]?.name).toBe('linked-skill')
  723. } finally {
  724. await fiber.dispose()
  725. }
  726. })
  727. it('validates watcher tunables at plugin load', async () => {
  728. const ctx = new Context()
  729. await ctx.plugin(SkillRegistry)
  730. await expect(ctx.plugin(SkillFileSystem, { watchMaxProjects: 0 })).rejects.toThrow('watchMaxProjects')
  731. await expect(ctx.plugin(SkillFileSystem, { watchPollIntervalMs: 1.5 })).rejects.toThrow('watchPollIntervalMs')
  732. await expect(ctx.plugin(SkillFileSystem, { watchStabilityThresholdMs: 0 })).rejects.toThrow('watchStabilityThresholdMs')
  733. })
  734. it('uses default home root resolution without exposing builtin skills', async () => {
  735. const previousDshHome = process.env.DSH_HOME
  736. const previousAgentsHome = process.env.DSH_AGENTS_HOME
  737. const previousBundledSkillDir = process.env.DSH_BUNDLED_SKILL_DIR
  738. const envHome = await tempDir('skill-env-home')
  739. try {
  740. process.env.DSH_HOME = join(envHome, '.dsh')
  741. process.env.DSH_AGENTS_HOME = join(envHome, '.agents')
  742. const bundled = join(envHome, 'bundled-skills')
  743. process.env.DSH_BUNDLED_SKILL_DIR = bundled
  744. await writeSkill(join(envHome, '.dsh/skills'), 'env-skill', 'Env skill')
  745. await writeSkill(bundled, 'env-bundled-skill', 'Env bundled skill')
  746. const ctx = new Context()
  747. await ctx.plugin(SkillRegistry)
  748. await ctx.plugin(SkillFileSystem, { watch: false })
  749. expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['env-bundled-skill', 'env-skill'])
  750. // Isolated providers see only their explicit roots: the environment
  751. // bundled root is a default root, so includeDefaultRoots: false must
  752. // drop it — isolated providers never re-claim the app's builtins.
  753. const isolated = new Context()
  754. await isolated.plugin(SkillRegistry)
  755. const customOnly = join(envHome, 'custom-only')
  756. await writeSkill(customOnly, 'custom-isolated-skill', 'Custom isolated skill')
  757. await isolated.plugin(SkillFileSystem, {
  758. providerName: 'isolated',
  759. includeDefaultRoots: false,
  760. customSkillDirs: [customOnly],
  761. watch: false,
  762. })
  763. expect((await isolated.skills.list()).map(skill => skill.name)).toEqual(['custom-isolated-skill'])
  764. await isolated.fiber.dispose()
  765. process.env.DSH_HOME = join(envHome, 'empty-dsh')
  766. delete process.env.DSH_BUNDLED_SKILL_DIR
  767. process.env.DSH_AGENTS_HOME = join(envHome, 'empty-agents')
  768. const empty = new Context()
  769. await empty.plugin(SkillRegistry)
  770. SkillFileSystem.apply(empty, { watch: false })
  771. expect(await empty.skills.list()).toEqual([])
  772. delete process.env.DSH_AGENTS_HOME
  773. expect(new SkillFileSystem.FileSystemSkillProvider(empty, {
  774. signal: new AbortController().signal,
  775. invalidate() {},
  776. }, { dshHome: join(envHome, 'empty-dsh') }).name).toBe('filesystem')
  777. } finally {
  778. if (previousDshHome === undefined) {
  779. delete process.env.DSH_HOME
  780. } else {
  781. process.env.DSH_HOME = previousDshHome
  782. }
  783. if (previousAgentsHome === undefined) {
  784. delete process.env.DSH_AGENTS_HOME
  785. } else {
  786. process.env.DSH_AGENTS_HOME = previousAgentsHome
  787. }
  788. if (previousBundledSkillDir === undefined) {
  789. delete process.env.DSH_BUNDLED_SKILL_DIR
  790. } else {
  791. process.env.DSH_BUNDLED_SKILL_DIR = previousBundledSkillDir
  792. }
  793. }
  794. })
  795. })