@xkwdstore/agent 2.1.7 → 2.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,39 @@
1
1
  # Changelog — @xkwdstore/agent
2
2
 
3
+ ## 2.1.8 — a bounded navigation owns its budget (no self-cancelling guard, one retry)
4
+
5
+ ### Fixed
6
+ - **A slow main-frame document was cancelled by the agent's own stall guard.**
7
+ The context navigation guard painted its `NAVIGATION_STALLED` page after a fixed
8
+ 30 s budget, and that paint is a `page.goto(data:)` — it CANCELS the caller's
9
+ in-flight `page.goto`. A document that would have committed at 31 s (or was
10
+ already committed but still loading) was therefore thrown away and the session
11
+ was left on a diagnostic page, even though the proxy answered in ~250 ms on the
12
+ next attempt. The guard now defers to the caller's own bounded-navigation
13
+ budget while one is in flight, so exactly one component decides the outcome;
14
+ browser-initiated navigations with no caller are still contained exactly as
15
+ before.
16
+ - **A committed-but-slow document was reported as a failure.** `page.goto` can
17
+ reject on a late subresource while the document is committed, rendered and
18
+ interactive. `navigateBounded` now reports that as a success (`committedButSlow`)
19
+ and never replaces the working page with the diagnostic page.
20
+ - **One transport blip failed the whole navigation.** `navigateBounded` now
21
+ retries ONCE, after a short backoff, for the transport-transient classes only
22
+ (`NAVIGATION_STALLED`, `NAVIGATION_TIMEOUT`, `NAVIGATION_UNREACHABLE`,
23
+ `NAVIGATION_ABORTED`) when no usable document committed. Security classes —
24
+ TLS/certificate, policy/blocked, DNS pin, proxy tunnel/auth — are never
25
+ retried and still fail closed.
26
+
27
+ Measured on the affected machine against the production proxy: `CONNECT` and
28
+ plain-HTTP absolute-form requests answer in ~250 ms (60/60 and 40/40 clean), yet
29
+ a main-frame `http://google.com` document intermittently gets no response at all
30
+ for the whole budget — one such case in 18 production-shaped attempts, in the
31
+ same session that had painted `NAVIGATION_STALLED` on `http://google.com` 30 s
32
+ after `https://x.com/home` loaded. Every subsequent attempt — all six URL
33
+ variants, 20/20 repeats, headed and headless, on a copy of that exact profile —
34
+ loaded Google in ~1.5 s. No timeout, proxy, TLS, DNS-pin, QUIC or WebRTC setting
35
+ was relaxed to achieve this.
36
+
3
37
  ## 2.1.7 — authenticated proxies load Google properties (HTTP/1.1 pin)
4
38
 
5
39
  ### Fixed
package/README.md CHANGED
@@ -4,9 +4,9 @@ Terminal-only, cryptographically authenticated CLI for XKWDStore browser automat
4
4
 
5
5
  ## Installation
6
6
 
7
- > **@xkwdstore/agent 2.1.7 is the current published version on npm.**
7
+ > **@xkwdstore/agent 2.1.8 is the current published version on npm.**
8
8
  >
