@sleep2agi/commhub-server 0.9.0-preview.194 → 0.9.0-preview.196

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.194",
3
+ "version": "0.9.0-preview.196",
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,50 @@
1
+ // #669 —— parent_task_id 的来源必须可区分(显式 / 推断)。
2
+ //
3
+ // 没有这个标记时,那张卡要求的前置统计做不出来:tasks 表只存 parent_task_id,
4
+ // 不记它是怎么来的(实测近 7 天 949 条里 0 条可区分)。
5
+ //
6
+ // 这里不测完整派发链路,只钉住「来源判定 + 写进 meta」这一段的行为:
7
+ // · 显式带了 parent_task_id → parent_source = "explicit"
8
+ // · 没带但被推断命中最近 delivered → parent_source = "inferred"
9
+ // · 两者都没有 → meta 里没有 parent_source
10
+ import { describe, expect, test } from "bun:test";
11
+ import { readFileSync } from "node:fs";
12
+
13
+ const src = readFileSync(new URL("./tools.ts", import.meta.url), "utf8");
14
+
15
+ describe("#669 parent source marker", () => {
16
+ test("显式 parent 记为 explicit,推断命中时改写为 inferred", () => {
17
+ expect(src).toContain('let parentSource: "explicit" | "inferred" | null = parentIn ? "explicit" : null;');
18
+ expect(src).toContain('if (recent?.task_id) { parentTaskId = recent.task_id; parentSource = "inferred"; }');
19
+ });
20
+
21
+ test("来源只在确实有 parent 时写进 meta,且序列化失败不影响派发", () => {
22
+ const block = src.slice(src.indexOf("let metaJsonWithParent = metaJson;"), src.indexOf("// round5 F2 fix"));
23
+ expect(block).toContain("if (parentSource && parentTaskId)");
24
+ expect(block).toContain("try {");
25
+ expect(block).toContain("parent_source: parentSource");
26
+ expect(block).toContain("catch {}");
27
+ });
28
+
29
+ test("两条 INSERT(inbox / tasks)都用合并后的 meta", () => {
30
+ const inserts = src.split("\n").filter((l) => l.includes("targetAlias, priority, task") || l.includes("effectiveNetId ?? null, parentTaskId"));
31
+ expect(inserts.some((l) => l.includes("metaJsonWithParent"))).toBe(true);
32
+ });
33
+
34
+ // 这条原本断言「REST 里根本没有 parent_source」——理由是「REST 不走推断,
35
+ // 不要造第二真相」。方向对,但结论错了一半:REST 确实**不该推断**,
36
+ // 可它建的子任务同样是显式父子关系,不标就会被统计漏掉。
37
+ //
38
+ // 实测(2026-10-29,.195 上线后 90 分钟):3 条带 parent 的任务全部走 REST、
39
+ // 全部没有 parent_source。而 MCP 那条路是标的 ⇒ 分母只含 MCP,
40
+ // explicit 占比会被系统性压低。#669 要统计的正是这个比例。
41
+ test("REST /api/task 标「显式」,但不推断", () => {
42
+ const rest = readFileSync(new URL("./server.ts", import.meta.url), "utf8");
43
+ expect(rest).toContain("body.parent_task_id ?? null");
44
+ expect(rest).toContain('parent_source: "explicit"');
45
+ // 不能出现第二份推断:REST 里没有「找最近 delivered」这种 SQL。
46
+ const restBlock = rest.slice(rest.indexOf("const metaWithParent"), rest.indexOf("const metaWithParent") + 400);
47
+ expect(restBlock.toLowerCase()).not.toContain("delivered");
48
+ expect(rest).not.toContain('parentSource = "inferred"');
49
+ });
50
+ });
package/src/server.ts CHANGED
@@ -4248,7 +4248,15 @@ return Bun.serve({
4248
4248
  : restAuth
4249
4249
  ? "user"
4250
4250
  : "legacy";
4251
- const metaJson = normalizeMetaJson(stampTaskAuthOrigin(mergedMeta, authOrigin));
4251
+ // #669 —— REST 这一侧只标「显式」,**绝不推断**(推断只存在于 MCP send_task)。
4252
+ // 但不标就会有一个偏差:REST 建的子任务全部落进「无标记」,
4253
+ // 而 MCP 建的显式子任务有标记 ⇒ 统计里 explicit 被系统性地压低。
4254
+ // 实测(2026-10-29):近 90 分钟 3 条带 parent 的任务全部走 REST、全部无标记。
4255
+ // parent_source 已排除出幂等指纹(task-idempotency.ts),加上它不影响重放判定。
4256
+ const metaWithParent = body.parent_task_id
4257
+ ? { ...(mergedMeta && typeof mergedMeta === "object" ? mergedMeta : {}), parent_source: "explicit" }
4258
+ : mergedMeta;
4259
+ const metaJson = normalizeMetaJson(stampTaskAuthOrigin(metaWithParent, authOrigin));
4252
4260
 
4253
4261
  // Client-request-id idempotency, mirroring the MCP `send_task` tool
4254
4262
  // (tools.ts). The app/Dashboard mint one `meta.client_request_id` per
@@ -46,6 +46,22 @@ describe("dashboard task idempotency", () => {
46
46
  })).toBe(true);
47
47
  });
