dsh-coding 0.0.0-stage → 0.1.1-rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +91 -2
  3. package/cordis.patch.yml +233 -0
  4. package/docs/DETAIL.md +183 -0
  5. package/docs/DEVELOPMENT.md +189 -0
  6. package/icon.svg +6 -0
  7. package/lib/apply-patch.js +268 -0
  8. package/lib/client/capsule.js +120 -0
  9. package/lib/client/card.js +220 -0
  10. package/lib/client/command-row.js +76 -0
  11. package/lib/client/config-page.js +392 -0
  12. package/lib/client/definition.js +164 -0
  13. package/lib/client/index.js +129 -0
  14. package/lib/client/locales.js +156 -0
  15. package/lib/client/mode-picker.js +77 -0
  16. package/lib/client/styles.js +283 -0
  17. package/lib/client.js +1691 -0
  18. package/lib/coding-projection.js +349 -0
  19. package/lib/config.js +204 -0
  20. package/lib/contract.js +124 -0
  21. package/lib/editor.js +23 -0
  22. package/lib/failures.js +142 -0
  23. package/lib/handoff.js +312 -0
  24. package/lib/index.js +115 -0
  25. package/lib/language.js +136 -0
  26. package/lib/messages.js +43 -0
  27. package/lib/models.js +154 -0
  28. package/lib/patch.js +235 -0
  29. package/lib/preset.js +1488 -0
  30. package/lib/reasoning-levels.js +28 -0
  31. package/lib/reasoning-projection.js +178 -0
  32. package/lib/reasoning.js +585 -0
  33. package/lib/stages.js +306 -0
  34. package/lib/state.js +550 -0
  35. package/lib/system-language.js +56 -0
  36. package/lib/types/apply-patch.d.ts +13 -0
  37. package/lib/types/client/capsule.d.ts +9 -0
  38. package/lib/types/client/card.d.ts +72 -0
  39. package/lib/types/client/command-row.d.ts +42 -0
  40. package/lib/types/client/config-page.d.ts +136 -0
  41. package/lib/types/client/definition.d.ts +51 -0
  42. package/lib/types/client/index.d.ts +10 -0
  43. package/lib/types/client/locales.d.ts +18 -0
  44. package/lib/types/client/mode-picker.d.ts +25 -0
  45. package/lib/types/client/styles.d.ts +28 -0
  46. package/lib/types/coding-projection.d.ts +116 -0
  47. package/lib/types/config.d.ts +193 -0
  48. package/lib/types/contract.d.ts +33 -0
  49. package/lib/types/editor.d.ts +9 -0
  50. package/lib/types/failures.d.ts +62 -0
  51. package/lib/types/handoff.d.ts +112 -0
  52. package/lib/types/index.d.ts +144 -0
  53. package/lib/types/language.d.ts +56 -0
  54. package/lib/types/messages.d.ts +27 -0
  55. package/lib/types/models.d.ts +82 -0
  56. package/lib/types/patch.d.ts +37 -0
  57. package/lib/types/preset.d.ts +22 -0
  58. package/lib/types/reasoning-levels.d.ts +20 -0
  59. package/lib/types/reasoning-projection.d.ts +64 -0
  60. package/lib/types/reasoning.d.ts +141 -0
  61. package/lib/types/stages.d.ts +164 -0
  62. package/lib/types/state.d.ts +261 -0
  63. package/lib/types/system-language.d.ts +10 -0
  64. package/lib/types/verification-tool.d.ts +32 -0
  65. package/lib/types/verification.d.ts +77 -0
  66. package/lib/types/verifier.d.ts +9 -0
  67. package/lib/types/volatile.d.ts +17 -0
  68. package/lib/verification-tool.js +176 -0
  69. package/lib/verification.js +337 -0
  70. package/lib/verifier.js +25 -0
  71. package/lib/volatile.js +20 -0
  72. package/locale/en.json +7 -0
  73. package/locale/zh.json +7 -0
  74. package/package.json +129 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ygzhang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,92 @@
