@sleep2agi/commhub-server 0.9.0-preview.173 → 0.9.0-preview.175

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.173",
3
+ "version": "0.9.0-preview.175",
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,31 @@
1
+ // #2614(第三轮)—— 首次 sweep 不是“等 N 秒”,而是“等循环安静”。
2
+ //
3
+ // 两次部署实测:60 秒后仍撞上风暴(max 1238ms);180 秒后撞得更狠(max 3919ms,风暴持续 5 分钟)。
4
+ // 这里钉住判据本身(纯函数),不看时钟、不看真实 hub。
5
+ import { describe, expect, test } from "bun:test";
6
+ import { firstSweepShouldWait, FIRST_SWEEP_QUIET_INFLIGHT } from "./retention.js";
7
+
8
+ const NOW = Date.parse("2026-10-27T12:00:00.000Z");
9
+ const iso = (s: number) => new Date(NOW - s * 1000).toISOString();
10
+
11
+ describe("firstSweepShouldWait (#2614)", () => {
12
+ test("waits while requests are queued", () => {
13
+ expect(firstSweepShouldWait({ now: NOW, inflight: FIRST_SWEEP_QUIET_INFLIGHT + 1, lastStallAtIso: null })).toBe(true);
14
+ });
15
+
16
+ test("waits while the loop stalled within the last minute", () => {
17
+ expect(firstSweepShouldWait({ now: NOW, inflight: 0, lastStallAtIso: iso(30) })).toBe(true);
18
+ });
19
+
20
+ test("runs once inflight is low and the last stall is old enough", () => {
21
+ expect(firstSweepShouldWait({ now: NOW, inflight: 3, lastStallAtIso: iso(120) })).toBe(false);
22
+ });
23
+
24
+ test("runs when there has never been a stall", () => {
25
+ expect(firstSweepShouldWait({ now: NOW, inflight: 0, lastStallAtIso: null })).toBe(false);
26
+ });
27
+
28
+ test("an unparseable stall timestamp does not block forever", () => {
29
+ expect(firstSweepShouldWait({ now: NOW, inflight: 0, lastStallAtIso: "not-a-date" })).toBe(false);
30
+ });
31
+ });
package/src/retention.ts CHANGED
@@ -24,6 +24,8 @@
24
24
  // COMMHUB_DB=/tmp/test-retention.db bun test src/retention.test.ts
25
25
 
26
26
  import { db } from "./db.js";
27
+ import { getMcpInflightStats } from "./mcp-inflight.js";
28
+ import { eventLoopStats } from "./event-loop-lag.js";
27
29
  import { sweepNodeRequestContent, type NodeRequestSweepResult } from "./node-request-retention.js";
28
30
 
