@sleep2agi/commhub-server 0.9.0-preview.239 → 0.9.0-preview.240

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.239",
3
+ "version": "0.9.0-preview.240",
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",
@@ -40,19 +40,20 @@ async function send(method: string, path: string, payload?: unknown): Promise<{
40
40
  }
41
41
 
42
42
  beforeAll(async () => {
43
- const srv = await import("./server.js");
44
- hub = srv.default ?? srv;
45
- const { register } = await import("./auth.js");
46
43
  db = (await import("./db.js")).db;
44
+ const { register } = await import("./auth.js");
47
45
  const stamp = Date.now();
48
46
  const reg = register(`asg_${stamp}`, PW);
49
47
  expect(reg.ok).toBe(true);
50
48
  TOKEN = reg.token!;
51
49
  NET = reg.network_id!;
52
50
  USER_ID = db.get<{ user_id: string }>("SELECT user_id FROM users WHERE username = ?1", `asg_${stamp}`)!.user_id;
53
- BASE = `http://127.0.0.1:${(hub.server ?? hub).port ?? 0}`;
54
- if (!BASE.endsWith("0")) return;
55
- BASE = "";
51
+ // 一个可解析的 node(agent_owner 必须是本网络 node)
52
+ db.run(`INSERT OR REPLACE INTO nodes (node_id, node_name, network_id, alias, created_at, updated_at)
53
+ VALUES ('n_asg_probe', 'asg-probe', ?1, 'asg-probe', ?2, ?2)`, [NET, Date.now()]);
54
+ const mod: any = await import("./server.js");
55
+ hub = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
56
+ BASE = `http://127.0.0.1:${hub.port}`;
56
57
  }, 60_000);
57
58
 
58
59
  afterAll(() => { try { hub?.stop?.(); } catch {} rmSync(DIR, { recursive: true, force: true }); });
@@ -77,4 +78,35 @@ describe("assignee never contradicts a validated owner/agent_owner reference", (
77
78
  // 全是空 ⇒ 空串,不是 "undefined"/"null"
78
79
  expect(displayAssignee({ assignee: null, owner_json: null, agent_owner_json: null })).toBe("");
79
80
  });
81
+
82
+ test("写侧对账:设 agent_owner 时旧 assignee 被回填为引用(不再累积分叉)", async () => {
83
+ const created = await send("POST", `/api/requirements?network_id=${NET}`, {
84
+ name: "assignee 写侧对账",
85
+ column: "pool",
86
+ assignee: "ANet基建", // 旧值(看起来有人、其实解析不到)
87
+ });
88
+ expect(created.status).toBe(201);
89
+ const id = created.body.requirement.id as string;
90
+ expect(created.body.requirement.assignee).toBe("ANet基建");
91
+
92
+ // 只改 agent_owner,不显式给 assignee ⇒ 回填为 node alias
93
+ const patched = await send("PATCH", `/api/requirements/${id}?network_id=${NET}`, {
94
+ agent_owner: { kind: "node", alias: "asg-probe" },
95
+ });
96
+ expect(patched.status).toBe(200);
97
+ expect(patched.body.requirement.assignee).toBe("asg-probe");
98
+
99
+ // 显式给 assignee 且**没有引用** ⇒ 自由文本就是唯一事实(系统外的人,例如「王工」)
100
+ const other = await send("POST", `/api/requirements?network_id=${NET}`, { name: "无引用的卡", column: "pool" });
101
+ expect(other.status).toBe(201);
102
+ const manual = await send("PATCH", `/api/requirements/${other.body.requirement.id}?network_id=${NET}`, { assignee: "王工" });
103
+ expect(manual.status).toBe(200);
104
+ expect(manual.body.requirement.assignee).toBe("王工");
105
+
106
+ // 两者**同时存在**时读侧以引用为准(这是有意的取舍:引用写入时被校验过,自由文本没有)。
107
+ // 记录在这里,免得下一个人把它当成 bug 再改回去。
108
+ const both = await send("PATCH", `/api/requirements/${id}?network_id=${NET}`, { assignee: "王工" });
109
+ expect(both.status).toBe(200);
110
+ expect(both.body.requirement.assignee).toBe("asg-probe");
111
+ });
80
112
  });
