@sleep2agi/commhub-server 0.9.0-preview.166 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/commhub-server",
3
- "version": "0.9.0-preview.166",
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++) {
@@ -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
 
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.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`);
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.
package/src/server.ts CHANGED
@@ -30,7 +30,7 @@ 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
34
  import { enqueueBroadcast } from "./broadcast-fanout.js";
35
35
  import { assertNodeActive } from "./lifecycle-guard.js";
36
36
  import { pendingInboxCount } from "./inbox-count.js";
@@ -2810,6 +2810,9 @@ return Bun.serve({
2810
2810
  // 单次 tick 的最大间隔(正常 ≈250ms),recent_stalls 保留最近 20 次
2811
2811
  // 超过阈值的停顿(含当时 RSS),用于和内存曲线对齐。
2812
2812
  event_loop: eventLoopStats(),
2813
+ // #2571 —— 告警阈值用的「每目标 p99 基线」最近一次刷新(只读聚合:时间/写了几行/现在共几行)。
2814
+ // 没有它,「这条告警为什么没报」只能进机器 sqlite 查。
2815
+ alert_baselines: aliasBaselineRefreshState(),
2813
2816
  auth: DEV_OPEN ? "dev-open" : "user-token",
2814
2817
  security: DEV_OPEN ? "dev-open" : "secured",
2815
2818
  tmux: TMUX_ENABLED ? "enabled" : "disabled",
@@ -5646,12 +5649,14 @@ export function startHub(opts?: { port?: number; hostname?: string }): ReturnTyp
5646
5649
  // 生产实测合计约 3.5 秒,正好落在会话重连风暴窗口里。现在等监听稳定后再跑:
5647
5650
  // 它们只修历史缺口,新行由触发器实时写入。
5648
5651
  const deferredBackfillTimer = setTimeout(() => {
5649
- try {
5650
- const r = runDeferredBootBackfills();
5651
- console.log(`[commhub] deferred boot backfills: node_id=${r.nodeIdsMs}ms terminal_events=${r.terminalEventsMs}ms`);
5652
- } catch (e: any) {
5653
- console.error(`[commhub] deferred boot backfills failed: ${e?.message || e}`);
5654
- }
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
+ })();
5655
5660
  }, Number(process.env.COMMHUB_BOOT_BACKFILL_DELAY_MS) >= 0 ? Number(process.env.COMMHUB_BOOT_BACKFILL_DELAY_MS) : 15_000);
5656
5661
  (deferredBackfillTimer as any)?.unref?.();
5657
5662