深入理解 DeepSeek Harness · 第 1 篇
一切皆插件:DeepSeek Harness 的设计命题
前情回顾
第 0 篇交给你三条线索与一张前提速查表:插件树靠 inject 表达依赖、事件用五种派发模式把观察与短路变成公开契约、日志用「模型可见即已记录」保证可重放。本篇从插件树这条线索切入,回答「一切皆插件」到底落在哪些代码上。
本章要回答的问题
一行 npx @deepseek-ai/dsh web 背后究竟挂载了什么?「没有特权核心可以打补丁」这句话如果只是宣传语,它应该经不起源码推敲——它在哪些行上被兑现?如果连 agent loop 都能被替换,启动器靠什么保证替换之后树还是完整的?
你会学到什么 / 先验知识
你会看懂 profile 与 bundle 的关系、五层内核的分工、以及「默认循环也是一个服务槽位」这条最硬的自证,并看到一个具体机制:全局必需条目清单如何在启动末段拦住激活失败的替换实现。先验只有第 0 篇的三条线索,不需要读过后续章节。
核心概念:从一行命令到一棵树
npx @deepseek-ai/dsh web 里的 web 不是可执行文件,而是一个 profile(配置档)。[文档] docs/architecture.md:15-21 逐字给出这套模型:
A running `dsh` is a plugin tree composed at boot from ordered layers. A **profile** is a named composition stored in the Harness home. It lists the bundles it stacks, holds any out-of-tree plugins it installs, and keeps the user's own `cordis.patch.yml`. `web`, `headless`, `sdk`, `sdk-minimal`, and `acp` ship as templates. A **bundle** is a distribution format for Cordis config rows and the code they mount, so whatever it inserts stays patchable by the layers above it. Each declares itself in its own `package.json` under a `dsh` field: `dsh.profile` lists a profile's bundles, and `dsh.bundle` points at a bundle's patch file.
bundle 定义里的关键限定是:它插入的任何东西都必须仍可被上层 patch。这不是打包格式偏好,而是「无特权核心」的前提——如果某个模块用硬编码写死,上层就无法替换它。
[源码] 五个内建 profile 的真实内容在 packages/util/official-bundles/src/bundles.ts:9-26:
/** The shipped profile templates auto-initialized on first use, by name. */ export const PROFILE_TEMPLATES: Record<string, ProfileTemplate> = { acp: { bundles: ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-acp-app'], }, web: { bundles: ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'], }, headless: { bundles: ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-headless'], }, sdk: { bundles: ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-sdk-app'], }, 'sdk-minimal': { bundles: ['@deepseek-ai/dsh-sdk-minimal'], }, }
四个 profile 都叠加 dsh-base 再加一片薄薄的模式层;sdk-minimal 是唯一的例外,它只挂一个 bundle。原因在第 12 篇展开,这里先记住结论:patch 能替换和插入行,但不能删除上层 bundle 已经挂载的运行时行为,所以需要一个自持完整树的例外。
graph TD
L1[① 微内核<br/>vendor/cordis/src<br/>Context / Service / Fiber / 五种派发] --> L2
L2[② 组合器<br/>vendor/loader + vendor/include<br/>!!js / disabled / patch] --> L3
L3[③ 启动层<br/>packages/boot/app-boot<br/>profile / required 审计 / HMR] --> L4
L4[④ 领域服务<br/>packages/core/* · packages/llm/*<br/>sessions / tools / llm / agentLoop] --> L5
L5[⑤ 组合清单<br/>packages/bundle/*/cordis.patch.yml<br/>按 id 插入可 patch 的行]
五个术语在这里先用一句话固定:!!js 是 Cordis 配置里在挂载时求值的表达式节点;disabled 是每次挂载决策时求值的启用条件;patch 是按行 id 整块替换或插入配置行的叠加文件;HMR 是配置热重载;声明合并是 TypeScript 把同名接口在多个模块中合并成一个的机制,dsh 用它定义 ctx.<key> 服务键。
这五层的意义在于:没有任何一层是「产品核心」。第 ④ 层的 ctx.sessions、ctx.tools、ctx.llm、ctx.agentLoop 都是通过声明合并挂到 Cordis Context 上的服务键;第 ⑤ 层只是把这些服务按 id 排进一棵可 patch 的树。想换掉其中任何一个,写一个 patch 把那一行的 config 整块替换即可。
源码走读:无特权核心的四处证据
第 0 篇已逐字引过下面前两块引文(见该篇「核心概念」与「源码走读」)。这里只取本篇需要的一件事,不重复解释。
命题本身(文档)。 [文档] docs/architecture.md:7-11 里真正要证的是这一句:Every part of the product is a plugin, including the model adapter, the tool registry, the session log, and the agent loop itself。其中「the agent loop itself」最重——下面三处源码把它从文档承诺变成可验证的事实。
证据一:默认循环是一个服务槽位。 [源码] packages/core/agent-loop/src/index.ts:174-186:
declare module '@deepseek-ai/cordis' { interface Context { agentLoop: AgentLoop /** * Launcher-owned exact session identities for configured agents, keyed by * the agent's config `id` and set with `ctx.provide()` before any Loader * entry mounts (see {@link CONFIGURED_AGENT_IDENTITIES_KEY}). A launcher * owns identity because only it knows whether the session already exists, * while the `cordis.yml` row keeps the model route as ordinary patchable * config. An entry with no matching key keeps its configured identity. */ configuredAgentIdentities?: ConfiguredAgentIdentities }
本篇只取其中一件事:agentLoop: AgentLoop 与 ctx.tools 在类型层面是同一种东西——一个由插件提供的服务键,因此可以被 patch 替换。ctx.provide() 注入身份与模型路由可 patch 的边界已在第 0 篇讲过,这里不重复。
证据二:替换是整行替换,且行序没有加载语义。 [源码] packages/bundle/base/cordis.patch.yml:1-13 是 base bundle 的开头注释:
# The dsh-base bundle patch: the shared core of each base-backed profile, applied as # ONE insert over the empty profile root. Later bundle patches and the user's # profile cordis.patch.yml address these rows by id, with the last write # winning per row. # # A patch replaces the targeted row's whole `config` rather than merging into # it, so a row whose value differs by mode does NOT live here: it belongs to # each mode bundle, keeping any single row down to one bundle layer plus the # user's. Mode-specific rows appear below only with shared plugin identity and # neutral defaults; each mode bundle restates its complete configuration. # # Row order carries no load semantics (activation is service-availability # driven); the grouping is for readers.
最后一个注释最容易被误读:列表顺序只是给读者看的,激活由服务可用性驱动。这就是 inject 替代手工启动顺序的直接后果——第 2 篇会走读 RegistryService.resolve() 如何把三种插件形态归一成一个 callback(回调)身份键。
证据三:启动器不持有特权模块。 [源码] packages/boot/app-boot/src/index.ts:1288-1306:
// Two failure labels: `prepare` runs before any config-tree entry mounts, // so its failure is host setup, not the plugin tree. let stage = 'host preparation failed' try { ctx.baseUrl = pathToFileURL(dirname(absoluteConfigPath)).href + '/' ctx.provide('dshHomePath', dshHomePath) ctx.provide('dshPath', dshPath) await ctx.plugin(Loader) await prepare?.(ctx) stage = 'plugin tree failed to load' await mountRootInclude(ctx, absoluteConfigPath, patches, bareModuleBaseUrl, binName) // A surface can finish and dispose the whole tree while startup is still // in flight, before the last entry settles. The Loader service goes with // it, and the activation audit describes a live tree — reading `ctx.loader` // past this point would throw a TypeError over an app that exited exactly // as asked. Re-check after settlement before auditing the tree. await ctx.get('loader')?.await() if (ctx.get('loader') === undefined) return ctx await auditStartupEntries(ctx, binName) return ctx
在插件树层面,启动器能做的只有:挂载 Loader、装根 Include、等树 settle、审计。这里的 settle 指所有 Loader 条目完成激活或失败、树进入稳定态。注释里那个「surface 可能在启动未完成时就 dispose 整棵树」的场景说明启动器自己也是树上的一个参与者,而不是树外的控制者。
证据四:必需条目清单兜底。 [源码] packages/boot/app-boot/src/index.ts:963-981:
/** * Entry ids whose presence defines a usable DSH application. * * The list is global rather than profile metadata. Missing or disabled ids do * not affect startup; an enabled listed entry must activate. The list covers * shared Agent execution, application endpoints, and Web bootstrap/transport. */ const requiredStartupEntryIds = new Set<string>([ 'agent-loop', 'tool-registry-native', 'tool-registry-ptc', 'webserver', 'web-runtime', 'modules', 'connection', 'headless-runner', 'acp', 'sdk-jsonrpc-server', ])
agent-loop 是全局清单的第一项。注意注释的精确语义:清单是全局的,不属于任何 profile 元数据;缺失或禁用的 id 不影响启动,但启用的条目必须激活成功。这条清单加上审计函数,才是「替换后树仍可用」的真正保证。
[源码] packages/boot/app-boot/src/index.ts:1205-1237 给出判据:
/** * Apply DSH startup policy to a settled Loader tree. * * Inactive entries from the global required list reject startup, including * options that their group rejected before creating an entry. Other * inactive entries join that failure diagnostic, or produce one warning when * no required entry failed and leave successful siblings running. * Required ids absent from the tree, and disabled required entries, are ignored. * A throwing disabled expression is an entry failure, not a disabled entry. * The bootstrap Include must activate so unreadable or invalid root config is fatal. ... */ export async function auditStartupEntries( ctx: Context, binName: string, warn: (line: string) => void = line => void process.stderr.write(line), ): Promise<void> { const failures = await inactiveEntries(ctx) const required = new Set(failures.filter(failure => (failure.entry !== undefined && failure.entry === bootstrapIncludes.get(ctx)) || requiredStartupEntryIds.has(failure.id))) if (required.size > 0) { throw new StartupError(startupDiagnostic(binName, failures, required), failures.map(failure => ({ id: failure.id, module: failure.name, required: required.has(failure), fiberState: failure.entry?.fiber?.state, outcome: failure.outcome, }))) } if (failures.length > 0) warn(activationDiagnostic(binName, failures)) }
所以第三问的答案是:你用 patch 换掉 agent-loop 那一行之后,只要它仍然启用却没激活成功,requiredStartupEntryIds 命中,auditStartupEntries 抛 StartupError,启动失败。注意这里不是「少一个必需服务就失败」——清单里未出现或被禁用的 id 会被忽略,bootstrap Include 才必须激活。这一 distinction 很重要,否则你会误判启动失败的成因。
设计取舍
① 为什么用扩展点而不是更多配置项? 配置项的语义是「在既定实现上选一个值」,扩展点的语义是「把整个实现换掉」。dsh 选后者,因为模型适配器、工具注册表、会话日志这些组件的差异不是参数级的——远程文件系统与本地文件系统不是某个 pathPrefix 能统一的。代价是 patch 与行 id 强耦合:上游改了 id,用户的 patch 就得跟着改。项目用 INSTALLATION_OWNED_PROFILE_TUPLES(bundles.ts:28-34)对早期版本写入的整段 bundle 列表做精确替换,把这种耦合限制在可识别的元组上。
② 为什么启动审计放在树 settle 之后,而不是逐行 fail-fast? 因为 patch 可以来自四个层级——bundle 层、profile 的 cordis.patch.yml、home patch、--patch overlays(docs/architecture.md:25)——任意一层都可能补齐某个服务;逐行报错会在服务尚未被后面某层提供时误判。代价是错误发现更晚,而且启动器必须处理「树在启动途中被卸载(dispose)」的竞态——app-boot/src/index.ts:1299-1305 那段 re-check 就是为此存在的。
③ 为什么允许 sdk-minimal 不叠加 base? 因为叠加模型只能替换和插入,无法移除 base 已挂载的运行时行为。与其让一个「最小 SDK」背着 settings、凭证、telemetry、subagent 这些它不需要的服务,不如让它自持一棵完整显式树([文档] packages/bundle/sdk-minimal/README.md:12,97)。代价是它刻意缺少这些共享服务,不能当成 base 的轻量版来用。
常见误解
「插件」在这套系统里不等于「工具」。工具只是插件贡献的一种能力;模型适配器、会话存储、批准策略、甚至 agent loop 本身都是插件。如果你把插件理解成「加一个函数给模型调」,第 9 篇的能力接缝会显得莫名其妙——那里的重点是换 provider 而不是加工具。
另一个误解是「必需清单意味着少一个服务就启动失败」。源码写得很清楚:缺失或禁用的必需 id 被忽略;只有启用却激活失败的必需条目,以及未激活的 bootstrap Include,才会抛 StartupError。
小结
一行 npx @deepseek-ai/dsh web 挂载的是一棵由 profile 叠加 bundle、各层贡献 ctx.tools 与 ctx.agentLoop 等服务槽位的插件树(答问①);「没有特权核心可打补丁」兑现于 packages/core/agent-loop/src/index.ts:174-186 的 declare module、packages/bundle/base/cordis.patch.yml:1-13 的整行替换规则与 packages/boot/app-boot/src/index.ts:1288-1306 的挂载流程(答问②);启动器不持有特权模块,靠全局必需清单 requiredStartupEntryIds(首项即 agent-loop,app-boot/src/index.ts:963-981)加上树 settle 后的 auditStartupEntries(:1205-1237)拦住激活失败的替换实现,从而保证替换后树仍可用(答问③)。
这条命题的代价同样具体:patch 与行 id 耦合、错误发现推迟到树 settle 之后、以及 sdk-minimal 那样必须自持完整树的例外。这三处代价会在第 4 篇的 overlay 与第 12 篇的 profile 设计里再次出现。
思考题
- 按第 0 篇的版本基准准备 Node/pnpm,运行
npx @deepseek-ai/dsh --profile web --dump-config(不发模型请求、无需凭证)。输出是一份 YAML;按 id 查找agent-loop行,写出它的id与config中的模型路由字段。 - 对比
packages/util/official-bundles/src/bundles.ts:10-26中web与sdk-minimal的 bundles 数组:列出sdk-minimal刻意缺失的三个共享服务(packages/bundle/sdk-minimal/README.md:12,97),并说明为什么 overlay 无法移除 base 已挂载的 effect。 - 在
packages/boot/app-boot/src/index.ts:1298-1306找到 audit 之前的 re-check。若删掉if (ctx.get('loader') === undefined) return ctx,当 surface 在启动途中 dispose 了整棵树、Loader服务随纤程一起卸载时,继续读ctx.loader属性代理会抛什么错?