emd-agents 0.16.1 → 0.17.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 (53) hide show
  1. package/README.md +31 -21
  2. package/dist/adapters/expo/index.js +17 -5
  3. package/dist/adapters/gradle/index.d.ts +3 -0
  4. package/dist/adapters/gradle/index.js +63 -0
  5. package/dist/adapters/xcode/index.d.ts +16 -0
  6. package/dist/adapters/xcode/index.js +116 -0
  7. package/dist/cli/main.js +17 -2
  8. package/dist/core/adapter.d.ts +12 -5
  9. package/dist/core/adapter.js +1 -1
  10. package/dist/core/buildrecord.d.ts +5 -0
  11. package/dist/core/buildrecord.js +22 -0
  12. package/dist/core/check.d.ts +4 -2
  13. package/dist/core/check.js +22 -9
  14. package/dist/core/device/adb.d.ts +23 -0
  15. package/dist/core/device/adb.js +128 -0
  16. package/dist/core/device/backend.d.ts +2 -2
  17. package/dist/core/device/backend.js +36 -9
  18. package/dist/core/doctor.d.ts +6 -1
  19. package/dist/core/doctor.js +100 -19
  20. package/dist/core/emulator.d.ts +29 -0
  21. package/dist/core/emulator.js +144 -0
  22. package/dist/core/gc.d.ts +1 -0
  23. package/dist/core/gc.js +6 -0
  24. package/dist/core/index.d.ts +2 -0
  25. package/dist/core/index.js +2 -0
  26. package/dist/core/input.d.ts +3 -0
  27. package/dist/core/input.js +7 -2
  28. package/dist/core/launch.d.ts +5 -0
  29. package/dist/core/launch.js +332 -69
  30. package/dist/core/log.d.ts +2 -0
  31. package/dist/core/log.js +20 -9
  32. package/dist/core/logs.d.ts +1 -0
  33. package/dist/core/logs.js +32 -17
  34. package/dist/core/owned.d.ts +1 -1
  35. package/dist/core/probe.d.ts +1 -0
  36. package/dist/core/probe.js +22 -8
  37. package/dist/core/process.d.ts +1 -0
  38. package/dist/core/process.js +30 -1
  39. package/dist/core/registry.js +2 -0
  40. package/dist/core/reload.d.ts +1 -1
  41. package/dist/core/reload.js +14 -7
  42. package/dist/core/result.d.ts +23 -0
  43. package/dist/core/result.js +7 -0
  44. package/dist/core/run.d.ts +2 -0
  45. package/dist/core/run.js +2 -0
  46. package/dist/core/setup.d.ts +1 -0
  47. package/dist/core/setup.js +9 -3
  48. package/dist/core/simulator.js +5 -1
  49. package/dist/core/tool.d.ts +1 -0
  50. package/dist/core/tool.js +5 -0
  51. package/dist/mcp/server.js +21 -5
  52. package/package.json +2 -2
  53. package/skills/emd/SKILL.md +8 -3
