@sleep2agi/commhub-server 0.9.0-preview.180 → 0.9.0-preview.181

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.180",
3
+ "version": "0.9.0-preview.181",
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,50 @@
1
+ // 探针:target_offline 连续跳过后,next_run_at 到底被推到哪里?
2
+ // 生产现象(2026-10-28 08:45):backoff_effective_seconds=5760,但实际间隔 360s。
3
+ // 这里用真实调度入口 dispatchScheduledOccurrence 复现,看 next_run_at 与实际 cadence。
4
+ // Run: COMMHUB_DB=/tmp/target-offline-backoff.db bun test src/scheduled-offline-backoff-probe.test.ts
5
+ import { describe, expect, test, beforeAll } from 'bun:test';
6
+ import { register } from './auth.js';
7
+ import { db } from './db.js';
8
+ import { dispatchScheduledOccurrence, getScheduleRow } from './scheduled-tasks.js';
9
+
10
+ const stamp = Date.now();
11
+ const TARGET_ID = `n_offb_${stamp}`, TARGET = `offb-${stamp}`;
12
+ let NET = '', USER = '';
13
+
14
+ function seed() {
15
+ const id = `sched_offb_${crypto.randomUUID()}`;
16
+ db.run(`INSERT INTO scheduled_tasks (schedule_id, network_id, created_by, name, target_node_id, target_alias,
17
+ task_content, priority, schedule_type, schedule_json, timezone, status, next_run_at, overlap_policy)
18
+ VALUES (?1,?2,?3,'offb',?4,?5,'x','normal','interval','{"type":"interval","every_seconds":180}','Asia/Shanghai','active',datetime('now','+3 minutes'),'skip')`,
19
+ [id, NET, USER, TARGET_ID, TARGET]);
20
+ return getScheduleRow(id)!;
21
+ }
22
+
23
+ beforeAll(() => {
24
+ const owner = register(`offb_owner_${stamp}`, 'OffbOwnerPassw0rd!', undefined, 'seed');
25
+ expect(owner.ok).toBe(true);
26
+ NET = owner.network_id!; USER = owner.user!.user_id;
27
+ db.run("INSERT INTO nodes (node_id, node_name, alias, runtime, network_id, owner_user_id) VALUES (?1,?2,?2,'codex',?3,?4)", [TARGET_ID, TARGET, NET, USER]);
28
+ // 目标会话:10 分钟没心跳 ⇒ offline
29
+ db.run("INSERT INTO sessions (resume_id, alias, status, node_id, network_id, last_seen_at) VALUES (?1,?2,'idle',?3,?4,datetime('now','-10 minutes'))",
30
+ [`r_offb_${stamp}`, TARGET, TARGET_ID, NET]);
31
+ });
32
+
33
+ describe('target_offline backoff 是否真的推后 next_run_at', () => {
34
+ test('连续 4 次离线跳过后,next_run_at 应 ≈ now + effective(而不是 base 的倍数)', () => {
35
+ const s = seed();
36
+ const rows: any[] = [];
37
+ for (let i = 0; i < 4; i++) {
38
+ const at = new Date(Date.now() + i * 10).toISOString();
39
+ const r = dispatchScheduledOccurrence(getScheduleRow(s.schedule_id)!, at, true, new Date());
40
+ const after = db.get<any>("SELECT backoff_effective_seconds, backoff_streak, next_run_at FROM scheduled_tasks WHERE schedule_id = ?1", s.schedule_id);
41
+ const nextMs = after.next_run_at ? Date.parse(String(after.next_run_at).replace(' ', 'T') + (String(after.next_run_at).endsWith('Z') ? '' : 'Z')) : NaN;
42
+ rows.push({ i, status: r.status, eff: after.backoff_effective_seconds, streak: after.backoff_streak, next_raw: after.next_run_at, gap_s: Number.isFinite(nextMs) ? Math.round((nextMs - Date.parse(at))/1000) : 'NaN' });
43
+ }
44
+ console.log('每次 fire 后:', JSON.stringify(rows));
45
+ const last = rows[rows.length - 1];
46
+ // 断言:最后一次的 next_run_at 与 effective 同量级(允许 ±2 个 base 的容差)
47
+ if (!last.eff) throw new Error('backoff_effective_seconds 始终为空:退避没有生效');
48
+ if (last.gap_s < last.eff) throw new Error(`next_run_at 只推后 ${last.gap_s}s,但 effective=${last.eff}s`);
49
+ });
50
+ });
@@ -456,7 +456,13 @@ export function dispatchScheduledOccurrence(row: ScheduledRow, scheduledFor: str
456
456
  if (bo && bo.effective_seconds) {
457
457
  const sched = db.get<{ next_run_at: string | null }>("SELECT next_run_at FROM scheduled_tasks WHERE schedule_id = ?1", row.schedule_id);
458
458
  const at = parseHubTimestamp(sched?.next_run_at);
459
+ // 可观测性(不是装饰):生产出现「backoff_effective_seconds=5760 但实际每 6 分钟跳一次」,
460
+ // 只看列分不清是哪一步把 next_run_at 写回去了。这行把「算出的 eff」与「此刻落库的 next」
461
+ // 打在一起;再配上本函数返回前的最终值,就能对上账。
462
+ console.error(`[commhub scheduled-tasks] offline-backoff schedule=${row.schedule_id} cause=${bo.cause} streak=${bo.streak} effective=${bo.effective_seconds}s next_before=${sched?.next_run_at ?? 'null'}`);
459
463
  if (at !== null) advance({ ...row, target_alias: targetNode.alias }, scheduledFor, new Date(at));
464
+ const after = db.get<{ next_run_at: string | null }>("SELECT next_run_at FROM scheduled_tasks WHERE schedule_id = ?1", row.schedule_id);
465
+ console.error(`[commhub scheduled-tasks] offline-backoff-final schedule=${row.schedule_id} next_after=${after?.next_run_at ?? 'null'}`);
460
466
  }
461
467
  } catch (e: any) {
462
468
  console.error(`[scheduled-tasks] no-progress backoff failed schedule=${row.schedule_id}: ${e?.message || e}`);