captchakraken 3.2.1 → 4.1.0

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.
Files changed (64) hide show
  1. package/AGENTS.md +7 -11
  2. package/LICENSE +165 -145
  3. package/NOTICE +4 -4
  4. package/README.md +12 -13
  5. package/bin/captchakraken.js +25 -0
  6. package/dist/cli-invocation.d.ts +6 -3
  7. package/dist/cli-invocation.js +26 -10
  8. package/dist/humanize.d.ts +37 -1
  9. package/dist/humanize.js +143 -9
  10. package/dist/index.d.ts +13 -12
  11. package/dist/index.js +25 -33
  12. package/dist/kinds.d.ts +15 -2
  13. package/dist/kinds.js +12 -4
  14. package/dist/playwright-types.d.ts +3 -0
  15. package/dist/selectors.d.ts +93 -11
  16. package/dist/selectors.js +226 -116
  17. package/dist/solver.d.ts +94 -12
  18. package/dist/solver.js +569 -203
  19. package/dist/types.d.ts +53 -8
  20. package/dist/verdicts.d.ts +3 -0
  21. package/dist/verdicts.js +40 -0
  22. package/package.json +8 -4
  23. package/python/AGENTS.md +9 -13
  24. package/python/LICENSE +165 -145
  25. package/python/NOTICE +4 -4
  26. package/python/README.md +13 -12
  27. package/python/pyproject.toml +27 -13
  28. package/python/src/captchakraken/__init__.py +43 -46
  29. package/python/src/captchakraken/action_types.py +32 -1
  30. package/python/src/captchakraken/captchakraken.config.json +416 -0
  31. package/python/src/captchakraken/cli.py +141 -31
  32. package/python/src/captchakraken/clipvideo.py +258 -0
  33. package/python/src/captchakraken/config.py +72 -7
  34. package/python/src/captchakraken/demo.py +149 -0
  35. package/python/src/captchakraken/humanize.py +141 -10
  36. package/python/src/captchakraken/image_processor.py +3 -7
  37. package/python/src/captchakraken/keyframes.py +96 -14
  38. package/python/src/captchakraken/kinds.py +14 -2
  39. package/python/src/captchakraken/models.json +37 -3
  40. package/python/src/captchakraken/page_solver.py +600 -252
  41. package/python/src/captchakraken/planner.py +311 -49
  42. package/python/src/captchakraken/pointer/__init__.py +20 -0
  43. package/python/src/captchakraken/pointer/detector.py +333 -0
  44. package/python/src/captchakraken/pointer/elements.py +273 -0
  45. package/python/src/captchakraken/pointer/heads.py +163 -0
  46. package/python/src/captchakraken/pointer/layout.py +319 -0
  47. package/python/src/captchakraken/pointer/marks.py +184 -0
  48. package/python/src/captchakraken/pointer/server.py +150 -0
  49. package/python/src/captchakraken/pointer/service.py +553 -0
  50. package/python/src/captchakraken/pointer/vllm_plugin.py +114 -0
  51. package/python/src/captchakraken/prompts.py +111 -34
  52. package/python/src/captchakraken/selectors.py +221 -135
  53. package/python/src/captchakraken/selfhost.py +423 -0
  54. package/python/src/captchakraken/server_manager.py +139 -11
  55. package/python/src/captchakraken/solver.py +169 -88
  56. package/python/src/captchakraken/tool_calls/board_painted.py +11 -6
  57. package/python/src/captchakraken/tool_calls/fill_tracker.py +152 -0
  58. package/python/src/captchakraken/tool_calls/find_checkbox.py +14 -59
  59. package/python/src/captchakraken/tool_calls/find_grid.py +273 -1191
  60. package/python/src/captchakraken/tool_calls/slide_tracker.py +1102 -0
  61. package/python/src/captchakraken/verdicts.py +52 -0
  62. package/dist/slide-geometry.d.ts +0 -6
  63. package/dist/slide-geometry.js +0 -38
  64. package/python/src/captchakraken/tool_calls/track_piece.py +0 -120
package/dist/solver.js CHANGED
@@ -33,7 +33,8 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.CaptchaKrakenSolver = exports.SOLVE_DEFAULTS = exports.INPUT_SETTLE_MS = void 0;
36
+ exports.CaptchaKrakenSolver = exports.SOLVE_DEFAULTS = exports.HOLD_MAX_MS = exports.HOLD_SETTLE_MS = exports.GRID_LOAD_CAP_FACTOR = exports.IDLE_DRIFT_PX = exports.IDLE_DRIFT_EVERY_MS = exports.INPUT_SETTLE_MS = void 0;
37
+ exports.withAnswerBox = withAnswerBox;
37
38
  exports.answerNeedsElementBox = answerNeedsElementBox;
38
39
  exports.isStaleHandleError = isStaleHandleError;
39
40
  exports.isClosedTargetError = isClosedTargetError;
@@ -55,7 +56,6 @@ const errors_1 = require("./errors");
55
56
  const limits_1 = require("./limits");
56
57
  const python_command_1 = require("./python-command");
57
58
  const cli_invocation_1 = require("./cli-invocation");
58
- const slide_geometry_1 = require("./slide-geometry");
59
59
  const model_name_1 = require("./model-name");
60
60
  const selectors_1 = require("./selectors");
61
61
  const execFileAsync = (0, util_1.promisify)(child_process_1.execFile);
