@sleep2agi/commhub-server 0.9.0-preview.212 → 0.9.0-preview.213
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 +41 -0
- package/src/server.ts +81 -0
- package/src/task-late-reply-http.test.ts +37 -0
- package/src/tools.ts +15 -0
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.213",
|
|
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",
|
|
@@ -581,6 +581,47 @@ describe("未绑定令牌的写调用:默认只记录,不拦", () => {
|
|
|
581
581
|
});
|
|
582
582
|
});
|
|
583
583
|
|
|
584
|
+
// #2719 —— 凭据台账:把「哪把钥匙、绑没绑、还在不在用」变成可查的东西。
|
|
585
|
+
//
|
|
586
|
+
// 为什么需要它:node-permission-report 是按**流量**聚合的,只列出「近 N 天有调用」的身份。
|
|
587
|
+
// 生产实测 760 个未吊销令牌里 381 个是 `node:` 开头但未绑定,而其中只有 4 个近 24h 出现过 ——
|
|
588
|
+
// 剩下 377 个在任何报表里都不存在,于是「这把钥匙还有人在用吗」永远答不清,也就不敢吊销。
|
|
589
|
+
describe("#2719 凭据台账", () => {
|
|
590
|
+
test("owner 能看到未绑定令牌,区分「有流量」与「零流量」", async () => {
|
|
591
|
+
const loudId = `tok_ledger_loud_${stamp}`;
|
|
592
|
+
const quietId = `tok_ledger_quiet_${stamp}`;
|
|
593
|
+
for (const [id, nm] of [[loudId, "node:ledger-loud"], [quietId, "node:ledger-quiet"]] as const) {
|
|
594
|
+
db.run("INSERT INTO api_tokens (token_id, token_hash, user_id, network_id, name, scope) VALUES (?1, ?2, ?3, ?4, ?5, 'network') ON CONFLICT (token_id) DO NOTHING",
|
|
595
|
+
[id, `hash_${id}`, user.boss.id, net, nm]);
|
|
596
|
+
}
|
|
597
|
+
const hour = new Date().toISOString().slice(0, 13);
|
|
598
|
+
db.run(`INSERT INTO node_tool_calls (network_id, node_id, tool, hour, hits) VALUES (?1, ?2, 'mcp:send_task', ?3, 5)
|
|
599
|
+
ON CONFLICT(network_id, node_id, tool, hour) DO UPDATE SET hits = node_tool_calls.hits + 5`,
|
|
600
|
+
[net, `token:${loudId}`, hour]);
|
|
601
|
+
|
|
602
|
+
const r = await call(user.boss.token, "GET", `/api/networks/${net}/token-ledger?limit=1000`);
|
|
603
|
+
expect(r.status).toBe(200);
|
|
604
|
+
const loud = r.body.tokens.find((t: any) => t.token_id === loudId);
|
|
605
|
+
const quiet = r.body.tokens.find((t: any) => t.token_id === quietId);
|
|
606
|
+
expect(loud.unbound_node).toBe(true);
|
|
607
|
+
expect(loud.calls_24h).toBe(5);
|
|
608
|
+
expect(loud.writes_24h).toBe(5);
|
|
609
|
+
expect(quiet.calls_24h).toBe(0); // 零流量也要在名单里 —— 这正是它存在的理由
|
|
610
|
+
expect(r.body.summary.unbound_node).toBeGreaterThanOrEqual(2);
|
|
611
|
+
// 绑定的节点令牌必须被标成「已绑定」,不能被算进未绑定
|
|
612
|
+
const bound = r.body.tokens.find((t: any) => t.token_id === node.nN.tokenId);
|
|
613
|
+
expect(bound.bound_node_id).toBe(node.nN.id);
|
|
614
|
+
expect(bound.unbound_node).toBe(false);
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
test("成员(非 owner/admin)看不到;节点令牌也看不到", async () => {
|
|
618
|
+
const r = await call(user.mem.token, "GET", `/api/networks/${net}/token-ledger`);
|
|
619
|
+
expect(r.status).toBe(403);
|
|
620
|
+
const n = await call(node.nN.token, "GET", `/api/networks/${net}/token-ledger`);
|
|
621
|
+
expect(n.status).toBe(403);
|
|
622
|
+
});
|
|
623
|
+
});
|
|
624
|
+
|
|
584
625
|
// #2969 续 —— REST 写接口白名单:唯一 choke point。
|
|
585
626
|
//
|
|
586
627
|
// 为什么必须在中央判一次:节点令牌的 REST 写面分散在两套判据里
|
package/src/server.ts
CHANGED
|
@@ -3193,6 +3193,87 @@ return Bun.serve({
|
|
|
3193
3193
|
}));
|
|
3194
3194
|
}
|
|
3195
3195
|
|
|
3196
|
+
// ── #2719 —— 凭据台账:哪把钥匙、绑没绑、最后一次用是什么时候、用它调过什么 ──
|
|
3197
|
+
//
|
|
3198
|
+
// 为什么不能只用 node-permission-report:那张报表是**按流量**聚合的,
|
|
3199
|
+
// 只列出「近 N 天有调用」的身份。生产实测 760 个未吊销令牌里有 381 个是
|
|
3200
|
+
// `node:` 开头但没绑定节点的,而其中只有 4 个在近 24h 出现过。
|
|
3201
|
+
// 剩下 377 个**在任何报表里都不存在** —— 于是「这把钥匙还有人在用吗」
|
|
3202
|
+
// 这个问题永远答不清,也就永远不敢吊销。
|
|
3203
|
+
//
|
|
3204
|
+
// 这里只读、只列元信息(不返回 token 值,Hub 本来也只存 hash),
|
|
3205
|
+
// 且要求「人的令牌 + 网络 owner/admin」—— 节点不能枚举网络的钥匙。
|
|
3206
|
+
const ledgerMatch = url.pathname.match(/^\/api\/networks\/([^/]+)\/token-ledger$/);
|
|
3207
|
+
if (ledgerMatch && req.method === "GET") {
|
|
3208
|
+
if (!restAuth) return withCors(req, Response.json({ ok: false, error: "auth required" }, { status: 401 }));
|
|
3209
|
+
if (restAuth.networkId) return userTokenRequired(req);
|
|
3210
|
+
const networkId = decodeURIComponent(ledgerMatch[1]);
|
|
3211
|
+
const role = getUserNetworkRole(restAuth.userId, networkId);
|
|
3212
|
+
if (!isAdmin && role !== "owner" && role !== "admin") {
|
|
3213
|
+
return withCors(req, Response.json({ ok: false, error: "permission_denied", message: "network owner/admin only" }, { status: 403 }));
|
|
3214
|
+
}
|
|
3215
|
+
const limit = Math.min(Math.max(Number(url.searchParams.get("limit") ?? 200) || 200, 1), 1000);
|
|
3216
|
+
const rows = db.all<{ token_id: string; name: string | null; bound_node_id: string | null; scope: string | null; created_at: string | null; last_used_at: string | null }>(
|
|
3217
|
+
`SELECT token_id, name, bound_node_id, scope, created_at, last_used_at
|
|
3218
|
+
FROM api_tokens
|
|
3219
|
+
WHERE network_id = ?1 AND revoked_at IS NULL
|
|
3220
|
+
ORDER BY name, created_at DESC LIMIT ?2`,
|
|
3221
|
+
networkId, limit,
|
|
3222
|
+
);
|
|
3223
|
+
// 近 24h 的调用面(按账本键 `token:<id>`)。没有这一列,台账只是一份名单;
|
|
3224
|
+
// 有了它才能回答「这条要不要吊销」。
|
|
3225
|
+
const sinceHour = new Date(Date.now() - 86_400_000).toISOString().slice(0, 13);
|
|
3226
|
+
const calls = db.all<{ node_id: string; tool: string; hits: number | string }>(
|
|
3227
|
+
`SELECT node_id, tool, SUM(hits) AS hits FROM node_tool_calls
|
|
3228
|
+
WHERE network_id = ?1 AND hour >= ?2 AND node_id LIKE 'token:%'
|
|
3229
|
+
GROUP BY node_id, tool ORDER BY SUM(hits) DESC`,
|
|
3230
|
+
networkId, sinceHour,
|
|
3231
|
+
);
|
|
3232
|
+
const byToken = new Map<string, Array<{ tool: string; hits: number }>>();
|
|
3233
|
+
for (const c of calls) {
|
|
3234
|
+
const id = c.node_id.slice(6);
|
|
3235
|
+
const list = byToken.get(id) ?? [];
|
|
3236
|
+
list.push({ tool: c.tool, hits: Number(c.hits) });
|
|
3237
|
+
byToken.set(id, list);
|
|
3238
|
+
}
|
|
3239
|
+
const tokens = rows.map((r) => {
|
|
3240
|
+
const tools = byToken.get(r.token_id) ?? [];
|
|
3241
|
+
const writes = tools.filter((t) => t.tool.startsWith("mcp:send_") || t.tool.startsWith("mcp:requirements_")
|
|
3242
|
+
|| t.tool.startsWith("mcp:projects_") || ["POST", "PUT", "PATCH", "DELETE"].includes(t.tool.split(" ")[0]))
|
|
3243
|
+
.reduce((n, t) => n + t.hits, 0);
|
|
3244
|
+
return {
|
|
3245
|
+
token_id: r.token_id,
|
|
3246
|
+
name: r.name,
|
|
3247
|
+
scope: r.scope,
|
|
3248
|
+
bound_node_id: r.bound_node_id,
|
|
3249
|
+
// 名字说自己是节点、却没有绑定 ⇒ 三张白名单都管不到它(nodeIdentity 会退化成 token: 键)
|
|
3250
|
+
looks_like_node: !!r.name && r.name.startsWith("node:"),
|
|
3251
|
+
unbound_node: !!r.name && r.name.startsWith("node:") && !r.bound_node_id,
|
|
3252
|
+
created_at: r.created_at,
|
|
3253
|
+
last_used_at: r.last_used_at,
|
|
3254
|
+
calls_24h: tools.reduce((n, t) => n + t.hits, 0),
|
|
3255
|
+
writes_24h: writes,
|
|
3256
|
+
top_tools: tools.slice(0, 3),
|
|
3257
|
+
};
|
|
3258
|
+
});
|
|
3259
|
+
const unbound = tokens.filter((t) => t.unbound_node);
|
|
3260
|
+
return withCors(req, Response.json({
|
|
3261
|
+
ok: true,
|
|
3262
|
+
network_id: networkId,
|
|
3263
|
+
since: new Date(Date.now() - 86_400_000).toISOString(),
|
|
3264
|
+
limit,
|
|
3265
|
+
summary: {
|
|
3266
|
+
total: tokens.length,
|
|
3267
|
+
bound: tokens.filter((t) => !!t.bound_node_id).length,
|
|
3268
|
+
unbound_node: unbound.length,
|
|
3269
|
+
// 「名义上还在、实际上没人用」的数量:这才是能不能吊销的判据
|
|
3270
|
+
unused_24h: tokens.filter((t) => t.calls_24h === 0).length,
|
|
3271
|
+
unbound_node_in_use: unbound.filter((t) => t.calls_24h > 0).length,
|
|
3272
|
+
},
|
|
3273
|
+
tokens,
|
|
3274
|
+
}));
|
|
3275
|
+
}
|
|
3276
|
+
|
|
3196
3277
|
// ── #1473 (#909): read-only Hub catalogs for the desktop client ──
|
|
3197
3278
|
//
|
|
3198
3279
|
// Skills / network secrets / Providers have existed on this Hub only as
|
|
@@ -150,3 +150,40 @@ test("nonterminal legacy reply retains its established behavior", async () => {
|
|
|
150
150
|
expect(db.get("SELECT status,result FROM tasks WHERE task_id=?1", openId)).toEqual({ status: "replied", result: "ordinary final" });
|
|
151
151
|
expect(db.get("SELECT COUNT(*) AS n FROM task_late_replies WHERE task_id=?1", openId).n).toBe(0);
|
|
152
152
|
});
|
|
153
|
+
|
|
154
|
+
// #3061 —— 任务**从未记下 turn 上下文**时,迟到结果也必须能收。
|
|
155
|
+
//
|
|
156
|
+
// 现场(2026-10-30 生产实测):
|
|
157
|
+
// 「仍在 TUI 会话里运行,等待回复已达 30 分钟上限」的失败任务 289 条,
|
|
158
|
+
// 其中 thread_id/turn_id 非空的 **0 条**;「任务未提交」781 条里只有 1 条有。
|
|
159
|
+
// ⇒ 这类任务的迟到结果永远匹配不上上下文,等于「活干完了但结果必定丢弃」。
|
|
160
|
+
// 全库 task_late_replies 只有 4 行,正是这条路走不通的证据。
|
|
161
|
+
test("既有任务没有 turn 上下文时,迟到结果仍可落账并投递给发送方", async () => {
|
|
162
|
+
const contextlessId = "task_late710_noctx";
|
|
163
|
+
db.run(`INSERT INTO tasks
|
|
164
|
+
(task_id,from_name,to_name,to_node_id,status,content,result,network_id,thread_id,turn_id,completed_at)
|
|
165
|
+
VALUES(?1,?2,'late710-worker',?3,'failed','never started a turn','共享会话忙,30 分钟内未提交',?4,NULL,NULL,datetime('now'))`,
|
|
166
|
+
[contextlessId, owner.user.username, workerId, owner.network_id]);
|
|
167
|
+
|
|
168
|
+
const accepted = await mcp(workerToken, "send_reply", {
|
|
169
|
+
in_reply_to: contextlessId,
|
|
170
|
+
text: "结果其实跑完了",
|
|
171
|
+
status: "replied",
|
|
172
|
+
thread_id: "thread_from_runtime_any",
|
|
173
|
+
turn_id: "turn_from_runtime_any",
|
|
174
|
+
});
|
|
175
|
+
expect(accepted).toMatchObject({ ok: true, late: true, duplicate: false });
|
|
176
|
+
// 任务行不被改写
|
|
177
|
+
expect(db.get("SELECT status,result FROM tasks WHERE task_id=?1", contextlessId))
|
|
178
|
+
.toEqual({ status: "failed", result: "共享会话忙,30 分钟内未提交" });
|
|
179
|
+
// 落进 append-only 账,并带上运行时给的上下文
|
|
180
|
+
const row = db.get("SELECT thread_id,turn_id,status,result FROM task_late_replies WHERE task_id=?1", contextlessId);
|
|
181
|
+
expect(row).toMatchObject({ thread_id: "thread_from_runtime_any", turn_id: "turn_from_runtime_any", status: "replied", result: "结果其实跑完了" });
|
|
182
|
+
// 发送方能在 inbox 里看到它(否则「活干完了没人知道」)
|
|
183
|
+
const inbox = await mcp(owner.token, "get_inbox", { alias: owner.user.username, limit: 100 });
|
|
184
|
+
expect(inbox.messages).toContainEqual(expect.objectContaining({ id: accepted.message_id, late: true, content: "结果其实跑完了" }));
|
|
185
|
+
// 不是这个任务的负责人,仍然拒(放宽只针对上下文,不放宽归属)
|
|
186
|
+
expect(await mcp(siblingToken, "send_reply", {
|
|
187
|
+
in_reply_to: contextlessId, text: "impostor", thread_id: "t", turn_id: "u",
|
|
188
|
+
})).toMatchObject({ ok: false, error: "late_reply_task_not_owned" });
|
|
189
|
+
});
|
package/src/tools.ts
CHANGED
|
@@ -137,6 +137,21 @@ function lateReplyContextMatches(
|
|
|
137
137
|
threadId: string,
|
|
138
138
|
turnId: string,
|
|
139
139
|
): boolean {
|
|
140
|
+
// #3061 —— 任务**从未记下 turn 上下文**时,不能用「上下文不符」把它挡回去。
|
|
141
|
+
//
|
|
142
|
+
// 现场(2026-10-30 生产实测):
|
|
143
|
+
// 「仍在 TUI 会话里运行,等待回复已达 30 分钟上限」的失败任务 289 条,
|
|
144
|
+
// 其中 thread_id/turn_id **非空的 0 条**;「任务未提交」781 条里只有 1 条有。
|
|
145
|
+
// ⇒ 这类任务的迟到结果永远匹配不上,等于**工作做完了但结果必定丢弃**。
|
|
146
|
+
// 全库 task_late_replies 只有 4 行,正是这条路走不通的证据。
|
|
147
|
+
//
|
|
148
|
+
// 放宽的边界(三条同时满足才放行):
|
|
149
|
+
// · 存的两个 id 都是 null(不是「存了 A 却来了 B」——那种仍然拒)
|
|
150
|
+
// · 任务已经是终态(调用点在别处已判)
|
|
151
|
+
// · 令牌就是该任务的负责人(late_reply_task_not_owned 的检查在调用点,未改动)
|
|
152
|
+
// 在这种情况下,迟到结果只会写进 append-only 的 task_late_replies,并投递给原发送方;
|
|
153
|
+
// 不改写任务行、不跑父任务回链,所以不会把已完成的任务「再改一遍」。
|
|
154
|
+
if (storedThreadId === null && storedTurnId === null) return true;
|
|
140
155
|
return storedThreadId === threadId && storedTurnId === turnId;
|
|
141
156
|
}
|
|
142
157
|
|