feat(mcp): attach to a test held at screen.pause() or --step

Merged
SA
5d ago
+3618-340

Stacked on #2003, which moves the session tools out of mcp/.

A test held at screen.pause() or at an e2e run --step hold now prints an ID, and you can attach e2e mcp to it. A coding agent can then look at the app and act on it while the test waits.

  • The pause header shows the ID: Paused (k3f9) in pays with a card [web]:. In a terminal, the ID is on the test's row. It's random, and it stays the same for every hold of that attempt.
  • open_session {attach: "k3f9"} opens a session on the held test's app. It has the same tools as a normal session on that target, and they work the same way: the grammar verbs, run_code, type_secret, screenshot, the project's tools, and recording. A test compares the two catalogs.
  • close_session detaches and leaves the test held. When you resume the test, the session ends right away and a call still in progress is cancelled.
  • Attached calls aren't recorded as steps in the report or the trace.
  • start_recording works when the run doesn't record the test. When it does (--video), it fails with the directory of the run's video, since an engine session holds one recording. A recording still running when the session ends is saved.
  • An unknown ID is answered with the held tests and their IDs.
  • A reporter's onStepPrompt gets the ID as attachId on each held step. That's the only new public field. The function that attaches to a held test isn't reachable from a reporter.
  • e2e run --step in a terminal now listens on the directory's step socket like a pause does, so an agent can attach at its holds. e2e run --step next from another shell resumes it as well.
  • Attached sessions are counted in usage telemetry through the existing session event, with an attached field.
open_session {attach: "k3f9"}
call {tool: "observe", session: "<id>"}
call {tool: "run_code", args: {code: "await screen.getByRole('link', 'Todos').tap();"}, session: "<id>"}
close_session {session: "<id>"}

How it works

The tools run in the worker, on the engine session the held attempt already has. The MCP server only forwards requests.

sequenceDiagram
  participant A as Coding agent
  participant M as e2e mcp
  participant R as e2e run (runner)
  participant W as Worker (held attempt)
  W->>R: step-hold
  R-->>A: prints "Paused (k3f9) ..."
  A->>M: open_session {attach: "k3f9"}
  M->>R: hello, attach (step socket)
  R->>W: step-attachment-request
  W-->>M: catalog and first screen, redacted
  A->>M: call {tool: "tap"}
  M->>R: call
  R->>W: step-attachment-request
  W-->>M: result, redacted
  Note over R,W: Enter or "--step next" ends the attachment
  • The runner gives each held attempt an ID and relays requests to its worker (StepAttachmentRelay, called from packages/e2e/src/run/scheduler.ts).
  • The worker builds the session catalog over the held attempt in packages/e2e/src/run/worker/held-test-tools.ts, with createStepExecution from #2003 and an empty step recorder, so the calls stay out of the test's steps. Every reply is redacted with the attempt's secret ledger before it's sent.
  • An attachment uses one connection on the existing step socket (packages/e2e/src/cli/step-control.ts). The messages are in packages/e2e/src/run/step-attachment-wire.ts. The first message carries a protocol version, and a mismatch is answered with both e2e versions.
  • With several workers paused, each prompt is now drawn from the hold it names (packages/e2e/src/cli/step.ts). Before, a prompt could show one test's title next to another test's screen.

Limits

  • App logs and screen updates aren't recorded on the test while an attached call runs. Late effects of a call can show up in the trace after it returns or is cancelled by a resume. Browser callbacks don't say which side caused them.
  • If the agent changes the app, the test resumes on the screen the agent left. A step that fails because of that is expected.
  • A directory has one step socket, owned by the first run that pauses there. A second run paused in the same directory can't be attached to. Its ID is answered with the held tests of the run that owns the socket.
  • If you resume while the agent is recording, the test continues right away. The video is saved in the background, and its path is in the MCP server's log.
  • e2e mcp and e2e run must start in the same directory.
  • There's no idle timeout for an attached session. It ends when the agent closes it, the test resumes, or the MCP server exits.

Verified

Ran it locally: yes

  • Testbed, the built CLI and a real e2e mcp over stdio. A stepped run holds the smoke test, and an MCP client attaches, runs a statement, saves a screenshot, records a video, and the test is resumed from a shell.
  • The attached video shows a coding agent doing the same from a prompt, against a headed run paused at screen.pause(). It was recorded before the IDs became random, so it shows p1.
$ e2e run tests/smoke.e2e.ts --grep "playground renders and navigates" --step   # no terminal
Paused (7s46) before step 2 of playground renders and navigates [web]:
  expect.toHaveTitle "Playground" (tests/smoke.e2e.ts:6)

> open_session {attach: "zzzz"}
NO_SESSION: no held test matches that attachment id
Held tests:
7s46  playground renders and navigates [web]

> open_session {attach: "7s46"}
Session <id> open on target "web". Attached to 7s46, "playground renders and navigates" on target "web".
Credentials: "admin" (username "admin", password secret "admin.password"). ...

> tools
Session <id> on target "web": 32 tools. Run one with call {tool, args}. Use tools {tool} to see a tool's arguments.

> call {tool: "run_code"}
The statement passed.
1. passed: await expect(screen.getByRole("heading", "Playground")).toBeVisible();

> call {tool: "screenshot", args: {path: "attach-shot.png"}}
Screenshot attached: 768 by 432 pixels (0.6 per CSS pixel).        # attach-shot.png is on disk

> call {tool: "start_recording"}
Recording 1 started. Act as usual; stop_recording saves the video, and close_session saves one still running.

> call {tool: "stop_recording"}
Recording 1 stopped after 2.2 s.
- apps/testbed/.e2e/videos/<id>/1.webm                              # the file is on disk

$ e2e run --step next
āœ“ expect.toHaveTitle "Playground" 1ms
Paused (7s46) before step 3 of playground renders and navigates [web]:

> call {tool: "observe"}   # after the resume
NO_SESSION: session "<id>" ended: the test resumed; call open_session for a new one

$ e2e run --step rest
      Tests  1 passed (1)
The stepped run has ended.
  • After the run, .e2e/report.json holds the test's 9 steps and none from the attached session.

https://github.com/user-attachments/assets/1582b103-4747-42ab-8a89-eada124935f6