@sleep2agi/commhub-server 0.9.0-preview.209 → 0.9.0-preview.210

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.209",
3
+ "version": "0.9.0-preview.210",
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",
@@ -98,6 +98,9 @@ describe("event-loop-lag: startEventLoopLagMonitor", () => {
98
98
  thresholdMs: 40,
99
99
  log: (m) => logs.push(m),
100
100
  rssMb: () => 4502,
101
+ // 这条测的是「停顿怎么打日志」,不是启动尖峰。测试进程刚起来几秒,
102
+ // 不关掉启动宽限的话第一个 tick 会被当成启动尖峰跳过(见 #2721 那组用例)。
103
+ bootGraceMs: 0,
101
104
  });
102
105
  const until = performance.now() + 120;
103
106
  while (performance.now() < until) { /* spin 阻塞事件循环 */ }
@@ -138,23 +141,40 @@ describe("db-adapter: 慢 SQL 日志", () => {
138
141
  });
139
142
  });
140
143
 
141
- // #2721 —— 启动预热窗内的 gap 不算停顿。
144
+ // #2721 —— **只跳过第一个 tick**。
142
145
  //
143
- // 实测:2026-10-29 进程 22:21:06 启动,14:21:09.835Z(= 启动后 3 秒)记到 gap 2972ms / stall 2722ms。
144
- // 那是模块加载 + 打开 DB + 迁移,不是线上卡顿。不排除它的后果是:每次部署都会在 /health 留一条
145
- // 与真实卡顿同形的记录,事后无法区分。
146
- describe("#2721 启动预热不算停顿", () => {
147
- test("预热窗内的 tick gap 不计入 stalls(窗口外照常计)", async () => {
148
- const stalls: string[] = [];
146
+ // 事故形状:进程启动后 3 秒记到 gap 2972ms —— 那是「监控开始 → 第一次 tick」被
147
+ // 模块加载/开库/迁移占满的时间,度量的是启动耗时,不是线上停顿。
148
+ //
149
+ // 第一版实现用的是 10 秒**时间窗**,等于监控在前 10 秒什么都不记,
150
+ // 把下面那条「阈值 40ms、spin 120ms 应打停顿日志」的既有测试整体吞掉了
151
+ // —— 修完 qa.yml 事故、QA 恢复后立刻红。窗口越宽越像「把监控关一会儿」。
152
+ describe("#2721 启动尖峰不算停顿(判据:进程 uptime)", () => {
153
+ test("进程刚启动时的第一个 gap 被跳过", async () => {
154
+ resetEventLoopLagForTests();
155
+ const logs: string[] = [];
156
+ const timer = startEventLoopLagMonitor({
157
+ intervalMs: 20, thresholdMs: 40, log: (m) => logs.push(m), rssMb: () => 123,
158
+ bootGraceMs: 15_000, uptimeMs: () => 3_000, // 模拟「启动后 3 秒」
159
+ });
160
+ const until = performance.now() + 120;
161
+ while (performance.now() < until) { /* spin 落在第一个 tick */ }
162
+ await Bun.sleep(80);
163
+ clearInterval(timer);
164
+ expect(logs).toEqual([]);
165
+ });
166
+
167
+ test("进程已经跑了一会儿 ⇒ 第一个 gap 照常记录(不能把监控关掉)", async () => {
168
+ resetEventLoopLagForTests();
169
+ const logs: string[] = [];
149
170
  const timer = startEventLoopLagMonitor({
150
- intervalMs: 10,
151
- thresholdMs: 1,
152
- warmupMs: 120,
153
- log: (m) => stalls.push(m),
171
+ intervalMs: 20, thresholdMs: 40, log: (m) => logs.push(m), rssMb: () => 123,
172
+ bootGraceMs: 15_000, uptimeMs: () => 600_000, // 进程跑了 10 分钟
154
173
  });
155
- await new Promise((r) => setTimeout(r, 300));
156
- clearInterval(timer as unknown as ReturnType<typeof setInterval>);
157
- // 预热窗内的模拟拖延不该产生 stall 日志;窗外的真实间隔是 ~10ms,也不该触发 threshold。
158
- expect(stalls).toHaveLength(0);
174
+ const until = performance.now() + 120;
175
+ while (performance.now() < until) { /* spin */ }
176
+ await Bun.sleep(80);
177
+ clearInterval(timer);
178
+ expect(logs.length).toBeGreaterThan(0);
159
179
  });
160
180
  });
@@ -123,8 +123,23 @@ export function startEventLoopLagMonitor(opts: {
123
123
  thresholdMs?: number;
124
124
  log?: (msg: string) => void;
125
125
  rssMb?: () => number;
126
- /** #2721 —— 启动预热窗(毫秒),窗内的 tick gap 不计入停顿。默认读 COMMHUB_STALL_WARMUP_MS / 10000。 */
127
- warmupMs?: number;
126
+ /**
127
+ * #2721 —— 启动期的 tick gap 不算停顿。判据是**进程启动多久了**,不是第几个 tick。
128
+ *
129
+ * 事故形状:进程 22:21:06 启动,14:21:09.835Z 记到 gap 2972ms —— 启动后 3 秒。
130
+ * 那段 gap 度量的是「监控开始 → 第一次 tick」被模块加载/开库/迁移占满的时间。
131
+ *
132
+ * 两版错误实现(都试过,都被既有测试抓住):
133
+ * ① 10 秒**时间窗** → 监控在前 10 秒什么都不记,「阈值 40ms + spin 120ms 应打日志」直接吞掉
134
+ * ② 只跳**第一个 tick** → 测试里的 spin 恰好落在第一个 tick,同样吞掉
135
+ *
136
+ * 真正的区别不是「第几个 tick」,而是**这段时间里进程有没有可能还在启动**。
137
+ * 所以:只有当 tick 到来时进程 uptime 仍小于 bootGraceMs,才把 gap 当作启动尖峰。
138
+ * 启动 20 秒之后的第一条 gap 依旧照常记录 —— 那时候它不可能是启动耗时。
139
+ */
140
+ bootGraceMs?: number;
141
+ /** 测试注入:返回进程已运行的毫秒数(默认 process.uptime())。 */
142
+ uptimeMs?: () => number;
128
143
  } = {}): ReturnType<typeof setInterval> {
129
144
  const intervalMs = opts.intervalMs ?? Math.max(50, envMs("COMMHUB_STALL_TICK_MS", 250));
130
145
  const thresholdMs = opts.thresholdMs ?? envMs("COMMHUB_STALL_LOG_MS", 2000);
@@ -136,7 +151,9 @@ export function startEventLoopLagMonitor(opts: {
136
151
  // 实测:2026-10-29 这次进程 22:21:06 起来,14:21:09.835Z 记到一次 gap 2972ms,
137
152
  // 也就是**启动后 3 秒**。那段时间在做模块加载 / 打开 DB / 迁移,第一个 tick 必然偏大。
138
153
  // 不排除它的后果是:每次部署都会在 `/health` 里留一条「停顿」,而它和线上真实卡顿长得一模一样。
139
- const warmupMs = opts.warmupMs ?? envMs("COMMHUB_STALL_WARMUP_MS", 10_000);
154
+ const bootGraceMs = opts.bootGraceMs ?? envMs("COMMHUB_STALL_BOOT_GRACE_MS", 15_000);
155
+ const uptimeMs = opts.uptimeMs ?? (() => process.uptime() * 1000);
156
+ let firstTick = true;
140
157
  const timer = setInterval(() => {
141
158
  const now = performance.now();
142
159
  const gap = now - last;
@@ -144,7 +161,12 @@ export function startEventLoopLagMonitor(opts: {
144
161
  const rss = rssMb();
145
162
  const served = servedSinceTick;
146
163
  servedSinceTick = 0;
147
- if (now - startedAt < warmupMs) return; // 预热窗内只更新 last,不记 gap/停顿
164
+ // 启动尖峰:只可能是**第一个** tick,且到来时进程才刚起来。
165
+ // 两个条件缺一不可 —— 只看「第一个 tick」会吞掉运行中立刻发生的真实停顿。
166
+ if (firstTick) {
167
+ firstTick = false;
168
+ if (uptimeMs() < bootGraceMs) return;
169
+ }
148
170
  const stall = recordGap(gap, { intervalMs, thresholdMs, rssMb: rss, served });
149
171
  if (thresholdMs > 0 && stall >= thresholdMs) {
150
172
  // 和 retention/slow-sql 的日志同一前缀,方便 grep。
@@ -522,7 +522,10 @@ describe("未绑定令牌的写调用:默认只记录,不拦", () => {
522
522
  const unboundKey = () => {
523
523
  const alias = `ghost-alias-${stamp}`;
524
524
  const tokenId = `tok_ghost_${stamp}`;
525
- db.run("INSERT OR IGNORE INTO api_tokens (token_id, token_hash, user_id, network_id, name, scope) VALUES (?1, ?2, ?3, ?4, ?5, 'network')",
525
+ // 🔴 不能用 `INSERT OR IGNORE`:那是 SQLite 专有语法,PostgreSQL 会直接
526
+ // `syntax error at or near "OR"`(test2123-hub-postgres-ladder 实测)。
527
+ // 两边都认的写法是 ON CONFLICT ... DO NOTHING。
528
+ db.run("INSERT INTO api_tokens (token_id, token_hash, user_id, network_id, name, scope) VALUES (?1, ?2, ?3, ?4, ?5, 'network') ON CONFLICT (token_id) DO NOTHING",
526
529
  [tokenId, `hash_${tokenId}`, user.boss.id, net, `node:${alias}`]);
527
530
  return tokenId;
528
531
  };