@parcha/agentrun-computer 0.1.0-beta.34 → 0.1.0-beta.36
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.
- package/README.md +251 -46
- package/box/Dockerfile +3 -1
- package/box/agentrun-archil +59 -7
- package/box/build-daytona.py +20 -4
- package/dist/browser.d.ts +73 -23
- package/dist/browser.js +141 -17
- package/dist/browserbase.d.ts +48 -25
- package/dist/browserbase.js +176 -29
- package/dist/chrome.d.ts +3 -4
- package/dist/chrome.js +6 -11
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/conformance/browserbase.js +1 -1
- package/dist/conformance/cases/provider.js +78 -3
- package/dist/conformance/daytona.d.ts +4 -0
- package/dist/conformance/daytona.js +7 -1
- package/dist/conformance/e2b.js +2 -1
- package/dist/conformance/fixture.js +24 -2
- package/dist/conformance/kernel.js +1 -1
- package/dist/conformance/kind.js +2 -1
- package/dist/conformance/{local.d.ts → local-chrome.d.ts} +7 -7
- package/dist/conformance/{local.js → local-chrome.js} +11 -11
- package/dist/conformance/orgo.d.ts +11 -5
- package/dist/conformance/orgo.js +37 -8
- package/dist/conformance/phone-harness.js +2 -1
- package/dist/conformance/report.js +3 -2
- package/dist/conformance/runner.js +25 -7
- package/dist/conformance/types.d.ts +21 -8
- package/dist/conformance/types.js +10 -0
- package/dist/conformance/windows-fixture.d.ts +1 -0
- package/dist/conformance/windows-fixture.js +37 -0
- package/dist/daytona.d.ts +16 -35
- package/dist/daytona.js +87 -95
- package/dist/descriptor.d.ts +116 -0
- package/dist/descriptor.js +92 -0
- package/dist/descriptors/browserbase.d.ts +25 -0
- package/dist/descriptors/browserbase.js +20 -0
- package/dist/descriptors/daytona.d.ts +83 -0
- package/dist/descriptors/daytona.js +81 -0
- package/dist/descriptors/docker.d.ts +12 -0
- package/dist/descriptors/docker.js +17 -0
- package/dist/descriptors/e2b.d.ts +22 -0
- package/dist/descriptors/e2b.js +19 -0
- package/dist/descriptors/index.d.ts +256 -0
- package/dist/descriptors/index.js +19 -0
- package/dist/descriptors/kernel.d.ts +21 -0
- package/dist/descriptors/kernel.js +17 -0
- package/dist/descriptors/local-chrome.d.ts +17 -0
- package/dist/descriptors/local-chrome.js +21 -0
- package/dist/descriptors/modal.d.ts +54 -0
- package/dist/descriptors/modal.js +48 -0
- package/dist/descriptors/orgo.d.ts +99 -0
- package/dist/descriptors/orgo.js +49 -0
- package/dist/descriptors/phone-harness.d.ts +33 -0
- package/dist/descriptors/phone-harness.js +23 -0
- package/dist/descriptors/shared.d.ts +7 -0
- package/dist/descriptors/shared.js +6 -0
- package/dist/desktop.d.ts +4 -4
- package/dist/desktop.js +4 -4
- package/dist/docker/egress.d.ts +5 -4
- package/dist/docker/egress.js +7 -6
- package/dist/docker/image.js +17 -15
- package/dist/docker/inspect.d.ts +147 -0
- package/dist/docker/inspect.js +335 -0
- package/dist/docker.d.ts +6 -6
- package/dist/docker.js +53 -47
- package/dist/drift.d.ts +3 -145
- package/dist/drift.js +5 -334
- package/dist/e2b.d.ts +2 -8
- package/dist/e2b.js +17 -23
- package/dist/index.d.ts +9 -12
- package/dist/index.js +14 -12
- package/dist/isolation.js +2 -2
- package/dist/janitor.d.ts +20 -2
- package/dist/janitor.js +67 -19
- package/dist/kernel.d.ts +19 -29
- package/dist/kernel.js +110 -32
- package/dist/kinds.d.ts +59 -34
- package/dist/kinds.js +19 -117
- package/dist/labels.d.ts +20 -0
- package/dist/labels.js +62 -1
- package/dist/{local.d.ts → local-chrome.d.ts} +8 -9
- package/dist/{local.js → local-chrome.js} +26 -19
- package/dist/local-supervisor.d.ts +3 -3
- package/dist/local-supervisor.js +3 -3
- package/dist/modal.d.ts +6 -25
- package/dist/modal.js +86 -88
- package/dist/mount-archil.d.ts +25 -3
- package/dist/mount-archil.js +43 -12
- package/dist/orgo-channel.js +16 -13
- package/dist/orgo-desktop.d.ts +162 -0
- package/dist/orgo-desktop.js +1856 -0
- package/dist/orgo-names.d.ts +178 -0
- package/dist/orgo-names.js +287 -0
- package/dist/orgo.d.ts +34 -20
- package/dist/orgo.js +424 -173
- package/dist/phone-harness.d.ts +7 -12
- package/dist/phone-harness.js +70 -54
- package/dist/protocol.d.ts +5 -0
- package/dist/provider.d.ts +153 -10
- package/dist/provider.js +48 -1
- package/dist/providers.d.ts +23 -24
- package/dist/providers.js +14 -17
- package/dist/result.d.ts +11 -1
- package/dist/result.js +11 -0
- package/dist/test-connection.js +2 -2
- package/package.json +42 -2
package/dist/phone-harness.d.ts
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
import type { KindCapabilities } from "./kinds.js";
|
|
2
2
|
import type { MountKind } from "./mount.js";
|
|
3
|
-
import { type
|
|
4
|
-
import { type AttachedComputer, type CallContext, type ComputerCapabilities, type ComputerConnection, type ComputerInfo, type ComputerProvider, type ComputerRef, type ComputerSpec, type CreatedComputer, type
|
|
3
|
+
import { type Rect, type UiNode } from "./protocol.js";
|
|
4
|
+
import { type AttachedComputer, type CallContext, type ComputerCapabilities, type ComputerConnection, type ComputerInfo, type ComputerProvider, type ComputerRef, type ComputerSpec, type CreatedComputer, type NetworkPolicy, type NetworkSpec, type Refusal, type ReleaseHow } from "./provider.js";
|
|
5
5
|
import { type Result } from "./result.js";
|
|
6
6
|
import { type ScreenWatch, type ScreenWatchHandlers, type WatchOptions } from "./screen-watch.js";
|
|
7
7
|
/** The phone harness's API. */
|
|
8
8
|
export declare const PHONE_HARNESS_URL = "https://api.phone-harness.com";
|
|
9
|
-
/** What a cloud Android offers through this provider, as its capabilities and hello declare them. */
|
|
10
|
-
export declare const PHONE_GROUPS: readonly Group[];
|
|
11
9
|
/** A phone mounts no folder. */
|
|
12
10
|
export declare const MOUNT: readonly MountKind[];
|
|
13
|
-
/** The
|
|
14
|
-
export declare const NETWORK: readonly NetworkForm[];
|
|
15
|
-
/** The one configuration it offers, as its capabilities() declare it: the kind registry's entry holds the same. */
|
|
11
|
+
/** The one configuration it offers, as its capabilities() declare it: its descriptor's. */
|
|
16
12
|
export declare const CAPABILITIES: readonly KindCapabilities[];
|
|
17
|
-
/** The image a phone is read back by: a cloud Android has none to choose, so a setting that names none takes this. */
|
|
18
|
-
export declare const PHONE_IMAGE = "android";
|
|
19
13
|
/** The longest lifetime by default: a rented phone bills by the minute. */
|
|
20
14
|
export declare const DEFAULT_MAX_LIFETIME_MS: number;
|
|
21
15
|
/** How long create waits for a phone to be ready by default: the service's own CLI waits 240 s. */
|
|
@@ -155,7 +149,7 @@ export declare class PhoneHarnessProvider implements ComputerProvider {
|
|
|
155
149
|
}, context?: CallContext): Promise<Result<AttachedComputer>>;
|
|
156
150
|
list(labels: Record<string, string>, context?: CallContext): Promise<Result<ComputerInfo[]>>;
|
|
157
151
|
network(ref: ComputerRef, policy?: NetworkSpec): Promise<Result<NetworkPolicy>>;
|
|
158
|
-
release(ref: ComputerRef, how:
|
|
152
|
+
release(ref: ComputerRef, how: ReleaseHow): Promise<Result<void>>;
|
|
159
153
|
connect(ref: ComputerRef): Promise<Result<ComputerConnection>>;
|
|
160
154
|
/**
|
|
161
155
|
* The live view: the phone client's screen.watch on a connection of the watch's own (daemonWatch), PNG frames from
|
|
@@ -177,8 +171,9 @@ export declare function decodeScreencap(raw: Buffer): Result<{
|
|
|
177
171
|
height: number;
|
|
178
172
|
rgba: Buffer;
|
|
179
173
|
}>;
|
|
180
|
-
/** An 8-bit RGBA PNG, non-interlaced, of rect of a width-wide RGBA frame.
|
|
181
|
-
|
|
174
|
+
/** An 8-bit RGBA PNG, non-interlaced, of rect of a width-wide RGBA frame. The rows are deflated off the event loop: a
|
|
175
|
+
* 1080x2400 screen took about 170 ms, every request of the server this harness runs in waiting on it. */
|
|
176
|
+
export declare function encodePng(rgba: Buffer, width: number, [x, y, w, h]: Rect): Promise<Buffer>;
|
|
182
177
|
/** uiautomator's dump as the protocol's tree: one root (the window's, or one over several), at most MAX_UI_NODES nodes. */
|
|
183
178
|
export declare function parseUiTree(xml: string, [width, height]: [number, number]): Result<{
|
|
184
179
|
root: UiNode;
|
package/dist/phone-harness.js
CHANGED
|
@@ -12,25 +12,23 @@ import { execFile } from "node:child_process";
|
|
|
12
12
|
import { createHash, randomBytes } from "node:crypto";
|
|
13
13
|
import { readFileSync } from "node:fs";
|
|
14
14
|
import { createConnection } from "node:net";
|
|
15
|
-
import {
|
|
15
|
+
import { promisify } from "node:util";
|
|
16
|
+
import { crc32, deflate } from "node:zlib";
|
|
16
17
|
import { checkActions } from "./actions.js";
|
|
18
|
+
import { PHONE_GROUPS, PHONE_HARNESS } from "./descriptors/phone-harness.js";
|
|
17
19
|
import { MAX_CHUNK_BYTES, MAX_UI_NODES, PROTOCOL_VERSION, } from "./protocol.js";
|
|
18
|
-
import { checkGpu,
|
|
19
|
-
import {
|
|
20
|
+
import { checkGpu, checkTenure, runLifetimeMs, } from "./provider.js";
|
|
21
|
+
import { failFor, ok, vendorFail } from "./result.js";
|
|
20
22
|
import { daemonWatch, redactWords } from "./screen-watch.js";
|
|
21
23
|
import { listTested } from "./test-connection.js";
|
|
24
|
+
/** This module's failures: one with code vendor names the phone harness. */
|
|
25
|
+
const fail = failFor("phone-harness");
|
|
22
26
|
/** The phone harness's API. */
|
|
23
27
|
export const PHONE_HARNESS_URL = "https://api.phone-harness.com";
|
|
24
|
-
/** What a cloud Android offers through this provider, as its capabilities and hello declare them. */
|
|
25
|
-
export const PHONE_GROUPS = ["screen", "input.touch", "input.keys", "apps", "apps.ui_tree"];
|
|
26
28
|
/** A phone mounts no folder. */
|
|
27
|
-
export const MOUNT =
|
|
28
|
-
/** The
|
|
29
|
-
export const
|
|
30
|
-
/** The one configuration it offers, as its capabilities() declare it: the kind registry's entry holds the same. */
|
|
31
|
-
export const CAPABILITIES = [{ kind: "android", groups: [...PHONE_GROUPS] }];
|
|
32
|
-
/** The image a phone is read back by: a cloud Android has none to choose, so a setting that names none takes this. */
|
|
33
|
-
export const PHONE_IMAGE = "android";
|
|
29
|
+
export const MOUNT = PHONE_HARNESS.mounts;
|
|
30
|
+
/** The one configuration it offers, as its capabilities() declare it: its descriptor's. */
|
|
31
|
+
export const CAPABILITIES = PHONE_HARNESS.capabilities;
|
|
34
32
|
/** The longest lifetime by default: a rented phone bills by the minute. */
|
|
35
33
|
export const DEFAULT_MAX_LIFETIME_MS = 30 * 60_000;
|
|
36
34
|
/** How long create waits for a phone to be ready by default: the service's own CLI waits 240 s. */
|
|
@@ -81,7 +79,7 @@ async function ledgerSafe(fn) {
|
|
|
81
79
|
}
|
|
82
80
|
catch (error) {
|
|
83
81
|
if (error instanceof LedgerFailure)
|
|
84
|
-
return
|
|
82
|
+
return vendorFail("phone-harness", error.message);
|
|
85
83
|
throw error;
|
|
86
84
|
}
|
|
87
85
|
}
|
|
@@ -113,7 +111,7 @@ export function adbCli(path = "adb", { port = ADB_SERVER_PORT, env } = {}) {
|
|
|
113
111
|
if (error && signal?.aborted)
|
|
114
112
|
return resolve(fail("aborted", "adb was aborted"));
|
|
115
113
|
if (error && typeof error.code !== "number")
|
|
116
|
-
return resolve(
|
|
114
|
+
return resolve(vendorFail("phone-harness", `adb could not run: ${error.message.split("\n")[0]}`));
|
|
117
115
|
resolve(ok({ exitCode: typeof code === "number" ? code : error ? 1 : 0, stdout: Buffer.from(stdout), stderr: Buffer.from(stderr).toString("utf8") }));
|
|
118
116
|
});
|
|
119
117
|
// stdin always ends, so no adb waits on it; it carries the input when there is one.
|
|
@@ -127,7 +125,7 @@ export function adbCli(path = "adb", { port = ADB_SERVER_PORT, env } = {}) {
|
|
|
127
125
|
const start = await started;
|
|
128
126
|
if (!start.ok || start.value.exitCode !== 0) {
|
|
129
127
|
started = undefined;
|
|
130
|
-
return start.ok ?
|
|
128
|
+
return start.ok ? vendorFail("phone-harness", `adb start-server failed: ${start.value.stderr.trim().split("\n")[0] || `exit code ${start.value.exitCode}`}`) : start;
|
|
131
129
|
}
|
|
132
130
|
const frame = (request) => `${Buffer.byteLength(request).toString(16).padStart(4, "0")}${request}`;
|
|
133
131
|
return new Promise((resolve) => {
|
|
@@ -157,10 +155,10 @@ export function adbCli(path = "adb", { port = ADB_SERVER_PORT, env } = {}) {
|
|
|
157
155
|
bound = true;
|
|
158
156
|
socket.write(frame(service));
|
|
159
157
|
});
|
|
160
|
-
socket.on("error", (error) => done(
|
|
158
|
+
socket.on("error", (error) => done(vendorFail("phone-harness", `the adb server on 127.0.0.1:${port} could not be reached for ${what}: ${error.message}`)));
|
|
161
159
|
socket.on("close", () => {
|
|
162
160
|
if (!bound)
|
|
163
|
-
return done(
|
|
161
|
+
return done(vendorFail("phone-harness", `the adb server closed host:transport-id:${transport} before it answered`));
|
|
164
162
|
// A phone's service answers OKAY and then its output, with no length: the output is what follows.
|
|
165
163
|
if (transport !== undefined && buffer.subarray(0, 4).toString("latin1") === "OKAY")
|
|
166
164
|
return done(ok(buffer.subarray(4).toString("utf8")));
|
|
@@ -187,8 +185,8 @@ export function adbAnswer(raw, what) {
|
|
|
187
185
|
if (status === "OKAY")
|
|
188
186
|
return ok(message);
|
|
189
187
|
if (status === "FAIL")
|
|
190
|
-
return
|
|
191
|
-
return
|
|
188
|
+
return vendorFail("phone-harness", `the adb server refused ${what}: ${redactWords(message.trim())}`);
|
|
189
|
+
return vendorFail("phone-harness", `the adb server answered ${what} with ${JSON.stringify(raw.subarray(0, 40).toString("latin1"))}, not OKAY or FAIL`);
|
|
192
190
|
}
|
|
193
191
|
/** The phone-harness provider from its options alone; throws in words when one is missing or wrong. */
|
|
194
192
|
export async function create(options) {
|
|
@@ -261,7 +259,7 @@ export class PhoneHarnessProvider {
|
|
|
261
259
|
os: "android",
|
|
262
260
|
daemon: "none",
|
|
263
261
|
lifetime: "vendor",
|
|
264
|
-
network: [...
|
|
262
|
+
network: [...PHONE_HARNESS.network],
|
|
265
263
|
isolation: "vm",
|
|
266
264
|
groups: [...PHONE_GROUPS],
|
|
267
265
|
maxLifetimeMs: this.#maxLifetimeMs,
|
|
@@ -269,7 +267,7 @@ export class PhoneHarnessProvider {
|
|
|
269
267
|
};
|
|
270
268
|
}
|
|
271
269
|
check(spec) {
|
|
272
|
-
const refusals =
|
|
270
|
+
const refusals = checkTenure({ lifetimeMs: spec?.lifetimeMs, owner: spec?.owner }, this.capabilities(), "phone harness");
|
|
273
271
|
if (typeof spec?.image !== "string" || spec.image === "")
|
|
274
272
|
refusals.push({ field: "image", message: "image is required: a name the phone is read back by (a cloud Android has no image to choose)" });
|
|
275
273
|
if (spec?.network !== "open")
|
|
@@ -351,7 +349,7 @@ export class PhoneHarnessProvider {
|
|
|
351
349
|
return fail("limit", `${where}: ${said} (session_limit)`);
|
|
352
350
|
if (response.status === 404)
|
|
353
351
|
return fail("not_found", `${where}: ${said} (404)`);
|
|
354
|
-
const failed =
|
|
352
|
+
const failed = vendorFail("phone-harness", `${where}: ${said} (${response.status}${answer.code ? `, ${answer.code}` : ""})`);
|
|
355
353
|
// A 5xx is no refusal: a gateway may answer it for a request the service went on to act on.
|
|
356
354
|
return response.status >= 500 ? { ...failed, uncertain: true } : failed;
|
|
357
355
|
}
|
|
@@ -418,10 +416,10 @@ export class PhoneHarnessProvider {
|
|
|
418
416
|
return fail("aborted", "create was aborted before the phone harness was asked");
|
|
419
417
|
const from = this.#now();
|
|
420
418
|
const requestKey = randomBytes(16).toString("hex");
|
|
421
|
-
const body = { platform: "android", kind: "emulator", timeout_seconds: Math.ceil(spec
|
|
419
|
+
const body = { platform: "android", kind: "emulator", timeout_seconds: Math.ceil(runLifetimeMs(spec) / 1000) };
|
|
422
420
|
// The key goes into the ledger before the POST: whatever answers are lost, list() can find the phone by it.
|
|
423
421
|
const pending = pendingId(requestKey);
|
|
424
|
-
await this.#ledger.put({ id: pending, labels: { ...(spec.labels ?? {}) }, image: spec.image, deadline: new Date(from + spec
|
|
422
|
+
await this.#ledger.put({ id: pending, labels: { ...(spec.labels ?? {}) }, image: spec.image, deadline: new Date(from + runLifetimeMs(spec)).toISOString(), request: requestKey });
|
|
425
423
|
const posted = await this.#api("POST", "/sessions", { body, headers: { "Idempotency-Key": requestKey }, ...(signal ? { signal } : {}) });
|
|
426
424
|
const cancelled = !posted.ok && posted.error.code === "aborted";
|
|
427
425
|
let first = posted.ok ? sessionOf(posted.value.json, "POST /sessions") : undefined;
|
|
@@ -435,10 +433,10 @@ export class PhoneHarnessProvider {
|
|
|
435
433
|
const kept = "the ledger keeps the request's key, so list() finds the phone if it was made";
|
|
436
434
|
const recovered = await this.#api("GET", `/sessions/requests/${requestKey}`);
|
|
437
435
|
if (!recovered.ok)
|
|
438
|
-
return fail(cancelled ? "aborted" : "
|
|
436
|
+
return fail(cancelled ? "aborted" : "vendor", `${why}; and the service has no session for the request: ${recovered.error.message}; ${kept}`);
|
|
439
437
|
first = sessionOf(recovered.value.json, `GET /sessions/requests/${requestKey}`);
|
|
440
438
|
if (!first.ok)
|
|
441
|
-
return fail(cancelled ? "aborted" : "
|
|
439
|
+
return fail(cancelled ? "aborted" : "vendor", `${why}; and its recovery answered no session: ${first.error.message}; ${kept}`);
|
|
442
440
|
}
|
|
443
441
|
else if (!posted.ok) {
|
|
444
442
|
// The phone harness refused the request: nothing was made, and the key is forgotten.
|
|
@@ -446,7 +444,7 @@ export class PhoneHarnessProvider {
|
|
|
446
444
|
return posted;
|
|
447
445
|
}
|
|
448
446
|
if (first === undefined || !first.ok)
|
|
449
|
-
return
|
|
447
|
+
return vendorFail("phone-harness", "POST /sessions answered no session");
|
|
450
448
|
const id = first.value.id;
|
|
451
449
|
/** A ledger write about the phone, or the phone ended in words: a phone its ledger cannot record would bill unseen. */
|
|
452
450
|
const record = async (write) => {
|
|
@@ -458,11 +456,11 @@ export class PhoneHarnessProvider {
|
|
|
458
456
|
// Ended at once, by the API alone: the ledger has just failed.
|
|
459
457
|
const ended = await this.#api("DELETE", `/sessions/${encodeURIComponent(id)}`);
|
|
460
458
|
const after = ended.ok || ended.error.code === "not_found" ? "the phone was ended" : `the phone ${id} could not be ended (${ended.error.message})`;
|
|
461
|
-
return
|
|
459
|
+
return vendorFail("phone-harness", `${error instanceof Error ? error.message : String(error)}; ${after}`);
|
|
462
460
|
}
|
|
463
461
|
};
|
|
464
462
|
const named = await record(async () => {
|
|
465
|
-
await this.#ledger.put({ id, labels: { ...(spec.labels ?? {}) }, image: spec.image, deadline: this.#deadlineOf(first.value, from + spec
|
|
463
|
+
await this.#ledger.put({ id, labels: { ...(spec.labels ?? {}) }, image: spec.image, deadline: this.#deadlineOf(first.value, from + runLifetimeMs(spec)) });
|
|
466
464
|
await this.#ledger.remove(pending);
|
|
467
465
|
});
|
|
468
466
|
if (!named.ok)
|
|
@@ -491,23 +489,23 @@ export class PhoneHarnessProvider {
|
|
|
491
489
|
return ended("timeout", `the phone ${id} was not ready within ${this.#readyMs / 1000} s: ${read.error.message}`);
|
|
492
490
|
}
|
|
493
491
|
if (!read.ok)
|
|
494
|
-
return ended(read.error.code === "gone" ? "
|
|
492
|
+
return ended(read.error.code === "gone" ? "vendor" : read.error.code, `the phone ${id} did not start: ${read.error.message}`);
|
|
495
493
|
session = read.value;
|
|
496
494
|
if (session.state === "ready")
|
|
497
495
|
break;
|
|
498
496
|
if (session.state !== "provisioning")
|
|
499
|
-
return ended("
|
|
497
|
+
return ended("vendor", `the phone ${id} did not start: ${this.#clean(session.error ?? session.state)}`);
|
|
500
498
|
if (this.#now() - from >= this.#readyMs)
|
|
501
499
|
return ended("timeout", `the phone ${id} was not ready within ${this.#readyMs / 1000} s`);
|
|
502
500
|
await this.#sleep(this.#pollMs);
|
|
503
501
|
}
|
|
504
|
-
const deadline = this.#deadlineOf(session, from + spec
|
|
502
|
+
const deadline = this.#deadlineOf(session, from + runLifetimeMs(spec));
|
|
505
503
|
const kept = await record(() => this.#ledger.put({ id, labels: { ...(spec.labels ?? {}) }, image: spec.image, deadline }));
|
|
506
504
|
if (!kept.ok)
|
|
507
505
|
return kept;
|
|
508
506
|
const took = this.#now() - from;
|
|
509
507
|
if (this.#now() >= Date.parse(deadline)) {
|
|
510
|
-
return ended("timeout", `the phone ${id}'s lifetime of ${spec
|
|
508
|
+
return ended("timeout", `the phone ${id}'s lifetime of ${runLifetimeMs(spec) / 1000} s ended before it was ready (it took ${Math.round(took / 100) / 10} s)`);
|
|
511
509
|
}
|
|
512
510
|
return ok({ id, name: id });
|
|
513
511
|
}
|
|
@@ -557,7 +555,7 @@ export class PhoneHarnessProvider {
|
|
|
557
555
|
if (!got.ok)
|
|
558
556
|
return got;
|
|
559
557
|
if (!Array.isArray(got.value.json))
|
|
560
|
-
return
|
|
558
|
+
return vendorFail("phone-harness", `GET /sessions answered ${show(got.value.json).slice(0, 200)}, not a list`);
|
|
561
559
|
const live = new Map();
|
|
562
560
|
for (const one of got.value.json) {
|
|
563
561
|
const session = sessionOf(one, "GET /sessions");
|
|
@@ -635,6 +633,8 @@ export class PhoneHarnessProvider {
|
|
|
635
633
|
async #release(ref, how) {
|
|
636
634
|
if (how === "stop")
|
|
637
635
|
return fail("unsupported", "a cloud phone has no stop: release it with delete, which ends the session");
|
|
636
|
+
if (how !== "delete")
|
|
637
|
+
return fail("unsupported", `a cloud phone is kept for no agent (the phone harness declares no persistence), so release ${JSON.stringify(how)} is refused and the phone is left as it is: release it with delete`);
|
|
638
638
|
const entry = await this.#ledger.get(ref.id);
|
|
639
639
|
if (entry?.released !== undefined) {
|
|
640
640
|
// Released already, and the service may still be ending it: ok. Its own GET says whether it is gone, and then it is
|
|
@@ -677,7 +677,7 @@ export class PhoneHarnessProvider {
|
|
|
677
677
|
return ok(id);
|
|
678
678
|
last = state ?? "not listed";
|
|
679
679
|
}
|
|
680
|
-
return
|
|
680
|
+
return vendorFail("phone-harness", `adb connected to the phone, and the adb server still lists it as ${last}, not a device`);
|
|
681
681
|
}
|
|
682
682
|
async #connect(ref) {
|
|
683
683
|
const entry = await this.#ledger.get(ref.id);
|
|
@@ -701,7 +701,7 @@ export class PhoneHarnessProvider {
|
|
|
701
701
|
// The vendor's shapes (OpenAPI SessionAdb): a DNS name, a port, and a code ph_ and base32 letters. Anything outside
|
|
702
702
|
// these characters is refused rather than passed to adb.
|
|
703
703
|
if (typeof host !== "string" || !/^[A-Za-z0-9.:-]+$/.test(host) || !Number.isSafeInteger(port) || typeof code !== "string" || !/^[A-Za-z0-9_-]{1,128}$/.test(code)) {
|
|
704
|
-
return
|
|
704
|
+
return vendorFail("phone-harness", `the phone harness gave ${ref.id} no adb link this provider can use (a host, a port and an unlock code)`);
|
|
705
705
|
}
|
|
706
706
|
// The endpoint and the code never reach an argv: the endpoint goes to the adb server over its socket, the unlock as
|
|
707
707
|
// a shell service on a socket the server bound to the phone, and every spawned adb names the phone by its transport id.
|
|
@@ -746,7 +746,7 @@ export class PhoneHarnessProvider {
|
|
|
746
746
|
const said = connected.value.trim();
|
|
747
747
|
// adb's answer names the endpoint, so none of it is passed on: the error is the provider's own words.
|
|
748
748
|
if (!said.includes("connected") || /cannot|failed|unable/i.test(said)) {
|
|
749
|
-
return letGo(
|
|
749
|
+
return letGo(vendorFail("phone-harness", `adb could not reach the phone of ${ref.id}: the adb server could not connect to its adb endpoint`));
|
|
750
750
|
}
|
|
751
751
|
const transport = await this.#transportOf(endpoint);
|
|
752
752
|
if (!transport.ok)
|
|
@@ -760,7 +760,7 @@ export class PhoneHarnessProvider {
|
|
|
760
760
|
return letGo(asked);
|
|
761
761
|
if (!asked.value.includes("ph-ok")) {
|
|
762
762
|
if (!/^locked\b/.test(asked.value.trim()))
|
|
763
|
-
return letGo(
|
|
763
|
+
return letGo(vendorFail("phone-harness", `the phone of ${ref.id} answered neither ph-ok nor locked`));
|
|
764
764
|
// The vendor's unlock is the shell command `unlock <code>`, once per connection (docs, guides/connect-adb): the
|
|
765
765
|
// shell service's own string, sent over the adb server's socket bound to the phone, so no argv holds the code.
|
|
766
766
|
// Probe 1 (2026-11-08) showed it live: "unlocked", and getprop answered after it.
|
|
@@ -774,7 +774,7 @@ export class PhoneHarnessProvider {
|
|
|
774
774
|
if (!probe.value.stdout.toString("utf8").includes("ph-ok")) {
|
|
775
775
|
// The vendor's answer ("wrong code") may echo the code, more than once, or an endpoint: all are taken out.
|
|
776
776
|
const words = redactWords(unlocked.value.trim().split("\n")[0].replaceAll(code, "[code]"));
|
|
777
|
-
return letGo(
|
|
777
|
+
return letGo(vendorFail("phone-harness", /\bunlocked\b/.test(unlocked.value) ? `the phone of ${ref.id} stayed locked after its unlock code` : `the phone of ${ref.id} refused its unlock code: ${words}`));
|
|
778
778
|
}
|
|
779
779
|
}
|
|
780
780
|
const client = new AdbClient(id, adb, release, this.#sleep, this.#now);
|
|
@@ -796,7 +796,7 @@ const carries = (labels, wanted) => Object.entries(wanted ?? {}).every(([key, va
|
|
|
796
796
|
function sessionOf(json, where) {
|
|
797
797
|
const s = json;
|
|
798
798
|
if (typeof s !== "object" || s === null || typeof s.id !== "string" || typeof s.state !== "string")
|
|
799
|
-
return
|
|
799
|
+
return vendorFail("phone-harness", `${where} answered ${show(json).slice(0, 200)}, not a session`);
|
|
800
800
|
return ok(s);
|
|
801
801
|
}
|
|
802
802
|
// --- the client: the protocol over adb ---------------------------------------------------------------------------
|
|
@@ -869,11 +869,14 @@ class AdbClient {
|
|
|
869
869
|
this.sleep = sleep;
|
|
870
870
|
this.now = now;
|
|
871
871
|
}
|
|
872
|
-
/** What the phone is: its ABI, its screen's size and density; then the hello it answers. */
|
|
872
|
+
/** What the phone is: its ABI, its Android release, its screen's size and density; then the hello it answers. */
|
|
873
873
|
async open(version) {
|
|
874
874
|
const abi = await this.#shell("getprop ro.product.cpu.abi");
|
|
875
875
|
if (!abi.ok)
|
|
876
876
|
return abi;
|
|
877
|
+
const release = await this.#shell("getprop ro.build.version.release");
|
|
878
|
+
if (!release.ok)
|
|
879
|
+
return release;
|
|
877
880
|
const size = await this.#shell("wm size");
|
|
878
881
|
if (!size.ok)
|
|
879
882
|
return size;
|
|
@@ -883,12 +886,14 @@ class AdbClient {
|
|
|
883
886
|
const sizes = [...size.value.matchAll(/(\d+)x(\d+)/g)];
|
|
884
887
|
const last = sizes.at(-1);
|
|
885
888
|
if (!last)
|
|
886
|
-
return
|
|
889
|
+
return vendorFail("phone-harness", `the phone (adb transport ${this.transport}) did not say its screen's size: ${size.value.trim().slice(0, 100)}`);
|
|
887
890
|
this.#size = [Number(last[1]), Number(last[2])];
|
|
888
891
|
const dpi = [...density.value.matchAll(/(\d+)/g)].at(-1);
|
|
889
892
|
this.#scale = dpi ? Number(dpi[1]) / 160 : 1;
|
|
890
893
|
const reported = abi.value.trim();
|
|
891
|
-
|
|
894
|
+
// An empty answer says no release: the hello then carries none.
|
|
895
|
+
const osVersion = release.value.trim();
|
|
896
|
+
this.#hello = { protocol: PROTOCOL_VERSION, version, os: "android", ...(osVersion ? { osVersion } : {}), kind: "android", arch: ARCH[reported] ?? reported, groups: [...PHONE_GROUPS] };
|
|
892
897
|
return ok(this.#hello);
|
|
893
898
|
}
|
|
894
899
|
close() {
|
|
@@ -916,7 +921,7 @@ class AdbClient {
|
|
|
916
921
|
return ran;
|
|
917
922
|
if (ran.value.exitCode !== 0) {
|
|
918
923
|
const why = (ran.value.stderr.trim() || ran.value.stdout.toString("utf8").trim()).split("\n")[0] || `exit code ${ran.value.exitCode}`;
|
|
919
|
-
return
|
|
924
|
+
return vendorFail("phone-harness", `${command.split(" ").slice(0, 2).join(" ")} on the phone failed: ${redactWords(why)}`);
|
|
920
925
|
}
|
|
921
926
|
return ok(ran.value.stdout.toString("utf8"));
|
|
922
927
|
}
|
|
@@ -925,7 +930,7 @@ class AdbClient {
|
|
|
925
930
|
if (!ran.ok)
|
|
926
931
|
return ran;
|
|
927
932
|
if (ran.value.exitCode !== 0)
|
|
928
|
-
return
|
|
933
|
+
return vendorFail("phone-harness", `${command.split(" ")[0]} on the phone failed: ${redactWords(ran.value.stderr.trim().split("\n")[0] || `exit code ${ran.value.exitCode}`)}`);
|
|
929
934
|
return ok(ran.value.stdout);
|
|
930
935
|
}
|
|
931
936
|
async call(method, params, options = {}) {
|
|
@@ -988,7 +993,10 @@ class AdbClient {
|
|
|
988
993
|
}
|
|
989
994
|
rect = r;
|
|
990
995
|
}
|
|
991
|
-
const bytes = encodePng(rgba, width, rect);
|
|
996
|
+
const bytes = await encodePng(rgba, width, rect);
|
|
997
|
+
// close() may have run while the frame was deflating: hold nothing for a closed connection.
|
|
998
|
+
if (this.#closed)
|
|
999
|
+
return fail("gone", "the connection to the phone is closed");
|
|
992
1000
|
const id = randomBytes(6).toString("hex");
|
|
993
1001
|
this.#held = { id, bytes };
|
|
994
1002
|
const data = bytes.subarray(0, MAX_CHUNK_BYTES);
|
|
@@ -1043,9 +1051,14 @@ class AdbClient {
|
|
|
1043
1051
|
const frameWidth = Math.min(width, maxWidth);
|
|
1044
1052
|
const frameHeight = Math.max(1, Math.floor((height * frameWidth) / width));
|
|
1045
1053
|
const pixels = frameWidth === width ? rgba : scaleRgba(rgba, width, height, frameWidth, frameHeight);
|
|
1054
|
+
// Encoded before the frame is published: a watch that reads the seq while this one encodes never gets an older
|
|
1055
|
+
// frame under the newer seq.
|
|
1056
|
+
const bytes = await encodePng(pixels, frameWidth, [0, 0, frameWidth, frameHeight]);
|
|
1057
|
+
if (signal.aborted || this.#closed)
|
|
1058
|
+
return aborted();
|
|
1046
1059
|
watch.seq += 1;
|
|
1047
1060
|
watch.digest = digest;
|
|
1048
|
-
watch.frame = { bytes
|
|
1061
|
+
watch.frame = { bytes, frameWidth, frameHeight, width, height };
|
|
1049
1062
|
return ok(this.#frameAnswer());
|
|
1050
1063
|
}
|
|
1051
1064
|
const left = deadline - this.now();
|
|
@@ -1280,7 +1293,7 @@ class AdbClient {
|
|
|
1280
1293
|
if (!started.ok)
|
|
1281
1294
|
return started;
|
|
1282
1295
|
if (/Error:|Exception/.test(started.value))
|
|
1283
|
-
return
|
|
1296
|
+
return vendorFail("phone-harness", `am start of ${component}: ${redactWords(started.value.trim().split("\n").at(-1) ?? "")}`);
|
|
1284
1297
|
return ok({});
|
|
1285
1298
|
}
|
|
1286
1299
|
async #terminate(id) {
|
|
@@ -1311,14 +1324,14 @@ function pointsOf(action) {
|
|
|
1311
1324
|
/** The frame `adb exec-out screencap` gives: a header (width, height, format, and a color space on newer Androids), then the pixels. */
|
|
1312
1325
|
export function decodeScreencap(raw) {
|
|
1313
1326
|
if (raw.length < 12)
|
|
1314
|
-
return
|
|
1327
|
+
return vendorFail("phone-harness", `screencap gave ${raw.length} bytes, not a frame`);
|
|
1315
1328
|
const width = raw.readUInt32LE(0);
|
|
1316
1329
|
const height = raw.readUInt32LE(4);
|
|
1317
1330
|
const format = raw.readUInt32LE(8);
|
|
1318
1331
|
const pixels = width * height * 4;
|
|
1319
1332
|
const header = raw.length - pixels;
|
|
1320
1333
|
if (!(width > 0 && height > 0 && (header === 12 || header === 16)))
|
|
1321
|
-
return
|
|
1334
|
+
return vendorFail("phone-harness", `screencap gave ${raw.length} bytes for a ${width}x${height} frame`);
|
|
1322
1335
|
const rgba = Buffer.from(raw.subarray(header));
|
|
1323
1336
|
// 1 is RGBA_8888, 2 RGBX_8888 (alpha unset), 5 BGRA_8888.
|
|
1324
1337
|
if (format === 2)
|
|
@@ -1345,8 +1358,11 @@ function scaleRgba(rgba, width, height, toWidth, toHeight) {
|
|
|
1345
1358
|
}
|
|
1346
1359
|
return out;
|
|
1347
1360
|
}
|
|
1348
|
-
/**
|
|
1349
|
-
|
|
1361
|
+
/** zlib's deflate on its thread pool: a screen's deflate never holds this process's event loop. */
|
|
1362
|
+
const deflateAsync = promisify(deflate);
|
|
1363
|
+
/** An 8-bit RGBA PNG, non-interlaced, of rect of a width-wide RGBA frame. The rows are deflated off the event loop: a
|
|
1364
|
+
* 1080x2400 screen took about 170 ms, every request of the server this harness runs in waiting on it. */
|
|
1365
|
+
export async function encodePng(rgba, width, [x, y, w, h]) {
|
|
1350
1366
|
const rows = Buffer.alloc((w * 4 + 1) * h);
|
|
1351
1367
|
for (let row = 0; row < h; row++) {
|
|
1352
1368
|
const from = ((y + row) * width + x) * 4;
|
|
@@ -1364,7 +1380,7 @@ export function encodePng(rgba, width, [x, y, w, h]) {
|
|
|
1364
1380
|
ihdr.writeUInt32BE(w, 0);
|
|
1365
1381
|
ihdr.writeUInt32BE(h, 4);
|
|
1366
1382
|
ihdr.set([8, 6, 0, 0, 0], 8);
|
|
1367
|
-
return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk("IHDR", ihdr), chunk("IDAT",
|
|
1383
|
+
return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk("IHDR", ihdr), chunk("IDAT", await deflateAsync(rows)), chunk("IEND", Buffer.alloc(0))]);
|
|
1368
1384
|
}
|
|
1369
1385
|
const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
|
|
1370
1386
|
const unescape = (text) => text.replace(/&(#x[0-9a-f]+|#\d+|amp|lt|gt|quot|apos);/gi, (_, name) => name[0] === "#" ? String.fromCodePoint(name[1] === "x" || name[1] === "X" ? Number.parseInt(name.slice(2), 16) : Number(name.slice(1))) : ENTITIES[name.toLowerCase()]);
|
|
@@ -1416,7 +1432,7 @@ export function parseUiTree(xml, [width, height]) {
|
|
|
1416
1432
|
stack.push(node);
|
|
1417
1433
|
}
|
|
1418
1434
|
if (top.length === 0)
|
|
1419
|
-
return
|
|
1435
|
+
return vendorFail("phone-harness", "uiautomator's dump held no node");
|
|
1420
1436
|
if (top.length === 1)
|
|
1421
1437
|
return ok({ root: top[0], nodes, truncated });
|
|
1422
1438
|
return ok({ root: { role: "hierarchy", bounds: [0, 0, width, height], enabled: true, children: top }, nodes: nodes + 1, truncated });
|
package/dist/protocol.d.ts
CHANGED
|
@@ -55,6 +55,11 @@ export interface HelloResult {
|
|
|
55
55
|
/** The daemon's own version (with daemon none, the provider's). */
|
|
56
56
|
version: string;
|
|
57
57
|
os: ComputerOs;
|
|
58
|
+
/**
|
|
59
|
+
* The OS's own version as the computer says it: a phone's Android release (getprop ro.build.version.release), such
|
|
60
|
+
* as "15". Absent where the computer does not say one.
|
|
61
|
+
*/
|
|
62
|
+
osVersion?: string;
|
|
58
63
|
kind: ComputerKind;
|
|
59
64
|
/** Go's name for the architecture: amd64, arm64. */
|
|
60
65
|
arch: string;
|