@developerz.ai/ui-debugger-mcp 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/README.md +45 -10
  2. package/dist/adapters/android/adb.d.ts +34 -5
  3. package/dist/adapters/android/adb.js +74 -20
  4. package/dist/adapters/android/adb.js.map +1 -1
  5. package/dist/adapters/android/android-adapter.d.ts +23 -7
  6. package/dist/adapters/android/android-adapter.js +106 -36
  7. package/dist/adapters/android/android-adapter.js.map +1 -1
  8. package/dist/adapters/android/android-adapter.test-helpers.d.ts +52 -0
  9. package/dist/adapters/android/android-adapter.test-helpers.js +115 -0
  10. package/dist/adapters/android/android-adapter.test-helpers.js.map +1 -0
  11. package/dist/adapters/android/commands.d.ts +27 -4
  12. package/dist/adapters/android/commands.js +66 -4
  13. package/dist/adapters/android/commands.js.map +1 -1
  14. package/dist/adapters/android/ports.d.ts +32 -0
  15. package/dist/adapters/android/ports.js +47 -0
  16. package/dist/adapters/android/ports.js.map +1 -0
  17. package/dist/adapters/android/uiautomator.d.ts +10 -2
  18. package/dist/adapters/android/uiautomator.js +36 -4
  19. package/dist/adapters/android/uiautomator.js.map +1 -1
  20. package/dist/adapters/browser/browser-adapter.d.ts +16 -13
  21. package/dist/adapters/browser/browser-adapter.js +196 -313
  22. package/dist/adapters/browser/browser-adapter.js.map +1 -1
  23. package/dist/adapters/browser/cdp.d.ts +40 -2
  24. package/dist/adapters/browser/cdp.js +223 -19
  25. package/dist/adapters/browser/cdp.js.map +1 -1
  26. package/dist/adapters/browser/extractor.d.ts +53 -0
  27. package/dist/adapters/browser/extractor.js +224 -0
  28. package/dist/adapters/browser/extractor.js.map +1 -0
  29. package/dist/adapters/browser/filters.d.ts +15 -0
  30. package/dist/adapters/browser/filters.js +93 -0
  31. package/dist/adapters/browser/filters.js.map +1 -0
  32. package/dist/adapters/browser/launch.d.ts +41 -0
  33. package/dist/adapters/browser/launch.js +61 -0
  34. package/dist/adapters/browser/launch.js.map +1 -0
  35. package/dist/adapters/budget.d.ts +16 -0
  36. package/dist/adapters/budget.js +25 -0
  37. package/dist/adapters/budget.js.map +1 -0
  38. package/dist/adapters/contract.d.ts +82 -6
  39. package/dist/adapters/desktop/atspi.d.ts +15 -0
  40. package/dist/adapters/desktop/atspi.js +37 -17
  41. package/dist/adapters/desktop/atspi.js.map +1 -1
  42. package/dist/adapters/desktop/capture.js +8 -1
  43. package/dist/adapters/desktop/capture.js.map +1 -1
  44. package/dist/adapters/desktop/desktop-adapter.d.ts +9 -2
  45. package/dist/adapters/desktop/desktop-adapter.js +84 -32
  46. package/dist/adapters/desktop/desktop-adapter.js.map +1 -1
  47. package/dist/adapters/desktop/input.d.ts +16 -1
  48. package/dist/adapters/desktop/input.js +30 -0
  49. package/dist/adapters/desktop/input.js.map +1 -1
  50. package/dist/adapters/desktop/proc.d.ts +18 -3
  51. package/dist/adapters/desktop/proc.js +48 -5
  52. package/dist/adapters/desktop/proc.js.map +1 -1
  53. package/dist/adapters/factory.d.ts +3 -1
  54. package/dist/adapters/factory.js +6 -2
  55. package/dist/adapters/factory.js.map +1 -1
  56. package/dist/adapters/limit.d.ts +10 -0
  57. package/dist/adapters/limit.js +19 -0
  58. package/dist/adapters/limit.js.map +1 -0
  59. package/dist/agent/belt/act.d.ts +44 -9
  60. package/dist/agent/belt/act.js +176 -21
  61. package/dist/agent/belt/act.js.map +1 -1
  62. package/dist/agent/belt/look.d.ts +1 -1
  63. package/dist/agent/belt/look.js +31 -2
  64. package/dist/agent/belt/look.js.map +1 -1
  65. package/dist/agent/belt/observe.d.ts +42 -16
  66. package/dist/agent/belt/observe.js +134 -22
  67. package/dist/agent/belt/observe.js.map +1 -1
  68. package/dist/agent/belt/report.d.ts +29 -9
  69. package/dist/agent/belt/report.js +77 -10
  70. package/dist/agent/belt/report.js.map +1 -1
  71. package/dist/agent/belt/trail.d.ts +34 -0
  72. package/dist/agent/belt/trail.js +64 -0
  73. package/dist/agent/belt/trail.js.map +1 -0
  74. package/dist/agent/capabilities.d.ts +4 -4
  75. package/dist/agent/capabilities.js +41 -13
  76. package/dist/agent/capabilities.js.map +1 -1
  77. package/dist/agent/loop.d.ts +70 -21
  78. package/dist/agent/loop.js +146 -48
  79. package/dist/agent/loop.js.map +1 -1
  80. package/dist/agent/prompts/compose.d.ts +9 -1
  81. package/dist/agent/prompts/compose.js +5 -4
  82. package/dist/agent/prompts/compose.js.map +1 -1
  83. package/dist/agent/prompts/debug-agent.d.ts +13 -1
  84. package/dist/agent/prompts/debug-agent.js +98 -14
  85. package/dist/agent/prompts/debug-agent.js.map +1 -1
  86. package/dist/agent/prompts/desktop-addendum.d.ts +1 -1
  87. package/dist/agent/prompts/desktop-addendum.js +1 -1
  88. package/dist/agent/prompts/web-addendum.d.ts +1 -1
  89. package/dist/agent/prompts/web-addendum.js +84 -16
  90. package/dist/agent/prompts/web-addendum.js.map +1 -1
  91. package/dist/cli/control.d.ts +3 -1
  92. package/dist/cli/control.js +26 -40
  93. package/dist/cli/control.js.map +1 -1
  94. package/dist/cli/help.d.ts +3 -1
  95. package/dist/cli/help.js +6 -2
  96. package/dist/cli/help.js.map +1 -1
  97. package/dist/cli/init.d.ts +3 -6
  98. package/dist/cli/init.js +64 -16
  99. package/dist/cli/init.js.map +1 -1
  100. package/dist/config/fingerprint.d.ts +32 -0
  101. package/dist/config/fingerprint.js +47 -0
  102. package/dist/config/fingerprint.js.map +1 -0
  103. package/dist/config/load.d.ts +7 -1
  104. package/dist/config/load.js +17 -4
  105. package/dist/config/load.js.map +1 -1
  106. package/dist/config/schema.d.ts +12 -5
  107. package/dist/config/schema.js +17 -7
  108. package/dist/config/schema.js.map +1 -1
  109. package/dist/errors.d.ts +14 -2
  110. package/dist/errors.js +24 -4
  111. package/dist/errors.js.map +1 -1
  112. package/dist/index.d.ts +1 -1
  113. package/dist/index.js +1 -1
  114. package/dist/main.js +26 -8
  115. package/dist/main.js.map +1 -1
  116. package/dist/mcp/server.d.ts +16 -1
  117. package/dist/mcp/server.js +23 -2
  118. package/dist/mcp/server.js.map +1 -1
  119. package/dist/mcp/server.test-helpers.d.ts +23 -0
  120. package/dist/mcp/server.test-helpers.js +49 -0
  121. package/dist/mcp/server.test-helpers.js.map +1 -0
  122. package/dist/mcp/tools/describe.js +5 -0
  123. package/dist/mcp/tools/describe.js.map +1 -1
  124. package/dist/mcp/tools/end-session.js +5 -0
  125. package/dist/mcp/tools/end-session.js.map +1 -1
  126. package/dist/mcp/tools/get-findings.js +23 -3
  127. package/dist/mcp/tools/get-findings.js.map +1 -1
  128. package/dist/mcp/tools/output.d.ts +50 -0
  129. package/dist/mcp/tools/output.js +55 -0
  130. package/dist/mcp/tools/output.js.map +1 -0
  131. package/dist/mcp/tools/result.d.ts +24 -1
  132. package/dist/mcp/tools/result.js +106 -5
  133. package/dist/mcp/tools/result.js.map +1 -1
  134. package/dist/mcp/tools/send-message.js +5 -0
  135. package/dist/mcp/tools/send-message.js.map +1 -1
  136. package/dist/mcp/tools/start-debug.js +7 -1
  137. package/dist/mcp/tools/start-debug.js.map +1 -1
  138. package/dist/services/debug-service.d.ts +47 -8
  139. package/dist/services/debug-service.js +185 -14
  140. package/dist/services/debug-service.js.map +1 -1
  141. package/dist/services/session-builder.d.ts +20 -3
  142. package/dist/services/session-builder.js +43 -13
  143. package/dist/services/session-builder.js.map +1 -1
  144. package/dist/services/summarize.d.ts +1 -1
  145. package/dist/services/summarize.js +8 -1
  146. package/dist/services/summarize.js.map +1 -1
  147. package/dist/session/atomic-write.d.ts +24 -0
  148. package/dist/session/atomic-write.js +39 -0
  149. package/dist/session/atomic-write.js.map +1 -0
  150. package/dist/session/findings-store.js +3 -4
  151. package/dist/session/findings-store.js.map +1 -1
  152. package/dist/session/process-identity.d.ts +16 -0
  153. package/dist/session/process-identity.js +29 -0
  154. package/dist/session/process-identity.js.map +1 -1
  155. package/dist/session/session.js +36 -4
  156. package/dist/session/session.js.map +1 -1
  157. package/dist/session/state-file.d.ts +64 -5
  158. package/dist/session/state-file.js +76 -9
  159. package/dist/session/state-file.js.map +1 -1
  160. package/dist/session/workspace.d.ts +11 -0
  161. package/dist/session/workspace.js +14 -1
  162. package/dist/session/workspace.js.map +1 -1
  163. package/package.json +4 -1
