@sleep2agi/commhub-server 0.9.0-preview.227 → 0.9.0-preview.229

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.227",
3
+ "version": "0.9.0-preview.229",
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,36 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { clipSafe, hasLoneSurrogate } from "./clip.js";
3
+
4
+ // 2026-10-31 —— 外部复核:「slice 会切断代理对 ⇒ 孤立 surrogate ⇒ JSON 合法但 UTF-8 非法」。
5
+ // 这组用例的价值在于**边界那一个位置**:把 emoji 恰好摆在切点上。
6
+ describe("clipSafe: 不切断代理对", () => {
7
+ test("短于上限原样返回(含 emoji)", () => {
8
+ expect(clipSafe("ab😀", 10)).toBe("ab😀");
9
+ });
10
+
11
+ test("切点落在代理对中间 ⇒ 少取一个 code unit,不产生孤立代理", () => {
12
+ // "😀" 在 index 0-1;max=2 会切在中间
13
+ const cut = clipSafe("😀X", 2);
14
+ expect(hasLoneSurrogate(cut)).toBe(false);
15
+ expect(cut.length).toBeLessThanOrEqual(2);
16
+ });
17
+
18
+ test("长串截断后仍是合法 UTF-16 且带省略号", () => {
19
+ const s = "a".repeat(3) + "😀" + "b".repeat(10);
20
+ const cut = clipSafe(s, 6);
21
+ expect(hasLoneSurrogate(cut)).toBe(false);
22
+ expect(cut.endsWith("…")).toBe(true);
23
+ expect(cut.length).toBeLessThanOrEqual(6);
24
+ });
25
+
26
+ test("对照:裸 slice 会制造孤立代理(证明这组用例不是空转)", () => {
27
+ const raw = ("😀" + "x".repeat(10)).slice(0, 1); // 只切到高代理
28
+ expect(hasLoneSurrogate(raw)).toBe(true);
29
+ });
30
+
31
+ test("max<=0 与非法 max 返回空串(不抛)", () => {
32
+ expect(clipSafe("abc", 0)).toBe("");
33
+ expect(clipSafe("abc", -1)).toBe("");
34
+ expect(clipSafe("abc", 1.5)).toBe("");
35
+ });
36
+ });
package/src/clip.ts ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * 按 UTF-16 长度截断,但**不切断代理对**。
3
+ *
4
+ * 2026-10-31 —— 外部复核(原话):
5
+ * 「量纲问题的后果不是『长度差 4 倍』,而是 `slice` 会切断代理对
6
+ * ⇒ 切在代理对中间 ⇒ 孤立 surrogate ⇒ JSON 合法但 UTF-8 非法
7
+ * ⇒ 后果不是『截短了』,是**审计行本身可能不是合法 UTF-8**,日志管道在那一行炸。」
8
+ *
9
+ * `s.slice(0, n)` 数的是 UTF-16 code unit,而一个 emoji(或任何 BMP 外字符)占两个。
10
+ * 若第 n 个位置正好落在高代理与低代理之间,切出来的字符串含**孤立高代理**:
11
+ * · JSON.stringify 会把它写成 "\ud83d"(语法上合法)
12
+ * · 编码成 UTF-8 时被替换成 U+FFFD 或直接报错(取决于运行时)
13
+ * · 于是同一行日志在不同管道里长得不一样,而 join 用的正是这一串
14
+ *
15
+ * 本函数只在边界上处理:若切点落在代理对中间,就少取一个 code unit,保证输出始终是
16
+ * 「合法 UTF-16 子串」。**它不改变长度上限的语义**(仍然是 ≤ max),只保证不产生孤立代理。
17
+ */
18
+ export function clipSafe(value: string, max: number, ellipsis = "…"): string {
19
+ if (!Number.isInteger(max) || max <= 0) return "";
20
+ if (value.length <= max) return value;
21
+ // 留出省略号的位置(省略号本身是 BMP 字符,占 1 个 code unit)
22
+ let end = Math.max(0, max - (ellipsis ? 1 : 0));
23
+ if (end > 0) {
24
+ const last = value.charCodeAt(end - 1);
25
+ const isHighSurrogate = last >= 0xd800 && last <= 0xdbff;
26
+ if (isHighSurrogate) end -= 1; // 丢掉高代理,避免留下孤立的一半
27
+ }
28
+ return value.slice(0, end) + (ellipsis ? ellipsis : "");
29
+ }
30
+
31
+ /** 判断一个字符串里有没有孤立代理 —— 测试与排障用。 */
32
+ export function hasLoneSurrogate(s: string): boolean {
33
+ for (let i = 0; i < s.length; i++) {
34
+ const c = s.charCodeAt(i);
35
+ const isHigh = c >= 0xd800 && c <= 0xdbff;
36
+ const isLow = c >= 0xdc00 && c <= 0xdfff;
37
+ if (isHigh) {
38
+ const next = s.charCodeAt(i + 1);
39
+ if (!(next >= 0xdc00 && next <= 0xdfff)) return true;
40
+ i += 1;
41
+ continue;
42
+ }
43
+ if (isLow) return true; // 没有前置高代理的低代理
44
+ }
45
+ return false;
46
+ }
@@ -1135,6 +1135,19 @@ describe("#473 anonymous /health — watchdog contract intact, no topology leak"
1135
1135
  }
