@sleep2agi/commhub-server 0.9.0-preview.208 → 0.9.0-preview.209

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.208",
3
+ "version": "0.9.0-preview.209",
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",
@@ -615,3 +615,31 @@ describe("#2969 REST 写白名单", () => {
615
615
  db.run("UPDATE nodes SET rest_allowlist = NULL WHERE node_id = ?1", [node.nN.id]);
616
616
  });
617
617
  });
618
+
619
+ // #2970 判据④ —— 演练发现的口径问题:
620
+ // `node_permission_log` 的行数**不等于**「打开 enforce 行为会变」的次数。
621
+ // 演练现场:log 模式下 projects_create 已被处理函数拒(user_token_required),
622
+ // 但仍记了一行 human_only —— 计入影响面就会高估。
623
+ describe("#2970 enforce 影响面分层", () => {
624
+ test("原因码分三类:显式永久拒 / 多数已在下游被拦 / 只有 enforce 才改变行为", async () => {
625
+ const { enforceImpactOf } = await import("./node-permissions.js");
626
+ // 显式原因:与开关无关,永远拒 ⇒ 不是增量
627
+ for (const r of ["mode_readonly", "mode_restricted_not_assigned", "peer_not_allowed", "tool_not_allowed", "rest_not_allowed"]) {
628
+ expect(enforceImpactOf(r as any)).toBe("explicit");
629
+ }
630
+ // human_only:多数写路由自带「只有人能操作」检查,log 下调用方本来就被拒
631
+ expect(enforceImpactOf("human_only")).toBe("likely_already_blocked");
632
+ // 只有这三类 + unbound_token 是 enforce 的真增量
633
+ for (const r of ["beyond_owner_visibility", "agent_not_granted_to_owner", "owner_unknown", "unbound_token"]) {
634
+ expect(enforceImpactOf(r as any)).toBe("changes_behavior");
635
+ }
636
+ });
637
+
638
+ test("报表带 enforce_impact 分层汇总", async () => {
639
+ const r = await call(user.boss.token, "GET", `/api/networks/${net}/node-permission-report`);
640
+ expect(r.status).toBe(200);
641
+ expect(r.body.enforce_impact).toBeTruthy();
642
+ // 每个节点也带同名结构
643
+ for (const n of r.body.nodes) expect(n.enforce_impact).toBeTruthy();
644
+ });
645
+ });
@@ -464,3 +464,31 @@ export const NODE_TOOL_CLASS: Readonly<Record<string, ToolClass>> = {
464
464
  projects_create: "human_only", projects_update: "human_only", review_skill: "human_only",
465
465
  read_opencode_catalog: "human_only", get_opencode_catalog: "human_only",
466
466
  };
467
+
468
+ /**
469
+ * #2970 判据④ 演练发现的口径问题 —— `node_permission_log` 的行数**不等于**
470
+ * 「打开 enforce 后行为会改变多少次」。
471
+ *
472
+ * 演练现场(`.208`,独立实例):
473
+ *
474
+ * log 模式 projects_create → user_token_required (调用方**也是被拒的**)
475
+ * node_permission_log += 1(reason=human_only)
476
+ * enforce projects_create → node_permission_denied / human_only
477
+ *
478
+ * 也就是说:`projects_create` 在 log 模式下就已经被处理函数自己的「只有人能做的操作」
479
+ * 检查挡住了。**这一条计入 would-be denial,但它并不是 enforce 的增量。**
480
+ *
481
+ * 所以把原因码分三类,报表按类求和 —— 否则切 enforce 时拿到的是一份高估的影响面:
482
+ *
483
+ * explicit 显式原因(MODE_REASONS):**永远拒**,与开关无关 ⇒ 不计入影响面
484
+ * likely_already_blocked human_only:多数写路由另有一道「只有人能操作」的检查,
485
+ * log 模式下调用方本来就被拒 ⇒ 通常不是增量(逐路由不同,标而不减)
486
+ * changes_behavior 只有 enforce 才会拒的理由 ⇒ **这才是真切会变的**
487
+ */
488
+ export type EnforceImpact = "explicit" | "likely_already_blocked" | "changes_behavior";
489
+
490
+ export function enforceImpactOf(reason: NodePermissionReason): EnforceImpact {
491
+ if (MODE_REASONS.has(reason)) return "explicit";
492
+ if (reason === "human_only") return "likely_already_blocked";
493
+ return "changes_behavior";
494
+ }
@@ -1962,7 +1962,7 @@ async function handleRequirementsRequestInner(ctx: RequirementsRequestContext):
1962
1962
  const row = scopedRow(ctx, decodeURIComponent(itemMatch[1]));
1963
1963
  if (row instanceof Response) return row;
1964
1964
  if (!canWrite(ctx, row.network_id)) return jsonError("permission_denied", 403);
1965
- const nodeDenied = nodeTaskGate(ctx, "PATCH /api/requirements/:id/checklist/:item", row);
1965
+ const nodeDenied = nodeTaskGate(ctx, "PATCH /api/requirements/:id/checklist/:id", row);
1966
1966
  if (nodeDenied) return nodeDenied;
1967
1967
  if (!canEditTask(taskCaller(ctx), row) && !canParticipantEditTask(taskCaller(ctx), row)) return taskDenied(ctx, row, "task_read_only");
1968
1968
  // 读-改-写在同一个同步段里完成(中间没有 await),同一进程里的两次勾选不会交错。
