@sleep2agi/commhub-server 0.9.0-preview.165 → 0.9.0-preview.166
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 +1 -1
- package/src/broadcast-fanout-2721.test.ts +99 -0
- package/src/broadcast-fanout.ts +71 -0
- package/src/db.ts +5 -0
- package/src/server.ts +7 -23
- package/src/tools.ts +7 -27
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.166",
|
|
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,99 @@
|
|
|
1
|
+
// #2721 —— 广播扇出分批:把「一条广播 = 几百次同步写」摊成「每批一次事务 + 批间让出」。
|
|
2
|
+
//
|
|
3
|
+
// 现场(2026-10-27 07:10,生产 `.165`):384 行 inbox 让 Hub 出现 143.9 秒的 timer 饥饿,
|
|
4
|
+
// RSS 冲到 2.9GB,`requests_since_last_tick` 高达 1175(饱和而非阻塞)。
|
|
5
|
+
//
|
|
6
|
+
// 这个文件钉住四件事:
|
|
7
|
+
// 1. 收件人逐个入库,条数与收件人一致(语义不变);
|
|
8
|
+
// 2. 每 chunk 一批、批间**真的**让出(`yieldBetweenChunks` 被调用 ⌈n/chunk⌉ 次);
|
|
9
|
+
// 3. 非 active 的收件人静默跳过(RFC-027 best-effort),且不推事件;
|
|
10
|
+
// 4. 事件只推给**真的入库了**的收件人(#1440 的语义)。
|
|
11
|
+
|
|
12
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
13
|
+
import { db } from "./db.js";
|
|
14
|
+
import { BROADCAST_CHUNK, enqueueBroadcast } from "./broadcast-fanout.js";
|
|
15
|
+
|
|
16
|
+
const stamp = `${Date.now()}`;
|
|
17
|
+
let NET = "";
|
|
18
|
+
|
|
19
|
+
function targets(n: number, netId = NET) {
|
|
20
|
+
return Array.from({ length: n }, (_, i) => ({
|
|
21
|
+
alias: `fan-${i}-${stamp}`,
|
|
22
|
+
node_id: `n_fan_${i}_${stamp}`,
|
|
23
|
+
network_id: netId,
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const inboxRows = (netId = NET) =>
|
|
28
|
+
db.get<{ n: number }>("SELECT COUNT(*) AS n FROM inbox WHERE network_id = ?1 AND type = 'broadcast'", [netId])!.n;
|
|
29
|
+
|
|
30
|
+
beforeAll(async () => {
|
|
31
|
+
const { register } = await import("./auth.js");
|
|
32
|
+
const auth = register(`fan_owner_${stamp}`, "Fan-Owner-Strong-1!", undefined, "tester");
|
|
33
|
+
expect(auth.ok).toBe(true);
|
|
34
|
+
NET = auth.network_id!;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
afterAll(() => {
|
|
38
|
+
try { db.run("DELETE FROM inbox WHERE network_id = ?1 AND type = 'broadcast'", [NET]); } catch {}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("#2721 broadcast fan-out is chunked", () => {
|
|
42
|
+
test("逐条入库,条数等于收件人数", async () => {
|
|
43
|
+
const before = inboxRows();
|
|
44
|
+
const res = await enqueueBroadcast(targets(120), "hello", "hub", NET, {
|
|
45
|
+
isActive: () => true,
|
|
46
|
+
notify: () => {},
|
|
47
|
+
yieldBetweenChunks: async () => {},
|
|
48
|
+
});
|
|
49
|
+
expect(res.ids).toHaveLength(120);
|
|
50
|
+
expect(res.delivered).toHaveLength(120);
|
|
51
|
+
expect(inboxRows() - before).toBe(120);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("批间真的让出:⌈120/50⌉ = 3 次", async () => {
|
|
55
|
+
let yields = 0;
|
|
56
|
+
await enqueueBroadcast(targets(120), "hello", "hub", NET, {
|
|
57
|
+
isActive: () => true,
|
|
58
|
+
notify: () => {},
|
|
59
|
+
yieldBetweenChunks: async () => { yields += 1; },
|
|
60
|
+
});
|
|
61
|
+
expect(yields).toBe(Math.ceil(120 / BROADCAST_CHUNK));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("自定义 chunk 生效(10 → 12 批)", async () => {
|
|
65
|
+
let yields = 0;
|
|
66
|
+
await enqueueBroadcast(targets(120), "hello", "hub", NET, {
|
|
67
|
+
isActive: () => true,
|
|
68
|
+
notify: () => {},
|
|
69
|
+
chunkSize: 10,
|
|
70
|
+
yieldBetweenChunks: async () => { yields += 1; },
|
|
71
|
+
});
|
|
72
|
+
expect(yields).toBe(12);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("非 active 收件人:不入库、不推事件", async () => {
|
|
76
|
+
const notified: string[] = [];
|
|
77
|
+
const ts = targets(10);
|
|
78
|
+
const res = await enqueueBroadcast(ts, "hello", "hub", NET, {
|
|
79
|
+
// 只让偶数号 active
|
|
80
|
+
isActive: (alias) => Number(alias.split("-")[1]) % 2 === 0,
|
|
81
|
+
notify: (alias) => notified.push(alias),
|
|
82
|
+
yieldBetweenChunks: async () => {},
|
|
83
|
+
});
|
|
84
|
+
expect(res.ids).toHaveLength(5);
|
|
85
|
+
expect(notified).toHaveLength(5);
|
|
86
|
+
expect(notified.every(a => Number(a.split("-")[1]) % 2 === 0)).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("空收件人列表:不写、不推、不让出", async () => {
|
|
90
|
+
let yields = 0;
|
|
91
|
+
const res = await enqueueBroadcast([], "hello", "hub", NET, {
|
|
92
|
+
isActive: () => true,
|
|
93
|
+
notify: () => {},
|
|
94
|
+
yieldBetweenChunks: async () => { yields += 1; },
|
|
95
|
+
});
|
|
96
|
+
expect(res.ids).toEqual([]);
|
|
97
|
+
expect(yields).toBe(0);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// #2721 —— 广播扇出:把「一条广播 = 几百次同步写 + 同一秒全员唤醒」摊开。
|
|
2
|
+
//
|
|
3
|
+
// 现场(2026-10-27 07:10–07:15,生产 `.165`,触发者是我自己的一条广播):
|
|
4
|
+
// 一条广播 → 384 行 inbox(每行一次 autocommit INSERT)
|
|
5
|
+
// → 07:11:16 起连续四次 timer 饥饿:3.5s / 20.0s / **143.9s** / 10.0s
|
|
6
|
+
// → RSS 639MB → 979MB → 2507MB → 2910MB,3 分钟后自行排空
|
|
7
|
+
// → `requests_since_last_tick` 分别是 145 / 123 / **1175** / 294
|
|
8
|
+
// ⇒ 当时的循环是**被打满**(8 req/s 持续),不是被阻塞。
|
|
9
|
+
//
|
|
10
|
+
// 这里只改**节奏**,不改语义:仍然逐行入库、仍然给每个收件人推事件,
|
|
11
|
+
// 但每 50 个收件人一批、批间让出事件循环,把「同一毫秒 384 次 commit」
|
|
12
|
+
// 变成「8 批、每批一次事务」。
|
|
13
|
+
//
|
|
14
|
+
// 为什么有用:真正的负载大头是收件 agent 随后各自的 report_status/get_inbox/send_reply
|
|
15
|
+
// (实测一条广播 ≈ 500+ 次调用)。把入库摊开几百毫秒,能让唤醒时刻自然分散,
|
|
16
|
+
// 而不是让 384 个 agent 在同一秒同时醒来。
|
|
17
|
+
|
|
18
|
+
import { db } from "./db.js";
|
|
19
|
+
|
|
20
|
+
/** 一批多少个收件人。50 是“一次事务不至于太长、又明显少于一次全量”的折中。 */
|
|
21
|
+
export const BROADCAST_CHUNK = 50;
|
|
22
|
+
|
|
23
|
+
export type BroadcastTarget = { alias: string; node_id: string | null; network_id: string | null };
|
|
24
|
+
export type BroadcastDelivered = { alias: string; netId: string | null };
|
|
25
|
+
|
|
26
|
+
export type BroadcastDeps = {
|
|
27
|
+
/** RFC-027 §2.3:非 active 的收件人静默跳过(广播是 best-effort)。 */
|
|
28
|
+
isActive: (alias: string, netId: string | null) => boolean;
|
|
29
|
+
/** 入库之后给该收件人推一次事件(原实现用 pushEvent + pendingInboxCount)。 */
|
|
30
|
+
notify: (alias: string, netId: string | null) => void;
|
|
31
|
+
/** 测试缝:批间的让出动作。 */
|
|
32
|
+
yieldBetweenChunks?: () => Promise<void>;
|
|
33
|
+
chunkSize?: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export async function enqueueBroadcast(
|
|
37
|
+
targets: readonly BroadcastTarget[],
|
|
38
|
+
message: string,
|
|
39
|
+
fromSession: string,
|
|
40
|
+
effectiveNetId: string | null,
|
|
41
|
+
deps: BroadcastDeps,
|
|
42
|
+
): Promise<{ ids: string[]; delivered: BroadcastDelivered[] }> {
|
|
43
|
+
const chunk = deps.chunkSize && deps.chunkSize > 0 ? deps.chunkSize : BROADCAST_CHUNK;
|
|
44
|
+
const ids: string[] = [];
|
|
45
|
+
const delivered: BroadcastDelivered[] = [];
|
|
46
|
+
const yieldNow = deps.yieldBetweenChunks ?? (() => Bun.sleep(0));
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < targets.length; i += chunk) {
|
|
49
|
+
const slice = targets.slice(i, i + chunk);
|
|
50
|
+
// 一批一个事务:把 N 次 autocommit 变成 1 次,既快又不会长时间占写锁。
|
|
51
|
+
db.transaction(() => {
|
|
52
|
+
for (const t of slice) {
|
|
53
|
+
const netId = effectiveNetId ?? t.network_id ?? null;
|
|
54
|
+
if (!deps.isActive(t.alias, netId)) continue;
|
|
55
|
+
const id = crypto.randomUUID();
|
|
56
|
+
db.run(
|
|
57
|
+
`INSERT INTO inbox (id, session_name, node_id, type, priority, content, from_session, network_id)
|
|
58
|
+
VALUES (?1, ?2, ?3, 'broadcast', 'normal', ?4, ?5, ?6)`,
|
|
59
|
+
[id, t.alias, t.node_id ?? null, message, fromSession, netId],
|
|
60
|
+
);
|
|
61
|
+
ids.push(id);
|
|
62
|
+
delivered.push({ alias: t.alias, netId });
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
// 批间让出:没有这一句,384 行仍然是在同一个 tick 里写完的。
|
|
66
|
+
await yieldNow();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const d of delivered) deps.notify(d.alias, d.netId);
|
|
70
|
+
return { ids, delivered };
|
|
71
|
+
}
|
package/src/db.ts
CHANGED
|
@@ -1599,6 +1599,11 @@ migrateSessionsNetworkAliasUnique();
|
|
|
1599
1599
|
try { db.exec("CREATE INDEX IF NOT EXISTS idx_sessions_network ON sessions(network_id)"); } catch {}
|
|
1600
1600
|
try { db.exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_sessions_network_alias_unique ON sessions(network_id, alias)"); } catch {}
|
|
1601
1601
|
try { db.exec("CREATE INDEX IF NOT EXISTS idx_tasks_network ON tasks(network_id)"); } catch {}
|
|
1602
|
+
// #2722 —— 任务统计(`SELECT status, COUNT(*) FROM tasks WHERE network_id=? GROUP BY status`)是
|
|
1603
|
+
// Dashboard/状态接口最常走的聚合:只有上面的单列索引时,它要扫该网络**全部**行再建临时 B 树
|
|
1604
|
+
// (生产 116k 行实测 **5325ms**;同库副本 294ms)。加覆盖索引后副本 **24ms(12×)**。
|
|
1605
|
+
// 只加列、不改任何查询;老库首次启动多花约 0.4 秒建索引。
|
|
1606
|
+
try { db.exec("CREATE INDEX IF NOT EXISTS idx_tasks_network_status ON tasks(network_id, status)"); } catch {}
|
|
1602
1607
|
// #500 — covering index for the full /api/status queue_depth GROUP BY (task-queue-ahead.ts queueDepthQuery): the 24 h
|
|
1603
1608
|
// created_at range plus every column that query reads, so it never touches the (wide) task rows. Here, not in the
|
|
1604
1609
|
// CREATE TABLE block: tasks.network_id only exists after the ALTER above. On a read-only copy of the production DB
|
package/src/server.ts
CHANGED
|
@@ -31,6 +31,7 @@ import { createSSEStream, createNetworkObserverStream, createUserEventStream, pu
|
|
|
31
31
|
import { enterMcpInflight, leaveMcpInflight, getMcpInflightStats } from "./mcp-inflight.js";
|
|
32
32
|
import { eventLoopStats, startEventLoopLagMonitor } from "./event-loop-lag.js";
|
|
33
33
|
import { refreshAliasBaseline } from "./alias-baseline.js";
|
|
34
|
+
import { enqueueBroadcast } from "./broadcast-fanout.js";
|
|
34
35
|
import { assertNodeActive } from "./lifecycle-guard.js";
|
|
35
36
|
import { pendingInboxCount } from "./inbox-count.js";
|
|
36
37
|
import { notifyExpiredTasks, type ExpiredTaskRow } from "./task-expiry-notice.js";
|
|
@@ -4372,29 +4373,12 @@ return Bun.serve({
|
|
|
4372
4373
|
if (body.filter_server) { sql += " AND server = ?"; params.push(body.filter_server); }
|
|
4373
4374
|
if (body.filter_status) { sql += " AND status = ?"; params.push(body.filter_status); }
|
|
4374
4375
|
const targets = db.all<{ alias: string; node_id: string | null; network_id: string | null }>(sql, ...params);
|
|
4375
|
-
|
|
4376
|
-
|
|
4377
|
-
|
|
4378
|
-
|
|
4379
|
-
|
|
4380
|
-
|
|
4381
|
-
// 2/2). Broadcast skips non-active recipients silently per its
|
|
4382
|
-
// best-effort semantics — mirrors the MCP broadcast handler
|
|
4383
|
-
// in tools.ts (PR1.1 site 6/6).
|
|
4384
|
-
const lc = assertNodeActive(t.alias, t.network_id ?? null);
|
|
4385
|
-
if (!lc.ok) continue;
|
|
4386
|
-
const id = crypto.randomUUID();
|
|
4387
|
-
db.run(
|
|
4388
|
-
`INSERT INTO inbox (id, session_name, node_id, type, priority, content, from_session, network_id)
|
|
4389
|
-
VALUES (?1, ?2, ?3, 'broadcast', 'normal', ?4, 'api', ?5)`,
|
|
4390
|
-
[id, t.alias, t.node_id ?? null, body.message, t.network_id]
|
|
4391
|
-
);
|
|
4392
|
-
ids.push(id);
|
|
4393
|
-
delivered.push({ alias: t.alias, netId: t.network_id });
|
|
4394
|
-
}
|
|
4395
|
-
for (const d of delivered) {
|
|
4396
|
-
pushEvent(d.alias, { type: "broadcast", inbox_count: pendingInboxCount(d.alias, d.netId) }, d.netId);
|
|
4397
|
-
}
|
|
4376
|
+
// #2721 —— 与 MCP broadcast 同一条分批路径(reason 见 broadcast-fanout.ts 顶部)。
|
|
4377
|
+
const { ids } = await enqueueBroadcast(targets, body.message, "api", null, {
|
|
4378
|
+
// RFC-027 §2.3 inbox-enqueue lifecycle guard (PR1.2a REST site 2/2).
|
|
4379
|
+
isActive: (alias, netId) => assertNodeActive(alias, netId).ok,
|
|
4380
|
+
notify: (alias, netId) => pushEvent(alias, { type: "broadcast", inbox_count: pendingInboxCount(alias, netId) }, netId),
|
|
4381
|
+
});
|
|
4398
4382
|
return withCors(req, Response.json({ ok: true, recipients: ids.length, message_ids: ids }));
|
|
4399
4383
|
}
|
|
4400
4384
|
|
package/src/tools.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { parseScheduledWorkOutcome, qualifyScheduledWorkOutcome, saveScheduledWorkOutcome } from "./scheduled-work-outcome.js";
|
|
2
|
+
import { enqueueBroadcast } from "./broadcast-fanout.js";
|
|
2
3
|
import { selectHumanInboxIds } from "./human-inbox.js";
|
|
3
4
|
import { hostSupervisorIdentityRows } from "./host-supervisor-identity.js";
|
|
4
5
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
@@ -3351,34 +3352,13 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
|
|
|
3351
3352
|
if (filter_status) { sql += " AND status = ?"; params.push(filter_status); }
|
|
3352
3353
|
|
|
3353
3354
|
const targets = db.all<{ alias: string; node_id: string | null; network_id: string | null }>(sql, ...params);
|
|
3354
|
-
|
|
3355
|
-
//
|
|
3356
|
-
|
|
3357
|
-
// announced a broadcast to nodes the lifecycle guard had just
|
|
3358
|
-
// skipped, i.e. nodes with nothing to fetch.
|
|
3359
|
-
const delivered: Array<{ alias: string; netId: string | null }> = [];
|
|
3360
|
-
|
|
3361
|
-
for (const t of targets) {
|
|
3355
|
+
// #2721 —— 分批入库 + 批间让出(见 broadcast-fanout.ts 顶部:一条广播 384 行曾把 Hub
|
|
3356
|
+
// 打成 143.9 秒的 timer 饥饿)。语义不变:仍然逐个收件人一行、仍然 best-effort 跳过非 active。
|
|
3357
|
+
const { ids } = await enqueueBroadcast(targets, message, "hub", effectiveNetId, {
|
|
3362
3358
|
// RFC-027 §2.3 inbox-enqueue lifecycle guard (PR1.1 site 6/6).
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
const netId = effectiveNetId ?? t.network_id ?? null;
|
|
3367
|
-
const lc = assertNodeActive(t.alias, netId);
|
|
3368
|
-
if (!lc.ok) continue;
|
|
3369
|
-
const id = uuidv4();
|
|
3370
|
-
db.run(
|
|
3371
|
-
`INSERT INTO inbox (id, session_name, node_id, type, priority, content, from_session, network_id)
|
|
3372
|
-
VALUES (?1, ?2, ?3, 'broadcast', 'normal', ?4, 'hub', ?5)`,
|
|
3373
|
-
[id, t.alias, t.node_id ?? null, message, netId]
|
|
3374
|
-
);
|
|
3375
|
-
ids.push(id);
|
|
3376
|
-
delivered.push({ alias: t.alias, netId });
|
|
3377
|
-
}
|
|
3378
|
-
|
|
3379
|
-
for (const d of delivered) {
|
|
3380
|
-
pushEvent(d.alias, { type: "broadcast", inbox_count: pendingInboxCount(d.alias, d.netId) }, d.netId);
|
|
3381
|
-
}
|
|
3359
|
+
isActive: (alias, netId) => assertNodeActive(alias, netId).ok,
|
|
3360
|
+
notify: (alias, netId) => pushEvent(alias, { type: "broadcast", inbox_count: pendingInboxCount(alias, netId) }, netId),
|
|
3361
|
+
});
|
|
3382
3362
|
|
|
3383
3363
|
return {
|
|
3384
3364
|
content: [
|