第十七章:Self-Modification 扩展 本章目标 :帮助你理解 Agent 如何检查/挂载自己的插件、运行时插件热重载、模型驱动的插件安装/卸载、以及安全边界与限制。阅读本章后,你应该能回答"Agent 如何动态扩展自己的能力"以及"Self-Modification 的安全边界在哪里"。
17.1 概述 17.1.1 什么是 Self-ModificationSelf-Modification 是 dsh 的实验性 功能,允许 Agent 在运行时检查和修改自己的插件配置。这使得 Agent 可以:
自省 :查看当前加载了哪些插件
动态扩展 :根据任务需要安装新插件
自适应 :根据环境变化调整配置
17.2 插件检查(Introspection) 17.2.1 CordisInspectRegistryService
class CordisInspectRegistryService {
register ( inspector: CordisInspector) : Disposable {
this . inspectors. set ( inspector. id, inspector)
return ( ) => {
this . inspectors. delete ( inspector. id)
}
}
query ( pluginId: string ) : PluginInfo | undefined {
return this . inspectors. get ( pluginId) ?. info ( )
}
list ( ) : PluginInfo[ ] {
return Array . from ( this . inspectors. values ( ) ) . map ( i => i. info ( ) )
}
}
17.2.2 PluginInfo
interface PluginInfo {
id: string
name: string
version: string
status: 'active' | 'inactive' | 'error'
dependencies: string [ ]
services: string [ ]
eventListeners: string [ ]
}
17.2.3 Agent 自省工具
ctx. tools. register ( {
name: 'inspect_plugins' ,
description: 'List all loaded plugins and their status' ,
parameters: {
type: 'object' ,
properties: {
filter: { type: 'string' , description: 'Filter by plugin name' }
}
} ,
async execute ( input) {
const plugins = ctx. cordisInspector. list ( )
if ( input. filter) {
return plugins. filter ( p => p. name. includes ( input. filter) )
}
return plugins
}
} )
17.3 插件安装 17.3.1 Plugin Installer
class DynamicCordisRunnerService {
async installPlugin ( plugin: PluginDefinition) : Promise < void > {
await this . validatePlugin ( plugin)
await this . checkDependencies ( plugin)
if ( plugin. remote) {
await this . downloadPlugin ( plugin)
}
await this . mountPlugin ( plugin)
this . ctx. emit ( 'plugin/installed' , { pluginId: plugin. id } )
}
private async mountPlugin ( plugin: PluginDefinition) : Promise < void > {
const module = await import ( plugin. entryPoint)
this . ctx. plugin ( module. default, plugin. config)
}
}
17.3.2 PluginDefinition
interface PluginDefinition {
id: string
name: string
version: string
entryPoint: string
config? : Record< string , unknown >
dependencies? : string [ ]
remote? : boolean
remoteUrl? : string
}
17.4 插件卸载 17.4.1 Plugin Uninstaller
class DynamicCordisRunnerService {
async uninstallPlugin ( pluginId: string ) : Promise < void > {
const dependents = this . getDependents ( pluginId)
if ( dependents. length > 0 ) {
throw new Error ( ` Cannot uninstall plugin ${ pluginId} : depended on by ${ dependents. join ( ', ' ) } ` )
}
await this . unmountPlugin ( pluginId)
await this . cleanupPlugin ( pluginId)
this . ctx. emit ( 'plugin/uninstalled' , { pluginId } )
}
private async unmountPlugin ( pluginId: string ) : Promise < void > {
const disposer = this . pluginDisposers. get ( pluginId)
if ( disposer) {
await disposer ( )
this . pluginDisposers. delete ( pluginId)
}
}
}
17.5 热重载 17.5.1 HMR(Hot Module Replacement)
class HmrService {
watchFiles ( directories: string [ ] ) : void {
for ( const dir of directories) {
const watcher = chokidar. watch ( dir, {
ignoreInitial: true ,
ignored: / node_modules /
} )
watcher. on ( 'change' , ( path) => {
this . handleFileChange ( path)
} )
}
}
private async handleFileChange ( path: string ) : Promise < void > {
const plugin = this . findPluginByFile ( path)
if ( ! plugin) return
await this . unmountPlugin ( plugin. id)
const module = await import ( path)
await this . mountPlugin ( {
id: plugin. id,
name: plugin. name,
entryPoint: path,
config: plugin. config
} )
this . ctx. emit ( 'plugin/reloaded' , { pluginId: plugin. id } )
}
}
17.5.2 配置热重载
class IncludeService {
watchConfig ( configPath: string ) : void {
const watcher = chokidar. watch ( configPath, {
ignoreInitial: true
} )
watcher. on ( 'change' , async ( ) => {
const newConfig = await this . loadConfig ( configPath)
await this . applyConfigChange ( newConfig)
this . ctx. emit ( 'config/reloaded' , { path: configPath } )
} )
}
}
17.6 安全边界 17.6.1 限制Self-Modification 功能有严格的安全限制:
权限控制 :只有授权的 Agent 可以修改插件
沙箱隔离 :插件在沙箱中运行
审计日志 :所有修改操作被记录
回滚能力 :支持插件修改的回滚
17.6.2 安全检查
class SecurityChecker {
checkInstallPermission ( agent: Agent, plugin: PluginDefinition) : boolean {
if ( ! agent. hasPermission ( 'plugin:install' ) ) {
return false
}
if ( plugin. remote && ! this . isTrustedSource ( plugin. remoteUrl) ) {
return false
}
if ( plugin. signature && ! this . verifySignature ( plugin) ) {
return false
}
return true
}
checkUninstallPermission ( agent: Agent, pluginId: string ) : boolean {
if ( ! agent. hasPermission ( 'plugin:uninstall' ) ) {
return false
}
const dependents = this . getDependents ( pluginId)
if ( dependents. length > 0 ) {
return false
}
return true
}
}
17.7 配置 17.7.1 Self-Modification 配置
- id : self- modification
config :
enabled : true
permissions :
install : true
uninstall : true
reload : true
security :
requireSignature : true
trustedSources :
- 'https://github.com/deepseek-harness'
- 'https://registry.npmjs.org/@deepseek-ai'
audit :
enabled : true
logPath : '${DSH_HOME}/audit/self-modification.log'
17.8 使用场景 17.8.1 动态扩展能力
async function handleComplexTask ( agent: Agent, task: string ) : Promise < void > {
const requirements = analyzeTaskRequirements ( task)
const currentPlugins = await agent. inspectPlugins ( )
for ( const requirement of requirements) {
if ( ! currentPlugins. some ( p => p. provides. includes ( requirement) ) ) {
await agent. installPlugin ( {
id: ` plugin- ${ requirement} ` ,
name: ` Plugin for ${ requirement} ` ,
entryPoint: ` ./plugins/ ${ requirement} .js `
} )
}
}
await agent. execute ( task)
}
17.8.2 自适应配置
async function adaptToEnvironment ( agent: Agent) : Promise < void > {
const environment = await agent. detectEnvironment ( )
if ( environment === 'production' ) {
await agent. updatePluginConfig ( 'logging' , { level: 'warn' } )
await agent. updatePluginConfig ( 'security' , { strictMode: true } )
} else {
await agent. updatePluginConfig ( 'logging' , { level: 'debug' } )
await agent. updatePluginConfig ( 'security' , { strictMode: false } )
}
}
17.9 小结
概念 一句话解释 Self-Modification Agent 动态修改自己的插件配置 Introspection 检查当前加载的插件 Plugin Installer 动态安装新插件 Plugin Uninstaller 动态卸载插件 HMR 热模块替换,运行时重载插件 Security Checker 安全检查,防止恶意修改
17.10 完整教程总结恭喜你完成了 DeepSeek Harness 补充教程的全部八章!加上之前的九章基础教程,你已经系统地学习了 dsh 的完整架构。
补充专题总结安全沙箱与隔离 :Landlock/Seatbelt/bwrap 沙箱、进程树隔离
Session 日志深度 :JSONL 格式、投影算法、日志重放
Typert 类型系统 :类型图生成、运行时注册、跨仓库发现
Web Client 架构 :React 组件、Slot 机制、WebSocket 通信
API Gateway 与 SDK :BFF 架构、JSON-RPC 协议、TS/Python SDK
凭证与授权 :凭证接缝、优先级链、OAuth 流程
Webhook 与外部事件 :签名验证、可信规则、CI/CD 集成
Self-Modification :插件自省、动态安装/卸载、热重载
参考资源本教程基于 DeepSeek Harness 源码编写,内容可能随项目更新而变化。