9
- > The current source version is **2.1.7** (browser connectivity: bounded
9
+ > The current source version is **2.1.8** (browser connectivity: bounded
10
10
  > navigation + capability-based proxy health, bounded authentication retries and
11
11
  > same-device recovery; an authenticated HTTP CONNECT proxy now pins HTTP/1.1 so
12
12
  > Google properties load instead of stalling on `about:blank`). See `reports/`
@@ -31,7 +31,7 @@ curl -L -o "%TEMP%\xkwdstore-install.ps1" https://raw.githubusercontent.com/Essa
31
31
  The generated launcher runs the pinned command:
32
32
 
33
33
  ```powershell
34
- npx --yes @xkwdstore/agent@2.1.7 start
34
+ npx --yes @xkwdstore/agent@2.1.8 start
35
35
  ```
36
36
 
37
37
  Automated installer tests use `-NoLaunch` or a mocked `npx` command so the
@@ -206,7 +206,7 @@ The agent does NOT update itself. To use a newer version, change the version
206
206
  number in the command. The pinned command for this release is:
207
207
 
208
208
  ```powershell
209
- npx --yes @xkwdstore/agent@2.1.7 start
209
+ npx --yes @xkwdstore/agent@2.1.8 start
210
210
  ```
211
211
 
212
212
  > 2.1.0 is the current local agent version (machine-wide hardening).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xkwdstore/agent",
3
- "version": "2.1.7",
3
+ "version": "2.1.8",
4
4
  "description": "XKWDStore Agent — machine-wide, cryptographically authenticated CLI for isolated browser automation",
5
5
  "main": "src/cli.js",
6
6
  "bin": {
@@ -26,7 +26,7 @@
26
26
  # to a worker in the active session. This script installs both halves.
27
27
  #
28
28
  # Options:
29
- # -AgentVersion <ver> pinned agent version (default 2.1.7)
29
+ # -AgentVersion <ver> pinned agent version (default 2.1.8)
30
30
  # -Server <url> server origin (default https://x.kwdstore.com)
31
31
  # -ServiceAccount <s> service identity (default LocalSystem)
32
32
  # -Uninstall remove service + startup (keeps ProgramData unless -PurgeData)
@@ -35,7 +35,7 @@
35
35
  # -NoLaunch (test) validate + write files, never start a process
36
36
 
37
37
  param(
38
- [string]$AgentVersion = '2.1.7',
38
+ [string]$AgentVersion = '2.1.8',
39
39
  [string]$Server = 'https://x.kwdstore.com',
40
40
  [string]$ServiceAccount = 'LocalSystem',
41
41
  [string]$AgentRoot,
@@ -456,10 +456,104 @@ function paintNavigationFailure(page, info = {}, opts = {}) {
456
456
  return work.finally(() => { try { _paintLocks.delete(page); } catch { /* ignore */ } });
457
457
  }
458
458
 
459
+ /**
460
+ * Failure codes that are TRANSPORT-transient and worth exactly ONE retry.
461
+ *
462
+ * Observed in production: a main-frame plain-HTTP document request through an
463
+ * authenticated proxy occasionally gets no response for the whole stall budget,
464
+ * while the very same proxy answers the next attempt in ~250 ms. That class is
465
+ * a blip in the egress, not a configuration defect — a single re-attempt heals
466
+ * it, and it must never be conflated with a hard failure (TLS, policy, DNS pin).
467
+ * `NAVIGATION_TIMEOUT` is included because a stall past the guard budget
468
+ * surfaces to the caller as a timeout.
469
+ */
470
+ const TRANSIENT_NAVIGATION_CODES = Object.freeze([
471
+ 'NAVIGATION_STALLED',
472
+ 'NAVIGATION_TIMEOUT',
473
+ 'NAVIGATION_UNREACHABLE',
474
+ // The guard's own containment paint CANCELS the pending document, which
475
+ // surfaces here as ERR_ABORTED. That is still a transport blip, not a user
476
+ // cancel, so it is retried like the others.
477
+ 'NAVIGATION_ABORTED',
478
+ ]);
479
+ /** Pause before the single transient re-attempt (lets a proxy slot/TLS state settle). */
480
+ const DEFAULT_RETRY_BACKOFF_MS = 750;
481
+
482
+ /**
483
+ * Pages with a bounded navigation in flight, and when that attempt's budget
484
+ * expires.
485
+ *
486
+ * The context-wide guard and `navigateBounded` watch the same document. The
487
+ * guard's stall paint REPLACES the page, which cancels the caller's `page.goto`
488
+ * and turns a recoverable request into an abort while the caller's own (longer)
489
+ * budget is untouched. While a bounded navigation is in flight the guard defers
490
+ * to the caller's budget, so exactly one component decides the outcome — and the
491
+ * single transient retry below can finish instead of being cancelled mid-flight.
492
+ *
493
+ * `page.goto` calls on one page are serialized by Playwright, so a single
494
+ * deadline per page is sufficient.
495
+ */
496
+ const _boundedNavInFlight = new WeakMap();
497
+ /** Grace after the caller's budget so the caller's own paint/retry wins the race. */
498
+ const BOUNDED_NAV_GUARD_GRACE_MS = 300;
499
+
500
+ function _boundedNavDeadline(page) {
501
+ if (!page || typeof page !== 'object') return 0;
502
+ const entry = _boundedNavInFlight.get(page);
503
+ return entry ? entry.deadline : 0;
504
+ }
505
+
506
+ function _boundedNavHold(page, timeoutMs) {
507
+ if (!page || typeof page !== 'object') return;
508
+ _boundedNavInFlight.set(page, { deadline: Date.now() + Math.max(0, timeoutMs) + BOUNDED_NAV_GUARD_GRACE_MS });
509
+ }
510
+
511
+ function _boundedNavRelease(page) {
512
+ if (!page || typeof page !== 'object') return;
513
+ _boundedNavInFlight.delete(page);
514
+ }
515
+
516
+ /**
517
+ * Did this attempt actually put a usable document in the tab?
518
+ *
519
+ * A `page.goto` rejection means the *navigation promise* failed — NOT that no
520
+ * page loaded. A slow subresource (or a late `load` event) can time out while
521
+ * the document is committed, rendered and interactive. Comparing the tab URL
522
+ * before and after the attempt is the only hang-free signal (`page.evaluate`
523
+ * can block on a wedged renderer, so it is deliberately not used):
524
+ *
525
+ * about:blank → https://www.google.com/ = committed, usable
526
+ * https://x.com/home → https://x.com/home = nothing happened, still stuck
527
+ *
528
+ * Reporting the first case as a failure — and then replacing that working page
529
+ * with a diagnostic page — destroys content the operator can use, so it is
530
+ * treated as a slow success instead.
531
+ *
532
+ * @returns {string|null} the committed http(s) URL, or null
533
+ */
534
+ function committedDocumentUrl(page, beforeUrl) {
535
+ if (!page || typeof page.url !== 'function') return null;
536
+ let current = '';
537
+ try { current = String(page.url() || ''); } catch { return null; }
538
+ if (!current) return null;
539
+ if (current === 'about:blank' || current.startsWith('about:blank#')) return null;
540
+ if (current.startsWith('chrome-error://')) return null;
541
+ if (!/^https?:\/\//i.test(current)) return null;
542
+ if (beforeUrl && current === beforeUrl) return null;
543
+ return current;
544
+ }
545
+
459
546
  /**
460
547
  * Bounded navigation. Never throws. On failure the diagnostic page is painted
461
548
  * (so the tab never stays on `about:blank`) and a typed result is returned.
462
549
  *
550
+ * Two behaviours make a transient proxy blip survivable without weakening any
551
+ * control:
552
+ * - a committed-but-slow document is reported as success (and never replaced
553
+ * by the diagnostic page);
554
+ * - a genuinely transient failure (no document, transport class) is retried
555
+ * ONCE before the diagnostic page is painted.
556
+ *
463
557
  * @param {object} page Playwright page
464
558
  * @param {string} url
465
559
  * @param {object} [opts]
@@ -468,47 +562,96 @@ function paintNavigationFailure(page, info = {}, opts = {}) {
468
562
  * @param {boolean} [opts.proxyActive]
469
563
  * @param {boolean} [opts.dnsPinned]
470
564
  * @param {boolean} [opts.paintOnFailure=true]
565
+ * @param {boolean} [opts.retryOnTransient=true] one retry for transport blips
566
+ * @param {number} [opts.retryBackoffMs]
471
567
  * @param {string} [opts.accountLabel]
472
568
  * @param {function} [opts.onFailure]
473
- * @returns {Promise<{ok:boolean, url?:string, status?:number, ms:number, code?:string, stage?:string, reason?:string, painted?:boolean}>}
569
+ * @returns {Promise<{ok:boolean, url?:string, status?:number, ms:number, code?:string, stage?:string, reason?:string, painted?:boolean, attempts?:number, retried?:boolean, committedButSlow?:boolean}>}
474
570
  */
475
571
  async function navigateBounded(page, url, opts = {}) {
476
572
  const timeoutMs = Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : DEFAULT_NAVIGATION_TIMEOUT_MS;
477
573
  const waitUntil = opts.waitUntil || 'domcontentloaded';
574
+ const retryOnTransient = opts.retryOnTransient !== false;
575
+ const backoffMs = Number.isFinite(opts.retryBackoffMs)
576
+ ? Math.max(0, opts.retryBackoffMs)
577
+ : DEFAULT_RETRY_BACKOFF_MS;
578
+ const MAX_ATTEMPTS = retryOnTransient ? 2 : 1;
478
579
  const t0 = Date.now();
580
+ let beforeUrl = '';
581
+ try { beforeUrl = String(page.url() || ''); } catch { beforeUrl = ''; }
582
+
583
+ let failure = null;
584
+ let attempts = 0;
479
585
  try {
480
- const response = await page.goto(url, { waitUntil, timeout: timeoutMs });
481
- return {
482
- ok: true,
483
- url: (() => { try { return page.url(); } catch { return url; } })(),
484
- status: response ? response.status() : null,
485
- ms: Date.now() - t0,
486
- };
487
- } catch (err) {
488
- const failure = classifyNavigationFailure(err, {
489
- url,
490
- proxyActive: opts.proxyActive === true,
491
- dnsPinned: opts.dnsPinned === true,
492
- });
493
- const paint = opts.paintOnFailure === false
494
- ? { painted: false }
495
- : await paintNavigationFailure(page, {
496
- ...failure,
497
- url,
498
- accountLabel: opts.accountLabel || null,
499
- }, { timeoutMs: Math.min(8000, timeoutMs) });
500
- const result = {
501
- ok: false,
502
- ms: Date.now() - t0,
503
- requestedUrl: url,
504
- ...failure,
505
- painted: paint.painted === true,
506
- };
507
- if (typeof opts.onFailure === 'function') {
508
- try { opts.onFailure(result); } catch { /* diagnostics must never break a session */ }
586
+ for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt += 1) {
587
+ attempts = attempt;
588
+ // Tell the context guard this attempt's budget is caller-owned, so it does
589
+ // not cancel our navigation with its own (shorter) containment paint.
590
+ _boundedNavHold(page, timeoutMs);
591
+ try {
592
+ const response = await page.goto(url, { waitUntil, timeout: timeoutMs });
593
+ const result = {
594
+ ok: true,
595
+ url: (() => { try { return page.url(); } catch { return url; } })(),
596
+ status: response ? response.status() : null,
597
+ ms: Date.now() - t0,
598
+ attempts,
599
+ };
600
+ if (attempts > 1) result.retried = true;
601
+ return result;
602
+ } catch (err) {
603
+ failure = classifyNavigationFailure(err, {
604
+ url,
605
+ proxyActive: opts.proxyActive === true,
606
+ dnsPinned: opts.dnsPinned === true,
607
+ });
608
+ // The navigation promise failed, but the document may be committed and
609
+ // usable. Never destroy it and never report a loaded page as a failure.
610
+ const committed = committedDocumentUrl(page, beforeUrl);
611
+ const loadOnly = failure.code === 'NAVIGATION_TIMEOUT'
612
+ || failure.code === 'NAVIGATION_STALLED'
613
+ || failure.code === 'NAVIGATION_ABORTED';
614
+ if (committed && loadOnly) {
615
+ return {
616
+ ok: true,
617
+ url: committed,
618
+ status: null,
619
+ ms: Date.now() - t0,
620
+ attempts,
621
+ retried: attempts > 1,
622
+ committedButSlow: true,
623
+ code: failure.code,
624
+ };
625
+ }
626
+ const transient = TRANSIENT_NAVIGATION_CODES.includes(failure.code);
627
+ if (!transient || attempt >= MAX_ATTEMPTS || committed) break;
628
+ if (backoffMs > 0) await new Promise((r) => setTimeout(r, backoffMs));
629
+ }
509
630
  }
510
- return result;
631
+ } finally {
632
+ _boundedNavRelease(page);
511
633
  }
634
+
635
+ const paint = opts.paintOnFailure === false
636
+ ? { painted: false }
637
+ : await paintNavigationFailure(page, {
638
+ ...failure,
639
+ url,
640
+ accountLabel: opts.accountLabel || null,
641
+ }, { timeoutMs: Math.min(8000, timeoutMs) });
642
+ const result = {
643
+ ok: false,
644
+ ms: Date.now() - t0,
645
+ requestedUrl: url,
646
+ ...failure,
647
+ painted: paint.painted === true,
648
+ attempts,
649
+ retried: attempts > 1,
650
+ };
651
+ if (typeof opts.onFailure === 'function') {
652
+ try { opts.onFailure(result); } catch { /* diagnostics must never break a session */ }
653
+ }
654
+ return result;
512
655
  }
513
656
 
514
657
  // ── Navigation guard (context-wide watchdog) ─────────────────────────────────
@@ -641,11 +784,23 @@ function installNavigationGuard(context, opts = {}) {
641
784
  const scheduleStall = (page, state) => {
642
785
  if (stopped || !state.pending) return;
643
786
  clearTracked(state.stallTimer);
787
+ // If a bounded navigation owns this page, wait for ITS budget instead of
788
+ // cancelling it: one decision-maker, and the caller can still retry.
789
+ const ownerDeadline = _boundedNavDeadline(page);
790
+ const waitMs = ownerDeadline > Date.now()
791
+ ? Math.max(250, ownerDeadline - Date.now()) + 250
792
+ : stallTimeoutMs;
644
793
  state.stallTimer = trackTimer(setTimeout(() => {
645
794
  const p = state.pending;
646
795
  if (stopped || !p || p.responded) return;
796
+ const stillOwned = _boundedNavDeadline(page);
797
+ if (stillOwned > Date.now()) {
798
+ // The caller's budget was extended (retry): keep deferring.
799
+ scheduleStall(page, state);
800
+ return;
801
+ }
647
802
  paintStall(page, state, p.url, Date.now() - p.startedAt).catch(() => { /* never throw */ });
648
- }, stallTimeoutMs));
803
+ }, waitMs));
649
804
  };
650
805
 
651
806
  /**
@@ -668,6 +823,12 @@ function installNavigationGuard(context, opts = {}) {
668
823
  if (closed) return;
669
824
  let url = '';
670
825
  try { url = page.url(); } catch { url = ''; }
826
+ if (_boundedNavDeadline(page) > Date.now()) {
827
+ // A bounded navigation still owns this page: it will paint its own
828
+ // (more specific) failure and may retry. Re-arm for later.
829
+ armCommitWatchdog(page, state);
830
+ return;
831
+ }
671
832
  if (url === '') {
672
833
  let target = (state.pending && state.pending.url) || state.unreachableUrl || null;
673
834
  // A late-attached pending navigation still has a history entry for the
@@ -954,6 +1115,8 @@ function installNavigationGuard(context, opts = {}) {
954
1115
  if (state.cdp) {
955
1116
  const p = state.pending;
956
1117
  if (!p || p.responded) continue;
1118
+ // A bounded navigation owns its budget; do not pre-empt it here either.
1119
+ if (_boundedNavDeadline(page) > Date.now()) continue;
957
1120
  const waited = Date.now() - p.startedAt;
958
1121
  if (waited < stallTimeoutMs) continue;
959
1122
  stalled += 1;
@@ -1439,7 +1602,8 @@ module.exports = {
1439
1602
  DEFAULT_STALL_TIMEOUT_MS,
1440
1603
  DEFAULT_HARD_STALL_TIMEOUT_MS,
1441
1604
  classifyNavigationFailure,
1442
- isStalledNavigation,
1605
+ isStalledNavigation,
1606
+ committedDocumentUrl,
1443
1607
  retryUrlFor,
1444
1608
  safeOrigin,
1445
1609
  sanitizeProxyForReport,
package/src/cli.js CHANGED
@@ -25,7 +25,7 @@ const crypto = require('crypto');
25
25
  const os = require('os');
26
26
  const path = require('path');
27
27
 
28
- const CLI_VERSION = '2.1.7';
28
+ const CLI_VERSION = '2.1.8';
29
29
  const PROTOCOL_VERSION = 2;
30
30
 
31
31
  // ── Command parsing ──────────────────────────────────────────────────────────
@@ -547,7 +547,7 @@ function status() {
547
547
  if (health) {
548
548
  const label = health.reauthorizationRequired
549
549
  ? (health.source === 'broker-log'
550
- ? 'AUTHENTICATION REJECTED (legacy broker is still retrying; upgrade/restart with 2.1.7)'
550
+ ? `AUTHENTICATION REJECTED (legacy broker is still retrying; upgrade/restart with ${CLI_VERSION})`
551
551
  : 'REAUTHORIZATION REQUIRED (automatic retries stopped)')
552
552
  : (health.state === 'connected' ? 'connected' : (health.state || 'unknown'));
553
553
  console.log(`Broker: ${label}`);