DeepSeek Harness 服务隔离与作用域
用两个插件组观察 shell 服务的实例与配置,再区分作用域继承、同名覆盖和工具可见集合。
学完这一章,你能够
- 用两个插件组观察 shell 服务的实例与配置,再区分作用域继承、同名覆盖和工具可见集合。
服务实例隔离与 agent 作用域
先区分两种隔离需求:Loader 的组内 service realm 负责让不同插件组拥有各自配置的服务实例;agent scope 则决定哪些按作用域登记的贡献能被当前上下文解析。二者解决的问题不同。
不能把 Loader 的 isolate 当成 agent scope 的别名。前者沿插件组创建服务实现上下文,后者围绕注册表层次工作;它们都不自动构成操作系统权限边界。
按插件组配置服务
在固定版本中,Loader 为标记 isolate 的插件组建立 entry-local realm,并在该 entry 的映射下装配服务。自编探针观察到两个组得到不同 realm token 与服务对象;这项结果只说明目标配置的服务分离。
isolate 配置属于 Loader 的组条目。把 shell 服务设置为组内隔离后,再分别给组条目提供子服务配置,才有可观察的组差异;单独写一段配置文本不足以证明运行时真的用了它。
目前的运行记录覆盖了 Loader group/isolate 与 dsh-bash-local 配置解析,未执行 plugin-a 或 plugin-b 的 shell 调用。因此“组内只看到自己的服务”仍需该产品消费者路径的端到端验证。
两个 BashLocal 实例的 resolve() 分别保留 5000 ms 与 60000 ms。它验证的是配置传递和值解析,不是 shell 进程实际耗时、超时中止、stdout 或 stderr。