package/README.md CHANGED
@@ -106,6 +106,10 @@ Install like any local MCP server — one entry in your `.mcp.json`:
106
106
  }
107
107
  ```
108
108
 
109
+ It's also published in the official [MCP Registry](https://modelcontextprotocol.io/registry) as
110
+ `io.github.developerz-ai/ui-debugger-mcp` — any client that browses the registry (instead of a
111
+ hand-written `.mcp.json` entry) can find and install it by that name.
112
+
109
113
  Then add a per-project `.ui-debugger-mcp.json` describing the app to debug
110
114
  (models, targets, urls). The fastest way is the `init` command:
111
115
 
@@ -167,10 +171,10 @@ and prints the `.mcp.json` snippet to paste.
167
171
 
168
172
  ```text
169
173
  // 4. In Claude Code (or any MCP client):
170
- start_debug { target: "web", goal: "log in and add item 3 to the cart" }
174
+ start_debug { target: "web", goal: "log in and add item 3 to the cart", url: "http://localhost:3000" }
171
175
 
172
176
  // 5. Poll until done:
173
- get_findings { session_id: "...", wait: true }
177
+ get_findings { session_id: "...", wait: 30000 }
174
178
 
175
179
  // 6. Read bugs[] + visual[] + summary. Fix code, repeat.
