@sleep2agi/commhub-server 0.9.0-preview.266 → 0.9.0-preview.267

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/build-info.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schema": 1,
3
- "commit": "dcb3b00f91e421f0d689af875c57b47ab9e4f66c",
3
+ "commit": "54d87dd2c5a1d5261b5953a6fc1204e34024c0cb",
4
4
  "dirty": false,
5
- "built_at": "2026-11-05T04:59:12.456Z",
5
+ "built_at": "2026-11-05T12:41:26.525Z",
6
6
  "source": "git"
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/commhub-server",
3
- "version": "0.9.0-preview.266",
3
+ "version": "0.9.0-preview.267",
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",
@@ -0,0 +1,96 @@
1
+ // #3119 —— list_tasks 的增量与「省掉 stats」。
2
+ //
3
+ // 生产实测(2026-11-05 近 6h):list_tasks 占全部工具调用 **35.7%**,是最大的一格;
4
+ // 提示词只能劝,改不了接口成本。这里补两个参数,让轮询本身变便宜:
5
+ // created_since 只取这之后创建的(空窗口回极短体)
6
+ // omit_stats 跳过按状态的全表 GROUP BY(轮询时它比任务列表还贵)
7
+ // server_time 服务端给水位,客户端别用本地时钟(差几秒就漏活或重复)
8
+ //
9
+ // 判据分四格,缺一格都只是「加了个参数」:
10
+ // ① created_since 真的过滤(旧的看不见、新的看得见)
11
+ // ② 镜像:不传 created_since ⇒ 行为与以前一致(旧调用方零影响)
12
+ // ③ omit_stats 真的不返回 stats,且返回值里明确写着 stats_omitted
13
+ // ④ server_time 一定在(两种分支都有),否则客户端拿不到水位
14
+ import './require-explicit-test-db';
15
+ import { afterAll, beforeEach, describe, expect, test } from 'bun:test';
16
+ import { db } from './db';
17
+ import { registerTools } from './tools';
18
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
19
+
20
+ const net = 'net_list_tasks_since';
21
+ const user = 'u_list_tasks_since';
22
+ // username 必须等于某个 nodes/sessions 的 alias,否则受限成员过滤会把任务全挡掉
23
+ const alias = user;
24
+
25
+ function client() {
26
+ const handlers: Record<string, any> = {};
27
+ const server = new McpServer({ name: 't-since', version: '0' }) as any;
28
+ const tool = server.tool.bind(server);
29
+ server.tool = (name: string, desc: any, shape: any, handler: any) => { handlers[name] = handler; return tool(name, desc, shape, handler); };
30
+ registerTools(server, undefined, null, user, alias, false, null);
31
+ return async (name: string, args: any = {}) => JSON.parse((await handlers[name](args)).content[0].text);
32
+ }
33
+
34
+ function cleanup() {
35
+ db.run('DELETE FROM tasks WHERE task_id LIKE ?1', ['t_since_%']);
36
+ db.run('DELETE FROM nodes WHERE network_id=?1', [net]);
37
+ db.run('DELETE FROM network_members WHERE network_id=?1', [net]);
38
+ db.run('DELETE FROM networks WHERE network_id=?1', [net]);
39
+ db.run('DELETE FROM users WHERE user_id=?1', [user]);
40
+ }
41
+
42
+ beforeEach(() => {
43
+ cleanup();
44
+ db.run("INSERT INTO users(user_id,username,password_hash,role) VALUES(?1,?1,'x','user')", [user]);
45
+ db.run('INSERT INTO networks(network_id,network_name,owner_id) VALUES(?1,?1,?2)', [net, user]);
46
+ db.run("INSERT INTO network_members(user_id,network_id,role) VALUES(?1,?2,'owner')", [user, net]);
47
+ // caller 的 username 必须能被认成「本网络的 Agent」,否则 addAgentTimelineScope 会按
48
+ // 受限成员把任务全过滤掉(那是安全边界,不是 bug)。建一行 nodes 让它认得出。
49
+ db.run('INSERT INTO nodes(node_id,node_name,alias,network_id) VALUES(?1,?2,?2,?3)', ['n_since', alias, net]);
50
+ // 两条任务:一条旧、一条新(时间列是 TEXT,字典序即时间序)
51
+ db.run(`INSERT INTO tasks(task_id, from_name, to_name, status, content, created_at, network_id)
52
+ VALUES ('t_since_old', 'peer', ?1, 'delivered', 'old work', '2026-11-05 01:00:00', ?2)`, [alias, net]);
53
+ db.run(`INSERT INTO tasks(task_id, from_name, to_name, status, content, created_at, network_id)
54
+ VALUES ('t_since_new', 'peer', ?1, 'delivered', 'new work', '2026-11-05 09:00:00', ?2)`, [alias, net]);
55
+ });
56
+ afterAll(cleanup);
57
+
58
+ describe("#3119 list_tasks 的增量与水印", () => {
59
+ test("① created_since 生效:旧的看不见、新的看得见", async () => {
60
+ const call = client();
61
+ const r = await call('list_tasks', { alias, network_id: net, created_since: '2026-11-05 08:00:00', omit_stats: true, limit: 20 });
62
+ expect(r.ok).toBe(true);
63
+ expect(r.tasks.map((t: any) => t.task_id)).toEqual(['t_since_new']);
64
+ });
65
+
66
+ test("② 镜像:不传 created_since ⇒ 两条都在(旧调用方零影响)", async () => {
67
+ const call = client();
68
+ const r = await call('list_tasks', { alias, network_id: net, limit: 20 });
69
+ expect(r.tasks.map((t: any) => t.task_id).sort()).toEqual(['t_since_new', 't_since_old']);
70
+ expect(r.stats).toBeDefined();
71
+ });
72
+
73
+ test("③ omit_stats ⇒ 不返回 stats,并明确写 stats_omitted(不是静默少一格)", async () => {
74
+ const call = client();
75
+ const r = await call('list_tasks', { alias, network_id: net, omit_stats: true, limit: 20 });
76
+ expect(r.stats).toBeUndefined();
77
+ expect(r.stats_omitted).toBe(true);
78
+ });
79
+
80
+ test("④ 两种分支都带 server_time(客户端的水位来源)", async () => {
81
+ const call = client();
82
+ const a = await call('list_tasks', { alias, network_id: net, limit: 20 });
83
+ const b = await call('list_tasks', { alias, network_id: net, omit_stats: true, limit: 20 });
84
+ expect(typeof a.server_time).toBe('string');
85
+ expect(typeof b.server_time).toBe('string');
86
+ expect(Number.isNaN(Date.parse(a.server_time))).toBe(false);
87
+ });
88
+
89
+ test("空窗口 ⇒ 短响应(tasks 空、无 stats、仍有 server_time)", async () => {
90
+ const call = client();
91
+ const r = await call('list_tasks', { alias, network_id: net, created_since: '2099-01-01 00:00:00', omit_stats: true, limit: 20 });
92
+ expect(r.tasks).toEqual([]);
93
+ expect(r.count).toBe(0);
94
+ expect(typeof r.server_time).toBe('string');
95
+ });
96
+ });
package/src/tools.ts CHANGED
@@ -3122,8 +3122,16 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3122
3122
  durable_terminal_cursor: z.boolean().optional().describe("Read terminal outbound journal in monotonic order"),
