@sleep2agi/commhub-server 0.9.0-preview.159 → 0.9.0-preview.161
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/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/server.ts +27 -4
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.ts +44 -17
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.161",
|
|
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,92 @@
|
|
|
1
|
+
// #2363 —— MCP 在飞计数的单测 + 源码断言。
|
|
2
|
+
//
|
|
3
|
+
// 计数值本身很小,但它是“RSS 尖峰能不能被解释”的唯一在线指标:`sse_connections`
|
|
4
|
+
// 只反映长连接,看不到同秒打进来的一批短请求。这里钉住三件事:
|
|
5
|
+
// 1. enter/leave 必须配平(含重复 leave 不把计数打成负数);
|
|
6
|
+
// 2. 峰值要能记住高水位,并能被显式重置;
|
|
7
|
+
// 3. server.ts 必须在**任何 await 之前**进入计数,并且在“正常关闭 / 抛异常”
|
|
8
|
+
// 两条路径上都退出计数——少一条就会让指标永远偏高,比没有指标更糟。
|
|
9
|
+
|
|
10
|
+
import { describe, expect, test, beforeEach } from "bun:test";
|
|
11
|
+
import { readFileSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import {
|
|
14
|
+
enterMcpInflight,
|
|
15
|
+
leaveMcpInflight,
|
|
16
|
+
getMcpInflightStats,
|
|
17
|
+
resetMcpInflightPeak,
|
|
18
|
+
} from "./mcp-inflight.js";
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
// 模块级单例:每个用例从 0 开始,避免用例顺序影响。
|
|
22
|
+
while (getMcpInflightStats().inflight > 0) leaveMcpInflight();
|
|
23
|
+
resetMcpInflightPeak();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("#2363 mcp inflight counter", () => {
|
|
27
|
+
test("enter/leave 配平,peak 记住高水位且不自己回落", () => {
|
|
28
|
+
expect(getMcpInflightStats()).toEqual({ inflight: 0, peak: 0 });
|
|
29
|
+
enterMcpInflight();
|
|
30
|
+
enterMcpInflight();
|
|
31
|
+
enterMcpInflight();
|
|
32
|
+
expect(getMcpInflightStats()).toEqual({ inflight: 3, peak: 3 });
|
|
33
|
+
leaveMcpInflight();
|
|
34
|
+
leaveMcpInflight();
|
|
35
|
+
// 当前只有 1 条在飞,但峰值仍是 3 —— 峰值是“撞过悬崖没有”的证据。
|
|
36
|
+
expect(getMcpInflightStats()).toEqual({ inflight: 1, peak: 3 });
|
|
37
|
+
leaveMcpInflight();
|
|
38
|
+
expect(getMcpInflightStats()).toEqual({ inflight: 0, peak: 3 });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("多余的 leave 不会把计数打成负数", () => {
|
|
42
|
+
leaveMcpInflight();
|
|
43
|
+
leaveMcpInflight();
|
|
44
|
+
expect(getMcpInflightStats().inflight).toBe(0);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("reset 把峰值对齐到当前在飞数(不会把峰值压到低于现况)", () => {
|
|
48
|
+
enterMcpInflight();
|
|
49
|
+
enterMcpInflight();
|
|
50
|
+
expect(getMcpInflightStats().peak).toBe(2);
|
|
51
|
+
leaveMcpInflight();
|
|
52
|
+
resetMcpInflightPeak();
|
|
53
|
+
expect(getMcpInflightStats()).toEqual({ inflight: 1, peak: 1 });
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("#2363 server wiring (source assertions)", () => {
|
|
58
|
+
const src = readFileSync(join(import.meta.dir, "server.ts"), "utf8");
|
|
59
|
+
|
|
60
|
+
test("进入计数发生在创建 transport 之前(请求体解析阶段也要计入)", () => {
|
|
61
|
+
const enterAt = src.indexOf("enterMcpInflight();");
|
|
62
|
+
const transportAt = src.indexOf("new WebStandardStreamableHTTPServerTransport({", enterAt);
|
|
63
|
+
expect(enterAt).toBeGreaterThan(0);
|
|
64
|
+
expect(transportAt).toBeGreaterThan(enterAt);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("两条退出路径都在:handleRequest 抛异常时、以及 MCP server 关闭后", () => {
|
|
68
|
+
// 抛异常路径
|
|
69
|
+
expect(src).toContain("settleMcpInflight();\n throw e;");
|
|
70
|
+
// 正常路径(setImmediate 里 close 完成后结算,且重复调用安全)
|
|
71
|
+
expect(src).toContain("mcpServer.close().catch(() => {}).finally(settleMcpInflight)");
|
|
72
|
+
// 幂等闸门:只允许真正减一次
|
|
73
|
+
expect(src).toContain("if (mcpInflightSettled) return;");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("/health 暴露的是聚合值(匿名也安全),且没有 new 的按会话明细", () => {
|
|
77
|
+
expect(src).toContain("mcp_inflight: getMcpInflightStats().inflight,");
|
|
78
|
+
expect(src).toContain("mcp_inflight_peak: getMcpInflightStats().peak,");
|
|
79
|
+
expect(src).not.toContain("mcp_inflight_sessions");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("这一版只观测:/mcp 入口不得出现 429 / 拒绝分支", () => {
|
|
83
|
+
const mcpStart = src.indexOf('if (url.pathname === "/mcp") {');
|
|
84
|
+
const mcpEnd = src.indexOf("// ── #461 SSE network observer stream ──", mcpStart);
|
|
85
|
+
expect(mcpStart).toBeGreaterThan(0);
|
|
86
|
+
expect(mcpEnd).toBeGreaterThan(mcpStart);
|
|
87
|
+
const block = src.slice(mcpStart, mcpEnd);
|
|
88
|
+
expect(block).not.toContain("429");
|
|
89
|
+
expect(block).not.toContain("hub_busy");
|
|
90
|
+
expect(block).not.toContain("Retry-After");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// #2363 —— MCP 在飞计数(先只观测,不拦请求)。
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要它:隔离 Hub 上用**生产库副本**实测,RSS 与“同时在飞的 MCP 请求数”
|
|
4
|
+
// 成正比(≈1.2–1.4 MB/条;`send_message` 与 `get_inbox` 同形),而与吞吐无关——
|
|
5
|
+
// 顺序发 3,255 条/60s(≈54 条/秒)时 RSS 稳定在 151–156 MB,而 50/100/200 并发
|
|
6
|
+
// 的峰值是 249/446/689 MB。`/health` 里的 `sse_connections` 只反映长连接,看不到
|
|
7
|
+
// 这个量,所以生产上“为什么 RSS 突然抬到 450 MB”一直只能事后猜。
|
|
8
|
+
//
|
|
9
|
+
// 本模块只做计数与峰值,**不拒绝、不排队、不改任何响应**——限流(有界队列 + 429)
|
|
10
|
+
// 是另一步,需要先有一天的真实分布再定上限。见 docs/runbooks/hub-memory-and-sse.md。
|
|
11
|
+
|
|
12
|
+
let inflight = 0;
|
|
13
|
+
let peak = 0;
|
|
14
|
+
|
|
15
|
+
/** /mcp 进入时调用(在任何 await 之前,保证计入请求体解析阶段)。 */
|
|
16
|
+
export function enterMcpInflight(): void {
|
|
17
|
+
inflight += 1;
|
|
18
|
+
if (inflight > peak) peak = inflight;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** /mcp 结束时调用(响应对象产出且 MCP server 已关闭);重复调用安全。 */
|
|
22
|
+
export function leaveMcpInflight(): void {
|
|
23
|
+
if (inflight > 0) inflight -= 1;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 当前在飞数与**自上次 reset 以来的峰值**。
|
|
28
|
+
* 峰值是“滚动高水位”,不会自己回落——运维看它判断是否撞到过悬崖,
|
|
29
|
+
* 看 `inflight` 判断现在是否还在里面。
|
|
30
|
+
*/
|
|
31
|
+
export function getMcpInflightStats(): { inflight: number; peak: number } {
|
|
32
|
+
return { inflight, peak };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 把峰值重置为当前在飞数(运维定位完一次尖峰后调用,或测试用)。 */
|
|
36
|
+
export function resetMcpInflightPeak(): void {
|
|
37
|
+
peak = inflight;
|
|
38
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -28,6 +28,7 @@ import { ensureNetworkRulesOnce, readNetworkRules, writeNetworkRules } from "./n
|
|
|
28
28
|
import { deriveTeamRulesView } from "./team-rules-status.js";
|
|
29
29
|
import { recentNodeIdentityConflicts } from "./node-identity-conflict.js";
|
|
30
30
|
import { createSSEStream, createNetworkObserverStream, createUserEventStream, pushEvent, pushNetworkObserverEvent, getSSEStats, PRINTABLE_OBSERVER_KEY_PREFIX, closeUserStreamsInNetwork, getUserPresence, onUserPresenceChange, pushUserEventToNetwork } from "./push.js";
|
|
31
|
+
import { enterMcpInflight, leaveMcpInflight, getMcpInflightStats } from "./mcp-inflight.js";
|
|
31
32
|
import { assertNodeActive } from "./lifecycle-guard.js";
|
|
32
33
|
import { pendingInboxCount } from "./inbox-count.js";
|
|
33
34
|
import { notifyExpiredTasks, type ExpiredTaskRow } from "./task-expiry-notice.js";
|
|
@@ -1005,10 +1006,12 @@ export function patrolOrphanTasks(): void {
|
|
|
1005
1006
|
}
|
|
1006
1007
|
|
|
1007
1008
|
function patrolDeliveredStaleTasks(): void {
|
|
1009
|
+
// #2571 —— watcher 现在按“目标节点此刻是否 working”分流写两类事件,但仍然是**同步**
|
|
1010
|
+
// (db.run 是同步的):巡逻旁路绝不因为 await 打乱调度时序。
|
|
1008
1011
|
try {
|
|
1009
1012
|
const result = recordDeliveredStaleEvents();
|
|
1010
1013
|
if (result.inserted > 0) {
|
|
1011
|
-
console.warn(`[patrol] recorded ${result.inserted} delivered-stale task warning(s)`);
|
|
1014
|
+
console.warn(`[patrol] recorded ${result.inserted} delivered-stale/queued-behind-busy task warning(s)`);
|
|
1012
1015
|
}
|
|
1013
1016
|
} catch (error: any) {
|
|
1014
1017
|
console.error(`[patrol] delivered-stale watcher failed: ${error?.message || error}`);
|
|
@@ -1092,6 +1095,16 @@ return Bun.serve({
|
|
|
1092
1095
|
callerIdentityRejected = true;
|
|
1093
1096
|
}
|
|
1094
1097
|
}
|
|
1098
|
+
// #2363 —— 在飞计数:从进入 /mcp 到 MCP server 关闭。Hub 的内存与“同时在飞的
|
|
1099
|
+
// 请求数”成正比(隔离实测 ≈1.2–1.4 MB/条;顺序 54 条/秒时 RSS 完全平稳),
|
|
1100
|
+
// 所以这个数比 req/s 更能解释 RSS 尖峰。只观测,不拦请求。
|
|
1101
|
+
enterMcpInflight();
|
|
1102
|
+
let mcpInflightSettled = false;
|
|
1103
|
+
const settleMcpInflight = (): void => {
|
|
1104
|
+
if (mcpInflightSettled) return;
|
|
1105
|
+
mcpInflightSettled = true;
|
|
1106
|
+
leaveMcpInflight();
|
|
1107
|
+
};
|
|
1095
1108
|
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
1096
1109
|
sessionIdGenerator: undefined,
|
|
1097
1110
|
});
|
|
@@ -1110,10 +1123,16 @@ return Bun.serve({
|
|
|
1110
1123
|
}
|
|
1111
1124
|
// #478:tools/list 按调用者过滤;X-Anet-Tools: all / ?tools=all 时节点令牌也列出协议工具。
|
|
1112
1125
|
const mcpServer = createServer(clientIP, enforceNetId, authCtx?.userId || null, callerAlias, !!token?.startsWith("ntok_"), authCtx?.tokenId || null, wantsAllTools(req, url), callerIdentityRejected);
|
|
1113
|
-
|
|
1114
|
-
|
|
1126
|
+
let response: Response;
|
|
1127
|
+
try {
|
|
1128
|
+
await mcpServer.connect(transport);
|
|
1129
|
+
response = await transport.handleRequest(req, mcpParsedBody === undefined ? undefined : { parsedBody: mcpParsedBody });
|
|
1130
|
+
} catch (e) {
|
|
1131
|
+
settleMcpInflight();
|
|
1132
|
+
throw e;
|
|
1133
|
+
}
|
|
1115
1134
|
// Disconnect after response to prevent McpServer leak
|
|
1116
|
-
setImmediate(() => mcpServer.close().catch(() => {}));
|
|
1135
|
+
setImmediate(() => { mcpServer.close().catch(() => {}).finally(settleMcpInflight); });
|
|
1117
1136
|
// #426: the MCP SDK's WebStandardStreamableHTTPServerTransport builds
|
|
1118
1137
|
// its own Response with `Content-Type: text/event-stream` (or
|
|
1119
1138
|
// `application/json` for JSON-mode) but WITHOUT charset. Legacy
|
|
@@ -2765,6 +2784,10 @@ return Bun.serve({
|
|
|
2765
2784
|
transport: "streamable-http",
|
|
2766
2785
|
sessions_count: count?.cnt ?? 0,
|
|
2767
2786
|
sse_connections: sse.total,
|
|
2787
|
+
// #2363 —— MCP 在飞数(只观测)。RSS 与它成正比(≈1.2–1.4 MB/条),
|
|
2788
|
+
// 而 `sse_connections` 只反映长连接,看不到“同秒打进来的一批请求”。
|
|
2789
|
+
mcp_inflight: getMcpInflightStats().inflight,
|
|
2790
|
+
mcp_inflight_peak: getMcpInflightStats().peak,
|
|
2768
2791
|
auth: DEV_OPEN ? "dev-open" : "user-token",
|
|
2769
2792
|
security: DEV_OPEN ? "dev-open" : "secured",
|
|
2770
2793
|
tmux: TMUX_ENABLED ? "enabled" : "disabled",
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// #2571 —— delivered_stale 的**分流**:目标在跑别的任务时不再报“可能离线”。
|
|
2
|
+
//
|
|
3
|
+
// 生产数据(24h):出现过 delivered_stale 的任务里 83% 最终成功,等待 p50 2.9 分钟、
|
|
4
|
+
// p90 27 分钟 ⇒ 事件事实成立,但把“目标正忙”写成“可能离线”会制造告警疲劳。
|
|
5
|
+
// 这里钉住三件事:
|
|
6
|
+
// 1. 目标 `working` ⇒ 写 `queued_behind_busy_<N>s`,**不写** `delivered_stale_<N>s`;
|
|
7
|
+
// 2. 目标 `idle`/`offline`/无 session ⇒ 写 `delivered_stale_<N>s`,**不写** busy 类;
|
|
8
|
+
// 3. 两类都仍然是 write-once(同一 task+阈值重复巡逻不重复写)。
|
|
9
|
+
|
|
10
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
11
|
+
import { db } from "./db.js";
|
|
12
|
+
import { recordDeliveredStaleEvents } from "./task-lifecycle-watcher.js";
|
|
13
|
+
|
|
14
|
+
const NOW = new Date("2026-08-10T00:01:01.000Z");
|
|
15
|
+
let networkId = "";
|
|
16
|
+
|
|
17
|
+
function sqliteUtcTimestamp(date: Date): string {
|
|
18
|
+
return date.toISOString().slice(0, 19).replace("T", " ");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function insertTask(taskId: string, toName: string, deliveredAt: string) {
|
|
22
|
+
db.run(
|
|
23
|
+
`INSERT INTO tasks (task_id, from_name, to_name, status, content, delivered_at, network_id)
|
|
24
|
+
VALUES (?1, 'sender', ?2, 'delivered', 'busy-vs-stale fixture', ?3, ?4)`,
|
|
25
|
+
[taskId, toName, deliveredAt, networkId],
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function insertSession(alias: string, status: string) {
|
|
30
|
+
db.run(
|
|
31
|
+
`INSERT INTO sessions (resume_id, alias, status, network_id, registered_at)
|
|
32
|
+
VALUES (?1, ?2, ?3, ?4, '2026-08-10 00:00:00')`,
|
|
33
|
+
[`res_${alias}`, alias, status, networkId],
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function eventsFor(taskId: string): string[] {
|
|
38
|
+
return db
|
|
39
|
+
.all<{ event_type: string }>(`SELECT event_type FROM task_events WHERE task_id = ?1 ORDER BY event_type`, [taskId])
|
|
40
|
+
.map((row) => row.event_type);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
beforeAll(async () => {
|
|
44
|
+
const { register } = await import("./auth.js");
|
|
45
|
+
const auth = register(`busy_watcher_${Date.now()}`, "BusyWatcher-Strong-1!", undefined, "watcher");
|
|
46
|
+
expect(auth.ok).toBe(true);
|
|
47
|
+
networkId = auth.network_id!;
|
|
48
|
+
|
|
49
|
+
// 目标在跑别的任务
|
|
50
|
+
insertSession("target-busy", "working");
|
|
51
|
+
insertTask("busy-60", "target-busy", "2026-08-10 00:00:01");
|
|
52
|
+
// 目标空闲却没开始(真异常)
|
|
53
|
+
insertSession("target-idle", "idle");
|
|
54
|
+
insertTask("idle-60", "target-idle", "2026-08-10 00:00:01");
|
|
55
|
+
// 目标掉线
|
|
56
|
+
insertSession("target-offline", "offline");
|
|
57
|
+
insertTask("offline-60", "target-offline", "2026-08-10 00:00:01");
|
|
58
|
+
// 完全没有 session 行(未知)
|
|
59
|
+
insertTask("unknown-60", "target-without-session", "2026-08-10 00:00:01");
|
|
60
|
+
}, 30_000);
|
|
61
|
+
|
|
62
|
+
afterAll(() => {
|
|
63
|
+
try {
|
|
64
|
+
db.run(`DELETE FROM task_events WHERE network_id = ?1`, [networkId]);
|
|
65
|
+
db.run(`DELETE FROM tasks WHERE network_id = ?1`, [networkId]);
|
|
66
|
+
db.run(`DELETE FROM sessions WHERE network_id = ?1`, [networkId]);
|
|
67
|
+
} catch {}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("#2571 stale vs queued-behind-busy", () => {
|
|
71
|
+
test("目标 working ⇒ 只写 queued_behind_busy_*,不写 delivered_stale_*", () => {
|
|
72
|
+
recordDeliveredStaleEvents(NOW);
|
|
73
|
+
const events = eventsFor("busy-60");
|
|
74
|
+
expect(events).toContain("task.warning.queued_behind_busy_60s");
|
|
75
|
+
expect(events).not.toContain("task.warning.delivered_stale_60s");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("目标 idle / offline / 无 session ⇒ 只写 delivered_stale_*", () => {
|
|
79
|
+
recordDeliveredStaleEvents(NOW);
|
|
80
|
+
for (const taskId of ["idle-60", "offline-60", "unknown-60"]) {
|
|
81
|
+
const events = eventsFor(taskId);
|
|
82
|
+
expect(events).toContain("task.warning.delivered_stale_60s");
|
|
83
|
+
expect(events).not.toContain("task.warning.queued_behind_busy_60s");
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("两类都仍然是 write-once(重复巡逻不重复写)", () => {
|
|
88
|
+
const before = db.get<{ n: number }>(
|
|
89
|
+
`SELECT COUNT(*) AS n FROM task_events WHERE network_id = ?1 AND event_type LIKE 'task.warning.%'`,
|
|
90
|
+
[networkId],
|
|
91
|
+
)!.n;
|
|
92
|
+
const second = recordDeliveredStaleEvents(NOW);
|
|
93
|
+
const after = db.get<{ n: number }>(
|
|
94
|
+
`SELECT COUNT(*) AS n FROM task_events WHERE network_id = ?1 AND event_type LIKE 'task.warning.%'`,
|
|
95
|
+
[networkId],
|
|
96
|
+
)!.n;
|
|
97
|
+
expect(second.inserted).toBe(0);
|
|
98
|
+
expect(after).toBe(before);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("detail 文案能区分“排队”和“没在跑”", () => {
|
|
102
|
+
const busy = db.get<{ detail: string }>(
|
|
103
|
+
`SELECT detail FROM task_events WHERE task_id='busy-60' AND event_type LIKE 'task.warning.queued_behind_busy_%'`,
|
|
104
|
+
);
|
|
105
|
+
const stale = db.get<{ detail: string }>(
|
|
106
|
+
`SELECT detail FROM task_events WHERE task_id='idle-60' AND event_type LIKE 'task.warning.delivered_stale_%'`,
|
|
107
|
+
);
|
|
108
|
+
expect(busy!.detail).toContain("queued after");
|
|
109
|
+
expect(stale!.detail).toContain("still delivered");
|
|
110
|
+
// 两类文案必须一眼可分:忙的那条不能说成“没在跑”,反之亦然
|
|
111
|
+
expect(busy!.detail).not.toContain("still delivered");
|
|
112
|
+
expect(stale!.detail).not.toContain("queued after");
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -18,6 +18,47 @@ function sqliteUtcTimestamp(date: Date): string {
|
|
|
18
18
|
* equal to the stable event name and protected by a unique index, so every
|
|
19
19
|
* threshold is write-once across restarts, patrols, and multiple Hub workers.
|
|
20
20
|
*/
|
|
21
|
+
/**
|
|
22
|
+
* #2571 —— 把「慢」和「坏」分开:目标正在跑别的任务时,事件写成 `queued_behind_busy_*`;
|
|
23
|
+
* 目标空闲/离线/未知时,才写成 `delivered_stale_*`。
|
|
24
|
+
*
|
|
25
|
+
* 为什么是一条 SQL(而不是两条 INSERT):`test683` 的 mutation 门用**字符串锚点**钉住
|
|
26
|
+
* “write-once 冲突门”“只认 delivered”“网络字段透传”三件事;拆成两条 INSERT 会让锚点
|
|
27
|
+
* 命中次数变成 0 或让 mutation 只命中一半 —— 那就把“可被变异测试覆盖”这条性质弄丢了。
|
|
28
|
+
* 因此这里仍然只保留**一条** INSERT,用 CASE 选择事件名与文案。
|
|
29
|
+
*/
|
|
30
|
+
function recordStale(thresholdSeconds: 30 | 60, cutoff: string): number {
|
|
31
|
+
const busyType = `task.warning.queued_behind_busy_${thresholdSeconds}s`;
|
|
32
|
+
const staleType = `task.warning.delivered_stale_${thresholdSeconds}s`;
|
|
33
|
+
const busyDetail = `queued after ${thresholdSeconds}s; target is working on another task`;
|
|
34
|
+
const staleDetail = thresholdSeconds === 30
|
|
35
|
+
? "still delivered after 30s; target is not working on another task and has not started"
|
|
36
|
+
: "still delivered after 60s; target is idle or offline and not consuming";
|
|
37
|
+
const result = db.run(
|
|
38
|
+
`INSERT INTO task_events
|
|
39
|
+
(task_id, from_status, to_status, event_type, event_key, actor, detail, network_id)
|
|
40
|
+
SELECT task_id, 'delivered', 'delivered',
|
|
41
|
+
CASE WHEN COALESCE((SELECT s.status FROM sessions s
|
|
42
|
+
WHERE s.alias = tasks.to_name AND s.network_id = tasks.network_id), 'unknown') = 'working'
|
|
43
|
+
THEN ?4 ELSE ?5 END,
|
|
44
|
+
CASE WHEN COALESCE((SELECT s.status FROM sessions s
|
|
45
|
+
WHERE s.alias = tasks.to_name AND s.network_id = tasks.network_id), 'unknown') = 'working'
|
|
46
|
+
THEN ?4 ELSE ?5 END,
|
|
47
|
+
'patrol',
|
|
48
|
+
CASE WHEN COALESCE((SELECT s.status FROM sessions s
|
|
49
|
+
WHERE s.alias = tasks.to_name AND s.network_id = tasks.network_id), 'unknown') = 'working'
|
|
50
|
+
THEN ?6 ELSE ?7 END,
|
|
51
|
+
network_id
|
|
52
|
+
FROM tasks
|
|
53
|
+
WHERE status = 'delivered'
|
|
54
|
+
AND delivered_at IS NOT NULL
|
|
55
|
+
AND delivered_at <= ?3
|
|
56
|
+
ON CONFLICT(task_id, event_key) DO NOTHING`,
|
|
57
|
+
[staleType, staleDetail, cutoff, busyType, staleType, busyDetail, staleDetail],
|
|
58
|
+
);
|
|
59
|
+
return result.changes;
|
|
60
|
+
}
|
|
61
|
+
|
|
21
62
|
export function recordDeliveredStaleEvents(now = new Date()): DeliveredStalePatrolResult {
|
|
22
63
|
if (!Number.isFinite(now.getTime())) throw new Error("invalid lifecycle patrol time");
|
|
23
64
|
|
|
@@ -25,24 +66,10 @@ export function recordDeliveredStaleEvents(now = new Date()): DeliveredStalePatr
|
|
|
25
66
|
let inserted = 0;
|
|
26
67
|
|
|
27
68
|
for (const thresholdSeconds of DELIVERED_STALE_THRESHOLDS) {
|
|
28
|
-
const eventType = `task.warning.delivered_stale_${thresholdSeconds}s`;
|
|
29
69
|
const cutoff = sqliteUtcTimestamp(new Date(now.getTime() - thresholdSeconds * 1000));
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const result = db.run(
|
|
34
|
-
`INSERT INTO task_events
|
|
35
|
-
(task_id, from_status, to_status, event_type, event_key, actor, detail, network_id)
|
|
36
|
-
SELECT task_id, 'delivered', 'delivered', ?1, ?1, 'patrol', ?2, network_id
|
|
37
|
-
FROM tasks
|
|
38
|
-
WHERE status = 'delivered'
|
|
39
|
-
AND delivered_at IS NOT NULL
|
|
40
|
-
AND delivered_at <= ?3
|
|
41
|
-
ON CONFLICT(task_id, event_key) DO NOTHING`,
|
|
42
|
-
[eventType, detail, cutoff],
|
|
43
|
-
);
|
|
44
|
-
byThreshold[thresholdSeconds] = result.changes;
|
|
45
|
-
inserted += result.changes;
|
|
70
|
+
const changes = recordStale(thresholdSeconds, cutoff);
|
|
71
|
+
byThreshold[thresholdSeconds] = changes;
|
|
72
|
+
inserted += changes;
|
|
46
73
|
}
|
|
47
74
|
|
|
48
75
|
return { inserted, by_threshold_seconds: byThreshold };
|