Feature Article

深入理解 DeepSeek Harness · 第 10 篇:边界与策略——作用域、沙箱与审批

三层边界各管一轴:作用域决定注册可见性而非安全边界;sandbox 必须在 spawn 前包装 argv 并 fail-closed;审批是完整接缝,ask/never 与闭合结果如何保证缺席即拒绝。附工作目录的会话级语义。

分类

深入理解 DeepSeek Harness · 第 10 篇

边界与策略:作用域、沙箱与审批

前情回顾

第 9 篇把能力接缝讲完,停在三个边界概念上:作用域(scope)、沙箱(sandbox)、审批(approval)。本篇把三者放在一起,回答它们在「谁能看到什么」「进程在哪个世界运行」「这个动作能不能做」三个层面上各自管什么。

本章要回答的问题

作用域到底隔离了什么、没隔离什么?sandbox 为什么要求 consumer 在 spawn 之前 wrap argv?审批策略 ask 与 never 差在哪,为什么结果必须 fail-closed?工作目录为什么是会话级事实、模型不能直接改?

你会学到什么 / 先验知识

你会看懂作用域的可见性/所有权双属性、sandbox 的包装点、审批接缝的 durable 策略与审计对、工具侧的机会式消费与降级,以及工作目录的会话级语义。先验是第 2 篇的服务仓库、第 8 篇的工具管线、第 9 篇的接缝三角色。

核心概念:三层边界,不是三层权限

graph LR
    S[作用域 scope<br/>注册可见性 + 生命周期<br/>不是安全边界] --> T[工具 / 提示片段 / 监听器]
    X[沙箱 sandbox<br/>进程创建时确定边界<br/>confine argv before spawn] --> P[子进程 / 终端]
    A[审批 approval<br/>这个动作能不能做<br/>ask / never + fail-closed] --> D[策略拒绝 / allowed-once]
    W[工作目录 working directory<br/>会话级事实<br/>workspace-root/change] --> S
    W --> X

三者是不同轴的约束:scope 决定「哪个 agent 看得到哪条注册」,sandbox 决定「进程在哪个执行世界里跑」,approval 决定「这个具体动作是否放行」。把它们混成一套权限系统会立刻出问题——最典型的就是拿 scope 当安全边界。

源码走读:五处证据

