Skip to content

refactor: review phase 2 (split large files, exhaustive Route, page sources, MJPEG through the daemon) - #192

Merged
Derrors merged 5 commits into
mainfrom
refactor/review-phase2
Nov 25, 2026
Merged

Derrors merged 5 commits into
mainfrom
refactor/review-phase2

Conversation

@Derrors

@Derrors Derrors commented Nov 25, 2026

Copy link
Copy Markdown
Owner

What and why

Phase 2 of the architecture review of 0.2.14, five commits:

  1. refactor(mcp): split lib.rs (2,229 lines) into schema.rs (instructions, tool definitions), args.rs (checking, arguments to requests), batch.rs, result.rs (answers to results, input delivery), server.rs (McpServer, stdio and HTTP) and tests.rs. lib.rs re-exports them: the crate's interface is unchanged. A pure move: with whitespace ignored the modules are the old file; only pub(crate) was added where modules use each other's items.
  2. refactor(wda): split WdaClient's methods (a 900-line impl block) into client/{session,screen,actions,typing}.rs, tests into client/tests.rs. A pure move: each of the 68 methods is unchanged; private ones are pub(super) for the sibling modules.
  3. refactor(viewer): page sources in web/. viewer.html (3,689 lines) is now web/index.html with @include lines for viewer.css and twelve scripts in the order they run; build.rs assembles it into OUT_DIR and viewer.rs embeds that, no Node needed. The page built is byte for byte the old viewer.html. The page tests read it through tests/viewer/page.mjs.
  4. refactor(daemon): one exhaustive Route. Daemon::route(g) decides once between Wda(&mut client), Native, Hid (WDA down, iOS 27+ on WDA) and Unavailable (WDA down below iOS 27); fifteen dispatch sites match it in full, so a new action has to say what it does on each. Each arm does what the old code did (including the error kinds); require_wda is gone. Fallback chains (taps, swipes, typing, buttons and screenshots try WDA and go on over HID when it turns out unreachable) stay as they were, which the doc comment on Route says.
  5. feat(daemon): WDA's MJPEG picture through the daemon (Request::MjpegStream). On iOS 17–26 the viewer connected to the phone's port 9100 itself, the one place besides wda-bridge that bypassed the daemon (D2) and could only work over USB (docs/dev/wifi.md, second version). The viewer's MJPEG hub now asks the daemon, which checks the page's UDID against the controlled device (from the snapshot, no device lock), asks WDA for the stream and passes it on byte for byte until either side closes.

Decided against, with reasons:

  • A ControlBackend trait: the backends don't share one shape (the fallback chains above), so a trait would mean rewriting about 4,000 lines of tool code. The exhaustive Route covers the stated risk, a forgotten branch.
  • ES modules for the page: the scripts share many globals (S, T, W, D…), and inline modules can't import each other; it would be a rewrite of the front end.
  • Dropping the LocalSet: evaluated. The supervision tick is already Send, and the connection handler has one real gap. The rest is rustc's higher-ranked Send inference ("not general enough"), reaching into ictl-ax and ictl-device without locations, and no stutter was measured. Written down in docs/architecture.md.
  • Deduplicating dependency versions: the old ones (reqwest 0.12, thiserror 1, tungstenite 0.24…) come from vendored upstream crates (apple-codesign, plume_core, omnisette).

Tests

