File size: 25,736 Bytes
67d18ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
import {
  CaptchaCpuGovernor,
  resolveCpuGovernorConfig,
  type CpuGovernorSnapshot,
} from "./captcha-cpu-governor.js";
import {
  captchaSolverConcurrency,
  runCaptchaSolve,
  setCaptchaSolverConcurrency,
  shutdownCaptchaSolver,
} from "./captcha-solver.js";
import { isCaptchaDuplicateError, isCaptchaIpBlockError, parseCertifyId } from "./captcha-token.js";

export interface CaptchaPoolOptions {
  /** @deprecated Use poolSizeMax β€” kept as max cap alias. */
  poolSize?: number;
  poolSizeMin?: number;
  poolSizeMax?: number;
  /** Idle seconds before shrinking toward min (default 120). */
  scaleDownIdleMs?: number;
  /** Deep-idle floor: pool target decays to this after sustained zero traffic
   *  (default 1). First token take restores the full poolSizeMin. */
  idleFloor?: number;
  /** Zero-traffic seconds after which background solving stops entirely β€”
   *  target floor decays to 0 (default 900). One banked token is still kept
   *  for an instant serve on wake-up. */
  deepIdleAfterMs?: number;
  tokenTtlMs?: number;
  refillIntervalMs?: number;
  solveRetries?: number;
  staggerMs?: number;
  solveConcurrency?: number;
  /** Parallel solves raced when a take finds the pool empty (default 2). */
  emptyTakeRace?: number;
  /** Host CPU ceiling for captcha workers (default 100, 0 = off). */
  cpuLimitPercent?: number;
  cpuGovernorIntervalSec?: number;
  /**
   * Called when minting fails with an IP-level block ("too many captcha
   * requests" family). The proxy wires this to the Telegram network reset so
   * an Aliyun IP block self-heals instead of stranding the token pool.
   */
  onCaptchaIpBlock?: (reason: string) => void;
}

type TokenEntry = { param: string; cachedAt: number; certifyId: string | null };

/** Tracks certifyIds already handed out or queued β€” prevents F008 duplicate usage. */
class CertifyIdRegistry {
  private seen = new Map<string, number>();

  constructor(private ttlMs: number) {}

  setTtlMs(ttlMs: number): void {
    this.ttlMs = ttlMs;
  }

  prune(): void {
    const cutoff = Date.now() - this.ttlMs;
    for (const [id, at] of this.seen) {
      if (at < cutoff) this.seen.delete(id);
    }
  }

  has(id: string): boolean {
    this.prune();
    return this.seen.has(id);
  }

  add(id: string): void {
    this.prune();
    this.seen.set(id, Date.now());
  }

  clear(): void {
    this.seen.clear();
  }
}

const DEFAULT_POOL_MIN = Number(process.env.CAPTCHA_POOL_MIN || 40);
const DEFAULT_POOL_MAX = Number(process.env.CAPTCHA_POOL_MAX || 120);
const DEFAULT_TOKEN_TTL_MS = Number(process.env.CAPTCHA_CACHE_TTL_MS || 95_000);
const DEFAULT_REFILL_INTERVAL_MS = Number(process.env.CAPTCHA_REFILL_INTERVAL_MS || 1_000);
const DEFAULT_STAGGER_MS = Number(process.env.CAPTCHA_SOLVE_STAGGER_MS || 0);
const DEFAULT_SOLVE_CONCURRENCY = Number(
	process.env.CAPTCHA_SOLVE_CONCURRENCY ||
		(process.env.ZCODE_CAPTCHA_LOW_CPU === "1" ? 3 : 8),
);
const DEFAULT_SCALE_DOWN_IDLE_MS = Number(process.env.CAPTCHA_POOL_SCALE_DOWN_IDLE_MS || 120_000);
const DEFAULT_IDLE_FLOOR = Number(process.env.CAPTCHA_POOL_IDLE_FLOOR || 1);
// Zero-traffic beyond this stops background solving entirely (floor 0): no
// mint churn, near-zero CPU. The next token take restores poolSizeMin.
const DEFAULT_DEEP_IDLE_AFTER_MS = Number(process.env.CAPTCHA_DEEP_IDLE_AFTER_MS || 900_000);
// Parallel solves raced on an empty-pool take β€” first success serves the
// client, the twins top up the pool. Cuts worst-case cold TTFB and widens
// odds against a bad rotated pe bundle stalling one racer.
const EMPTY_TAKE_RACE = Math.max(1, Number(process.env.CAPTCHA_EMPTY_TAKE_RACE || 3));
const SOLVE_RETRIES = Number(process.env.ZCODE_CAPTCHA_RETRIES || 4);
const SCALE_UP_STEP = 20;
const TAKE_RATE_WINDOW_MS = 120_000;

