@sleep2agi/commhub-server 0.9.0-preview.231 → 0.9.0-preview.233

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/commhub-server",
3
- "version": "0.9.0-preview.231",
3
+ "version": "0.9.0-preview.233",
4
4
  "description": "CommHub Server — AI Agent communication hub with MCP protocol, multi-network isolation, user auth, and MCP tools (17 collaboration-core + node/provider ops tools; authoritative list: docs-site/docs/api/mcp-tools.md).",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/clip.ts CHANGED
@@ -9,8 +9,10 @@
9
9
  * `s.slice(0, n)` 数的是 UTF-16 code unit,而一个 emoji(或任何 BMP 外字符)占两个。
10
10
  * 若第 n 个位置正好落在高代理与低代理之间,切出来的字符串含**孤立高代理**:
11
11
  * · JSON.stringify 会把它写成 "\ud83d"(语法上合法)
12
- * · 编码成 UTF-8 时被替换成 U+FFFD 或直接报错(取决于运行时)
13
- * · 于是同一行日志在不同管道里长得不一样,而 join 用的正是这一串
12
+ * · 2026-10-31 更正(外部复核实测):Node 编码时**不是报错**,而是把孤立代理替换成 U+FFFD(efbfbd)
13
+ * ⇒ 危害是**静默、不可逆、非单射**:两种不同的坏输入会编码成同一个字节串,
14
+ * 而「日志里那一串」从此既不能 join、也不能反推原文。
15
+ * · 所以这里要防的不是「非法 UTF-8」,是「**悄悄换掉一个字符**」。
14
16
  *
15
17
  * 本函数只在边界上处理:若切点落在代理对中间,就少取一个 code unit,保证输出始终是
16
18
  * 「合法 UTF-16 子串」。**它不改变长度上限的语义**(仍然是 ≤ max),只保证不产生孤立代理。
@@ -34,7 +34,7 @@ export const PUBLIC_HEALTH_KEY_READERS: Readonly<Record<string, AnonReaderEntry>
34
34
  status: { reader: "cli", why: "客户端展示 Hub 的细分状态(ok / degraded)" },
35
35
  status_reasons: { reader: "cli", why: "anet status 把 status_reasons 拼成一行原因给用户看(bin/cli.ts)" },
36
36
  version: { reader: "cli", why: "版本不一致提示(anet 与 hub 的版本对齐)" },
37
- version_source: { reader: "operator", why: "运维判断「版本没变」是没发出去还是被 env 覆盖;仓内无代码读" },
37
+ version_source: { reader: "operator", why: "运维判断「版本没变」是没发出去、被 env 覆盖,还是读不到/读到空;仓内无代码读" },
38
38
  version_role: { reader: "external", why: "docs/release-hub-runbook.md 描述的仓外 dashboard 读它区分构建;仓内无消费者,未验" },
39
39
  api_version: { reader: "cli", why: "客户端与 Hub 协商协议版本,不匹配时给出升级提示" },
40
40
  transport: { reader: "cli", why: "协议协商(streamable-http)" },
