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,4 +1,4 @@
1
- // 删除 / 回收站族的 model 工具(2026-10-15):五个域各一条 `*_manager_remove`。
1
+ // 删除 / 回收站族的 model 工具:五个域各一条 `*_manager_remove`。
2
2
  //
3
3
  // 为什么单开一族:此前模型**只能停用、不能删除** —— "把这条过时的记忆删掉"这类要求手里
4
4
  // 没有出口,模型会去调 `*_switch(enabled:false)` 然后回一句"已停用",用户以为删了。面板上
@@ -6,29 +6,37 @@
6
6
  //
7
7
  // 三条纪律:
8
8
  // · **删除一律进回收站**(可恢复),与面板同一条路 —— 回执如实说"可恢复"。
9
- // · **不做 purge**(永久删除):低频且不可恢复,用户裁定"使用频率低的可以不要,
9
+ // · **不做 purge**(永久删除):低频且不可恢复,用户要求"使用频率低的可以不要,
10
10
  // 太多功能反而冗余" —— 清空回收站留给面板。
11
11
  // · `list` 是只读(不进场景冻结守卫,与 `trash-preview` 只读口径一致);`trash` /
12
12
  // `restore` 是写,过 `lockedSceneGuard()`。
13
13
  //
14
14
  // 参数名统一 `id`:`trash` 时是资源标识(记忆 `<场景>/<名>`、技能名、人设名、预设 id、
15
15
  // 场景名),`restore` 时是 `action=list` 给出的**回收站条目 id** —— 两种含义在描述里写清。
16
- import { locateSkillRoot, text } from './deps.js';
16
+ import { globalActionNote, locateSkillRoot, requireGlobalScope, scopeGlobalOnlyDesc, sessionLayerOn, text } from './deps.js';
17
17
  /** 三个动词共用的描述骨架。 */
18
18
  function removeDescription(noun) {
19
- return 'Delete one ' + noun + ' (moves it to the trash), restore one from the trash, or list the trash. '
20
- + 'Nothing here is permanent — emptying the trash is a panel action. '
19
+ return 'Delete one ' + noun + ' (to the trash), restore one, or list the trash. '
20
+ + 'Deletes are recoverable; this tool never empties the trash. '
21
21
  + 'Blocked while the active scene is locked. Only on the user\'s instruction.';
22
22
  }
23
23
  /** `action` 参数:三个动词 + 每个动词要哪个 `id`。 */
24
- const ACTION_DESC = 'trash = delete the item named in `id` (moves it to the trash); restore = put back the trash entry `id` (get its id from action=list); list = list the trash (no `id`).';
24
+ const ACTION_DESC = 'trash = delete the item named by `id`; restore = put back a trash entry id (from list); list = list the trash (no `id`).';
25
+ /** 删除只有全局一份(会话层没有可删的东西),四个域同一句话。 */
26
+ const REMOVE_SCOPE_DESC = scopeGlobalOnlyDesc('one machine-wide copy', 'switch it off here instead');
27
+ /**
28
+ * 场景的变体:`REMOVE_SCOPE_DESC` 让模型「在本会话里把它关掉」,
29
+ * 而场景根本没有「关掉」这个动作 —— `scene_manager_switch` 只有 enter / exit。照抄那一句
30
+ * 会让模型去找一个不存在的开关,或者干脆回一句「做不到」。
31
+ */
32
+ const REMOVE_SCOPE_DESC_SCENE = scopeGlobalOnlyDesc('the scene record and archive are machine-wide', 'exit it here with scene_manager_switch (action=exit)');
25
33
  /**
26
34
  * 恢复 op 回传的**最终落盘名**。五个域把名字放在不同字段上:
27
35
  * · 记忆 / 技能 / 人设 / 场景 → `name`(退号恢复时它是新名字,不是回收站里的原名)
28
36
  * · 提示词预设 → `id`(预设的 id 就是它的名字)
29
37
  * 记忆还会把投影对象放在 `rule.id` 上,作为第三顺位兜底。
30
38
  *
31
- * 还要解一层 `data`(2026-10-15 复核 P1-6 时漏掉的那半):技能的 op 表外面包了
39
+ * 还要解一层 `data`(漏掉的那半):技能的 op 表外面包了
32
40
  * `wrap`(`skills/service.ts:239` —— `{ ok: true, data: result }`),名字在 `r.data.name`,
33
41
  * 不在 `r.name` 上。其余四个域的 op 是扁平返回。两边都要看,否则技能域的回执仍会退回 trashId。
34
42
  */
@@ -46,8 +54,15 @@ function restoredName(r) {
46
54
  };
47
55
  return pick(r) || pick(r && r.data);
48
56
  }