type ResolvedPoolOpts = {
  poolSizeMin: number;
  poolSizeMax: number;
  idleFloor: number;
  deepIdleAfterMs: number;
  scaleDownIdleMs: number;
  tokenTtlMs: number;
  refillIntervalMs: number;
  solveRetries: number;
  staggerMs: number;
  solveConcurrency: number;
  emptyTakeRace: number;
  onCaptchaIpBlock?: (reason: string) => void;
};

function resolvePoolOpts(opts: CaptchaPoolOptions): ResolvedPoolOpts {
  const max = opts.poolSizeMax ?? opts.poolSize ?? DEFAULT_POOL_MAX;
  const min = Math.min(opts.poolSizeMin ?? DEFAULT_POOL_MIN, max);
  return {
    poolSizeMin: min,
    poolSizeMax: max,
    idleFloor: Math.min(opts.idleFloor ?? DEFAULT_IDLE_FLOOR, min),
    deepIdleAfterMs: Math.max(
      opts.deepIdleAfterMs ?? DEFAULT_DEEP_IDLE_AFTER_MS,
      opts.scaleDownIdleMs ?? DEFAULT_SCALE_DOWN_IDLE_MS,
    ),
    scaleDownIdleMs: opts.scaleDownIdleMs ?? DEFAULT_SCALE_DOWN_IDLE_MS,
    tokenTtlMs: opts.tokenTtlMs ?? DEFAULT_TOKEN_TTL_MS,
    refillIntervalMs: opts.refillIntervalMs ?? DEFAULT_REFILL_INTERVAL_MS,
    solveRetries: opts.solveRetries ?? SOLVE_RETRIES,
    staggerMs: opts.staggerMs ?? DEFAULT_STAGGER_MS,
    solveConcurrency: opts.solveConcurrency ?? DEFAULT_SOLVE_CONCURRENCY,
    emptyTakeRace: Math.max(1, opts.emptyTakeRace ?? EMPTY_TAKE_RACE),
    onCaptchaIpBlock: opts.onCaptchaIpBlock,
  };
}

export class CaptchaTokenPool {
  private tokens: TokenEntry[] = [];
  private opts: ResolvedPoolOpts;
  private effectiveTarget: number;
  private refillTimer: ReturnType<typeof setInterval> | null = null;
  private refillInFlight = false;
  private lastSolveAt = 0;
  private activeSolves = 0;
  private cfg: CaptchaConfig | null = null;
  private pausedUntil = 0;
  private lastTakeAt: number;
  private takeTimestamps: number[] = [];
  private certifyIds: CertifyIdRegistry;
  private governor: CaptchaCpuGovernor | null = null;
  // Mint-storm detection: sliding windows of failed vs successful mint
  // attempts. A burst of failures with zero successes means velocity/IP
  // flagging β€” fire the telegram IP reset automatically.
  private mintFailures: number[] = [];
  private mintSuccesses: number[] = [];
  private lastStormResetAt = 0;