package/src/server.ts CHANGED
@@ -107,7 +107,7 @@ import { handleRequirementsRequest } from "./requirements.js";
107
107
  import { readHubProviders } from "./hub-provider-config.js";
108
108
  import { requestNodeSecrets, getScopedSecrets } from "./scoped-secrets.js";
109
109
  import { readEnvTransportLog } from "./env-transport-gate.js";
110
- import { normalizePeerAllowlist, normalizeToolAllowlist, restAllowlistVerdict, restRouteLabel } from "./node-permissions.js";
110
+ import { enforceImpactOf, normalizePeerAllowlist, normalizeToolAllowlist, restAllowlistVerdict, restRouteLabel } from "./node-permissions.js";
111
111
  import { labelRoute, mcpRouteLabel, recordRouteTiming, routeStats, setRouteCallerMasterToken, setRouteCallerTokenReader } from "./route-timing.js";
112
112
  import { handleExternalScheduleEditRequest } from "./external-schedule-edits.js";
113
113
  import { createNodeSessionControlRelay } from "./node-session-control.js";
@@ -3104,17 +3104,21 @@ return Bun.serve({
3104
3104
  FROM node_permission_log WHERE network_id = ?1 AND hour >= ?2
3105
3105
  GROUP BY node_id, route, reason ORDER BY node_id, route, reason`,
3106
3106
  networkId, sinceHour);
3107
- 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; unbound_token_name?: string | null }>();
3107
+ 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; unbound_token_name?: string | null; enforce_impact: Record<string, number> }>();
3108
3108
  for (const r of rows) {
3109
3109
  let entry = byNode.get(r.node_id);
3110
3110
  if (!entry) {
3111
3111
  const n = db.get<{ alias: string | null; permission_mode: string | null }>("SELECT alias, permission_mode FROM nodes WHERE node_id = ?1", r.node_id);
3112
- entry = { node_id: r.node_id, alias: n?.alias ?? null, permission_mode: n?.permission_mode || "normal", total: 0, by_reason: {}, routes: [] };
3112
+ entry = { node_id: r.node_id, alias: n?.alias ?? null, permission_mode: n?.permission_mode || "normal", total: 0, by_reason: {}, routes: [], enforce_impact: {} };
3113
3113
  byNode.set(r.node_id, entry);
3114
3114
  }
3115
3115
  const hits = Number(r.hits);
3116
3116
  entry.total += hits;
3117
3117
  entry.by_reason[r.reason] = (entry.by_reason[r.reason] ?? 0) + hits;
3118
+ // #2970 判据④ 演练发现:这些行数**不等于**「打开 enforce 行为会变」的次数。
3119
+ // 按 enforceImpactOf 分三类,让管理员看得出哪些是真增量、哪些本来就被拦着。
3120
+ const impact = enforceImpactOf(r.reason as any);
3121
+ entry.enforce_impact[impact] = (entry.enforce_impact[impact] ?? 0) + hits;
3118
3122
  entry.routes.push({ route: r.route, reason: r.reason, hits, sample: r.sample, last_hour: r.last_hour });
3119
3123
  }
3120
3124
  // #2969 —— 按节点的工具调用明细。
@@ -3140,7 +3144,7 @@ return Bun.serve({
3140
3144
  for (const nodeId of callsByNode.keys()) {
3141
3145
  if (byNode.has(nodeId)) continue;
3142
3146
  const n = db.get<{ alias: string | null; permission_mode: string | null }>("SELECT alias, permission_mode FROM nodes WHERE node_id = ?1", nodeId);
3143
- byNode.set(nodeId, { node_id: nodeId, alias: n?.alias ?? null, permission_mode: n?.permission_mode || "normal", total: 0, by_reason: {}, routes: [] });
3147
+ byNode.set(nodeId, { node_id: nodeId, alias: n?.alias ?? null, permission_mode: n?.permission_mode || "normal", total: 0, by_reason: {}, routes: [], enforce_impact: {} });
3144
3148
  }
3145
3149
  for (const entry of byNode.values()) {
3146
3150
  const list = callsByNode.get(entry.node_id) ?? [];
@@ -3173,10 +3177,17 @@ return Bun.serve({
3173
3177
  );
3174
3178
  const toolCalls = callRows.map((r) => ({ tool: r.tool, hits: Number(r.hits) }));
3175
3179
  const deniedTotal = nodes.reduce((n, e) => n + e.total, 0);
3180
+ // 顶层汇总:切 enforce 的真实影响面是多少(explicit / likely_already_blocked 都不算)
3181
+ const enforceImpact: Record<string, number> = {};
3182
+ for (const e of nodes) {
3183
+ for (const [k, v] of Object.entries(e.enforce_impact)) enforceImpact[k] = (enforceImpact[k] ?? 0) + v;
3184
+ }
3176
3185
  const callsTotal = toolCalls.reduce((n, e) => n + e.hits, 0);
3177
3186
  return withCors(req, Response.json({
3178
3187
  ok: true, network_id: networkId, since: new Date(sinceMs).toISOString(), mode: nodePermissionsFlag(),
3179
3188
  total: deniedTotal,
3189
+ // changes_behavior 才是「打开 enforce 会改变行为」的次数;另外两类都不是增量。
3190
+ enforce_impact: enforceImpact,
3180
3191
  nodes,
3181
3192
  tool_calls: { total: callsTotal, by_tool: toolCalls },
3182
3193
  }));