49
- /** 一条删除工具的 execute:三个动词的分派与回执。 */
50
- async function runRemove(deps, cfg, args) {
57
+ /**
58
+ * 一条删除工具的 execute:三个动词的分派与回执。
59
+ *
60
+ * 会话层:删除是**全局**动作 —— 技能正文、人设 `.md`、场景档案、MCP 补丁
61
+ * 提示词预设都只有一份,没有「本会话的副本」,所以这一格的口径就是「全局动作」。
62
+ * 所以这里不写会话层,只在**这个对话真有该域覆盖**时补一句尾注:
63
+ * 删完之后那条会话覆盖还在(读时忽略,对账才清),不说明的话用户会以为它跟着没了。
64
+ */
65
+ async function runRemove(deps, cfg, args, agent) {
51
66
  const action = String((args && args.action) || '');
52
67
  if (action !== 'trash' && action !== 'restore' && action !== 'list') {
53
68
  throw new Error('action must be trash / restore / list (got: ' + (action || '(empty)') + ')');
@@ -62,6 +77,10 @@ async function runRemove(deps, cfg, args) {
62
77
  const blocked = await deps.lockedSceneGuard();
63
78
  if (blocked)
64
79
  throw new Error(blocked);
80
+ // 会话层:`scope: "session"` 是模型**以为**自己能删本会话那一份时的错误
81
+ // 当场纠回来。省略 / "global" 照旧走全局。五条删除都是全局动作 ——
82
+ // 连提示词那一族也一样(提示词根本不进会话域,更没有「本会话的预设」可删)。
83
+ requireGlobalScope(args, cfg.noun + 's', !sessionLayerOn(deps));
65
84
  const id = String((args && args.id) || '').trim();
66
85
  if (id === '')
67
86
  throw new Error('missing argument: id (required by action=' + action + ')');
@@ -69,15 +88,17 @@ async function runRemove(deps, cfg, args) {
69
88
  if (!r || r.ok === false)
70
89
  throw new Error((r && r.error) || (action === 'trash' ? 'failed to move it to the trash' : 'restore failed'));
71
90
  if (action === 'trash') {
72
- return 'OK: ' + cfg.label + ' "' + id + '" moved to the trash (recoverable; permanent deletion lives in the panel trash)';
91
+ return 'OK: ' + cfg.label + ' "' + id + '" moved to the trash (recoverable; permanent deletion lives in the panel trash)' +
92
+ (cfg.domain === undefined ? '' : globalActionNote(deps, agent, cfg.domain, cfg.label));
73
93
  }
74
- // 恢复的回执要说**恢复成了什么**,而不是回收站条目 id(2026-10-15 smoke-test P1-6):
94
+ // 恢复的回执要说**恢复成了什么**,而不是回收站条目 id:
75
95
  // `id` 是 `action=list` 给的那一串(记忆 `mv…`、技能 `<时间戳>-<uuid>`),模型看到它
76
96
  // 认不出恢复了哪一条;退号恢复时最终落盘的名字还与回收站里的原名不同,更要说准。
77
97
  const name = restoredName(r) || id;
78
98
  return 'OK: restored ' + cfg.label + ' "' + name + '" from the trash' +
79
99
  (name === id ? '' : ' (trash entry ' + id + ')');
80
100
  }
101
+ /** 注册删除类工具:技能 / 人设 / 场景三种对象各一个动词。 */
81
102
  export function buildRemoveTools(deps) {
82
103
  const { defineTool, register } = deps;
83
104
  const mem = deps.memoryOps;
@@ -86,7 +107,7 @@ export function buildRemoveTools(deps) {
86
107
  const pr = deps.promptOps;
87
108
  const sc = deps.sceneOps;
88
109
  const memoryCfg = {
89
- noun: 'memory', label: 'memory',
110
+ noun: 'memory', label: 'memory', domain: 'memory',
90
111
  trash: (a) => mem['rules-remove']({ id: String(a.id || '') }),
91
112
  restore: (a) => mem['rules-restore']({ trashId: String(a.id || '') }),
92
113
  list: async () => {
@@ -97,8 +118,8 @@ export function buildRemoveTools(deps) {
97
118
  },
98
119
  };
99
120
  const skillCfg = {
100
- noun: 'skill', label: 'skill',
101
- // 与 `skill_manager_switch` 同一条(smoke-test P0-1):不给 root 就按名字在全部来源里
121
+ noun: 'skill', label: 'skill', domain: 'skills',
122
+ // 与 `skill_manager_switch` 同一条:不给 root 就按名字在全部来源里
102
123
  // 找胜出的那一份。默认值原本是 dsh,而 `skill_manager_save` 落 hub —— 「建完就删」
103
124
  // 必然报「技能不存在」。删一个真的不存在的技能时退回 dsh,交给 op 报它自己的错。
104
125
  trash: async (a) => {
@@ -117,7 +138,7 @@ export function buildRemoveTools(deps) {
117
138
  },
118
139
  };
119
140
  const subagentCfg = {
120
- noun: 'persona', label: 'persona',
141
+ noun: 'persona', label: 'persona', domain: 'subagents',
121
142
  trash: (a) => sub['subagent-delete']({ name: String(a.id || '') }),
122
143
  restore: (a) => sub['subagent-trash-restore']({ id: String(a.id || '') }),
123
144
  list: async () => {
@@ -140,7 +161,7 @@ export function buildRemoveTools(deps) {
140
161
  },
141
162
  };
142
163
  const sceneCfg = {
143
- noun: 'scene', label: 'scene',
164
+ noun: 'scene', label: 'scene', domain: 'scene',
144
165
  // 删场景走 handlers 里那份(`withAgentsMdSync` 包过):直调 `memoriesService.ops` 会漏掉
145
166
  // 「场景绑的预设曾写进 AGENTS.md,删了要跟着处理」那一步。
146
167
  trash: (a) => deps.sceneRemove({ name: String(a.id || '') }),
@@ -157,50 +178,55 @@ export function buildRemoveTools(deps) {
157
178
  description: removeDescription('memory'),
158
179
  parameters: {
159
180
  action: { type: 'string', required: true, enum: ['trash', 'restore', 'list'], description: ACTION_DESC },
160
- id: { type: 'string', description: 'For trash: the memory id `<scene>/<name>`. For restore: the trash entry id from action=list. Not needed for list.' },
181
+ id: { type: 'string', description: 'For trash: the memory id `<scene>/<name>`; for restore: a trash entry id; omit for list.' },
182
+ scope: { type: 'string', enum: ['session', 'global'], description: REMOVE_SCOPE_DESC },
161
183
  },
162
184
  output: { schema: { type: 'string' }, render: (_a, v) => text(String(v)) },
163
- async execute(args) { return runRemove(deps, memoryCfg, args); },
185
+ async execute(args, exec) { return runRemove(deps, memoryCfg, args, exec && exec.agent); },
164
186
  }));
165
187
  register(defineTool({
166
188
  name: 'skill_manager_remove',
167
189
  description: removeDescription('skill'),
168
190
  parameters: {
169
191
  action: { type: 'string', required: true, enum: ['trash', 'restore', 'list'], description: ACTION_DESC },
170
- id: { type: 'string', description: 'For trash: the skill name. For restore: the trash entry id from action=list. Not needed for list.' },
192
+ id: { type: 'string', description: 'For trash: the skill name; for restore: a trash entry id; omit for list.' },
193
+ scope: { type: 'string', enum: ['session', 'global'], description: REMOVE_SCOPE_DESC },
171
194
  root: { type: 'string', description: 'Source root the skill lives in. Default: whichever source holds that skill; only used by action=trash.' },
172
195
  },
173
196
  output: { schema: { type: 'string' }, render: (_a, v) => text(String(v)) },
174
- async execute(args) { return runRemove(deps, skillCfg, args); },
197
+ async execute(args, exec) { return runRemove(deps, skillCfg, args, exec && exec.agent); },
175
198
  }));
176
199
  register(defineTool({
177
200
  name: 'subagent_manager_remove',
178
201
  description: removeDescription('persona'),
179
202
  parameters: {
180
203
  action: { type: 'string', required: true, enum: ['trash', 'restore', 'list'], description: ACTION_DESC },
181
- id: { type: 'string', description: 'For trash: the persona name. For restore: the trash entry id from action=list. Not needed for list.' },
204
+ id: { type: 'string', description: 'For trash: the persona name; for restore: a trash entry id; omit for list.' },
205
+ scope: { type: 'string', enum: ['session', 'global'], description: REMOVE_SCOPE_DESC },
182
206
  },
183
207
  output: { schema: { type: 'string' }, render: (_a, v) => text(String(v)) },
184
- async execute(args) { return runRemove(deps, subagentCfg, args); },
208
+ async execute(args, exec) { return runRemove(deps, subagentCfg, args, exec && exec.agent); },
185
209
  }));
186
210
  register(defineTool({
187
211
  name: 'prompt_manager_remove',
188
212
  description: removeDescription('AGENTS.md preset'),
189
213
  parameters: {
190
214
  action: { type: 'string', required: true, enum: ['trash', 'restore', 'list'], description: ACTION_DESC },
191
- id: { type: 'string', description: 'For trash: the preset id. For restore: the trash entry id from action=list. Not needed for list.' },
215
+ id: { type: 'string', description: 'For trash: the preset id; for restore: a trash entry id; omit for list.' },
216
+ scope: { type: 'string', enum: ['session', 'global'], description: scopeGlobalOnlyDesc('a preset writes the one global baseline', 'there is nothing per-conversation to delete') },
192
217
  },
193
218
  output: { schema: { type: 'string' }, render: (_a, v) => text(String(v)) },
194
- async execute(args) { return runRemove(deps, promptCfg, args); },
219
+ async execute(args, exec) { return runRemove(deps, promptCfg, args, exec && exec.agent); },
195
220
  }));
196
221
  register(defineTool({
197
222
  name: 'scene_manager_remove',
198
223
  description: removeDescription('scene'),
199
224
  parameters: {
200
225
  action: { type: 'string', required: true, enum: ['trash', 'restore', 'list'], description: ACTION_DESC },
201
- id: { type: 'string', description: 'For trash: the scene name. For restore: the trash entry id from action=list. Not needed for list.' },
226
+ id: { type: 'string', description: 'For trash: the scene name; for restore: a trash entry id; omit for list.' },
227
+ scope: { type: 'string', enum: ['session', 'global'], description: REMOVE_SCOPE_DESC_SCENE },
202
228
  },
203
229
  output: { schema: { type: 'string' }, render: (_a, v) => text(String(v)) },
204
- async execute(args) { return runRemove(deps, sceneCfg, args); },
230
+ async execute(args, exec) { return runRemove(deps, sceneCfg, args, exec && exec.agent); },
205
231
  }));
206
232
  }
@@ -1,4 +1,4 @@
1
- // 场景域的 model 工具(2026-09-23 从记忆族分出来;同日补上 `_switch` 与 `_list`):
1
+ // 场景域的 model 工具(从记忆族分出来;同日补上 `_switch` 与 `_list`):
2
2
  // `scene_manager_list` → `_switch` → `_save`。**注册顺序就是兼容页「模型工具表」的显示顺序**
3
3
  // (`tools/table.ts` 刻意不定义第二份排序),这一族按其余四族同形的排法排:先查、再拨开关、
4
4
  // 最后写定义 —— 场景族补上这两条时是照"先建后切"的语义顺序注册的,成了五族里唯一的例外。
@@ -8,9 +8,9 @@
8
8
  // 预算)。合成一条工具会让 `description` 变成双义参数、门禁按参数分叉,而这两件事的失败
9
9
  // 形状都是静默的。
10
10
  //
11
- // 为什么有 `scene_manager_list`(2026-09-23 用户裁定:「记忆就管记忆,场景就管场景」):
11
+ // 为什么有 `scene_manager_list`:
12
12
  // 场景清单此前挂在 `memory_manager_list` 的输出末尾(它最初解释的是"这条记忆为什么没被注入",
13
- // 0.14.x 那次把档案也并进去时明确写过"不加新工具、不加新 op")。省下那 106 tok/轮的代价是
13
+ // 后来把档案也并了进去,为的是不加新工具、不加新 op)。省下那 106 tok/轮的代价是
14
14
  // **引用方向反了** —— 场景族两条工具都要模型去调记忆族的工具看现状,而工具表可以单独关掉
15
15
  // `memory_manager_list`(`tools/table.ts`),关掉后 `_switch` 描述里那句"照抄它打印的场景名"
16
16
  // 就悬空了。六个注入域里也只有场景没有自己的 list。
@@ -20,14 +20,14 @@
20
20
  // 写侧为什么是两条、而不是一条带 `action` 的:
21
21
  // · `_save` 只写**定义**(场景记录 + 档案),不改变现在;
22
22
  // · `_switch` 改的是**现在**(进入 = 六处开关一起动 + 收窄注入 + 改写 AGENTS.md)。
23
- // 危险度差一档,回执要说的事也完全不同。0.14.0 当初把「启用与进入」整个留在界面,
23
+ // 危险度差一档,回执要说的事也完全不同。当初把「启用与进入」整个留在界面,
24
24
  // 理由是"那是改运行时环境的动作";补上 `_switch` 的取舍记在 CHANGELOG 里 —— 要点是
25
25
  // **场景段的动作句仍然不加回来**:「上下文注入只给现在是什么」是同一天定下的原则,
26
26
  // 不因为多了一条工具就破例(工具描述本来就是写"什么时候该用它"的地方)。
27
- import { text, toolNameList } from './deps.js';
27
+ import { effectiveScope, globalUnchangedNote, isolationOffNote, requireGlobalScope, scopeGlobalOnlyDesc, scopeOnlyNote, scopeSessionDesc, sessionLayerOn, sessionScopeOf, text, toolNameList } from './deps.js';
28
28
  import { FACTORY_TOOL_TABLE_PRESET } from './table.js';
29
29
  /** 档案里由模型设置的段(`memories` 段在 P5 已废弃,不再是档案的一部分)。
30
- * `mcpNotes`(0.20.0)与其余三段同族:整段替换、段缺席 = 不碰这一域。 */
30
+ * `mcpNotes`与其余三段同族:整段替换、段缺席 = 不碰这一域。 */
31
31
  const ARCHIVE_SECTIONS = ['mcp', 'mcpNotes', 'skills', 'subagents'];
32
32
  /** 段里的名单:数组段是名字列表,`mcp` 段是 serverName → `'*'` | 工具名数组。段缺席 = 空。 */
33
33
  function segmentNames(value) {
@@ -79,16 +79,16 @@ function archiveText(archive) {
79
79
  const notes = keys(archive.mcpNotes);
80
80
  if (notes.length)
81
81
  parts.push('notes ' + toolNameList(notes));
82
- // 模型工具表方案(0.15.0 起档案里有这一栏):进入时换的是**模型自己能用什么**,不报就是
82
+ // 模型工具表方案(档案里有这一栏):进入时换的是**模型自己能用什么**,不报就是
83
83
  // 让它事后发现自己少了工具却说不出原因。只给方案名 —— 关停名单不在档案里,硬算要多读一次
84
84
  // 方案清单,而真正需要条数的时机是切换之后(那条回执有)。没绑就不冒这一格(与备注同口径)。
85
85
  const tt = toolTableLabel(archive.toolTablePreset);
86
86
  if (tt)
87
87
  parts.push('tool-table preset "' + tt + '"');
88
- // 快捷提示词绑定(0.20.0):进了这个场景会动到 `/` 菜单里哪些条目,不报就只能靠猜。
88
+ // 快捷提示词绑定:进了这个场景会动到 `/` 菜单里哪些条目,不报就只能靠猜。
89
89
  // 空表是"进入后全部停用",与"没绑"(这一域不碰)是两句相反的话,不能压成一个 "0 条"。
90
90
  //
91
- // 印 **id 而不是条数**(2026-10-15):`scene_manager_save.quickPrompts` 的描述让模型
91
+ // 印 **id 而不是条数**:`scene_manager_save.quickPrompts` 的描述让模型
92
92
  // "copy them from a scene binding in the listing" —— 而此前这里只印「N 条」,清单里根本
93
93
  // 没有 id 可抄,那句是一条**指不到的死路**(模型照做只会白跑一趟,然后去问用户)。
94
94
  // 与 `skills` / `subagents` 两段同款(那两段本来就是 `toolNameList` 印名字)。
@@ -128,18 +128,38 @@ function segmentDiff(key, beforeValue, afterValue) {
128
128
  *
129
129
  * 探测不到 / 什么都没做(`unchanged`)就不占行:那两种情况下文件确实没变,
130
130
  * 说一句"没变"只是噪音。
131
+ *
132
+ * `baselineError`:切进去了**但退不回来** —— 场景提示词已写进 AGENTS.md
133
+ * 而"进场景之前那份基线"没能落盘。此前这一支在客户端有(45-scenes.js 的
134
+ * `scenes.agents.note.baselineLost`)、工具回执里没有,于是模型侧唯一能看到的版本是
135
+ * "切成功了"。它比 `error` 更该说:文件已经改了,只是没有退路。
136
+ *
137
+ * `snapshotMissing`:**模式快照丢了**,退出场景只能落盘模式位、运行时
138
+ * (MCP / 技能 / 人设 / 工具表)回不来。客户端有计划卡上的同款文案
139
+ * (`scenes.exit.noSnapshot`),工具回执此前同样没有 —— 模型只能看到"已退出"。
131
140
  */
132
- function agentsMdNote(sync) {
141
+ function agentsMdNote(sync, action) {
133
142
  if (!sync || typeof sync !== 'object')
134
143
  return '';
144
+ const notes = [];
135
145
  if (sync.error)
136
- return '\nWARN: the global baseline (AGENTS.md) was not synced: ' + String(sync.error);
146
+ notes.push('\nWARN: the global baseline (AGENTS.md) was not synced: ' + String(sync.error));
137
147
  if (sync.applied)
138
- return '\nAGENTS.md <- the scene-bound preset "' + String(sync.applied) + '"';
148
+ notes.push('\nAGENTS.md <- the scene-bound preset "' + String(sync.applied) + '"');
139
149
  if (sync.restored === true)
140
- return '\nAGENTS.md <- restored the pre-scene baseline';
141
- return '';
150
+ notes.push('\nAGENTS.md <- restored the pre-scene baseline');
151
+ if (sync.baselineError) {
152
+ notes.push('\nWARN: AGENTS.md was switched, but the pre-scene baseline could not be saved ('
153
+ + String(sync.baselineError) + ') — leaving this scene will NOT restore your previous global prompt.'
154
+ + ' The multi-generation backups under __last-applied__/AGENTS.md.bak-* still hold earlier copies.');
155
+ }
156
+ if (action === 'exit' && sync.snapshotMissing === true) {
157
+ notes.push('\nWARN: no mode snapshot was recorded for the scene that was left — the runtime'
158
+ + ' (MCP servers / skills / personas / tool table) was NOT rolled back; only the mode bit was cleared.');
159
+ }
160
+ return notes.join('');
142
161
  }
162
+ /** 注册场景域的 model 工具(列表 / 进入 / 保存 / 删除)。 */
143
163
  export function buildSceneTools(deps) {
144
164
  const { defineTool, register } = deps;
145
165
  register(defineTool({
@@ -150,10 +170,16 @@ export function buildSceneTools(deps) {
150
170
  // 默认的隐藏名单里):静态文本没法按 toolVisible 改写,点名一条模型没有的工具只会让它
151
171
  // 白跑一趟。要说"去哪看"就用中性说法(the scene listing / the "Current scene" reminder
152
172
  // —— 后者是常驻源,只要场景域开着就在)。点名只留在**运行时回执**里、按可见性分叉。
153
- description: 'List every scene with its archive (what entering it switches on: MCP servers, skills, personas, quick prompts, and the bound model tool-table scheme when there is one), bound prompt preset, memory count and enabled / entered / locked state. The scene currently entered is also shown in the "Current scene" reminder.',
173
+ description: 'List every scene with its archive (what entering it switches on: MCP servers, skills, personas, quick prompts, and the bound tool-table scheme), bound prompt preset, memory count and enabled / entered / locked state. The scene currently entered is also shown in the "Current scene" reminder.',
154
174
  parameters: {},
155
175
  output: { schema: { type: 'string' }, render: (_a, v) => text(v) },
156
- async execute() {
176
+ async execute(_args, exec) {
177
+ // 会话层(`scene_manager_list` 标注本会话与全局默认):
178
+ // 列表里的 on/off 是**全局**的启用集合;本会话进入的场景是另一件事(只改这个对话
179
+ // 看到什么)。两者都不说清,模型会把「全局进入的是 A」读成「这个对话看到的也是 A」。
180
+ const scoped = sessionScopeOf(deps, exec && exec.agent);
181
+ const pinned = scoped?.scope?.scene;
182
+ const detachedHere = scoped?.scope?.detached === true;
157
183
  const r = await deps.rulesOps['rules-list']({});
158
184
  if (!r || r.ok === false)
159
185
  throw new Error((r && r.error) || 'failed to read scenes');
@@ -185,6 +211,10 @@ export function buildSceneTools(deps) {
185
211
  // 模型会把"启用着"读成"现在就是它" —— 而记忆与提示词只按**进入**的那一个注入。
186
212
  if (entered !== null && name === entered)
187
213
  state.push('entered');
214
+ // 本会话进入的那个场景:与 `entered` 是两个不同的东西 —— 前者只改
215
+ // 这个对话看到什么,后者真的切了运行时。同一个名字两个标,不冲突。
216
+ if (pinned !== undefined && name === pinned)
217
+ state.push('entered-in-this-conversation');
188
218
  if (s.locked === true)
189
219
  state.push('locked');
190
220
  // 打**场景名**而不是 label:`scene_manager_save` / `_switch` 要的都是名字(单个
@@ -198,10 +228,17 @@ export function buildSceneTools(deps) {
198
228
  bits.push('archive: ' + (archives === null ? 'unreadable (does not mean absent)' : archiveText(archives[name])));
199
229
  return bits.join(' · ');
200
230
  });
231
+ // 会话层:这一层进入的场景只改这一层的视图,不切运行时 —— 这一行必须与
232
+ // `entered` 分开报。退出场景之后这一层不回全局,而是回到创建时的样子。
233
+ const sessionLine = pinned !== undefined
234
+ ? '\nthe view here is scene "' + pinned + '" (its own view: the machine-wide runtime was not switched and AGENTS.md was not written).'
235
+ : (detachedHere
236
+ ? '\nthe scene was left here: this scope shows what it looked like when it was created (its own view; the machine-wide runtime was not touched).'
237
+ : '');
201
238
  return 'scenes (' + scenes.length + ' · ' +
202
239
  (r.activeMode === 'all' ? 'the enabled set is the historical default (all enabled)' : 'the enabled set is narrowed to a single scene') +
203
240
  (entered === null ? '; no scene is entered right now' : '; currently entered: "' + entered + '"') + '):\n' +
204
- (lines.join('\n') || '(no scenes)');
241
+ (lines.join('\n') || '(no scenes)') + sessionLine;
205
242
  },
206
243
  }));
207
244
  register(defineTool({
@@ -212,10 +249,14 @@ export function buildSceneTools(deps) {
212
249
  description: 'Enter a scene, or leave the one currently entered. Entering switches on its archive and everything else off, narrows injection to its memories, and applies its bound prompt preset to AGENTS.md. Leaving restores the runtime snapshot taken on entry. Only when the user asks or approves.',
213
250
  parameters: {
214
251
  action: { type: 'string', required: true, enum: ['enter', 'exit'], description: 'enter = switch to the scene named in `scene`; exit = leave the current one (no scene needed).' },
215
- scene: { type: 'string', description: 'Scene name (one path segment), required for enter. Use the exact name, not the display label — the scene listing and the "Current scene" reminder both print names.' },
252
+ scene: { type: 'string', description: 'Scene name (one path segment), required for enter — the exact name, not the display label.' },
253
+ // 默认只改**这个对话看到什么**,不切运行时、不写 AGENTS.md。
254
+ // 进入时可能让场景勾中的 MCP 服务器 / 技能来源按租约临时运行(连接与来源启用是
255
+ // 进程级的);那一步只在回执里出现,别在参数里长篇解释。
256
+ scope: { type: 'string', enum: ['session', 'global'], description: scopeSessionDesc('the scene is entered for that scope only: no runtime switch, AGENTS.md untouched (its MCP servers may keep running for this scope). global = the machine-wide current scene: switches the runtime and applies the bound prompt to AGENTS.md.') },
216
257
  },
217
258
  output: { schema: { type: 'string' }, render: (_a, v) => text(v) },
218
- async execute(args) {
259
+ async execute(args, exec) {
219
260
  const action = String((args && args.action) || '');
220
261
  if (action !== 'enter' && action !== 'exit') {
221
262
  throw new Error('action must be enter / exit (got: ' + (action || '(empty)') + ')');
@@ -223,6 +264,34 @@ export function buildSceneTools(deps) {
223
264
  // 刻意**不过** `lockedSceneGuard()`:场景**自身**的启停不在冻结清单里(冻结的是五个
224
265
  // 域的内容)。锁定中的场景正被进入时不能退出 —— 那条守卫在 `scene-mode-set` 的包装里
225
266
  // (index.ts),它以 ok:false 返回,下面如实抛出去。
267
+ // 会话层进出场景**不切全局运行时** ——
268
+ // 不调 `scene-mode-set`、不调 `rules-set-active`、不写 AGENTS.md。它把场景作为本会话的
269
+ // 一层记下来:档案里勾了的在其生效集合内、没勾的在本会话关掉,记忆 / 场景段 / 目录按那个
270
+ // 场景投影。两个例外要如实说:进入时会把场景勾中的 MCP 服务器 / 技能来源在**全局**拉起
271
+ // (连接与来源启用是进程级的,没有会话版本),那一步的影响由回执里的 `warnings` 带出来。
272
+ // 全局粒度下没有"本会话的场景"这回事 —— 进 / 出就是切机器级运行时,走下面那条路。
273
+ if (effectiveScope(deps, args) === 'session') {
274
+ if (!deps.sessionScopeSet)
275
+ throw new Error('per-conversation scenes are unavailable in this host: pass scope="global" to switch the machine-wide scene');
276
+ let wanted = null;
277
+ if (action === 'enter') {
278
+ wanted = String((args && args.scene) || '').trim();
279
+ if (wanted === '')
280
+ throw new Error('action=enter needs scene: the scene name (copy it from the scene_manager_list line)');
281
+ if (wanted.indexOf('/') >= 0 || wanted.indexOf('\\') >= 0)
282
+ throw new Error('a scene name is a single path segment (no / or \\): ' + wanted);
283
+ }
284
+ // `scope-set` 的 scene 分支读的是 `value`:场景名 = 进入,null = 退出(下层换成基线)。
285
+ const sc = await deps.sessionScopeSet(exec && exec.agent, 'scene', '', wanted);
286
+ if (!sc || sc.ok === false)
287
+ throw new Error((sc && sc.error) || 'failed to switch the scene here');
288
+ const warnings = sc.warnings !== undefined && sc.warnings.length > 0 ? '\n' + sc.warnings.join('\n') : '';
289
+ return action === 'enter'
290
+ ? 'OK: scene "' + wanted + '" is now entered' + scopeOnlyNote(deps) + ' (its own view)' +
291
+ '. The machine-wide runtime did not switch and AGENTS.md was not written. ' + globalUnchangedNote(deps) + warnings +
292
+ ' Pass scope="global" to enter it for real.'
293
+ : 'OK: left the scene' + scopeOnlyNote(deps) + ' — back to how it looked when it was created (its own view). The runtime was not touched. ' + globalUnchangedNote(deps);
294
+ }
226
295
  let scene = '';
227
296
  if (action === 'enter') {
228
297
  scene = String((args && args.scene) || '').trim();
@@ -288,60 +357,81 @@ export function buildSceneTools(deps) {
288
357
  if (Array.isArray(mode.stale) && mode.stale.length) {
289
358
  parts.push('dropped keys this machine does not have: ' + mode.stale.join(', '));
290
359
  }
291
- return head + (parts.length ? ' · ' + parts.join(' · ') : '') + agentsMdNote(act.agentsMd) +
292
- (noop ? '\n(' + (action === 'enter' ? 'already in the target scene' : 'already outside every scene') + ' — runtime unchanged)' : '');
360
+ // `snapshotMissing` 从模式侧带过来:`act` 是启用集合那次调用的产物,本身没有它,
361
+ // 而"退出时快照丢了"这件事只有模式引擎知道(见 archive-engine.ts 的 scene-mode-set)。
362
+ // 进 / 退两个方向都报 —— 进场景时它说的是"你即将进入一个退不出来的场景"。
363
+ const snapshotMissing = mode && mode.exit && mode.exit.snapshotMissing === true;
364
+ // 会话层:全局切了场景,但**本会话**若钉着别的场景,这里看到的东西不会变。
365
+ // 不点这一句,模型会以为「切了场景 = 我这边也切了」,然后拿错误的场景解释上下文。
366
+ const pinned = sessionScopeOf(deps, exec && exec.agent)?.scope?.scene;
367
+ const pinnedNote = pinned === undefined
368
+ ? ''
369
+ : '\nnote: this scope pins scene "' + pinned + '" for its own view, so what it sees did not change. Clear that pin (scene_manager_switch with scope="session", action="exit") to follow this one.';
370
+ // 全局分支。全局粒度下 askedForSession 也走这里 —— 点明"隔离关着,所以这就是全局"。
371
+ const askedForSession = (args && args.scope) !== 'global';
372
+ return head + (parts.length ? ' · ' + parts.join(' · ') : '')
373
+ + agentsMdNote(act.agentsMd, snapshotMissing ? action : undefined) +
374
+ (noop ? '\n(' + (action === 'enter' ? 'already in the target scene' : 'already outside every scene') + ' — runtime unchanged)' : '') +
375
+ pinnedNote +
376
+ (askedForSession ? isolationOffNote(deps) : '');
293
377
  },
294
378
  }));
295
379
  register(defineTool({
296
380
  name: 'scene_manager_save',
297
381
  // 「写入指引」保留("写成指令而不是备注"—— 这是场景说明质量的杠杆),但不再声称
298
- // "注入后模型会被要求照办":2026-09-23 定稿把场景段的授权语删了(注入只陈述现状),
382
+ // "注入后模型会被要求照办":定稿把场景段的授权语删了(注入只陈述现状),
299
383
  // 参数说明与注入侧各说各话时,模型信的那份是错的。改为如实说明去向。
300
- description: 'Save a scene — a named bundle the user enters: entering it switches on exactly what its archive lists (MCP servers, scene-level MCP note overrides, skills, personas, quick prompts, and the model tool table when a scheme is bound) and everything else off. Creates it if absent, else updates it. An archive section you give replaces that section whole — include what should stay; the receipt reports what got displaced. Saving does not enter it; entering is a separate action. Only on the user\'s instruction.',
384
+ // 描述不再逐项列举档案里有哪些段(参数表本身就是那张清单,模型读参数时两处都在手上):
385
+ // 这里只留参数表**答不了**的三件事 —— 场景是什么、整段替换的语义、保存不等于进入。
386
+ description: 'Save a scene — a named bundle the user enters: entering switches on exactly what its archive lists and everything else off. Creates it if absent. Omitted fields keep their values; an archive section you give replaces that section whole (include what should stay — the receipt reports what got displaced). Saving does not enter it. Only on the user\'s instruction.',
301
387
  parameters: {
302
388
  scene: { type: 'string', required: true, description: 'Scene name (one path segment, ≤64 chars, no / \\ or < > : " | ? *, not `_shared`). Created if absent.' },
303
- label: { type: 'string', description: 'Display name for this scene. Omit = keep.' },
304
- description: { type: 'string', description: 'What this scene is for; shown beside the scene name in the "Current scene" reminder while the scene is on — write an instruction, not a note. Omit = keep.' },
305
- // 与下面 `mcp` / `skills` / `subagents` 同一句式("Omit = keep; X = 一个都不"):
306
- // 界面 2026-10-10 补了「无」这个选项,工具侧的空串语义要跟着说清,否则模型只知道
389
+ label: { type: 'string', description: 'Display name for this scene.' },
390
+ description: { type: 'string', description: 'What this scene is for — write an instruction, not a note; it shows beside the scene name while the scene is on.' },
391
+ // 与下面 `mcp` / `skills` / `subagents` 同一句式("X = 一个都不"):
392
+ // 界面 补了「无」这个选项,工具侧的空串语义要跟着说清,否则模型只知道
307
393
  // 「省略 = 保持」,不知道还有一条「解绑」的路。
308
- prompt: { type: 'string', description: 'Prompt preset id to bind; while this scene is on it is the global baseline. Omit = keep; \'\' = bind none (this scene leaves AGENTS.md alone).' },
309
- // 模型工具表方案绑定(0.20.0 补上模型侧入口:0.15.0 起界面可绑、档案里一直有这一栏,
310
- // `_list` / `_switch` 也读它,唯独 `_save` 写不了)。'' = 解绑只能走
394
+ prompt: { type: 'string', description: 'Prompt preset id to bind; while this scene is on it is the global baseline. \'\' = bind none (this scene leaves AGENTS.md alone).' },
395
+ // 模型工具表方案绑定:界面早就能绑、档案里一直有这一栏,`_list` / `_switch` 也读它,
396
+ // 唯独 `_save` 写不了。'' = 解绑只能走
311
397
  // `rules-update-scene` —— `scene-archive-save` 对载荷里**不带的栏**一律保留现值
312
398
  // (archive-engine 的「不带 ≠ 解绑」),所以解绑与设置分成两条路,见 execute 的 ②。
313
399
  // 哨兵 `factory-default` 与界面下拉同值(tools/table.ts 的 FACTORY_TOOL_TABLE_PRESET);
314
400
  // 不存在的方案名由引擎按 stale 丢弃并在进入时报告,op 不硬拒(与悬空绑定同口径)。
315
- toolTablePreset: { type: 'string', description: "Model tool-table scheme to bind while this scene is on. Omit = keep; '' = unbind (the scene leaves the tool table alone); 'factory-default' = the factory default; otherwise the name of a scheme saved in the panel. An unknown name is dropped and reported." },
316
- // 快捷提示词绑定(0.20.0)。段语义与上面三个档案段相反:**空数组是合法值**
401
+ toolTablePreset: { type: 'string', description: "Model tool-table scheme to bind while this scene is on. '' = unbind; 'factory-default' = the factory default; otherwise a saved scheme name. An unknown name is dropped and reported." },
402
+ // 快捷提示词绑定。段语义与上面三个档案段相反:**空数组是合法值**
317
403
  // (进入后全部停用),不是"清除这一段"。本工具不提供解绑(界面可解)—— 与
318
404
  // mcp / skills / subagents 三段没有解绑参数同一条口径。id 从场景清单里别的场景的
319
405
  // 绑定、或用户口述里来;不存在的 id 被引擎丢弃并在回执报出来。
320
- quickPrompts: { type: 'array', items: { type: 'string' }, description: 'Quick-prompt ids to switch on when this scene is entered; every other one is switched off, and the pre-entry state is restored on exit. Omit = keep; [] = switch all off on entry. Unknown ids are dropped and reported — copy them from a scene binding in the listing, or ask the user.' },
406
+ quickPrompts: { type: 'array', items: { type: 'string' }, description: 'Quick-prompt ids to switch on when this scene is entered; every other one goes off, and the pre-entry state is restored on exit. [] = all off on entry. Unknown ids are dropped and reported — copy them from the listing.' },
321
407
  mcp: {
322
408
  type: 'object',
323
409
  // 官方要求对象类型**显式声明开放性**(`additionalProperties` 是必需字段,且只能是
324
410
  // boolean)—— 嵌套对象不允许带 schema,所以值的形状(`'*'` 还是工具名数组)只能在
325
411
  // 描述里说清。这不是偷懒:键是服务器名,本身是动态的。
326
412
  additionalProperties: true,
327
- description: "Archive: serverName -> '*' (whole server) or a list of tool names to leave on. Omit = keep; {} = switch none on.",
413
+ description: "Archive: serverName -> '*' (whole server) or a list of tool names to leave on. {} = switch none on.",
328
414
  },
329
- // 场景级 MCP 备注覆盖(0.20.0):进入这个场景时用这份盖掉全局备注,退出时还原。
415
+ // 场景级 MCP 备注覆盖:进入这个场景时用这份盖掉全局备注,退出时还原。
330
416
  // 与 mcp 段同一句式(省略 = 保持;{} = 一个都不覆盖)—— 界面存档编辑器早就能改,
331
417
  // `_list` 也显示它,`_save` 此前不接这一段。
332
418
  mcpNotes: {
333
419
  type: 'object',
334
420
  additionalProperties: true,
335
- description: "Archive: serverName -> note that overrides that server's global note while this scene is on (the global note comes back on exit). Omit = keep; {} = drop every scene-level override.",
421
+ description: "Archive: serverName -> note overriding that server's global note while this scene is on (back on exit). {} = drop all scene-level overrides.",
336
422
  },
337
- skills: { type: 'array', items: { type: 'string' }, description: 'Archive: skill keys `<root>/<name>` to switch on. Omit = keep; [] = switch none on.' },
338
- subagents: { type: 'array', items: { type: 'string' }, description: 'Archive: persona names to switch on. Omit = keep; [] = switch none on.' },
423
+ skills: { type: 'array', items: { type: 'string' }, description: 'Archive: skill keys `<root>/<name>` to switch on. [] = switch none on.' },
424
+ subagents: { type: 'array', items: { type: 'string' }, description: 'Archive: persona names to switch on. [] = switch none on.' },
425
+ // 场景记录与档案是**机器级**的,没有「本会话的一份」。这个参数只为一件事:
426
+ // 模型以为自己在改会话时,当场纠回来(见 deps.ts 的 requireGlobalScope)。
427
+ scope: { type: 'string', enum: ['session', 'global'], description: scopeGlobalOnlyDesc('the scene record and archive are machine-wide', 'entering a scene for one conversation is a switch action') },
339
428
  },
340
429
  output: { schema: { type: 'string' }, render: (_a, v) => text(v) },
341
- async execute(args) {
430
+ async execute(args, exec) {
342
431
  const blocked = await deps.lockedSceneGuard();
343
432
  if (blocked)
344
433
  throw new Error(blocked);
434
+ requireGlobalScope(args, 'scenes', !sessionLayerOn(deps));
345
435
  const scene = String((args && args.scene) || '').trim();
346
436
  if (scene === '')
347
437
  throw new Error('missing argument: scene (the scene name)');
@@ -419,7 +509,7 @@ export function buildSceneTools(deps) {
419
509
  // ③ 档案 read-modify-write:段(mcp / mcpNotes / skills / subagents)与两个绑定栏的
420
510
  // 「设置」方向合并成一份,只经 `scene-archive-save` 一条路落盘。
421
511
  // **必须 read-modify-write**:`scene-archive-save` 是**整份替换**,只把模型给的段
422
- // 传过去,没给的段会被静默清掉 —— 与 0.14.0 里 `subagent_manager_save` 省略字段被
512
+ // 传过去,没给的段会被静默清掉 —— 与 `subagent_manager_save` 省略字段被
423
513
  // 清空同一形状,只是这次清掉的是"这个场景进入时要开哪些东西"。
424
514
  // 注意段语义:**段不存在 = 不限制(全部照常)**,段存在但为空 = 全部停用。所以模型给
425
515
  // `mcp: {}` 得到的是"全不勾",不是"清除该段" —— 后者只能靠不给。
@@ -495,10 +585,17 @@ export function buildSceneTools(deps) {
495
585
  // 点名 scene_manager_switch 的那句按可见性分叉(它在出厂默认名单里就是关着的):
496
586
  // 部分启用(开 save 不开 switch)时指向一条模型没有的工具,只会让它白跑一趟 ——
497
587
  // 与 mcp/skill 的 listHint 同一条纪律。
498
- const enterHint = deps.toolVisible('scene_manager_switch')
588
+ const enterHint = deps.toolVisibleFor('scene_manager_switch', exec && exec.agent)
499
589
  ? 'To take effect, enter it with scene_manager_switch.'
500
590
  : 'To take effect, the user has to enter this scene manually (scene_manager_switch is not available in this session).';
501
- return 'OK: scene "' + scene + '"' + parts.join('; ') + warnTail + clearWarn +
591
+ // 会话层:场景记录与档案是**机器级**的(一份索引 + 一份档案),本会话能改的
592
+ // 只有「这个对话用哪个场景」。不说清的话,模型会把「存好了」读成「只在这个对话里生效」。
593
+ const pinned = sessionScopeOf(deps, exec && exec.agent)?.scope?.scene;
594
+ const globalNote = pinned === undefined
595
+ ? ''
596
+ : '\nnote: scenes are global (one record and one archive for the whole machine). The pin here only decides which scene this scope uses' +
597
+ ' (currently "' + pinned + '"); ' + globalUnchangedNote(deps);
598
+ return 'OK: scene "' + scene + '"' + parts.join('; ') + warnTail + clearWarn + globalNote +
502
599
  (collapsed
503
600
  ? '\nnote: every scene was enabled until now (the historical all-enabled default); this call narrowed the enabled set to one scene, and the new one is not it — the user should confirm the enabled state.'
504
601
  : '') +