dsh-plugin-tool-management 0.21.1 → 0.21.3

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 (143) hide show
  1. package/CHANGELOG.md +111 -1
  2. package/README.md +43 -7
  3. package/README_EN.md +41 -7
  4. package/docs/{update.md → UPDATE.md} +38 -36
  5. package/docs/images/1-EN.png +0 -0
  6. package/docs/images/1.png +0 -0
  7. package/docs/images/2-EN.png +0 -0
  8. package/docs/images/2.png +0 -0
  9. package/docs/images/3-EN.png +0 -0
  10. package/docs/images/3.png +0 -0
  11. package/docs/images/4-EN.png +0 -0
  12. package/docs/images/4.png +0 -0
  13. package/docs/images/5-EN.png +0 -0
  14. package/docs/images/5.png +0 -0
  15. package/docs/images/6-EN.png +0 -0
  16. package/docs/images/6.png +0 -0
  17. package/docs/images/7-EN.png +0 -0
  18. package/docs/images/7.png +0 -0
  19. package/docs/images/8-EN.png +0 -0
  20. package/docs/images/8.png +0 -0
  21. package/lib/approval-policy.js +2 -2
  22. package/lib/audit-log.js +1 -1
  23. package/lib/boot/reconcile.js +157 -0
  24. package/lib/client.js +6668 -3145
  25. package/lib/compat/capability-specs.js +13 -17
  26. package/lib/compat/host-contracts.js +2 -0
  27. package/lib/compat/host-probe.js +35 -38
  28. package/lib/compat/host-roots.js +86 -15
  29. package/lib/compat/patch-dialect.js +14 -1
  30. package/lib/compat/preset-reach.js +75 -102
  31. package/lib/compat/probe.js +86 -131
  32. package/lib/compat/runtime-notes.js +1 -1
  33. package/lib/context-inject-contract.js +6 -5
  34. package/lib/context-inject-frame.js +124 -73
  35. package/lib/context-inject-select.js +66 -12
  36. package/lib/context-inject-surface.js +52 -4
  37. package/lib/context-inject.js +176 -39
  38. package/lib/desktop-cookie-prune.js +95 -0
  39. package/lib/diagnostics.js +79 -0
  40. package/lib/exports/transcript.js +4 -4
  41. package/lib/host-names.js +6 -6
  42. package/lib/http-api.js +73 -48
  43. package/lib/http-fence.js +44 -9
  44. package/lib/hub.js +12 -13
  45. package/lib/imports/parsers.js +25 -21
  46. package/lib/imports/unzip.js +48 -0
  47. package/lib/imports/upload.js +13 -37
  48. package/lib/index.js +3026 -279
  49. package/lib/mcp/disabled-tools.js +97 -0
  50. package/lib/mcp/loader-token.js +4 -4
  51. package/lib/mcp/manager.js +636 -226
  52. package/lib/mcp/override-blocks.js +1 -1
  53. package/lib/mcp/patch-yaml.js +42 -31
  54. package/lib/mcp/restart-marker.js +110 -0
  55. package/lib/mcp/secret-guard.js +6 -6
  56. package/lib/mcp/state-section.js +190 -60
  57. package/lib/mcp/tool-visibility.js +364 -73
  58. package/lib/memories/archive-engine.js +67 -42
  59. package/lib/memories/archive.js +23 -3
  60. package/lib/memories/constants.js +40 -127
  61. package/lib/memories/discovery.js +41 -12
  62. package/lib/memories/index-io.js +238 -33
  63. package/lib/memories/projection.js +36 -20
  64. package/lib/memories/prompt-layer.js +38 -0
  65. package/lib/memories/search.js +86 -0
  66. package/lib/memories/service.js +350 -111
  67. package/lib/memories/snapshot.js +83 -36
  68. package/lib/op-registry.js +73 -15
  69. package/lib/ops/candidates.js +14 -5
  70. package/lib/ops/compat.js +56 -13
  71. package/lib/ops/ctx.js +1 -1
  72. package/lib/ops/memory.js +89 -20
  73. package/lib/ops/prompts.js +57 -13
  74. package/lib/ops/quick-prompts.js +25 -2
  75. package/lib/ops/scene-records.js +29 -23
  76. package/lib/ops/scene-sync.js +30 -10
  77. package/lib/ops/scope.js +1094 -0
  78. package/lib/ops/sessions.js +15 -11
  79. package/lib/ops/snapshot.js +65 -29
  80. package/lib/ops/state-doctor.js +40 -5
  81. package/lib/ops/trash-retention.js +2 -1
  82. package/lib/ops/trash.js +6 -6
  83. package/lib/paths.js +50 -3
  84. package/lib/prompts/preset-id.js +5 -1
  85. package/lib/prompts/project-instructions.js +162 -0
  86. package/lib/prompts/quick-service.js +6 -4
  87. package/lib/prompts/service.js +30 -18
  88. package/lib/request-gate.js +32 -29
  89. package/lib/scene-linkage.js +17 -4
  90. package/lib/scene-prompt-sync.js +11 -10
  91. package/lib/scene-settings.js +1 -0
  92. package/lib/scenes/candidates.js +17 -11
  93. package/lib/scope/intent-store.js +106 -0
  94. package/lib/scope/intent.js +376 -0
  95. package/lib/scope/resolve.js +190 -0
  96. package/lib/scope/scope-overrides.js +894 -0
  97. package/lib/scope/store.js +263 -0
  98. package/lib/scope/workspace-key.js +64 -0
  99. package/lib/sessions/archive-schemas.js +9 -0
  100. package/lib/sessions/bridge.js +42 -56
  101. package/lib/sessions/history.js +9 -9
  102. package/lib/sessions/workspace.js +21 -23
  103. package/lib/settings-sidecars.js +51 -24
  104. package/lib/sidebar-settings.js +1 -0
  105. package/lib/skills/catalog.js +207 -35
  106. package/lib/skills/core-util.js +5 -0
  107. package/lib/skills/core.js +36 -23
  108. package/lib/skills/frontmatter.js +144 -11
  109. package/lib/skills/homes.js +2 -0
  110. package/lib/skills/import-pipeline.js +8 -1
  111. package/lib/skills/manager-state.js +30 -6
  112. package/lib/skills/official.js +14 -11
  113. package/lib/skills/readonly-discovery.js +1 -0
  114. package/lib/skills/service.js +157 -31
  115. package/lib/skills/skill-roots.js +25 -7
  116. package/lib/skills/skill-trash.js +14 -3
  117. package/lib/skills/system-trash.js +3 -1
  118. package/lib/skills/upload-pipeline.js +19 -3
  119. package/lib/slash-settings.js +1 -0
  120. package/lib/stale-write.js +82 -0
  121. package/lib/subagents/catalog.js +52 -18
  122. package/lib/subagents/child-preset.js +7 -10
  123. package/lib/subagents/persona-parse.js +35 -13
  124. package/lib/subagents/persona-write.js +12 -8
  125. package/lib/subagents/run-provider.js +13 -15
  126. package/lib/subagents/service.js +16 -14
  127. package/lib/subagents/tool-filter.js +1 -1
  128. package/lib/subagents/tools.js +113 -41
  129. package/lib/token-config.js +34 -14
  130. package/lib/tool-table-switch.js +142 -49
  131. package/lib/tools/deps.js +172 -6
  132. package/lib/tools/mcp.js +231 -54
  133. package/lib/tools/memory.js +139 -39
  134. package/lib/tools/prompt.js +81 -17
  135. package/lib/tools/remove.js +52 -26
  136. package/lib/tools/scene.js +139 -42
  137. package/lib/tools/skills.js +211 -51
  138. package/lib/tools/subagent.js +63 -32
  139. package/lib/tools/table.js +63 -25
  140. package/lib/trash-id.js +3 -2
  141. package/lib/trash-retention.js +7 -7
  142. package/lib/update-check.js +222 -0
  143. package/package.json +18 -15
@@ -1,3 +1,9 @@
1
+ // 人设文件的读侧与渲染:解析 frontmatter、规整名字、把中性骨架拼成某个语言的提示词,
2
+ // 以及工具结果/空结果的文案收口。
3
+ //
4
+ // 从 subagents/service.ts 整段搬来,一行未改。这一层不认识宿主也不写文件:进来一段文本,
5
+ // 出去一个 PersonaDoc;创建与更新走的解析口径与扫描时同一份,两处不能各写一套。
6
+ import { decodeYamlScalar } from '../skills/frontmatter.js';
1
7
  /** 逗号/顿号分隔的名字清单(空串 → undefined)。 */
2
8
  export function listOf(value) {
3
9
  const text = String(value ?? '').trim();
@@ -62,9 +68,11 @@ export function parsePresetToolRules(frontmatter) {
62
68
  * 变成一行无主文本。限长是防脏值(正常档位 id 都很短)。
63
69
  */
64
70
  export const REASONING_EFFORT_MAX_LENGTH = 64;
71
+ /** 归一化推理强度:去换行、trim、按上限截断。 */
65
72
  export function normalizeReasoningEffort(value) {
66
73
  return String(value ?? '').replace(/[\r\n]+/g, ' ').trim().slice(0, REASONING_EFFORT_MAX_LENGTH);
67
74
  }
75
+ /** 解析人设 `.md`:frontmatter 可选,缺名字时用文件名兜底。 */
68
76
  export function parsePersona(raw, fallbackName) {
69
77
  const m = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(raw);
70
78
  const data = {};
@@ -72,19 +80,34 @@ export function parsePersona(raw, fallbackName) {
72
80
  let body = raw;
73
81
  let toolsByPreset;
74
82
  if (m) {
83
+ // 新写入用 `output:` + YAML 序列;旧文件是重复的 `output: 一行`。两种都读。
84
+ let inOutputSequence = false;
75
85
  for (const line of m[1].split(/\r?\n/)) {
86
+ if (inOutputSequence) {
87
+ const item = /^\s*-\s?(.*)$/.exec(line);
88
+ if (item) {
89
+ const value = decodeYamlScalar(item[1]).trim();
90
+ if (value !== '')
91
+ outputLines.push(value);
92
+ continue;
93
+ }
94
+ if (line.trim() === '')
95
+ continue;
96
+ inOutputSequence = false;
97
+ }
76
98
  const kv = /^([A-Za-z_-]+)\s*:\s*(.*)$/.exec(line.trim());
77
99
  if (!kv)
78
100
  continue;
79
101
  const key = kv[1].toLowerCase();
80
- // `output:` 是**可重复**的键(一条要求一行,按顺序收集)。frontmatter 是逐行解析的,
81
- // 多行契约塞不进一个值里;写成重复键比引入块标量语法简单,也一眼看得懂。
82
102
  if (key === 'output') {
83
- if (kv[2].trim() !== '')
84
- outputLines.push(kv[2].trim());
103
+ const value = decodeYamlScalar(kv[2]).trim();
104
+ if (value === '')
105
+ inOutputSequence = true;
106
+ else
107
+ outputLines.push(value);
85
108
  continue;
86
109
  }
87
- data[key] = kv[2].trim();
110
+ data[key] = decodeYamlScalar(kv[2]).trim();
88
111
  }
89
112
  toolsByPreset = parsePresetToolRules(m[1]);
90
113
  body = raw.slice(m[0].length);
@@ -147,7 +170,7 @@ export function isChinesePersona(name, description, body) {
147
170
  return cjk * 2 >= latin;
148
171
  }
149
172
  /**
150
- * 人设 → 子代理系统提示词里那一段的**成品文本**(2026-09-17)。
173
+ * 人设 → 子代理系统提示词里那一段的**成品文本**。
151
174
  *
152
175
  * 为什么需要这层框:宿主给子代理的 persona 只占一个槽位 —— `deployment:persona-prefix`,
153
176
  * section order 0,位置紧贴 `harness:identity`(order -1000,那句 "You are an AI agent
@@ -164,7 +187,6 @@ export function isChinesePersona(name, description, body) {
164
187
  * 框里四样东西各有分工,缺一样就退化成原来的裸文本:
165
188
  * - `# 角色:名` —— 结构信号(markdown 标题是模型最强的分段线索)+ 可引用的名字;
166
189
  * - 归属 + 授权一行 —— "由调用方指定、与默认倾向冲突时以它为准",给正文上位效力;
167
- * 2026-09-17 补了半句"任务说要什么,怎么做以它为准":不写这句,模型会把 `task`
168
190
  * (父代理按自己的框架写的具体指令)读成人设的上级,人设只剩风格提示的分量;
169
191
  * - 边界一句 —— 角色决定**怎么做**,不改变**能做什么**。这不是装饰:官方
170
192
  * `subagent:delegation` 运行时上下文已经声明"权限在启动时固定",两处不呼应的话,
@@ -173,7 +195,7 @@ export function isChinesePersona(name, description, body) {
173
195
  *
174
196
  * 语言跟随人设整体(名字 + 描述 + 正文):中文人设配中文框,英文配英文。
175
197
  *
176
- * 框里**不列 `description`**(2026-09-17 用户裁定,此前列):那个字段的用途是**给调用方
198
+ * 框里**不列 `description`**:那个字段的用途是**给调用方
177
199
  * 选用**(本机那份写的是"当用户提到审查或使用审查子智能体时使用"),对已经在执行的人设
178
200
  * 子代理是错位信息 —— 可能被读成"满足某个条件才按这个角色"的前置条件。要给人设子代理
179
201
  * 看的简介属于正文(`description` 仍照常参与语言判定与目录展示)。
@@ -182,7 +204,7 @@ export function isChinesePersona(name, description, body) {
182
204
  */
183
205
  export function renderPersonaPrompt(persona) {
184
206
  // 名字与正文/输出过同一道中和:名字原样进框时,宿主对 section 文本的严格插值会把
185
- // `{{…}}` 当未注册变量抛错,委派直接硬失败 —— 0.9.5 的中和只盖了正文与输出,漏了名字
207
+ // `{{…}}` 当未注册变量抛错,委派直接硬失败 —— 中和只盖了正文与输出,漏了名字
186
208
  // (validPersonaName 不挡花括号,手写 frontmatter 造得出这种名字)。
187
209
  const name = neutralizePromptVariables(String(persona.name || '').trim()) || '(unnamed)';
188
210
  const raw = String(persona.body ?? '');
@@ -191,7 +213,7 @@ export function renderPersonaPrompt(persona) {
191
213
  return name;
192
214
  const rawDescription = String(persona.description ?? '').trim();
193
215
  const output = neutralizePromptVariables(String(persona.output ?? '').trim());
194
- // 身份定位句(0.19.0):此前只有"你正在以「X」的身份执行本次委派任务"一句,它说的是
216
+ // 身份定位句:此前只有"你正在以「X」的身份执行本次委派任务"一句,它说的是
195
217
  // **角色**,没说**身份**。而子会话是整份 join 父会话预设的(官方 `composeFrom`,无分项
196
218
  // 开关),于是 `~/.dsh/AGENTS.md` 那份"写给主代理的操作手册"也进了它的上下文 —— 通篇是
197
219
  // "最新用户消息""向用户确认""开分支""等 review"。子代理据此认定自己是主代理。
@@ -228,7 +250,7 @@ export function renderPersonaPrompt(persona) {
228
250
  /** 一个人设没写 `catalogDepth` 时的目录注入深度:1 —— 只在顶层注入目录。 */
229
251
  export const DEFAULT_PERSONA_CATALOG_DEPTH = 1;
230
252
  /**
231
- * 允许追问人设的标记(0.19.0),由人设目录与 `subagent_manager_list` 两处共用。
253
+ * 允许追问人设的标记,由人设目录与 `subagent_manager_list` 两处共用。
232
254
  *
233
255
  * 为什么标在目录行上:`continuable` 改变的是**委派的返回值形态**(回执一个 id 而不是产出),
234
256
  * 模型必须在这一刻就知道 —— 否则它会按"等下会拿到结果"来安排后续动作,然后拿到一个 id
@@ -266,7 +288,6 @@ export function catalogDepthOf(persona) {
266
288
  * 域该不该注入、实况报什么状态,全都问它。各写一份必然分叉。
267
289
  *
268
290
  * ⚠️ 它回答的**不是**"能不能委派"(那由官方决定,且默认能嵌套到 3 层)。这个区分是
269
- * 2026-09-17 用户实测后纠正的:此前函数名是 `canDelegateTo`,把"目录可见性"当成了
270
291
  * "委派可行性",于是同一个子会话里官方工具能委派、我们的工具被自己的守卫拦下。
271
292
  */
272
293
  export function catalogInjectedAt(persona, depth) {
@@ -275,7 +296,7 @@ export function catalogInjectedAt(persona, depth) {
275
296
  }
276
297
  /** frontmatter 里目录注入深度的几种写法都认(含旧的 `maxDepth`,见下)。 */
277
298
  export function parseCatalogDepth(data) {
278
- // 旧键 `maxDepth` 也读:0.9.5 定稿前这个字段叫过那个名字,且当时语义是"递归上限"。
299
+ // 旧键 `maxDepth` 也读:这个字段早先叫过那个名字,且当时语义是"递归上限"。
279
300
  // 读它只是为了不让手写过旧键的文件静默失效;**写回时一律写新键**(见 serializePersona)。
280
301
  const raw = data.catalogdepth ?? data['catalog-depth'] ?? data.catalog_depth ?? data.maxdepth ?? data['max-depth'] ?? data.max_depth;
281
302
  if (raw === undefined || raw.trim() === '')
@@ -285,6 +306,7 @@ export function parseCatalogDepth(data) {
285
306
  return undefined;
286
307
  return value;
287
308
  }
309
+ /** 子智能体结果回传的截断上限 16 KiB:它是进父会话上下文的正文,不是日志。 */
288
310
  export const RESULT_MAX = 16 * 1024;
289
311
  /**
290
312
  * 从内容块数组里按 `type` 取文本并拼接 —— 官方 `finalText` 的口径
@@ -3,6 +3,7 @@
3
3
  // 从 subagents/service.ts 整段搬来,一行未改。只写用户填过的键这条纪律留在函数自己的
4
4
  // 注释里;名字谓词仍收敛到 ../paths.ts,这里不另写一份。
5
5
  import { isValidSegment } from '../paths.js';
6
+ import { yamlScalar } from '../skills/frontmatter.js';
6
7
  import { DEFAULT_PERSONA_CATALOG_DEPTH, normalizeReasoningEffort, truthyFlag } from './persona-parse.js';
7
8
  /** 人设名长度上限(字符)。 */
8
9
  export const PERSONA_NAME_MAX = 64;
@@ -33,7 +34,7 @@ export function serializePersona(args) {
33
34
  const byPreset = presetRulesOf(args?.toolsByPreset);
34
35
  // 目录注入深度:只在显式给了合法值时写入。默认(1)**不落盘** —— 免得每个新建的人设都
35
36
  // 多一行说明"它和默认一样",也保证老的人设文件回写后逐字节不变。
36
- // 入参名兼容三种:catalogDepth(新)、maxDepth / max_depth(0.9.5 定稿前的旧名)。
37
+ // 入参名兼容三种:catalogDepth(新)、maxDepth / max_depth(旧名)。
37
38
  const catalogDepthRaw = args?.catalogDepth ?? args?.catalog_depth ?? args?.maxDepth ?? args?.max_depth;
38
39
  const catalogDepth = catalogDepthRaw === undefined || catalogDepthRaw === null || String(catalogDepthRaw).trim() === ''
39
40
  ? undefined
@@ -44,25 +45,28 @@ export function serializePersona(args) {
44
45
  // 说明"它和默认一样",也保证老的人设文件回写后逐字节不变。入参兼容布尔与字符串两种形态
45
46
  // (界面传布尔,手写/导入的 frontmatter 走解析侧已归一成布尔)。
46
47
  const continuable = args?.continuable === true || truthyFlag(String(args?.continuable ?? ''));
47
- // `output` 可传字符串(按行拆)或数组(界面直接给数组):一条要求写成一行重复键。
48
+ // `output` 可传字符串(按行拆)或数组(界面直接给数组):一条要求写成序列的一项。
48
49
  const outputLines = (Array.isArray(args?.output) ? args.output.map((v) => String(v)) : String(args?.output ?? '').split(/\r?\n/))
49
50
  .map((line) => line.trim())
50
51
  .filter((line) => line !== '');
51
52
  const lines = ['---'];
52
53
  if (description)
53
- lines.push('description: ' + description);
54
+ lines.push('description: ' + yamlScalar(description));
54
55
  if (provider)
55
- lines.push('provider: ' + provider);
56
+ lines.push('provider: ' + yamlScalar(provider));
56
57
  if (model)
57
- lines.push('model: ' + model);
58
+ lines.push('model: ' + yamlScalar(model));
58
59
  if (reasoningEffort)
59
- lines.push('reasoningEffort: ' + reasoningEffort);
60
+ lines.push('reasoningEffort: ' + yamlScalar(reasoningEffort));
60
61
  if (hasCatalogDepth)
61
62
  lines.push('catalogDepth: ' + String(catalogDepth));
62
63
  if (continuable)
63
64
  lines.push('continuable: true');
64
- for (const line of outputLines)
65
- lines.push('output: ' + line);
65
+ if (outputLines.length) {
66
+ lines.push('output:');
67
+ for (const line of outputLines)
68
+ lines.push(' - ' + yamlScalar(line));
69
+ }
66
70
  if (tools.length)
67
71
  lines.push('tools: ' + tools.join(', '));
68
72
  if (toolsDeny.length)
@@ -16,6 +16,7 @@ import { childPresetDefinition } from './child-preset.js';
16
16
  export { catalogInjectedAt, catalogDepthOf, DEFAULT_PERSONA_CATALOG_DEPTH, emptyResultNote, neutralizePromptVariables, normalizeReasoningEffort, parsePersona, parsePresetToolRules, renderPersonaPrompt, REASONING_EFFORT_MAX_LENGTH, textOfBlocks, UNLIMITED_PERSONA_CATALOG_DEPTH, } from './persona-parse.js';
17
17
  export { presetRulesOf, serializePersona, validPersonaName } from './persona-write.js';
18
18
  export { decideToolFilter } from './tool-filter.js';
19
+ /** 子智能体运行侧:按 `spawn` / `fork` 挂 provider,并负责一次委派的人设与工具限制。 */
19
20
  export function createRunProvider(deps) {
20
21
  const PROVIDER_PACKAGES = {
21
22
  spawn: { pkg: '@deepseek-ai/dsh-subagent-spawn-in-process', label: 'spawn (fresh session)' },
@@ -68,16 +69,14 @@ export function createRunProvider(deps) {
68
69
  return runtime;
69
70
  }
70
71
  /**
71
- * Install the persona's optional tool mask on one exact child scope. The
72
- * official request no longer accepts toolFilter, so this is the only place
73
- * the mask can be applied without widening the child's inherited tools.
72
+ * 把某个人设的可选工具掩码装到**恰好那一个**子作用域上。
74
73
  *
75
- * Must run inside `agent/created` for that exact child (0.2.1):the official
76
- * factory awaits every serial `agent/created` listener **before** it releases
77
- * the queued first prompt (`dsh-agent/lib/types/index.js:334` → `announce`),
78
- * so a restriction installed here is already in force at the child's first
79
- * tool call. Installing after `startActivation()` resolves would race: the
80
- * first turn could already have run with the inherited tools.
74
+ * 官方请求已不接受 toolFilter,所以这是唯一能在不放宽子会话继承工具的前提下装掩码的地方。
75
+ * 必须在那个子会话的 `agent/created` 里装:官方工厂会先 await 所有串行
76
+ * `agent/created` 监听器,**之后**才放行排队的第一条提示词
77
+ *(`dsh-agent/lib/types/index.js:334` → `announce`),所以在这里装上的限制在子会话第一次
78
+ * 调工具时已经生效。等到 `startActivation()` resolve 之后再装就有竞态:第一轮可能已经
79
+ * 带着继承来的工具跑完了。
81
80
  */
82
81
  function restrictChildTools(childAgent, filter) {
83
82
  const tools = childAgent?.ctx?.get?.('tools');
@@ -175,10 +174,10 @@ export function createRunProvider(deps) {
175
174
  // 这里**刻意没有**"预算用尽就拒绝"的前置检查:宿主统一决定委派深度
176
175
  // (当前默认 maxDepth=1),本工具不传 maxDepth,也不自行判断能否继续委派。
177
176
  //
178
- // 通道选择(方案 C,2026-09-17):`inherit` → 官方 fork provider(子会话被**本次会话已完成的
177
+ // 通道选择:`inherit` → 官方 fork provider(子会话被**本次会话已完成的
179
178
  // 对话**播种,`task` 只需写新增部分);默认 → spawn(全新会话,任务必须自包含)。
180
- // 为什么必须有这一档:官方的 `subagent_fork` 靠"继承上下文"赢走过模型的选择 —— 本机实测
181
- // (session-ee722e23)上下文里明明有「先在这里选人设…用 subagent_manager_run 执行」与
179
+ // 为什么必须有这一档:官方的 `subagent_fork` 靠"继承上下文"赢走过模型的选择 —— 实测中
180
+ // 上下文里明明有「先在这里选人设…用 subagent_manager_run 执行」与
182
181
  // `code-review` 人设,模型在**读进 README 之后**仍选了 `subagent_fork`:那能把已读内容带过去、
183
182
  // 不必复述。人设通道缺这一档时,"贴合人设的任务"会持续被官方工具抢走 —— 补齐能力比改文案
184
183
  // 更根本(模型选的是省力,不是没看见)。
@@ -193,7 +192,6 @@ export function createRunProvider(deps) {
193
192
  // provider 与 model 是模型路由的两半:DSH 的 resolveModel(provider, model) 不做
194
193
  // `provider/model` 字符串拆分,只改 model 会落在**主会话的 provider** 上——跨来源
195
194
  // 指定模型(如 sensenova 的 sensenova-6.8-flash-lite)必须两个键一起给。
196
- // 2026-09-23 加 `reasoningEffort`:条件是"三个键任一有值"—— 只设强度(不改模型)也得
197
195
  // 把这个对象发出去,否则那一格永远不生效。官方合并语义(`dsh-subagent/lib/index.js:471-483`)
198
196
  // 是"先继承主会话的三项、再用这里的覆盖",所以只给 reasoningEffort 时 provider/model
199
197
  // 照旧继承;反过来,改了 provider/model 而没给 reasoningEffort 时,**继承来的那一档会被
@@ -332,8 +330,8 @@ export function createRunProvider(deps) {
332
330
  await releasePreset?.().catch(() => undefined);
333
331
  }
334
332
  }
335
- // v1 串行:同一时刻至多一次**启动**在途(设计 §3.2)。
336
- // ⚠️ 0.19.0 起这条不再等于"至多一个子代理在跑":允许追问(`continuable`)的子代理在
333
+ // v1 串行:同一时刻至多一次**启动**在途。
334
+ // ⚠️这条不再等于"至多一个子代理在跑":允许追问(`continuable`)的子代理在
337
335
  // `startActivation()` 返回后仍在后台运行,而这里只串行化"启动"这一步。这是官方通道的
338
336
  // 固有语义(后台子级本来就允许并存),不是本插件放宽了限制。
339
337
  // `continuable: true` 的返回值是 `{ text: '', stopReason: 'started' }` —— 产出不在这里,
@@ -1,7 +1,7 @@
1
- // 人设运行侧(provider 挂载 + 一次委派),2026-10-01 从本文件的工厂体剥出。
1
+ // 人设运行侧(provider 挂载 + 一次委派),从本文件的工厂体剥出。
2
2
  import { createRunProvider } from "./run-provider.js";
3
3
  // src/subagents/service.ts —— 轻量子智能体:人设发现(TTL 扫描)+ 官方 ctx.subagents.start 薄封装。
4
- // 设计 §3:人设 = $DSH_HOME/tool-management/agents/<name>.md(frontmatter 可选,缺省派生);v1 串行运行;
4
+ // 人设 = $DSH_HOME/tool-management/agents/<name>.md(frontmatter 可选,缺省派生);串行运行;
5
5
  // provider 缺失时经 createRequire 挂载宿主侧包:`spawn`(新会话,默认)与 `fork`(继承本次会话)。
6
6
  //
7
7
  // v0.4 目录变更:人设由 `$DSH_HOME/subagents/` 搬到 `$DSH_HOME/tool-management/agents/`
@@ -9,6 +9,7 @@ import { createRunProvider } from "./run-provider.js";
9
9
  import { createRequire } from 'node:module';
10
10
  import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
11
11
  import { join } from 'node:path';
12
+ import { writeFileAtomically } from '../memories/index-io.js';
12
13
  import { resolveDshHome } from '../skills/core.js';
13
14
  import { listTrashEntries, moveOutOfTrash, moveToTrash, purgeTrashEntry, readTrashEntry } from '../hub.js';
14
15
  import { expandUploads, planPersonaImport } from '../imports/upload.js';
@@ -61,6 +62,7 @@ async function relocateLegacyPersonas(dir) {
61
62
  }
62
63
  }
63
64
  }
65
+ /** 人设域服务门面:人设发现(TTL 扫描)与委派入口。 */
64
66
  export function createSubagentService(ctx, opts) {
65
67
  const dir = opts?.subagentsDir || defaultPersonasDir();
66
68
  const stateDir = opts?.stateDir && opts.stateDir.trim() !== '' ? opts.stateDir : join(resolveDshHome(), 'tool-management');
@@ -142,12 +144,12 @@ export function createSubagentService(ctx, opts) {
142
144
  }
143
145
  async function writeEnabled(list) {
144
146
  await mkdir(stateDir, { recursive: true });
145
- await writeFile(stateFile, JSON.stringify({ version: 1, enabled: list }, null, 2), 'utf8');
147
+ await writeFileAtomically(stateFile, JSON.stringify({ version: 1, enabled: list }, null, 2));
146
148
  enabledCache = list;
147
149
  // 写入后重取指纹,让下一次 readEnabled 直接命中缓存(否则会白读一次)。
148
150
  enabledStamp = await fileStamp(stateFile);
149
151
  }
150
- /** 新建/导入/恢复的人设默认停用(v0.8.5 用户裁定,与技能 / MCP 同口径):
152
+ /** 新建/导入/恢复的人设默认停用(与技能 / MCP 同口径):
151
153
  * 文件缺失(含旧数据)时先把「全部启用」物化成显式全集**并排除新名**——
152
154
  * 否则新名会随"文件缺失=全启用"的兼容语义被误判为启用。已有显式集合时新名
153
155
  * 不在集合里,天然停用,无需写盘。 */
@@ -231,7 +233,7 @@ export function createSubagentService(ctx, opts) {
231
233
  const fresh = [];
232
234
  try {
233
235
  const entries = await readdir(dir);
234
- // 只列**能管得动**的人设(2026-09-30 审查 P2-18):`validPersonaName` 走
236
+ // 只列**能管得动**的人设:`validPersonaName` 走
235
237
  // `paths.ts` 的 `isValidSegment`,隐藏名(`.foo`)被判 `hidden` —— 于是此前列表里
236
238
  // 会出现一个「看得见、改不了、删不掉」的人设(任何 op 都拒它)。同一条谓词在这里
237
239
  // 收口;大小写也按 `relocateLegacyPersonas` 的 `toLowerCase()` 口径统一(`A.MD` 在
@@ -249,14 +251,14 @@ export function createSubagentService(ctx, opts) {
249
251
  }
250
252
  return withEnabled(docs, await readEnabled());
251
253
  }
252
- // provider 在场性(设计 §3.2,落地方式见 cordis.patch.yml 的取舍注释):
254
+ // provider 在场性(落地方式见 cordis.patch.yml 的取舍注释):
253
255
  // 官方通道探测(ctx.subagents.list())→ 缺失才挂载 → 失败把原因原样带出(不吞、不谎报)。
254
256
  // 不是一次性静默标记:每次调用都先探测,宿主后来注册了 provider 也能立刻跟上。
255
257
  //
256
- // 两个 provider(2026-09-17 加 fork,理由见 runOnce 的通道说明):
258
+ // 两个 provider(加 fork,理由见 runOnce 的通道说明):
257
259
  // spawn = 新起的独立会话(默认);fork = **继承本次会话已完成的对话**(官方 subagent_fork 用的是它)。
258
- // ---------- 运行侧:provider 挂载与一次委派(0.16.x 设计 §3.2) ----------
259
- // 正文 2026-10-01 整段搬到 ./run-provider.js(一字未改,只把 ctx / req / message 三个
260
+ // ---------- 运行侧:provider 挂载与一次委派 ----------
261
+ // 正文 整段搬到 ./run-provider.js(一字未改,只把 ctx / req / message 三个
260
262
  // 闭包名字改走 deps)。`req` 由**这里**造好再传过去:provider 包是按 service.js 自己的
261
263
  // 位置解析的,把 createRequire 那行搬走等于换解析基准。
262
264
  const { runSerial } = createRunProvider({ ctx, req, message });
@@ -299,8 +301,8 @@ export function createSubagentService(ctx, opts) {
299
301
  catch (e) {
300
302
  return { ok: false, error: `创建人设目录失败: ${dir}(${message(e)})` };
301
303
  }
302
- await writeFile(target, serializePersona(args), 'utf8');
303
- // v0.8.5:新建默认不启动——显式集合下新名天然停用;文件缺失时先物化全集并排除新名。
304
+ await writeFileAtomically(target, serializePersona(args));
305
+ // 新建默认不启动 —— 显式集合下新名天然停用;文件缺失时先物化全集并排除新名。
304
306
  cache = null;
305
307
  return withEnabledNote({ ok: true, name }, () => materializeEnabledExcluding([name]));
306
308
  },
@@ -380,7 +382,7 @@ export function createSubagentService(ctx, opts) {
380
382
  if (!validPersonaName(entry.name))
381
383
  return { ok: false, error: `回收站里的人设名不合法: ${entry.name}` };
382
384
  /*
383
- * 同名先问一句(用户 2026-10-14):撞名时**默认仍拒绝**(绝不覆盖),但把候选名字一起
385
+ * 同名先问一句(用户要求):撞名时**默认仍拒绝**(绝不覆盖),但把候选名字一起
384
386
  * 回(`params.next`),界面弹窗才能如实写出"确认后会叫 `写作助手(1)`";用户确认后带
385
387
  * `dedupe: true` 重来,这一次真的按退号的名字落盘。
386
388
  *
@@ -411,7 +413,7 @@ export function createSubagentService(ctx, opts) {
411
413
  return { ok: false, error: `恢复失败: ${message(e)}` };
412
414
  }
413
415
  await purgeTrashEntry('subagents', id);
414
- // v0.8.5:回收站恢复默认不启动(与新建/导入同口径)。
416
+ // 回收站恢复默认不启动(与新建 / 导入同口径)。
415
417
  // 启停名单必须按**最终落盘的名字**写:退号恢复时它不是回收站里的原名。
416
418
  cache = null;
417
419
  return withEnabledNote({ ok: true, name, renamed: name !== entry.name }, () => materializeEnabledExcluding([name]));
@@ -468,7 +470,7 @@ export function createSubagentService(ctx, opts) {
468
470
  await materializeEnabledExcluding(imported); });
469
471
  },
470
472
  /**
471
- * 子智能体开关(v0.8):停用 = 不注入目录段、subagent_manager_list/run 不可见;文件本体不动。
473
+ * 子智能体开关:停用 = 不注入目录段、`subagent_manager_list` / `run` 不可见;文件本体不动。
472
474
  * `enabled` 必须显式给布尔值 —— 与 rules-toggle 同一条口径:不按"翻转"推断,
473
475
  * 免得参数丢了时界面以为改了、实际什么都没动。
474
476
  */
@@ -32,7 +32,7 @@ export function stripReserved(names) {
32
32
  *
33
33
  * 所以:
34
34
  * - 当前预设配了名单(且名单非空)→ 用它;白名单额外并入**当时真实在跑的 MCP 工具**
35
- * (官方 allow 是"清单之外全砍",不并进来会把 MCP 一起砍掉;用户裁定:子代理要能
35
+ * (官方 allow 是"清单之外全砍",不并进来会把 MCP 一起砍掉;用户要求:子代理要能
36
36
  * 用当前启动的 MCP);
37
37
  * - 当前预设没配 → 回落旧的全局 `tools` / `toolsDeny`(老文件行为不变);
38
38
  * - 名单里有已经消失的工具名、或写了保留名 → **丢掉并在 note 里如实说明**(不接受静默失效);
@@ -1,11 +1,9 @@
1
1
  // src/subagents/tools.ts —— 模型工具:subagent_manager_list / subagent_manager_run(人设清单 + 官方 ctx.subagents 委派)。
2
2
  // 本文件返回的是 **author 侧 tool 定义**(parameters 是 property-spec 形态),装配处必须经
3
- // defineTool() 编译后再 register(C4:裸 register 会把未编译的参数声明直接发给模型 API)。
3
+ // defineTool() 编译后再 register —— 裸 register 会把未编译的参数声明直接发给模型 API。
4
4
  // 参数与 output schema 用 as const 保留字面量类型,defineTool 才能推断出参数表。
5
5
  import { CONTINUABLE_MARK } from './service.js';
6
- // 深度探针(`subagentDepthOf`)与目录判据(`catalogInjectedAt`)都不再需要:
7
- // 2026-09-17 方案 A 之后,这两个工具都不再看会话深度 —— 委派由官方决定(默认能嵌套到 3 层),
8
- // `catalogDepth` 只管常驻目录注入到哪些会话。
6
+ /** 把一段文本包成工具回执要的内容块数组(各工具域共用这一份形状)。 */
9
7
  export function text(v) {
10
8
  return [{ type: 'text', text: v }];
11
9
  }
@@ -13,17 +11,17 @@ export function text(v) {
13
11
  * 可见性过滤(两道闸,按序):
14
12
  * ① 人设开关(全局):停用的绝不进上下文 —— 即便它被场景绑定(场景启动时会自动把
15
13
  * 绑定的人设启用,见 archive-engine 的 applySubagentSwitches)。
16
- * ② 场景绑定校验(设计 §3.3):启用场景的 subagents 并集;空并集 = 全部可用。
14
+ * ② 场景绑定校验:启用场景的 subagents 并集;空并集 = 全部可用。
17
15
  * 之前只有②:全局默认所有子智能体都进上下文,没有开关可言。
18
16
  *
19
- * `includeUnavailable`(2026-10-14 对抗性审查 P1-7)**只给 `subagent_manager_list` 的
17
+ * `includeUnavailable`**只给 `subagent_manager_list` 的
20
18
  * `all=true` 用**,两道闸一起放开:停用的人设此前**在任何地方都查不到** —— 注入目录只列可用的,
21
19
  * `subagent_manager_switch` 又默认关着,于是模型既发现不了它、也就无从提示用户去启用。
22
20
  * 另外三个 `*_list`(MCP / 技能 / 记忆)早就有 `all=true` 这个出口,人设是缺的那一个。
23
21
  * 注意放开的只是**可见性**:`subagent_manager_run` 仍然按完整两道闸判"能不能委派",
24
22
  * 列出来 ≠ 可委派(回执里那句「人设不可用」照旧成立)。
25
23
  */
26
- export async function filterBySceneBinding(docs, enabledSceneLists, includeUnavailable = false) {
24
+ export function filterBySceneBinding(docs, enabledSceneLists, includeUnavailable = false) {
27
25
  const live = includeUnavailable ? docs : docs.filter((d) => d.enabled !== false);
28
26
  const bound = new Set(enabledSceneLists.flat());
29
27
  if (includeUnavailable || !bound.size)
@@ -31,12 +29,58 @@ export async function filterBySceneBinding(docs, enabledSceneLists, includeUnava
31
29
  const allowed = live.filter((d) => bound.has(d.name));
32
30
  return { allowed, reason: allowed.length ? null : `the enabled scene's subagent bindings: ${[...bound].join(', ')}` };
33
31
  }
32
+ /**
33
+ * 本会话这一格怎么说:四层并值(覆盖 → 场景层 → 基线)优先;层里没话说时按
34
+ * **全局口径**判(场景绑定 ∩ 全局启停)。
35
+ *
36
+ * 与旧版的关键差别:那时的会话覆盖只能"关"(场景绑定先过滤、覆盖再过滤
37
+ * 两者叠加),现在**能救回**场景绑定的排除项 —— 进入场景之后还想开什么就开什么。
38
+ */
39
+ function decideInSession(persona, flag, boundSet) {
40
+ const f = flag ? flag(persona.name) : undefined;
41
+ if (typeof f === 'boolean')
42
+ return f;
43
+ if (persona.enabled === false)
44
+ return false;
45
+ return boundSet.size === 0 || boundSet.has(persona.name);
46
+ }
47
+ /** 从 exec 取本会话的某一格解析(解析失败 = 跟随全局,绝不把异常抛给模型)。 */
48
+ function flagOfExec(resolve, exec) {
49
+ if (resolve === undefined)
50
+ return undefined;
51
+ try {
52
+ return resolve(exec);
53
+ }
54
+ catch {
55
+ return undefined;
56
+ }
57
+ }
58
+ /** 从 exec 取本会话的人设**原始覆盖表**(只用于"本会话显式关了几条"那句统计)。 */
59
+ function overridesOfExec(resolve, exec) {
60
+ if (resolve === undefined)
61
+ return undefined;
62
+ try {
63
+ return resolve(exec);
64
+ }
65
+ catch {
66
+ return undefined;
67
+ }
68
+ }
69
+ /**
70
+ * `subagent_manager_list` 的工具定义:列出人设目录。
71
+ *
72
+ * 与注入进上下文的「Current subagents」提醒是同一份目录的两种视图 —— 那条提醒只带当前可委派
73
+ * 的那部分,这条工具负责给出全量(`all=true` 时含停用与场景外的),所以两边都必须能被模型找到。
74
+ *
75
+ * @param subagents 宿主侧注入的人设清单、场景绑定与会话层实现
76
+ * @returns 可直接交给工具注册器的工具对象
77
+ */
34
78
  export function defineSubagentManagerListTool(subagents) {
35
79
  return {
36
80
  name: 'subagent_manager_list',
37
- // `depth-filtered` 删掉(2026-10-15 注入文本审查):注入深度是**注入侧**的实现细节,
81
+ // `depth-filtered` 删掉:注入深度是**注入侧**的实现细节
38
82
  // 模型看到的是"目录里有哪些人设",`catalogDepth` 那个词对它没有任何可操作性。
39
- description: 'List personas (pre-configured subagent profiles) with their descriptions. The "Current subagents" reminder carries a view of the same catalog; call this for the always-current full list, or pass all=true to include personas that are disabled or outside the active scene.',
83
+ description: 'List personas (pre-configured subagent profiles) with their descriptions. The "Current subagents" reminder carries a view of the same catalog; call this for the always-current full list, or all=true to include disabled personas and ones outside the active scene.',
40
84
  parameters: {
41
85
  all: { type: 'boolean', description: 'Include disabled personas and ones outside the active scene (default false). Listed ≠ delegable: `run` still refuses those.' },
42
86
  },
@@ -47,8 +91,19 @@ export function defineSubagentManagerListTool(subagents) {
47
91
  async execute(args, exec) {
48
92
  const includeUnavailable = Boolean(args?.all === true);
49
93
  const docs = await subagents.list();
50
- const { allowed, reason } = await filterBySceneBinding(docs, await subagents.sceneLists(), includeUnavailable);
51
- // 这里**不再按深度过滤**(2026-09-17 用户裁定,方案 A):`catalogDepth` 只决定常驻目录
94
+ const sceneLists = await subagents.sceneLists(exec);
95
+ const bound = filterBySceneBinding(docs, sceneLists, includeUnavailable);
96
+ // 会话层:四层并值优先(本会话显式开的**能救回**场景绑定之外的人设)
97
+ // 层里没话说时按全局口径(场景绑定 ∩ 全局启停)判。`all=true` 放开可见性,
98
+ // 但仍逐条标出"现在不可委派"。
99
+ const flag = flagOfExec(subagents.flagFor, exec);
100
+ const overrides = overridesOfExec(subagents.overridesFor, exec);
101
+ const boundSet = new Set(sceneLists.flat());
102
+ const allowed = includeUnavailable
103
+ ? docs
104
+ : docs.filter((p) => decideInSession(p, flag, boundSet));
105
+ const reason = allowed.length === 0 ? bound.reason : null;
106
+ // 这里**不再按深度过滤**:`catalogDepth` 只决定常驻目录
52
107
  // 注入到哪些会话,与"能不能委派"无关 —— 委派由官方决定(`dsh-tool-subagent` 默认能嵌套
53
108
  // 到 3 层)。此前按深度过滤等于把可用人设藏起来:目录不注入时模型只能靠这个工具查,
54
109
  // 而工具又不列全,结果是"明明能委派却查不到人设"。
@@ -56,7 +111,7 @@ export function defineSubagentManagerListTool(subagents) {
56
111
  // (与技能列表标 `shadowed-by:` 同一条理由 —— 列出来不等于能用)。
57
112
  const lines = allowed.map((p) => '- ' + p.name + ' — ' + (p.description || '(no description)') +
58
113
  (p.continuable === true ? CONTINUABLE_MARK : '') +
59
- (includeUnavailable ? (p.enabled === false ? ' (disabled — turn it on first)' : '') : ''));
114
+ (includeUnavailable ? (!decideInSession(p, flag, boundSet) ? ' (disabled — turn it on first)' : '') : ''));
60
115
  // 默认口径下有多少个没列出来(停用的 + 不在启用场景里的):不报的话,模型看到一份短名单
61
116
  // 会当成"本机只有这些人设"。数量照报,与另外三个 list 的表头同一口径。
62
117
  const hiddenCount = includeUnavailable ? 0 : docs.length - allowed.length;
@@ -67,50 +122,55 @@ export function defineSubagentManagerListTool(subagents) {
67
122
  catch {
68
123
  notice = '';
69
124
  }
125
+ // 会话层(`subagent_manager_list` 按会话过滤):本会话关掉的人设
126
+ // 与全局停用**同形**(默认不列、all=true 标 disabled),但换个对话就回来了 ——
127
+ // 不说清的话,模型会把这份短名单当成「本机只有这些人设」。
128
+ const offHere = overrides === undefined
129
+ ? 0
130
+ : Object.keys(overrides).filter((name) => {
131
+ const doc = docs.find((d) => d.name === name);
132
+ return overrides[name] === false && doc !== undefined && doc.enabled !== false;
133
+ }).length;
70
134
  const header = 'persona subagents (' + allowed.length + ')' +
71
- (hiddenCount > 0 ? ' (' + hiddenCount + ' more are disabled or outside the current scene; pass all=true to see them)' : '') + ':\n';
135
+ (hiddenCount > 0 ? ' (' + hiddenCount + ' more are disabled or outside the current scene; pass all=true to see them)' : '') +
136
+ (offHere > 0 ? ' (' + offHere + ' switched off here only)' : '') + ':\n';
72
137
  return header + (lines.join('\n') || '(none)') + (reason ? '\nnote: ' + reason : '') + notice;
73
138
  },
74
139
  };
75
140
  }
141
+ /**
142
+ * `subagent_manager_run` 的工具定义:把一个人设当子智能体跑一次。
143
+ *
144
+ * @param subagents 宿主侧注入的人设清单、场景绑定与工具限制实现
145
+ * @returns 可直接交给工具注册器的工具对象
146
+ */
76
147
  export function defineSubagentManagerRunTool(subagents) {
77
148
  return {
78
149
  name: 'subagent_manager_run',
79
150
  // 描述按「是什么 → 两种模式 → 何时用(含与官方两个委派工具的分界)→ 何时改用别的」组织。
80
- // 2026-09-17 补了**分界规则**与 `inherit`:此前上下文里虽然注入了人设目录与「用
81
- // subagent_manager_run 执行」,但没有任何一句话说明它和官方 `subagent` / `subagent_fork`
82
- // 何时该用谁 —— 本机实测(session-ee722e23)模型读完 README 后选了官方 `subagent_fork`
83
- // (fork 能继承已读内容、不必复述)。现在人设通道也有 fork(`inherit`),分界只剩
84
- // 「要不要后台跑」一件事,所以规则能写成"贴合人设的一律走这里"。
85
151
  //
86
- // 「何时不用」那一段保留(2026-09-17,用户采纳的四条里的第 6 条):参照 Claude Code 的
87
- // Agent 工具(`AgentTool/prompt.ts:232-240`),把"不该用"写成**带替代工具**的具体清单
88
- // (具体路径→Read;找定义→Grep/Glob),比笼统说"这个很贵"有用得多。
152
+ // 与官方 `subagent` / `subagent_fork` 的分界必须写明:上下文里只有「先在这里选人设…
153
+ // 用 subagent_manager_run 执行」这一句时,模型读完 README 仍会选 `subagent_fork`
154
+ // (它能继承已读内容、不必复述)。人设通道也有 fork(`inherit`)之后,差别只剩
155
+ // 「要不要后台跑」一件事 —— 所以规则能写成"贴合人设的一律走这里"。
156
+ //
157
+ // 「何时不用」写成**带替代工具**的具体清单(具体路径 → Read;找定义 → Grep/Glob),
158
+ // 比笼统说"这个很贵"有用得多(参照 Claude Code 的 `AgentTool/prompt.ts:232-240`)。
89
159
  //
90
- // 2026-09-23(用户裁定):与官方两个委派工具的**分界规则**从描述里删掉 —— 注入通道的
91
- // `how` 行(context-inject.ts 的 `DOMAIN_FRAME.subagents`)已经逐字说过一遍,而两份都在
92
- // 每轮上下文里 = 同一件事付两次 token。留注入那份(它出现在"正在选工具"的那一刻)。
160
+ // 篇幅纪律:工具表每轮都发,描述里每一句都在付 token。`how` 行
161
+ // (context-inject.ts 的 `DOMAIN_FRAME.subagents`)已经逐字说过一遍,两份都在每轮上下文里
162
+ // = 同一件事付两次 token,所以留注入那份(它出现在"正在选工具"的那一刻),这里只留参数段
163
+ // 独有的信息。判据是"删掉的那句在描述里已经有了吗"。
93
164
  // 代价如实记下:子智能体域被关掉、或走压制型预设时上下文里没有那条 how 行,模型只剩本
94
165
  // 描述与 `agent` 参数说明 —— 够它认出这条是带人设的委派通道,但"没有人设贴合时才用官方
95
166
  // 那两个"这层分界就没人说了。
96
167
  //
97
- // 2026-10-14(对抗性审查 P0-2):`subagent_manager_list` 已移出出厂默认隐藏名单,所以
98
- // `agent` 参数说明可以点名它(此前只能写"Persona name.",因为那条工具默认不在模型手里)。
99
- // 注释里那句 "Persona name from subagent_manager_list" 从 2026-09-23 起就与代码逐字不符,
100
- // 现在代码终于跟上了。
101
- //
102
- // 2026-09-23(用户裁定):本工具的 `task` / `inherit` 两个参数说明与上面的 Modes 段
103
- // **逐字重复**,而工具表每轮都发 —— 同一件事付两次 token。实测 `subagent_manager_run`
104
- // 整份 407 tok(参数段:`inherit` 86 / `task` 57 / `agent` 19),其中 `inherit` 是全表
105
- // 最贵的单个参数。收完两处重复后目标 ≈325 tok。判据是"删掉的那句在描述里已经有了吗":
106
- // Modes 段留着(它是模型的入口),参数说明只留"这一段独有的信息"。
107
168
  // `inherit` 那句"轮中委派拿不到当前轮"必须留 —— 它是这条参数唯一会让人写错 task 的地方。
108
- description: 'Run a named persona as a subagent: it gets the persona as its own system prompt, works on `task`, and returns only its final output. A persona marked `(continuable)` instead returns an agent id and runs in the background — continue it with `send_message`.\n\nModes: by default a fresh child that cannot see this conversation, so `task` must be self-contained. With `inherit: true` it also gets this conversation\'s **finished** turns (like the host\'s `subagent_fork`) — the current turn is never included, so a mid-turn hand-off still needs a self-contained `task`.\n\n`task` = the goal plus the context it needs; leave method and output format to the persona.\n\nUse it when the work matches a persona in the "Current subagents" reminder (a review, an investigation, a piece of writing) and the detail should not sit in your own context. Not for reading a file (Read), finding a definition (Grep/Glob), or touching two or three files.',
169
+ description: 'Run a named persona as a subagent: it gets the persona as its system prompt, works on `task`, and returns only its final output. A persona marked `(continuable)` returns an agent id and runs in the background — continue it with `send_message`.\n\nBy default the child cannot see this conversation, so `task` must be self-contained; `inherit: true` also hands it this conversation\'s **finished** turns (never the current one). `task` = the goal plus the context it needs; leave method and output format to the persona.\n\nUse it when the work matches a persona in the "Current subagents" reminder and the detail should not sit in your own context. Not for reading a file (Read), finding a definition (Grep/Glob), or touching two or three files.',
109
170
  parameters: {
110
- agent: { type: 'string', required: true, description: 'Persona name — see the "Current subagents" reminder, or list personas with subagent_manager_list.' },
111
- // `task` 的说明只留"这是什么"(2026-10-15 注入文本审查):上面描述里那句
112
- // "leave method and output format to the persona." 此前在这里**逐字重复**了一遍
113
- // (57 字符),而模型填这个参数时两处都在手上 —— 同一件事付两次 token。
171
+ agent: { type: 'string', required: true, description: 'Persona name — see the "Current subagents" reminder.' },
172
+ // `task` 的说明只留"这是什么":描述里已经写过"把方法与输出格式留给人设",
173
+ // 模型填这个参数时两处都在手上 —— 同一件事付两次 token。
114
174
  task: { type: 'string', required: true, description: 'The task for the subagent.' },
115
175
  inherit: { type: 'boolean', description: 'Inherit this conversation\'s finished turns (current turn excluded). Default false.' },
116
176
  },
@@ -128,13 +188,25 @@ export function defineSubagentManagerRunTool(subagents) {
128
188
  if (!exec || !exec.agent)
129
189
  throw new Error('no caller agent (exec.agent is unavailable): cannot create a subagent run');
130
190
  const docs = await subagents.list();
131
- const { allowed, reason } = await filterBySceneBinding(docs, await subagents.sceneLists());
191
+ const sceneLists = await subagents.sceneLists(exec);
192
+ const bound = filterBySceneBinding(docs, sceneLists);
193
+ // 会话层:四层并值优先(本会话显式开的能救回场景绑定之外的人设)
194
+ // 层里没话说时按全局口径(场景绑定 ∩ 全局启停)。看不见的人设**调不动** ——
195
+ // 否则会话层就等于只改了目录(对 MCP 用的是同一条口径)。
196
+ const flag = flagOfExec(subagents.flagFor, exec);
197
+ const boundSet = new Set(sceneLists.flat());
198
+ const allowed = docs.filter((p) => decideInSession(p, flag, boundSet));
199
+ const reason = allowed.length === 0 ? bound.reason : null;
132
200
  const persona = allowed.find((p) => p.name === name);
133
201
  if (!persona) {
202
+ // 关掉的人设要单独说清 —— 否则模型会以为"这个人设不存在",转而去编一个名字。
203
+ const offHere = docs.some((p) => p.name === name);
204
+ if (offHere)
205
+ throw new Error('persona unavailable: ' + name + ' (turned off — here, by its global switch, or outside the active scene; the user can enable it)');
134
206
  const available = allowed.map((p) => p.name).join(', ') || '(none)';
135
207
  throw new Error('persona unavailable: ' + name + (reason ? ' (' + reason + ')' : ' (available: ' + available + ')'));
136
208
  }
137
- // 这里**刻意没有**"深度不够就拒绝"的检查(2026-09-17 用户裁定,方案 A)。
209
+ // 这里**刻意没有**"深度不够就拒绝"的检查。
138
210
  // 它此前基于一个错误假设:以为传 `maxDepth: 1` 会让子会话再委派必然失败。实际上官方
139
211
  // 的 `maxDepth` 只用**宿主配置**那一档(0.2.1 默认 1)、provider 只在**传了值**时才校验,
140
212
  // 所以能不能继续委派问官方就行。那个检查的唯一净效果是让**我们的**工具比官方严 ——