Feature Article

深入理解 DeepSeek Harness · 第 12 篇:Profiles 与 Bundles——五套产品形态如何拼装

五个 profile 为什么四个叠加 dsh-base、sdk-minimal 却自持完整树?模式层为何用显式禁用而非沉默删除?preset patch 改了什么?HMR 为何是一行可 patch 的普通配置。

分类

深入理解 DeepSeek Harness · 第 12 篇

Profiles 与 Bundles:五套产品形态如何拼装

前情回顾

前十一篇讲完运行时主干与边界。从第 12 篇起转向工程化装配:同一套插件运行时,怎么被拼成 Web、Headless、SDK、ACP 与最小 SDK 五种产品形态。

本章要回答的问题

五个 profile 各自叠了哪些 bundle?为什么四个都叠加 dsh-base 而 sdk-minimal 是例外?模式层为什么用「禁用」而不是「删除」?preset patch 到底改了什么?HMR 为什么是一行可 patch 的普通配置?

你会学到什么 / 先验知识

你会看清 profile / bundle / patch 三者如何分层、薄模式层的具体做法、sdk-minimal 例外的代价、preset 的整行重述语义,以及 HMR 的回滚路径。先验是第 1 篇的 bundle 定义与第 4 篇的 patch 栈。

核心概念:从空列表开始组合

第 1 篇已引过 docs/architecture.md:15-29 与 PROFILE_TEMPLATES(packages/util/official-bundles/src/bundles.ts:9-26),这里只取新角度:五个模板的差异不在代码,而在 bundle 列表。四个 profile 都是 dsh-base 加一片模式层;sdk-minimal 只有 @deepseek-ai/dsh-sdk-minimal 一个 bundle。

graph LR
    E[空条目列表] --> B[dsh-base<br/>模型 / 工具 / 持久化 / 沙箱 / 设置 / 凭证 / 遥测]
    B --> W[dsh-web-app]
    B --> H[dsh-headless]
    B --> S[dsh-sdk-app]
    B --> A[dsh-acp-app]
    M[dsh-sdk-minimal<br/>自持完整显式树] -. 不叠加 base .-> R[最终插件树]
    W --> R
    H --> R
    S --> R
    A --> R

源码走读:五处证据

证据一:薄模式层只替换必要行。 [源码] packages/bundle/headless/cordis.patch.yml:31-41:

# Replacing config retains the base mode/root defaults while adding the CLI grants.
- id: sandbox-policy
  inject: [headlessStartup]
  config:
    mode: !!js process.env.DSH_PERMISSION_MODE ?? 'workspace-write'
    workspaceRoot: !!js process.cwd()
    additionalWritableRoots: !!js ctx.headlessStartup.additionalWritableRoots

- id: hmr
  disabled: true

Headless 只做两件事:把 base 的 sandbox-policy 行整块替换成带 CLI 授予的版本(注释明确说保留 base 的 mode/root 默认值,只加 CLI grants),再把 hmr 行禁用。模式层的薄,来自 patch 的整行替换语义——它不需要复制 base 的全文。

证据二:Web 用禁用而不是删除。 [源码] packages/bundle/web-app/cordis.patch.yml:557-566:

# ── the agent plane moves behind agent presets ─────────────────────────────
#
# Every row below composes what ONE agent contributes to the host registries:
# its tools, its prompt sections, its delegation backends. The base keeps them
# for the TUI, which is single-session and composes its agent process-wide; the
# Web surface disables them here and lets each session mount a preset instead.
#
# Disabling rather than deleting is deliberate: the base is shared, and a row
# absent from a surface overlay would silently reappear the day someone reorders
# the composition.

这段注释把第 4 篇的「overlay 无法删除」推到实际设计:Web 每个 session 要用 agent preset 挂自己的工具,于是把 base 里进程级的 agent 行禁用;如果靠「不列这一行」来表达,哪天有人重排组合,它就会悄悄回来。禁用是显式意图,沉默的缺席不是。

