@sleep2agi/commhub-server 0.9.0-preview.179 → 0.9.0-preview.180

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.179",
3
+ "version": "0.9.0-preview.180",
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,49 @@
1
+ // #2756 —— `/health` 必须能看见 Hub 自己所在磁盘的水位。
2
+ //
3
+ // 起因:2026-10-28 04:05 我「偶尔巡检」才发现根分区 95%(剩 28G)。
4
+ // 上一轮清理(85%)之后它只用了一天多就涨回 10 个点 —— 靠人记得看,必然会漏;
5
+ // 而 Hub 的 DB / 日志 / 运行时目录都写在这块盘上,写满即生产停摆。
6
+ //
7
+ // 这条测试钉三件事:
8
+ // 1. 形状稳定(path / total_gb / used_gb / avail_gb / used_pct 都有);
9
+ // 2. 数值自洽(used + avail ≈ total,used_pct 与两者一致);
10
+ // 3. 探针打在 DB 目录上,不是写死 `/`。
11
+ //
12
+ // Run: COMMHUB_DB=/tmp/2756-health-disk.db bun test src/health-host-disk-2756.test.ts
13
+
14
+ import { describe, expect, test } from "bun:test";
15
+ import { hostDiskState } from "./server.js";
16
+
17
+ describe("#2756 /health.host_disk", () => {
18
+ test("形状完整且数值自洽", () => {
19
+ const d = hostDiskState();
20
+ expect(d).not.toBeNull();
21
+ const v = d!;
22
+ expect(typeof v.path).toBe("string");
23
+ expect(v.path.startsWith("/")).toBe(true);
24
+ expect(v.total_gb).toBeGreaterThan(0);
25
+ expect(v.avail_gb).toBeGreaterThanOrEqual(0);
26
+ expect(v.used_gb).toBeGreaterThanOrEqual(0);
27
+ expect(v.used_gb + v.avail_gb).toBeCloseTo(v.total_gb, 0);
28
+ expect(v.used_pct).toBeGreaterThanOrEqual(0);
29
+ expect(v.used_pct).toBeLessThanOrEqual(100);
30
+ });
31
+
32
+ test("探针目录来自 COMMHUB_DB 所在目录(不是写死 /)", () => {
33
+ const d = hostDiskState();
34
+ const db = process.env.COMMHUB_DB || "";
35
+ if (db.includes("/")) {
36
+ const dir = db.replace(/[^/]*$/, "");
37
+ expect(d!.path).toBe(dir);
38
+ }
39
+ });
40
+
41
+ test("水位与 df 同量级(10% 以内偏差,防止把字节单位算错)", () => {
42
+ const d = hostDiskState()!;
43
+ // 用 statfs 自身再算一遍独立值作对照
44
+ const fs = require("node:fs");
45
+ const st = fs.statfsSync(d.path);
46
+ const pct = Math.round((1 - Number(st.bavail) / Number(st.blocks)) * 1000) / 10;
47
+ expect(Math.abs(d.used_pct - pct)).toBeLessThan(10);
48
+ });
49
+ });
package/src/server.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { statfsSync } from "node:fs";
2
+ import { homedir } from "node:os";
1
3
  import { hostSupervisorIdentityRows } from "./host-supervisor-identity.js";
2
4
  import { buildServeErrorResponse } from "./serve-error.js";
3
5
  import { sanitizeRuntimeReadiness } from "./runtime-readiness.js";
@@ -2818,6 +2820,13 @@ return Bun.serve({
2818
2820
  // #2721 —— 最近一次广播(时间/收件人数/批数/耗时)。广播是唯一能一次性唤醒
2819
2821
  // 几百个 agent 的动作;没有这一格,事后只能把停顿记录和聊天记录对时间。
2820
2822
  broadcast: broadcastRecentState(),
2823
+ // #2756 —— Hub 自己所在磁盘的水位。
2824
+ //
2825
+ // 为什么要进 /health:2026-10-28 04:05 我做巡检时才发现根分区已经 95%(剩 28G)——
2826
+ // 上一轮清理(85%)之后它只用了一天多就涨回去 10 个点,靠人「偶尔看一眼」
2827
+ // 必然会漏。Hub 的 DB、日志、运行时目录都写在这块盘上,写满即生产停摆,
2828
+ // 所以这个数字应该和 sessions/stalls 一样随手可见。
2829
+ host_disk: hostDiskState(),
2821
2830
  auth: DEV_OPEN ? "dev-open" : "user-token",
2822
2831
  security: DEV_OPEN ? "dev-open" : "secured",
2823
2832
  tmux: TMUX_ENABLED ? "enabled" : "disabled",
@@ -5808,3 +5817,31 @@ export function startHub(opts?: { port?: number; hostname?: string }): ReturnTyp
5808
5817
  startedHub = { server, startedAt: new Date().toISOString() };
5809
5818
  return server;
5810
5819
  }
5820
+ /**
5821
+ * #2756 —— Hub 所在文件系统的水位。
5822
+ *
5823
+ * 只报「一块盘」:数据库目录所在的那块。多挂载点的主机(比如把 DB 放到单独卷)
5824
+ * 也能对得上,因为我们 statfs 的是 DB 的实际路径,不是写死 `/`。
5825
+ *
5826
+ * 为什么不在这里做告警:告警需要阈值与通知渠道,属于运维策略;这里只提供**可观测**。
5827
+ * 客户端/巡检脚本按 `used_pct > 90` 自己决定要不要叫。
5828
+ */
5829
+ export function hostDiskState(): { path: string; total_gb: number; used_gb: number; avail_gb: number; used_pct: number } | null {
5830
+ try {
5831
+ // 探针打在**数据库实际所在目录**上,而不是写死 `/`:多挂载点主机也能对得上。
5832
+ const dbFile = process.env.COMMHUB_DB || `${process.env.HOME || homedir()}/.commhub/commhub.db`;
5833
+ const probe = dbFile.includes("/") ? dbFile.replace(/[^/]*$/, "") || "/" : "/";
5834
+ const st = statfsSync(probe);
5835
+ const total = Number(st.blocks) * Number(st.bsize);
5836
+ const avail = Number(st.bavail) * Number(st.bsize);
5837
+ if (!Number.isFinite(total) || total <= 0) return null;
5838
+ const used = total - avail;
5839
+ const gb = (n: number) => Math.round((n / 1024 ** 3) * 10) / 10;
5840
+ return { path: probe, total_gb: gb(total), used_gb: gb(used), avail_gb: gb(avail), used_pct: Math.round((used / total) * 1000) / 10 };
5841
+ } catch {
5842
+ return null;
5843
+ }
5844
+ }
5845
+
5846
+
5847
+