loop-guard.spec.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import {
  2. LOOP_GUARD_PRESETS,
  3. LoopMessage,
  4. configOf,
  5. detectLoop,
  6. similarity,
  7. } from './loop-guard';
  8. /**
  9. * 自动对话回环检测。
  10. *
  11. * 阈值来自 2026-10-21 的实测数据(两个容器互相对话 28.6 分钟 / 96 条消息 /
  12. * 最长连续 57 条无人类介入),不是拍脑袋定的。
  13. */
  14. describe('loop-guard', () => {
  15. const HUMAN = '@xhvrjtmwqh:fmode.cn';
  16. const A = '@k0nrevcfik:fmode.cn';
  17. const B = '@zgdcmgnqcd:fmode.cn';
  18. const isHuman = (s: string) => s === HUMAN;
  19. function msg(sender: string, body: string, ts: number): LoopMessage {
  20. return { sender, body, ts };
  21. }
  22. /** 造一串连续 agent 消息(模拟互相「收到」「还在处理」) */
  23. function agentBurst(n: number, startTs = 1_000_000, humanFirst = false): LoopMessage[] {
  24. const out: LoopMessage[] = [];
  25. if (humanFirst) out.push(msg(HUMAN, '你们互相测一下', startTs));
  26. const phrases = [
  27. '收到',
  28. '还在处理中,请稍等',
  29. '对方回了,我继续盯着',
  30. '再等一轮',
  31. '结果出来了',
  32. ];
  33. for (let i = 0; i < n; i++) {
  34. out.push(msg(i % 2 ? A : B, phrases[i % phrases.length] + i, startTs + 1000 * (i + 1)));
  35. }
  36. return out;
  37. }
  38. describe('预设与配置', () => {
  39. it('off 模式返回 null(不检测)', () => {
  40. expect(configOf('off')).toBeNull();
  41. });
  42. it('三档阈值递增', () => {
  43. expect(LOOP_GUARD_PRESETS.strict.maxStreak).toBeLessThan(
  44. LOOP_GUARD_PRESETS.standard.maxStreak,
  45. );
  46. expect(LOOP_GUARD_PRESETS.standard.maxStreak).toBeLessThan(
  47. LOOP_GUARD_PRESETS.loose.maxStreak,
  48. );
  49. });
  50. });
  51. describe('① 连续数 + 内容重复(必须同时满足)', () => {
  52. /**
  53. * 🔴 规则修正(2026-10-21 实测)
  54. *
  55. * 起初「连续 5 条 agent」就触发,结果误伤了正常干活:
  56. * 用户让容器改白名单,它连发 7 条(改配置/diff/重启/验证/汇报),
  57. * 内容各不相同 —— 这是执行过程,不是回环。
  58. *
  59. * 现在的判据:连续数达标 **且** 内容在原地打转。
  60. */
  61. it('🔴 连续 57 条且内容重复(实测回环)→ 判定回环', () => {
  62. const msgs = agentBurst(57);
  63. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, 1_000_000 + 60_000);
  64. expect(v.loop).toBeTrue();
  65. expect(v.streak).toBe(57);
  66. });
  67. it('🔴 连续 7 条但内容各不相同(正常执行任务)→ **不触发**', () => {
  68. const t0 = 1_000_000;
  69. const work = [
  70. [A, '我先备份配置文件'],
  71. [A, '已备份到 .env.bak-p0-20261021'],
  72. [A, '现在修改 MATRIX_ALLOWED_USERS 这一行'],
  73. [A, 'diff 确认只改了 552 行'],
  74. [A, '正在重启 gateway'],
  75. [A, '重启完成,PID 已变'],
  76. [A, 'post-restart 验证通过'],
  77. ] as [string, string][];
  78. const msgs = work.map(([sender, body], i) => msg(sender, body, t0 + i * 10_000));
  79. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 70_000);
  80. expect(v.streak).toBe(7);
  81. expect(v.loop)
  82. .withContext('执行任务的连续消息不应被判为回环')
  83. .toBeFalse();
  84. });
  85. it('只有 3 条不同内容 → 标准模式不触发(连续与重复都未达)', () => {
  86. const t0 = 1_100_000;
  87. const msgs = [
  88. msg(A, '表单校验写好了', t0),
  89. msg(B, '接口加了参数校验', t0 + 5_000),
  90. msg(A, '前端已经接上了', t0 + 10_000),
  91. ];
  92. expect(detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 15_000).loop).toBeFalse();
  93. });
  94. it('🔴 人类说过话后连续数是重新计算的(复读场景)', () => {
  95. // 人类先说一句,然后两个容器开始原地打转
  96. const t0 = 1_050_000;
  97. const msgs: LoopMessage[] = [msg(HUMAN, '你们对接一下接口', t0)];
  98. for (let i = 0; i < 10; i++) {
  99. msgs.push(msg(i % 2 ? A : B, '收到', t0 + (i + 1) * 5_000));
  100. }
  101. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 60_000);
  102. expect(v.streak).toBe(10);
  103. expect(v.loop).toBeTrue();
  104. });
  105. it('人类在中间插话 → 连续数归零', () => {
  106. const msgs = [
  107. ...agentBurst(4),
  108. msg(HUMAN, '停一下', 1_010_000),
  109. ...agentBurst(2, 1_020_000),
  110. ];
  111. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard);
  112. expect(v.streak).toBe(2);
  113. expect(v.loop).toBeFalse();
  114. });
  115. it('严格模式阈值更低:同样 3 条重复内容触发', () => {
  116. const t0 = 1_200_000;
  117. const msgs = [A, B, A].map((s, i) => msg(s, '收到', t0 + i * 5_000));
  118. expect(detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.strict, t0 + 15_000).loop).toBeTrue();
  119. });
  120. });
  121. describe('② 速率检测', () => {
  122. it('一分钟内 15 条(阈值 10)→ 触发', () => {
  123. const now = 2_000_000;
  124. // 连续数故意做小,确保是靠速率触发
  125. const msgs: LoopMessage[] = [];
  126. for (let i = 0; i < 15; i++) msgs.push(msg(A, `消息${i}`, now - 50_000 + i * 1000));
  127. const v = detectLoop(msgs, isHuman, { maxStreak: 100, maxPerMinute: 10, repeatWindow: 99 }, now);
  128. expect(v.loop).toBeTrue();
  129. expect(v.reason).toBe('rate');
  130. });
  131. it('一分钟内 5 条 → 不触发(隔离连续数规则,只测速率)', () => {
  132. const now = 2_000_000;
  133. const msgs: LoopMessage[] = [];
  134. for (let i = 0; i < 5; i++) msgs.push(msg(A, `消息${i}`, now - 20_000 + i * 1000));
  135. // maxStreak 设很大:确保不是被连续数规则误触发
  136. const v = detectLoop(msgs, isHuman, { maxStreak: 100, maxPerMinute: 10, repeatWindow: 99 }, now);
  137. expect(v.loop).toBeFalse();
  138. expect(v.perMinute).toBe(5);
  139. });
  140. });
  141. describe('③ 内容重复检测', () => {
  142. /*
  143. * 🔴 2026-11-13 行为变更:**重复检测不再独立触发**。
  144. *
  145. * 原规则「尾部连续 N 条内容相似就算回环」,不看连续数、也不看人类是否介入。
  146. * 结果容器连续输出几条结构相近的工具行就误报,用户反馈
  147. * 「我正常输出也会这样」。现在重复只在「连续数也达标」时才计入。
  148. */
  149. it('🔴 只有 3 条重复、未达连续数阈值 → 不触发(旧行为会误报)', () => {
  150. const msgs = [
  151. msg(A, '收到', 1),
  152. msg(B, '收到。', 2),
  153. msg(A, '收到!', 3),
  154. ];
  155. const v = detectLoop(msgs, isHuman, { maxStreak: 99, maxPerMinute: 99, repeatWindow: 3 });
  156. expect(v.loop).withContext('未达连续数阈值,不应判回环').toBeFalse();
  157. });
  158. it('🔴 达到连续数阈值 + 内容重复 → 才判回环', () => {
  159. const msgs = Array.from({ length: 6 }, (_, i) => msg(A, `收到${'。'.repeat(i)}`, i));
  160. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard);
  161. expect(v.loop).toBeTrue();
  162. expect(v.reason).toBe('streak');
  163. });
  164. it('内容各不相同的三条 → 不触发', () => {
  165. const msgs = [
  166. msg(A, '我把登录页的样式调好了', 1),
  167. msg(B, '后端接口我加了校验', 2),
  168. msg(A, '那我把前端接上', 3),
  169. ];
  170. const v = detectLoop(msgs, isHuman, { maxStreak: 99, maxPerMinute: 99, repeatWindow: 3 });
  171. expect(v.loop).toBeFalse();
  172. });
  173. });
  174. describe('边界', () => {
  175. it('空消息列表 → 不触发', () => {
  176. const v = detectLoop([], isHuman, LOOP_GUARD_PRESETS.standard);
  177. expect(v.loop).toBeFalse();
  178. expect(v.streak).toBe(0);
  179. });
  180. it('off 模式 → 永远不触发(用户明确选择不限)', () => {
  181. const v = detectLoop(agentBurst(100), isHuman, null);
  182. expect(v.loop).toBeFalse();
  183. });
  184. it('全是人类消息 → 连续数为 0', () => {
  185. const msgs = [msg(HUMAN, 'a', 1), msg(HUMAN, 'b', 2)];
  186. expect(detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard).streak).toBe(0);
  187. });
  188. });
  189. describe('similarity 工具', () => {
  190. it('完全相同 → 1', () => {
  191. expect(similarity('收到', '收到')).toBe(1);
  192. });
  193. it('完全不相关 → 接近 0', () => {
  194. expect(similarity('登录页样式', '后端接口校验')).toBeLessThan(0.2);
  195. });
  196. it('标点差异不影响相似度', () => {
  197. expect(similarity('收到', '收到。')).toBeGreaterThan(0.9);
  198. });
  199. });
  200. describe('🔴 真实协作场景(防止误伤正常对话)', () => {
  201. it('人类下达任务、两容器交替干活、人类偶尔确认 → 不触发', () => {
  202. const t0 = 5_000_000;
  203. const seq: [string, string][] = [
  204. [HUMAN, '登录页做好了吗,前端和后端对接一下'],
  205. [A, '我这边表单校验写好了'],
  206. [B, '接口我加了 /api/login,参数是 phone 和 code'],
  207. [A, '收到,我把请求接上'],
  208. [B, '联调通过,你可以试一下'],
  209. [HUMAN, '好,我看看'],
  210. [A, '顺便把错误提示也补了'],
  211. ];
  212. const msgs = seq.map(([sender, body], i) => msg(sender, body, t0 + i * 30_000));
  213. // 逐条喂入,任何一步都不应触发
  214. for (let i = 1; i <= msgs.length; i++) {
  215. const v = detectLoop(msgs.slice(0, i), isHuman, LOOP_GUARD_PRESETS.standard, msgs[i - 1].ts);
  216. expect(v.loop)
  217. .withContext(`第 ${i} 条被误判:${v.detail}`)
  218. .toBeFalse();
  219. }
  220. });
  221. it('连续 5 条但每条内容不同(在干活)→ 不触发', () => {
  222. const t0 = 6_000_000;
  223. const five = [A, B, A, B, A].map((s, i) => msg(s, `第${i}条不同的工作内容`, t0 + i * 10_000));
  224. expect(detectLoop(five, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 50_000).loop).toBeFalse();
  225. });
  226. it('连续 5 条且内容重复(在打转)→ 触发', () => {
  227. const t0 = 6_100_000;
  228. const five = [A, B, A, B, A].map((s, i) => msg(s, '收到', t0 + i * 10_000));
  229. expect(detectLoop(five, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 50_000).loop).toBeTrue();
  230. });
  231. });
  232. /*
  233. * 🔴 2026-11-08 真实故障回归(2026-11-09 修复)。
  234. *
  235. * 用户报告「发消息容器不回」。三方采样后发现容器 13 秒后已正常回复,
  236. * 是 LoopGuard 把回复「挡」在了界面提示层:横幅显示「检测到自动对话循环,已暂停」,
  237. * 用户据此以为系统停了。
  238. *
  239. * 根因是 `repeat` 规则在同一条用户消息跨会话重复时触发 —— 用户自己发的
  240. * 内容是用户,不是复读机。
  241. */
  242. describe('🔴 误判回归:用户自己的内容不算回环', () => {
  243. it('三个会话各发一句「你好」,凑到第三句 → 不触发', () => {
  244. const t0 = 7_000_000;
  245. const msgs: LoopMessage[] = [
  246. { sender: HUMAN, body: '你好', ts: t0, fromMe: true },
  247. { sender: HUMAN, body: '你好', ts: t0 + 20_000, fromMe: true },
  248. { sender: HUMAN, body: '你好', ts: t0 + 40_000, fromMe: true },
  249. ];
  250. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 40_000);
  251. expect(v.loop).withContext(v.detail ?? '').toBeFalse();
  252. });
  253. it('我方连发 6 条相同内容 → 不触发(且不进 agent 速率统计)', () => {
  254. const t0 = 7_100_000;
  255. const msgs = Array.from({ length: 6 }, (_, i) => ({
  256. sender: HUMAN,
  257. body: '测试',
  258. ts: t0 + i * 1000,
  259. fromMe: true,
  260. }));
  261. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 6000);
  262. expect(v.loop).toBeFalse();
  263. expect(v.streak).toBe(0);
  264. expect(v.perMinute).toBe(0);
  265. });
  266. it('agent 连发 2 条「收到」+ 我方 2 条相同内容 → 连续段只有 2 条,不触发', () => {
  267. const t0 = 7_200_000;
  268. const msgs: LoopMessage[] = [
  269. { sender: A, body: '收到', ts: t0, fromMe: false },
  270. { sender: A, body: '收到', ts: t0 + 1000, fromMe: false },
  271. { sender: HUMAN, body: '你好', ts: t0 + 2000, fromMe: true },
  272. { sender: HUMAN, body: '你好', ts: t0 + 3000, fromMe: true },
  273. ];
  274. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 3000);
  275. expect(v.loop).withContext(v.detail ?? '').toBeFalse();
  276. expect(v.streak).toBe(0);
  277. });
  278. it('重复内容被人类消息隔断在不同段 → 不触发', () => {
  279. const t0 = 7_300_000;
  280. const msgs: LoopMessage[] = [
  281. { sender: A, body: '收到', ts: t0, fromMe: false },
  282. { sender: A, body: '收到', ts: t0 + 1000, fromMe: false },
  283. { sender: HUMAN, body: '继续', ts: t0 + 2000, fromMe: true },
  284. { sender: A, body: '收到', ts: t0 + 3000, fromMe: false },
  285. { sender: A, body: '收到', ts: t0 + 4000, fromMe: false },
  286. ];
  287. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 4000);
  288. expect(v.loop).withContext(v.detail ?? '').toBeFalse();
  289. });
  290. it('agent 连续 3 条「收到」未达阈值 → 不触发(2026-11-13 收紧口径)', () => {
  291. const t0 = 7_400_000;
  292. const msgs: LoopMessage[] = [
  293. { sender: HUMAN, body: '你好', ts: t0, fromMe: true },
  294. { sender: A, body: '收到', ts: t0 + 1000, fromMe: false },
  295. { sender: A, body: '收到。', ts: t0 + 2000, fromMe: false },
  296. { sender: A, body: '收到!', ts: t0 + 3000, fromMe: false },
  297. ];
  298. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 3000);
  299. /*
  300. * standard 的 maxStreak=5,这里只有 3 条连续 → 不达阈值。
  301. * 旧逻辑会因「内容重复」独立触发而误报。
  302. */
  303. expect(v.loop).toBeFalse();
  304. });
  305. it('agent 连续 6 条相同内容 → 仍能识别(真回环不放过)', () => {
  306. const t0 = 7_400_000;
  307. const msgs: LoopMessage[] = [
  308. { sender: HUMAN, body: '帮我改配置', ts: t0, fromMe: true },
  309. ...Array.from({ length: 6 }, (_, i) => ({
  310. sender: A,
  311. body: '收到',
  312. ts: t0 + 1000 * (i + 1),
  313. fromMe: false as const,
  314. })),
  315. ];
  316. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 7000);
  317. expect(v.loop).toBeTrue();
  318. expect(v.reason).toBe('streak');
  319. });
  320. it('保护期内(quiet=true)不触发,但诊断值照常返回', () => {
  321. const t0 = 7_500_000;
  322. const msgs = [A, A, A].map((s, i) => ({
  323. sender: s,
  324. body: '收到',
  325. ts: t0 + i * 1000,
  326. fromMe: false,
  327. }));
  328. const v = detectLoop(msgs, isHuman, LOOP_GUARD_PRESETS.standard, t0 + 3000, { quiet: true });
  329. expect(v.loop).toBeFalse();
  330. expect(v.streak).toBe(3);
  331. });
  332. });
  333. });