证据三:preset patch 是整行重述。 [源码] packages/bundle/web-app/presets/standard.patch.yml:1-14:

# Agent preset standard: one `@deepseek-ai/dsh-agent-preset` declaration inserted
# after the web patch. Edits saved from the Web editor override this row's
# `config.plugins` by id from the profile patch.
- insert:
    - id: preset-standard
      name: '@deepseek-ai/dsh-agent-preset'
      config:
        id: standard
        order: 1
        plugins:
          - id: persona
            name: '@deepseek-ai/dsh-persona'
            config:
              prefix: You are a coding agent powered by the {{model}} model.

insert 加一行 id 为 preset-standard 的 preset 声明,其 config.plugins 是一组按 id 排列的插件行。Web 编辑器保存的改动会从 profile patch 按 id 覆盖这一行的 config.plugins——这正是第 4 篇说的「preset patch 没有独立解析器,只是整行重述 config.plugins 并沿用同一优先级」的真实形态。

证据四:sdk-minimal 的例外与代价。 [文档] packages/bundle/sdk-minimal/README.md:12:

Use `dsh --profile sdk-minimal` when an SDK client needs a small, explicit coding-agent runtime. By default, the profile advertises a platform-selected persistent shell and `read_image`, persists sessions as uncompressed JSONL, and selects the model from the SDK initialization request. It supplies a complete Cordis tree and excludes `dsh-base`, Web, settings, managed credentials, telemetry, text file tools, workspace instructions, skills, jobs, and subagents. Its danger-full-access policy lets the shell modify any path available to the process, so use it only with an isolated workspace.

它排除的不是一两行,而是一整组共享服务:settings、托管凭证、遥测、文本文件工具、workspace 指令、skills、jobs、subagents。代价写得很直白:danger-full-access 让 shell 能改进程可达的任何路径,所以只能用在隔离 workspace 里。这个 profile 不是 base 的「轻量版」,而是另一棵显式树。

证据五:HMR 是普通条目,且有回滚。 [源码] packages/boot/hmr/src/index.ts:703-746:

    const removed = new Set<Plugin>()
    try {
      for (const { previous: plugin, replacement, filename, runtime, fibers, replacements, activated } of attempts) {
        if (!runtime) continue
        const path = relative(this.baseDir, fileURLToPath(filename))

        removed.add(plugin)
        try {
          this.ctx.registry.delete(plugin)
          await Promise.all(fibers.map(({ fiber }) => fiber.await()))
        } catch (err) {
          this.ctx.logger.warn('failed to dispose plugin at %C', path)
          this.ctx.logger.warn(err)
        }

        try {
          await reload(replacement, fibers, replacements, activated)
          this.ctx.logger.info('reload plugin at %C', path)
        } catch (err) {
          this.ctx.logger.warn('failed to reload plugin at %C', path)
          this.ctx.logger.warn(err)
          throw err
        }
      }
    } catch (error) {
      // Restore caches and re-register old plugins after a replacement failure.
      rollback()
      for (const { previous: plugin, fibers, activated } of attempts) {
        if (!removed.has(plugin)) continue
        try {
          for (const { runtime } of activated) {
            if (runtime && this.ctx.registry.get(runtime.callback) === runtime) {
              const replacementFibers = [...runtime.fibers]
              this.ctx.registry.delete(runtime.callback)
              // Failed startup errors remain on fibers after their teardown finishes.
              await Promise.allSettled(replacementFibers.map(fiber => fiber.await()))
            }
          }
          await reload(plugin, fibers)
        } catch (err) {
          this.ctx.logger.warn(err)
        }
      }
      throw error
    }

