@parcha/agentrun-computer 0.0.0-stage → 0.1.0-beta.32

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 (173) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +8 -0
  3. package/README.md +2401 -2
  4. package/box/Dockerfile +69 -0
  5. package/box/agentrun-archil +168 -0
  6. package/box/build-daytona.py +26 -0
  7. package/box/build-e2b.mjs +32 -0
  8. package/box/build-modal.py +17 -0
  9. package/dist/actions.d.ts +18 -0
  10. package/dist/actions.js +139 -0
  11. package/dist/browser.d.ts +151 -0
  12. package/dist/browser.js +305 -0
  13. package/dist/browserbase.d.ts +139 -0
  14. package/dist/browserbase.js +408 -0
  15. package/dist/child.d.ts +40 -0
  16. package/dist/child.js +127 -0
  17. package/dist/client.d.ts +22 -0
  18. package/dist/client.js +136 -0
  19. package/dist/computerd-linux-amd64 +0 -0
  20. package/dist/computerd-linux-arm64 +0 -0
  21. package/dist/conformance/archil.d.ts +68 -0
  22. package/dist/conformance/archil.js +152 -0
  23. package/dist/conformance/browserbase.d.ts +35 -0
  24. package/dist/conformance/browserbase.js +45 -0
  25. package/dist/conformance/cases/apps.d.ts +2 -0
  26. package/dist/conformance/cases/apps.js +155 -0
  27. package/dist/conformance/cases/browser.d.ts +4 -0
  28. package/dist/conformance/cases/browser.js +105 -0
  29. package/dist/conformance/cases/drift.d.ts +2 -0
  30. package/dist/conformance/cases/drift.js +64 -0
  31. package/dist/conformance/cases/exec.d.ts +2 -0
  32. package/dist/conformance/cases/exec.js +125 -0
  33. package/dist/conformance/cases/files.d.ts +2 -0
  34. package/dist/conformance/cases/files.js +294 -0
  35. package/dist/conformance/cases/health.d.ts +2 -0
  36. package/dist/conformance/cases/health.js +71 -0
  37. package/dist/conformance/cases/index.d.ts +9 -0
  38. package/dist/conformance/cases/index.js +37 -0
  39. package/dist/conformance/cases/input.d.ts +12 -0
  40. package/dist/conformance/cases/input.js +109 -0
  41. package/dist/conformance/cases/isolation.d.ts +21 -0
  42. package/dist/conformance/cases/isolation.js +92 -0
  43. package/dist/conformance/cases/lifetime.d.ts +2 -0
  44. package/dist/conformance/cases/lifetime.js +64 -0
  45. package/dist/conformance/cases/mount.d.ts +2 -0
  46. package/dist/conformance/cases/mount.js +252 -0
  47. package/dist/conformance/cases/process.d.ts +2 -0
  48. package/dist/conformance/cases/process.js +240 -0
  49. package/dist/conformance/cases/provider.d.ts +2 -0
  50. package/dist/conformance/cases/provider.js +302 -0
  51. package/dist/conformance/cases/screen.d.ts +9 -0
  52. package/dist/conformance/cases/screen.js +230 -0
  53. package/dist/conformance/cases/settings.d.ts +57 -0
  54. package/dist/conformance/cases/settings.js +142 -0
  55. package/dist/conformance/cases/touch.d.ts +2 -0
  56. package/dist/conformance/cases/touch.js +117 -0
  57. package/dist/conformance/cases/util.d.ts +7 -0
  58. package/dist/conformance/cases/util.js +8 -0
  59. package/dist/conformance/cdp.d.ts +13 -0
  60. package/dist/conformance/cdp.js +88 -0
  61. package/dist/conformance/cli.d.ts +29 -0
  62. package/dist/conformance/cli.js +231 -0
  63. package/dist/conformance/daytona-sweep.d.ts +18 -0
  64. package/dist/conformance/daytona-sweep.js +118 -0
  65. package/dist/conformance/daytona.d.ts +83 -0
  66. package/dist/conformance/daytona.js +107 -0
  67. package/dist/conformance/docker.d.ts +38 -0
  68. package/dist/conformance/docker.js +70 -0
  69. package/dist/conformance/drift-daemon.d.ts +8 -0
  70. package/dist/conformance/drift-daemon.js +41 -0
  71. package/dist/conformance/e2b-diagnose.d.ts +16 -0
  72. package/dist/conformance/e2b-diagnose.js +61 -0
  73. package/dist/conformance/e2b-sweep.d.ts +18 -0
  74. package/dist/conformance/e2b-sweep.js +125 -0
  75. package/dist/conformance/e2b.d.ts +74 -0
  76. package/dist/conformance/e2b.js +111 -0
  77. package/dist/conformance/fixture.d.ts +65 -0
  78. package/dist/conformance/fixture.js +98 -0
  79. package/dist/conformance/index.d.ts +8 -0
  80. package/dist/conformance/index.js +8 -0
  81. package/dist/conformance/kernel.d.ts +31 -0
  82. package/dist/conformance/kernel.js +40 -0
  83. package/dist/conformance/kind.d.ts +23 -0
  84. package/dist/conformance/kind.js +58 -0
  85. package/dist/conformance/modal-sweep.d.ts +22 -0
  86. package/dist/conformance/modal-sweep.js +190 -0
  87. package/dist/conformance/modal.d.ts +69 -0
  88. package/dist/conformance/modal.js +76 -0
  89. package/dist/conformance/orgo.d.ts +41 -0
  90. package/dist/conformance/orgo.js +69 -0
  91. package/dist/conformance/phone-harness.d.ts +61 -0
  92. package/dist/conformance/phone-harness.js +241 -0
  93. package/dist/conformance/png.d.ts +16 -0
  94. package/dist/conformance/png.js +132 -0
  95. package/dist/conformance/report.d.ts +2 -0
  96. package/dist/conformance/report.js +86 -0
  97. package/dist/conformance/runner.d.ts +11 -0
  98. package/dist/conformance/runner.js +585 -0
  99. package/dist/conformance/tar.d.ts +13 -0
  100. package/dist/conformance/tar.js +105 -0
  101. package/dist/conformance/types.d.ts +257 -0
  102. package/dist/conformance/types.js +24 -0
  103. package/dist/daemon-copy.d.ts +40 -0
  104. package/dist/daemon-copy.js +259 -0
  105. package/dist/daytona.d.ts +178 -0
  106. package/dist/daytona.js +1484 -0
  107. package/dist/desktop.d.ts +24 -0
  108. package/dist/desktop.js +68 -0
  109. package/dist/docker/egress-main.d.ts +1 -0
  110. package/dist/docker/egress-main.js +68 -0
  111. package/dist/docker/egress-proxy.d.ts +53 -0
  112. package/dist/docker/egress-proxy.js +450 -0
  113. package/dist/docker/egress-table.d.ts +39 -0
  114. package/dist/docker/egress-table.js +197 -0
  115. package/dist/docker/egress.d.ts +109 -0
  116. package/dist/docker/egress.js +318 -0
  117. package/dist/docker/image.d.ts +49 -0
  118. package/dist/docker/image.js +302 -0
  119. package/dist/docker/stage.d.ts +21 -0
  120. package/dist/docker/stage.js +290 -0
  121. package/dist/docker/volume.d.ts +24 -0
  122. package/dist/docker/volume.js +67 -0
  123. package/dist/docker.d.ts +150 -0
  124. package/dist/docker.js +1200 -0
  125. package/dist/drift.d.ts +200 -0
  126. package/dist/drift.js +416 -0
  127. package/dist/e2b.d.ts +335 -0
  128. package/dist/e2b.js +1515 -0
  129. package/dist/entry.d.ts +2 -0
  130. package/dist/entry.js +24 -0
  131. package/dist/files.d.ts +50 -0
  132. package/dist/files.js +208 -0
  133. package/dist/index.d.ts +27 -0
  134. package/dist/index.js +26 -0
  135. package/dist/isolation.d.ts +29 -0
  136. package/dist/isolation.js +71 -0
  137. package/dist/janitor.d.ts +50 -0
  138. package/dist/janitor.js +135 -0
  139. package/dist/kernel.d.ts +113 -0
  140. package/dist/kernel.js +358 -0
  141. package/dist/kinds.d.ts +79 -0
  142. package/dist/kinds.js +133 -0
  143. package/dist/labels.d.ts +23 -0
  144. package/dist/labels.js +50 -0
  145. package/dist/modal.d.ts +302 -0
  146. package/dist/modal.js +1374 -0
  147. package/dist/mount-archil.d.ts +164 -0
  148. package/dist/mount-archil.js +471 -0
  149. package/dist/mount.d.ts +57 -0
  150. package/dist/mount.js +104 -0
  151. package/dist/orgo-channel.d.ts +96 -0
  152. package/dist/orgo-channel.js +391 -0
  153. package/dist/orgo.d.ts +139 -0
  154. package/dist/orgo.js +1006 -0
  155. package/dist/phone-harness.d.ts +185 -0
  156. package/dist/phone-harness.js +1420 -0
  157. package/dist/processes.d.ts +24 -0
  158. package/dist/processes.js +40 -0
  159. package/dist/protocol.d.ts +729 -0
  160. package/dist/protocol.js +54 -0
  161. package/dist/provider.d.ts +245 -0
  162. package/dist/provider.js +37 -0
  163. package/dist/providers.d.ts +68 -0
  164. package/dist/providers.js +25 -0
  165. package/dist/result.d.ts +15 -0
  166. package/dist/result.js +2 -0
  167. package/dist/screen-watch.d.ts +150 -0
  168. package/dist/screen-watch.js +490 -0
  169. package/dist/test-connection.d.ts +17 -0
  170. package/dist/test-connection.js +26 -0
  171. package/dist/wait.d.ts +23 -0
  172. package/dist/wait.js +65 -0
  173. package/package.json +62 -4