@@ -1256,6 +1256,20 @@ function patchRequirement(ctx: RequirementsRequestContext, row: Row, body: Recor
1256
1256
  }
1257
1257
  let people;
1258
1258
  try { people = assignments(body, row.network_id, row, hiddenNodeFilter(ctx, row.network_id), ctx.strictOwner === true); } catch (e) { return errorFrom(e); }
1259
+
1260
+ // 2026-10-31 —— **写侧对账**(#3270 的另一半):
1261
+ // 读侧已经以 owner/agent_owner 为准(displayAssignee),但库里那一列仍会停在旧值。
1262
+ // 一个派生字段只要有第二个写入路径,就迟早会与事实分叉;同一次写入里把它对齐,分叉就不会累积。
1263
+ // 规则:调用方**没有显式给 assignee** 时,用本次写入后的引用回填;显式给了就以调用方为准
1264
+ // (那可能是系统外的人,例如「王工」—— 用引用覆盖它等于丢信息)。
1265
+ if (!has("assignee") && (has("owner") || has("agent_owner"))) {
1266
+ const fromRef = displayAssignee({
1267
+ assignee: "",
1268
+ owner_json: people.ownerJson,
1269
+ agent_owner_json: people.agentOwnerJson,
1270
+ });
1271
+ if (fromRef) assignee = fromRef.slice(0, 80);
1272
+ }
1259
1273
  // RFC-040:只凭负责人身份改这张卡时,负责人 / 负责 Agent 只能交给本部门的人(或自己)/ 本部门成员的 Agent,
1260
1274
  // 不能把卡推出本部门(推出去等于替别的部门派活)。清空(null)不限制。
1261
1275
  const headOnly = (has("owner") || has("agent_owner")) ? headOnlyEditScope(caller, row) : null;
@@ -1470,14 +1484,29 @@ type LightRow = { requirement_id: string; seq?: number | null; network_id: strin
1470
1484
  * 没有引用时保留自由文本 —— 它可能是系统外的人(例如「王工」),删掉就等于丢信息。
1471
1485
  */
1472
1486
  export function displayAssignee(row: { assignee?: string | null; owner_json?: string | null; agent_owner_json?: string | null }): string {
1473
- const pick = (json: string | null | undefined): string => {
1474
- if (!json) return "";
1487
+ const parse = (json: string | null | undefined): { kind?: string; id?: string; alias?: string; username?: string } | null => {
1488
+ if (!json) return null;
1489
+ try { return JSON.parse(json); } catch { return null; }
1490
+ };
1491
+ // 引用存的是 {kind, id}(名字在写入时已被解析成 id)⇒ 读侧要把 id 换回**给人看的名字**,
1492
+ // 否则 assignee 会变成 "n_xxxx",那比停在旧值更糟(至少旧值是句话)。
1493
+ const name = (ref: { kind?: string; id?: string; alias?: string; username?: string } | null): string => {
1494
+ if (!ref) return "";
1495
+ const inline = String(ref.alias || ref.username || "").trim();
1496
+ if (inline) return inline;
1497
+ if (!ref.id) return "";
1475
1498
  try {
1476
- const ref = JSON.parse(json) as { alias?: string; username?: string; id?: string } | null;
1477
- return String(ref?.alias || ref?.username || ref?.id || "").trim();
1478
- } catch { return ""; }
1499
+ if (ref.kind === "node") {
1500
+ const r = db.get<{ alias: string | null }>("SELECT alias FROM nodes WHERE node_id = ?1", ref.id);
1501
+ if (r?.alias) return String(r.alias);
1502
+ } else if (ref.kind === "user") {
1503
+ const r = db.get<{ username: string | null }>("SELECT username FROM users WHERE user_id = ?1", ref.id);
1504
+ if (r?.username) return String(r.username);
1505
+ }
1506
+ } catch { /* 读不到就退回 id:比空更好,且不会崩 */ }
1507
+ return String(ref.id);
1479
1508
  };
1480
- return pick(row.agent_owner_json) || pick(row.owner_json) || String(row.assignee || "").trim();
1509
+ return name(parse(row.agent_owner_json)) || name(parse(row.owner_json)) || String(row.assignee || "").trim();
1481
1510
  }
1482
1511
 
1483
1512
  const parseRef = (json: string | null) => { try { return json ? JSON.parse(json) : null; } catch { return null; } };