  constructor(opts: CaptchaPoolOptions = {}) {
    // The module-level singleton constructs with no opts before config load;
    // resolvePoolOpts would fall back to env/global defaults there (e.g.
    // poolSizeMin 40) and configure()'s clamp would preserve that as the
    // starting target β€” minting a storm of soon-expired tokens at every
    // boot. With no explicit opts, defer sizing to the first configure().
    const hasOpts = Object.keys(opts).length > 0;
    this.opts = resolvePoolOpts(opts);
    this.effectiveTarget = hasOpts ? this.opts.poolSizeMin : 0;
    // Seed idle clock at startup so a no-traffic proxy decays to idleFloor
    // instead of solving 40-60 tokens forever (lastTakeAt=0 disabled decay).
    this.lastTakeAt = Date.now();
    this.certifyIds = new CertifyIdRegistry(this.opts.tokenTtlMs);
    this.initGovernor(opts);
    setCaptchaSolverConcurrency(this.opts.solveConcurrency);
  }

  configure(opts: CaptchaPoolOptions): void {
    this.opts = resolvePoolOpts({ ...this.opts, ...opts });
    this.certifyIds.setTtlMs(this.opts.tokenTtlMs);
    this.effectiveTarget = Math.max(
      this.opts.poolSizeMin,
      Math.min(this.effectiveTarget, this.opts.poolSizeMax),
    );
    this.initGovernor(opts);
    // Propagate the resolved concurrency to the solver daemon β€” the
    // import-time constructor runs before config load and would otherwise
    // leave the daemon at its fallback (8) instead of the configured value.
    setCaptchaSolverConcurrency(this.opts.solveConcurrency);
    this.trimToTarget();
  }

  stats(): {
    ready: number;
    target: number;
    min: number;
    max: number;
    activeSolves: number;
    solverWorkers: number;
    cpu?: CpuGovernorSnapshot;
  } {
    this.pruneExpired();
    return {
      ready: this.tokens.length,
      target: this.effectiveTarget,
      min: this.opts.poolSizeMin,
      max: this.opts.poolSizeMax,
      activeSolves: this.activeSolves,
      solverWorkers: captchaSolverConcurrency(),
      cpu: this.governor?.enabled ? this.governor.snapshot() : undefined,
    };
  }

  invalidate(): void {
    this.tokens = [];
    this.certifyIds.clear();
    this.pausedUntil = Date.now() + this.opts.staggerMs;
  }

  startBackgroundRefill(cfg: CaptchaConfig): void {
    this.cfg = cfg;
    this.stopBackgroundRefill();
    this.governor?.start();
    void this.refill({ urgent: false });
    this.refillTimer = setInterval(() => {
      void this.refill({ urgent: false });
    }, this.opts.refillIntervalMs);
  }

  stopBackgroundRefill(): void {
    this.governor?.stop();
    if (this.refillTimer) {
      clearInterval(this.refillTimer);
      this.refillTimer = null;
    }
  }

  async takeToken(cfg: CaptchaConfig): Promise<string> {
    this.cfg = cfg;

    const readyBefore = this.tokens.length;
    const token = this.popFresh();
    if (token) {
      this.onTokenTaken(false, readyBefore - 1);
      void this.refill({ urgent: true });
      return token;
    }

    this.onTokenTaken(true, 0);
    // Re-size immediately from current demand (this take is already in the
    // window) so an empty-pool arrival widens the buffer right away.
    this.effectiveTarget = Math.max(this.effectiveTarget, this.computeActiveTarget());
    let param: string;
    try {
      // Overall deadline for the racing chain: during mint storms a racer can
      // grind through its retry budget (~30-45s) β€” cap the client-facing wait
      // and let the background waves finish the job instead.
      const raceDeadlineMs = Number(process.env.CAPTCHA_SOLVE_RACE_DEADLINE_MS || 25_000);
      param = await Promise.race([
        this.solveRaced(cfg),
        new Promise<never>((_, rej) =>
          setTimeout(
            () => rej(new Error(`captcha take deadline (${raceDeadlineMs}ms)`)),
            Math.max(1_000, raceDeadlineMs),
          ),
        ),
      ]);
    } catch (err) {
      // Mints fail in clusters (pe-stall storms, F008 velocity). Background
      // refill waves keep retrying β€” give them a short window to land a
      // token before surfacing a failure to the client. This converts most
      // storm-window 503s into slower successes.
      const graceMs = Number(process.env.CAPTCHA_TAKE_GRACE_MS || 10_000);
      const deadline = Date.now() + Math.max(0, graceMs);
      while (Date.now() < deadline) {
        await new Promise((r) => setTimeout(r, 400));
        const token = this.popFresh();
        if (token) {
          this.markParamIssued(token);
          void this.refill({ urgent: true });
          return token;
        }
      }
      throw err;
    }
    this.markParamIssued(param);
    void this.refill({ urgent: true });
    return param;
  }

