@sleep2agi/commhub-server 0.9.0-preview.205 → 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 +40 -0
- package/src/server.ts +38 -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 —— 调用面计数:拒绝日志给不了分母。
|
|
@@ -464,3 +485,22 @@ describe("空数组不猜:拒收,清空请传 null", () => {
|
|
|
464
485
|
expect(row.body.nodes[0].tool_allowlist).toBeNull();
|
|
465
486
|
});
|
|
466
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 之和),
|