shapewall 0.0.0-stage → 0.2.2

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 (304) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/Dockerfile.agent +45 -0
  3. package/LICENSE +202 -0
  4. package/NOTICE +12 -0
  5. package/README.md +651 -2
  6. package/THIRD_PARTY.md +165 -0
  7. package/bin/shapewall.js +29 -0
  8. package/dist/agent/package-lock.json +8384 -0
  9. package/dist/client/assets/BoardChat-D5SFd8HE.js +28 -0
  10. package/dist/client/assets/BuzzDialog-HWXBy52W.js +1 -0
  11. package/dist/client/assets/ChecksPanel-DTdImWoD.js +1 -0
  12. package/dist/client/assets/History-CEv6b-av.js +1 -0
  13. package/dist/client/assets/LocalSettings-C4vriEVQ.js +1 -0
  14. package/dist/client/assets/Management-DCfhcEUY.js +1 -0
  15. package/dist/client/assets/QuickConnect-DQGgUsl1.js +1 -0
  16. package/dist/client/assets/ShareDialog-C_plIto0.js +2 -0
  17. package/dist/client/assets/app-update-dialog-2hPz_N9G.js +1 -0
  18. package/dist/client/assets/circle-x-BZMPi8Zf.js +1 -0
  19. package/dist/client/assets/index-ByQRKVit.css +2 -0
  20. package/dist/client/assets/index-DweQ8Qtu.js +29 -0
  21. package/dist/client/assets/refresh-cw-CRow0Nbq.js +1 -0
  22. package/dist/client/assets/runtime-Bft6x_6E.js +4 -0
  23. package/dist/client/assets/square-A8EnIYLR.js +1 -0
  24. package/dist/client/assets/tts-dist-DYlBQmwG.js +18 -0
  25. package/dist/client/assets/tts-en-g2p-BPUtBFGG.js +1 -0
  26. package/dist/client/assets/tts-ort-wasm-simd-threaded-qh98TCA_.wasm +0 -0
  27. package/dist/client/assets/tts-ort.wasm.bundle.min-rEkcs_ow.js +12 -0
  28. package/dist/client/assets/tts-rolldown-runtime-D8NudN84.js +1 -0
  29. package/dist/client/assets/tts-spacy-en-tokenizer-6SNMl--A.json +1 -0
  30. package/dist/client/assets/tts-tts.worker-Bzm6aWbv.js +1 -0
  31. package/dist/client/assets/tts-us_gold-DcjeNMH1.json +92589 -0
  32. package/dist/client/assets/tts-us_silver-C964mEB5.json +93363 -0
  33. package/dist/client/assets/ui-CLbws0rx.js +41 -0
  34. package/dist/client/build-info.json +1 -0
  35. package/dist/client/favicon.svg +7 -0
  36. package/dist/client/icons/apple-touch-icon.png +0 -0
  37. package/dist/client/icons/icon-192.png +0 -0
  38. package/dist/client/icons/icon-512.png +0 -0
  39. package/dist/client/icons/maskable-512.png +0 -0
  40. package/dist/client/index.html +25 -0
  41. package/dist/client/manifest.webmanifest +15 -0
  42. package/dist/client/sw.js +53 -0
  43. package/dist/client/theme-boot.js +27 -0
  44. package/dist/console/assets/LocalAccountsApp-BQCcmj3x.js +1 -0
  45. package/dist/console/assets/LocalConsole-Bb2FUDs7.js +1 -0
  46. package/dist/console/assets/app-update-dialog-DIeYepLZ.js +1 -0
  47. package/dist/console/assets/console-C7uJLLOn.js +51 -0
  48. package/dist/console/assets/console-R8IBY3Am.css +2 -0
  49. package/dist/console/build-info.json +1 -0
  50. package/dist/console/console.html +21 -0
  51. package/dist/console/favicon.svg +7 -0
  52. package/dist/console/icons/apple-touch-icon.png +0 -0
  53. package/dist/console/icons/icon-192.png +0 -0
  54. package/dist/console/icons/icon-512.png +0 -0
  55. package/dist/console/icons/maskable-512.png +0 -0
  56. package/dist/console/manifest.webmanifest +15 -0
  57. package/dist/console/theme-boot.js +27 -0
  58. package/docs/agent-hardening.md +342 -0
  59. package/docs/agent-recipes.md +187 -0
  60. package/docs/agent-workflow.md +320 -0
  61. package/docs/agents.md +894 -0
  62. package/docs/architecture.md +242 -0
  63. package/docs/buzz.md +140 -0
  64. package/docs/capture-warnings.md +57 -0
  65. package/docs/config.md +489 -0
  66. package/docs/deployment.md +289 -0
  67. package/docs/design-system.md +162 -0
  68. package/docs/github.md +107 -0
  69. package/docs/hosted.md +764 -0
  70. package/docs/images/board-overview.png +0 -0
  71. package/docs/images/journeys.png +0 -0
  72. package/docs/images/live-comment.png +0 -0
  73. package/docs/images/phone-comment.png +0 -0
  74. package/docs/images/phone-overview.png +0 -0
  75. package/docs/images/variant.png +0 -0
  76. package/docs/interaction-audits.md +40 -0
  77. package/docs/journeys.md +137 -0
  78. package/docs/local-accounts.md +80 -0
  79. package/docs/notifications.md +237 -0
  80. package/docs/page-checks.md +69 -0
  81. package/docs/performance.md +174 -0
  82. package/docs/starter-agent.md +325 -0
  83. package/docs/updates.md +87 -0
  84. package/examples/audits/interaction.audit.json +17 -0
  85. package/package.json +132 -4
  86. package/src/accounts/avatar.js +121 -0
  87. package/src/accounts/local.js +464 -0
  88. package/src/agent/acp/command.js +204 -0
  89. package/src/agent/acp/detect.js +75 -0
  90. package/src/agent/acp/sandbox.js +160 -0
  91. package/src/agent/agent-health.js +75 -0
  92. package/src/agent/backends/acp.js +1297 -0
  93. package/src/agent/backends/index.js +61 -0
  94. package/src/agent/backends/openai.js +308 -0
  95. package/src/agent/composer.js +221 -0
  96. package/src/agent/config.js +338 -0
  97. package/src/agent/context.js +191 -0
  98. package/src/agent/control.js +122 -0
  99. package/src/agent/errors.js +17 -0
  100. package/src/agent/feedback-loop.js +137 -0
  101. package/src/agent/manage.js +94 -0
  102. package/src/agent/next-steps.js +51 -0
  103. package/src/agent/owner-mcp.js +38 -0
  104. package/src/agent/plain-text.js +52 -0
  105. package/src/agent/ratings-core.js +213 -0
  106. package/src/agent/ratings.js +100 -0
  107. package/src/agent/reply-text.js +67 -0
  108. package/src/agent/routes.js +1065 -0
  109. package/src/agent/run.js +326 -0
  110. package/src/agent/settings-routes.js +704 -0
  111. package/src/agent/showable.js +23 -0
  112. package/src/agent/starter/approvals.js +181 -0
  113. package/src/agent/starter/build-image.js +90 -0
  114. package/src/agent/starter/cli.js +163 -0
  115. package/src/agent/starter/disk.js +53 -0
  116. package/src/agent/starter/doctor.js +180 -0
  117. package/src/agent/starter/egress-proxy.js +351 -0
  118. package/src/agent/starter/gate.js +90 -0
  119. package/src/agent/starter/loop.js +114 -0
  120. package/src/agent/starter/model-relay.js +289 -0
  121. package/src/agent/starter/pause.js +14 -0
  122. package/src/agent/starter/probe.js +272 -0
  123. package/src/agent/starter/prompt.js +41 -0
  124. package/src/agent/starter/push-guard.js +146 -0
  125. package/src/agent/starter/rpc.js +164 -0
  126. package/src/agent/starter/runtime.js +287 -0
  127. package/src/agent/starter/sandbox-main.js +121 -0
  128. package/src/agent/starter/settings.js +290 -0
  129. package/src/agent/starter/source.js +69 -0
  130. package/src/agent/starter/supervisor.js +755 -0
  131. package/src/agent/starter/sweep.js +46 -0
  132. package/src/agent/starter/tools.js +524 -0
  133. package/src/agent/starter/watch.js +84 -0
  134. package/src/agent/starter/worktree.js +409 -0
  135. package/src/agent/text-diff.js +210 -0
  136. package/src/agent/thread-history.js +223 -0
  137. package/src/agent/threads.js +399 -0
  138. package/src/agent/tools.js +200 -0
  139. package/src/agent/turn-changes.js +424 -0
  140. package/src/agent/undo-state.js +26 -0
  141. package/src/agent/user-settings.js +308 -0
  142. package/src/auth/record.js +375 -0
  143. package/src/broken-links.js +164 -0
  144. package/src/buzz/client.js +75 -0
  145. package/src/buzz/config.js +151 -0
  146. package/src/buzz/environment.js +13 -0
  147. package/src/buzz/gateway.js +192 -0
  148. package/src/buzz/routes.js +61 -0
  149. package/src/buzz/signer-child.js +205 -0
  150. package/src/buzz/store.js +89 -0
  151. package/src/buzz/transport.js +60 -0
  152. package/src/buzz/verify.js +140 -0
  153. package/src/capture/actions.js +159 -0
  154. package/src/capture/capture.js +776 -0
  155. package/src/capture/dom-names.js +189 -0
  156. package/src/capture/element-map.js +509 -0
  157. package/src/capture/live.js +827 -0
  158. package/src/capture/media-scheme.js +317 -0
  159. package/src/capture/network-summary.js +86 -0
  160. package/src/capture/pacing.js +34 -0
  161. package/src/capture/requests.js +167 -0
  162. package/src/capture/sanitize.js +752 -0
  163. package/src/checks/accessibility.js +134 -0
  164. package/src/checks/budgets.js +58 -0
  165. package/src/checks/cli.js +55 -0
  166. package/src/checks/comparison.js +113 -0
  167. package/src/checks/environments.js +32 -0
  168. package/src/checks/flow-config.js +82 -0
  169. package/src/checks/flow-origin.js +53 -0
  170. package/src/checks/flows.js +398 -0
  171. package/src/checks/lighthouse-auth.js +145 -0
  172. package/src/checks/lighthouse-findings.js +130 -0
  173. package/src/checks/lighthouse.js +395 -0
  174. package/src/checks/robots.js +85 -0
  175. package/src/checks/routes.js +52 -0
  176. package/src/checks/run-control.js +259 -0
  177. package/src/checks/runner.js +724 -0
  178. package/src/checks/schema.js +133 -0
  179. package/src/checks/seo-analysis.js +443 -0
  180. package/src/checks/seo-probe.js +190 -0
  181. package/src/checks/seo-snapshot.js +99 -0
  182. package/src/checks/store.js +214 -0
  183. package/src/checks/transport.js +47 -0
  184. package/src/cli/args.js +319 -0
  185. package/src/cli/capture.js +79 -0
  186. package/src/cli/commands.js +92 -0
  187. package/src/cli/comments.js +84 -0
  188. package/src/cli/help.js +243 -0
  189. package/src/cli/init.js +214 -0
  190. package/src/cli/misc.js +64 -0
  191. package/src/cli/notify.js +87 -0
  192. package/src/cli/open.js +22 -0
  193. package/src/cli/serve.js +274 -0
  194. package/src/cli/variant.js +82 -0
  195. package/src/cli/watch.js +70 -0
  196. package/src/cli.js +130 -0
  197. package/src/comment-fields.js +76 -0
  198. package/src/comments-cli.js +208 -0
  199. package/src/config-reload.js +100 -0
  200. package/src/config.js +1061 -0
  201. package/src/devices-cli.js +21 -0
  202. package/src/diff.js +95 -0
  203. package/src/discover.js +284 -0
  204. package/src/doctor.js +295 -0
  205. package/src/event-log.js +268 -0
  206. package/src/events-cli.js +102 -0
  207. package/src/events.js +113 -0
  208. package/src/export.js +119 -0
  209. package/src/feedback.js +246 -0
  210. package/src/github.js +551 -0
  211. package/src/hosted/LICENSE.md +105 -0
  212. package/src/hosted/access-cli.js +78 -0
  213. package/src/hosted/admin.js +682 -0
  214. package/src/hosted/agent-approvals.js +163 -0
  215. package/src/hosted/agent-policy.js +189 -0
  216. package/src/hosted/app.js +510 -0
  217. package/src/hosted/auth-routes.js +540 -0
  218. package/src/hosted/auth.js +536 -0
  219. package/src/hosted/backup.js +483 -0
  220. package/src/hosted/board-context.js +35 -0
  221. package/src/hosted/board-routes.js +486 -0
  222. package/src/hosted/board-view.js +94 -0
  223. package/src/hosted/clean-worker.js +24 -0
  224. package/src/hosted/cleaner.js +89 -0
  225. package/src/hosted/comments.js +355 -0
  226. package/src/hosted/console-routes.js +401 -0
  227. package/src/hosted/cookies.js +18 -0
  228. package/src/hosted/crypto.js +80 -0
  229. package/src/hosted/db.js +496 -0
  230. package/src/hosted/device-routes.js +175 -0
  231. package/src/hosted/device.js +233 -0
  232. package/src/hosted/events.js +63 -0
  233. package/src/hosted/guards.js +172 -0
  234. package/src/hosted/guest-view.js +65 -0
  235. package/src/hosted/http.js +39 -0
  236. package/src/hosted/import.js +300 -0
  237. package/src/hosted/mail.js +73 -0
  238. package/src/hosted/mcp-http.js +71 -0
  239. package/src/hosted/membership.js +114 -0
  240. package/src/hosted/operator.js +478 -0
  241. package/src/hosted/ops-cli.js +345 -0
  242. package/src/hosted/pages.js +146 -0
  243. package/src/hosted/permissions.js +113 -0
  244. package/src/hosted/presence.js +372 -0
  245. package/src/hosted/projects.js +124 -0
  246. package/src/hosted/public.js +109 -0
  247. package/src/hosted/push.js +622 -0
  248. package/src/hosted/quotas.js +115 -0
  249. package/src/hosted/ratelimit.js +96 -0
  250. package/src/hosted/rebind.js +316 -0
  251. package/src/hosted/retention.js +128 -0
  252. package/src/hosted/server.js +260 -0
  253. package/src/hosted/setup-link.js +64 -0
  254. package/src/hosted/setup.js +323 -0
  255. package/src/hosted/shares.js +444 -0
  256. package/src/hosted/tailscale.js +204 -0
  257. package/src/hosted/tenancy.js +301 -0
  258. package/src/hosted/token-api.js +455 -0
  259. package/src/hosted/token-events.js +60 -0
  260. package/src/hosted/tokens.js +57 -0
  261. package/src/hosted/turnstile.js +49 -0
  262. package/src/hosted/usercontent.js +154 -0
  263. package/src/hosted/zip.js +155 -0
  264. package/src/invocation.js +58 -0
  265. package/src/journey-paths.js +282 -0
  266. package/src/journeys.js +189 -0
  267. package/src/links.js +106 -0
  268. package/src/local-projects.js +182 -0
  269. package/src/lock.js +193 -0
  270. package/src/mcp.js +417 -0
  271. package/src/mentions.js +151 -0
  272. package/src/net.js +15 -0
  273. package/src/notify/config.js +275 -0
  274. package/src/notify/index.js +301 -0
  275. package/src/notify/lease.js +73 -0
  276. package/src/notify/messages.js +212 -0
  277. package/src/notify/run.js +174 -0
  278. package/src/pixeldiff.js +49 -0
  279. package/src/png.js +143 -0
  280. package/src/reactions.js +111 -0
  281. package/src/remote/cli.js +133 -0
  282. package/src/remote/credentials.js +62 -0
  283. package/src/remote/ids.js +12 -0
  284. package/src/remote/login.js +155 -0
  285. package/src/remote/remote-mcp.js +237 -0
  286. package/src/remote/remote-watch.js +291 -0
  287. package/src/remote/remote.js +313 -0
  288. package/src/remote/share-state.js +78 -0
  289. package/src/remote/share.js +369 -0
  290. package/src/remote/terminal.js +13 -0
  291. package/src/serve-state.js +64 -0
  292. package/src/server/app.js +824 -0
  293. package/src/server/devices.js +186 -0
  294. package/src/server/local-console.js +28 -0
  295. package/src/server/local-user.js +59 -0
  296. package/src/server/pairing-routes.js +159 -0
  297. package/src/server/share-routes.js +89 -0
  298. package/src/server/tailscale.js +97 -0
  299. package/src/sitemap.js +431 -0
  300. package/src/store.js +983 -0
  301. package/src/tokens.js +11 -0
  302. package/src/update/routes.js +245 -0
  303. package/src/watch.js +403 -0
  304. package/src/which.js +58 -0
