@sleep2agi/commhub-server 0.9.0-preview.191 → 0.9.0-preview.192
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.
|
|
3
|
+
"version": "0.9.0-preview.192",
|
|
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",
|
|
@@ -50,6 +50,25 @@ describe("节点忙熔断:三项判据", () => {
|
|
|
50
50
|
expect(r.enabled).toBe(false); // 默认关闭
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
+
// 熔断后不能「永不投递」:否则节点拿不到成功记录 ⇒ 永远解不开(死锁)。
|
|
54
|
+
test("熔断期间按探针间隔放行一条(自愈通道)", () => {
|
|
55
|
+
process.env.COMMHUB_NODE_BUSY_CIRCUIT = "1";
|
|
56
|
+
// 黑洞节点:刚投递过 ⇒ 拦
|
|
57
|
+
db.run(`INSERT INTO tasks (task_id, content, from_name, to_name, network_id, status, created_at)
|
|
58
|
+
VALUES ('t_probe_now','x','hub','黑洞节点',?1,'failed',datetime('now'))`, [NET]);
|
|
59
|
+
const blocked = nodeCanAcceptDispatch("黑洞节点", NET);
|
|
60
|
+
expect(blocked.ok).toBe(false);
|
|
61
|
+
expect(blocked.probe).toBeUndefined();
|
|
62
|
+
// 把最近投递推到 2 小时前(默认探针间隔 1h)⇒ 放行,且标记 probe
|
|
63
|
+
// 注意:前面的用例也给这个节点插过 created_at=now 的任务,MAX(created_at) 会取到它们
|
|
64
|
+
db.run("UPDATE tasks SET created_at = datetime('now','-2 hours') WHERE to_name = '黑洞节点'");
|
|
65
|
+
const probe = nodeCanAcceptDispatch("黑洞节点", NET);
|
|
66
|
+
expect(probe.ok).toBe(true);
|
|
67
|
+
expect(probe.probe).toBe(true);
|
|
68
|
+
// 还原,避免影响后续用例(它们依赖「最近刚投递过」这个前态)
|
|
69
|
+
db.run("UPDATE tasks SET created_at = datetime('now') WHERE to_name = '黑洞节点'");
|
|
70
|
+
});
|
|
71
|
+
|
|
53
72
|
test("默认关闭时投递判断恒放行(不改任何既有行为)", () => {
|
|
54
73
|
delete process.env.COMMHUB_NODE_BUSY_CIRCUIT;
|
|
55
74
|
expect(circuitEnabled()).toBe(false);
|
|
@@ -31,6 +31,28 @@ export function circuitEnabled(): boolean {
|
|
|
31
31
|
}
|
|
32
32
|
export function circuitWindowHours(): number { return envInt("COMMHUB_NODE_BUSY_CIRCUIT_HOURS", 6, 1); }
|
|
33
33
|
export function circuitMinFails(): number { return envInt("COMMHUB_NODE_BUSY_CIRCUIT_FAILS", 5, 1); }
|
|
34
|
+
/**
|
|
35
|
+
* 熔断期间**放探针**的间隔(小时)。
|
|
36
|
+
*
|
|
37
|
+
* 🔴 为什么必须有:熔断后完全不再投递 ⇒ 节点永远拿不到成功记录 ⇒ 永远解不开(死锁)。
|
|
38
|
+
* 所以每 N 小时放一条探针任务:它若成功,replied 计数不再为 0,熔断自动解除。
|
|
39
|
+
*/
|
|
40
|
+
export function circuitProbeHours(): number { return envInt("COMMHUB_NODE_BUSY_CIRCUIT_PROBE_HOURS", 1, 1); }
|
|
41
|
+
|
|
42
|
+
/** 该节点最近一次**被投递**的时刻(ms)。没有投递过返回 null。 */
|
|
43
|
+
function lastDeliveryMs(alias: string, networkId: string | null): number | null {
|
|
44
|
+
try {
|
|
45
|
+
const row = db.get<{ t: string | null }>(
|
|
46
|
+
"SELECT MAX(created_at) AS t FROM tasks WHERE to_name = ?1 AND (?2 IS NULL OR network_id = ?2)",
|
|
47
|
+
alias, networkId,
|
|
48
|
+
);
|
|
49
|
+
if (!row?.t) return null;
|
|
50
|
+
const ms = Date.parse(String(row.t).replace(" ", "T") + "Z");
|
|
51
|
+
return Number.isFinite(ms) ? ms : null;
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
34
56
|
|
|
35
57
|
export type NodeServingStats = { alias: string; network_id: string | null; delivered: number; replied: number; busy_failed: number };
|
|
36
58
|
|
|
@@ -69,7 +91,7 @@ export function scanNodeBusyCircuit(now = new Date()): CircuitScanResult {
|
|
|
69
91
|
* 这个节点现在能不能投递。默认永远 true(未开启 = 不拦截)。
|
|
70
92
|
* 供排程派发判断;纯读,不改任何状态。
|
|
71
93
|
*/
|
|
72
|
-
export function nodeCanAcceptDispatch(alias: string, networkId: string | null): { ok: boolean; reason?: string; busy_failed?: number } {
|
|
94
|
+
export function nodeCanAcceptDispatch(alias: string, networkId: string | null): { ok: boolean; reason?: string; busy_failed?: number; probe?: boolean } {
|
|
73
95
|
if (!circuitEnabled()) return { ok: true };
|
|
74
96
|
const windowHours = circuitWindowHours();
|
|
75
97
|
const since = new Date(Date.now() - windowHours * 3_600_000).toISOString().slice(0, 19).replace("T", " ");
|
|
@@ -88,6 +110,12 @@ export function nodeCanAcceptDispatch(alias: string, networkId: string | null):
|
|
|
88
110
|
if (!row) return { ok: true };
|
|
89
111
|
const stats = { delivered: Number(row.delivered ?? 0), replied: Number(row.replied ?? 0), busy_failed: Number(row.busy_failed ?? 0) };
|
|
90
112
|
if (!isNotServing(stats)) return { ok: true };
|
|
113
|
+
// 熔断期间每 N 小时放一条探针,否则节点永远拿不到成功记录、永远解不开。
|
|
114
|
+
const probeHours = circuitProbeHours();
|
|
115
|
+
const last = lastDeliveryMs(alias, networkId);
|
|
116
|
+
if (last === null || Date.now() - last >= probeHours * 3_600_000) {
|
|
117
|
+
return { ok: true, probe: true, busy_failed: stats.busy_failed };
|
|
118
|
+
}
|
|
91
119
|
return {
|
|
92
120
|
ok: false,
|
|
93
121
|
reason: `节点近 ${windowHours}h 内 ${stats.busy_failed} 次忙类失败、0 次成功(投递也只会白等),已跳过`,
|
package/src/scheduled-tasks.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { sessionTaskOnDispatch } from "./session-task-on-dispatch.js";
|
|
|
6
6
|
import { TASK_CONTENT_MAX, sessionTaskPreview } from "./shared/task-content-limit.js";
|
|
7
7
|
import { assertNodeActive } from "./lifecycle-guard.js";
|
|
8
8
|
import { assertNodeHealthy } from "./node-health-guard.js";
|
|
9
|
+
import { nodeCanAcceptDispatch } from "./node-serviceability.js";
|
|
9
10
|
import { addNetworkScope, canRestWriteNetwork, singleNetworkId, type RestNetworkScope } from "./network-scope.js";
|
|
10
11
|
import { canMessageAgent, type AgentRef } from "./agent-access.js";
|
|
11
12
|
import { dispatchVerdict } from "./node-permissions.js";
|
|
@@ -587,6 +588,24 @@ export function dispatchScheduledOccurrence(row: ScheduledRow, scheduledFor: str
|
|
|
587
588
|
return;
|
|
588
589
|
}
|
|
589
590
|
|
|
591
|
+
// #2880 —— 节点级忙熔断(默认关闭;COMMHUB_NODE_BUSY_CIRCUIT=1 才生效)。
|
|
592
|
+
// 与 lifecycle/health 的区别:节点没坏、也没停,只是最近一直没有产出(忙类失败多且 0 成功),
|
|
593
|
+
// 投进去必然白等 ~30 分钟。所以记 **skipped** 而不是 failed —— 这不是故障,是「现在别投」。
|
|
594
|
+
// 熔断期间每 COMMHUB_NODE_BUSY_CIRCUIT_PROBE_HOURS 小时会放一条探针(见 node-serviceability.ts),
|
|
595
|
+
// 避免「不投递 ⇒ 拿不到成功 ⇒ 永远解不开」的死锁。
|
|
596
|
+
{
|
|
597
|
+
const circuit = nodeCanAcceptDispatch(node.alias, row.network_id);
|
|
598
|
+
if (!circuit.ok) {
|
|
599
|
+
finalStatus = "skipped";
|
|
600
|
+
db.run(
|
|
601
|
+
"UPDATE scheduled_task_runs SET status = 'skipped', error_code = 'target_busy_no_output', error_message = ?1, completed_at = datetime('now') WHERE run_id = ?2",
|
|
602
|
+
[String(circuit.reason ?? "target_busy_no_output").slice(0, 500), runId],
|
|
603
|
+
);
|
|
604
|
+
if (advanceSchedule) advance(row, scheduledFor, advanceAfter);
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
590
609
|
// #460 — the target reports a degraded layer (App Server down / TUI gone / login revoked): the run fails
|
|
591
610
|
// with that reason instead of queueing a task nobody will execute. Unknown / stale health runs as before.
|
|
592
611
|
const health = assertNodeHealthy(node.alias, row.network_id, { forceAllowed: false });
|