feat(plugin-inventory): offline bundle and pnpm plugin install for the desktop

Adds an install surface to the plugin-inventory gateway: availableBundles lists
the curated offline-installable optional bundles (AVAILABLE_BUNDLES); install
composes an offline bundle into the profile's dsh.profile.bundles, or for a
registry spec runs pnpm against the writable profile via the bundled Node and a
vendored pnpm (gated behind the dshAllowPluginInstall context flag, set only by
the desktop boot); uninstall removes a bundle layer. The reconcile logic from
`dsh plugin add` moves into app-boot as shared helpers. The desktop vendored
pnpm into the harness and sets the allow-install env; the plugin-list SPA gains
an installable-bundles section. Tests cover the guard, install helpers, and the
SPA section at 100% host coverage.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Pine
2026-08-14 18:46:39 +08:00
parent 8f1c764614
commit 6cd7c5a590
25 changed files with 929 additions and 110 deletions
@@ -0,0 +1,146 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { readProfileManifest } from '@deepseek-ai/dsh-app-boot'
import { composeOfflineBundle, resolvePnpm, runPnpmInstall, uninstallBundle } from '../src/install.ts'
const dirs: string[] = []
function makeProfile(bundles: string[] = []): string {
const dir = mkdtempSync(join(tmpdir(), 'dsh-install-'))
dirs.push(dir)
writeFileSync(
join(dir, 'package.json'),
JSON.stringify({ name: 'dsh-profile-test', dsh: { profile: { bundles } } }, undefined, 2),
)
return dir
}
/** Make a bundle resolvable from a profile dir's node_modules. */
function makeBundle(dir: string, name: string): void {
const pkgDir = join(dir, 'node_modules', name)
mkdirSync(pkgDir, { recursive: true })
writeFileSync(join(pkgDir, 'package.json'), JSON.stringify({
name,
dsh: { bundle: { patch: './cordis.patch.yml' } },
}))
writeFileSync(join(pkgDir, 'cordis.patch.yml'), '[]\n')
}
/** A fake pnpm CLI that exits with the given code. */
function makeFakePnpm(dir: string, exitCode: number): string {
const file = join(dir, `pnpm-${exitCode}.cjs`)
writeFileSync(file, `process.exit(${exitCode})\n`)
return file
}
afterEach(() => {
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true })
})
describe('composeOfflineBundle', () => {
it('appends a resolvable bundle and stays idempotent', () => {
const dir = makeProfile()
makeBundle(dir, 'example-bundle')
composeOfflineBundle('dsh', dir, join(dir, 'package.json'), 'example-bundle')
expect(readProfileManifest('dsh', dir).dsh?.profile?.bundles).toEqual(['example-bundle'])
composeOfflineBundle('dsh', dir, join(dir, 'package.json'), 'example-bundle')
expect(readProfileManifest('dsh', dir).dsh?.profile?.bundles).toEqual(['example-bundle'])
})
it('fails loud for an unresolvable bundle', () => {
const dir = makeProfile()
expect(() => { composeOfflineBundle('dsh', dir, join(dir, 'package.json'), 'missing') }).toThrow(/cannot resolve/)
})
})
describe('uninstallBundle', () => {
it('removes a bundle from the layer list', () => {
const dir = makeProfile(['example-bundle'])
uninstallBundle('dsh', dir, 'example-bundle')
expect(readProfileManifest('dsh', dir).dsh?.profile?.bundles).toEqual([])
})
})
describe('runPnpmInstall', () => {
it('reconciles without throwing on pnpm success', () => {
const dir = makeProfile()
const before = readProfileManifest('dsh', dir)
const pnpm = makeFakePnpm(dir, 0)
runPnpmInstall({
binName: 'dsh', profileDir: dir, installAnchor: join(dir, 'package.json'),
nodeBin: process.execPath, pnpmCjs: pnpm, spec: 'example', before,
})
})
it('throws when pnpm fails', () => {
const dir = makeProfile()
const before = readProfileManifest('dsh', dir)
const pnpm = makeFakePnpm(dir, 1)
expect(() => { runPnpmInstall({
binName: 'dsh', profileDir: dir, installAnchor: join(dir, 'package.json'),
nodeBin: process.execPath, pnpmCjs: pnpm, spec: 'example', before,
}) }).toThrow(/pnpm install failed/)
})
})
describe('composeOfflineBundle with a bare manifest', () => {
it('initializes an absent bundle list', () => {
const dir = makeProfile()
rmSync(join(dir, 'package.json'))
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'dsh-profile-test' }))
makeBundle(dir, 'example-bundle')
composeOfflineBundle('dsh', dir, join(dir, 'package.json'), 'example-bundle')
expect(readProfileManifest('dsh', dir).dsh?.profile?.bundles).toEqual(['example-bundle'])
})
})
describe('uninstallBundle with a non-present bundle', () => {
it('leaves the list unchanged', () => {
const dir = makeProfile(['other'])
uninstallBundle('dsh', dir, 'absent')
expect(readProfileManifest('dsh', dir).dsh?.profile?.bundles).toEqual(['other'])
})
it('handles an absent bundle list', () => {
const dir = makeProfile()
rmSync(join(dir, 'package.json'))
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'dsh-profile-test' }))
uninstallBundle('dsh', dir, 'absent')
expect(readProfileManifest('dsh', dir).dsh?.profile?.bundles).toEqual([])
})
})
describe('runPnpmInstall spawn failure', () => {
it('throws the spawn error when node cannot start', () => {
const dir = makeProfile()
const before = readProfileManifest('dsh', dir)
expect(() => { runPnpmInstall({
binName: 'dsh', profileDir: dir, installAnchor: join(dir, 'package.json'),
nodeBin: '/nonexistent/node', pnpmCjs: '/x/pnpm.cjs', spec: 'x', before,
}) }).toThrow()
})
})
describe('resolvePnpm', () => {
it('honors a DSH_PNPM override', () => {
expect(resolvePnpm('/x/bin/node', { DSH_PNPM: '/vendored/pnpm.cjs' })).toBe('/vendored/pnpm.cjs')
})
it('derives the vendored pnpm beside the node harness root and misses when absent', () => {
expect(resolvePnpm('/nonexistent/bin/node', {})).toBeUndefined()
})
it('finds a vendored pnpm beside the node harness root', () => {
const dir = makeProfile()
const harnessRoot = join(dir, 'harness')
const pnpmDir = join(harnessRoot, 'pnpm', 'node_modules', 'pnpm', 'bin')
mkdirSync(pnpmDir, { recursive: true })
writeFileSync(join(pnpmDir, 'pnpm.cjs'), '')
const nodeBin = join(harnessRoot, 'bin', 'node')
mkdirSync(join(harnessRoot, 'bin'), { recursive: true })
writeFileSync(nodeBin, '')
expect(resolvePnpm(nodeBin, {})).toBe(join(pnpmDir, 'pnpm.cjs'))
})
})
@@ -1,6 +1,11 @@
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { afterEach, describe, expect, it } from 'vitest'
import { Context, type Plugin } from '@deepseek-ai/cordis'
import Loader from '@deepseek-ai/cordis-plugin-loader'
import { readProfileManifest } from '@deepseek-ai/dsh-app-boot'
import { remoteMethods } from '@deepseek-ai/dsh-typert-protocol'
import PluginInventoryGateway, { type PluginEntryId } from '../src/index.ts'
@@ -34,7 +39,7 @@ async function harness(): Promise<{
}
describe('PluginInventoryGateway', () => {
it('publishes one direct list method under the pluginInventory namespace', async () => {
it('publishes direct methods under the pluginInventory namespace', async () => {
const { inventory } = await harness()
expect(inventory.typertRemote).toMatchObject({
serviceKey: 'pluginInventory',
@@ -43,6 +48,9 @@ describe('PluginInventoryGateway', () => {
expect(remoteMethods(inventory)).toEqual([
{ method: 'list', invocation: { kind: 'direct' } },
{ method: 'setEnabled', invocation: { kind: 'direct' } },
{ method: 'availableBundles', invocation: { kind: 'direct' } },
{ method: 'install', invocation: { kind: 'direct' } },
{ method: 'uninstall', invocation: { kind: 'direct' } },
])
})
@@ -117,4 +125,161 @@ describe('PluginInventoryGateway', () => {
await ctx.loader.remove(pendingId)
expect(inventory.list().entries.some(entry => entry.entryId === pendingId)).toBe(false)
})
it('availableBundles reports installed state from the profile manifest', async () => {
const { ctx, inventory } = await harness()
const dir = mkdtempSync(join(tmpdir(), 'dsh-inv-'))
contexts.push(ctx)
ctx.baseUrl = pathToFileURL(dir + '/').href
writeFileSync(join(dir, 'package.json'), JSON.stringify({
name: 'dsh-profile-test',
dsh: { profile: { bundles: ['@deepseek-ai/dsh-image-recognition-bundle'] } },
}))
try {
const snapshot = inventory.availableBundles()
expect(snapshot.available).toEqual([
{ name: '@deepseek-ai/dsh-image-recognition-bundle', installed: true },
])
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('install requires an install anchor and gating for registry specs', async () => {
const { ctx, inventory } = await harness()
const dir = mkdtempSync(join(tmpdir(), 'dsh-inv-'))
ctx.baseUrl = pathToFileURL(dir + '/').href
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'dsh-profile-test', dsh: { profile: { bundles: [] } } }))
try {
expect(() => inventory.install({ type: 'bundle', name: 'x' })).toThrow(/install anchor is unavailable/)
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
expect(() => inventory.install({ type: 'registry', spec: 'x' })).toThrow(/not permitted/)
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('uninstall removes a bundle from the profile manifest', async () => {
const { ctx, inventory } = await harness()
const dir = mkdtempSync(join(tmpdir(), 'dsh-inv-'))
ctx.baseUrl = pathToFileURL(dir + '/').href
writeFileSync(join(dir, 'package.json'), JSON.stringify({
name: 'dsh-profile-test',
dsh: { profile: { bundles: ['@deepseek-ai/dsh-image-recognition-bundle'] } },
}))
try {
expect(inventory.uninstall('@deepseek-ai/dsh-image-recognition-bundle').restartRequired).toBe(true)
expect(inventory.availableBundles().available[0]!.installed).toBe(false)
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('composes an offline bundle when the anchor is available', async () => {
const { ctx, inventory } = await harness()
const dir = mkdtempSync(join(tmpdir(), 'dsh-inv-'))
ctx.baseUrl = pathToFileURL(dir + '/').href
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'dsh-profile-test', dsh: { profile: { bundles: [] } } }))
mkdirSync(join(dir, 'node_modules', 'b'), { recursive: true })
writeFileSync(join(dir, 'node_modules', 'b', 'package.json'), JSON.stringify({ name: 'b', dsh: { bundle: { patch: './cordis.patch.yml' } } }))
writeFileSync(join(dir, 'node_modules', 'b', 'cordis.patch.yml'), '[]\n')
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
try {
inventory.install({ type: 'bundle', name: 'b' })
expect(readProfileManifest('dsh', dir).dsh?.profile?.bundles).toEqual(['b'])
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('installs a registry spec via bundled pnpm when permitted', async () => {
const { ctx, inventory } = await harness()
const dir = mkdtempSync(join(tmpdir(), 'dsh-inv-'))
ctx.baseUrl = pathToFileURL(dir + '/').href
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'dsh-profile-test', dsh: { profile: { bundles: [] } } }))
const fakePnpm = join(dir, 'pnpm.cjs')
writeFileSync(fakePnpm, 'process.exit(0)\n')
process.env.DSH_PNPM = fakePnpm
try {
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
ctx.provide('dshAllowPluginInstall', true)
expect(inventory.install({ type: 'registry', spec: 'some-pkg' }).restartRequired).toBe(true)
} finally {
delete process.env.DSH_PNPM
rmSync(dir, { recursive: true, force: true })
}
})
it('fails loud without a profile directory', async () => {
const { inventory } = await harness()
expect(() => inventory.availableBundles()).toThrow(/profile directory/)
})
it('treats a missing profile manifest as no installed bundles', async () => {
const { ctx, inventory } = await harness()
const dir = mkdtempSync(join(tmpdir(), 'dsh-inv-'))
ctx.baseUrl = pathToFileURL(dir + '/').href
try {
expect(inventory.availableBundles().available[0]!.installed).toBe(false)
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('setEnabled fails loud for an unknown entry', async () => {
const { inventory } = await harness()
await expect(inventory.setEnabled('missing' as PluginEntryId, false)).rejects.toThrow(/cannot resolve entry missing/)
})
it('setEnabled persists the override when anchored to a profile directory', async () => {
const { ctx, inventory } = await harness()
const dir = mkdtempSync(join(tmpdir(), 'dsh-inv-'))
ctx.baseUrl = pathToFileURL(dir + '/').href
try {
const id = await ctx.loader.create({ name: 'cordis:user-toggleable' }) as PluginEntryId
await inventory.setEnabled(id, false)
expect(readFileSync(join(dir, 'cordis.patch.yml'), 'utf8')).toContain('disabled: true')
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('setEnabled reverts an enable whose fiber cannot activate', async () => {
const { ctx, inventory } = await harness()
const id = await ctx.loader.create({ name: 'cordis:pending' }) as PluginEntryId
await expect(inventory.setEnabled(id, true)).rejects.toThrow(/could not start/)
expect(inventory.list().entries.find(entry => entry.entryId === id)?.enabled).toBe(false)
})
it('registry install fails loud when bundled pnpm is absent', async () => {
const { ctx, inventory } = await harness()
const dir = mkdtempSync(join(tmpdir(), 'dsh-inv-'))
ctx.baseUrl = pathToFileURL(dir + '/').href
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'dsh-profile-test', dsh: { profile: { bundles: [] } } }))
delete process.env.DSH_PNPM
try {
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
ctx.provide('dshAllowPluginInstall', true)
expect(() => inventory.install({ type: 'registry', spec: 'x' })).toThrow(/bundled pnpm is unavailable/)
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('registry install fails loud when the profile manifest is missing', async () => {
const { ctx, inventory } = await harness()
const dir = mkdtempSync(join(tmpdir(), 'dsh-inv-'))
ctx.baseUrl = pathToFileURL(dir + '/').href
const fakePnpm = join(dir, 'pnpm.cjs')
writeFileSync(fakePnpm, 'process.exit(0)\n')
process.env.DSH_PNPM = fakePnpm
try {
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
ctx.provide('dshAllowPluginInstall', true)
expect(() => inventory.install({ type: 'registry', spec: 'x' })).toThrow(/failed to read profile manifest/)
} finally {
delete process.env.DSH_PNPM
rmSync(dir, { recursive: true, force: true })
}
})
})
@@ -1,4 +1,4 @@
import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
@@ -35,4 +35,21 @@ describe('persistPluginDisabled', () => {
expect(text.match(/image-recognition-http/g)).toHaveLength(1)
expect(text).not.toContain('disabled: true')
})
it('creates the patch file when it does not yet exist', () => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-plugin-persist-'))
persistPluginDisabled(dir, 'image-recognition-http', true)
const text = readFileSync(join(dir, 'cordis.patch.yml'), 'utf8')
expect(text).toContain('image-recognition-http')
expect(text).toContain('disabled: true')
rmSync(dir, { recursive: true, force: true })
})
it('treats a non-array patch file as empty', () => {
const dir = profile('not-an-array\n')
persistPluginDisabled(dir, 'image-recognition-http', true)
const text = readFileSync(join(dir, 'cordis.patch.yml'), 'utf8')
expect(text).toContain('image-recognition-http')
expect(text).not.toContain('not-an-array')
})
})