3123
3123
  after_terminal_seq: z.number().int().min(0).optional().describe("Exclusive terminal journal watermark"),
3124
3124
  limit: z.number().min(1).max(100).optional().default(20),
3125
+ // #3119 —— 轮询增量。生产实测(2026-11-05 近 6h)list_tasks 占全部工具调用 35.7%、
3126
+ // 是最大的一格:每次轮询都拉全量再自己筛。给它一个「只要这之后创建的」,
3127
+ // 空窗口就回极短体,比任何提示词都省(提示词只能劝,改不了接口成本)。
3128
+ created_since: z.string().max(64).optional()
3129
+ .describe("Only tasks created after this timestamp (ISO 8601 or 'YYYY-MM-DD HH:MM:SS'). Use the previous response's server_time as the next value."),
3130
+ // #3119 —— stats 是全表 GROUP BY:轮询时它比任务列表本身还贵。
3131
+ omit_stats: z.boolean().optional().default(false)
3132
+ .describe("Skip the per-status counts (they scan every task in scope). Pollers should set true."),
3125
3133
  },
3126
- async ({ alias, status, from_name, from_node_id, network_id: netId, before_created_at, before_task_id, durable_cursor, durable_terminal_cursor, after_terminal_seq, limit }) => {
3134
+ async ({ alias, status, from_name, from_node_id, network_id: netId, before_created_at, before_task_id, durable_cursor, durable_terminal_cursor, after_terminal_seq, limit, created_since, omit_stats }) => {
3127
3135
  const readScope = resolveReadScope(netId);
3128
3136
  if (readScope.denied) return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: readScope.denied }) }] };
