@sleep2agi/commhub-server 0.9.0-preview.261 → 0.9.0-preview.262
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 +2 -2
- package/package.json +1 -1
- package/src/health-redaction.test.ts +23 -0
- package/src/parent-source-orphan.test.ts +121 -0
- package/src/tools.ts +9 -0
package/build-info.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sleep2agi/commhub-server",
|
|
3
|
-
"version": "0.9.0-preview.
|
|
3
|
+
"version": "0.9.0-preview.262",
|
|
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",
|
|
@@ -183,6 +183,29 @@ describe("#473 anonymous /health — watchdog contract intact, no topology leak"
|
|
|
183
183
|
// 🔴 这条断言的语义是:**目前如此**,不是「应该如此」。
|
|
184
184
|
// 将来谁把计数上移到中间件,这条会红 —— 那就对了:他必须同时更新
|
|
185
185
|
// 模块注释里的覆盖比例(现在是 2/6 类出口)。
|
|
186
|
+
// #669 —— 新加的计数必须**两侧都验**:鉴权侧看得到形状,匿名侧看不到键。
|
|
187
|
+
// 只验一侧时有两种假绿:
|
|
188
|
+
// · 只验匿名侧「没有」⇒ 字段根本没加也能过;
|
|
189
|
+
// · 只验鉴权侧「有」 ⇒ 它同时泄露给了匿名读者也能过。
|
|
190
|
+
// 这两句合起来才是「加进了 counters,且仍在鉴权侧」这个事实。
|
|
191
|
+
test("#669 counters.parent_infer 在鉴权投影里,匿名投影里没有", async () => {
|
|
192
|
+
const authed = await (await fetch(`${BASE}/health`, { headers: { Authorization: `Bearer ${adminToken}` } })).json() as any;
|
|
193
|
+
const pi = authed.counters?.parent_infer;
|
|
194
|
+
expect(pi).toBeTruthy();
|
|
195
|
+
// 形状是契约:五个键,一个不多一个不少。
|
|
196
|
+
expect(Object.keys(pi).sort()).toEqual([
|
|
197
|
+
"accepted", "rejected_ambiguous", "rejected_too_old", "since", "window_ms",
|
|
198
|
+
]);
|
|
199
|
+
// 窗口常量本身也是读数:「10 分钟」这个策略值必须从服务端读得到,
|
|
200
|
+
// 否则客户端只能写死一份拷贝,而那两份迟早不一致。
|
|
201
|
+
expect(pi.window_ms).toBe(600_000);
|
|
202
|
+
|
|
203
|
+
const anon = await (await fetch(`${BASE}/health`)).json() as any;
|
|
204
|
+
expect(anon.counters).toBeUndefined();
|
|
205
|
+
// 否定断言要配镜像:不能只在顶层没有就过 —— 也可能被拍平成了 counters_parent_infer。
|
|
206
|
+
expect(JSON.stringify(anon).includes("parent_infer")).toBe(false);
|
|
207
|
+
});
|
|
208
|
+
|
|
186
209
|
test("KNOWN GAP: unauthenticated /api/task rejection is NOT counted (see rejection-counters.ts)", async () => {
|
|
187
210
|
const before = await (await fetch(`${BASE}/health`, { headers: { Authorization: `Bearer ${adminToken}` } })).json() as any;
|
|
188
211
|
const beforeTotal = before.counters.task_rejections.total as number;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// #3348 —— 父被丢弃时,**来源必须一起丢弃**。
|
|
2
|
+
//
|
|
3
|
+
// ## 生产读数(修之前)
|
|
4
|
+
//
|
|
5
|
+
// tasks 131684 行里 2 条:
|
|
6
|
+
// meta = {"parent_source":"explicit"},parent_task_id / in_reply_to / thread_id 全空
|
|
7
|
+
// 5bc82d72 2026-10-30 16:08:38 · d002b739 2026-10-31 02:23:09
|
|
8
|
+
//
|
|
9
|
+
// ## 为什么会出现
|
|
10
|
+
//
|
|
11
|
+
// `send_task` 里的顺序是:
|
|
12
|
+
//
|
|
13
|
+
// ① `if (parentSource && parentTaskId) { meta.parent_source = parentSource }` ← 拼 meta
|
|
14
|
+
// ② 校验显式父是否存在;不存在 ⇒ `parentTaskId = null` ← 丢父
|
|
15
|
+
// ③ INSERT(meta 用 ①,父用 ② 之后的值)
|
|
16
|
+
//
|
|
17
|
+
// ⇒ ② 与 ① 之间没有连线。`meta` 里保留了「有一个 explicit 父」这个陈述,
|
|
18
|
+
// 而那一行本身没有父。
|
|
19
|
+
//
|
|
20
|
+
// ## 为什么这条不变式的正向断言抓不到它
|
|
21
|
+
//
|
|
22
|
+
// `#669` 的不变式是「父有值 ⇒ 来源有值」。这一格是「来源有值 ⇒ 父有值」的反面,
|
|
23
|
+
// 两个方向各自都合理,合起来留了一个空格 —— 而空格里正好能站下一行坏数据。
|
|
24
|
+
// `scripts/report-parent-source-coverage.py` 现在把这一格单列成 `source, no parent` 并报数。
|
|
25
|
+
//
|
|
26
|
+
// ## 这里测什么
|
|
27
|
+
//
|
|
28
|
+
// 走**真 handler**(不是镜像映射):派一条带不存在父的任务,断言
|
|
29
|
+
// ① 任务照样建成(丢父是「不阻塞派发」的决定,不是错误)
|
|
30
|
+
// ② parent_task_id 落库为 NULL
|
|
31
|
+
// ③ meta_json 里**没有** parent_source —— 这一条是本次修的
|
|
32
|
+
import { afterAll, beforeEach, describe, expect, test } from "bun:test";
|
|
33
|
+
import { mkdtempSync } from "fs";
|
|
34
|
+
import { join } from "path";
|
|
35
|
+
import { tmpdir } from "os";
|
|
36
|
+
|
|
37
|
+
process.env.COMMHUB_DB = join(mkdtempSync(join(tmpdir(), "anet-orphan-source-")), "commhub.db");
|
|
38
|
+
|
|
39
|
+
const { McpServer } = await import("@modelcontextprotocol/sdk/server/mcp.js");
|
|
40
|
+
const { db } = await import("./db.js");
|
|
41
|
+
const { registerTools } = await import("./tools.js");
|
|
42
|
+
|
|
43
|
+
const NET = "net_orphan_src";
|
|
44
|
+
const USER = "u_orphan_src";
|
|
45
|
+
const PEER = "osp-peer";
|
|
46
|
+
const TARGET = "osp-target";
|
|
47
|
+
|
|
48
|
+
type Handler = (args: any, extra?: any) => Promise<{ content: Array<{ type: "text"; text: string }> }>;
|
|
49
|
+
|
|
50
|
+
function cleanup(): void {
|
|
51
|
+
for (const table of ["task_events", "inbox", "tasks", "sessions", "nodes", "network_members", "networks"]) {
|
|
52
|
+
try { db.run(`DELETE FROM ${table} WHERE network_id = ?1`, [NET]); } catch {}
|
|
53
|
+
}
|
|
54
|
+
try { db.run("DELETE FROM users WHERE user_id = ?1", [USER]); } catch {}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function seed(): void {
|
|
58
|
+
db.run("INSERT INTO users (user_id, username, password_hash, role, created_at) VALUES (?1, ?2, 'x', 'user', datetime('now'))", [USER, USER]);
|
|
59
|
+
db.run("INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1, ?1, ?2, datetime('now'))", [NET, USER]);
|
|
60
|
+
db.run("INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1, ?2, 'owner', datetime('now'))", [USER, NET]);
|
|
61
|
+
for (const alias of [PEER, TARGET]) {
|
|
62
|
+
db.run(
|
|
63
|
+
`INSERT INTO nodes (node_id, node_name, alias, network_id, lifecycle_state)
|
|
64
|
+
VALUES (?1, ?2, ?2, ?3, 'active')`,
|
|
65
|
+
[`n_${alias}`, alias, NET],
|
|
66
|
+
);
|
|
67
|
+
db.run(
|
|
68
|
+
`INSERT INTO sessions (resume_id, alias, status, node_id, network_id, updated_at, last_seen_at)
|
|
69
|
+
VALUES (?1, ?2, 'idle', ?3, ?4, datetime('now'), datetime('now'))`,
|
|
70
|
+
[`r_${alias}`, alias, `n_${alias}`, NET],
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function handlers(): Record<string, Handler> {
|
|
76
|
+
const server = new McpServer({ name: "test-orphan-source", version: "0" }) as any;
|
|
77
|
+
const out: Record<string, Handler> = {};
|
|
78
|
+
const original = server.tool.bind(server);
|
|
79
|
+
server.tool = (name: string, ...args: any[]) => {
|
|
80
|
+
const h = args.at(-1);
|
|
81
|
+
if (typeof h === "function") out[name] = h;
|
|
82
|
+
return original(name, ...args);
|
|
83
|
+
};
|
|
84
|
+
registerTools(server, undefined, NET, USER, PEER, false, null);
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function call(handler: Handler, args: any): Promise<any> {
|
|
89
|
+
const r = await handler(args);
|
|
90
|
+
return JSON.parse(r.content[0].text);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
beforeEach(() => { cleanup(); seed(); });
|
|
94
|
+
afterAll(() => { cleanup(); });
|
|
95
|
+
|
|
96
|
+
describe("#3348 不存在的显式父:父与来源一起丢", () => {
|
|
97
|
+
test("父不存在 ⇒ 任务照样建成,但 parent_task_id 为 NULL 且 meta 里没有 parent_source", async () => {
|
|
98
|
+
const ghost = "00000000-0000-4000-8000-000000000000"; // 不存在的父
|
|
99
|
+
const r = await call(handlers().send_task, {
|
|
100
|
+
alias: TARGET,
|
|
101
|
+
task: "osp dispatch with a ghost parent",
|
|
102
|
+
priority: "normal",
|
|
103
|
+
from_session: PEER,
|
|
104
|
+
network_id: NET,
|
|
105
|
+
parent_task_id: ghost,
|
|
106
|
+
});
|
|
107
|
+
// 丢父是「不阻塞派发」的决定:这条**不能**变成错误,否则一个幻觉 id 会挡掉整条任务。
|
|
108
|
+
expect(r.ok).toBe(true);
|
|
109
|
+
|
|
110
|
+
const row = db.get<{ task_id: string; parent_task_id: string | null; meta_json: string | null }>(
|
|
111
|
+
"SELECT task_id, parent_task_id, meta_json FROM tasks WHERE content = ?1",
|
|
112
|
+
["osp dispatch with a ghost parent"],
|
|
113
|
+
);
|
|
114
|
+
expect(row).toBeTruthy();
|
|
115
|
+
expect(row!.parent_task_id).toBeNull();
|
|
116
|
+
// 关键断言:来源不许单独留下。
|
|
117
|
+
// 留着的后果是库里出现「有来源、没父」这一格 —— #669 的不变式两个方向都放过它,
|
|
118
|
+
// 于是它既不被算作 explicit 也不被算作无父,只在有人专门数「source 无 parent」时才看得见。
|
|
119
|
+
expect(String(row!.meta_json ?? "")).not.toContain("parent_source");
|
|
120
|
+
});
|
|
121
|
+
});
|
package/src/tools.ts
CHANGED
|
@@ -2174,6 +2174,15 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
|
|
|
2174
2174
|
// still proceed; the LLM may have meant to dispatch fresh.
|
|
2175
2175
|
console.log(`[${ts()}] ⚠ send_task: parent_task_id=${parentIn.slice(0, 8)} not found, dropping parent link`);
|
|
2176
2176
|
parentTaskId = null;
|
|
2177
|
+
// #3348 —— **来源必须与父一起丢弃**。
|
|
2178
|
+
// 上面那段 meta 是在父校验**之前**拼的(`if (parentSource && parentTaskId)` 当时为真),
|
|
2179
|
+
// 而这里才把 parentTaskId 置空 ⇒ 落库会留下
|
|
2180
|
+
// parent_task_id = NULL,meta.parent_source = "explicit"
|
|
2181
|
+
// 这一格。#669 那条不变式正是「父有值 ⇒ 来源有值」,它只挡住了反方向
|
|
2182
|
+
// (父为空时来源也不必在),于是这一格两边都放过。
|
|
2183
|
+
// 生产实测:131684 行里有 2 条落在这里(10-30、10-31 各一条)。
|
|
2184
|
+
parentSource = null;
|
|
2185
|
+
metaJsonWithParent = metaJson;
|
|
2177
2186
|
} else if ((parentRow.network_id ?? null) !== (effectiveNetId ?? null)) {
|
|
2178
2187
|
console.log(`[${ts()}] 🚫 send_task: cross-network parent rejected, parent=${parentIn.slice(0, 8)} parent-net=${parentRow.network_id ?? "null"} caller-net=${effectiveNetId ?? "null"}`);
|
|
2179
2188
|
return { content: [{ type: "text" as const, text: JSON.stringify({
|