176
180
  ```
@@ -181,7 +185,7 @@ It's a **conversation**, not a remote control — five fat tools, not one-per-cl
181
185
 
182
186
  | Tool | What it does |
183
187
  |------|--------------|
184
- | `start_debug` | Open a run: `{ target, goal, criteria?, timeout? }`. The small agent drives autonomously. Returns `{ session_id }`. |
188
+ | `start_debug` | Open a run: `{ target, goal, url?, criteria?, timeout? }`. `url` is required when the target has no configured url. The small agent drives autonomously. Returns `{ session_id }`. |
185
189
  | `get_findings` | Poll status + structured findings (functional bugs + visual issues) + evidence. Long-poll with `wait`. |
186
190
  | `send_message` | Talk to the running agent mid-flight — add work, redirect, or answer a question. |
187
191
  | `describe` | List the configured targets + models for this project. |
@@ -191,6 +195,14 @@ A run is **always time-capped**: `start_debug`'s `timeout` (seconds) overrides t
191
195
  default 300s, so a session can never hang forever — it auto-ends and frees the
192
196
  profile lock when the cap fires.
193
197
 
198
+ Every tool result carries **both** a pretty-printed text block and a typed
199
+ `structuredContent` payload validated against a declared `outputSchema` — parse
200
+ the structured half, don't scrape the text. Tools also declare MCP annotations
201
+ (`readOnlyHint`, `destructiveHint`, `idempotentHint`, `openWorldHint`) so clients
202
+ can render/gate them correctly, and evidence paths (screenshots, `replay.mp4`,
203
+ logs) ride as `resource_link` content items, not inline strings. Full shapes in
204
+ [`docs/reference.md`](docs/reference.md).
205
+
194
206
  Typical loop from a smart agent:
195
207
 
196
208
  ```text