  async prefill(cfg: CaptchaConfig, count?: number): Promise<void> {
    this.cfg = cfg;
    const target = Math.min(count ?? this.effectiveTarget, this.effectiveTarget);
    while (this.tokens.length + this.activeSolves < target) {
      const need = target - this.tokens.length - this.activeSolves;
      if (need <= 0) break;
      const concurrency = this.governor?.enabled
        ? this.governor.backgroundConcurrency(this.tokens.length)
        : this.opts.solveConcurrency;
      await this.solveBatch(cfg, need, Math.max(1, concurrency));
    }
  }

  requestUrgentRefill(): void {
    void this.refill({ urgent: true });
  }

  private poolHasCertifyId(certifyId: string): boolean {
    return this.tokens.some((t) => t.certifyId === certifyId);
  }

  private isDuplicateParam(param: string): boolean {
    const certifyId = parseCertifyId(param);
    if (!certifyId) return false;
    return this.certifyIds.has(certifyId) || this.poolHasCertifyId(certifyId);
  }

  private markParamIssued(param: string): void {
    const certifyId = parseCertifyId(param);
    if (certifyId) this.certifyIds.add(certifyId);
  }

  private popFresh(): string | null {
    this.pruneExpired();
    while (this.tokens.length > 0) {
      const entry = this.tokens.pop()!;
      if (entry.certifyId && this.certifyIds.has(entry.certifyId)) continue;
      this.markParamIssued(entry.param);
      return entry.param;
    }
    return null;
  }

  private pushToken(param: string): void {
    this.pruneExpired();
    if (this.tokens.length >= this.effectiveTarget) return;
    const certifyId = parseCertifyId(param);
    if (certifyId && (this.certifyIds.has(certifyId) || this.poolHasCertifyId(certifyId))) {
      return;
    }
    this.tokens.push({ param, cachedAt: Date.now(), certifyId });
  }

  private pruneExpired(): void {
    const now = Date.now();
    this.tokens = this.tokens.filter((t) => now - t.cachedAt < this.opts.tokenTtlMs);
  }

  private deficit(): number {
    this.pruneExpired();
    return Math.max(0, this.effectiveTarget - this.tokens.length - this.activeSolves);
  }

  private pruneTakeWindow(): void {
    const cutoff = Date.now() - TAKE_RATE_WINDOW_MS;
    this.takeTimestamps = this.takeTimestamps.filter((t) => t > cutoff);
  }

  private demandTarget(): number {
    this.pruneTakeWindow();
    if (this.takeTimestamps.length === 0) return this.opts.poolSizeMin;
    const ratePerSec = this.takeTimestamps.length / (TAKE_RATE_WINDOW_MS / 1000);
    const ttlSec = this.opts.tokenTtlMs / 1000;
    return Math.ceil(ratePerSec * ttlSec * 1.25);
  }

  /**
   * Target while traffic is recent: measured demand (take rate Γ— TTL Γ—
   * margin) plus one token of arrival-jitter headroom, floored at
   * poolSizeMin, capped by governor + poolSizeMax. This is the ONLY sizing
   * authority while active β€” bursts raise demand, quiet periods drain it.
   */
  private computeActiveTarget(): number {
    const demand = Math.max(this.demandTarget(), this.opts.poolSizeMin);
    const raw = Math.ceil(demand * 1.25) + 1;
    const governorMax = this.governor?.maxPoolTarget() ?? this.opts.poolSizeMax;
    return Math.max(
      this.currentFloor(),
      Math.min(this.opts.poolSizeMax, governorMax, raw),
    );
  }

  private onTokenTaken(wasEmpty: boolean, readyAfter: number): void {
    this.lastTakeAt = Date.now();
    this.takeTimestamps.push(this.lastTakeAt);
    this.pruneTakeWindow();
    this.wakeFromIdle();
    // Sizing is demand-driven via computeActiveTarget() on the next refill
    // tick; takes only need to restore the active floor instantly.
  }

  private maybeScaleDown(): void {
    if (this.lastTakeAt === 0) return;
    const idleMs = Date.now() - this.lastTakeAt;
    if (idleMs < this.opts.scaleDownIdleMs) return;

    this.pruneTakeWindow();
    const recentTakes = this.takeTimestamps.filter((t) => t > Date.now() - 60_000).length;
    if (recentTakes > 1) return;

    // Deep idle: decay the effective floor itself so Chromium workers and
    // solve churn wind down β€” first toward idleFloor, then to zero after
    // deepIdleAfterMs (no background mints at all). Any token take snaps the
    // floor back to full poolSizeMin.
    const floor = this.currentFloor();
    if (this.effectiveTarget > floor) {
      this.effectiveTarget = Math.max(floor, this.effectiveTarget - SCALE_UP_STEP);
      this.trimToTarget();
    }
  }

  /**
   * Active traffic β†’ poolSizeMin. After scaleDownIdleMs of no takes β†’
   * idleFloor (keeper churn β‰ˆ 1 mint/TTL). After deepIdleAfterMs β†’ 0:
   * background solving stops; the banked token still serves a wake-up take.
   */
  private currentFloor(): number {
    if (this.lastTakeAt === 0) return this.opts.poolSizeMin;
    const idleMs = Date.now() - this.lastTakeAt;
    if (idleMs >= this.opts.deepIdleAfterMs) return 0;
    if (idleMs >= this.opts.scaleDownIdleMs) {
      return Math.min(this.opts.idleFloor, this.opts.poolSizeMin);
    }
    return this.opts.poolSizeMin;
  }

  /** Traffic returned: restore the full configured floor immediately. */
  private wakeFromIdle(): void {
    if (this.effectiveTarget < this.opts.poolSizeMin) {
      this.effectiveTarget = this.opts.poolSizeMin;
    }
  }

  private trimToTarget(): void {
    this.pruneExpired();
    // Never trim the last banked token: it serves a wake-up take instantly
    // (free TTFB) and expires naturally via pruneExpired.
    const floor = Math.max(this.effectiveTarget, 1);
    while (this.tokens.length > floor) {
      this.tokens.shift();
    }
  }

  private applyGovernorCaps(): void {
    if (!this.governor?.enabled) return;
    const governorMax = this.governor.maxPoolTarget();
    // Floor is idle-aware (idleFloor β†’ 0 past deepIdleAfterMs) β€” don't clamp
    // it back up (that would refill forever during idle).
    const floor = this.currentFloor();
    this.effectiveTarget = Math.max(
      floor,
      Math.min(this.effectiveTarget, governorMax, this.opts.poolSizeMax),
    );
  }

  private async refill(opts: { urgent: boolean }): Promise<void> {
    if (!this.cfg) return;
    // Refresh sizing BEFORE the in-flight guard: a fill batch with slow or
    // stalling solves can hold refillInFlight for minutes, and freezing the
    // target that whole time starves the buffer exactly when demand exists.
    const idleDecay =
      this.lastTakeAt > 0 && Date.now() - this.lastTakeAt >= this.opts.scaleDownIdleMs;
    if (!idleDecay) {
      this.effectiveTarget = this.computeActiveTarget();
    } else {
      this.maybeScaleDown();
    }

    // Idle decay: drop the target toward the current floor (idleFloor β†’ 0
    // past deepIdleAfterMs) and skip background solves while enough banked
    // tokens remain. This is what stops mint churn at zero traffic.
    if (!opts.urgent && idleDecay) {
      if (this.tokens.length >= Math.max(this.currentFloor(), 1)) return;
    }

    if (this.refillInFlight || Date.now() < this.pausedUntil) return;

    this.applyGovernorCaps();

    const ready = this.tokens.length;
    const concurrency = this.governor?.enabled
      ? opts.urgent
        ? Math.max(1, this.governor.backgroundConcurrency(ready) || this.governor.snapshot().concurrency)
        : this.governor.backgroundConcurrency(ready)
      : this.opts.solveConcurrency;

    if (!opts.urgent && concurrency === 0 && ready >= this.opts.poolSizeMin) {
      return;
    }

    const need = this.deficit();
    if (need <= 0) return;

    this.refillInFlight = true;
    try {
      await this.solveBatch(this.cfg, need, Math.max(1, concurrency));
    } finally {
      this.refillInFlight = false;
    }
  }

