@sleep2agi/commhub-server 0.9.0-preview.251 → 0.9.0-preview.252

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/build-info.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schema": 1,
3
- "commit": "5e4967928cf61365b6b0b1ed91aee293576a8250",
3
+ "commit": "9bb1a3995a3273eed06927d6cd3f87a1c9042a55",
4
4
  "dirty": false,
5
- "built_at": "2026-10-31T20:01:25.689Z",
5
+ "built_at": "2026-10-31T20:59:13.981Z",
6
6
  "source": "ci"
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/commhub-server",
3
- "version": "0.9.0-preview.251",
3
+ "version": "0.9.0-preview.252",
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",
@@ -0,0 +1,103 @@
1
+ // #3294 —— 节点侧的**字节标识**:report_status 收 build_commit ⇒ /api/nodes 投影可见。
2
+ //
3
+ // 为什么不是「加一列就完了」:Hub 的 report_status schema 对**不认识的键是静默丢弃**的
4
+ // (源码里那段 #1545 注释写得很清楚)。所以这条链路必须三处同时成立:
5
+ // ① schema 里有这个键(否则 zod 丢掉)
6
+ // ② 写路径把它落库(否则写不进去)
7
+ // ③ 读路径把它投影出来(否则界面上看不见)
8
+ // 少任何一处,现场表现都是「节点明明发了、hub 上没有」,与 daemon 侧 bug 一模一样。
9
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
10
+ import { mkdtempSync, rmSync } from "node:fs";
11
+ import { tmpdir } from "node:os";
12
+ import { join } from "node:path";
13
+ import { register, createNetworkTokenForNode } from "./auth.js";
14
+ import { db } from "./db.js";
15
+
16
+ const DIR = mkdtempSync(join(tmpdir(), "anet-build-commit-"));
17
+ const STAMP = Date.now();
18
+ let BASE = "";
19
+ let hub: any = null;
20
+ let USER = "";
21
+ let NET = "";
22
+ let ntok = "";
23
+ let ALIAS = "";
24
+ const PW = "BuildCommitPassw0rd!x";
25
+ const COMMIT = "0123456789abcdef0123456789abcdef01234567"; // 40 hex,固定值便于断言
26
+
27
+ const auth = (t: string) => ({ Authorization: `Bearer ${t}` });
28
+
29
+ async function rpc(method: string, params: any, token = ntok) {
30
+ const res = await fetch(`${BASE}/mcp`, {
31
+ method: "POST",
32
+ headers: { ...auth(token), "Content-Type": "application/json", Accept: "application/json, text/event-stream" },
33
+ body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: method, arguments: params } }),
34
+ });
35
+ const raw = await res.text();
36
+ const line = raw.split("\n").find((l) => l.startsWith("data: "));
37
+ const env = JSON.parse(line ? line.slice(6) : raw);
38
+ try { return JSON.parse(env.result.content[0].text); } catch { return { ok: false, raw }; }
39
+ }
40
+
41
+ async function nodes(token = ntok) {
42
+ const res = await fetch(`${BASE}/api/nodes?network_id=${NET}`, { headers: auth(token) });
43
+ return (await res.json()) as any;
44
+ }
45
+
46
+ beforeAll(async () => {
47
+ process.env.COMMHUB_DB ||= join(DIR, "hub.db");
48
+ process.env.COMMHUB_UPLOADS_DIR = join(DIR, "uploads");
49
+ process.env.HOST = "127.0.0.1";
50
+ USER = `bc_user_${STAMP}`;
51
+ ALIAS = `bc-node-${STAMP}`;
52
+ const reg = register(USER, PW);
53
+ NET = reg.network_id!;
54
+ const minted = createNetworkTokenForNode(reg.user!.user_id, NET, ALIAS, `node_bc_${STAMP}`);
55
+ expect(minted.ok).toBe(true);
56
+ ntok = minted.token!;
57
+ const mod: any = await import("./server.js");
58
+ hub = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
59
+ BASE = `http://127.0.0.1:${hub.port}`;
60
+ }, 30_000);
61
+
62
+ afterAll(() => {
63
+ try { hub?.stop?.(true); } catch {}
64
+ try { rmSync(DIR, { recursive: true, force: true }); } catch {}
65
+ });
66
+
67
+ function report(extra: Record<string, unknown> = {}) {
68
+ return rpc("report_status", {
69
+ resume_id: `resume_bc_${STAMP}`, alias: ALIAS, status: "idle",
70
+ version: "2.5.0-preview.169", node_id: `node_bc_${STAMP}`, network_id: NET, ...extra,
71
+ });
72
+ }
73
+
74
+ describe("#3294 节点上报 build_commit 的三段链路", () => {
75
+ // 顺序是有意的:先没有、再有、再坏。同一行节点,三个阶段各读一次。
76
+ test("① 没报 ⇒ null,且 note 说明「这不是缺陷」", async () => {
77
+ // 老节点不报这个字段;把「没报」与「没有标识」压成同一个 null 会让人去查错方向,
78
+ // 所以投影里同时给一个 note。
79
+ expect((await report()).ok).toBe(true);
80
+ const row = (await nodes()).nodes.find((n: any) => n.alias === ALIAS);
81
+ expect(row).toBeDefined();
82
+ expect(row.build_commit).toBeNull();
83
+ expect(typeof row.build_note).toBe("string");
84
+ expect(row.build_note).toContain("没有上报");
85
+ });
86
+
87
+ test("② 🔴 报了 ⇒ 落库 ⇒ /api/nodes 投影里读得到,且与 version 是两个东西", async () => {
88
+ expect((await report({ build_commit: COMMIT })).ok).toBe(true);
89
+ const row = (await nodes()).nodes.find((n: any) => n.alias === ALIAS);
90
+ expect(row.build_commit).toBe(COMMIT);
91
+ // ⚠️ `/api/nodes` 的投影里**没有** version 这一列(实测:拿到的 row.version 是 undefined)。
92
+ // 所以「两个东西」这句不能靠对比 row.version 来断言 —— 那会变成在测一个不存在的字段。
93
+ // 用上报时的字面量比:字节标识不可能是版本标签。
94
+ expect(row.build_commit).not.toBe("2.5.0-preview.169");
95
+ });
96
+
97
+ test("③ 形状不对的 build_commit 被 schema 拒(不是静默丢弃)", async () => {
98
+ // 与「静默丢弃」相反的那一半:**已知键但值不合规**必须报错,
99
+ // 否则「发了个坏值」会和「没发」同形。
100
+ const r = await report({ resume_id: `resume_bc3_${STAMP}`, build_commit: "NOT-HEX" });
101
+ expect(r.ok).toBeFalsy();
102
+ });
103
+ });
package/src/db.ts CHANGED
@@ -2480,6 +2480,11 @@ try { db.exec("ALTER TABLE sessions ADD COLUMN secret_keys TEXT"); } catch {}
2480
2480
  // the node itself. Appended at EOF like the other post-1.0 columns so the doc
