@sleep2agi/commhub-server 0.9.0-preview.221 → 0.9.0-preview.223
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.
|
|
3
|
+
"version": "0.9.0-preview.223",
|
|
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",
|
|
@@ -1030,6 +1030,75 @@ describe("#473 anonymous /health — watchdog contract intact, no topology leak"
|
|
|
1030
1030
|
// 它却一直躺在那里,看起来「这个字段是公开的」⇒ 下一个人把同名字段加回来时**默认就是公开的**。
|
|
1031
1031
|
// ⇒ 白名单会从「审批记录」退化成「历史残留」。
|
|
1032
1032
|
// 这与今晚那条镜像规则同构:否定断言必须配肯定断言,否则前者可被「永远不给」满足。
|
|
1033
|
+
// 2026-10-31 —— **2×2 的第四格**(外部复核 (e):「等价性断言必须按 (通道 × 配置) 组合进行」)。
|
|
1034
|
+
// 本仓的 /health 有两个维度:谁在读(匿名 / 鉴权)× 有没有带 X-Probe-Nonce。
|
|
1035
|
+
// 前面只覆盖了三格(匿名、匿名+探针、鉴权),**鉴权+探针**这一格没测过 ——
|
|
1036
|
+
// 而它正是「探针回显会不会被鉴权分支吃掉」的唯一读数。
|
|
1037
|
+
test("all four cells of (audience × probe-header) carry the fields they must", async () => {
|
|
1038
|
+
const anon = await (await fetch(`${BASE}/health`)).json() as any;
|
|
1039
|
+
const anonProbe = await (await fetch(`${BASE}/health`, { headers: { "X-Probe-Nonce": "cell-anon" } })).json() as any;
|
|
1040
|
+
const authed = await (await fetch(`${BASE}/health`, { headers: { Authorization: `Bearer ${adminToken}` } })).json() as any;
|
|
1041
|
+
const authedProbe = await (await fetch(`${BASE}/health`, {
|
|
1042
|
+
headers: { Authorization: `Bearer ${adminToken}`, "X-Probe-Nonce": "cell-authed" },
|
|
1043
|
+
})).json() as any;
|
|
1044
|
+
|
|
1045
|
+
// 探针格:带头的两格都必须回显 nonce,且 served_at 都在(窗口两端同域)
|
|
1046
|
+
expect(anonProbe.probe_nonce).toBe("cell-anon");
|
|
1047
|
+
expect(authedProbe.probe_nonce).toBe("cell-authed");
|
|
1048
|
+
for (const body of [anon, anonProbe, authed, authedProbe]) {
|
|
1049
|
+
expect(typeof body.served_at).toBe("string");
|
|
1050
|
+
}
|
|
1051
|
+
// 不带头的一格不该出现 probe_nonce(否则「这一次的凭据」变成常驻字段)
|
|
1052
|
+
expect("probe_nonce" in anon).toBe(false);
|
|
1053
|
+
// 鉴权格必须比匿名格多(否则白名单形同虚设)——第四格同样成立
|
|
1054
|
+
expect(Object.keys(authedProbe).length).toBeGreaterThan(Object.keys(anonProbe).length);
|
|
1055
|
+
});
|
|
1056
|
+
|
|
1057
|
+
// 2026-10-31 —— **未被读的字段 = 未验证的字段**(外部复核 ①:
|
|
1058
|
+
// 「not_applicable 占用了 hash 槽却活到今天,是因为那个字段从未被读取;
|
|
1059
|
+
// 字段『看起来有值』比『未被测的代码』更隐蔽」)。
|
|
1060
|
+
// 断言:HEALTH_FIELD_META 里每个键都必须至少被一条 server 测试提到;例外显式登记。
|
|
1061
|
+
// 上面那条 ratchet 抓出来的两个「被产出但没人读」字段,这里补上断言(而不是登记成例外)。
|
|
1062
|
+
test("alert_baselines and clk_tck_assumed_reason are actually read (not just produced)", async () => {
|
|
1063
|
+
const body = await (await fetch(`${BASE}/health`, { headers: { Authorization: `Bearer ${adminToken}` } })).json() as any;
|
|
1064
|
+
// alert_baselines:null(还没刷过)或 {at, rows, aliases} —— 两种形态都必须能说清
|
|
1065
|
+
expect(body).toHaveProperty("alert_baselines");
|
|
1066
|
+
if (body.alert_baselines !== null) {
|
|
1067
|
+
expect(typeof body.alert_baselines.at).toBe("string");
|
|
1068
|
+
expect(Number.isInteger(body.alert_baselines.rows)).toBe(true);
|
|
1069
|
+
expect(Number.isInteger(body.alert_baselines.aliases)).toBe(true);
|
|
1070
|
+
}
|
|
1071
|
+
// clk_tck_assumed_reason:一个必须能解释「为什么用假设值」的字符串(空串是合法的:读到了真实值)
|
|
1072
|
+
expect(typeof body.clk_tck_assumed_reason).toBe("string");
|
|
1073
|
+
// 它与 clk_tck_source 配对:source=assumed 时必须有理由,否则必须是空串
|
|
1074
|
+
if (body.clk_tck_source === "assumed") expect(body.clk_tck_assumed_reason.length).toBeGreaterThan(0);
|
|
1075
|
+
else expect(body.clk_tck_assumed_reason).toBe("");
|
|
1076
|
+
});
|
|
1077
|
+
|
|
1078
|
+
test("every /health field is read by at least one test (unread = unverified)", async () => {
|
|
1079
|
+
const { readFileSync, readdirSync, statSync } = await import("node:fs");
|
|
1080
|
+
const { join } = await import("node:path");
|
|
1081
|
+
const src = readFileSync(new URL("./server.ts", import.meta.url), "utf8");
|
|
1082
|
+
const start = src.indexOf("HEALTH_FIELD_META");
|
|
1083
|
+
const block = src.slice(start, src.indexOf("};", start));
|
|
1084
|
+
const keys = [...block.matchAll(/^ {2}([A-Za-z_][A-Za-z0-9_]*):\s*\{/gm)].map((m) => m[1]);
|
|
1085
|
+
expect(keys.length).toBeGreaterThan(30);
|
|
1086
|
+
const walk = (dir: string, out: string[] = []): string[] => {
|
|
1087
|
+
for (const name of readdirSync(dir)) {
|
|
1088
|
+
const full = join(dir, name);
|
|
1089
|
+
if (statSync(full).isDirectory()) walk(full, out);
|
|
1090
|
+
else if (name.endsWith(".test.ts")) out.push(full);
|
|
1091
|
+
}
|
|
1092
|
+
return out;
|
|
1093
|
+
};
|
|
1094
|
+
const testSource = walk(join(import.meta.dir)).map((f) => readFileSync(f, "utf8")).join("\n");
|
|
1095
|
+
const ALLOW_UNREAD: Record<string, string> = {};
|
|
1096
|
+
const unread = keys.filter((k) => !ALLOW_UNREAD[k] && !new RegExp(`\\b${k}\\b`).test(testSource));
|
|
1097
|
+
if (unread.length) {
|
|
1098
|
+
throw new Error(`这 ${unread.length} 个字段被产出但没有任何测试读它们:${unread.join(", ")}\n ⇒ 要么加断言,要么登记进 ALLOW_UNREAD 并写明为什么可以不读。`);
|
|
1099
|
+
}
|
|
1100
|
+
});
|
|
1101
|
+
|
|
1033
1102
|
test("no dead entries: every allow-listed key is actually served to anonymous callers", async () => {
|
|
1034
1103
|
const { PUBLIC_HEALTH_KEYS } = await import("./server.js");
|
|
1035
1104
|
const plain = await (await fetch(`${BASE}/health`)).json() as any;
|
|
@@ -263,4 +263,21 @@ describe("GET /api/providers", () => {
|
|
|
263
263
|
db.run(`DELETE FROM hub_provider_configs WHERE network_id = ?1 AND provider_id = 'hc-first'`, [ownerNet]);
|
|
264
264
|
});
|
|
265
265
|
|
|
266
|
+
|
|
267
|
+
// 2026-10-31 —— **写侧状态码是穷尽表**(外部复核:三元式默认分支 = 未映射与真实含义同值)。
|
|
268
|
+
// 两条断言分开:① 映射键 == 码清单;② 码清单里的每个码都真的在源码里被产出(跨源)。
|
|
269
|
+
test("provider write: every error code has a status, and every status code is really produced", async () => {
|
|
270
|
+
const { PROVIDER_WRITE_ERROR_CODES, PROVIDER_WRITE_STATUS } = await import("./hub-provider-config.js");
|
|
271
|
+
const { readFileSync } = await import("node:fs");
|
|
272
|
+
expect(Object.keys(PROVIDER_WRITE_STATUS).sort()).toEqual([...PROVIDER_WRITE_ERROR_CODES].sort());
|
|
273
|
+
for (const code of PROVIDER_WRITE_ERROR_CODES) {
|
|
274
|
+
expect(Number.isInteger(PROVIDER_WRITE_STATUS[code])).toBe(true);
|
|
275
|
+
expect(PROVIDER_WRITE_STATUS[code]).toBeGreaterThanOrEqual(400);
|
|
276
|
+
}
|
|
277
|
+
// 跨源:这些码必须能在源码里找到(`denied('x')` 或 vault 的 code 字面量),
|
|
278
|
+
// 否则清单会退化成「我们以为会发生的错误」。
|
|
279
|
+
const src = readFileSync(new URL("./hub-provider-config.ts", import.meta.url), "utf8");
|
|
280
|
+
const missing = [...PROVIDER_WRITE_ERROR_CODES].filter((c) => !src.includes(`'${c}'`) && !src.includes(`"${c}"`));
|
|
281
|
+
if (missing.length) throw new Error(`码清单里这些码在源码中找不到产出点:${missing.join(", ")}`);
|
|
282
|
+
});
|
|
266
283
|
});
|
|
@@ -7,6 +7,52 @@ import { requestProviderWrite } from './daemon-provider-snapshots';
|
|
|
7
7
|
type Human = { userId?: string | null; isNode: boolean; networkId: string | null };
|
|
8
8
|
type Config = { id: string; label: string; enabled: boolean; protocol: 'openai-responses' | 'openai-chat-completions' | 'anthropic-messages'; baseUrl: string; models: string[] };
|
|
9
9
|
const denied = (error: string) => ({ ok: false as const, error });
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 2026-10-31 —— **写侧错误的 HTTP 状态必须是穷尽表**(外部复核:三元式默认分支
|
|
13
|
+
* = 未映射与某个真实含义同值 ⇒ 口令字段;应改成 `Record<Code, number>`)。
|
|
14
|
+
*
|
|
15
|
+
* 原来 REST 路由里写的是:
|
|
16
|
+
* provider_owner_required ? 403 : revision_conflict ? 409 : access_denied ? 403 : 400
|
|
17
|
+
* ⇒ 新增一个错误码时它会**默认落到 400**,而 400 对某些码是错的(例如 vault 缺失应是 409/503)。
|
|
18
|
+
*
|
|
19
|
+
* 这里的码集合来自本文件里所有 `denied('...')` 与 VaultError 的 code;
|
|
20
|
+
* 测试会断言「映射键 == 码清单」,并且**码清单必须能在源码里逐个找到**(跨源,不是自说自话)。
|
|
21
|
+
*/
|
|
22
|
+
export const PROVIDER_WRITE_ERROR_CODES = [
|
|
23
|
+
"user_token_required",
|
|
24
|
+
"provider_owner_required",
|
|
25
|
+
"provider_access_denied",
|
|
26
|
+
"invalid_revision",
|
|
27
|
+
"invalid_hub_provider",
|
|
28
|
+
"invalid_provider_key",
|
|
29
|
+
"invalid_provider_secret_reference",
|
|
30
|
+
"provider_secret_reference_missing",
|
|
31
|
+
"provider_key_required",
|
|
32
|
+
"hub_provider_revision_conflict",
|
|
33
|
+
"hub_provider_disabled",
|
|
34
|
+
"hub_provider_save_failed",
|
|
35
|
+
"vault_unavailable",
|
|
36
|
+
] as const;
|
|
37
|
+
export type ProviderWriteErrorCode = (typeof PROVIDER_WRITE_ERROR_CODES)[number];
|
|
38
|
+
export const PROVIDER_WRITE_STATUS: Record<ProviderWriteErrorCode, number> = {
|
|
39
|
+
user_token_required: 401,
|
|
40
|
+
provider_owner_required: 403,
|
|
41
|
+
provider_access_denied: 403,
|
|
42
|
+
invalid_revision: 400,
|
|
43
|
+
invalid_hub_provider: 400,
|
|
44
|
+
invalid_provider_key: 400,
|
|
45
|
+
invalid_provider_secret_reference: 400,
|
|
46
|
+
provider_secret_reference_missing: 400,
|
|
47
|
+
provider_key_required: 400,
|
|
48
|
+
hub_provider_revision_conflict: 409,
|
|
49
|
+
hub_provider_disabled: 409,
|
|
50
|
+
hub_provider_save_failed: 500,
|
|
51
|
+
vault_unavailable: 503,
|
|
52
|
+
};
|
|
53
|
+
export function providerWriteStatus(code: string): number {
|
|
54
|
+
return (PROVIDER_WRITE_STATUS as Record<string, number>)[code] ?? 400;
|
|
55
|
+
}
|
|
10
56
|
const idValid = (id: unknown) => typeof id === 'string' && /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(id);
|
|
11
57
|
function auth(h: Human, write: boolean, owner = false) {
|
|
12
58
|
if (h.isNode || !h.userId || !h.networkId) return denied('user_token_required');
|
|
@@ -109,6 +109,24 @@ afterAll(() => {
|
|
|
109
109
|
try { rmSync(DIR, { recursive: true, force: true }); } catch {}
|
|
110
110
|
});
|
|
111
111
|
|
|
112
|
+
// 2026-10-31 —— **穷尽映射的运行时钉**(外部复核 (c):「应改成 Record<ErrorCode, number> 由 tsc 报红」)。
|
|
113
|
+
// 本仓 server 没有 tsc 门(只有 agent-network 在 e2e-docker 里 typecheck),
|
|
114
|
+
// 所以那条约束只有写成运行时断言才真的存在:
|
|
115
|
+
// 码集合 = TAG_CREATE_ERRORS(唯一来源)|映射键必须与它逐字相等|每个值必须是合法 HTTP 状态
|
|
116
|
+
test("tag create: every error code has a status, and nothing else does", async () => {
|
|
117
|
+
const { TAG_CREATE_ERRORS, TAG_CREATE_STATUS } = await import("./requirements.js");
|
|
118
|
+
expect(Object.keys(TAG_CREATE_STATUS).sort()).toEqual([...TAG_CREATE_ERRORS].sort());
|
|
119
|
+
for (const code of TAG_CREATE_ERRORS) {
|
|
120
|
+
const status = TAG_CREATE_STATUS[code];
|
|
121
|
+
expect(Number.isInteger(status)).toBe(true);
|
|
122
|
+
expect(status).toBeGreaterThanOrEqual(400);
|
|
123
|
+
expect(status).toBeLessThan(500);
|
|
124
|
+
}
|
|
125
|
+
// 反例守卫:默认分支的「未映射 ⇒ 400」不许再出现(那正是被替换掉的那行)
|
|
126
|
+
const src = await (await import("node:fs")).readFileSync(new URL("./requirements.ts", import.meta.url), "utf8");
|
|
127
|
+
expect(src).not.toMatch(/result\.error === "tag_exists" \? 409/);
|
|
128
|
+
});
|
|
129
|
+
|
|
112
130
|
describe("GET /api/requirements/tags", () => {
|
|
113
131
|
test("owner:tags 仍是排序后的字符串数组(旧 App 形状不变),多出 counts / colors / can_manage", async () => {
|
|
114
132
|
const r = await tagsOf(admin.token);
|
package/src/requirements.ts
CHANGED
|
@@ -692,8 +692,13 @@ function networkTagNames(networkId: string): Set<string> {
|
|
|
692
692
|
* 新增一个错误码时它**默认落到 400**,而有些码 400 是错的(冲突应是 409),且没人会提醒你。
|
|
693
693
|
* 改成穷尽表之后,漏一个码 = tsc 红。
|
|
694
694
|
*/
|
|
695
|
-
|
|
696
|
-
|
|
695
|
+
// 2026-10-31 —— **本仓 server 没有 tsc 门**(只有 agent-network 在 e2e-docker 里 typecheck),
|
|
696
|
+
// 所以「漏一个码 = tsc 红」在这里是空头支票。⇒ 用运行时钉住:
|
|
697
|
+
// 码集合由这个数组当**唯一来源**,测试断言映射表的键与它逐字相等。
|
|
698
|
+
// 这样新增一个码只改一处(数组),而「忘了加 status」会红在测试里,不依赖编辑器。
|
|
699
|
+
export const TAG_CREATE_ERRORS = ["tag_exists", "tag_parent_not_found", "tag_depth_exceeded"] as const;
|
|
700
|
+
export type TagCreateError = (typeof TAG_CREATE_ERRORS)[number];
|
|
701
|
+
export const TAG_CREATE_STATUS: Record<TagCreateError, number> = {
|
|
697
702
|
tag_exists: 409,
|
|
698
703
|
tag_parent_not_found: 404,
|
|
699
704
|
tag_depth_exceeded: 400,
|
package/src/server.ts
CHANGED
|
@@ -106,7 +106,7 @@ import { listTaskLateReplies } from "./task-late-replies.js";
|
|
|
106
106
|
import { assertScheduledTaskBackendSupported, handleScheduledTaskRequest, startScheduledTaskScheduler } from "./scheduled-tasks.js";
|
|
107
107
|
import { startDueReminderTimer } from "./requirement-due-reminders.js";
|
|
108
108
|
import { handleRequirementsRequest } from "./requirements.js";
|
|
109
|
-
import { readHubProviders, putHubProvider } from "./hub-provider-config.js";
|
|
109
|
+
import { readHubProviders, putHubProvider, providerWriteStatus } from "./hub-provider-config.js";
|
|
110
110
|
import { requestNodeSecrets, getScopedSecrets } from "./scoped-secrets.js";
|
|
111
111
|
import { readEnvTransportLog } from "./env-transport-gate.js";
|
|
112
112
|
import { enforceImpactOf, normalizePeerAllowlist, normalizeToolAllowlist, restAllowlistVerdict, restRouteLabel } from "./node-permissions.js";
|
|
@@ -4254,10 +4254,8 @@ return Bun.serve({
|
|
|
4254
4254
|
}
|
|
4255
4255
|
const saved = putHubProvider({ userId: restAuth.userId ?? null, isNode: false, networkId }, revision, body?.provider);
|
|
4256
4256
|
if (!saved.ok) {
|
|
4257
|
-
|
|
4258
|
-
|
|
4259
|
-
: saved.error === "provider_access_denied" ? 403 : 400;
|
|
4260
|
-
return withCors(req, Response.json(saved, { status }));
|
|
4257
|
+
// 状态码来自穷尽表(hub-provider-config.ts: PROVIDER_WRITE_STATUS),不再在这里写三元式
|
|
4258
|
+
return withCors(req, Response.json(saved, { status: providerWriteStatus(saved.error) }));
|
|
4261
4259
|
}
|
|
4262
4260
|
return withCors(req, Response.json({ ...saved, network_id: networkId }));
|
|
4263
4261
|
}
|