29
31
  export type RetentionConfig = {
@@ -131,12 +133,60 @@ const DEFAULT_CHUNK_ROWS = 2000;
131
133
  const INCREMENTAL_VACUUM_PAGES = 200;
132
134
  const MAX_TABLE_MS = 10 * 60 * 1000;
133
135
 
134
- /** 启动后第一次 sweep 的延迟;默认 60 秒(见 startRetentionSweeper 里的实测)。 */
136
+ /**
137
+ * #2614(第三轮)—— 首次 sweep 要等**循环真的安静下来**,而不是固定等 N 秒。
138
+ *
139
+ * 2026-10-27 两次部署实测:
140
+ * ```
141
+ * 18:55 部署:60 秒后 sweep → 3894ms / max 1238ms(风暴 2 分钟后仍在)
142
+ * 19:51 部署:180 秒后 sweep → 9153ms / max 3919ms(风暴持续 5 分钟,RSS 3.6 GB)
143
+ * ```
144
+ * 固定延迟只是把「撞上风暴」变成「可能撞上风暴」——风暴长短取决于同时重连多少节点。
145
+ *
146
+ * 判据(都来自 `/health` 的同名指标):
147
+ * · `mcp_inflight < 20`:没有成批请求在排队;
148
+ * · 最近一次 stall 至少在 60 秒前:循环刚刚不抖。
149
+ * 每 15 秒看一次,最多等 15 分钟;等不到就照常跑(宁可慢,不可不删)。
150
+ */
151
+ export const FIRST_SWEEP_QUIET_INFLIGHT = 20;
152
+ export const FIRST_SWEEP_QUIET_STALL_AGE_MS = 60_000;
153
+ export const FIRST_SWEEP_MAX_WAIT_MS = 15 * 60_000;
154
+ export const FIRST_SWEEP_POLL_MS = 15_000;
155
+
156
+ /** 纯函数,便于单测:把「此刻该不该继续等」与时钟解耦。 */
157
+ export function firstSweepShouldWait(input: {
158
+ now: number;
159
+ inflight: number;
160
+ lastStallAtIso: string | null;
161
+ }): boolean {
162
+ if (input.inflight >= FIRST_SWEEP_QUIET_INFLIGHT) return true;
163
+ if (!input.lastStallAtIso) return false;
164
+ const last = Date.parse(input.lastStallAtIso);
165
+ if (!Number.isFinite(last)) return false;
166
+ return input.now - last < FIRST_SWEEP_QUIET_STALL_AGE_MS;
167
+ }
168
+
169
+ async function waitForQuietLoop(): Promise<void> {
170
+ const startedMs = Date.now();
171
+ while (Date.now() - startedMs < FIRST_SWEEP_MAX_WAIT_MS) {
172
+ const stats = eventLoopStats();
173
+ if (!firstSweepShouldWait({
174
+ now: Date.now(),
175
+ inflight: getMcpInflightStats().inflight,
176
+ lastStallAtIso: stats.last_stall_at,
177
+ })) {
178
+ return;
179
+ }
180
+ await Bun.sleep(FIRST_SWEEP_POLL_MS);
181
+ }
182
+ }
183
+
184
+ /** 启动后第一次 sweep 的延迟;默认 180 秒(见 startRetentionSweeper 里的实测)。 */
135
185
  function readFirstSweepDelayMs(): number {
136
186
  const raw = process.env.COMMHUB_RETENTION_FIRST_DELAY_MS;
137
- if (raw === undefined || raw.trim() === "") return 60_000;
187
+ if (raw === undefined || raw.trim() === "") return 180_000;
138
188
  const n = Number(raw);
139
- return Number.isFinite(n) && n >= 0 ? Math.floor(n) : 60_000;
189
+ return Number.isFinite(n) && n >= 0 ? Math.floor(n) : 180_000;
140
190
  }
141
191
 
142
192
  function readChunkRows(): number {
@@ -516,8 +566,23 @@ export function startRetentionSweeper(
516
566
  // ```
517
567
  // 同一批删除在稳态是 0.24ms/行,在启动那一分钟是 4.2ms/行(慢 17 倍)——
518
568
  // 不是删除变慢了,是它在和重连风暴抢 CPU/WAL。而这一分钟的阻塞最容易被误记成
519
- // “retention 又卡了”。默认延后 60 秒(可用 COMMHUB_RETENTION_FIRST_DELAY_MS 覆盖,0=立即)。
520
- if (firstDelayMs > 0) setTimeout(() => { void sweep(); }, firstDelayMs);
521
- else setImmediate(() => { void sweep(); });
569
+ // “retention 又卡了”。默认延后 180 秒(可用 COMMHUB_RETENTION_FIRST_DELAY_MS 覆盖,0=立即)。
570
+ //
571
+ // 为什么是 180 而不是 60(.173 上线后实测修正):
572
+ // ```
573
+ // 18:55:24 boot
574
+ // 18:55:38 stall 20.6s (served 812) ← 重连风暴
575
+ // 18:56:24 retention(+60s) swept 3894ms, max 1238ms, telemetry=2964 行/2935ms = 0.99ms/行
576
+ // 18:56:26 stall 2.7s · 18:57:28 stall 3.4s · 18:57:31 stall 2.6s ← 风暴 2 分钟还没排空
577
+ // ```
578
+ // 稳态同一批删除是 0.24ms/行 ⇒ 60 秒时风暴仍在,max 单批被推到 1.2 秒(越过 1 秒慢 SQL 阈值)。
579
+ const firstSweep = async () => {
580
+ // 先等固定延迟越过重连风暴的**起点**,再等循环真的安静(见 waitForQuietLoop)。
581
+ if (firstDelayMs > 0) await Bun.sleep(firstDelayMs);
582
+ await waitForQuietLoop();
583
+ await sweep();
584
+ };
585
+ if (firstDelayMs > 0) setTimeout(() => { void firstSweep(); }, 0).unref?.();
586
+ else setImmediate(() => { void firstSweep(); });
522
587
  return setInterval(() => { void sweep(); }, intervalMs);
523
588
  }