@@ -227,7 +239,7 @@ One Chrome profile = one run. If a previous run crashed without cleaning up:
227
239
  ```bash
228
240
  npx @developerz.ai/ui-debugger-mcp stop # graceful teardown
229
241
  ```
230
- Or delete `./tmp/ui-debugger-mcp/state.json` and restart the MCP server.
242
+ Or delete `./tmp/ui-debugger-mcp/<project>/state.json` and restart the MCP server.
231
243
 
232
244
  **Run times out with no findings**
233
245
  Default cap is 300 s. Raise it per-call:
@@ -239,11 +251,34 @@ If the agent is stuck at login, add `?debug-ai=true` to your app's login route
239
251
  pattern.
240
252
 
241
253
  **`get_findings` returns empty `bugs[]` / `visual[]`**
242
- The run may still be in progress — use `wait: true`:
254
+ The run may still be in progress — use `wait` (ms) to long-poll:
243
255
  ```text
244
- get_findings { session_id: "...", wait: true }
256
+ get_findings { session_id: "...", wait: 30000 }
245
257
  ```
246
- Check `./tmp/ui-debugger-mcp/<project>/logs/agent.log` for the agent's trace.
258
+ Check `./tmp/ui-debugger-mcp/<project>/sessions/<id>/logs/agent.log` for the agent's trace.
259
+
260
+ **Run fails instantly: "… is not a valid model ID"**
261
+ The model string in `.ui-debugger-mcp.json` is not a catalog id. OpenRouter takes
262
+ `provider/model` with optional `:floor` / `:nitro` routing suffixes — a `#…`
263
+ suffix is rejected outright. Use plain ids (`deepseek/deepseek-v4-flash`).
264
+
265
+ **Config edits seem to have no effect**
266
+ Config is read ONCE when the MCP server starts, so an edit cannot reach a running
267
+ server. Since v1.3 the server refuses to start a run on a changed file and tells
268
+ you to reconnect it (in Claude Code: `/mcp` → reconnect `ui-debugger`) rather
269
+ than silently running the old settings.
270
+
271
+ **"Another server already has a session" and you can't see it**
272
+ That run belongs to a different MCP server process on the same project. You can
273
+ now READ it — `get_findings { session_id: "<the id in the error>" }` serves its
274
+ on-disk findings — so you can tell a live run from one that settled and is just
275
+ waiting to be closed. To take the project over: `ui-debugger-mcp stop`.
276
+
277
+ **Run ends `failed` with no bugs and no explanation**
278
+ That means the driver never called `report` — it used its whole step budget.
279
+ The summary says so explicitly; it is NOT a crash and NOT a clean pass. Narrow
280
+ the goal, or raise `timeout`. Read `logs/agent.log` and the screenshots for what
281
+ it actually did.
247
282
 
248
283
  **`replay.mp4` not generated**
249
284
  ffmpeg is optional. Install it and retry, or ignore — findings and screenshots
@@ -289,7 +324,7 @@ See [`docs/idea/`](docs/idea/) for design notes.
289
324
 
290
325
  ## Credits / influences
291
326
 
