dsh-plugin-mesh 0.5.7 → 0.5.8
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/AGENTS.md +9 -2
- package/README.md +68 -6
- package/client/client.js +270 -30
- package/cordis.patch.yml +4 -0
- package/lib/_selftest-at-mention-host.mjs +377 -0
- package/lib/_selftest-at-mention.mjs +168 -0
- package/lib/_selftest-prompt.mjs +7 -0
- package/lib/_selftest-reply-route.mjs +49 -1
- package/lib/_selftest-update.mjs +9 -0
- package/lib/_selftest-upgrade.mjs +619 -0
- package/lib/_selftest-workbench.mjs +130 -20
- package/lib/at-mention.js +109 -0
- package/lib/index.js +501 -98
- package/lib/prompt.js +5 -1
- package/lib/update.js +3 -0
- package/lib/upgrade.js +635 -0
- package/llms.txt +3 -1
- package/package.json +7 -4
- package/scripts/check-tdz.mjs +1 -1
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 聊天窗口 @ 派发(宿主侧)的端到端自测。
|
|
3
|
+
*
|
|
4
|
+
* 与 _selftest-reply-route.mjs 同一套手法:把 lib/index.js 放进 VM 里加载,
|
|
5
|
+
* 用假对端 HTTP 服务收包,驱动真实的 `agent/pre-step` 监听器 —— 不装 DSH、
|
|
6
|
+
* 不调模型、不出网。
|
|
7
|
+
*
|
|
8
|
+
* 盯住的是「静默失败」最容易发生的那几处:
|
|
9
|
+
* · 消息开头的 @目标 真的变成了一次 POST,正文被完整带过去;
|
|
10
|
+
* · 改写后的消息明确说「已派发 / 未派发」,不会让 agent 在本机重做一遍;
|
|
11
|
+
* · 离线 / 同名歧义 / 只有 @ 没正文 → 不发包,但原因必须写出来;
|
|
12
|
+
* · 同一条消息被 pre-step 拿到两次 → 只派发一次;
|
|
13
|
+
* · 正文中间的 @ 不自动派发。
|
|
14
|
+
*
|
|
15
|
+
* node --experimental-vm-modules lib/_selftest-at-mention-host.mjs
|
|
16
|
+
*/
|
|
17
|
+
import assert from "node:assert/strict";
|
|
18
|
+
import vm from "node:vm";
|
|
19
|
+
import http from "node:http";
|
|
20
|
+
import { readFileSync, mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
|
|
21
|
+
import { tmpdir } from "node:os";
|
|
22
|
+
import { join } from "node:path";
|
|
23
|
+
import { conversationKey } from "./conversation.js";
|
|
24
|
+
|
|
25
|
+
let passed = 0;
|
|
26
|
+
let failed = 0;
|
|
27
|
+
async function test(name, fn) {
|
|
28
|
+
try {
|
|
29
|
+
await fn();
|
|
30
|
+
passed += 1;
|
|
31
|
+
console.log(` ✓ ${name}`);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
failed += 1;
|
|
34
|
+
console.log(` ✗ ${name}`);
|
|
35
|
+
console.log(` ${err?.message ?? err}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const root = mkdtempSync(join(tmpdir(), "mesh-at-test-"));
|
|
40
|
+
mkdirSync(join(root, "mesh"), { recursive: true });
|
|
41
|
+
writeFileSync(join(root, "mesh", "state.json"), JSON.stringify({ secret: "test-only-shared-secret" }));
|
|
42
|
+
|
|
43
|
+
// ── 假对端:收 /mesh/v1/task ──
|
|
44
|
+
const posts = [];
|
|
45
|
+
const server = http.createServer((req, res) => {
|
|
46
|
+
let raw = "";
|
|
47
|
+
req.on("data", (c) => {
|
|
48
|
+
raw += c;
|
|
49
|
+
});
|
|
50
|
+
req.on("end", () => {
|
|
51
|
+
let body = null;
|
|
52
|
+
try {
|
|
53
|
+
body = JSON.parse(raw);
|
|
54
|
+
} catch {
|
|
55
|
+
body = { raw };
|
|
56
|
+
}
|
|
57
|
+
posts.push({ url: req.url, token: req.headers["x-dsh-mesh-token"], body });
|
|
58
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
59
|
+
res.end(JSON.stringify({ ok: true }));
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
63
|
+
const peerPort = server.address().port;
|
|
64
|
+
|
|
65
|
+
// ── 在 VM 里加载 lib/index.js,并暴露要测的内部函数 ──
|
|
66
|
+
const entryUrl = new URL("./index.js", import.meta.url);
|
|
67
|
+
const entrySource =
|
|
68
|
+
readFileSync(entryUrl, "utf8") +
|
|
69
|
+
"\nexport { registerAtMentions, dispatchTaskToPeer, resolvePeerTarget, classifyInbound };\n";
|
|
70
|
+
|
|
71
|
+
const context = vm.createContext({
|
|
72
|
+
console,
|
|
73
|
+
Buffer,
|
|
74
|
+
URL,
|
|
75
|
+
AbortSignal,
|
|
76
|
+
AbortController,
|
|
77
|
+
setTimeout,
|
|
78
|
+
clearTimeout,
|
|
79
|
+
setInterval,
|
|
80
|
+
clearInterval,
|
|
81
|
+
fetch,
|
|
82
|
+
process: { ...process, env: { ...process.env, DSH_HOME: root } },
|
|
83
|
+
});
|
|
84
|
+
const schema = new Proxy(() => schema, { get: () => schema });
|
|
85
|
+
const mocks = {
|
|
86
|
+
"@deepseek-ai/schemastery": { default: schema },
|
|
87
|
+
"@deepseek-ai/dsh-brand": { brandString: (value) => value },
|
|
88
|
+
"@deepseek-ai/dsh-tools": { defineTool: (tool) => tool },
|
|
89
|
+
"@deepseek-ai/dsh-llm": { createUserMessage: (message) => message },
|
|
90
|
+
};
|
|
91
|
+
const module = new vm.SourceTextModule(entrySource, {
|
|
92
|
+
context,
|
|
93
|
+
identifier: entryUrl.href,
|
|
94
|
+
initializeImportMeta: (meta) => {
|
|
95
|
+
meta.url = entryUrl.href;
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
await module.link(async (specifier) => {
|
|
99
|
+
const exports = mocks[specifier] ?? (await import(specifier.startsWith(".") ? new URL(specifier, entryUrl).href : specifier));
|
|
100
|
+
return new vm.SyntheticModule(
|
|
101
|
+
Object.keys(exports),
|
|
102
|
+
function () {
|
|
103
|
+
for (const key of Object.keys(exports)) this.setExport(key, exports[key]);
|
|
104
|
+
},
|
|
105
|
+
{ context },
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
await module.evaluate();
|
|
109
|
+
const { registerAtMentions, resolvePeerTarget, classifyInbound } = module.namespace;
|
|
110
|
+
|
|
111
|
+
// ── 假环境 ──
|
|
112
|
+
const peer = (machine, extra = {}) => ({
|
|
113
|
+
id: `id-${machine}`,
|
|
114
|
+
machine,
|
|
115
|
+
online: true,
|
|
116
|
+
host: "127.0.0.1",
|
|
117
|
+
port: peerPort,
|
|
118
|
+
transport: "lan",
|
|
119
|
+
endpoints: [{ transport: "lan", host: "127.0.0.1", port: peerPort, lastSeen: Date.now() }],
|
|
120
|
+
...extra,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
function makeHarness({ peers = [peer("mac")], atDispatch = true, allOff = false } = {}) {
|
|
124
|
+
const logs = [];
|
|
125
|
+
const warns = [];
|
|
126
|
+
const outbound = [];
|
|
127
|
+
const threadState = new Map();
|
|
128
|
+
const listeners = new Map();
|
|
129
|
+
const ctx = {
|
|
130
|
+
on: (name, fn, opts) => {
|
|
131
|
+
listeners.set(name, { fn, opts });
|
|
132
|
+
return () => listeners.delete(name);
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
const deps = {
|
|
136
|
+
discovery: {
|
|
137
|
+
peers: () => peers,
|
|
138
|
+
selfRecord: () => ({ machine: "me", id: "me", host: "127.0.0.1", port: 0 }),
|
|
139
|
+
},
|
|
140
|
+
identity: { name: "me" },
|
|
141
|
+
config: { atDispatch, requestTimeoutMs: 2000 },
|
|
142
|
+
outbound,
|
|
143
|
+
threadState,
|
|
144
|
+
hostDispatched: new Map(),
|
|
145
|
+
log: (m) => logs.push(String(m)),
|
|
146
|
+
warn: (m) => warns.push(String(m)),
|
|
147
|
+
onHistoryChange: () => {},
|
|
148
|
+
meshSwitch: { status: () => ({ all: allOff }) },
|
|
149
|
+
};
|
|
150
|
+
registerAtMentions(ctx, deps);
|
|
151
|
+
const listener = listeners.get("agent/pre-step")?.fn;
|
|
152
|
+
/**
|
|
153
|
+
* 跑一次 pre-step:喂一条 user 消息,返回模型将看到的消息列表。
|
|
154
|
+
*/
|
|
155
|
+
async function step(text, { agentSession = "s-1", id = `msg-${Math.random()}`, role } = {}) {
|
|
156
|
+
assert.ok(listener, "agent/pre-step 监听器没有注册");
|
|
157
|
+
const message = role || {
|
|
158
|
+
id,
|
|
159
|
+
role: "user",
|
|
160
|
+
source: { kind: "user" },
|
|
161
|
+
content: [{ type: "text", text }],
|
|
162
|
+
};
|
|
163
|
+
const next = async () => ({ kind: "enter", messages: [message] });
|
|
164
|
+
const decision = await listener({ agent: { session: { id: agentSession } }, signal: new AbortController().signal }, next);
|
|
165
|
+
return { decision, message };
|
|
166
|
+
}
|
|
167
|
+
return { deps, listener, step, logs, warns, outbound, threadState };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const textOf = (message) => message.content.filter((b) => b.type === "text").map((b) => b.text).join("\n");
|
|
171
|
+
|
|
172
|
+
console.log("\n── 正常路径:开头 @在线机器 → 真的发出去 ──");
|
|
173
|
+
|
|
174
|
+
await test("派发一次 POST:完整正文、带配对码、thread 绑定到当前会话", async () => {
|
|
175
|
+
posts.length = 0;
|
|
176
|
+
const h = makeHarness();
|
|
177
|
+
const { decision } = await h.step("@mac 看看磁盘空间");
|
|
178
|
+
assert.equal(posts.length, 1, "应当正好发一条");
|
|
179
|
+
const post = posts[0];
|
|
180
|
+
assert.equal(post.url, "/mesh/v1/task");
|
|
181
|
+
assert.equal(post.token, "test-only-shared-secret");
|
|
182
|
+
assert.equal(post.body.task, "看看磁盘空间", "正文必须去掉 @目标");
|
|
183
|
+
assert.equal(post.body.originSession, "s-1", "回执要落回当前会话");
|
|
184
|
+
assert.equal(post.body.human, true, "聊天窗口 @ 是人敲的,要带 human 标记(对端才不会被长度闸门吞掉)");
|
|
185
|
+
assert.match(post.body.thread, /^at-t-[0-9a-f]+$/, `thread 形态:${post.body.thread}`);
|
|
186
|
+
assert.equal(h.threadState.size, 1, "thread 必须绑定到本机会话");
|
|
187
|
+
|
|
188
|
+
const shown = textOf(decision.messages[0]);
|
|
189
|
+
assert.match(shown, /看看磁盘空间/);
|
|
190
|
+
assert.match(shown, /已派发/);
|
|
191
|
+
assert.match(shown, /等待回执/);
|
|
192
|
+
assert.match(shown, /不要调用任何工具/);
|
|
193
|
+
assert.ok(shown.includes(post.body.id), "改写里要带任务 id,方便对账");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
await test("多个 @目标 → 每个目标各发一次", async () => {
|
|
197
|
+
posts.length = 0;
|
|
198
|
+
const h = makeHarness({ peers: [peer("mac"), peer("hos")] });
|
|
199
|
+
const { decision } = await h.step("@mac @hos 一起看看");
|
|
200
|
+
assert.equal(posts.length, 2);
|
|
201
|
+
assert.deepEqual(posts.map((p) => p.body.task), ["一起看看", "一起看看"]);
|
|
202
|
+
const shown = textOf(decision.messages[0]);
|
|
203
|
+
assert.match(shown, /mac/);
|
|
204
|
+
assert.match(shown, /hos/);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
await test("同一条消息被 pre-step 拿到两次 → 只派发一次", async () => {
|
|
208
|
+
posts.length = 0;
|
|
209
|
+
const h = makeHarness();
|
|
210
|
+
await h.step("@mac 看看磁盘", { id: "same-id" });
|
|
211
|
+
const second = await h.step("@mac 看看磁盘", { id: "same-id" });
|
|
212
|
+
assert.equal(posts.length, 1, "重复投喂不能重复派发");
|
|
213
|
+
assert.match(textOf(second.decision.messages[0]), /@mac/, "第二次原样放行(不做第二次改写)");
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
console.log("\n── 不该派发 / 必须说出原因 ──");
|
|
217
|
+
|
|
218
|
+
await test("正文中间的 @ 不自动派发,消息原样放行", async () => {
|
|
219
|
+
posts.length = 0;
|
|
220
|
+
const h = makeHarness();
|
|
221
|
+
const { decision } = await h.step("请让 @mac 看看磁盘");
|
|
222
|
+
assert.equal(posts.length, 0);
|
|
223
|
+
assert.equal(decision.messages[0].content[0].text, "请让 @mac 看看磁盘");
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
await test("未知名字的 @ 不当派发目标(文件引用 / 打错字)", async () => {
|
|
227
|
+
posts.length = 0;
|
|
228
|
+
const h = makeHarness();
|
|
229
|
+
for (const text of ["@AGENTS.md 看看", "@macx hi"]) {
|
|
230
|
+
const { decision } = await h.step(text);
|
|
231
|
+
assert.equal(decision.messages[0].content[0].text, text, `${text} 不该被改写`);
|
|
232
|
+
}
|
|
233
|
+
assert.equal(posts.length, 0);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
await test("离线机器:不发包,但必须写清「当前离线」且「没有在本机执行」", async () => {
|
|
237
|
+
posts.length = 0;
|
|
238
|
+
const h = makeHarness({ peers: [peer("mac", { online: false })] });
|
|
239
|
+
const { decision } = await h.step("@mac 看看磁盘");
|
|
240
|
+
assert.equal(posts.length, 0, "离线不该发包");
|
|
241
|
+
const shown = textOf(decision.messages[0]);
|
|
242
|
+
assert.match(shown, /当前离线/);
|
|
243
|
+
assert.match(shown, /没有在本机执行/);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
await test("只有 @ 名字没有正文:不发包,并说明原因", async () => {
|
|
247
|
+
posts.length = 0;
|
|
248
|
+
const h = makeHarness();
|
|
249
|
+
const { decision } = await h.step("@mac");
|
|
250
|
+
assert.equal(posts.length, 0);
|
|
251
|
+
const shown = textOf(decision.messages[0]);
|
|
252
|
+
assert.match(shown, /没有正文/);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
await test("同名且多台在线:歧义要报出来,不猜目标", async () => {
|
|
256
|
+
posts.length = 0;
|
|
257
|
+
const h = makeHarness({ peers: [peer("mac"), peer("mac", { id: "id-mac-2", port: peerPort + 1 })] });
|
|
258
|
+
const resolved = resolvePeerTarget(h.deps.discovery, "mac");
|
|
259
|
+
assert.match(String(resolved.error || ""), /匹配到 2 台机器/);
|
|
260
|
+
const { decision } = await h.step("@mac 看看磁盘");
|
|
261
|
+
assert.equal(posts.length, 0);
|
|
262
|
+
assert.match(textOf(decision.messages[0]), /匹配到 2 台机器/);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
await test("一键关闭(全部关):不发包,并说明是本机自己关的", async () => {
|
|
266
|
+
posts.length = 0;
|
|
267
|
+
const h = makeHarness({ allOff: true });
|
|
268
|
+
const { decision } = await h.step("@mac 看看磁盘");
|
|
269
|
+
assert.equal(posts.length, 0);
|
|
270
|
+
assert.match(textOf(decision.messages[0]), /已全部关闭/);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
console.log("\n── 开关与作用域 ──");
|
|
274
|
+
|
|
275
|
+
await test("atDispatch=false → 完全不挂监听器", async () => {
|
|
276
|
+
const h = makeHarness({ atDispatch: false });
|
|
277
|
+
assert.equal(h.listener, undefined);
|
|
278
|
+
assert.ok(h.logs.some((l) => l.includes("已关闭")), "关闭也要留一句话,别让人以为坏了");
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
await test("非 user 来源的消息(对端回执 / 插件注入)不参与 @ 派发", async () => {
|
|
282
|
+
posts.length = 0;
|
|
283
|
+
const h = makeHarness();
|
|
284
|
+
const webhook = {
|
|
285
|
+
id: "w-1",
|
|
286
|
+
role: "user",
|
|
287
|
+
source: { kind: "webhook" },
|
|
288
|
+
content: [{ type: "text", text: "@mac 看看磁盘" }],
|
|
289
|
+
};
|
|
290
|
+
await h.step("@mac 看看磁盘", { role: webhook });
|
|
291
|
+
assert.equal(posts.length, 0);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
console.log("\n── 对端闸门:人手动派发的短消息不能被吞成「仅记录」──");
|
|
295
|
+
|
|
296
|
+
const gateConfig = { minTaskChars: 8, maxWakesPerThread: 30, threadWindowMs: 3600000, threadCooldownMs: 5000 };
|
|
297
|
+
|
|
298
|
+
await test("6 个字的 @ 任务:带 human 时放行(否则就是真机遇到的「仅记录」)", () => {
|
|
299
|
+
const task = { fromMachine: "lipeng-mac", thread: "at-t-1", task: "你的磁盘占用", human: true };
|
|
300
|
+
const verdict = classifyInbound(task, new Map(), gateConfig, Date.now(), () => 30);
|
|
301
|
+
assert.equal(verdict.wake, true, `被判成 ${verdict.kind}: ${verdict.reason}`);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
await test("同样的短消息,没有 human 标记 → 仍然只入箱(agent 互刷的闸门不能被拆掉)", () => {
|
|
305
|
+
const task = { fromMachine: "lipeng-mac", thread: "at-t-1", task: "你的磁盘占用" };
|
|
306
|
+
const verdict = classifyInbound(task, new Map(), gateConfig, Date.now(), () => 30);
|
|
307
|
+
assert.equal(verdict.wake, false);
|
|
308
|
+
assert.equal(verdict.kind, "short");
|
|
309
|
+
assert.match(verdict.reason, /minTaskChars/);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
await test("纯寒暄(「收到」)带 human 也放行;不带 human 仍被拦成 notice", () => {
|
|
313
|
+
// minTaskChars 调到 1,让这条只测"寒暄闸门"(默认 8 时它先被长度闸门拦住)。
|
|
314
|
+
const noLengthGate = { ...gateConfig, minTaskChars: 1 };
|
|
315
|
+
const human = classifyInbound({ fromMachine: "a", thread: "t", task: "收到", human: true }, new Map(), noLengthGate, Date.now(), () => 30);
|
|
316
|
+
assert.equal(human.wake, true, `被判成 ${human.kind}: ${human.reason}`);
|
|
317
|
+
const agent = classifyInbound({ fromMachine: "a", thread: "t", task: "收到" }, new Map(), noLengthGate, Date.now(), () => 30);
|
|
318
|
+
assert.equal(agent.wake, false);
|
|
319
|
+
assert.equal(agent.kind, "notice");
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
await test("noWake 的优先级高于 human:显式回执仍不唤醒", () => {
|
|
323
|
+
const verdict = classifyInbound(
|
|
324
|
+
{ fromMachine: "a", thread: "t", task: "你的磁盘占用", human: true, noWake: true },
|
|
325
|
+
new Map(),
|
|
326
|
+
gateConfig,
|
|
327
|
+
Date.now(),
|
|
328
|
+
() => 30,
|
|
329
|
+
);
|
|
330
|
+
assert.equal(verdict.wake, false);
|
|
331
|
+
assert.equal(verdict.kind, "noWake");
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
await test("限速闸门对 human 仍然生效(人类也不能把对端刷爆)", () => {
|
|
335
|
+
const now = Date.now();
|
|
336
|
+
const key = conversationKey({ fromMachine: "a", thread: "t" });
|
|
337
|
+
const state = new Map([[key, { wakesAt: Array.from({ length: 3 }, () => now - 1000), sessionId: "s" }]]);
|
|
338
|
+
const verdict = classifyInbound({ fromMachine: "a", thread: "t", task: "看下磁盘和内存", human: true }, state, gateConfig, now, () => 3);
|
|
339
|
+
assert.equal(verdict.wake, false);
|
|
340
|
+
assert.equal(verdict.kind, "cap");
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
console.log("\n── 假对端记录 ──");
|
|
344
|
+
await test("派发失败(对端 500)也要改写消息并说出原因", async () => {
|
|
345
|
+
const failing = http.createServer((req, res) => {
|
|
346
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
347
|
+
res.end(JSON.stringify({ error: "对端内部错误" }));
|
|
348
|
+
});
|
|
349
|
+
await new Promise((resolve) => failing.listen(0, "127.0.0.1", resolve));
|
|
350
|
+
const failPort = failing.address().port;
|
|
351
|
+
const h = makeHarness({
|
|
352
|
+
peers: [
|
|
353
|
+
{
|
|
354
|
+
id: "id-bad",
|
|
355
|
+
machine: "bad",
|
|
356
|
+
online: true,
|
|
357
|
+
host: "127.0.0.1",
|
|
358
|
+
port: failPort,
|
|
359
|
+
transport: "lan",
|
|
360
|
+
endpoints: [{ transport: "lan", host: "127.0.0.1", port: failPort, lastSeen: Date.now() }],
|
|
361
|
+
},
|
|
362
|
+
],
|
|
363
|
+
});
|
|
364
|
+
const { decision } = await h.step("@bad 看看磁盘");
|
|
365
|
+
const shown = textOf(decision.messages[0]);
|
|
366
|
+
assert.match(shown, /未能派发/);
|
|
367
|
+
assert.match(shown, /对端内部错误/);
|
|
368
|
+
assert.equal(h.outbound.length, 1);
|
|
369
|
+
assert.equal(h.outbound[0].ok, false);
|
|
370
|
+
await new Promise((resolve) => failing.close(resolve));
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
server.close();
|
|
374
|
+
rmSync(root, { recursive: true, force: true });
|
|
375
|
+
|
|
376
|
+
console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败\n`);
|
|
377
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
@@ -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);
|
package/lib/_selftest-prompt.mjs
CHANGED
|
@@ -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):预建会话里先注入
|
|
@@ -46,7 +46,7 @@ async function makeNode(machine) {
|
|
|
46
46
|
"@deepseek-ai/dsh-tools": { defineTool: (tool) => tool },
|
|
47
47
|
"@deepseek-ai/dsh-llm": { createUserMessage: (message) => message },
|
|
48
48
|
};
|
|
49
|
-
const module = new vm.SourceTextModule(entrySource + "\nexport { registerTools, startPeerServer };", {
|
|
49
|
+
const module = new vm.SourceTextModule(entrySource + "\nexport { registerTools, startPeerServer, dispatchTaskToPeer };", {
|
|
50
50
|
context, identifier: entryUrl.href, initializeImportMeta: (meta) => { meta.url = entryUrl.href; },
|
|
51
51
|
});
|
|
52
52
|
await module.link(async (specifier) => {
|
|
@@ -91,6 +91,8 @@ async function makeNode(machine) {
|
|
|
91
91
|
};
|
|
92
92
|
const options = { ...node, discovery, config: node.config, log: noop, warn: noop, waker, meshSwitch, capFor: () => node.config.maxWakesPerThread, capOverrides: new Map(), throttled: new Map() };
|
|
93
93
|
// startPeerServer 现在返回句柄(一键关闭要重绑监听地址),端口在 handle.port 上。
|
|
94
|
+
node.options = options;
|
|
95
|
+
node.dispatch = (input) => module.namespace.dispatchTaskToPeer(options, input);
|
|
94
96
|
const handle = await module.namespace.startPeerServer(ctx, options);
|
|
95
97
|
node.port = handle.port;
|
|
96
98
|
node.server = handle.server;
|
|
@@ -190,6 +192,52 @@ try {
|
|
|
190
192
|
assert.equal(a.created, 1);
|
|
191
193
|
console.log("✓ noWake, wake cap and explicit newSession still apply");
|
|
192
194
|
|
|
195
|
+
// 人发起的派发(聊天窗口 @ 设备)→ 对端回执即使标了 noWake 也必须投递。
|
|
196
|
+
//
|
|
197
|
+
// 真机 2026-10-30:dshgo-dsh 把"机器状态回报"当成回执发了 noWake,结果
|
|
198
|
+
// 停在 noted-only,会话里一个字都看不到 —— 用户等着的答案被"只记录"吞了。
|
|
199
|
+
// 闸门该拦的是 agent 互刷的寒暄,不是人问的问题的答案。
|
|
200
|
+
const humanDispatch = await a.dispatch({
|
|
201
|
+
id: "t-human-1",
|
|
202
|
+
target: b.peer,
|
|
203
|
+
task: "查一下你的机器负载,内存网络等",
|
|
204
|
+
thread: "at-t-human-1",
|
|
205
|
+
originSession: "original-session",
|
|
206
|
+
human: true,
|
|
207
|
+
});
|
|
208
|
+
assert.equal(humanDispatch.ok, true, JSON.stringify(humanDispatch));
|
|
209
|
+
await until(() => ["dispatched", "injected-thread-session"].includes(b.inbound.at(-1)?.status));
|
|
210
|
+
const humanReplyBefore = original.messages.length;
|
|
211
|
+
// 对端 agent 的回执从它为本任务建的那个会话来发(originSession 必须与
|
|
212
|
+
// 它本机绑定的会话一致,否则 mesh_run 会拒绝跨会话派发)。
|
|
213
|
+
const humanSession = [...b.agents.keys()].at(-1);
|
|
214
|
+
assert.match(String(humanSession), /^mesh-B-/, "对端应当为本任务建了会话");
|
|
215
|
+
const humanSendText = await b.send("A", "at-t-human-1", humanSession, { noWake: true });
|
|
216
|
+
assert.match(String(humanSendText), /已派发/, String(humanSendText));
|
|
217
|
+
const humanReplyRec = a.inbound.at(-1);
|
|
218
|
+
assert.equal(humanReplyRec.thread, "at-t-human-1");
|
|
219
|
+
assert.notEqual(humanReplyRec.status, "noted-only", "人发起派发的回执不能被「仅记录」吞掉");
|
|
220
|
+
await until(() => original.messages.length > humanReplyBefore);
|
|
221
|
+
assert.equal(a.created, 1, "回执要注入原会话,不能再新建会话");
|
|
222
|
+
console.log("✓ noWake reply to a human-initiated dispatch still lands in the live session");
|
|
223
|
+
|
|
224
|
+
// 但 agent 之间没有 human 标记的 noWake 回执,仍然只记录(循环刹车不能被拆)。
|
|
225
|
+
const agentReplyBefore = original.messages.length;
|
|
226
|
+
const sentAgent = await a.dispatch({
|
|
227
|
+
id: "t-agent-1",
|
|
228
|
+
target: b.peer,
|
|
229
|
+
task: "agent 之间的例行核对:这是一条没有 human 标记的普通长任务文本",
|
|
230
|
+
thread: "agent-thread-1",
|
|
231
|
+
originSession: "original-session",
|
|
232
|
+
});
|
|
233
|
+
assert.equal(sentAgent.ok, true);
|
|
234
|
+
await until(() => ["dispatched", "injected-thread-session"].includes(b.inbound.at(-1)?.status));
|
|
235
|
+
const agentSession = [...b.agents.keys()].at(-1);
|
|
236
|
+
assert.match(String(await b.send("A", "agent-thread-1", agentSession, { noWake: true })), /已派发/);
|
|
237
|
+
assert.equal(a.inbound.at(-1).status, "noted-only");
|
|
238
|
+
assert.equal(original.messages.length, agentReplyBefore, "agent 互刷的 noWake 仍然不唤醒");
|
|
239
|
+
console.log("✓ agent-originated noWake replies stay recorded-only");
|
|
240
|
+
|
|
193
241
|
// 一键关闭的工具面:三个范围、默认关闭、不认识的范围要明确报错。
|
|
194
242
|
const switchTool = b.tools.get("mesh_switch");
|
|
195
243
|
assert.ok(switchTool, "必须注册 mesh_switch");
|
package/lib/_selftest-update.mjs
CHANGED
|
@@ -263,5 +263,14 @@ await testAsync("没有 fetch 的运行环境 → 说清楚无法检查,而不
|
|
|
263
263
|
assert.ok(r.error.includes("fetch"), r.error);
|
|
264
264
|
});
|
|
265
265
|
|
|
266
|
+
test("pendingUpgrade 原样带出:磁盘装好了但进程没重启,界面靠它给重启/重载", () => {
|
|
267
|
+
const pending = { version: "0.6.0", previous: "0.5.6", restartRequired: true, reloadRequired: false, inferred: false };
|
|
268
|
+
const r = buildUpdateReport({ current: "0.5.6", diskVersion: "0.6.0", latest: okLatest, pendingUpgrade: pending });
|
|
269
|
+
assert.equal(r.pendingUpgrade.restartRequired, true);
|
|
270
|
+
assert.equal(r.pendingUpgrade.version, "0.6.0");
|
|
271
|
+
const none = buildUpdateReport({ current: "0.6.0", diskVersion: "0.6.0", latest: okLatest });
|
|
272
|
+
assert.equal(none.pendingUpgrade, null);
|
|
273
|
+
});
|
|
274
|
+
|
|
266
275
|
console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败`);
|
|
267
276
|
process.exit(failed === 0 ? 0 : 1);
|