1136
1136
  });
1137
1137
 
1138
+ // 2026-10-31 —— **单一出口**(外部复核 (a):
1139
+ // 「一个必须在『所有字段写完之后』执行的变换,只有收敛到单一出口才可靠 —— 多出口 × 惰性 = 迟早漏」)。
1140
+ // 这条门只看一件事:`shape(...)` 在源码里只能出现在**一处**。
1141
+ // 出现第二处 ⇒ 说明又有一个 return 各自记得调它 ⇒ 下一个人加第三个出口时就会裸奔。
1142
+ test("the anonymous projection is applied at exactly one call site", async () => {
1143
+ const { readFileSync } = await import("node:fs");
1144
+ const src = readFileSync(new URL("./server.ts", import.meta.url), "utf8");
1145
+ const calls = [...src.matchAll(/Response\.json\(\s*shape\(/g)];
1146
+ expect(calls.length).toBe(1);
1147
+ // 定义处也算一处(`const shape = ...`),但它不是 Response.json 包裹,所以上面的正则不会数到
1148
+ expect((src.match(/const shape = /g) || []).length).toBe(1);
1149
+ });
1150
+
1138
1151
  test("no dead entries: every allow-listed key is actually served to anonymous callers", async () => {
1139
1152
  const { PUBLIC_HEALTH_KEYS } = await import("./server.js");
1140
1153
  const plain = await (await fetch(`${BASE}/health`)).json() as any;
@@ -1,6 +1,7 @@
1
1
  // 需求池。长期卡片,存在 Hub 上,手机和电脑读同一份。
2
2
  // 不是 tasks:tasks 是正在派给节点的活,状态由节点收尾。
3
3
  import { errorBody, errorFrom, RequirementFieldError, type ErrorContext } from "./requirements-errors.js";
4
+ import { clipSafe } from "./clip.js";
4
5
  import { createHash } from "node:crypto";
5
6
  import { parsePeopleQuery, peopleDirectory, resolvePersonName } from "./requirements-people.js";
6
7
  import { encodeCursor, matchesTaskId, matchesTerms, parseListQuery, type ListQuery, type NameMaps } from "./requirements-search.js";
@@ -256,7 +257,8 @@ const REPO = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
256
257
  const ISSUE_URL = /^https:\/\/github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\/issues\/(\d+)\/?$/;
257
258
 
258
259
  function clipTitle(value: unknown): string {
259
- return typeof value === "string" ? value.replace(/[\r\n\t]/g, " ").trim().slice(0, 120) : "";
260
+ // 120 是**渲染上限**;clipSafe 保证截断不产生孤立代理(否则该标题在 JSON 里「合法」、在 UTF-8 里非法)
261
+ return typeof value === "string" ? clipSafe(value.replace(/[\r\n\t]/g, " ").trim(), 120) : "";
260
262
  }
261
263
 
262
264
  function issueFromParts(repo: string, number: number, title: string) {
@@ -16,6 +16,7 @@
16
16
  // characters so a client cannot forge extra log lines.
17
17
 
18
18
  import { recordTaskRejection } from "./rejection-counters.js";
19
+ import { clipSafe } from "./clip.js";
19
20
 
20
21
  function ts(): string {
21
22
  return new Date().toTimeString().slice(0, 8);
@@ -25,7 +26,9 @@ function clip(value: unknown, max = 64): string {
25
26
  if (typeof value !== "string" || !value) return "-";
26
27
  const clean = value.replace(/[\u0000-\u001f\u007f\s]+/g, " ").trim();
27
28
  if (!clean) return "-";
28
- return clean.length > max ? `${clean.slice(0, max)}…` : clean;
29
+ // 2026-10-31 —— 用 clipSafe 而不是 slice:切点落在代理对中间会产出孤立 surrogate,
30
+ // 那一行日志本身就不是合法 UTF-8(外部复核:后果不是「截短了」,是日志管道在那一行炸)。
31
+ return clipSafe(clean, max + 1);
29
32
  }
30
33
 
31
34
  export interface TaskRejectionContext {