292
- - [`ai-task-master`](../ai-task-master) — build template (orchestrator + subagents)
293
- - [`gold-standards-in-ai`](../gold-standards-in-ai) — MCP & code conventions
294
- - [`claude-code-bible`](../../sebyx07/claude-code-bible) — agent-first patterns
327
+ - `ai-task-master` — build template (orchestrator + subagents), reference repo, not published
328
+ - `gold-standards-in-ai` — MCP & code conventions, reference repo, not published
329
+ - `claude-code-bible` — agent-first patterns ([sebyx07/claude-code-bible](https://github.com/sebyx07/claude-code-bible))
295
330
  - [Model Context Protocol](https://modelcontextprotocol.io/docs/develop/connect-local-servers)
@@ -7,15 +7,44 @@
7
7
  * live in the pure builders beside it (`commands.ts`, `uiautomator.ts`) so they stay
8
8
  * unit-testable without spawning. {@link AdbCli} is injected as a seam in tests.
9
9
  *
10
+ * One subprocess call underneath ({@link AdbExec}, injectable) resolving raw bytes; the
11
+ * text channels decode UTF-8 on top, so `exec-out` stays binary-safe by construction.
12
+ *
10
13
  * Three channels, mirroring `adb`'s own surface:
11
14
  * - {@link Adb.shell} — `adb shell <cmd>` → text (am/input/uiautomator/getprop/logcat).
12
15
  * - {@link Adb.execOut} — `adb exec-out <cmd>` → **raw bytes** (binary-safe `screencap -p`).
13
16
  * - {@link Adb.adb} — top-level `adb <args>` (`wait-for-device`, `emu kill`).
14
17
  *
15
- * Target selection is baked into the `flags` at construction: `['-s', serial]` for an
16
- * attached device, `['-e']` for the single managed emulator. Fails loud — a non-zero
17
- * exit or a missing binary rejects as an {@link AdbError}, never a silent fallback.
18
+ * Target selection is baked into the `flags` at construction and is always an explicit
19
+ * serial — `['-s', serial]`, the configured one when attaching, `emulator-<port>` for
20
+ * the emulator a managed run started (never the ambiguous `-e`). Fails loud — a non-zero
21
+ * exit, a timeout or a missing binary rejects as an {@link AdbError}, never a silent
22
+ * fallback; {@link pendingStateOrThrow} is the one narrow exception a boot poll may retry.
23
+ */
24
+ /**
25
+ * Triage a `get-state` rejection during a boot poll: return `''` (poll again) only when
26
+ * the device is merely not up yet, otherwise rethrow. Honest failure — a missing `adb`
27
+ * or a wedged call must surface now, not 120 s later as a misleading "no device appeared".
18
28
  */
29
+ export declare function pendingStateOrThrow(error: unknown): string;
30
+ /**
31
+ * The one subprocess call the transport makes — resolves **raw stdout bytes** (text
32
+ * channels decode on top, so `exec-out` stays binary-safe). Injectable so argv-shape
33
+ * tests assert the argv they would have spawned instead of spawning: a real child's
34
+ * stdout is at the mercy of the machine, and argv ordering is a pure property.
35
+ */
36
+ export type AdbExec = (bin: string, args: string[], timeoutMs: number) => Promise<Uint8Array>;
37
+ /** The real {@link AdbExec}: `execFile`, capped, buffer-encoded (never UTF-8 mangled). */
38
+ export declare const execFileBytes: AdbExec;
39
+ /** Construction knobs for {@link AdbCli} — also the seams unit tests drive it through. */
40
+ export interface AdbCliOptions {
41
+ /** Per-call timeout in ms (default {@link DEFAULT_TIMEOUT_MS}). */
42
+ timeoutMs?: number;
43
+ /** Binary to invoke (default `adb`). */
44
+ bin?: string;
45
+ /** Subprocess runner (default {@link execFileBytes}). */
46
+ exec?: AdbExec;
47
+ }
19
48
  /** The ADB transport the adapter depends on — implemented by {@link AdbCli}, faked in tests. */
20
49
  export interface Adb {
21
50
  /** `adb <target> shell <command…>` → stdout text. */
@@ -27,11 +56,11 @@ export interface Adb {
27
56
  }
28
57
  /**
29
58
  * `execFile`-backed {@link Adb}. Construct with the device-selection `flags`
30
- * (`['-s', serial]` to attach · `['-e']` for the managed emulator).
59
+ * (`['-s', serial]` — the config serial when attaching, `emulator-<port>` when managed).
31
60
  */
32
61
  export declare class AdbCli implements Adb {
33
62
  #private;
34
- constructor(flags: string[]);
63
+ constructor(flags: string[], options?: AdbCliOptions);
35
64
  shell(command: string[]): Promise<string>;
36
65
  adb(args: string[]): Promise<string>;
37
66
  execOut(command: string[]): Promise<Uint8Array>;
@@ -7,14 +7,19 @@
7
7
  * live in the pure builders beside it (`commands.ts`, `uiautomator.ts`) so they stay
8
8
  * unit-testable without spawning. {@link AdbCli} is injected as a seam in tests.
9
9
  *
10
+ * One subprocess call underneath ({@link AdbExec}, injectable) resolving raw bytes; the
11
+ * text channels decode UTF-8 on top, so `exec-out` stays binary-safe by construction.
12
+ *
10
13
  * Three channels, mirroring `adb`'s own surface:
11
14
  * - {@link Adb.shell} — `adb shell <cmd>` → text (am/input/uiautomator/getprop/logcat).
12
15
  * - {@link Adb.execOut} — `adb exec-out <cmd>` → **raw bytes** (binary-safe `screencap -p`).
13
16
  * - {@link Adb.adb} — top-level `adb <args>` (`wait-for-device`, `emu kill`).
14
17
  *
15
- * Target selection is baked into the `flags` at construction: `['-s', serial]` for an
16
- * attached device, `['-e']` for the single managed emulator. Fails loud — a non-zero
17
- * exit or a missing binary rejects as an {@link AdbError}, never a silent fallback.
18
+ * Target selection is baked into the `flags` at construction and is always an explicit
19
+ * serial — `['-s', serial]`, the configured one when attaching, `emulator-<port>` for
20
+ * the emulator a managed run started (never the ambiguous `-e`). Fails loud — a non-zero
21
+ * exit, a timeout or a missing binary rejects as an {@link AdbError}, never a silent
22
+ * fallback; {@link pendingStateOrThrow} is the one narrow exception a boot poll may retry.
18
23
  */
19
24
  import { execFile } from 'node:child_process';
20
25
  import { promisify } from 'node:util';
@@ -23,14 +28,64 @@ const pExecFile = promisify(execFile);
23
28
  const ADB = 'adb';
24
29
  /** 64 MiB stdout cap — `screencap -p` PNGs run a few MB on hi-dpi panels; text replies are tiny. */
25
30
  const MAX_BUFFER = 64 * 1024 * 1024;
31
+ /**
32
+ * Per-invocation wall-clock cap. `adb` wedges in practice — a stuck `uiautomator dump`,
33
+ * a half-dead emulator, an adb server mid-restart — and an unbounded `execFile` would
34
+ * hang the whole run behind it. 30 s is far above any healthy call (a cold `screencap`
35
+ * or a big hierarchy dump lands in single-digit seconds).
36
+ *
37
+ * The long boot wait is *not* one call: the adapter polls `get-state`/`getprop` against
38
+ * its own 120 s deadline, so every individual call stays under this cap.
39
+ */
40
+ const DEFAULT_TIMEOUT_MS = 30_000;
41
+ /**
42
+ * Kill signal on expiry. SIGKILL, not SIGTERM: a wedged `adb` blocked on the transport
43
+ * is exactly the process that ignores a polite signal, and Node only rejects once the
44
+ * child is actually gone.
45
+ */
46
+ const KILL_SIGNAL = 'SIGKILL';
47
+ /** Decoder for the text channels — one instance, `adb` speaks UTF-8. */
48
+ const DECODER = new TextDecoder();
49
+ /**
50
+ * `adb` failure phrases that mean "the device is not up **yet**" — the only rejection a
51
+ * boot poll may retry. Deliberately narrow: a missing binary, an ambiguous target
52
+ * (`more than one device`) or a timeout are real failures, not "keep waiting".
53
+ */
54
+ const PENDING_DEVICE = /no devices\/emulators found|device offline|device '[^']*' not found|device still (connecting|authorizing)/i;
55
+ /**
56
+ * Triage a `get-state` rejection during a boot poll: return `''` (poll again) only when
57
+ * the device is merely not up yet, otherwise rethrow. Honest failure — a missing `adb`
58
+ * or a wedged call must surface now, not 120 s later as a misleading "no device appeared".
59
+ */
60
+ export function pendingStateOrThrow(error) {
61
+ if (error instanceof AdbError && PENDING_DEVICE.test(error.message))
62
+ return '';
63
+ throw error;
64
+ }
65
+ /** The real {@link AdbExec}: `execFile`, capped, buffer-encoded (never UTF-8 mangled). */
66
+ export const execFileBytes = async (bin, args, timeoutMs) => {
67
+ const { stdout } = await pExecFile(bin, args, {
68
+ encoding: 'buffer',
69
+ maxBuffer: MAX_BUFFER,
70
+ timeout: timeoutMs,
71
+ killSignal: KILL_SIGNAL,
72
+ });
73
+ return Uint8Array.from(stdout);
74
+ };
26
75
  /**
27
76
  * `execFile`-backed {@link Adb}. Construct with the device-selection `flags`
28
- * (`['-s', serial]` to attach · `['-e']` for the managed emulator).
77
+ * (`['-s', serial]` — the config serial when attaching, `emulator-<port>` when managed).
29
78
  */
30
79
  export class AdbCli {
31
80
  #flags;
32
- constructor(flags) {
81
+ #timeoutMs;
82
+ #bin;
83
+ #exec;
84
+ constructor(flags, options = {}) {
33
85
  this.#flags = flags;
86
+ this.#timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
87
+ this.#bin = options.bin ?? ADB;
88
+ this.#exec = options.exec ?? execFileBytes;
34
89
  }
35
90
  shell(command) {
36
91
  return this.#text(['shell', ...command]);
@@ -38,33 +93,32 @@ export class AdbCli {
38
93
  adb(args) {
39
94
  return this.#text(args);
40
95
  }
41
- async execOut(command) {
42
- const args = ['exec-out', ...command];
43
- try {
44
- const { stdout } = await pExecFile(ADB, [...this.#flags, ...args], {
45
- encoding: 'buffer',
46
- maxBuffer: MAX_BUFFER,
47
- });
48
- return Uint8Array.from(stdout);
49
- }
50
- catch (error) {
51
- throw this.#wrap(args, error);
52
- }
96
+ execOut(command) {
97
+ return this.#bytes(['exec-out', ...command]);
53
98
  }
99
+ /** UTF-8 view of the byte read — `adb`'s text channels (`am`, `getprop`, `logcat`) are UTF-8. */
54
100
  async #text(args) {
101
+ return DECODER.decode(await this.#bytes(args));
102
+ }
103
+ async #bytes(args) {
55
104
  try {
56
- const { stdout } = await pExecFile(ADB, [...this.#flags, ...args], { maxBuffer: MAX_BUFFER });
57
- return stdout;
105
+ return await this.#exec(this.#bin, [...this.#flags, ...args], this.#timeoutMs);
58
106
  }
59
107
  catch (error) {
60
108
  throw this.#wrap(args, error);
61
109
  }
62
110
  }
63
- /** Wrap any subprocess failure as a loud {@link AdbError} (ENOENT → a clear install hint). */
111
+ /**
112
+ * Wrap any subprocess failure as a loud {@link AdbError}: ENOENT → a clear install
113
+ * hint, expiry (the child was killed by our `timeout`) → a wedge report naming the cap.
114
+ */
64
115
  #wrap(args, error) {
65
116
  if (error instanceof Error && error.code === 'ENOENT') {
66
117
  return new AdbError('android: `adb` not found on PATH (install Android platform-tools)');
67
118
  }
119
+ if (error instanceof Error && error.killed === true) {
120
+ return new AdbError(`adb ${args[0] ?? ''} timed out after ${this.#timeoutMs}ms (${KILL_SIGNAL}ed — adb or the emulator is wedged)`);
121
+ }
68
122
  const message = error instanceof Error ? error.message : String(error);
69
123
  return new AdbError(`adb ${args[0] ?? ''} failed: ${message}`);
70
124
  }
@@ -1 +1 @@
1
- {"version":3,"file":"adb.js","sourceRoot":"","sources":["../../../src/adapters/android/adb.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,MAAM,SAAS,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;AAEtC,MAAM,GAAG,GAAG,KAAK,CAAC;AAElB,oGAAoG;AACpG,MAAM,UAAU,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;AAYpC;;;GAGG;AACH,MAAM,OAAO,MAAM;IACR,MAAM,CAAW;IAE1B,YAAY,KAAe;QACzB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,OAAiB;QACrB,OAAO,IAAI,CAA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AAC,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;QACjF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,IAAc,EAAE,KAAc;QAClC,IAAI,KAAK,YAAY,KAAK,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACjF,OAAO,IAAI,QAAQ,CAAC,mEAAmE,CAAC,CAAC;QAC3F,CAAC;QACD,IAAI,KAAK,YAAY,KAAK,IAAK,KAA8B,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC;YAC9E,OAAO,IAAI,QAAQ,CACjB,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,oBAAoB,IAAI,CAAC,UAAU,OAAO,WAAW,qCAAqC,CAC/G,CAAC;QACJ,CAAC;QACD,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvE,OAAO,IAAI,QAAQ,CAAC,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,YAAY,OAAO,EAAE,CAAC,CAAC;IACjE,CAAC;CACF"}
@@ -9,8 +9,10 @@
9
9
  *
10
10
  * **Managed or attach** (`idea/adapters.md`): with an `adbSerial` the adapter attaches
11
11
  * to a running device and `close` is disconnect-only (never stops it); otherwise it
12
- * boots the configured `emulator @avd`, waits for `sys.boot_completed`, and `close`
13
- * stops it. Android is fully framework-driven — no vision needed for actions.
12
+ * boots the configured `emulator @avd` on a free console port (`ports.ts`), waits for
13
+ * `sys.boot_completed`, and `close` stops it. Either way every call is bound to one
14
+ * explicit serial (`adb -s <serial>`), so a managed run can never drive — or kill — an
15
+ * emulator it did not start. Android is fully framework-driven — no vision for actions.
14
16
  *
15
17
  * `console` is logcat-backed (real crash/error signal); `network` has no ADB channel
16
18
  * and throws loud ({@link AdapterError}). Every backend failure surfaces as an
@@ -33,14 +35,20 @@ export type SpawnEmulator = (bin: string, args: string[]) => ChildProcess;
33
35
  export interface AndroidAdapterInit {
34
36
  /** Resolved android-target config (avd, emulatorPath, adbSerial). */
35
37
  config: AndroidTarget;
36
- /** Override the ADB transport (defaults to {@link AdbCli}); a seam for tests. */
37
- adb?: Adb;
38
+ /**
39
+ * Override the ADB transport (defaults to an {@link AdbCli} bound to the device
40
+ * serial); a seam for tests. As a function it is the transport *factory* and gets
41
+ * the serial the adapter bound to — managed mode calls it at boot.
42
+ */
43
+ adb?: Adb | ((serial: string) => Adb);
38
44
  /** Override the read source (defaults to {@link AdbUiAutomator}); a seam for tests. */
39
45
  ui?: UiAutomatorSource;
40
46
  /** Override the emulator spawn (defaults to `spawn`, detached); a seam for tests. */
41
47
  spawn?: SpawnEmulator;
42
48
  /** Override the managed-boot deadline (defaults to {@link BOOT_WAIT_MS}); a seam for tests. */
43
49
  bootWaitMs?: number;
50
+ /** Override the managed console-port pick (defaults to {@link pickEmulatorPort}); a test seam. */
51
+ pickPort?: () => Promise<number>;
44
52
  }
45
53
  /**
46
54
  * The android {@link Adapter}: an ADB + uiautomator pair behind one contract.
@@ -49,10 +57,14 @@ export interface AndroidAdapterInit {
49
57
  export declare class AndroidAdapter implements Adapter {
50
58
  #private;
51
59
  private constructor();
52
- /** Wire the adapter from config: `adbSerial` → attach (`-s serial`), else managed emulator (`-e`). */
60
+ /**
61
+ * Wire the adapter from config. Both modes talk to **one explicit serial**
62
+ * (`adb -s <serial>`): attach reads it from `adbSerial` and binds here, managed
63
+ * only learns it when it picks a console port and spawns — so it binds at boot.
64
+ */
53
65
  static create(init: AndroidAdapterInit): AndroidAdapter;
54
66
  /** Ensure the device is up (managed: boot the emulator), then start the activity/package. */
55
- open(target: string): Promise<void>;
67
+ open(target: string, timeoutMs?: number): Promise<void>;
56
68
  find(opts: Query): Promise<Node | null>;
57
69
  readState(opts?: Query): Promise<Node[]>;
58
70
  click(target: NodeRef): Promise<void>;
@@ -65,6 +77,10 @@ export declare class AndroidAdapter implements Adapter {
65
77
  console(opts?: LogQuery): Promise<ConsoleEntry[]>;
66
78
  /** Android exposes no network channel over ADB — unsupported, surfaced loud. */
67
79
  network(): Promise<NetworkEntry[]>;
68
- /** Attach: disconnect-only (never stop the device). Managed: stop the emulator. */
80
+ /**
81
+ * Attach: disconnect-only (never stop the device). Managed: stop **our** emulator —
82
+ * the kill goes to the bound serial (`adb -s emulator-<port> emu kill`), so a
83
+ * co-running emulator we never started is untouched.
84
+ */
69
85
  close(): Promise<void>;
70
86
  }