Skip to content

Optimize WebKit navigation and evaluation hot paths #702

@shaun0927

Description

@shaun0927

Tracked by #713.

Background

OpenSafari’s core work is WebKit Remote Debugging Protocol automation. Navigation, screenshot, DOM inspection, and evaluate are frequent paths, so reducing protocol round trips improves real user speed.

Objective

Add fast value-return evaluation and reduce redundant WebKit RPCs in navigation and screenshot flows.

Scope

  • Add a value-oriented evaluate helper for non-promise primitive/object results.
  • Batch post-navigation state reads.
  • Cache enabled protocol domains per target.
  • Reduce fixed sleeps where event or state checks are sufficient.

Target files and paths

  • src/webkit/client.ts
  • new src/webkit/evaluate.ts or extracted command module if refactoring begins
  • tests/unit/webkit*.test.ts or new tests for evaluation/navigation helpers

Implementation steps

  1. Introduce evaluateValue<T> or an equivalent option using returnByValue: true.
  2. Update screenshot viewport query to use the fast path.
  3. Combine final URL, readyState, and status reads into one evaluation.
  4. Track enabled domains to avoid duplicate Page.enable and Network.enable calls.
  5. Add tests using mocked send calls to assert lower RPC count.

Acceptance criteria

  • Object-returning simple evaluations can complete in one protocol call.
  • Navigation final state is read with one evaluation call.
  • Repeated navigation does not re-enable already enabled domains for the same target.
  • Existing public evaluate behavior remains compatible.

Verification

  • npm run lint -- --quiet
  • Targeted WebKit client unit tests
  • Manual smoke on macOS simulator: navigate, screenshot, read_page still work.

Dependencies / recommended order

Can run after proxy readiness work or independently if tests use mocks. Prefer before large WebKitClient refactor so performance behavior is captured by tests.

Risks and cautions

  • Do not break promise-returning evaluation semantics.
  • Do not use Chrome CDP APIs; preserve WebKit protocol constraints from CLAUDE.md.
  • Do not remove existing error handling paths.

Non-goals

  • Do not rewrite all WebKitClient responsibilities in this issue.
  • Do not introduce Playwright/CDP abstractions.
  • Do not change screenshot protocol from Page.snapshotRect.

Ambiguity reducers

  • “Fast path” applies only when callers expect serializable values.
  • The existing generic evaluate remains available for promise/objectId cases.
  • RPC count assertions should focus on specific helper calls, not entire workflows with simulator variability.

Validity review

Proceed. It is central to the automation product because most user-visible actions pass through WebKit RPCs.

Implementation detail refinements

  • Add helper first, then migrate one or two hot paths per PR.
  • Prefer typed return helpers over adding more any casts.
  • Use WebKit event support only after confirming protocol availability in tests or live smoke.

Verification checklist reinforcement

  • Screenshot viewport query uses value fast path.
  • Navigation final state uses one combined evaluation.
  • Tests prove public API output shape is unchanged.
  • No CDP-only APIs are introduced.

Metadata

Metadata

Assignees

No one assigned

    Labels

    automation-roadmapOpenSafari automation roadmap work itemsperformancePerformance optimization

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions