@parcha/agentrun-serve 0.1.0-beta.36 → 0.1.0-beta.37

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 (325) hide show
  1. package/CHANGELOG.md +170 -0
  2. package/README.md +158 -52
  3. package/dist/attach-conformance-cli.d.ts +1 -0
  4. package/dist/attach-conformance-cli.js +18 -0
  5. package/dist/attach-conformance.d.ts +25 -0
  6. package/dist/attach-conformance.js +343 -0
  7. package/dist/browser-cost.d.ts +1 -1
  8. package/dist/browser-live.d.ts +20 -8
  9. package/dist/browser-live.js +50 -27
  10. package/dist/cap.d.ts +0 -17
  11. package/dist/cap.js +0 -91
  12. package/dist/catalog.d.ts +1 -12
  13. package/dist/catalog.js +3 -33
  14. package/dist/cli.js +13 -4
  15. package/dist/composio-preset.d.ts +36 -0
  16. package/dist/composio-preset.js +179 -0
  17. package/dist/computer-attach.d.ts +125 -0
  18. package/dist/computer-attach.js +330 -0
  19. package/dist/computer-calls.d.ts +48 -0
  20. package/dist/computer-calls.js +126 -0
  21. package/dist/computer-channel.d.ts +57 -0
  22. package/dist/computer-channel.js +115 -0
  23. package/dist/computer-commands.d.ts +1 -1
  24. package/dist/computer-held-routes.d.ts +35 -0
  25. package/dist/computer-held-routes.js +106 -0
  26. package/dist/computer-held.d.ts +48 -0
  27. package/dist/computer-held.js +87 -0
  28. package/dist/computer-keys.d.ts +21 -1
  29. package/dist/computer-keys.js +53 -11
  30. package/dist/computer-live.d.ts +160 -0
  31. package/dist/computer-live.js +643 -0
  32. package/dist/computer-view.d.ts +13 -0
  33. package/dist/computer-view.js +50 -3
  34. package/dist/computer-viewer.d.ts +6 -0
  35. package/dist/computer-viewer.js +297 -0
  36. package/dist/computers.d.ts +33 -7
  37. package/dist/computers.js +50 -8
  38. package/dist/console/assets/index-Cn6JX1uH.js +563 -0
  39. package/dist/console/assets/index-DRN62V5v.css +1 -0
  40. package/dist/console/index.html +2 -2
  41. package/dist/faux-computers.d.ts +9 -20
  42. package/dist/faux-computers.js +53 -25
  43. package/dist/github-connection.d.ts +10 -2
  44. package/dist/github-connection.js +65 -2
  45. package/dist/github-faux.js +1 -1
  46. package/dist/graph-view.d.ts +1 -1
  47. package/dist/harness.d.ts +1 -0
  48. package/dist/harness.js +35 -1
  49. package/dist/host-tools.d.ts +1 -1
  50. package/dist/hosted-search.d.ts +5 -4
  51. package/dist/hosted-search.js +2 -7
  52. package/dist/import-sources.d.ts +10 -0
  53. package/dist/import-sources.js +44 -2
  54. package/dist/import.d.ts +3 -3
  55. package/dist/import.js +37 -6
  56. package/dist/learn/check.d.ts +4 -1
  57. package/dist/learn/check.js +1 -1
  58. package/dist/learn/commit.d.ts +4 -2
  59. package/dist/learn/commit.js +5 -5
  60. package/dist/learn/folder.d.ts +6 -0
  61. package/dist/learn/folder.js +4 -1
  62. package/dist/learn/general.d.ts +66 -0
  63. package/dist/learn/general.js +73 -0
  64. package/dist/learn/grade.d.ts +4 -0
  65. package/dist/learn/grade.js +22 -1
  66. package/dist/learn/reflect.d.ts +10 -1
  67. package/dist/learn/reflect.js +14 -4
  68. package/dist/learn/skills.d.ts +33 -5
  69. package/dist/learn/skills.js +53 -7
  70. package/dist/learned-workflow.d.ts +24 -0
  71. package/dist/learned-workflow.js +66 -3
  72. package/dist/learning-batch.d.ts +283 -0
  73. package/dist/learning-batch.js +574 -0
  74. package/dist/learning-evals.d.ts +33 -0
  75. package/dist/learning-evals.js +98 -0
  76. package/dist/learning-routes.d.ts +25 -2
  77. package/dist/learning-routes.js +39 -6
  78. package/dist/learning-words.d.ts +6 -0
  79. package/dist/learning-words.js +6 -0
  80. package/dist/learning.d.ts +28 -15
  81. package/dist/learning.js +84 -44
  82. package/dist/live/stream.js +6 -3
  83. package/dist/live/translate.d.ts +4 -3
  84. package/dist/live/translate.js +8 -4
  85. package/dist/live-reach.d.ts +37 -0
  86. package/dist/live-reach.js +62 -0
  87. package/dist/mcp-gateway.d.ts +9 -0
  88. package/dist/mcp-gateway.js +60 -11
  89. package/dist/mcp-oauth.d.ts +57 -7
  90. package/dist/mcp-oauth.js +133 -18
  91. package/dist/mcp-private-hosts.d.ts +4 -4
  92. package/dist/mcp-private-hosts.js +20 -12
  93. package/dist/mcp-probe.d.ts +19 -2
  94. package/dist/mcp-probe.js +44 -9
  95. package/dist/mcp-secret-writes.js +4 -0
  96. package/dist/mcp-servers-store.d.ts +38 -15
  97. package/dist/mcp-servers-store.js +112 -20
  98. package/dist/mcp-store.d.ts +10 -4
  99. package/dist/mcp-store.js +17 -6
  100. package/dist/model-form.d.ts +26 -0
  101. package/dist/model-form.js +80 -0
  102. package/dist/model.js +23 -10
  103. package/dist/openapi.js +386 -21
  104. package/dist/page.d.ts +18 -0
  105. package/dist/page.js +48 -20
  106. package/dist/paid-calls.d.ts +1 -1
  107. package/dist/place-offers.d.ts +2 -3
  108. package/dist/place-offers.js +3 -7
  109. package/dist/place.js +1 -2
  110. package/dist/print.d.ts +15 -1
  111. package/dist/print.js +14 -3
  112. package/dist/propose.d.ts +3 -0
  113. package/dist/propose.js +13 -5
  114. package/dist/provider.js +3 -2
  115. package/dist/reflect-range.d.ts +8 -1
  116. package/dist/reflect-range.js +1 -1
  117. package/dist/run-launch.d.ts +14 -2
  118. package/dist/run-launch.js +9 -4
  119. package/dist/run-learning-served.d.ts +2 -2
  120. package/dist/run-reads-over-store.d.ts +1 -1
  121. package/dist/run-repo.d.ts +20 -0
  122. package/dist/run-repo.js +63 -0
  123. package/dist/run-timeline-served.d.ts +1 -1
  124. package/dist/run-view.d.ts +3 -3
  125. package/dist/run-view.js +14 -7
  126. package/dist/runner/adapter-loop.d.ts +23 -7
  127. package/dist/runner/adapter-loop.js +648 -62
  128. package/dist/runner/adapter-run-spec.d.ts +11 -3
  129. package/dist/runner/adapter-run-spec.js +77 -22
  130. package/dist/runner/adapters/pi-computer.d.ts +7 -0
  131. package/dist/runner/adapters/pi-computer.js +43 -0
  132. package/dist/runner/adapters/pi.d.ts +9 -4
  133. package/dist/runner/adapters/pi.js +76 -14
  134. package/dist/runner/beside.d.ts +7 -3
  135. package/dist/runner/beside.js +21 -11
  136. package/dist/runner/claim-secrets.d.ts +4 -0
  137. package/dist/runner/claim-secrets.js +6 -1
  138. package/dist/runner/cli-run-spec.d.ts +20 -3
  139. package/dist/runner/cli-run-spec.js +113 -43
  140. package/dist/runner/commands.d.ts +3 -1
  141. package/dist/runner/commands.js +28 -3
  142. package/dist/runner/computer-channel.d.ts +34 -0
  143. package/dist/runner/computer-channel.js +61 -0
  144. package/dist/runner/conformance/harness.js +80 -14
  145. package/dist/runner/conformance/subject.d.ts +1 -1
  146. package/dist/runner/dispatch.js +3 -0
  147. package/dist/runner/end-of-run.d.ts +22 -12
  148. package/dist/runner/end-of-run.js +38 -16
  149. package/dist/runner/github-app.d.ts +2 -1
  150. package/dist/runner/github-app.js +4 -2
  151. package/dist/runner/harness-loops.js +15 -1
  152. package/dist/runner/held-run.d.ts +65 -0
  153. package/dist/runner/held-run.js +158 -0
  154. package/dist/runner/journal-kinds.d.ts +20 -1
  155. package/dist/runner/journal-kinds.js +14 -0
  156. package/dist/runner/journal-reader.d.ts +5 -4
  157. package/dist/runner/journal-reader.js +12 -10
  158. package/dist/runner/learning-ask.d.ts +7 -1
  159. package/dist/runner/learning-ask.js +21 -6
  160. package/dist/runner/learning-dispatch.d.ts +22 -0
  161. package/dist/runner/learning-dispatch.js +77 -10
  162. package/dist/runner/learning-end.d.ts +22 -1
  163. package/dist/runner/learning-end.js +92 -12
  164. package/dist/runner/learning-factory.d.ts +1 -1
  165. package/dist/runner/learning-factory.js +11 -5
  166. package/dist/runner/learning-loop.d.ts +67 -7
  167. package/dist/runner/learning-loop.js +173 -21
  168. package/dist/runner/learning-seed.d.ts +7 -0
  169. package/dist/runner/learning-seed.js +13 -0
  170. package/dist/runner/live-ticker.js +1 -0
  171. package/dist/runner/loop-contract.d.ts +8 -1
  172. package/dist/runner/loop.d.ts +5 -1
  173. package/dist/runner/loop.js +2 -1
  174. package/dist/runner/main.d.ts +13 -1
  175. package/dist/runner/main.js +27 -5
  176. package/dist/runner/mcp-claim.d.ts +10 -7
  177. package/dist/runner/mcp-claim.js +113 -24
  178. package/dist/runner/mcp-github.js +1 -1
  179. package/dist/runner/mcp-run.d.ts +5 -5
  180. package/dist/runner/mcp-run.js +15 -3
  181. package/dist/runner/metrics.d.ts +11 -2
  182. package/dist/runner/metrics.js +12 -2
  183. package/dist/runner/pr-checkout.d.ts +1 -0
  184. package/dist/runner/pr-checkout.js +1 -1
  185. package/dist/runner/reflect-hook.d.ts +18 -0
  186. package/dist/runner/reflect-hook.js +20 -8
  187. package/dist/runner/runner.d.ts +26 -0
  188. package/dist/runner/runner.js +184 -15
  189. package/dist/runner/scratch-sweep.d.ts +13 -0
  190. package/dist/runner/scratch-sweep.js +62 -0
  191. package/dist/runner/sessions.d.ts +1 -1
  192. package/dist/runner/store-port.d.ts +4 -2
  193. package/dist/runner/store-secrets.js +4 -3
  194. package/dist/runner/subagent.js +3 -1
  195. package/dist/runner/workflow-host.d.ts +6 -1
  196. package/dist/runner/workflow-host.js +1 -1
  197. package/dist/runner/workflow-loop.d.ts +1 -1
  198. package/dist/runner/workflow-spec.d.ts +14 -2
  199. package/dist/runner/workflow-spec.js +56 -20
  200. package/dist/runs-over-store.d.ts +6 -1
  201. package/dist/runs-over-store.js +38 -9
  202. package/dist/seal-key.js +3 -3
  203. package/dist/sealed-settings-store.d.ts +5 -2
  204. package/dist/sealed-settings-store.js +10 -4
  205. package/dist/secret-writes.d.ts +5 -4
  206. package/dist/secret-writes.js +5 -4
  207. package/dist/server-secrets-store.d.ts +4 -3
  208. package/dist/server-secrets-store.js +15 -9
  209. package/dist/server-secrets.js +3 -2
  210. package/dist/server.d.ts +43 -14
  211. package/dist/server.js +472 -126
  212. package/dist/settings-wake.d.ts +21 -0
  213. package/dist/settings-wake.js +59 -0
  214. package/dist/standard-webhooks.d.ts +4 -1
  215. package/dist/standard-webhooks.js +8 -4
  216. package/dist/store/command-wake.d.ts +12 -0
  217. package/dist/store/command-wake.js +37 -0
  218. package/dist/store/dialect.d.ts +6 -0
  219. package/dist/store/dialect.js +5 -0
  220. package/dist/store/index.d.ts +1 -1
  221. package/dist/store/journal/storage.d.ts +3 -2
  222. package/dist/store/journal/storage.js +13 -5
  223. package/dist/store/postgres/dialect.js +4 -0
  224. package/dist/store/postgres/migrations/0005_learning_rounds.sql +14 -0
  225. package/dist/store/postgres/migrations/0006_run_changes_channel_attempts.sql +57 -0
  226. package/dist/store/postgres/migrations/0007_viewer_host.sql +11 -0
  227. package/dist/store/postgres/migrations/0008_paired_computers.sql +30 -0
  228. package/dist/store/postgres/migrations/0009_commands_answer.sql +10 -0
  229. package/dist/store/schema.d.ts +1 -1
  230. package/dist/store/schema.js +77 -8
  231. package/dist/store/sqlite/run-patch.d.ts +7 -5
  232. package/dist/store/sqlite/run-patch.js +28 -13
  233. package/dist/store/sqlite/store.js +272 -54
  234. package/dist/store/store.d.ts +268 -9
  235. package/dist/store/store.js +12 -1
  236. package/dist/store-reads/cap.d.ts +1 -1
  237. package/dist/store-reads/cap.js +8 -1
  238. package/dist/store-reads/decisions.d.ts +2 -2
  239. package/dist/store-reads/decisions.js +1 -1
  240. package/dist/store-reads/import-summary.d.ts +1 -1
  241. package/dist/store-reads/journal.d.ts +5 -4
  242. package/dist/store-reads/journal.js +73 -15
  243. package/dist/store-reads/learning.d.ts +20 -4
  244. package/dist/store-reads/learning.js +74 -18
  245. package/dist/store-reads/runs.d.ts +1 -1
  246. package/dist/store-reads/workflow.d.ts +1 -1
  247. package/dist/store-view.d.ts +5 -0
  248. package/dist/store-view.js +26 -11
  249. package/dist/template-eval.js +6 -4
  250. package/dist/template-github-permissions.d.ts +22 -8
  251. package/dist/template-github-permissions.js +35 -15
  252. package/dist/timeline/timeline.d.ts +27 -0
  253. package/dist/timeline/timeline.js +10 -9
  254. package/dist/timeline/util.d.ts +4 -0
  255. package/dist/timeline/util.js +14 -0
  256. package/dist/tool-budget.d.ts +19 -7
  257. package/dist/tool-budget.js +14 -8
  258. package/dist/tool-gate.d.ts +16 -9
  259. package/dist/tool-gate.js +53 -42
  260. package/dist/tool-plane.d.ts +16 -12
  261. package/dist/tool-plane.js +9 -4
  262. package/dist/tools.d.ts +13 -4
  263. package/dist/tools.js +13 -4
  264. package/dist/trigger-adapter.d.ts +4 -0
  265. package/dist/trigger-secrets-shown.d.ts +2 -2
  266. package/dist/trigger-secrets-shown.js +1 -1
  267. package/dist/trigger-signatures.d.ts +25 -6
  268. package/dist/trigger-signatures.js +41 -7
  269. package/dist/trigger-targets.d.ts +2 -0
  270. package/dist/trigger-targets.js +1 -0
  271. package/dist/trigger-webhook.js +5 -2
  272. package/dist/triggers.d.ts +7 -3
  273. package/dist/triggers.js +35 -5
  274. package/dist/turns.js +16 -1
  275. package/dist/validate.js +11 -1
  276. package/dist/web-fetch.d.ts +1 -1
  277. package/dist/web-search.d.ts +10 -5
  278. package/dist/web-search.js +7 -6
  279. package/dist/webhooks.d.ts +145 -102
  280. package/dist/webhooks.js +559 -322
  281. package/dist/workflow/decisions.d.ts +1 -1
  282. package/dist/workflow/node-runner.d.ts +2 -1
  283. package/dist/workflow/node.d.ts +2 -1
  284. package/dist/workflow/node.js +3 -1
  285. package/dist/workflow/pi-versions.d.ts +23 -0
  286. package/dist/workflow/pi-versions.js +57 -0
  287. package/dist/workflow/record-tool.d.ts +2 -1
  288. package/dist/workflow/store.d.ts +1 -1
  289. package/dist/workflow/task.js +3 -2
  290. package/dist/workflow-run.d.ts +1 -1
  291. package/dist/workspace/attached-computers.d.ts +41 -0
  292. package/dist/workspace/attached-computers.js +114 -0
  293. package/dist/workspace/computer-deps.d.ts +16 -9
  294. package/dist/workspace/computer-deps.js +19 -7
  295. package/dist/workspace/computer-for-claim.d.ts +17 -8
  296. package/dist/workspace/computer-for-claim.js +62 -21
  297. package/dist/workspace/computer-hold.d.ts +11 -27
  298. package/dist/workspace/computer-hold.js +36 -56
  299. package/dist/workspace/computer-ledger.d.ts +3 -1
  300. package/dist/workspace/computer-ledger.js +4 -0
  301. package/dist/workspace/computer-mcp.d.ts +25 -0
  302. package/dist/workspace/computer-mcp.js +41 -0
  303. package/dist/workspace/computer-ports.d.ts +4 -13
  304. package/dist/workspace/computer-ports.js +4 -107
  305. package/dist/workspace/computer-script.d.ts +37 -0
  306. package/dist/workspace/computer-script.js +138 -0
  307. package/dist/workspace/executors.d.ts +1 -1
  308. package/dist/workspace/index.d.ts +4 -2
  309. package/dist/workspace/index.js +3 -1
  310. package/dist/workspace/kind-skills/desktop/SKILL.md +119 -84
  311. package/dist/workspace/kind-skills/phone/SKILL.md +100 -85
  312. package/dist/workspace/resolve-computer.d.ts +3 -0
  313. package/dist/workspace/resolve-computer.js +8 -2
  314. package/dist/workspace/run-tools.d.ts +32 -15
  315. package/dist/workspace/run-tools.js +23 -30
  316. package/package.json +19 -16
  317. package/templates/market-monitor/agent.md +1 -1
  318. package/dist/console/assets/index-BI4Byidu.js +0 -550
  319. package/dist/console/assets/index-C19gUF2n.css +0 -1
  320. package/dist/run-db.d.ts +0 -53
  321. package/dist/run-db.js +0 -116
  322. package/dist/workflow/readers.d.ts +0 -36
  323. package/dist/workflow/readers.js +0 -145
  324. package/dist/workflow/wal.d.ts +0 -11
  325. package/dist/workflow/wal.js +0 -106
