@sleep2agi/commhub-server 0.9.0-preview.230 → 0.9.0-preview.232

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.230",
3
+ "version": "0.9.0-preview.232",
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",
@@ -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)" },
@@ -663,6 +663,25 @@ describe("#473 anonymous /health — watchdog contract intact, no topology leak"
663
663
  // #3174 —— stall_count / last_stall_at 原来只有 [startedAt, now] 这个**隐含**窗口:
664
664
  // 单独引用它,读者会把「计数器归零」读成「从未卡过」。
665
665
  // ⇒ 现在窗口两端跟着读数一起走(event_loop.since / event_loop.observed_at)。
666
+ // 2026-10-31 —— **0 只能作为读数出现,不能冒充「没测」**(外部复核 ④:
667
+ // 「『未取』被系统翻译成『正常』——而零恰好是最好看的那个值」)。
668
+ // event_loop 的计数类字段天然可能是 0(真的没停顿)。所以必须能区分:
669
+ // 窗口存在 ⇒ 0 是「这段时间内没有停顿」(读数)
670
+ // 窗口为零长 ⇒ 0 是「还没测」(不是读数)
671
+ // 断言:窗口两端必须同时存在,且 since ≤ observed_at;窗口为空时**不得**声称 0 是结论。
672
+ test("zero-valued stall counters carry a window, so 0 is a reading rather than 'not measured'", async () => {
673
+ const body = await (await fetch(`${BASE}/health`)).json() as any;
674
+ const el = body.event_loop;
675
+ const since = Date.parse(el.since), observed = Date.parse(el.observed_at);
676
+ expect(Number.isFinite(since)).toBe(true);
677
+ expect(Number.isFinite(observed)).toBe(true);
678
+ expect(observed).toBeGreaterThanOrEqual(since);
679
+ if (el.stall_count === 0) {
680
+ // 0 停顿 + 正长度窗口 = 读数;0 停顿 + 零长度窗口 = 还没测,此时不能当成「没有停顿」
681
+ expect(observed - since).toBeGreaterThan(0);
682
+ }
683
+ });
684
+
666
685
  describe("event_loop carries its own window (no implicit [startedAt, now])", () => {
667
686
  test("both ends are present, and they are ordered", async () => {
668
687
  const body = await (await fetch(`${BASE}/health`)).json() as any;
@@ -1102,23 +1121,23 @@ describe("#473 anonymous /health — watchdog contract intact, no topology leak"
1102
1121
  // 2026-10-31 —— **有覆盖、零仲裁**(外部复核 (d)):version 有两个来源
1103
1122
  // (standalone 二进制注入的 env / 磁盘 package.json),此前只报结果、不报谁赢了。
1104
1123
  // version_source 就是那个仲裁位;下面四例钉住它的语义。
1105
- 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 () => {
1106
1125
  const { resolveServerVersion } = await import("./server.js");
1107
1126
  expect(resolveServerVersion("0.9.0-preview.999", () => "0.9.0-preview.1"))
1108
1127
  .toEqual({ version: "0.9.0-preview.999", source: "embedded_env" });
1109
1128
  // env 形状不对 ⇒ 落到磁盘,而不是把非法值当版本号
1110
1129
  expect(resolveServerVersion("latest", () => "0.9.0-preview.1"))
1111
1130
  .toEqual({ version: "0.9.0-preview.1", source: "package_json" });
1112
- // 磁盘读失败 ⇒ unknown,而不是伪装成某个来源
1131
+ // 磁盘读失败 ⇒ read_failed;读到空 ⇒ empty_value(**两个成因两个值**,不是一个 unknown)
1113
1132
  expect(resolveServerVersion(undefined, () => { throw new Error("ENOENT"); }))
1114
- .toEqual({ version: "?", source: "unknown" });
1133
+ .toEqual({ version: "?", source: "read_failed" });
1115
1134
  expect(resolveServerVersion(" ", () => "?"))
1116
- .toEqual({ version: "?", source: "unknown" });
1135
+ .toEqual({ version: "?", source: "empty_value" });
1117
1136
 
1118
1137
  // 线上读数与语义自洽:unknown ⇔ version 是 "?"
1119
1138
  const body = await (await fetch(`${BASE}/health`)).json() as any;
1120
- expect(["embedded_env", "package_json", "unknown"]).toContain(body.version_source);
1121
- 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("?");
1122
1141
  else expect(body.version).not.toBe("?");
1123
1142
  });
1124
1143
 
@@ -406,4 +406,32 @@ describe("GET /api/providers", () => {
406
406
  db.run(`DELETE FROM nodes WHERE node_id = ?1`, [nodeId]);
407
407
  }
408
408
  });
409
+
410
+ // 2026-10-31 —— **格式约束与数量约束分开**(外部复核 (a):
411
+ // 「白名单管『怎么写』,这条管『写几处』」)。
412
+ // 这两张状态映射各自只能有**一处定义**,且必须被生产代码**至少读一次**:
413
+ // 只有定义、没有读取 ⇒ 它是一张装饰表(写 1 处、读 0 处)。
414
+ test("status maps are defined exactly once and actually read by production code", async () => {
415
+ const { readFileSync, readdirSync, statSync } = await import("node:fs");
416
+ const { join } = await import("node:path");
417
+ const root = join(import.meta.dir);
418
+ const walk = (dir: string, out: string[] = []): string[] => {
419
+ for (const name of readdirSync(dir)) {
420
+ const full = join(dir, name);
421
+ if (statSync(full).isDirectory()) walk(full, out);
422
+ else if (name.endsWith(".ts") && !name.endsWith(".test.ts")) out.push(full);
423
+ }
424
+ return out;
425
+ };
426
+ const files = walk(root).map((f) => ({ f, src: readFileSync(f, "utf8") }));
427
+ for (const name of ["PROVIDER_WRITE_STATUS", "TAG_CREATE_STATUS"]) {
428
+ const defs = files.filter(({ src }) => src.includes(`export const ${name}`));
429
+ expect(defs.length).toBe(1);
430
+ const readers = files.flatMap(({ f, src }) =>
431
+ src.split("\n").map((line, i) => ({ line, f, i }))
432
+ .filter(({ line }) => line.includes(name) && !line.includes(`export const ${name}`)));
433
+ // 「读」= 出现但不是定义行(索引 / 展开 / 断言都算)
434
+ expect(readers.length).toBeGreaterThan(0);
435
+ }
436
+ });
409
437
  });
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 })); }