dsh-plugin-mesh 0.5.7 → 0.5.9

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.
@@ -0,0 +1,168 @@
1
+ /**
2
+ * 聊天窗口 @ 设备的纯逻辑自测。
3
+ *
4
+ * 这里盯住的是**最容易悄悄出错、又最难在界面上一眼看出来**的几件事:
5
+ * · 只认消息开头的 @(正文中间的 @ 不能自动派发)
6
+ * · 机器名可以含空格 → 按最长名字优先匹配,不按空白切词
7
+ * · 名字大小写不敏感,但派发目标必须是已知名字的规范写法
8
+ * · 改写后的消息必须明确「已派发 / 未派发」以及「不要在本机执行」
9
+ *
10
+ * node lib/_selftest-at-mention.mjs
11
+ */
12
+ import { strict as assert } from "node:assert";
13
+ import { matchLeadingMentions, buildAtPrompt, isMentionableName, AT_MENTION_LIMIT } from "./at-mention.js";
14
+
15
+ let passed = 0;
16
+ let failed = 0;
17
+ function test(name, fn) {
18
+ try {
19
+ fn();
20
+ passed += 1;
21
+ console.log(` ✓ ${name}`);
22
+ } catch (err) {
23
+ failed += 1;
24
+ console.log(` ✗ ${name}`);
25
+ console.log(` ${err?.message ?? err}`);
26
+ }
27
+ }
28
+
29
+ const NAMES = ["mac", "文超-win", "mac studio", "hos"];
30
+
31
+ console.log("\n── 认出开头的 @目标 ──");
32
+
33
+ test("单个 @名字 + 正文", () => {
34
+ const r = matchLeadingMentions("@mac 看看磁盘空间", NAMES);
35
+ assert.deepEqual(r.mentions.map((m) => m.machine), ["mac"]);
36
+ assert.equal(r.body, "看看磁盘空间");
37
+ });
38
+
39
+ test("多个 @目标(空格分隔)", () => {
40
+ const r = matchLeadingMentions("@mac @hos 都看看磁盘", NAMES);
41
+ assert.deepEqual(r.mentions.map((m) => m.machine), ["mac", "hos"]);
42
+ assert.equal(r.body, "都看看磁盘");
43
+ });
44
+
45
+ test("开头有空白也算开头", () => {
46
+ const r = matchLeadingMentions(" \n@mac hi", NAMES);
47
+ assert.deepEqual(r.mentions.map((m) => m.machine), ["mac"]);
48
+ assert.equal(r.body, "hi");
49
+ });
50
+
51
+ test("只有 @名字、没有正文 → 认出目标,正文为空", () => {
52
+ const r = matchLeadingMentions("@mac", NAMES);
53
+ assert.equal(r.mentions.length, 1);
54
+ assert.equal(r.body, "");
55
+ });
56
+
57
+ test("名字可以含空格,按最长名字优先匹配", () => {
58
+ const r = matchLeadingMentions("@mac studio 编译一下", NAMES);
59
+ assert.deepEqual(r.mentions.map((m) => m.machine), ["mac studio"]);
60
+ assert.equal(r.body, "编译一下");
61
+ });
62
+
63
+ test("中文/带连字符的名字", () => {
64
+ const r = matchLeadingMentions("@文超-win 打个补丁", NAMES);
65
+ assert.deepEqual(r.mentions.map((m) => m.machine), ["文超-win"]);
66
+ assert.equal(r.body, "打个补丁");
67
+ });
68
+
69
+ test("大小写不敏感,但目标取已知规范写法", () => {
70
+ const r = matchLeadingMentions("@MAC hi", NAMES);
71
+ assert.deepEqual(r.mentions.map((m) => m.machine), ["mac"]);
72
+ });
73
+
74
+ console.log("\n── 不该派发的地方 ──");
75
+
76
+ test("正文中间的 @ 不算:那是提到某台机器", () => {
77
+ const r = matchLeadingMentions("请让 @mac 看看磁盘", NAMES);
78
+ assert.deepEqual(r.mentions, []);
79
+ assert.equal(r.body, "请让 @mac 看看磁盘");
80
+ });
81
+
82
+ test("不是已知名字的 @ 不算(文件引用 / 打错字都走这条路)", () => {
83
+ for (const text of ["@AGENTS.md 看看", "@macx hi", "@ src/index.js"]) {
84
+ const r = matchLeadingMentions(text, NAMES);
85
+ assert.deepEqual(r.mentions, [], text);
86
+ }
87
+ });
88
+
89
+ test("前缀相同但不是整词:@macx 不能被当成 @mac", () => {
90
+ const r = matchLeadingMentions("@macx hi", NAMES);
91
+ assert.deepEqual(r.mentions, []);
92
+ });
93
+
94
+ test("没给名字列表时什么都不认", () => {
95
+ assert.deepEqual(matchLeadingMentions("@mac hi", []).mentions, []);
96
+ assert.deepEqual(matchLeadingMentions("@mac hi", undefined).mentions, []);
97
+ });
98
+
99
+ test(`一条消息最多认 ${AT_MENTION_LIMIT} 个目标(再多是广播,不是派活)`, () => {
100
+ const many = Array.from({ length: 20 }, (_, i) => `m${i}`);
101
+ const text = many.map((m) => `@${m}`).join(" ") + " hi";
102
+ const r = matchLeadingMentions(text, many);
103
+ assert.equal(r.mentions.length, AT_MENTION_LIMIT);
104
+ });
105
+
106
+ test("空消息 / 非字符串都不炸", () => {
107
+ for (const v of ["", null, undefined, 42]) {
108
+ const r = matchLeadingMentions(v, NAMES);
109
+ assert.deepEqual(r.mentions, []);
110
+ assert.equal(r.body, "");
111
+ }
112
+ });
113
+
114
+ console.log("\n── 名字本身的可提及性 ──");
115
+
116
+ test("带 @ 或控制字符的名字不可提及(会破坏 mention 语法)", () => {
117
+ assert.equal(isMentionableName("mac"), true);
118
+ assert.equal(isMentionableName("mac studio"), true);
119
+ assert.equal(isMentionableName(""), false);
120
+ assert.equal(isMentionableName("@mac"), false);
121
+ assert.equal(isMentionableName("mac\nwin"), false);
122
+ });
123
+
124
+ console.log("\n── 改写后的模型消息 ──");
125
+
126
+ test("派发成功:说明已发出,并明确不要在本机执行/回派", () => {
127
+ const text = buildAtPrompt({ body: "看看磁盘", results: [{ machine: "mac", id: "t-abc" }] });
128
+ assert.match(text, /看看磁盘/);
129
+ assert.match(text, /已派发/);
130
+ assert.match(text, /t-abc/);
131
+ assert.match(text, /不要调用任何工具/);
132
+ assert.match(text, /等待回执/);
133
+ });
134
+
135
+ test("派发失败:原因写出来,且明确「没有在本机执行」", () => {
136
+ const text = buildAtPrompt({ body: "看看磁盘", results: [{ machine: "mac", error: "这台机器当前离线" }] });
137
+ assert.match(text, /未能派发/);
138
+ assert.match(text, /当前离线/);
139
+ assert.match(text, /没有在本机执行/);
140
+ assert.match(text, /不要自己动手/);
141
+ });
142
+
143
+ test("成功与失败混在一起:两种结论都在", () => {
144
+ const text = buildAtPrompt({
145
+ body: "一起看看",
146
+ results: [
147
+ { machine: "mac", id: "t-1" },
148
+ { machine: "hos", error: "找不到机器 hos" },
149
+ ],
150
+ });
151
+ assert.match(text, /已派发/);
152
+ assert.match(text, /未能派发/);
153
+ });
154
+
155
+ test("正文为空时不留一条空白消息,而是说出原因", () => {
156
+ const text = buildAtPrompt({ body: "", results: [{ machine: "mac", error: "消息里只有 @ 设备名,没有正文" }] });
157
+ assert.match(text, /没有正文/);
158
+ assert.doesNotMatch(text.split("\n")[0], /^\s*$/);
159
+ });
160
+
161
+ test("正文里的反引号 / 换行原样保留(不改写用户内容)", () => {
162
+ const body = "第一行\n第二行 `code`";
163
+ const text = buildAtPrompt({ body, results: [{ machine: "mac", id: "t-1" }] });
164
+ assert.ok(text.startsWith(body), text);
165
+ });
166
+
167
+ console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败\n`);
168
+ process.exit(failed === 0 ? 0 : 1);
@@ -9,7 +9,7 @@
9
9
  import { strict as assert } from "node:assert";
10
10
  import { homedir, tmpdir } from "node:os";
11
11
  import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
12
- import { join } from "node:path";
12
+ import { delimiter, join } from "node:path";
13
13
  import {
14
14
  augmentedPath,
15
15
  capabilityAge,
@@ -324,14 +324,14 @@ test("按 max 截断", () => {
324
324
  console.log("\n── augmentedPath:只许加,绝不许减 ──");
325
325
 
326
326
  test("原 PATH 的每一项都还在(补全不能把已有路径挤掉)", () => {
327
- const before = String(process.env.PATH ?? "").split(":").filter(Boolean);
328
- const after = new Set(augmentedPath().split(":").filter(Boolean));
327
+ const before = String(process.env.PATH ?? "").split(delimiter).filter(Boolean);
328
+ const after = new Set(augmentedPath().split(delimiter).filter(Boolean));
329
329
  for (const d of before) assert.ok(after.has(d), `补全后丢了 ${d}`);
330
330
  });
331
331
 
332
332
  test("没有重复项", () => {
333
333
  // 报出**是哪几个**重复:只报"不该有重复"的话,下次在别人机器上红了还是得猜。
334
- const parts = augmentedPath().split(":").filter(Boolean);
334
+ const parts = augmentedPath().split(delimiter).filter(Boolean);
335
335
  const seen = new Set();
336
336
  const dup = [];
337
337
  for (const d of parts) {
@@ -349,7 +349,7 @@ test("同一个目录换了写法(末尾斜杠)也只能出现一次", () =>
349
349
  const spelled = `${existing}/`;
350
350
  const out = augmentedPath(spelled, process.platform);
351
351
  const norm = (d) => __normPathForCompare(d, process.platform);
352
- const keys = out.split(":").filter(Boolean).map(norm);
352
+ const keys = out.split(delimiter).filter(Boolean).map(norm);
353
353
  assert.equal(new Set(keys).size, keys.length, `按目录判重后仍重复:${out}`);
354
354
  });
355
355
 
@@ -421,8 +421,12 @@ test("自校验式判据:问的是「我们自己的兜底有没有起作用
421
421
  __extraPathDirs(process.platform).filter((d) => !__storeAliasDir.test(d)).length === 0,
422
422
  "判据必须精确等价于「补全一个目录都没加进去」(Store 别名目录除外)",
423
423
  );
424
- // 本机 darwin 上 win32 那套 %ProgramFiles% 路径确实都不存在 → 判不可靠(可证)。
425
- assert.equal(__pathLookupUnreliable("win32"), true);
424
+ // 在非 Windows 开发机上,win32 那套 %ProgramFiles% 路径确实都不存在 → 判不可靠。
425
+ // 但**在 Windows runner 上这些目录是真实存在的**,这条断言不成立;所以只把
426
+ // 它当"非 Windows 上的可证性"检查,Windows 交给上面那条等价性断言。
427
+ if (process.platform !== "win32") {
428
+ assert.equal(__pathLookupUnreliable("win32"), true);
429
+ }
426
430
  });
427
431
 
428
432
  test("Store 别名目录(WindowsApps)不算「补全真的生效」的证据", () => {
@@ -118,6 +118,13 @@ test("新任务:仍然要求回派(否则对端永远收不到结果)", ()
118
118
  assert.ok(!p.includes("不要再用 mesh_run 回派"));
119
119
  });
120
120
 
121
+ test("结果回派不能带 noWake(带了对端只记录、答案进不了会话)", () => {
122
+ const p = buildPrompt({ id: "t-3", from: "hos/u0", fromMachine: "hos", thread: "T", task: "查负载" });
123
+ assert.ok(p.includes("结果不要带 noWake"), p.slice(0, 400));
124
+ assert.ok(!/纯回执带 noWake: true/.test(p), "旧文案会把真实结果也坑成「仅记录」");
125
+ assert.ok(p.includes("「收到/好的」") || p.includes("收到/好的"), "noWake 只留给没有内容的确认");
126
+ });
127
+
121
128
  // ── 手动派发:用户气泡里只能是用户自己那句话 ──
122
129
  //
123
130
  // 真机事故(2026-10-21 splitmsg → lipeng-mac,t-d5176adbec0a):预建会话里先注入
@@ -8,6 +8,17 @@ import { tmpdir } from "node:os";
8
8
  import { join } from "node:path";
9
9
  import { conversationKey } from "./conversation.js";
10
10
 
11
+ // Node 20 + vm.SourceTextModule 双上下文:Linux 上稳定 SIGSEGV(2026-10-31 两次
12
+ // CI 复现),macOS 上偶发(2026-11-03 CI 门禁一次、本机一次)。段错误发生在任何
13
+ // 断言之前,进程里捕获不了 —— 只能让 Node 20 整体跳过。同一套件由 Node 24 的
14
+ // gate 和 Node 22 的 linux portability 覆盖;其它 suite 在 Node 20 上照常跑。
15
+ const NODE_MAJOR = Number(process.versions.node.split(".")[0]);
16
+ if (NODE_MAJOR === 20) {
17
+ console.log("skip: Node 20 在 vm.SourceTextModule 双上下文下会 SIGSEGV(上游 V8 bug,macOS/Linux 都出现过)");
18
+ console.log(" 该套件由 Node 22(linux portability)与 Node 24 gate 覆盖");
19
+ process.exit(0);
20
+ }
21
+
11
22
  const root = mkdtempSync(join(tmpdir(), "mesh-reply-test-"));
12
23
  const cleanups = [];
13
24
  const entryUrl = new URL("./index.js", import.meta.url);
@@ -46,7 +57,7 @@ async function makeNode(machine) {
46
57
  "@deepseek-ai/dsh-tools": { defineTool: (tool) => tool },
47
58
  "@deepseek-ai/dsh-llm": { createUserMessage: (message) => message },
48
59
  };
49
- const module = new vm.SourceTextModule(entrySource + "\nexport { registerTools, startPeerServer };", {
60
+ const module = new vm.SourceTextModule(entrySource + "\nexport { registerTools, startPeerServer, dispatchTaskToPeer };", {
50
61
  context, identifier: entryUrl.href, initializeImportMeta: (meta) => { meta.url = entryUrl.href; },
51
62
  });
52
63
  await module.link(async (specifier) => {
@@ -91,6 +102,8 @@ async function makeNode(machine) {
91
102
  };
92
103
  const options = { ...node, discovery, config: node.config, log: noop, warn: noop, waker, meshSwitch, capFor: () => node.config.maxWakesPerThread, capOverrides: new Map(), throttled: new Map() };
93
104
  // startPeerServer 现在返回句柄(一键关闭要重绑监听地址),端口在 handle.port 上。
105
+ node.options = options;
106
+ node.dispatch = (input) => module.namespace.dispatchTaskToPeer(options, input);
94
107
  const handle = await module.namespace.startPeerServer(ctx, options);
95
108
  node.port = handle.port;
96
109
  node.server = handle.server;
@@ -190,6 +203,52 @@ try {
190
203
  assert.equal(a.created, 1);
191
204
  console.log("✓ noWake, wake cap and explicit newSession still apply");
192
205
 
206
+ // 人发起的派发(聊天窗口 @ 设备)→ 对端回执即使标了 noWake 也必须投递。
207
+ //
208
+ // 真机 2026-10-30:dshgo-dsh 把"机器状态回报"当成回执发了 noWake,结果
209
+ // 停在 noted-only,会话里一个字都看不到 —— 用户等着的答案被"只记录"吞了。
210
+ // 闸门该拦的是 agent 互刷的寒暄,不是人问的问题的答案。
211
+ const humanDispatch = await a.dispatch({
212
+ id: "t-human-1",
213
+ target: b.peer,
214
+ task: "查一下你的机器负载,内存网络等",
215
+ thread: "at-t-human-1",
216
+ originSession: "original-session",
217
+ human: true,
218
+ });
219
+ assert.equal(humanDispatch.ok, true, JSON.stringify(humanDispatch));
220
+ await until(() => ["dispatched", "injected-thread-session"].includes(b.inbound.at(-1)?.status));
221
+ const humanReplyBefore = original.messages.length;
222
+ // 对端 agent 的回执从它为本任务建的那个会话来发(originSession 必须与
223
+ // 它本机绑定的会话一致,否则 mesh_run 会拒绝跨会话派发)。
224
+ const humanSession = [...b.agents.keys()].at(-1);
225
+ assert.match(String(humanSession), /^mesh-B-/, "对端应当为本任务建了会话");
226
+ const humanSendText = await b.send("A", "at-t-human-1", humanSession, { noWake: true });
227
+ assert.match(String(humanSendText), /已派发/, String(humanSendText));
228
+ const humanReplyRec = a.inbound.at(-1);
229
+ assert.equal(humanReplyRec.thread, "at-t-human-1");
230
+ assert.notEqual(humanReplyRec.status, "noted-only", "人发起派发的回执不能被「仅记录」吞掉");
231
+ await until(() => original.messages.length > humanReplyBefore);
232
+ assert.equal(a.created, 1, "回执要注入原会话,不能再新建会话");
233
+ console.log("✓ noWake reply to a human-initiated dispatch still lands in the live session");
234
+
235
+ // 但 agent 之间没有 human 标记的 noWake 回执,仍然只记录(循环刹车不能被拆)。
236
+ const agentReplyBefore = original.messages.length;
237
+ const sentAgent = await a.dispatch({
238
+ id: "t-agent-1",
239
+ target: b.peer,
240
+ task: "agent 之间的例行核对:这是一条没有 human 标记的普通长任务文本",
241
+ thread: "agent-thread-1",
242
+ originSession: "original-session",
243
+ });
244
+ assert.equal(sentAgent.ok, true);
245
+ await until(() => ["dispatched", "injected-thread-session"].includes(b.inbound.at(-1)?.status));
246
+ const agentSession = [...b.agents.keys()].at(-1);
247
+ assert.match(String(await b.send("A", "agent-thread-1", agentSession, { noWake: true })), /已派发/);
248
+ assert.equal(a.inbound.at(-1).status, "noted-only");
249
+ assert.equal(original.messages.length, agentReplyBefore, "agent 互刷的 noWake 仍然不唤醒");
250
+ console.log("✓ agent-originated noWake replies stay recorded-only");
251
+
193
252
  // 一键关闭的工具面:三个范围、默认关闭、不认识的范围要明确报错。
194
253
  const switchTool = b.tools.get("mesh_switch");
195
254
  assert.ok(switchTool, "必须注册 mesh_switch");
@@ -10,6 +10,9 @@
10
10
  * node lib/_selftest-update.mjs
11
11
  */
12
12
  import { strict as assert } from "node:assert";
13
+ import { tmpdir } from "node:os";
14
+ import { join } from "node:path";
15
+ import { pathToFileURL } from "node:url";
13
16
  import {
14
17
  buildUpdateReport,
15
18
  buildUpgradeCommand,
@@ -101,9 +104,14 @@ test("更新量级:major / minor / patch / prerelease / none", () => {
101
104
 
102
105
  console.log("\n── 升级命令 / 安装目录 ──");
103
106
 
104
- test("插件安装目录从 import.meta.url 推出(lib/ 的上一级,带结尾斜杠)", () => {
105
- const dir = pluginDirFromUrl("file:///Users/peng/.dsh/profiles/desktop/node_modules/dsh-plugin-mesh/lib/index.js");
106
- assert.equal(dir, "/Users/peng/.dsh/profiles/desktop/node_modules/dsh-plugin-mesh/");
107
+ test("插件安装目录从 import.meta.url 推出(lib/ 的上一级,带结尾分隔符)", () => {
108
+ // 用 pathToFileURL 现造一个本机安装路径:Windows 上不能拿 POSIX 的
109
+ // `file:///Users/...` 去喂 fileURLToPath(没有盘符会直接抛),否则断言测的
110
+ // 是"非法 URL 返回空串",不是这个函数的行为。
111
+ const entryUrl = pathToFileURL(join(tmpdir(), "dsh-profiles", "node_modules", "dsh-plugin-mesh", "lib", "index.js")).href;
112
+ const dir = pluginDirFromUrl(entryUrl);
113
+ assert.ok(/dsh-plugin-mesh[\\/]$/.test(dir), dir);
114
+ assert.equal(dir.slice(0, -1).split(/[\\/]/).pop(), "dsh-plugin-mesh", dir);
107
115
  });
108
116
 
109
117
  test("升级命令用的是 release 真实资产名与 tag(与 scripts/release-plugin.sh 一致)", () => {
@@ -263,5 +271,14 @@ await testAsync("没有 fetch 的运行环境 → 说清楚无法检查,而不
263
271
  assert.ok(r.error.includes("fetch"), r.error);
264
272
  });
265
273
 
274
+ test("pendingUpgrade 原样带出:磁盘装好了但进程没重启,界面靠它给重启/重载", () => {
275
+ const pending = { version: "0.6.0", previous: "0.5.6", restartRequired: true, reloadRequired: false, inferred: false };
276
+ const r = buildUpdateReport({ current: "0.5.6", diskVersion: "0.6.0", latest: okLatest, pendingUpgrade: pending });
277
+ assert.equal(r.pendingUpgrade.restartRequired, true);
278
+ assert.equal(r.pendingUpgrade.version, "0.6.0");
279
+ const none = buildUpdateReport({ current: "0.6.0", diskVersion: "0.6.0", latest: okLatest });
280
+ assert.equal(none.pendingUpgrade, null);
281
+ });
282
+
266
283
  console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败`);
267
284
  process.exit(failed === 0 ? 0 : 1);