@sleep2agi/commhub-server 0.9.0-preview.222 → 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",
|
|
@@ -1054,6 +1054,51 @@ describe("#473 anonymous /health — watchdog contract intact, no topology leak"
|
|
|
1054
1054
|
expect(Object.keys(authedProbe).length).toBeGreaterThan(Object.keys(anonProbe).length);
|
|
1055
1055
|
});
|
|
1056
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
|
+
|
|
1057
1102
|
test("no dead entries: every allow-listed key is actually served to anonymous callers", async () => {
|
|
1058
1103
|
const { PUBLIC_HEALTH_KEYS } = await import("./server.js");
|
|
1059
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');
|
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
|
}
|