plugin-manager.spec.ts 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /**
  2. * The plugin manager over a real profile: a temporary harness home with one
  3. * profile, packages staged the way pnpm leaves them, the host tree booted
  4. * through `boot()` with the profile runtime the launcher provides, and a
  5. * fake pnpm that edits the profile the way the real one does.
  6. */
  7. import { EventEmitter } from 'node:events'
  8. import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
  9. import { mkdtemp } from 'node:fs/promises'
  10. import { tmpdir } from 'node:os'
  11. import { join } from 'node:path'
  12. import { PassThrough } from 'node:stream'
  13. import type { ChildProcess } from 'node:child_process'
  14. import { afterEach, describe, expect, it } from 'vitest'
  15. import { Context, type Plugin } from '@deepseek-ai/cordis'
  16. import Loader from '@deepseek-ai/cordis-plugin-loader'
  17. import type { PatchOptions } from '@deepseek-ai/cordis-plugin-include'
  18. import {
  19. boot, bundleLayerPatches, loadOptionalPatches, loadProfile, ProfileRuntime, readProbeCache, rootIncludeEntry,
  20. type Profile,
  21. } from '@deepseek-ai/dsh-app-boot'
  22. import { remoteMethods, RemoteError } from '@deepseek-ai/dsh-typert-protocol'
  23. import PluginManager, {
  24. type PluginInstallLogChunk, type PluginManagerInternals, type SpawnLike,
  25. } from '@deepseek-ai/dsh-host-plugin-manager'
  26. import type {} from '@deepseek-ai/dsh-host-plugin-manager/types'
  27. const NAME = 'dsh-test'
  28. /** A complete manager config: the schema fills defaults at load, the type does not. */
  29. function managerConfig(overrides: Partial<PluginManager['config']> = {}): PluginManager['config'] {
  30. return { pnpmCommand: 'pnpm', installTimeoutMs: 1_000, probeTimeoutMs: 20_000, installLogTailBytes: 16_384, ...overrides }
  31. }
  32. const contexts: Context[] = []
  33. afterEach(async () => {
  34. await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
  35. })
  36. /** Builtins the staged bundles name; registered on every boot. */
  37. const good: Plugin.Function = () => {}
  38. const throws: Plugin.Function = () => { throw new Error('boom at apply') }
  39. let flakyCalls = 0
  40. const flaky: Plugin.Function = () => {
  41. flakyCalls += 1
  42. if (flakyCalls === 1) throw new Error('flaky first start')
  43. }
  44. const provider: Plugin.Function = (ctx) => { ctx.effect(() => ctx.reflect.provide('fixtureSvc', { ready: true })) }
  45. const lonelyProvider: Plugin.Function = (ctx) => { ctx.effect(() => ctx.reflect.provide('lonelySvc', { ready: true })) }
  46. const consumer: Plugin.Object = { inject: ['fixtureSvc'], apply() {} }
  47. const prepare = (ctx: Context): void => {
  48. ctx.loader.builtins.good = good
  49. ctx.loader.builtins.throws = throws
  50. ctx.loader.builtins.flaky = flaky
  51. ctx.loader.builtins.provider = provider
  52. ctx.loader.builtins['lonely-provider'] = lonelyProvider
  53. ctx.loader.builtins.consumer = consumer
  54. }
  55. interface StagedHome {
  56. home: string
  57. profileDir: string
  58. anchor: string
  59. }
  60. /** A harness home with one empty live profile and an install anchor that carries nothing. */
  61. async function stageHome(patchReload: 'live' | 'startup' = 'live'): Promise<StagedHome> {
  62. const home = await mkdtemp(join(tmpdir(), 'dsh-plugin-manager-'))
  63. const profileDir = join(home, 'profiles', 'web')
  64. mkdirSync(profileDir, { recursive: true })
  65. writeFileSync(join(profileDir, 'package.json'), JSON.stringify({
  66. name: 'dsh-profile-web', private: true, dependencies: {}, dsh: { profile: { bundles: [], patchReload } },
  67. }, null, 2))
  68. writeFileSync(join(profileDir, 'cordis.yml'), '[]\n')
  69. const anchorDir = join(home, 'anchor')
  70. mkdirSync(anchorDir, { recursive: true })
  71. const anchor = join(anchorDir, 'package.json')
  72. writeFileSync(anchor, JSON.stringify({ name: 'dsh-anchor', version: '0.0.0', dependencies: {} }))
  73. return { home, profileDir, anchor }
  74. }
  75. interface StagedPackage {
  76. /** The bundle patch text; omitted stages a bundle-less package. */
  77. patch?: string
  78. /** `index.js` text, exported as the package main. */
  79. main?: string
  80. version?: string
  81. stage?: 'boot' | 'runtime'
  82. plugins?: { name: string; title?: string; config?: unknown }[]
  83. files?: Record<string, string>
  84. }
  85. /** Stage one package under the profile's node_modules, the way pnpm leaves it. */
  86. function stagePackage(profileDir: string, name: string, staged: StagedPackage): void {
  87. const dir = join(profileDir, 'node_modules', name)
  88. mkdirSync(dir, { recursive: true })
  89. writeFileSync(join(dir, 'package.json'), JSON.stringify({
  90. name,
  91. version: staged.version ?? '1.0.0',
  92. description: `staged ${name}`,
  93. type: 'module',
  94. ...staged.main === undefined ? {} : { main: 'index.js' },
  95. dsh: {
  96. title: `Title of ${name}`,
  97. ...staged.patch === undefined ? {} : { bundle: { patch: './cordis.patch.yml', ...staged.stage === undefined ? {} : { stage: staged.stage } } },
  98. ...staged.plugins === undefined ? {} : { plugins: staged.plugins },
  99. },
  100. }, null, 2))
  101. if (staged.patch !== undefined) writeFileSync(join(dir, 'cordis.patch.yml'), staged.patch)
  102. if (staged.main !== undefined) writeFileSync(join(dir, 'index.js'), staged.main)
  103. for (const [file, text] of Object.entries(staged.files ?? {})) {
  104. mkdirSync(join(dir, file, '..'), { recursive: true })
  105. writeFileSync(join(dir, file), text)
  106. }
  107. }
  108. /** Add a dependency to the profile manifest, as `pnpm add` does. */
  109. function addDependency(profileDir: string, name: string, spec = '1.0.0'): void {
  110. const path = join(profileDir, 'package.json')
  111. const manifest = JSON.parse(readFileSync(path, 'utf8')) as { dependencies: Record<string, string> }
  112. manifest.dependencies[name] = spec
  113. writeFileSync(path, JSON.stringify(manifest, null, 2))
  114. }
  115. function manifestOf(profileDir: string): { dependencies: Record<string, string>; dsh: { profile: { bundles: string[] } } } {
  116. return JSON.parse(readFileSync(join(profileDir, 'package.json'), 'utf8')) as ReturnType<typeof manifestOf>
  117. }
  118. /** What the fake pnpm does for one invocation. */
  119. type PnpmBehavior = (args: readonly string[]) => { code: number | null; stdout?: string; stderr?: string; hang?: boolean; error?: unknown }
  120. /** A fake `spawn` that runs `behavior` on the next tick and reports through a child-like emitter. */
  121. function fakePnpm(profileDir: string, behavior: PnpmBehavior, calls: string[][] = []): SpawnLike {
  122. return (command, args, options) => {
  123. calls.push([command, ...args])
  124. expect(options.cwd).toBe(profileDir)
  125. const child = new EventEmitter() as EventEmitter & { stdout: PassThrough; stderr: PassThrough; kill: (signal?: string) => boolean }
  126. child.stdout = new PassThrough()
  127. child.stderr = new PassThrough()
  128. let killed = false
  129. child.kill = () => { killed = true; return true }
  130. setTimeout(() => {
  131. const outcome = behavior(args)
  132. if (outcome.error !== undefined) {
  133. child.emit('error', outcome.error)
  134. return
  135. }
  136. if (outcome.stdout !== undefined) child.stdout.write(outcome.stdout)
  137. if (outcome.stderr !== undefined) child.stderr.write(outcome.stderr)
  138. if (outcome.hang === true) {
  139. // Report the kill the timeout sends, as a real child would.
  140. const poll = setInterval(() => {
  141. if (!killed) return
  142. clearInterval(poll)
  143. child.emit('close', null)
  144. }, 10)
  145. return
  146. }
  147. setTimeout(() => { child.emit('close', outcome.code) }, 5)
  148. }, 5)
  149. return child as unknown as ChildProcess
  150. }
  151. }
  152. /** The pnpm every install test shares: `add <name>` stages nothing (the test did) and records the dependency. */
  153. function recordingPnpm(profileDir: string, calls: string[][] = []): SpawnLike {
  154. return fakePnpm(profileDir, (args) => {
  155. const [verb, target] = args
  156. if (verb === 'add' && target !== undefined) {
  157. addDependency(profileDir, target)
  158. return { code: 0, stdout: `+ ${target} 1.0.0\n` }
  159. }
  160. if (verb === 'remove' && target !== undefined) {
  161. const path = join(profileDir, 'package.json')
  162. const manifest = JSON.parse(readFileSync(path, 'utf8')) as { dependencies: Record<string, string> }
  163. const { [target]: _removed, ...remaining } = manifest.dependencies
  164. writeFileSync(path, JSON.stringify({ ...manifest, dependencies: remaining }, null, 2))
  165. rmSync(join(profileDir, 'node_modules', target), { recursive: true, force: true })
  166. return { code: 0, stdout: `- ${target}\n` }
  167. }
  168. return { code: 1, stderr: 'unexpected pnpm invocation\n' }
  169. }, calls)
  170. }
  171. interface Booted {
  172. ctx: Context
  173. manager: PluginManager
  174. runtime: ProfileRuntime
  175. changes: { reason: string; packageName?: string }[]
  176. log: PluginInstallLogChunk[]
  177. }
  178. /** Boot the profile the way the launcher does, then mount the manager over it. */
  179. async function bootProfile(staged: StagedHome, internals: PluginManagerInternals = {}, config: Partial<PluginManager['config']> = {}): Promise<Booted> {
  180. const load = (): Profile => loadProfile(NAME, 'web', staged.anchor, staged.home)
  181. const composeFor = (profile: Profile): PatchOptions[] => structuredClone([
  182. ...profile.layers.flatMap(bundleLayerPatches),
  183. ...loadOptionalPatches(NAME, profile.patchPath) ?? [],
  184. ])
  185. const profile = load()
  186. const ctx = await boot(NAME, join(staged.profileDir, 'cordis.yml'), composeFor(profile), prepare)
  187. contexts.push(ctx)
  188. await ctx.plugin(ProfileRuntime, {
  189. profile,
  190. installAnchor: staged.anchor,
  191. loadProfile: load,
  192. compose: composeFor,
  193. rootEntry: () => rootIncludeEntry(ctx),
  194. readUserPatches: () => loadOptionalPatches(NAME, profile.patchPath) ?? [],
  195. })
  196. class TestManager extends PluginManager {
  197. constructor(context: Context, managerConfig: PluginManager['config']) {
  198. super(context, managerConfig, internals)
  199. }
  200. }
  201. const changes: Booted['changes'] = []
  202. const log: PluginInstallLogChunk[] = []
  203. ctx.on('plugins/changed', (change) => { changes.push(change) })
  204. ctx.on('plugins/install-log', (chunk) => { log.push(chunk) })
  205. await ctx.plugin(TestManager, managerConfig(config))
  206. const manager = ctx.get('pluginManager')
  207. if (manager === undefined) throw new Error('manager did not mount')
  208. return { ctx, manager, runtime: ctx.profileRuntime, changes, log }
  209. }
  210. const entryIds = (ctx: Context): string[] => [...ctx.loader.entries()].map(entry => entry.id)
  211. const BUNDLE_ONE_ROW = '- insert:\n - id: hello\n name: cordis:good\n'
  212. describe('PluginManager', () => {
  213. it('publishes the plugins namespace with one direct method per operation', async () => {
  214. const staged = await stageHome()
  215. const { manager } = await bootProfile(staged)
  216. expect(manager.typertRemote).toMatchObject({ serviceKey: 'pluginManager', namespace: 'plugins' })
  217. expect(remoteMethods(manager).map(marker => marker.method)).toEqual([
  218. 'list', 'install', 'uninstall', 'enable', 'disable', 'retry', 'addRow', 'removeRow', 'setRowDisabled', 'dependents',
  219. ])
  220. })
  221. it('reports plugins/unavailable without a profile runtime', async () => {
  222. const ctx = new Context()
  223. contexts.push(ctx)
  224. await ctx.plugin(Loader)
  225. await ctx.plugin(PluginManager, managerConfig())
  226. await expect(ctx.get('pluginManager')?.list()).rejects.toMatchObject({ code: 'plugins/unavailable' })
  227. })
  228. describe('list', () => {
  229. it('folds installed, enabled, and probed facts into one view per package', async () => {
  230. const staged = await stageHome()
  231. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW, plugins: [{ name: './extra.js', title: 'Extra' }], files: { 'extra.js': 'export const name = "extra"\nexport function apply() {}\n' } })
  232. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  233. stagePackage(staged.profileDir, 'ext-plugin', { main: 'export const name = "p"\nexport function apply() {}\n' })
  234. addDependency(staged.profileDir, 'ext-bundle')
  235. addDependency(staged.profileDir, 'ext-lib')
  236. addDependency(staged.profileDir, 'ext-plugin')
  237. const { manager } = await bootProfile(staged)
  238. const views = await manager.list()
  239. expect(views.map(view => [view.name, view.kind, view.status, view.installed, view.enabled, view.trust])).toEqual([
  240. ['ext-bundle', 'bundle', 'disabled', true, false, 'external'],
  241. ['ext-lib', 'library', 'plain', true, false, 'external'],
  242. ['ext-plugin', 'plugin', 'plain', true, false, 'external'],
  243. ])
  244. const bundle = views[0]
  245. expect(bundle).toMatchObject({ version: '1.0.0', title: 'Title of ext-bundle', description: 'staged ext-bundle', stage: 'runtime', liveReload: true })
  246. // Rows come from the probe while the bundle is not composed, prefixed the way the launcher will prefix them.
  247. expect(bundle?.rows).toEqual([{ entryId: 'ext-bundle/hello', originalId: 'hello', moduleName: 'cordis:good', enabled: true, phase: null }])
  248. expect(bundle?.addable).toEqual([{ moduleName: 'ext-bundle/extra.js', declaredName: './extra.js', title: 'Extra', ok: true }])
  249. expect(bundle?.probedAt).toEqual(expect.any(String) as string)
  250. expect(readProbeCache(staged.profileDir, 'ext-bundle', '1.0.0')?.kind).toBe('bundle')
  251. })
  252. it('reads a composed bundle\'s rows from the live tree with their failures', async () => {
  253. const staged = await stageHome()
  254. stagePackage(staged.profileDir, 'ext-mixed', { patch: '- insert:\n - id: ok\n name: cordis:good\n - id: bad\n name: cordis:throws\n' })
  255. addDependency(staged.profileDir, 'ext-mixed')
  256. const manifest = manifestOf(staged.profileDir)
  257. manifest.dsh.profile.bundles.push('ext-mixed')
  258. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  259. const { manager } = await bootProfile(staged)
  260. const [view] = await manager.list()
  261. expect(view).toMatchObject({ name: 'ext-mixed', status: 'partial', enabled: true })
  262. expect(view?.reason).toContain('boom at apply')
  263. expect(view?.rows.map(row => [row.entryId, row.phase, row.failure?.stage])).toEqual([
  264. ['include:ext-mixed/ok', 'active', undefined],
  265. ['include:ext-mixed/bad', 'failed', 'apply'],
  266. ])
  267. })
  268. it('reports a package the probe refuses as not enableable, and a stale layer as restart-required', async () => {
  269. const staged = await stageHome('startup')
  270. stagePackage(staged.profileDir, 'ext-broken', { patch: BUNDLE_ONE_ROW, main: 'throw new Error("no import for you")\n' })
  271. addDependency(staged.profileDir, 'ext-broken')
  272. stagePackage(staged.profileDir, 'ext-later', { patch: BUNDLE_ONE_ROW })
  273. addDependency(staged.profileDir, 'ext-later')
  274. const { manager } = await bootProfile(staged)
  275. // Enabled after boot on a startup-reload profile: the manifest says yes, the tree says no.
  276. const manifest = manifestOf(staged.profileDir)
  277. manifest.dsh.profile.bundles.push('ext-later')
  278. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  279. const views = await manager.list()
  280. expect(views.find(view => view.name === 'ext-broken')).toMatchObject({ status: 'not-enableable', reason: expect.stringContaining('no import for you') as string })
  281. expect(views.find(view => view.name === 'ext-later')).toMatchObject({ status: 'restart-required', liveReload: false })
  282. })
  283. it('reports a package whose probe cannot run with the probe\'s failure', async () => {
  284. const staged = await stageHome()
  285. stagePackage(staged.profileDir, 'ext-odd', { patch: BUNDLE_ONE_ROW })
  286. addDependency(staged.profileDir, 'ext-odd')
  287. stagePackage(staged.profileDir, 'ext-refused', { patch: BUNDLE_ONE_ROW })
  288. addDependency(staged.profileDir, 'ext-refused')
  289. const { manager } = await bootProfile(staged, {
  290. probe: ({ packageName }) => packageName === 'ext-odd'
  291. ? Promise.reject(new Error('probe exploded'))
  292. : Promise.resolve({ packageName, kind: 'bundle', ok: false, reason: 'foreign cordis', cordisSameCopy: false, rows: [], overrides: [], addable: [], checkedAt: 'now' }),
  293. })
  294. const views = await manager.list()
  295. expect(views.find(view => view.name === 'ext-odd')).toMatchObject({ status: 'not-enableable', reason: 'probe exploded', cordisSameCopy: null, rows: [] })
  296. expect(views.find(view => view.name === 'ext-refused')).toMatchObject({ status: 'not-enableable', reason: 'foreign cordis', cordisSameCopy: false })
  297. await expect(manager.enable('ext-odd')).rejects.toMatchObject({ code: 'plugins/not-enableable', details: { reason: 'cannot be probed: probe exploded' } })
  298. await expect(manager.enable('ext-refused')).rejects.toMatchObject({ code: 'plugins/not-enableable', details: { reason: 'foreign cordis' } })
  299. })
  300. it('reads a hand-written manifest: no dependencies, a template bundle, a ghost, and a builtin layer added after boot', async () => {
  301. const staged = await stageHome('startup')
  302. stagePackage(staged.profileDir, 'tpl', { patch: BUNDLE_ONE_ROW })
  303. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', dsh: { profile: { bundles: ['tpl'], patchReload: 'startup' } } }))
  304. const { manager } = await bootProfile(staged)
  305. expect((await manager.list()).map(view => [view.name, view.trust, view.status, view.installed, view.rows.length])).toEqual([['tpl', 'builtin', 'running', false, 1]])
  306. // A manifest with no dsh section at all knows no bundles.
  307. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web' }))
  308. expect(await manager.list()).toEqual([])
  309. // Bundles the manifest names after boot: one staged, one that resolves to nothing.
  310. stagePackage(staged.profileDir, 'tpl-later', { patch: BUNDLE_ONE_ROW })
  311. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', dsh: { profile: { bundles: ['tpl', 'tpl-later', 'ghost'], patchReload: 'startup' } } }))
  312. const views = await manager.list()
  313. expect(views.map(view => [view.name, view.trust, view.kind, view.status])).toEqual([
  314. ['tpl', 'builtin', 'bundle', 'running'],
  315. ['tpl-later', 'builtin', 'bundle', 'restart-required'],
  316. ['ghost', 'builtin', 'library', 'plain'],
  317. ])
  318. await expect(manager.uninstall('ghost')).rejects.toMatchObject({ code: 'plugins/not-installed' })
  319. // Retrying takes the bundle out first, which a template bundle refuses.
  320. await expect(manager.retry('ghost')).rejects.toMatchObject({ code: 'gateway/bad-request' })
  321. })
  322. it('folds disabled, user-disabled, and waiting rows', async () => {
  323. const staged = await stageHome()
  324. stagePackage(staged.profileDir, 'ext-off', { patch: '- insert:\n - id: a\n name: cordis:good\n disabled: true\n - id: b\n name: cordis:good\n' })
  325. addDependency(staged.profileDir, 'ext-off')
  326. stagePackage(staged.profileDir, 'ext-waiting', { patch: '- insert:\n - id: w\n name: cordis:consumer\n' })
  327. addDependency(staged.profileDir, 'ext-waiting')
  328. const { manager } = await bootProfile(staged)
  329. await manager.enable('ext-off')
  330. await manager.enable('ext-waiting')
  331. await manager.setRowDisabled({ kind: 'global' }, 'ext-off/b', true)
  332. const views = await manager.list()
  333. const off = views.find(view => view.name === 'ext-off')
  334. expect(off).toMatchObject({ status: 'running' })
  335. expect(off?.rows.map(row => [row.originalId, row.enabled, row.disabledBy, row.phase])).toEqual([
  336. ['a', false, 'composition', null],
  337. ['b', false, 'user', null],
  338. ])
  339. await manager.setRowDisabled({ kind: 'global' }, 'ext-off/b', true)
  340. const waiting = views.find(view => view.name === 'ext-waiting')
  341. expect(waiting).toMatchObject({ status: 'failed', reason: expect.stringContaining('fixtureSvc') as string })
  342. expect(waiting?.rows).toEqual([expect.objectContaining({ entryId: 'include:ext-waiting/w', phase: 'pending', failure: expect.objectContaining({ stage: 'inject-pending' }) as object })])
  343. // Another bundle's recorded failure is not this one's row.
  344. expect(off?.rows.some(row => row.failure !== undefined)).toBe(false)
  345. })
  346. it('reads anonymous and gated probe rows, unprefixed for a first-party package', async () => {
  347. const staged = await stageHome()
  348. stagePackage(staged.profileDir, 'ext-anon', { patch: '- insert:\n - name: cordis:good\n - id: gated\n name: cordis:good\n disabled: true\n' })
  349. addDependency(staged.profileDir, 'ext-anon')
  350. stagePackage(staged.profileDir, 'fp-bundle', { patch: BUNDLE_ONE_ROW })
  351. addDependency(staged.profileDir, 'fp-bundle')
  352. const manifest = JSON.parse(readFileSync(join(staged.profileDir, 'package.json'), 'utf8')) as { dsh: { profile: Record<string, unknown> } }
  353. manifest.dsh.profile.firstParty = ['fp-bundle']
  354. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest))
  355. const { manager } = await bootProfile(staged)
  356. const views = await manager.list()
  357. expect(views.find(view => view.name === 'ext-anon')?.rows).toEqual([
  358. { entryId: 'cordis:good', moduleName: 'cordis:good', enabled: true, phase: null },
  359. { entryId: 'ext-anon/gated', originalId: 'gated', moduleName: 'cordis:good', enabled: false, disabledBy: 'composition', phase: null },
  360. ])
  361. await manager.enable('fp-bundle')
  362. const firstParty = (await manager.list()).find(view => view.name === 'fp-bundle')
  363. expect(firstParty).toMatchObject({ trust: 'builtin', status: 'running' })
  364. expect(firstParty?.rows.map(row => row.entryId)).toEqual(['include:hello'])
  365. await manager.disable('fp-bundle')
  366. expect((await manager.list()).find(view => view.name === 'fp-bundle')?.rows.map(row => row.entryId)).toEqual(['hello'])
  367. })
  368. })
  369. describe('install', () => {
  370. it('runs pnpm add, records the dependency, probes the package, and leaves it disabled', async () => {
  371. const staged = await stageHome()
  372. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  373. const calls: string[][] = []
  374. const { manager, changes, log } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  375. const result = await manager.install('github:acme/ext-new')
  376. expect(calls).toEqual([['pnpm', 'add', 'github:acme/ext-new']])
  377. // The fake pnpm records the spec itself as the dependency name, which
  378. // resolves to nothing: a plain dependency whose probe cannot run.
  379. expect(result).toEqual({ installed: ['github:acme/ext-new'], enabled: [], installedOnly: [], plain: ['github:acme/ext-new'], jobId: expect.any(String) as string })
  380. expect(log.map(chunk => [chunk.stream, chunk.text, chunk.exitCode])).toEqual([
  381. ['stdout', '+ github:acme/ext-new 1.0.0\n', undefined],
  382. ['stdout', '', 0],
  383. ])
  384. expect(changes).toEqual([{ reason: 'install' }])
  385. })
  386. it('reconciles by the installed name, and enables the new bundle when asked', async () => {
  387. const staged = await stageHome()
  388. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  389. const { ctx, manager, changes } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  390. const result = await manager.install('ext-new', { enable: true })
  391. expect(result).toMatchObject({ installed: ['ext-new'], enabled: ['ext-new'], installedOnly: [], plain: [] })
  392. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['ext-new'])
  393. expect(entryIds(ctx)).toEqual(expect.arrayContaining(['include:bundle/ext-new', 'include:ext-new/hello']))
  394. expect(readProbeCache(staged.profileDir, 'ext-new')).toBeDefined()
  395. expect(changes.map(change => change.reason)).toEqual(['enable', 'install'])
  396. expect((await manager.list()).find(view => view.name === 'ext-new')?.status).toBe('running')
  397. })
  398. it('reports a plain dependency and a non-zero exit with the log tail', async () => {
  399. const staged = await stageHome()
  400. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  401. const { manager } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir) })
  402. expect(await manager.install('ext-lib')).toMatchObject({ installed: ['ext-lib'], plain: ['ext-lib'], installedOnly: [] })
  403. expect((await manager.list()).find(view => view.name === 'ext-lib')?.status).toBe('plain')
  404. await manager.uninstall('ext-lib')
  405. expect((await manager.list()).some(view => view.name === 'ext-lib')).toBe(false)
  406. })
  407. it('installs into a manifest that declares no dependencies yet', async () => {
  408. const staged = await stageHome()
  409. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', dsh: { profile: { bundles: [], patchReload: 'live' } } }))
  410. stagePackage(staged.profileDir, 'ext-new', { patch: BUNDLE_ONE_ROW })
  411. const { manager } = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, () => {
  412. const path = join(staged.profileDir, 'package.json')
  413. const manifest = JSON.parse(readFileSync(path, 'utf8')) as Record<string, unknown>
  414. writeFileSync(path, JSON.stringify({ ...manifest, dependencies: { 'ext-new': '1.0.0' } }))
  415. return { code: 0 }
  416. }) })
  417. expect(await manager.install('ext-new')).toMatchObject({ installed: ['ext-new'], installedOnly: ['ext-new'] })
  418. })
  419. it('fails loud on a non-zero exit, a spawn error, a timeout, and an empty spec', async () => {
  420. const staged = await stageHome()
  421. const exits = await bootProfile(staged, { spawn: fakePnpm(staged.profileDir, () => ({ code: 1, stderr: 'ERR_PNPM_NO_MATCHING_VERSION\n' })) })
  422. await expect(exits.manager.install('nope')).rejects.toMatchObject({
  423. code: 'plugins/install-failed', details: { spec: 'nope', exitCode: 1, log: 'ERR_PNPM_NO_MATCHING_VERSION\n' },
  424. })
  425. expect(exits.log.at(-1)).toMatchObject({ exitCode: 1 })
  426. await expect(exits.manager.install(' ')).rejects.toMatchObject({ code: 'gateway/bad-request' })
  427. const erroringHome = await stageHome()
  428. const erroring = await bootProfile(erroringHome, { spawn: fakePnpm(erroringHome.profileDir, () => ({ code: null, error: 'spawn pnpm ENOENT' })) })
  429. await expect(erroring.manager.install('x')).rejects.toMatchObject({ code: 'plugins/install-failed', details: { exitCode: null } })
  430. expect(erroring.log.some(chunk => chunk.text.includes('ENOENT'))).toBe(true)
  431. const hangingHome = await stageHome()
  432. const hanging = await bootProfile(hangingHome, { spawn: fakePnpm(hangingHome.profileDir, () => ({ code: null, hang: true })) })
  433. await expect(hanging.manager.install('x')).rejects.toMatchObject({ code: 'plugins/install-failed' })
  434. expect(hanging.log.some(chunk => chunk.text.includes('timed out'))).toBe(true)
  435. })
  436. it('keeps only the tail of a long log in the failure', async () => {
  437. const staged = await stageHome()
  438. const { manager } = await bootProfile(staged, {
  439. spawn: fakePnpm(staged.profileDir, () => ({ code: 2, stdout: 'a'.repeat(300), stderr: 'b'.repeat(300) })),
  440. }, { installLogTailBytes: 256 })
  441. await expect(manager.install('x')).rejects.toMatchObject({ details: { log: 'b'.repeat(300) } })
  442. })
  443. })
  444. describe('enable, disable, and retry', () => {
  445. it('composes an installed bundle live, and takes it out again', async () => {
  446. const staged = await stageHome()
  447. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  448. addDependency(staged.profileDir, 'ext-bundle')
  449. const { ctx, manager, changes } = await bootProfile(staged)
  450. expect(await manager.enable('ext-bundle')).toEqual({ changed: true, effect: 'live' })
  451. expect(entryIds(ctx)).toContain('include:ext-bundle/hello')
  452. expect(await manager.enable('ext-bundle')).toEqual({ changed: false, effect: 'live' })
  453. expect(await manager.disable('ext-bundle')).toEqual({ changed: true, effect: 'live' })
  454. expect(entryIds(ctx)).not.toContain('include:ext-bundle/hello')
  455. expect(await manager.disable('ext-bundle')).toEqual({ changed: false, effect: 'live' })
  456. expect(changes.map(change => [change.reason, change.packageName])).toEqual([
  457. ['enable', 'ext-bundle'], ['enable', 'ext-bundle'], ['disable', 'ext-bundle'], ['disable', 'ext-bundle'],
  458. ])
  459. })
  460. it('only writes the manifest on a startup-reload profile', async () => {
  461. const staged = await stageHome('startup')
  462. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW })
  463. addDependency(staged.profileDir, 'ext-bundle')
  464. const { ctx, manager } = await bootProfile(staged)
  465. expect(await manager.enable('ext-bundle')).toEqual({ changed: true, effect: 'restart' })
  466. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['ext-bundle'])
  467. expect(entryIds(ctx)).not.toContain('include:ext-bundle/hello')
  468. expect(await manager.disable('ext-bundle')).toEqual({ changed: true, effect: 'restart' })
  469. })
  470. it('refuses what cannot be enabled or disabled', async () => {
  471. const staged = await stageHome()
  472. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  473. addDependency(staged.profileDir, 'ext-lib')
  474. stagePackage(staged.profileDir, 'ext-broken', { patch: BUNDLE_ONE_ROW, main: 'throw new Error("no import for you")\n' })
  475. addDependency(staged.profileDir, 'ext-broken')
  476. const { manager } = await bootProfile(staged)
  477. await expect(manager.enable('absent')).rejects.toMatchObject({ code: 'plugins/not-installed' })
  478. await expect(manager.enable('ext-lib')).rejects.toMatchObject({ code: 'plugins/not-enableable', details: { reason: expect.stringContaining('declares no dsh.bundle') as string } })
  479. await expect(manager.enable('ext-broken')).rejects.toMatchObject({ code: 'plugins/not-enableable', details: { reason: expect.stringContaining('no import for you') as string } })
  480. // A template bundle is not a dependency and cannot be disabled.
  481. const manifest = manifestOf(staged.profileDir)
  482. manifest.dsh.profile.bundles.push('template')
  483. writeFileSync(join(staged.profileDir, 'package.json'), JSON.stringify(manifest, null, 2))
  484. await expect(manager.disable('template')).rejects.toMatchObject({ code: 'gateway/bad-request' })
  485. await expect(manager.retry('ext-lib')).rejects.toMatchObject({ code: 'gateway/bad-request' })
  486. })
  487. it('restores the layer list when the tree rejects a boot-stage bundle', async () => {
  488. const staged = await stageHome()
  489. stagePackage(staged.profileDir, 'ext-fatal', { patch: '- insert:\n - id: bad\n name: cordis:throws\n', stage: 'boot' })
  490. addDependency(staged.profileDir, 'ext-fatal')
  491. stagePackage(staged.profileDir, 'ext-fine', { patch: BUNDLE_ONE_ROW })
  492. addDependency(staged.profileDir, 'ext-fine')
  493. const { ctx, manager } = await bootProfile(staged)
  494. await manager.enable('ext-fine')
  495. await expect(manager.enable('ext-fatal')).rejects.toMatchObject({
  496. code: 'plugins/enable-failed', details: { packageName: 'ext-fatal', reason: expect.stringContaining('boom at apply') as string },
  497. })
  498. expect(manifestOf(staged.profileDir).dsh.profile.bundles).toEqual(['ext-fine'])
  499. expect(entryIds(ctx)).toContain('include:ext-fine/hello')
  500. expect(entryIds(ctx)).not.toContain('include:bad')
  501. })
  502. it('retries an isolated bundle by composing it again', async () => {
  503. const staged = await stageHome()
  504. stagePackage(staged.profileDir, 'ext-flaky', { patch: '- insert:\n - id: once\n name: cordis:flaky\n' })
  505. addDependency(staged.profileDir, 'ext-flaky')
  506. flakyCalls = 0
  507. const { manager, changes } = await bootProfile(staged)
  508. await manager.enable('ext-flaky')
  509. expect((await manager.list())[0]).toMatchObject({ status: 'failed', reason: expect.stringContaining('flaky first start') as string })
  510. expect(await manager.retry('ext-flaky')).toEqual({ changed: true, effect: 'live' })
  511. expect((await manager.list())[0]).toMatchObject({ status: 'running' })
  512. expect(changes.map(change => change.reason)).toEqual(['enable', 'disable', 'enable', 'retry'])
  513. })
  514. })
  515. describe('rows in user layers', () => {
  516. it('adds, disables, re-enables, and removes a row in the live global layer', async () => {
  517. const staged = await stageHome()
  518. stagePackage(staged.profileDir, '@acme/ext-plugin', { main: 'export const name = "p"\nexport function apply() {}\n' })
  519. addDependency(staged.profileDir, '@acme/ext-plugin')
  520. const { ctx, manager, changes } = await bootProfile(staged)
  521. const added = await manager.addRow('@acme/ext-plugin', { kind: 'global' })
  522. expect(added).toEqual({ target: { kind: 'global' }, rowId: 'acme/ext-plugin', file: join(staged.profileDir, 'cordis.patch.yml') })
  523. expect(readFileSync(added.file, 'utf8')).toBe('- insert:\n - id: acme/ext-plugin\n name: "@acme/ext-plugin"\n config: {}\n')
  524. // Composed live: the row is in the tree, though its module cannot import from the temp home.
  525. expect(entryIds(ctx)).toContain('include:acme/ext-plugin')
  526. await expect(manager.addRow('@acme/ext-plugin', { kind: 'global' })).rejects.toMatchObject({ code: 'plugins/row-conflict' })
  527. await manager.setRowDisabled({ kind: 'global' }, 'acme/ext-plugin', true)
  528. expect(ctx.loader.resolve('include:acme/ext-plugin')?.disabled).toBe(true)
  529. expect(readFileSync(added.file, 'utf8')).toContain('- id: acme/ext-plugin\n disabled: true\n')
  530. await manager.setRowDisabled({ kind: 'global' }, 'acme/ext-plugin', false)
  531. expect(ctx.loader.resolve('include:acme/ext-plugin')?.disabled).toBe(false)
  532. expect(readFileSync(added.file, 'utf8')).not.toContain('disabled')
  533. await manager.removeRow({ kind: 'global' }, 'acme/ext-plugin')
  534. expect(entryIds(ctx)).not.toContain('include:acme/ext-plugin')
  535. await expect(manager.removeRow({ kind: 'global' }, 'acme/ext-plugin')).rejects.toMatchObject({ code: 'gateway/bad-request' })
  536. expect(changes.map(change => change.reason)).toEqual(['row', 'row', 'row', 'row'])
  537. })
  538. it('detects a conflict through the layer file when the tree was not recomposed', async () => {
  539. const staged = await stageHome('startup')
  540. stagePackage(staged.profileDir, 'ext-plugin', { main: 'export const name = "p"\nexport function apply() {}\n' })
  541. addDependency(staged.profileDir, 'ext-plugin')
  542. const { ctx, manager } = await bootProfile(staged)
  543. await manager.addRow('ext-plugin', { kind: 'global' })
  544. expect(entryIds(ctx)).not.toContain('include:ext-plugin')
  545. await expect(manager.addRow('ext-plugin', { kind: 'global' })).rejects.toMatchObject({ code: 'plugins/row-conflict' })
  546. })
  547. it('adds a declared addable module with its default config and an explicit id, and refuses the rest', async () => {
  548. const staged = await stageHome()
  549. stagePackage(staged.profileDir, 'ext-bundle', {
  550. patch: BUNDLE_ONE_ROW,
  551. plugins: [
  552. { name: './tools/sql.js', title: 'SQL', config: { dsn: 'sqlite://' } },
  553. { name: './missing.js' },
  554. ],
  555. files: { 'tools/sql.js': 'export const name = "sql"\nexport function apply() {}\n' },
  556. })
  557. addDependency(staged.profileDir, 'ext-bundle')
  558. const { manager } = await bootProfile(staged)
  559. const added = await manager.addRow('ext-bundle', { kind: 'global' }, { module: './tools/sql.js', id: 'sql' })
  560. expect(added.rowId).toBe('sql')
  561. expect(readFileSync(added.file, 'utf8')).toContain('- id: sql\n name: ext-bundle/tools/sql.js\n config:\n dsn: sqlite://\n')
  562. await expect(manager.addRow('ext-bundle', { kind: 'global' })).rejects.toMatchObject({ code: 'plugins/not-enableable' })
  563. await expect(manager.addRow('ext-bundle', { kind: 'global' }, { module: './missing.js' })).rejects.toMatchObject({ code: 'plugins/not-enableable' })
  564. await expect(manager.addRow('absent', { kind: 'global' })).rejects.toMatchObject({ code: 'plugins/not-installed' })
  565. })
  566. it('writes a preset\'s layer through the roster, and refuses a preset target without one', async () => {
  567. const staged = await stageHome()
  568. stagePackage(staged.profileDir, 'ext-plugin', { main: 'export const name = "p"\nexport function apply() {}\n' })
  569. addDependency(staged.profileDir, 'ext-plugin')
  570. const { ctx, manager } = await bootProfile(staged)
  571. await expect(manager.addRow('ext-plugin', { kind: 'preset', preset: 'standard' })).rejects.toMatchObject({ code: 'plugins/unavailable' })
  572. const overlay = join(staged.home, '.agent-presets', 'standard', 'cordis.patch.yml')
  573. const roster = {
  574. overlayPathFor: (id: string) => Promise.resolve(join(staged.home, '.agent-presets', id, 'cordis.patch.yml')),
  575. compositionInventory: () => Promise.resolve([{ id: 'standard', rows: [{ entryId: 'tool-web' }] }]),
  576. list: () => Promise.resolve([{ id: 'standard', overlayPath: existsSync(overlay) ? overlay : undefined }]),
  577. }
  578. ctx.provide('agentPresets', roster)
  579. // No layer exists yet: nothing references the package.
  580. expect((await manager.dependents('ext-plugin')).references).toEqual([])
  581. // A preset the inventory does not know has no rows to conflict with.
  582. const elsewhere = await manager.addRow('ext-plugin', { kind: 'preset', preset: 'other' })
  583. expect(elsewhere.file).toBe(join(staged.home, '.agent-presets', 'other', 'cordis.patch.yml'))
  584. const added = await manager.addRow('ext-plugin', { kind: 'preset', preset: 'standard' })
  585. expect(added).toEqual({ target: { kind: 'preset', preset: 'standard' }, rowId: 'ext-plugin', file: overlay })
  586. await expect(manager.addRow('ext-plugin', { kind: 'preset', preset: 'standard' }, { id: 'tool-web' })).rejects.toMatchObject({ code: 'plugins/row-conflict' })
  587. await manager.setRowDisabled({ kind: 'preset', preset: 'standard' }, 'tool-web', true)
  588. expect(readFileSync(overlay, 'utf8')).toBe('- insert:\n - id: ext-plugin\n name: ext-plugin\n config: {}\n- id: tool-web\n disabled: true\n')
  589. // The preset's layer is not part of the host tree.
  590. expect(entryIds(ctx)).not.toContain('include:ext-plugin')
  591. const dependents = await manager.dependents('ext-plugin')
  592. expect(dependents.references).toEqual([{ target: { kind: 'preset', preset: 'standard' }, rowId: 'ext-plugin', moduleName: 'ext-plugin' }])
  593. await manager.removeRow({ kind: 'preset', preset: 'standard' }, 'ext-plugin')
  594. expect(readFileSync(overlay, 'utf8')).toBe('- id: tool-web\n disabled: true\n')
  595. })
  596. })
  597. describe('dependents and uninstall', () => {
  598. it('names the services other rows inject and the user-layer rows that reference the package', async () => {
  599. const staged = await stageHome()
  600. // Beside the provider: a row nobody injects, and a row switched off (no fiber to read).
  601. stagePackage(staged.profileDir, 'ext-provider', { patch: '- insert:\n - id: svc\n name: cordis:provider\n - id: lonely\n name: cordis:lonely-provider\n - id: off\n name: cordis:good\n disabled: true\n' })
  602. addDependency(staged.profileDir, 'ext-provider')
  603. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), [
  604. '- insert:',
  605. ' - name: ext-provider/anonymous.js',
  606. ' disabled: true',
  607. ' - id: ref',
  608. ' name: ext-provider/tools/x.js',
  609. ' disabled: true',
  610. ' - id: grp',
  611. ' name: cordis:group',
  612. ' group: true',
  613. ' config:',
  614. ' - id: nested-ref',
  615. ' name: ext-provider',
  616. ' disabled: true',
  617. '',
  618. ].join('\n'))
  619. const { manager, runtime } = await bootProfile(staged)
  620. await manager.enable('ext-provider')
  621. // A built-in row injecting the bundle's service, composed once the
  622. // provider is up (a boot would refuse a row left waiting).
  623. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), `${readFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'utf8')}- insert:\n - id: needs-svc\n name: cordis:consumer\n`)
  624. await runtime.recompose()
  625. const dependents = await manager.dependents('ext-provider')
  626. expect(dependents.services).toEqual([{ service: 'fixtureSvc', providedBy: 'include:ext-provider/svc', injectedBy: ['include:needs-svc'] }])
  627. expect(dependents.references.map(reference => reference.rowId)).toEqual(['ref', 'nested-ref'])
  628. expect(dependents.references[0]).toEqual({ target: { kind: 'global' }, rowId: 'ref', moduleName: 'ext-provider/tools/x.js' })
  629. })
  630. it('ignores an unreadable user layer while collecting references', async () => {
  631. const staged = await stageHome()
  632. stagePackage(staged.profileDir, 'ext-lib', { main: 'export const x = 1\n' })
  633. addDependency(staged.profileDir, 'ext-lib')
  634. const { manager } = await bootProfile(staged)
  635. writeFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'a: [\n')
  636. expect((await manager.dependents('ext-lib')).references).toEqual([])
  637. })
  638. it('disables, drops references, removes the package, and forgets its probe', async () => {
  639. const staged = await stageHome()
  640. stagePackage(staged.profileDir, 'ext-bundle', { patch: BUNDLE_ONE_ROW, plugins: [{ name: './extra.js' }], files: { 'extra.js': 'export function apply() {}\n' } })
  641. addDependency(staged.profileDir, 'ext-bundle')
  642. const calls: string[][] = []
  643. const { ctx, manager, changes } = await bootProfile(staged, { spawn: recordingPnpm(staged.profileDir, calls) })
  644. await manager.enable('ext-bundle')
  645. await manager.addRow('ext-bundle', { kind: 'global' }, { module: './extra.js' })
  646. expect(entryIds(ctx)).toEqual(expect.arrayContaining(['include:ext-bundle/hello', 'include:ext-bundle/extra.js']))
  647. expect(existsSync(join(staged.profileDir, '.dsh-plugins', 'ext-bundle.json'))).toBe(true)
  648. await manager.uninstall('ext-bundle')
  649. expect(calls).toEqual([['pnpm', 'remove', 'ext-bundle']])
  650. expect(manifestOf(staged.profileDir)).toMatchObject({ dependencies: {}, dsh: { profile: { bundles: [] } } })
  651. expect(entryIds(ctx)).not.toContain('include:ext-bundle/hello')
  652. expect(entryIds(ctx)).not.toContain('include:ext-bundle/extra.js')
  653. expect(readFileSync(join(staged.profileDir, 'cordis.patch.yml'), 'utf8')).toBe('[]\n')
  654. expect(existsSync(join(staged.profileDir, '.dsh-plugins', 'ext-bundle.json'))).toBe(false)
  655. expect(changes.map(change => change.reason)).toEqual(['enable', 'row', 'disable', 'uninstall'])
  656. await expect(manager.uninstall('ext-bundle')).rejects.toMatchObject({ code: 'plugins/not-installed' })
  657. })
  658. })
  659. })
  660. describe('RemoteError codes', () => {
  661. it('declare their details', () => {
  662. const error = new RemoteError('plugins/row-conflict', 'taken', { rowId: 'x', target: { kind: 'global' } })
  663. expect(error.details).toEqual({ rowId: 'x', target: { kind: 'global' } })
  664. })
  665. })