pi-web-ui 0.113.1 → 0.114.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/CHANGELOG.md +43 -1
  2. package/README.md +117 -246
  3. package/README.zh-CN.md +145 -217
  4. package/README.zh-TW.md +145 -217
  5. package/bin/device-hub-cli.mjs +577 -0
  6. package/bin/pi-device-hub.mjs +9 -0
  7. package/bin/pi-web-ui.mjs +47 -15
  8. package/dist/server/agent-service-daemon.js +102 -24
  9. package/dist/server/agent-service.js +2289 -1120
  10. package/dist/server/approval-rules.js +29 -3
  11. package/dist/server/ask-user-question-tool.js +8 -8
  12. package/dist/server/atomic-file.js +35 -14
  13. package/dist/server/attachment-store.js +71 -35
  14. package/dist/server/attachments.js +18 -8
  15. package/dist/server/canvas/canvas-agent-tool.js +65 -15
  16. package/dist/server/canvas/canvas-dispatch.js +61 -20
  17. package/dist/server/canvas/canvas-orchestrator.js +516 -199
  18. package/dist/server/canvas/canvas-scheduler.js +99 -0
  19. package/dist/server/canvas/canvas-store.js +107 -54
  20. package/dist/server/canvas/canvas-validation.js +131 -0
  21. package/dist/server/canvas/canvas-workspace.js +284 -0
  22. package/dist/server/client-id-utils.js +8 -0
  23. package/dist/server/client-state.js +104 -22
  24. package/dist/server/communicate-tool.js +76 -0
  25. package/dist/server/compact-context-tool.js +2 -3
  26. package/dist/server/compaction-instructions.js +75 -0
  27. package/dist/server/compaction-markers.js +7 -2
  28. package/dist/server/compaction-state.js +84 -0
  29. package/dist/server/composer-drafts.js +43 -40
  30. package/dist/server/context-evidence.js +346 -0
  31. package/dist/server/conversation-communication.js +368 -0
  32. package/dist/server/conversation-read-tool.js +3 -4
  33. package/dist/server/conversation-tool-names.js +14 -0
  34. package/dist/server/conversation-tool.js +57 -0
  35. package/dist/server/delegate-mode.js +17 -4
  36. package/dist/server/device-center/bot-catalog.js +88 -0
  37. package/dist/server/device-center/bot-execution.js +106 -0
  38. package/dist/server/device-center/bots.js +170 -0
  39. package/dist/server/device-center/collab-service.js +153 -0
  40. package/dist/server/device-center/collab-tool.js +184 -0
  41. package/dist/server/device-center/connector.js +332 -0
  42. package/dist/server/device-center/desktop-linux.js +372 -0
  43. package/dist/server/device-center/desktop-native.js +413 -0
  44. package/dist/server/device-center/desktop-page.js +232 -0
  45. package/dist/server/device-center/desktop-setup.js +30 -0
  46. package/dist/server/device-center/desktop.js +387 -0
  47. package/dist/server/device-center/device-portals.js +241 -0
  48. package/dist/server/device-center/execution-failure.js +70 -0
  49. package/dist/server/device-center/executor.js +371 -0
  50. package/dist/server/device-center/file-sync.js +493 -0
  51. package/dist/server/device-center/hub-auth.js +69 -0
  52. package/dist/server/device-center/hub-page.js +68 -0
  53. package/dist/server/device-center/hub-setup.js +214 -0
  54. package/dist/server/device-center/hub.js +829 -0
  55. package/dist/server/device-center/local-page.js +17 -0
  56. package/dist/server/device-center/local.js +476 -0
  57. package/dist/server/device-center/main.js +118 -0
  58. package/dist/server/device-center/native-desktop/macos.js +192 -0
  59. package/dist/server/device-center/native-desktop/windows.js +118 -0
  60. package/dist/server/device-center/path-portal.js +120 -0
  61. package/dist/server/device-center/relay.js +959 -0
  62. package/dist/server/device-center/runtime.js +253 -0
  63. package/dist/server/device-center/smoke-preview.js +3 -0
  64. package/dist/server/device-center/store.js +122 -0
  65. package/dist/server/device-center/task-error.js +14 -0
  66. package/dist/server/device-center/transfers.js +293 -0
  67. package/dist/server/device-center/workspaces.js +45 -0
  68. package/dist/server/dispatch-domain-handlers.js +83 -27
  69. package/dist/server/dsh/dsh-agent-service.js +277 -158
  70. package/dist/server/dsh/dsh-client.js +12 -2
  71. package/dist/server/durable/durable-agent-service.js +1 -1
  72. package/dist/server/evidence-tool-hooks.js +49 -0
  73. package/dist/server/file-save.js +95 -0
  74. package/dist/server/file-transfer-routes.js +97 -47
  75. package/dist/server/files-service.js +38 -26
  76. package/dist/server/goal-review-gate.js +16 -6
  77. package/dist/server/goal-service.js +54 -30
  78. package/dist/server/http-proxy.js +120 -0
  79. package/dist/server/index.js +229 -223
  80. package/dist/server/load-timing.js +31 -0
  81. package/dist/server/mcp-bridge.js +219 -94
  82. package/dist/server/mcp-hot-reload.js +31 -5
  83. package/dist/server/mcp-manager.js +192 -93
  84. package/dist/server/mcp-oauth-login.js +169 -0
  85. package/dist/server/mcp-oauth-ui.js +137 -0
  86. package/dist/server/mcp-oauth.js +395 -0
  87. package/dist/server/mcp-project-runtime.js +207 -0
  88. package/dist/server/message-window.js +74 -21
  89. package/dist/server/net-guard.js +122 -20
  90. package/dist/server/payload-limits.js +17 -0
  91. package/dist/server/pending-question.js +82 -0
  92. package/dist/server/plan-mode.js +338 -137
  93. package/dist/server/plugin-assets.js +8 -4
  94. package/dist/server/plugin-chat-attachments.js +48 -0
  95. package/dist/server/plugin-facilities.js +67 -220
  96. package/dist/server/plugin-installer.js +2 -1
  97. package/dist/server/plugin-removal.js +49 -0
  98. package/dist/server/plugin-secrets.js +179 -0
  99. package/dist/server/plugins.js +262 -92
  100. package/dist/server/preset-fields.js +49 -0
  101. package/dist/server/preset-plugin-dependencies.js +57 -0
  102. package/dist/server/preset-share.js +112 -72
  103. package/dist/server/protocol-version.js +1 -1
  104. package/dist/server/queue-modes.js +10 -0
  105. package/dist/server/remote-ssh-service.js +160 -67
  106. package/dist/server/request-context.js +164 -0
  107. package/dist/server/sandbox-data.js +655 -0
  108. package/dist/server/sandbox-engines.js +7 -0
  109. package/dist/server/sandbox-runtime-gate.js +41 -0
  110. package/dist/server/sandbox-service.js +139 -698
  111. package/dist/server/sandbox-tool.js +21 -4
  112. package/dist/server/scheduler-tasks.js +63 -48
  113. package/dist/server/serialize.js +48 -19
  114. package/dist/server/session-branch.js +25 -3
  115. package/dist/server/session-parser.js +28 -18
  116. package/dist/server/session-scan.js +130 -0
  117. package/dist/server/session-search.js +51 -37
  118. package/dist/server/settings-service.js +267 -111
  119. package/dist/server/setup-config.js +58 -0
  120. package/dist/server/skill-maintenance.js +112 -24
  121. package/dist/server/subagent-helpers.js +4 -1
  122. package/dist/server/subagent-notify.js +4 -1
  123. package/dist/server/subagent-security-gate.js +52 -11
  124. package/dist/server/subagent-task.js +27 -0
  125. package/dist/server/subagents.js +217 -46
  126. package/dist/server/tabs.js +9 -1
  127. package/dist/server/terminals.js +467 -363
  128. package/dist/server/tool-guards.js +42 -6
  129. package/dist/server/tool-manager.js +47 -20
  130. package/dist/server/tool-overrides.js +13 -11
  131. package/dist/server/tool-prompt-overrides.js +32 -2
  132. package/dist/server/tool-prompts.js +1 -1
  133. package/dist/server/turn-timing.js +11 -0
  134. package/dist/server/universal/session-store.js +48 -6
  135. package/dist/server/universal/universal-service.js +1 -1
  136. package/dist/server/universal/universal-session.js +334 -231
  137. package/dist/server/uploads.js +64 -5
  138. package/dist/server/worktree-service.js +32 -24
  139. package/dist/server/ws-pending-queue.js +73 -27
  140. package/docs/device-center.md +142 -0
  141. package/docs/device-desktop.md +48 -0
  142. package/package.json +4 -1
  143. package/plugin-sdk/index.d.ts +28 -6
  144. package/plugin-sdk/ssh-host-trust.mjs +225 -0
  145. package/plugins/catalog.json +1 -1
  146. package/themes/aetheris.css +1 -107
  147. package/themes/ayu-light.css +5 -29
  148. package/themes/bases/flow-base.css +0 -6
  149. package/themes/bases/statusbar-flat-base.css +112 -0
  150. package/themes/bases/zhujian-base.css +1 -0
  151. package/themes/bases/zhupi-base.css +1 -0
  152. package/themes/catppuccin-latte.css +4 -27
  153. package/themes/catppuccin.css +4 -35
  154. package/themes/claude-code-dark.css +1 -0
  155. package/themes/codex.css +4 -28
  156. package/themes/cyberpunk.css +3 -61
  157. package/themes/dark-teal.css +0 -1
  158. package/themes/dazzle.css +3 -73
  159. package/themes/everforest-light.css +4 -27
  160. package/themes/geist.css +4 -28
  161. package/themes/gruvbox-light.css +4 -27
  162. package/themes/kanagawa-lotus.css +4 -27
  163. package/themes/md-preview.css +3 -213
  164. package/themes/mist.css +4 -24
  165. package/themes/nord.css +4 -35
  166. package/themes/one-dark.css +4 -37
  167. package/themes/paper.css +4 -16
  168. package/themes/rose-pine-dawn.css +4 -28
  169. package/themes/sakura.css +4 -24
  170. package/themes/solarized-light.css +4 -27
  171. package/themes/tokyo-night.css +3 -34
  172. package/themes/white.css +4 -26
  173. package/themes/zhujian-dark.css +1 -0
  174. package/themes/zhupi-dark.css +1 -0
  175. package/web/dist/.build-assets.json +1 -0
  176. package/web/dist/assets/BgTasksModal-CS7UNzcw.js +1 -0
  177. package/web/dist/assets/BgTasksModal-Dk-tp78W.js +1 -0
  178. package/web/dist/assets/CanvasStudio-BEJZE5BI.js +1579 -0
  179. package/web/dist/assets/CanvasStudio-CgUNHqos.js +1579 -0
  180. package/web/dist/assets/DeviceCenter-C8_CbxQ3.js +3 -0
  181. package/web/dist/assets/DeviceCenter-CVblvhNX.js +3 -0
  182. package/web/dist/assets/Dropdown-Oug0DDte.js +1 -0
  183. package/web/dist/assets/FilePreview-CvdX6nr6.js +2 -0
  184. package/web/dist/assets/FilePreview-kCZhfBAF.js +2 -0
  185. package/web/dist/assets/GlobalSearchModal-BLH63yYP.js +1 -0
  186. package/web/dist/assets/GlobalSearchModal-DAkvlvp6.js +1 -0
  187. package/web/dist/assets/GroupConversation-Cgo4xbpa.js +1 -0
  188. package/web/dist/assets/GroupConversation-DZ71jEi8.js +1 -0
  189. package/web/dist/assets/ModelConfigModal-BpWdFdQC.js +2 -0
  190. package/web/dist/assets/PiSetupModal-mkWPSxMj.js +1 -0
  191. package/web/dist/assets/ProviderOAuthControls-VK_jqXYS.js +1 -0
  192. package/web/dist/assets/SCMPanel-BP1TUT-V.js +2 -0
  193. package/web/dist/assets/SCMPanel-BscPm9kf.js +2 -0
  194. package/web/dist/assets/SettingsModal-C8UgMjVM.js +20 -0
  195. package/web/dist/assets/SettingsModal-VynB7jNb.js +20 -0
  196. package/web/dist/assets/TerminalPanel-D6cPRFei.js +3 -0
  197. package/web/dist/assets/TerminalPanel-DL20uKgU.js +3 -0
  198. package/web/dist/assets/TuiOverlayModal-Dst3BmT8.js +1 -0
  199. package/web/dist/assets/TuiOverlayModal-NCkpNtTp.js +1 -0
  200. package/web/dist/assets/UniversalPathPicker-CfQFc_c3.js +1 -0
  201. package/web/dist/assets/UniversalPathPicker-DwxwNcFw.js +1 -0
  202. package/web/dist/assets/app-globals-CnO6KlEM.js +325 -0
  203. package/web/dist/assets/desktop-DPyTy4M3.js +1 -0
  204. package/web/dist/assets/es-DWMIuDU2.js +2 -0
  205. package/web/dist/assets/index-CPgAH5q7.js +83 -0
  206. package/web/dist/assets/index-Cje8iugL.js +83 -0
  207. package/web/dist/assets/index-DnKeiDbj.css +1 -0
  208. package/web/dist/assets/markdown-BggnuQ4H.js +40 -0
  209. package/web/dist/assets/markdown-C8LL_u4z.css +1 -0
  210. package/web/dist/assets/math-1uRJCsA9.js +257 -0
  211. package/web/dist/assets/math-BorAY7qD.css +1 -0
  212. package/web/dist/assets/plugin-host-CqA4yW6w.js +1 -0
  213. package/web/dist/assets/shortcut-stack-hhrDyRBn.js +1 -0
  214. package/web/dist/assets/theme-BqdAjEjF.js +1 -0
  215. package/web/dist/assets/uuid-BzDIGCwP.js +1 -0
  216. package/web/dist/index.html +15 -9
  217. package/web/dist/sw.js +1 -1
  218. package/web/dist/assets/TerminalPanel-Ci55JtJL.js +0 -3
  219. package/web/dist/assets/index-ByiO9f-N.js +0 -2008
  220. package/web/dist/assets/index-ycGLh4a4.css +0 -1
  221. package/web/dist/assets/markdown-BJZlfvdP.js +0 -296
  222. package/web/dist/assets/markdown-DSNUiDzp.css +0 -1
@@ -0,0 +1,58 @@
1
+ /** Validate a candidate without mutating runtime state; publish its token only after save succeeds. */
2
+ export function saveSetupConfig(input, previous, current, save) {
3
+ const body = (input ?? {});
4
+ const next = { ...previous };
5
+ const nextToken = typeof body.token === "string" ? body.token.trim() : current.token;
6
+ if (typeof body.token === "string") {
7
+ if (nextToken)
8
+ next.token = nextToken;
9
+ else
10
+ delete next.token;
11
+ }
12
+ const fields = [
13
+ ["host", (s) => /^[A-Za-z0-9.:_-]+$/.test(s) && s.length <= 253],
14
+ ["allowHosts", (s) => /^[A-Za-z0-9.,:_\-\s]+$/.test(s)],
15
+ ["allowOrigins", (s) => s.length <= 2000],
16
+ ];
17
+ for (const [key, valid] of fields) {
18
+ const value = body[key];
19
+ if (value === undefined)
20
+ continue;
21
+ if (typeof value !== "string")
22
+ return { ok: false, status: 400, error: `${key} 必须是字符串` };
23
+ const s = value.trim();
24
+ if (s && !valid(s))
25
+ return { ok: false, status: 400, error: `${key} 格式不合法:${s}` };
26
+ if (s)
27
+ next[key] = s;
28
+ else
29
+ delete next[key];
30
+ }
31
+ if (body.port !== undefined && body.port !== "" && body.port !== null) {
32
+ // HTTP JSON inputs can include objects/arrays; never let coercion throw or accept booleans.
33
+ const p = typeof body.port === "string" || typeof body.port === "number" ? Number(body.port) : NaN;
34
+ if (!Number.isInteger(p) || p <= 0 || p > 65535) {
35
+ return { ok: false, status: 400, error: `port 必须是 1-65535:${String(body.port)}` };
36
+ }
37
+ next.port = p;
38
+ }
39
+ else if (body.port === "") {
40
+ delete next.port;
41
+ }
42
+ const norm = (s) => s
43
+ .split(",")
44
+ .map((x) => x.trim().toLowerCase())
45
+ .filter(Boolean)
46
+ .join(",");
47
+ const restartRequired = (typeof body.host === "string" && body.host.trim() !== current.host) ||
48
+ (body.port !== undefined && body.port !== "" && body.port !== null && Number(body.port) !== current.port) ||
49
+ (typeof body.allowHosts === "string" && norm(body.allowHosts) !== current.allowHosts.join(",")) ||
50
+ (typeof body.allowOrigins === "string" && norm(body.allowOrigins) !== current.allowOrigins.join(","));
51
+ try {
52
+ save(next);
53
+ }
54
+ catch (err) {
55
+ return { ok: false, status: 500, error: `写入配置失败:${err instanceof Error ? err.message : String(err)}` };
56
+ }
57
+ return { ok: true, nextToken, tokenApplied: nextToken !== current.token, restartRequired, saved: next };
58
+ }
@@ -6,8 +6,8 @@
6
6
  * 2. 技能文件合规性与健康检查(文件大小、8KB 上限、frontmatter 规范等);
7
7
  * 3. 跨 Agent 技能物理同步/软链维护(一键汇聚外部技能至当前工作区或全局目录)。
8
8
  */
9
- import { copyFileSync, existsSync, mkdirSync, readFileSync, statSync, symlinkSync } from "node:fs";
10
- import { basename, dirname, join } from "node:path";
9
+ import { closeSync, constants, copyFileSync, existsSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync, } from "node:fs";
10
+ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
11
11
  import { discoverDiskSkills, getMultiAgentSkillCandidateDirs, parseSkillFrontmatter, } from "./skill-discovery.js";
12
12
  import { decodeText } from "./text-sniff.js";
13
13
  /** 执行全源技能扫描与维护统计 */
@@ -178,52 +178,140 @@ export function verifySkillFile(filePath) {
178
178
  issues,
179
179
  };
180
180
  }
181
+ /** Path-segment comparison also handles sibling prefixes and Windows drive boundaries. */
182
+ function isWithin(root, path) {
183
+ const rel = relative(root, path);
184
+ return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
185
+ }
186
+ /** Resolve existing ancestors too, so a missing target below a junction cannot hide recursion. */
187
+ function physicalPath(path) {
188
+ try {
189
+ return realpathSync(path);
190
+ }
191
+ catch (err) {
192
+ if (err.code !== "ENOENT")
193
+ throw err;
194
+ const parent = dirname(path);
195
+ if (parent === path)
196
+ throw err;
197
+ return join(physicalPath(parent), basename(path));
198
+ }
199
+ }
200
+ function destinationExists(path) {
201
+ try {
202
+ lstatSync(path); // Dangling links are conflicts as well.
203
+ return true;
204
+ }
205
+ catch (err) {
206
+ if (err.code !== "ENOENT")
207
+ throw err;
208
+ return false;
209
+ }
210
+ }
211
+ /** Copy internal links as resources; reject escapes and cycles before following them. */
212
+ function copySkillDirectory(source, destination, root, ancestors = new Set()) {
213
+ const physical = realpathSync(source);
214
+ if (!isWithin(root, physical))
215
+ throw new Error("Skill resource escapes source directory");
216
+ if (ancestors.has(physical))
217
+ throw new Error("Recursive skill resource link");
218
+ const nextAncestors = new Set(ancestors).add(physical);
219
+ for (const entry of readdirSync(source)) {
220
+ const from = join(source, entry);
221
+ const to = join(destination, entry);
222
+ if (!isWithin(root, realpathSync(from)))
223
+ throw new Error("Skill resource escapes source directory");
224
+ const stat = statSync(from);
225
+ if (stat.isDirectory()) {
226
+ mkdirSync(to);
227
+ copySkillDirectory(from, to, root, nextAncestors);
228
+ }
229
+ else if (stat.isFile()) {
230
+ copyFileSync(from, to, constants.COPYFILE_EXCL);
231
+ }
232
+ else {
233
+ throw new Error("Unsupported skill resource type");
234
+ }
235
+ }
236
+ }
181
237
  /**
182
238
  * 跨 Agent 技能物理同步/软链维护:
183
239
  * 将外部 Agent(如 Claude Code 或通用 Agents)的技能同步或软链至工作区或指定目录中。
184
240
  */
