@sleep2agi/commhub-server 0.9.0-preview.161 → 0.9.0-preview.163

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.
@@ -4,7 +4,7 @@ import { db, logTaskEvent } from "./db.js";
4
4
  import { TASK_EVENT_REST_COLUMNS } from "./rest-projections.js";
5
5
  import { recordDeliveredStaleEvents } from "./task-lifecycle-watcher.js";
6
6
 
7
- const NOW = new Date("2026-08-10T00:01:01.000Z");
7
+ const NOW = new Date("2026-08-10T01:00:01.000Z");
8
8
  let server: ReturnType<typeof Bun.serve>;
9
9
  let base = "";
10
10
  let token = "";
@@ -34,9 +34,9 @@ beforeAll(async () => {
34
34
  token = auth.token!;
35
35
  networkId = auth.network_id!;
36
36
 
37
- insertTask("stale-29", "delivered", "2026-08-10 00:00:32");
38
- insertTask("stale-30", "delivered", "2026-08-10 00:00:31");
39
- insertTask("stale-60", "delivered", "2026-08-10 00:00:01");
37
+ insertTask("stale-599", "delivered", "2026-08-10 00:50:02");
38
+ insertTask("stale-600", "delivered", "2026-08-10 00:50:01");
39
+ insertTask("stale-1800", "delivered", "2026-08-10 00:30:01");
40
40
  insertTask("stale-acked", "acked", "2026-08-10 00:00:00");
41
41
  insertTask("stale-no-delivery", "delivered", null);
42
42
 
@@ -70,21 +70,21 @@ async function waitUntil(ready: () => boolean, timeoutMs: number, what: string |
70
70
  }
71
71
 
72
72
  describe("#167 Hub delivered-stale lifecycle watcher", () => {
73
- test("30s/60s thresholds are exact and non-delivered tasks stay silent", () => {
73
+ test("10m/30m thresholds are exact and non-delivered tasks stay silent", () => {
74
74
  const first = recordDeliveredStaleEvents(NOW);
75
75
  expect(first.inserted).toBe(3);
76
- expect(first.by_threshold_seconds).toEqual({ 30: 2, 60: 1 });
76
+ expect(first.by_threshold_seconds).toEqual({ 600: 2, 1800: 1 });
77
77
 
78
- expect(db.get<{ count: number }>("SELECT COUNT(*) AS count FROM task_events WHERE task_id = 'stale-29'")!.count).toBe(0);
79
- expect(db.get<{ count: number }>("SELECT COUNT(*) AS count FROM task_events WHERE task_id = 'stale-30'")!.count).toBe(1);
80
- expect(db.get<{ count: number }>("SELECT COUNT(*) AS count FROM task_events WHERE task_id = 'stale-60'")!.count).toBe(2);
78
+ expect(db.get<{ count: number }>("SELECT COUNT(*) AS count FROM task_events WHERE task_id = 'stale-599'")!.count).toBe(0);
79
+ expect(db.get<{ count: number }>("SELECT COUNT(*) AS count FROM task_events WHERE task_id = 'stale-600'")!.count).toBe(1);
80
+ expect(db.get<{ count: number }>("SELECT COUNT(*) AS count FROM task_events WHERE task_id = 'stale-1800'")!.count).toBe(2);
81
81
  expect(db.get<{ count: number }>("SELECT COUNT(*) AS count FROM task_events WHERE task_id = 'stale-acked'")!.count).toBe(0);
82
82
  expect(db.get<{ count: number }>("SELECT COUNT(*) AS count FROM task_events WHERE task_id = 'stale-no-delivery'")!.count).toBe(0);
83
83
 
84
- const sixty = db.all<any>("SELECT * FROM task_events WHERE task_id = 'stale-60' ORDER BY event_type");
84
+ const sixty = db.all<any>("SELECT * FROM task_events WHERE task_id = 'stale-1800' ORDER BY event_type");
85
85
  expect(sixty.map((row) => row.event_type)).toEqual([
86
- "task.warning.delivered_stale_30s",
87
- "task.warning.delivered_stale_60s",
86
+ "task.warning.delivered_stale_1800s",
87
+ "task.warning.delivered_stale_600s",
88
88
  ]);
89
89
  expect(sixty.map((row) => row.event_key)).toEqual(sixty.map((row) => row.event_type));
90
90
  expect(sixty.every((row) => row.from_status === "delivered" && row.to_status === "delivered")).toBe(true);
@@ -95,21 +95,21 @@ describe("#167 Hub delivered-stale lifecycle watcher", () => {
95
95
  test("a second patrol is write-once for each task and threshold", () => {
96
96
  const second = recordDeliveredStaleEvents(NOW);
97
97
  expect(second.inserted).toBe(0);
98
- expect(second.by_threshold_seconds).toEqual({ 30: 0, 60: 0 });
98
+ expect(second.by_threshold_seconds).toEqual({ 600: 0, 1800: 0 });
99
99
  expect(db.get<{ count: number }>(
100
100
  "SELECT COUNT(*) AS count FROM task_events WHERE event_type LIKE 'task.warning.delivered_stale_%'",
101
101
  )!.count).toBe(3);
102
102
  });
103
103
 
104
104
  test("ordinary lifecycle events get standard event names without a dedupe key", () => {
105
- logTaskEvent("stale-30", null, "delivered", "target");
106
- logTaskEvent("stale-30", "delivered", "acked", "target");
107
- logTaskEvent("stale-30", "acked", "running", "target");
108
- logTaskEvent("stale-30", "running", "replied", "target");
109
- logTaskEvent("stale-30", "running", "failed", "target");
110
- logTaskEvent("stale-30", "delivered", "expired", "target");
105
+ logTaskEvent("stale-600", null, "delivered", "target");
106
+ logTaskEvent("stale-600", "delivered", "acked", "target");
107
+ logTaskEvent("stale-600", "acked", "running", "target");
108
+ logTaskEvent("stale-600", "running", "replied", "target");
109
+ logTaskEvent("stale-600", "running", "failed", "target");
110
+ logTaskEvent("stale-600", "delivered", "expired", "target");
111
111
  const rows = db.all<any>(
112
- "SELECT event_type, event_key FROM task_events WHERE task_id = 'stale-30' AND actor = 'target' ORDER BY id",
112
+ "SELECT event_type, event_key FROM task_events WHERE task_id = 'stale-600' AND actor = 'target' ORDER BY id",
113
113
  );
114
114
  expect(rows).toEqual([
115
115
  { event_type: "task.send.delivered", event_key: null },
@@ -125,7 +125,7 @@ describe("#167 Hub delivered-stale lifecycle watcher", () => {
125
125
  expect(TASK_EVENT_REST_COLUMNS).toContain("event_type");
126
126
  expect(TASK_EVENT_REST_COLUMNS).not.toContain("event_key" as any);
127
127
  const response = await fetch(
128
- `${base}/api/task_events?network_id=${encodeURIComponent(networkId)}&task_id=stale-60`,
128
+ `${base}/api/task_events?network_id=${encodeURIComponent(networkId)}&task_id=stale-1800`,
129
129
  { headers: { Authorization: `Bearer ${token}` } },
130
130
  );
131
131
  expect(response.status).toBe(200);
@@ -177,7 +177,7 @@ describe("#167 Hub delivered-stale lifecycle watcher", () => {
177
177
  `INSERT INTO tasks
178
178
  (task_id, from_name, to_name, status, content, delivered_at, network_id)
179
179
  VALUES (?1, 'sender', 'target', 'delivered', 'live watcher fixture', ?2, ?3)`,
180
- [taskId, sqliteUtcTimestamp(new Date(Date.now() - 28_000)), networkId],
180
+ [taskId, sqliteUtcTimestamp(new Date(Date.now() - 601_000)), networkId],
181
181
  );
182
182
  expect(childDb.query<{ count: number }, [string]>(
183
183
  "SELECT COUNT(*) AS count FROM task_events WHERE task_id = ?1",
@@ -188,10 +188,10 @@ describe("#167 Hub delivered-stale lifecycle watcher", () => {
188
188
  // 实测在 CI 上被这一条打红过(#798 让这个文件第一次进 CI 才暴露)。
189
189
  // 改成「轮询到目标状态,或到期报错」:常态下快 ~60 倍,慢的时候等得起。
190
190
  const countStale = () => childDb.query<{ count: number }, [string]>(
191
- "SELECT COUNT(*) AS count FROM task_events WHERE task_id = ?1 AND event_type = 'task.warning.delivered_stale_30s'",
191
+ "SELECT COUNT(*) AS count FROM task_events WHERE task_id = ?1 AND event_type = 'task.warning.delivered_stale_600s'",
192
192
  ).get(taskId)!.count;
193
193
  await waitUntil(() => countStale() === 1, 20_000,
194
- "watcher did not write the delivered_stale_30s event");
194
+ "watcher did not write the delivered_stale_600s event");
195
195
  expect(countStale()).toBe(1);
196
196
  } finally {
197
197
  childDb.close();
@@ -1,6 +1,6 @@
1
1
  import { db } from "./db.js";
2
2
 
3
- export const DELIVERED_STALE_THRESHOLDS = [30, 60] as const;
3
+ export const DELIVERED_STALE_THRESHOLDS = [600, 1800] as const;
4
4
 
5
5
  export type DeliveredStalePatrolResult = {
6
6
  inserted: number;
@@ -11,6 +11,19 @@ function sqliteUtcTimestamp(date: Date): string {
11
11
  return date.toISOString().slice(0, 19).replace("T", " ");
12
12
  }
13
13
 
14
+ // #2571(第二轮)—— 阈值从 30s/60s 提到 10min/30min。
15
+ //
16
+ // 第一轮只做了“按目标忙/闲分流”,上线后(19:45–22:30)实测**告警量反而翻倍**
17
+ // (33/h → 73/h,33/h → 104/h),因为同一个任务会先在忙时写 busy、目标转闲后
18
+ // 再写一次 stale(46 个任务两类都出现)。而被标 stale 的任务里仍有 73.5%
19
+ // 最终成功。真正的病因不是“忙 vs 闲”,是**阈值本身离真实分布太远**:
20
+ // 7 天 11877 个成功任务,delivered → 首次活动 p50 = 1 秒、p90 = 6 秒、
21
+ // p95 = 57 秒、p99 = 12 分钟。
22
+ // ⇒ 30 秒的判据会命中 6.4% 的**正常**任务,60 秒命中 4.9%,等于把 p95–p99
23
+ // 的长尾全报警。改成 10 分钟(p99 之下)才有意义:超过它的比例 1.2%,
24
+ // 30 分钟是 0.2%,只留真异常。
25
+ //
26
+ // event_key 里带秒数 ⇒ 改阈值会产生新的 key,历史事件不受影响、可并存对比。
14
27
  /**
15
28
  * Persist Hub-authoritative warnings for tasks which are still delivered.
16
29
  *
@@ -27,13 +40,18 @@ function sqliteUtcTimestamp(date: Date): string {
27
40
  * 命中次数变成 0 或让 mutation 只命中一半 —— 那就把“可被变异测试覆盖”这条性质弄丢了。
28
41
  * 因此这里仍然只保留**一条** INSERT,用 CASE 选择事件名与文案。
29
42
  */
30
- function recordStale(thresholdSeconds: 30 | 60, cutoff: string): number {
43
+ function thresholdLabel(thresholdSeconds: 600 | 1800): string {
44
+ return thresholdSeconds === 600 ? "10m" : "30m";
45
+ }
46
+
47
+ function recordStale(thresholdSeconds: 600 | 1800, cutoff: string): number {
31
48
  const busyType = `task.warning.queued_behind_busy_${thresholdSeconds}s`;
32
49
  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";
50
+ const label = thresholdLabel(thresholdSeconds);
51
+ const busyDetail = `queued after ${label}; target is working on another task`;
52
+ const staleDetail = thresholdSeconds === 600
53
+ ? `still delivered after ${label}; target is not working on another task and has not started`
54
+ : `still delivered after ${label}; target is idle or offline and not consuming`;
37
55
  const result = db.run(
38
56
  `INSERT INTO task_events
39
57
  (task_id, from_status, to_status, event_type, event_key, actor, detail, network_id)
@@ -62,7 +80,7 @@ function recordStale(thresholdSeconds: 30 | 60, cutoff: string): number {
62
80
  export function recordDeliveredStaleEvents(now = new Date()): DeliveredStalePatrolResult {
63
81
  if (!Number.isFinite(now.getTime())) throw new Error("invalid lifecycle patrol time");
64
82
 
65
- const byThreshold = { 30: 0, 60: 0 };
83
+ const byThreshold = { 600: 0, 1800: 0 };
66
84
  let inserted = 0;
67
85
 
68
86
  for (const thresholdSeconds of DELIVERED_STALE_THRESHOLDS) {