3129
3137
  if (durable_cursor) {
@@ -3180,6 +3188,7 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3180
3188
  if (status) { sql += ` AND status = ?${params.length + 1}`; params.push(status); }
3181
3189
  if (from_name) { sql += ` AND from_name = ?${params.length + 1}`; params.push(from_name); }
3182
3190
  if (from_node_id) { sql += ` AND from_node_id = ?${params.length + 1}`; params.push(from_node_id); }
3191
+ if (created_since) { sql += ` AND created_at > ?${params.length + 1}`; params.push(created_since); }
3183
3192
  if (before_created_at && before_task_id) {
3184
3193
  sql += ` AND (created_at < ?${params.length + 1} OR (created_at = ?${params.length + 1} AND task_id < ?${params.length + 2}))`;
3185
3194
  params.push(before_created_at, before_task_id);
@@ -3188,7 +3197,22 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3188
3197
  params.push(limit);
3189
3198
  const tasks = db.all(sql, ...params);
3190
3199
 
3191
- // Stats
3200
+ // Stats(#3119:轮询时默认仍然返回,调用方可用 omit_stats 显式关掉)
3201
+ if (omit_stats) {
3202
+ return { content: [{ type: "text" as const, text: JSON.stringify({
3203
+ ok: true,
3204
+ ...(durable_cursor ? { capability: "list_tasks.immutable-node-cursor.v1" } : {}),
3205
+ tasks,
3206
+ count: tasks.length,
3207
+ next_cursor: tasks.length === limit ? {
3208
+ before_created_at: tasks[tasks.length - 1]?.created_at,
3209
+ before_task_id: tasks[tasks.length - 1]?.task_id,
3210
+ } : null,
3211
+ // 客户端把这个值原样当下一轮的 created_since,避免两边时钟不一致。
3212
+ server_time: new Date().toISOString(),
3213
+ stats_omitted: true,
3214
+ }) }] };
3215
+ }
3192
3216
  const statsParams: any[] = [];
3193
3217
  let statsSql = "SELECT status, COUNT(*) as count FROM tasks WHERE 1=1";
3194
3218
  statsSql = addAgentTimelineScope(statsSql, statsParams, readScope, { from: "from_name", to: "to_name", fromNodeId: "from_node_id", toNodeId: "to_node_id" });
@@ -3207,6 +3231,8 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3207
3231
  before_created_at: tasks[tasks.length - 1]?.created_at,
3208
3232
  before_task_id: tasks[tasks.length - 1]?.task_id,
3209
3233
  } : null,
3234
+ // #3119 —— 轮询水位由服务端给,客户端别用本地时钟(两边差几秒就会漏活或重复)。
3235
+ server_time: new Date().toISOString(),
3210
3236
  stats,
3211
3237
  }),
3212
3238
  }],