1. Unit tests

  • cargo test --workspace --all-features --locked: 551 passed; cargo clippy --workspace --all-targets --all-features --locked -- -D warnings; cargo fmt --check per package; node --test tests/viewer/*.test.mjs: 34 passed
  • New: the_route_without_wda_follows_the_ios_version_and_backend, the_embedded_page_is_assembled, the_phone_s_bytes_pass_through_until_the_page_goes, another_device_than_the_controlled_one_is_refused
  • No existing test removed, ignored or loosened (the WDA client tests' fixture paths gained a ../)
  • #[ignore] tests of the changed area: none run

2. Real-device regression

  • Devices, host: iPhone 15 Pro Max (iPhone16,2), iOS 27.2.1, native control; iPad mini 5 (iPad11,1), iPadOS 26.7.3, WDA; Debian 12
  • The device runs used a local branch merging the five commits; the tree of this PR is that tree exactly, apart from one line of docs/dev/regression-cases.md (release: 0.2.14 #183, filled in with the results below)
  • run.py: iPhone 3 rounds, 48 calls, 0 failed; iPad 5 rounds, 80 calls, 0 failed
  • scenarios.py: iPhone everything (tools typing delivery collect editing fresh batch menu idle url lock) 75/75; iPad everything but idle 60/60, lock 3 runs 39/39 (see below)
  • Compared with main: iPhone 17.73 s → 17.78 s total (main at 460f0c1, same hour); type_text flagged on medians of 3, but across four sets of runs today it ranges 2.76–4.55 s, main included. iPad 122.3 s → 115.0 s for 5 rounds (main at 25a0333, this morning); compare.py refused a step-by-step comparison because the Clock tabs picked differ, and per-tool medians are within noise
  • Longer run: about 1 h on both devices, a WDA hang (feat(viewer): the page's requests share a connection #103) recovered on its own, automatic unlocking on both

On the iPad, lock fails a lock now and then: the IOHID power press takes 5.4 s instead of 0.4 s and the screen stays on. It comes in stretches and on main too (also seen while testing #187–#191). Here this branch failed 3 locks in 4 runs at first, main passed 6 in the same stretch, then this branch passed 9 of 9. Nothing this PR changes runs differently in that scenario.

run.py output
this branch:
### iPhone16,2, iOS 27.2.1, 430x932 pt, native
--- 48 calls, 0 failed, 54.4s in all        (3 rounds)
### iPad11,1, iOS 26.7.3, 1024x768 pt, WDA
--- 80 calls, 0 failed, 115.0s in all       (5 rounds)

main:
### iPhone16,2, iOS 27.2.1, 430x932 pt, native
--- 48 calls, 0 failed, 62.0s in all        (3 rounds, 460f0c1; the first step includes a 10 s automatic unlock)
### iPad11,1, iOS 26.7.3, 1024x768 pt, WDA
--- 80 calls, 0 failed, 122.3s in all       (5 rounds, 25a0333)

3. Regression cases (docs/dev/regression-cases.md)

  • Device cases re-run through scenarios.py as above
  • Added release: 0.2.14 #183 (MJPEG through the daemon)

4. Real-device proof of the fix or feature

  • Device: iPad mini 5, iPadOS 26.7.3, WDA
  • Three GET /stream?udid=… at once: the viewer process had no connection to usbmuxd (before: one per phone stream), only one more to the daemon; the daemon had two more (the viewer, the phone's 9100); each stream got about 150 JPEG parts in 12 s; once they closed, the daemon's connection to the phone closed too. GET /video through the same source gave VAAPI-encoded H.264
  • Without ?udid= the viewer asks for the device it last knew (here the iPhone, selected when it started) and the daemon refuses: "the controlled device changed". Pages always send their device
  • Not applicable for commits 1–4: refactoring without a change in behaviour (the page byte-identical; the moves checked mechanically)
  • Untested: a macOS host

Checklist

  • MCP tool names, parameters and default outputs stay compatible (D10); Request::MjpegStream is new and only the viewer sends it
  • No new dependencies
  • CHANGELOG.md and CHANGELOG.zh-CN.md (Unreleased, Improvements) for the MJPEG change; the refactors aren't user-visible
  • No private data in the outputs above

🤖 Generated with Claude Code

Derrors and others added 5 commits November 25, 2026 12:50
ictl-mcp's lib.rs had grown to 2,200 lines: tool definitions, argument
checking, batch steps, results and the server in one file. It is now
schema.rs (instructions and tool definitions), args.rs (checking and
turning arguments into requests), batch.rs (iphone_batch steps),
result.rs (answers to tool results, input delivery), server.rs
(McpServer over stdio and HTTP) and tests.rs. lib.rs re-exports them,
so the crate's interface is unchanged.

A pure move: concatenated and with whitespace ignored the modules are
the old file; only `pub(crate)` was added where one module uses another's
items, and rustfmt dropped a few trailing commas in the tests.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
client.rs was 2,100 lines, most of it one 900-line impl block and its
tests. The methods now sit in client/ by what they do: session.rs (the
transport, the XCTest session, stall detection, idle-wait settings),
screen.rs (size, rotation, lock state, the element tree, screenshots,
alerts, the keyboard, the passcode field), actions.rs (gestures, buttons,
IOHID events, apps, links) and typing.rs (the focused field). The free
functions and types stay in client.rs; the tests move to client/tests.rs.

A pure move: each of the 68 methods is the same as before with
whitespace ignored; methods that were private are `pub(super)` so the
sibling modules can call them, and the tests' fixture paths gained a
`../` for the deeper directory.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
viewer.html was one 3,700-line file: 860 lines of CSS, the markup and a
2,560-line script. Its sources are now apps/iphone-control/web/:
index.html with `@include` lines for viewer.css and the scripts in the
order they run (js/i18n, ui, state, demo, screen, info, video, device,
tasks, alert, scope, layout). build.rs replaces each include with its
file and writes the page to OUT_DIR, which viewer.rs embeds as before;
no Node or other tool is needed to build.

The scripts are plain scripts in one scope, as the single file was, so
nothing about how the page runs changes: the page build.rs puts
together is byte for byte the old viewer.html. Turning them into ES
modules would mean giving every shared global (S, T, W, D…) an import,
a rewrite of its own; inline modules couldn't import each other anyway.

The page tests read the assembled page through tests/viewer/page.mjs,
which expands the includes the same way. A unit test checks that the
embedded page has every include expanded and stays strict.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
…ilable

Actions that have a way per backend were written as
`match self.wda(g) { Some(w) => …, None if native => …, None => … }`
or `if self.wda(g).is_none() && self.native(g) { … }` followed by
`self.require_wda(g)?`, in seven files. A new action could leave out a
branch without the compiler noticing.

`Daemon::route(g)` now decides once: `Route::Wda(&mut WdaClient)`,
`Native`, `Hid` (WDA down, iOS 27+ set to use WDA) or `Unavailable`
(WDA down below iOS 27), and the fifteen dispatch sites match it in full:
the element tree, the lock check of a read-only viewer, settling, alerts,
waiting for text, launching, terminating, opening links, unlocking, raw
screenshots, taps at a point, long presses, locking, status and the
display sample. Each arm does what the old code did, including the
error kind (`preflight` where it was); `require_wda` has no callers left
and is removed.

Fallback chains stay as they were: taps, swipes, typing, buttons and
screenshots try WDA first and go on over HID when it turns out
unreachable, which a route decided up front can't express. So do the
questions only WDA answers.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
…tream)

On iOS 17-26 the viewer connected to the phone's port 9100 itself
(Device::find + connect_port), the one place besides wda-bridge where
something other than the daemon reached for the phone (D2), and one that
could only work over USB (docs/dev/wifi.md, second version).

The viewer's MJPEG hub now asks the daemon (`open_mjpeg_stream`, the
page's UDID as expect_udid). The daemon checks it against the controlled
device (from the published snapshot: no device lock, a page shouldn't
wait behind a long action to see the screen), asks WDA for the stream
and passes its answer on byte for byte until either side closes, so the
viewer's reader is unchanged. Like ScreenStream, the maintenance lease
is checked when it opens and not held while it runs.

Also from the review: the LocalSet evaluation is written down in
docs/architecture.md (the supervision tick is already Send; the rest is
rustc's higher-ranked Send inference, not worth it without a measured
problem).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@Derrors
Derrors merged commit 680e1b3 into main Nov 25, 2026
7 checks passed
@Derrors
Derrors deleted the refactor/review-phase2 branch November 25, 2026 05:45
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