feat(plugin-inventory): live plugin activation, mirror install, and toggle lists

Installing a plugin now activates it without a restart: the CLI boot provides a
dshReloadProfile handle that re-runs the profile composition and applies it to
the running root Include, and the install/uninstall Remotes recompose live when
the handle is present (restartRequired: false). Registry installs try the
ordered INSTALL_REGISTRIES mirrors with the official npm registry as the final
fallback, erroring only when every source is unreachable. The enable/disable
guard splits into a REQUIRED_PLUGINS blacklist and a USER_TOGGLEABLE_PLUGINS
whitelist (default toggleable) generated from the running plugin list, and the
offline optional-bundle catalog is emptied (default bundles are not
installable/uninstallable). The plugin-list tab becomes a registry install form
and shows immediate-activation instead of a restart notice.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Pine
2026-08-14 20:45:00 +08:00
parent a0efe02c04
commit e18cfbfb93
16 changed files with 486 additions and 196 deletions
@@ -1,9 +1,12 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { mkdirSync, mkdtempSync, readFileSync, 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'
import {
composeOfflineBundle, INSTALL_REGISTRIES, resolvePnpm, runPnpmInstall,
runPnpmInstallWithRegistries, uninstallBundle,
} from '../src/install.ts'
const dirs: string[] = []
@@ -35,6 +38,22 @@ function makeFakePnpm(dir: string, exitCode: number): string {
return file
}
/**
* A fake pnpm that records its argv to `process.env.RECORD` and exits 1 when the
* `--registry` value matches `process.env.FAIL_REG`, else `process.env.EXIT`.
*/
function makeRecordingPnpm(dir: string): string {
const file = join(dir, 'recording.cjs')
writeFileSync(file, [
"const fs = require('fs')",
'const args = process.argv.slice(2)',
'fs.writeFileSync(process.env.RECORD, JSON.stringify(args))',
"const reg = args[args.indexOf('--registry') + 1]",
'process.exit(reg === process.env.FAIL_REG ? 1 : Number(process.env.EXIT ?? 0))',
].join('\n'))
return file
}
afterEach(() => {
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true })
})
@@ -83,6 +102,67 @@ describe('runPnpmInstall', () => {
nodeBin: process.execPath, pnpmCjs: pnpm, spec: 'example', before,
}) }).toThrow(/pnpm install failed/)
})
it('passes --registry when a registry is given', () => {
const dir = makeProfile()
const record = join(dir, 'record.json')
const pnpm = makeRecordingPnpm(dir)
process.env.RECORD = record
process.env.EXIT = '0'
try {
runPnpmInstall({
binName: 'dsh', profileDir: dir, installAnchor: join(dir, 'package.json'),
nodeBin: process.execPath, pnpmCjs: pnpm, spec: 'x',
before: readProfileManifest('dsh', dir), registry: 'https://mirror.example',
})
const args = JSON.parse(readFileSync(record, 'utf8')) as string[]
expect(args).toContain('--registry')
expect(args).toContain('https://mirror.example')
} finally {
delete process.env.RECORD
delete process.env.EXIT
}
})
})
describe('runPnpmInstallWithRegistries', () => {
it('tries registries until one succeeds', () => {
const dir = makeProfile()
const record = join(dir, 'record.json')
const pnpm = makeRecordingPnpm(dir)
process.env.RECORD = record
process.env.EXIT = '0'
process.env.FAIL_REG = 'https://bad.example'
try {
runPnpmInstallWithRegistries({
binName: 'dsh', profileDir: dir, installAnchor: join(dir, 'package.json'),
nodeBin: process.execPath, pnpmCjs: pnpm, spec: 'x',
before: readProfileManifest('dsh', dir),
}, ['https://bad.example', 'https://good.example'])
const args = JSON.parse(readFileSync(record, 'utf8')) as string[]
expect(args).toContain('https://good.example')
} finally {
delete process.env.RECORD
delete process.env.EXIT
delete process.env.FAIL_REG
}
})
it('throws when every registry fails', () => {
const dir = makeProfile()
const pnpm = makeFakePnpm(dir, 1)
expect(() => { runPnpmInstallWithRegistries({
binName: 'dsh', profileDir: dir, installAnchor: join(dir, 'package.json'),
nodeBin: process.execPath, pnpmCjs: pnpm, spec: 'x',
before: readProfileManifest('dsh', dir),
}, ['https://a.example', 'https://b.example']) }).toThrow(/across all registries/)
})
})
describe('INSTALL_REGISTRIES', () => {
it('ends with the official npm registry as the fallback', () => {
expect(INSTALL_REGISTRIES[INSTALL_REGISTRIES.length - 1]).toBe('https://registry.npmjs.org')
})
})
describe('composeOfflineBundle with a bare manifest', () => {
@@ -2,7 +2,7 @@ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'nod
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { afterEach, describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it, vi } 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'
@@ -126,20 +126,14 @@ describe('PluginInventoryGateway', () => {
expect(inventory.list().entries.some(entry => entry.entryId === pendingId)).toBe(false)
})
it('availableBundles reports installed state from the profile manifest', async () => {
it('availableBundles reports an empty offline catalog', 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'] } },
}))
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'dsh-profile-test', dsh: { profile: { bundles: [] } } }))
try {
const snapshot = inventory.availableBundles()
expect(snapshot.available).toEqual([
{ name: '@deepseek-ai/dsh-image-recognition-bundle', installed: true },
])
expect(inventory.availableBundles().available).toEqual([])
} finally {
rmSync(dir, { recursive: true, force: true })
}
@@ -151,25 +145,40 @@ describe('PluginInventoryGateway', () => {
ctx.baseUrl = pathToFileURL(dir + '/').href
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'dsh-profile-test', dsh: { profile: { bundles: [] } } }))
try {
expect(() => inventory.installPlugin({ type: 'bundle', name: 'x' })).toThrow(/install anchor is unavailable/)
await expect(inventory.installPlugin({ type: 'bundle', name: 'x' })).rejects.toThrow(/install anchor is unavailable/)
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
expect(() => inventory.installPlugin({ type: 'registry', spec: 'x' })).toThrow(/not permitted/)
await expect(inventory.installPlugin({ type: 'registry', spec: 'x' })).rejects.toThrow(/not permitted/)
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('uninstall removes a bundle from the profile manifest', async () => {
it('refuses to install a default bundle', 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: [] } } }))
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
try {
await expect(inventory.installPlugin({ type: 'bundle', name: '@deepseek-ai/dsh-image-recognition-bundle' }))
.rejects.toThrow(/not installable/)
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('refuses to uninstall a default bundle and removes an optional one', 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'] } },
dsh: { profile: { bundles: ['@deepseek-ai/dsh-image-recognition-bundle', 'optional-bundle'] } },
}))
try {
expect(inventory.uninstall('@deepseek-ai/dsh-image-recognition-bundle').restartRequired).toBe(true)
expect(inventory.availableBundles().available[0]!.installed).toBe(false)
await expect(inventory.uninstall('@deepseek-ai/dsh-image-recognition-bundle')).rejects.toThrow(/cannot be uninstalled/)
expect((await inventory.uninstall('optional-bundle')).restartRequired).toBe(true)
expect(readProfileManifest('dsh', dir).dsh?.profile?.bundles).toEqual(['@deepseek-ai/dsh-image-recognition-bundle'])
} finally {
rmSync(dir, { recursive: true, force: true })
}
@@ -185,7 +194,7 @@ describe('PluginInventoryGateway', () => {
writeFileSync(join(dir, 'node_modules', 'b', 'cordis.patch.yml'), '[]\n')
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
try {
inventory.installPlugin({ type: 'bundle', name: 'b' })
await inventory.installPlugin({ type: 'bundle', name: 'b' })
expect(readProfileManifest('dsh', dir).dsh?.profile?.bundles).toEqual(['b'])
} finally {
rmSync(dir, { recursive: true, force: true })
@@ -203,7 +212,7 @@ describe('PluginInventoryGateway', () => {
try {
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
ctx.provide('dshAllowPluginInstall', true)
expect(inventory.installPlugin({ type: 'registry', spec: 'some-pkg' }).restartRequired).toBe(true)
expect((await inventory.installPlugin({ type: 'registry', spec: 'some-pkg' })).restartRequired).toBe(true)
} finally {
delete process.env.DSH_PNPM
rmSync(dir, { recursive: true, force: true })
@@ -215,12 +224,12 @@ describe('PluginInventoryGateway', () => {
expect(() => inventory.availableBundles()).toThrow(/profile directory/)
})
it('treats a missing profile manifest as no installed bundles', async () => {
it('treats a missing profile manifest as an empty catalog', 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)
expect(inventory.availableBundles().available).toEqual([])
} finally {
rmSync(dir, { recursive: true, force: true })
}
@@ -260,7 +269,7 @@ describe('PluginInventoryGateway', () => {
try {
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
ctx.provide('dshAllowPluginInstall', true)
expect(() => inventory.installPlugin({ type: 'registry', spec: 'x' })).toThrow(/bundled pnpm is unavailable/)
await expect(inventory.installPlugin({ type: 'registry', spec: 'x' })).rejects.toThrow(/bundled pnpm is unavailable/)
} finally {
rmSync(dir, { recursive: true, force: true })
}
@@ -276,10 +285,30 @@ describe('PluginInventoryGateway', () => {
try {
ctx.provide('dshInstallAnchor', join(dir, 'package.json'))
ctx.provide('dshAllowPluginInstall', true)
expect(() => inventory.installPlugin({ type: 'registry', spec: 'x' })).toThrow(/failed to read profile manifest/)
await expect(inventory.installPlugin({ type: 'registry', spec: 'x' })).rejects.toThrow(/failed to read profile manifest/)
} finally {
delete process.env.DSH_PNPM
rmSync(dir, { recursive: true, force: true })
}
})
it('triggers a live recompose when a reload handle is provided', 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'))
const reload = vi.fn(async () => {})
ctx.provide('dshReloadProfile', reload)
try {
const result = await inventory.installPlugin({ type: 'bundle', name: 'b' })
expect(result.restartRequired).toBe(false)
expect(reload).toHaveBeenCalledOnce()
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
})
@@ -4,22 +4,24 @@ import { isRequiredPlugin, isUserToggleable } from '../src/required.ts'
describe('isRequiredPlugin (blacklist/whitelist, default-open)', () => {
it('marks the blacklist core as required', () => {
for (const name of ['@deepseek-ai/cordis-plugin-loader', '@deepseek-ai/dsh-session', 'cordis:required']) {
for (const name of ['@deepseek-ai/cordis-plugin-loader', '@deepseek-ai/dsh-session', 'cordis:required', 'cordis:include']) {
expect(isRequiredPlugin(name)).toBe(true)
expect(isUserToggleable(name)).toBe(false)
}
})
it('defaults everything else to toggleable', () => {
for (const name of ['@deepseek-ai/dsh-hmr', '@deepseek-ai/dsh-tool-todo', 'cordis:user-toggleable']) {
it('whitelists the currently-disabled set as toggleable', () => {
for (const name of [
'@deepseek-ai/dsh-tool-pwsh', '@deepseek-ai/dsh-skill-badge',
'@deepseek-ai/dsh-session-telemetry-otel', '@deepseek-ai/dsh-pwsh-sandbox',
]) {
expect(isRequiredPlugin(name)).toBe(false)
expect(isUserToggleable(name)).toBe(true)
}
})
it('whitelist overrides the blacklist for an explicitly toggleable plugin', () => {
// image-recognition is on the whitelist, so it is never required.
for (const name of ['@deepseek-ai/dsh-image-recognition', '@deepseek-ai/dsh-image-recognition-http']) {
it('defaults unknown modules to toggleable', () => {
for (const name of ['@fixture/never', 'cordis:user-toggleable']) {
expect(isRequiredPlugin(name)).toBe(false)
expect(isUserToggleable(name)).toBe(true)
}
@@ -27,8 +29,7 @@ describe('isRequiredPlugin (blacklist/whitelist, default-open)', () => {
})
describe('AVAILABLE_BUNDLES', () => {
it('lists the offline-installable optional bundles', () => {
expect(AVAILABLE_BUNDLES).toContain('@deepseek-ai/dsh-image-recognition-bundle')
expect(new Set(AVAILABLE_BUNDLES).size).toBe(AVAILABLE_BUNDLES.length)
it('is empty: no optional bundles ship as offline-installable yet', () => {
expect(AVAILABLE_BUNDLES).toEqual([])
})
})