证据一:作用域是可见性/所有权边界,不是安全边界。 先定三个术语:**贡献集(contribution set)**是某个作用域内的一组注册;(一个作用域由 createScope(ctx, key) 铸造(mint),其 ctx 带上作用域标签(scope tag),名下注册同时可见于该作用域并随其卸载(packages/core/scope/README.md:29-31);scopeTarget 用作用域标签过滤事件(:75)。 [文档] packages/core/scope/README.md:12 与 :27:

`dsh-scope` lets plugin authors give each agent or group an isolated contribution set with a shared lifetime. Child scopes inherit ancestor contributions, with the nearest definition taking precedence, while ancestor scopes can observe descendant activity; neither relationship works in reverse. Disposing a scope removes everything owned by it.
Plugin authors use `dsh-scope` to give one agent (or one group) its own registration world. The registries in the core group build on it — a tool registered through `agent.ctx` is visible only to that agent — and the same primitive serves any custom registry or scope-filtered event.

:60 把边界说死:The registration context determines both visibility and ownership: a registration made through a scoped context is visible in that scope and disposed with it ... The primitive routes trusted same-process plugins; it is not a sandbox or an authority boundary. 后半句还说:交出 Scope.ctx 也等于交出铸造者的服务解析 API。:98 更列出限制:只有 scope-aware 的 API 才隔离状态——注册表要按 scopeOf() 归档、事件要经 scopeTarget() 派发;任意普通 Cordis 服务仅仅因为通过 scoped context 调用,仍然是 context-global。

所以 scope 没隔离的是:普通服务、以及同进程内被信任的插件之间的访问。它不是 authority boundary,不能挡住恶意同进程插件——那是「进程隔离 / 同进程信任模型」的问题;sandbox 只管子进程的 confinement,不管插件之间的信任。

证据二:sandbox 在 spawn 前包装 argv。 第 9 篇已引过 SandboxProvider.confine(argv, policy) 的 fail-closed 契约(packages/sandbox/sandbox/src/index.ts:195-226),其 JSDoc(:214-225)明确返回 ConfinedArgv——即 consumer 实际要 spawn 的替换 argv,并附带后端达到的强制程度。[文档] docs/subsystems/sandbox.md:23 说明 danger-full-access 的消费者直接 spawn 原始 argv、不调用 ctx.sandbox;:30 说明强制程度是报告出来的事实:full 表示后端治理模式承诺的全部文件效果,partial 表示只治理子集,要求绝对承诺的消费者必须拒绝或显式呈现这个区别。

关键在时序:边界在进程创建那一刻确定——一旦子进程已经起来,再谈 confinement 就晚了;SubprocessRuntime.spawn() 不认识 policy(第 9 篇 subprocess/src/index.ts:95-108),所以包装必须发生在调用方 spawn 之前。

10.3 审批接缝:完整的三角色能力

证据三:审批是完整的接缝,不是一行配置。 先定两个术语:approval/request 是应答者瀑布(answerer waterfall)——每个应答者要么拥有决定权并返回结果,要么调 next() 委派;approval/asked + approval/decided 是只记日志(log-only)的审计对,不进入模型转录。[文档] docs/subsystems/approval.md:1-47(节选,剥离了 Markdown 链接):

The user-approval seam of dsh-user-approval answers one question: may this specific action proceed? It owns the shared request/outcome vocabulary, the `ctx.approval` dispatch service, the `approval/request` answerer waterfall, the log-only audit pair, and the per-session `ask`/`never` policy. UI channels may provide human answerers; the ACP automation bridge provides one-shot machine decisions for its own agents. Callers such as dsh-tools and dsh-tool-bash consume the closed outcome and fail closed unless it is `allowed-once`.
`ApprovalOutcome` is closed and fail-closed. `allowed-once` grants only the asked-about action; callers deny on `rejected`, `cancelled`, and `unavailable`. A missing, non-owning, throwing, or non-conforming answerer becomes `unavailable` rather than opening the gate.
`ApprovalPolicy` determines what happens before interactive answerers run. `ask` delegates to the composed answerer chain, whose no-answer default is `unavailable`; `never` deterministically returns `rejected` without dispatching any answerer. The effective value is the last `approval/policy` event in the session log, falling back to the service config.

三个要点:结果类型是闭合枚举(allowed-once / rejected / cancelled / unavailable),除第一个外全部拒绝;缺失、不拥有、抛错、不符合契约的应答者都变成 unavailable,不会打开闸门;策略是会话日志里最后一条 approval/policy 决定的,因此重放能还原。

[源码] packages/interaction/user-approval/src/index.ts:25-29 与 :41 显示 ctx.approval 的声明合并与 approval/policy durable 事件:

declare module '@deepseek-ai/cordis' {
  interface Context {
    approval: ApprovalService
  }
}
    'approval/policy': {
      policy: ApprovalPolicy
      /** Marks an override seeded into a child at delegation. */
      source?: 'delegation'
    }

审计对 [源码] packages/interaction/user-approval/src/index.ts:243-262:

  async request(req: ApprovalRequest): Promise<ApprovalOutcome> {
    const session = req.agent.session
    if (!this.approvalStateOf(session).turnOpen) {
      throw new Error(
        'approval.request() outside an open turn: the approval/asked + approval/decided audit pair '
        + 'must be turn-enclosed (a bare event between turns is crash-tail garbage on reload). '
        + 'Ask from inside the turn that needs the decision.',
      )
    }
    const id = brandString<ApprovalRequestId>(randomUUID())
    session.append('approval/asked', {
      id,
      toolName: req.toolName,
      ...req.callId !== undefined ? { callId: req.callId } : {},
      ...req.reason !== undefined ? { reason: req.reason } : {},
    })
    const outcome = await this.decide(req, session)
    session.append('approval/decided', { id, outcome })
    return outcome
  }

approval/asked 与 approval/decided 用同一个 ApprovalRequestId 配对,且必须在 open turn 内——turn 之间的裸事件在重载时会被当成崩溃尾巴垃圾。ApprovalRequestId 是品牌类型(docs/subsystems/approval.md:11,18),不能与 tool-call id 或 agent/session id 互换。

证据四:工具侧机会式消费。 第 8 篇的 serviceAsk() 源码 packages/core/tools/src/pipeline.ts:526-537 用 ctx.get('approval') 读取服务:没有服务、会话中途卸载、或没有 agent 可审计时,都返回 APPROVAL_UNAVAILABLE 拒绝。这是「缺失即拒绝」的具体实现——审批不是可选增强,缺席时系统会收紧而不是放松。

证据五:工作目录是会话级事实。 这里的 workspace-root/change 是记录目录移动的事件(docs/subsystems/working-directory.md:5,11),老日志里的 working-directory/change 已被服务忽略。[文档] docs/subsystems/working-directory.md:9 与 :13:

`ctx.workingDirectory.get(session)` reads the logged value. `ensure(agent)` validates it and restores the original project when the directory disappeared. `set(agent, path)` validates and records a move that a Host operation chose, such as worktree creation, the forge panel, or an SDK request; the model has no tool that moves the directory.
Changes are local to one Session. Relative filesystem operations, newly started processes, instruction discovery, skills, and file completion use the active directory. The sandbox `workspace-write` boundary, the Files tree and directory listings, and the change summaries follow it too. Existing terminals and persistent shells retain their own directories. Project membership remains anchored to the original project.

目录变更写成 workspace-root/change,且模型没有移动目录的工具——这是有意的:目录决定相对路径解析与新进程的 cwd,让模型改它会同时动摇文件系统与沙箱写根。

设计取舍

① 为什么 scope 不做安全边界? 因为同进程可信插件之间需要的是轻量隔离:一眼看出某条工具注册属于哪个 agent、随谁的生命周期卸载。加上权限判断会让每次注册都变成安全决策。代价是两个概念要分清——scope 管可见性,sandbox 管 authority;把 scope 当防护会误判。

② 为什么 never 策略在服务内部强制执行? docs/subsystems/approval.md:33 明确说 never 在派发任何 answerer 之前就确定性地返回 rejected。这样后置注册、甚至用 prepend 插入的 answerer 都无法绕过。代价是该策略不可被插件覆盖——headless/CI 场景需要的正是这种确定性。

③ 为什么审批结果闭合且 fail-closed? 因为调用方必须能对「没拿到批准」做唯一处理:拒绝。unavailable 让「没有审批通道」与「人明确拒绝」区分开,但两者都不放行。代价是可用性下降——没有 UI 的环境里所有需要审批的工具都不可用,要么配 ACP 机器应答者,要么用 never 明确表态。

常见误解

「scope 能挡住恶意插件。」 不能。packages/core/scope/README.md:60 逐字写着 it is not a sandbox or an authority boundary。

「没有 ApprovalService 就等于自动放行。」 反了。pipeline.ts:526-537 的降级是 APPROVAL_UNAVAILABLE 拒绝。

「模型可以用工具改工作目录。」 不能。docs/subsystems/working-directory.md:9 明说 the model has no tool that moves the directory;移动只来自 Host 操作。

小结

三层边界各管一轴:作用域决定注册可见性与随谁卸载,但普通服务仍 context-global、同进程插件仍相互信任,所以它不是安全边界(packages/core/scope/README.md:12,27,60,98);sandbox 必须在 spawn 前包装 argv,因为可执行边界在进程创建那一刻就确定、起来之后无法追认,且 SubprocessRuntime.spawn() 不认识 policy,所以契约要求 fail-closed(packages/sandbox/sandbox/src/index.ts:195-226、docs/subsystems/sandbox.md:30);审批必须 fail-closed,因为调用方对「没拿到批准」只能有一种处理——拒绝,ask 委派应答链、never 在服务内确定性拒绝,结果闭合且除 allowed-once 外全部拒绝(docs/subsystems/approval.md:5,21,33),审计对 approval/asked + approval/decided 必须在 open turn 内用品牌 id 配对(packages/interaction/user-approval/src/index.ts:243-262)。工具侧用 ctx.get('approval') 机会式消费、缺席即降级拒绝(packages/core/tools/src/pipeline.ts:526-537);工作目录之所以是会话级事实、模型无工具可改,是因为它决定相对路径解析与新进程 cwd,让模型改它会同时动摇文件系统与沙箱写根(docs/subsystems/working-directory.md:9,13)。

代价落在三处:scope 与 authority 必须分清、never 不可被插件覆盖、无审批通道时工具不可用。第 11 篇进入子智能体,看这些边界如何在并发协作者之间生效。

思考题

  1. 在 packages/core/scope/README.md:60 找到 it is not a sandbox or an authority boundary,再对照 :98 的「只有 scope-aware API 隔离状态」。如果把一个恶意插件放进某个 agent 的 scope,scope 能阻止它通过普通服务读取另一个 agent 的工具吗?同进程恶意插件的边界应由什么机制提供——sandbox 能解决吗?
  2. 在 docs/subsystems/approval.md:28 找到闭合结果类型。如果新增一个 allowed-forever 结果并要求调用方「默认放行」,会破坏哪条不变量?为什么 unavailable 必须与 rejected 同属拒绝侧?
  3. 在 packages/interaction/user-approval/src/index.ts:243-262 找到 approval/asked 与 approval/decided 的配对。如果允许在 turn 之间写一条裸的 approval/decided,重载时会发生什么?为什么 id 要用品牌类型而不是普通 string?

RESPONSES / LOG

读者来信 / RESPONSES

发表评论

评论需审核后展示。

暂无评论