@sleep2agi/commhub-server 0.9.0-preview.204 → 0.9.0-preview.206
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 +1 -1
- package/src/node-permissions-http.test.ts +57 -0
- package/src/server.ts +38 -2
- package/src/tools.ts +9 -2
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.206",
|
|
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",
|
|
@@ -251,6 +251,27 @@ describe("GET /api/networks/:id/node-permission-report", () => {
|
|
|
251
251
|
expect((await call(node.nN.token, "GET", `/api/networks/${net}/node-permission-report`)).status).toBe(403);
|
|
252
252
|
expect((await call(user.boss.token, "GET", `/api/networks/${net}/node-permission-report?since=${encodeURIComponent(new Date(Date.now() + 3_600_000).toISOString())}`)).body.total).toBe(0);
|
|
253
253
|
});
|
|
254
|
+
|
|
255
|
+
// #2969 —— 设白名单之前,管理员要能看「这台节点实际用过哪些工具」,
|
|
256
|
+
// 否则白名单只能凭想象写,第一次上线就拦掉在用的东西。
|
|
257
|
+
//
|
|
258
|
+
// 关键的第二条:一台**从不越界**但天天在用的节点,过去在报表里根本不存在
|
|
259
|
+
// (nodes 只从「被拒过的行」聚合)。它恰恰是最该出现在名单里的那台。
|
|
260
|
+
test("按节点的工具调用明细:从不越界的节点也要出现", async () => {
|
|
261
|
+
await mcp(node.nN.token, "list_tasks", { network_id: net, limit: 1 });
|
|
262
|
+
await mcp(node.nN.token, "list_tasks", { network_id: net, limit: 1 });
|
|
263
|
+
const r = await call(user.boss.token, "GET", `/api/networks/${net}/node-permission-report`);
|
|
264
|
+
expect(r.status).toBe(200);
|
|
265
|
+
const nN = r.body.nodes.find((n: any) => n.node_id === node.nN.id);
|
|
266
|
+
expect(nN).toBeTruthy();
|
|
267
|
+
expect(nN.tool_calls_total).toBeGreaterThanOrEqual(2);
|
|
268
|
+
const listTasks = nN.tool_calls.find((t: any) => t.tool === "mcp:list_tasks");
|
|
269
|
+
expect(listTasks.hits).toBeGreaterThanOrEqual(2);
|
|
270
|
+
// 排序也变了:按调用量降序,让「用得最多」的排在最前
|
|
271
|
+
expect(r.body.nodes[0].tool_calls_total).toBeGreaterThanOrEqual(nN.tool_calls_total);
|
|
272
|
+
// 网络级合计保持不变(旧口径不能因为这次改动而变)
|
|
273
|
+
expect(r.body.tool_calls.total).toBeGreaterThanOrEqual(nN.tool_calls_total);
|
|
274
|
+
});
|
|
254
275
|
});
|
|
255
276
|
|
|
256
277
|
// #2823 —— 调用面计数:拒绝日志给不了分母。
|
|
@@ -292,6 +313,23 @@ describe("#2823 调用面计数(node_tool_calls)", () => {
|
|
|
292
313
|
expect(denyRows(node.nN.id)).toBe(before.denials);
|
|
293
314
|
});
|
|
294
315
|
|
|
316
|
+
// #2971 —— 读类 / 需求类 / 排程类原来根本不到闸里,账本因此完全看不到它们。
|
|
317
|
+
// 而敏感读恰恰在这一类里(list_network_secrets / list_providers / tail_node_logs)。
|
|
318
|
+
// 修法只有一个方向:**记账,但不判定** —— 拦截语义必须零变化。
|
|
319
|
+
test("#2971 读类工具会被计数,且行为不变(不被拦、不进拒绝日志)", async () => {
|
|
320
|
+
const before = { calls: callHits(node.nN.id, "mcp:list_providers"), denials: denyRows(node.nN.id) };
|
|
321
|
+
const r = await mcp(node.nN.token, "list_providers", { network_id: net });
|
|
322
|
+
expect(r.error).not.toBe("node_permission_denied");
|
|
323
|
+
expect(callHits(node.nN.id, "mcp:list_providers")).toBe(before.calls + 1);
|
|
324
|
+
expect(denyRows(node.nN.id)).toBe(before.denials);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("#2971 always 类不进账本(心跳会把账本淹掉)", async () => {
|
|
328
|
+
const before = toolCallTotal(node.nN.id);
|
|
329
|
+
await mcp(node.nN.token, "get_inbox", { alias: node.nN.alias });
|
|
330
|
+
expect(toolCallTotal(node.nN.id)).toBe(before);
|
|
331
|
+
});
|
|
332
|
+
|
|
295
333
|
test("本来会被拒的调用两个表都记(分子与分母同源)", async () => {
|
|
296
334
|
const beforeCalls = callHits(node.nN.id, "mcp:projects_create");
|
|
297
335
|
const beforeDeny = hitsOf(node.nN.id, "mcp:projects_create", "human_only");
|
|
@@ -447,3 +485,22 @@ describe("空数组不猜:拒收,清空请传 null", () => {
|
|
|
447
485
|
expect(row.body.nodes[0].tool_allowlist).toBeNull();
|
|
448
486
|
});
|
|
449
487
|
});
|
|
488
|
+
|
|
489
|
+
// #2969 / #2971 —— 未绑定节点的令牌是权限体系的一个真实盲点:
|
|
490
|
+
// 账本用 `token:<token_id>` 当键,它们没有 nodes 行 ⇒ 两张白名单都管不到(toolAllowlist 为 null)。
|
|
491
|
+
// 报表必须把它们标出来,否则管理员会以为「名单之外没有别人在派活」。
|
|
492
|
+
describe("未绑定节点的令牌在报表里必须可见", () => {
|
|
493
|
+
test("token: 键的条目带 unbound_token=true,真实节点不带", async () => {
|
|
494
|
+
db.run(`INSERT INTO node_tool_calls (network_id, node_id, tool, hour, hits) VALUES (?1, ?2, 'mcp:send_task', ?3, 4)
|
|
495
|
+
ON CONFLICT(network_id, node_id, tool, hour) DO UPDATE SET hits = node_tool_calls.hits + 4`,
|
|
496
|
+
[net, "token:tok_unbound_test", new Date().toISOString().slice(0, 13)]);
|
|
497
|
+
const r = await call(user.boss.token, "GET", `/api/networks/${net}/node-permission-report`);
|
|
498
|
+
expect(r.status).toBe(200);
|
|
499
|
+
const unbound = r.body.nodes.find((n: any) => n.node_id === "token:tok_unbound_test");
|
|
500
|
+
expect(unbound).toBeTruthy();
|
|
501
|
+
expect(unbound.unbound_token).toBe(true);
|
|
502
|
+
expect(unbound.tool_calls_total).toBeGreaterThanOrEqual(4);
|
|
503
|
+
const real = r.body.nodes.find((n: any) => n.node_id === node.nN.id);
|
|
504
|
+
expect(real.unbound_token).toBeUndefined();
|
|
505
|
+
});
|
|
506
|
+
});
|
package/src/server.ts
CHANGED
|
@@ -3044,7 +3044,7 @@ return Bun.serve({
|
|
|
3044
3044
|
FROM node_permission_log WHERE network_id = ?1 AND hour >= ?2
|
|
3045
3045
|
GROUP BY node_id, route, reason ORDER BY node_id, route, reason`,
|
|
3046
3046
|
networkId, sinceHour);
|
|
3047
|
-
const byNode = new Map<string, { node_id: string; alias: string | null; permission_mode: string; total: number; by_reason: Record<string, number>; routes: Array<{ route: string; reason: string; hits: number; sample: string | null; last_hour: string }
|
|
3047
|
+
const byNode = new Map<string, { node_id: string; alias: string | null; permission_mode: string; total: number; by_reason: Record<string, number>; routes: Array<{ route: string; reason: string; hits: number; sample: string | null; last_hour: string }>; tool_calls?: Array<{ tool: string; hits: number }>; tool_calls_total?: number; unbound_token?: boolean }>();
|
|
3048
3048
|
for (const r of rows) {
|
|
3049
3049
|
let entry = byNode.get(r.node_id);
|
|
3050
3050
|
if (!entry) {
|
|
@@ -3057,7 +3057,43 @@ return Bun.serve({
|
|
|
3057
3057
|
entry.by_reason[r.reason] = (entry.by_reason[r.reason] ?? 0) + hits;
|
|
3058
3058
|
entry.routes.push({ route: r.route, reason: r.reason, hits, sample: r.sample, last_hour: r.last_hour });
|
|
3059
3059
|
}
|
|
3060
|
-
|
|
3060
|
+
// #2969 —— 按节点的工具调用明细。
|
|
3061
|
+
// 为什么要这个:管理员在设「只能用哪些工具」之前,需要先看**这台节点实际用过哪些**,
|
|
3062
|
+
// 否则白名单只能凭想象写,第一次上线就是拦掉在用的工具。
|
|
3063
|
+
// 之前账本只有网络级合计(by_tool),且 nodes 只从「被拒过的行」聚合 ——
|
|
3064
|
+
// 一台从不越界但天天在用的节点,在报表里根本不存在。
|
|
3065
|
+
//
|
|
3066
|
+
// #2971 之后账本包含读类工具,这里的明细才第一次是「全部调用面」而不是「会被拦的那部分」。
|
|
3067
|
+
const perNodeCalls = db.all<{ node_id: string; tool: string; hits: number | string }>(
|
|
3068
|
+
`SELECT node_id, tool, SUM(hits) AS hits FROM node_tool_calls
|
|
3069
|
+
WHERE network_id = ?1 AND hour >= ?2
|
|
3070
|
+
GROUP BY node_id, tool ORDER BY node_id, SUM(hits) DESC`,
|
|
3071
|
+
networkId, sinceHour,
|
|
3072
|
+
);
|
|
3073
|
+
const callsByNode = new Map<string, Array<{ tool: string; hits: number }>>();
|
|
3074
|
+
for (const r of perNodeCalls) {
|
|
3075
|
+
const list = callsByNode.get(r.node_id) ?? [];
|
|
3076
|
+
list.push({ tool: r.tool, hits: Number(r.hits) });
|
|
3077
|
+
callsByNode.set(r.node_id, list);
|
|
3078
|
+
}
|
|
3079
|
+
// 有调用但没被拒过的节点也要出现在结果里(total = 0)。
|
|
3080
|
+
for (const nodeId of callsByNode.keys()) {
|
|
3081
|
+
if (byNode.has(nodeId)) continue;
|
|
3082
|
+
const n = db.get<{ alias: string | null; permission_mode: string | null }>("SELECT alias, permission_mode FROM nodes WHERE node_id = ?1", nodeId);
|
|
3083
|
+
byNode.set(nodeId, { node_id: nodeId, alias: n?.alias ?? null, permission_mode: n?.permission_mode || "normal", total: 0, by_reason: {}, routes: [] });
|
|
3084
|
+
}
|
|
3085
|
+
for (const entry of byNode.values()) {
|
|
3086
|
+
const list = callsByNode.get(entry.node_id) ?? [];
|
|
3087
|
+
entry.tool_calls = list;
|
|
3088
|
+
entry.tool_calls_total = list.reduce((n, e) => n + e.hits, 0);
|
|
3089
|
+
// 未绑定节点的令牌:账本用 `token:<token_id>` 当键(nodeIdentity 的老路径)。
|
|
3090
|
+
// 它们**没有 nodes 行**,所以两张白名单都管不到它们(toolAllowlist 为 null ⇒ 不限制)。
|
|
3091
|
+
// 报表里必须显式标出来,否则管理员会以为「名单之外没有别人在派活」。
|
|
3092
|
+
// 这里只标注,不改变任何判定。
|
|
3093
|
+
if (entry.node_id.startsWith("token:")) entry.unbound_token = true;
|
|
3094
|
+
}
|
|
3095
|
+
const nodes = [...byNode.values()].sort((a, b) =>
|
|
3096
|
+
b.tool_calls_total - a.tool_calls_total || b.total - a.total || a.node_id.localeCompare(b.node_id));
|
|
3061
3097
|
// #2823 —— 调用面分母。`total` 只数「本来会被拒」的次数;没有分母时
|
|
3062
3098
|
// 「0 命中」既可能是没人越界,也可能是没人碰过这些工具,两者结论相反。
|
|
3063
3099
|
// 这里单独给一份 classified 工具的真实调用数(按小时合并后的 hits 之和),
|
package/src/tools.ts
CHANGED
|
@@ -35,7 +35,7 @@ import { addAgentNetworkScope, addAgentTimelineScope, addNetworkScope, canRestWr
|
|
|
35
35
|
import { canMessageAgent, restrictedNetworkIds, RESTRICTED_MEMBER_TOOLS, type AgentRef } from "./agent-access.js";
|
|
36
36
|
import { listedToolFilter, scopeToolsList, type ToolCaller } from "./tool-audience.js";
|
|
37
37
|
import { agentBatchScheduleInterval, agentCancelSchedule, agentCreateSchedule, agentGetSchedule, agentListSchedules, agentRunScheduleNow, agentScheduleRuns, agentUpdateSchedule } from "./schedule-agent.js";
|
|
38
|
-
import { broadcastVerdict, dispatchVerdict, humanOnlyVerdict, NODE_TOOL_CLASS, nodeDecide, nodeIdentity, nodePermissionDeniedBody, writeVerdict, type NodeIdentity, type Verdict, toolAllowlistVerdict } from "./node-permissions.js";
|
|
38
|
+
import { broadcastVerdict, dispatchVerdict, humanOnlyVerdict, NODE_TOOL_CLASS, nodeDecide, nodeIdentity, nodePermissionDeniedBody, recordNodeToolCall, writeVerdict, type NodeIdentity, type Verdict, toolAllowlistVerdict } from "./node-permissions.js";
|
|
39
39
|
import { countInFlightTasks } from "./node-in-flight-tasks.js";
|
|
40
40
|
import { restrictedMemberAttachmentsDenied } from "./restricted-files.js";
|
|
41
41
|
import { errorBody } from "./requirements-errors.js";
|
|
@@ -296,8 +296,15 @@ function guardNodePermissionTools(server: McpServer, tokenId: string, networkId:
|
|
|
296
296
|
if (allowVerdict && nodeDecide(id, `mcp:${name}`, allowVerdict)) {
|
|
297
297
|
return denied(allowVerdict.reason, `mcp:${name}`);
|
|
298
298
|
}
|
|
299
|
+
// #2971 —— 账本要覆盖「到达工具的每一次调用」,不只是会被拦的那几类。
|
|
300
|
+
// 读类 / 需求类 / 排程类原来根本不到这里 ⇒ node_tool_calls 漏掉它们,
|
|
301
|
+
// 而 read 类里就有 list_network_secrets / list_providers / tail_node_logs 这些敏感读。
|
|
302
|
+
// 🔴 只计数、不判定:拦截语义零变化(这几类本来也不在这个闸里判)。
|
|
303
|
+
// always 类仍然不记:那是心跳(get_inbox / report_status),记进来只会把账本淹掉。
|
|
299
304
|
const gated = cls !== "always" && cls !== "read" && cls !== "requirements" && cls !== "schedule";
|
|
300
|
-
if (gated) {
|
|
305
|
+
if (!gated) {
|
|
306
|
+
if (cls !== "always") recordNodeToolCall(id, `mcp:${name}`);
|
|
307
|
+
} else {
|
|
301
308
|
const args = callArgs[0] && typeof callArgs[0] === "object" ? callArgs[0] as Record<string, unknown> : {};
|
|
302
309
|
const verdict = verdictFor(id, name, args);
|
|
303
310
|
if (nodeDecide(id, `mcp:${name}`, verdict)) return denied(verdict!.reason, `mcp:${name}`);
|