* feat(system-prompt): admit section updates incrementally * docs(system-prompt): accept retirement restart gap * test(system-prompt): cover retirement and reentry before admission * test(system-prompt): align rebase fixtures and persistence history * fix: align section prompt consumers and CI fixtures * test: pin credentials for section snapshot fixtures * test: align remaining snapshots with section admission * test: preserve section metadata in Web and PowerShell replays * refactor(system-prompt): retire sections through Cordis cleanup * chore: refresh section admission history after rebase * fix(system-prompt): advance admission head after compaction * fix(system-prompt): trim sections before admission hashing * docs: move system upgrade guides to current version * docs: add system message contribution cookbook * perf(system-prompt): avoid indexing history for admission * refactor(system-prompt): derive admission from logged sections * test(system-prompt): update lifecycle and compaction consumers * test(system-prompt): refresh CLI section event expectations * test(system-prompt): refresh question composer section metadata * feat(system-prompt): resolve same-name sections by scope * refactor(persona): use ordinary section registrations * refactor(system-prompt): clarify assembly and related control flow * test(system-prompt): reconcile rebased section snapshots * test(python): use current system text helper after rebase * test(system-prompt): refresh CLI expected section blocks * test(system-prompt): reconcile latest master integration * test(agent-loop): seed tools before deferred addition assertion * refactor(compaction): replace system heads when compacting prefixes * refactor(system-prompt): separate source text and notice attribution * fix(system-prompt): preserve legacy instructions in compaction snapshots * refactor(compaction): build system snapshots while reading the summary prefix * refactor(compaction): rebuild current system instructions * refactor(system-prompt): defer compaction rebuild to admission * refactor(system-prompt): remove redundant fixtures and setup * fix(plan-mode): announce committed user transitions * docs(system-prompt): reconcile section lifecycle and compaction design * refactor(system-prompt): consolidate projection version increments * fix(system-prompt): reconcile master integration and replay fixtures * docs(system-prompt): align upgrade guide with current release * refactor(system-prompt): make admission operations explicit * fix(system-prompt): reconcile master API and release changes * docs(system-prompt): regenerate catalogs after master refresh * refactor(llm-pi-ai): delegate text rendering to pi-ai * fix(llm-pi-ai): preserve blank lines between system text parts * refactor(system-prompt): remove redundant retirement metadata * refactor(system-prompt): own admission and use canonical surface targets * refactor(system-prompt): compare captured sections at commitment Assembly fixes section text and lifecycle; PromptAssembly.systemUpdates() compares them with the committed projection when the loop commits. This replaces SystemPrompt.admit(), needsRebuild() and isCurrent(), the SystemAdmission export, the loop's reassembly after compaction, and SessionSurface.systemHeadResetSeq. The projection records a pending rebuild as a boolean that an attributed reset clears. After a compaction inside a step, the rebuilt head uses the sections captured for that step; later source changes arrive as ordinary updates at the next step. * test: await the session read fixture and type a matcher for lint * test(tool-executor-ptc): describe same-name section override on executor replacement Duplicate section names no longer reject a scoped executor; the earlier registration is overridden and returns as an ordinary update when the executor leaves. * docs(upgrade-guide): move system-prompt integrations to v0.2.2-rc.3 The root version on master is 0.2.2-rc.3; verify-upgrade-guides rejects a guide added under an earlier version directory. * refactor(system-prompt): rebuild over system messages without sections A system message without source.sections now marks rebuilding pending, like a compaction summary. The next model attempt replaces the head with current sections and clears later system messages. This removes the separate legacy flag and its appended whole-prompt replacement and notice. Sessions written by earlier releases, migrated logs, and imported DeepSeek conversations take the same path, so the importer and its tests return to their master versions. * test(web): refresh the imported-conversation recording and drop a stray bytecode file The Web recording for a resumed DeepSeek import was recorded while the prompt was appended after the imported history; the head is replaced again, as on master. The .pyc under the macOS signing skill was staged by accident in an earlier merge. * refactor(session): scan system nodes on read surface.systemNodes is read only when a system rebuild is written. Scanning the current surface nodes on each read restores the surface fold's plan types and state to their master definitions, which no longer carry the system message. * refactor(session): rename SystemSurfaceNode.hasContent to isEmpty The flag reports that the logged content array has no blocks. isEmpty matches the documented "empty head" wording and covers a head that was written empty without ever being cleared. * refactor(llm): join system text through extractTextContent extractTextContent gains a trim option that trims each text block and omits blocks left empty. The DeepSeek serializer, the pi-ai converter, and the prompt inspector use it with a blank-line separator in place of three hand-written joins. The pi-ai converter now trims system and developer text like the other two, so whitespace-only blocks are omitted there as well. * test(snapshots): refresh the PowerShell and cordis-inspect recordings The two PowerShell scenarios still held the initial system message as one unattributed block; replay now writes one block per section with its attribution. The cordis-inspect-jsdoc recording embeds the API catalog, which names SystemSurfaceNode.isEmpty since the rename. * test(plan-mode,harness-dev-context): cover a rejected pre-step and a repeated legacy prompt A rejected pre-step must leave a queued plan-mode switch pending and unannounced until the next accepted step. A second unattributed system message with the same legacy paragraphs must keep the admitted paragraphs. * test(web): refresh Web recordings for section-attributed system messages Twenty-seven Web recordings still held the initial system message as one unattributed block. Replay now writes one text block per section with its attribution; twenty-six recordings differ only in that line. plan-review also records the plan-mode switch notice before the first prompt and appends a plan:status update on exit in place of a head replacement and a series header, so its expanded golden gains the notice row. The Creator persona test joins the system text blocks with blank lines, as the adapters do, before matching the persona line. * refactor(system-prompt): require the retry argument of systemUpdates The agent loop passes it on every attempt. A custom loop that omitted it on a retry received a full comparison in place of the pending rebuild, with no diagnostic; the type now makes each caller state which attempt it is. * docs(upgrade-guide): move the system prompt integrations guide to v0.2.2-rc.5 The root version is 0.2.2-rc.5 after the master merge, and verify-upgrade-guides rejects a guide added under an earlier version directory. * test(web): refresh the mention path presence recording for section-attributed system messages * test: align two specs that master left failing The DeepSeek settings spec waited for a saved notice carrying the provider label from before the rename to DeepSeek API. The Windows packaging workflow spec seeded one of the two helpers that the Stage workflow helpers step copies. * chore: remove a Python cache file committed by mistake
18 KiB
DeepSeek Harness 架构
English | 中文
改动 packages/ 下的任何内容之前,请先阅读本文。本文假定你已了解 Cordis;如果尚未了解,请先阅读入门或教程。
Cordis
Cordis 是 dsh 底层的框架:插件向共享上下文贡献服务、类型化事件和可逆的副作用。产品的每一部分都是插件,包括模型适配器、工具注册表、会话日志,以及 agent loop(智能体循环)本身,因此每个都可以从配置替换。
不存在需要打补丁的特权内核:扩展 dsh 的方式是把插件挂载到其他插件旁边,而各项注册都是副作用,会在其插件卸载时撤销。
Profile 与组合包
运行中的 dsh 是一棵插件树,由启动时按序叠加的各层组合而成。
profile 是存放在 Harness home 中的具名组装。它列出自己叠放的组合包,存放自己安装的树外插件,并保存用户自己的 cordis.patch.yml。web、headless、sdk、sdk-minimal 和 acp 作为模板随发行版交付。
组合包是 Cordis 配置项及其挂载代码的分发格式,因此它插入的内容始终可被其上各层 patch。
两者都在各自的 package.json 中通过 dsh 字段声明自己:dsh.profile 列出一个 profile 的组合包,dsh.bundle 指向一个组合包的 patch 文件。
dsh-base 是 web、headless、sdk 与 acp profile 的共享第一层:模型适配器、工具、持久化、沙箱与审批策略、设置、凭据、遥测。dsh-web-app 增加浏览器应用,dsh-headless 增加不带服务器的一次性运行器,dsh-sdk-app 增加 SDK JSON-RPC 服务器,dsh-acp-app 增加仅用于自动化的 ACP 服务器。dsh-sdk-minimal 是刻意保留的例外:一个组合包拥有完整的显式 SDK 配置树,不应用 dsh-base。
各层依次应用在空条目列表之上:profile 中有序的组合包、其 cordis.patch.yml、home patch,最后是 --patch overlay。一条 patch 按 id 定位某个条目并替换其整个 config,或插入新条目。preset patch 在目标声明的 config.plugins 内应用这些操作,沿用相同的层优先级。
YAML 控制 HMR:base 启用仅监视配置的 dsh-hmr;Headless、SDK 和 ACP 禁用它;sdk-minimal 不包含它。Profile patch 覆盖这些默认值。HMR 协调监听和重载;启动器提供 profile 数据和就绪信号。
base 提供用于 Web 和 Agent 的插件管理器。
要查看你的机器启动的配置树:
dsh --profile web --dump-config
它打印出的任何条目,都可以由你自己的 patch 替换。
应用启动
受支持的 Node 应用通过具名 dsh profile 启动。随附 profile 为 web、headless、sdk、sdk-minimal 和 acp,可通过 dsh --profile <name> 或 dsh <name> 选择。plugin 表示管理命令;同名 profile 必须用 --profile plugin 选择。TypeScript SDK 会解析其同版本 dsh 依赖并选择 sdk;自定义插件组合继续由 profile 与有序 patch 文件表达,而不是另一个可执行文件或内联应用树。sdk-minimal 是位于同一 launcher 后的仓库自有独立组合包,而不是由调用方提供的 Cordis 配置树。
Vendored CLI、仅用于构建和测试的可执行文件、进程内直接挂载插件以及私有浏览器 WebWorker 预览都不属于 Harness 应用启动器。verify-application-entrypoints将每个包 bin、可执行源码、根 demo 以及根脚本 start:web 与 dev:web 归入显式类别,并拒绝任何绕过 dsh 的 Node 应用路径。
Python SDK 遵循相同的应用架构。其运行时 wheel 把普通 dsh CLI 打包为 deepseek-harness-sdk-runtime-<platform>-<arch>,客户端默认以显式 Harness home 启动 dsh --profile sdk。极简示例选择随附的 sdk-minimal profile。Python 暴露 profile 选择与有序 patch 文件,而不是完整 Cordis 树;持久外部插件通过 dsh plugin 安装。已删除的私有直读配置载体没有兼容 bin 或回退 parser。
桌面应用
Electron 桌面应用在签名资源中携带精确匹配的 dsh 生产运行时,并拥有保留的 $DSH_HOME/profiles/desktop。共享 helper 初始化 profile 文件、协调 bundle,并解析安装与 bundle 的依赖而不替换 pnpm 拥有的包。Desktop 与 npm CLI 共享产品数据,但包、启用选择与锁文件保持独立。Desktop 内置 CLI 管理其已初始化的插件。
Electron 使用 Electron Node 模式启动私有 Desktop Host。Host 调用共享 CLI profile runner 与完整 Web 应用。窗口立即加载打包 Web 资源,等待启动注入后在同一文档中激活客户端插件。Web 负责 RPC 与流;桌面载体将本地页面连接到已认证的 Host。Node IPC 承载启动注入、就绪、致命错误与关闭。Desktop 默认监听系统分配的端口,profile 配置可覆盖。壳拥有的 UI 通过内置 pnpm 执行插件事务,并遵循正常用户与 profile 配置。
核心包
| 包 | 职责 | ctx 键 |
|---|---|---|
core/session |
仅追加的 SessionEvent 日志和内存存储 |
ctx.sessions |
core/system-prompt |
提示词片段与工具 schema 的组装 | ctx.systemPrompt |
core/tools |
作用域化的工具注册表和带把关的执行流水线 | ctx.tools |
core/agent |
Agent 接口、活跃 agent 注册表和 agent/* 事件 |
ctx.agents |
core/agent-loop |
实现该接口的默认驱动器 | ctx.agentLoop |
core/scope |
按 agent 划分作用域的注册原语 | 库,无 ctx 键 |
llm/llm |
消息与流式词汇表,以及适配器 seam | ctx.llm |
事件
事件在三个域内提供扩展点。
- 会话事件是追加到日志并通过
session/event广播的持久事实。当某个事实必须在重新加载后仍然存在时,使用它。 - Agent 事件(
agent/*)携带活跃Agent:inbox、步骤、状态、请求、验证、续跑。要观察或拦截进行中的工作时,使用它。 - 能力事件无需导入循环即可向某个 seam(
fs/*、tools/*、telemetry/*)附加策略和适配器。
Subagent Activation 拥有运行时树成员关系。
AgentLoop 回滚失败的初始化,并在启动已排队工作前等待串行 agent/created;关闭时即使队列清理失败,也会取消并等待当前工作,再释放资源(顺序)。
轮次流程
一个步骤是一次模型请求加上它调用的工具。一个轮次包含零个或多个步骤:它在领取首条输入之前打开,并在不再欠下任何工作时关闭。
turn/start
claim next-step input plus one queued message
snapshot Session model selection; assemble prompt sections + step-admitted tool schemas
-> agent/pre-step; project runtime context at fallback
reject | enter(messages, startsRequestSeries?)
reject, or a first enter rewritten empty -> close the turn with no step
step/start
agent/request -> prepareCall (cancellation commits neither system nor users)
commit system/message updates; rebuild the system head after compaction
append entered messages as user/message; reconcile retained runtime context
log request/header and request/context as needed
derive and freeze model history from the log
stream the bound prepared call -> llm/stream -> agent/assistant-stream start
agent/assistant-stream chunk*
assistant/message | assistant/attempt -> agent/assistant-stream end
tool/call* -> tools/pre-execute -> tools/execute -> tools/post-execute -> tool/result*
step/end
tools owe another request, or next-step input arrived -> claim -> next step
-> agent/turn-stopping
turn/end
turn/*、step/*、system/message、user/message、assistant/message、assistant/attempt 和 tool/* 是持久会话事件;其余是分属三个事件域的实时扩展点。agent/assistant-stream 发布进程本地 start、瞬态 chunk 与 end frame。loop 会在 committed end frame 前把完整紧凑 stream 提交为一个 message 或仅日志 attempt;Web Session-follow adapter 是该 live event 唯一的远程消费方。agent/pre-step、agent/request、llm/stream 和三个 tools/* 事件是 waterfall(瀑布式事件),其监听器必须调用 next() 才能委托下去;agent/turn-stopping 是 serial 事件,没有 next()。
fork 创建存储调用方提供的历史;共享前缀的后端先持久化父前缀(生命周期)。工厂 setup 在 Session 进入 SessionStore 并完成 session/created 之后、agent/created 之前运行;收尾事件在移除之前提交。
输入通过同一个 inbox 到达驱动器;注入的上下文等待一条唤醒消息。AgentLoop 的持久 inbox 投影使待处理输入在没有活跃 Agent 时仍可读取。
循环在组装前将 Session 模型选择捕获到 AssembleContext.modelSelection 与 agent/pre-step.modelSelection;重试保留该选择,省略推理强度时使用适配器默认值。系统组装同时捕获活跃来源值、生命周期与已提交的 Session 准入,由此确定是追加更新还是重建完整提示词。agent/pre-step 监听器接纳、改写或拒绝已领取消息;首次领取被拒绝或为空时,关闭不含步骤的持久轮次。enter 决策可设置 startsRequestSeries:循环记录新的 request/header(原因为 series,或在封装同时变化时为携带 startsSeries: true 的 change)。包装监听器通过 { ...decision, messages } 保留该声明。
接纳并记录 step/start 后,已弃用的 agent/request hook 在不改变提供方、模型或推理强度选择的前提下调整控制参数;prepareCall() 绑定适配器。在任一异步阶段取消,都不会提交系统候选与已接纳用户消息。每次尝试刷新运行时事实并派生历史;首次尝试提交这两者。
重试保留其组装结果;此后的来源变化留到下一次组装处理。surface 替换和图片省略决定开启新请求序列;未变化的恢复延续序列。每次尝试追加该组装的 systemUpdates() 写入;在已记录压缩摘要之后的首次尝试(包括重试)中,这些写入通过普通 system/message 事件清空保留的系统增量,并以捕获的指令替换初始系统节点。Agent-loop 规定请求与重试时序。
循环发送不可变请求,同时保持取消有效,仅对完全冻结的对象复用冻结证据;agent-loop 负责请求构建与取消原因。
失败步骤会记录缺失的工具结果。
ctx.tools.execute() 接收原始调用批次和并发上限。
会话日志
会话日志是模型所见上下文的来源。deriveMessages() 从中投影出模型历史。每个 assistant/message 都嵌入产生其组装内容的精确紧凑带时间 stream;assistant/attempt 保留已到达 settlement 的失败、重试、取消与 stream error attempt,且不添加模型历史。fork、恢复、transcript(文本记录)、遥测与持久化都从这些持久 settlement 派生,实时 UI 增量则来自 agent/assistant-stream;如果进程在 settlement 前硬中断,则不会留下持久 attempt stream(见决策)。
Session 消费方只了解当前逻辑格式。仅 header 的 stat 与 list 会重新扫描每个 Session 目录,选择数值最高的规范 generation,并在不加载事件或发布后继的情况下转换受支持的历史 header。已存储 Session 的 open 选择同一 generation,拒绝未来版本,或只 Decode 并组合一次构建时静态确定的相邻迁移链,再返回经过校验的当前逻辑事件。只读 open 直接使用这份内存结果,不发布后继;写 open 则先编码、校验并在未改变源的旁边排他发布最终版本命名的后继。未被后续事件封住的普通中断尾部仍由句柄消费方修复;只有在后续 turn/start 已经封住一种有限的已发布 restart 时,migration 才会插入缺失的 interrupted turn/end。JSONL v0 使用 session.jsonl[.zstd],v1 及后续版本使用小写 session.vN.jsonl[.zstd];已提交 generation 路径绝不重命名、替换或删除。JSONL provider 负责物理 framing、压缩、generation 选择与排他发布,每个相邻迁移包只负责一个 vN -> vN+1 步骤(决策)。
模型可见即已记录。 每个模型请求都必须能从日志重建。新增模型可见输入需要会话事件。修改现有消息内容的插件注册纯消息投影,独立读取器显式传入相同的处理器。 工具变更不依赖能力;Session 工具历史提供提供方声明。
投影 seam。 dsh-session-projection 提供 ctx.sessionProjections:已注册单元折叠已提交事件,载体通过 snapshot() 读取选定的客户端视图。领域服务等待注册完成,在内部保留句柄,并提供业务方法(注册生命周期)。AgentRegistry 通过 turnBoundaryOf(session) 提供轮次和步骤状态。AgentLoop 持有 modelSelection 和 inbox 状态(Loop 生命周期)。
工作目录 提供用户上下文和执行路径,不改变原始项目标识、沙箱写入根目录或已有进程目录。
能力 seam
一个 seam 是一项可替换能力,包含三种角色:声明接口的 Service Definition、实现它的 Service Provider,以及使用它的 Consumer(通常是面向模型的工具)。一个包可以合并承担多个角色,但单一角色本身不是 seam;添加一项能力意味着把三者一并设计(能力图)。
seam 正是替换一个提供方就能改变整个产品的原因。文件系统与进程提供方共享同一个执行世界,因此把它们指向远程沙箱,也就把 Bash、PTY 和 LSP 一并搬了过去,无需提供方专用 fork。subagent 提供方在同一个接口之后同样千差万别,从新建一个子 agent,到把一个轮次委派给另一个产品。
Agent Teams在根 Session 开启后提供任务策略和共享任务板。普通 subagent 控制工具负责身份、通信、发现、中断和收件箱等待。
新行为的归属位置
新行为附加到已有文档记录的扩展点。改动循环本身时,本映射随之更新。
| 目标 | 机制 |
|---|---|
| 添加模型提供方 | 在 ctx.llm 上注册其适配器 |
| 添加面向模型的能力 | 在 ctx.tools 上注册;其 schema 加入提示词组装 |
| 让某个会话拥有不同的能力集合 | 组装一个 agent preset;其中的服务行需要 isolate realm |
| 添加 shell 执行 | 注册 ctx.shell 后端;本地后端通过 ctx.subprocess spawn 进程 |
| 添加持久化终端执行 | 注册 ctx.terminals 后端和 dsh-tool-terminal |
| 添加用户命令 | 在 ctx.commands 上注册;它无需模型轮次即可分派 |
| 管理后台任务 | 在 ctx.jobs 上注册;job_* 工具读取或停止任务 |
| 从外部 webhook 启动 Session | 在 ctx.webhookRuntime 上注册可信规则,并挂载提供方适配器 |
| 添加文件系统访问或策略 | 注册 ctx.fs 提供方,或监听 fs/* 事件 |
| 限制所启动的进程 | 使用 ctx.sandbox 后端;消费方在启动进程前包装 argv |
| 拦截请求、工具或轮次 | 使用相应的 agent/* 或 tools/* 事件;agent/turn-stopping 会停止轮次 |
| 添加模型可见上下文 | 调用 agent.inject();它会落到下一次获准的请求中 |
| 添加 UI 或编辑器集成 | 驱动 ctx.agents 并从 session/event 渲染 |
| 添加 Web Client Chat 节点 | 注册 ConversationNodeDefinition + keyed renderer |
| 添加持久会话状态 | 扩展 SessionEventMap;从日志渲染和回放 |
| 生成会话标题 | 注册唯一的 ctx.sessionTitle 提供方 |
| 管理同会话目标 | 使用 ctx.goals;通过 agent/* 续跑 |
| 在轮次边界 fork 会话 | ctx.agents.create({ sessionId, source, inheritedEventCount, meta: { isSeeded: true } }) 在发布之前存储提供的父前缀与子会话事件 |
| 在新后端存储会话 | 基于共享的句柄脚手架实现 SessionPersistence(create/open/stat/list/export) |
| 将注册项限定到单个 agent | 使用该 agent 的 agent.ctx |
扩展实操手册将功能映射到能力,并索引包、工具、LLM(大语言模型)适配器和设置页的分步指南。Conversation 子系统负责 Chat node 组装。