Agent 跑着换掉 shell,为什么不留残余:DSH 底下的 Cordis 上线以来的每一次修改,新的在前。
Cordis 这篇修三处画面排版
增加 17 字,删去 67 字
⋯ 146 段没有变化
const u1 = arrow(tool, slots[0], { dashed: true, label: "inject" })
const u2 = arrow(tool, slots[1], { dashed: true, label: "inject" })
const u3 = arrow(provs[1], slots[2], { dashed: true, label: "inject", bend: -25 })
const u4 = arrow(provs[1], slots[3], { dashed: true, bend: -35 })
const alt1 = box("bash-local", { tone: "faint", w: 120, h: 34, x: 310, y: 60 })
const alt2 = box("subprocess-ssh", { tone: "faint", w: 140, h: 34, x: 670, y: 60 })
⋯ 75 段没有变化
const sp = box("系统提示词", { sub: "含 bash 的用法", tone: "c3", w: 140, h: 50, x: 330, y: 150 })
const ts = box("工具 schema", { sub: "来自 ctx.tools,含 bash", tone: "c3", w: 160, h: 50, x: 330, y: 230 })
const req = box("请求", { tone: "c4", w: 90, h: 44, x: 520500, y: 190 })
const a1 = arrow(turn, sp)
const a2 = arrow(turn, ts)
const a3 = arrow(sp, req)
const a4 = arrow(ts, req)
const llm = box("ctx.llm", { tone: "c4", w: 110, h: 44, x: 690730, y: 190 })
const a5 = arrow(req, llm, { label: "agent/request" })
const reply = box("assistant/message", { sub: "调用 bash:npm test", tone: "c2", w: 210, h: 54, x: 600640, y: 330 })
const a6 = arrow(llm, reply, { label: "llm/stream" })
const hook = text("这两个事件是 waterfall,插件可以在这里改请求、包住流", { size: 14, color: "soft", x: 400, y: 420 })
⋯ 41 段没有变化
const askA = arrow(pre, appr, { dashed: true, label: "ask" })
const shell = box("ctx.shell", { sub: "bash-sandbox", tone: "c4", w: 150, h: 50, x: 510, y: 220 })
const sb = box("ctx.sandbox", { sub: "macOS:Seatbelt\nLinux: / bwrap", tone: "c5", w: 160170, h: 6254, x: 400, y: 340 })
const sp = box("ctx.subprocess", { sub: "启动进程", tone: "c4", w: 150, h: 50, x: 620, y: 340 })
const e1 = arrow(exe, shell, { label: "bash 工具" })
⋯ 301 段没有变化
Cordis 这篇删掉讲稿里开场和第一步重复的半句
增加 29 字,删去 102 字
⋯ 110 段没有变化
step("dsh web 由两个组合包加两层补丁叠成。", () => {
say("从最外面看。敲下 dsh web,它先找到自己的组合包:dsh-base 和 dsh-web-app,后面还能再叠用户自己的补丁和命令行补丁。")
show(toStack, stack, ...layers)
})
⋯ 40 段没有变化
step("每个服务占 ctx 上一个名字,由一个插件提供。", () => {
say("放大一层,看插件和插件之间。每个服务在 ctx 上占一个名字:工具表、shell、沙箱、子进程,各由一个插件提供。")
show(...provs, ...pArrows)
})
⋯ 33 段没有变化
step("inject:声明要用哪些服务。", () => {
say("再放大一层,打开这个插件。第一行是依赖声明:工具表、shell、系统提示词,缺一个,它就等着不启动。")
set(src, { lines: [1] })
show(n1)
⋯ 96 段没有变化
step("先记下 tool/call,再过 tools/pre-execute:没人拦就放行。", () => {
say("模型说要用 bash 跑测试。这次调用先记进会话,然后过第一道关:执行前的事件。没有插件拦,默认就放行。")
show(s1, pre)
})
⋯ 42 段没有变化
step("工具结果要交回模型,就开下一步,再发一次请求。", () => {
say("命令跑完还没结束。工具的结果要交回给模型,于是开始下一步,再发一次请求。")
show(a1, step2)
})
⋯ 43 段没有变化
step("卸掉本机 shell,bash 工具跟着卸载,登记一起撤销。", () => {
say("最后一个转折:要是在跑的过程中,把 shell 换掉呢?实测把本机 shell 卸掉,bash 工具没人通知,自己就跟着卸载了,工具表里的 bash 也一起撤掉。")
show(lines[0], lines[1], lines[2])
hide(fromL)
⋯ 21 段没有变化
:::notes
最后看一个转折。
最后看一个转折:要是在跑的过程中,把 shell 换掉呢?
:::
⋯ 48 段没有变化
step("插件树:97 个条目按服务到齐来启动。", () => {
say("回到开头那三句话。每个能力都是插件,在 DSH 里,就是那棵九十七个条目的插件树,按服务到齐的先后启动。")
show(lk[0], where[0])
})
⋯ 122 段没有变化
Cordis 这篇按四幕重写:先讲问题,再拆 DSH 的层,再跟一次请求走
增加 14639 字,删去 10994 字
## 换掉一个能力,Agent 不想停
## 一句“跑一下测试”背后的能力
:::article
DeepSeek Harness(dsh)是 DeepSeek 开源的 agent harness,:cite[一切皆插件,由 Cordis 驱动]{b=mae86c3z}。:cite[它的基础配置里有 97 个插件条目]{b=t3wa4p4e}。
:::
:::scene{title="一个插件加载时做了什么条请求要动用哪些能力"}
```js
const agentask = box("Agent 正在帮我跑一下测试", { sub: "第 37 轮", tone: "c2", w: 170190, h: 5646, x: 400, y: 5040 })
const plug = box("bash 工具", { sub: "插件", tone: "c3", w: 150, h: 52, x: 400, y: 190 })
const chain = [
const shellL = box("本机 shell模型", { tone: "faintc4", w: 140100, h: 44, x: 150130, y: 190150 }),
const use = arrow(plug, shellL, { label: "用" })
box("bash 工具", { tone: "c3", w: 110, h: 44, x: 300, y: 150 }),
const sinks = [
box("shell", { tone: "c3", w: 100, h: 44, x: 470, y: 150 }),
box("工具表沙箱", { tone: "faintc5", w: 110100, h: 4044, x: 130640, y: 350150 }),
box("系统提示词", { tone: "faint", w: 130, h: 40, x: 310, y: 350 }),
box("事件监听", { tone: "faint", w: 120, h: 40, x: 490, y: 350 }),
box("定时器", { tone: "faint", w: 110, h: 40, x: 670, y: 350 }),
]
const regslinks = sinks[0, 1, 2].map((si) => arrow(plug, schain[i], { dashed:chain[i true+ }1]))
const sandlog = box("沙箱 shell会话记录", { tone: "c5faint", w: 140120, h: 4440, x: 650400, y: 190240 })
const q = text("卸掉的要收干净,用到它的要跟着换", { size: 18, bold: true, color: "accent", x: 400, y: 425 })
const toLog = arrow(chain[1], log, { dashed: true })
show(agent)
const more = ["系统提示词", "审批", "上下文压缩", "子 Agent", "进程管理", "文件系统"].map((n, i) =>
box(n, { tone: "faint", w: 110, h: 36, x: 105 + i * 118, y: 320 }))
const count = text("DSH 基础配置:97 个插件条目", { size: 16, bold: true, x: 400, y: 375 })
const vary = [
text("Windows 上换 PowerShell", { size: 14, color: "c5", x: 160, y: 95 }),
text("远程时换成 SSH", { size: 14, color: "c5", x: 640, y: 95 }),
]
const q = text("怎样让几十种能力像积木一样装、拆、换?", { size: 19, bold: true, color: "accent", x: 400, y: 425 })
show(ask)
step("Agent 一句“跑着一个长任务下测试”,它的要用上一串能力是一个个插件挂上去的。", () => {
say("先想象一个 Agent 正在跑一个很长的任务,已经到了第三十七轮。它能调的工具,比如 bash,都是一个个插件挂上去的。")
say("用户就说了一句:帮我跑一下测试。Agent 要把它做完,得用上一串能力:先问模型,模型决定调用 bash,bash 交给 shell,shell 在沙箱里跑,每一步还要记进会话。")
show(plug, use, shellL)
show(chain[0], links[0], chain[1], links[1], chain[2], links[2], chain[3])
show(toLog, log)
})
step("这只是一个条请求用到的,DSH 基础配置里的插件加载时,会往好几处登记东西有 97 个。", () => {
say("这个 bash 工具插件,加载的时候可不止干一件事:往工具表里登记自己,往系统提示词里写说明,还要挂事件监听、开定时器。")
say("这还只是一条请求用到的。DSH 的基础配置里,这样的插件条目一共有九十七个。")
together(() => {
show(...more)
show(...regscount)
show(...sinks)
})
})
step("现在想把 bash 换到沙箱里跑它们互相依赖,任务不停还要按场景换实现。", () => {
say("这时候你想换一下,bash 别在本机跑了,挪到沙箱里去。任务还不想停。")
say("麻烦在于,它们互相依赖,bash 离不开 shell,shell 离不开沙箱;还得按场景换实现,Windows 上换成 PowerShell,远程的时候换成 SSH,有时候还想在跑的过程中换掉一个。")
show(sand, { from: "right" })
show(...vary)
pulse(sand)
pulse(chain[2], chain[3])
})
step("难在两件事:卸掉的要收干净,用到它的要跟着换。", () => {
step("问题来了:几十种能力,怎样装、拆、换都不乱?", () => {
say("难的是两件事。卸掉一个插件,它登记过的东西得一条不剩地收回来;换掉一个能力,用到它的插件得跟着换过去。")
say("要是全写死在主程序里,启动顺序得人来排,换一个要改一串地方,卸掉一个还会留下它登记过的东西。所以问题是:几十种能力,怎样才能像积木一样装、拆、换?")
show(q)
})
⋯ 2 段没有变化
:::notes
今天聊拆一拆 DeepSeek 开源的 agent harness,简称 DSH,看它底下那个叫 Cordis 的插件框架,到底解决了什么问题。
:::
## 手写登记:重载一次,多一份
## Cordis 的回答:三句话
:::article
下面三个实验都用 DSH 仓库里自带的 Cordis 实际跑过,代码附在文末。
:cite[Cordis 的设计论文把动态组合分成两个维度]{b=geaa72sq}:插件之间的依赖能声明、能跟着变,是空间上的;插件移除时副作用能全部撤销,是时间上的。
:::
:::scene{title="不用框架,热重载Cordis 的三次(实测)句话"}
```js
const hTq = text("工具表怎样让几十种能力像积木一样装、拆、换?", { size: 1618, bold: true, color: "soft", x: 220400, y: 40 })
const hL = text("事件监听", { size: 16, bold: true, x: 580, y: 40 })
const cards = [
const tools = [1, 2, 3].map((v) => box("read_file每个能力都是插件", { sub: "v模型、工具、shell、沙箱、会话……" + v, tone: "faintc3", w: 150230, h: 4664, x: 220145, y: 40 + v * 72170 })),
const lis = [1, box("插件之间只隔着 2ctx", 3].map((v){ =>sub: box("监听器在 v"ctx +上提供服务,用 v,inject {声明要用", tone: "faintc2", w: 150250, h: 4064, x: 580400, y: 40 + v * 72170 })),
const box("登记过的都能撤销", ver{ =sub: text("第卸掉一个插件,它做过的事全部收回", 1tone: 次加载"c5", { sizew: 18240, boldh: true64, x: 400655, y: 330170 }),
const fire = text("发一次 tool/result:三个监听器都响了", { size: 18, bold: true, color: "accent", x: 400, y: 330 })
]
show(hT, hL)
const space = text("空间:插件之间的关系", { size: 15, bold: true, color: "c2", x: 400, y: 250 })
const time = text("时间:插件走后收不收得干净", { size: 15, bold: true, color: "c5", x: 655, y: 250 })
const next = text("下面一层层拆开,看 DSH 怎么用这三句话", { size: 17, bold: true, color: "accent", x: 400, y: 340 })
show(q)
step("插件加载:登记第一句:每个工具,挂能力都是一个监听器插件。", () => {
say("插件第一次加载,往工具表里放一个 read_file,再挂一个监听器。")
say("Cordis 的回答可以压成三句话。第一句,每个能力都是一个插件,连模型适配器、会话记录、Agent 循环本身都是。")
show(ver, tools[0], liscards[0])
})
step("改了代码热重载,新代码又登记一遍,旧的没人收。", () => {
step("第二句:插件之间不互相引用,只在 ctx 上提供和使用服务。", () => {
say("改了代码,热重载。新代码又跑一遍,又登记了一份。可旧的那份,谁也没去收。")
say("第二句,插件之间不直接引用对方的代码。谁提供能力,就挂到一个叫 ctx 的公共上下文上;谁要用,就声明一句我要用它。")
set(ver, { text: "重载第 1 次" })
show(cards[1])
show(tools[1], lis[1])
})
step("再重载一次,就有第三份句:插件做过的登记都能撤销。", () => {
say("再重载一次,就变成三份了。")
say("第三句,插件往外登记的每一样东西,都带着撤销的办法。插件一卸,它做过的事全部收回来。")
set(ver, { text: "重载第 2 次" })
show(cards[2])
show(tools[2], lis[2])
})
step("发一次事件,三个监听器都响;工具表里躺着三个 read_file。", () => {
step("论文把后两句叫空间上和时间上的可组合。", () => {
say("这时候发一次工具结果的事件,三个监听器全响了。模型拿到的工具表里也躺着三个 read_file,前两个指向早就该没了的旧代码。")
say("Cordis 的论文,把后两句分别叫空间上的可组合和时间上的可组合:一个管插件之间的关系,一个管插件走的时候收得干不干净。")
hideshow(verspace, time)
show(firenext)
pulse(...lis)
highlight(tools[0], tools[1])
})
```
⋯ 1 段没有变化
:::notes
先拿一段手写的代码实际跑一下。
Cordis 给的答案,其实很短。
:::
## 每条登记都带着一个撤销
## 第一层:DSH 是一棵插件树
:::article
Cordis 的设计论文把这叫时间上的可组合::cite[移除一个组件时,能完全撤销它产生的副作用]{b=geaa72sq}。具体做法是,:cite[通过 Cordis 接口做的登记都是 effect,卸载时撤销]{b=jv4vmnfp}。
:cite[运行中的 dsh 是一棵插件树,启动时把几层配置依次叠在一个空的条目列表上]{b=t3wa4p4e};:cite[条目的先后不决定加载,插件按服务是否可用来激活]{b=t3wa4p4e}。
:::
:::scene{title="Cordisdsh 怎web 启动时发生了什么收回一个插件的登记(实测)"}
```js
const v1 = box("插件 v1", { tone: "c3", w: 130, h: 50, x: 120, y: 120 })
const MAP = ["插件树", "服务", "插件内部"]
const e1map = boxMAP.map("登记(m, read_file"i) => box(m, { tone: i === 0 ? "accent" : "faint", w: 16076, h: 4026, x: 380560 + i * 84, y: 8018 }))
const e2cmd = box("挂监听器dsh web", { tone: "faintc2", w: 160110, h: 4044, x: 38090, y: 160110 })
const k1 = line(v1, e1)
const layers = ["dsh-base", "dsh-web-app", "home patch", "--patch"].map((n, i) =>
const k2 = line(v1, e2)
box(n, { tone: "faint", w: 130, h: 34, x: 280, y: 80 + i * 40 }))
const u1stack = boxframe("↺ 拿走 read_file依次叠加", { tone: "c5", w: 170, h: 40, xaround: 640layers, ypad: 8010 })
const u2 = box("↺ 摘掉监听器", { tone: "c5", w: 170, h: 40, x: 640, y: 160 })
const toStack = arrow(cmd, stack)
const p1 = arrow(e1, u1, { dashed: true })
const list = box("插件条目清单", { sub: "base 里有 97 条", tone: "c3", w: 150, h: 54, x: 490, y: 140 })
const p2toList = arrow(e2, u2stack, { dashed: true }list)
const orderloader = textbox("卸载:后登记的先撤Loader", { sizetone: 16"c4", boldw: true100, colorh: "c5"44, x: 640680, y: 225140 })
const v3 = box("插件 v3", { tone: "c2", w: 130, h: 50, x: 120, y: 250 })
const toLoader = arrow(list, loader)
const resultroot = textcircle("工具表:read_file v3\n发一次事件:只有监听器 v3 响", { sizer: 169, tone: "c4", x: 450400, y: 250280 })
const r = arrow(v3, result)
const nodes = [-3, -2, -1, 0, 1, 2, 3].map((k) => circle("", { r: 9, tone: k === 1 ? "c2" : "faint", x: 400 + k * 70, y: 360 }))
show(v1)
const edges = nodes.map((n) => line(root, n))
const fromLoader = arrow(loader, root, { bend: 30 })
const rule = text("谁要的服务到齐了,谁先启动", { size: 16, bold: true, color: "accent", x: 400, y: 415 })
show(...map, cmd)
step("插件的每次登记都走dsh Cordisweb 的接口,记在这由两个插件名下组合包加两层补丁叠成。", () => {
say("Cordis 换了个做法。插件的每一次登记,都要通过它的接口来做,它会把这件事记在这个插件名下。")
say("从最外面看。敲下 dsh web,它先找到自己的组合包:dsh-base 和 dsh-web-app,后面还能再叠用户自己的补丁和命令行补丁。")
show(k1, e1toStack, k2stack, e2...layers)
})
step("登记的同时几层依次叠在一个空列表上,留下怎么撤销得到一份插件条目清单。", () => {
say("而且登记的同时,顺手留下怎么撤销:放进去的工具怎么拿出来,挂上的监听器怎么摘掉。")
say("这几层依次叠在一个空列表上,得到一份插件清单。光基础包就有九十七条。")
show(p1, u1, p2toList, u2list)
})
step("Loader 把每个条目挂成一个插件卸载时,撤销倒着执行组成一遍棵插件树。", () => {
say("等这个插件要卸载了,Cordis 就把这些撤销倒着执行一遍,后登记的先撤。")
say("然后 Loader 把清单里的每一条,挂成一个插件,组成一棵插件树。")
show(order)
show(toLoader, loader, fromLoader, root, ...edges, ...nodes)
highlight(u2)
hide(k2, e2, p2)
highlight(u1)
hide(k1, e1, p1)
hide(u1, u2, order)
set(v1, { sub: "已卸载" })
dim(v1)
})
step("同样重载两次,最后只剩最新的一份。", () => {
step("清单的先后不决定启动顺序,按服务是否到齐来激活。", () => {
say("同样重载两次,实测下来,工具表里只剩最新的一个,发事件也只有一个监听器响。")
say("注意,清单的先后不决定谁先启动。一个插件要的服务都到齐了,它才启动,所以没人需要去排顺序。")
show(v3, r, resultrule)
pulse(v3...nodes)
})
```
⋯ 1 段没有变化
:::notes
Cordis 的第一条保证管的是时间:插件走的时候,要把它来的时候做过的事全部撤销。
先从最外面看,一个跑起来的 DSH 是什么样。
:::
## 插件的一生
## 第二层:插件之间只隔着 ctx
:::article
:cite[插件实例的状态从 PENDING 走到 DISPOSED,出错是 FAILED]{b=62zjhzh3};:cite[卸载会递归卸载它挂的子插件]{b=jv4vmnfp}。
DSH 把可替换的能力做成 ctx 上的接缝,比如 :cite[ctx.shell 有本机、沙箱、PowerShell 几个提供方]{b=e8rj3j9z},:cite[使用方 tool-bash 只认 ctx 键]{b=e8rj3j9z}。插件用 inject 声明要用的服务,:cite[服务没到齐之前停在 PENDING]{b=bxhebtem}。
:::
:::scene{title="fiberctx 上的几个状态服务:谁提供,谁使用"}
```js
const namesMAP = ["PENDING", "LOADING", "ACTIVE插件树", "UNLOADING服务", "DISPOSED插件内部"]
const subs = ["等依赖", "加载中", "在跑", "撤销中", "已收干净"]
const map = MAP.map((m, i) => box(m, { tone: i === 1 ? "accent" : "faint", w: 76, h: 26, x: 560 + i * 84, y: 18 }))
const st = names.map((n, i) => box(n, { sub: subs[i], tone: i === 2 ? "c2" : "faint", w: 128, h: 50, x: 80 + i * 160, y: 110 }))
const keys = ["ctx.tools", "ctx.shell", "ctx.sandbox", "ctx.subprocess"]
const links = [0, 1, 2, 3].map((i) => arrow(st[i], st[i + 1]))
const slots = keys.map((k, i) => box(k, { tone: "c4", w: 140, h: 44, x: 130 + i * 180, y: 220 }))
const failed = box("FAILED", { sub: "加载出错", tone: "accent", w: 128, h: 50, x: 240, y: 240 })
const provs = ["dsh-tools", "bash-sandbox", "sandbox-local", "subprocess-local"].map((n, i) =>
const toF = arrow(st[1], failed)
box(n, { sub: "提供", tone: "faint", w: 140, h: 44, x: 130 + i * 180, y: 110 }))
const parent = box("父插件", { tone: "c3", w: 120, h: 44, x: 400, y: 300 })
const pArrows = provs.map((p, i) => arrow(p, slots[i]))
const kidstool = [box("子插件tool-bash", { tonesub: "faint", w: 100, h: 40, x: 300, y: 400面向模型的 }),bash box("子插件工具", { tone: "faintc3", w: 100180, h: 4050, x: 500220, y: 400340 })]
const kl = kids.map((k) => line(parent, k))
const u1 = arrow(tool, slots[0], { dashed: true, label: "inject" })
show(st[0])
const u2 = arrow(tool, slots[1], { dashed: true, label: "inject" })
const u3 = arrow(provs[1], slots[2], { dashed: true, label: "inject", bend: -25 })
const u4 = arrow(provs[1], slots[3], { dashed: true, bend: -35 })
const alt1 = box("bash-local", { tone: "faint", w: 120, h: 34, x: 310, y: 60 })
const alt2 = box("subprocess-ssh", { tone: "faint", w: 140, h: 34, x: 670, y: 60 })
const swap = text("同一个名字,可以换提供方", { size: 16, bold: true, color: "accent", x: 560, y: 340 })
show(...map, ...slots)
step("每个插件实例都有服务占 ctx 上一个 fiber名字,记着它走到了哪由一步个插件提供。", () => {
say("每个插件跑起来,Cordis 都给它配一个叫 fiber 的东西,记着它走到了哪一步。依赖齐了才开始加载,加载完就在跑了。")
say("放大一层,看插件和插件之间。每个服务在 ctx 上占一个名字:工具表、shell、沙箱、子进程,各由一个插件提供。")
show(links[0], st[1], links[1], st[2])
show(...provs, ...pArrows)
})
step("加载时抛了异常bash 工具不引用任何实现,就停在只声明要用 FAILEDtools 和 shell。", () => {
say("加载的时候要是出了错,就停在失败这个状态。")
say("bash 工具不引用任何一个具体实现,它只声明:我要用工具表,我要用 shell。")
show(toFtool, failedu1, u2)
})
step("卸载先进入沙箱 UNLOADINGshell 自己也是使用方,撤销全部跑完才算它要 DISPOSEDsandbox 和 subprocess。", () => {
say("卸载的时候,先进入撤销中,等刚才那些撤销全部跑完,包括异步的,才算真正收干净。")
say("提供 shell 的那个沙箱插件,自己也是使用方,它又要用沙箱和子进程这两个服务。一层托一层。")
show(links[2], st[3], links[3], st[4])
show(u3, u4)
pulse(provs[1])
})
step("它挂上的子插件,会跟着同一起卸个名字可以换提供方:本机版、沙箱版、SSH 版。", () => {
say("插件自己也可以再挂子插件。父插件一卸,子插件跟着一起卸,不会留下没人管的孩子。")
say("同一个名字背后可以换人:shell 有本机版和沙箱版,子进程有本机版,也有走 SSH 的远程版。使用方一个字不用改。")
show(parentalt1, ...klalt2, ...kidsswap)
dim(...kids)
dim(parent)
})
```
⋯ 1 段没有变化
:::notes
撤销什么时候跑,要看插件走到了哪一步。
放大一层,看插件和插件之间是怎么连起来的。
:::
## 依赖写成声明,不写成引用
## 第三层:拆开 bash 工具这个插件
:::article
插件用 inject 声明要用的服务,:cite[服务没到齐之前停在 PENDING]{b=bxhebtem},启动顺序由依赖决定。
tool-bash :cite[注入 tools、shell、systemPrompt 等服务]{b=mm5pv78b},:cite[ctx.tools.register 本身就是一个 effect]{b=mm5pv78b};:cite[通过 Cordis 接口做的登记,插件卸载时都会撤销]{b=jv4vmnfp}。下面“重新加载三次”的对比是实测的,代码附在文末。
:::
:::scene{title="tool-bash 工具等 shell(实测)里面有什么"}
```js
const tool = box("bash 工具", { sub: "inject: ['shell']", tone: "c3", w: 170, h: 56, x: 160, y: 150 })
const MAP = ["插件树", "服务", "插件内部"]
const slotmap = MAP.map((m, i) => box("ctx.shell"m, { subtone: i === 2 ? "空accent", tone: "faint", w: 15076, h: 5626, x: 430560 + i * 84, y: 15018 }))
const need = arrow(tool, slot, { label: "要用", dashed: true })
const src = code("export const inject = ['tools', 'shell', 'systemPrompt', ...]\n\nexport function apply(ctx) {\n ctx.systemPrompt.section({ name: 'tool:bash', ... })\n ctx.tools.register(bashTool)\n}\n\n// 执行时\nctx.shell.execute(spec)", { lines: [], x: 280, y: 200 })
const statebox1 = textframe("状态:PENDING 工具表:[]tool-bash", { size: 16, mono: true, xaround: 300[src], ypad: 30014 })
const localn1 = boxtext("本机 shell要用的服务,没到齐就等着", { tonesize: "c4"15, wcolor: 140"c2", halign: 48"start", x: 680590, y: 150120 })
const fill = arrow(local, slot, { label: "提供" })
const n2 = text("往提示词和工具表里登记", { size: 15, color: "c3", align: "start", x: 590, y: 195 })
const outn3 = text("bash 工具启动 → 本机执行不知道背后是哪个 lsshell", { size: 1615, boldcolor: true"c4", coloralign: "c4start", x: 300590, y: 360280 })
show(tool)
const test = text("重新加载 3 次(实测)\n手写登记:留下 3 个工具\nCordis:只剩 1 个", { size: 15, bold: true, color: "accent", align: "start", x: 590, y: 370 })
show(...map, box1, src)
step("bash 工具不引用哪个具体的 shell,只inject:声明要用 ctx.shell哪些服务。", () => {
say("bash 工具不去引用哪个具体的 shell,它只声明一句:我要用 shell 这个服务。至于谁来提供,它不管。")
say("再放大一层,打开这个插件。第一行是依赖声明:工具表、shell、系统提示词,缺一个,它就等着不启动。")
show(need, slot)
set(src, { lines: [1] })
show(n1)
})
step("还没人apply:往系统提供 shell,它就停在 PENDING,示词里加一条也不段说明,往工具表里登记 bash。", () => {
say("这时候还没人提供 shell,它就停在等依赖的状态,工具表里一条也没登记,不会跑一半。")
say("启动的时候它做两件事:往系统提示词里加一段 bash 的用法,再往工具表里登记 bash 这个工具。")
show(state)
set(src, { lines: [4, 5] })
show(n2)
})
step("本机执行时调用 ctx.shell 一挂上,它才启动,拿到的具体是本机的实现哪个 shell 它不管。", () => {
say("本机 shell 一挂上,它马上启动,拿到的就是本机的实现。")
say("真要跑命令时,它只调用 ctx 上的 shell。背后是本机还是沙箱,它不知道,也不用知道。")
show(local, fill)
set(src, { lines: [9] })
set(slot, { sub: "本机" })
show(n3)
set(state, { text: "状态:ACTIVE 工具表:[bash]" })
show(out)
})
step("两处登记都会在插件卸载时撤销。", () => {
say("刚才那两处登记,都是可撤销的。实测把一个插件重新加载三次:手写的登记留下三份,用 Cordis 只剩最新的一份。")
set(src, { lines: [4, 5] })
show(test)
})
```
:::
:::notes
第二条保证管的是空间,也就是插件和插件之间的依赖。
再放大一层,打开一个插件看看里面。
:::
## 运行中把 shell 换成沙箱
## 跟着这条请求走:先问模型
:::article
inject 不是一次性检查::cite[服务消失,依赖它的插件跟着卸载;服务回来,再加载]{b=bxhebtem}。下面的毫秒数是实测的。
下面三页按 DSH 文档里一轮对话的顺序走:先 :cite[组装提示词和工具 schema]{b=d9c52v09},再 :cite[经过 agent/request 发请求、llm/stream 流式返回]{b=d9c52v09}。
:::
:::scene{title="换 shell 时 bash 工具经历了什么(实测)"}
:::scene{title="“跑一下测试”进来以后"}
```js
const tool = box("bash 工具", { tone: "c2", w: 150, h: 50, x: 150, y: 110 })
const MAP = ["进来", "问模型", "调用工具", "交回去"]
const slotmap = MAP.map((m, i) => box("ctx.shell"m, { subtone: i <= 1 ? "本机accent", tone: "c4faint", w: 15076, h: 5026, x: 400520 + i * 84, y: 11018 }))
const use = arrow(tool, slot)
const msg = box("帮我跑一下测试", { tone: "c2", w: 170, h: 44, x: 110, y: 110 })
const localturn = box("本机 shellturn/start", { tone: "c4faint", w: 140120, h: 4640, x: 650110, y: 60200 })
const sand = box("沙箱 shell", { tone: "c5", w: 140, h: 46, x: 650, y: 170 })
const t1 = arrow(msg, turn)
const fromL = arrow(local, slot)
const sp = box("系统提示词", { sub: "含 bash 的用法", tone: "c3", w: 140, h: 50, x: 330, y: 150 })
const fromS = arrow(sand, slot)
const ts = box("工具 schema", { sub: "来自 ctx.tools,含 bash", tone: "c3", w: 160, h: 50, x: 330, y: 230 })
const rows = [
const req = box("请求", { tone: "c4", w: 90, h: 44, x: 520, y: 190 })
"26ms 卸载本机 shell",
const a1 = arrow(turn, sp)
"26ms bash 工具卸载,工具表移除 bash",
const a2 = arrow(turn, ts)
"38ms bash 工具状态:PENDING",
const a3 = arrow(sp, req)
"38ms 挂上沙箱 shell",
const a4 = arrow(ts, req)
"39ms bash 工具重新启动:沙箱执行 ls",
const llm = box("ctx.llm", { tone: "c4", w: 110, h: 44, x: 690, y: 190 })
]
const a5 = arrow(req, llm, { label: "agent/request" })
const lines = rows.map((s, i)reply => textbox(s"assistant/message", { sizesub: 15,"调用 mono:bash:npm truetest", aligntone: "startc2", xw: 170210, yh: 27054, +x: i600, *y: 34330 }))
show(tool, use, slot, local, fromL)
const a6 = arrow(llm, reply, { label: "llm/stream" })
const hook = text("这两个事件是 waterfall,插件可以在这里改请求、包住流", { size: 14, color: "soft", x: 400, y: 420 })
show(...map, msg)
step("卸掉本机消息进来,Agent shell开始一轮。", () => {
say("现在把本机 shell 卸掉。")
say("第一步,消息进来,Agent 开始新的一轮,先在会话里记下一个开始。")
show(lines[0]t1, turn)
hide(fromL)
dim(local)
set(slot, { sub: "空", tone: "faint" })
})
step("bash 组装请求:系统提示词,加上工具跟着卸载,它表里所有工具的登记一起撤销 schema。", () => {
say("bash 工具没有人去通知它,它自己就跟着卸载了,工具表里的 bash 也一起撤掉。")
say("然后组装请求:系统提示词,再加上工具表里每个工具的说明书。刚才 bash 插件登记的那两样,就是在这里派上用场的。")
show(lines[1], lines[2])
show(a1, sp, a2, ts, a3, a4, req)
set(tool, { tone: "faint" })
dim(use)
})
step("挂上沙箱 shell,bash请求发给 工具重新启动ctx.llm,用上新实现模型流式回答。", () => {
say("再把沙箱 shell 挂上,bash 工具重新启动,这回拿到的就是沙箱。前后一共十几毫秒,bash 工具的代码一个字没改。")
say("请求交给 ctx 上的模型服务,模型一个字一个字地流式回答。")
show(sanda5, fromSllm)
setsend(slota5, { sub: "沙箱", tonelabel: "c5请求" })
undimshow(usehook)
set(tool, { tone: "c2" })
show(lines[3], lines[4])
pulse(tool)
})
step("模型的回答里带着一个工具调用:bash,npm test。", () => {
say("回答收完,记进会话。里面带着一个工具调用:用 bash 跑 npm test。")
show(a6, reply)
pulse(reply)
})
```
:::
:::notes
接着把 shell 换掉。
最后一幕,跟着这条请求,按真实的顺序走一遍。
:::
## DSH 里就是这么换的
## 工具调用穿过的流水线
:::article
dsh-bash-sandbox :cite[同样注册成 ctx.shell,面向模型的 bash 工具不用改]{b=rt1xr9fr},:cite[沙箱跑不起来时报 SANDBOX_UNAVAILABLE]{b=rt1xr9fr}。:cite[同一个上下文只能有一个 shell 实现]{b=p6thj0fr},:cite[基础配置按 process.platform 挑沙箱版]{b=p6thj0fr}。
:cite[pre-execute 默认放行,返回 ask 才交给 ctx.approval]{b=8mevct0j},之后过守卫检查,再进入 tools/execute。沙箱 shell :cite[把 bash -c 交给 ctx.sandbox 包一层隔离]{b=5k7mr1me},再通过 ctx.subprocess 启动进程;:cite[基础配置默认 workspace-write,只允许写工作区]{b=5k7mr1me}。:cite[要越出沙箱权限时才申请审批,批准只管这一次]{b=qf8kfwrh}。
:::
:::scene{title="DSHbash: 的基础配置怎么挑npm shelltest 从调用到结果"}
```js
const cfg = code("- id: bash-sandbox\n disabled: win32\n- id: pwsh-sandbox\n disabled: 非 win32\n- id: tool-bash", { lines: [1, 2], x: 160, y: 150 })
const MAP = ["进来", "问模型", "调用工具", "交回去"]
const slotmap = MAP.map((m, i) => box("ctx.shell"m, { tone: i === 2 ? "c5accent" : "faint", w: 15076, h: 5026, x: 470520 + i * 84, y: 15018 }))
const toolcall = box("bash 工具tool/call", { sub: "dsh-tool-bash", tone: "c2faint", w: 160110, h: 5440, x: 70090, y: 150100 })
const use = arrow(tool, slot)
const pre = box("tools/pre-execute", { sub: "默认放行", tone: "c3", w: 170, h: 50, x: 290, y: 100 })
const bashSexe = box("dsh-bash-sandboxtools/execute", { sub: "macOS、Linux", tone: "c5c3", w: 180150, h: 5044, x: 380510, y: 300100 })
const pwshSpost = box("dsh-pwshtools/post-sandboxexecute", { sub: "Windows", tone: "faintc3", w: 180170, h: 5044, x: 600700, y: 300100 })
const pbs1 = arrow(bashScall, slotpre)
const pps2 = arrow(pwshS, slotpre, { dashed: true }exe)
const local = text("想在本机跑:换成 dsh-bash-local,bash 工具不用改", { size: 15, color: "c4", x: 470, y: 390 })
const s3 = arrow(exe, post)
const failappr = textbox("沙箱起不来 → SANDBOX_UNAVAILABLE,不会退回本机ctx.approval", { sizesub: 16"问人", boldtone: true"c5", colorw: "accent"140, h: 46, x: 470290, y: 430200 })
show(cfg, slot)
const askA = arrow(pre, appr, { dashed: true, label: "ask" })
const shell = box("ctx.shell", { sub: "bash-sandbox", tone: "c4", w: 150, h: 50, x: 510, y: 220 })
const sb = box("ctx.sandbox", { sub: "macOS:Seatbelt\nLinux:bwrap", tone: "c5", w: 160, h: 62, x: 400, y: 340 })
const sp = box("ctx.subprocess", { sub: "启动进程", tone: "c4", w: 150, h: 50, x: 620, y: 340 })
const e1 = arrow(exe, shell, { label: "bash 工具" })
const e2 = arrow(shell, sb, { label: "bash -c 'npm test'" })
const e3 = arrow(shell, sp)
const mode = text("默认 workspace-write:只能写工作区", { size: 15, bold: true, color: "c5", x: 400, y: 420 })
const res = box("tool/result", { tone: "c2", w: 130, h: 44, x: 700, y: 220 })
const r1 = arrow(post, res)
show(...map, call)
step("几个 shell 实现都注册成 ctx.shell,一个上下文只挂一个。", () => {
step("先记下 tool/call,再过 tools/pre-execute:没人拦就放行。", () => {
say("DSH 里真有好几个 shell 实现:本机的、沙箱的、PowerShell 的,都把自己注册成 shell,一个上下文里只能挂一个。")
say("模型说要用 bash 跑测试。这次调用先记进会话,然后过第一道关:执行前的事件。没有插件拦,默认就放行。")
show(bashS, pb, pwshSs1, pppre)
})
step("基础配置按系统挑:默认沙箱版,Windows有插件要拦就返回 换成ask,交给 PowerShellctx.approval 沙箱去问人。", () => {
say("它的基础配置就是这么挑的:默认挂 bash 的沙箱版,到了 Windows 上把它关掉,换成 PowerShell 的沙箱版。")
say("要是有插件觉得这事得问一下人,就在这里返回一个询问,交给审批服务。审批不是写死在工具里的,是挂在这道关上的。")
pulseshow(cfgaskA, appr)
highlight(bashS)
})
step("用 shell 的放行后进入 tools/execute,bash 工具不知道背后是哪个实现调用 ctx.shell。", () => {
say("用 shell 的 bash 工具根本不知道背后是哪个。想改回本机跑,换一个插件就行,它一个字不用动。")
say("放行以后进入执行。bash 工具这时候才调用 ctx 上的 shell,在基础配置里,它就是沙箱版的 shell。")
show(tools2, useexe, locale1, shell)
})
step("沙箱起不来就直接报错 shell 先包一层隔离,不会悄悄换回本机再启动进程跑 npm test。", () => {
say("还有个细节:沙箱要是起不来,命令直接报沙箱不可用,绝不会悄悄退回本机去跑。")
say("沙箱 shell 不直接跑命令。它先把 bash 减 c npm test 交给沙箱服务包一层隔离,Mac 上用 Seatbelt,Linux 上用 bwrap,再交给子进程服务真正启动。默认只允许写工作区。")
show(fail)
show(e2, sb, e3, sp)
show(mode)
})
step("结果经过 tools/post-execute,记成 tool/result。", () => {
say("命令跑完,结果再过一道执行后的关,最后记进会话,成为这次调用的结果。")
show(s3, post, r1, res)
pulse(res)
})
```
:::
:::notes
这不是只在实验里成立。
模型说要用 bash,这次调用要穿过一条流水线。
:::
## 同一个插件,两个作用域
## 结果交回模型,这一轮结束
:::article
ctx.isolate(name) :cite[给子上下文一个独立的服务作用域,换实现不影响父作用域]{b=g3dsvf0v};:cite[DSH 标准预设里,计划模式和压缩的插件组都用了 isolate]{b=g3dsvf0v}。下面主 Agent、子 Agent 的分法是实验里的设定。
:cite[工具结果需要再请求模型就进入下一步,否则经过 agent/turn-stopping 后 turn/end]{b=d9c52v09}。图里测试输出和回答的内容是示意。
:::
:::scene{title="主作用域和隔离出来的作用域(实测)"}
:::scene{title="一轮对话怎么收尾"}
```js
const lShell = box("本机 shell", { tone: "c4", w: 140, h: 46, x: 170, y: 120 })
const MAP = ["进来", "问模型", "调用工具", "交回去"]
const lToolmap = boxMAP.map("bash(m, 工具"i) => box(m, { subtone: "主i Agent=== 3 ? ",accent" tone: "c2faint", w: 14076, h: 5026, x: 170520 + i * 84, y: 23018 }))
const lUse = arrow(lTool, lShell)
const res = box("tool/result", { sub: "示意:3 passed", tone: "c2", w: 150, h: 50, x: 110, y: 130 })
const lOutstep2 = textbox("本机执行 rm -rf build下一步:再问模型", { sizetone: 15"c4", monow: true170, colorh: "c4"46, x: 170340, y: 320130 })
const rootans = framebox("根上下文测试全部通过", { aroundsub: [lShell"不再调用工具", lTooltone: "c2", lOut]w: 160, padh: 2250, x: 580, y: 130 })
const sShellstop = box("沙箱 shellturn/end", { tone: "c5faint", w: 140110, h: 4640, x: 590580, y: 120230 })
const sTool = box("bash 工具", { sub: "子 Agent", tone: "c2", w: 140, h: 50, x: 590, y: 230 })
const a1 = arrow(res, step2)
const sUsea2 = arrow(sToolstep2, sShellans)
const sOut = text("沙箱执行 rm -rf build", { size: 15, mono: true, color: "c5", x: 590, y: 320 })
const a3 = arrow(ans, stop, { label: "agent/turn-stopping" })
const sub = frame("isolate('shell')", { around: [sShell, sTool, sOut], pad: 22 })
const used = ["agent-loop", "system-prompt", "dsh-tools", "llm", "tool-bash", "bash-sandbox", "sandbox-local", "subprocess-local"]
const same = arrow(lTool, sTool, { label: "同一个插件", dashed: true, bend: -30 })
const chips = used.map((n, i) => box(n, { tone: "c3", w: 150, h: 34, x: 115 + (i % 4) * 190, y: 330 + Math.floor(i / 4) * 48 }))
show(root, lShell, lTool, lUse)
const all = frame("这一轮用到的插件", { around: chips, pad: 12 })
show(...map, res)
step("根上下文里,bash 工具用的是本机 shell结果要交回模型,就开下一步,再发一次请求。", () => {
say("在根上下文里挂着本机 shell,主 Agent 的 bash 工具接的就是它。这时候在同一个上下文里再挂一个沙箱 shell,实测会直接报错,说 shell 已经被注册过了。")
say("命令跑完还没结束。工具的结果要交回给模型,于是开始下一步,再发一次请求。")
show(lOuta1, step2)
})
step("用 isolate 隔模型读到测试输出一支,在里面挂沙箱 shell不再调用工具,给出回答。", () => {
say("再从根上隔出一支,专门给 shell 开一个独立的作用域,在里面挂上沙箱 shell。")
say("模型读到测试的输出,觉得事情办完了,不再调用工具,直接回答:测试全部通过。")
show(suba2, sShellans)
})
step("同一个 bash 没有欠下的工具挂进作,这一支,拿到的就是沙箱轮结束。", () => {
say("把同一个 bash 工具插件也挂进这一支,它拿到的就是沙箱。两边同时在跑,谁也不影响谁。")
say("没有欠下的工作,这一轮就结束了。")
show(same, sTool, sUsea3, sOutstop)
pulse(sOut)
})
step("一路经过的每一站,都是某个插件挂上去的。", () => {
say("回头数一数,这一路经过的每一站,Agent 循环、提示词、工具表、模型、bash、沙箱、子进程,全是一个个插件挂上去的。")
show(all, ...chips)
})
```
:::
:::notes
一个上下文只能挂一个 shell,那要两个同时用呢?
命令跑完,还没结束。
:::
## 热重载就是先卸再装
## 跑着的时候换掉 shell,流程不用改
:::article
热重载建立在同一套机制上::cite[先卸载撤销 effect,再按依赖加载新代码]{b=8hecejrw}。
:cite[服务消失,依赖它的插件跟着卸载;服务回来,再加载]{b=bxhebtem}。下面的毫秒数是实测的。:cite[DSH 的基础配置按 process.platform 挑沙箱版 shell]{b=p6thj0fr},:cite[bash 工具不用改]{b=rt1xr9fr}。:cite[改代码后的热更新也是先卸载撤销 effect,再按依赖加载新代码]{b=8hecejrw}。
:::
:::scene{title="改一个文件之后"}
:::scene{title="换 shell 时 bash 工具经历了什么(实测)"}
```js
const file = box("bash-tool.ts", { sub: "保存", tone: "faint", w: 160, h: 50, x: 120, y: 120 })
const pipe = ["pre-execute", "execute", "ctx.shell", "post-execute"].map((n, i) =>
const oldP = box("旧实例"n, { tone: i === 2 ? "c3c4" : "faint", w: 130, h: 5036, x: 380130 + i * 180, y: 12050 }))
const newP = box("新实例", { tone: "c2", w: 130, h: 50, x: 640, y: 120 })
const pl = [0, 1, 2].map((i) => arrow(pipe[i], pipe[i + 1]))
const a1tool = arrowbox(file,"bash oldP工具", { labeltone: "卸载c2", w: 140, h: 46, x: 150, y: 160 })
const a2 = arrow(oldP, newP, { label: "加载" })
const slot = box("ctx.shell", { sub: "本机", tone: "c4", w: 140, h: 46, x: 400, y: 160 })
const undo = text("撤销旧实例的登记", { size: 15, color: "c5", x: 380, y: 200 })
const use = arrow(tool, slot)
const depslocal = textbox("按 inject本机 等依赖到齐shell", { sizetone: 15"c4", colorw: "c2"130, h: 40, x: 640650, y: 200130 })
const samesand = textbox("和换沙箱 shell 是同一套机制", { sizetone: 18"c5", boldw: true130, colorh: "accent"40, x: 400650, y: 290200 })
show(file, oldP)
const fromL = arrow(local, slot)
const fromS = arrow(sand, slot)
const rows = [
"26ms 卸载本机 shell",
"26ms bash 工具卸载,工具表移除 bash",
"38ms bash 工具状态:PENDING",
"38ms 挂上沙箱 shell",
"39ms bash 工具重新启动:沙箱执行 ls",
]
const lines = rows.map((s, i) => text(s, { size: 14, mono: true, align: "start", x: 190, y: 270 + i * 30 }))
const same = text("流水线上其余的环节一个没动;改代码后的热更新,走的也是这条路", { size: 15, bold: true, color: "accent", x: 400, y: 430 })
show(...pipe, ...pl, tool, use, slot, local, fromL)
step("保存文件后,先把旧实例卸掉本机 shell,bash 工具跟着卸载,登记全部一起撤销。", () => {
say("你改了 bash 工具的代码,一保存,旧实例先卸掉,它登记过的东西全部撤销。")
say("最后一个转折:要是在跑的过程中,把 shell 换掉呢?实测把本机 shell 卸掉,bash 工具没人通知,自己就跟着卸载了,工具表里的 bash 也一起撤掉。")
show(a1, undo)
show(lines[0], lines[1], lines[2])
dimhide(oldPfromL)
dim(local)
set(slot, { sub: "空", tone: "faint" })
set(tool, { tone: "faint" })
dim(use)
})
step("再加载新代码挂上沙箱 shell,照样按依赖bash 工具重新启动,前后十几毫秒。", () => {
say("然后加载新代码,照样等它要的服务都在了再启动。")
say("再挂上沙箱 shell,bash 工具重新启动,接上了新的实现,前后十几毫秒。")
show(a2, newPsand, depsfromS)
set(slot, { sub: "沙箱", tone: "c5" })
undim(use)
set(tool, { tone: "c2" })
show(lines[3], lines[4])
pulse(tool)
})
step("热重载流水线上其余环节不需要额外用改;改代码后的魔法热更新也是先卸再装。", () => {
say("所以热重载不是另外一套东西,跟刚才换 shell 走的是同一条路。")
say("整条流水线,除了 shell 这一站,别的一个没动。DSH 的基础配置就是这样按系统挑 shell 的。你改了插件代码以后的热更新,走的也是这条路:先卸掉,再装上。")
show(same)
pulse(pipe[2])
})
```
⋯ 1 段没有变化
:::notes
前面两条保证一接上,热重载就是顺手的事。
最后看一个转折。
:::
⋯ 8 段没有变化
const d1 = box("关连接", { tone: "faint", w: 110, h: 40, x: 120, y: 220 })
const d2 = box("删临时文件", { tone: "faint", w: 130, h: 40, x: 290, y: 220 })
const par = text("异步撤销是并发同时跑的", { size: 16, bold: true, color: "c5", x: 560, y: 220 })
const raw = code("setInterval(tick, 200)", { x: 200, y: 350 })
const leak = text("绕开 Cordis 的资源,卸载时没人收", { size: 16, bold: true, color: "accent", x: 540, y: 350 })
⋯ 4 段没有变化
})
step("多个异步撤销会同时跑,有先后要求的要写在一起。", () => {
say("第二,撤销虽然倒着启动开始,但几个异步的撤销是同时跑的。比如先关连接再删文件,这种有先后的,得写进同一个撤销里。")
show(d1, d2, par)
pulse(d1, d2)
})
step("自己开的定时器、连接,不包进 effect 就不会被收。", () => {
say("第三,Cordis 只收它知道的东西。自己随手开一个定时器,不包进通过它的接口里,卸载的时候照样没人管。")
show(raw, leak)
})
⋯ 2 段没有变化
:::notes
这套东西办法也不是没有代价。
:::
## 两条保证
## 回到那三句话
:::scene{title="Cordis 给三句话,在 harnessDSH 的两条保证里各落在哪"}
```js
const tH = box("时间", { tone: "c3", w: 120, h: 50, x: 220, y: 90 })
const cards = [
const sH = box("空间每个能力都是插件", { tone: "c2c3", w: 120200, h: 5046, x: 580140, y: 9080 }),
const tB = text box("插件走的时候\n登记过的东西全部撤销之间只隔着 ctx", { sizetone: 17"c2", w: 210, h: 46, x: 220400, y: 20080 }),
const sB = text("依赖的服务变了\n用它的插件跟着重启", { size: 17, x: 580, y: 200 })
box("登记过的都能撤销", { tone: "c5", w: 200, h: 46, x: 660, y: 80 }),
const both = text("Agent 跑着的时候增、删、换能力,不用重启,也不留残余", { size: 18, bold: true, color: "accent", x: 400, y: 320 })
]
show(tH, sH)
const where = [
text("97 个条目长成一棵插件树\n按服务到齐来启动", { size: 15, x: 140, y: 190 }),
text("bash 只认 ctx.shell\n本机、沙箱、SSH 随便换", { size: 15, x: 400, y: 190 }),
text("卸掉一个插件\n工具表里不留残余", { size: 15, x: 660, y: 190 }),
]
const lk = where.map((w, i) => arrow(cards[i], w))
const end = text("所以 Agent 跑着的时候,能力可以随时装、拆、换", { size: 18, bold: true, color: "accent", x: 400, y: 320 })
show(...cards)
step("时间:插件卸载时,登记过的东西全部撤销树:97 个条目按服务到齐来启动。", () => {
say("回头看,Cordis 给了 harness 两条保证。一条管时间:插件走的时候,来的时候做过的事全部撤销。")
say("回到开头那三句话。每个能力都是插件,在 DSH 里,就是那棵九十七个条目的插件树,按服务到齐的先后启动。")
show(tB)
show(lk[0], where[0])
})
step("空间ctx:依赖的服务变了bash 只认 shell 这个名字,用它的插件跟着变实现随便换。", () => {
say("一条管空间:插件之间只声明依赖,依赖的服务一变,用它的插件跟着卸载、跟着重启。")
say("插件之间只隔着 ctx,所以 bash 只认 shell 这个名字,背后是本机、沙箱还是远程,随便换。")
show(sB)
show(lk[1], where[1])
})
step("两条合起来,Agent 跑着就能增删换能力撤销:卸掉一个插件,工具表里不留残余。", () => {
say("两条登记过的都能撤销,所以卸掉一个插件,工具表里干干净净。三句话合在一起,一个Agent 跑着的 Agent时候,能力就能随时加能力装、去能力拆、换能力,不用重启,也不留残余。今天就聊到这儿。")
show(both)
show(lk[2], where[2])
show(end)
})
```
⋯ 1 段没有变化
:::notes
最后收个尾。
最后,回到开头那三句话。
:::
⋯ 1 段没有变化
::::article
三两个实验都在 DSH 仓库(提交 ce7073d)里用 tsx 运行,Cordis 从仓库的 `vendor/cordis/lib/index.js` 引入。下面把引入路径写成包名,实际运行时换成你本地 DSH 仓库里那个文件的路径。
:::fold{title="实验一:手写登记和 Cordis 各重新加载三次"}
```ts
import { Context } from '@deepseek-ai/cordis'
⋯ 99 段没有变化
```
:::
:::fold{title="实验三:用 isolate 给一支单独的 shell"}
```ts
import { Context, Service } from '@deepseek-ai/cordis'
class LocalShell extends Service {
constructor(ctx: Context) { super(ctx, 'shell') }
run(c: string) { return `本机执行 ${c}` }
}
class SandboxShell extends Service {
constructor(ctx: Context) { super(ctx, 'shell') }
run(c: string) { return `沙箱执行 ${c}` }
}
const bashTool = (who: string) => ({
name: 'bash-tool',
inject: ['shell'],
apply(ctx: any) { console.log(`${who} 的 bash 工具 → ${ctx.shell.run('rm -rf build')}`) },
})
const root = new Context()
root.plugin(LocalShell)
root.plugin(bashTool('主 Agent'))
const sub = root.isolate('shell')
sub.plugin(SandboxShell)
sub.plugin(bashTool('子 Agent'))
```
输出:
```text
主 Agent 的 bash 工具 → 本机执行 rm -rf build
子 Agent 的 bash 工具 → 沙箱执行 rm -rf build
```
:::
::::
首次发布