@@ -0,0 +1,144 @@
1
+ import { dirname, join } from "node:path";
2
+ import { startLogged } from "./process.js";
3
+ import { runTool } from "./tool.js";
4
+ // Android emulators, one per worktree: the base AVD runs read-only on a port
5
+ // EMD reserves, so several worktrees share one AVD and none writes to it
6
+ // (#254). The port is the owned resource (decision 0006).
7
+ // An SDK tool from ANDROID_HOME (or ANDROID_SDK_ROOT), else from PATH.
8
+ export function sdkTool(name) {
9
+ const home = process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT;
10
+ const file = process.platform === "win32" ? `${name}.exe` : name;
11
+ if (!home)
12
+ return file;
13
+ return join(home, name === "adb" ? "platform-tools" : "emulator", file);
14
+ }
15
+ const quote = (s) => `'${s.replaceAll("'", "'\\''")}'`;
16
+ // `emulator -list-avds` prints one AVD name a line, after any INFO lines.
17
+ export const parseAvds = (stdout) => stdout
18
+ .split("\n")
19
+ .map((line) => line.trim())
20
+ .filter((line) => line && !/^(INFO|WARNING|ERROR)\b/.test(line));
21
+ // The AVD `wanted` names, else the first one listed.
22
+ export const pickAvd = (avds, wanted) => wanted === undefined ? avds[0] : avds.find((avd) => avd === wanted);
23
+ // `adb devices`: serial and state, one a line after the header.
24
+ export const parseAdbDevices = (stdout) => stdout
25
+ .split("\n")
26
+ .slice(1)
27
+ .map((line) => line.trim().split(/\s+/))
28
+ .filter((parts) => parts.length >= 2)
29
+ .map(([serial, state]) => ({ serial, state }));
30
+ // Emulator console ports are even, from 5554 to 5682; adb uses port + 1.
31
+ export function pickEmulatorPort(taken) {
32
+ for (let port = 5554; port <= 5682; port += 2) {
33
+ if (!taken.has(String(port)))
34
+ return String(port);
35
+ }
36
+ throw new Error("No free emulator port between 5554 and 5682.");
37
+ }
38
+ export const serialOf = (port) => `emulator-${port}`;
39
+ // The console ports of the emulators adb lists, EMD's or not.
40
+ export const emulatorPorts = (devices) => devices.flatMap(({ serial }) => serial.match(/^emulator-(\d+)$/)?.[1] ?? []);
41
+ const adb = (serial, args, seconds) => runTool(`adb ${args.join(" ")}`, sdkTool("adb"), ["-s", serial, ...args], seconds);
42
+ // The AVD an emulator runs: `adb emu avd name` prints it, then OK.
43
+ export async function avdName(serial) {
44
+ const { stdout } = await adb(serial, ["emu", "avd", "name"], 30);
45
+ return stdout.split("\n")[0]?.trim() || serial;
46
+ }
47
+ export async function installApk(serial, apk) {
48
+ await adb(serial, ["install", "-r", apk], 300);
49
+ }
50
+ // `cmd package resolve-activity --brief` prints the activity last, as
51
+ // package/class.
52
+ export const parseActivity = (stdout) => stdout
53
+ .split("\n")
54
+ .map((line) => line.trim())
55
+ .filter((line) => /^[\w.]+\/[\w.$]+$/.test(line))
56
+ .at(-1);
57
+ // Stops the app if it runs, starts its launcher activity, waits until it shows
58
+ // and returns its pid.
59
+ export async function startApp(serial, appId) {
60
+ await adb(serial, ["shell", "am", "force-stop", appId], 30);
61
+ const resolved = await adb(serial, [
62
+ "shell",
63
+ "cmd",
64
+ "package",
65
+ "resolve-activity",
66
+ "--brief",
67
+ "-c",
68
+ "android.intent.category.LAUNCHER",
69
+ appId,
70
+ ], 30);
71
+ const activity = parseActivity(resolved.stdout);
72
+ if (!activity)
73
+ throw new Error(`${appId} has no launcher activity.`);
74
+ // am prints an error and still exits 0 on some Android versions.
75
+ const { stdout } = await adb(serial, ["shell", "am", "start", "-W", "-n", activity], 120);
76
+ if (/^Error/m.test(stdout))
77
+ throw new Error(stdout.trim());
78
+ const pid = await adb(serial, ["shell", "pidof", appId], 30).then((result) => Number(result.stdout.trim().split(/\s+/)[0]), () => Number.NaN);
79
+ if (!(pid > 0))
80
+ throw new Error(`${appId} is not running after the start.`);
81
+ return pid;
82
+ }
83
+ export async function listAvds() {
84
+ const { stdout } = await runTool("emulator -list-avds", sdkTool("emulator"), ["-list-avds"], 30);
85
+ return parseAvds(stdout);
86
+ }
87
+ export async function adbDevices() {
88
+ const { stdout } = await runTool("adb devices", sdkTool("adb"), ["devices"], 30);
89
+ return parseAdbDevices(stdout);
90
+ }
91
+ // Starts `avd` read-only on `port`, with no window. Its output goes to logPath.
92
+ export function startEmulator(avd, port, logPath) {
93
+ return startLogged([
94
+ quote(sdkTool("emulator")),
95
+ `-avd ${quote(avd)}`,
96
+ "-read-only",
97
+ `-port ${port}`,
98
+ // Software rendering: with no window, a host GPU may be missing (CI).
99
+ "-no-window -gpu swiftshader_indirect -no-audio -no-boot-anim -no-snapshot-save",
100
+ ].join(" "), { cwd: process.cwd(), logPath });
101
+ }
102
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
103
+ // Resolves when Android reports the boot complete; throws after `seconds`.
104
+ export async function untilBooted(serial, seconds) {
105
+ const until = Date.now() + seconds * 1000;
106
+ while (Date.now() < until) {
107
+ const done = await runTool("adb getprop", sdkTool("adb"), ["-s", serial, "shell", "getprop", "sys.boot_completed"], 20).then(({ stdout }) => stdout.trim() === "1", () => false);
108
+ if (done)
109
+ return;
110
+ await sleep(2000);
111
+ }
112
+ throw new Error(`${serial} did not finish booting in ${seconds}s.`);
113
+ }
114
+ // Stops the emulator on `port`. One that is not running counts as stopped;
115
+ // any other failure throws, so the port stays owned until a retry works.
116
+ export async function stopEmulator(port) {
117
+ const serial = serialOf(port);
118
+ if (!(await adbDevices()).some((d) => d.serial === serial))
119
+ return;
120
+ await runTool("adb emu kill", sdkTool("adb"), ["-s", serial, "emu", "kill"], 30);
121
+ }
122
+ // The CPU ABI the emulator runs, such as x86_64 or arm64-v8a.
123
+ export async function deviceAbi(serial) {
124
+ const { stdout } = await adb(serial, ["shell", "getprop", "ro.product.cpu.abi"], 30);
125
+ return stdout.trim();
126
+ }
127
+ // Makes `port` on the emulator reach `port` on this machine, so the app finds
128
+ // its dev server at localhost.
129
+ export async function reversePort(serial, port) {
130
+ await adb(serial, ["reverse", `tcp:${port}`, `tcp:${port}`], 30);
131
+ }
132
+ // Streams the app's tagged lines to logPath. The buffer is cleared first, so
133
+ // the file holds only this run: the emulator keeps earlier runs' lines while it
134
+ // runs. logcat's -e keeps only messages that match, so the file stays small.
135
+ export async function startLogcat(serial, logPath) {
136
+ await adb(serial, ["logcat", "-c"], 30);
137
+ return startLogged(`${quote(sdkTool("adb"))} -s ${serial} logcat -v epoch -e EMD:`, { cwd: dirname(logPath), logPath });
138
+ }
139
+ // A PNG of the screen at `path`.
140
+ export async function screenshot(serial, path) {
141
+ const remote = "/data/local/tmp/emd-screenshot.png";
142
+ await adb(serial, ["shell", "screencap", "-p", remote], 60);
143
+ await adb(serial, ["pull", remote, path], 60);
144
+ }
package/dist/core/gc.d.ts CHANGED
@@ -5,6 +5,7 @@ export type GcResult = {
5
5
  key: string;
6
6
  root: string;
7
7
  simulator?: string;
8
+ emulator?: string;
8
9
  derivedData?: string;
9
10
  mb: number;
10
11
  status: "stale" | "removed" | "skipped" | "failed";
package/dist/core/gc.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { existsSync, readdirSync, rmSync, statSync } from "node:fs";
2
2
  import { join, relative, sep } from "node:path";
3
+ import { serialOf, stopEmulator } from "./emulator.js";
3
4
  import { diskMB } from "./isolate.js";
4
5
  import { firstLine } from "./log.js";
5
6
  import { listLedgers, removeLedger } from "./owned.js";
@@ -108,11 +109,13 @@ export async function gc(options = {}) {
108
109
  const wanted = ledger.resources.find((r) => r.kind === "simulator")?.id;
109
110
  const sim = simulators.find((s) => s.id === wanted || s.name === wanted);
110
111
  const derivedData = ledger.resources.find((r) => r.kind === "derivedData")?.id;
112
+ const emulator = ledger.resources.find((r) => r.kind === "emulator")?.id;
111
113
  const reason = problem(ledger, sim);
112
114
  entries.push({
113
115
  entry: {
114
116
  ...base,
115
117
  ...(sim && { simulator: sim.id }),
118
+ ...(emulator && { emulator: serialOf(emulator) }),
116
119
  ...(derivedData && { derivedData }),
117
120
  mb: (await diskMB(join(homeDir(), "derived", ledger.key))) +
118
121
  (await diskMB(join(homeDir(), "bin", ledger.key))),
@@ -142,6 +145,9 @@ export async function gc(options = {}) {
142
145
  try {
143
146
  if (sim)
144
147
  await removeSimulator(sim);
148
+ const emulator = ledger.resources.find((r) => r.kind === "emulator");
149
+ if (emulator)
150
+ await stopEmulator(emulator.id);
145
151
  rmSync(join(homeDir(), "derived", ledger.key), {
146
152
  recursive: true,
147
153
  force: true,
@@ -1,4 +1,5 @@
1
1
  export type { LaunchPlan, PackageManager, Platform, ProjectInfo, StackAdapter, StackDetection, StackId, } from "./adapter.js";
2
+ export { platforms } from "./adapter.js";
2
3
  export { check } from "./check.js";
3
4
  export { resetChecks } from "./checkrecord.js";
4
5
  export { detect } from "./detect.js";
@@ -9,6 +10,7 @@ export { type GcResult, gc } from "./gc.js";
9
10
  export { swipe, tap, typeText } from "./input.js";
10
11
  export { jobStatus, launchInline, runJob, startJob, waitForJob, } from "./jobs.js";
11
12
  export { logs } from "./logs.js";
13
+ export { xcodeGenerators } from "./probe.js";
12
14
  export { loadedAdapters } from "./registry.js";
13
15
  export { type CheckResult, checkResultSchema, type DetectResult, detectResultSchema, type EvidenceResult, evidenceResultSchema, type InputResult, inputResultSchema, type JobView, jobViewSchema, type LaunchResult, type LogsResult, launchResultSchema, logsResultSchema, } from "./result.js";
14
16
  export { type SetupResult, setup } from "./setup.js";
@@ -1,3 +1,4 @@
1
+ export { platforms } from "./adapter.js";
1
2
  export { check } from "./check.js";
2
3
  export { resetChecks } from "./checkrecord.js";
3
4
  export { detect } from "./detect.js";
@@ -8,6 +9,7 @@ export { gc } from "./gc.js";
8
9
  export { swipe, tap, typeText } from "./input.js";
9
10
  export { jobStatus, launchInline, runJob, startJob, waitForJob, } from "./jobs.js";
10
11
  export { logs } from "./logs.js";
12
+ export { xcodeGenerators } from "./probe.js";
11
13
  export { loadedAdapters } from "./registry.js";
12
14
  export { checkResultSchema, detectResultSchema, evidenceResultSchema, inputResultSchema, jobViewSchema, launchResultSchema, logsResultSchema, } from "./result.js";
13
15
  export { setup } from "./setup.js";
@@ -1,5 +1,8 @@
1
+ import type { Platform } from "./adapter.js";
2
+ import { type DeviceBackend } from "./device/backend.js";
1
3
  import type { InputResult } from "./result.js";
2
4
  import { type RunRef } from "./run.js";
5
+ export declare const backendsFor: (platform: Platform) => DeviceBackend[];
3
6
  export type TapOptions = RunRef & {
4
7
  id?: string;
5
8
  text?: string;
@@ -1,3 +1,4 @@
1
+ import { adbBackend } from "./device/adb.js";
1
2
  import { axeBackend } from "./device/axe.js";
2
3
  import { detectEnvironment, directions, firstWorking, readScreen, secondsTo, } from "./device/backend.js";
3
4
  import { maestroBackend } from "./device/maestro.js";
@@ -6,7 +7,10 @@ import { firstLine } from "./log.js";
6
7
  import { resolveRun } from "./run.js";
7
8
  // Taps, typing and swipes on a launched app (decision 0009). Each acts once
8
9
  // and says which backend did it; whether the app reacted is for `check`.
9
- const backends = [axeBackend, maestroBackend];
10
+ // Best first (decisions 0004, 0005). Maestro works on both platforms.
11
+ export const backendsFor = (platform) => platform === "android"
12
+ ? [adbBackend, maestroBackend]
13
+ : [axeBackend, maestroBackend];
10
14
  const until = (seconds) => Date.now() + seconds * 1000;
11
15
  // Runs `run` against the launched app. `run` returns extra result fields, or
12
16
  // a `reason` when it failed.
@@ -14,7 +18,8 @@ async function act(what, { jobId }, run) {
14
18
  const resolved = resolveRun({ jobId });
15
19
  if ("reason" in resolved)
16
20
  return { verdict: "failed", reason: resolved.reason };
17
- const { device, appId } = resolved.run;
21
+ const { device, appId, platform } = resolved.run;
22
+ const backends = backendsFor(platform);
18
23
  let first = {};
19
24
  try {
20
25
  const env = await detectEnvironment();
@@ -1,6 +1,11 @@
1
+ import { type Platform } from "./adapter.js";
1
2
  import type { LaunchResult } from "./result.js";
2
3
  export type LaunchOptions = {
3
4
  dir?: string;
5
+ platform?: Platform;
6
+ prepare?: string;
7
+ scheme?: string;
8
+ configuration?: string;
4
9
  device?: string;
5
10
  timeoutSeconds?: number;
6
11
  };