配置差异的适用边界
不同任务可以选择不同服务参数;这种分组配置用于表达任务需求差异,不应被写成性能提升或安全隔离保证。
快速交互与长任务可以提出不同 timeout 配置需求,但本次没有测量任务完成时间或超时后的实际进程行为。教学解释需把“配置可不同”与“运行效果已验证”分开。
本次针对 BashLocal 的两个 group 实例观察到各自的有效 timeout;不能从这一个服务推及组内所有服务都自动隔离。
scope 身份与显式父链
dsh-scope 使用不透明对象身份标识查找作用域。当前 Agent 路径把 Agent 对象作为自身 scope key;这是该 API 的做法,不是每一个外部调用者都被强制采用的规则。
scope-aware 注册接口从传入上下文解析 owner:没有作用域时进入全局层,有作用域时归入对应层。普通服务若不使用这些接口,不会因此自动加入同一可见性规则。
父层可见性来自明确建立的 scope 链。子 scope 配置了 parent 后,可在解析时查到祖先贡献;没有绑定 parent 的 sibling 不会因为业务上有关联而自动继承。
parentSession 或 delegationDepth 这类业务字段只记录会话谱系,不会自行建立注册表父链。当前 Agent 构造路径创建自身 scope 时没有提供 parent;产品 subagent 如何绑定仍未端到端运行。
shadowing 与工具可见集合
shadowing 是分层注册表里的同名解析:子层可以在自身合并视图中替代祖先或全局同名项,兄弟 scope 仍保持自己的视图。这只适用于相应的命名注册层。
合并顺序从全局开始,再叠加显式祖先,最后叠加当前 scope,因此最近一层同名项优先。移除子层贡献后,解析结果会回落到祖先;再移除祖先时才回落到全局。
tools.restrict 是 ToolsService 的可见工具筛选。固定实现针对继承/全局工具组合 allow/deny 条件,多层限制会共同生效;当前 scope 自行登记的工具另有加入路径。
过滤后的 definitions/schemas 视图不会列出未通过筛选的继承工具,因此可以影响产品准备给模型的工具目录;这次只检查了注册表和 schema 视图,没有生成真实模型提示。
这里的“看不见”只指受该 ToolsService 过滤后的工具集合,不能推广为网络、文件系统、其他服务或系统进程不可访问,也不能当作通用授权边界。
固定流水线的解析代码在缺少可用工具定义时会返回 ToolNotFoundError,早于工具主体调用;本次是代码路径核对,没有实际派发被过滤工具,也没有记录运行时拒绝响应。
复习要点
复习时应分别记住:isolate 管组级服务实例;scope 管作用域注册层且支持显式祖先;shadowing 是最近同名项覆盖;restriction 是 ToolsService 工具视图筛选。
下面的自测分别检查组配置结果、继承需要的显式绑定,以及同名覆盖与工具过滤的区别;答案旁同时标记哪些产品级运行尚未做。
自测与答案
同一 shell 服务在两个组中如何区分实例和配置? 在固定 Loader 探针中,两个组得到不同服务上下文;BashLocal resolve() 保留 5000 ms 和 60000 ms。命令输出与真正超时行为尚未观察。
已有子会话关系时,什么条件下可以读取父作用域贡献? 不自动继承。祖先贡献可沿明确建立的 scope parent 链解析;业务 lineage 字段不代替绑定。产品 subagent 路径仍待端到端检查。
同名覆盖和可见工具筛选分别改变什么? shadowing 让当前层同名注册覆盖可见祖先项;tools.restrict 筛选继承/全局工具的可见集合。现有测试覆盖 schema/注册表视图,没运行实际 dispatch。
作用域术语与运行边界
| 概念 | 本站解释 | 观察边界 |
|---|---|---|
| scope | scope 是作用域注册与查找所用的上下文。固定版本支持显式父链,子层可见祖先贡献;作用域不限定为两层,也不会从业务关系自动推导父级。 | 显式绑定链的最近层优先和未绑定兄弟隔离已由 7 项本地探针观察;产品 subagent 链路未做端到端验证。 |
| scope key | scope key 是用于索引作用域状态的不透明身份,不应靠 parentSession 等普通对象字段判定注册可见性;父子关系必须通过 API 明确建立。 | 对象身份分离与未绑定兄弟不继承已观察;没有把 key 当作权限令牌或安全边界。 |
| agent.ctx | agent.ctx 将作用域上下文交给支持 scope-aware 注册/查询的接口;生命周期归属取决于该上下文和接口实现,不代表所有服务都自动隔离。 | 固定源码与本地作用域探针支持上下文/作用域区分;此行的完整产品 Agent 注册/销毁路径仍需绑定实际运行。 |
| shadowing | 分层合并时,最近的子作用域同名项覆盖祖先和全局项;其他名字仍保留。移除子层贡献后解析可回退到父层,再回退到全局。 | 最近层覆盖、兄弟互不读取对方父级贡献,以及 dispose 后回退均在固定版本探针中观察。 |
| restriction | tools.restrict 仅筛选 ToolsService 作用域视图中继承/全局工具;多层限制取交集,自有注册另行加入。它是工具可见集合规则,不能直接等同通用权限或操作系统隔离。 | 11 项探针只观察注册/Schema 视图和限制合并;没有执行工具分发或 Bash 命令。 |
| lineage | parentSession、delegationDepth 等业务谱系字段只描述数据关系;只有显式作用域父链绑定才参与注册继承。 | 源码检查确认 scope lookup 读取显式父链;产品 subagent 创建链未端到端运行,故不把字段独立判断写成运行事实。 |
复跑两组 YAML 配置
import { registerHooks, createRequire } from 'node:module'
import fs from 'node:fs'
import path from 'node:path'
import assert from 'node:assert/strict'
import { createHash } from 'node:crypto'
import { pathToFileURL, fileURLToPath } from 'node:url'
if(process.argv.length!==3) throw new Error('Usage: node observe.mjs <trusted pinned checkout>')
const repo=path.resolve(process.argv[2])
const base=pathToFileURL(repo+path.sep).href
const req=createRequire(import.meta.url)
const ts=req(path.join(repo,'node_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/typescript.js'))
const yamlPath=path.join(repo,'node_modules/.pnpm/yaml@2.9.0/node_modules/yaml/dist/index.js')
const {parse}=await import(pathToFileURL(yamlPath))
const aliases={
'@deepseek-ai/cordis':'vendor/cordis/src/index.ts', '@deepseek-ai/cosmokit':'vendor/cosmokit/src/index.ts',
'@deepseek-ai/schemastery':'vendor/schemastery/src/index.ts', '@deepseek-ai/dsh-shell':'packages/shell/shell/src/index.ts',
'@deepseek-ai/dsh-timeout':'packages/util/timeout/src/index.ts', '@deepseek-ai/dsh-bash-local':'packages/shell/bash-local/src/index.ts',
'@deepseek-ai/dsh-subprocess':'packages/subprocess/subprocess/src/index.ts', '@deepseek-ai/dsh-error':'packages/util/error/src/index.ts',
'@deepseek-ai/dsh-http-proxy':'packages/util/http-proxy/src/index.ts'
}
registerHooks({resolve(s,c,n){if(aliases[s])return n(base+aliases[s],c);return n(s,c)},load(u,c,n){if(u.startsWith(base)&&u.endsWith('.ts'))return {format:'module',source:ts.transpile(fs.readFileSync(fileURLToPath(u),'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext,target:ts.ScriptTarget.ES2022}}),shortCircuit:true};return n(u,c)}})
const authoredYaml=fs.readFileSync(new URL('./groups.yml',import.meta.url),'utf8')
const parsed=parse(authoredYaml)
assert.equal(parsed.length,2)
const {Context,Service}=await import('@deepseek-ai/cordis')
const {Loader,Group}=await import(base+'vendor/loader/src/index.ts')
const {LocalBashExecutor}=await import('@deepseek-ai/dsh-bash-local')
const sha=p=>createHash('sha256').update(fs.readFileSync(p)).digest('hex')
const yamlSha=createHash('sha256').update(authoredYaml).digest('hex')
class SubprocessStub extends Service{constructor(ctx){super(ctx,'subprocess')}}
const SubprocessPlugin=ctx=>{new SubprocessStub(ctx)}
const ctx=new Context(); await ctx.plugin(SubprocessPlugin); await ctx.plugin(Loader,{baseUrl:import.meta.url})
ctx.loader.builtins.group=Group; ctx.loader.builtins['bash-local']=LocalBashExecutor
for (const entry of parsed) await ctx.loader.create(entry)
await ctx.loader.await()
const groupA=ctx.loader.resolve('authored-group-a'),groupB=ctx.loader.resolve('authored-group-b')
const entryA=ctx.loader.resolve('authored-bash-a'),entryB=ctx.loader.resolve('authored-bash-b')
const shellA=Array.from(entryA.fiber.provisions).find(p=>p.name==='shell')?.value
const shellB=Array.from(entryB.fiber.provisions).find(p=>p.name==='shell')?.value
const tokenA=groupA.ctx[Symbol.for('cordis.isolate')].shell,tokenB=groupB.ctx[Symbol.for('cordis.isolate')].shell
assert.ok(shellA&&shellB)
const resolvedA=shellA.resolve({command:'printf authored-a'})
const resolvedB=shellB.resolve({command:'printf authored-b'})
const cases=[
{name:'authored YAML parses to exactly two group entries',expected:2,actual:parsed.length,passed:parsed.length===2},
{name:'each group entry enables shell isolation',expected:[true,true],actual:parsed.map(x=>x.isolate.shell),passed:parsed.every(x=>x.isolate.shell===true)},
{name:'authored child entries retain distinct IDs',expected:['authored-bash-a','authored-bash-b'],actual:parsed.map(x=>x.config[0].id),passed:parsed[0].config[0].id!==parsed[1].config[0].id},
{name:'Loader creates distinct group realms',expected:'different isolate tokens',actual:tokenA!==tokenB,passed:tokenA!==tokenB},
{name:'group entries resolve different BashLocal service objects',expected:'distinct service instances',actual:shellA!==shellB,passed:shellA!==shellB},
{name:'group A resolve receives configured timeoutMs',expected:5000,actual:resolvedA.timeoutMs,passed:resolvedA.timeoutMs===5000},
{name:'group B resolve receives configured timeoutMs',expected:60000,actual:resolvedB.timeoutMs,passed:resolvedB.timeoutMs===60000},
{name:'resolve preserves authored command inputs',expected:{a:'printf authored-a',b:'printf authored-b'},actual:{a:resolvedA.command,b:resolvedB.command},passed:resolvedA.command==='printf authored-a'&&resolvedB.command==='printf authored-b'}
]
assert.ok(cases.every(c=>c.passed))
const files=['vendor/loader/src/index.ts','vendor/loader/src/config/isolate.ts','vendor/loader/src/config/group.ts','packages/shell/bash-local/src/index.ts','packages/shell/shell/src/index.ts','packages/util/timeout/src/index.ts']
const result={checked_at:new Date().toISOString(),course:'deepseek-harness',chapter_source_key:'5fbd2fd3a5afa28db6acb677',purpose:'Independently authored ASCII-indented YAML variant parsed with the pinned YAML v2 package and loaded through the pinned Cordis Loader, group plugin, and BashLocal service; no Bash command or subprocess was executed.',environment:{node:process.version,typescript:ts.version,yaml:JSON.parse(fs.readFileSync(path.join(repo,'node_modules/.pnpm/yaml@2.9.0/node_modules/yaml/package.json'),'utf8')).version,pinned_repo:'4b399c39a6d1479cd84ca591b4d7c88e24d38a96',network_used:false,source_scripts_executed:false},authored_yaml:authoredYaml,authored_yaml_sha256:yamlSha,source_yaml_executed:false,subprocess_execution_tested:false,timeout_enforcement_tested:false,source_hashes:Object.fromEntries(files.map(f=>[f,sha(path.join(repo,f))])),probe_source_sha256:sha(fileURLToPath(import.meta.url)),cases,case_count:cases.length,passed_count:cases.filter(c=>c.passed).length,status:'pinned_yaml_parse_loader_group_and_bashlocal_resolve_pass_subprocess_execution_pending',generated:false,source_facts_reviewed:false,technical_reviewed:false,user_accepted:false,published:false}
await ctx.fiber.dispose()
console.log(JSON.stringify(result,null,2))
- 解析配置后创建两个独立 BashLocal 实例。resolve() 保留组内命令文本,返回 5000 与 60000 毫秒。
所需环境:Node.js 24.22.3,固定版本 DeepSeek Harness 源码副本与本地锁定依赖。
当前源码 SHA256:faabae2858c59c5b23e99ad4f3bf3f916166accb918310f2ba6cd1332d320b17
下载并复跑
解压后按 README 指定现有源码副本。程序读取同目录 groups.yml,输出配置解析观察。
所需环境:Node.js 24.22.3,固定版本 DeepSeek Harness 源码副本与本地锁定依赖。
ZIP SHA256:8fd1617684d3ceb1b1e14fcebb2ae6a4dd80de30b1a736f3db9083e754cb43b1
常见错误与边界
- isolate 和 agent scope 分别管理服务实例与注册层。
- 业务谱系字段不会自动建立作用域父链。
- resolve() 返回的配置值本身无法证明进程超时行为。
练习与解答
同一 shell 服务在两个组中如何区分实例和配置?
展开参考解答
在固定 Loader 探针中,两个组得到不同服务上下文;BashLocal resolve() 保留 5000 ms 和 60000 ms。命令输出与真正超时行为尚未观察。