48
48
 
49
+ // #669 —— 回归:parent_source 是 Hub 派生的,不参与幂等比较。
50
+ // 现场(test1210 抓到):第一次调用可能没有可推断的父任务(meta 不带该字段),
51
+ // 重放时上一条已 delivered ⇒ 推断命中 ⇒ meta 带上 parent_source:"inferred",
52
+ // 于是同一请求被判 idempotency_conflict。本地复现后修复(比较时排除该字段)。
53
+ test("parent_source (Hub-derived) does not break replay equality", () => {
54
+ const row: StoredIdempotentTask = {
55
+ task_id: "idem_x", from_name: "admin", to_name: "worker", priority: "normal",
56
+ content: "hello", network_id: "net_a", status: "delivered",
57
+ meta_json: JSON.stringify({ client_request_id: "dreq_0123456789abcdef", parent_source: "inferred" }),
58
+ };
59
+ expect(idempotentTaskMatches(row, {
60
+ fromName: "admin", toName: "worker", priority: "normal", content: "hello", networkId: "net_a",
61
+ metaJson: JSON.stringify({ client_request_id: "dreq_0123456789abcdef" }),
62
+ })).toBe(true);
63
+ });
64
+
49
65
  test("same request id with changed payload is a conflict", () => {
50
66
  const row: StoredIdempotentTask = {
51
67
  task_id: "idem_x", from_name: "admin", to_name: "worker", priority: "normal",
@@ -61,7 +61,13 @@ function logicalRequestMeta(value: string | null): unknown {
61
61
  // auth_origin is a Hub-derived audit fact, not part of the client request.
62
62
  // Ignore it for replay equality so a request first accepted by the old Hub
63
63
  // (without the stamp) remains idempotent across a rolling upgrade.
64
- const { auth_origin: _authOrigin, ...logical } = parsed as Record<string, unknown>;
64
+ //
65
+ // #669 —— parent_source 同理:它是 Hub 记录的「parent_task_id 是怎么来的」
66
+ // (explicit / inferred),由派发时的推断结果决定,**不是客户端请求的一部分**。
67
+ // 不排除它会让幂等重放失败:第一次调用可能没有可推断的父任务(不带该字段),
68
+ // 重放时上一条已经是 delivered(推断命中 ⇒ 带 parent_source:"inferred"),
69
+ // 于是同一请求被判成 idempotency_conflict。test1210 抓到的就是这个。
70
+ const { auth_origin: _authOrigin, parent_source: _parentSource, ...logical } = parsed as Record<string, unknown>;
65
71
  return logical;
66
72
  }
67
73
 
package/src/tools.ts CHANGED
@@ -2060,6 +2060,10 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
2060
2060
  // network A dispatch and chain-reply into the wrong tenant. The
2061
2061
  // explicit-parent path is verified below in F2.
2062
2062
  let parentTaskId: string | null = parentIn ?? null;
2063
+ // #669 —— 记录 parent_task_id 是「显式指定」还是「推断」得来的。
2064
+ // 没有这个标记时,那张卡要求的前置统计(多少子任务靠推断挂上)根本做不出来:
2065
+ // tasks 表只存了 parent_task_id,不记它是怎么来的(实测近 7 天 949 条里 0 条可区分)。
2066
+ let parentSource: "explicit" | "inferred" | null = parentIn ? "explicit" : null;
2063
2067
  if (!humanSender && !parentTaskId && from_session && from_session !== "hub" && from_session !== "api") {
2064
2068
  try {
2065
2069
  const recentParams: any[] = [from_session];
@@ -2067,7 +2071,17 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
2067
2071
  recentSql = addScope(recentSql, recentParams, effectiveNetId);
2068
2072
  recentSql += " ORDER BY created_at DESC LIMIT 1";
2069
2073
  const recent = db.get<{ task_id: string }>(recentSql, ...recentParams);
2070
- if (recent?.task_id) parentTaskId = recent.task_id;
2074
+ if (recent?.task_id) { parentTaskId = recent.task_id; parentSource = "inferred"; }
2075
+ } catch {}
2076
+ }
2077
+
2078
+ // #669 —— 把来源写进 meta(只在确实有 parent 时)。失败不影响派发:
2079
+ // 这只是给后续统计用的旁证字段,不能让一次序列化异常挡住一条任务。
2080
+ let metaJsonWithParent = metaJson;
2081
+ if (parentSource && parentTaskId) {
2082
+ try {
2083
+ const base = metaJson ? JSON.parse(metaJson) : {};
2084
+ metaJsonWithParent = normalizeMetaJson({ ...base, parent_source: parentSource });
2071
2085
  } catch {}
2072
2086
  }
2073
2087
 
@@ -2225,12 +2239,12 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
2225
2239
  db.run(
2226
2240
  `INSERT INTO inbox (id, task_id, session_name, node_id, type, priority, content, context, from_session, requires_response, network_id, meta_json)
2227
2241
  VALUES (?1, ?1, ?2, ?3, 'task', ?4, ?5, ?6, ?7, 'reply', ?8, ?9)`,
2228
- [id, targetAlias, targetNodeId, priority, task, context ?? null, from_session, effectiveNetId ?? null, metaJson]
2242
+ [id, targetAlias, targetNodeId, priority, task, context ?? null, from_session, effectiveNetId ?? null, metaJsonWithParent]
2229
2243
  );
2230
2244
  db.run(
2231
2245
  `INSERT INTO tasks (task_id, from_node_id, from_name, to_node_id, to_name, priority, status, content, requires_response, created_at, delivered_at, expires_at, network_id, parent_task_id, meta_json)
2232
2246
  VALUES (?1, ?2, ?3, ?4, ?5, ?6, 'delivered', ?7, 'reply', datetime('now'), datetime('now'), datetime('now', ?8), ?9, ?10, ?11)`,
2233
- [id, fromNodeId, from_session, targetNodeId, targetAlias, priority, task, `+${ttl_seconds || 3600} seconds`, effectiveNetId ?? null, parentTaskId, metaJson]
2247
+ [id, fromNodeId, from_session, targetNodeId, targetAlias, priority, task, `+${ttl_seconds || 3600} seconds`, effectiveNetId ?? null, parentTaskId, metaJsonWithParent]
2234
2248
  );
2235
2249
  const touchParams: any[] = [sessionTaskPreview(task), targetAlias];
2236
2250
  let touchSql = `UPDATE sessions SET task = ${sessionTaskOnDispatch("?1")}, updated_at = datetime('now') WHERE alias = ?2`;