@@ -0,0 +1,287 @@
1
+ // The container runtime for the starter agent: docker or podman, and the
2
+ // exact arguments every container is started with.
3
+ //
4
+ // Two containers per run, on a network made for that run:
5
+ //
6
+ // agent container --network <run network> (internal: no route out, no
7
+ // outside DNS); the fresh worktree mounted read-write at
8
+ // /work and nothing else; the host user's uid (never root),
9
+ // read-only root file system, every capability dropped,
10
+ // no-new-privileges, memory / CPU / process limits, and an
11
+ // environment of a few fixed names (no host variable, no
12
+ // key: model calls go through the host). Proxy variables
13
+ // point at:
14
+ // egress proxy on the run network and on an ordinary one, passing on
15
+ // only the allowed host:port pairs (egress-proxy.js).
16
+ //
17
+ // Never mounted: the home folder, SSH keys, a browser profile, the docker
18
+ // or podman socket, the project's own .git. Images are never pulled
19
+ // (--pull never): the image is built on this machine (Dockerfile.agent).
20
+
21
+ import { spawn } from 'node:child_process'
22
+ import { findProgram } from '../../watch.js'
23
+ import { PROXY_PORT, formatAllowEntry } from './egress-proxy.js'
24
+
25
+ export const RUNTIMES = ['podman', 'docker']
26
+ export const RUNTIME_VAR = 'SHAPEWALL_AGENT_RUNTIME'
27
+ export const IMAGE_VAR = 'SHAPEWALL_AGENT_IMAGE'
28
+ export const DEFAULT_IMAGE = 'localhost/shapewall-agent:latest'
29
+ export const EGRESS_NETWORK = 'shapewall-egress'
30
+ export const WORK_DIR = '/work'
31
+ export const HOME_DIR = '/tmp/home'
32
+ export const RUN_LABEL = 'shapewall.agent.run'
33
+ // The pid of the shapewall process that made it: the startup sweep removes what dead runs left.
34
+ export const OWNER_LABEL = 'shapewall.agent.owner'
35
+ // The agent runs from an execute-only copy of node (not dumpable, see Dockerfile.agent).
36
+ export const AGENT_ENTRY = ['/opt/shapewall/node-agent', '/opt/shapewall/bin/shapewall.js', 'agent', 'sandbox']
37
+ export const PROXY_ENTRY = ['node', '/opt/shapewall/src/agent/starter/egress-proxy.js']
38
+ export const PROBE_ENTRY = ['node', '/opt/shapewall/src/agent/starter/probe.js']
39
+ // This machine, as a container reaches it (only for a person's --allow-host).
40
+ export const HOST_GATEWAY = { podman: 'host.containers.internal', docker: 'host.docker.internal' }
41
+
42
+ // disk: the cap on the mounted folder, measured by the host (disk.js).
43
+ export const DEFAULT_LIMITS = { memory: '4g', cpus: 2, pids: 512, tmp: '1g', disk: '2g' }
44
+ const MEMORY_RE = /^[1-9]\d{0,5}[mg]$/
45
+ const IMAGE_RE = /^[a-z0-9][a-z0-9._/:@-]{0,254}$/
46
+ const RUN_ID_RE = /^[a-z0-9][a-z0-9-]{3,40}$/
47
+
48
+ // Which runtime: --runtime, then SHAPEWALL_AGENT_RUNTIME, then podman, then
49
+ // docker, whichever is on PATH. { name, bin } or { problem }.
50
+ export function detectRuntime({ flag = null, env = process.env, find = findProgram } = {}) {
51
+ const wanted = flag || env[RUNTIME_VAR] || null
52
+ if (wanted && !RUNTIMES.includes(wanted)) return { problem: `--runtime / ${RUNTIME_VAR}: podman or docker (got "${wanted}")` }
53
+ for (const name of wanted ? [wanted] : RUNTIMES) {
54
+ const bin = find(name, env)
55
+ if (bin) return { name, bin }
56
+ }
57
+ return {
58
+ problem: wanted
59
+ ? `${wanted} is not on PATH`
60
+ : 'neither podman nor docker is on PATH: install one (podman runs rootless by default) to use the starter agent',
61
+ }
62
+ }
63
+
64
+ // The image: --image, then SHAPEWALL_AGENT_IMAGE. Never from the config: the
65
+ // proxy runs from the same image, so a repo that chose it could choose what
66
+ // leaves the sandbox.
67
+ export function agentImage({ flag = null, env = process.env } = {}) {
68
+ const image = String(flag || env[IMAGE_VAR] || DEFAULT_IMAGE).trim()
69
+ if (!IMAGE_RE.test(image)) throw new Error(`--image / ${IMAGE_VAR}: "${image.slice(0, 80)}" is not an image name`)
70
+ return image
71
+ }
72
+
73
+ export function checkLimits(limits = {}) {
74
+ const value = { ...DEFAULT_LIMITS, ...limits }
75
+ if (!MEMORY_RE.test(String(value.memory))) throw new Error('memory: a size such as 2g or 1536m')
76
+ if (!MEMORY_RE.test(String(value.tmp))) throw new Error('tmp: a size such as 1g')
77
+ if (!MEMORY_RE.test(String(value.disk))) throw new Error('disk: a size such as 2g or 500m')
78
+ if (!(Number(value.cpus) > 0 && Number(value.cpus) <= 16)) throw new Error('cpus: a number from 0.1 to 16')
79
+ if (!(Number.isInteger(Number(value.pids)) && value.pids >= 32 && value.pids <= 4096)) throw new Error('pids: 32 to 4096')
80
+ return value
81
+ }
82
+
83
+ // A bind mount source as -v takes it: no ":" or "," (they separate options).
84
+ function mountSource(dir) {
85
+ if (/[:,\n]/.test(dir)) throw new Error(`the worktree path "${dir}" has ":" or ","; pick another --work-dir`)
86
+ return dir
87
+ }
88
+
89
+ const names = (runId) => {
90
+ if (!RUN_ID_RE.test(runId)) throw new Error('bad run id')
91
+ return { network: `shapewall-agent-${runId}`, agent: `shapewall-agent-${runId}`, proxy: `shapewall-egress-${runId}` }
92
+ }
93
+ export const runNames = names
94
+
95
+ export function networkCreateArgs(runId, owner = process.pid) {
96
+ return ['network', 'create', '--internal', '--label', `${RUN_LABEL}=${runId}`, '--label', `${OWNER_LABEL}=${owner}`, names(runId).network]
97
+ }
98
+
99
+ export function egressNetworkArgs() {
100
+ return ['network', 'create', '--label', 'shapewall.agent.egress=1', EGRESS_NETWORK]
101
+ }
102
+
103
+ // The proxy container, created on the egress network (then connected to the
104
+ // run network, then started attached so its log lines reach us).
105
+ export function proxyCreateArgs({ runtime, runId, image, allow, owner = process.pid }) {
106
+ const { proxy } = names(runId)
107
+ const args = [
108
+ 'create',
109
+ '--pull',
110
+ 'never',
111
+ '--name',
112
+ proxy,
113
+ '--network',
114
+ EGRESS_NETWORK,
115
+ '--label',
116
+ `${RUN_LABEL}=${runId}`,
117
+ '--label',
118
+ `${OWNER_LABEL}=${owner}`,
119
+ '--user',
120
+ '65534:65534',
121
+ '--read-only',
122
+ '--cap-drop',
123
+ 'ALL',
124
+ '--security-opt',
125
+ 'no-new-privileges',
126
+ '--memory',
127
+ '256m',
128
+ '--cpus',
129
+ '0.5',
130
+ '--pids-limit',
131
+ '64',
132
+ '--init',
133
+ '--env',
134
+ `SHAPEWALL_EGRESS_ALLOW=${allow.map(formatAllowEntry).join(',')}`,
135
+ '--env',
136
+ `SHAPEWALL_EGRESS_PORT=${PROXY_PORT}`,
137
+ ]
138
+ // Docker needs to be told the host's name; podman adds host.containers.internal itself.
139
+ if (runtime === 'docker' && allow.some((entry) => entry.host === HOST_GATEWAY.docker)) {
140
+ args.push('--add-host', `${HOST_GATEWAY.docker}:host-gateway`)
141
+ }
142
+ args.push(image, ...PROXY_ENTRY)
143
+ return args
144
+ }
145
+
146
+ // The fixed environment of the agent container: where home and caches are,
147
+ // and the proxy. Nothing from the host.
148
+ export function agentEnv(runId) {
149
+ const proxy = `http://${names(runId).proxy}:${PROXY_PORT}`
150
+ return {
151
+ HOME: HOME_DIR,
152
+ TMPDIR: '/tmp',
153
+ CI: '1',
154
+ HTTPS_PROXY: proxy,
155
+ HTTP_PROXY: proxy,
156
+ https_proxy: proxy,
157
+ http_proxy: proxy,
158
+ NO_PROXY: '',
159
+ no_proxy: '',
160
+ // Node's fetch and http honour the proxy variables only with this.
161
+ NODE_USE_ENV_PROXY: '1',
162
+ npm_config_cache: '/tmp/npm-cache',
163
+ npm_config_update_notifier: 'false',
164
+ PLAYWRIGHT_BROWSERS_PATH: '/ms-playwright',
165
+ SHAPEWALL_SANDBOX: '1',
166
+ }
167
+ }
168
+
169
+ // The agent container. `uid`/`gid` are the host user's (the worktree's
170
+ // owner), so files the agent writes are yours; with podman, keep-id maps
171
+ // them one to one in a rootless user namespace.
172
+ // `loginVolume` (the claude-code and codex profiles, not available yet)
173
+ // would mount a named volume as the home folder for the CLI's login.
174
+ export function agentRunArgs({
175
+ runtime,
176
+ runId,
177
+ image,
178
+ worktree,
179
+ uid,
180
+ gid,
181
+ limits = {},
182
+ loginVolume = null,
183
+ entry = AGENT_ENTRY,
184
+ owner = process.pid,
185
+ }) {
186
+ if (!Number.isInteger(uid) || !Number.isInteger(gid)) throw new Error('the host uid and gid are needed')
187
+ if (uid === 0) throw new Error('refusing to run the starter agent as root: run shapewall as your own user')
188
+ const limit = checkLimits(limits)
189
+ const { network, agent } = names(runId)
190
+ const args = [
191
+ 'run',
192
+ '--rm',
193
+ '-i',
194
+ '--pull',
195
+ 'never',
196
+ '--name',
197
+ agent,
198
+ '--hostname',
199
+ 'shapewall-agent',
200
+ '--network',
201
+ network,
202
+ '--label',
203
+ `${RUN_LABEL}=${runId}`,
204
+ '--label',
205
+ `${OWNER_LABEL}=${owner}`,
206
+ ]
207
+ if (runtime === 'podman') args.push('--userns', 'keep-id')
208
+ args.push(
209
+ '--user',
210
+ `${uid}:${gid}`,
211
+ '--read-only',
212
+ '--tmpfs',
213
+ `/tmp:rw,nosuid,nodev,size=${limit.tmp}`,
214
+ '--cap-drop',
215
+ 'ALL',
216
+ '--security-opt',
217
+ 'no-new-privileges',
218
+ '--memory',
219
+ limit.memory,
220
+ '--memory-swap',
221
+ limit.memory,
222
+ '--cpus',
223
+ String(limit.cpus),
224
+ '--pids-limit',
225
+ String(limit.pids),
226
+ '--shm-size',
227
+ '256m',
228
+ '--ipc',
229
+ 'private',
230
+ // No --init: the agent itself is pid 1. An init process would hold the
231
+ // line protocol's stdout too, and unlike the agent (see Dockerfile.agent)
232
+ // anything running as the same user could open it through /proc.
233
+ '--stop-timeout',
234
+ '5',
235
+ '-v',
236
+ `${mountSource(worktree)}:${WORK_DIR}:rw,Z`,
237
+ )
238
+ if (loginVolume) {
239
+ if (!/^shapewall-agent-[a-z-]{1,40}$/.test(loginVolume)) throw new Error('bad login volume name')
240
+ args.push('-v', `${loginVolume}:/home/agent:rw`)
241
+ }
242
+ args.push('--workdir', WORK_DIR)
243
+ for (const [name, value] of Object.entries(agentEnv(runId))) args.push('--env', `${name}=${value}`)
244
+ args.push(image, ...entry)
245
+ return args
246
+ }
247
+
248
+ // A thin wrapper around the runtime's CLI. `exec` runs one command and
249
+ // collects its output; `spawn` starts a long one (the containers). The CLI
250
+ // itself gets the host environment it needs to find its socket or rootless
251
+ // state (XDG_RUNTIME_DIR, DOCKER_HOST, ...); containers do not.
252
+ export function createRuntime({ name, bin, env = process.env, spawnImpl = spawn }) {
253
+ const childEnv = { ...env }
254
+ return {
255
+ name,
256
+ bin,
257
+ exec(args, { input = null, timeoutMs = 60000 } = {}) {
258
+ return new Promise((resolve) => {
259
+ let child
260
+ try {
261
+ child = spawnImpl(bin, args, { env: childEnv, stdio: ['pipe', 'pipe', 'pipe'], shell: false, windowsHide: true })
262
+ } catch (error) {
263
+ resolve({ code: null, stdout: '', stderr: error.message })
264
+ return
265
+ }
266
+ let stdout = ''
267
+ let stderr = ''
268
+ const timer = setTimeout(() => child.kill('SIGKILL'), timeoutMs)
269
+ child.stdout.on('data', (chunk) => (stdout = (stdout + chunk).slice(-200000)))
270
+ child.stderr.on('data', (chunk) => (stderr = (stderr + chunk).slice(-20000)))
271
+ child.stdin.on('error', () => {})
272
+ child.stdin.end(input ?? undefined)
273
+ child.once('error', (error) => {
274
+ clearTimeout(timer)
275
+ resolve({ code: null, stdout, stderr: stderr || error.message })
276
+ })
277
+ child.once('close', (code) => {
278
+ clearTimeout(timer)
279
+ resolve({ code, stdout, stderr })
280
+ })
281
+ })
282
+ },
283
+ spawn(args) {
284
+ return spawnImpl(bin, args, { env: childEnv, stdio: ['pipe', 'pipe', 'pipe'], shell: false, windowsHide: true })
285
+ },
286
+ }
287
+ }
@@ -0,0 +1,121 @@
1
+ // `shapewall agent sandbox`: the starter agent inside its container. Not for
2
+ // people to run: `shapewall agent run` starts it and talks to it over stdin
3
+ // and stdout (rpc.js).
4
+ //
5
+ // It reads the task, runs the optional setup command, then the turn loop
6
+ // with the fixed tools, and ends with one `result` line. The model is called
7
+ // through the host (rpc.js, method "model"): there is no API key in here, so
8
+ // no command the agent runs, approved or not, can read one.
9
+
10
+ import { createOpenAIBackend } from '../backends/openai.js'
11
+ import { quoteData } from '../context.js'
12
+ import { runLoop } from './loop.js'
13
+ import { starterSystemPrompt } from './prompt.js'
14
+ import { createCaller, readLines, writeLine } from './rpc.js'
15
+ import { createTools, runProgram } from './tools.js'
16
+
17
+ const SETUP_MS_MAX = 15 * 60 * 1000
18
+
19
+ export async function runSandbox({ input = process.stdin, output = process.stdout, root = '/work', now = () => Date.now() } = {}) {
20
+ const caller = createCaller(output)
21
+ const controller = new AbortController()
22
+ const task = await new Promise((resolve, reject) => {
23
+ let first = true
24
+ readLines(input, (message) => {
25
+ if (first) {
26
+ first = false
27
+ if (message.t !== 'task') reject(new Error('the first message must be the task'))
28
+ else resolve(message)
29
+ return
30
+ }
31
+ if (['answer', 'head', 'chunk', 'end'].includes(message.t)) caller.handle(message)
32
+ else if (message.t === 'stop') {
33
+ controller.abort(message.reason || 'stopped')
34
+ caller.cancel('the run was stopped')
35
+ }
36
+ })
37
+ input.on('end', () => {
38
+ if (first) reject(new Error('no task on stdin'))
39
+ controller.abort('stopped')
40
+ caller.cancel('the run was stopped')
41
+ })
42
+ })
43
+ const emit = (event) => writeLine(output, { t: 'event', ...event })
44
+ const started = now()
45
+ const deadline = started + Math.max(1000, Number(task.budget?.wallMs) || 60000)
46
+ const remainingMs = () => Math.max(1000, deadline - now())
47
+ const settings = {
48
+ // Not reached: the host sends the request to the endpoint it was configured with.
49
+ baseUrl: 'http://model.shapewall.invalid/v1',
50
+ model: task.model.model,
51
+ keyVar: null,
52
+ tools: true,
53
+ vision: false,
54
+ usage: true,
55
+ temperature: task.model.temperature ?? null,
56
+ answerTokens: task.model.maxTokens || null,
57
+ maxTokens: null,
58
+ // The host times each call (and retries it once), and always ends it.
59
+ connectTimeoutMs: 0,
60
+ }
61
+ const backend = createOpenAIBackend(settings, { env: {}, fetchImpl: caller.fetch })
62
+ const tools = createTools({
63
+ root,
64
+ rpc: caller,
65
+ testCommand: task.tools?.testCommand || null,
66
+ capture: Boolean(task.tools?.capture),
67
+ projectDir: typeof task.tools?.projectDir === 'string' ? task.tools.projectDir : '.',
68
+ shell: Boolean(task.tools?.shell),
69
+ remainingMs,
70
+ signal: controller.signal,
71
+ })
72
+ let setup = null
73
+ if (task.tools?.setupCommand) {
74
+ emit({ type: 'tool', id: 'setup', name: 'setup', label: 'Set up the project', state: 'running' })
75
+ const result = await runProgram(task.tools.setupCommand, {
76
+ cwd: root,
77
+ timeoutMs: Math.min(SETUP_MS_MAX, remainingMs()),
78
+ signal: controller.signal,
79
+ })
80
+ setup = { ok: result.code === 0, code: result.code, output: result.output.slice(-4000) }
81
+ emit({
82
+ type: 'tool',
83
+ id: 'setup',
84
+ name: 'setup',
85
+ label: 'Set up the project',
86
+ state: setup.ok ? 'done' : 'error',
87
+ detail: setup.ok ? 'done' : `exit ${result.code}`,
88
+ })
89
+ }
90
+ // The setup command's output is the repo's (install scripts print what
91
+ // they like): quoted as data, never as words of the task.
92
+ const taskText =
93
+ setup && !setup.ok
94
+ ? `${task.text}\n\n[Shapewall] The setup command failed (exit ${setup.code}). The end of its output is data:\n${quoteData('setup', { exit: setup.code }, { output: setup.output })}`
95
+ : task.text
96
+ const outcome = await runLoop({
97
+ backend,
98
+ settings,
99
+ system: starterSystemPrompt({ tools: tools.names }),
100
+ task: taskText,
101
+ tools,
102
+ budget: { turns: task.budget.turns, tokens: task.budget.tokens },
103
+ deadline,
104
+ emit,
105
+ signal: controller.signal,
106
+ now,
107
+ })
108
+ writeLine(output, {
109
+ t: 'result',
110
+ stop: outcome.stop,
111
+ text: outcome.text,
112
+ error: outcome.error || null,
113
+ usage: outcome.usage,
114
+ status: tools.state.status,
115
+ statusNote: tools.state.statusNote,
116
+ changed: [...tools.state.changed],
117
+ setup: setup ? { ok: setup.ok, code: setup.code } : null,
118
+ ms: now() - started,
119
+ })
120
+ return outcome
121
+ }