@sleep2agi/commhub-server 0.9.0-preview.165 → 0.9.0-preview.167
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/alias-baseline-2571.test.ts +10 -0
- package/src/alias-baseline.ts +11 -0
- package/src/broadcast-fanout-2721.test.ts +99 -0
- package/src/broadcast-fanout.ts +71 -0
- package/src/db.ts +26 -7
- package/src/server.ts +19 -30
- 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.167",
|
|
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",
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
ALIAS_BASELINE_TABLE,
|
|
18
18
|
BASELINE_MIN_SAMPLES,
|
|
19
19
|
aliasBaseline,
|
|
20
|
+
aliasBaselineRefreshState,
|
|
20
21
|
ensureAliasBaselineTable,
|
|
21
22
|
refreshAliasBaseline,
|
|
22
23
|
} from "./alias-baseline.js";
|
|
@@ -137,6 +138,15 @@ describe("#2571 alias p99 baseline", () => {
|
|
|
137
138
|
expect(row!.p99Seconds).toBeLessThanOrEqual(11);
|
|
138
139
|
});
|
|
139
140
|
|
|
141
|
+
test("刷新状态可读(/health 用):时间 / 写入行数 / 当前总行数", () => {
|
|
142
|
+
refreshAliasBaseline(NOW);
|
|
143
|
+
const state = aliasBaselineRefreshState();
|
|
144
|
+
expect(state).not.toBeNull();
|
|
145
|
+
expect(typeof state!.at).toBe("string");
|
|
146
|
+
expect(state!.rows).toBeGreaterThanOrEqual(0);
|
|
147
|
+
expect(state!.aliases).toBeGreaterThanOrEqual(1); // 前面用例已写入若干 alias
|
|
148
|
+
});
|
|
149
|
+
|
|
140
150
|
test("样本不足不写基线行(退回全局)", () => {
|
|
141
151
|
const alias = `bl-thin-${stamp}`;
|
|
142
152
|
for (let i = 0; i < BASELINE_MIN_SAMPLES - 1; i++) {
|
package/src/alias-baseline.ts
CHANGED
|
@@ -29,6 +29,15 @@ export const BASELINE_P99_FACTOR = 1.5;
|
|
|
29
29
|
*/
|
|
30
30
|
export const BASELINE_MAX_SECONDS = 1800;
|
|
31
31
|
|
|
32
|
+
// #2571 —— 最近一次刷新的结果,供 /health 暴露(只读、聚合,不含 alias 明细)。
|
|
33
|
+
// 为什么需要:告警阈值依赖这张缓存表;「为什么这条没报/为什么报了」第一个要看的就是
|
|
34
|
+
// 「基线最近一次什么时候算的、算出来几行」。否则只能进机器 sqlite。
|
|
35
|
+
let lastRefresh: { at: string; rows: number; aliases: number } | null = null;
|
|
36
|
+
|
|
37
|
+
export function aliasBaselineRefreshState(): { at: string; rows: number; aliases: number } | null {
|
|
38
|
+
return lastRefresh;
|
|
39
|
+
}
|
|
40
|
+
|
|
32
41
|
export function ensureAliasBaselineTable(): void {
|
|
33
42
|
db.exec(`
|
|
34
43
|
CREATE TABLE IF NOT EXISTS ${ALIAS_BASELINE_TABLE} (
|
|
@@ -84,6 +93,8 @@ export function refreshAliasBaseline(now = new Date()): { rows: number } {
|
|
|
84
93
|
);
|
|
85
94
|
// 窗口内没有样本的 alias(长期不活跃)不该继续用旧基线兜着。
|
|
86
95
|
db.run(`DELETE FROM ${ALIAS_BASELINE_TABLE} WHERE computed_at < ?1`, [since]);
|
|
96
|
+
const aliases = db.get<{ n: number }>(`SELECT COUNT(*) AS n FROM ${ALIAS_BASELINE_TABLE}`)?.n ?? 0;
|
|
97
|
+
lastRefresh = { at: iso, rows: result.changes, aliases: Number(aliases) };
|
|
87
98
|
return { rows: result.changes };
|
|
88
99
|
}
|
|
89
100
|
|
|
@@ -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
|
@@ -1482,25 +1482,39 @@ console.log("[commhub] node_id backfill: deferred until after start (#2664)");
|
|
|
1482
1482
|
*
|
|
1483
1483
|
* 调用点:startHub() 监听成功后延迟数秒执行(见 server.ts)。
|
|
1484
1484
|
*/
|
|
1485
|
-
export function runDeferredBootBackfills(): { nodeIdsMs: number; terminalEventsMs: number } {
|
|
1485
|
+
export async function runDeferredBootBackfills(): Promise<{ nodeIdsMs: number; terminalEventsMs: number; chunks: number }> {
|
|
1486
|
+
// #2664(第二批)—— 前一版把两条回填整体挪到启动后,但**每条仍是一次性长查询**:
|
|
1487
|
+
// 生产实测 `node_id=1194ms terminal_events=1593ms`,对应一次 2.7秒级的 stall
|
|
1488
|
+
// (`.165`/`.166` 启动窗口都观测到 3–3.7 秒)。这里把它们切成小块、块间让出事件循环,
|
|
1489
|
+
// 让「2.7 秒卡一下」变成「若干次 <300ms 的让出」。
|
|
1486
1490
|
const t0 = performance.now();
|
|
1487
1491
|
try { backfillMessageNodeIds(); } catch (e: any) {
|
|
1488
1492
|
console.error(`[commhub] node_id backfill failed: ${e?.message || e}`);
|
|
1489
1493
|
}
|
|
1494
|
+
await Bun.sleep(0); // 两条之间先让一次:即使前半段偏慢,也不会和后半段叠成一段长阻塞
|
|
1490
1495
|
const t1 = performance.now();
|
|
1496
|
+
let chunks = 0;
|
|
1491
1497
|
try {
|
|
1492
1498
|
if (db.dialect === "sqlite") {
|
|
1493
|
-
db.
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1499
|
+
const maxRow = db.get<{ m: number | null }>("SELECT MAX(rowid) AS m FROM tasks")?.m ?? 0;
|
|
1500
|
+
const CHUNK = 20000;
|
|
1501
|
+
for (let lo = 0; lo <= maxRow; lo += CHUNK) {
|
|
1502
|
+
db.run(`INSERT OR IGNORE INTO task_terminal_events (task_id, network_id, from_node_id, completed_at)
|
|
1503
|
+
SELECT task_id, network_id, from_node_id, COALESCE(completed_at, created_at)
|
|
1504
|
+
FROM tasks
|
|
1505
|
+
WHERE rowid BETWEEN ?1 AND ?2
|
|
1506
|
+
AND status IN ('replied', 'failed', 'cancelled', 'expired')
|
|
1507
|
+
ORDER BY COALESCE(completed_at, created_at), task_id`,
|
|
1508
|
+
[lo, lo + CHUNK - 1]);
|
|
1509
|
+
chunks++;
|
|
1510
|
+
await Bun.sleep(0); // 每 2 万行让出一次
|
|
1511
|
+
}
|
|
1498
1512
|
}
|
|
1499
1513
|
} catch (e: any) {
|
|
1500
1514
|
console.error(`[commhub] task_terminal_events backfill failed: ${e?.message || e}`);
|
|
1501
1515
|
}
|
|
1502
1516
|
const t2 = performance.now();
|
|
1503
|
-
return { nodeIdsMs: Math.round(t1 - t0), terminalEventsMs: Math.round(t2 - t1) };
|
|
1517
|
+
return { nodeIdsMs: Math.round(t1 - t0), terminalEventsMs: Math.round(t2 - t1), chunks };
|
|
1504
1518
|
}
|
|
1505
1519
|
|
|
1506
1520
|
// ── P0: sessions alias uniqueness is network-scoped.
|
|
@@ -1599,6 +1613,11 @@ migrateSessionsNetworkAliasUnique();
|
|
|
1599
1613
|
try { db.exec("CREATE INDEX IF NOT EXISTS idx_sessions_network ON sessions(network_id)"); } catch {}
|
|
1600
1614
|
try { db.exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_sessions_network_alias_unique ON sessions(network_id, alias)"); } catch {}
|
|
1601
1615
|
try { db.exec("CREATE INDEX IF NOT EXISTS idx_tasks_network ON tasks(network_id)"); } catch {}
|
|
1616
|
+
// #2722 —— 任务统计(`SELECT status, COUNT(*) FROM tasks WHERE network_id=? GROUP BY status`)是
|
|
1617
|
+
// Dashboard/状态接口最常走的聚合:只有上面的单列索引时,它要扫该网络**全部**行再建临时 B 树
|
|
1618
|
+
// (生产 116k 行实测 **5325ms**;同库副本 294ms)。加覆盖索引后副本 **24ms(12×)**。
|
|
1619
|
+
// 只加列、不改任何查询;老库首次启动多花约 0.4 秒建索引。
|
|
1620
|
+
try { db.exec("CREATE INDEX IF NOT EXISTS idx_tasks_network_status ON tasks(network_id, status)"); } catch {}
|
|
1602
1621
|
// #500 — covering index for the full /api/status queue_depth GROUP BY (task-queue-ahead.ts queueDepthQuery): the 24 h
|
|
1603
1622
|
// created_at range plus every column that query reads, so it never touches the (wide) task rows. Here, not in the
|
|
1604
1623
|
// 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
|
@@ -30,7 +30,8 @@ import { recentNodeIdentityConflicts } from "./node-identity-conflict.js";
|
|
|
30
30
|
import { createSSEStream, createNetworkObserverStream, createUserEventStream, pushEvent, pushNetworkObserverEvent, getSSEStats, PRINTABLE_OBSERVER_KEY_PREFIX, closeUserStreamsInNetwork, getUserPresence, onUserPresenceChange, pushUserEventToNetwork } from "./push.js";
|
|
31
31
|
import { enterMcpInflight, leaveMcpInflight, getMcpInflightStats } from "./mcp-inflight.js";
|
|
32
32
|
import { eventLoopStats, startEventLoopLagMonitor } from "./event-loop-lag.js";
|
|
33
|
-
import { refreshAliasBaseline } from "./alias-baseline.js";
|
|
33
|
+
import { aliasBaselineRefreshState, 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";
|
|
@@ -2809,6 +2810,9 @@ return Bun.serve({
|
|
|
2809
2810
|
// 单次 tick 的最大间隔(正常 ≈250ms),recent_stalls 保留最近 20 次
|
|
2810
2811
|
// 超过阈值的停顿(含当时 RSS),用于和内存曲线对齐。
|
|
2811
2812
|
event_loop: eventLoopStats(),
|
|
2813
|
+
// #2571 —— 告警阈值用的「每目标 p99 基线」最近一次刷新(只读聚合:时间/写了几行/现在共几行)。
|
|
2814
|
+
// 没有它,「这条告警为什么没报」只能进机器 sqlite 查。
|
|
2815
|
+
alert_baselines: aliasBaselineRefreshState(),
|
|
2812
2816
|
auth: DEV_OPEN ? "dev-open" : "user-token",
|
|
2813
2817
|
security: DEV_OPEN ? "dev-open" : "secured",
|
|
2814
2818
|
tmux: TMUX_ENABLED ? "enabled" : "disabled",
|
|
@@ -4372,29 +4376,12 @@ return Bun.serve({
|
|
|
4372
4376
|
if (body.filter_server) { sql += " AND server = ?"; params.push(body.filter_server); }
|
|
4373
4377
|
if (body.filter_status) { sql += " AND status = ?"; params.push(body.filter_status); }
|
|
4374
4378
|
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
|
-
}
|
|
4379
|
+
// #2721 —— 与 MCP broadcast 同一条分批路径(reason 见 broadcast-fanout.ts 顶部)。
|
|
4380
|
+
const { ids } = await enqueueBroadcast(targets, body.message, "api", null, {
|
|
4381
|
+
// RFC-027 §2.3 inbox-enqueue lifecycle guard (PR1.2a REST site 2/2).
|
|
4382
|
+
isActive: (alias, netId) => assertNodeActive(alias, netId).ok,
|
|
4383
|
+
notify: (alias, netId) => pushEvent(alias, { type: "broadcast", inbox_count: pendingInboxCount(alias, netId) }, netId),
|
|
4384
|
+
});
|
|
4398
4385
|
return withCors(req, Response.json({ ok: true, recipients: ids.length, message_ids: ids }));
|
|
4399
4386
|
}
|
|
4400
4387
|
|
|
@@ -5662,12 +5649,14 @@ export function startHub(opts?: { port?: number; hostname?: string }): ReturnTyp
|
|
|
5662
5649
|
// 生产实测合计约 3.5 秒,正好落在会话重连风暴窗口里。现在等监听稳定后再跑:
|
|
5663
5650
|
// 它们只修历史缺口,新行由触发器实时写入。
|
|
5664
5651
|
const deferredBackfillTimer = setTimeout(() => {
|
|
5665
|
-
|
|
5666
|
-
|
|
5667
|
-
|
|
5668
|
-
|
|
5669
|
-
|
|
5670
|
-
|
|
5652
|
+
void (async () => {
|
|
5653
|
+
try {
|
|
5654
|
+
const r = await runDeferredBootBackfills();
|
|
5655
|
+
console.log(`[commhub] deferred boot backfills: node_id=${r.nodeIdsMs}ms terminal_events=${r.terminalEventsMs}ms chunks=${r.chunks}`);
|
|
5656
|
+
} catch (e: any) {
|
|
5657
|
+
console.error(`[commhub] deferred boot backfills failed: ${e?.message || e}`);
|
|
5658
|
+
}
|
|
5659
|
+
})();
|
|
5671
5660
|
}, Number(process.env.COMMHUB_BOOT_BACKFILL_DELAY_MS) >= 0 ? Number(process.env.COMMHUB_BOOT_BACKFILL_DELAY_MS) : 15_000);
|
|
5672
5661
|
(deferredBackfillTimer as any)?.unref?.();
|
|
5673
5662
|
|
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: [
|