concurrency.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * ConcurrencyController 的纯状态机用例(docs/dynamic-workflow/concurrency.md「The controller」
  3. * 的两行控制器用例)。无时钟:`now` 由用例传入。度量是模型请求(决策 34),阻尼靠 epoch(决策 35),
  4. * 减系数 0.75、K = 4、lastGood 只由完成的 streak 设定(决策 40)。
  5. */
  6. import { describe, expect, it } from "vitest";
  7. import {
  8. CONCURRENCY_FLOOR,
  9. CONCURRENCY_IDLE_RESET_MS,
  10. ConcurrencyController,
  11. } from "../../src/engine/index.js";
  12. const KEY = "anthropic/claude";
  13. // 成功阈值按规格独立写出,避免测试与实现一同漂移。
  14. const K = 4;
  15. const T0 = 1_000_000;
  16. /** 准入 n 个请求,返回它们的 epoch。 */
  17. function admit(c: ConcurrencyController, n: number, now = T0): number[] {
  18. const epochs: number[] = [];
  19. for (let i = 0; i < n; i++) epochs.push(c.admitted(now));
  20. return epochs;
  21. }
  22. /** 准入 n 个请求并让它们全部在当前 epoch 成功;返回期间产生的全部 change。 */
  23. function roundTrip(c: ConcurrencyController, n: number, now = T0) {
  24. const changes = [];
  25. for (let i = 0; i < n; i++) {
  26. const epoch = c.admitted(now);
  27. changes.push(...c.succeeded(now, epoch));
  28. }
  29. return changes;
  30. }
  31. /** 一个当前 epoch 的 429(先准入再限流)。 */
  32. function throttleNow(
  33. c: ConcurrencyController,
  34. reason: "rate_limited" | "provider_overloaded" | "offpeak_queued" = "rate_limited",
  35. retryAfterMs?: number,
  36. now = T0,
  37. ) {
  38. const epoch = c.admitted(now);
  39. return c.throttled(now, epoch, reason, retryAfterMs);
  40. }
  41. describe("ConcurrencyController — 减(决策 4/6/11/35/40)", () => {
  42. it("当前 epoch 的限流 → cap × 0.75 向下取整并翻 epoch;反复限流一路减到地板 1", () => {
  43. const c = new ConcurrencyController(KEY, 16);
  44. expect(c.snapshot().epoch).toBe(0);
  45. // 减 cap 不设 lastGood(未被证明),只记 lastBad = 撞墙时的 cap。
  46. expect(throttleNow(c)).toEqual([
  47. { key: KEY, previous: 16, next: 12, reason: "rate_limited", lastBad: 16 },
  48. ]);
  49. expect(c.snapshot()).toMatchObject({ epoch: 1, lastBad: 16 });
  50. expect(c.snapshot().lastGood).toBeUndefined();
  51. for (const expected of [9, 6, 4, 3, 2, 1]) {
  52. const [change] = throttleNow(c, "provider_overloaded");
  53. expect(change?.next).toBe(expected);
  54. }
  55. expect(c.snapshot()).toMatchObject({
  56. cap: CONCURRENCY_FLOOR,
  57. epoch: 7,
  58. inFlight: 0,
  59. lastBad: 2,
  60. });
  61. // 地板上再限流:cap 不变、无 Retry-After ⇒ 不发事件,但仍翻一页(同一批请求只裁决一次)。
  62. expect(throttleNow(c)).toEqual([]);
  63. expect(c.snapshot().epoch).toBe(8);
  64. });
  65. it("同一批 13 个同 epoch 的 429 只减一次、只发一条事件;其余只清 streak、减 inFlight", () => {
  66. const c = new ConcurrencyController(KEY, 13);
  67. const epochs = admit(c, 13);
  68. expect(new Set(epochs)).toEqual(new Set([0]));
  69. expect(c.snapshot().inFlight).toBe(13);
  70. const changes = [];
  71. for (const epoch of epochs) changes.push(...c.throttled(T0, epoch, "rate_limited"));
  72. expect(changes).toEqual([
  73. { key: KEY, previous: 13, next: 9, reason: "rate_limited", lastBad: 13 },
  74. ]);
  75. expect(c.snapshot()).toMatchObject({ cap: 9, epoch: 1, inFlight: 0, successStreak: 0 });
  76. });
  77. it("旧 epoch 的 429 不动 cap、不发事件,但清 streak、减 inFlight、刷新 cooldown", () => {
  78. const c = new ConcurrencyController(KEY, 8);
  79. const stale = admit(c, 3); // epoch 0
  80. throttleNow(c); // 8→6, epoch 1
  81. c.waiters(T0, 1);
  82. roundTrip(c, K - 1); // 当前 epoch 的成功:streak K−1(还差一次兑现)
  83. expect(c.snapshot()).toMatchObject({ cap: 6, successStreak: K - 1, inFlight: 3 });
  84. expect(c.throttled(T0, stale[0]!, "rate_limited", 9_000)).toEqual([]);
  85. expect(c.snapshot()).toMatchObject({
  86. cap: 6,
  87. epoch: 1,
  88. successStreak: 0,
  89. inFlight: 2,
  90. cooldownUntil: T0 + 9_000,
  91. });
  92. // 更早的 Retry-After 不会把 cooldown 往前拉。
  93. expect(c.throttled(T0, stale[1]!, "rate_limited", 1_000)).toEqual([]);
  94. expect(c.snapshot().cooldownUntil).toBe(T0 + 9_000);
  95. });
  96. it("下一次裁决必须来自新 cap 下发出的请求:旧 epoch 全部撞墙后,新 epoch 的 429 才再减", () => {
  97. const c = new ConcurrencyController(KEY, 13);
  98. const stale = admit(c, 13);
  99. for (const epoch of stale) c.throttled(T0, epoch, "rate_limited"); // 13→9 一次
  100. expect(c.snapshot().cap).toBe(9);
  101. expect(throttleNow(c)).toEqual([
  102. { key: KEY, previous: 9, next: 6, reason: "rate_limited", lastBad: 9 },
  103. ]);
  104. });
  105. it("Retry-After 冻结新准入直到 deadline;地板上带 Retry-After 的限流仍发事件", () => {
  106. const c = new ConcurrencyController(KEY, 4);
  107. const [change] = throttleNow(c, "rate_limited", 20_000);
  108. expect(change).toMatchObject({ previous: 4, next: 3, cooldownMs: 20_000 });
  109. expect(c.canAdmit(T0 + 19_999)).toBe(false);
  110. expect(c.canAdmit(T0 + 20_000)).toBe(true);
  111. throttleNow(c, "rate_limited", undefined, T0 + 20_000); // 3→2
  112. throttleNow(c, "rate_limited", undefined, T0 + 20_000); // 2→1
  113. expect(c.snapshot().cap).toBe(1);
  114. expect(throttleNow(c, "offpeak_queued", 5_000, T0 + 20_001)).toEqual([
  115. { key: KEY, previous: 1, next: 1, reason: "offpeak_queued", lastBad: 1, cooldownMs: 5_000 },
  116. ]);
  117. });
  118. it("canAdmit 在 inFlight ≥ cap 时为假,release 后为真", () => {
  119. const c = new ConcurrencyController(KEY, 2);
  120. const [a, b] = admit(c, 2);
  121. expect(c.canAdmit(T0)).toBe(false);
  122. c.succeeded(T0, a!);
  123. expect(c.canAdmit(T0)).toBe(true);
  124. c.ended(T0);
  125. expect(c.snapshot().inFlight).toBe(0);
  126. void b;
  127. });
  128. it("瞬态非限流失败只清 streak、减 inFlight,cap 不动", () => {
  129. const c = new ConcurrencyController(KEY, 8);
  130. c.waiters(T0, 1);
  131. roundTrip(c, K - 1);
  132. admit(c, 1);
  133. expect(c.snapshot()).toMatchObject({ successStreak: K - 1, inFlight: 1 });
  134. expect(c.failedTransient(T0)).toEqual([]);
  135. expect(c.snapshot()).toMatchObject({ cap: 8, successStreak: 0, inFlight: 0, epoch: 0 });
  136. });
  137. it("ended 只减 inFlight", () => {
  138. const c = new ConcurrencyController(KEY, 8);
  139. c.waiters(T0, 1);
  140. roundTrip(c, K - 1);
  141. admit(c, 2);
  142. expect(c.ended(T0)).toEqual([]);
  143. expect(c.snapshot()).toMatchObject({ successStreak: K - 1, inFlight: 1, cap: 8 });
  144. });
  145. });
  146. describe("ConcurrencyController — 增(决策 4/15/33/35/40)", () => {
  147. function lowered(): ConcurrencyController {
  148. const c = new ConcurrencyController(KEY, 16);
  149. throttleNow(c); // cap 12, lastBad 16, lastGood 缺席, epoch 1
  150. return c;
  151. }
  152. it("K 次当前 epoch 的连续成功证明当前 cap(lastGood = cap),有等待者才 +1", () => {
  153. const c = lowered();
  154. c.waiters(T0, 1);
  155. expect(roundTrip(c, K - 1)).toEqual([]);
  156. expect(c.snapshot().lastGood).toBeUndefined();
  157. expect(roundTrip(c, 1)).toEqual([
  158. { key: KEY, previous: 12, next: 13, reason: "recovered", lastGood: 12, lastBad: 16 },
  159. ]);
  160. expect(c.snapshot().successStreak).toBe(0);
  161. });
  162. it("无等待者时 streak 照累积、lastGood 照证明,但 cap 不兑现;等待者出现后下一次成功即兑现", () => {
  163. const c = lowered();
  164. c.waiters(T0, 0);
  165. expect(roundTrip(c, K)).toEqual([]);
  166. expect(c.snapshot()).toMatchObject({ cap: 12, successStreak: K, lastGood: 12 });
  167. c.waiters(T0, 1);
  168. expect(roundTrip(c, 1)[0]?.next).toBe(13);
  169. });
  170. it("旧 epoch 的成功不计 streak(减 cap 后仍在飞的那批请求跑完不算证据)", () => {
  171. const c = new ConcurrencyController(KEY, 32);
  172. c.waiters(T0, 3);
  173. const stale = admit(c, 32);
  174. c.throttled(T0, stale[0]!, "rate_limited"); // cap 24, epoch 1
  175. for (const epoch of stale.slice(1)) expect(c.succeeded(T0, epoch)).toEqual([]);
  176. expect(c.snapshot()).toMatchObject({ cap: 24, successStreak: 0, inFlight: 0 });
  177. expect(c.snapshot().lastGood).toBeUndefined();
  178. // 新 epoch 的 K 次成功才兑现。
  179. expect(roundTrip(c, K)[0]?.next).toBe(25);
  180. });
  181. it("永不越过天花板;天花板上的完整 streak 仍把它证明为 lastGood", () => {
  182. const c = new ConcurrencyController(KEY, 2);
  183. throttleNow(c); // cap 1
  184. c.waiters(T0, 1);
  185. const changes = roundTrip(c, K * 3);
  186. expect(changes.map((x) => x.next)).toEqual([2]);
  187. expect(c.snapshot()).toMatchObject({ cap: 2, lastGood: 2 });
  188. });
  189. });
  190. describe("ConcurrencyController — 记住可用水位(决策 22/40)", () => {
  191. /** 从 cap 16 减到 12,再一路爬到 target(每级 K 次成功)。 */
  192. function climbTo(target: number): ConcurrencyController {
  193. const c = new ConcurrencyController(KEY, 16);
  194. throttleNow(c);
  195. c.waiters(T0, 1);
  196. while (c.snapshot().cap < target) roundTrip(c, K);
  197. return c;
  198. }
  199. it("lastGood 之上被限流 → 退回 lastGood 并记 lastBad,不按系数减,仍翻 epoch", () => {
  200. const c = climbTo(14);
  201. expect(c.snapshot()).toMatchObject({ cap: 14, lastGood: 13, epoch: 1 });
  202. expect(throttleNow(c)).toEqual([
  203. { key: KEY, previous: 14, next: 13, reason: "rate_limited", lastGood: 13, lastBad: 14 },
  204. ]);
  205. expect(c.snapshot().epoch).toBe(2);
  206. });
  207. it("lastGood 及以下被限流 → 墙下移:按系数减、清掉 lastGood(已证伪)、记 lastBad", () => {
  208. const c = climbTo(14);
  209. throttleNow(c); // 回 13
  210. expect(throttleNow(c)).toEqual([
  211. { key: KEY, previous: 13, next: 9, reason: "rate_limited", lastBad: 13 },
  212. ]);
  213. expect(c.snapshot().lastGood).toBeUndefined();
  214. });
  215. it("每级需 K 次成功(K−1 次不够);新 cap 要靠自己的 streak 重新证明", () => {
  216. const c = climbTo(14);
  217. throttleNow(c); // 14 > lastGood 13 ⇒ 回 13
  218. expect(throttleNow(c)[0]).toMatchObject({ previous: 13, next: 9 });
  219. expect(roundTrip(c, K - 1)).toEqual([]);
  220. expect(c.snapshot().lastGood).toBeUndefined();
  221. expect(roundTrip(c, 1)[0]).toMatchObject({ next: 10, lastGood: 9 });
  222. const small = new ConcurrencyController(KEY, 6);
  223. throttleNow(small); // cap 4, lastBad 6
  224. small.waiters(T0, 1);
  225. roundTrip(small, K); // cap 5, lastGood 4
  226. roundTrip(small, K); // cap 6, lastGood 5
  227. throttleNow(small); // 6 > lastGood 5 ⇒ 回 5,lastBad 6
  228. expect(small.snapshot()).toMatchObject({ cap: 5, lastGood: 5, lastBad: 6 });
  229. });
  230. it("真实上限 5、天花板 13:13 →429→ 9 →429→ 6 →429→ 4 → 5 → 6 →429→ 5,稳定在 5", () => {
  231. const c = new ConcurrencyController(KEY, 13);
  232. c.waiters(T0, 1);
  233. expect(throttleNow(c)[0]?.next).toBe(9);
  234. expect(throttleNow(c)[0]?.next).toBe(6);
  235. expect(throttleNow(c)[0]?.next).toBe(4);
  236. expect(roundTrip(c, K)[0]).toMatchObject({ next: 5, lastGood: 4 });
  237. expect(roundTrip(c, K)[0]).toMatchObject({ next: 6, lastGood: 5 });
  238. expect(throttleNow(c)[0]).toMatchObject({ previous: 6, next: 5, lastGood: 5, lastBad: 6 });
  239. // 此后每 K 次成功探一次 6,每次只花一个 429、退回 5。
  240. for (let round = 0; round < 3; round++) {
  241. expect(roundTrip(c, K)[0]?.next).toBe(6);
  242. expect(throttleNow(c)[0]).toMatchObject({ previous: 6, next: 5, lastBad: 6 });
  243. }
  244. expect(c.snapshot()).toMatchObject({ cap: 5, lastGood: 5, lastBad: 6 });
  245. });
  246. });
  247. describe("ConcurrencyController — 空闲重置(决策 20)", () => {
  248. it("空闲 5 分钟且无在飞时,任一信号先重置到天花板并清空 lastGood/lastBad/cooldown,翻 epoch", () => {
  249. const c = new ConcurrencyController(KEY, 16);
  250. throttleNow(c, "rate_limited", 60_000); // cap 12, epoch 1, inFlight 0
  251. const later = T0 + CONCURRENCY_IDLE_RESET_MS;
  252. expect(c.observe(later - 1)).toEqual([]);
  253. expect(c.observe(later)).toEqual([{ key: KEY, previous: 12, next: 16, reason: "idle_reset" }]);
  254. expect(c.snapshot()).toMatchObject({ cap: 16, successStreak: 0, epoch: 2 });
  255. expect(c.snapshot().lastGood).toBeUndefined();
  256. expect(c.snapshot().lastBad).toBeUndefined();
  257. expect(c.snapshot().cooldownUntil).toBeUndefined();
  258. // 重置是幂等的:再来一个空闲信号不再发事件、不再翻页。
  259. expect(c.observe(later + 1)).toEqual([]);
  260. expect(c.snapshot().epoch).toBe(2);
  261. });
  262. it("有在飞时不重置", () => {
  263. const c = new ConcurrencyController(KEY, 16);
  264. admit(c, 1);
  265. throttleNow(c); // inFlight 1
  266. expect(c.observe(T0 + CONCURRENCY_IDLE_RESET_MS * 2)).toEqual([]);
  267. expect(c.snapshot().cap).toBe(12);
  268. });
  269. it("重置后从天花板起步的第一次 429 按系数减(lastGood 已清空);重置与限流在同一信号里是两条 change", () => {
  270. const c = new ConcurrencyController(KEY, 16);
  271. throttleNow(c);
  272. const later = T0 + CONCURRENCY_IDLE_RESET_MS;
  273. // 治理器的准入路径:observe(重置)→ admitted → …;这里模拟一个跳过 observe 直接带旧 epoch 来的 429:
  274. // idleReset 先翻页,所以它按旧 epoch 处理——不减,只有 idle_reset 一条。
  275. const epoch = c.admitted(T0);
  276. c.ended(T0);
  277. expect(c.throttled(later, epoch, "rate_limited")).toEqual([
  278. { key: KEY, previous: 12, next: 16, reason: "idle_reset" },
  279. ]);
  280. // 走正规路径:observe 之后准入再限流,才是对天花板的评价。
  281. expect(c.observe(later)).toEqual([]);
  282. expect(throttleNow(c, "rate_limited", undefined, later)).toEqual([
  283. { key: KEY, previous: 16, next: 12, reason: "rate_limited", lastBad: 16 },
  284. ]);
  285. });
  286. });
  287. describe("ConcurrencyController — 增长上限(docs/dynamic-workflow/concurrency.md「Increase」「The governor」)", () => {
  288. it("起点是 initial;自动增长越过 initial、停在 growthLimit;完整 streak 仍把 growthLimit 证明为 lastGood", () => {
  289. const c = new ConcurrencyController(KEY, 4, 8);
  290. expect(c.snapshot()).toMatchObject({ initial: 4, growthLimit: 8, cap: 4 });
  291. c.waiters(T0, 1);
  292. const changes = roundTrip(c, K * 10);
  293. expect(changes.map((x) => x.next)).toEqual([5, 6, 7, 8]);
  294. expect(changes.every((x) => x.reason === "recovered")).toBe(true);
  295. expect(c.snapshot()).toMatchObject({ cap: 8, lastGood: 8 });
  296. });
  297. it("缺省 growthLimit = initial:与从前「天花板即上界」逐字相同", () => {
  298. const c = new ConcurrencyController(KEY, 6);
  299. expect(c.snapshot()).toMatchObject({ initial: 6, growthLimit: 6 });
  300. c.waiters(T0, 1);
  301. expect(roundTrip(c, K * 3)).toEqual([]);
  302. });
  303. it("抬高增长上限不立刻动 cap、不发事件;cap 此后一级一级爬上去", () => {
  304. const c = new ConcurrencyController(KEY, 4, 8);
  305. expect(c.setGrowthLimit(T0, 20)).toEqual([]);
  306. expect(c.snapshot()).toMatchObject({ growthLimit: 20, cap: 4 });
  307. c.waiters(T0, 1);
  308. expect(roundTrip(c, K * 20).map((x) => x.next)).toEqual(
  309. Array.from({ length: 16 }, (_, i) => 5 + i),
  310. );
  311. expect(c.snapshot().cap).toBe(20);
  312. });
  313. it("压低到 cap 之下:cap 当场拉到新上限、发 limit_lowered、lastGood 夹到上限、清 streak、翻 epoch", () => {
  314. const c = new ConcurrencyController(KEY, 4, 20);
  315. c.waiters(T0, 1);
  316. roundTrip(c, K * 12); // cap 16,lastGood 15
  317. expect(c.snapshot().cap).toBe(16);
  318. roundTrip(c, 2); // streak 2
  319. const stale = c.admitted(T0); // 旧 cap 下发出的请求
  320. const epochBefore = c.snapshot().epoch;
  321. expect(c.setGrowthLimit(T0, 8)).toEqual([
  322. { key: KEY, previous: 16, next: 8, reason: "limit_lowered", lastGood: 8 },
  323. ]);
  324. expect(c.snapshot()).toMatchObject({
  325. growthLimit: 8,
  326. cap: 8,
  327. lastGood: 8,
  328. successStreak: 0,
  329. epoch: epochBefore + 1,
  330. });
  331. // 那个旧 cap 下发出的请求撞 429:不是对新 cap 的裁决,不再减。
  332. expect(c.throttled(T0, stale, "rate_limited")).toEqual([]);
  333. expect(c.snapshot().cap).toBe(8);
  334. });
  335. it("压低但 cap 本就不高于新上限:不动 cap、不发事件、不翻 epoch", () => {
  336. const c = new ConcurrencyController(KEY, 4, 20);
  337. const epoch = c.snapshot().epoch;
  338. expect(c.setGrowthLimit(T0, 8)).toEqual([]);
  339. expect(c.snapshot()).toMatchObject({ growthLimit: 8, cap: 4, epoch });
  340. });
  341. it("增长上限永不低于 initial", () => {
  342. const c = new ConcurrencyController(KEY, 6, 12);
  343. expect(c.setGrowthLimit(T0, 2)).toEqual([]);
  344. expect(c.snapshot()).toMatchObject({ growthLimit: 6, cap: 6 });
  345. });
  346. it("空闲重置回 initial,而不是回增长上限", () => {
  347. const c = new ConcurrencyController(KEY, 4, 8);
  348. c.waiters(T0, 1);
  349. roundTrip(c, K * 10); // cap 8
  350. c.waiters(T0, 0);
  351. const later = T0 + CONCURRENCY_IDLE_RESET_MS;
  352. expect(c.observe(later)).toEqual([{ key: KEY, previous: 8, next: 4, reason: "idle_reset" }]);
  353. expect(c.snapshot()).toMatchObject({ cap: 4, growthLimit: 8 });
  354. expect(c.snapshot().lastGood).toBeUndefined();
  355. });
  356. });
  357. describe("ConcurrencyController — 抬高上界时直接起跳(seed,docs/dynamic-workflow/concurrency.md「Seeding」)", () => {
  358. it("seed 把 cap 当场拉到目标(不超过增长上限),发 seeded,清 lastGood、清 streak、翻 epoch", () => {
  359. const c = new ConcurrencyController(KEY, 8, 32);
  360. c.waiters(T0, 1);
  361. roundTrip(c, K + 2); // cap 9,lastGood 8,streak 2
  362. const epoch = c.snapshot().epoch;
  363. expect(c.seed(T0, 32)).toEqual([{ key: KEY, previous: 9, next: 32, reason: "seeded" }]);
  364. expect(c.snapshot()).toMatchObject({ cap: 32, successStreak: 0, epoch: epoch + 1 });
  365. expect(c.snapshot().lastGood).toBeUndefined();
  366. expect(c.snapshot().lastBad).toBeUndefined();
  367. });
  368. it("目标超过增长上限时只跳到增长上限", () => {
  369. const c = new ConcurrencyController(KEY, 8, 20);
  370. expect(c.seed(T0, 50)).toEqual([{ key: KEY, previous: 8, next: 20, reason: "seeded" }]);
  371. });
  372. it("目标不高于 cap:什么都不做", () => {
  373. const c = new ConcurrencyController(KEY, 8, 32);
  374. const epoch = c.snapshot().epoch;
  375. expect(c.seed(T0, 8)).toEqual([]);
  376. expect(c.seed(T0, 3)).toEqual([]);
  377. expect(c.snapshot()).toMatchObject({ cap: 8, epoch });
  378. });
  379. it("起跳之后第一次 429 按系数减(32 → 24),而不是退回旧的 lastGood", () => {
  380. const c = new ConcurrencyController(KEY, 8, 32);
  381. c.waiters(T0, 1);
  382. roundTrip(c, K); // lastGood 8
  383. c.seed(T0, 32);
  384. expect(throttleNow(c)).toEqual([
  385. { key: KEY, previous: 32, next: 24, reason: "rate_limited", lastBad: 32 },
  386. ]);
  387. });
  388. it("五分钟内有过任何 429(哪怕是旧 epoch 的)即不起跳;过了五分钟再起跳", () => {
  389. const c = new ConcurrencyController(KEY, 8, 32);
  390. const stale = c.admitted(T0);
  391. throttleNow(c); // 当前 epoch:8 → 6
  392. c.throttled(T0 + 1_000, stale, "rate_limited"); // 旧 epoch,同样算「刚被拒过」
  393. expect(c.snapshot().lastThrottledAt).toBe(T0 + 1_000);
  394. expect(c.seed(T0 + 2_000, 32)).toEqual([]);
  395. expect(c.snapshot().cap).toBe(6);
  396. // 一直有请求在飞,所以不会空闲重置;五分钟后证据过期,起跳放行。
  397. c.admitted(T0 + 1_000);
  398. const later = T0 + 1_000 + CONCURRENCY_IDLE_RESET_MS;
  399. expect(c.seed(later, 32)).toEqual([{ key: KEY, previous: 6, next: 32, reason: "seeded" }]);
  400. });
  401. it("空闲重置清掉 lastThrottledAt", () => {
  402. const c = new ConcurrencyController(KEY, 8, 32);
  403. throttleNow(c);
  404. const later = T0 + CONCURRENCY_IDLE_RESET_MS;
  405. expect(c.observe(later)).toHaveLength(1);
  406. expect(c.snapshot().lastThrottledAt).toBeUndefined();
  407. });
  408. });