dsh-plugin-dev-kb 1.0.0

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 (234) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -0
  3. package/cordis.patch.yml +12 -0
  4. package/kb/INDEX.md +210 -0
  5. package/kb/README.md +69 -0
  6. package/kb/extra/AGENTS.md +75 -0
  7. package/kb/extra/api-gateway.md +164 -0
  8. package/kb/extra/api-gateway.zh.md +164 -0
  9. package/kb/extra/cookbook/adding-a-vendored-package.md +59 -0
  10. package/kb/extra/cookbook/adding-a-vendored-package.zh.md +59 -0
  11. package/kb/extra/cookbook/maintaining-dsh-code-review.md +64 -0
  12. package/kb/extra/cookbook/maintaining-dsh-code-review.zh.md +64 -0
  13. package/kb/extra/cookbook/responding-to-pr-review-on-a-stack.md +32 -0
  14. package/kb/extra/cookbook/responding-to-pr-review-on-a-stack.zh.md +32 -0
  15. package/kb/extra/defensive-patterns.md +33 -0
  16. package/kb/extra/defensive-patterns.zh.md +33 -0
  17. package/kb/extra/development.md +171 -0
  18. package/kb/extra/development.zh.md +171 -0
  19. package/kb/extra/event-producer-consumer.md +76 -0
  20. package/kb/extra/event-producer-consumer.zh.md +78 -0
  21. package/kb/extra/glossary.md +45 -0
  22. package/kb/extra/glossary.zh.md +45 -0
  23. package/kb/extra/graph-atlas.md +24 -0
  24. package/kb/extra/graph-atlas.zh.md +26 -0
  25. package/kb/extra/i18n/README.md +60 -0
  26. package/kb/extra/i18n/README.zh.md +60 -0
  27. package/kb/extra/i18n/style-samples.md +87 -0
  28. package/kb/extra/i18n/terminology.md +214 -0
  29. package/kb/extra/i18n/translation-prompt.md +263 -0
  30. package/kb/extra/i18n/translation-rules.md +69 -0
  31. package/kb/extra/i18n/translation-rules.zh.md +69 -0
  32. package/kb/extra/module-graph.md +1641 -0
  33. package/kb/extra/module-graph.zh.md +1643 -0
  34. package/kb/extra/postmortem/0001-acp-default-export-drops-inject.md +113 -0
  35. package/kb/extra/postmortem/0001-acp-default-export-drops-inject.zh.md +113 -0
  36. package/kb/extra/postmortem/0002-js-expression-disabled-filesystem-tools.md +47 -0
  37. package/kb/extra/postmortem/0002-js-expression-disabled-filesystem-tools.zh.md +47 -0
  38. package/kb/extra/postmortem/0003-web-agent-gui-feedback-loop.md +53 -0
  39. package/kb/extra/postmortem/0003-web-agent-gui-feedback-loop.zh.md +53 -0
  40. package/kb/extra/postmortem/0004-landlock-partial-notice-misclassified-child-failures.md +55 -0
  41. package/kb/extra/postmortem/0004-landlock-partial-notice-misclassified-child-failures.zh.md +55 -0
  42. package/kb/extra/postmortem/README.md +18 -0
  43. package/kb/extra/postmortem/README.zh.md +18 -0
  44. package/kb/extra/rescope.md +53 -0
  45. package/kb/extra/rescope.zh.md +53 -0
  46. package/kb/extra/subsystems/attachment.md +125 -0
  47. package/kb/extra/subsystems/attachment.zh.md +125 -0
  48. package/kb/extra/subsystems/extensions.md +364 -0
  49. package/kb/extra/subsystems/extensions.zh.md +364 -0
  50. package/kb/extra/subsystems/feedback.md +266 -0
  51. package/kb/extra/subsystems/feedback.zh.md +266 -0
  52. package/kb/extra/testing.md +49 -0
  53. package/kb/extra/testing.zh.md +49 -0
  54. package/kb/extra/web-styling.md +25 -0
  55. package/kb/extra/web-styling.zh.md +25 -0
  56. package/kb/meta/search-index.json +1328 -0
  57. package/kb/meta/site-pages.txt +168 -0
  58. package/kb/meta/source.json +13 -0
  59. package/kb/meta/topics.md +75 -0
  60. package/kb/site/develop/basic/config.md +108 -0
  61. package/kb/site/develop/basic/index.md +146 -0
  62. package/kb/site/develop/basic/publish.md +185 -0
  63. package/kb/site/develop/basic/tool.md +54 -0
  64. package/kb/site/develop/cordis-tutorial/01-first-plugin.md +95 -0
  65. package/kb/site/develop/cordis-tutorial/02-lifecycle-and-effects.md +98 -0
  66. package/kb/site/develop/cordis-tutorial/03-services.md +98 -0
  67. package/kb/site/develop/cordis-tutorial/04-events.md +144 -0
  68. package/kb/site/develop/cordis-tutorial/05-config.md +84 -0
  69. package/kb/site/develop/cordis-tutorial/06-composition-and-hmr.md +113 -0
  70. package/kb/site/develop/cordis-tutorial/07-into-the-harness.md +107 -0
  71. package/kb/site/develop/cordis-tutorial/index.md +62 -0
  72. package/kb/site/develop/framework/events.md +145 -0
  73. package/kb/site/develop/framework/index.md +139 -0
  74. package/kb/site/develop/framework/service.md +152 -0
  75. package/kb/site/develop/practice/index.md +157 -0
  76. package/kb/site/develop/practice/llm-adapter.md +190 -0
  77. package/kb/site/en/develop/basic/config.md +108 -0
  78. package/kb/site/en/develop/basic/index.md +146 -0
  79. package/kb/site/en/develop/basic/publish.md +185 -0
  80. package/kb/site/en/develop/basic/tool.md +54 -0
  81. package/kb/site/en/develop/cordis-tutorial/01-first-plugin.md +95 -0
  82. package/kb/site/en/develop/cordis-tutorial/02-lifecycle-and-effects.md +98 -0
  83. package/kb/site/en/develop/cordis-tutorial/03-services.md +98 -0
  84. package/kb/site/en/develop/cordis-tutorial/04-events.md +144 -0
  85. package/kb/site/en/develop/cordis-tutorial/05-config.md +84 -0
  86. package/kb/site/en/develop/cordis-tutorial/06-composition-and-hmr.md +113 -0
  87. package/kb/site/en/develop/cordis-tutorial/07-into-the-harness.md +107 -0
  88. package/kb/site/en/develop/cordis-tutorial/index.md +60 -0
  89. package/kb/site/en/develop/framework/events.md +145 -0
  90. package/kb/site/en/develop/framework/index.md +139 -0
  91. package/kb/site/en/develop/framework/service.md +150 -0
  92. package/kb/site/en/develop/practice/index.md +157 -0
  93. package/kb/site/en/develop/practice/llm-adapter.md +190 -0
  94. package/kb/site/en/guide/providers-custom-form.png +0 -0
  95. package/kb/site/en/guide/providers-models-page.png +0 -0
  96. package/kb/site/en/guide/providers.md +100 -0
  97. package/kb/site/en/guide/python-sdk.md +106 -0
  98. package/kb/site/en/guide/quickstart.md +32 -0
  99. package/kb/site/en/index.md +8 -0
  100. package/kb/site/en/reference/agent-lifecycle.md +86 -0
  101. package/kb/site/en/reference/capability-seams.md +475 -0
  102. package/kb/site/en/reference/config-catalog.md +3155 -0
  103. package/kb/site/en/reference/cookbook/adding-a-conversation-node.md +235 -0
  104. package/kb/site/en/reference/cookbook/adding-a-package.md +120 -0
  105. package/kb/site/en/reference/cookbook/adding-a-settings-card.md +102 -0
  106. package/kb/site/en/reference/cookbook/adding-a-tool.md +96 -0
  107. package/kb/site/en/reference/cookbook/adding-an-llm-adapter.md +45 -0
  108. package/kb/site/en/reference/cookbook/extension-cookbook.md +131 -0
  109. package/kb/site/en/reference/cordis-api/context.md +368 -0
  110. package/kb/site/en/reference/cordis-api/events.md +211 -0
  111. package/kb/site/en/reference/cordis-api/fiber.md +379 -0
  112. package/kb/site/en/reference/cordis-api/inherited.md +43 -0
  113. package/kb/site/en/reference/cordis-api/registry.md +156 -0
  114. package/kb/site/en/reference/cordis-api/service.md +106 -0
  115. package/kb/site/en/reference/cordis-primer.md +46 -0
  116. package/kb/site/en/reference/index.md +131 -0
  117. package/kb/site/en/reference/persistence-catalog.md +949 -0
  118. package/kb/site/en/reference/subsystems/approval.md +173 -0
  119. package/kb/site/en/reference/subsystems/client-modules.md +121 -0
  120. package/kb/site/en/reference/subsystems/code-runtime.md +194 -0
  121. package/kb/site/en/reference/subsystems/commands.md +190 -0
  122. package/kb/site/en/reference/subsystems/compaction.md +241 -0
  123. package/kb/site/en/reference/subsystems/core.md +1073 -0
  124. package/kb/site/en/reference/subsystems/credentials.md +136 -0
  125. package/kb/site/en/reference/subsystems/filesystem.md +498 -0
  126. package/kb/site/en/reference/subsystems/goal.md +280 -0
  127. package/kb/site/en/reference/subsystems/index.md +58 -0
  128. package/kb/site/en/reference/subsystems/invariants.md +91 -0
  129. package/kb/site/en/reference/subsystems/jobs.md +293 -0
  130. package/kb/site/en/reference/subsystems/llm-streaming.md +920 -0
  131. package/kb/site/en/reference/subsystems/lsp.md +205 -0
  132. package/kb/site/en/reference/subsystems/permission-presets.md +134 -0
  133. package/kb/site/en/reference/subsystems/persistence.md +388 -0
  134. package/kb/site/en/reference/subsystems/plan.md +90 -0
  135. package/kb/site/en/reference/subsystems/sandbox.md +221 -0
  136. package/kb/site/en/reference/subsystems/schedule.md +189 -0
  137. package/kb/site/en/reference/subsystems/scope.md +62 -0
  138. package/kb/site/en/reference/subsystems/session-projection.md +265 -0
  139. package/kb/site/en/reference/subsystems/session-query.md +498 -0
  140. package/kb/site/en/reference/subsystems/session-reference.md +111 -0
  141. package/kb/site/en/reference/subsystems/session-telemetry.md +197 -0
  142. package/kb/site/en/reference/subsystems/session-title.md +207 -0
  143. package/kb/site/en/reference/subsystems/session.md +852 -0
  144. package/kb/site/en/reference/subsystems/settings.md +313 -0
  145. package/kb/site/en/reference/subsystems/shell.md +306 -0
  146. package/kb/site/en/reference/subsystems/skills.md +334 -0
  147. package/kb/site/en/reference/subsystems/spill.md +120 -0
  148. package/kb/site/en/reference/subsystems/storage.md +232 -0
  149. package/kb/site/en/reference/subsystems/subagent.md +737 -0
  150. package/kb/site/en/reference/subsystems/subprocess.md +327 -0
  151. package/kb/site/en/reference/subsystems/system-prompt.md +210 -0
  152. package/kb/site/en/reference/subsystems/terminal.md +187 -0
  153. package/kb/site/en/reference/subsystems/token-meter.md +93 -0
  154. package/kb/site/en/reference/subsystems/tools.md +723 -0
  155. package/kb/site/en/reference/subsystems/typert.md +339 -0
  156. package/kb/site/en/reference/subsystems/user-questions.md +181 -0
  157. package/kb/site/en/reference/subsystems/web-server.md +111 -0
  158. package/kb/site/en/reference/subsystems/web.md +202 -0
  159. package/kb/site/en/reference/subsystems/workflow.md +281 -0
  160. package/kb/site/en/reference/subsystems/workspace.md +231 -0
  161. package/kb/site/en/reference/tool-catalog.md +1877 -0
  162. package/kb/site/en/reference/tool-execution-pipeline.md +66 -0
  163. package/kb/site/guide/providers-custom-form.zh.png +0 -0
  164. package/kb/site/guide/providers-models-page.zh.png +0 -0
  165. package/kb/site/guide/providers.md +100 -0
  166. package/kb/site/guide/python-sdk.md +106 -0
  167. package/kb/site/guide/quickstart.md +32 -0
  168. package/kb/site/index.md +8 -0
  169. package/kb/site/reference/agent-lifecycle.md +86 -0
  170. package/kb/site/reference/capability-seams.md +475 -0
  171. package/kb/site/reference/config-catalog.md +3154 -0
  172. package/kb/site/reference/cookbook/adding-a-conversation-node.md +235 -0
  173. package/kb/site/reference/cookbook/adding-a-package.md +120 -0
  174. package/kb/site/reference/cookbook/adding-a-settings-card.md +102 -0
  175. package/kb/site/reference/cookbook/adding-a-tool.md +98 -0
  176. package/kb/site/reference/cookbook/adding-an-llm-adapter.md +45 -0
  177. package/kb/site/reference/cookbook/extension-cookbook.md +133 -0
  178. package/kb/site/reference/cordis-api/context.md +368 -0
  179. package/kb/site/reference/cordis-api/events.md +211 -0
  180. package/kb/site/reference/cordis-api/fiber.md +379 -0
  181. package/kb/site/reference/cordis-api/inherited.md +43 -0
  182. package/kb/site/reference/cordis-api/registry.md +156 -0
  183. package/kb/site/reference/cordis-api/service.md +106 -0
  184. package/kb/site/reference/cordis-primer.md +52 -0
  185. package/kb/site/reference/index.md +135 -0
  186. package/kb/site/reference/persistence-catalog.md +949 -0
  187. package/kb/site/reference/subsystems/approval.md +173 -0
  188. package/kb/site/reference/subsystems/client-modules.md +121 -0
  189. package/kb/site/reference/subsystems/code-runtime.md +194 -0
  190. package/kb/site/reference/subsystems/commands.md +190 -0
  191. package/kb/site/reference/subsystems/compaction.md +241 -0
  192. package/kb/site/reference/subsystems/core.md +1081 -0
  193. package/kb/site/reference/subsystems/credentials.md +136 -0
  194. package/kb/site/reference/subsystems/filesystem.md +498 -0
  195. package/kb/site/reference/subsystems/goal.md +280 -0
  196. package/kb/site/reference/subsystems/index.md +58 -0
  197. package/kb/site/reference/subsystems/invariants.md +91 -0
  198. package/kb/site/reference/subsystems/jobs.md +293 -0
  199. package/kb/site/reference/subsystems/llm-streaming.md +926 -0
  200. package/kb/site/reference/subsystems/lsp.md +205 -0
  201. package/kb/site/reference/subsystems/permission-presets.md +134 -0
  202. package/kb/site/reference/subsystems/persistence.md +388 -0
  203. package/kb/site/reference/subsystems/plan.md +90 -0
  204. package/kb/site/reference/subsystems/sandbox.md +221 -0
  205. package/kb/site/reference/subsystems/schedule.md +189 -0
  206. package/kb/site/reference/subsystems/scope.md +62 -0
  207. package/kb/site/reference/subsystems/session-projection.md +265 -0
  208. package/kb/site/reference/subsystems/session-query.md +498 -0
  209. package/kb/site/reference/subsystems/session-reference.md +111 -0
  210. package/kb/site/reference/subsystems/session-telemetry.md +197 -0
  211. package/kb/site/reference/subsystems/session-title.md +207 -0
  212. package/kb/site/reference/subsystems/session.md +854 -0
  213. package/kb/site/reference/subsystems/settings.md +313 -0
  214. package/kb/site/reference/subsystems/shell.md +306 -0
  215. package/kb/site/reference/subsystems/skills.md +334 -0
  216. package/kb/site/reference/subsystems/spill.md +120 -0
  217. package/kb/site/reference/subsystems/storage.md +232 -0
  218. package/kb/site/reference/subsystems/subagent.md +739 -0
  219. package/kb/site/reference/subsystems/subprocess.md +327 -0
  220. package/kb/site/reference/subsystems/system-prompt.md +210 -0
  221. package/kb/site/reference/subsystems/terminal.md +187 -0
  222. package/kb/site/reference/subsystems/token-meter.md +93 -0
  223. package/kb/site/reference/subsystems/tools.md +723 -0
  224. package/kb/site/reference/subsystems/typert.md +339 -0
  225. package/kb/site/reference/subsystems/user-questions.md +181 -0
  226. package/kb/site/reference/subsystems/web-server.md +111 -0
  227. package/kb/site/reference/subsystems/web.md +202 -0
  228. package/kb/site/reference/subsystems/workflow.md +281 -0
  229. package/kb/site/reference/subsystems/workspace.md +231 -0
  230. package/kb/site/reference/tool-catalog.md +1880 -0
  231. package/kb/site/reference/tool-execution-pipeline.md +66 -0
  232. package/package.json +40 -0
  233. package/scripts/rebuild-index.mjs +88 -0
  234. package/skills/dsh-plugin-dev-kb.md +66 -0