@@ -0,0 +1,138 @@
1
+ // computer_script's run: the model's JavaScript in a QuickJS isolate here in serve (pi-codemode's CodemodeSandbox, a
2
+ // worker thread per script), whose only binding is the claim's computer. The script sees `computer` (the surface's
3
+ // script prelude builds it inside the isolate over one host function, __computerCall) and pi-codemode's output helpers
4
+ // (text, image, console); no network, no keys, no timers, no modules, no file of this machine. Each call of the computer
5
+ // goes through the run's hands (Hands.computer), so a person's hold parks and refuses it as it does a per-step tool's.
6
+ //
7
+ // The answer: the script's text and images in the order it made them, then its returned value as JSON (its images'
8
+ // data left out: they are the answer's images); `structured` is the value. A script that threw answers its words, with
9
+ // what it showed before.
10
+ import { CodemodeSandbox } from "@earendil-works/pi-codemode";
11
+ import { COMPUTER_CALL_GLOBAL, scriptPrelude, withoutImageData } from "@parcha/agentrun-computer/surface";
12
+ /** A script's longest run by default, and at most, in ms. */
13
+ export const SCRIPT_DEFAULT_MS = 120_000;
14
+ export const SCRIPT_MAX_MS = 600_000;
15
+ /** The isolate's memory. */
16
+ const SCRIPT_MEMORY_BYTES = 256 * 1024 * 1024;
17
+ /** The settings key a script's computer call is held to, as the run's own tool of that name is: the shell is bash's, a
18
+ * file's write is write's, a file's read, listing or stat is read's. The other ops (looking, acting) are the computer
19
+ * kind's, which the call of computer_script or codemode was already held to. */
20
+ const HELD_AS = Object.freeze({ "shell.run": "bash", "files.write": "write", "files.read": "read", "files.list": "read", "files.stat": "read" });
21
+ /** What a script's `computer.shell` and `computer.files` are held to: the run's gate, which a person's settings (a denied
22
+ * bash, a read-only bash policy, a denied write) reach through the one decision the run's own tools get. A script can't
23
+ * stop to ask, so a call the gate would ask about is refused in words that send the model to the tool itself. Until a
24
+ * gate is installed (the spec installs it once built) every one of those calls is refused. */
25
+ export class ScriptGuard {
26
+ shell;
27
+ #decide;
28
+ /** `shell`: what the run's bash adds under its bash policy (bash-read-only.ts policyEnv, policyPrelude): the environment
29
+ * that locks git's config down, set over any a script gives, and the line that runs git only at the floor version. */
30
+ constructor(shell = { env: {}, prelude: "" }) {
31
+ this.shell = shell;
32
+ }
33
+ /** `op`'s arguments as the computer is sent them: a script's shell command with the policy's safeguards, as the bash
34
+ * tool's own command has them, which no option of the script's overrides. */
35
+ shaped(op, args) {
36
+ if (op !== "shell.run")
37
+ return args;
38
+ const given = (args ?? {});
39
+ const env = { ...(given.env ?? {}), ...this.shell.env };
40
+ return { ...given, ...(Object.keys(env).length > 0 ? { env } : {}), ...(this.shell.prelude !== "" && typeof given.command === "string" ? { command: `${this.shell.prelude}${given.command}` } : {}) };
41
+ }
42
+ install(decide) {
43
+ this.#decide = decide;
44
+ }
45
+ /** Why `op` with `args` may not run now, or undefined when the gate lets it. */
46
+ async refusal(op, args) {
47
+ const tool = HELD_AS[op];
48
+ if (tool === undefined)
49
+ return undefined;
50
+ if (!this.#decide)
51
+ return `computer call ${op} was not run: this run's tool settings are not attached to its scripts yet`;
52
+ const given = (args ?? {});
53
+ const decision = await this.#decide(tool, tool === "bash" ? { command: given.command, ...(given.cwd !== undefined ? { cwd: given.cwd } : {}) } : { path: given.path });
54
+ if (decision === "allow")
55
+ return undefined;
56
+ if (decision === "ask")
57
+ return `computer call ${op} was not run: this agent's ${tool} setting asks a person first, and a script can't wait for one. Call the ${tool} tool itself, or ask for the setting to be allow.`;
58
+ return `computer call ${op} was not run: ${decision.deny}`;
59
+ }
60
+ /** `call` of the computer's port with a script's computer call held to the gate first: the refusal throws in the
61
+ * script, where its `await` is. */
62
+ async call(op, args, send) {
63
+ const refused = await this.refusal(op, args);
64
+ if (refused !== undefined)
65
+ throw new Error(refused);
66
+ return (await send(this.shaped(op, args ?? {}))).value ?? null;
67
+ }
68
+ }
69
+ /** Every image in `value` (`{ type: "image", data }`), in the order withImageData puts data back into them. */
70
+ function imagesIn(value) {
71
+ if (Array.isArray(value))
72
+ return value.flatMap(imagesIn);
73
+ if (value === null || typeof value !== "object")
74
+ return [];
75
+ const record = value;
76
+ if (record.type === "image" && typeof record.data === "string")
77
+ return [{ data: record.data, mimeType: typeof record.mimeType === "string" ? record.mimeType : "image/png" }];
78
+ return Object.values(record).flatMap(imagesIn);
79
+ }
80
+ /** The script's own output and its value, as the model reads them. The images come as the structured value wants them
81
+ * back (withImageData fills its data-less image objects in order): the returned value's own first, then the ones the
82
+ * script showed with image(...), so a returned image keeps its own pixels whatever was shown. */
83
+ function answerOf(output, value) {
84
+ const words = [];
85
+ const shown = [];
86
+ for (const item of output) {
87
+ if (item.type === "text")
88
+ words.push(item.text);
89
+ else
90
+ shown.push({ data: item.data, mimeType: item.mimeType });
91
+ }
92
+ if (value !== undefined)
93
+ words.push(typeof value === "string" ? value : JSON.stringify(withoutImageData(value)));
94
+ return { text: words.join("\n") || "(the script returned nothing and showed nothing)", images: [...imagesIn(value), ...shown] };
95
+ }
96
+ /** computer_script's executor for a computer that is `card`: each script in its own isolate, its computer calls through
97
+ * the call's hands, the shell's and the files' held to `guard`. */
98
+ export function computerScriptExecutor(card, guard) {
99
+ const prelude = scriptPrelude(card);
100
+ return async (args, call) => {
101
+ const { code, timeoutMs } = (args ?? {});
102
+ if (typeof code !== "string" || code.trim() === "")
103
+ return { text: "", error: "computer_script needs code: the body of an async function, with `computer` bound" };
104
+ const port = call.hands.computer;
105
+ if (!port)
106
+ return { text: "", error: "computer_script needs a desktop or a phone computer: this run has none" };
107
+ const limit = typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0 ? Math.min(timeoutMs, SCRIPT_MAX_MS) : SCRIPT_DEFAULT_MS;
108
+ const sandbox = new CodemodeSandbox({
109
+ timeoutMs: limit,
110
+ memoryLimitBytes: SCRIPT_MEMORY_BYTES,
111
+ globals: [{
112
+ name: COMPUTER_CALL_GLOBAL,
113
+ spread: true,
114
+ // The value whole, images and all: a script shows one with image(...). The JSON round trip is the isolate's.
115
+ execute: async (given, context) => {
116
+ const [op, opArgs] = given;
117
+ return (await guard.call(String(op), opArgs, (sent) => port.call(String(op), sent ?? {}, AbortSignal.any([context.signal, ...(call.signal ? [call.signal] : [])]))));
118
+ },
119
+ }],
120
+ });
121
+ try {
122
+ // The prelude is one line, so the script's own line numbers hold.
123
+ const result = await sandbox.execute(`${prelude}\n${code}`, call.signal ? { signal: call.signal } : {});
124
+ if (!result.ok) {
125
+ if (result.error.kind === "aborted" && call.signal?.aborted)
126
+ throw call.signal.reason ?? new Error(result.error.message);
127
+ const shown = answerOf(result.output, undefined);
128
+ const why = result.error.kind === "timeout" ? `the script ran past its ${limit} ms: ${result.error.message}` : result.error.message;
129
+ return { text: result.output.length > 0 ? shown.text : "", error: why, ...(shown.images.length > 0 ? { images: shown.images } : {}) };
130
+ }
131
+ const shown = answerOf(result.output, result.value);
132
+ return { text: shown.text, ...(shown.images.length > 0 ? { images: shown.images } : {}), ...(result.value === undefined ? {} : { structured: withoutImageData(result.value) }) };
133
+ }
134
+ finally {
135
+ await sandbox.close();
136
+ }
137
+ };
138
+ }
@@ -1,4 +1,4 @@
1
- import type { Context } from "@earendil-works/chord";
1
+ import type { Context } from "@earendil-works/chord/context";
2
2
  import type { ToolRegistration } from "@earendil-works/pi-durable";
3
3
  import type { ExecutionEnv } from "@earendil-works/pi-durable/env";
4
4
  import type { ToolAnswer, ToolCall } from "@parcha/agentrun-kernel/kernel";
@@ -13,8 +13,10 @@ export { readOnlyWorkspace, SERVER_READS_ONLY, serverRunWorkspace, type ServerRu
13
13
  export { type LedgerEntry, type ProviderLedger, storeLedger } from "./computer-ledger.js";
14
14
  export { type LearningPlace, type LearningWorkspace, learningWorkspace, type LearningWorkspaceOptions } from "./learning-workspace.js";
15
15
  export { ARCHIL_MAX_EXEC_S, handsOf, MAX_EXEC_S, processesOf, shellEnv } from "./hands.js";
16
- export { type RunComputer, type RunHold, runTools, type RunTools, withKindTools } from "./run-tools.js";
17
- export { inputOf, phoneOf, screenOf } from "./computer-ports.js";
16
+ export { type RunComputer, type RunHold, runTools, type RunTools, type ScriptComputer, withKindTools } from "./run-tools.js";
17
+ export { portOf } from "./computer-ports.js";
18
+ export { computerScriptExecutor, SCRIPT_DEFAULT_MS, SCRIPT_MAX_MS, ScriptGuard, type ScriptDecide } from "./computer-script.js";
19
+ export { COMPUTER_MCP_NAME, computerMcp, type ComputerMcpAnswer, mcpAnswerOf } from "./computer-mcp.js";
18
20
  export { type ClaimSide, computerReadiness, type ComputerSetting, type KindRegistry, type LaunchedComputer, type Port, resolveComputer, type ResolvedComputer, runShellOf, type ShellPlace, shellPlaceOf, type StartedComputer, } from "./resolve-computer.js";
19
21
  export { computerProviderFactories, computerProviderMakers, type ProviderLoad } from "./computer-providers.js";
20
22
  export { claimComputer, claimKinds, type ComputerDeps, computerDepsFrom, type ComputerDepsOptions, computerSpecOf, DAEMON_CACHE_IN_VOLUME, DOCKER_HOST_ADDRESSES_ENV, type DockerComputerOptions, dockerHostAddressesOf, dockerVolumeOf, } from "./computer-deps.js";
@@ -15,7 +15,9 @@ export { storeLedger } from "./computer-ledger.js";
15
15
  export { learningWorkspace } from "./learning-workspace.js";
16
16
  export { ARCHIL_MAX_EXEC_S, handsOf, MAX_EXEC_S, processesOf, shellEnv } from "./hands.js";
17
17
  export { runTools, withKindTools } from "./run-tools.js";
18
- export { inputOf, phoneOf, screenOf } from "./computer-ports.js";
18
+ export { portOf } from "./computer-ports.js";
19
+ export { computerScriptExecutor, SCRIPT_DEFAULT_MS, SCRIPT_MAX_MS, ScriptGuard } from "./computer-script.js";
20
+ export { COMPUTER_MCP_NAME, computerMcp, mcpAnswerOf } from "./computer-mcp.js";
19
21
  export { computerReadiness, resolveComputer, runShellOf, shellPlaceOf, } from "./resolve-computer.js";
20
22
  export { computerProviderFactories, computerProviderMakers } from "./computer-providers.js";
21
23
  export { claimComputer, claimKinds, computerDepsFrom, computerSpecOf, DAEMON_CACHE_IN_VOLUME, DOCKER_HOST_ADDRESSES_ENV, dockerHostAddressesOf, dockerVolumeOf, } from "./computer-deps.js";
@@ -1,106 +1,141 @@
1
1
  ---
2
2
  name: desktop-computer
3
- description: Use a desktop computer through the desktop_screen and desktop_input tools. It covers looking, acting with the pointer and the keyboard in batches, checking what happened, recovering, and reading the computer's refusals. Use when a run has a desktop and the task needs its screen, its apps or its windows.
3
+ description: Use a desktop computer (a Mac, or a Linux desktop) through computer_observe, computer_act, computer_screenshot and computer_script. It covers reading an app's elements, acting on them by ref, running known steps as one script, checking what happened, recovering, and reading the computer's refusals. Use when a run has a desktop and the task needs its apps or its windows.
4
4
  ---
5
5
 
6
6
  # Using a desktop
7
7
 
8
- The run has a desktop: a computer with a screen, a pointer and a keyboard, rented while the run lives. You see it
9
- through screenshots and act on it as a person would, with the mouse and the keys. Input answers in words, never with a
10
- picture, so you take a screenshot to see what your actions did.
8
+ The run has a desktop, rented while the run lives. You read an app's window as a list of its elements and act on
9
+ them by their refs, the way an accessibility tool does. Where the desktop can't list elements (no `ui` group), you see
10
+ it through screenshots and act at their pixels. The tools say which one you have.
11
11
 
12
12
  ## The tools
13
13
 
14
- **`desktop_screen` `{region?}`** takes a screenshot. With no region, it shows the whole screen, and the answer gives
15
- its size in pixels. With `region: {x, y, w, h}`, it shows only that part, sharper for small text. The coordinates stay
16
- the whole screen's: a button you find in a region is clicked at its whole-screen point.
17
-
18
- **`desktop_input` `{actions}`** runs a list of 1 to 100 actions, in order:
19
-
20
- | Action | Fields | What it does |
21
- |---|---|---|
22
- | `move` | `at` | Moves the pointer there. |
23
- | `click` | `at?`, `button?` (`left`, `right`, `middle`), `count?` (1, 2, 3) | Clicks at `at`, or where the pointer is. `count: 2` is a double click. |
24
- | `drag` | `path` (2+ points), `button?` | Presses at the first point, moves through the rest, releases at the last. |
25
- | `scroll` | `at?`, `dx?`, `dy?`, `unit` (`clicks` or `px`) | Scrolls there. `dy` above 0 scrolls down. Wheel clicks are the usual unit. |
26
- | `type` | `text` | Types the text, as given, into what has focus. |
27
- | `key` | `keys` | Presses one key, or several together as a combo: `["ctrl", "c"]`. |
28
-
29
- A key is a name or one printable character. The names are: `enter`, `tab`, `space`, `backspace`, `delete`, `escape`,
30
- `insert`, `up`, `down`, `left`, `right`, `home`, `end`, `pageup`, `pagedown`, `f1` to `f12`, `shift`, `ctrl`, `alt`,
31
- `super` and `capslock`. A character is one printable character, such as `"a"`, `"A"` or `"/"`. Use `type` for words,
32
- and `key` for keys and combos.
33
-
34
- **Points are `[x, y]` in screenshot pixels**, origin top left, `x` to the right and `y` down. Take them from the latest
35
- screenshot, and aim at the middle of what you want.
36
-
37
- ## The loop: look, act, check, recover
38
-
39
- 1. **Look.** Take a screenshot first, and again after anything that may change the screen. To read small text (a
40
- menu, a field, a status line), take a region screenshot of it.
41
- 2. **Act.** Put actions in one batch when you know what each will do without seeing the screen between them: click a
42
- field, type, press `enter`. End the batch where you need to see the result. A click that opens a menu or a new
43
- window ends a batch: the next point depends on what opens.
44
- 3. **Check.** Take a screenshot and look for the change the actions should have made. That might be the text in the
45
- field, the menu open, the window moved, or the page loaded. A batch that finished without an error only says the
46
- computer ran the actions, not that the app did what you meant.
47
- 4. **Recover.** If nothing changed, find out why before you try again:
48
- - Maybe another window or a dialog is in front.
49
- - Maybe the click landed beside the target.
50
- - Maybe the app is still busy.
51
- Try a better point once. After two or three tries that change nothing, change approach: use the keyboard instead
52
- of the pointer (`tab` to move, `enter` to press), a menu instead of a button, or close and reopen the app.
14
+ **`computer_observe` `{app?, image?, full?}`** shows an app's window (the one in front when you name none) as compact
15
+ lines, one per element:
16
+
17
+ ```
18
+ state s3 · com.apple.Notes "Notes" · window "Groceries" 900×700 pt
19
+ @e1 window "Groceries"
20
+ @e4 textbox "Title" = "Groceries" [focused]
21
+ @e7 button "Save"
22
+ ```
23
+
24
+ Each line is a ref (`@e7`), a role, a name, a value after `=`, what else the element says in parentheses (a help tag)
25
+ and flags in brackets. A child is indented under its parent. When an app draws its own window, the computer can't
26
+ list its elements: the outline says so, the window's picture comes with it, and you act at points in that picture. The head line names the **state** (`s3`). `image: true` adds the window's picture, which you need only for
27
+ what elements can't tell you: layout, colours, images. `full: true` lists every element, not only the named or
28
+ actionable ones.
29
+
30
+ **`computer_act` `{app?, steps, state?, expect?, observe?}`** runs steps in order:
31
+
32
+ | Step | What it does |
33
+ |---|---|
34
+ | `{press: "@e7"}` | The element's own action (a button's press, a menu item's pick). The surest step. |
35
+ | `{set: "@e4", value: "Milk"}` | Sets a field's text, a slider's value, a switch's `"on"` or `"off"`. |
36
+ | `{click: "@e7"}` or `{click: [x, y]}` | A click on the element, or at a point; `button: "right"`, `count: 2`. |
37
+ | `{type: "text", into?: "@e4"}` | Types, into the element when named, else where the focus is. |
38
+ | `{key: "super+s"}` | A key or a chord: names or one character, joined by `+` (`enter`, `ctrl+shift+t`). |
39
+ | `{scroll: "@e9", dy: 300}` | Scrolls by points: `dy` above 0 shows what is below. |
40
+ | `{drag: [[x1, y1], [x2, y2]]}` | Presses at the first point, lets go at the last. |
41
+ | `{perform: "@e7", action: "AXShowMenu"}` | One of the element's own platform actions. |
42
+
43
+ Each step answers its **route** (`semantic`: through the element; `page`: through the page; `background`: an event to
44
+ the app; `foreground`: the real pointer and keys) and whether a readback **verified** it. The answer ends with whether
45
+ the window changed and its new state. `expect: {text: "Saved"}` waits after the steps until that text shows (`gone:
46
+ true`: until it is gone); no steps and an expect is a wait. `observe: true` adds the new outline: one call instead of
47
+ two. The first refused step stops the rest.
48
+
49
+ Prefer refs. A step at a point (`[x, y]`, in the window's points) is read against a state, the last one you observed
50
+ unless you name another, and is refused as **stale** once the window moved or redrew under it: observe again and use
51
+ the new state.
52
+
53
+ **`computer_screenshot` `{region?}`** is the whole screen, or `region: [x, y, w, h]` of it, in screen pixels.
54
+
55
+ **`computer_script` `{code}`** runs JavaScript in an isolate whose only binding is `computer`. Its tool description
56
+ gives the `computer` object's types for this desktop. The body is an async function: `await` the calls and `return`
57
+ what you need; `text(...)` and `image(...)` show words and pictures beside the value.
58
+
59
+ ```js
60
+ const notes = computer.app("com.apple.Notes");
61
+ await notes.observe();
62
+ const title = notes.find({ role: "textbox", text: "Title" })[0];
63
+ await notes.set(title.ref, "Groceries");
64
+ const saved = await notes.key("super+s", { expect: { text: "Saved" } });
65
+ return { saved: saved.expect, state: saved.state };
66
+ ```
67
+
68
+ **Your harness's own code mode.** pi's `codemode` binds `computer` as computer_script does. Codex's `exec` does not:
69
+ there, call the tools, each answering its value as JSON text, or call `tools.computer_script` with the code above.
70
+
71
+ ```js
72
+ const notes = JSON.parse(await tools.computer_observe({ app: "com.apple.Notes" }));
73
+ const title = notes.elements.find((node) => node.role === "textbox" && node.name === "Title");
74
+ ```
75
+
76
+ **The screen itself.** Prefer an app's steps. A script's `computer.input.act([...])` is the screen-level way out, at
77
+ screenshot pixels (`computer.screen.shot()`), for what one app's window can't reach: a drag from one app to another (a
78
+ file from Finder into a Mail message), or anything outside every window.
79
+
80
+ ## How to work: observe, then act by ref, then check
81
+
82
+ 1. **Observe** the app first. Find your target by its name and role, and note its ref.
83
+ 2. **Act** by ref. Several steps you are sure of go in one `computer_act`: set a field, press a button. End the call
84
+ where you need to see the result.
85
+ 3. **Check** what the answer says: each step's route and verified, `changed`, and the expect's `met` or `unmet`. A
86
+ step that ran is not proof the app did what you meant. When the answer doesn't settle it, observe again.
87
+ 4. **Recover.** A refused step says why. `stale`: observe again, then act on the new refs or state. Nothing changed:
88
+ a dialog may be in front, or the element may be disabled (`[disabled]`). Try once more another way (`press`
89
+ instead of `click`, the keyboard instead of the pointer, a menu instead of a button), then change approach.
90
+
91
+ ## When to write a script
92
+
93
+ A script is one call where tools would be many. Use one when:
94
+
95
+ - **You know the steps.** A form you have seen, a menu path you know, a sequence you have done once: run it as one
96
+ script with an `expect` at the end, instead of one act per step.
97
+ - **You need a few facts out of a big window.** Observe and `find()` inside the script, and return only what the next
98
+ decision needs: a ref, a value, a count, three rows. Never return whole observations: their elements are long, and
99
+ you pay for every line you read. `find({ role, text, id })`: a role is the same name on every OS, a text matches
100
+ the name, the value or what the parentheses say, and an id is the app's own for the element.
101
+ - **You loop.** Scroll a list until a row shows, or read each row of a table.
102
+
103
+ Don't script steps you have not seen yet. When you are unsure what is on the screen, observe before you act, by tool
104
+ or inside the script, and decide from what it shows.
105
+
106
+ ## Without the ui group (a Linux desktop)
107
+
108
+ The desktop lists no elements. `computer_observe` answers a screenshot of the whole screen, and `computer_act`'s steps
109
+ are raw input at its pixels: `click`, `type`, `key`, `scroll` and `drag` at points, with no refs, no states and no
110
+ expect. `press`, `set` and `perform` are refused. Take the points from the latest screenshot, aim at the middle of a
111
+ target, and take another screenshot to see what your steps did. Click a field before typing into it.
53
112
 
54
113
  ## Habits that work
55
114
 
56
- - **Focus before typing.** Click the field, then type. Typed text goes to whatever has focus, which may not be the
57
- field you see.
58
- - **Prefer the keyboard where it is exact.**
59
- - `ctrl` + `a` selects all.
60
- - `ctrl` + `l` focuses a browser's address bar.
61
- - `escape` closes a menu or a dialog.
62
- - `alt` + `f4` closes a window.
63
- - `enter` confirms.
64
- Shortcuts don't depend on where things are drawn.
65
- - **Scroll where the content is.** A scroll goes to what is under its point. Put `at` on the list or the page you mean,
66
- not on a sidebar.
67
- - **Wait by looking.** If a page or an app is loading, take another screenshot before you act. Don't repeat a click
68
- while the screen is still changing: the second click may land on the new screen.
69
- - **Windows.** If the window you need is behind another one, click its visible part or its taskbar entry, or use
70
- `alt` + `tab`. Then look again to check that it is in front.
71
- - **Dialogs first.** A dialog, a notice or a cookie banner blocks what is under it. Read it, answer it or close it,
72
- then go on.
115
+ - **The keyboard is exact.** `super+a` (Mac) or `ctrl+a` selects all, `escape` closes a menu or a dialog, `enter`
116
+ confirms. A chord doesn't depend on where things are drawn.
117
+ - **Dialogs first.** A sheet, an alert or a permission prompt blocks the window under it: answer it, then go on.
118
+ - **Wait by expecting.** Put an `expect` on the step that starts something slow, rather than observing in a loop.
119
+ - **The right app.** Name the app (`app: "com.apple.Notes"`) when more than one is open. In a script,
120
+ `computer.apps.open(id)` opens one, and `computer.app(id)` binds it.
73
121
 
74
122
  ## When a tool answers an error
75
123
 
76
- Errors come in words, and they say what was wrong. Read them, and act on what they say. Don't repeat the same call.
124
+ Errors come in words, and they say what was wrong. Read them, act on what they say, and don't repeat the same call.
77
125
 
78
- - **A malformed action** (an unknown key name, a point that is not two numbers, an action that is not one of the six):
79
- the whole batch is refused, and nothing ran. Fix the action the words name, and send the batch again.
80
- - **A batch that failed partway** says how many actions ran. Take a screenshot, see where it stopped, and go on from
81
- there. Don't resend the actions that already ran.
82
- - **More than 100 actions**: split the batch.
83
- - **"The person had this desktop"**: a person took the desktop over and has handed it back, and your input was not
84
- done. The screen may have changed, so take a screenshot before you act again.
85
- - **The computer is gone or unreachable**: the run's desktop ended (its time ran out, or it was released). Don't keep
86
- calling. Say what you finished and what you could not.
87
-
88
- ## What not to do
89
-
90
- - Don't guess coordinates from memory of another screen or another resolution. Use this screenshot's pixels.
91
- - Don't send a long batch blind across screens you have not seen. One wrong click early sends the rest astray.
92
- - Don't take screenshots in a loop just to wait. Each one takes time, and the desktop's time is limited and may be
93
- billed.
94
- - Don't type secrets you were not given for this task, and don't change the computer's settings beyond what the task
95
- needs.
126
+ - **"The person had this computer"**: a person took the desktop over and has handed it back, and your act was not
127
+ done. The screen may have changed: observe again before you act.
128
+ - **A step refused as `unsupported`** names what this desktop can't do. Use another step.
129
+ - **The computer is gone or unreachable**: the run's desktop ended. Don't keep calling. Say what you finished and what
130
+ you could not.
96
131
 
97
132
  ## Time and cost
98
133
 
99
- The desktop lives only for the run's lifetime, and may be billed while it runs. Make a short plan before the first
100
- action: which app, which steps, and what will show success. Batch what is certain. Stop once the task is done, or once
101
- it can't be done.
134
+ The desktop lives only for the run's lifetime and may be billed while it runs. Make a short plan first: which app,
135
+ which steps, what will show success. Script what is certain, observe what is not, and stop once the task is done or
136
+ can't be done. Don't type secrets you were not given for this task.
102
137
 
103
138
  ## Finishing
104
139
 
105
- Before you call the task done, take a final screenshot that shows the result. Describe what is on it in your answer,
106
- and name anything you could not finish or check.
140
+ Before you call the task done, check the result: an observe that shows it, or a screenshot when it is visual. Say what
141
+ it shows in your answer, and name anything you could not finish or check.