热重载是「delete 旧 plugin → await 旧纤程卸载 → reload 新实现」;失败语义要分清:卸载失败(:710-716)只记警告并继续,不抛错也不回滚——因为旧实现的清理未完成不应当阻止新实现接管;只有 reload 失败(:718-725)才 throw 进入 :727 的 catch,调用 rollback() 并逐个重新注册旧 plugin,且用 Promise.allSettled 等待失败启动遗留的纤程。这是第 3 篇「副作用可逆」在工程层的兑现——配置改变不必重启进程,但重新加载失败时必须能完整撤回。

设计取舍

① 为什么四个 profile 共享 base 而 sdk-minimal 自持完整树? 因为共享 base 让模式层只需要几行 patch,安全修复也只改一处;代价是 overlay 无法移除 base 已挂载的行为。SDK 的最小形态恰恰需要移除那些行为,所以只能用一棵自持的完整显式树(sdk-minimal/README.md:12)。

② 为什么 Web 用禁用而不是删除? 因为 base 是共享的,某行在 surface overlay 里「没出现」不表示它不属于产品;一旦组合顺序改变,它会悄悄复活。显式 disabled: true 把意图写进配置,代价是树里仍保留这些行(诊断输出更长),但语义稳定。

③ 为什么 HMR 走 delete→reload→rollback 而不是另起一棵树? 因为插件的服务注册、事件监听与状态都绑在纤程上,重新加载同一 callback 就能复用身份与依赖图;代价是必须处理「树在重载中被 dispose」的竞态与失败回滚,于是有了 rollback() 与 Promise.allSettled 那段代码。

常见误解

「五个 profile 是五套独立代码。」 不是,四个是 base 加薄 patch;只有 sdk-minimal 自持完整树。

「overlay 能移除 base 的运行时行为。」 不能,applyPatches() 只有 insert 与字段赋值,没有删除分支(第 4 篇)。这正是禁用与自持树两种做法的由来。

「preset patch 有专属解析器。」 没有。它就是 insert 一行 dsh-agent-preset,Web 编辑器再按 id 覆盖该行的 config.plugins(standard.patch.yml:1-3)。

小结

五个 profile 中四个叠加 dsh-base,因为共享基础层让模式层只需几行 patch、安全修复也只改一处;sdk-minimal 之所以例外,是因为 overlay 无法移除 base 已挂载的运行时行为,它只能自持完整树,代价是排除 settings、凭证、遥测、jobs、subagents 等共享服务(packages/util/official-bundles/src/bundles.ts:9-26、packages/bundle/sdk-minimal/README.md:12)。薄模式层靠整行替换与显式禁用表达意图(packages/bundle/headless/cordis.patch.yml:31-41、web-app/cordis.patch.yml:557-566);preset 只是 insert 一行再由 profile patch 按 id 覆盖 config.plugins(standard.patch.yml:1-14);HMR 是可 patch 的普通条目,卸载失败只记警告继续、reload 失败才 rollback()(packages/boot/hmr/src/index.ts:703-746)。

代价落在三处:基础层共享导致 overlay 不能移除行为、禁用让树里保留冗余行、HMR 要处理竞态与回滚。第 13 篇讲跨进程接缝,看这棵 Host 树如何被浏览器调用。

思考题

  1. 在 packages/bundle/web-app/cordis.patch.yml:564-566 找到「Disabling rather than deleting is deliberate」。如果 Web overlay 只是不列 base 里的某行而不显式禁用,哪天组合顺序改变会发生什么?这和第 4 篇的 applyPatches() 无删除分支有什么关系?
  2. 对比 packages/util/official-bundles/src/bundles.ts:14-25 的 web 与 sdk-minimal:为什么后者不能靠「叠加 base 再 patch 掉不需要的服务」实现?它为此付出了什么代价?
  3. 在 packages/boot/hmr/src/index.ts:727-746 找到 rollback 路径。如果删掉 Promise.allSettled(replacementFibers.map(...)),一个启动失败的替换插件在回滚时会留下什么?这与第 3 篇的 disposer 等待有什么关系?

RESPONSES / LOG

读者来信 / RESPONSES

发表评论

评论需审核后展示。

暂无评论