@@ -0,0 +1,241 @@
1
+ // The phone harness for the conformance CLI and the live harness (`--provider phone-harness`): the provider as create()
2
+ // builds it from the settings this module reads (COMPUTER_PHONE_HARNESS_*), with its ledger in a JSON file so a sweep in
3
+ // another process sees the run's phones, and its drifted twin, whose phone's hello reports another version. A phone is
4
+ // rented by the minute, so nothing is built without AGENTRUN_COMPUTER_LIVE_PHONE_HARNESS=1, and a run's phones live at
5
+ // most 10 min.
6
+ // The provider reads no environment: this module reads these names and passes them in as options.
7
+ import { randomBytes } from "node:crypto";
8
+ import { linkSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
9
+ import { tmpdir } from "node:os";
10
+ import { dirname, join } from "node:path";
11
+ import { adbCli, create, PHONE_IMAGE } from "../phone-harness.js";
12
+ /** The environment this module reads, by name. */
13
+ export const PHONE_ENV = {
14
+ /** The phone harness API key: this module's own name, never the bare PHONE_HARNESS_API_KEY. */
15
+ key: "COMPUTER_PHONE_HARNESS_API_KEY",
16
+ /** android (the default); ios is refused until it is built. */
17
+ platform: "COMPUTER_PHONE_HARNESS_PLATFORM",
18
+ /** The adb CLI's path; default adb on PATH. */
19
+ adb: "COMPUTER_PHONE_HARNESS_ADB",
20
+ /** The ledger's JSON file; default agentrun-phone-ledger.json in the temp dir. */
21
+ ledger: "COMPUTER_PHONE_HARNESS_LEDGER",
22
+ /** 1 to rent phones at all: each one is billed by the minute. */
23
+ live: "AGENTRUN_COMPUTER_LIVE_PHONE_HARNESS",
24
+ };
25
+ /** The names this module reads, under the conformance modules' common name. */
26
+ export const ENV = PHONE_ENV;
27
+ /** A conformance run's phones live at most this long. */
28
+ const RUN_MAX_LIFETIME_MS = 10 * 60_000;
29
+ /** The provider's settings this module reads from env, each only when its name is set. */
30
+ export function settingsFromEnv(env = process.env) {
31
+ return {
32
+ ...(env[PHONE_ENV.key] ? { apiKey: env[PHONE_ENV.key] } : {}),
33
+ ...(env[PHONE_ENV.platform] ? { platform: env[PHONE_ENV.platform] } : {}),
34
+ ...(env[PHONE_ENV.adb] ? { adb: env[PHONE_ENV.adb] } : {}),
35
+ ...(env[PHONE_ENV.ledger] ? { ledgerPath: env[PHONE_ENV.ledger] } : {}),
36
+ };
37
+ }
38
+ /** How long a writer waits for a live writer's lock on the ledger. A write holds it for well under a millisecond. */
39
+ const LOCK_WAIT_MS = 10_000;
40
+ /** Whether the process pid is alive on this host: signal 0 tests it and sends nothing. */
41
+ function alive(pid) {
42
+ try {
43
+ process.kill(pid, 0);
44
+ return true;
45
+ }
46
+ catch (error) {
47
+ // EPERM: it lives, under another user.
48
+ return error.code === "EPERM";
49
+ }
50
+ }
51
+ /** A lock file's words, `<pid> <token>`, or undefined when it is gone. */
52
+ function lockWords(lockPath) {
53
+ try {
54
+ return readFileSync(lockPath, "utf8");
55
+ }
56
+ catch (error) {
57
+ if (error.code === "ENOENT")
58
+ return undefined;
59
+ throw error;
60
+ }
61
+ }
62
+ /** Whether a lock's words name a holder that is gone: a pid no process holds, or no pid at all (a file left torn). */
63
+ const leftByTheDead = (words) => {
64
+ const pid = Number(words.split(" ")[0]);
65
+ return !(Number.isSafeInteger(pid) && pid > 0) || !alive(pid);
66
+ };
67
+ /**
68
+ * Takes a file lock at lockPath, made whole with its holder's words (`<pid> <token>`): it is written to a file of its
69
+ * own, then hard-linked into place, which fails when the lock exists, so no reader ever sees it empty. Answers whether
70
+ * it was taken.
71
+ */
72
+ function take(lockPath, words) {
73
+ const temp = `${lockPath}.${randomBytes(6).toString("hex")}`;
74
+ writeFileSync(temp, words, { mode: 0o600 });
75
+ try {
76
+ linkSync(temp, lockPath);
77
+ return true;
78
+ }
79
+ catch (error) {
80
+ if (error.code !== "EEXIST")
81
+ throw error;
82
+ return false;
83
+ }
84
+ finally {
85
+ unlinkSync(temp);
86
+ }
87
+ }
88
+ /**
89
+ * Removes the lock at lockPath if it still holds `dead`, the words of a holder found gone. Breakers take turns by a
90
+ * second lock (<lock>.break, also naming its holder), and the lock is read again under it. So a lock is broken once:
91
+ * a waiter that judged the same dead lock never removes the lock another has taken since.
92
+ *
93
+ * Answers whether it moved things on: it broke the lock (or found it gone), or removed a breaker that had died. False
94
+ * when another breaker, alive, holds <lock>.break: the caller waits for it, under its deadline, as for any live holder.
95
+ */
96
+ function breakDead(lockPath, dead, words) {
97
+ const breaker = `${lockPath}.break`;
98
+ if (!take(breaker, words)) {
99
+ // Another waiter is breaking it. A breaker that died midway leaves its own lock, named by it: that one goes too.
100
+ // Two waiters may both find it dead and both remove it: the second finds it gone, and goes on.
101
+ const other = lockWords(breaker);
102
+ if (other === undefined)
103
+ return true;
104
+ if (leftByTheDead(other) && lockWords(breaker) === other) {
105
+ removeIfThere(breaker);
106
+ return true;
107
+ }
108
+ return false;
109
+ }
110
+ try {
111
+ if (lockWords(lockPath) === dead)
112
+ removeIfThere(lockPath);
113
+ }
114
+ finally {
115
+ removeIfThere(breaker);
116
+ }
117
+ return true;
118
+ }
119
+ /** Removes the file at path. One another waiter removed first is gone already, as asked: that is no failure. */
120
+ function removeIfThere(path) {
121
+ try {
122
+ unlinkSync(path);
123
+ }
124
+ catch (error) {
125
+ if (error.code !== "ENOENT")
126
+ throw error;
127
+ }
128
+ }
129
+ /**
130
+ * A ledger in a JSON file (mode 0600), read whole on each call and written whole by rename, so a reader never sees half
131
+ * a file. Writes (put, remove) are one at a time across processes: in this process through one queue, and between
132
+ * processes by a lock file beside the ledger (<path>.lock), so a write never drops another's entry.
133
+ *
134
+ * The lock names its holder (`<pid> <token>`), and is judged by that holder, not by its age. A live writer's lock is
135
+ * never taken, however old: a write waits for it, up to waitMs (10 s by default) for one holder that never lets go,
136
+ * then fails in words. A lock that keeps passing between live writers is progress, and is waited for. A lock whose
137
+ * holder is gone (it crashed mid-write) is broken at once, by one waiter.
138
+ */
139
+ export function fileLedger(path, { waitMs = LOCK_WAIT_MS } = {}) {
140
+ const lockPath = `${path}.lock`;
141
+ let queue = Promise.resolve();
142
+ /** fn under the lock, after every earlier write of this process. */
143
+ const locked = (fn) => {
144
+ const turn = queue.then(async () => {
145
+ mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
146
+ const words = `${process.pid} ${randomBytes(8).toString("hex")}\n`;
147
+ // waitMs bounds one holder that never lets go, not other writers' progress. A lock that passes from writer to
148
+ // writer (its words change) is progress: the wait starts again. Under load, writers that each hold it briefly can
149
+ // together keep it longer than waitMs, and that is no stuck writer (CI, run 42672928685).
150
+ let seen;
151
+ let until = Date.now() + waitMs;
152
+ for (let wait = 2; !take(lockPath, words); wait = Math.min(wait * 2, 50)) {
153
+ const held = lockWords(lockPath);
154
+ if (held === undefined)
155
+ continue;
156
+ if (held !== seen) {
157
+ seen = held;
158
+ until = Date.now() + waitMs;
159
+ }
160
+ // A dead holder's lock is broken at once; a live breaker at work on it is waited for, like a live holder.
161
+ if (leftByTheDead(held) && breakDead(lockPath, held, words))
162
+ continue;
163
+ if (Date.now() >= until)
164
+ throw new Error(`the phone ledger ${path} stayed locked by another writer for ${waitMs / 1000} s, which held it all that time (${lockPath})`);
165
+ await new Promise((resolve) => setTimeout(resolve, wait));
166
+ }
167
+ try {
168
+ return fn();
169
+ }
170
+ finally {
171
+ // Only its own lock: never one a breaker let another take.
172
+ if (lockWords(lockPath) === words)
173
+ removeIfThere(lockPath);
174
+ }
175
+ });
176
+ queue = turn.catch(() => undefined);
177
+ return turn;
178
+ };
179
+ const read = () => {
180
+ try {
181
+ const parsed = JSON.parse(readFileSync(path, "utf8"));
182
+ return typeof parsed === "object" && parsed !== null ? parsed : {};
183
+ }
184
+ catch (error) {
185
+ if (error.code === "ENOENT")
186
+ return {};
187
+ throw new Error(`the phone ledger ${path} is unreadable: ${error.message}`);
188
+ }
189
+ };
190
+ const write = (entries) => {
191
+ mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
192
+ const temp = `${path}.${randomBytes(4).toString("hex")}.tmp`;
193
+ writeFileSync(temp, `${JSON.stringify(entries, null, 2)}\n`, { mode: 0o600 });
194
+ renameSync(temp, path);
195
+ };
196
+ return {
197
+ put: (entry) => locked(() => write({ ...read(), [entry.id]: entry })),
198
+ get: (id) => read()[id],
199
+ list: () => Object.values(read()),
200
+ remove: (id) => locked(() => {
201
+ const entries = read();
202
+ if (!(id in entries))
203
+ return;
204
+ delete entries[id];
205
+ write(entries);
206
+ }),
207
+ };
208
+ }
209
+ function optionsOf({ env = process.env, adb, ...rest }) {
210
+ if (env[PHONE_ENV.live] !== "1")
211
+ throw new Error(`a conformance run rents a cloud phone, billed by the minute: set ${PHONE_ENV.live}=1 to run it`);
212
+ const settings = settingsFromEnv(env);
213
+ if (!settings.apiKey)
214
+ throw new Error(`set ${PHONE_ENV.key} to the phone harness API key`);
215
+ return {
216
+ apiKey: settings.apiKey,
217
+ ...(settings.platform ? { platform: settings.platform } : {}),
218
+ // Every adb the run spawns, from a path given as the adb option or by its setting, starts from this environment
219
+ // with every adb variable dropped (adbCli's env), and this module's own names too: the API key has no business in
220
+ // adb's environment. A transport given as the option is used as it is: it spawns nothing of this module's.
221
+ adb: adb !== undefined && typeof adb !== "string" ? adb : adbCli(adb ?? settings.adb ?? "adb", { env: Object.fromEntries(Object.entries(env).filter(([name]) => !Object.values(PHONE_ENV).includes(name))) }),
222
+ ledger: fileLedger(settings.ledgerPath ?? join(tmpdir(), "agentrun-phone-ledger.json")),
223
+ maxLifetimeMs: RUN_MAX_LIFETIME_MS,
224
+ ...rest,
225
+ };
226
+ }
227
+ /** The provider the CLI runs. */
228
+ export async function createProvider(options = {}) {
229
+ return create(optionsOf(options));
230
+ }
231
+ /** The same provider, whose phone's hello reports another version: connect refuses it, for the drift.version case. */
232
+ export async function createDriftedProvider(options = {}) {
233
+ return create({ ...optionsOf(options), daemonVersion: "0.0.0-drift" });
234
+ }
235
+ /** The image a run names: a cloud Android has none to choose, so it is read back as this name. */
236
+ export function conformanceImage() {
237
+ return PHONE_IMAGE;
238
+ }
239
+ export const conformanceNotes = [
240
+ "phone-harness: a temporary cloud Android emulator, driven over adb; a phone is billed by the minute, and lives at most 10 min in a run",
241
+ ];
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The most bytes a PNG this reader accepts may take: every pixel as RGBA with a filter byte per row, stored with no
3
+ * compression, plus 4 MiB for zlib's and the chunks' framing. A reader of a shot stops there.
4
+ */
5
+ export declare const MAX_PNG_BYTES: number;
6
+ export interface Png {
7
+ width: number;
8
+ height: number;
9
+ /** The pixel's red, green and blue (alpha dropped). */
10
+ rgb(x: number, y: number): [number, number, number];
11
+ }
12
+ /**
13
+ * Reads a PNG whole: its header, then every pixel. Throws in words when the bytes are not a PNG, when IHDR is out of
14
+ * bounds or a format it does not read, and when the pixels do not inflate to exactly the size IHDR gives or do not decode.
15
+ */
16
+ export declare function readPng(bytes: Uint8Array): Png;
@@ -0,0 +1,132 @@
1
+ // Enough of PNG for the screen cases, with no dependency: the size from IHDR and every pixel, decoded when the bytes are
2
+ // read, so a shot whose pixels do not decode is refused at once, not when a pixel is first asked for. It reads 8-bit,
3
+ // non-interlaced images (gray, RGB, palette, gray and alpha, RGBA) and refuses any other in words. What it inflates is
4
+ // held to the exact size IHDR gives, and IHDR to the bounds below, so a hostile PNG is refused, never inflated.
5
+ import { inflateSync } from "node:zlib";
6
+ const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
7
+ /** Channels per pixel, by color type: gray, RGB, palette, gray and alpha, RGBA. */
8
+ const CHANNELS = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
9
+ /**
10
+ * The largest image read: 16384 pixels a side (the largest texture or canvas a screen is drawn to on common GPUs and
11
+ * browsers) and 2^25 pixels in all (an 8K display, 7680x4320, fits). At most 128 MiB of RGBA pixels, plus a filter byte
12
+ * per row: a screenshot is never larger, and a larger IHDR is refused before anything is inflated.
13
+ */
14
+ const MAX_SIDE = 16_384;
15
+ const MAX_PIXELS = 2 ** 25;
16
+ /**
17
+ * The most bytes a PNG this reader accepts may take: every pixel as RGBA with a filter byte per row, stored with no
18
+ * compression, plus 4 MiB for zlib's and the chunks' framing. A reader of a shot stops there.
19
+ */
20
+ export const MAX_PNG_BYTES = MAX_PIXELS * 4 + MAX_SIDE + 4 * 1024 * 1024;
21
+ /** Undoes each row's filter: raw is height rows of a filter byte and stride bytes; bpp is bytes per pixel. */
22
+ function unfilter(raw, width, height, bpp) {
23
+ const stride = width * bpp;
24
+ const out = Buffer.alloc(height * stride);
25
+ for (let y = 0; y < height; y++) {
26
+ const filter = raw[y * (stride + 1)];
27
+ const line = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
28
+ const row = out.subarray(y * stride, (y + 1) * stride);
29
+ const up = y > 0 ? out.subarray((y - 1) * stride, y * stride) : undefined;
30
+ if (filter === undefined || filter > 4)
31
+ throw new Error(`the screenshot's PNG has filter ${filter} on row ${y}`);
32
+ for (let i = 0; i < stride; i++) {
33
+ const a = i >= bpp ? row[i - bpp] : 0;
34
+ const b = up ? up[i] : 0;
35
+ const c = up && i >= bpp ? up[i - bpp] : 0;
36
+ let predicted;
37
+ if (filter === 0)
38
+ predicted = 0;
39
+ else if (filter === 1)
40
+ predicted = a;
41
+ else if (filter === 2)
42
+ predicted = b;
43
+ else if (filter === 3)
44
+ predicted = (a + b) >> 1;
45
+ else {
46
+ const p = a + b - c;
47
+ const pa = Math.abs(p - a);
48
+ const pb = Math.abs(p - b);
49
+ const pc = Math.abs(p - c);
50
+ predicted = pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
51
+ }
52
+ row[i] = (line[i] + predicted) & 0xff;
53
+ }
54
+ }
55
+ return out;
56
+ }
57
+ /**
58
+ * Reads a PNG whole: its header, then every pixel. Throws in words when the bytes are not a PNG, when IHDR is out of
59
+ * bounds or a format it does not read, and when the pixels do not inflate to exactly the size IHDR gives or do not decode.
60
+ */
61
+ export function readPng(bytes) {
62
+ const data = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
63
+ if (data.length < 33 || !data.subarray(0, 8).equals(SIGNATURE))
64
+ throw new Error("the screenshot is not a PNG: it lacks the PNG signature");
65
+ if (data.toString("latin1", 12, 16) !== "IHDR")
66
+ throw new Error("the screenshot's PNG does not start with IHDR");
67
+ const width = data.readUInt32BE(16);
68
+ const height = data.readUInt32BE(20);
69
+ const depth = data[24];
70
+ const colorType = data[25];
71
+ const interlace = data[28];
72
+ if (width < 1 || height < 1 || width > MAX_SIDE || height > MAX_SIDE || width * height > MAX_PIXELS) {
73
+ throw new Error(`the screenshot's PNG is ${width}x${height}: the suite reads 1 to ${MAX_SIDE} pixels a side and at most ${MAX_PIXELS} pixels`);
74
+ }
75
+ const channels = CHANNELS[colorType];
76
+ if (depth !== 8 || channels === undefined || interlace !== 0) {
77
+ throw new Error(`the screenshot's PNG is bit depth ${depth}, color type ${colorType}, interlace ${interlace}: the suite reads 8-bit, non-interlaced images`);
78
+ }
79
+ const idat = [];
80
+ let palette;
81
+ for (let at = 8; at + 8 <= data.length;) {
82
+ const length = data.readUInt32BE(at);
83
+ const type = data.toString("latin1", at + 4, at + 8);
84
+ const body = data.subarray(at + 8, at + 8 + length);
85
+ if (type === "IDAT")
86
+ idat.push(body);
87
+ else if (type === "PLTE")
88
+ palette = body;
89
+ else if (type === "IEND")
90
+ break;
91
+ at += 12 + length;
92
+ }
93
+ if (idat.length === 0)
94
+ throw new Error("the screenshot's PNG has no IDAT");
95
+ // The exact size of the filtered pixels: a filter byte and width * channels bytes per row, at 8 bits a channel.
96
+ const expected = height * (width * channels + 1);
97
+ let raw;
98
+ try {
99
+ raw = inflateSync(Buffer.concat(idat), { maxOutputLength: expected });
100
+ }
101
+ catch (error) {
102
+ if (error.code === "ERR_BUFFER_TOO_LARGE")
103
+ throw new Error(`the screenshot's PNG inflates past the ${expected} bytes its IHDR gives`);
104
+ throw new Error(`the screenshot's PNG does not inflate: ${error instanceof Error ? error.message : String(error)}`);
105
+ }
106
+ if (raw.length !== expected)
107
+ throw new Error(`the screenshot's PNG holds ${raw.length} bytes of pixels, not the ${expected} its IHDR gives`);
108
+ const pixels = unfilter(raw, width, height, channels);
109
+ if (colorType === 3) {
110
+ const entries = palette === undefined ? 0 : Math.floor(palette.length / 3);
111
+ const highest = pixels.reduce((max, index) => Math.max(max, index), 0);
112
+ if (highest >= entries)
113
+ throw new Error(`the screenshot's PNG has no palette entry ${highest}: its palette has ${entries}`);
114
+ }
115
+ return {
116
+ width,
117
+ height,
118
+ rgb(x, y) {
119
+ if (!(Number.isInteger(x) && Number.isInteger(y) && x >= 0 && y >= 0 && x < width && y < height))
120
+ throw new Error(`pixel (${x}, ${y}) is outside the ${width}x${height} screenshot`);
121
+ const at = (y * width + x) * channels;
122
+ if (colorType === 3) {
123
+ const index = pixels[at] * 3;
124
+ const entry = palette;
125
+ return [entry[index], entry[index + 1], entry[index + 2]];
126
+ }
127
+ if (channels <= 2)
128
+ return [pixels[at], pixels[at], pixels[at]];
129
+ return [pixels[at], pixels[at + 1], pixels[at + 2]];
130
+ },
131
+ };
132
+ }
@@ -0,0 +1,2 @@
1
+ import type { ConformanceReport } from "./types.js";
2
+ export declare function formatReport(report: ConformanceReport): string;
@@ -0,0 +1,86 @@
1
+ const GROUPS = ["health", "exec", "files", "process", "provider", "drift", "isolation", "mount", "screen", "input", "touch", "apps", "lifetime", "browser"];
2
+ const NAMES_WIDTH = 44;
3
+ const INDENT = " ".repeat("core ".length + "provider ".length + 1);
4
+ /** The case names, wrapped to the column's width: the first line, then the rest. */
5
+ function wrap(names) {
6
+ const lines = [];
7
+ for (const name of names) {
8
+ const last = lines.at(-1);
9
+ if (last !== undefined && last.length + 1 + name.length <= NAMES_WIDTH)
10
+ lines[lines.length - 1] = `${last} ${name}`;
11
+ else
12
+ lines.push(name);
13
+ }
14
+ return lines;
15
+ }
16
+ export function formatReport(report) {
17
+ const { daemon, results, cleanup } = report;
18
+ const answers = report.daemonMode === "none" ? "the provider" : "computerd";
19
+ const reached = daemon ? `${answers} ${daemon.version} ${daemon.os}/${daemon.arch}` : report.daemonMode === "none" ? "no computer reached" : "no daemon reached";
20
+ const lines = [`computer conformance · ${report.provider} · ${report.kind}, daemon ${report.daemonMode} · ${reached}`];
21
+ // A Core case skipped for an optional group the computer does not declare is not Core for this kind of computer (a
22
+ // linux computer is never held to screen): it is listed as optional, out of Core's lines and count.
23
+ const optional = (r) => r.status === "skip" && r.undeclared !== undefined;
24
+ const levelOf = (r) => (r.level === "core" && optional(r) ? "optional" : r.level);
25
+ for (const level of ["core", "full", "optional"]) {
26
+ for (const group of GROUPS) {
27
+ const here = results.filter((r) => levelOf(r) === level && r.group === group);
28
+ if (here.length === 0)
29
+ continue;
30
+ const count = (status) => here.filter((r) => r.status === status).length;
31
+ const counts = [
32
+ count("pass") > 0 ? `✓ ${count("pass")}` : "",
33
+ count("fail") > 0 ? `✗ ${count("fail")}` : "",
34
+ count("skip") > 0 ? `- ${count("skip")}` : "",
35
+ ]
36
+ .filter(Boolean)
37
+ .join(" ");
38
+ const [first = "", ...rest] = wrap(here.map((r) => r.id.slice(group.length + 1)));
39
+ lines.push(`${level} ${group.padEnd(9)} ${first.padEnd(NAMES_WIDTH)} ${counts}`, ...rest.map((more) => `${INDENT}${more}`));
40
+ for (const r of here)
41
+ if (r.status !== "pass")
42
+ lines.push(` ${r.status === "fail" ? "✗" : "-"} ${r.id}: ${r.reason ?? ""}`);
43
+ }
44
+ }
45
+ if (cleanup.late.length > 0)
46
+ lines.push(`cleanup - deleted after their case ended: ${cleanup.late.join(", ")}`);
47
+ if (cleanup.left.length > 0 || cleanup.errors.length > 0) {
48
+ lines.push(`cleanup ✗ ${cleanup.left.length > 0 ? `left ${cleanup.left.join(", ")}` : "could not show nothing was left"}`);
49
+ for (const error of cleanup.errors)
50
+ lines.push(` ✗ ${error}`);
51
+ }
52
+ const notRun = report.coreNotRun ?? [];
53
+ if (notRun.length > 0) {
54
+ const [first = "", ...rest] = wrap(notRun);
55
+ lines.push(`core ${"not run".padEnd(9)} ${first}`, ...rest.map((more) => `${INDENT}${more}`));
56
+ }
57
+ for (const note of report.notes ?? [])
58
+ lines.push(`note: ${note}`);
59
+ for (const { computer, note } of report.computerNotes ?? [])
60
+ lines.push(`note ${computer}: ${note}`);
61
+ // What a provider said through onNote, such as a release whose final sync or checkin did not go cleanly.
62
+ for (const { computer, note } of report.providerNotes ?? [])
63
+ lines.push(`provider note ${computer}: ${note}`);
64
+ const core = results.filter((r) => levelOf(r) === "core");
65
+ const notDeclared = results.filter((r) => levelOf(r) === "optional").length;
66
+ const full = results.filter((r) => r.level === "full");
67
+ const passed = (rs) => rs.filter((r) => r.status === "pass").length;
68
+ // A run that left Core cases out has no Core verdict: PARTIAL, never PASS.
69
+ let verdict = `core ${report.core.toUpperCase()} ${passed(core)}/${core.length}`;
70
+ if (notDeclared > 0)
71
+ verdict += ` · ${notDeclared} optional ${notDeclared === 1 ? "case" : "cases"} skipped, not declared`;
72
+ if (notRun.length > 0)
73
+ verdict += ` · ${notRun.length} Core ${notRun.length === 1 ? "case" : "cases"} not run`;
74
+ verdict += full.length > 0 ? ` · full ${passed(full)}/${full.length}` : " · full: none run";
75
+ if (report.undeclared.length > 0)
76
+ verdict += ` · ${report.undeclared.join(" ")} not declared`;
77
+ if (report.only)
78
+ verdict += ` · only ${report.only.join(",")}`;
79
+ lines.push(verdict);
80
+ // A run by kind ends on the kind's verdict, which is what it was for.
81
+ if (report.kindVerdict) {
82
+ const { kind, verdict: outcome, reasons } = report.kindVerdict;
83
+ lines.push(outcome === "pass" ? `kind ${kind} PASS` : `kind ${kind} FAIL: ${reasons.join("; ")}`);
84
+ }
85
+ return `${lines.join("\n")}\n`;
86
+ }
@@ -0,0 +1,11 @@
1
+ import type { ComputerProvider } from "../provider.js";
2
+ import { type ConformanceOptions, type ConformanceReport } from "./types.js";
3
+ /** The label every computer of a run carries, with the run's id. */
4
+ export declare const RUN_LABEL = "agentrun.conformance";
5
+ /**
6
+ * Runs the cases against the provider and reports each one. Every computer carries the run's label; each one a case
7
+ * makes is deleted when the case ends, the shared one when the run ends, then a janitor provider is swept by the label
8
+ * and the label is listed to show nothing is left. Every provider call is bounded: one that never answers fails its case
9
+ * or is named in the cleanup, and a computer that arrives after its case ended is deleted when it arrives.
10
+ */
11
+ export declare function runConformance(provider: ComputerProvider, options: ConformanceOptions): Promise<ConformanceReport>;