@sleep2agi/commhub-server 0.9.0-preview.162 → 0.9.0-preview.164

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.
@@ -0,0 +1,81 @@
1
+ // #2668 —— 投给**已离线**节点的任务不该躺满 24 小时。
2
+ //
3
+ // 现场(2026-10-27,生产):
4
+ // 任务 3b3c63e0 → TM基建鲸,投递后目标 session 立刻 offline,任务以 `delivered` 状态挂了 **2 小时 56 分**;
5
+ // 10 分钟 / 30 分钟两档告警都按时报了,然后**没有任何处置**;`expires_at` 是创建后 24 小时。
6
+ // 同期队列里还有几个同类任务。
7
+ //
8
+ // 为什么这是问题(三条,都实测过):
9
+ // 1. 队列里挂着**不可能被执行**的任务(目标不在),却计入“在途/积压”;与 #2363 的 in-flight 判读混在一起;
10
+ // 2. 发送方(常常是另一个 Agent)在自己 30 分钟超时后早已放弃,Hub 侧看起来还在“投递中”;
11
+ // 3. 目标重新上线后会消费这个陈旧任务 —— 可能做三小时前的工作。
12
+ //
13
+ // 本模块只做一件最小的事:**目标明确不在(session=offline 或没有 session 行)且投递已超过 TTL** ⇒ 过期。
14
+ // 「目标忙」不算(那走 queued_behind_busy,不做处置);`created` 状态不算(还没投递);
15
+ // 已经被节点取走(`consumed_at` 有值)的不算(那是节点在处理,交给 #758 的 orphan 判据)。
16
+
17
+ import { db } from "./db.js";
18
+
19
+ export const OFFLINE_DELIVERY_TTL_ENV = "COMMHUB_OFFLINE_DELIVERY_TTL_MINUTES";
20
+ export const OFFLINE_DELIVERY_TTL_DEFAULT_MINUTES = 30;
21
+
22
+ export function offlineDeliveryTtlMinutes(): number {
23
+ const raw = process.env[OFFLINE_DELIVERY_TTL_ENV];
24
+ if (raw === undefined || raw.trim() === "") return OFFLINE_DELIVERY_TTL_DEFAULT_MINUTES;
25
+ const n = Number(raw);
26
+ return Number.isFinite(n) && n >= 0 ? Math.floor(n) : OFFLINE_DELIVERY_TTL_DEFAULT_MINUTES;
27
+ }
28
+
29
+ export type OfflineDroppedTask = {
30
+ task_id: string;
31
+ network_id: string | null;
32
+ from_name: string | null;
33
+ from_node_id: string | null;
34
+ to_name: string | null;
35
+ content: string;
36
+ created_at: string;
37
+ expires_at: string | null;
38
+ parent_task_id: string | null;
39
+ meta_json: string | null;
40
+ waited_minutes: number;
41
+ };
42
+
43
+ /**
44
+ * 找出并过期「投给离线目标的陈年投递」。返回被过期的行(调用方负责发事件与通知,与 TTL 那条路径一致)。
45
+ * 同步执行:调用点在 5 分钟一次的 patrol 里,单次量级是几十行。
46
+ */
47
+ export function expireDeliveredToOfflineTargets(now = new Date()): OfflineDroppedTask[] {
48
+ const ttl = offlineDeliveryTtlMinutes();
49
+ if (ttl <= 0) return [];
50
+ const nowIso = now.toISOString();
51
+ const ageClause = `datetime(delivered_at) <= datetime(?1, '-${ttl} minutes')`;
52
+
53
+ const due = db.all<OfflineDroppedTask>(
54
+ `SELECT t.task_id, t.network_id, t.from_name, t.from_node_id, t.to_name, t.content,
55
+ t.created_at, t.expires_at, t.parent_task_id, t.meta_json,
56
+ CAST((julianday(?1) - julianday(t.delivered_at)) * 1440 AS INTEGER) AS waited_minutes
57
+ FROM tasks t
58
+ WHERE t.status = 'delivered'
59
+ AND t.delivered_at IS NOT NULL
60
+ AND t.consumed_at IS NULL
61
+ AND ${ageClause}
62
+ AND COALESCE((SELECT s.status FROM sessions s
63
+ WHERE s.alias = t.to_name AND s.network_id = t.network_id), 'offline') = 'offline'`,
64
+ [nowIso],
65
+ );
66
+ if (due.length === 0) return [];
67
+
68
+ const dropped: OfflineDroppedTask[] = [];
69
+ for (const task of due) {
70
+ const changed = db.run(
71
+ `UPDATE tasks SET status = 'expired', completed_at = datetime('now')
72
+ WHERE task_id = ?1 AND status = 'delivered' AND consumed_at IS NULL`,
73
+ [task.task_id],
74
+ );
75
+ if (changed.changes < 1) continue;
76
+ // 与 TTL 过期一致:把 inbox 行 ack 掉,否则 unread 徽标与 stop/delete 门会一直看到它。
77
+ db.run(`UPDATE inbox SET acked = 1 WHERE COALESCE(task_id, id) = ?1 AND acked = 0`, [task.task_id]);
78
+ dropped.push(task);
79
+ }
80
+ return dropped;
81
+ }