  /** Fire up to `need` solves in parallel waves of solveConcurrency. */
  private async solveBatch(cfg: CaptchaConfig, need: number, concurrency = this.opts.solveConcurrency): Promise<void> {
    let remaining = need;
    while (remaining > 0 && this.tokens.length + this.activeSolves < this.effectiveTarget) {
      const wave = Math.min(remaining, concurrency, this.deficit());
      if (wave <= 0) break;
      if (this.opts.staggerMs > 0) await this.waitForStagger();
      const results = await Promise.allSettled(
        Array.from({ length: wave }, () => this.solveOne(cfg)),
      );
      for (const r of results) {
        if (r.status === "fulfilled") {
          this.pushToken(r.value);
        } else {
          console.warn("[captcha-pool] parallel solve failed:", r.reason);
        }
      }
      remaining -= wave;
      if (results.every((r) => r.status === "rejected")) break;
    }
  }

  private async waitForStagger(): Promise<void> {
    const elapsed = Date.now() - this.lastSolveAt;
    const wait = this.opts.staggerMs - elapsed;
    if (wait > 0) {
      await new Promise((r) => setTimeout(r, wait));
    }
  }

  private noteMintFailure(reason: string): void {
    const now = Date.now();
    this.mintFailures.push(now);
    const cutoff = now - 360_000;
    this.mintFailures = this.mintFailures.filter((t) => t > cutoff);
    this.maybeFireMintStormReset(reason);
  }

  private noteMintSuccess(): void {
    const now = Date.now();
    this.mintSuccesses.push(now);
    const cutoff = now - 360_000;
    this.mintSuccesses = this.mintSuccesses.filter((t) => t > cutoff);
  }

  /** Velocity/IP flagging signature: many failed mints in the last 5 min
   *  while nothing succeeded in the last 3. Fires the telegram IP reset at
   *  most once per cooldown β€” requestCaptchaIpResetSync dedupes further. */
  private maybeFireMintStormReset(latestReason: string): void {
    const now = Date.now();
    const fails = this.mintFailures.filter((t) => t > now - 300_000).length;
    const succs = this.mintSuccesses.filter((t) => t > now - 180_000).length;
    if (fails < 8 || succs > 0) return;
    if (now - this.lastStormResetAt < 12 * 60_000) return;
    this.lastStormResetAt = now;
    console.warn(
      `[captcha] mint storm detected (${fails} failures/5min, ${succs} successes/3min; latest: ${latestReason.slice(0, 120)}) -> requesting IP reset`,
    );
    try {
      this.opts.onCaptchaIpBlock?.(`mint storm: ${fails} failed mints in 5min`);
    } catch {
      // callback is best-effort; never break the pool
    }
  }

  private async solveFresh(cfg: CaptchaConfig): Promise<string> {
    this.activeSolves += 1;
    this.lastSolveAt = Date.now();
    try {
      let lastErr: string | null = null;
      let sawIpBlock = false;
      for (let attempt = 1; attempt <= this.opts.solveRetries; attempt += 1) {
        try {
          const param = await runCaptchaSolve(cfg.sceneId, cfg.region, cfg.prefix);
          if (!param) {
            lastErr = "solver returned empty";
            continue;
          }
          if (this.isDuplicateParam(param)) {
            lastErr = `duplicate certifyId ${parseCertifyId(param) ?? "?"}`;
            continue;
          }
          this.noteMintSuccess();
          return param;
        } catch (err) {
          lastErr = err instanceof Error ? err.message : String(err);
          if (isCaptchaDuplicateError(lastErr)) {
            lastErr = `duplicate certifyId (F008)`;
          } else if (isCaptchaIpBlockError(lastErr)) {
            sawIpBlock = true;
          }
          this.noteMintFailure(lastErr);
        }
      }
      // All retries failed. If any failure was an IP-level block, ask the
      // caller to rotate the egress (Telegram network reset) so the next
      // mint has a fresh IP instead of being stuck unable to mint.
      if (sawIpBlock && this.opts.onCaptchaIpBlock) {
        try {
          this.opts.onCaptchaIpBlock(lastErr ?? "captcha ip block");
        } catch (_) {
          // callback is best-effort; never let it break the pool
        }
      }
      throw new Error(`captcha failed after ${this.opts.solveRetries} attempts: ${lastErr ?? "unknown"}`);
    } finally {
      this.activeSolves -= 1;
    }
  }