2481
2481
  // and test846 line anchors below it never drift.
2482
2482
  try { db.exec("ALTER TABLE sessions ADD COLUMN env_transport TEXT"); } catch {}
2483
+ // 2026-10-31 —— #3294:节点侧的**字节标识**(与 /health 的 git_commit 同一概念,但对象不同)。
2484
+ // 只有 version 时,「两个 .169 是不同字节」无法分辨;而 #3178 的批量升级前后都要问这句话。
2485
+ // 可为 NULL:老节点不报 ⇒ 「没报」与「没有标识」在这一列上同形,所以读侧必须配一个 reason
2486
+ // (见 /api/nodes 的 build_note)。
2487
+ try { db.exec("ALTER TABLE sessions ADD COLUMN build_commit TEXT"); } catch {}
2483
2488
 
2484
2489
  // #1510 — hourly count of secret writes that a transport gate WOULD have refused
2485
2490
  // (nodes reporting a plain-HTTP hub link). Log-only until an operator decides.
package/src/server.ts CHANGED
@@ -6830,6 +6830,9 @@ return Bun.serve({
6830
6830
  const wantedIds = new Set<string>(wanted.map((r) => String(r.node_id)));
6831
6831
  const transportByKey = new Map<string, string | null>();
6832
6832
  const aliasTransport = new Map<string, string | null>();
6833
+ // #3294 —— 与 transport 同一批行里取 build_commit(同一个键、同一套兜底顺序)。
6834
+ const buildByKey = new Map<string, string | null>();
6835
+ const aliasBuild = new Map<string, string | null>();
6833
6836
  if (wanted.length) {
6834
6837
  // 🔴 编号参数不能复用:`?1..?n` 出现两次,而实参给了 2n 个 ——
6835
6838
  // SQLite 只认得 n 个绑定位置,多出来的直接抛错,整条 GET /api/nodes 变 500。
@@ -6839,19 +6842,21 @@ return Bun.serve({
6839
6842
  const ids = wanted.map((r) => String(r.node_id));
6840
6843
  const phA = ids.map((_, i) => `?${i + 1}`).join(",");
6841
6844
  const phB = ids.map((_, i) => `?${ids.length + i + 1}`).join(",");
6842
- for (const row of db.all<{ node_id: string | null; alias: string | null; network_id: string | null; env_transport: string | null }>(
6843
- `SELECT node_id, alias, network_id, env_transport FROM sessions
6845
+ for (const row of db.all<{ node_id: string | null; alias: string | null; network_id: string | null; env_transport: string | null; build_commit: string | null }>(
6846
+ `SELECT node_id, alias, network_id, env_transport, build_commit FROM sessions
6844
6847
  WHERE node_id IN (${phA}) OR alias IN (${phB})
6845
6848
  ORDER BY updated_at DESC`,
6846
6849
  ...ids, ...ids,
6847
6850
  )) {
6848
6851
  if (row.node_id && wantedIds.has(row.node_id) && !transportByKey.has(row.node_id)) transportByKey.set(row.node_id, row.env_transport);
6852
+ if (row.node_id && wantedIds.has(row.node_id) && !buildByKey.has(row.node_id)) buildByKey.set(row.node_id, row.build_commit ?? null);
6849
6853
  // alias 只在**同一网络**内兜底:不同网络可以有同名 alias,
6850
6854
  // 跨网络命中会把 A 网的 transport 显示成 B 网那台 —— 这一格是给人做安全决策用的,
6851
6855
  // 宁可显示 null(未知)也不能显示错的值。
6852
6856
  if (row.alias && row.network_id && wantedIds.has(row.alias)) {
6853
6857
  const key = `${row.network_id}|${row.alias}`;
6854
6858
  if (!aliasTransport.has(key)) aliasTransport.set(key, row.env_transport);
6859
+ if (!aliasBuild.has(key)) aliasBuild.set(key, row.build_commit ?? null);
6855
6860
  }
6856
6861
  }
6857
6862
  }
@@ -6865,6 +6870,15 @@ return Bun.serve({
6865
6870
  if (byId !== undefined) return byId;
6866
6871
  return alias ? aliasTransport.get(`${networkId}|${alias}`) ?? null : null;
6867
6872
  };
6873
+ // #3294 —— 与 envTransportOf 同构:先按 node_id,再按 (network, node_id),最后按 (network, alias)。
6874
+ const buildCommitOf = (nodeId: string, alias: string | null, networkId: string | null): string | null => {
6875
+ const direct = buildByKey.get(nodeId);
6876
+ if (direct !== undefined) return direct;
6877
+ if (!networkId) return null;
6878
+ const byId = aliasBuild.get(`${networkId}|${nodeId}`);
6879
+ if (byId !== undefined) return byId;
6880
+ return alias ? aliasBuild.get(`${networkId}|${alias}`) ?? null : null;
6881
+ };
6868
6882
  // Add a single new field `role: string | null` per row, extracted from
6869
6883
  // config_snapshot JSON. Daemon nodes that posted role='host_supervisor'
6870
6884
  // via report_status surface here; dashboard uses this for daemon
@@ -6973,6 +6987,10 @@ return Bun.serve({
6973
6987
  // #1510 —— 管理员要能看出「谁的 transport 未知」才敢打开 strict。
6974
6988
  // 这一格**只读**,不参与任何判定(判定在 env-transport-policy.ts)。
6975
6989
  env_transport: envTransportOf(r.node_id, r.alias, r.network_id),
6990
+ // #3294 —— 「跑的是哪一份字节」。null 有两种成因(节点没报 / 老节点不报这个字段),
6991
+ // 所以配一个 note 说明该怎么读它 —— 单一 null 会把两种成因压成一个。
6992
+ build_commit: buildCommitOf(r.node_id, r.alias, r.network_id),
6993
+ build_note: "null = 这个节点没有上报 build_commit(老版本节点不会报);空字符串不出现",
6976
6994
  restart_readiness: restartReadiness(r.node_id, r.alias, r.network_id, role, r.lifecycle_state ?? null),
6977
6995
  };
6978
6996
  });
package/src/tools.ts CHANGED
@@ -884,6 +884,9 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
884
884
  // it knows the last hop. Reporting only; no write path reads it yet.
885
885
  env_transport: z.enum(["loopback", "https", "plain"]).optional().describe("Hub link kind"),
886
886
  version: z.string().max(100).optional().describe("Agent version"),
887
+ // 2026-10-31 —— #3294:**字节标识**。40 位 hex;也接受 12 位前缀(CI 里截过的那种)。
888
+ // 与 version 分开:version 是标签,build_commit 是「哪一份字节」。
889
+ build_commit: z.string().regex(/^[0-9a-f]{7,40}$/).optional().describe("Build commit (hex, 7–40), from the packaged build-info.json"),
887
890
  tmux_name: z.string().max(200).optional().describe("tmux session name"),
888
891
  // V2 fields
889
892
  node_id: z.string().max(200).optional().describe("Stable node identifier"),
@@ -1125,7 +1128,7 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
1125
1128
  // instead of rejecting the whole report (see node-health-store.ts).
1126
1129
  health: nodeHealthSchema,
1127
1130
  },
1128
- async ({ resume_id, alias, status, task, output, score, progress, server: srv, hostname: hn, agent: ag, project_dir: pd, os_user: osUser, version: ver, tmux_name: tmux, node_id, session_id, config_path, channels, model: mdl, node_name: nn, network_id: netId, host, process_telemetry: proc, external_schedules: externalSchedules, config_snapshot: cfgSnap, rules_file_capable: rulesFileCapable, skills_capable: skillsCapable, files_capable: filesCapable, logs_capable: logsCapable, team_rules: teamRules, health: nodeHealth, secret_keys: secretKeys, env_transport: envTransport }) => {
1131
+ async ({ resume_id, alias, status, task, output, score, progress, server: srv, hostname: hn, agent: ag, project_dir: pd, os_user: osUser, version: ver, tmux_name: tmux, node_id, session_id, config_path, channels, model: mdl, node_name: nn, network_id: netId, host, process_telemetry: proc, external_schedules: externalSchedules, config_snapshot: cfgSnap, rules_file_capable: rulesFileCapable, skills_capable: skillsCapable, files_capable: filesCapable, logs_capable: logsCapable, team_rules: teamRules, health: nodeHealth, secret_keys: secretKeys, env_transport: envTransport, build_commit: buildCommit }) => {
1129
1132
  const effectiveNetId = getNetworkId(netId);
1130
1133
  const sessionNetId = effectiveNetId ?? "default";
1131
1134
  if (!callerTokenIsNetwork || !enforceNetworkId) {
@@ -1338,6 +1341,13 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
1338
1341
  [resume_id, envTransport],
1339
1342
  );
1340
1343
  }
1344
+ // 2026-10-31 —— #3294:同一手法(自己的 UPDATE,INSERT 字节形状不动)。
1345
+ if (buildCommit) {
1346
+ db.run(
1347
+ `UPDATE sessions SET build_commit = ?2, updated_at = datetime('now') WHERE resume_id = ?1`,
1348
+ [resume_id, buildCommit],
1349
+ );
1350
+ }
1341
1351
  if (Array.isArray(secretKeys)) {
1342
1352
  db.run(
1343
1353
  `UPDATE sessions SET secret_keys = ?2, updated_at = datetime('now') WHERE resume_id = ?1`,