@sleep2agi/commhub-server 0.9.0-preview.234 → 0.9.0-preview.235

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.234",
3
+ "version": "0.9.0-preview.235",
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",
@@ -1,3 +1,16 @@
1
+ // 2026-10-31 —— **本地直跑会看到 4 条红,那不是回归**(与 hub-provider-config.test.ts 同一类)。
2
+ //
3
+ // 实测(本机,逐文件跑,独立 DB):
4
+ // PASS 13 / FAIL 4
5
+ // 失败点的期望都是 status:"succeeded",实际收到 "failed"
6
+ // (reconcileProviderSnapshots 写回前会对 `daemon_provider_writes` 做一次
7
+ // validateWrite 的逐字段确认;本地没有那套容器网络/编译产物时这一步必失败)
8
+ //
9
+ // 反证(**这一步才是关键**,否则「环境问题」只是一句免罪声明):
10
+ // 把本次改动 `git stash -u` 后在同一个 HEAD 上重跑 ⇒ PASS 13 / FAIL 4,同一个 4 条。
11
+ // ⇒ 它在我改之前就红,且红的集合完全相同。
12
+ //
13
+ // 判定这个文件是否健康,看 `hub-provider-core.yml` 的容器测试,不看本地 bun test。
1
14
  import "./require-explicit-test-db";
2
15
  import { afterAll, beforeEach, expect, test } from "bun:test";
3
16
  import { readFileSync, mkdtempSync, mkdirSync, writeFileSync, rmSync, chmodSync } from "node:fs";
@@ -312,7 +312,12 @@ describe("GET /api/providers", () => {
312
312
  expect("hint" in b.body).toBe(false);
313
313
 
314
314
  // ③ 已生效:selection 原样带回,并给出 revision
315
- q("dpa_probe_2", "completed", JSON.stringify({ expected_revision: 7, provider_id: "main", model: "m1" }), null);
315
+ // 2026-10-31 —— 这条 fixture 原来写 `"completed"`,而**生产端从不写这个值**
316
+ // (daemon 成功时写的是 `succeeded`,见 daemon-provider-snapshots.ts:322)。
317
+ // 于是这条断言只在「兜底分支」上成立:它证明的不是「applied 状态能读到」,
318
+ // 而是「任何没枚举到的值都会被读成 applied」——一条把缺陷当契约保护起来的断言。
319
+ // ⇒ fixture 改用生产者真的会写的值;兜底方向另立一条断言(见下面的 unknown 用例)。
320
+ q("dpa_probe_2", "succeeded", JSON.stringify({ expected_revision: 7, provider_id: "main", model: "m1" }), null);
316
321
  const c = await read();
317
322
  expect(c.body.state).toBe("applied");
318
323
  expect(c.body.selection).toMatchObject({ provider_id: "main", model: "m1" });
@@ -324,6 +329,17 @@ describe("GET /api/providers", () => {
324
329
  const d = await read();
325
330
  expect(d.body.state).toBe("failed");
326
331
  expect(d.body.error).toBe("daemon_rejected_selection");
332
+
333
+ // ⑤ 未知状态:**兜底不许是最乐观的那一档**
334
+ // 原实现是 `… ? "failed" : "applied"` ⇒ 任何没被枚举到的 status 都折叠成「已生效」。
335
+ // daemon 侧只要新增一个状态(取消 / 回滚 / 拼错),界面就会读到「切好了」—— 唯一会主动撒谎的折叠方向。
336
+ // 现在闭集显式映射,不认识的值报 unknown 且把原值带出去(不猜、不美化)。
337
+ q("dpa_probe_4", "cancelled_by_operator", null, null);
338
+ const e = await read();
339
+ expect(e.body.state).toBe("unknown");
340
+ expect(e.body.state).not.toBe("applied");
341
+ expect(e.body.observed_status).toBe("cancelled_by_operator");
342
+ expect(String(e.body.hint)).toContain("不等于已生效");
327
343
  } finally {
328
344
  db.run(`DELETE FROM daemon_provider_snapshots WHERE daemon_node_id = ?1`, [nodeId]);
329
345
  db.run(`DELETE FROM node_daemon_bindings WHERE request_id = 'bind_ps'`);
package/src/server.ts CHANGED
@@ -3947,10 +3947,19 @@ return Bun.serve({
3947
3947
  ORDER BY created_at DESC LIMIT 1`,
3948
3948
  node.network_id, node.node_id);
3949
3949
  // 三态而不是「有/没有」:从未切换过 / 正在切 / 切失败,界面要给的下一步完全不同。
3950
- const state = !row ? "never_switched"
3951
- : row.status === "pending" ? "pending"
3952
- : row.status === "failed" || row.status === "expired" ? "failed"
3953
- : "applied";
3950
+ //
3951
+ // 2026-10-31 —— **兜底不许是最乐观的那一档**:
3952
+ // 原来写的是 `… ? "failed" : "applied"`,即任何**没被枚举到**的 status 都会折叠成「已生效」。
3953
+ // 而 daemon 侧新增一个状态(取消 / 回滚 / 拼错的 succeeded_)时,界面会读到「切好了」——
3954
+ // 这是所有折叠方向里唯一会**主动撒谎**的方向。
3955
+ // ⇒ 改成闭集显式映射;不在表里的 status 报 unknown 并把原值带出去(不猜、不美化)。
3956
+ const SELECTION_STATE_BY_STATUS: Record<string, "pending" | "applied" | "failed"> = {
3957
+ pending: "pending",
3958
+ succeeded: "applied", // daemon 侧成功的实际取值(不是 "applied")
3959
+ failed: "failed",
3960
+ expired: "failed", // 过期=没生效;对使用者与失败同一档
3961
+ };
3962
+ const state = !row ? "never_switched" : (SELECTION_STATE_BY_STATUS[row.status] ?? "unknown");
3954
3963
  let selection: unknown = null;
3955
3964
  if (row?.snapshot_json) { try { selection = JSON.parse(row.snapshot_json); } catch { selection = null; } }
3956
3965
  return withCors(req, Response.json({
@@ -3964,6 +3973,12 @@ return Bun.serve({
3964
3973
  ...(state === "never_switched"
3965
3974
  ? { hint: "这个节点还没有被切换过 provider;显示的是它的本地默认配置(不是 Hub 的读数)" }
3966
3975
  : {}),
3976
+ ...(state === "unknown"
3977
+ ? {
3978
+ observed_status: row?.status ?? null,
3979
+ hint: "Hub 读到一个它不认识的切换状态;**这不等于已生效**,也不等于失败 —— 去 daemon 侧看这次请求的实际结果",
3980
+ }
3981
+ : {}),
3967
3982
  }));
3968
3983
  }
3969
3984
  // 2026-10-31 —— **写侧:切这个节点的 provider / 模型**(#3146 的第一个动作面)。