@@ -0,0 +1,723 @@
1
+ ---
2
+ editSource: "docs/subsystems/tools.zh.md"
3
+ outline: [2,3]
4
+ ---
5
+
6
+ # 工具
7
+
8
+ [dsh-tools](https://github.com/deepseek-ai/deepseek-harness/tree/master/packages/core/tools) 的工具流水线。[core.md](./core.md) 介绍了核心包共用、用于编写流水线的类型 `ToolDefinition`;面向模型的 [`ToolSchema`](./llm-streaming.md#the-model-request-and-result) 协议类型与模型请求一起声明。本页记录 `ToolDefinition` 的每个字段、用于构建它的类型化 schema DSL、带守卫的执行类型和 UI 展示类型。
9
+
10
+ 源码:[`packages/core/tools/src/index.ts`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/index.ts) · [`packages/core/tools/src/schema.ts`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/schema.ts) · [`packages/core/tools/src/presentation.ts`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/presentation.ts)
11
+
12
+ ## `ToolDefinition` — 一个已注册的工具
13
+
14
+ 由一个 `ToolSchema`(面向模型的字段)、必需的规范输出声明、`execute` 函数、仅供宿主使用的调度器元数据、可选的最终内容回调和可选 UI 展示函数组成。注册表持有这些定义,循环通过它们分派调用。注册表的 `schemas()` 通过显式允许列表构建面向模型的 `ToolSchema[]`;`output`/`execute`/`finalizeContent`/`timeoutMs`/`isConcurrencySafe`/`presentCall`/`presentResult` 绝不能泄漏到模型请求中。
15
+
16
+ ```ts type-equiv
17
+ /** Tool-owned canonical output contract used after the body returns a JSON value. */
18
+ interface ToolOutputDefinition {
19
+ /** Raw supported JSON Schema enforced against every successful canonical value. */
20
+ readonly schema: JsonSchemaNode
21
+ /** Pure projection from validated arguments and value to Native/model content. */
22
+ render(args: unknown, value: JsonValue): ContentBlock[]
23
+ /** Pure replayable presentation projection, computed only for top-level calls. */
24
+ presentationMeta?(args: unknown, value: JsonValue): JsonValue
25
+ }
26
+ ```
27
+
28
+ ```ts type-equiv
29
+ /** A registered tool: its schema plus the execution function. */
30
+ interface ToolDefinition extends ToolSchema {
31
+ /** Mandatory canonical output declaration. */
32
+ readonly output: ToolOutputDefinition
33
+ /**
34
+ * Run one accepted call and return only its canonical lossless-JSON value.
35
+ * Async work must observe or forward `exec.signal` and settle only after its
36
+ * owned work reaches quiescence. The registry preserves caller cancellation
37
+ * through around-dispatch signal replacement and does not abandon this
38
+ * promise, but it cannot hard-kill same-process code.
39
+ * @param args - losslessly snapshotted, frozen model arguments.
40
+ * @param exec - execution identity, cancellation signal, and context deferral.
41
+ * @returns the canonical value declared by `output.schema`.
42
+ */
43
+ execute(args: unknown, exec: ToolRunContext): Promise<unknown>
44
+ /**
45
+ * Synchronous last-mile transform for model-facing content. The registry
46
+ * snapshots this callback when execution starts and invokes it exactly once
47
+ * for every normalized outcome, including pipeline failures that bypass
48
+ * `tools/post-execute`, immediately before lossless materialization.
49
+ * Returning `undefined` preserves the content; every other result field
50
+ * remains registry-owned. The callback must be total and must not throw.
51
+ * @param exec - immutable execution identity and arguments.
52
+ * @param result - complete normalized outcome before materialization.
53
+ * @returns replacement content, or `undefined` to preserve it.
54
+ */
55
+ finalizeContent?(exec: Readonly<ToolExecution>, result: Readonly<ToolExecutionResult>): ContentBlock[] | undefined
56
+ /**
57
+ * Cooperative tool-call timeout budget in milliseconds. Omit for no deadline.
58
+ * Enforced by `@deepseek-ai/dsh-tool-call-timeout-policy` (a `tools/execute` wrapper); it
59
+ * is NEVER sent to the model — `schemas()` whitelists only name/description/
60
+ * parameters. Declaring it asserts this tool forwards `exec.signal` to a
61
+ * cooperative implementation that can reach quiescence when the signal aborts.
62
+ */
63
+ timeoutMs?: number
64
+ /**
65
+ * Pure synchronous classifier for overlap with sibling tool calls. Only
66
+ * `true` opts in; omission, exceptions, non-`true` returns, and invalid
67
+ * `defineTool` arguments are exclusive. This metadata is never model-visible.
68
+ *
69
+ * Opted-in executions must not mutate parent-owned state. Shared state must
70
+ * tolerate concurrent dispatch; recorder races are permitted only when they
71
+ * commute or fail closed. See the
72
+ * [parallel-tool-call Agent Note](../../../../.agents/notes/implemented/feature/2026-07-10-parallel-tool-call-execution.md)
73
+ * for the full contract.
74
+ * @param args - parsed arguments; `defineTool` validates before calling.
75
+ * @returns Whether this call may join a parallel group.
76
+ */
77
+ isConcurrencySafe?(args: unknown): boolean
78
+ /**
79
+ * Optional: how to present the PENDING state of one call in a UI, derived from
80
+ * the call's `args` (parsed arguments, `unknown` — the tool validates/narrows
81
+ * its own input). Returns a {@link ToolCallView} (a `card`-tagged render intent),
82
+ * or `undefined` (or omit the method) to fall back to a generic presentation
83
+ * (title = tool name, raw args as input). Pure and side-effect-free: a UI may
84
+ * call it during live streaming AND a session-log replay, so it must depend
85
+ * only on `args`.
86
+ */
87
+ presentCall?(args: unknown): ToolCallView | undefined
88
+ /**
89
+ * Optional: how to present the COMPLETED state, given the same `args` and the
90
+ * durable result projection (`content`, failure state, and optional `meta`). Returns a
91
+ * {@link ToolResultView}, or `undefined` (or omit the method) to keep the
92
+ * pending title and render the raw result content. Pure and side-effect-free
93
+ * for the same replay reason.
94
+ */
95
+ presentResult?(args: unknown, result: ToolResult): ToolResultView | undefined
96
+ }
97
+ ```
98
+
99
+ `execute` 接收 `args: unknown`——原始的 `ToolDefinition` 自行校验输入。第一方工具不需要手写校验;它们使用 `defineTool`,由后者代为校验并收窄参数类型、根据 `output.schema` 推导函数体返回类型,并为两个输出投影器提供类型约束。`finalizeContent` 特意接收不可变的执行对象而非类型化参数,因为无效输入和外层流水线失败也会到达该回调;它可以施加工具自有的内容限制,同时保留 `isError`、规范值、结构化错误身份、延迟上下文与展示元数据。
100
+
101
+ ## 统一的 JSON 值 schema DSL
102
+
103
+ 插件作者使用同一套词汇描述类型化参数和类型化输出值。`ValueSchemaSpec` 支持 `string`、`number`、`integer`、`boolean`、`null`、`array`、`object`、仅作者侧可用的 `json`,以及要求恰好命中一个分支的 `oneOf`;标量 `enum` 和 `const` 值必须与节点类型匹配。显式对象节点始终声明 `additionalProperties: true | false`。参数定义仍是隐式的开放对象属性映射,每个必填属性都附带 `required: true`。
104
+
105
+ 源码:[`packages/core/tools/src/schema.ts`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/schema.ts)
106
+
107
+ ```ts type-equiv
108
+ /** One author-facing schema for any lossless JSON value root. */
109
+ type ValueSchemaSpec =
110
+ | StringValueSchemaSpec
111
+ | NumberValueSchemaSpec
112
+ | IntegerValueSchemaSpec
113
+ | BooleanValueSchemaSpec
114
+ | NullValueSchemaSpec
115
+ | ArrayValueSchemaSpec
116
+ | ObjectValueSchemaSpec
117
+ | JsonValueSchemaSpec
118
+ | OneOfValueSchemaSpec
119
+ ```
120
+
121
+ ```ts type-equiv
122
+ /** One implicit parameter-root property, optionally required. */
123
+ type ParameterPropertySpec = ValueSchemaSpec & { required?: true }
124
+ ```
125
+
126
+ ```ts type-equiv
127
+ /**
128
+ * Tool parameter schema. The map itself is an implicit open object root;
129
+ * requiredness remains a per-property `required: true` annotation.
130
+ */
131
+ type ParameterSchemaSpec = {
132
+ [key: string]: ParameterPropertySpec
133
+ [key: symbol]: never
134
+ }
135
+ ```
136
+
137
+ `{ type: 'json' }` 推导为 `JsonValue`,并编译成仅含注解、不施加约束的原始 schema。输出根可以是对象、数组、标量或 null。`InferValue<S>` 在 16 层容器内保留字面量约束与对象开放性,之后回退为 `JsonValue`,避免耗尽 TypeScript 的类型实例化栈。`InferArgs<P>` 依据逐属性的必填标记生成必填和可选的字符串键:
138
+
139
+ ```ts type-equiv
140
+ /**
141
+ * Infer the TypeScript value accepted by an author-facing value schema. Exact
142
+ * inference is bounded to 16 container levels, then falls back to `JsonValue`.
143
+ */
144
+ type InferValue<S> = InferValueAt<S, []>
145
+ ```
146
+
147
+ ```ts type-equiv
148
+ /** Infer the TypeScript argument object for an implicit parameter schema. */
149
+ type InferArgs<S> = InferProperties<S, []>
150
+ ```
151
+
152
+ `defineTool({ name, description, parameters, output, execute, … })` 将参数推导与 `parameterSchemaSpecToJsonSchema()` 和 `validateArgs()` 绑定,并将 `execute`/`render`/`presentationMeta` 与 `InferValue<OutputSchema>` 绑定。schema 记录只包含自有且可枚举的字符串键,schema 数组是稠密的内建数组,因此推导、编译与校验观察到的是同一份声明。精确推导保持到 16 层容器,之后放宽为 `JsonValue`;运行时校验仍会继续遍历完整 schema。`valueSchemaSpecToJsonSchema()` 通过同一套已强制执行的原始子集编译输出声明。参数不匹配时抛出 `ToolArgsError`(`INVALID_ARGS`);函数体或后置策略产生的值无效时抛出 `ToolOutputError`(`INVALID_TOOL_OUTPUT`)。两者都经由常规工具错误路径处理。原始 JSON Schema 默认保持开放;不支持的关键字会被拒绝,而不会在未强制执行的情况下获准进入。
153
+
154
+ 注册是一项受信任的同进程约定。注册表以 readonly 输入借用已类型化定义,要求它声明 `output`,校验其原始 schema,并检查 `timeoutMs` 必须为正有限值等语义要求;`schemas()` 在构建请求时生成面向模型的投影,使执行和展示共享同一份已解析定义,而不会将回调泄漏到协议上。
155
+
156
+ ## `ToolRestriction` — 单个作用域对其继承内容的实时过滤器
157
+
158
+ `ToolRestriction` 作用于该作用域继承来的工具:部署全局层,加上其链上的每个祖先作用域。注册表将 readonly 名称编译为私有集合,对多个限制取交集,再叠加该作用域**自身**的注册——后者不受约束,因此被委派的子 agent 会保留其回报所依赖的工具。仅 deny 的过滤器允许后续未列出的继承工具通过,而 allow 列表则排除它们。
159
+
160
+ ```ts type-equiv
161
+ /**
162
+ * Per-scope filter over global tools. Restrictions intersect and do not affect
163
+ * scoped registrations or the reserved Code Mode transport.
164
+ */
165
+ interface ToolRestriction {
166
+ /** Global tool names that stay visible; everything else is removed. */
167
+ readonly allow?: readonly string[]
168
+ /** Global tool names removed from visibility. */
169
+ readonly deny?: readonly string[]
170
+ }
171
+ ```
172
+
173
+ ## 执行:可扩展的 waterfall(瀑布式事件)加单调策略
174
+
175
+ `ctx.tools.execute()` 接受由调用方拥有且包含必需 readonly `signal` 的 `ToolExecutionInput`,将其解析后的 JSON 参数一次性物化为流水线拥有的 `ToolExecution`,然后让调用依次经过 `tools/pre-execute`(可重排的 allow/deny/ask waterfall)→ 已注册的单调 guard → `tools/execute`(环绕分派包装层)→ `tools/post-execute`(检查/替换结果)→ 可选且由定义拥有的 `finalizeContent` → `tools/result`(不可变的权威结果)。只有 `tools/execute` 视图可以替换必需的 signal。最终产出为 `ToolExecutionResult`。
176
+
177
+ ```ts type-equiv
178
+ /** Opaque call identity that permits correlation without exposing mutable execution state. */
179
+ type ToolExecutionToken = symbol & { readonly [toolExecutionTokenBrand]: true }
180
+ ```
181
+
182
+ ```ts type-equiv
183
+ /**
184
+ * Caller-supplied description of one tool call. {@link ToolRuntime.execute}
185
+ * adds the registry-owned token to form a pipeline {@link ToolExecution};
186
+ * callers do not choose that token.
187
+ */
188
+ interface ToolExecutionInput {
189
+ readonly callId: CallId
190
+ /**
191
+ * Root model-requested call owning this execution tree. Callers omit it for
192
+ * a root execution; nested dispatchers propagate the enclosing value.
193
+ */
194
+ readonly rootCallId?: CallId
195
+ readonly name: string
196
+ /** Losslessly JSON-serializable parsed arguments (tools validate their own schema). */
197
+ readonly arguments: unknown
198
+ /** The agent on whose behalf the call runs (set by the agent loop). */
199
+ readonly agent?: Agent
200
+ /**
201
+ * Opaque token of the enclosing transport execution, when one exists. Code
202
+ * Mode sets this on SDK sub-dispatches so commit-style observers can wait for
203
+ * the outer `run_code` outcome without receiving its live mutable execution.
204
+ * The token also marks the call as a transport sub-dispatch rather than a
205
+ * model-direct call: under `mode: 'code'`, only calls WITH a parent may
206
+ * execute a native tool name — a model-direct call (no parent) is denied as
207
+ * `UNKNOWN_TOOL` before the policy pipeline. See {@link ToolRuntime.execute}.
208
+ */
209
+ readonly parent?: ToolExecutionToken
210
+ /** Required caller-owned cancellation for this invocation. */
211
+ readonly signal: AbortSignal
212
+ }
213
+ ```
214
+
215
+ 工具函数体接收运行时扩展。`deferContext()` 把上下文附着到本次执行自己的结果上——既是组合工具转运嵌套分派上下文的通道,也可供叶子工具铸造插件来源指令——而不会在外层调用尚未结束时注入这些上下文。
216
+
217
+ ```ts type-equiv
218
+ /**
219
+ * Runtime context handed to a tool implementation after the registry has
220
+ * accepted a {@link ToolExecution}. {@link deferContext} attaches context to
221
+ * this execution's own result — a composite tool ferries nested-dispatch
222
+ * context back to the outer result, and a leaf tool may mint a fresh
223
+ * plugin-sourced instruction; the loop appends it only after the
224
+ * `tool/result`.
225
+ */
226
+ interface ToolRunContext extends ToolExecution {
227
+ /**
228
+ * Defer one context — typically a nested-dispatch context ferried by a
229
+ * composite tool, or a fresh plugin-sourced instruction — until this tool's
230
+ * final result reaches the agent loop. Contexts retain their individual
231
+ * source and metadata and are emitted in call order.
232
+ */
233
+ deferContext(context: UserMessage): void
234
+ /**
235
+ * Mark a successful final result as terminal for the current agent turn.
236
+ * The marker rides this execution's own result (`concludesTurn` exists only
237
+ * on {@link ToolExecutionSuccess}); a composite that dispatches nested
238
+ * calls forwards it from the nested result, exactly like
239
+ * `additionalContexts`, so only an authoritative nested success can
240
+ * conclude the enclosing run.
241
+ */
242
+ concludeTurn(): void
243
+ }
244
+ ```
245
+
246
+ agent loop(智能体循环)向注册表查询每个待处理调用的执行模式,并据此形成独占屏障和滚动池并行执行:
247
+
248
+ ```ts type-equiv
249
+ /**
250
+ * Scheduling mode for one pending call. `parallel` may overlap with siblings;
251
+ * `exclusive` runs alone and forms an ordering barrier.
252
+ */
253
+ type ToolExecutionMode =
254
+ | { kind: 'parallel' }
255
+ | { kind: 'exclusive' }
256
+ ```
257
+
258
+ Code Mode 的桥接层还会把每个已结算的子分派暴露给 `tools/code-dispatch-log` waterfall,该 waterfall 可以更改持久事件所存的内容副本(程序取得的值和模型可见结果均不受影响):
259
+
260
+ ```ts type-equiv
261
+ /**
262
+ * One settled `run_code` sub-dispatch about to be logged, as seen by the
263
+ * `tools/code-dispatch-log` waterfall: the parent execution (session owner,
264
+ * outer call identity), the sub-call identity, and the outcome whose durable
265
+ * copy a listener may reshape. `content` is the RENDERED result projection
266
+ * (what a native `tool/result` would carry) — the program itself received
267
+ * the structured `value` (or just the error message on failure); only the
268
+ * `tool/code-dispatch` event's copy changes.
269
+ */
270
+ interface CodeDispatchLog {
271
+ /** The outer `run_code` execution. */
272
+ readonly exec: ToolExecution
273
+ /** The calling agent (the scope routing key and the spill owner), when the outer call has one. */
274
+ readonly agent?: Agent
275
+ /** Deterministic sub-call id (`<parent>:code:<n>`). */
276
+ readonly subCallId: CallId
277
+ /** The dispatched sub-tool name. */
278
+ readonly name: string
279
+ /** Whether the sub-call settled as an error. */
280
+ readonly isError: boolean
281
+ /** The sub-call's complete model-facing content (the settle event's default payload). */
282
+ readonly content: ContentBlock[]
283
+ }
284
+ ```
285
+
286
+ ```ts type-equiv
287
+ /**
288
+ * One pending tool call inside the registry pipeline. Parsed arguments cross
289
+ * one lossless-JSON materialization boundary before policy and are deep-frozen;
290
+ * call identity, the caller signal, and the registry-assigned {@link token} are
291
+ * readonly. The registry freezes the complete object before `tools/result`
292
+ * observers run.
293
+ */
294
+ interface ToolExecution extends ToolExecutionInput {
295
+ /** Root model-requested call, resolved for every root and nested execution. */
296
+ readonly rootCallId: CallId
297
+ /** Registry-assigned identity shared with nested calls only as their opaque `parent` token. */
298
+ readonly token: ToolExecutionToken
299
+ }
300
+ ```
301
+
302
+ ```ts type-equiv
303
+ /**
304
+ * Around-dispatch view of a {@link ToolExecution}. A `tools/execute` wrapper
305
+ * may replace the signal for its delegated lifetime, but it cannot remove it.
306
+ * The registry fuses every replacement with the captured caller signal.
307
+ */
308
+ interface ToolDispatchExecution extends Omit<ToolExecution, 'signal'> {
309
+ /** Cancellation signal visible to the next wrapper or tool body. */
310
+ signal: AbortSignal
311
+ }
312
+ ```
313
+
314
+ `ToolExecutionToken` 是不透明的运行时 `Symbol`,仅用于身份比较。策略执行前,`execute()` 会物化并冻结参数、拒绝非 JSON 输入并分配 token。身份字段、调用方必需的 signal 和可选的 parent token 均保持 readonly。`ToolDispatchExecution` 包装层可以替换 signal 但不能移除;注册表会在调用工具函数体前重新融合调用方的 signal。最终观察者接收冻结的执行身份。
315
+
316
+ `ToolGuard` 是感知作用域的最终预分派策略。其返回类型有意不包含 allow 结果:`undefined` 保留 waterfall 的决策,而返回的 reason 只能缩减权限,因此后续监听器无法撤销它。
317
+
318
+ ```ts type-equiv
319
+ /**
320
+ * A monotonic execution guard evaluated after every `tools/pre-execute`
321
+ * listener and before the tool body. Returning a reason denies the call;
322
+ * returning `undefined` leaves it unchanged. Because guards have no allow
323
+ * result, listener ordering cannot turn a denial back into permission.
324
+ * @param execution - the identity-protected call after extensible pre-execute policy completed.
325
+ * @returns a final denial reason, or `undefined` to leave the call allowed.
326
+ */
327
+ type ToolGuard = (execution: Readonly<ToolExecution>) => string | undefined
328
+ ```
329
+
330
+ ```ts type-equiv
331
+ /** Canonical failure detail; internal routing information remains optional. */
332
+ interface ToolFailure {
333
+ /** Human-readable failure message without the Native `Error: ` envelope. */
334
+ message: string
335
+ /** Internal error class/code used by policy and durable diagnostics. */
336
+ info?: ToolErrorInfo
337
+ }
338
+ ```
339
+
340
+ ```ts type-equiv
341
+ /** Successful canonical tool execution, including its Native/model projection. */
342
+ interface ToolExecutionSuccess {
343
+ readonly isError: false
344
+ /** Execution-local canonical value; deliberately omitted from durable events. */
345
+ readonly value: JsonValue
346
+ readonly content: ContentBlock[]
347
+ readonly error?: never
348
+ readonly meta?: JsonValue
349
+ readonly additionalContexts?: UserMessage[]
350
+ /** The agent loop stops after committing this successful result batch. */
351
+ readonly concludesTurn?: true
352
+ }
353
+ ```
354
+
355
+ ```ts type-equiv
356
+ /** Failed canonical tool execution; failures never carry a successful value. */
357
+ interface ToolExecutionFailure {
358
+ readonly isError: true
359
+ readonly error: ToolFailure
360
+ readonly value?: never
361
+ readonly content: ContentBlock[]
362
+ readonly meta?: JsonValue
363
+ readonly additionalContexts?: UserMessage[]
364
+ readonly concludesTurn?: never
365
+ }
366
+ ```
367
+
368
+ ```ts type-equiv
369
+ /** The discriminated, execution-local outcome of one tool call. */
370
+ type ToolExecutionResult = ToolExecutionSuccess | ToolExecutionFailure
371
+ ```
372
+
373
+ 结果仅承载产出。调用身份保留在不可变的 `ToolExecution` 上,后者伴随结果经过每个钩子,并出现在持久化的 `tool/call` / `tool/result` 会话事件上,因此包装层无法创建第二个相互矛盾的身份。规范的 `value` 仅存在于执行期间:循环只持久化 `content`、`error` 和 `meta`,`tool/code-dispatch` 则原样存储子调用渲染后的 `content` 与 `isError`。回放可以重现展示,却无法重建规范的中间值。
374
+
375
+ 成功时,注册表会快照并校验函数体返回值,将其冻结,然后调用纯渲染器;对于直接的外层调用,还会调用可选的元数据投影器。注册表会在 `tools/result` 之前另行物化持久展示字段;无效值、渲染器/投影器失败或非 JSON 展示都会转为 JSON 安全的 `isError`。因此,最终实时观察者能看到精确的执行期值,以及可安全用于后续持久追加的字段。
376
+
377
+ 在得到最终内容之前,注册表会物化候选结果;若内容、结构化错误、附加上下文或展示元数据无法物化,则会转为仍可到达 `finalizeContent` 的 JSON 安全 `isError` 结果。注册表恰好调用该回调一次,随后在 `tools/result` 之前立即物化并冻结已接受的结果,因此实时观察到的产出可安全用于后续持久化的 `tool/result` 追加。
378
+
379
+ 每个拦截 waterfall 返回一个类型化的 **Decision**(与 `agent/*` waterfall 共享的惯用模式)。`tools/pre-execute` 监听器接收 `(exec, next)` 并返回 `PreToolDecision`;`tools/execute` 包装层返回 `ToolExecutionResult`;`tools/post-execute` 监听器接收 `(exec, result, next)` 并返回 `PostToolDecision`:
380
+
381
+ ```ts type-equiv
382
+ /**
383
+ * Pre-dispatch decision. `allow` runs the call; `deny` materializes an error;
384
+ * `ask` runs only after an approval service returns `allowed-once` and otherwise
385
+ * denies. Input rewriting is excluded because arguments are already logged and
386
+ * presented.
387
+ */
388
+ type PreToolDecision =
389
+ | { kind: 'allow' }
390
+ | { kind: 'deny'; reason: string }
391
+ | { kind: 'ask'; reason?: string }
392
+ ```
393
+
394
+ ```ts type-equiv
395
+ /**
396
+ * Post-dispatch decision: accept, replace one projection, attach context for the
397
+ * next request, or block by turning corrective feedback into an error result.
398
+ */
399
+ type PostToolDecision =
400
+ | { kind: 'accept'; content?: ContentBlock[]; value?: never; additionalContexts?: UserMessage[] }
401
+ | { kind: 'accept'; value: JsonValue; content?: never; additionalContexts?: UserMessage[] }
402
+ | { kind: 'block'; feedback: ContentBlock[]; additionalContexts?: UserMessage[] }
403
+ ```
404
+
405
+ 调用 `next()` 获取默认决策,或直接返回一个决策以短路。前置策略可以 deny 或 ask;只有 `allowed-once` 才继续执行,而未授权、缺少审批通道或服务、或无 agent 的请求都会变为拒绝。Guard 仍可施加最终拒绝。参数不可被改写,因为历史记录、审计、UI 和执行必须保持一致。
406
+
407
+ 后置策略可以替换内容或值,但不能同时替换两者。替换内容会保留规范值和现有元数据;替换值会重新校验并重新计算内容/元数据;阻止会移除值,并转为包含纠正反馈的 `isError`。内容替换是展示策略,而非保密策略;需要隐藏程序化值的监听器必须阻止或替换该值。`tools/result` 在归一化后接收冻结的执行和结果;观察者无法对其进行变换,观察者的失败也会被隔离。未知工具和抛出异常的工具都会变为结构化错误(`ToolNotFoundError` 映射为 `UNKNOWN_TOOL`),调用失败但不终止当前轮次。
408
+
409
+ ## 已强制执行的原始 JSON Schema 子集
410
+
411
+ subagent、工作流、MCP 和动态注册提供的原始 schema 使用作者侧 DSL 在协议层的对应表示。`assertSupportedJsonSchema()` 接受任意 JSON 根,`validateJsonSchemaValue()` 强制执行该 schema,`JsonSchemaError` 则报告每条不受支持或格式错误的 schema 路径。仅含注解的空节点表示不受约束的无损 JSON。`oneOf` 至少要求两个分支,且一个值必须恰好匹配其中一个。仍要求对象根的消费方调用 `assertObjectJsonSchema()` 并携带 `ObjectJsonSchema`;这样,subagent/工作流中由调用方定义的结构化输出可以继续以对象为根,而不会限制共享词汇。
412
+
413
+ ```ts type-equiv
414
+ /** Scalar JSON values supported by `enum` and `const`. */
415
+ type JsonSchemaScalar = string | number | boolean | null
416
+ ```
417
+
418
+ ```ts type-equiv
419
+ /** Single-type keywords accepted by the enforced subset. */
420
+ type JsonSchemaType = 'object' | 'array' | 'string' | 'number' | 'integer' | 'boolean' | 'null'
421
+ ```
422
+
423
+ ```ts type-equiv
424
+ /**
425
+ * One raw JSON Schema node in the enforced subset. The optional fields express
426
+ * the external wire schema; {@link assertSupportedJsonSchema} rejects invalid
427
+ * combinations before a caller treats the node as trusted.
428
+ */
429
+ interface JsonSchemaNode {
430
+ /** Omit with no constraints for any JSON value, or use `oneOf`. */
431
+ type?: JsonSchemaType
432
+ /** Exactly one branch must validate; at least two branches are required. */
433
+ oneOf?: JsonSchemaNode[]
434
+ /** Nested property schemas (`type: 'object'` only). */
435
+ properties?: Record<string, JsonSchemaNode>
436
+ /** Required property names; each must appear in `properties`. */
437
+ required?: string[]
438
+ /** `false` rejects undeclared keys; absent/`true` follows JSON Schema's open default. */
439
+ additionalProperties?: boolean
440
+ /** Item schema (`type: 'array'` only); absent accepts any JSON item. */
441
+ items?: JsonSchemaNode
442
+ /** Allowed values for a scalar node. */
443
+ enum?: JsonSchemaScalar[]
444
+ /** The single allowed value for a scalar node. */
445
+ const?: JsonSchemaScalar
446
+ /** Annotation, ignored for validation. */
447
+ description?: string
448
+ /** Annotation, ignored for validation. */
449
+ title?: string
450
+ /** Annotation, ignored for validation but required to be lossless JSON. */
451
+ default?: JsonValue
452
+ /** Annotation, ignored for validation but required to be lossless JSON. */
453
+ examples?: JsonValue
454
+ }
455
+ ```
456
+
457
+ ```ts type-equiv
458
+ /** A consumer-constrained object-rooted schema. */
459
+ type ObjectJsonSchema = JsonSchemaNode & { type: 'object' }
460
+ ```
461
+
462
+ ## 工具展示 UI 词汇
463
+
464
+ 工具希望其调用在 UI 中如何呈现(编辑器工具调用卡片、CLI(命令行界面)日志行),提供方无关,使工具在不依赖任何客户端协议的情况下描述自身。`presentCall`/`presentResult` 返回一个 **`card` 标签的渲染意图**——一个可辨识联合类型,UI 桥接层据此分发:
465
+
466
+ - `ToolCallView`(待执行):`{ card: 'generic', title, kind?, rawInput?, content?, locations? }`(默认卡片;`locations` 是 `{ path, line? }[]`,表示调用读取/修改的文件,供编辑器跟随)、`{ card: 'terminal', title, description?, cwd? }`(shell 命令→终端卡片)、或 `{ card: 'diff', title, diffs, locations? }`(文件创建/修改→行内 diff 卡片;`diffs` 是 `{ path, oldText, newText }[]`,新文件时 `oldText: null`)。
467
+ - `ToolResultView`(已完成):`{ card: 'generic', title?, content? }`、`{ card: 'terminal', title?, output?, exitCode?, signal? }`(捕获的运行输出 + 退出状态;有能力的 UI 显示退出状态标签,其他 UI 可以派生围栏 ` ```console ` 回退)、`{ card: 'diff', title?, diffs }`(已完成的文件变更→要展示的变更,通常是从变更前后内容计算出带上下文行的已应用 hunk,或在没有前像时的整文件 diff)、`{ card: 'search', shape, title?, truncated, total, … }`(已完成的发现型搜索→`shape: 'matches'`(grep)为按文件分组的匹配,`shape: 'paths'`(glob)为扁平路径列表;`truncated`/`total` 报告内联结果是否被截断,使 UI 永不把部分结果当作完整结果呈现;该视图不携带结果文本——无 search 卡片的 UI 回退到原始结果内容)、`{ card: 'read', title?, path, offset, lines, totalLines, lang?, content? }`(已完成的文件读取→带行号、可选语法高亮的代码视图;`offset` 是窗口请求的 1-based 起始行,即使 `lines` 为空也保留;`lang` 是从扩展名推得的语言提示,`content` 是无读取能力的 UI 回退时使用的去信封文本)、或 `{ card: 'web', kind: 'search' | 'fetch', title?, … }`(已完成的 web 检索;`kind: 'search'` 携带结构化的 `sources`/`answer?`/`truncated`,`kind: 'fetch'` 携带 `url`/`statusCode`/`truncated`,不具备 `web` 能力的 UI 回退到原始结果内容——正文不会重复进视图)。已完成视图会替换待执行视图,因此变更工具即使与调用时的片段重复也要返回 diff 结果;搜索和 web 检索都没有 `card` 的调用时对应视图(其 pending 状态保持为 generic 卡片,因为结构化结果只在 `execute` 之后才存在)。
468
+
469
+ `ToolCallKind`(`'read' | 'edit' | 'delete' | 'move' | 'search' | 'execute' | 'fetch' | 'other'`)用于为通用卡片选择图标。`FileLocation`(`{ path, line? }`)、`FileDiff`(`{ path, oldText, newText }`)与 `ReadFileLine`(`{ number, text }`,读取窗口中一行带 1-based 行号的内容)是共享的文件卡片词汇。该设计由[渲染意图联合类型 Agent Note](https://github.com/deepseek-ai/deepseek-harness/blob/master/.agents/notes/implemented/architecture/2026-07-02-tool-render-intent-union.md)固定;host/client 运行时将这套中性词汇投影为各自的视图。
470
+
471
+ 完整的展示字段文档见 [`packages/core/tools/src/presentation.ts`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/presentation.ts)。`bash` schema 与执行器见 [shell.md](./shell.md);通用后台控制见 [jobs.md](./jobs.md)。
472
+
473
+ <!-- BEGIN GENERATED cordis-surface (gen-cordis-catalog.ts) — do not edit between markers -->
474
+
475
+ <a id="cordis-surface"></a>
476
+
477
+ ## Cordis API
478
+
479
+ Generated from source by `scripts/gen-cordis-catalog.ts` (verified fresh by `pnpm run verify-cordis-catalog` in doc-sync; regenerate with `pnpm run gen-cordis-catalog`) — this section is byte-identical in both language sides of the page. Signature blocks use a `ts cordis-catalog` fence and keep the original source JSDoc; dispatch modes are defined in the [primer](../cordis-primer.md#dispatch-modes), and the framework-inherited `ctx` API lives in [cordis-api/inherited.md](../cordis-api/inherited.md).
480
+
481
+ <a id="ctxtools--toolruntime"></a>
482
+
483
+ ### `ctx.tools` — `ToolRuntime`
484
+
485
+ Tool registry and execution pipeline. Scoped registrations shadow globals; one visibility resolver feeds presentation, lookup, and dispatch.
486
+
487
+ ```ts cordis-catalog
488
+ /**
489
+ * Present the calling scope's tools in `mode` instead of the deployment
490
+ * default. Nearest scope on the chain wins, so a preset's standing
491
+ * declaration covers every agent joined under it.
492
+ *
493
+ * Scoped only, and one declaration per scope: this is how an agent preset
494
+ * composes Code Mode agents beside native ones in the same process, and a
495
+ * process-global override would be the `mode` config field instead.
496
+ * @param mode - the presentation the covered agents' models see.
497
+ * @returns the exact disposer that restores the deployment default.
498
+ */
499
+ presentAs(mode: ToolPresentationMode): () => void
500
+
501
+ /**
502
+ * Register globally or in the calling agent scope. Scoped tools shadow
503
+ * globals; duplicates within one layer and the reserved `run_code` name fail.
504
+ * @param definition - tool schema, execution, and optional finalization/presentation callbacks.
505
+ * @returns the exact disposer that unregisters the tool.
506
+ */
507
+ register(definition: ToolDefinition): () => void
508
+
509
+ /**
510
+ * Restrict global tools for the calling agent scope. Empty filters, unknown
511
+ * names, scope-local names, and reserved transport names fail. Restrictions
512
+ * intersect; scoped registrations remain visible.
513
+ * @param filter - global-tool mask: `allow` (keep only) and/or `deny` (remove).
514
+ * @returns the exact disposer that lifts this restriction.
515
+ */
516
+ restrict(filter: ToolRestriction): () => void
517
+
518
+ /**
519
+ * Register a monotonic guard after the extensible `tools/pre-execute`
520
+ * waterfall. A plain-context guard applies globally; one registered through
521
+ * `agent.ctx` applies only to that agent. Any matching guard may deny by
522
+ * returning a reason, while no guard can force-allow a call another guard
523
+ * denied. The exact effect disposer is returned for ordered ownership and
524
+ * HMR cleanup.
525
+ * @param guard - synchronous check; a returned string denies the execution.
526
+ * @returns the exact disposer that unregisters the guard.
527
+ */
528
+ guard(guard: ToolGuard): () => void
529
+
530
+ /**
531
+ * Look up a tool as one scope sees it (scoped
532
+ * shadows global; a restricted-away global reads as absent). Presenters pass
533
+ * the calling agent so the rendered card matches the definition that
534
+ * actually executed.
535
+ * @param name - the tool name as registered.
536
+ * @param scope - the viewing scope (the agent); omitted = the global view.
537
+ * @returns the definition the scope resolves, or undefined when none is visible.
538
+ */
539
+ get(name: string, scope?: ScopeKey): ToolDefinition | undefined
540
+
541
+ /**
542
+ * Project visible definitions onto the allowlisted model-facing schema fields,
543
+ * excluding execution and presentation callbacks.
544
+ * @param scope - the viewing scope (the agent); omitted = the global view.
545
+ * @returns one deep-cloned schema per visible tool.
546
+ */
547
+ schemas(scope?: ScopeKey): ToolSchema[]
548
+
549
+ /**
550
+ * Classify a pending call through the caller's visible tool definition. Only
551
+ * an exact `true` is parallel; unknown, hidden, undeclared, invalid, or
552
+ * throwing classifiers are exclusive.
553
+ * @param exec - call name, parsed arguments, and optional agent scope.
554
+ * @returns the fail-closed scheduling mode.
555
+ */
556
+ executionMode(exec: ToolExecutionInput): ToolExecutionMode
557
+
558
+ /**
559
+ * Execute through pre-policy, guards, around-dispatch, post-policy,
560
+ * definition-owned content finalization, and final notification. Tool and
561
+ * listener failures resolve as materialized error results; an invisible tool
562
+ * reports `UNKNOWN_TOOL`. The returned outcome is the same lossless, frozen
563
+ * snapshot final observers receive. Cancellation
564
+ * arriving after entry and before final result materialization skips a
565
+ * not-yet-started body with `ABORTED_BEFORE_DISPATCH` or replaces a
566
+ * successful started outcome with `ABORTED`; already-started work is still
567
+ * drained and may retain a tool-owned structured error.
568
+ * @param exec - the typed same-process call input. The registry assigns its
569
+ * correlation token before policy begins.
570
+ * @returns the materialized final result.
571
+ */
572
+ async execute(exec: ToolExecutionInput): Promise<ToolExecutionResult>
573
+ ```
574
+
575
+ Types: [ScopeKey](./scope.md)
576
+
577
+ Source: [`packages/core/tools/src/index.ts:787`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/index.ts)
578
+
579
+ <a id="tools-events"></a>
580
+
581
+ ### `tools/*` events
582
+
583
+ <a id="toolschange--emit"></a>
584
+
585
+ #### `tools/change` — emit
586
+
587
+ A tool was registered or unregistered, or a scoped restriction changed (the available tool set changed — possibly for one scope only). An UNFILTERED registry-subject notification, deliberately not scope-filtered dispatch: a global change concerns every agent's next assembly, so a scoped listener subscribing here sees every change, not just its own scope's.
588
+
589
+ ```ts cordis-catalog
590
+ /**
591
+ * A tool was registered or unregistered, or a scoped restriction changed
592
+ * (the available tool set changed — possibly for one scope only). An
593
+ * UNFILTERED registry-subject notification, deliberately not scope-filtered
594
+ * dispatch: a global change concerns every agent's next assembly, so a
595
+ * scoped listener subscribing here sees every change, not just its own
596
+ * scope's.
597
+ * @mode emit
598
+ */
599
+ 'tools/change'(): void
600
+ ```
601
+
602
+ Source: [`packages/core/tools/src/index.ts:207`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/index.ts)
603
+
604
+ <a id="toolscode-dispatch-log--waterfall"></a>
605
+
606
+ #### `tools/code-dispatch-log` — waterfall
607
+
608
+ Allow a listener to replace content in the DURABLE LOG COPY of one `run_code` sub-dispatch outcome before the bridge appends its `tool/code-dispatch` event. `next()` keeps the content unchanged; a listener may return replacement blocks (e.g. the spill policy's preview + locator for an oversized text result). Only the logged copy is affected — the program already received the complete value, and the model sees neither. A throwing listener is contained: the bridge falls back to logging the original settled content. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): agent-scoped listeners receive only that agent's dispatches.
609
+
610
+ ```ts cordis-catalog
611
+ /**
612
+ * Allow a listener to replace content in the DURABLE LOG COPY of one
613
+ * `run_code` sub-dispatch outcome before the bridge appends its
614
+ * `tool/code-dispatch` event. `next()` keeps the
615
+ * content unchanged; a listener may return replacement blocks (e.g. the
616
+ * spill policy's preview + locator for an oversized text result). Only the
617
+ * logged copy is affected — the program already received the complete
618
+ * value, and the model sees neither. A throwing listener is contained:
619
+ * the bridge falls back to logging the original settled content.
620
+ * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): agent-scoped listeners receive only that agent's dispatches.
621
+ * @param dispatch - the parent execution, sub-call identity, and the settled content to log.
622
+ * @mode waterfall
623
+ */
624
+ 'tools/code-dispatch-log'(this: Scoped<ToolRuntime>, dispatch: CodeDispatchLog, next: () => Promise<ContentBlock[]>): Promise<ContentBlock[]>
625
+ ```
626
+
627
+ Types: [ContentBlock](./llm-streaming.md) · [Scoped](./scope.md)
628
+
629
+ Source: [`packages/core/tools/src/index.ts:189`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/index.ts)
630
+
631
+ <a id="toolsexecute--waterfall"></a>
632
+
633
+ #### `tools/execute` — waterfall
634
+
635
+ Around-dispatch waterfall for timeout, retry, or metrics. `next()` returns a normalized result; wrappers may change only `exec.signal`, while call identity remains immutable. The registry re-fuses the original caller signal before the body, so replacement cannot detach caller cancellation; wrappers must still restore their signal and reach quiescence. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): agent-scoped listeners receive only that agent's calls.
636
+
637
+ ```ts cordis-catalog
638
+ /**
639
+ * Around-dispatch waterfall for timeout, retry, or metrics. `next()` returns
640
+ * a normalized result; wrappers may change only `exec.signal`, while call
641
+ * identity remains immutable. The registry re-fuses the original caller
642
+ * signal before the body, so replacement cannot detach caller cancellation;
643
+ * wrappers must still restore their signal and reach quiescence.
644
+ * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): agent-scoped listeners receive only that agent's calls.
645
+ * @param exec - the allowed call about to dispatch (name, parsed arguments, caller agent, signal).
646
+ * @mode waterfall
647
+ */
648
+ 'tools/execute'(this: Scoped<ToolRuntime>, exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult>
649
+ ```
650
+
651
+ Types: [Scoped](./scope.md)
652
+
653
+ Source: [`packages/core/tools/src/index.ts:163`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/index.ts)
654
+
655
+ <a id="toolspost-execute--waterfall"></a>
656
+
657
+ #### `tools/post-execute` — waterfall
658
+
659
+ Accept, replace, enrich, or block a normalized dispatch result. `next()` accepts it unchanged; thrown tools still reach this waterfall as errors. Async listeners must observe `exec.signal`; after they settle, caller cancellation replaces only a successful accepted outcome with the code selected by whether the tool body was invoked. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): agent-scoped listeners receive only that agent's calls.
660
+
661
+ ```ts cordis-catalog
662
+ /**
663
+ * Accept, replace, enrich, or block a normalized dispatch result. `next()`
664
+ * accepts it unchanged; thrown tools still reach this waterfall as errors. Async
665
+ * listeners must observe `exec.signal`; after they settle, caller
666
+ * cancellation replaces only a successful accepted outcome with the code
667
+ * selected by whether the tool body was invoked.
668
+ * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): agent-scoped listeners receive only that agent's calls.
669
+ * @param exec - the call that just ran (name, parsed arguments, caller agent).
670
+ * @param result - the dispatch outcome a listener may accept, replace, or block.
671
+ * @mode waterfall
672
+ */
673
+ 'tools/post-execute'(this: Scoped<ToolRuntime>, exec: ToolExecution, result: Readonly<ToolExecutionResult>, next: () => Promise<PostToolDecision>): Promise<PostToolDecision>
674
+ ```
675
+
676
+ Types: [Scoped](./scope.md)
677
+
678
+ Source: [`packages/core/tools/src/index.ts:175`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/index.ts)
679
+
680
+ <a id="toolspre-execute--waterfall"></a>
681
+
682
+ #### `tools/pre-execute` — waterfall
683
+
684
+ Allow, deny, or ask before dispatch. `next()` delegates to allow; missing approval support turns `ask` into denial. Async gates must observe `exec.signal`; the registry rechecks cancellation after they settle but never abandons their promise. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): agent-scoped listeners receive only that agent's calls.
685
+
686
+ ```ts cordis-catalog
687
+ /**
688
+ * Allow, deny, or ask before dispatch. `next()` delegates to allow; missing
689
+ * approval support turns `ask` into denial. Async gates must observe
690
+ * `exec.signal`; the registry rechecks cancellation after they settle but
691
+ * never abandons their promise.
692
+ * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): agent-scoped listeners receive only that agent's calls.
693
+ * @param exec - the pending call (name, parsed arguments, caller agent).
694
+ * @mode waterfall
695
+ */
696
+ 'tools/pre-execute'(this: Scoped<ToolRuntime>, exec: ToolExecution, next: () => Promise<PreToolDecision>): Promise<PreToolDecision>
697
+ ```
698
+
699
+ Types: [Scoped](./scope.md)
700
+
701
+ Source: [`packages/core/tools/src/index.ts:152`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/index.ts)
702
+
703
+ <a id="toolsresult--emit"></a>
704
+
705
+ #### `tools/result` — emit
706
+
707
+ Observe the frozen, lossless-JSON final outcome. Listener failures are contained. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): keyed by `exec.agent`.
708
+
709
+ ```ts cordis-catalog
710
+ /**
711
+ * Observe the frozen, lossless-JSON final outcome. Listener failures are contained.
712
+ * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): keyed by `exec.agent`.
713
+ * @param exec - the execution object that traversed the pipeline.
714
+ * @param result - a deep-frozen snapshot of the final returned result.
715
+ * @mode emit
716
+ */
717
+ 'tools/result'(this: Scoped<ToolRuntime>, exec: Readonly<ToolExecution>, result: Readonly<ToolExecutionResult>): undefined
718
+ ```
719
+
720
+ Types: [Scoped](./scope.md)
721
+
722
+ Source: [`packages/core/tools/src/index.ts:197`](https://github.com/deepseek-ai/deepseek-harness/blob/master/packages/core/tools/src/index.ts)
723
+ <!-- END GENERATED cordis-surface -->