@@ -1121,23 +1121,23 @@ describe("#473 anonymous /health — watchdog contract intact, no topology leak"
1121
1121
  // 2026-10-31 —— **有覆盖、零仲裁**(外部复核 (d)):version 有两个来源
1122
1122
  // (standalone 二进制注入的 env / 磁盘 package.json),此前只报结果、不报谁赢了。
1123
1123
  // version_source 就是那个仲裁位;下面四例钉住它的语义。
1124
- test("version_source names which override won (embedded_env / package_json / unknown)", async () => {
1124
+ test("version_source names which override won (and splits the two failure causes)", async () => {
1125
1125
  const { resolveServerVersion } = await import("./server.js");
1126
1126
  expect(resolveServerVersion("0.9.0-preview.999", () => "0.9.0-preview.1"))
1127
1127
  .toEqual({ version: "0.9.0-preview.999", source: "embedded_env" });
1128
1128
  // env 形状不对 ⇒ 落到磁盘,而不是把非法值当版本号
1129
1129
  expect(resolveServerVersion("latest", () => "0.9.0-preview.1"))
1130
1130
  .toEqual({ version: "0.9.0-preview.1", source: "package_json" });
1131
- // 磁盘读失败 ⇒ unknown,而不是伪装成某个来源
1131
+ // 磁盘读失败 ⇒ read_failed;读到空 ⇒ empty_value(**两个成因两个值**,不是一个 unknown)
1132
1132
  expect(resolveServerVersion(undefined, () => { throw new Error("ENOENT"); }))
1133
- .toEqual({ version: "?", source: "unknown" });
1133
+ .toEqual({ version: "?", source: "read_failed" });
1134
1134
  expect(resolveServerVersion(" ", () => "?"))
1135
- .toEqual({ version: "?", source: "unknown" });
1135
+ .toEqual({ version: "?", source: "empty_value" });
1136
1136
 
1137
1137
  // 线上读数与语义自洽:unknown ⇔ version 是 "?"
1138
1138
  const body = await (await fetch(`${BASE}/health`)).json() as any;
1139
- expect(["embedded_env", "package_json", "unknown"]).toContain(body.version_source);
1140
- if (body.version_source === "unknown") expect(body.version).toBe("?");
1139
+ expect(["embedded_env", "package_json", "read_failed", "empty_value"]).toContain(body.version_source);
1140
+ if (body.version_source === "read_failed" || body.version_source === "empty_value") expect(body.version).toBe("?");
1141
1141
  else expect(body.version).not.toBe("?");
1142
1142
  });
1143
1143
 
@@ -434,4 +434,33 @@ describe("GET /api/providers", () => {
434
434
  expect(readers.length).toBeGreaterThan(0);
435
435
  }
436
436
  });
437
+
438
+ // 2026-10-31 —— **枚举里不能有打不中的值**(外部复核:
439
+ // 「5 值枚举里 2 个不可达(stage_missing / id_not_uuid)——那是装饰,
440
+ // 而读者会以为那两条分支存在且被测过」)。
441
+ // 判据:每个成员的字面量必须在**生产源码**里出现(类型声明行不算)。
442
+ test("every enum member is actually produced somewhere in production code", async () => {
443
+ const { readFileSync, readdirSync, statSync } = await import("node:fs");
444
+ const { join } = await import("node:path");
445
+ const walk = (dir: string, out: string[] = []): string[] => {
446
+ for (const name of readdirSync(dir)) {
447
+ const full = join(dir, name);
448
+ if (statSync(full).isDirectory()) walk(full, out);
449
+ else if (name.endsWith(".ts") && !name.endsWith(".test.ts")) out.push(full);
450
+ }
451
+ return out;
452
+ };
453
+ const prod = walk(join(import.meta.dir)).map((f) => readFileSync(f, "utf8")).join("\n");
454
+ const enums: Record<string, string[]> = {
455
+ version_source: ["embedded_env", "package_json", "read_failed", "empty_value"],
456
+ provider_selection_state: ["never_switched", "pending", "applied", "failed"],
457
+ };
458
+ for (const [label, members] of Object.entries(enums)) {
459
+ const dead = members.filter((m) => !new RegExp(`["']${m}["']`).test(prod));
460
+ if (dead.length) {
461
+ throw new Error(`${label} 里有打不中成员(生产源码没出现过这个字面量):${dead.join(", ")}\n` +
462
+ " ⇒ 要么删掉该成员,要么补上产出它的分支;留着等于给读者一条不存在的分支。");
463
+ }
464
+ }
465
+ });
437
466
  });
package/src/server.ts CHANGED
@@ -176,7 +176,11 @@ const MCP_LABEL_MAX_BYTES = 1024 * 1024;
176
176
  * · 或以为在用二进制内嵌版本,其实是磁盘那个
177
177
  * 两个方向都只能靠猜。⇒ 现在把来源作为字段一起给出。
