@sleep2agi/commhub-server 0.9.0-preview.213 → 0.9.0-preview.214

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.213",
3
+ "version": "0.9.0-preview.214",
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",
@@ -614,6 +614,20 @@ describe("#2719 凭据台账", () => {
614
614
  expect(bound.unbound_node).toBe(false);
615
615
  });
616
616
 
617
+ // 🔴 线上第一次真实调用就抓到的 bug:`?limit=5` 时 summary.total 报 5,
618
+ // 而那个网络实际有 580 个令牌。分页只该影响 tokens 数组,不该影响计数。
619
+ test("分页只截断 tokens,不影响 summary(summary 必须是全量)", async () => {
620
+ const full = await call(user.boss.token, "GET", `/api/networks/${net}/token-ledger?limit=1000`);
621
+ expect(full.status).toBe(200);
622
+ const page = await call(user.boss.token, "GET", `/api/networks/${net}/token-ledger?limit=1`);
623
+ expect(page.status).toBe(200);
624
+ expect(page.body.tokens.length).toBe(1);
625
+ expect(page.body.returned).toBe(1);
626
+ // 两个请求的 summary 必须完全一样
627
+ expect(page.body.summary).toEqual(full.body.summary);
628
+ expect(page.body.summary.total).toBeGreaterThan(1);
629
+ });
630
+
617
631
  test("成员(非 owner/admin)看不到;节点令牌也看不到", async () => {
618
632
  const r = await call(user.mem.token, "GET", `/api/networks/${net}/token-ledger`);
619
633
  expect(r.status).toBe(403);
package/src/server.ts CHANGED
@@ -3256,20 +3256,42 @@ return Bun.serve({
3256
3256
  top_tools: tools.slice(0, 3),
3257
3257
  };
3258
3258
  });
3259
- const unbound = tokens.filter((t) => t.unbound_node);
3259
+ // 🔴 summary 必须按**全量**算,不能按这一页算。
3260
+ // 2026-10-30 上线后第一次真实调用就抓到了:`?limit=5` 时 summary.total 报 5,
3261
+ // 而那个网络实际有 580 个令牌 —— 管理员会以为「只有 5 把钥匙」。
3262
+ // 分页只应影响 tokens 数组,不应影响计数。
3263
+ const agg = db.get<{ total: number | string; bound: number | string; unbound_node: number | string; unbound_node_in_use: number | string; unused_24h: number | string }>(
3264
+ `SELECT
3265
+ COUNT(*) AS total,
3266
+ SUM(CASE WHEN bound_node_id IS NOT NULL THEN 1 ELSE 0 END) AS bound,
3267
+ SUM(CASE WHEN name LIKE 'node:%' AND bound_node_id IS NULL THEN 1 ELSE 0 END) AS unbound_node,
3268
+ SUM(CASE WHEN name LIKE 'node:%' AND bound_node_id IS NULL
3269
+ AND EXISTS (SELECT 1 FROM node_tool_calls k
3270
+ WHERE k.node_id = 'token:' || t.token_id AND k.hour >= ?2)
3271
+ THEN 1 ELSE 0 END) AS unbound_node_in_use,
3272
+ SUM(CASE WHEN NOT EXISTS (SELECT 1 FROM node_tool_calls k
3273
+ WHERE k.node_id = 'token:' || t.token_id AND k.hour >= ?2)
3274
+ THEN 1 ELSE 0 END) AS unused_24h
3275
+ FROM api_tokens t
3276
+ WHERE t.network_id = ?1 AND t.revoked_at IS NULL`,
3277
+ networkId, sinceHour,
3278
+ );
3279
+ const num = (v: number | string | null | undefined) => Number(v ?? 0);
3260
3280
  return withCors(req, Response.json({
3261
3281
  ok: true,
3262
3282
  network_id: networkId,
3263
3283
  since: new Date(Date.now() - 86_400_000).toISOString(),
3264
3284
  limit,
3285
+ // summary 是全量;tokens 只是这一页(returned 一眼能看出被截断没有)
3265
3286
  summary: {
3266
- total: tokens.length,
3267
- bound: tokens.filter((t) => !!t.bound_node_id).length,
3268
- unbound_node: unbound.length,
3287
+ total: num(agg?.total),
3288
+ bound: num(agg?.bound),
3289
+ unbound_node: num(agg?.unbound_node),
3269
3290
  // 「名义上还在、实际上没人用」的数量:这才是能不能吊销的判据
3270
- unused_24h: tokens.filter((t) => t.calls_24h === 0).length,
3271
- unbound_node_in_use: unbound.filter((t) => t.calls_24h > 0).length,
3291
+ unused_24h: num(agg?.unused_24h),
3292
+ unbound_node_in_use: num(agg?.unbound_node_in_use),
3272
3293
  },
3294
+ returned: tokens.length,
3273
3295
  tokens,
3274
3296
  }));
3275
3297
  }