  private async solveOne(cfg: CaptchaConfig): Promise<string> {
    return this.solveFresh(cfg);
  }

  /**
   * Empty-pool serve: race up to `emptyTakeRace` solves in parallel and
   * resolve with the first success so the client waits for one mint, not a
   * sequential retry chain. Extra successes are banked into the pool instead
   * of being wasted; the race only rejects when every racer fails.
   */
  private solveRaced(cfg: CaptchaConfig): Promise<string> {
    const racers = Math.max(1, Math.min(this.opts.emptyTakeRace, this.opts.solveConcurrency));
    if (racers <= 1) return this.solveFresh(cfg);

    return new Promise<string>((resolve, reject) => {
      let pending = racers;
      let served = false;
      let lastErr: unknown = null;
      for (let i = 0; i < racers; i += 1) {
        this.solveFresh(cfg).then(
          (param) => {
            if (served) {
              // Lost the race but minted a valid token β€” bank it.
              this.pushToken(param);
            } else {
              served = true;
              resolve(param);
            }
          },
          (err) => {
            lastErr = err ?? lastErr;
          },
        ).finally(() => {
          pending -= 1;
          if (pending === 0 && !served) {
            reject(
              lastErr instanceof Error
                ? lastErr
                : new Error(String(lastErr ?? "captcha race failed")),
            );
          }
        });
      }
    });
  }

  private initGovernor(opts: CaptchaPoolOptions): void {
    const cfg = resolveCpuGovernorConfig({
      poolSizeMin: this.opts.poolSizeMin,
      poolSizeMax: this.opts.poolSizeMax,
      solveConcurrency: this.opts.solveConcurrency,
      cpuLimitPercent: opts.cpuLimitPercent,
      cpuGovernorIntervalSec: opts.cpuGovernorIntervalSec,
    });
    if (!this.governor) {
      this.governor = new CaptchaCpuGovernor(cfg);
    } else {
      this.governor.configure(cfg);
    }
  }
}

export interface CaptchaConfig {
  enabled: boolean;
  prefix: string;
  sceneId: string;
  region: string;
}

const pool = new CaptchaTokenPool();

export function configureCaptchaPool(opts: CaptchaPoolOptions): void {
  pool.configure(opts);
}

export function getCaptchaPoolStats(): {
  ready: number;
  target: number;
  min?: number;
  max?: number;
  activeSolves: number;
  solverWorkers?: number;
} {
  return pool.stats();
}

export function stopCaptchaPool(): void {
  pool.stopBackgroundRefill();
  shutdownCaptchaSolver();
}

export async function takeCaptchaToken(cfg: CaptchaConfig): Promise<string> {
  if (!cfg.enabled || !cfg.prefix || !cfg.sceneId) {
    throw new Error("Captcha config unavailable from ZCode API");
  }
  return pool.takeToken(cfg);
}

export function startCaptchaPoolRefill(cfg: CaptchaConfig): void {
  if (!cfg.enabled || !cfg.prefix || !cfg.sceneId) return;
  pool.startBackgroundRefill(cfg);
}

export async function prefillCaptchaPool(cfg: CaptchaConfig, count?: number): Promise<void> {
  if (!cfg.enabled || !cfg.prefix || !cfg.sceneId) return;
  await pool.prefill(cfg, count);
}

export function urgentCaptchaRefill(): void {
  pool.requestUrgentRefill();
}