dsh-notify-say 0.5.10 → 0.5.11
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/CHANGELOG.md +4 -0
- package/lib/config-schema.js +21 -0
- package/lib/config-store.js +12 -0
- package/lib/index.js +271 -47
- package/package.json +1 -1
- package/test/idle-gate-contract.test.mjs +200 -28
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
> 本文件由 `tools/release-build.ps1` 从开发提交自动生成,**请勿手工编辑**。
|
|
4
4
|
> 只列面向使用者的变更(新增功能与修复),不含内部维护提交。
|
|
5
5
|
|
|
6
|
+
## v0.5.11 — 2026-11-27
|
|
7
|
+
|
|
8
|
+
- fix(notify-say): 空闲门改静默期判定,修子代理交接瞬间误播「完成」(v0.5.11)
|
|
9
|
+
|
|
6
10
|
## v0.5.9 — 2026-11-26
|
|
7
11
|
|
|
8
12
|
- fix(ci): Node 20 静态 import globSync 导致 CI 连红四次,加 D20 判据锁住
|
package/lib/config-schema.js
CHANGED
|
@@ -152,6 +152,24 @@ export const FIELD_DEFAULTS = Object.freeze({
|
|
|
152
152
|
notifyOnQuestion: true,
|
|
153
153
|
subagents: false,
|
|
154
154
|
idleGate: true,
|
|
155
|
+
/**
|
|
156
|
+
* v0.5.11:空闲门的**静默期**(毫秒)。
|
|
157
|
+
*
|
|
158
|
+
* 语义:确认"真的没人动了"再播「完成」——窗口期内任何活动都重新计时,
|
|
159
|
+
* 窗口到点后再重跑一遍空闲判据,仍然干净才放行。
|
|
160
|
+
*
|
|
161
|
+
* ## 为什么需要它(别删这段:它是这个参数的唯一存在理由)
|
|
162
|
+
*
|
|
163
|
+
* DSH 的固定时序是「子代理 turn/end + agent/status=idle 先到,结算消息
|
|
164
|
+
* 139~172ms 之后才投进父代理 inbox」(2026-11-23 原始会话日志实测 3 例:
|
|
165
|
+
* 139 / 155 / 159ms)。那一刻从任何单一快照看都是"真空闲":子代理已 idle、
|
|
166
|
+
* jobs 为空、父轮次也已关闭 —— 于是旧实现立刻播「完成」,而父代理紧接着
|
|
167
|
+
* 就被这条消息叫起来继续干活。用户听到的就是"主代理还没开始就播了"。
|
|
168
|
+
*
|
|
169
|
+
* 默认 1500ms:实测最大缝 172ms,留 8 倍余量;同时短到不影响正常手感。
|
|
170
|
+
* **0 = 关闭静默期**,退回 v0.5.10 的"即时释放"行为(出问题时的一步回退)。
|
|
171
|
+
*/
|
|
172
|
+
idleGateQuietMs: 1500,
|
|
155
173
|
// ── v0.4.2:音源与 Toast ────────────────────────────────────────────────
|
|
156
174
|
/** 音源:tts=系统语音合成(零下载)、bundled=已装语音包、system=系统提示音、custom=自定义文件、none=不发声 */
|
|
157
175
|
audioSource: 'tts',
|
|
@@ -258,6 +276,9 @@ export function buildConfig(z) {
|
|
|
258
276
|
.description('子代理会话也提醒(默认只提醒主会话)'),
|
|
259
277
|
idleGate: z.boolean().default(FIELD_DEFAULTS.idleGate)
|
|
260
278
|
.description('等真正干完再提醒:有子代理/后台任务在跑时先不播「完成」,等全部结束再播一次'),
|
|
279
|
+
idleGateQuietMs: z.number().step(100).min(0).max(60000)
|
|
280
|
+
.default(FIELD_DEFAULTS.idleGateQuietMs)
|
|
281
|
+
.description('空闲门静默期(毫秒):最后一段活动过去多久才算"真的干完",0 = 关闭(退回即时释放)'),
|
|
261
282
|
|
|
262
283
|
// ── 音源(v0.4.2)────────────────────────────────────────────────────
|
|
263
284
|
//
|
package/lib/config-store.js
CHANGED
|
@@ -155,6 +155,16 @@ const RULES = {
|
|
|
155
155
|
return Number.isFinite(n) && n >= 0 && n <= 3600 ? Math.round(n) : undefined;
|
|
156
156
|
},
|
|
157
157
|
|
|
158
|
+
// v0.5.11:空闲门静默期(毫秒)。0 = 关闭(退回 v0.5.10 的即时放行)。
|
|
159
|
+
//
|
|
160
|
+
// 必须进白名单:白名单外的键在保存时会被 sanitize 当"未知键"丢弃,
|
|
161
|
+
// 用户改了却静默不生效 —— devReload 当年正是这么踩的(见 BOOL_KEYS 的注释)。
|
|
162
|
+
// 上限 60 秒:静默期再长就不是"等一下看它动不动",而是"忘了播"。
|
|
163
|
+
idleGateQuietMs: (v) => {
|
|
164
|
+
const n = Number(v);
|
|
165
|
+
return Number.isFinite(n) && n >= 0 && n <= 60000 ? Math.round(n) : undefined;
|
|
166
|
+
},
|
|
167
|
+
|
|
158
168
|
// v0.4.8:TTS 自定义台词(每种事件一段,空串 = 用内置台词库)
|
|
159
169
|
//
|
|
160
170
|
// 长度上限 60 字:SAPI 念 60 字约 5 秒,符合"一句话提醒"的定位。
|
|
@@ -466,5 +476,7 @@ export function describe(cfg) {
|
|
|
466
476
|
debounceSeconds: Math.round((Number(cfg.debounceMs) || 0) / 1000),
|
|
467
477
|
globalCooldownSeconds: Math.round((Number(cfg.globalCooldownMs) || 0) / 1000),
|
|
468
478
|
idleGateTimeoutSeconds: Math.round((Number(cfg.idleGateTimeoutMs) || 0) / 1000),
|
|
479
|
+
// v0.5.11:静默期(毫秒)原样回传,前端按毫秒展示
|
|
480
|
+
idleGateQuietMs: Math.round(Number(cfg.idleGateQuietMs) || 0),
|
|
469
481
|
};
|
|
470
482
|
}
|
package/lib/index.js
CHANGED
|
@@ -508,6 +508,11 @@ export function normalizeConfig(raw) {
|
|
|
508
508
|
if (Number.isFinite(a) && a >= 0 && a <= 3600) out.approvalDebounceMs = Math.round(a * 1000);
|
|
509
509
|
const g = Number(raw.idleGateTimeoutSeconds);
|
|
510
510
|
if (Number.isFinite(g) && g >= 0 && g <= 86400) out.idleGateTimeoutMs = Math.round(g * 1000);
|
|
511
|
+
// v0.5.11:空闲门静默期(毫秒)。存在理由见 config-schema.js 的
|
|
512
|
+
// FIELD_DEFAULTS.idleGateQuietMs 考古注释(DSH 先 idle、139ms 后才投递结算消息)。
|
|
513
|
+
// 缺省/非法 → 1500(不能因为老配置文件没有这个键就退回旧行为)。
|
|
514
|
+
const qm = Number(raw.idleGateQuietMs);
|
|
515
|
+
out.idleGateQuietMs = Number.isFinite(qm) && qm >= 0 && qm <= 60000 ? Math.round(qm) : 1500;
|
|
511
516
|
// v0.4.7:跨会话冷却(0 = 关闭)
|
|
512
517
|
const gc = Number(raw.globalCooldownSeconds);
|
|
513
518
|
if (Number.isFinite(gc) && gc >= 0 && gc <= 3600) out.globalCooldownMs = Math.round(gc * 1000);
|
|
@@ -833,6 +838,52 @@ export function collectInFlight(jobSnapshots, agentList, rootId) {
|
|
|
833
838
|
return items;
|
|
834
839
|
}
|
|
835
840
|
|
|
841
|
+
/**
|
|
842
|
+
* 静默期到点时的裁决(v0.5.11)。**纯函数**:不读时钟、不碰定时器、无副作用,
|
|
843
|
+
* 所以 ABA 这类时序问题能直接断言"确切状态 + 确切原因码",不必起真实流程。
|
|
844
|
+
*
|
|
845
|
+
* 返回(每个值都只有一种含义,便于日志与测试对账):
|
|
846
|
+
* · 'quiet-disabled' 静默期被关(quietMs<=0)→ 立刻放行(v0.5.10 行为)
|
|
847
|
+
* · 'activity-since-arm' 起窗之后又有活动(含"瞬间启动又结束"的 ABA)→ 重新计时
|
|
848
|
+
* · 'still-busy' 窗口期内虽然没序号变化,但判据复查发现还忙 → 继续等
|
|
849
|
+
* · 'release' 期间无人动 + 判据全干净 → 允许播报
|
|
850
|
+
*
|
|
851
|
+
* ⚠️ 顺序不能换:必须先判"期间有没有动过",再跑判据。反过来的话,
|
|
852
|
+
* "动过但恰好又回到空闲"会被当成干净放行 —— 那正是误播本身。
|
|
853
|
+
*
|
|
854
|
+
* @param {{armedSeq:number, activitySeq:number, holdReason:string|null, quietMs:number}} a
|
|
855
|
+
* @returns {'quiet-disabled'|'activity-since-arm'|'still-busy'|'release'}
|
|
856
|
+
*/
|
|
857
|
+
export function quietVerdict(a) {
|
|
858
|
+
const s = a && typeof a === 'object' ? a : {};
|
|
859
|
+
const quietMs = Number(s.quietMs);
|
|
860
|
+
if (!Number.isFinite(quietMs) || quietMs <= 0) return 'quiet-disabled';
|
|
861
|
+
if (Number(s.armedSeq) !== Number(s.activitySeq)) return 'activity-since-arm';
|
|
862
|
+
if (s.holdReason) return 'still-busy';
|
|
863
|
+
return 'release';
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* 静默期"再等一会儿"的退避长度(v0.5.11)。**纯函数**。
|
|
868
|
+
*
|
|
869
|
+
* 为什么需要它:到点复查发现还忙(`still-busy`)时,不能"撂挑子等下一次事件"——
|
|
870
|
+
* `gateHoldReason` 会返回 `agent-unavailable` / `turn-boundary-unavailable`
|
|
871
|
+
* 这类"当下读不到"的原因,而它们**没有**必然后续边沿事件;一味等下去,
|
|
872
|
+
* 这条挂起项就饿到 `onIdleGateTimeout`(默认 10 分钟)才动,而超时播的是
|
|
873
|
+
* 进度提醒,不是完成语音。
|
|
874
|
+
*
|
|
875
|
+
* 上限 500ms 是"轮询的安全上限"(既不空转,也远小于静默期),
|
|
876
|
+
* 且永远不超过 quietMs 本身 —— `quietMs=0` 时由调用方走即时放行分支。
|
|
877
|
+
*
|
|
878
|
+
* @param {number} quietMs 静默期长度
|
|
879
|
+
* @returns {number} 退避毫秒数
|
|
880
|
+
*/
|
|
881
|
+
export function quietBackoffMs(quietMs) {
|
|
882
|
+
const n = Number(quietMs);
|
|
883
|
+
if (!Number.isFinite(n) || n <= 0) return 0;
|
|
884
|
+
return Math.min(n, 500);
|
|
885
|
+
}
|
|
886
|
+
|
|
836
887
|
/**
|
|
837
888
|
* 把内部 `cfg` 映射成**给前端看的 schema 形状**。
|
|
838
889
|
*
|
|
@@ -886,6 +937,11 @@ export function effectiveForClient(c) {
|
|
|
886
937
|
// v0.4.7 冷却:毫秒 → 秒(前端下拉用的是秒)
|
|
887
938
|
debounceSeconds: Math.round((Number(g.debounceMs) || 0) / 1000),
|
|
888
939
|
globalCooldownSeconds: Math.round((Number(g.globalCooldownMs) || 0) / 1000),
|
|
940
|
+
// v0.5.11:空闲门静默期(毫秒,原样回传)
|
|
941
|
+
//
|
|
942
|
+
// ⚠️ 加配置项必须同时进三处,漏一处就是 v0.4.18 那个"用户改了不生效":
|
|
943
|
+
// ① config-store.js 的 RULES(保存时不丢)② normalizeConfig(读进来)③ 这里(前端拿得到)
|
|
944
|
+
idleGateQuietMs: Math.round(Number(g.idleGateQuietMs) || 0),
|
|
889
945
|
// v0.4.3
|
|
890
946
|
devReload: g.devReload,
|
|
891
947
|
// v0.4.34 日志级别(info / debug)
|
|
@@ -1110,8 +1166,17 @@ export function apply(ctx, config) {
|
|
|
1110
1166
|
const approvalSeen = new Map(); // 'id:<approvalId>' | 'session:<sid>|<tool>' -> ts
|
|
1111
1167
|
const questionSeen = new Map(); // 'sid:callId' -> ts(同一提问只提醒一次;answered 即清)
|
|
1112
1168
|
const lastTurnEnd = new Map(); // sessionId -> { kind, at }(agent/status idle 时取用)
|
|
1113
|
-
const pendingDone = new Map(); // sessionId -> { session, psKind, title, body, timer, inflight }
|
|
1169
|
+
const pendingDone = new Map(); // sessionId -> { session, psKind, title, body, timer, inflight, quietTimer, quietResets }
|
|
1114
1170
|
const prevStatus = new Map(); // agentId -> 'idle' | 'running'(边沿判定)
|
|
1171
|
+
/**
|
|
1172
|
+
* 单调活动序号(v0.5.11,静默期的 Epoch)。
|
|
1173
|
+
*
|
|
1174
|
+
* 只在"状态真的变了"的边沿递增,绝不读墙钟。原因:静默窗要判的是
|
|
1175
|
+
* 「从起窗到现在有没有人动过」,用 `Date.now()` 差值算剩余时间会受
|
|
1176
|
+
* 系统时间跳变影响(NTP 校时/用户改表 → 窗口瞬间到期或永不到期),
|
|
1177
|
+
* 而序号只有单调递增一个方向,ABA 也能被识别(seq 变了 = 期间动过)。
|
|
1178
|
+
*/
|
|
1179
|
+
let activitySeq = 0;
|
|
1115
1180
|
/** fire-and-forget spawn:并发上限 + 超时 taskkill /T 兜底(旧插件实测加固的移植)。 */
|
|
1116
1181
|
/**
|
|
1117
1182
|
* 播放失败后是否已经降级过一次蜂鸣。
|
|
@@ -2319,6 +2384,8 @@ function hasVoiceLine(psKind) {
|
|
|
2319
2384
|
const p = pendingDone.get(id);
|
|
2320
2385
|
if (!p) return;
|
|
2321
2386
|
clearTimeout(p.timer);
|
|
2387
|
+
if (p.recheckTimer) clearTimeout(p.recheckTimer);
|
|
2388
|
+
if (p.quietTimer) { clearTimeout(p.quietTimer); p.quietTimer = null; }
|
|
2322
2389
|
pendingDone.delete(id);
|
|
2323
2390
|
}
|
|
2324
2391
|
|
|
@@ -2342,13 +2409,19 @@ function hasVoiceLine(psKind) {
|
|
|
2342
2409
|
*
|
|
2343
2410
|
* 超时路径不走这里 —— 超时走 notifyProgress(不消耗完成通知)。
|
|
2344
2411
|
*/
|
|
2345
|
-
function flushPending(id) {
|
|
2412
|
+
function flushPending(id, quiet) {
|
|
2346
2413
|
const p = pendingDone.get(id);
|
|
2347
2414
|
if (!p) return;
|
|
2348
2415
|
clearTimeout(p.timer);
|
|
2349
2416
|
if (p.recheckTimer) clearTimeout(p.recheckTimer);
|
|
2417
|
+
if (p.quietTimer) { clearTimeout(p.quietTimer); p.quietTimer = null; }
|
|
2350
2418
|
pendingDone.delete(id);
|
|
2351
|
-
|
|
2419
|
+
// v0.5.11:记下这次放行走过多长的静默期、期间被重置过几次。
|
|
2420
|
+
// 排查误播/延迟时,"quiet=0" 说明静默期没生效(旧行为),
|
|
2421
|
+
// "resets>0" 说明窗口期间确实有活动(正常且期望)。
|
|
2422
|
+
const quietMs = quiet && Number.isFinite(quiet.quiet) ? quiet.quiet : 0;
|
|
2423
|
+
const resets = quiet && Number.isFinite(quiet.resets) ? quiet.resets : 0;
|
|
2424
|
+
appendNotifyLog(`[flush] idle-gate kind=${p.psKind} sess=${sessionKey(p.session)} worktree-idle quiet=${quietMs} resets=${resets}`);
|
|
2352
2425
|
// evKey 用「这一轮的序号」:同一轮无论哪条路径都算同一个事件。
|
|
2353
2426
|
emitDone(p.session, p.psKind, p.title, p.body, p.evKey || undefined);
|
|
2354
2427
|
}
|
|
@@ -2388,7 +2461,9 @@ function hasVoiceLine(psKind) {
|
|
|
2388
2461
|
if (stillBusy === false) {
|
|
2389
2462
|
// 复查确认真空闲 → 这是正常完成,走 flush
|
|
2390
2463
|
appendNotifyLog(`[idle-gate] timeout recheck: idle now id=${id}`);
|
|
2391
|
-
|
|
2464
|
+
// 账目照实传:`quiet=0` 的语义是"静默期没参与这次放行",
|
|
2465
|
+
// 不能因为这次走的是超时复查就永远记 0(否则事后对账分辨不出来)。
|
|
2466
|
+
flushPending(id, { quiet: quietMsOf(), resets: p.quietResets || 0 });
|
|
2392
2467
|
return;
|
|
2393
2468
|
}
|
|
2394
2469
|
|
|
@@ -2443,8 +2518,10 @@ function hasVoiceLine(psKind) {
|
|
|
2443
2518
|
existing.body = body;
|
|
2444
2519
|
existing.inflight = inflight;
|
|
2445
2520
|
if (inflight.length === 0) {
|
|
2446
|
-
// 工作树已空 →
|
|
2447
|
-
|
|
2521
|
+
// 工作树已空 → 合并后播一次(内容用最新的)。
|
|
2522
|
+
// v0.5.11:走静默期,不立刻 flush —— 合并路径同样是"看到空闲就播",
|
|
2523
|
+
// 一样会掉进「先 idle、139ms 后才投递结算消息」那条缝(见 armQuiet 注释)。
|
|
2524
|
+
armQuiet(id);
|
|
2448
2525
|
} else {
|
|
2449
2526
|
skipLog('idle-gate-merge', psKind, session,
|
|
2450
2527
|
`merged into pending, still ${inflight.length} inflight`);
|
|
@@ -2498,52 +2575,174 @@ function hasVoiceLine(psKind) {
|
|
|
2498
2575
|
* 用户方案明确要求:"状态无法查询时不得当成工作树完成"。
|
|
2499
2576
|
* 现在改为保留 + 记诊断,由超时兜底或下次事件重试。
|
|
2500
2577
|
*/
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2578
|
+
/**
|
|
2579
|
+
* 复查一个挂起项:现在能不能播?返回 null = 真空闲可放行;返回原因码 = 继续等。
|
|
2580
|
+
*
|
|
2581
|
+
* v0.5.11 从 reevaluatePending 里抽出来,因为静默期到点后**必须再跑一遍
|
|
2582
|
+
* 同一套判据**才能放行;两处写两份判据必然漂移(本项目栽过"双份真相源")。
|
|
2583
|
+
*/
|
|
2584
|
+
function gateHoldReason(id, trigger) {
|
|
2585
|
+
// ① agent
|
|
2586
|
+
let agent;
|
|
2587
|
+
try {
|
|
2588
|
+
const as = svc('agents');
|
|
2589
|
+
agent = as && typeof as.get === 'function' ? as.get(id) : undefined;
|
|
2590
|
+
} catch { agent = undefined; }
|
|
2591
|
+
if (!agent) {
|
|
2592
|
+
// 不等于完成 —— 可能只是注册表暂时读不到。保留,等下次或超时。
|
|
2593
|
+
appendNotifyLog(`[idle-gate] pending kept: agent unavailable id=${id} trigger=${trigger}`, { detail: true });
|
|
2594
|
+
return 'agent-unavailable';
|
|
2595
|
+
}
|
|
2596
|
+
|
|
2597
|
+
// ② turnBoundary(权威信号)
|
|
2598
|
+
const tb = turnBoundaryOf(agent.session);
|
|
2599
|
+
if (!tb) {
|
|
2600
|
+
appendNotifyLog(`[idle-gate] pending kept: turnBoundary unavailable id=${id} trigger=${trigger}`, { detail: true });
|
|
2601
|
+
return 'turn-boundary-unavailable';
|
|
2602
|
+
}
|
|
2603
|
+
|
|
2604
|
+
// ⚠️ v0.4.65:**不再**用 lastTurn 递增判断「用户发了新消息」。
|
|
2605
|
+
// 实测证伪:主 Agent 处理子任务返回时内部续跑,lastTurn 也会递增
|
|
2606
|
+
// (2026-10-19 日志:pending dropped turn 9->10,但用户什么都没发)。
|
|
2607
|
+
// 按需求约定「不能用间接信号猜测用户输入」→ 保守退化:保留 pending,
|
|
2608
|
+
// 后续结果合并,最终稳定空闲时最多播一次。
|
|
2609
|
+
// 代价:用户真发新消息时旧通知会补播一次(危害小于误作废)。
|
|
2610
|
+
|
|
2611
|
+
// ④ 轮次还开着 → 还在干,留着
|
|
2612
|
+
if (tb.open !== null) {
|
|
2613
|
+
return 'turn-open';
|
|
2614
|
+
}
|
|
2615
|
+
|
|
2616
|
+
// ⑤ 还有在飞工作 → 留着(把最新快照记下来,日志与超时判断都用它)
|
|
2617
|
+
const inflight = inFlightOf(agent);
|
|
2618
|
+
if (inflight.length > 0) {
|
|
2504
2619
|
const p = pendingDone.get(id);
|
|
2505
|
-
if (
|
|
2620
|
+
if (p) p.inflight = inflight;
|
|
2621
|
+
return 'inflight';
|
|
2622
|
+
}
|
|
2506
2623
|
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
const as = svc('agents');
|
|
2511
|
-
agent = as && typeof as.get === 'function' ? as.get(id) : undefined;
|
|
2512
|
-
} catch { agent = undefined; }
|
|
2513
|
-
if (!agent) {
|
|
2514
|
-
// 不等于完成 —— 可能只是注册表暂时读不到。保留,等下次或超时。
|
|
2515
|
-
appendNotifyLog(`[idle-gate] pending kept: agent unavailable id=${id} trigger=${trigger}`, { detail: true });
|
|
2516
|
-
continue;
|
|
2517
|
-
}
|
|
2624
|
+
// ⑥ 判据全过 —— 但**还不播**:交给静默期再观察一段(起窗是调用方的事)
|
|
2625
|
+
return null;
|
|
2626
|
+
}
|
|
2518
2627
|
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
}
|
|
2628
|
+
/** 静默期长度:0 = 关闭(退回 v0.5.10 即时释放),非法值按默认 1500。 */
|
|
2629
|
+
function quietMsOf() {
|
|
2630
|
+
const n = Number(cfg.idleGateQuietMs);
|
|
2631
|
+
return Number.isFinite(n) && n >= 0 ? Math.min(n, 60000) : 1500;
|
|
2632
|
+
}
|
|
2525
2633
|
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2634
|
+
/**
|
|
2635
|
+
* 记录一次"真的动过了"的活动(静默期的重置信号)。
|
|
2636
|
+
*
|
|
2637
|
+
* 只允许在**状态真的变化**的边沿调用(agent/status 值改变、jobs 事件到达)。
|
|
2638
|
+
* 绝不能对"重复推送同一个状态"的心跳调用 —— 否则静默窗永远等不到尾沿,
|
|
2639
|
+
* 完成通知会被无限期推迟(这是防抖类实现的经典事故)。
|
|
2640
|
+
*/
|
|
2641
|
+
function markActivity() {
|
|
2642
|
+
activitySeq += 1;
|
|
2643
|
+
let cleared = false;
|
|
2644
|
+
for (const p of pendingDone.values()) {
|
|
2645
|
+
if (p.quietTimer) { clearTimeout(p.quietTimer); p.quietTimer = null; cleared = true; }
|
|
2646
|
+
}
|
|
2647
|
+
// ⚠️ 只清定时器 = 这条挂起项失去窗口 → 若之后再也没有边沿事件,
|
|
2648
|
+
// 它要等到 onIdleGateTimeout(10 分钟)才动,而那播的是"进度提醒",
|
|
2649
|
+
// 不是完成语音。所以清完必须**重新起窗**(异步,避免在事件回调里递归
|
|
2650
|
+
// 跑一整轮判据)。reevaluatePending 内部有 has() 守卫,不会双播。
|
|
2651
|
+
if (cleared) {
|
|
2652
|
+
queueMicrotask(() => {
|
|
2653
|
+
try { reevaluatePending('activity'); } catch { /* 重估失败不该影响事件链 */ }
|
|
2654
|
+
});
|
|
2655
|
+
}
|
|
2656
|
+
}
|
|
2532
2657
|
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2658
|
+
/**
|
|
2659
|
+
* 进入静默期(尾沿防抖)等待。
|
|
2660
|
+
*
|
|
2661
|
+
* ## 为什么需要这一层(考古注释,别删)
|
|
2662
|
+
*
|
|
2663
|
+
* DSH 的时序是「子代理 turn/end + agent/status=idle 先到,结算消息
|
|
2664
|
+
* 139~172ms 之后才投进父代理 inbox」(2026-11-23 原始会话日志实测 3 例:
|
|
2665
|
+
* 139 / 155 / 159ms;`dsh-subagent` 的 sendWaking 也只在父代理 idle 时
|
|
2666
|
+
* 才用 queue→next-turn)。那一刻从**任何单一快照**看都是"真空闲":
|
|
2667
|
+
* 子代理已 idle、jobs 为空、父轮次也已关闭。旧实现据此立刻播「完成」,
|
|
2668
|
+
* 而父代理紧接着就被这条消息叫起来继续干活 —— 用户听到的就是
|
|
2669
|
+
* "主代理还没开始就播了"。只靠快照判定,这个问题不可能修对:缺的不是
|
|
2670
|
+
* 更多判据,而是"再等一会儿看它会不会动"。
|
|
2671
|
+
*
|
|
2672
|
+
* 所以:不写子代理特判(保持正交),改成任何活动都重置的静默窗;
|
|
2673
|
+
* 顺带覆盖 job settled→宿主延迟投递这类同形竞态。
|
|
2674
|
+
*/
|
|
2675
|
+
function armQuiet(id, delayMs) {
|
|
2676
|
+
const p = pendingDone.get(id);
|
|
2677
|
+
if (!p) return;
|
|
2678
|
+
const quietMs = quietMsOf();
|
|
2679
|
+
if (!(quietMs > 0)) {
|
|
2680
|
+
// 关掉静默期 → 保持 v0.5.10 行为(一步回退开关)
|
|
2681
|
+
flushPending(id, { quiet: 0, resets: 0 });
|
|
2682
|
+
return;
|
|
2683
|
+
}
|
|
2684
|
+
// 已经在窗口里就不重置:窗口只由 markActivity 重置。
|
|
2685
|
+
// (若这里也重置,jobs 事件的高频重估会把窗口越推越远 → 永不播报)
|
|
2686
|
+
if (p.quietTimer) return;
|
|
2687
|
+
const armedSeq = activitySeq;
|
|
2688
|
+
const wait = Number.isFinite(Number(delayMs)) && delayMs >= 0
|
|
2689
|
+
? Math.min(Number(delayMs), quietMs)
|
|
2690
|
+
: quietMs;
|
|
2691
|
+
p.quietTimer = setTimeout(() => {
|
|
2692
|
+
p.quietTimer = null;
|
|
2693
|
+
try { verifyQuiet(id, armedSeq); } catch { /* 兜底失败不影响主流程 */ }
|
|
2694
|
+
}, wait);
|
|
2695
|
+
if (p.quietTimer && typeof p.quietTimer.unref === 'function') p.quietTimer.unref();
|
|
2696
|
+
}
|
|
2537
2697
|
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2698
|
+
/**
|
|
2699
|
+
* 静默期到点:先验"期间没人动过"(防 ABA),再重跑判据,全干净才播。
|
|
2700
|
+
*
|
|
2701
|
+
* ABA 场景:窗口期内"瞬间启动又结束"(子代理刚被唤醒又立刻 idle),
|
|
2702
|
+
* 前后快照可能完全一样,只有 activitySeq 能证明"中间动过"。
|
|
2703
|
+
*/
|
|
2704
|
+
function verifyQuiet(id, armedSeq) {
|
|
2705
|
+
const p = pendingDone.get(id);
|
|
2706
|
+
if (!p) return;
|
|
2707
|
+
// 先跑判据,再交给纯函数裁决(顺序由 quietVerdict 保证:先验序号,再认 still-busy)
|
|
2708
|
+
const why = gateHoldReason(id, 'quiet-verify');
|
|
2709
|
+
const verdict = quietVerdict({
|
|
2710
|
+
armedSeq, activitySeq, holdReason: why, quietMs: quietMsOf(),
|
|
2711
|
+
});
|
|
2712
|
+
if (verdict === 'quiet-disabled') {
|
|
2713
|
+
// 挂起期间用户把静默期关了 → 回到即时放行(一步回退不卡死)
|
|
2714
|
+
flushPending(id, { quiet: 0, resets: p.quietResets || 0 });
|
|
2715
|
+
return;
|
|
2716
|
+
}
|
|
2717
|
+
if (verdict === 'activity-since-arm') {
|
|
2718
|
+
// 起窗后动过(含 ABA:瞬间启动又结束,快照看不出差别)→ 重新计时
|
|
2719
|
+
p.quietResets = (p.quietResets || 0) + 1;
|
|
2720
|
+
appendNotifyLog('[idle-gate] quiet reset '
|
|
2721
|
+
+ `sess=${sessionKey(p.session)} seq=${armedSeq}->${activitySeq}`, { detail: true });
|
|
2722
|
+
armQuiet(id);
|
|
2723
|
+
return;
|
|
2724
|
+
}
|
|
2725
|
+
if (verdict === 'still-busy') {
|
|
2726
|
+
// 这 1.5 秒里又忙起来了:**短退避**再看一眼,而不是"撂挑子等事件"。
|
|
2727
|
+
// 原因:gateHoldReason 可能返回 agent-unavailable / turn-boundary-unavailable
|
|
2728
|
+
// 这类"读不到"的原因,而它们**没有**必然后续边沿事件 → 一旦只等事件,
|
|
2729
|
+
// 这条挂起项就饿到 10 分钟超时兜底(那时播的是进度提醒,不是完成语音)。
|
|
2730
|
+
// 退避取 min(quietMs, 500):既是轮询的安全上限,也不会退化成高频空转。
|
|
2731
|
+
appendNotifyLog(`[idle-gate] quiet keep sess=${sessionKey(p.session)} reason=${why}`, { detail: true });
|
|
2732
|
+
armQuiet(id, quietBackoffMs(quietMsOf()));
|
|
2733
|
+
return;
|
|
2734
|
+
}
|
|
2735
|
+
flushPending(id, { quiet: quietMsOf(), resets: p.quietResets || 0 });
|
|
2736
|
+
}
|
|
2544
2737
|
|
|
2545
|
-
|
|
2546
|
-
|
|
2738
|
+
function reevaluatePending(trigger) {
|
|
2739
|
+
if (pendingDone.size === 0) return;
|
|
2740
|
+
for (const id of [...pendingDone.keys()]) {
|
|
2741
|
+
// 上一项的处理可能已经把它从表里删了(放行后即出表)
|
|
2742
|
+
if (!pendingDone.has(id)) continue;
|
|
2743
|
+
if (gateHoldReason(id, trigger) !== null) continue;
|
|
2744
|
+
// 判据全过 ≠ 立刻播:先进静默期,到点且期间没人动才播(v0.5.11)
|
|
2745
|
+
armQuiet(id);
|
|
2547
2746
|
}
|
|
2548
2747
|
}
|
|
2549
2748
|
|
|
@@ -2865,9 +3064,22 @@ function hasVoiceLine(psKind) {
|
|
|
2865
3064
|
}
|
|
2866
3065
|
}, { global: true });
|
|
2867
3066
|
|
|
3067
|
+
// ── 收件箱入队边沿:消息进队列 = "马上要干活"的最早信号(v0.5.11)─────────
|
|
3068
|
+
// 为什么需要它:交接那条结算消息走的正是 inbox 投递,它比 agent/status 晚
|
|
3069
|
+
// 139~172ms(见 armQuiet 的考古注释)。父代理从 idle 被唤醒之前,先有这个
|
|
3070
|
+
// 边沿;只靠"父代理变 running"来兜底太晚 —— 静默窗若恰好在这一瞬到点,
|
|
3071
|
+
// 就会在消息落地前放行。多这一路信号是正交的:不认子代理、不看 jobs,
|
|
3072
|
+
// 只认"有东西进了收件箱"。
|
|
3073
|
+
ctx.on('agent/inbox/inserted', () => {
|
|
3074
|
+
try { markActivity(); } catch { /* 记账失败不该影响投递本身 */ }
|
|
3075
|
+
}, { global: true });
|
|
3076
|
+
|
|
2868
3077
|
// ── agent 状态边沿:running→idle 判完成;任何变化都重估挂起项 ────────────
|
|
2869
3078
|
// 依据:agent/status 文档「idle means no driver remains scheduled or active」
|
|
2870
|
-
// (dsh-agent/lib/types/runtime-types.d.ts:247
|
|
3079
|
+
// (dsh-agent/lib/types/runtime-types.d.ts:247)。
|
|
3080
|
+
// ⚠️ v0.5.11 修正:旧注释把"子代理 idle"当成唤醒信号,那正是误播的根源 ——
|
|
3081
|
+
// 子代理 idle 时父代理的收件箱**还没**收到结算消息,此刻的快照看起来是
|
|
3082
|
+
// 真空闲。现在它只算"一次活动"(重置静默窗),不再等于"可以播了"。
|
|
2871
3083
|
ctx.on('agent/status', (payload) => {
|
|
2872
3084
|
try {
|
|
2873
3085
|
const agent = payload && payload.agent;
|
|
@@ -2877,6 +3089,9 @@ function hasVoiceLine(psKind) {
|
|
|
2877
3089
|
const prev = prevStatus.get(id);
|
|
2878
3090
|
// 有界记账(值带时间戳,供 TTL 清理用;读边沿时取 .status)
|
|
2879
3091
|
rememberBounded(prevStatus, id, { status, at: Date.now() }, Date.now(), (v) => v && v.at);
|
|
3092
|
+
// v0.5.11:只有**状态值真的变了**才算一次活动(静默窗的重置信号)。
|
|
3093
|
+
// 重复推送同一状态不能重置窗口,否则窗口永远等不到尾沿。
|
|
3094
|
+
if (!prev || prev.status !== status) markActivity();
|
|
2880
3095
|
// ⚠️ v0.4.65:done-edge 事件**不能**先 reevaluate,否则同一事件里
|
|
2881
3096
|
// 会先 flush 旧挂起项(播一次)、再走 handleDone(又播一次)——
|
|
2882
3097
|
// 2026-10-19 实测用户听到两个语音,就是这么来的。
|
|
@@ -2958,6 +3173,9 @@ function hasVoiceLine(psKind) {
|
|
|
2958
3173
|
ctx.effect(() => js.events.subscribe({ owners: 'all' }, (event) => {
|
|
2959
3174
|
// 输出流事件太频繁,与"任务是否结束"无关,直接忽略。
|
|
2960
3175
|
if (event && event.type === 'output') return;
|
|
3176
|
+
// v0.5.11:jobs 的每个非 output 事件都是状态边沿(启动/结束/中止)
|
|
3177
|
+
// → 重置静默窗。output 已在上行 return,不会被高频输出饿死窗口。
|
|
3178
|
+
markActivity();
|
|
2961
3179
|
// ⚠️ v0.4.66(第 2 批 P0-②):后台 job 失败要有**独立提醒**。
|
|
2962
3180
|
// 原先这里只调 onAny()(重估空闲门),完全不看 job 的结算状态,
|
|
2963
3181
|
// 于是「后台任务失败、主回合没产生 error」时用户听不到任何声音。
|
|
@@ -2979,7 +3197,13 @@ function hasVoiceLine(psKind) {
|
|
|
2979
3197
|
|
|
2980
3198
|
// ── 卸载清理:挂起计时器与状态(监听器由插件 fiber 自动回收)──────────────
|
|
2981
3199
|
ctx.effect(() => () => {
|
|
2982
|
-
for (const p of pendingDone.values())
|
|
3200
|
+
for (const p of pendingDone.values()) {
|
|
3201
|
+
clearTimeout(p.timer);
|
|
3202
|
+
// v0.5.11:静默期定时器必须一起清 —— 热重载时旧实例若留着它会
|
|
3203
|
+
// 在卸载后照样 flush 一次(幽灵播报),用户以为"插件都关了还响"。
|
|
3204
|
+
if (p.recheckTimer) clearTimeout(p.recheckTimer);
|
|
3205
|
+
if (p.quietTimer) clearTimeout(p.quietTimer);
|
|
3206
|
+
}
|
|
2983
3207
|
pendingDone.clear();
|
|
2984
3208
|
prevStatus.clear();
|
|
2985
3209
|
lastTurnEnd.clear();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-notify-say",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.11",
|
|
4
4
|
"description": "Desktop notifications for DSH Desktop that actually speak: Windows TTS out of the box (zero download), VOICEVOX voice packs optional, a selectable system-sound fallback, and an idle gate that waits for running subagents before saying the turn is done. Includes a settings-page GUI (sound source, per-event overrides, jump-back-to-session, cooldowns) and a self hot-reload for development.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 空闲门契约测试(防回归:这些规则每一条都对应一次真实事故)。
|
|
3
3
|
*
|
|
4
|
-
* ##
|
|
4
|
+
* ## 历史
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* 所以契约测试要**反过来断言:这个判定必须不存在**(防止有人再加回去)。
|
|
6
|
+
* · v0.4.65:删除 `lastTurn` 递增判定(被实测证伪)→ K3 反过来断言它不许回来。
|
|
7
|
+
* · v0.5.11:把"看到空闲就播"改成"静默期判定"(尾沿防抖)。原因:DSH 的
|
|
8
|
+
* `agent/status=idle` 比结算消息投递早 139~172ms,那一刻从任何单一快照看
|
|
9
|
+
* 都是"真空闲",于是旧实现立刻播「完成」,而父代理随即被叫起来继续干
|
|
10
|
+
* (用户原话:"主代理还没开始,这个时候它会播报一次,这应该算是误播")。
|
|
11
|
+
* 对应新增 K13~K18,并把 K1/K2/K4/K5/K7/K12 的锚点从 reevaluatePending
|
|
12
|
+
* 挪到 gateHoldReason / 新签名上。
|
|
14
13
|
*/
|
|
15
14
|
import { test } from 'node:test';
|
|
16
15
|
import assert from 'node:assert/strict';
|
|
@@ -18,6 +17,8 @@ import { readFileSync } from 'node:fs';
|
|
|
18
17
|
import { fileURLToPath } from 'node:url';
|
|
19
18
|
import { dirname, join } from 'node:path';
|
|
20
19
|
|
|
20
|
+
import { quietVerdict, quietBackoffMs } from '../lib/index.js';
|
|
21
|
+
|
|
21
22
|
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
22
23
|
const INDEX = readFileSync(join(ROOT, 'lib/index.js'), 'utf8');
|
|
23
24
|
|
|
@@ -71,7 +72,7 @@ function fnBody(sig) {
|
|
|
71
72
|
|
|
72
73
|
// ══ 已删除的规则(反断言,防回归)══════════════════════════════════════
|
|
73
74
|
test('K3: 不得再用 lastTurn 递增判定「用户新消息」(已实测证伪)', () => {
|
|
74
|
-
const body = fnBody('function
|
|
75
|
+
const body = fnBody('function gateHoldReason(id, trigger) {');
|
|
75
76
|
assert.doesNotMatch(body, /tb\.lastTurn\s*>\s*p\.lastTurn/,
|
|
76
77
|
'lastTurn 递增不能当用户输入证据 —— 主 Agent 处理子任务返回时内部续跑也会递增');
|
|
77
78
|
});
|
|
@@ -84,35 +85,42 @@ test('K3b: 不得出现「pending dropped: new turn started」的日志', () =>
|
|
|
84
85
|
|
|
85
86
|
// ══ 保留的规则 ═════════════════════════════════════════════════════════
|
|
86
87
|
test('K1: 不得因 agent.status === running 就 clearPending', () => {
|
|
87
|
-
const body = fnBody('function
|
|
88
|
+
const body = fnBody('function gateHoldReason(id, trigger) {');
|
|
88
89
|
assert.doesNotMatch(body, /agent\.status\s*===\s*'running'\)\s*\{\s*clearPending/,
|
|
89
90
|
'主 Agent 处理子任务返回时也是 running(T3 的规则)');
|
|
90
91
|
});
|
|
91
92
|
|
|
92
|
-
test('K2: agent
|
|
93
|
-
const body = fnBody('function
|
|
93
|
+
test('K2: agent 拿不到时必须保留(返回原因码),不能 flush 也不能进静默期', () => {
|
|
94
|
+
const body = fnBody('function gateHoldReason(id, trigger) {');
|
|
94
95
|
const idx = body.indexOf('if (!agent)');
|
|
95
96
|
assert.ok(idx > 0, '要有 agent 判空');
|
|
96
97
|
const seg = body.slice(idx, idx + 400);
|
|
97
|
-
assert.match(seg, /
|
|
98
|
+
assert.match(seg, /return 'agent-unavailable'/, 'agent 读不到 ≠ 完成,必须保留(T12)');
|
|
98
99
|
assert.doesNotMatch(seg, /flushPending/, 'agent 读不到时绝不能 flush');
|
|
100
|
+
assert.doesNotMatch(seg, /armQuiet/, 'agent 读不到时也不能进静默期(白等一轮还会播错)');
|
|
99
101
|
});
|
|
100
102
|
|
|
101
103
|
test('K4: 轮次还开着(open !== null)→ 保留,不播', () => {
|
|
102
|
-
const body = fnBody('function
|
|
104
|
+
const body = fnBody('function gateHoldReason(id, trigger) {');
|
|
103
105
|
assert.match(body, /tb\.open\s*!==\s*null/, '要有"轮次进行中"判定(T3)');
|
|
104
106
|
const idx = body.indexOf('tb.open !== null');
|
|
105
107
|
const seg = body.slice(idx, idx + 200);
|
|
106
|
-
assert.match(seg, /
|
|
108
|
+
assert.match(seg, /return 'turn-open'/, '轮次开着要继续等');
|
|
107
109
|
});
|
|
108
110
|
|
|
109
|
-
test('K5:
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
assert.
|
|
114
|
-
assert.
|
|
115
|
-
|
|
111
|
+
test('K5: 判据全过也不许直接播 —— 必须先过静默期(v0.5.11)', () => {
|
|
112
|
+
// ① 判据本身必须是纯判定,不许顺手播报(否则"到点重跑判据"会播两次)
|
|
113
|
+
const hold = fnBody('function gateHoldReason(id, trigger) {');
|
|
114
|
+
assert.match(hold, /inflight\.length > 0/, '要有在飞判定(T7)');
|
|
115
|
+
assert.doesNotMatch(hold, /flushPending/, 'gateHoldReason 只回答"能不能播",不许产生副作用');
|
|
116
|
+
assert.doesNotMatch(hold, /armQuiet/, '同上:起窗不是判据的职责');
|
|
117
|
+
|
|
118
|
+
// ② 重估路径必须先进静默期,绝不能再出现"判据过了就 flush"
|
|
119
|
+
const re = fnBody('function reevaluatePending(trigger) {');
|
|
120
|
+
assert.match(re, /gateHoldReason\(id, trigger\) !== null\) continue;/, '先跑判据');
|
|
121
|
+
assert.match(re, /armQuiet\(id\)/, '判据全过 → 进静默期');
|
|
122
|
+
assert.doesNotMatch(re, /flushPending/,
|
|
123
|
+
'静默期内直接播报就是误播入口(DSH 先 idle、139ms 后才投递结算消息)');
|
|
116
124
|
});
|
|
117
125
|
|
|
118
126
|
test('K6: 超时走 onIdleGateTimeout(进度提醒),不直接消耗完成通知', () => {
|
|
@@ -129,11 +137,15 @@ test('K6: 超时走 onIdleGateTimeout(进度提醒),不直接消耗完成
|
|
|
129
137
|
'进度提醒**不得**消耗 pending —— 真正完成时还要再播一次');
|
|
130
138
|
});
|
|
131
139
|
|
|
132
|
-
test('K7: flushPending 不再接受 viaTimeout
|
|
133
|
-
const body = fnBody('function flushPending(id) {');
|
|
140
|
+
test('K7: flushPending 不再接受 viaTimeout;但必须记录静默期账目', () => {
|
|
141
|
+
const body = fnBody('function flushPending(id, quiet) {');
|
|
134
142
|
assert.doesNotMatch(body, /viaTimeout/,
|
|
135
143
|
'viaTimeout 已拆到 onIdleGateTimeout,flushPending 只管"真完成"');
|
|
136
144
|
assert.match(body, /worktree-idle/, 'flush 的 reason 固定是 worktree-idle');
|
|
145
|
+
// v0.5.11:误播/延迟排查全靠这两个字段
|
|
146
|
+
assert.match(body, /quiet=\$\{quietMs\}/, '日志要记 quiet=<ms>');
|
|
147
|
+
assert.match(body, /resets=\$\{resets\}/, '日志要记 resets=<次数>');
|
|
148
|
+
assert.match(body, /clearTimeout\(p\.quietTimer\)/, '放行时必须把静默期定时器一并清掉');
|
|
137
149
|
});
|
|
138
150
|
|
|
139
151
|
test('K8: turnBoundaryOf 读不到要返回 null', () => {
|
|
@@ -175,6 +187,166 @@ test('K12: 两条播报路径必须用同一个 evKey(同一轮互斥)', ()
|
|
|
175
187
|
assert.match(sched, /evKey,/, 'pending 要存 evKey');
|
|
176
188
|
|
|
177
189
|
// flushPending 用 pending 里存的 evKey
|
|
178
|
-
const flush = fnBody('function flushPending(id) {');
|
|
190
|
+
const flush = fnBody('function flushPending(id, quiet) {');
|
|
179
191
|
assert.match(flush, /p\.evKey/, 'flush 要用 pending 里的 evKey');
|
|
180
|
-
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// ══ v0.5.11:静默期(尾沿防抖)══════════════════════════════════════════
|
|
195
|
+
test('K13: 静默期只用单调 activitySeq,不读墙钟算剩余时间', () => {
|
|
196
|
+
const mark = fnBody('function markActivity() {');
|
|
197
|
+
assert.match(mark, /activitySeq \+= 1/, '活动边沿必须递增序号');
|
|
198
|
+
assert.doesNotMatch(mark, /Date\.now/, '不得用墙钟:校时/改表会让窗口瞬间到期或永不到期');
|
|
199
|
+
// 重置点必须真的把窗口清掉,否则"起窗后动过"仍会按时放行
|
|
200
|
+
assert.match(mark, /clearTimeout\(p\.quietTimer\)/, '记活动时必须清掉旧窗口');
|
|
201
|
+
|
|
202
|
+
for (const sig of ['function armQuiet(id, delayMs) {', 'function verifyQuiet(id, armedSeq) {']) {
|
|
203
|
+
assert.doesNotMatch(fnBody(sig), /Date\.now/, sig + ' 不得读墙钟');
|
|
204
|
+
}
|
|
205
|
+
const arm = fnBody('function armQuiet(id, delayMs) {');
|
|
206
|
+
assert.match(arm, /const armedSeq = activitySeq;/, '起窗时要记下当时的序号');
|
|
207
|
+
assert.match(arm, /setTimeout\(/, '起窗要挂定时器');
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('K14: 静默期到点必须"先验序号、再重跑判据",才允许放行', () => {
|
|
211
|
+
const body = fnBody('function verifyQuiet(id, armedSeq) {');
|
|
212
|
+
assert.match(body, /quietVerdict\(/, '裁决必须走纯函数(ABA 才能被单测覆盖)');
|
|
213
|
+
assert.match(body, /armedSeq, activitySeq,/, '要把起窗序号与当前序号一起交给裁决');
|
|
214
|
+
assert.match(body, /gateHoldReason\(id, 'quiet-verify'\)/, '到点必须重跑判据 —— 窗口安静 ≠ 状态干净');
|
|
215
|
+
assert.ok(body.indexOf('quietVerdict') < body.indexOf('flushPending'),
|
|
216
|
+
'必须先裁决再放行');
|
|
217
|
+
// 反断言:放行必须带裁决结果(quiet/resets),不能是"到点就播"
|
|
218
|
+
const flushes = body.match(/flushPending\(id[^)]*\)/g) || [];
|
|
219
|
+
assert.equal(flushes.length, 2, '只允许两个放行点:quiet-disabled 与 release');
|
|
220
|
+
for (const f of flushes) {
|
|
221
|
+
assert.match(f, /quiet:/, '每次放行都要把静默期账目传给日志(' + f + ')');
|
|
222
|
+
}
|
|
223
|
+
assert.ok(body.lastIndexOf('verdict') < body.lastIndexOf('flushPending'),
|
|
224
|
+
'最后一次放行必须在裁决之后');
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test('K15: markActivity 只挂在"状态真的变了"的边沿(防心跳饿死窗口)', () => {
|
|
228
|
+
// ① agent/status:必须先判状态值变了,再记活动
|
|
229
|
+
// 注:这个 handler 以 `}, { global: true });` 收尾,fnBody 的"最后一行只有 }"
|
|
230
|
+
// 护栏会误判,所以这里直接切片。
|
|
231
|
+
const stAt = INDEX.indexOf("ctx.on('agent/status', (payload) => {");
|
|
232
|
+
assert.ok(stAt > 0, '应能找到 agent/status 处理器');
|
|
233
|
+
const stEnd = INDEX.indexOf('{ global: true });', stAt);
|
|
234
|
+
assert.ok(stEnd > stAt, '应能找到 agent/status 处理器结尾');
|
|
235
|
+
const st = INDEX.slice(stAt, stEnd);
|
|
236
|
+
const callIdx = st.indexOf('markActivity()');
|
|
237
|
+
assert.ok(callIdx > 0, 'agent/status 要记活动');
|
|
238
|
+
assert.match(st.slice(0, callIdx), /prev\.status !== status/,
|
|
239
|
+
'必须先判"状态值变了"再记活动;重复推送同一状态不能重置窗口');
|
|
240
|
+
|
|
241
|
+
// ② jobs 事件:output 洪流必须先被挡掉
|
|
242
|
+
const jobsAt = INDEX.indexOf("js.events.subscribe({ owners: 'all' }");
|
|
243
|
+
assert.ok(jobsAt > 0, '应能找到 jobs 订阅');
|
|
244
|
+
const jobs = INDEX.slice(jobsAt, jobsAt + 1200);
|
|
245
|
+
const outIdx = jobs.indexOf("event.type === 'output'");
|
|
246
|
+
const actIdx = jobs.indexOf('markActivity()');
|
|
247
|
+
assert.ok(outIdx > 0, 'jobs 订阅要先过滤 output');
|
|
248
|
+
assert.ok(actIdx > outIdx, 'markActivity 必须在 output 过滤之后(否则高频输出永不收尾)');
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test('K16: 卸载/热重载必须清掉静默期定时器(防幽灵播报)', () => {
|
|
252
|
+
const at = INDEX.indexOf('ctx.effect(() => () => {');
|
|
253
|
+
assert.ok(at > 0, '应能找到卸载清理路径');
|
|
254
|
+
const seg = INDEX.slice(at, at + 900);
|
|
255
|
+
assert.match(seg, /clearTimeout\(p\.quietTimer\)/, '卸载清理必须清 quietTimer');
|
|
256
|
+
|
|
257
|
+
const clears = INDEX.split('clearTimeout(p.quietTimer)').length - 1;
|
|
258
|
+
assert.ok(clears >= 3,
|
|
259
|
+
`quietTimer 至少在 markActivity / clearPending / flushPending / dispose 里清理(实际 ${clears} 处)`);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test('K17: idleGateQuietMs=0 必须退回 v0.5.10 的即时释放(一步回退开关)', () => {
|
|
263
|
+
const body = fnBody('function armQuiet(id, delayMs) {');
|
|
264
|
+
assert.match(body, /if \(!\(quietMs > 0\)\)/,
|
|
265
|
+
'判据要写成 !(quietMs > 0),别用 === 0 —— 负数/NaN 也得进兜底');
|
|
266
|
+
const at = body.indexOf('!(quietMs > 0)');
|
|
267
|
+
assert.match(body.slice(at, at + 260), /flushPending\(id/, '关闭时立刻放行,不许卡住不播');
|
|
268
|
+
// 而且必须有注释/常量来源,免得后人以为是魔法数
|
|
269
|
+
assert.match(body, /quietMsOf\(\)/, '静默期长度要从配置读(不是写死 1500)');
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test('K18: quietVerdict —— ABA 必须判 activity-since-arm,顺序不许换', () => {
|
|
273
|
+
const base = { armedSeq: 7, activitySeq: 7, holdReason: null, quietMs: 1500 };
|
|
274
|
+
assert.equal(quietVerdict(base), 'release', '期间无人动 + 判据全干净 → 放行');
|
|
275
|
+
|
|
276
|
+
// ABA:期间"瞬间启动又结束",前后快照一样,只有序号能证明中间动过
|
|
277
|
+
assert.equal(quietVerdict({ ...base, activitySeq: 8 }), 'activity-since-arm',
|
|
278
|
+
'序号前进 = 期间动过 → 必须重新计时,绝不能放行');
|
|
279
|
+
|
|
280
|
+
assert.equal(quietVerdict({ ...base, holdReason: 'inflight' }), 'still-busy');
|
|
281
|
+
assert.equal(quietVerdict({ ...base, quietMs: 0 }), 'quiet-disabled', '0 = 关闭');
|
|
282
|
+
assert.equal(quietVerdict({ ...base, quietMs: Number.NaN }), 'quiet-disabled', 'NaN 按关闭处理');
|
|
283
|
+
|
|
284
|
+
// 顺序:动过优先于"仍然忙"
|
|
285
|
+
assert.equal(quietVerdict({ ...base, activitySeq: 9, holdReason: 'turn-open' }),
|
|
286
|
+
'activity-since-arm', '先验序号再认 busy —— 否则"动过又回到忙"会被误判成普通等待');
|
|
287
|
+
|
|
288
|
+
// 脏输入不许抛
|
|
289
|
+
assert.equal(quietVerdict(null), 'quiet-disabled');
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// ── v0.5.11 复审补强(独立审查 2026-11-27 指出的三处「窗口会丢掉」)──────────
|
|
293
|
+
|
|
294
|
+
test('K19: markActivity 清掉窗口后必须重新起窗(否则饿到 10 分钟超时)', () => {
|
|
295
|
+
const body = fnBody('function markActivity() {');
|
|
296
|
+
// ① 序号必须 +1(静默期只认序号,不读墙钟)
|
|
297
|
+
assert.match(body, /activitySeq \+= 1/, '活动必须推进 activitySeq');
|
|
298
|
+
assert.doesNotMatch(body, /Date\.now\(\)/,
|
|
299
|
+
'静默期不许读墙钟算时间(墙钟会跳变;活动序号才是单调的)');
|
|
300
|
+
// ② 清定时器时必须记账,"清过"和"没清过"要走不同路径
|
|
301
|
+
assert.match(body, /cleared/, '清定时器要留痕,否则无法决定是否重估');
|
|
302
|
+
// ③ 清完必须异步重跑判据 → 重新起窗。只清不挂 = 这条挂起项失去窗口。
|
|
303
|
+
assert.match(body, /queueMicrotask\(/,
|
|
304
|
+
'清窗口后必须排一次重估(同步重跑会在事件回调里递归走完整判据)');
|
|
305
|
+
assert.match(body, /reevaluatePending\(/, '重估入口不许绕过 reevaluatePending');
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test('K20: 复查发现仍忙 → 短退避重挂,不许「撂挑子等事件」', () => {
|
|
309
|
+
const body = fnBody('function verifyQuiet(id, armedSeq) {');
|
|
310
|
+
const at = body.indexOf("verdict === 'still-busy'");
|
|
311
|
+
assert.ok(at > 0, 'still-busy 分支必须存在');
|
|
312
|
+
const seg = body.slice(at, at + 700);
|
|
313
|
+
const armAt = seg.indexOf('armQuiet(id, quietBackoffMs(');
|
|
314
|
+
assert.ok(armAt > 0,
|
|
315
|
+
'still-busy 必须短退避重挂:agent-unavailable / turn-boundary-unavailable '
|
|
316
|
+
+ '这类原因没有必然后续边沿事件,只等事件会饿到超时兜底(超时播的是进度提醒)');
|
|
317
|
+
// 允许 return,但它必须在重挂之后 —— 先 return 就等于"等下一个事件"
|
|
318
|
+
const firstReturn = seg.indexOf('return;');
|
|
319
|
+
assert.ok(firstReturn < 0 || armAt < firstReturn,
|
|
320
|
+
'still-busy 分支必须先重挂再 return,不许裸 return(那就是「等下一个事件」)');
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test('K21: 超时复查放行也要记 quiet/resets 账目(否则 quiet=0 语义被污染)', () => {
|
|
324
|
+
const body = fnBody('function onIdleGateTimeout(id) {');
|
|
325
|
+
assert.match(body, /flushPending\(id, \{ quiet: quietMsOf\(\)/,
|
|
326
|
+
'超时复查确认真空闲后的放行,必须如实传 quiet(quiet=0 专指「静默期没参与」)');
|
|
327
|
+
// 全文件不得再有裸 flushPending(id) —— 那会恒记 quiet=0 resets=0
|
|
328
|
+
const bare = INDEX.split(/flushPending\(id\);|\n\s*flushPending\(id\)\n/).length - 1;
|
|
329
|
+
assert.equal(bare, 0, `不允许裸 flushPending(id)(实测 ${bare} 处)—— 账目会记成 quiet=0`);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
test('K22: idleGateQuietMs 必须三处登记(漏一处 = 用户改了不生效)', () => {
|
|
333
|
+
const store = readFileSync(join(ROOT, 'lib/config-store.js'), 'utf8');
|
|
334
|
+
const schema = readFileSync(join(ROOT, 'lib/config-schema.js'), 'utf8');
|
|
335
|
+
assert.match(store, /^\s*idleGateQuietMs:\s*\(v\)/m,
|
|
336
|
+
'config-store.js 的 RULES 必须收这个键:白名单外的键保存时被 sanitize 丢弃');
|
|
337
|
+
assert.match(store, /idleGateQuietMs:\s*Math\.round/,
|
|
338
|
+
'config-store.js 的客户端形状映射必须回传它');
|
|
339
|
+
assert.match(INDEX, /idleGateQuietMs:\s*Math\.round/,
|
|
340
|
+
'effectiveForClient 必须映射它,否则前端 GET 拿不到(v0.4.18 同类事故)');
|
|
341
|
+
assert.match(schema, /^\s*idleGateQuietMs:/m, 'config-schema 的 FIELD_DEFAULTS 要有它');
|
|
342
|
+
assert.match(INDEX, /out\.idleGateQuietMs\s*=/, 'normalizeConfig 要算它');
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test('K23: quietBackoffMs —— 上限 500ms 且不超过静默期本身', () => {
|
|
346
|
+
assert.equal(quietBackoffMs(1500), 500, '默认 1500 → 退避 500');
|
|
347
|
+
assert.equal(quietBackoffMs(300), 300, '静默期比上限短时取静默期(不许比它长)');
|
|
348
|
+
assert.equal(quietBackoffMs(0), 0, '0 = 关闭,退避也是 0(由调用方走即时放行)');
|
|
349
|
+
assert.equal(quietBackoffMs(Number.NaN), 0, 'NaN 不许变成 NaN 毫秒定时器');
|
|
350
|
+
assert.equal(quietBackoffMs(-1), 0, '负数按关闭处理');
|
|
351
|
+
assert.equal(quietBackoffMs(60000), 500, '上限 60 秒时退避仍是 500(不许跟着变长)');
|
|
352
|
+
});
|