Skip to content

perf: smart-wait — waitForCapture, wait({ selector }), daemon backoff#527

Merged
jackwener merged 16 commits intomainfrom
codex/release-1.5.2
Mar 27, 2026
Merged

perf: smart-wait — waitForCapture, wait({ selector }), daemon backoff#527
jackwener merged 16 commits intomainfrom
codex/release-1.5.2

Conversation

@jackwener
Copy link
Copy Markdown
Owner

Summary

Three-layer performance optimization eliminating unnecessary fixed sleeps:

  • Layer 1 – waitForCapture(): New IPage method that polls window.__opencli_xhr every 100ms and resolves as soon as the first network capture arrives (instead of blind wait(N)). Fixes a correctness bug where DOM-stable detection would resolve before the API response arrived. Replaces wait(N) in 7 INTERCEPT adapters (36kr, twitter/followers, twitter/following, twitter/notifications, producthunt) and in stepIntercept pipeline step.

  • Layer 2 – wait({ selector }): Extends WaitOptions with selector?: string and adds waitForSelectorJs() (polls querySelector every 100ms). Replaces blind wait(5) React hydration waits in 15 Twitter UI adapters and 8 calls across medium/substack/bloomberg/sinablog.

  • Layer 3 – daemon exponential backoff: Replaces fixed 300ms poll in _ensureDaemon() with [50, 100, 200, 400, 800, 1500, 3000] backoff — a connected extension is detected ~250ms faster on average.

Changes

File Change
src/browser/dom-helpers.ts Add waitForCaptureJs(), waitForSelectorJs()
src/types.ts Add selector? to WaitOptions, waitForCapture() to IPage
src/browser/page.ts Implement waitForCapture(), wait({ selector })
src/browser/cdp.ts Same as page.ts
src/pipeline/steps/intercept.ts Use installInterceptor + waitForCapture + getInterceptedRequests
7 INTERCEPT adapters wait(N)waitForCapture(N)
15 Twitter UI adapters wait(5)wait({ selector: '[data-testid="primaryColumn"]' })
medium/substack/bloomberg/sinablog wait(N)wait({ selector, timeout: N })
src/browser/mcp.ts Exponential backoff for daemon cold-start

Test plan

  • npx vitest run --project unit --project adapter — 91 files, 707 tests, all pass
  • npx tsc --noEmit — zero type errors
  • 7 new unit tests for waitForCaptureJs and waitForSelectorJs in dom-helpers.test.ts

Remove redundant command table (already in docs/adapters/index.md).
Keep badges, quick-start, and star history only.
- CDPPage.wait(N>=1) now uses waitForDomStableJs instead of fixed sleep,
  matching Page.wait() behavior and saving unnecessary idle time
- waitForSelectorJs switches from 100ms polling to MutationObserver,
  resolving instantly when the target element appears in the DOM
- Update dom-helpers tests to stub MutationObserver for Node eval context
@jackwener jackwener merged commit 70bd87b into main Mar 27, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant