@sleep2agi/commhub-server 0.9.0-preview.164 → 0.9.0-preview.165
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/db.ts +37 -6
- package/src/server.ts +15 -1
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.165",
|
|
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",
|
package/src/db.ts
CHANGED
|
@@ -1467,7 +1467,41 @@ export function backfillMessageNodeIds() {
|
|
|
1467
1467
|
console.log(`[commhub] node_id backfill: inbox=${inboxDirect + inboxRenamed}, tasks.to=${tasksToDirect + tasksToRenamed}, tasks.from=${tasksFromDirect + tasksFromRenamed}, total=${total}, took=${Math.round(performance.now() - t0)}ms`);
|
|
1468
1468
|
}
|
|
1469
1469
|
|
|
1470
|
-
|
|
1470
|
+
// #2664 —— 不在 import 时跑(见 runDeferredBootBackfills 的说明)。这里留一行**准确**的日志:
|
|
1471
|
+
// · 有测试/运维会 grep `node_id backfill` 来确认迁移路径被走到过;
|
|
1472
|
+
// · 真正的汇总行(带 took=)在 runDeferredBootBackfills() 里,启动后 15 秒出现。
|
|
1473
|
+
console.log("[commhub] node_id backfill: deferred until after start (#2664)");
|
|
1474
|
+
|
|
1475
|
+
/**
|
|
1476
|
+
* #2664 —— 两个 boot 期的历史回填,挪到「服务已开始监听之后」再跑。
|
|
1477
|
+
*
|
|
1478
|
+
* 为什么:这两条语句在生产上合计约 3.5 秒(慢 SQL 日志实测:`UPDATE tasks … FROM` 1511ms +
|
|
1479
|
+
* `INSERT OR IGNORE INTO task_terminal_events` 2137ms),而它们只修**历史缺口**——
|
|
1480
|
+
* 新行由触发器实时写入。把 3.5 秒挡在启动路径上,等于让 369 个会话的重连风暴多等 3.5 秒,
|
|
1481
|
+
* 而这段时间里 Hub 本来就最脆弱。
|
|
1482
|
+
*
|
|
1483
|
+
* 调用点:startHub() 监听成功后延迟数秒执行(见 server.ts)。
|
|
1484
|
+
*/
|
|
1485
|
+
export function runDeferredBootBackfills(): { nodeIdsMs: number; terminalEventsMs: number } {
|
|
1486
|
+
const t0 = performance.now();
|
|
1487
|
+
try { backfillMessageNodeIds(); } catch (e: any) {
|
|
1488
|
+
console.error(`[commhub] node_id backfill failed: ${e?.message || e}`);
|
|
1489
|
+
}
|
|
1490
|
+
const t1 = performance.now();
|
|
1491
|
+
try {
|
|
1492
|
+
if (db.dialect === "sqlite") {
|
|
1493
|
+
db.run(`INSERT OR IGNORE INTO task_terminal_events (task_id, network_id, from_node_id, completed_at)
|
|
1494
|
+
SELECT task_id, network_id, from_node_id, COALESCE(completed_at, created_at)
|
|
1495
|
+
FROM tasks
|
|
1496
|
+
WHERE status IN ('replied', 'failed', 'cancelled', 'expired')
|
|
1497
|
+
ORDER BY COALESCE(completed_at, created_at), task_id`);
|
|
1498
|
+
}
|
|
1499
|
+
} catch (e: any) {
|
|
1500
|
+
console.error(`[commhub] task_terminal_events backfill failed: ${e?.message || e}`);
|
|
1501
|
+
}
|
|
1502
|
+
const t2 = performance.now();
|
|
1503
|
+
return { nodeIdsMs: Math.round(t1 - t0), terminalEventsMs: Math.round(t2 - t1) };
|
|
1504
|
+
}
|
|
1471
1505
|
|
|
1472
1506
|
// ── P0: sessions alias uniqueness is network-scoped.
|
|
1473
1507
|
// Older SQLite databases created `alias TEXT UNIQUE`, which prevents the same
|
|
@@ -1656,11 +1690,8 @@ if (db.dialect === "sqlite") {
|
|
|
1656
1690
|
VALUES (NEW.task_id, NEW.network_id, NEW.from_node_id, COALESCE(NEW.completed_at, datetime('now')));
|
|
1657
1691
|
END;
|
|
1658
1692
|
`);
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
FROM tasks
|
|
1662
|
-
WHERE status IN ('replied', 'failed', 'cancelled', 'expired')
|
|
1663
|
-
ORDER BY COALESCE(completed_at, created_at), task_id`);
|
|
1693
|
+
// #2664 —— 回填本身挪到 runDeferredBootBackfills()(启动后再跑)。这里只留触发器定义:
|
|
1694
|
+
// 触发器负责**新**行的写入,回填只修历史缺口,不该占启动的关键路径。
|
|
1664
1695
|
} else {
|
|
1665
1696
|
db.exec(`
|
|
1666
1697
|
CREATE OR REPLACE FUNCTION record_task_terminal_event() RETURNS trigger AS $$
|
package/src/server.ts
CHANGED
|
@@ -23,7 +23,7 @@ import { normalizeSessionRuntime } from "./runtime-label.js";
|
|
|
23
23
|
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
|
|
24
24
|
import { z } from "zod/v4";
|
|
25
25
|
import { registerTools } from "./tools.js";
|
|
26
|
-
import { db, logTaskEvent, logAudit, syncScheduledRunForTask } from "./db.js";
|
|
26
|
+
import { db, logTaskEvent, logAudit, syncScheduledRunForTask, runDeferredBootBackfills } from "./db.js";
|
|
27
27
|
import { ensureNetworkRulesOnce, readNetworkRules, writeNetworkRules } from "./network-rules.js";
|
|
28
28
|
import { deriveTeamRulesView } from "./team-rules-status.js";
|
|
29
29
|
import { recentNodeIdentityConflicts } from "./node-identity-conflict.js";
|
|
@@ -5658,6 +5658,19 @@ export function startHub(opts?: { port?: number; hostname?: string }): ReturnTyp
|
|
|
5658
5658
|
// “同一秒里 retention 跑了 10 秒 + 有两百个请求在排队”。
|
|
5659
5659
|
const eventLoopLagTimer = startEventLoopLagMonitor();
|
|
5660
5660
|
|
|
5661
|
+
// #2664 —— 两个历史回填(node_id 映射 / task_terminal_events)原来在 import 阶段跑,
|
|
5662
|
+
// 生产实测合计约 3.5 秒,正好落在会话重连风暴窗口里。现在等监听稳定后再跑:
|
|
5663
|
+
// 它们只修历史缺口,新行由触发器实时写入。
|
|
5664
|
+
const deferredBackfillTimer = setTimeout(() => {
|
|
5665
|
+
try {
|
|
5666
|
+
const r = runDeferredBootBackfills();
|
|
5667
|
+
console.log(`[commhub] deferred boot backfills: node_id=${r.nodeIdsMs}ms terminal_events=${r.terminalEventsMs}ms`);
|
|
5668
|
+
} catch (e: any) {
|
|
5669
|
+
console.error(`[commhub] deferred boot backfills failed: ${e?.message || e}`);
|
|
5670
|
+
}
|
|
5671
|
+
}, Number(process.env.COMMHUB_BOOT_BACKFILL_DELAY_MS) >= 0 ? Number(process.env.COMMHUB_BOOT_BACKFILL_DELAY_MS) : 15_000);
|
|
5672
|
+
(deferredBackfillTimer as any)?.unref?.();
|
|
5673
|
+
|
|
5661
5674
|
// #2571(第三轮)—— 每个目标自己的投递延迟 p99 基线,供 delivered_stale 判据使用。
|
|
5662
5675
|
// 生产库上重算一次约 0.46s(116k tasks / 7 天窗口),所以只每小时一次、且与 retention 错开。
|
|
5663
5676
|
// 失败不影响任何请求路径:基线表只是缓存,读不到就退回全局阈值。
|
|
@@ -5720,6 +5733,7 @@ export function startHub(opts?: { port?: number; hostname?: string }): ReturnTyp
|
|
|
5720
5733
|
clearInterval(retentionSweeperTimer);
|
|
5721
5734
|
clearInterval(eventLoopLagTimer);
|
|
5722
5735
|
clearInterval(aliasBaselineTimer);
|
|
5736
|
+
clearTimeout(deferredBackfillTimer);
|
|
5723
5737
|
clearInterval(staleSweeperTimer);
|
|
5724
5738
|
clearInterval(rateLimitSweepTimer);
|
|
5725
5739
|
clearInterval(taskPatrolTimer);
|