@sleep2agi/commhub-server 0.9.0-preview.241 → 0.9.0-preview.242

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.241",
3
+ "version": "0.9.0-preview.242",
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",
@@ -1338,6 +1338,21 @@ describe("#473 anonymous /health — watchdog contract intact, no topology leak"
1338
1338
  expect(/git|commit|build[-_]?(sha|id)?/.test("x-commhub")).toBe(false);
1339
1339
  });
1340
1340
 
1341
+ // 2026-10-31 —— **一致时零证据**(外部复核:「status / db / mineruProxy 三个字段是同一个位,
1342
+ // 一致时零证据」)。本仓的 status 与 status_reasons 由构造保证一致 ⇒
1343
+ // 「status=='ok' ⟺ reasons 为空」是恒真断言,保护不了任何东西。
1344
+ // ⇒ 改成断言**映射本身**:给一个理由就必须 degraded;且映射是纯函数、可被单独喂输入。
1345
+ test("status mapping is a pure function of the reasons list (one reason is enough to degrade)", async () => {
1346
+ const { statusFromReasons } = await import("./server.js");
1347
+ expect(statusFromReasons([])).toBe("ok");
1348
+ expect(statusFromReasons(["db_unreadable"])).toBe("degraded");
1349
+ expect(statusFromReasons(["license_unreadable"])).toBe("degraded");
1350
+ expect(statusFromReasons(["anything_at_all"])).toBe("degraded"); // 不认识的理由也不许吞
1351
+ // 且线上载荷与函数一致(不是两套逻辑)
1352
+ const live = await (await fetch(`${BASE}/health`)).json() as any;
1353
+ expect(live.status).toBe(statusFromReasons(live.status_reasons ?? []));
1354
+ });
1355
+
1341
1356
  test("no dead entries: every allow-listed key is actually served to anonymous callers", async () => {
1342
1357
  const { PUBLIC_HEALTH_KEYS } = await import("./server.js");
1343
1358
  const plain = await (await fetch(`${BASE}/health`)).json() as any;
@@ -375,6 +375,27 @@ describe("GET /api/providers", () => {
375
375
  // ⇒ A、B 两条向量改变的是同一个出口量」)。
376
376
  // 对应到本路由:五个 state 如果**去掉 state 字段**就分不出来,那 state 就是唯一信息源,
377
377
  // 载荷只是标签的复读 —— 秩 = 1。判据:把 state 删掉之后,五个载荷仍两两可区分。
378
+ // 2026-10-31 —— **从属字段在主条件为假时不得有值**(外部复核 (d):
379
+ // 「从属字段在主字段为假时不得有值;不校验就会存下不可能的状态,
380
+ // 后来者无法区分它与历史遗留,只会当成有效读数。」)
381
+ // 本路由的三个从属键:
382
+ // error ⟺ state === "failed"
383
+ // observed_status ⟺ state === "unknown"
384
+ // hint ⟺ state ∈ {never_switched, unknown}(pending/applied 不该有 hint)
385
+ const has = (o: any, k: string) => Object.prototype.hasOwnProperty.call(o, k);
386
+ const cases: Array<[string, any, { error: boolean; observed_status: boolean; hint: boolean }]> = [
387
+ ["never_switched", a.body, { error: false, observed_status: false, hint: true }],
388
+ ["pending", b.body, { error: false, observed_status: false, hint: false }],
389
+ ["applied", c.body, { error: false, observed_status: false, hint: false }],
390
+ ["failed", d.body, { error: true, observed_status: false, hint: false }],
391
+ ["unknown", e.body, { error: false, observed_status: true, hint: true }],
392
+ ];
393
+ for (const [label, body, want] of cases) {
394
+ expect({ label, error: has(body, "error") }).toEqual({ label, error: want.error });
395
+ expect({ label, observed: has(body, "observed_status") }).toEqual({ label, observed: want.observed_status });
396
+ expect({ label, hint: has(body, "hint") }).toEqual({ label, hint: want.hint });
397
+ }
398
+
378
399
  const strip = (b: any) => { const { state: _s, ...rest } = b; return JSON.stringify(rest); };
379
400
  const five = [a, b, c, d, e].map((r) => strip(r.body));
380
401
  expect(new Set(five).size).toBe(5); // 两两不同 ⇒ 秩 ≥ 2,state 不是唯一维度
package/src/server.ts CHANGED
@@ -1212,6 +1212,17 @@ export const SELECTION_STATE_BY_STATUS: Record<string, "pending" | "applied" | "
1212
1212
  expired: "failed", // 过期=没生效;对使用者与失败同一档
1213
1213
  };
1214
1214
 
1215
+ /**
1216
+ * 2026-10-31 —— `status` 与 `status_reasons` **是同一个位的两种说法**(外部复核:
1217
+ * 「三字段一致时零证据」)。既然它们由构造保证一致,那么「status=='ok' ⟺ reasons 为空」
1218
+ * 这条断言**恒真**,它保护不了任何东西。
1219
+ * ⇒ 把映射抽成一个纯函数,测试改为断言**映射本身**(给一个理由就必须 degraded),
1220
+ * 这样「有人把映射改成忽略 reasons」会红,而「两者一致」这种同义反复不会写成测试。
1221
+ */
1222
+ export function statusFromReasons(reasons: readonly string[]): "ok" | "degraded" {
1223
+ return reasons.length === 0 ? "ok" : "degraded";
1224
+ }
1225
+
1215
1226
  export const HEALTH_FIELD_META: Readonly<Record<string, HealthFieldMeta>> = {
1216
1227
  // ── gauge:聚合 / 粗标签 ─────────────────────────────────────────────
1217
1228
  ok: {
@@ -3680,7 +3691,7 @@ return Bun.serve({
3680
3691
  const body: Record<string, unknown> = {
3681
3692
  ok: true,
3682
3693
  // 200 是**承诺**(watchdog 契约),不等于「健康」。健康与否放在 body 里:
3683
- status: statusReasons.length === 0 ? "ok" : "degraded",
3694
+ status: statusFromReasons(statusReasons),
3684
3695
  status_reasons: statusReasons,
3685
3696
  version: SERVER_VERSION,
3686
3697
  version_source: SERVER_VERSION_SOURCE,