v5: record per-step code from the @wdio/session agent history and drop MCP's per-action templates

Aperta
#214 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
30/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
nodejs, typescript
Ambito
testing, tooling

Direzione di ricerca

The helper goes in src/session/agent.ts beside runActionWithChanges (line 75); read that together with #197's run_code history logic and src/recording/step-recorder.ts, which stores step.code. Model the new test on tests/tools/run-code.test.ts (real agent, stub browser) and use set_geolocation on classic Chromium as the regression. Then remove the routed-tool cases from generateStep in src/recording/code-generator.ts (lines 292-311) and confirm nothing reaches the navigateAction/tapAction fallbacks before deleting them. Done means npm test, tsc --noEmit and npm run lint are green.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Target: feature/v5 (checked at b2b1bf2).
Depends on: #197 (run_code), which already reads exec code from the agent history. Upstream, not blocking: webdriverio/webdriverio#16493 (exec through the agent API), the S5 exporter draft and S6 (frame code is TypeScript) in docs/architecture/v5-upstream-issue-drafts.md.

Problem

docs/architecture/v5-proposal.md:224 plans to "reduce src/recording/code-generator.ts to what session cannot generate; per-action code comes from the agent". On feature/v5 this is half done, and the code comes from two sources.

  • Agent-routed tools record AgentActionResult.code: runActionWithChanges calls recordCode(result.code) (src/session/agent.ts:75), withRecording stores it as step.code (src/recording/step-recorder.ts:11, :52), and generateStep prints step.code before it looks at any per-tool case (src/recording/code-generator.ts:145).
  • run_code (#197) records the history entry instead: it remembers agent.history.at(-1)?.n before the call and records the first exec entry with a higher n.

result.code is the code shown to the agent. The history entry (HistoryEntry { n, time, kind, code, action?, args?, path? }, node_modules/@wdio/session/build/types.d.ts:165-175) is what session's own exporter replays. The two are not always the same. In @wdio/session 10.2.0 (node_modules/@wdio/session/build/agent.js, the bundle behind @wdio/session/agent):

  • geolocation on a classic Chromium session returns only the Emulation.setGeolocationOverride call as code. The history entry adds the preload and execute scripts, so the location survives a reload (agent.js:6050-6072). MCP's generated script drops them today.
  • exec returns no code, only a history entry (agent.js:4303-4308; refs are rewritten to selectors by rewriteRefs, agent.js:4221, build/index.js:7104). This is why #197 had to read the history.
  • Read actions (get, is) return code and append no history entry (agent.js:4326-4395). MCP routes none of them today. If one is routed later, the current wiring would record a line that reads but does not act.
  • Session appends an entry only when the action's history is non-empty (agent.js:8181-8189), and strips history from the result (:8191).

The per-tool templates that duplicate agent code are still in generateStep:

  • click_element (code-generator.ts:294) and set_value (:297): without an agent these tools return noSession(), which is recorded as an error step, so these cases are never reached.
  • navigate (:292) and the selector branch of tap_element (:311): reached only through the no-agent fallbacks navigateAction and tapAction. agentFor() returns undefined only when the active session has no metadata, where getBrowser() throws anyway (src/session/state.ts:35-41). Unverified whether any path reaches them.
  • switch_frame (:301): reached when the agent returns empty code, which happens for top/parent on BiDi when there is no current page (agent.js:1824, :1845).
  • isSnapshotRef/unreplayableRef (:125-134): routed tools never hit this, because the agent resolves a ref to a stable selector before it emits code (resolveTarget, agent.js:1108-1131).
Which tools go through the agent (feature/v5)

Through agentFor + runAction/runActionWithChanges, so their code comes from the agent:

MCP tool Agent action
click_element click
set_value fill
select_option select
press_key press
perform_actions click, fill, select, check, uncheck, press (src/tools/page-input.tool.ts:41-46)
navigate navigate
switch_frame frame
set_cookie, delete_cookies cookies (set/clear)
set_geolocation geolocation, web only and only without altitude (src/tools/device.tool.ts:76-80)
tap_element tap, selector form only (src/tools/gestures.tool.ts:32-34)
snapshot snapshot / find (read-only, not instrumented)
run_code (#197) exec via runAction

They call WebdriverIO directly through getBrowser(), so MCP has to write their code itself:

  • execute_script (uses agent.scope as the browser, but calls execute itself; src/tools/execute-script.tool.ts:35-36)
  • scroll, swipe, drag_and_drop, coordinate tap_element
  • set_geolocation on mobile or with altitude
  • switch_context, rotate_device, hide_keyboard
  • switch_tab, emulate_device
  • mock, get_mock_calls, manage_mock
  • execute_electron_script, trigger_electron_deeplink
  • open_web_extension, launch_chrome, get_elements
  • start_session / attach_session (the agent is used only for the page report)

Proposal

  1. Read the history in one place. Add a helper in src/session/agent.ts that takes n before the action, and after a successful action records the code of every entry whose n is higher. Use every matching entry, not find: one call can append more than one. runActionWithChanges and run_code both go through it. Drop recordCode(result.code). A failed action appends nothing, so error steps are unchanged.

  2. Remove the per-tool templates for routed tools: click_element, set_value, the selector branch of tap_element, and navigate together with the navigateAction/tapAction no-agent fallbacks once it is confirmed that nothing reaches them. Replace the switch_frame case with one generic line for a successful routed step that recorded no code (e.g. // switch_frame: no code recorded), so no tool needs its own case for that.

  3. code-generator.ts keeps what session cannot produce:

    • the session-start / remote() preamble, including cloud (BrowserStack, Sauce Labs, TestMu, TestingBot, Digital.ai), Appium and Electron
    • the attach file shape
    • teardown and cloud status marking
    • error-step comments
    • the __session_transition__ sentinel
    • the $/$$ shim (:137, :423-425)
    • the cases for tools that do not go through the agent (list above)

    Shrinking it further depends on the upstream exporter (S5).

The line numbers cited in v5-proposal.md:224 refer to main at 982d6e7 (710 lines). On feature/v5 the file has 732 lines, so those citations need updating when this lands.

Gaps

These are agent actions the routed tools use whose history entry has no code, or has code that does not replay:

  • snapshot, find: no entry. They only read, so this is expected.
  • frame on BiDi:
    • top/parent with no current page produce no entry (agent.js:1824, :1845).
    • The page lookup carries a TypeScript non-null assertion ! (agent.js:1534), so a generated script with a frame switch does not parse as JavaScript. This is upstream S6.
  • Ref with no stable selector: the agent emits ref("eN") /* no selector matches this element, this step only replays in this session */ (agent.js:1124). That does not run in a standalone script, and session's own generateSpec rejects any ref( (agent.js:6726-6731). MCP should comment out such a step, not emit it.
  • exec: no entry when the code is read-only or history: false (agent.js:4303). #197 already covers this.
  • cookies get/list: no code. MCP does not route these.

Every other action in scope does write history code in 10.2.0:

  • click, including coordinates and new tab (:2795, :2829, :2833)
  • fill (:3046-3095), type (:3335, :3341), press (:3378), select (:3401-3403), check/uncheck (:3495)
  • navigate (:2760)
  • cookies set/clear (:6416, :6421)
  • geolocation (:6049, :6064, :6074)
  • tap (:2875), swipe (:3541), long-press (:3547), drag (:3508), scroll (:3516-3525)

Out of scope here, noted so they are not lost:

  • Direct tools that are recorded but have no generator case print // [unknown tool] <name>: switch_context, rotate_device, hide_keyboard.
  • switch_tab and emulate_device are not wrapped in instrument (src/server.ts:166, :169), so they are missing from generated scripts entirely.
  • Session has code-emitting actions for all five (contexts, rotate, keyboard, tabs, emulate). Routing them is a separate change.
  • swipe and drag with a duration, coordinate tap and offset drag, and MCP's scroll (window.scrollBy vs session's wheel browser.scroll) have no agent equivalent and stay direct (v5-proposal.md:181-187).

Acceptance

  • For every agent-routed tool, a step's recorded code equals the joined code of the history entries appended during that call. A test uses the real agent against a stub browser, as tests/tools/run-code.test.ts does in #197.
  • A regression test: set_geolocation on a classic Chromium session records the preload and execute lines.
  • runActionWithChanges and run_code record through the same helper, and nothing reads result.code for recording.
  • generateStep has no case for click_element, set_value or the selector form of tap_element, and none for navigate once its fallback is gone. Routed tools leave no per-action template behind.
  • A step whose code contains ref( is emitted as a comment, not as code.
  • npm test, ./node_modules/.bin/tsc --noEmit and npm run lint are green.
Lingua principale
TypeScript
Stelle
45
Fork
20
Merge medio
2g 9h
PR unite (30g)
16

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di webdriverio/mcp

Tutte le issue di webdriverio/mcp

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.