@chaoset/adaptive-perf 0.3.2 → 0.5.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.
package/lib/index.mjs CHANGED
@@ -7,32 +7,46 @@
7
7
  * "Current runtime context" 快照(文件策略 / 审批策略等动态段);
8
8
  * 2. 工具目录极小(只有 bash + str_replace_editor)—— 每个请求的
9
9
  * tool schema 与(PTC 的)SDK 参考段都随之变小;
10
- * 3. persona 为 complete 固定提示,无其它全局 prompt 段。
10
+ * 3. persona 为 complete 固定提示,无其它全局 prompt 段;
11
+ * 4. 不挂 skill 目录提醒(~9KB)与 AGENTS.md 摘要注入。
11
12
  *
12
- * 本插件在三个维度上做"动态自适应"(而不是静态裁剪):
13
+ * 本插件在多个维度上做"动态自适应"(而不是静态裁剪),全部参照
14
+ * dsh-anchored-standard(https://github.com/xiaobright/dsh-anchored-standard)
15
+ * 的实测结论:
13
16
  * A. 运行时上下文抑制:对目标 preset 的会话调用
14
17
  * agent.ctx.systemPrompt.suppressRuntimeContext()(与极简模式同一机制),
15
18
  * 每次请求省掉快照文本,零功能损失。
16
- * B. 首轮锚定(bootstrap,参照 dsh-anchored-standard 的实测结论):
17
- * 决定首轮"轨迹"(模型思维链风格)的是请求 #1 可见的**工具 schema**
18
- * 与**自动注入的上下文提醒**,而不是静态目录大小。因此:
19
- * - 请求 #1 的工具目录收窄到真实 Minimal 工具对
20
- * (bash + str_replace_editor)——256000 输出预算下 5/5 锚定,
21
- * 而任何 standard schema 11/11 落入 standard-like 轨迹;
22
- * - 请求 #1 剥离自动注入的上下文(技能目录提醒 skill-catalog、
23
- * AGENTS.md 摘要 agent-instructions)——目录在场时锚定完全无法
24
- * 复现(0/9);用户主动的技能手势不过滤;
25
- * - 会话出现首次**持久**晋升信号(首个 tool/call 或 assistant/message,
26
- * promoteOn 可选 either/tool-call/assistant-message)后恢复完整目录;
27
- * 阶段从持久会话事件推导,resume/reload 不丢状态;
28
- * - compaction 会把会话打回"第二次首轮":compaction/end 之后回到
29
- * 受控目录(bootstrap 工具对 + compactionTools 核心工作集),直到
30
- * 出现新的持久晋升信号(纪元感知);
31
- * - bootstrap.maxTokens(可选)给请求 #1 封顶输出预算,晋升后剥离。
32
- * C. 工具目录自适应精简:会话启动时按"工具族"默认隐藏高开销低频工具
19
+ * B. 真实 Minimal 工具对(bootstrap.realPair,0.5.0 新增):首轮"轨迹"
20
+ * (模型思维链风格)由请求 #1 可见的**工具 schema** 决定,且只有与
21
+ * Minimal 逐字相同的 schema 才能锚定(issue #11:真实工具对 5/5 锚定,
22
+ * 任何 standard schema——包括 sandboxed bash/read——11/11 落入
23
+ * standard-like)。仅收窄目录不够:standard/code preset 根本没有
24
+ * str_replace_editor,其 bash 也是 sandboxed schema。因此本插件把
25
+ * Minimal 的**真实工具对**(持久 PTY bash + str_replace_editor,官方
26
+ * minimal preset 同一批插件、同一份描述)挂进 agent 作用域工具层
27
+ * (scoped registration 按名阴影继承工具),首轮目录 = 真实工具对;
28
+ * PTC SDK 参考段同样按可见目录渲染,同步缩小。
29
+ * C. 首轮锚定(bootstrap):请求 #1 只暴露真实工具对,剥离自动注入上下文
30
+ * (技能目录提醒 skill-catalog、AGENTS.md 摘要 agent-instructions;
31
+ * 用户主动的技能手势不过滤);首个**持久**晋升信号(首个 tool/call
32
+ * assistant/message,promoteOn 可选 either/tool-call/assistant-message)
33
+ * 后进入 **resident 目录**:工具对 + 常驻发现工具(dev_tool_search /
34
+ * skill_search / skill_load)+ 已解锁工具,而不是一次性 dump 完整
35
+ * Standard 目录(避免 post-promotion regression);阶段从持久会话事件
36
+ * 推导,resume/reload 不丢状态;compaction/end 之后回到受控目录
37
+ * (工具对 + compactionTools)直到新的晋升信号(纪元感知);
38
+ * bootstrap.maxTokens(可选)给请求 #1 封顶输出预算,晋升后剥离。
39
+ * D. 常驻上下文抑制(suppressInjectedContext,0.5.0 新增):技能目录提醒
40
+ * 与 AGENTS.md 摘要**不再在晋升后恢复**——参考实现实测它们即使在后置
41
+ * 阶段也扰动轨迹且每个请求多耗数千 token。替代品:
42
+ * - skill_search / skill_load:按需发现并注入单个技能的完整说明
43
+ * (晋升后常驻,替代 ~9KB 的 <available_skills> 注入);
44
+ * - instruction-hint:晋升后只注入**一次**的短提示——"这些指令文件
45
+ * 存在,需要时自己读",而不是每请求 dump 全文(文件探测走宿主 fs,
46
+ * 探测失败不注入,绝不抛错)。
47
+ * E. 工具目录自适应精简:会话启动时按"工具族"默认隐藏高开销低频工具
33
48
  * (子代理 / 工作流 / ralph / goal 等编排类),只保留核心编码工具,
34
- * 让标准模式的目录逼近极简、PTC 的 SDK 段同步缩小;随后根据两类信号
35
- * 在会话内"单调升级"放行对应工具族(只升不降,限制请求缓存失效次数):
49
+ * 随后根据两类信号在会话内"单调升级"放行对应工具族(只升不降):
36
50
  * - 关键词信号:用户消息命中某工具族的触发词(如"子代理")→ 放行该族;
37
51
  * - 失败信号:PTC 的 run_code 程序调用被隐藏工具报 UNKNOWN_TOOL
38
52
  * (tools/result 失败文本含工具名)→ 放行该族,下次程序即可调用。
@@ -40,24 +54,28 @@
40
54
  * 实现全部使用 dsh 公开服务契约(Inspect 确认):
41
55
  * - ctx.agentPresets.composedPreset(agent.ctx) 读取会话所属 preset
42
56
  * - agent.ctx.systemPrompt.suppressRuntimeContext() 抑制上下文快照
57
+ * - agent.ctx.systemPrompt.section() 按名阴影 prompt 段(persona/引导段)
43
58
  * - agent.ctx.tools.schemas(agent) 读取该 agent 当前可见工具(限制交集)
44
59
  * - agent.ctx.tools.restrict({ deny }) 按 agent 作用域裁剪继承工具
45
- * - system-prompt/assemble(waterfall) 请求装配(早期实现;bootstrap 目录
46
- * 收窄改用 tools.restrict——PTC 模式下 assembly.tools 只有 run_code,
47
- * 过滤会降级失效,而 restrict 同时驱动 API 目录与 PTC SDK 参考段)
48
- * - agent/pre-step(waterfall) 剥离自动注入的上下文消息(source.kind
60
+ * - agent.ctx.tools.register() 在 agent 作用域注册工具(按名阴影继承工具,
61
+ * 自身层注册不受 restrict 影响——view() own layer 豁免)
62
+ * - agent.ctx.plugin() 挂载官方 minimal preset 同款插件(持久 bash /
63
+ * str_replace_editor / 其依赖的 terminals 与本地 fs
64
+ * - agent/pre-step(waterfall) 剥离自动注入上下文 + 注入 instruction-hint
49
65
  * - agent/request(waterfall) 首轮输出预算封顶
50
66
  * - session/event 增量喂入持久事件(晋升 / compaction 纪元)
51
67
  * - agent/created、agent/disposed、agent/inbox/inserted、tools/result 事件
52
68
  * 监听器都注册在 host 根作用域(与 dsh-agent-presets 自身同款用法),事件按
53
- * scope 过滤分发,子作用域派发的事件根监听器可收到;三个 waterfall 均以
69
+ * scope 过滤分发,子作用域派发的事件根监听器可收到;两个 waterfall 均以
54
70
  * prepend 注册保证"最外层"(剥离是最后一道变换)。
55
71
  *
56
72
  * 配置:cordis.patch.yml 的 config(安装默认)与
57
73
  * ~/.dsh/plugins/adaptive-perf/config.json(设置页 UI,权威)合并,
58
74
  * 保存后热生效(新会话立即生效;已运行会话按新配置重算限制)。
59
75
  *
60
- * 本文件不依赖任何 dsh 内部包(纯 ESM + ctx 服务),可独立安装。
76
+ * 依赖:真实工具对需要官方插件包(@deepseek-ai/dsh-terminal 等,声明为
77
+ * optionalDependencies)。它们不可用时本插件自动降级(告警 + 退回旧行为:
78
+ * 只收窄目录、不替换 schema),绝不拖垮 harness。
61
79
  */
62
80
 
63
81
  import { createConfigStore } from './config-store.mjs';
@@ -99,6 +117,17 @@ export const DEFAULT_CONFIG = {
99
117
  escalateOnKeyword: true,
100
118
  /** 工具调用失败(UNKNOWN_TOOL 文本含隐藏工具名)时自动放行该族。 */
101
119
  escalateOnUnknownTool: true,
120
+ /**
121
+ * 常驻上下文抑制(0.5.0):true = 整个会话剥离自动注入上下文
122
+ * (skill-catalog 技能目录提醒、agent-instructions AGENTS.md 摘要),
123
+ * 不再像旧版那样在晋升后恢复——替代品是 skill_search/skill_load 按需发现
124
+ * 与 instruction-hint 一次性提示。false = 只在 bootstrap(请求 #1)剥离。
125
+ */
126
+ suppressInjectedContext: true,
127
+ /** 晋升后常驻 skill_search / skill_load 两个按需技能发现工具。 */
128
+ skillDiscovery: true,
129
+ /** 晋升后注入一次"指令文件存在,需要时自读"的短提示(替代全文注入)。 */
130
+ instructionHint: true,
102
131
  /** 核心编码工具(不进入任何限制族;此处仅作文档展示)。 */
103
132
  coreTools: [
104
133
  'bash', 'read', 'write', 'edit', 'glob', 'grep', 'read_image',
@@ -132,6 +161,13 @@ export const DEFAULT_CONFIG = {
132
161
  bootstrap: {
133
162
  /** 是否启用首轮锚定。 */
134
163
  enabled: true,
164
+ /**
165
+ * 是否挂载真实 Minimal 工具对(0.5.0):把官方 minimal preset 的持久
166
+ * PTY bash + str_replace_editor 挂进 agent 作用域(按名阴影 standard 的
167
+ * sandboxed bash),使请求 #1 的 schema 与 Minimal 逐字相同。依赖
168
+ * @deepseek-ai 官方插件包,不可用时自动降级为旧行为(仅收窄目录)。
169
+ */
170
+ realPair: true,
135
171
  /** 请求 #1 可见的工具(真实 Minimal 工具对)。 */
136
172
  tools: ['bash', 'str_replace_editor'],
137
173
  /** 晋升触发:either(默认)/ tool-call / assistant-message。 */
@@ -140,9 +176,32 @@ export const DEFAULT_CONFIG = {
140
176
  suppressedContextSources: ['skill-catalog', 'agent-instructions'],
141
177
  /** compaction/end 之后、再次晋升前可用的额外核心工作集。 */
142
178
  compactionTools: ['read', 'write', 'edit', 'glob', 'grep', 'todo_write', 'ask_user_question'],
179
+ /** 晋升后常驻的按需发现工具(dsh-anchored-standard 的 resident discovery set)。 */
180
+ discoveryTools: ['dev_tool_search', 'skill_search', 'skill_load'],
143
181
  /** 请求 #1 的输出预算封顶(token);0 = 不封顶(opt-in)。 */
144
182
  maxTokens: 0,
145
183
  },
184
+ /**
185
+ * 极简提示词层(语域锚定,参照 dsh-anchored-standard 的完整条件)。
186
+ *
187
+ * 参考实现指出:首轮"轨迹"(思维链风格)锚定需要 **Minimal 的完整 system
188
+ * prompt**——只收窄工具 schema 不够。本层对目标 preset 的会话:
189
+ * - suppressSections:按名阴影屏蔽全局引导段(harness:identity、
190
+ * harness:source、app:web-surface),空段在装配时被丢弃,等同极简
191
+ * complete persona 的效果(plan-mode 段、PTC 的 SDK 段不受影响);
192
+ * - persona:按名阴影替换 deployment:persona 为极简语域的短 persona
193
+ * (默认与极简模式逐字相同;留空则不替换)。
194
+ * 两处都是 agent 作用域的 prompt 段阴影,仅影响目标会话,随会话销毁自动
195
+ * 释放;对已恢复/已晋升的旧会话同样生效(不依赖 bootstrap 阶段)。
196
+ */
197
+ minimalPrompt: {
198
+ /** 是否启用极简提示词层。 */
199
+ enabled: true,
200
+ /** 替换后的 persona 文本;与极简模式相同可完全对齐语域。留空不替换。 */
201
+ persona: 'You are a helpful software engineer assistant.',
202
+ /** 屏蔽全局引导段(identity / source / web-surface)。 */
203
+ suppressSections: true,
204
+ },
146
205
  };
147
206
 
148
207
  /** 轻量配置校验/归一化:非法字段回退默认值,避免 config.json 或 remote.set
@@ -197,12 +256,23 @@ export function normalizeConfig(source, defaults = DEFAULT_CONFIG) {
197
256
  const maxTokens = Number.isSafeInteger(maxTokensRaw) && maxTokensRaw > 0 ? maxTokensRaw : 0;
198
257
  const bootstrap = {
199
258
  enabled: boolValue(rawBootstrap.enabled, db.enabled),
259
+ realPair: boolValue(rawBootstrap.realPair, db.realPair),
200
260
  tools: stringList(rawBootstrap.tools, db.tools),
201
261
  promoteOn,
202
262
  suppressedContextSources: stringListOrEmpty(rawBootstrap.suppressedContextSources, db.suppressedContextSources),
203
263
  compactionTools: stringListOrEmpty(rawBootstrap.compactionTools, db.compactionTools),
264
+ discoveryTools: stringListOrEmpty(rawBootstrap.discoveryTools, db.discoveryTools),
204
265
  maxTokens,
205
266
  };
267
+ const rawMP = raw.minimalPrompt !== null && typeof raw.minimalPrompt === 'object' && !Array.isArray(raw.minimalPrompt)
268
+ ? raw.minimalPrompt
269
+ : defaults.minimalPrompt;
270
+ const dm = defaults.minimalPrompt;
271
+ const minimalPrompt = {
272
+ enabled: boolValue(rawMP.enabled, dm.enabled),
273
+ persona: typeof rawMP.persona === 'string' ? rawMP.persona.trim() : dm.persona,
274
+ suppressSections: boolValue(rawMP.suppressSections, dm.suppressSections),
275
+ };
206
276
  return {
207
277
  enabled: boolValue(merged.enabled, defaults.enabled),
208
278
  presets: stringList(merged.presets, defaults.presets),
@@ -210,9 +280,13 @@ export function normalizeConfig(source, defaults = DEFAULT_CONFIG) {
210
280
  leanByDefault: boolValue(merged.leanByDefault, defaults.leanByDefault),
211
281
  escalateOnKeyword: boolValue(merged.escalateOnKeyword, defaults.escalateOnKeyword),
212
282
  escalateOnUnknownTool: boolValue(merged.escalateOnUnknownTool, defaults.escalateOnUnknownTool),
283
+ suppressInjectedContext: boolValue(merged.suppressInjectedContext, defaults.suppressInjectedContext),
284
+ skillDiscovery: boolValue(merged.skillDiscovery, defaults.skillDiscovery),
285
+ instructionHint: boolValue(merged.instructionHint, defaults.instructionHint),
213
286
  coreTools: stringList(merged.coreTools, defaults.coreTools),
214
287
  families,
215
288
  bootstrap,
289
+ minimalPrompt,
216
290
  };
217
291
  }
218
292
 
@@ -221,7 +295,8 @@ export function validateConfig(partial) {
221
295
  if (partial === null || typeof partial !== 'object' || Array.isArray(partial)) {
222
296
  throw new TypeError('adaptive-perf config must be a plain object');
223
297
  }
224
- const boolFields = ['enabled', 'suppressRuntimeContext', 'leanByDefault', 'escalateOnKeyword', 'escalateOnUnknownTool'];
298
+ const boolFields = ['enabled', 'suppressRuntimeContext', 'leanByDefault', 'escalateOnKeyword', 'escalateOnUnknownTool',
299
+ 'suppressInjectedContext', 'skillDiscovery', 'instructionHint'];
225
300
  for (const key of boolFields) {
226
301
  if (partial[key] !== void 0 && typeof partial[key] !== 'boolean') {
227
302
  throw new TypeError(`adaptive-perf config field "${key}" must be a boolean`);
@@ -258,22 +333,52 @@ export function validateConfig(partial) {
258
333
  if (b.enabled !== void 0 && typeof b.enabled !== 'boolean') {
259
334
  throw new TypeError('adaptive-perf config field "bootstrap.enabled" must be a boolean');
260
335
  }
336
+ if (b.realPair !== void 0 && typeof b.realPair !== 'boolean') {
337
+ throw new TypeError('adaptive-perf config field "bootstrap.realPair" must be a boolean');
338
+ }
261
339
  if (b.promoteOn !== void 0 && !['either', 'tool-call', 'assistant-message'].includes(b.promoteOn)) {
262
340
  throw new TypeError('adaptive-perf config field "bootstrap.promoteOn" must be "either" | "tool-call" | "assistant-message"');
263
341
  }
264
342
  if (b.maxTokens !== void 0 && !(Number.isSafeInteger(b.maxTokens) && b.maxTokens >= 0)) {
265
343
  throw new TypeError('adaptive-perf config field "bootstrap.maxTokens" must be a non-negative safe integer');
266
344
  }
267
- for (const key of ['tools', 'suppressedContextSources', 'compactionTools']) {
345
+ for (const key of ['tools', 'suppressedContextSources', 'compactionTools', 'discoveryTools']) {
268
346
  if (b[key] !== void 0 && !(Array.isArray(b[key]) && b[key].every((v) => typeof v === 'string'))) {
269
347
  throw new TypeError(`adaptive-perf config field "bootstrap.${key}" must be an array of strings`);
270
348
  }
271
349
  }
272
350
  }
351
+ if (partial.minimalPrompt !== void 0) {
352
+ const m = partial.minimalPrompt;
353
+ if (m === null || typeof m !== 'object' || Array.isArray(m)) {
354
+ throw new TypeError('adaptive-perf config field "minimalPrompt" must be an object');
355
+ }
356
+ if (m.enabled !== void 0 && typeof m.enabled !== 'boolean') {
357
+ throw new TypeError('adaptive-perf config field "minimalPrompt.enabled" must be a boolean');
358
+ }
359
+ if (m.suppressSections !== void 0 && typeof m.suppressSections !== 'boolean') {
360
+ throw new TypeError('adaptive-perf config field "minimalPrompt.suppressSections" must be a boolean');
361
+ }
362
+ if (m.persona !== void 0 && typeof m.persona !== 'string') {
363
+ throw new TypeError('adaptive-perf config field "minimalPrompt.persona" must be a string');
364
+ }
365
+ }
273
366
  }
274
367
 
275
368
  // ── 纯函数(导出便于回归测试)────────────────────────────────────────────
276
369
 
370
+ /** 被极简提示词层屏蔽的全局引导段(name, order,与 dsh-app-boot /
371
+ * dsh-web-app / dsh-system-prompt 的注册一致)。 */
372
+ export const SECTION_SHADOWS = [
373
+ ['harness:identity', -100],
374
+ ['harness:source', -99],
375
+ ['app:web-surface', -98],
376
+ ];
377
+
378
+ /** persona 段的注册名与顺序(dsh-system-prompt 的 PERSONA_SECTION/PERSONA_ORDER)。 */
379
+ export const PERSONA_SECTION_NAME = 'deployment:persona';
380
+ export const PERSONA_SECTION_ORDER = 0;
381
+
277
382
  /** 提取一条用户消息的顶层文本(text 块拼接)。 */
278
383
  export function extractUserText(message) {
279
384
  if (message === null || typeof message !== 'object' || !Array.isArray(message.content)) return '';
@@ -313,6 +418,416 @@ export function collectFailureText(result) {
313
418
  return parts.join('\n');
314
419
  }
315
420
 
421
+ // ── 真实 Minimal 工具对(0.5.0,参照 dsh-anchored-standard issue #11)──────
422
+
423
+ /**
424
+ * 官方 minimal preset 的持久 bash 工具描述,逐字一致。schema(参数)由
425
+ * @deepseek-ai/dsh-tool-bash-persistent 提供,与 minimal preset 挂的是同一
426
+ * 个包;描述在这里显式传入,保证与 minimal 的 agent.cordis.yml 完全一致。
427
+ */
428
+ export const MINIMAL_BASH_DESCRIPTION = [
429
+ 'Run commands in a bash shell',
430
+ '* When invoking this tool, the contents of the "command" parameter does NOT need to be XML-escaped.',
431
+ "* You don't have access to the internet via this tool.",
432
+ '* You do have access to a mirror of common linux and python packages via apt and pip.',
433
+ '* State is persistent across command calls and discussions with the user.',
434
+ "* To inspect a particular line range of a file, e.g. lines 10-25, try 'sed -n 10,25p /path/to/the/file'.",
435
+ '* Please avoid commands that may produce a very large amount of output.',
436
+ "* Please run long lived commands in the background, e.g. 'sleep 10 &' or start a server in the background.",
437
+ ].join('\n');
438
+
439
+ /**
440
+ * 被真实工具对按名阴影的工具引导段:standard 的 sandboxed bash 注册了
441
+ * `tool:bash`(order 105)引导段,挂载真实工具对后该文本已与可见工具不符,
442
+ * 按名阴影为空段(装配时丢弃)。
443
+ */
444
+ export const TOOL_GUIDANCE_SHADOWS = [
445
+ ['tool:bash', 105],
446
+ ];
447
+
448
+ /** 真实工具对挂载所需的官方插件包说明符(声明在 optionalDependencies)。 */
449
+ export const REAL_PAIR_SPECS = {
450
+ terminal: '@deepseek-ai/dsh-terminal',
451
+ terminalBash: '@deepseek-ai/dsh-terminal-bash',
452
+ bashPersistent: '@deepseek-ai/dsh-tool-bash-persistent',
453
+ fsLocal: '@deepseek-ai/dsh-fs-local',
454
+ strReplaceEditor: '@deepseek-ai/dsh-tool-str-replace-editor',
455
+ };
456
+
457
+ /**
458
+ * 加载真实 Minimal 工具对所需模块。任一包不可用即整体返回 null(调用方
459
+ * 降级为旧行为),绝不抛出。
460
+ * @param importFn - 测试可注入的 import 实现(默认动态 import)。
461
+ */
462
+ export async function loadRealPairModules(importFn) {
463
+ const dynamicImport = importFn ?? ((spec) => import(spec));
464
+ const modules = {};
465
+ for (const [key, spec] of Object.entries(REAL_PAIR_SPECS)) {
466
+ try {
467
+ const mod = await dynamicImport(spec);
468
+ modules[key] = mod?.default ?? mod;
469
+ } catch {
470
+ modules[key] = null;
471
+ }
472
+ }
473
+ if (Object.keys(REAL_PAIR_SPECS).some((key) => modules[key] === null)) return null;
474
+ return modules;
475
+ }
476
+
477
+ /**
478
+ * 计算真实工具对的挂载清单(纯函数,便于测试):
479
+ * 顺序 = 依赖在前(terminals → backend → bash;fs → editor)。
480
+ * @param modules - loadRealPairModules 的结果。
481
+ * @param cwd - str_replace_editor 本地 fs 的工作目录(会话 cwd)。
482
+ */
483
+ export function realPairMounts(modules, cwd) {
484
+ if (modules === null || modules === void 0) return [];
485
+ const resolvedCwd = typeof cwd === 'string' && cwd.length > 0
486
+ ? cwd
487
+ : (process.env.DSH_CWD && process.env.DSH_CWD.trim().length > 0 ? process.env.DSH_CWD : process.cwd());
488
+ return [
489
+ { module: modules.terminal, config: {} },
490
+ { module: modules.terminalBash, config: { timeoutMs: 300000 } },
491
+ { module: modules.bashPersistent, config: { timeoutMs: 300000, description: MINIMAL_BASH_DESCRIPTION } },
492
+ { module: modules.fsLocal, config: { cwd: resolvedCwd } },
493
+ { module: modules.strReplaceEditor, config: { maxOutputChars: 16000 } },
494
+ ];
495
+ }
496
+
497
+ /**
498
+ * 在 agent 作用域挂载真实工具对(scoped registration 按名阴影继承工具)。
499
+ * 每个挂载是一个独立 fiber,dispose 时全部副作用(工具/schema/服务)撤销。
500
+ * @param agent - 目标 agent(其 ctx 必须带 scope 标签)。
501
+ * @param mounts - realPairMounts 的结果。
502
+ * @param warn - 日志回调。
503
+ * @returns 可释放的 disposer 列表。
504
+ */
505
+ export function mountRealPair(agent, mounts, warn) {
506
+ const disposers = [];
507
+ if (agent === null || typeof agent !== 'object' || typeof agent.ctx?.plugin !== 'function') return disposers;
508
+ for (const mount of mounts) {
509
+ if (mount?.module === null || mount?.module === void 0) continue;
510
+ try {
511
+ const fiber = agent.ctx.plugin(mount.module, mount.config ?? {});
512
+ disposers.push(() => {
513
+ try { fiber.dispose(); } catch {}
514
+ });
515
+ } catch (error) {
516
+ warn?.(`mountRealPair failed for ${String(mount.module?.name ?? 'module')}: ${String((error && error.message) || error)}`);
517
+ }
518
+ }
519
+ return disposers;
520
+ }
521
+
522
+ // ── instruction-hint(0.5.0,参照 dsh-anchored-standard)──────────────────
523
+
524
+ /** 项目链候选指令文件名(探测顺序)与用户全局候选。 */
525
+ export const INSTRUCTION_PROJECT_CANDIDATES = ['AGENTS.md', 'CLAUDE.md', 'AGENTS.local.md', 'CLAUDE.local.md'];
526
+ export const INSTRUCTION_USER_CANDIDATE = 'AGENTS.md';
527
+
528
+ /** 平台无关的路径拼接(与参考实现一致)。 */
529
+ export function joinPath(dir, segment) {
530
+ if (dir.endsWith('/') || dir.endsWith('\\')) return dir + segment;
531
+ const sep = dir.includes('\\') ? '\\' : '/';
532
+ return dir + sep + segment;
533
+ }
534
+
535
+ /** 绝对 POSIX/Windows 路径的父目录。 */
536
+ export function parentPath(path) {
537
+ const idx = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
538
+ if (idx <= 0) return path;
539
+ const parent = path.slice(0, idx);
540
+ return parent.length === 0 ? path : parent;
541
+ }
542
+
543
+ /** 从 cwd 向上找项目根(含 .git/.hg/.svn 标记的最近祖先)。 */
544
+ export async function findProjectRoot(fs, cwd, signal) {
545
+ let current = cwd;
546
+ for (;;) {
547
+ for (const marker of ['.git', '.hg', '.svn']) {
548
+ try {
549
+ const target = await fs.resolve(joinPath(current, marker), { cwd, signal });
550
+ const info = await fs.stat(target, signal);
551
+ if (info !== void 0) return current;
552
+ } catch {
553
+ // 探测失败 = 标记不存在,继续。
554
+ }
555
+ }
556
+ const parent = parentPath(current);
557
+ if (parent === current || parent.length === 0) return cwd;
558
+ current = parent;
559
+ }
560
+ }
561
+
562
+ /** 列出某目录下存在的候选指令文件。 */
563
+ export async function presentInDir(fs, dir, candidates, signal) {
564
+ const found = [];
565
+ for (const candidate of candidates) {
566
+ try {
567
+ const target = await fs.resolve(joinPath(dir, candidate), { cwd: dir, signal });
568
+ const info = await fs.stat(target, signal);
569
+ if (info !== void 0 && info.type === 'file') found.push(candidate);
570
+ } catch {
571
+ // 不存在/不可读——跳过。
572
+ }
573
+ }
574
+ return found;
575
+ }
576
+
577
+ /**
578
+ * 探测会话的指令文件(项目链 + $DSH_HOME/AGENTS.md)。任何失败都返回
579
+ * null/空集——提示注入绝不能让会话受损。
580
+ * @returns { projectFiles, root, userGlobalFiles } 或 null(无 fs / 无 cwd)。
581
+ */
582
+ export async function probeInstructionFiles(fs, cwd, dshHome, signal) {
583
+ if (fs === null || fs === void 0 || typeof cwd !== 'string' || cwd.length === 0) return null;
584
+ const projectFiles = [];
585
+ let root = cwd;
586
+ try {
587
+ root = await findProjectRoot(fs, cwd, signal);
588
+ projectFiles.push(...await presentInDir(fs, root, INSTRUCTION_PROJECT_CANDIDATES, signal));
589
+ } catch {
590
+ // 项目链探测失败——忽略,仍报告用户全局文件。
591
+ }
592
+ const userGlobalFiles = [];
593
+ if (typeof dshHome === 'string' && dshHome.length > 0) {
594
+ try {
595
+ userGlobalFiles.push(...await presentInDir(fs, dshHome, [INSTRUCTION_USER_CANDIDATE], signal));
596
+ } catch {
597
+ // 不可读——忽略。
598
+ }
599
+ }
600
+ if (projectFiles.length === 0 && userGlobalFiles.length === 0) return null;
601
+ return { projectFiles, root, userGlobalFiles };
602
+ }
603
+
604
+ // ── 按需发现工具(resident discovery set,参照 dsh-anchored-standard)─────
605
+
606
+ /**
607
+ * 从 agent/pre-step 的 decision.messages 剥离指定 source.kind 的消息。
608
+ * 保留原数组引用(未变化时)便于调用方判断。
609
+ */
610
+ export function stripSuppressedMessages(messages, suppressedSources) {
611
+ if (!Array.isArray(messages) || suppressedSources.size === 0) return messages;
612
+ const kept = messages.filter((message) => {
613
+ const kind = message?.source?.kind;
614
+ return typeof kind !== 'string' || !suppressedSources.has(kind);
615
+ });
616
+ return kept.length === messages.length ? messages : kept;
617
+ }
618
+
619
+ /**
620
+ * dev_tool_search:搜索完整可见目录并按名解锁工具(解锁结果下一请求生效,
621
+ * 经 syncBootstrap 重算 deny 集;解锁记录写入 a.unlocked,resume-safe 由
622
+ * loadUnlockedFromEvents 从持久事件恢复)。
623
+ */
624
+ export function createDevToolSearch({ schemasOf, onUnlock, catalogHint }) {
625
+ const hint = catalogHint ?? [
626
+ 'This session starts with a minimal resident set: bash, str_replace_editor.',
627
+ 'Everything else is unlocked on demand through this tool.',
628
+ ].join('\n');
629
+ return {
630
+ name: 'dev_tool_search',
631
+ description: [
632
+ 'Discover and unlock tools that are NOT currently available.',
633
+ '',
634
+ hint,
635
+ '',
636
+ 'If the current task needs internet, delegation, workflows, goals, images, background jobs, or multi-agent coordination, call dev_tool_search FIRST — do not try to work around them with bash.',
637
+ '',
638
+ 'Usage: pass `query` to search the catalog (returns matching tool names + descriptions), then pass `toolNames` with exact names to unlock them. Unlocked tools appear from the next request on and stay unlocked for the session.',
639
+ ].join('\n'),
640
+ parameters: {
641
+ type: 'object',
642
+ properties: {
643
+ query: { type: 'string', description: 'search keywords (e.g. "web", "subagent")' },
644
+ toolNames: { type: 'array', description: 'exact tool names to unlock', items: { type: 'string' } },
645
+ },
646
+ required: [],
647
+ additionalProperties: false,
648
+ },
649
+ output: {
650
+ schema: { type: 'object', additionalProperties: false, properties: { text: { type: 'string' } }, required: ['text'] },
651
+ render: (_a, value) => [{ type: 'text', text: value.text }],
652
+ },
653
+ async execute(args, exec) {
654
+ const query = args !== null && typeof args === 'object' && typeof args.query === 'string' ? args.query.trim() : '';
655
+ const unlock = args !== null && typeof args === 'object' && Array.isArray(args.toolNames)
656
+ ? args.toolNames.filter((name) => typeof name === 'string' && name.length > 0)
657
+ : [];
658
+ const lines = [];
659
+ if (unlock.length > 0) {
660
+ let available = new Set();
661
+ try {
662
+ const schemas = typeof schemasOf === 'function' ? (schemasOf(exec) ?? []) : [];
663
+ for (const schema of schemas) {
664
+ if (schema !== null && typeof schema === 'object' && typeof schema.name === 'string') available.add(schema.name);
665
+ }
666
+ } catch {}
667
+ const ok = unlock.filter((name) => available.has(name));
668
+ if (ok.length > 0 && typeof onUnlock === 'function') {
669
+ try { await onUnlock(ok, exec); } catch {}
670
+ }
671
+ if (ok.length > 0) lines.push(`Unlocked for the next request: ${ok.join(', ')}`);
672
+ const missing = unlock.filter((name) => !available.has(name));
673
+ if (missing.length > 0) lines.push(`Not found in catalog: ${missing.join(', ')}`);
674
+ }
675
+ if (query.length === 0 && unlock.length === 0) {
676
+ lines.push('Provide `query` to search the catalog, or `toolNames` to unlock tools.');
677
+ return { text: lines.join('\n') };
678
+ }
679
+ if (query.length === 0) {
680
+ return { text: lines.join('\n') || 'Nothing to do.' };
681
+ }
682
+ try {
683
+ const schemas = typeof schemasOf === 'function' ? (schemasOf(exec) ?? []) : [];
684
+ const wanted = query.toLowerCase().split(/[^a-z0-9_]+/).filter(Boolean);
685
+ const matches = schemas
686
+ .filter((schema) => {
687
+ if (schema === null || typeof schema !== 'object') return false;
688
+ const haystack = `${schema.name || ''} ${schema.description || ''}`.toLowerCase();
689
+ return wanted.every((token) => haystack.includes(token));
690
+ })
691
+ .slice(0, 25);
692
+ if (matches.length === 0) {
693
+ lines.push(`No tools match "${query}".`);
694
+ } else {
695
+ lines.push(`Matching tools (${matches.length}):`);
696
+ for (const schema of matches) {
697
+ const desc = String(schema.description || '').split('\n')[0].slice(0, 90);
698
+ lines.push(`- ${schema.name}: ${desc}`);
699
+ }
700
+ lines.push('Unlock with dev_tool_search({"toolNames": ["<exact name>"]}).');
701
+ }
702
+ } catch (error) {
703
+ lines.push(`catalog search unavailable: ${String((error && error.message) || error)}`);
704
+ }
705
+ return { text: lines.join('\n') };
706
+ },
707
+ };
708
+ }
709
+
710
+ /**
711
+ * skill_search:按关键词搜索可用技能(仅摘要,替代 ~9KB 目录注入)。
712
+ * skillsOf 返回宿主 skills 服务(调用期解析)。
713
+ */
714
+ export function createSkillSearch({ skillsOf }) {
715
+ const tokens = (text) => (text || '').toLowerCase().split(/[^a-z0-9_-]+/).filter(Boolean);
716
+ return {
717
+ name: 'skill_search',
718
+ description: 'Search the available skills by keyword and return matching skill names with short descriptions. This session keeps NO skill catalog in the prompt — if a task looks like it matches a skill (document conversion, image processing, game reviews, markdown, PDF, spreadsheets, …), call skill_search FIRST to find it, then skill_load to activate it. Do NOT assume skill names from memory.',
719
+ parameters: {
720
+ type: 'object',
721
+ properties: { query: { type: 'string', description: 'search keywords (e.g. "pdf", "obsidian", "game review")' } },
722
+ required: ['query'],
723
+ additionalProperties: false,
724
+ },
725
+ output: {
726
+ schema: { type: 'object', additionalProperties: false, properties: { text: { type: 'string' } }, required: ['text'] },
727
+ render: (_a, value) => [{ type: 'text', text: value.text }],
728
+ },
729
+ async execute(args, exec) {
730
+ const wanted = tokens(args?.query);
731
+ try {
732
+ const skills = skillsOf();
733
+ if (skills === void 0 || typeof skills.list !== 'function') {
734
+ return { text: 'skill_search unavailable: skills service is not mounted.' };
735
+ }
736
+ const agent = exec?.agent;
737
+ const lookup = {
738
+ scope: agent,
739
+ cwd: agent?.session?.header?.cwd,
740
+ signal: exec?.signal,
741
+ };
742
+ const all = await skills.list(lookup);
743
+ const matches = (Array.isArray(all) ? all : []).filter((skill) => {
744
+ if (wanted.length === 0) return true;
745
+ const haystack = tokens(`${skill?.name ?? ''} ${skill?.description ?? ''} ${skill?.whenToUse ?? ''}`).join(' ');
746
+ return wanted.every((token) => haystack.includes(token));
747
+ });
748
+ const head = matches.slice(0, 20);
749
+ const lines = head.map((skill) => {
750
+ const desc = String(skill?.description || '').split('\n')[0];
751
+ return `- ${skill?.name}: ${desc}`;
752
+ });
753
+ if (lines.length === 0) return { text: `No skills match "${args?.query}". Use skill_search with other keywords.` };
754
+ const extra = matches.length > 20 ? `\n…(${matches.length - 20} more)` : '';
755
+ return { text: `Matching skills (${matches.length}):\n${lines.join('\n')}${extra}\n\nLoad one with skill_load (exact name).` };
756
+ } catch (error) {
757
+ return { text: `skill_search unavailable: ${String((error && error.message) || error)}` };
758
+ }
759
+ },
760
+ };
761
+ }
762
+
763
+ /**
764
+ * skill_load:按精确名加载单个技能的完整说明,经 agent.inject 作为
765
+ * source.kind='skill-invocation' 的下一条上下文消息注入(用户主动技能手势
766
+ * 同款来源,绝不被默认抑制集剥离)。
767
+ */
768
+ export function createSkillLoad({ skillsOf }) {
769
+ return {
770
+ name: 'skill_load',
771
+ description: 'Load the full instructions of ONE skill by its exact name (from skill_search results) and inject them for the next request. Call this before acting on a task that matches the skill.',
772
+ parameters: {
773
+ type: 'object',
774
+ properties: { name: { type: 'string', description: 'exact skill name (kebab-case, from skill_search)' } },
775
+ required: ['name'],
776
+ additionalProperties: false,
777
+ },
778
+ output: {
779
+ schema: { type: 'object', additionalProperties: false, properties: { text: { type: 'string' } }, required: ['text'] },
780
+ render: (_a, value) => [{ type: 'text', text: value.text }],
781
+ },
782
+ async execute(args, exec) {
783
+ try {
784
+ const agent = exec?.agent;
785
+ if (agent === void 0) return { text: 'skill_load requires an agent context.' };
786
+ const skills = skillsOf();
787
+ if (skills === void 0 || typeof skills.get !== 'function') {
788
+ return { text: 'skill_load unavailable: skills service is not mounted.' };
789
+ }
790
+ const name = typeof args?.name === 'string' ? args.name.trim() : '';
791
+ if (name.length === 0) return { text: 'skill_load requires a skill name.' };
792
+ const skill = await skills.get(name, {
793
+ scope: agent,
794
+ cwd: agent.session?.header?.cwd,
795
+ signal: exec?.signal,
796
+ });
797
+ if (skill === void 0) {
798
+ return { text: `No skill named "${name}". Run skill_search to list available skills.` };
799
+ }
800
+ const body = extractSkillBody(skill);
801
+ if (body.length === 0) {
802
+ return { text: `Skill "${name}" has no loadable body.` };
803
+ }
804
+ if (typeof agent.inject !== 'function') {
805
+ return { text: `Skill "${name}" found, but injection is unavailable in this agent.` };
806
+ }
807
+ agent.inject({
808
+ id: `skill-load-${name}-${Date.now()}`,
809
+ role: 'user',
810
+ content: [{ type: 'text', text: body }],
811
+ source: { kind: 'skill-invocation', name, form: 'instructions' },
812
+ });
813
+ return { text: `Skill "${name}" loaded; its instructions will be injected for the next request.` };
814
+ } catch (error) {
815
+ return { text: `skill_load failed: ${String((error && error.message) || error)}` };
816
+ }
817
+ },
818
+ };
819
+ }
820
+
821
+ /** 提取已加载技能定义的模型可见正文(与参考实现一致)。 */
822
+ export function extractSkillBody(skill) {
823
+ const content = skill?.content ?? skill?.instructions ?? skill?.body;
824
+ if (typeof content === 'string') return content;
825
+ if (Array.isArray(content)) {
826
+ return content.map((part) => (typeof part === 'string' ? part : JSON.stringify(part))).join('\n');
827
+ }
828
+ return '';
829
+ }
830
+
316
831
  // ── bootstrap 锚定(参照 dsh-anchored-standard 的实测结论)──────────────
317
832
 
318
833
  /** promoteOn 配置 → 晋升的持久事件类型集合。 */
@@ -398,7 +913,7 @@ export function applyBootstrapBudget(config, promoted, maxTokens) {
398
913
 
399
914
  // ── host 插件 ────────────────────────────────────────────────────────────
400
915
 
401
- export function apply(ctx, config) {
916
+ export async function apply(ctx, config, options = {}) {
402
917
  // fail-safe:初始化失败只记录,绝不让本插件拖垮 harness(host 层挂载时
403
918
  // entry 异常会导致进程启动失败)。
404
919
  try {
@@ -419,6 +934,41 @@ export function apply(ctx, config) {
419
934
  /** 当前生效配置(热更新;后续 store 创建后以 config.json 权威合并结果覆盖)。 */
420
935
  let cfg = normalizeConfig(patchConfig);
421
936
 
937
+ /** 真实 Minimal 工具对模块(0.5.0)。加载失败 = null = 降级旧行为。
938
+ * options.realPairModules 仅供测试注入。 */
939
+ const realPairModules = options.realPairModules === void 0
940
+ ? (cfg.bootstrap.enabled && cfg.bootstrap.realPair ? await loadRealPairModules() : null)
941
+ : options.realPairModules;
942
+ /** 是否已尝试加载过真实工具对模块(避免配置热更新时反复重试)。 */
943
+ let realPairModulesTried = options.realPairModules !== void 0 || (cfg.bootstrap.enabled && cfg.bootstrap.realPair);
944
+ if (cfg.bootstrap.enabled && cfg.bootstrap.realPair && realPairModules === null) {
945
+ try {
946
+ ctx.logger?.warn?.('adaptive-perf: real Minimal tool pair modules unavailable (optional @deepseek-ai deps missing?); falling back to catalog-only bootstrap');
947
+ } catch {}
948
+ }
949
+
950
+ /** 已注入过 instruction-hint 的会话(内存去重,与参考实现一致)。 */
951
+ const hintedSessions = new Set();
952
+
953
+ /**
954
+ * 配置热更新把 realPair 从关闭切到开启时补加载官方模块(初始未启用时
955
+ * 不加载)。加载成功后对已接管会话补挂。
956
+ */
957
+ function ensureRealPairModules() {
958
+ if (realPairModulesTried || cfg.bootstrap.enabled === false || cfg.bootstrap.realPair === false) return;
959
+ realPairModulesTried = true;
960
+ loadRealPairModules().then((mods) => {
961
+ if (mods === null) return;
962
+ realPairModules = mods;
963
+ info('real Minimal tool pair modules loaded; mounting for live agents');
964
+ for (const a of agents.values()) {
965
+ syncRealPair(a);
966
+ applyMinimalPrompt(a);
967
+ syncBootstrap(a);
968
+ }
969
+ }).catch(() => {});
970
+ }
971
+
422
972
  /** agentId -> 会话自适应状态。 */
423
973
  const agents = new Map();
424
974
 
@@ -459,9 +1009,38 @@ export function apply(ctx, config) {
459
1009
  }
460
1010
  }
461
1011
 
1012
+ /** 应用/更新极简提示词层(persona 阴影 + 全局引导段屏蔽 +
1013
+ * 真实工具对替换后的 tool 引导段阴影)。 */
1014
+ function applyMinimalPrompt(a) {
1015
+ for (const disposer of a.promptDisposers) {
1016
+ try { disposer(); } catch {}
1017
+ }
1018
+ a.promptDisposers = [];
1019
+ if (!cfg.enabled || !cfg.minimalPrompt.enabled) return;
1020
+ const sp = a.agent.ctx.systemPrompt;
1021
+ try {
1022
+ if (cfg.minimalPrompt.suppressSections) {
1023
+ for (const [name, order] of SECTION_SHADOWS) {
1024
+ a.promptDisposers.push(sp.section({ name, order, text: '' }));
1025
+ }
1026
+ }
1027
+ if (a.realPairActive) {
1028
+ for (const [name, order] of TOOL_GUIDANCE_SHADOWS) {
1029
+ a.promptDisposers.push(sp.section({ name, order, text: '' }));
1030
+ }
1031
+ }
1032
+ const persona = cfg.minimalPrompt.persona.trim();
1033
+ if (persona.length > 0) {
1034
+ a.promptDisposers.push(sp.section({ name: PERSONA_SECTION_NAME, order: PERSONA_SECTION_ORDER, text: persona }));
1035
+ }
1036
+ info(`agent ${a.agent.id}: minimal prompt layer active`);
1037
+ } catch (error) {
1038
+ warn(`minimalPrompt failed for agent ${a.agent.id}: ${error && error.message || error}`);
1039
+ }
1040
+ }
1041
+
462
1042
  /** 应用/更新工具族限制(lean 模式)。总开关关闭、族被禁用、已升级、
463
- * 已从配置删除的族一律放行。 */
464
- function applyFamilies(a) {
1043
+ * 已从配置删除的族一律放行。 */ function applyFamilies(a) {
465
1044
  if (!cfg.enabled || !cfg.leanByDefault) {
466
1045
  for (const [id, disposer] of a.familyDisposers) {
467
1046
  try { disposer(); } catch {}
@@ -514,6 +1093,111 @@ export function apply(ctx, config) {
514
1093
  info(`agent ${a.agent.id}: escalated family "${familyId}" (${trigger})`);
515
1094
  }
516
1095
 
1096
+ /** 从持久事件恢复 dev_tool_search 已解锁的工具(resume-safe)。 */
1097
+ function loadUnlockedFromEvents(a) {
1098
+ const events = a.agent.session && Array.isArray(a.agent.session.events) ? a.agent.session.events : [];
1099
+ for (const event of events) {
1100
+ if (event === null || typeof event !== 'object' || event.type !== 'tool/call') continue;
1101
+ if (event.data === null || typeof event.data !== 'object' || event.data.name !== 'dev_tool_search') continue;
1102
+ let args = event.data.arguments;
1103
+ if (typeof args === 'string') {
1104
+ try { args = JSON.parse(args); } catch { continue; }
1105
+ }
1106
+ if (args === null || typeof args !== 'object' || Array.isArray(args)) continue;
1107
+ if (Array.isArray(args.toolNames)) {
1108
+ for (const name of args.toolNames) {
1109
+ if (typeof name === 'string' && name.length > 0) a.unlocked.add(name);
1110
+ }
1111
+ }
1112
+ }
1113
+ }
1114
+
1115
+ /**
1116
+ * 挂载/卸载真实 Minimal 工具对(0.5.0):把官方 minimal preset 同款插件
1117
+ * (持久 PTY bash + str_replace_editor 及其依赖的 terminals/本地 fs)
1118
+ * 挂进 agent 作用域。scoped registration 按名阴影 standard 的 sandboxed
1119
+ * bash,且自身层注册不受 restrict 影响(view() 对 own layer 豁免)——
1120
+ * 首轮目录因此是"真实工具对 + 被 deny 的其他继承工具"。
1121
+ * 依赖模块缺失(realPairModules === null)或 Windows(PTY 仅 linux/darwin)
1122
+ * 时静默降级:不挂载,退回旧行为(仅收窄目录)。
1123
+ */
1124
+ function syncRealPair(a) {
1125
+ for (const dispose of a.realPairDisposers) {
1126
+ try { dispose(); } catch {}
1127
+ }
1128
+ a.realPairDisposers = [];
1129
+ const active = cfg.enabled && cfg.bootstrap.enabled && cfg.bootstrap.realPair
1130
+ && realPairModules !== null && process.platform !== 'win32';
1131
+ if (active) {
1132
+ const mounts = realPairMounts(realPairModules, a.agent.session?.header?.cwd);
1133
+ a.realPairDisposers = mountRealPair(a.agent, mounts, warn);
1134
+ if (a.realPairDisposers.length > 0) {
1135
+ info(`agent ${a.agent.id}: real Minimal tool pair mounted (${mounts.length} plugins)`);
1136
+ }
1137
+ }
1138
+ a.realPairActive = a.realPairDisposers.length > 0;
1139
+ }
1140
+
1141
+ /**
1142
+ * 给目标 agent 注册 on-demand 发现工具(dsh-anchored-standard 的 resident
1143
+ * discovery pattern):晋升后常驻 bootstrap.discoveryTools 列出的工具——
1144
+ * dev_tool_search(搜索完整目录并按名解锁,解锁结果下一请求生效,写入
1145
+ * a.unlocked 后 syncBootstrap 重算 deny 集)+ skill_search / skill_load
1146
+ * (常驻上下文抑制的替代品:按需发现/加载技能)。
1147
+ * 已存在的同名工具(preset 自带)不重复注册。
1148
+ */
1149
+ function syncDiscoveryTools(a, enabled) {
1150
+ const want = enabled ? cfg.bootstrap.discoveryTools : [];
1151
+ for (const [name, disposer] of a.toolDisposers) {
1152
+ if (!want.includes(name)) {
1153
+ try { disposer(); } catch {}
1154
+ a.toolDisposers.delete(name);
1155
+ }
1156
+ }
1157
+ if (!enabled) return;
1158
+ const scopedTools = a.agent.ctx && a.agent.ctx.tools;
1159
+ if (scopedTools === void 0 || typeof scopedTools.register !== 'function') return;
1160
+ for (const name of want) {
1161
+ if (a.toolDisposers.has(name)) continue;
1162
+ if (a.visibleNames.has(name)) continue; // preset/宿主已提供同名工具
1163
+ const def = discoveryToolDef(a, name);
1164
+ if (def === null) continue;
1165
+ try {
1166
+ const disposer = scopedTools.register(def);
1167
+ if (typeof disposer === 'function') a.toolDisposers.set(name, disposer);
1168
+ } catch (error) {
1169
+ warn(`register ${name} failed for agent ${a.agent.id}: ${error && error.message || error}`);
1170
+ }
1171
+ }
1172
+ }
1173
+
1174
+ /** 构造一个发现工具定义;未知名称返回 null。 */
1175
+ function discoveryToolDef(a, name) {
1176
+ if (name === 'dev_tool_search') {
1177
+ return createDevToolSearch({
1178
+ schemasOf: (exec) => {
1179
+ const agent = exec !== null && typeof exec === 'object' ? exec.agent : void 0;
1180
+ return agent === void 0 ? [] : tools.schemas(agent);
1181
+ },
1182
+ onUnlock: async (names, exec) => {
1183
+ const agent = exec !== null && typeof exec === 'object' ? exec.agent : void 0;
1184
+ const state = agent === void 0 ? void 0 : agents.get(agent.id);
1185
+ if (state === void 0) return;
1186
+ for (const name of names) state.unlocked.add(name);
1187
+ syncBootstrap(state);
1188
+ },
1189
+ catalogHint: 'This session starts with a minimal resident set: bash, str_replace_editor, skill_search, skill_load. Everything else is unlocked on demand through this tool.',
1190
+ });
1191
+ }
1192
+ if (name === 'skill_search' && cfg.skillDiscovery) {
1193
+ return createSkillSearch({ skillsOf: () => ctx.get('skills') });
1194
+ }
1195
+ if (name === 'skill_load' && cfg.skillDiscovery) {
1196
+ return createSkillLoad({ skillsOf: () => ctx.get('skills') });
1197
+ }
1198
+ return null;
1199
+ }
1200
+
517
1201
  /** 完整接管一个 agent(幂等)。 */
518
1202
  function handleAgent(agent) {
519
1203
  if (agents.has(agent.id)) return;
@@ -521,11 +1205,16 @@ export function apply(ctx, config) {
521
1205
  const a = {
522
1206
  agent,
523
1207
  suppressDisposer: null,
1208
+ promptDisposers: [],
524
1209
  familyDisposers: new Map(),
525
1210
  bootstrapDisposer: null,
526
1211
  bootstrapKey: null,
527
1212
  escalated: new Set(),
528
1213
  visibleNames: new Set(),
1214
+ unlocked: new Set(),
1215
+ toolDisposers: new Map(),
1216
+ realPairDisposers: [],
1217
+ realPairActive: false,
529
1218
  };
530
1219
  // 记录当前可见目录(限制前);仅记录真实存在的工具,后续 restrict 与
531
1220
  // 实际目录取交集,preset 升级改名/删除工具不会让本插件崩溃。
@@ -538,8 +1227,11 @@ export function apply(ctx, config) {
538
1227
  } catch (error) {
539
1228
  warn(`schemas lookup failed for agent ${agent.id}: ${error && error.message || error}`);
540
1229
  }
1230
+ loadUnlockedFromEvents(a);
541
1231
  agents.set(agent.id, a);
542
1232
  applySuppression(a);
1233
+ syncRealPair(a);
1234
+ applyMinimalPrompt(a);
543
1235
  applyFamilies(a);
544
1236
  syncBootstrap(a);
545
1237
  info(`agent ${agent.id}: adaptive-perf active (preset ${String(agentPresets.composedPreset(agent.ctx))})`);
@@ -554,6 +1246,18 @@ export function apply(ctx, config) {
554
1246
  if (a.suppressDisposer !== null) {
555
1247
  try { a.suppressDisposer(); } catch {}
556
1248
  }
1249
+ for (const disposer of a.promptDisposers) {
1250
+ try { disposer(); } catch {}
1251
+ }
1252
+ a.promptDisposers = [];
1253
+ for (const disposer of a.toolDisposers.values()) {
1254
+ try { disposer(); } catch {}
1255
+ }
1256
+ a.toolDisposers.clear();
1257
+ for (const dispose of a.realPairDisposers) {
1258
+ try { dispose(); } catch {}
1259
+ }
1260
+ a.realPairDisposers = [];
557
1261
  if (a.bootstrapDisposer !== null) {
558
1262
  try { a.bootstrapDisposer(); } catch {}
559
1263
  }
@@ -644,8 +1348,12 @@ export function apply(ctx, config) {
644
1348
  }
645
1349
 
646
1350
  /**
647
- * bootstrap 阶段的保留工具集:bootstrap 工具对 + PTC 的直接调用工具
648
- * (run_code)+ compaction 后恢复期的工作集。已晋升返回 null。
1351
+ * bootstrap 阶段的保留工具集:
1352
+ * - 未晋升:bootstrap 工具对 + PTC 的直接调用工具(run_code)+
1353
+ * compaction 后恢复期的工作集;
1354
+ * - 已晋升:bootstrap 工具对 + resident discovery 工具 +
1355
+ * dev_tool_search 已解锁工具 + 已升级工具族(dsh-anchored-standard 的
1356
+ * resident catalog,避免晋升后一次性 dump 完整 Standard 目录)。
649
1357
  *
650
1358
  * 注意:不能靠过滤 system-prompt/assemble 的 assembly.tools 来实现——
651
1359
  * PTC 模式下 assembly.tools 只有 [run_code],Minimal 工具对不在其中,
@@ -654,17 +1362,32 @@ export function apply(ctx, config) {
654
1362
  * SDK 参考段,且天然保留 run_code(不在 deny 里)。
655
1363
  */
656
1364
  function bootstrapKeepSet(a) {
1365
+ if (!cfg.enabled || !cfg.bootstrap.enabled) return null;
1366
+ const session = a.agent.session;
1367
+ if (session === void 0) return null; // 无会话信息时保持既有行为(测试/未知场景)
657
1368
  const phase = bootstrapPhaseOf(a.agent);
658
- if (phase.promoted) return null;
659
1369
  const keep = new Set(cfg.bootstrap.tools);
660
1370
  if (a.visibleNames.has('run_code')) keep.add('run_code');
1371
+ if (phase.promoted) {
1372
+ for (const name of cfg.bootstrap.discoveryTools) keep.add(name);
1373
+ for (const name of a.unlocked) keep.add(name);
1374
+ for (const [id, family] of Object.entries(cfg.families)) {
1375
+ if (a.escalated.has(id)) {
1376
+ for (const name of familyDeny(a, family)) keep.add(name);
1377
+ }
1378
+ }
1379
+ return keep;
1380
+ }
661
1381
  if (phase.boundary >= 0) for (const name of cfg.bootstrap.compactionTools) keep.add(name);
662
1382
  return keep;
663
1383
  }
664
1384
 
665
1385
  /** 按当前阶段同步 bootstrap 的临时 restrict(幂等;仅阶段/配置变化时重挂)。 */
666
1386
  function syncBootstrap(a) {
1387
+ const phase = bootstrapPhaseOf(a.agent);
667
1388
  const keep = bootstrapKeepSet(a);
1389
+ // discovery 工具只在晋升后的 resident 阶段注册;bootstrap/compaction 阶段隐藏。
1390
+ syncDiscoveryTools(a, keep !== null && phase.promoted);
668
1391
  const key = keep === null ? null : [...keep].sort().join(',');
669
1392
  if (key === a.bootstrapKey) return;
670
1393
  if (a.bootstrapDisposer !== null) {
@@ -698,18 +1421,59 @@ export function apply(ctx, config) {
698
1421
  }
699
1422
  });
700
1423
 
701
- // 首轮剥离自动注入的上下文(技能目录提醒 / AGENTS.md 摘要)。prepend +
702
- // 根作用域注册保证是最后一道变换(后注册的注入者无法再补回)。
703
- ctx.on('agent/pre-step', async ({ agent }, next) => {
1424
+ // 上下文剥离 + instruction-hint。prepend + 根作用域注册保证是最后一道
1425
+ // 变换(后注册的注入者无法再补回)。两个职责同在一个监听器里,顺序天然
1426
+ // 正确:先剥离被抑制来源,再(晋升后一次性)追加指令文件提示。
1427
+ // 剥离与 bootstrap 开关正交:常驻抑制(suppressInjectedContext)在
1428
+ // bootstrap 关闭时也生效;instruction-hint 依赖晋升阶段(bootstrap 机制)。
1429
+ ctx.on('agent/pre-step', async ({ agent, signal }, next) => {
704
1430
  const decision = await next();
705
1431
  if (decision === null || typeof decision !== 'object' || decision.kind === 'reject') return decision;
706
1432
  try {
707
- if (!bootstrapActiveFor(agent)) return decision;
1433
+ if (!cfg.enabled || !isTarget(agent)) return decision;
708
1434
  const phase = bootstrapPhaseOf(agent);
709
- if (phase.promoted || cfg.bootstrap.suppressedContextSources.length === 0) return decision;
710
- if (!Array.isArray(decision.messages)) return decision;
711
- const kept = filterBootstrapMessages(decision.messages, new Set(cfg.bootstrap.suppressedContextSources));
712
- return kept === decision.messages ? decision : { ...decision, messages: kept };
1435
+ const suppressed = new Set(cfg.bootstrap.suppressedContextSources);
1436
+ let out = decision;
1437
+ // 剥离条件:bootstrap 未晋升(旧行为)或常驻抑制开启(0.5.0 默认:
1438
+ // 整个会话不再注入技能目录与 AGENTS.md 摘要,替代品是按需发现工具)。
1439
+ const stripActive = (cfg.bootstrap.enabled && !phase.promoted) || cfg.suppressInjectedContext;
1440
+ if (suppressed.size > 0 && stripActive && Array.isArray(out.messages)) {
1441
+ const kept = stripSuppressedMessages(out.messages, suppressed);
1442
+ out = kept === out.messages ? out : { ...out, messages: kept };
1443
+ }
1444
+ // instruction-hint:晋升后一次性注入"指令文件存在,需要时自读"。
1445
+ if (cfg.bootstrap.enabled && cfg.instructionHint && phase.promoted && Array.isArray(out.messages)) {
1446
+ const session = agent.session;
1447
+ if (session !== null && typeof session === 'object' && typeof session.id === 'string' && !hintedSessions.has(session.id)) {
1448
+ hintedSessions.add(session.id);
1449
+ const fs = ctx.get('fs');
1450
+ const dshHome = process.env.DSH_HOME?.trim() || `${process.env.HOME || process.env.USERPROFILE || ''}/.dsh`;
1451
+ const probe = await probeInstructionFiles(fs, session.header?.cwd, dshHome, signal);
1452
+ if (probe !== null) {
1453
+ const sections = [];
1454
+ if (probe.projectFiles.length > 0) {
1455
+ sections.push(`Workspace instruction files exist: ${probe.projectFiles.join(', ')} (project root: ${probe.root}).`);
1456
+ }
1457
+ if (probe.userGlobalFiles.length > 0) {
1458
+ sections.push(`A user-global instruction file exists: ${INSTRUCTION_USER_CANDIDATE}.`);
1459
+ }
1460
+ const text = [
1461
+ ...sections,
1462
+ 'Do NOT assume their content. When a task touches this workspace, read the relevant instruction files first and follow them.',
1463
+ ].join(' ');
1464
+ out = {
1465
+ ...out,
1466
+ messages: [...out.messages, {
1467
+ id: `instruction-hint-${session.id}`,
1468
+ role: 'user',
1469
+ content: [{ type: 'text', text }],
1470
+ source: { kind: 'instruction-hint', form: 'hint' },
1471
+ }],
1472
+ };
1473
+ }
1474
+ }
1475
+ }
1476
+ return out;
713
1477
  } catch (error) {
714
1478
  // 过滤失败绝不吞掉用户的上下文:原样放行。
715
1479
  warnBootstrapOnce(`bootstrap context filter failed, keeping injected context: ${String((error && error.message) || error)}`);
@@ -752,8 +1516,12 @@ export function apply(ctx, config) {
752
1516
  onUpdate: (merged) => {
753
1517
  cfg = normalizeConfig({ ...patchConfig, ...merged });
754
1518
  info('config hot-updated; recomputing restrictions for live agents');
1519
+ // realPair 从关闭切到开启:补加载官方模块(首次尝试失败不反复重试)。
1520
+ ensureRealPairModules();
755
1521
  // 补挂此前创建的会话(例如插件从 disabled 切换为 enabled,或新增了
756
- // 目标 preset):新符合条件的 agent 立即接管。
1522
+ // 目标 preset):新符合条件的 agent 立即接管(handleAgent 已应用全部
1523
+ // 层,下面的重算只针对此前已接管的 agent,避免同一 onUpdate 内重复)。
1524
+ const existing = new Set(agents.keys());
757
1525
  if (agentsService !== void 0 && typeof agentsService.list === 'function') {
758
1526
  try {
759
1527
  for (const agent of agentsService.list()) handleAgent(agent);
@@ -761,8 +1529,12 @@ export function apply(ctx, config) {
761
1529
  warn(`config-update agent sweep failed: ${error && error.message || error}`);
762
1530
  }
763
1531
  }
764
- for (const a of agents.values()) {
1532
+ for (const agentId of existing) {
1533
+ const a = agents.get(agentId);
1534
+ if (a === void 0) continue;
765
1535
  applySuppression(a);
1536
+ syncRealPair(a);
1537
+ applyMinimalPrompt(a);
766
1538
  applyFamilies(a);
767
1539
  syncBootstrap(a);
768
1540
  }