178
178
  */
179
- export type VersionSource = "embedded_env" | "package_json" | "unknown";
179
+ // 2026-10-31 —— 外部复核:「diagnosticIdValid 失败时**静默丢键** ⇒ id_absent 与 id_malformed
180
+ // 不可区分。」同一形状在本字段上:原来 unknown 同时表示「读失败」与「读到空」,
181
+ // 而两者的下一步完全不同(查文件权限 vs 修 package.json)。
182
+ // ⇒ 拆成两个值,不新增字段:**两个成因就不是一个取值**。
183
+ export type VersionSource = "embedded_env" | "package_json" | "read_failed" | "empty_value";
180
184
  export function resolveServerVersion(
181
185
  embeddedRaw: string | undefined,
182
186
  readPackageVersion: () => string,
@@ -188,9 +192,9 @@ export function resolveServerVersion(
188
192
  try {
189
193
  const v = readPackageVersion();
190
194
  if (v && v !== "?") return { version: v, source: "package_json" };
191
- return { version: "?", source: "unknown" };
195
+ return { version: "?", source: "empty_value" };
192
196
  } catch {
193
- return { version: "?", source: "unknown" };
197
+ return { version: "?", source: "read_failed" };
194
198
  }
195
199
  }
196
200
  const RESOLVED_VERSION = resolveServerVersion(process.env.ANET_EMBEDDED_COMMHUB_VERSION, () => {
@@ -3928,7 +3932,14 @@ return Bun.serve({
3928
3932
  "SELECT node_id, network_id FROM nodes WHERE node_id = ?1", nodeRef);
3929
3933
  const role = node?.network_id ? getUserNetworkRole(restAuth.userId, node.network_id) : null;
3930
3934
  if (!node || !node.network_id || (!role && !isAdmin)) {
3931
- return withCors(req, Response.json({ ok: false, error: "node_not_found" }, { status: 404 }));
3935
+ // 2026-10-31 —— 外部复核:「当一个选项对应两种修法相反的成因时,它不是选项,是问题的前半句。」
3936
+ // 这里 404 同时表示「不存在」与「不在你的可见范围」,两者修法相反(找对节点 vs 申请权限)。
3937
+ // 合并是**故意的**(否则可以枚举网络里有哪些节点),所以不能靠 hint 猜一个成因 ——
3938
+ // 只能把「这是有意合并的」写进载荷,让前端不臆断。
3939
+ return withCors(req, Response.json({
3940
+ ok: false, error: "node_not_found",
3941
+ note: "不存在,或不在你的可见范围内(两者故意同码,避免枚举)",
3942
+ }, { status: 404 }));
3932
3943
  }
3933
3944
  const row = db.get<{ request_id: string; status: string; observed_at: number | null; snapshot_json: string | null; error: string | null }>(
3934
3945
  `SELECT request_id, status, observed_at, snapshot_json, error FROM daemon_provider_snapshots
@@ -3966,7 +3977,11 @@ return Bun.serve({
3966
3977
  "SELECT node_id, network_id FROM nodes WHERE node_id = ?1", nodeRef);
3967
3978
  const role = node?.network_id ? getUserNetworkRole(restAuth.userId, node.network_id) : null;
3968
3979
  if (!node || !node.network_id || (!role && !isAdmin)) {
3969
- return withCors(req, Response.json({ ok: false, error: "node_not_found" }, { status: 404 }));
3980
+ // 同 GET:404 同时表示「不存在」与「不可见」,两者修法相反,合并是故意的 ⇒ 只说明「有意合并」
3981
+ return withCors(req, Response.json({
3982
+ ok: false, error: "node_not_found",
3983
+ note: "不存在,或不在你的可见范围内(两者故意同码,避免枚举)",
3984
+ }, { status: 404 }));
3970
3985
  }
3971
3986
  let body: any = null;
3972
3987
  try { body = await req.json(); } catch { return withCors(req, Response.json({ ok: false, error: "invalid_json" }, { status: 400 })); }