@sleep2agi/commhub-server 0.9.0-preview.283 → 0.9.0-preview.285
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 +2 -2
- package/package.json +1 -1
- package/src/expiry-counters.test.ts +51 -0
- package/src/expiry-counters.ts +24 -5
- package/src/node-grants-http.test.ts +46 -0
- package/src/node-grants.ts +5 -2
- package/src/server.ts +2 -1
- package/src/tools.ts +18 -0
package/build-info.json
CHANGED
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.285",
|
|
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",
|
|
@@ -234,3 +234,54 @@ describe("2026-11-13 内存计数也以「最后一次回复」为界(恢复
|
|
|
234
234
|
expect(parseHubTime("garbage")).toBeNull();
|
|
235
235
|
});
|
|
236
236
|
});
|
|
237
|
+
|
|
238
|
+
describe("2026-11-16 内存计数只数「最后一次回复之后」的那几条(admin:TMA客户端欧 内存 4、回复后实际 2,仍被拒)", () => {
|
|
239
|
+
const NOW = Date.parse("2026-11-16T03:00:00.000Z");
|
|
240
|
+
const sqlite = (ms: number) => new Date(ms).toISOString().replace("T", " ").slice(0, 19);
|
|
241
|
+
const iso = (ms: number) => new Date(ms).toISOString();
|
|
242
|
+
const memAt = (times: number[]) => ({
|
|
243
|
+
since: "2026-11-15T13:17:00.000Z", total: times.length, by_node: { tma: times.length },
|
|
244
|
+
last_at: { tma: iso(times[times.length - 1]) }, recent_at: { tma: times.map(iso) },
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("🔴 回复前 3 次 + 回复后 1 次 ⇒ 只算 1 次,放行", () => {
|
|
248
|
+
const h = 60 * 60_000;
|
|
249
|
+
const r = neverConsumeGate({
|
|
250
|
+
alias: "tma", counters: memAt([NOW - 13 * h, NOW - 12 * h, NOW - 11 * h, NOW - 1 * h]), force: false, forceAllowed: true, now: NOW,
|
|
251
|
+
persistent: { expiries: 1, last_at: sqlite(NOW - h), last_reply_at: sqlite(NOW - 2 * h) },
|
|
252
|
+
});
|
|
253
|
+
expect(r.ok).toBe(true);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("负数用例:回复后又过期 3 次 ⇒ 仍然拒,拒绝体带 last_reply_at", () => {
|
|
257
|
+
const m = 60_000;
|
|
258
|
+
const r = neverConsumeGate({
|
|
259
|
+
alias: "tma", counters: memAt([NOW - 90 * m, NOW - 30 * m, NOW - 20 * m, NOW - 10 * m]), force: false, forceAllowed: true, now: NOW,
|
|
260
|
+
persistent: { expiries: 0, last_at: null, last_reply_at: sqlite(NOW - 60 * m) },
|
|
261
|
+
});
|
|
262
|
+
expect(r.ok).toBe(false);
|
|
263
|
+
if (!r.ok) {
|
|
264
|
+
expect(r.expiries).toBe(3);
|
|
265
|
+
expect(r.last_reply_at).toBe(sqlite(NOW - 60 * m));
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test("没有 recent_at(旧快照)⇒ 退回原来的整桶计数", () => {
|
|
270
|
+
const m = 60_000;
|
|
271
|
+
const r = neverConsumeGate({
|
|
272
|
+
alias: "tma", counters: { since: "x", total: 4, by_node: { tma: 4 }, last_at: { tma: iso(NOW - 10 * m) } }, force: false, forceAllowed: true, now: NOW,
|
|
273
|
+
persistent: { expiries: 0, last_at: null, last_reply_at: sqlite(NOW - 60 * m) },
|
|
274
|
+
});
|
|
275
|
+
expect(r.ok).toBe(false);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test("noteNeverConsumedExpiry 记下逐条时刻,最多 16 条", () => {
|
|
279
|
+
resetNeverConsumedExpiryCountersForTest(new Date(NOW));
|
|
280
|
+
for (let i = 0; i < 20; i++) noteNeverConsumedExpiry("tma", NOW + i * 1000);
|
|
281
|
+
const c = neverConsumedExpiryCounters();
|
|
282
|
+
expect(c.by_node.tma).toBe(20);
|
|
283
|
+
expect(c.recent_at?.tma.length).toBe(16);
|
|
284
|
+
expect(c.recent_at?.tma[15]).toBe(iso(NOW + 19 * 1000));
|
|
285
|
+
expect(c.last_at.tma).toBe(iso(NOW + 19 * 1000));
|
|
286
|
+
});
|
|
287
|
+
});
|
package/src/expiry-counters.ts
CHANGED
|
@@ -25,9 +25,17 @@ export type NeverConsumedExpiryCounters = {
|
|
|
25
25
|
by_node: Record<string, number>;
|
|
26
26
|
/** 每个节点最近一次「从未取走」的时刻(ISO)。**光有计数会永久拉黑一台恢复了的节点。** */
|
|
27
27
|
last_at: Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* 2026-11-16 —— 每个节点最近几次「从未取走」的时刻(ISO,旧→新,最多 RECENT_CAP 条)。
|
|
30
|
+
* 闸要数的是「最后一次回复**之后**」的过期;只有 count + lastAt 时,回复之前攒下的过期
|
|
31
|
+
* 会和回复之后的一条一起被算进去(现场:TMA客户端欧 内存 4、库里回复之后只有 2,仍被拒)。
|
|
32
|
+
*/
|
|
33
|
+
recent_at?: Record<string, string[]>;
|
|
28
34
|
};
|
|
29
35
|
|
|
30
|
-
|
|
36
|
+
/** 每个节点保留的最近过期时刻条数(够判阈值 3,键有限、不随请求膨胀)。 */
|
|
37
|
+
const RECENT_CAP = 16;
|
|
38
|
+
type Bucket = { count: number; lastAt: number; recent: number[] };
|
|
31
39
|
const counters = new Map<string, Bucket>();
|
|
32
40
|
let since = new Date().toISOString();
|
|
33
41
|
|
|
@@ -35,19 +43,22 @@ let since = new Date().toISOString();
|
|
|
35
43
|
export function noteNeverConsumedExpiry(node: string, now = Date.now()): void {
|
|
36
44
|
const key = node || "(unknown)";
|
|
37
45
|
const prev = counters.get(key);
|
|
38
|
-
|
|
46
|
+
const recent = [...(prev?.recent ?? []), now].slice(-RECENT_CAP);
|
|
47
|
+
counters.set(key, { count: (prev?.count ?? 0) + 1, lastAt: now, recent });
|
|
39
48
|
}
|
|
40
49
|
|
|
41
50
|
export function neverConsumedExpiryCounters(): NeverConsumedExpiryCounters {
|
|
42
51
|
let total = 0;
|
|
43
52
|
const by_node: Record<string, number> = {};
|
|
44
53
|
const last_at: Record<string, string> = {};
|
|
54
|
+
const recent_at: Record<string, string[]> = {};
|
|
45
55
|
for (const [k, v] of [...counters.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
46
56
|
by_node[k] = v.count;
|
|
47
57
|
last_at[k] = new Date(v.lastAt).toISOString();
|
|
58
|
+
recent_at[k] = v.recent.map(t => new Date(t).toISOString());
|
|
48
59
|
total += v.count;
|
|
49
60
|
}
|
|
50
|
-
return { since, total, by_node, last_at };
|
|
61
|
+
return { since, total, by_node, last_at, recent_at };
|
|
51
62
|
}
|
|
52
63
|
|
|
53
64
|
/** 仅测试用:清零并重置窗口。 */
|
|
@@ -166,7 +177,7 @@ export function persistentNeverConsumedExpiries(
|
|
|
166
177
|
|
|
167
178
|
export type NeverConsumeGate =
|
|
168
179
|
| { ok: true }
|
|
169
|
-
| { ok: false; error: "target_never_consumes"; alias: string; expiries: number; window_since: string; message: string };
|
|
180
|
+
| { ok: false; error: "target_never_consumes"; alias: string; expiries: number; window_since: string; last_reply_at: string | null; message: string };
|
|
170
181
|
|
|
171
182
|
export function neverConsumeGate(input: {
|
|
172
183
|
alias: string;
|
|
@@ -190,7 +201,13 @@ export function neverConsumeGate(input: {
|
|
|
190
201
|
const memLastAt = parseHubTime(input.counters.last_at?.[input.alias]);
|
|
191
202
|
const lastReplyAt = parseHubTime(input.persistent?.last_reply_at ?? undefined);
|
|
192
203
|
const memorySupersededByReply = memLastAt !== null && lastReplyAt !== null && lastReplyAt >= memLastAt;
|
|
193
|
-
|
|
204
|
+
// 2026-11-16 —— 只比「最近一次」不够:回复之后哪怕再过期一条,回复之前攒的也会全部复活。
|
|
205
|
+
// 有逐条时刻时,只数**晚于最后一次回复**的那几条(与 DB 证据同一条边界)。
|
|
206
|
+
const recent = input.counters.recent_at?.[input.alias];
|
|
207
|
+
const fromMemory = memorySupersededByReply ? 0
|
|
208
|
+
: recent && lastReplyAt !== null
|
|
209
|
+
? recent.map(parseHubTime).filter((t): t is number => t !== null && t > lastReplyAt).length
|
|
210
|
+
: (input.counters.by_node[input.alias] ?? 0);
|
|
194
211
|
const fromDb = input.persistent?.expiries ?? 0;
|
|
195
212
|
const expiries = Math.max(fromMemory, fromDb);
|
|
196
213
|
if (expiries < limit) return { ok: true };
|
|
@@ -204,6 +221,7 @@ export function neverConsumeGate(input: {
|
|
|
204
221
|
alias: input.alias,
|
|
205
222
|
expiries,
|
|
206
223
|
window_since: input.counters.since,
|
|
224
|
+
last_reply_at: input.persistent?.last_reply_at ?? null,
|
|
207
225
|
message:
|
|
208
226
|
`this target has ${fromDb} expired task(s) with no reply since its last reply`
|
|
209
227
|
+ `${input.persistent?.last_reply_at ? ` (${input.persistent.last_reply_at})` : " (it has never replied)"}; `
|
|
@@ -231,6 +249,7 @@ export function neverConsumeGate(input: {
|
|
|
231
249
|
alias: input.alias,
|
|
232
250
|
expiries,
|
|
233
251
|
window_since: input.counters.since,
|
|
252
|
+
last_reply_at: input.persistent?.last_reply_at ?? null,
|
|
234
253
|
message:
|
|
235
254
|
`this target has expired ${expiries} task(s) without ever taking one (window since ${input.counters.since}); `
|
|
236
255
|
+ `nothing will queue correctly here until it consumes again — pass force:true (user tokens) if you know it recovered`,
|
|
@@ -237,3 +237,49 @@ describe("任务看板 —— 授权让节点看得到 / 改得了", () => {
|
|
|
237
237
|
expect(await cardIds(node.mgr.token)).not.toContain(cardA);
|
|
238
238
|
});
|
|
239
239
|
});
|
|
240
|
+
|
|
241
|
+
// admin 2026-11-16「节点新增权限可以编辑其他人的 AGENTS.md 的管理权限」:rules 能力。
|
|
242
|
+
describe("rules 授权 —— 节点写别的节点的规则文件", () => {
|
|
243
|
+
const write = (token: string, nodeId: string) => mcp(token, "write_node_rules_file", { node_id: nodeId, content: "# rules\n" });
|
|
244
|
+
const clearQueue = () => db.run("DELETE FROM node_rules_requests WHERE node_id IN (?1, ?2, ?3)", [node.mgr.id, node.a.id, node.b.id]);
|
|
245
|
+
|
|
246
|
+
test("写自己的不需要授权", async () => {
|
|
247
|
+
clearQueue();
|
|
248
|
+
await putGrants(user.boss.token, node.mgr.id, []);
|
|
249
|
+
const r = await write(node.mgr.token, node.mgr.id);
|
|
250
|
+
expect(r.ok).toBe(true);
|
|
251
|
+
});
|
|
252
|
+
test("没授权写别人的 ⇒ rules_write_not_granted", async () => {
|
|
253
|
+
clearQueue();
|
|
254
|
+
const r = await write(node.mgr.token, node.a.id);
|
|
255
|
+
expect(r.ok).toBe(false);
|
|
256
|
+
expect(r.error).toBe("rules_write_not_granted");
|
|
257
|
+
});
|
|
258
|
+
test("rules·node·edit ⇒ 只能写那一个节点", async () => {
|
|
259
|
+
clearQueue();
|
|
260
|
+
expect((await putGrants(user.boss.token, node.mgr.id, [{ capability: "rules", scope_kind: "node", scope_id: node.a.id, level: "edit" }])).status).toBe(200);
|
|
261
|
+
expect((await write(node.mgr.token, node.a.id)).ok).toBe(true);
|
|
262
|
+
expect((await write(node.mgr.token, node.b.id)).error).toBe("rules_write_not_granted");
|
|
263
|
+
});
|
|
264
|
+
test("rules·department·edit 覆盖子树;read 级别不能写", async () => {
|
|
265
|
+
clearQueue();
|
|
266
|
+
await putGrants(user.boss.token, node.mgr.id, [{ capability: "rules", scope_kind: "department", scope_id: deptB, level: "edit" }]);
|
|
267
|
+
expect((await write(node.mgr.token, node.b.id)).ok).toBe(true);
|
|
268
|
+
clearQueue();
|
|
269
|
+
await putGrants(user.boss.token, node.mgr.id, [{ capability: "rules", scope_kind: "network", level: "read" }]);
|
|
270
|
+
expect((await write(node.mgr.token, node.b.id)).error).toBe("rules_write_not_granted");
|
|
271
|
+
});
|
|
272
|
+
test("只读节点即使有 edit 授权也不能写别人的", async () => {
|
|
273
|
+
clearQueue();
|
|
274
|
+
await putGrants(user.boss.token, node.mgr.id, [{ capability: "rules", scope_kind: "network", level: "edit" }]);
|
|
275
|
+
db.run("UPDATE nodes SET permission_mode = 'readonly' WHERE node_id = ?1", [node.mgr.id]);
|
|
276
|
+
const r = await write(node.mgr.token, node.a.id);
|
|
277
|
+
db.run("UPDATE nodes SET permission_mode = 'normal' WHERE node_id = ?1", [node.mgr.id]);
|
|
278
|
+
expect(r.ok).toBe(false);
|
|
279
|
+
});
|
|
280
|
+
test("人(用户令牌)照旧能写", async () => {
|
|
281
|
+
clearQueue();
|
|
282
|
+
await putGrants(user.boss.token, node.mgr.id, []);
|
|
283
|
+
expect((await write(user.boss.token, node.a.id)).ok).toBe(true);
|
|
284
|
+
});
|
|
285
|
+
});
|
package/src/node-grants.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// 也可以只管某个部门下的」)。
|
|
3
3
|
//
|
|
4
4
|
// 模型 = 能力 × 范围 × 级别,按行存在 node_grants:
|
|
5
|
-
// capability schedules(第一期);board(第二期,随 requirements
|
|
5
|
+
// capability schedules(第一期);board(第二期,随 requirements 读写判定一起上线);rules(写别的节点的 AGENTS.md)
|
|
6
6
|
// scope_kind network(整个网络,scope_id = "")| department(该部门及全部下级,scope_id = department_id)
|
|
7
7
|
// | node(单个节点,scope_id = node_id)
|
|
8
8
|
// level read(只看)| edit(看 + 改)
|
|
@@ -33,7 +33,10 @@ db.exec(`
|
|
|
33
33
|
CREATE INDEX IF NOT EXISTS idx_node_grants_node ON node_grants(node_id);
|
|
34
34
|
`);
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
// rules(admin 2026-11-16「节点新增权限可以编辑其他人的 AGENTS.md 的管理权限」):
|
|
37
|
+
// 节点令牌写**别的节点**的规则文件(AGENTS.md / CLAUDE.md)必须有 rules·edit 覆盖目标;写自己的不需要。
|
|
38
|
+
// 读照旧(read 类工具),人(用户令牌)不受影响。
|
|
39
|
+
export const GRANT_CAPABILITIES = ["schedules", "board", "rules"] as const;
|
|
37
40
|
// 第二期(board):能力与判定 + requirements 读写判定已在同一个 PR 里一起上 ⇒ 现在开闸。
|
|
38
41
|
export const GRANT_CAPABILITIES_PLANNED = [] as const;
|
|
39
42
|
export type GrantCapability = (typeof GRANT_CAPABILITIES)[number];
|
package/src/server.ts
CHANGED
|
@@ -3923,7 +3923,8 @@ return Bun.serve({
|
|
|
3923
3923
|
since: startedAtIso,
|
|
3924
3924
|
task_rejections: taskRejectionCounters(),
|
|
3925
3925
|
// #3271 —— 「到死都没被取走」按节点分桶。窗口用同一个 since。
|
|
3926
|
-
|
|
3926
|
+
// recent_at 只给闸用(逐条时刻,按节点最多 16 条),不进这个读数,免得读数随节点数膨胀。
|
|
3927
|
+
task_expiries_never_consumed: (({ recent_at: _recent, ...rest }) => rest)(neverConsumedExpiryCounters()),
|
|
3927
3928
|
// #3314 B 类 —— 故意匿名的那两条路由(register / login)没有身份门,
|
|
3928
3929
|
// 它们的健康度只能从「限流与锁定命中了几次、打到了几个不同来源」读出来。
|
|
3929
3930
|
// 比值才是有意义的部分:命中数 ≫ 去重数 ⇒ 单点重试;两者接近 ⇒ 换 IP / 撒网。
|
package/src/tools.ts
CHANGED
|
@@ -58,6 +58,7 @@ import {
|
|
|
58
58
|
validateOpenCodeV2ProviderModel,
|
|
59
59
|
ValidationError,
|
|
60
60
|
} from "./create-node-validate.js";
|
|
61
|
+
import { grantLevelForTarget } from "./node-grants.js";
|
|
61
62
|
import {
|
|
62
63
|
putPendingEnvBlob,
|
|
63
64
|
takePendingEnvBlob,
|
|
@@ -4487,6 +4488,23 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
|
|
|
4487
4488
|
}
|
|
4488
4489
|
}
|
|
4489
4490
|
const nodeId = node.node_id;
|
|
4491
|
+
// admin 2026-11-16 —— 节点令牌改**别的节点**的规则文件要有 rules·edit 授权(节点页「管理权限」里给)。
|
|
4492
|
+
// 改自己的照旧放行;人(用户令牌)不走这条。只读节点一律降为 read ⇒ 不能写。
|
|
4493
|
+
if (op === "write" && callerTokenIsNetwork) {
|
|
4494
|
+
const caller = resolveNodeCaller(callerTokenId);
|
|
4495
|
+
const self = caller.ok && (caller.kind === "node" ? caller.nodeId === nodeId : false)
|
|
4496
|
+
|| caller.ok && node.alias === caller.alias;
|
|
4497
|
+
if (!self) {
|
|
4498
|
+
const callerNodeId = caller.ok && caller.kind === "node" ? caller.nodeId : null;
|
|
4499
|
+
const mode = callerNodeId ? db.get<{ permission_mode: string | null }>("SELECT permission_mode FROM nodes WHERE node_id = ?1", callerNodeId)?.permission_mode ?? null : null;
|
|
4500
|
+
const level = callerNodeId && !nodeId.startsWith("session:")
|
|
4501
|
+
? grantLevelForTarget(node.network_id || "default", callerNodeId, "rules", nodeId, mode)
|
|
4502
|
+
: null;
|
|
4503
|
+
if (level !== "edit") {
|
|
4504
|
+
return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "rules_write_not_granted", target: node.alias, message: "a node may only edit its own rules file; editing another node's AGENTS.md needs a 'rules' edit grant covering that node (node page → 管理权限), or use a user login" }) }] };
|
|
4505
|
+
}
|
|
4506
|
+
}
|
|
4507
|
+
}
|
|
4490
4508
|
if (isLogsOp(op) && !canReadNodeLogs(nodeId, node.network_id || "default")) {
|
|
4491
4509
|
return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "logs_permission_denied", message: "only the node's owner or a network owner/admin can read its run log" }) }] };
|
|
4492
4510
|
}
|