185
241
  export function syncAgentSkills(opts) {
186
- const targetDir = opts.targetDir ? opts.targetDir : join(opts.cwd, ".pi", "skills");
242
+ const targetDir = resolve(opts.targetDir || join(opts.cwd, ".pi", "skills"));
187
243
  const mode = opts.mode || "copy";
188
244
  const dryRun = !!opts.dryRun;
189
245
  const scan = scanAllAgentSkills({ cwd: opts.cwd });
190
246
  const targetSources = opts.sources && opts.sources.length > 0 ? new Set(opts.sources) : null;
191
247
  const synced = [];
192
248
  const skipped = [];
193
- if (!dryRun && !existsSync(targetDir)) {
194
- mkdirSync(targetDir, { recursive: true });
195
- }
196
249
  for (const skill of scan.skills) {
197
250
  if (targetSources && skill.source && !targetSources.has(skill.source)) {
198
251
  continue;
199
252
  }
200
- // 如果目标技能已经在目标目录中,跳过
201
- if (skill.filePath.startsWith(targetDir)) {
202
- skipped.push({ name: skill.name, reason: "Already in target directory" });
253
+ // Names come from untrusted frontmatter, not necessarily from a directory name.
254
+ if (!skill.name ||
255
+ skill.name === "." ||
256
+ skill.name === ".." ||
257
+ /[<>:"/\\|?*\x00-\x1f]/.test(skill.name) ||
258
+ /[. ]$/.test(skill.name)) {
259
+ skipped.push({ name: skill.name, reason: "Invalid skill name" });
203
260
  continue;
204
261
  }
205
- const isDirSkill = basename(skill.filePath).toLowerCase() === "skill.md";
206
- const destPath = isDirSkill ? join(targetDir, skill.name, "SKILL.md") : join(targetDir, `${skill.name}.md`);
207
- if (existsSync(destPath)) {
208
- skipped.push({ name: skill.name, reason: "Destination already exists" });
262
+ if (isWithin(targetDir, resolve(skill.filePath))) {
263
+ skipped.push({ name: skill.name, reason: "Already in target directory" });
209
264
  continue;
210
265
  }
211
- if (!dryRun) {
212
- try {
213
- if (isDirSkill) {
214
- mkdirSync(dirname(destPath), { recursive: true });
215
- }
266
+ const isDirSkill = basename(skill.filePath).toLowerCase() === "skill.md";
267
+ const destination = join(targetDir, isDirSkill ? skill.name : `${skill.name}.md`);
268
+ const destPath = isDirSkill ? join(destination, "SKILL.md") : destination;
269
+ let ownedDestination = false;
270
+ try {
271
+ const physicalTarget = physicalPath(targetDir);
272
+ const source = realpathSync(isDirSkill ? dirname(skill.filePath) : skill.filePath);
273
+ if (isWithin(physicalTarget, source)) {
274
+ skipped.push({ name: skill.name, reason: "Already in target directory" });
275
+ continue;
276
+ }
277
+ if (isDirSkill && isWithin(source, physicalTarget)) {
278
+ skipped.push({ name: skill.name, reason: "Target directory is inside source skill" });
279
+ continue;
280
+ }
281
+ if (destinationExists(destination)) {
282
+ skipped.push({ name: skill.name, reason: "Destination already exists" });
283
+ continue;
284
+ }
285
+ if (!dryRun) {
286
+ mkdirSync(targetDir, { recursive: true });
216
287
  if (mode === "symlink") {
217
- symlinkSync(skill.filePath, destPath);
288
+ symlinkSync(source, destination, isDirSkill ? (process.platform === "win32" ? "junction" : "dir") : "file");
218
289
  }
219
290
  else {
220
- copyFileSync(skill.filePath, destPath);
291
+ // Exclusively claim the destination; cleanup must never remove a conflicting entry.
292
+ if (isDirSkill) {
293
+ mkdirSync(destination);
294
+ ownedDestination = true;
295
+ copySkillDirectory(source, destination, source);
296
+ }
297
+ else {
298
+ const fd = openSync(destination, "wx");
299
+ ownedDestination = true;
300
+ try {
301
+ writeFileSync(fd, readFileSync(source));
302
+ }
303
+ finally {
304
+ closeSync(fd);
305
+ }
306
+ }
221
307
  }
222
308
  }
223
- catch (err) {
224
- skipped.push({ name: skill.name, reason: `FS Error: ${err.message}` });
225
- continue;
226
- }
309
+ }
310
+ catch (err) {
311
+ if (ownedDestination)
312
+ rmSync(destination, { recursive: isDirSkill, force: true });
313
+ skipped.push({ name: skill.name, reason: `FS Error: ${err.message}` });
314
+ continue;
227
315
  }
228
316
  synced.push({ name: skill.name, from: skill.filePath, to: destPath });
229
317
  }
@@ -5,7 +5,7 @@ import { SUBAGENT_PROMPT_SNAPSHOT_CAP } from "./agent-formatters.js";
5
5
  export function isSubagentActive(conv) {
6
6
  if (!conv.isSubagent)
7
7
  return false;
8
- if (conv.promptInFlight)
8
+ if (conv.promptInFlight || conv.subagentTaskPending?.())
9
9
  return true;
10
10
  try {
11
11
  return Boolean(conv.session.isStreaming);
@@ -28,6 +28,8 @@ export function countActiveSubagents(convs, spawningCount = 0) {
28
28
  export function subagentRunOutcome(conv) {
29
29
  // 自动重试等待期结局未定:瞬时 error 不算失败,避免向主对话误报
30
30
  // 「子代理运行失败」(耗尽后 auto_retry_end 清旗,真正失败照常通知)。
31
+ if (conv.subagentTaskStopped)
32
+ return { canceled: true };
31
33
  if (conv.retryState)
32
34
  return {};
33
35
  try {
@@ -101,6 +103,7 @@ export function toSubagentSnapshot(conv) {
101
103
  : fullPrompt,
102
104
  state,
103
105
  streaming,
106
+ taskPending: conv.subagentTaskPending?.() ?? false,
104
107
  error,
105
108
  canceled,
106
109
  messageCount,
@@ -66,7 +66,10 @@ export function formatSubagentsCompletionNotice(subagents, lang = "en", fallback
66
66
  else {
67
67
  statusLabel = pick(lang, "✅ 正常完成", "✅ Done", "subagents.notify.status.done");
68
68
  }
69
- const rawOutput = conv.session.getLastAssistantText() ?? "";
69
+ const manualSummary = !outcome.error && !outcome.canceled && conv.subagentTask?.mode === "manual"
70
+ ? conv.subagentTask.summary?.trim()
71
+ : undefined;
72
+ const rawOutput = manualSummary || conv.session.getLastAssistantText() || "";
70
73
  const conclusion = extractSubagentConclusion(rawOutput, lang);
71
74
  const indentedConclusion = conclusion
72
75
  .split("\n")
@@ -1,6 +1,7 @@
1
1
  import { pick } from "./i18n.js";
2
- import { bashCommandIsReadOnly } from "./plan-mode.js";
2
+ import { bashCommandHasSupportedSyntax, bashCommandIsReadOnly, readOnlyToolCallAllowed } from "./plan-mode.js";
3
3
  import { goalReviewBashAllowed } from "./goal-review-gate.js";
4
+ import { isReadOnlyConversationAction, conversationToolAllowed, isConversationTool, } from "./conversation-tool-names.js";
4
5
  /** 常见只读角色关键词(转小写后包含即判定为 readonly)。 */
5
6
  const READONLY_ROLE_KEYWORDS = ["explore", "scout", "research", "oracle", "librarian", "reader"];
6
7
  /** 常见审查验证角色关键词(转小写后包含即判定为 verify)。 */
@@ -30,7 +31,7 @@ export function resolveSecurityMode(nameOrType, template) {
30
31
  }
31
32
  /** 别名映射:将调用方可能传入的泛称归一化为实际注册的工具名。 */
32
33
  const TOOL_NAME_ALIASES = {
33
- terminal: "bash",
34
+ terminal: "terminal",
34
35
  shell: "bash",
35
36
  sh: "bash",
36
37
  cmd: "bash",
@@ -91,7 +92,10 @@ export function parseRequiredTools(raw, availableTools) {
91
92
  continue;
92
93
  seen.add(mapped);
93
94
  if (availableSet) {
94
- if (availableSet.has(mapped)) {
95
+ if (availableSet.has(mapped) ||
96
+ (mapped.startsWith("terminal_") &&
97
+ availableSet.has("terminal") &&
98
+ ["create", "list", "close", "input", "key", "read", "wait"].includes(mapped.slice(9)))) {
95
99
  allowedTools.push(mapped);
96
100
  }
97
101
  else {
@@ -149,7 +153,7 @@ export const SUBAGENT_TERMINAL_INPUT_TOOLS = new Set([
149
153
  /** 越权派发工具:受限子代理禁止再派生/操控其他子代理。 */
150
154
  export const SUBAGENT_DISPATCH_TOOLS = new Set(["delegate_task"]);
151
155
  /** bash 类工具名。 */
152
- const BASH_TOOL_NAMES = new Set(["bash", "bash_execute", "run_command", "shell", "terminal", "terminal_exec"]);
156
+ const BASH_TOOL_NAMES = new Set(["bash", "bash_execute", "run_command", "shell", "terminal_exec"]);
153
157
  /** 取工具参数中的命令字段。 */
154
158
  function commandOf(params) {
155
159
  if (!params || typeof params !== "object")
@@ -170,12 +174,27 @@ function commandOf(params) {
170
174
  * @param params 调用参数
171
175
  * @param allowedTools 显式工具白名单集合(若设置)
172
176
  */
177
+ /** Keep legacy allowlists action-scoped; never upgrade terminal_read to unrestricted terminal. */
178
+ export function subagentAllowedToolCall(allowed, name, params) {
179
+ if (conversationToolAllowed(allowed, name))
180
+ return true;
181
+ if (name !== "terminal" || !params || typeof params !== "object")
182
+ return false;
183
+ const action = params.action;
184
+ return (typeof action === "string" &&
185
+ ["create", "list", "close", "input", "key", "read", "wait"].includes(action) &&
186
+ allowed.has(`terminal_${action}`));
187
+ }
188
+ /** Visibility only; execution must still call subagentAllowedToolCall with actual params. */
189
+ export function subagentAllowedToolVisible(allowed, name) {
190
+ return (conversationToolAllowed(allowed, name) ||
191
+ (name === "terminal" &&
192
+ ["create", "list", "close", "input", "key", "read", "wait"].some((action) => allowed.has(`terminal_${action}`))));
193
+ }
173
194
  export function subagentSecurityDenial(mode, toolName, params, allowedTools) {
174
195
  const name = (toolName ?? "").trim();
175
- if (!name)
176
- return undefined;
177
196
  // 1. 显式白名单门控(若配置了 allowedTools)
178
- if (allowedTools && allowedTools.size > 0 && !allowedTools.has(name)) {
197
+ if (allowedTools && allowedTools.size > 0 && !subagentAllowedToolCall(allowedTools, name, params)) {
179
198
  return {
180
199
  kind: "not-allowed-tool",
181
200
  reason: `受限子代理:工具 "${name}" 不在本次任务允许的工具白名单内(仅限:${[...allowedTools].join(", ")})。`,
@@ -194,11 +213,25 @@ export function subagentSecurityDenial(mode, toolName, params, allowedTools) {
194
213
  reasonEn: `Restricted subagent (${mode}): dispatch tool "${name}" is blocked. Complete inspection and return findings within this session.`,
195
214
  };
196
215
  }
197
- if (name === "subagent") {
216
+ if (name === "communicate") {
217
+ const action = params && typeof params === "object" ? params.action : undefined;
218
+ if (typeof action !== "string" || !["read", "send", "request", "reply", "escalate"].includes(action)) {
219
+ return {
220
+ kind: "dispatch-tool",
221
+ reason: "受限子代理:未知通信操作。",
222
+ reasonEn: "Restricted subagent: unknown communication action.",
223
+ };
224
+ }
225
+ return undefined;
226
+ }
227
+ if (isConversationTool(name)) {
198
228
  const action = typeof params === "object" && params !== null ? params.action : undefined;
199
229
  const act = typeof action === "string" ? action.trim().toLowerCase() : "";
200
- // 只放行只读查询类 action,拦截 spawn/steer/stop/handoff/wait_all
201
- if (act !== "get_result" && act !== "list" && act !== "templates") {
230
+ // Completion reports this execution result; it does not dispatch or mutate another task.
231
+ if (act === "complete")
232
+ return undefined;
233
+ // Allow transcript inspection and collaboration queries; block dispatch and mutations.
234
+ if (!isReadOnlyConversationAction(act)) {
202
235
  return {
203
236
  kind: "dispatch-tool",
204
237
  reason: `受限子代理(${mode}):禁止调用 subagent(action="${act || "spawn"}") 派生或操控其他子代理。`,
@@ -244,7 +277,7 @@ export function subagentSecurityDenial(mode, toolName, params, allowedTools) {
244
277
  }
245
278
  else if (mode === "verify") {
246
279
  // 审查验证模式:允许只读命令 + 测试套件(goalReviewBashAllowed)
247
- if (!goalReviewBashAllowed(cmd)) {
280
+ if (!bashCommandHasSupportedSyntax(cmd) || !goalReviewBashAllowed(cmd)) {
248
281
  return {
249
282
  kind: "bash-write",
250
283
  reason: `审查验证子代理(verify):命令被阻断(仅允许只读命令与测试套件,禁止重定向写或破坏性操作):\`${cmd.slice(0, 160)}\`。`,
@@ -252,6 +285,14 @@ export function subagentSecurityDenial(mode, toolName, params, allowedTools) {
252
285
  };
253
286
  }
254
287
  }
288
+ return undefined;
289
+ }
290
+ if (!readOnlyToolCallAllowed(name, params)) {
291
+ return {
292
+ kind: "not-allowed-tool",
293
+ reason: `受限子代理(${mode}):工具 "${name}" 的本次调用未声明为只读,已拒绝。请使用 read/grep 或结构化查询。`,
294
+ reasonEn: `Restricted subagent (${mode}): tool "${name}" is not an explicitly supported read-only call. Use read/grep or structured queries.`,
295
+ };
255
296
  }
256
297
  return undefined;
257
298
  }
@@ -0,0 +1,27 @@
1
+ export function createSubagentTask(mode = true) {
2
+ return { mode, completionRequested: false, generation: 0 };
3
+ }
4
+ /** Only a NEW task/reset advances generation. Child replies and automatic wakes retain intent. */
5
+ export function restartSubagentTask(task) {
6
+ return { mode: task.mode, completionRequested: false, generation: task.generation + 1 };
7
+ }
8
+ /** Record intent only. The host must recheck live dependencies and idle state before closing. */
9
+ export function requestSubagentCompletion(task, summary, generation = task.generation) {
10
+ if (generation !== task.generation)
11
+ return task;
12
+ return { ...task, completionRequested: true, summary: summary.trim() };
13
+ }
14
+ /**
15
+ * Pure snapshot predicate, never a completion side effect. The host resolves dependencies
16
+ * across owners immediately before settlement. complete cannot close a streaming task or
17
+ * bypass children/questions/communications. Manual idle without dependencies falls back to
18
+ * the final assistant summary, even without complete. Error/cancel bypass dependency waits;
19
+ * the host must notify these failures even in manual mode. Notifications are generation-bound.
20
+ */
21
+ export function isSubagentTaskPending(task, activity) {
22
+ if (activity.error || activity.canceled)
23
+ return false;
24
+ if (activity.streaming)
25
+ return true;
26
+ return (task.mode === "manual" && !!(activity.activeChildren || activity.pendingQuestions || activity.pendingCommunications));
27
+ }