@@ -128,6 +128,40 @@ function readPngDimensions(filePath) {
128
128
  const VENDORS_WITH_BESPOKE_HANDLING = new Set([kinds_1.Vendor.HCAPTCHA, kinds_1.Vendor.RECAPTCHA]);
129
129
  // Long enough to resolve a locator `all()` just returned, short enough that a widget gone in between reads as stale, not hung.
130
130
  const HANDLE_TIMEOUT_MS = 1000;
131
+ /**
132
+ * A typed captcha's text box widens the shot only when it sits by the image: the union may be at most this many
133
+ * times the image's area. A vendor-named box found in the enclosing form can be a whole login form away.
134
+ */
135
+ const ANSWER_BOX_MAX_GROWTH = 4;
136
+ /**
137
+ * A typed captcha's image and its text box, photographed and pointed into as one element.
138
+ *
139
+ * BotDetect's widget element is a 280x50 strip holding only the image; its text box is a sibling. Every training
140
+ * board and every real capture of a typed captcha shows the box under the image, and on the bare strip the pointer
141
+ * read the image as a click puzzle or doubled a glyph. The answer's points are taken in this union's frame, so
142
+ * `boundingBox` is the union everywhere the round reads it; everything else is the element's own.
143
+ */
144
+ function withAnswerBox(el, box) {
145
+ const union = async () => {
146
+ const a = await el.boundingBox();
147
+ const b = a ? await box.boundingBox() : null;
148
+ if (!a || !b)
149
+ return a;
150
+ const x0 = Math.min(a.x, b.x), y0 = Math.min(a.y, b.y);
151
+ const x1 = Math.max(a.x + a.width, b.x + b.width), y1 = Math.max(a.y + a.height, b.y + b.height);
152
+ if ((x1 - x0) * (y1 - y0) > ANSWER_BOX_MAX_GROWTH * Math.max(a.width * a.height, 1))
153
+ return a;
154
+ return { x: x0, y: y0, width: x1 - x0, height: y1 - y0 };
155
+ };
156
+ return new Proxy(el, {
157
+ get(target, key) {
158
+ if (key === 'boundingBox')
159
+ return union;
160
+ const v = Reflect.get(target, key, target);
161
+ return typeof v === 'function' ? v.bind(target) : v;
162
+ },
163
+ });
164
+ }
131
165
  /** Every visible match of `selectors`, in selector order, queried at once. A selector this adapter can't parse is skipped, not fatal. */
132
166
  const visible = async (scope, selectors) => (await Promise.all(selectors.map((s) => scope.locator(s).filter({ visible: true }).all().catch(() => [])))).flat();
133
167
  const handleOf = (at) => at.elementHandle({ timeout: HANDLE_TIMEOUT_MS }).catch(() => null);
@@ -137,11 +171,14 @@ const frameOf = async (at) => (await (await handleOf(at))?.contentFrame().catch(
137
171
  * Where the host-page selectors are read: the page, then the inside of every VISIBLE vendor enclave. A hidden enclave is
138
172
  * skipped because Playwright judges an element visible within its own document, whatever its frame's visibility.
139
173
  */
140
- const hostScopes = async (page) => [page, ...(await Promise.all((await visible(page, selectors_1.ENCLAVE_SELECTORS)).map(frameOf))).filter((f) => f !== null)];
174
+ const hostScopes = async (page, enclaves) => [page, ...(await Promise.all((await visible(page, enclaves)).map(frameOf))).filter((f) => f !== null)];
141
175
  const visibleIn = async (scopes, selectors) => (await Promise.all(scopes.map((scope) => visible(scope, selectors)))).flat();
142
176
  const hasText = async (el) => !!((await el.textContent().catch(() => null)) ?? '').trim();
143
177
  const SLIDE_TOLERANCE_PX = 2;
144
178
  const SLIDE_MAX_CORRECTIONS = 3;
179
+ // The nudge that shows the tracker what the handle moves: enough to separate the part from still ground, little
180
+ // enough that a piece resting against the board's edge stays on it. Mirrors page_solver.py SLIDE_PROBE_PX.
181
+ const SLIDE_PROBE_PX = 8;
145
182
  /** An allow-list of what needs nothing: the other direction raises instead of clicking at the origin. */
146
183
  function answerNeedsElementBox(actions) {
147
184
  return actions.some((a) => a?.action !== kinds_1.ActionKind.DONE);
@@ -166,6 +203,16 @@ const NOT_THIS_BOARD_POLLS = 3;
166
203
  // How long a control we hovered, pressed or typed into takes to stop repainting in reaction. A board read inside
167
204
  // this window is wearing our own feedback, which is not a screen it ever showed by itself.
168
205
  exports.INPUT_SETTLE_MS = 400;
206
+ // While a reCAPTCHA tile reloads the hand rests on it, drifting a few pixels every second or two. Reaching for a
207
+ // different reloading tile on every 250 ms poll read as a cursor darting about the board.
208
+ exports.IDLE_DRIFT_EVERY_MS = [1200, 2800];
209
+ exports.IDLE_DRIFT_PX = 3;
210
+ // How much longer than gridLoadTimeoutMs a reCAPTCHA tile that is still painting is waited on.
211
+ exports.GRID_LOAD_CAP_FACTOR = 3;
212
+ // A press-and-hold is released once the widget's picture has stopped changing for this long after its fill
213
+ // moved, and at HOLD_MAX_MS whatever it shows. Press & Hold widgets ask for seconds, not tens of seconds.
214
+ exports.HOLD_SETTLE_MS = 900;
215
+ exports.HOLD_MAX_MS = 12000;
169
216
  /** `viewportSize()` is null on a context launched without one (camoufox), where only the window knows. */
170
217
  async function viewportOf(page) {
171
218
  const view = page.viewportSize() ?? await page.evaluate?.(() => ({ width: window.innerWidth, height: window.innerHeight }));
@@ -232,6 +279,7 @@ class CaptchaKrakenSolver {
232
279
  constructor(config = {}) {
233
280
  this.videoBudgetMs = 0;
234
281
  this.videoBudgetGranted = false;
282
+ this.sel = selectors_1.DEFAULT_TABLE;
235
283
  this.stepIndex = 0;
236
284
  this.solveStartMs = 0;
237
285
  this.cvWorker = null;
@@ -241,6 +289,15 @@ class CaptchaKrakenSolver {
241
289
  this.cvWorkerBuf = '';
242
290
  /** Answers keyed by screenshot hash; a hit means the answer already ran and changed nothing. */
243
291
  this.solutionCache = new Map();
292
+ /** The other complete plans the pointer returned for this board, best first; a refresh is last. */
293
+ this.fallbackPlans = [];
294
+ /** The screenshot those plans answered. A different board makes them the wrong answer, not an alternative. */
295
+ this.fallbackBoardHash = null;
296
+ /** True only once THIS board refused the plan at the front. Nothing is pressed speculatively. */
297
+ this.fallbackArmed = false;
298
+ this.exchangesAtAnswer = 0;
299
+ /** Earlier screens of the same paged puzzle (FunCaptcha), oldest first; the CLI sends them as `--history`. */
300
+ this.pointerHistory = [];
244
301
  this.repeatedAnswerSeen = false;
245
302
  this.knownAnimated = false;
246
303
  this.animatedProbeDone = false;
@@ -250,8 +307,9 @@ class CaptchaKrakenSolver {
250
307
  this.keyframeSteadyScreens = 0;
251
308
  this.solveDeadlineAt = 0;
252
309
  this.lastAnswerSig = null;
310
+ this.triedAnswers = new Set();
311
+ this.refreshedBoards = new Set();
253
312
  this.noProgressRounds = 0;
254
- this.resampleLevel = 0;
255
313
  /** The recording `classifyByRecording` started and left running for the branch below to finish or drop. */
256
314
  this.pendingBurst = null;
257
315
  /**
@@ -264,6 +322,7 @@ class CaptchaKrakenSolver {
264
322
  this.animatedFilm = null;
265
323
  this.lastBurstFps = null;
266
324
  this.actedOnBoard = false;
325
+ this.submittedOnBoard = false;
267
326
  this.lastSubmitFrameHash = null;
268
327
  this.solveSessionId = null;
269
328
  /** Which vendor and site this solve is for, sent to the hosted API; the site is a hostname only. */
@@ -283,6 +342,9 @@ class CaptchaKrakenSolver {
283
342
  /** When our last gesture ended; the board's reaction to it is never read as the board's own motion. */
284
343
  this.lastInputAt = -Infinity;
285
344
  this.config = config;
345
+ if (config.customSelectors || config.selectors !== undefined) {
346
+ this.sel = new selectors_1.SelectorTable(config.customSelectors, config.selectors ?? selectors_1.DEFAULT_CONFIG_PATH);
347
+ }
286
348
  this.human = (0, humanize_js_1.resolveHumanizer)({ ...config, startingMousePosition: config.startingMousePosition ?? { x: 100, y: 100 } });
287
349
  }
288
350
  get lastMousePosition() {
@@ -359,6 +421,7 @@ class CaptchaKrakenSolver {
359
421
  this.budget = new timing_1.PhaseBudget();
360
422
  this.verdicts = null;
361
423
  let solvedForReport = false;
424
+ await this.wakeLocalServer();
362
425
  try {
363
426
  const result = await this.solveImpl(page);
364
427
  if (result)
@@ -371,6 +434,7 @@ class CaptchaKrakenSolver {
371
434
  console.error(this.budget.report());
372
435
  // The camera outlives a round by design, so it has to be ended by the thing that outlives the solve.
373
436
  await this.stopAnimatedFilm();
437
+ this.dropPointerHistory();
374
438
  this.teardownCvWorker();
375
439
  this.cvWorkerReady = null;
376
440
  // Set by solveImpl, which the narrowing from the reset above cannot see.
@@ -390,7 +454,7 @@ class CaptchaKrakenSolver {
390
454
  return new Promise((resolve) => {
391
455
  try {
392
456
  const { cliRoot, py } = this.resolveCli();
393
- const child = (0, child_process_1.spawn)(py, ['-m', 'captchakraken.cli', 'report-outcome', sessionId, solved ? kinds_1.Outcome.SOLVED : kinds_1.Outcome.FAILED], { cwd: cliRoot, env: cliEnv(cliRoot, this.routingEnv()), stdio: ['ignore', 'pipe', 'pipe'] });
457
+ const child = (0, child_process_1.spawn)(py, ['-m', 'captchakraken.cli', 'report-outcome', sessionId, solved ? kinds_1.Outcome.SOLVED : kinds_1.Outcome.FAILED], { cwd: cliRoot, env: (0, cli_invocation_1.solveEnv)(cliEnv(cliRoot, this.routingEnv()), this.apiKey), stdio: ['ignore', 'pipe', 'pipe'] });
394
458
  let stdout = '';
395
459
  child.stdout.on('data', (chunk) => { stdout += chunk.toString(); });
396
460
  // The engine words its own warnings; a hosted refusal must be as visible here as it is from Python.
@@ -562,14 +626,18 @@ class CaptchaKrakenSolver {
562
626
  break;
563
627
  }
564
628
  if (await this.isChallengeFreshlyRendered(page)) {
565
- this.resampleLevel = 0;
566
629
  this.actedOnBoard = false;
630
+ this.submittedOnBoard = false;
567
631
  // ONE FILM, ONE BOARD — enforced where the board actually changes. The recorded answer
568
632
  // describes the board it was cut from, so replaying it here presses that board's
569
633
  // coordinates onto this one: live, a keyframe answer from an earlier board landed on
570
634
  // hCaptcha's reference photo and then pressed Skip, six rounds running.
571
635
  await this.stopAnimatedFilm();
572
636
  this.knownAnimated = false;
637
+ this.fallbackPlans = [];
638
+ this.fallbackBoardHash = null;
639
+ this.fallbackArmed = false;
640
+ this.dropPointerHistory();
573
641
  // Its own second look, but only once IT fails: what armed it was evidence about the previous board,
574
642
  // and left set it filmed every still board after a solve's first miss.
575
643
  this.repeatedAnswerSeen = false;
@@ -587,9 +655,15 @@ class CaptchaKrakenSolver {
587
655
  throw blocked('its server answered 429');
588
656
  if (await this.isBlocked(page))
589
657
  throw blocked('it is showing its try-again-later screen');
590
- if (roundSaid === kinds_1.Verdict.REJECTED)
658
+ if (roundSaid === kinds_1.Verdict.REJECTED) {
591
659
  console.log(`[verdict] the vendor rejected the answer after ${Date.now() - verdictT0}ms`);
592
- if (this.bannerIsFatalAfterRetry(await this.bannerKind(page))) {
660
+ // Only a refusal earns the server's next plan. Without it a board that merely repainted would have
661
+ // the previous board's plan pressed onto it.
662
+ this.fallbackArmed = true;
663
+ }
664
+ // Only "select all matching images" means tiles were missed; "please try again" is a wrong answer and
665
+ // deals a new board, which is asked fresh.
666
+ if ((await this.bannerKind(page)) === kinds_1.RecaptchaBanner.SELECT_MORE) {
593
667
  console.log('reCAPTCHA under-selection error; the next round asks for the missed tiles.');
594
668
  pendingRetryMode = kinds_1.RetryMode.MISSED_TILES;
595
669
  }
@@ -597,10 +671,10 @@ class CaptchaKrakenSolver {
597
671
  return done();
598
672
  if (this.noProgressRounds) {
599
673
  const stuck = `No progress: the model returned the same answer ${this.noProgressRounds + 1} times running`;
600
- // A board answered the same way after every resample is one the model cannot read: measured, no solve ever
601
- // followed a third identical answer, and each further round cost the board 3-7s.
602
- if (this.noProgressRounds >= (cfg.maxNoProgressRounds ?? 2))
674
+ // A repeat pressed nothing, so a further round is only worth its 3-7s if it can do something else.
675
+ if (this.noProgressRounds >= (cfg.maxNoProgressRounds ?? 2) || !this.anotherMoveLeft(pendingRetryMode)) {
603
676
  throw new Error(`${stuck}.`);
677
+ }
604
678
  await again(stuck);
605
679
  }
606
680
  else if (!didInteract) {
@@ -610,6 +684,9 @@ class CaptchaKrakenSolver {
610
684
  this.repeatedAnswerSeen = true; // what arms the second look
611
685
  await again('The solver performed no interactions this round');
612
686
  }
687
+ else {
688
+ lastFailure = null;
689
+ }
613
690
  }
614
691
  const last = lastFailure;
615
692
  const usage = ` Total usage: ${JSON.stringify((0, token_usage_1.aggregateTokenUsage)(cumulativeTokenUsage))}`;
@@ -638,12 +715,12 @@ class CaptchaKrakenSolver {
638
715
  /** The vendor showing its refusal screen: rate-limited or flagged, with no board to answer. */
639
716
  async isBlocked(page) {
640
717
  try {
641
- const probes = selectors_1.WIDGET_PROBES.filter((p) => p.role === kinds_1.FrameRole.CHALLENGE && selectors_1.SELECTORS[p.vendor].blocked);
642
- const scopes = await hostScopes(page);
718
+ const probes = this.sel.widgetProbes.filter((p) => p.role === kinds_1.FrameRole.CHALLENGE && this.sel.selectors[p.vendor].blocked);
719
+ const scopes = await hostScopes(page, this.sel.enclave);
643
720
  const shown = await Promise.all(probes.map(async (p) => {
644
721
  const [at] = await visibleIn(scopes, [p.selector]);
645
722
  const frame = at && await frameOf(at);
646
- return !!frame && (await Promise.all((selectors_1.SELECTORS[p.vendor].blocked ?? []).map((sel) => this.visibleWithText(frame, sel)))).some(Boolean);
723
+ return !!frame && (await Promise.all((this.sel.selectors[p.vendor].blocked ?? []).map((sel) => this.visibleWithText(frame, sel)))).some(Boolean);
647
724
  }));
648
725
  return shown.some(Boolean);
649
726
  }
@@ -652,14 +729,24 @@ class CaptchaKrakenSolver {
652
729
  }
653
730
  }
654
731
  /** The env the engine reads its routing headers from: session, and the vendor and site once they are known. */
732
+ /** This solver's endpoint and key: two solvers in one process may talk to different servers. */
733
+ get baseUrl() {
734
+ return this.config.baseUrl ?? process.env.VLLM_BASE_URL;
735
+ }
736
+ get apiKey() {
737
+ return this.config.apiKey ?? process.env.CAPTCHA_KRAKEN_API_KEY ?? process.env.VLLM_API_KEY;
738
+ }
655
739
  routingEnv() {
656
740
  return {
741
+ ...(this.config.baseUrl ? { VLLM_BASE_URL: this.config.baseUrl } : {}),
657
742
  ...(this.solveSessionId ? { CAPTCHA_KRAKEN_SESSION: this.solveSessionId } : {}),
658
743
  ...(this.solveVendor ? { CAPTCHA_KRAKEN_VENDOR: this.solveVendor } : {}),
659
744
  ...(this.solveSite ? { CAPTCHA_KRAKEN_SITE: this.solveSite } : {}),
660
745
  ...(this.solveWidgetHost ? { CAPTCHA_KRAKEN_WIDGET_HOST: this.solveWidgetHost } : {}),
661
746
  // This driver warns about a legacy model name itself, once; the engine it starts per round stays quiet.
662
747
  CAPTCHA_KRAKEN_MODEL_WARNING: '0',
748
+ // The config's detector choice; the caller's own environment variable still wins, as it does in Python.
749
+ ...(process.env.CAPTCHA_KRAKEN_DETECTOR_MODEL ? {} : { CAPTCHA_KRAKEN_DETECTOR_MODEL: this.sel.config.detector }),
663
750
  };
664
751
  }
665
752
  /** Fire the optional onStep observer with a fresh screenshot; best-effort, never fails the solve. */
@@ -685,7 +772,8 @@ class CaptchaKrakenSolver {
685
772
  }
686
773
  async solveSingle(page, widget, attempt, retryMode = null) {
687
774
  const cfg = this.config;
688
- const { el: captchaElement, vendor: puzzleSource, role: frameRole } = widget;
775
+ const { vendor: puzzleSource, role: frameRole } = widget;
776
+ let captchaElement = widget.el;
689
777
  this.solveVendor = puzzleSource;
690
778
  const frame = await captchaElement.contentFrame();
691
779
  this.solveWidgetHost = frame ? hostname(frame) : null;
@@ -694,12 +782,12 @@ class CaptchaKrakenSolver {
694
782
  await this.clickCheckbox(page, widget, frame);
695
783
  return { didInteract: true, tokenUsage: [] };
696
784
  }
697
- if (frame && frameRole === kinds_1.FrameRole.CHALLENGE && selectors_1.SELECTORS[puzzleSource].images) {
785
+ if (frame && frameRole === kinds_1.FrameRole.CHALLENGE && this.sel.selectors[puzzleSource].images) {
698
786
  if (this.lastSubmitFrameHash) {
699
787
  await this.ph(kinds_1.Phase.AWAIT_NEXT_ROUND, () => this.waitForChangeSince(captchaElement, this.lastSubmitFrameHash));
700
788
  this.lastSubmitFrameHash = null;
701
789
  }
702
- await this.ph(kinds_1.Phase.HCAPTCHA_IMAGES, () => this.waitForBoardImages(frame, selectors_1.SELECTORS[puzzleSource]));
790
+ await this.ph(kinds_1.Phase.BOARD_IMAGES, () => this.waitForBoardImages(frame, this.sel.selectors[puzzleSource]));
703
791
  }
704
792
  // THE TWO QUESTIONS BELOW ARE PUT TO A PAINTED BOARD. Which expert answers the round is read out of the
705
793
  // DOM and whether the board cycles is read off its motion, and a widget that has not drawn yet has no
@@ -711,9 +799,12 @@ class CaptchaKrakenSolver {
711
799
  const painted = puzzleSource === kinds_1.Vendor.RECAPTCHA ? null
712
800
  : await this.ph(kinds_1.Phase.BOARD_PAINT, () => this.waitForBoardPainted(captchaElement));
713
801
  // Only the DOM can tell a typed captcha from a click puzzle; hCaptcha and reCAPTCHA never type.
714
- const textMode = !VENDORS_WITH_BESPOKE_HANDLING.has(puzzleSource) && (await this.answerBox(scope, widget.at)) !== null;
715
- if (textMode)
802
+ const answerBox = VENDORS_WITH_BESPOKE_HANDLING.has(puzzleSource) ? null : await this.answerBox(scope, widget.at);
803
+ const textMode = answerBox !== null;
804
+ if (textMode) {
716
805
  console.log('Widget has a text box; solving as a distorted-text captcha.');
806
+ captchaElement = withAnswerBox(captchaElement, answerBox);
807
+ }
717
808
  // A reCAPTCHA board is read by its grid below, not filmed.
718
809
  const filmable = puzzleSource !== kinds_1.Vendor.RECAPTCHA && !textMode;
719
810
  if (filmable && !this.knownAnimated)
@@ -768,12 +859,17 @@ class CaptchaKrakenSolver {
768
859
  const screenshotPath = tmp('captcha');
769
860
  const settledFrame = (isAnimated || painted?.verdict !== kinds_1.PaintVerdict.PAINTED)
770
861
  ? null : (this.pendingBurst?.stableFrame() ?? null);
771
- if (settledFrame && fs.existsSync(settledFrame)) {
862
+ const filmed = !!settledFrame && fs.existsSync(settledFrame);
863
+ if (filmed) {
772
864
  fs.copyFileSync(settledFrame, screenshotPath);
773
865
  }
774
866
  else {
775
867
  await this.ph(kinds_1.Phase.SCREENSHOT, () => this.shot(captchaElement, screenshotPath, cfg.elementScreenshotTimeoutMs ?? 8000));
776
868
  }
869
+ const boardHash = await this.boardHash(captchaElement, screenshotPath, filmed);
870
+ // A refresh that brought the same picture back has replaced nothing, and every plan has been spent on it.
871
+ if (this.refreshedBoards.has(boardHash))
872
+ throw new Error('No progress: the board came back unchanged after a refresh.');
777
873
  if (this.stepIndex === 0) {
778
874
  await this.emitStep(captchaElement, kinds_1.SolveStage.INITIAL, 'initial (pre-action)', puzzleSource, frameRole, attempt, establishedGridSize ? { gridSize: establishedGridSize } : undefined);
779
875
  }
@@ -784,9 +880,22 @@ class CaptchaKrakenSolver {
784
880
  let burstDir = null;
785
881
  try {
786
882
  let response = null;
883
+ let fromFallback = false;
884
+ if (this.fallbackPlans.length) {
885
+ if (this.fallbackArmed && this.fallbackBoardHash === boardHash) {
886
+ response = { actions: this.fallbackPlans.shift(), token_usage: [] };
887
+ fromFallback = true;
888
+ }
889
+ else {
890
+ // The board that refused the first plan is not this one, or the last round did not refuse it.
891
+ this.fallbackPlans = [];
892
+ this.fallbackBoardHash = null;
893
+ this.fallbackArmed = false;
894
+ }
895
+ }
787
896
  const ask = (imagePath) => this.getSolution(imagePath, puzzleSource, retryMode, textMode);
788
897
  const askAnimated = () => this.ph(kinds_1.Phase.INFERENCE, () => this.withIdleWander(page, captchaElement, () => this.getAnimatedSolution(burstDir)));
789
- if (isAnimated) {
898
+ if (isAnimated && !response) {
790
899
  // The camera was paused while the last answer was performed; from here the board is its own again.
791
900
  this.animatedFilm?.resume();
792
901
  if (this.animatedPlan?.response) {
@@ -891,6 +1000,11 @@ class CaptchaKrakenSolver {
891
1000
  else if (!response) {
892
1001
  response = await this.ph(kinds_1.Phase.INFERENCE, () => this.solveFrameFreshnessGuarded(captchaElement, screenshotPath, (imagePath) => this.withIdleWander(page, captchaElement, () => ask(imagePath))));
893
1002
  }
1003
+ if (!fromFallback) {
1004
+ this.fallbackPlans = response.fallbacks ?? [];
1005
+ this.fallbackBoardHash = this.fallbackPlans.length ? boardHash : null;
1006
+ this.fallbackArmed = false;
1007
+ }
894
1008
  const actionList = Array.isArray(response.actions) ? response.actions : [response.actions];
895
1009
  allTokenUsage = response.token_usage;
896
1010
  // A `done` answer carries no coordinates, and several vendors close the widget the moment they accept.
@@ -905,10 +1019,27 @@ class CaptchaKrakenSolver {
905
1019
  // Tier 3 grades this line with Tier 2's grader to split driver bugs from model misses (see TRIBAL_KNOWLEDGE.md).
906
1020
  console.log('[answer] ' + JSON.stringify({ actions: actionList }));
907
1021
  // A repeated answer is not re-performed: the widget already refused it, and every extra press is
908
- // behaviour a vendor scores. Re-asking with a fresh sample or a recording is the round's only move.
1022
+ // behaviour a vendor scores. Re-asking, or asking a recording, is the round's only move.
909
1023
  // `noteAnswer` drops the refused animated answer on its way through, so the next round re-asks.
910
- if (this.noteAnswer(actionList, retryMode))
1024
+ // An animated board's screens are SUPPOSED to change; keying a repeat on them would never fire. The
1025
+ // board hash is for the still path only, where a paged puzzle's same next-arrow is a new page.
1026
+ if (this.noteAnswer(actionList, retryMode, isAnimated ? null : boardHash))
911
1027
  return { didInteract: false, tokenUsage: allTokenUsage };
1028
+ // FunCaptcha pages one option at a time and the model reads the earlier screens as history. Without
1029
+ // them it has nothing to compare against and keeps answering "next arrow", including on the match.
1030
+ if (!isAnimated && puzzleSource === kinds_1.Vendor.FUNCAPTCHA
1031
+ && actionList.length && actionList.every((a) => a?.action === kinds_1.ActionKind.CLICK)) {
1032
+ try {
1033
+ if (fs.existsSync(screenshotPath)) {
1034
+ const saved = tmp('pghist');
1035
+ fs.copyFileSync(screenshotPath, saved);
1036
+ this.pointerHistory.push(saved);
1037
+ while (this.pointerHistory.length > 8)
1038
+ unlink(this.pointerHistory.shift());
1039
+ }
1040
+ }
1041
+ catch { /* a screen we could not keep simply is not history */ }
1042
+ }
912
1043
  console.log(`Executing ${actionList.length} actions.`);
913
1044
  // Stop filming before we touch it. Everything from here to the vendor's verdict is our own answer
914
1045
  // landing, and a frame of the board wearing our clicks is not a screen the board ever showed.
@@ -937,7 +1068,7 @@ class CaptchaKrakenSolver {
937
1068
  performedAction = answered = true;
938
1069
  await this.emitStep(captchaElement, kinds_1.SolveStage.CLICK, `clicked ${bboxes.length || 1} target(s)`, puzzleSource, frameRole, attempt, { bboxes });
939
1070
  }
940
- else if (action.action === kinds_1.ActionKind.DRAG && !action.source_bounding_box) {
1071
+ else if (action.action === kinds_1.ActionKind.SLIDE || (action.action === kinds_1.ActionKind.DRAG && !action.source_bounding_box)) {
941
1072
  if (await this.executeSlide(page, captchaElement, scope, action, requireBox())) {
942
1073
  performedAction = slid = true;
943
1074
  await this.emitStep(captchaElement, kinds_1.SolveStage.DRAG, 'slid the piece into the slot', puzzleSource, frameRole, attempt, { action });
@@ -951,12 +1082,25 @@ class CaptchaKrakenSolver {
951
1082
  performedAction = answered = true;
952
1083
  await this.emitStep(captchaElement, kinds_1.SolveStage.DRAG, 'drag', puzzleSource, frameRole, attempt, { action });
953
1084
  }
1085
+ else if (action.action === kinds_1.ActionKind.HOLD) {
1086
+ // The widget submits itself when the hold completes, like a slide on release.
1087
+ const completed = await this.executeHold(page, captchaElement, action, requireBox(), this.sel.selectors[widget.vendor].holdCompletes ?? false);
1088
+ performedAction = slid = true;
1089
+ await this.emitStep(captchaElement, kinds_1.SolveStage.HOLD, completed ? 'held until the widget completed' : 'released the hold at the cap', puzzleSource, frameRole, attempt, { action });
1090
+ }
954
1091
  else if (action.action === kinds_1.ActionKind.TYPE) {
955
1092
  if (await this.executeType(page, scope, action, widget.at)) {
956
1093
  performedAction = answered = true;
957
1094
  await this.emitStep(captchaElement, kinds_1.SolveStage.TYPE, 'typed the code', puzzleSource, frameRole, attempt, { action });
958
1095
  }
959
1096
  }
1097
+ else if (action.action === kinds_1.ActionKind.REFRESH) {
1098
+ // Nothing after a refresh, Verify included, belongs to the board it replaces.
1099
+ const refreshed = await this.executeRefresh(page, captchaElement, scope, action, elementBox);
1100
+ if (refreshed)
1101
+ await this.emitStep(captchaElement, kinds_1.SolveStage.REFRESH, 'asked for a new puzzle', puzzleSource, frameRole, attempt, { action });
1102
+ return { didInteract: refreshed, tokenUsage: allTokenUsage };
1103
+ }
960
1104
  else if (action.action === kinds_1.ActionKind.WAIT && action.duration_ms > 0) {
961
1105
  await delay(action.duration_ms);
962
1106
  performedAction = true;
@@ -964,15 +1108,22 @@ class CaptchaKrakenSolver {
964
1108
  }
965
1109
  }
966
1110
  // A slide submits itself on release, and any Verify found afterwards belongs to the host page and would
967
- // submit the guarded form mid-verdict. An empty or `done` plan still presses Verify/Skip.
968
- const verifyButton = frame || !slid ? await this.getVerifyButton(scope) : null;
969
- if (!slid && (answered || !performedAction) && verifyButton) {
1111
+ // submit the guarded form mid-verdict, unless this board has already taken a Verify from us: then that button
1112
+ // is the widget's, and a slide on it submits nothing by itself. An empty or `done` plan still presses
1113
+ // Verify/Skip. FunCaptcha is the exception: its answer is the click itself — an arrow pages, Submit (which the
1114
+ // model clicks on the match) submits — so the generic fallback submits page one right after an arrow turned it.
1115
+ const submits = slid ? this.submittedOnBoard : answered || !performedAction;
1116
+ const verifyButton = submits && puzzleSource !== kinds_1.Vendor.FUNCAPTCHA ? await this.getVerifyButton(scope) : null;
1117
+ if (verifyButton) {
970
1118
  console.log(`Clicking Verify to submit (${puzzleSource}).`);
971
1119
  await this.moveAndClick(page, verifyButton);
972
- performedAction = true;
1120
+ performedAction = this.submittedOnBoard = true;
973
1121
  await this.emitStep(captchaElement, kinds_1.SolveStage.SUBMIT, 'submitted (Verify/Next)', puzzleSource, frameRole, attempt);
974
1122
  this.lastSubmitFrameHash = await this.elementFrameHash(captchaElement);
975
1123
  }
1124
+ else if (answered && puzzleSource !== kinds_1.Vendor.FUNCAPTCHA) {
1125
+ this.keepPlansOfThisManyClicks(actionList);
1126
+ }
976
1127
  }
977
1128
  finally {
978
1129
  await this.releasePendingBurst();
@@ -989,11 +1140,15 @@ class CaptchaKrakenSolver {
989
1140
  * checkbox picture with a drag.
990
1141
  */
991
1142
  async clickCheckbox(page, widget, frame) {
992
- const box = await this.checkboxBox(frame, widget.vendor);
1143
+ const box = frame ? await this.checkboxBox(frame, widget.vendor) : (this.sel.selectors[widget.vendor].box?.length ? widget.el : null);
993
1144
  if (box)
994
1145
  await this.move(page, box);
995
- else
996
- await this.performSmoothMove(page, ...await this.findCheckboxOnScreen(widget.el));
1146
+ else {
1147
+ const at = await this.findCheckboxOnScreen(page, widget.el);
1148
+ if (!at)
1149
+ return;
1150
+ await this.performSmoothMove(page, ...at);
1151
+ }
997
1152
  await this.human.pause(kinds_1.PauseKind.BETWEEN);
998
1153
  await this.gesture(() => this.human.click(page, this.human.at));
999
1154
  await this.awaitWhatTheCheckboxOpened(page);
@@ -1003,7 +1158,7 @@ class CaptchaKrakenSolver {
1003
1158
  * a closed shadow root, and a frame can refuse access; both are found on screen instead.
1004
1159
  */
1005
1160
  async checkboxBox(frame, vendor) {
1006
- const box = selectors_1.SELECTORS[vendor].box;
1161
+ const box = this.sel.selectors[vendor].box;
1007
1162
  if (!frame || !box?.length)
1008
1163
  return null;
1009
1164
  try {
@@ -1014,8 +1169,28 @@ class CaptchaKrakenSolver {
1014
1169
  return null;
1015
1170
  }
1016
1171
  }
1017
- /** One capture and OpenCV: a point inside the tick box, in page coordinates. No model is asked. */
1018
- async findCheckboxOnScreen(el) {
1172
+ /**
1173
+ * A point inside the tick box, in page coordinates, or null when the widget passed without drawing one. Turnstile
1174
+ * shows "Verifying…" before its box, so an empty capture is looked at again until `detectionTimeoutMs`.
1175
+ */
1176
+ async findCheckboxOnScreen(page, el) {
1177
+ const deadline = Date.now() + (this.config.detectionTimeoutMs ?? exports.SOLVE_DEFAULTS.detectionTimeoutMs);
1178
+ for (;;) {
1179
+ const at = await this.checkboxOnScreen(el);
1180
+ if (at)
1181
+ return at;
1182
+ if (await this.isCaptchaSolved(page))
1183
+ return null;
1184
+ if (Date.now() >= deadline) {
1185
+ const e = new Error('UNSUPPORTED_CAPTCHA: the checkbox widget shows no tick box to click');
1186
+ e.unsupported = true;
1187
+ throw e;
1188
+ }
1189
+ await delay(DETECTION_POLL_MS);
1190
+ }
1191
+ }
1192
+ /** One capture and the element detector, no model: a point inside the tick box, or null when none is drawn. */
1193
+ async checkboxOnScreen(el) {
1019
1194
  const box = await el.boundingBox();
1020
1195
  if (!box)
1021
1196
  throw new Error('Could not get bounding box of captcha element');
@@ -1030,11 +1205,8 @@ class CaptchaKrakenSolver {
1030
1205
  finally {
1031
1206
  unlink(shot);
1032
1207
  }
1033
- if (!found) {
1034
- const e = new Error('UNSUPPORTED_CAPTCHA: the checkbox widget shows no tick box to click');
1035
- e.unsupported = true;
1036
- throw e;
1037
- }
1208
+ if (!found)
1209
+ return null;
1038
1210
  const [x, y, w, h] = found.map((v) => v / scale);
1039
1211
  return [box.x + x + w * (0.3 + Math.random() * 0.4), box.y + y + h * (0.3 + Math.random() * 0.4)];
1040
1212
  }
@@ -1054,7 +1226,7 @@ class CaptchaKrakenSolver {
1054
1226
  });
1055
1227
  }
1056
1228
  getVerifyButton(scope) {
1057
- return this.findControl(scope, selectors_1.SUBMIT_SELECTORS);
1229
+ return this.findControl(scope, this.sel.submit);
1058
1230
  }
1059
1231
  /** Any element at `selector` carrying a non-blank value; response fields are hidden, so no visibility filter. */
1060
1232
  async hasValue(scopes, selector) {
@@ -1069,19 +1241,16 @@ class CaptchaKrakenSolver {
1069
1241
  const frame = await frameOf(checkbox);
1070
1242
  return !!frame && (await visible(frame, [checked])).length > 0;
1071
1243
  }
1072
- bannerIsFatalAfterRetry(kind) {
1073
- return kind === kinds_1.RecaptchaBanner.SELECT_MORE || kind === kinds_1.RecaptchaBanner.REJECTED;
1074
- }
1075
1244
  /** Which verdict banner the open challenge shows; `dynamic-more` is the dynamic board's normal flow, not an error. */
1076
1245
  async bannerKind(page) {
1077
- const probes = selectors_1.WIDGET_PROBES.filter((p) => p.role === kinds_1.FrameRole.CHALLENGE && selectors_1.SELECTORS[p.vendor].banners);
1078
- const scopes = await hostScopes(page);
1246
+ const probes = this.sel.widgetProbes.filter((p) => p.role === kinds_1.FrameRole.CHALLENGE && this.sel.selectors[p.vendor].banners);
1247
+ const scopes = await hostScopes(page, this.sel.enclave);
1079
1248
  const kinds = await Promise.all(probes.map(async (p) => {
1080
1249
  const [at] = await visibleIn(scopes, [p.selector]);
1081
1250
  const frame = at && await frameOf(at);
1082
1251
  if (!frame)
1083
1252
  return null;
1084
- const shown = await Promise.all((selectors_1.SELECTORS[p.vendor].banners ?? []).map(async ([sel, kind]) => (await this.visibleWithText(frame, sel)) ? kind : null));
1253
+ const shown = await Promise.all((this.sel.selectors[p.vendor].banners ?? []).map(async ([sel, kind]) => (await this.visibleWithText(frame, sel)) ? kind : null));
1085
1254
  return shown.find((k) => k !== null) ?? null;
1086
1255
  }));
1087
1256
  return kinds.find((k) => k !== null) ?? null;
@@ -1089,14 +1258,14 @@ class CaptchaKrakenSolver {
1089
1258
  /** The vendor's own done signal: a response token, a painted success state, or a checked box. */
1090
1259
  async isCaptchaSolved(page) {
1091
1260
  try {
1092
- const checkboxes = selectors_1.WIDGET_PROBES.filter((p) => p.role === kinds_1.FrameRole.CHECKBOX && selectors_1.SELECTORS[p.vendor].checked);
1093
- const scopes = await hostScopes(page);
1261
+ const checkboxes = this.sel.widgetProbes.filter((p) => p.role === kinds_1.FrameRole.CHECKBOX && this.sel.selectors[p.vendor].checked);
1262
+ const scopes = await hostScopes(page, this.sel.enclave);
1094
1263
  const signals = await Promise.all([
1095
- ...selectors_1.RESPONSE_SELECTORS.map((s) => this.hasValue(scopes, s)),
1096
- visibleIn(scopes, selectors_1.ACCEPTED_SELECTORS).then((found) => found.length > 0),
1264
+ ...this.sel.response.map((s) => this.hasValue(scopes, s)),
1265
+ visibleIn(scopes, this.sel.accepted).then((found) => found.length > 0),
1097
1266
  ...checkboxes.map(async (p) => {
1098
1267
  const [at] = await visibleIn(scopes, [p.selector]);
1099
- return !!at && this.checkedIn(at, selectors_1.SELECTORS[p.vendor].checked);
1268
+ return !!at && this.checkedIn(at, this.sel.selectors[p.vendor].checked);
1100
1269
  }),
1101
1270
  ]);
1102
1271
  return signals.some(Boolean);
@@ -1108,15 +1277,15 @@ class CaptchaKrakenSolver {
1108
1277
  /** A next round has painted, as opposed to the answered frame animating closed. */
1109
1278
  async isChallengeFreshlyRendered(page) {
1110
1279
  try {
1111
- const probes = selectors_1.WIDGET_PROBES.filter((p) => p.role === kinds_1.FrameRole.CHALLENGE && selectors_1.SELECTORS[p.vendor].fresh);
1112
- const scopes = await hostScopes(page);
1280
+ const probes = this.sel.widgetProbes.filter((p) => p.role === kinds_1.FrameRole.CHALLENGE && this.sel.selectors[p.vendor].fresh);
1281
+ const scopes = await hostScopes(page, this.sel.enclave);
1113
1282
  const fresh = await Promise.all(probes.map(async (p) => {
1114
1283
  const [at] = await visibleIn(scopes, [p.selector]);
1115
1284
  const el = at && await handleOf(at);
1116
1285
  if (!el || (this.lastSubmitFrameHash && (await this.elementFrameHash(el)) === this.lastSubmitFrameHash))
1117
1286
  return false;
1118
1287
  const frame = await el.contentFrame();
1119
- return !!frame && this.visibleWithText(frame, selectors_1.SELECTORS[p.vendor].fresh);
1288
+ return !!frame && this.visibleWithText(frame, this.sel.selectors[p.vendor].fresh);
1120
1289
  }));
1121
1290
  return fresh.some(Boolean);
1122
1291
  }
@@ -1141,7 +1310,7 @@ class CaptchaKrakenSolver {
1141
1310
  const bg = getComputedStyle(el).backgroundImage;
1142
1311
  return !!bg && bg !== 'none' && !/url\(["']?["']?\)/.test(bg);
1143
1312
  });
1144
- }, { prompt: s.fresh ?? '', images: (s.images ?? []).join(',') }, { timeout: this.config.hcaptchaImagesTimeoutMs ?? 3000 });
1313
+ }, { prompt: s.fresh ?? '', images: (s.images ?? []).join(',') }, { timeout: this.config.boardImagesTimeoutMs ?? this.config.hcaptchaImagesTimeoutMs ?? 3000 });
1145
1314
  }
1146
1315
  catch { /* timed out or detached mid-load; screenshot anyway */ }
1147
1316
  }
@@ -1167,12 +1336,12 @@ class CaptchaKrakenSolver {
1167
1336
  return [];
1168
1337
  }
1169
1338
  const blob = names.join(' ');
1170
- return selectors_1.VENDORS.filter(([, s]) => s.hosts.some((h) => blob.includes(h))).map(([vendor]) => vendor);
1339
+ return this.sel.vendors.filter(([, s]) => s.hosts.some((h) => blob.includes(h))).map(([vendor]) => vendor);
1171
1340
  }
1172
1341
  /** Vendors showing a frame no selector names. Only asked once detection has found nothing, and a passive frame (an invisible badge, an enclave) is not a widget, so what is left is markup the table no longer matches. */
1173
1342
  async unmatchedVendorFrames(page) {
1174
- const scopes = await hostScopes(page);
1175
- const counts = await Promise.all(selectors_1.VENDORS.map(async ([vendor, s]) => {
1343
+ const scopes = await hostScopes(page, this.sel.enclave);
1344
+ const counts = await Promise.all(this.sel.vendors.map(async ([vendor, s]) => {
1176
1345
  if (!s.hosts.length)
1177
1346
  return null;
1178
1347
  const [frames, passive] = await Promise.all([visibleIn(scopes, s.hosts.map((h) => `iframe[src*="${h}"]`)), visibleIn(scopes, s.passive ?? [])]);
@@ -1197,12 +1366,12 @@ class CaptchaKrakenSolver {
1197
1366
  * count: without them this port failed fast in under a second on every GeeTest/Yidun page and disagreed with Python in Tier 3.
1198
1367
  */
1199
1368
  async hasInteractiveWidgetInDom(page) {
1200
- return (await Promise.all(selectors_1.WIDGET_PROBES.map((p) => page.locator(p.selector).count().catch(() => 0)))).some((n) => n > 0);
1369
+ return (await Promise.all(this.sel.widgetProbes.map((p) => page.locator(p.selector).count().catch(() => 0)))).some((n) => n > 0);
1201
1370
  }
1202
1371
  /** Open challenges first, then unsolved checkboxes, then the inline vendors; every probe is queried at once. */
1203
1372
  async detectCaptcha(page) {
1204
- const scopes = await hostScopes(page);
1205
- const found = await Promise.all(selectors_1.WIDGET_PROBES.map(async (p) => {
1373
+ const scopes = await hostScopes(page, this.sel.enclave);
1374
+ const found = await Promise.all(this.sel.widgetProbes.map(async (p) => {
1206
1375
  const [at] = await visibleIn(scopes, [p.selector]);
1207
1376
  return at && !(await this.alreadyAccepted(scopes, at, p)) ? { ...p, at } : null;
1208
1377
  }));
@@ -1212,7 +1381,7 @@ class CaptchaKrakenSolver {
1212
1381
  }
1213
1382
  /** A checkbox the vendor has already accepted is not a captcha to solve. */
1214
1383
  async alreadyAccepted(scopes, at, { role, vendor }) {
1215
- const { response, checked } = selectors_1.SELECTORS[vendor];
1384
+ const { response, checked } = this.sel.selectors[vendor];
1216
1385
  if (role !== kinds_1.FrameRole.CHECKBOX)
1217
1386
  return false;
1218
1387
  const [token, box] = await Promise.all([!!response && this.hasValue(scopes, response), !!checked && this.checkedIn(at, checked)]);
@@ -1220,14 +1389,14 @@ class CaptchaKrakenSolver {
1220
1389
  }
1221
1390
  loraName(cliRoot) {
1222
1391
  if (this.loraNameCache === null)
1223
- this.loraNameCache = (0, model_name_1.resolveLoraName)({ cliRoot, baseUrl: process.env.VLLM_BASE_URL });
1392
+ this.loraNameCache = (0, model_name_1.resolveLoraName)({ cliRoot, baseUrl: this.baseUrl });
1224
1393
  return this.loraNameCache;
1225
1394
  }
1226
- /** The `--model` argv, or nothing: without VLLM_BASE_URL the CLI can see a credentials file this process cannot. */
1395
+ /** The `--model` argv, or nothing: without a base URL the CLI can see a credentials file this process cannot. */
1227
1396
  modelName(cliRoot) {
1228
1397
  const explicit = this.config.model ?? process.env.CAPTCHA_LORA_NAME;
1229
- const name = explicit || (process.env.VLLM_BASE_URL ? this.loraName(cliRoot) : undefined);
1230
- if (name && !this.legacyModelWarned && (0, model_name_1.isHostedEndpoint)(process.env.VLLM_BASE_URL)
1398
+ const name = explicit || (this.baseUrl ? this.loraName(cliRoot) : undefined);
1399
+ if (name && !this.legacyModelWarned && (0, model_name_1.isHostedEndpoint)(this.baseUrl)
1231
1400
  && (0, model_name_1.isLegacyHostedName)(name, cliRoot)) {
1232
1401
  this.legacyModelWarned = true;
1233
1402
  console.error(`[captchakraken] ${model_name_1.LEGACY_MODEL_WARNING.replace('{model}', name)}`);
@@ -1269,6 +1438,12 @@ class CaptchaKrakenSolver {
1269
1438
  return {};
1270
1439
  }
1271
1440
  }
1441
+ /** Loading a model takes as long as the whole solve budget, so a cold server must not spend it. A stopped one says so on the first request. */
1442
+ async wakeLocalServer() {
1443
+ const stateDir = process.env.CAPTCHA_KRAKEN_STATE_DIR || path.join(os.homedir(), '.captchakraken');
1444
+ if (fs.existsSync(path.join(stateDir, 'selfhost.json')))
1445
+ await this.runCliTool(['server', 'ensure']);
1446
+ }
1272
1447
  /** Start the persistent CV worker once; false when it cannot start, so callers fall back to one-shot spawns. */
1273
1448
  ensureCvWorker() {
1274
1449
  if (this.cvWorkerReady)
@@ -1441,17 +1616,31 @@ class CaptchaKrakenSolver {
1441
1616
  unlink(f);
1442
1617
  }
1443
1618
  }
1619
+ /**
1620
+ * Hold the next ask until every tile has painted: a model asked about a half-loaded board answers it.
1621
+ *
1622
+ * Past the timeout a blank tile that has stopped changing is taken as painted (a pale photo, or a tile whose
1623
+ * image never came), and a board with no grid to read is let through. A tile still painting is waited on until
1624
+ * GRID_LOAD_CAP_FACTOR times that, or the solve's deadline.
1625
+ */
1444
1626
  async waitForGridCellsLoaded(captchaElement, opts) {
1445
1627
  const interval = opts?.intervalMs ?? this.config.gridLoadPollIntervalMs ?? 250;
1446
1628
  const timeout = opts?.timeoutMs ?? this.config.gridLoadTimeoutMs ?? 8000;
1447
- return (await this.pollFrames(captchaElement, timeout, interval, async (frames) => {
1629
+ const start = Date.now();
1630
+ const verdict = await this.pollFrames(captchaElement, timeout * exports.GRID_LOAD_CAP_FACTOR, interval, async (frames) => {
1448
1631
  if (frames.length < 2)
1449
1632
  return undefined;
1633
+ const late = Date.now() - start >= timeout;
1450
1634
  const res = await this.runCvTool('grid-cell-states', { a: frames[0], b: frames[1] }, ['grid-cell-states', frames[0], frames[1]]);
1451
- const ok = res && res.grid !== null && Array.isArray(res.loaded) && res.loaded.length > 0
1452
- && Array.isArray(res.empty) && res.empty.length === 0 && Array.isArray(res.changing) && res.changing.length === 0;
1453
- return ok ? true : undefined;
1454
- })) === true;
1635
+ if (!res || res.grid === null)
1636
+ return late ? false : undefined;
1637
+ if (!Array.isArray(res.loaded) || !res.loaded.length || !Array.isArray(res.changing) || res.changing.length)
1638
+ return undefined;
1639
+ return !Array.isArray(res.empty) || !res.empty.length || late ? true : undefined;
1640
+ });
1641
+ if (verdict !== true)
1642
+ console.log('[captchakraken] [recaptcha-grid] the board never finished loading; asking about it as it stands');
1643
+ return verdict === true;
1455
1644
  }
1456
1645
  async getGridBoxes(captchaElement) {
1457
1646
  const f = tmp('findgrid');
@@ -1480,12 +1669,25 @@ class CaptchaKrakenSolver {
1480
1669
  const [x1, y1, x2, y2] = session.gridBoxes[cell - 1];
1481
1670
  return { x: session.elementBox.x + (x1 + x2) / 2 * session.scaleX, y: session.elementBox.y + (y1 + y2) / 2 * session.scaleY };
1482
1671
  }
1672
+ /** Rest the hand on a tile: reach it once, then drift a few pixels inside it. */
1483
1673
  async hoverCell(page, session, cell) {
1484
1674
  if (!this.human.hovers)
1485
1675
  return;
1486
- const [x1, y1, x2, y2] = session.gridBoxes[0];
1487
- const center = this.cellCenterPage(cell, session);
1488
- await this.performSmoothMove(page, center.x + (Math.random() - 0.5) * (x2 - x1) * session.scaleX * 0.4, center.y + (Math.random() - 0.5) * (y2 - y1) * session.scaleY * 0.4);
1676
+ const [x1, y1, x2, y2] = session.gridBoxes[cell - 1];
1677
+ const left = session.elementBox.x + x1 * session.scaleX, top = session.elementBox.y + y1 * session.scaleY;
1678
+ const right = session.elementBox.x + x2 * session.scaleX, bottom = session.elementBox.y + y2 * session.scaleY;
1679
+ let [x, y] = this.human.at;
1680
+ if (x >= left && x <= right && y >= top && y <= bottom) {
1681
+ x += (0, humanize_js_1.gauss)(exports.IDLE_DRIFT_PX);
1682
+ y += (0, humanize_js_1.gauss)(exports.IDLE_DRIFT_PX);
1683
+ }
1684
+ else {
1685
+ const center = this.cellCenterPage(cell, session);
1686
+ x = center.x + (Math.random() - 0.5) * (right - left) * 0.4;
1687
+ y = center.y + (Math.random() - 0.5) * (bottom - top) * 0.4;
1688
+ }
1689
+ const inset = Math.min(4, (right - left) / 4, (bottom - top) / 4);
1690
+ await this.performSmoothMove(page, Math.min(Math.max(x, left + inset), right - inset), Math.min(Math.max(y, top + inset), bottom - inset));
1489
1691
  }
1490
1692
  /** Per-cell state with the session's cached boxes: a blank mid-refresh frame has no lattice to detect. */
1491
1693
  async gridCellStates(session, frameA, frameB) {
@@ -1502,8 +1704,13 @@ class CaptchaKrakenSolver {
1502
1704
  hoverLoop(page, session, cells) {
1503
1705
  if (!cells.length)
1504
1706
  return undefined;
1505
- let i = 0;
1506
- return async () => { await this.hoverCell(page, session, cells[i++ % cells.length]).catch(() => { }); };
1707
+ let due = 0;
1708
+ return async () => {
1709
+ if (Date.now() < due)
1710
+ return;
1711
+ due = Date.now() + exports.IDLE_DRIFT_EVERY_MS[0] + Math.random() * (exports.IDLE_DRIFT_EVERY_MS[1] - exports.IDLE_DRIFT_EVERY_MS[0]);
1712
+ await this.hoverCell(page, session, cells[0]).catch(() => { });
1713
+ };
1507
1714
  }
1508
1715
  /**
1509
1716
  * `chipped`: the photos were kept, press Verify. `loading`: those tiles are being swapped, read the board again.
@@ -1579,6 +1786,12 @@ class CaptchaKrakenSolver {
1579
1786
  });
1580
1787
  continue;
1581
1788
  }
1789
+ if (action.action === kinds_1.ActionKind.REFRESH) {
1790
+ // A new board replaces this one: nothing clicked on it is submitted, and the next loop reads the new one.
1791
+ const frame = await captchaElement.contentFrame();
1792
+ const refreshed = !!frame && await this.executeRefresh(page, captchaElement, frame, action, elementBox);
1793
+ return { didInteract: refreshed || performedAction, tokenUsage: allTokenUsage };
1794
+ }
1582
1795
  if (action.action !== kinds_1.ActionKind.CLICK) {
1583
1796
  console.warn(`[recaptcha-grid] round ${round}: unexpected action '${action.action}'; re-solving.`);
1584
1797
  continue;
@@ -1592,6 +1805,11 @@ class CaptchaKrakenSolver {
1592
1805
  const clickedThisRound = [];
1593
1806
  for (const bbox of bboxes) {
1594
1807
  const cell = this.bboxToCell(bbox, session.gridBoxes, session.screenshotW, session.screenshotH);
1808
+ if (cell != null && clickedThisRound.includes(cell)) {
1809
+ // Two points in one tile: a second press deselects what the first selected.
1810
+ console.log(`[recaptcha-grid] round ${round}: cell ${cell} is already clicked; not pressing it again.`);
1811
+ continue;
1812
+ }
1595
1813
  await this.executeClick(page, captchaElement, { action: kinds_1.ActionKind.CLICK, target_bounding_box: bbox }, elementBox);
1596
1814
  if (cell != null) {
1597
1815
  clickedOrder.push(cell);
@@ -1969,7 +2187,7 @@ class CaptchaKrakenSolver {
1969
2187
  /** Slice a burst into keyframes and solve them in one model request. The frame number is the guard. */
1970
2188
  async getAnimatedSolution(framesDir) {
1971
2189
  const { cliRoot, py } = this.resolveCli();
1972
- const { apiKey = process.env.CAPTCHA_KRAKEN_API_KEY ?? process.env.VLLM_API_KEY } = this.config;
2190
+ const { apiKey } = this;
1973
2191
  const m = this.modelName(cliRoot);
1974
2192
  const args = ['-m', 'captchakraken.cli', 'solve-animated', '--frames-dir', framesDir,
1975
2193
  '--fps', String(this.lastBurstFps ?? this.config.videoBurstFps ?? 10), ...(m ? ['--model', m] : [])];
@@ -1991,7 +2209,7 @@ class CaptchaKrakenSolver {
1991
2209
  }
1992
2210
  /** The routing values and the credential travel in the environment, never in argv. */
1993
2211
  solveEnvironment(cliRoot, apiKey) {
1994
- return (0, cli_invocation_1.solveEnv)(cliEnv(cliRoot, this.routingEnv()), apiKey, this.resampleLevel);
2212
+ return (0, cli_invocation_1.solveEnv)(cliEnv(cliRoot, this.routingEnv()), apiKey);
1995
2213
  }
1996
2214
  /** Exit 2 is an unsupported puzzle, exit 3 the hosted API's own refusal; both are relayed, not reworded. */
1997
2215
  cliError(error, unsupportedMessage, failMessage) {
@@ -2077,27 +2295,53 @@ class CaptchaKrakenSolver {
2077
2295
  console.log(`[animated] widget never matched the chosen keyframe within ${timeout}ms (closest diff=${best.toFixed(4)}); clicking on the model's coordinates anyway, and recording afresh next round`);
2078
2296
  return false;
2079
2297
  }
2298
+ dropPointerHistory() {
2299
+ for (const screen of this.pointerHistory)
2300
+ unlink(screen);
2301
+ this.pointerHistory = [];
2302
+ }
2303
+ /** A click board with no Verify submits itself at a count, so a plan of any other count leaves its tiles ticked
2304
+ * and never submits, and the next plan's clicks untick them. */
2305
+ keepPlansOfThisManyClicks(plan) {
2306
+ const clicks = (actions) => actions.length && actions.every((a) => a?.action === kinds_1.ActionKind.CLICK)
2307
+ ? actions.reduce((n, a) => n + (a.target_bounding_boxes?.length ?? 1), 0)
2308
+ : null;
2309
+ const count = clicks(plan);
2310
+ if (count === null)
2311
+ return;
2312
+ this.fallbackPlans = this.fallbackPlans.filter((other) => [null, count].includes(clicks(other)));
2313
+ }
2080
2314
  resetSolveState() {
2081
2315
  this.solutionCache.clear();
2316
+ this.fallbackPlans = [];
2317
+ this.fallbackBoardHash = null;
2318
+ this.fallbackArmed = false;
2319
+ this.dropPointerHistory();
2082
2320
  this.repeatedAnswerSeen = false;
2083
2321
  this.knownAnimated = false;
2084
2322
  this.animatedProbeDone = false;
2085
2323
  this.discardAnimatedPlan();
2086
2324
  this.lastSubmitFrameHash = null;
2325
+ this.submittedOnBoard = false;
2087
2326
  this.keyframeMode = null;
2088
2327
  this.keyframeSteadyScreens = 0;
2089
2328
  this.solveDeadlineAt = 0;
2090
2329
  this.lastAnswerSig = null;
2330
+ this.triedAnswers.clear();
2331
+ this.refreshedBoards.clear();
2091
2332
  this.noProgressRounds = 0;
2092
- this.resampleLevel = 0;
2093
2333
  this.videoBudgetMs = 0;
2094
2334
  this.videoBudgetGranted = false;
2095
2335
  }
2096
- /** Keyed on the retry mode too: the missed-tiles answer legitimately overlaps the previous one. */
2097
- static answerSignature(actions, retryMode) {
2336
+ /**
2337
+ * Keyed on the BOARD and the retry mode, not the answer alone. A paged puzzle's next arrow sits at the
2338
+ * same point on every page, so a signature over the actions alone would call a correct page turn a
2339
+ * repeated answer and stop pressing it.
2340
+ */
2341
+ static answerSignature(actions, retryMode, boardHash) {
2098
2342
  const round3 = (v) => typeof v === 'number' ? Math.round(v * 1000) / 1000 : Array.isArray(v) ? v.map(round3) : v ?? null;
2099
2343
  try {
2100
- return JSON.stringify([retryMode ?? null, actions.map((a) => [a?.action ?? null, round3(a?.target_bounding_boxes),
2344
+ return JSON.stringify([boardHash, retryMode ?? null, actions.map((a) => [a?.action ?? null, round3(a?.target_bounding_boxes),
2101
2345
  round3(a?.target_bounding_box), round3(a?.target_coordinates), round3(a?.source_bounding_box), a?.text ?? null])]);
2102
2346
  }
2103
2347
  catch {
@@ -2110,29 +2354,70 @@ class CaptchaKrakenSolver {
2110
2354
  if (plan?.response)
2111
2355
  this.animatedPlan = { burstDir: plan.burstDir, response: null };
2112
2356
  }
2113
- /** True when this answer already ran and changed nothing: resample, and let the recording path have a go. */
2114
- noteAnswer(actions, retryMode) {
2115
- const sig = CaptchaKrakenSolver.answerSignature(actions, retryMode);
2116
- if (sig !== null && sig === this.lastAnswerSig) {
2357
+ /** True when this answer already ran and changed nothing: re-ask, and let the recording path have a go. */
2358
+ noteAnswer(actions, retryMode, boardHash = null) {
2359
+ const sig = CaptchaKrakenSolver.answerSignature(actions, retryMode, boardHash);
2360
+ // A refresh asks for a different board, so the same refresh twice is two boards, not a refused answer. An
2361
+ // answer that could not be signed could not be read either, and is not looked into.
2362
+ if (sig !== null && actions.some((a) => a?.action === kinds_1.ActionKind.REFRESH)) {
2363
+ this.noProgressRounds = 0;
2364
+ this.lastAnswerSig = null;
2365
+ if (boardHash !== null)
2366
+ this.refreshedBoards.add(boardHash);
2367
+ return false;
2368
+ }
2369
+ // A vendor can deal the same picture again, which clears the plans but not what was pressed on it. A paged
2370
+ // puzzle is exempt: coming back to a page after the others is a new question, because the history differs.
2371
+ const tried = sig !== null && boardHash !== null && !this.pointerHistory.length && this.triedAnswers.has(sig);
2372
+ if (sig !== null && (sig === this.lastAnswerSig || tried)) {
2117
2373
  this.noProgressRounds++;
2118
2374
  console.log(`[no-progress] the model returned the same answer again (${this.noProgressRounds} in a row) — the previous one already ran and changed nothing`);
2119
- this.resampleLevel++;
2120
- // A raised sample never reaches the wire while the cached animated answer stands in front of it.
2375
+ // A re-ask never reaches the wire while the cached animated answer stands in front of it.
2121
2376
  this.invalidateAnimatedAnswer();
2122
2377
  this.repeatedAnswerSeen = true;
2378
+ // The plan at the front of the queue ran and changed nothing. A board that said nothing to its server has
2379
+ // refused nothing, though, and another plan is one more press.
2380
+ this.fallbackArmed = !this.boardStayedSilent();
2123
2381
  return true;
2124
2382
  }
2125
2383
  this.noProgressRounds = 0;
2126
2384
  this.lastAnswerSig = sig;
2385
+ if (sig !== null)
2386
+ this.triedAnswers.add(sig);
2387
+ this.exchangesAtAnswer = this.verdicts?.exchanges ?? 0;
2127
2388
  return false;
2128
2389
  }
2390
+ /** Whether the round after a repeat can differ. At temperature 0 the same question gets the same answer, so it
2391
+ * takes a different question (a retry mode, a film) or one of the model's other plans. */
2392
+ anotherMoveLeft(retryMode) {
2393
+ return !!retryMode || this.knownAnimated
2394
+ || (this.config.videoSolveEnabled !== false && this.repeatedAnswerSeen && !this.animatedProbeDone)
2395
+ || (this.fallbackArmed && this.fallbackPlans.length > 0);
2396
+ }
2397
+ boardStayedSilent() {
2398
+ const log = this.verdicts;
2399
+ return !!log?.listening && log.exchanges === this.exchangesAtAnswer;
2400
+ }
2401
+ /** The board's identity, always from a plain photograph: a filmed frame of an unchanged board differs in bytes. */
2402
+ async boardHash(element, shot, filmed) {
2403
+ if (!filmed)
2404
+ return sha1(shot);
2405
+ const plain = tmp('board');
2406
+ try {
2407
+ await this.shot(element, plain, this.config.elementScreenshotTimeoutMs ?? 8000);
2408
+ return sha1(plain);
2409
+ }
2410
+ finally {
2411
+ fs.rmSync(plain, { force: true });
2412
+ }
2413
+ }
2129
2414
  /** A cache hit costs no inference, and means the answer already ran: the board cycles. */
2130
2415
  async answerFor(cacheKey, ask) {
2131
2416
  const cached = this.solutionCache.get(cacheKey);
2132
2417
  if (cached) {
2133
2418
  console.log('[dedup] this exact picture was already answered and the answer already ran — the challenge is cycling, not still; re-solving it as animated.');
2134
2419
  this.repeatedAnswerSeen = true;
2135
- return { actions: cached.actions, token_usage: [] };
2420
+ return { ...cached, token_usage: [] };
2136
2421
  }
2137
2422
  const fresh = await ask();
2138
2423
  this.solutionCache.set(cacheKey, fresh);
@@ -2206,14 +2491,20 @@ class CaptchaKrakenSolver {
2206
2491
  catch {
2207
2492
  cacheKey = null;
2208
2493
  }
2209
- const ask = () => this.askModel(imagePath, puzzleSource, retryMode, textMode);
2494
+ const ask = () => this.askModel(imagePath, puzzleSource, retryMode, textMode).catch((e) => {
2495
+ // No answer is an empty plan, which presses Skip for a new board. An empty text box is a wrong answer.
2496
+ if (!e?.unsupported || textMode)
2497
+ throw e;
2498
+ return { actions: [], token_usage: [] };
2499
+ });
2210
2500
  return cacheKey ? this.answerFor(cacheKey, ask) : ask();
2211
2501
  }
2212
2502
  /** One inference through the CLI. */
2213
2503
  async askModel(imagePath, puzzleSource = kinds_1.Vendor.UNKNOWN, retryMode = null, textMode = false) {
2214
2504
  const { cliRoot, py } = this.resolveCli();
2215
- const { apiKey = process.env.CAPTCHA_KRAKEN_API_KEY ?? process.env.VLLM_API_KEY } = this.config;
2216
- const args = (0, cli_invocation_1.buildSolveArgs)({ imagePath, model: this.modelName(cliRoot), puzzleSource, retryMode, textMode, expert: this.config.expert });
2505
+ const { apiKey } = this;
2506
+ const args = (0, cli_invocation_1.buildSolveArgs)({ imagePath, model: this.modelName(cliRoot), puzzleSource, retryMode, textMode,
2507
+ expert: this.config.expert, history: this.pointerHistory });
2217
2508
  console.log(`Executing CaptchaKraken CLI: ${(0, cli_invocation_1.redactCommand)([py, ...args].join(' '), apiKey)}`);
2218
2509
  try {
2219
2510
  const { stdout, stderr } = await execFileAsync(py, args, { cwd: cliRoot, env: this.solveEnvironment(cliRoot, apiKey), maxBuffer: 10 * 1024 * 1024, timeout: this.askTimeoutMs(), killSignal: 'SIGKILL' });
@@ -2222,24 +2513,7 @@ class CaptchaKrakenSolver {
2222
2513
  console.error('CaptchaKraken CLI stderr:', stderr);
2223
2514
  if (!stdout.trim())
2224
2515
  throw new Error(`CLI returned empty output. Stderr: ${stderr}`);
2225
- let actions = [];
2226
- let tokenUsage = [];
2227
- for (const line of stdout.trim().split('\n')) {
2228
- try {
2229
- const parsed = JSON.parse(line);
2230
- if (parsed.actions !== undefined && parsed.token_usage !== undefined) {
2231
- actions = parsed.actions;
2232
- tokenUsage = parsed.token_usage;
2233
- break;
2234
- }
2235
- if (Array.isArray(parsed))
2236
- actions = parsed;
2237
- else if (parsed.action && (parsed.target_bounding_box || parsed.target_coordinates || parsed.action === kinds_1.ActionKind.WAIT))
2238
- actions = [parsed];
2239
- }
2240
- catch { /* not json or not relevant */ }
2241
- }
2242
- return { actions, token_usage: tokenUsage };
2516
+ return (0, cli_invocation_1.parseSolveOutput)(stdout);
2243
2517
  }
2244
2518
  catch (error) {
2245
2519
  throw this.cliError(error, 'Cannot solve this kind of captcha', 'Failed to execute captcha solver CLI');
@@ -2277,7 +2551,7 @@ class CaptchaKrakenSolver {
2277
2551
  }
2278
2552
  // While a speculative burst is filming, the film is the better answer to "the screen changed".
2279
2553
  if (opts.recordingInFlight)
2280
- return { actions: response.actions, token_usage: mergedUsage };
2554
+ return { ...response, token_usage: mergedUsage };
2281
2555
  let changedDuringInference = 0;
2282
2556
  for (let i = 0; i < maxReSolves; i++) {
2283
2557
  if (!(await this.captchaFrameChangedSince(captchaElement, currentPath, threshold)))
@@ -2285,7 +2559,7 @@ class CaptchaKrakenSolver {
2285
2559
  if (++changedDuringInference >= 2 && !this.actedOnBoard && untouched()) {
2286
2560
  this.repeatedAnswerSeen = true;
2287
2561
  console.log('[freshness] the frame changed twice during inference with nothing clicked — this board cycles; recording it rather than re-solving a screen that has gone.');
2288
- return { actions: response.actions, token_usage: mergedUsage };
2562
+ return { ...response, token_usage: mergedUsage };
2289
2563
  }
2290
2564
  await this.waitForBoardPainted(captchaElement);
2291
2565
  const fresh = tmp(`freshsolve_${Date.now()}`);
@@ -2301,7 +2575,7 @@ class CaptchaKrakenSolver {
2301
2575
  response = await runQuery(currentPath);
2302
2576
  mergedUsage.push(...response.token_usage);
2303
2577
  }
2304
- return { actions: response.actions, token_usage: mergedUsage };
2578
+ return { ...response, token_usage: mergedUsage };
2305
2579
  }
2306
2580
  finally {
2307
2581
  unlink(liveAnchor);
@@ -2476,6 +2750,17 @@ class CaptchaKrakenSolver {
2476
2750
  return [action.target_coordinates[0] * elementBox.width, action.target_coordinates[1] * elementBox.height];
2477
2751
  return null;
2478
2752
  }
2753
+ /**
2754
+ * The press point of a hold: the model's own point, the middle of its box. A hold can be on a bar narrower than that
2755
+ * box, and a random spot in it lands off the bar, or close enough to its edge that the hand's drift lets go: the
2756
+ * widget then never fills, and the same answer fails the same way.
2757
+ */
2758
+ holdPointFor(action, elementBox) {
2759
+ if (!action.target_bounding_box)
2760
+ return null;
2761
+ const [minX, minY, maxX, maxY] = action.target_bounding_box;
2762
+ return [(minX + maxX) / 2 * elementBox.width, (minY + maxY) / 2 * elementBox.height];
2763
+ }
2479
2764
  /** Park on the target first, so only a mouse-down separates the right screen from the click. */
2480
2765
  async clickWhenFrameMatches(page, element, action, elementBox, awaitKeyframe) {
2481
2766
  const rel = this.clickPointFor(action, elementBox);
@@ -2498,18 +2783,115 @@ class CaptchaKrakenSolver {
2498
2783
  }
2499
2784
  await this.gesture(() => this.human.click(page, [elementBox.x + rel[0], elementBox.y + rel[1]]));
2500
2785
  }
2501
- async executeDrag(page, _element, action, elementBox) {
2786
+ /**
2787
+ * Press-and-hold until the widget completes: true when it did, false when the cap ran out. Complete is the vendor's
2788
+ * done signal, the widget closing, or its picture having changed (the fill) and then sat still for HOLD_SETTLE_MS.
2789
+ * When the answer names the fill's line, the fill reaching its end is complete too: see `fillReached`. Except on a
2790
+ * widget that `completesItself` (HUMAN's button): it verifies only once it has shown its done state, and letting go
2791
+ * AT full resets it, so only its own signals end the hold.
2792
+ */
2793
+ async executeHold(page, element, action, elementBox, completesItself = false) {
2502
2794
  this.actedOnBoard = true;
2503
- const center = (bbox) => [elementBox.x + ((bbox[0] + bbox[2]) / 2) * elementBox.width, elementBox.y + ((bbox[1] + bbox[3]) / 2) * elementBox.height];
2504
- await this.gesture(() => this.human.drag(page, center(action.source_bounding_box), center(action.target_bounding_box)));
2795
+ const rel = this.holdPointFor(action, elementBox);
2796
+ if (!rel)
2797
+ return false;
2798
+ const first = await this.elementFrameHash(element);
2799
+ const seen = { hash: first, since: Date.now(), changed: false };
2800
+ const shots = action.fill_from_bounding_box && action.fill_to_bounding_box && !completesItself ? [tmp('hold_0'), tmp('hold_1')] : null;
2801
+ const looks = [];
2802
+ const done = async () => {
2803
+ if (await this.isCaptchaSolved(page))
2804
+ return true;
2805
+ if (!(await element.boundingBox().catch(() => null)))
2806
+ return true;
2807
+ const n = looks.length;
2808
+ if (shots && (await this.fillReached(element, action, elementBox, shots, looks)))
2809
+ return true;
2810
+ // A look that just shot the widget is its frame too: a second screenshot per look slowed the looks enough for
2811
+ // a fast fill to pass the mark unseen between two.
2812
+ const fresh = shots && looks.length > n ? shots[n === 0 ? 0 : 1] : null;
2813
+ const h = fresh ? sha1(fresh) : await this.elementFrameHash(element);
2814
+ if (h !== seen.hash) {
2815
+ Object.assign(seen, { hash: h, since: Date.now(), changed: seen.changed || h !== first });
2816
+ return false;
2817
+ }
2818
+ return seen.changed && Date.now() - seen.since >= exports.HOLD_SETTLE_MS;
2819
+ };
2820
+ try {
2821
+ return await this.gesture(() => (0, humanize_js_1.hold)(this.human, page, [elementBox.x + rel[0], elementBox.y + rel[1]], done, exports.HOLD_MAX_MS));
2822
+ }
2823
+ finally {
2824
+ for (const f of shots ?? [])
2825
+ unlink(f);
2826
+ }
2505
2827
  }
2506
2828
  /**
2507
- * The slider piece's box: the first visible match small enough to be a piece. Runs during detection, so it must
2508
- * not set `actedOnBoard`: marking there disabled `shouldSpeculate` on every slide solve.
2829
+ * One look at a held widget's fill (`fill_tracker.py` behind the CV worker): true once it reaches the answer's `to`,
2830
+ * after waiting out the tracker's `release_in` when the fill arrives between looks.
2831
+ * The first look only shoots the reference, after the press, so the button's own pressed look is not read as fill;
2832
+ * `looks` carries what each later look saw. A look that cannot be shot or read is not a release: the widget's own
2833
+ * signals still end the hold.
2509
2834
  */
2510
- async measurePieceBox(scope, widgetWidth) {
2511
- const boxes = await Promise.all((await handles(await visible(scope, selectors_1.PIECE_SELECTORS))).map((h) => h.boundingBox().catch(() => null)));
2512
- return boxes.find((b) => !!b && b.width >= slide_geometry_1.MIN_PIECE_PX && b.width <= widgetWidth * slide_geometry_1.MAX_PIECE_FRACTION) ?? null;
2835
+ async fillReached(element, action, elementBox, shots, looks) {
2836
+ const [first, current] = shots;
2837
+ const { width: w, height: h } = elementBox;
2838
+ const timeout = this.config.elementScreenshotTimeoutMs ?? 8000;
2839
+ try {
2840
+ if (!looks.length) {
2841
+ const before = performance.now() / 1000;
2842
+ await this.shot(element, first, timeout);
2843
+ looks.push({ t: (before + performance.now() / 1000) / 2, edge: null, k: this.shotScale(first, w) });
2844
+ return false;
2845
+ }
2846
+ // The shot was taken somewhere inside the call; its middle is the best guess, and the release is timed from it.
2847
+ const before = performance.now() / 1000;
2848
+ await this.shot(element, current, timeout);
2849
+ const shotAt = (before + performance.now() / 1000) / 2;
2850
+ const k = looks[0].k;
2851
+ const history = looks.slice(1).map(({ t, edge }) => ({ t, edge }));
2852
+ const at = (b) => [((b[0] + b[2]) / 2) * w * k, ((b[1] + b[3]) / 2) * h * k];
2853
+ const gaps = history.slice(1).map((o, i) => o.t - history[i].t);
2854
+ const request = {
2855
+ first, current, start: at(action.fill_from_bounding_box), end: at(action.fill_to_bounding_box), t: shotAt,
2856
+ history, first_t: looks[0].t, poll_s: gaps.length ? gaps.reduce((x, y) => x + y, 0) / gaps.length : 0.25, tol: SLIDE_TOLERANCE_PX * k,
2857
+ };
2858
+ const res = await this.runCvTool('fill-step', request, ['fill-step', JSON.stringify(request)]);
2859
+ if (!res?.observation)
2860
+ return false;
2861
+ looks.push(res.observation);
2862
+ if (res.release) {
2863
+ console.log(`[hold] the fill reaches its mark ${(res.release_in ?? 0).toFixed(2)}s after a look ${((res.length - res.edge) / k).toFixed(1)}px short of it`);
2864
+ const wait = (shotAt + (res.release_in ?? 0)) * 1000 - performance.now();
2865
+ if (wait > 0)
2866
+ await new Promise((r) => setTimeout(r, wait));
2867
+ }
2868
+ return Boolean(res.release);
2869
+ }
2870
+ catch {
2871
+ return false;
2872
+ }
2873
+ }
2874
+ /** Deal a new puzzle: the vendor's own control, else the model's point on one. False when there is neither. */
2875
+ async executeRefresh(page, element, scope, action, elementBox) {
2876
+ const control = await this.findControl(scope, this.sel.refresh);
2877
+ if (control) {
2878
+ this.actedOnBoard = true;
2879
+ await this.moveAndClick(page, control);
2880
+ }
2881
+ else if (action.target_bounding_box && elementBox) {
2882
+ await this.executeClick(page, element, { action: kinds_1.ActionKind.CLICK, target_bounding_box: action.target_bounding_box }, elementBox);
2883
+ }
2884
+ else {
2885
+ console.log('The model asked for a new puzzle, and there is no control to deal one; ending the round.');
2886
+ return false;
2887
+ }
2888
+ console.log('Asked for a new puzzle instead of solving this one.');
2889
+ return true;
2890
+ }
2891
+ async executeDrag(page, _element, action, elementBox) {
2892
+ this.actedOnBoard = true;
2893
+ const center = (bbox) => [elementBox.x + ((bbox[0] + bbox[2]) / 2) * elementBox.width, elementBox.y + ((bbox[1] + bbox[3]) / 2) * elementBox.height];
2894
+ await this.gesture(() => this.human.drag(page, center(action.source_bounding_box), center(action.target_bounding_box)));
2513
2895
  }
2514
2896
  async findControl(scope, selectors) {
2515
2897
  const [first] = await visible(scope, selectors);
@@ -2521,10 +2903,10 @@ class CaptchaKrakenSolver {
2521
2903
  * BotDetect, whose 280x50 `.BDC_CaptchaDiv` holds only the image while `#captchaCode` sits in a sibling div.
2522
2904
  */
2523
2905
  async answerBox(scope, at) {
2524
- const inside = await this.findControl(scope, selectors_1.TEXT_INPUT_SELECTORS);
2906
+ const inside = await this.findControl(scope, this.sel.textInput);
2525
2907
  if (inside !== null || !at)
2526
2908
  return inside;
2527
- const around = await Promise.all(['ancestor::fieldset[1]', 'ancestor::form[1]'].map((axis) => this.findControl(at.locator(`xpath=${axis}`), selectors_1.TEXT_INPUT_VENDOR_SELECTORS)));
2909
+ const around = await Promise.all(['ancestor::fieldset[1]', 'ancestor::form[1]'].map((axis) => this.findControl(at.locator(`xpath=${axis}`), this.sel.textInputVendor)));
2528
2910
  return around.find((h) => h !== null) ?? null;
2529
2911
  }
2530
2912
  async executeType(page, scope, action, at) {
@@ -2543,101 +2925,86 @@ class CaptchaKrakenSolver {
2543
2925
  console.log(`Typed ${text.length} character(s) into the captcha field.`);
2544
2926
  return true;
2545
2927
  }
2546
- async trackPiece(element, beforePath, afterPath, exclude, travel = 0) {
2547
- try {
2548
- await this.shot(element, afterPath, this.config.elementScreenshotTimeoutMs ?? 8000);
2549
- const res = await this.runCvTool('track-piece', { before: beforePath, after: afterPath, exclude, travel }, ['track-piece', beforePath, afterPath, JSON.stringify(exclude), String(travel)]);
2550
- return res && res.bbox ? { bbox: res.bbox, piece: res.piece ?? null } : null;
2551
- }
2552
- catch (e) {
2553
- console.warn('track-piece failed:', e);
2928
+ /** One look of the slide loop (`slide_tracker.py` behind the CV worker); null when the part cannot be followed. */
2929
+ async slideStep(request) {
2930
+ const res = await this.runCvTool('slide-step', request, ['slide-step', JSON.stringify(request)]);
2931
+ // A reply's `error` is the distance still to go; a failure is `{error: why}` with no position.
2932
+ if (!res?.pos) {
2933
+ console.warn(`[slide] ${res?.error ?? 'the tracker gave no answer'}; released where the handle is`);
2554
2934
  return null;
2555
2935
  }
2936
+ return res;
2556
2937
  }
2557
2938
  shotScale(shot, cssWidth) {
2558
2939
  const dims = readPngDimensions(shot);
2559
2940
  return dims && cssWidth > 0 ? dims.width / cssWidth : 1;
2560
2941
  }
2561
- /** Aim the handle at the slot once, then look and correct until the piece is home. Release is the submit. */
2942
+ /**
2943
+ * Press the handle, nudge it to see what it moves, then steer that part's point onto its target. Release is the submit.
2944
+ * A generation-3 answer names all three points, so nothing is read from the page but pixels. A generation-2 answer
2945
+ * names only the target: its handle comes from the selector table and the part it moves is whatever moved most.
2946
+ * That path goes when the last generation-2 model does.
2947
+ */
2562
2948
  async executeSlide(page, element, scope, action, elementBox) {
2563
2949
  this.actedOnBoard = true;
2564
- const tb = action.target_bounding_box;
2565
- const targetX = ((tb[0] + tb[2]) / 2) * elementBox.width;
2566
- const handle = await this.findControl(scope, selectors_1.SLIDER_HANDLE_SELECTORS);
2567
- if (!handle) {
2568
- const piece = await this.findControl(scope, selectors_1.PIECE_SELECTORS);
2569
- const box = piece ? await piece.boundingBox() : null;
2950
+ const { width: w, height: h } = elementBox;
2951
+ const at = (b) => [((b[0] + b[2]) / 2) * w, ((b[1] + b[3]) / 2) * h];
2952
+ const goal = at(action.target_bounding_box);
2953
+ let handle;
2954
+ let seed;
2955
+ if (action.action === kinds_1.ActionKind.SLIDE) {
2956
+ handle = at(action.handle_bounding_box);
2957
+ seed = at(action.source_bounding_box);
2958
+ }
2959
+ else {
2960
+ const slider = await this.findControl(scope, this.sel.sliderHandle);
2961
+ const piece = slider ? null : await this.findControl(scope, this.sel.piece);
2962
+ const box = await (slider ?? piece)?.boundingBox().catch(() => null);
2570
2963
  if (!box) {
2571
2964
  console.warn('Slide action, but the widget has neither a slider nor a draggable piece.');
2572
2965
  return false;
2573
2966
  }
2574
- const targetY = ((tb[1] + tb[3]) / 2) * elementBox.height;
2575
- console.log('No slider track; dragging the piece to the slot directly.');
2576
- await this.gesture(() => this.human.drag(page, [box.x + box.width / 2, box.y + box.height / 2], [elementBox.x + targetX, elementBox.y + targetY]));
2577
- return true;
2578
- }
2579
- const hbox = await handle.boundingBox();
2580
- if (!hbox)
2581
- return false;
2582
- const startX = hbox.x + hbox.width / 2;
2583
- const holdY = hbox.y + hbox.height / 2;
2584
- // Mask from the handle's band to the widget's bottom, not the handle's bottom: the filled track moves too, and
2585
- // masked to the band alone the loop once derived a 135.4px piece on Tencent's track against 42.0px masked to the bottom.
2586
- const pad = Math.max(4, hbox.height * 0.35);
2587
- const band = [0, hbox.y - elementBox.y - pad, elementBox.width, elementBox.height];
2588
- const shots = [tmp('slide_0'), tmp('slide_1')];
2967
+ handle = [box.x + box.width / 2 - elementBox.x, box.y + box.height / 2 - elementBox.y];
2968
+ // A piece with no track is the handle itself, and what it moves is it.
2969
+ seed = piece ? handle : null;
2970
+ }
2971
+ const press = [elementBox.x + handle[0], elementBox.y + handle[1]];
2972
+ const probe = goal[0] >= (seed ?? handle)[0] ? SLIDE_PROBE_PX : -SLIDE_PROBE_PX;
2973
+ const timeout = this.config.elementScreenshotTimeoutMs ?? 8000;
2974
+ const [first, probed, current] = [tmp('slide_0'), tmp('slide_1'), tmp('slide_2')];
2589
2975
  try {
2590
- await this.move(page, handle, { paddingPercentage: 30 });
2976
+ await this.performSmoothMove(page, ...press);
2591
2977
  await this.human.press(page);
2592
2978
  await this.human.pause(kinds_1.PauseKind.GRAB);
2593
- await this.shot(element, shots[0], this.config.elementScreenshotTimeoutMs ?? 8000);
2594
- const scale = this.shotScale(shots[0], elementBox.width);
2595
- const exclude = band.map((v) => v * scale);
2596
- const liveCentre = async () => {
2597
- const b = await this.measurePieceBox(scope, elementBox.width);
2598
- return b ? b.x + b.width / 2 - elementBox.x : null;
2979
+ await this.shot(element, first, timeout);
2980
+ const k = this.shotScale(first, w);
2981
+ await this.performSmoothMove(page, press[0] + probe, press[1]);
2982
+ await this.human.pause(kinds_1.PauseKind.PROBE);
2983
+ await this.shot(element, probed, timeout);
2984
+ const ask = {
2985
+ first, probe: probed, probe_s: probe * k, goal: [goal[0] * k, goal[1] * k], seed: seed && [seed[0] * k, seed[1] * k],
2986
+ handle: [handle[0] * k, handle[1] * k], bounds: [-handle[0] * k, (w - handle[0]) * k], tol: SLIDE_TOLERANCE_PX * k,
2599
2987
  };
2600
- const restCentre = (await liveCentre()) ?? (startX - elementBox.x);
2601
- let offset = targetX - restCentre;
2602
- await this.performSmoothMove(page, startX + offset, holdY);
2603
- const widths = [];
2604
- let lastBox = null;
2605
- let lastPiece = null;
2606
- let lastCentre = null;
2607
- let ratio = 1;
2608
- let i = 0;
2609
- for (; i < SLIDE_MAX_CORRECTIONS; i++) {
2610
- await this.human.pause(kinds_1.PauseKind.PROBE);
2611
- const seen = await this.trackPiece(element, shots[0], shots[1], exclude, offset * ratio * scale);
2612
- if (seen) {
2613
- widths.push([offset, (seen.bbox[2] - seen.bbox[0]) / scale]);
2614
- lastBox = seen.bbox;
2615
- lastPiece = seen.piece;
2616
- }
2617
- const solved = (0, slide_geometry_1.solveSlideGeometry)(widths, elementBox.width);
2618
- ratio = solved.ratio;
2619
- const live = await liveCentre();
2620
- const pieceCentre = live !== null ? live
2621
- : lastPiece !== null ? lastPiece.centre / scale
2622
- : (lastBox && solved.pieceWidth !== null ? lastBox[2] / scale - solved.pieceWidth / 2 : null);
2623
- if (pieceCentre === null)
2624
- continue;
2625
- lastCentre = pieceCentre;
2626
- const error = targetX - pieceCentre;
2627
- console.log(`[slide] ${live !== null ? 'the piece element' : lastPiece !== null ? 'the pixel diff' : "the diff's union"} puts it at ${Math.round(pieceCentre)}px, want ${Math.round(targetX)}px, ratio ${ratio.toFixed(2)}`);
2628
- if (Math.abs(error) <= SLIDE_TOLERANCE_PX)
2988
+ let look = probed;
2989
+ let s = probe * k;
2990
+ const history = [];
2991
+ for (let moves = 0;; moves++) {
2992
+ const reply = await this.slideStep({ ...ask, current: look, s, history });
2993
+ if (!reply)
2629
2994
  break;
2630
- offset += error / ratio;
2631
- await this.performSmoothMove(page, startX + offset, holdY);
2632
- }
2633
- if (lastCentre === null) {
2634
- console.warn('Slider: the piece never resolved on screen; released where the opening sweep put it.');
2635
- }
2636
- else if (i >= SLIDE_MAX_CORRECTIONS) {
2637
- const settled = await liveCentre();
2638
- const left = targetX - (settled !== null ? settled : lastCentre);
2639
- if (Math.abs(left) > SLIDE_TOLERANCE_PX)
2640
- console.warn(`[slide] out of corrections with ${left >= 0 ? '+' : ''}${left.toFixed(1)}px still to go — releasing off-target`);
2995
+ if (look !== probed)
2996
+ history.push(reply.observation);
2997
+ if (reply.done || moves >= SLIDE_MAX_CORRECTIONS) {
2998
+ if (reply.error > ask.tol)
2999
+ console.warn(`[slide] releasing ${(reply.error / k).toFixed(1)}px off target`);
3000
+ break;
3001
+ }
3002
+ s = reply.next;
3003
+ const [dx, dy] = Array.isArray(s) ? s : [s, 0];
3004
+ await this.performSmoothMove(page, press[0] + dx / k, press[1] + dy / k);
3005
+ await this.human.pause(kinds_1.PauseKind.PROBE);
3006
+ await this.shot(element, current, timeout);
3007
+ look = current;
2641
3008
  }
2642
3009
  await this.human.pause(kinds_1.PauseKind.SETTLE);
2643
3010
  }
@@ -2646,9 +3013,8 @@ class CaptchaKrakenSolver {
2646
3013
  await this.human.release(page);
2647
3014
  }
2648
3015
  catch { /* the page may have navigated */ }
2649
- this.lastInputAt = Date.now();
2650
- for (const s of shots)
2651
- unlink(s);
3016
+ for (const f of [first, probed, current])
3017
+ unlink(f);
2652
3018
  }
2653
3019
  return true;
2654
3020
  }