1
- # Temporary Holding Version
1
+ # DSH Coding 编程智能体
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ DSH 编程智能体运行时插件,借鉴 Claude 任务编排机制,Codex 五段式契约层层推进,DeepSeek Pro 分析需求、编排任务,Flash 子智能体去干活,确保任务交付质量和 Token 价值最大化。
4
+
5
+ ## 安装
6
+
7
+ 插件市场 / **插件** 页面:**添加插件** → 输入 `dsh-coding`(或本地目录绝对路径)。
8
+
9
+ ```bash
10
+ dsh plugin --profile <profile> add dsh-coding
11
+ ```
12
+
13
+ 安装插入五行:Host 设置服务、`Coding` 预设,以及三个内部 worker 预设(planner / implement / verify)。已有会话不受影响;只有选择 `Coding` 预设的新会话启用运行时。
14
+
15
+ ## 配置
16
+
17
+ 装好后在 **插件 → DSH Coding 编程智能体** 页面可视化编辑(保存后立即生效)。`verificationCommands`、`verificationTimeoutMs` 与两个验证预算键是普通字段,需改插件包配置(`cordis.patch.yml`):
18
+
19
+ ```yaml
20
+ - id: dsh-coding
21
+ name: "dsh-coding"
22
+ config:
23
+ defaultMode: auto # auto | consult | review | codex
24
+ language: auto # auto | zh | en
25
+ reasoning: auto # auto | low | high | max
26
+ plannerWorkerModel: provider/model # 必填
27
+ implementWorkerModel: provider/model # 必填
28
+ verifyWorkerModel: provider/model # 必填
29
+ verificationTimeoutMs: 360000 # 单次验证命令超时(ms);0 表示不限制
30
+ verificationCommands: {} # 按 kind / kind:scope 覆盖验证命令
31
+ maxVerifyAttempts: 5 # 验证失败重试预算;0 表示不限制
32
+ maxSameFailure: 2 # 相同失败签名连续出现上限;0 表示不限制
33
+ ```
34
+
35
+ 默认值:planner 用 `deepseek-account/deepseek-v4-pro`(pro 模型负责勘探现状与拆分任务),implement / verify 用 `deepseek-account/deepseek-flash`(flash 模型执行编辑与核查);三个字段都可以在插件页里改。
36
+
37
+ 插件页里三个模型字段是**下拉选择**,选项来自当前 DSH 已配置的模型目录(`ctx.remote.llm`:已注册 provider + 可配置 provider + 各自声明的模型);选错(空值,或不在已加载列表里)时字段与提示都用警告色,并禁用保存。**恢复默认**只把 `defaultMode`、三个 worker 模型、`language`、`reasoning` 回填为默认值,仍需再点保存才会写入。
38
+
39
+ 三个 worker 模型**不回退**主代理模型:缺失、格式不对或 provider 不存在时插件挂载失败;派发前还会校验模型是否可用(`ctx.llm.resolveModelInfo`),失败时错误给出配置字段名、当前路由与该 provider 的可用模型,修好配置后重试原调用即可。
40
+
41
+ ## 使用
42
+
43
+ 1. 新建会话,预设选择 **Coding**。三个 worker 预设只用于 `delegate_*` 派发,不出现在预设列表。
44
+ 2. 空白首页输入框旁可选 **工作模式**(自动 / 问答 / 审查 / Codex)。默认 **自动** 先做需求分析,再用 `coding_mode` 选定后续工作流;不确定时回退到问答。
45
+ 3. 正常提需求:需求分析 → 规划(pro worker 勘探现状并输出单一职责子任务)→ 任务单 → 实现(flash worker)→ 验证。每个子任务写清目标、涉及文件、验收方式与不碰的范围。
46
+
47
+ | 阶段 | 主代理可以做什么 |
48
+ | --- | --- |
49
+ | 需求分析 requirements | 读文件、搜索、检查命令;不能改文件、不能验证、不能委派 |
50
+ | 规划 plan | 读 + `delegate_plan` + `todo_write`;先用 `coding_handoff` 记录 goal,再写任务单;没有任务单不能进入实现 |
51
+ | 实现 implement | 只剩 `delegate_implement`、`coding_handoff`、`todo_write` 与控制类工具;编辑工具被移除 |
52
+ | 验证 verify | 读 + `run_verification` / `delegate_verify` + `todo_write`;先记录验收标准,失败按预算重试;失败不能交付 |
53
+ | 交付 deliver | 只读汇报;可把已完成条目勾成 completed;预算耗尽时为 BLOCKED 交付 |
54
+
55
+ | 控制类工具 | 作用 |
56
+ | --- | --- |
57
+ | `coding_stage` | 查看或推进阶段,放行与否由插件判定 |
58
+ | `coding_mode` / `coding_reasoning` | 选择工作流 / 调整推理等级 |
59
+ | `coding_handoff` | 读写跨阶段交接记录(goal / constraints / acceptance / findings / decisions / open / tasks / changes);`{ reset: true }` 只重置验证重试预算,不清空记录 |
60
+
61
+ | 命令 | 作用 |
62
+ | --- | --- |
63
+ | `/coding` | 查看当前阶段、模式、任务单与验证状态(状态行含 `handoff goal=…, acceptance=N`、`attempt N`、`BLOCKED`、`delivery BLOCKED`) |
64
+ | `/coding auto / consult / review / codex` | 切换模式并新开一轮(`agent.followup()`) |
65
+ | `/coding set <模式>` | 静默设置模式(卡片按钮与首页选择器使用) |
66
+ | `/coding stage <阶段>` | 在当前轮内推进阶段(`agent.steer()`) |
67
+ | `/coding reset` | 重置验证重试预算(交接记录与最近一次验证结果保留) |
68
+
69
+ 每轮结束处有**会话控制卡片**(模式、阶段路径、推理等级、验证状态、任务单进度、改动文件数;最新一轮可切换模式),每次 Coding 工具调用处有**阶段胶囊**。两者都从持久会话投影回放:刷新、重开、历史翻页都显示当时的真实状态,旧轮冻结在它实际运行的模式与阶段。
70
+
71
+ ### 交接与验证预算
72
+
73
+ - 每个阶段的结论写进 `coding_handoff`,而不是留在对话里:planner 返回的任务、implement 报告的文件、verify 的发现会自动并入;记录随 checkpoint 持久化,重启与翻页可恢复,进入 worker 提示词时按角色裁剪并有字符/条数上限。
74
+ - 结构化任务必须带验收标准。codex 模式下 plan 前要有 goal,verify 前要有 acceptance,deliver 前两者都要有。
75
+ - 验证命令有总时限(`verificationTimeoutMs`,默认 6 分钟 = 360000 ms,可配置,`0` 不限制):到点终止并记为一次失败,避免测试脚本死循环一直等待。
76
+ - 验证失败按预算重试(默认 5 次失败、同一失败签名 2 次);耗尽后 `coding_stage(deliver)` 放行但标记为 BLOCKED 交付,`coding_handoff({ reset: true })` 或 `/coding reset` 重置预算,只有验证通过才自动清零——这是防止死循环的出口。
77
+
78
+ ### 失败提示
79
+
80
+ - 关键调度点(子代理派发、验证启动/超时、工具表裁剪、会话恢复/投影、推理等级、交接写入、控制命令)遇到阻断性异常时,下一步会注入一条**运行中断**通知,写明原因与处理方式;状态行与会话 checkpoint 同时记录 `failure <point>`。
81
+ - 通知只出现一次,不新开 Turn;同一环节恢复成功后自动清除,重启只恢复 `failure` 状态、不重复提示。
82
+ - 普通验证失败仍走预算 / BLOCKED 路径,不产生该通知。
83
+ - 启动即失败(worker 路由或依赖缺失)没有下一步可注入,插件挂载错误里会附一段 `What to do`。
84
+
85
+ ## 文档
86
+
87
+ - [设计思路与分层结构](docs/DETAIL.md)
88
+ - [开发者文档](docs/DEVELOPMENT.md)
89
+
90
+ ## 许可
91
+
92
+ MIT
@@ -0,0 +1,233 @@
1
+ # dsh-coding bundle.
2
+ #
3
+ # One passive Host settings row plus three preset declarations:
4
+ # preset-dsh-coding the `Coding` preset a session selects
5
+ # preset-dsh-coding-implement the isolated implement worker (internal)
6
+ # preset-dsh-coding-verify the isolated verify worker (internal)
7
+ #
8
+ # The worker presets are internal, but DSH has no hidden-preset flag, so they
9
+ # are declared openly, ordered last, and named as workers. Their plugin lists ARE
10
+ # the workers' tool whitelists: a subagent gets a fresh flat registration scope,
11
+ # so the parent's tool restrictions are never inherited — the composition is the
12
+ # only honest place to state what a worker may hold.
13
+ - insert:
14
+ # Verified configuration. `implementWorkerModel` / `verifyWorkerModel` are
15
+ # required `provider/model` routes: a worker never falls back to the lead's
16
+ # model, and an unset route fails the mount instead of spending tokens.
17
+ - id: dsh-coding
18
+ name: "dsh-coding"
19
+ config:
20
+ defaultMode: auto
21
+ implementWorkerModel: deepseek-account/deepseek-flash
22
+ plannerWorkerModel: deepseek-account/deepseek-v4-pro
23
+ verifyWorkerModel: deepseek-account/deepseek-flash
24
+ # auto follows the DSH interface locale, then the host locale; zh/en pin it.
25
+ language: auto
26
+ # auto classifies each turn; low/high/max pin every turn.
27
+ reasoning: auto
28
+ # 验证失败重试预算:0 表示不限制。耗尽后允许 BLOCKED 交付,避免死循环。
29
+ maxVerifyAttempts: 5
30
+ # 连续相同失败签名上限:0 表示不限制。
31
+ maxSameFailure: 2
32
+ # 单次验证命令的最长运行时间(毫秒);超时终止并记为失败。0 表示不限制。
33
+ verificationTimeoutMs: 360000
34
+ verificationCommands: {}
35
+
36
+ - id: preset-dsh-coding
37
+ name: "@deepseek-ai/dsh-agent-preset"
38
+ config:
39
+ id: dsh-coding
40
+ name: Coding
41
+ description: >-
42
+ DSH 编程智能体运行时:requirements→plan→implement→verify→deliver 阶段由插件强制执行,
43
+ implement 阶段只保留委派工具,编辑由隔离子代理完成,验证结果决定能否交付。
44
+ A DSH coding-agent runtime: enforced requirements→plan→implement→verify→deliver stages,
45
+ delegated edits from isolated worker subagents, and a verification gate before delivery.
46
+ order: 4
47
+ plugins:
48
+ # Identity and temperament only. The workflow contract is injected per
49
+ # stage by dsh-coding/preset, one fragment at a time — never as a global
50
+ # system prompt that the model can re-read but the runtime cannot enforce.
51
+ - id: persona
52
+ name: "@deepseek-ai/dsh-persona"
53
+ config:
54
+ prefix: |-
55
+ You are the Coding agent of the DeepSeek Harness (DSH): a pragmatic senior software engineer assigned to this repository. {{model}} is the model behind you; DSH supplies the workspace, the tools, and this session.
56
+
57
+ What you optimize for:
58
+ - Ground truth over recall. Inspect the real file, run the real command, read the real error before deciding. Never claim a check you did not run.
59
+ - The smallest correct change. Reuse existing patterns, functions, and types; add no abstraction, dependency, or drive-by refactor the task does not need.
60
+ - A user who is better informed after every turn. Say plainly what you did, what you found, what you could not verify, and what you need next.
61
+
62
+ How you show up: direct, concrete, calm. No flattery, no filler, no self-narration. Short sentences with real paths, commands, and file:line references instead of pasted walls of text. Admit a mistake or a dead end quickly.
63
+
64
+ The runtime below enforces the coding rhythm: it decides which stage you are in, which tools you may use, and whether verification passed. Treat a refusal as the rule, not as a suggestion to work around.
65
+ includeRuntimeContext: true
66
+
67
+ - id: agent-instructions
68
+ name: "@deepseek-ai/dsh-agent-instructions"
69
+ config:
70
+ maxBytes: 65536
71
+
72
+ - id: tool-bash
73
+ name: "@deepseek-ai/dsh-tool-bash"
74
+ disabled: !!js process.platform === 'win32'
75
+ - id: tool-pwsh
76
+ name: "@deepseek-ai/dsh-tool-pwsh"
77
+ disabled: !!js process.platform !== 'win32'
78
+ - id: tool-fs
79
+ name: "@deepseek-ai/dsh-tool-fs"
80
+ config:
81
+ # `apply_patch` here would be the built-in editor; the Coding runtime
82
+ # registers the Codex-format editor itself so the lead can lose it.
83
+ applyPatch: false
84
+ - id: tool-fs-search
85
+ name: "@deepseek-ai/dsh-tool-fs-search"
86
+ config:
87
+ sampleOverCapGlobResults: false
88
+ - id: tool-jobs
89
+ name: "@deepseek-ai/dsh-tool-jobs"
90
+ - id: tool-todo
91
+ name: "@deepseek-ai/dsh-tool-todo"
92
+ config:
93
+ allowParallelInProgress: true
94
+ - id: tool-web
95
+ name: "@deepseek-ai/dsh-tool-web"
96
+ config:
97
+ fetch: true
98
+ searchTimeoutMs: 60000
99
+
100
+ - id: skill-filesystem
101
+ name: "@deepseek-ai/dsh-skill-filesystem"
102
+ - id: tool-skill
103
+ name: "@deepseek-ai/dsh-tool-skill"
104
+
105
+ # The Codex patch editor. The stage gate refuses it outside a stage
106
+ # that admits writes, and `implement` also removes it from the table.
107
+ - id: dsh-coding-editor
108
+ name: "dsh-coding/editor"
109
+
110
+ # The enforced runtime: stage machine, dual gate, delegation tools,
111
+ # structured verification, per-stage contract injection.
112
+ - id: dsh-coding-preset
113
+ name: "dsh-coding/preset"
114
+
115
+ - id: compaction
116
+ name: cordis:group
117
+ group: true
118
+ isolate:
119
+ compaction: true
120
+ toolResultPruner: true
121
+ config:
122
+ - id: compaction-basic
123
+ name: "@deepseek-ai/dsh-compaction-basic"
124
+ config:
125
+ auto: false
126
+ - id: command-compact
127
+ name: "@deepseek-ai/dsh-command-compact"
128
+ - id: tool-result-pruner
129
+ name: "@deepseek-ai/dsh-compaction-tool-result-pruner"
130
+ config:
131
+ thresholdChars: 8192
132
+ headChars: 4096
133
+ tailChars: 1024
134
+
135
+ # Planner worker: read + search + web only. It explores the project and
136
+ # produces a single-purpose task list; it cannot edit, run a shell, or
137
+ # delegate further.
138
+ - id: preset-dsh-coding-planner
139
+ name: "@deepseek-ai/dsh-agent-preset"
140
+ config:
141
+ id: dsh-coding-planner
142
+ name: Coding planner worker
143
+ description: >-
144
+ Internal worker of the Coding preset: explores the workspace and returns a
145
+ single-purpose task list from the requirement. Dispatched by delegate_plan.
146
+ order: 89
147
+ plugins:
148
+ - id: persona
149
+ name: "@deepseek-ai/dsh-persona"
150
+ config:
151
+ prefix: |-
152
+ You are the planner worker of the DSH Coding runtime. You have a fresh context: the delegating agent's requirement is complete on its own. Explore the workspace with read, glob, grep, and web; you may not edit files, run a shell, or delegate further. Produce a plan of single-purpose sub-tasks, each doing exactly one thing.
153
+ includeRuntimeContext: false
154
+ - id: tool-fs
155
+ name: "@deepseek-ai/dsh-tool-fs"
156
+ config:
157
+ applyPatch: false
158
+ write: false
159
+ edit: false
160
+ - id: tool-fs-search
161
+ name: "@deepseek-ai/dsh-tool-fs-search"
162
+ config:
163
+ sampleOverCapGlobResults: false
164
+ - id: tool-web
165
+ name: "@deepseek-ai/dsh-tool-web"
166
+ config:
167
+ fetch: true
168
+ searchTimeoutMs: 60000
169
+
170
+ # Implement worker: read, search, write, edit, and the Codex apply_patch
171
+ # editor. No shell, no web, no delegation — it cannot widen its own scope.
172
+ - id: preset-dsh-coding-implement
173
+ name: "@deepseek-ai/dsh-agent-preset"
174
+ config:
175
+ id: dsh-coding-implement
176
+ name: Coding implement worker
177
+ description: >-
178
+ Internal worker of the Coding preset: edits files from a self-contained brief and
179
+ reports the files it changed. Dispatched by delegate_implement.
180
+ order: 90
181
+ plugins:
182
+ - id: persona
183
+ name: "@deepseek-ai/dsh-persona"
184
+ config:
185
+ prefix: |-
186
+ You are the implement worker of the DSH Coding runtime. You have a fresh context: the delegating agent's brief is complete on its own. You may read, search, and edit files; you may not run shell commands, use the web, or delegate further.
187
+
188
+ Work in small, reviewable edits: read before you write, change only what the brief needs, and never commit, push, or tag. When you are done, report through structured_output — the files you changed are the evidence the lead verifies.
189
+ includeRuntimeContext: false
190
+ - id: tool-fs
191
+ name: "@deepseek-ai/dsh-tool-fs"
192
+ config:
193
+ applyPatch: false
194
+ write: true
195
+ edit: true
196
+ - id: tool-fs-search
197
+ name: "@deepseek-ai/dsh-tool-fs-search"
198
+ config:
199
+ sampleOverCapGlobResults: false
200
+ - id: dsh-coding-editor
201
+ name: "dsh-coding/editor"
202
+
203
+ # Verify worker: read-only. `run_verification` is its only way to execute
204
+ # anything, and that tool cannot write a file.
205
+ - id: preset-dsh-coding-verify
206
+ name: "@deepseek-ai/dsh-agent-preset"
207
+ config:
208
+ id: dsh-coding-verify
209
+ name: Coding verify worker
210
+ description: >-
211
+ Internal worker of the Coding preset: runs checks read-only and reports findings with
212
+ evidence. Dispatched by delegate_verify.
213
+ order: 91
214
+ plugins:
215
+ - id: persona
216
+ name: "@deepseek-ai/dsh-persona"
217
+ config:
218
+ prefix: |-
219
+ You are the verify worker of the DSH Coding runtime. You hold no edit tool: you read files, search them, and run checks through run_verification. Never claim a passing check you did not run; report findings with file:line and a reproducible scenario, ordered by impact.
220
+ includeRuntimeContext: false
221
+ - id: tool-fs
222
+ name: "@deepseek-ai/dsh-tool-fs"
223
+ config:
224
+ applyPatch: false
225
+ write: false
226
+ edit: false
227
+ readImage: false
228
+ - id: tool-fs-search
229
+ name: "@deepseek-ai/dsh-tool-fs-search"
230
+ config:
231
+ sampleOverCapGlobResults: false
232
+ - id: dsh-coding-verifier
233
+ name: "dsh-coding/verifier"
package/docs/DETAIL.md ADDED
@@ -0,0 +1,183 @@
1
+ # DSH Coding 编程智能体 — 设计思路与分层结构
2
+
3
+ DSH 编程智能体运行时插件,借鉴 Claude 任务编排机制,Codex 五段式契约层层推进,DeepSeek Pro 分析需求、编排任务,Flash 子智能体去干活,确保任务交付质量和 Token 价值最大化。
4
+
5
+ 本文说明插件为什么这样设计、由哪些层组成,以及每层的职责边界;供使用者理解行为、供维护者规划迭代。目录、构建与测试见 [DEVELOPMENT.md](DEVELOPMENT.md)。
6
+
7
+ ## 1. 设计出发点
8
+
9
+ 普通 Coding 预设靠系统提示词要求模型“先需求分析、再规划、再修改、再验证”,模型可以遵守也可以跳过。本插件把同一套节奏交给 DSH 的事件系统执行:
10
+
11
+ - **阶段由插件判定**:`requirements → plan → implement → verify → deliver`,模型只能提出推进请求;是否放行由插件依据事实(是否完成需求分析、是否有任务单、是否有编辑落盘、最近一次验证是否通过)决定。
12
+ - **工具由插件裁剪**:实现阶段主代理的工具表只剩委派与控制类工具,编辑工具既不可见,也会被兜底守卫拒绝。
13
+ - **编辑在隔离子代理中完成**:implement worker 有独立上下文与独立模型路由,只拿到显式放行的编辑工具。
14
+ - **阶段之间靠有界记录交接**:目标、验收标准、决策与任务写进 `coding_handoff`,worker 只读自己那一份摘要,而不是靠对话历史传递需求。
15
+ - **验证结果决定能否交付**:验证失败时推进到交付会被拒绝,并阻止下一步;重试预算耗尽后只放行一次 BLOCKED 交付。
16
+ - **状态在对话里可见**:每轮一张会话控制卡片、每次 Coding 工具调用一枚阶段胶囊,均从持久会话事件/投影回放。
17
+
18
+ ## 2. 分层结构
19
+
20
+ ```
21
+ ┌──────────────────────────────────────────────────────────────┐
22
+ │ 投影层 模式选择器 · 会话控制卡片 · 阶段胶囊(只读) │
23
+ ├──────────────────────────────────────────────────────────────┤
24
+ │ 编排层 状态机 · 双层门禁(pre-execute + guard) · pre-step │
25
+ ├──────────────────────────────────────────────────────────────┤
26
+ │ 执行层 隔离子代理(planner / implement / verify)· 模型路由 │
27
+ ├──────────────────────────────────────────────────────────────┤
28
+ │ 交接层 coding_handoff · 有界记录 · checkpoint 持久化 │
29
+ ├──────────────────────────────────────────────────────────────┤
30
+ │ 验证层 run_verification · 结构化报告 · 失败回流 │
31
+ ├──────────────────────────────────────────────────────────────┤
32
+ │ 推理层 自动分类 / 配置固定 / 会话覆盖 │
33
+ └──────────────────────────────────────────────────────────────┘
34
+ ```
35
+
36
+ ### 2.1 编排层:状态机 + 双层门禁
37
+
38
+ | 机制 | 位置 | 作用 |
39
+ | --- | --- | --- |
40
+ | `coding_stage` | `src/preset.ts` → `src/state.ts` | 请求查看/推进阶段,`checkAdvance` 判定 |
41
+ | `tools/pre-execute` waterfall | `src/preset.ts` | 返回 `{ kind: 'deny', reason }`,reason 含当前阶段、允许阶段与补救动作 |
42
+ | `ctx.tools.guard()` | `src/preset.ts` | 同一条规则注册为单调拒绝,pre-execute 放行后仍然生效 |
43
+ | `agent/pre-step` | `src/preset.ts` | 注入当前阶段契约与状态;跳过门禁时拒绝该 Step |
44
+
45
+ **为什么用 `deny` 而不是 `ask`**:`ask` 会把调用转交审批通道,语义是“询问用户是否允许”;阶段门禁是确定性规则,模型需要看到 `Error: <reason>` 并据此调整行为。
46
+
47
+ **两层分工**:waterfall 负责友好拒绝与可操作提示,guard 是最终安全边界。真实派发顺序是 pre-execute →(ask)→ `checkGuards` → 派发,所以即使别的监听器先返回 `allow`,guard 仍拒绝。
48
+
49
+ **阶段门禁条件**(`checkAdvance`):
50
+
51
+ | 目标阶段 | 条件 |
52
+ | --- | --- |
53
+ | `plan` | 至少一次成功的只读/检查观察(`requirementsObserved > 0`);codex 模式还要求 handoff 里已有 goal |
54
+ | `implement` | 存在 `todo_write` 写入的结构化任务单 |
55
+ | `verify` | codex 模式下有落盘编辑,且 handoff 里已有验收标准;review 模式不要求 |
56
+ | `deliver` | codex 模式要求最近一次验证通过(预算耗尽时放行一次 BLOCKED 交付),且 handoff 里有 goal 与验收标准;review 模式要求至少跑过一次 |
57
+ | 选工作流 | auto 模式先 `coding_mode` 选择 consult / review / codex |
58
+
59
+ 回退(如 verify → implement)不受前向条件限制;模式切换后若当前阶段不属于新模式,自动回到该模式的首阶段。
60
+
61
+ **跳阶段的代价**:拒绝后 `agent/pre-step` 返回 `{ kind: 'reject' }`,Turn 以 `blocked` 结束;被拒绝的 Step 不把消息放回 Inbox,拒绝原因会在下一步契约里重新注入,模型因此知道为什么被拦下。
62
+
63
+ **失败上报**(`src/failures.ts`):运行时在八个调度点捕获阻断性异常——`projection`、`restore`、`tool-table`、`reasoning`、`delegate`、`verification`、`handoff`、`command`。`makeFailure(point, error, language)` 生成 `CodingFailure { point, reason, action, at }`:`reason` 是异常消息(裁剪到 600 字符,空消息记为 `unknown error`),`action` 是该点针对性的下一步(按会话语言 zh/en 渲染,点名的配置字段与操作可直接照做)。`recordFailure(state, failure)` 同时写入 `failure`(当前未恢复的异常)与一次性 `failureNotice`;`agent/pre-step` 在应用推理等级之后用 `takeFailureNotice` 取走它,作为一条 `dsh-coding-control` 消息(`## dsh-coding 运行中断(<点>)` + 原因 + 处理)注入本步。该环节下一次成功时 `clearFailure(state, point)` 按点清除,因此一个子系统恢复不会掩盖另一个仍存在的异常;重启时 `restoreFromView` 只恢复 `failure`、不恢复通知,用户不会重新收到同一条提示。`statusLine` 显示 `failure <point>`,`checkpointOf` 把 `failure` 随工具结果持久化,旧 checkpoint 没有该字段照旧兼容。这条通道**不新增 Turn、不新增事件类型、只在异常时注入一条消息**:稳态下没有任何额外消息,历史不被改写,因此不影响缓存命中率。
64
+
65
+ ### 2.2 执行层:子代理隔离 + 模型路由
66
+
67
+ 实现阶段主代理的工具表被 `agent.ctx.tools.restrict({ allow })` 裁剪为:
68
+
69
+ ```
70
+ delegate_implement · coding_handoff · todo_write · coding_stage · coding_mode · coding_reasoning
71
+ ```
72
+
73
+ `delegate_*` 派发时显式传入:`provider: 'spawn'`、`agentPreset`(= 子代理工具白名单)、`agentOptions.provider/model`(配置里的 worker 路由)、`delivery: 'caller'`(结果只回给本次调用)、`outputSchema`(结构化报告)。子代理拿到全新的扁平注册作用域,父级 `restrict` 不会带入,因此白名单只能写在组合里。
74
+
75
+ 三个 worker 预设的插件列表(`cordis.patch.yml`)就是它们的权限清单:
76
+
77
+ | worker | 实际工具 |
78
+ | --- | --- |
79
+ | planner | `read` / `glob` / `grep` / `web_search` / `web_fetch`(只读) |
80
+ | implement | `read` / `write` / `edit` / `glob` / `grep` / `apply_patch` |
81
+ | verify | `read` / `glob` / `grep` / `run_verification`(无编辑、无 shell) |
82
+
83
+ **模型路由与缺失处理**(`src/models.ts`):
84
+
85
+ - 路由只有一个出口:`resolveWorkerRoute(config, role)` 按角色读固定字段(`plannerWorkerModel` / `implementWorkerModel` / `verifyWorkerModel`),要求正好是 `provider/model`,返回带 `role`、字段名与渲染文本的路由。`preflight` 在挂载时对三个角色各解析一次,因此缺失或格式错误直接挂载失败;注册表可读时 provider 是否存在也在挂载时检查。
86
+ - 模型是否存在要查模型目录,而目录可能需要凭据,所以挂载阶段不判模型:目录不可用时校验延后到派发,不阻塞挂载。
87
+ - 派发前 `assertWorkerModel(ctx, route)` 现查现用:provider 不在 `ctx.llm.listProviders()` 里抛 `WorkerModelError`(`PROVIDER_MISSING`);`ctx.llm.resolveModelInfo` 失败抛 `MODEL_UNAVAILABLE`,并附上 `ctx.llm.listModels` 给出的该 provider 可用模型(目录不可用时列表为空)。
88
+ - 错误文案给出配置字段名、当前路由、可用模型与修复位置,并明确「worker 永不回退主代理模型」。失败的那次派发会记进 worker 记录,修好配置后重试原调用即可;同一路由重试只会得到同样的失败。
89
+ - 默认路由:planner 用 `deepseek-account/deepseek-v4-pro`(pro 模型),implement / verify 用 `deepseek-account/deepseek-flash`;`deepseek-account` 当前内置目录是 `deepseek-flash` 与 `deepseek-v4-pro`,默认值必须在这个集合内。
90
+
91
+ **失败上报**:模型校验(`assertWorkerModel`)、`startActivation` 启动、worker 结果异常,以及 worker 跑完但没有完成,都在 `delegate` 点走同一条 `recordFailure` 通道;派发成功时 `clearFailure(state, 'delegate')` 清除。异常仍以工具错误返回给主代理,额外的一次性通知保证用户也看到原因与处理方式。
92
+
93
+ ### 2.3 验证层:语义化验证 + 结构化回流
94
+
95
+ `run_verification({ kind, scope })`:
96
+
97
+ 1. **解析命令**:先看配置 `verificationCommands["<kind>:<scope>"]` → `["<kind>"]`,否则从最近的 `package.json` 的 scripts 按 `test / build / lint / typecheck` 候选挑选,并按 lockfile 判断包管理器(pnpm / npm / yarn / bun)。
98
+ 2. **确定目录**:`targeted` 与 `package` 用最近的包目录,`full` 用仓库根目录。
99
+ 3. **执行**:经 `ctx.subprocess.spawn()` 运行,不硬编码本地执行路径;输出按上限收集。
100
+ 4. **结构化结果**:`passed`、`command`、`exitCode`、`durationMs`、`failedTests`、`errors`、`output`,写入状态机。
101
+ 5. **回流**:失败时 `tools/post-execute` 追加“验证失败 / 回到实现阶段修复”,`coding_stage(deliver)` 与 `agent/pre-step` 据此拦截。
102
+
103
+ `delegate_verify` 的结构化报告写入 `source: 'worker'` 的验证记录。报告必须给出实际运行过的命令,否则记为失败——交付门禁不接受没有命令证据的“看起来通过”。
104
+
105
+ **命令级超时**(`verificationTimeoutMs`,默认 `360000` ms,`0` 不限制):`executeVerification` 用 `Promise.race` 给 `ctx.subprocess` 句柄的 `done` 加一个截止时间;到点调用 `terminate()` 请求终止,并按 `exitCode: null`、`passed: false` 返回,`errors` 里写明“超过 `verificationTimeoutMs`(N ms)已被终止”。这个结果同样经 `recordVerification` 计入失败预算,因此死循环的测试脚本会持续消耗 `maxVerifyAttempts`,最终进入 BLOCKED 交付。超时是**命令级**的:它约束单次验证子进程的运行时间,不是 `run_verification` 工具调用级的预算。
106
+
107
+ **重试预算与阻塞交付**:每次验证结果都只经 `recordVerification(state, outcome, budget)` 记账。失败一次就累计一次 `verificationAttempts`,同时按**失败签名**区分“在修”和“在重复”:签名 = 排序后的 `failedTests` + 前 3 条 `errors` + `exitCode`,与上一次相同时 `sameFailureCount` 加一。预算来自配置 `maxVerifyAttempts`(默认 5)与 `maxSameFailure`(默认 2),`0` 表示不限制;任一耗尽即写入 `verifyBlocked`。此后 codex 模式下 `coding_stage(deliver)` 返回 `blocked: true`:推进被放行,但状态记为 `deliveryBlocked`,状态行显示 `BLOCKED after N attempts` 与 `delivery BLOCKED`。`coding_handoff({ reset: true })` 或 `/coding reset` 只清预算(交接记录与最近一次验证结果保留),只有验证通过才会自动清零。
108
+
109
+ **失败上报**:验证命令无法启动(命令为空或解析不出可执行命令)或超过 `verificationTimeoutMs` 时,除计入预算外还会在 `verification` 点记一次失败并注入运行中断通知;普通断言失败仍只走预算与 BLOCKED 路径,不产生该通知。验证通过时 `clearFailure(state, 'verification')` 清除。
110
+
111
+ ### 2.4 投影层:模式选择 + 逐轮卡片 + 阶段胶囊
112
+
113
+ - **首条消息模式选择**:`conversation.hero.options` 注册 `dsh-coding/mode-picker`,仅在空白首页且为 Coding 会话时出现;点选走静默 `/coding set <模式>`,不会先起一轮空对话。
114
+ - **会话控制卡片**:`conversation.chat.turnTail` 注册 `dsh-coding/turn-card`。每轮一张,显示模式、该模式开放的阶段路径、推理等级与来源、验证状态、任务单进度、改动文件数;最新一张可切换模式,旧卡片冻结在那一轮实际运行的模式与阶段。
115
+ - **阶段胶囊**:`conversation.chat.node` 以 `coding-stage` 键注册 `ConversationNodeDefinition` 与渲染器,每次 Coding 工具调用处显示该次调用后的阶段与验证。
116
+ - **数据源**:持久会话投影 `dshCodingStage`(`ctx.sessionProjections`),折叠 `agent-preset/selected`、`tool/call`、`tool/result.meta`(整值 checkpoint)、`command/run`、`turn/end`;`dshCodingReasoning` 另折叠推理等级并在 `turn/end` 冻结每轮快照。
117
+ - **只读**:卡片与胶囊没有自定义事件处理,控制动作一律回流为 `/coding` 命令,前端不直接改 Host 状态。
118
+ - **视觉**:样式集中在 `src/client/styles.ts`,只用 `--dsw-*` 主题 token,不自定义字体、字号与配色。
119
+ - **配置页**(`plugins.bundle.config`):`defaultMode` / `language` / `reasoning` 仍为下拉;三个 worker 模型字段也是下拉,选项由 `ctx.remote.llm` 的 `listProviders` + `listConfigurableProviders` + `discoverModels` 现取现拼(`provider/model` 为值,`显示名 · 模型名` 为标签,按 provider 再按标签排序),当前值不在目录里时保留一项并标注“不在当前模型列表”,字段与提示转警告色并禁用保存;目录加载失败同样只显示警告 + 重试按钮并禁用保存,不会静默写入未校验路由。**恢复默认**只回填 `defaultMode`、三个模型、`language`、`reasoning`(`DEFAULT_DRAFT`),不自动保存。页面顶部固定显示“请保持 Coding 预设全部开启状态,否则本插件无法工作。”;模型提示在无效时为警告色、有效时为普通提示色。
120
+
121
+ ### 2.5 推理层:自动分类 + 配置固定 + 会话覆盖
122
+
123
+ `src/reasoning.ts` 在每次模型调用前决定本轮的推理等级,优先级从高到低:
124
+
125
+ 1. 配置 `reasoning: low|high|max`:固定所有轮次,`coding_reasoning` 只报告不接受覆盖;
126
+ 2. 会话覆盖:聊天框选择的 effort 或 `coding_reasoning` 工具;
127
+ 3. 确定性分类器:问答/建议/单文件保持 Low;多文件、模块级审查或重构 High;项目级搭建/重构/审查、无法一次修复的缺陷 Max。同一轮内只升不降。
128
+
129
+ `agent/inbox/claimed` 在 Host 快照 step selection 之前同步选 effort,`agent/pre-step` 再兜底;选择经 `agent.model.select()` 写入,因此从下一步生效。
130
+
131
+ ### 2.6 交接层:一份有界记录,取代“靠对话记住需求”
132
+
133
+ 子代理有全新上下文,重启与翻页又只回放持久事件,所以阶段之间靠**显式记录**交接,而不是靠对话历史。`src/handoff.ts` 定义这份记录:
134
+
135
+ - **字段**:`goal`、`constraints`、`acceptance`(交付验证标准)、`findings`、`decisions`、`open`、`tasks`(每项 `goal` / `files` / `acceptance` / `avoid`)、`changes`(`file` + `summary`),外加 `version` 与 `updatedAt`。
136
+ - **读写**:工具 `coding_handoff` 属于 `stage` 类,每个阶段都能用(implement 阶段的裁剪表也保留它)。省略的字段不改动;列表追加并去重;`goal` 覆盖;`tasks` 整体替换;`changes` 按 `file` 合并,后来的摘要更新。`{ reset: true }` 只重置验证预算。
137
+ - **自动写回**:planner 返回的 `tasks` 与 summary、implement 报告的文件(记入 `changes`)与 notes、verify 的 findings 由运行时并入,模型不必重复抄写。
138
+ - **持久化与注入**:`checkpointOf` 携带交接记录(与 `verifyAttempts` / `verifyBlocked` 同批),`restoreFromView` 在重启与翻页时读回;`agent/pre-step` 用 `handoffDigest` 注入摘要;三个 worker 的提示词用 `handoffForWorker` 按角色裁剪——planner 拿 goal / constraints / 验收标准 / findings / 待答问题,implement 另加 decisions 与任务清单,verify 拿 goal / 验收标准 / changes / decisions / findings。
139
+ - **上限与校验**:`HANDOFF_TEXT_LIMIT` 600、`HANDOFF_ITEMS_LIMIT` 10、`HANDOFF_TASKS_LIMIT` 8、`HANDOFF_CHANGES_LIMIT` 20、`HANDOFF_DIGEST_LIMIT` 1200、`HANDOFF_WORKER_LIMIT` 2000;持久 JSON 一律经 `asHandoff` 重校验,版本不符或形状损坏按“无记录”处理。
140
+ - **门禁**:codex 模式下 `plan` 前必须有 goal,`verify` 前必须有 acceptance,`deliver` 前两者都要有——状态行的 `handoff goal=set|missing, acceptance=N` 就是这一判定的可见形式。
141
+
142
+ ## 3. 阶段化交付契约
143
+
144
+ 交付契约按阶段拆成五段(`src/contract.ts`),随 `agent/pre-step` 注入,而不是塞进一段全局系统提示词:
145
+
146
+ | 阶段 | 要点 |
147
+ | --- | --- |
148
+ | requirements | 确认目标、交付物、约束;读 AGENTS.md 与目标文件;只读;用 `coding_handoff` 记录 goal / constraints / acceptance(codex 无 goal 不能进 plan);需求不清用 `coding_mode` 回退问答 |
149
+ | plan | 目标与可观察验收;`delegate_plan` 输出单一职责子任务,并自动并入交接记录;每个子任务写清目标、涉及文件、验收标准、不碰的范围;`todo_write` 3–6 步 |
150
+ | implement | 主代理不能编辑;给 worker 自包含任务并带上交接的 goal / constraints / 验收标准;报告的文件自动并入 changes;编辑数决定验证门禁 |
151
+ | verify | 先窄后宽;先有 acceptance 才能进入;失败按预算记账,耗尽后 deliver 只能 BLOCKED;不得声称未运行的检查 |
152
+ | deliver | 需 goal + acceptance;先结论、再文件路径、再验证证据与残余风险;BLOCKED 交付只报失败证据 |
153
+
154
+ ## 4. 配置参考
155
+
156
+ | 字段 | 必填 | 默认 | 说明 |
157
+ | --- | --- | --- | --- |
158
+ | `defaultMode` | 否 | `auto` | 新会话初始模式 |
159
+ | `plannerWorkerModel` | 是 | `deepseek-account/deepseek-v4-pro` | `provider/model`;默认 pro 模型负责勘探与拆分任务 |
160
+ | `implementWorkerModel` | 是 | — | `provider/model` |
161
+ | `verifyWorkerModel` | 是 | — | `provider/model` |
162
+ | `language` | 否 | `auto` | `auto` 跟随 DSH 界面 → 系统语言 → 宿主 locale |
163
+ | `reasoning` | 否 | `auto` | `auto` 每轮分类;`low`/`high`/`max` 固定所有轮次 |
164
+ | `maxVerifyAttempts` | 否 | `5` | 允许的失败验证次数;`0` 不限制;耗尽后放行 BLOCKED 交付 |
165
+ | `maxSameFailure` | 否 | `2` | 相同失败签名连续出现上限;`0` 不限制 |
166
+ | `verificationTimeoutMs` | 否 | `360000` | 单次验证命令超时(ms),`0` 不限制;超时记为失败 |
167
+ | `verificationCommands` | 否 | `{}` | 按 `kind` 或 `kind:scope` 覆盖自动探测的命令 |
168
+
169
+ worker 路由分三步校验:Host 设置行要求必填且格式为 `provider/model`,预设插件挂载时再解析一次(注册表可读时也检查 provider),派发前用 `assertWorkerModel` 校验模型可用性。任何一步失败都不会回退主代理模型。两个预算键是普通字段,只能在插件包配置里改。
170
+
171
+ ## 5. 已知限制
172
+
173
+ 1. **门禁是工具级边界**:`bash` 能改文件,所以它在 implement / verify / deliver 被拒,verify worker 完全没有 shell;精确命令只走 `run_verification`。requirements / plan 仍允许 shell(需求分析需要),这是有意保留的能力缺口。
174
+ 2. **DSH 没有隐藏预设的开关**:本插件在 Host 侧过滤对外花名册;三个 worker 预设仍在注册表中供子代理派发,但不出现在客户端预设列表。
175
+ 3. **不写自定义持久事件类型**:DSH 拒绝重开含未知非 `ignorable` 事件的会话,所以投影折叠的是本插件工具已经产生的持久事件。
176
+ 4. **未分类的第三方工具不受阶段门禁约束**(门禁只覆盖它能判断语义的编码工具),避免误伤其他插件的交互类工具。
177
+ 5. **verify worker 的报告是子代理的声明**:插件要求报告携带命令否则判失败,但无法验证该命令确实执行过;主代理可在 verify 阶段用 `read` 与 `run_verification` 复核。
178
+ 6. **交接记录是模型主动写的摘要**:插件只校验形状、去重并裁剪长度,无法验证内容真实;写漏的 goal / acceptance 只会让门禁拒绝,不会让门禁变宽松。
179
+ 7. **验证预算按失败签名判定**:模型换一种失败方式(不同失败测试、不同错误行或退出码)会重新计数,`maxSameFailure` 因此可能永远不耗尽,最终只能靠 `maxVerifyAttempts` 停下。
180
+ 8. **`verifyBlocked` 只阻止“声称成功”**:它放行 `deliver` 的推进并标记为 BLOCKED,但交付内容仍是模型自我报告,插件无法验证报告是否诚实。
181
+ 9. **模型目录不可用时无法提前发现不存在的模型**:只有派发时的 `WorkerModelError` 会暴露它;目录可读时同一错误会列出可用模型。
182
+ 10. **超时只保证插件侧返回失败并请求终止**:`terminate` 只是请求;若子进程 provider 不响应,宿主上残留的进程仍需外部清理。
183
+ 11. **失败通知是尽力而为的提示**:它随下一次 `agent/pre-step` 注入;若异常直接终止了 Turn(没有下一步),就没有可注入的位置,此时原因只能由 DSH 的 Turn 错误或插件挂载错误呈现。