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,755 @@
1
+ // `shapewall agent run`: one comment, one sandboxed run, one branch.
2
+ //
3
+ // On the host, in order:
4
+ // 1. the trigger gate (gate.js): guests never, untrusted authors only when
5
+ // a trusted member tagged @agent;
6
+ // 2. claim the comment (agent status null -> queued, a compare-and-set),
7
+ // then working;
8
+ // 3. a fresh checkout of the commit, with no .git in it (worktree.js);
9
+ // 4. the run network (internal), the egress proxy on it, and the agent
10
+ // container with only the worktree mounted (runtime.js);
11
+ // 5. the task on the container's stdin (no secret in it), then serve its
12
+ // calls: model calls (model-relay.js: the host adds the key, talks to
13
+ // the configured endpoint only and enforces the turn and token budget),
14
+ // replies on the comment, and approvals for shell commands (a card in
15
+ // the comment's chat thread, approvals.js);
16
+ // 6. stop it at the wall time, whatever it is doing;
17
+ // 7. commit what changed on the branch, optionally push it and open a pull
18
+ // request, reply on the comment with the outcome and the budget used,
19
+ // and set the agent status (done or needs-human);
20
+ // 8. remove the containers, the network and the worktree (the branch stays).
21
+
22
+ import fs from 'node:fs'
23
+ import { isIP } from 'node:net'
24
+ import path from 'node:path'
25
+ import { newId } from '../threads.js'
26
+ import { appendAssistantMessage, changeMessage } from '../threads.js'
27
+ import { agentKey } from '../config.js'
28
+ import { redact } from '../backends/openai.js'
29
+ import { boardDir } from '../../store.js'
30
+ import { approvalLink, commandHash, createApproval, showCommand, updateCard, waitForApproval, APPROVAL_MS } from './approvals.js'
31
+ import { privateAddress } from './egress-proxy.js'
32
+ import { gateComment, commentForTask } from './gate.js'
33
+ import { createModelRelay } from './model-relay.js'
34
+ import { taskMessage } from './prompt.js'
35
+ import { readLines, writeLine } from './rpc.js'
36
+ import { EGRESS_NETWORK, agentRunArgs, egressNetworkArgs, networkCreateArgs, proxyCreateArgs, runNames } from './runtime.js'
37
+ import { REPLIES_MAX, REPLY_MAX, SHELL_COMMAND_MAX } from './tools.js'
38
+ import { branchName, commitWorktree, createWorktree, newRunId, pushBranch, removeWorktree, workRoot } from './worktree.js'
39
+ import { starterSummary } from '../turn-changes.js'
40
+ import { guardedFiles } from './push-guard.js'
41
+ import { formatBytes, sizeBytes, watchDisk } from './disk.js'
42
+
43
+ const KILL_GRACE_MS = 5000
44
+ const HOST_GRACE_MS = 20000
45
+ const LOG_MAX = 2 * 1024 * 1024
46
+ const SUMMARY_MAX = 1100
47
+ // Run log bounds: a command's text, a turn's answer text, a tool call's arguments.
48
+ const COMMAND_LOG_MAX = 2000
49
+ const TEXT_LOG_MAX = 4000
50
+ const ARGS_LOG_MAX = 600
51
+ const SENSITIVE_RE = /(^|\/)(\.[^/]+|package\.json|package-lock\.json|Makefile|Dockerfile|justfile|shapewall\.config\.json)(\/|$)/i
52
+
53
+ export class StarterError extends Error {
54
+ constructor(code, message) {
55
+ super(message)
56
+ this.code = code
57
+ }
58
+ }
59
+
60
+ // The allow list: the registry and the person's extra hosts. The model
61
+ // endpoint is not on it: model calls go through the host (model-relay.js).
62
+ export function egressAllowList(settings) {
63
+ const out = []
64
+ for (const entry of [settings.registry, ...settings.extraHosts].filter(Boolean)) {
65
+ if (!out.some((item) => item.host === entry.host && item.port === entry.port)) out.push(entry)
66
+ }
67
+ return out
68
+ }
69
+
70
+ // Whether `shapewall capture` in the sandbox can reach the site at baseUrl:
71
+ // only through the proxy, so its host:port must be on the allow list, and
72
+ // never a loopback name or address (in the container that is the container
73
+ // itself) or a private address the person did not allow as private. Decided
74
+ // before the run, so the model is not offered a tool that cannot work.
75
+ export function captureReachable(baseUrl, allow) {
76
+ let url
77
+ try {
78
+ url = new URL(baseUrl)
79
+ } catch {
80
+ return false
81
+ }
82
+ if (!['http:', 'https:'].includes(url.protocol)) return false
83
+ const host = url.hostname.replace(/^\[|\]$/g, '').toLowerCase()
84
+ const port = Number(url.port) || (url.protocol === 'https:' ? 443 : 80)
85
+ if (host === 'localhost' || host.endsWith('.localhost')) return false
86
+ if (isIP(host) && /^(127\.|0\.)|^::1?$|^::ffff:127\./.test(host)) return false
87
+ const entry = allow.find((item) => item.host.toLowerCase().replace(/^\[|\]$/g, '') === host && item.port === port)
88
+ if (!entry) return false
89
+ return !(isIP(host) && privateAddress(host) && !entry.private)
90
+ }
91
+
92
+ const plural = (count, word) => `${count.toLocaleString('en-US')} ${word}${count === 1 ? '' : 's'}`
93
+ export function formatDuration(ms) {
94
+ const seconds = Math.max(0, Math.round(ms / 1000))
95
+ const minutes = Math.floor(seconds / 60)
96
+ return minutes ? `${minutes}m ${String(seconds % 60).padStart(2, '0')}s` : `${seconds}s`
97
+ }
98
+
99
+ const STOP_TEXT = {
100
+ answered: null,
101
+ turns: 'Stopped: the turn budget ran out.',
102
+ tokens: 'Stopped: the token budget ran out.',
103
+ time: 'Stopped: the time budget ran out.',
104
+ stopped: 'Stopped before it finished.',
105
+ error: 'Stopped: the model endpoint failed.',
106
+ crashed: 'Stopped: the sandbox ended without a result.',
107
+ disk: 'Stopped: the checkout went over its disk limit, so nothing was committed.',
108
+ }
109
+
110
+ // "Stopped: the model endpoint failed (could not reach it: timed out after 180 s)."
111
+ // One sentence, the reason in brackets, whatever punctuation the reason had.
112
+ function stopLine(text, reason) {
113
+ const detail = String(reason || '')
114
+ .trim()
115
+ .slice(0, 160)
116
+ .replace(/[.\s]+$/, '')
117
+ if (!detail) return text
118
+ return `${text.replace(/\.$/, '')} (${detail.charAt(0).toLowerCase()}${detail.slice(1)}).`
119
+ }
120
+
121
+ // The reply posted on the comment when the run ends.
122
+ export function outcomeReply({ result, status, branch, commit, pr, budget, ms, blocked }) {
123
+ const lines = []
124
+ const head = status === 'done' ? (commit ? 'made a change for review' : 'finished without changing files') : 'needs a person'
125
+ lines.push(`Starter agent ${head}.`)
126
+ const summary = String(result?.text || '').trim()
127
+ if (summary) lines.push('', summary.length > SUMMARY_MAX ? `${summary.slice(0, SUMMARY_MAX - 3)}...` : summary)
128
+ if (result?.statusNote) lines.push('', `Note: ${String(result.statusNote).slice(0, 300)}`)
129
+ lines.push('')
130
+ if (commit) {
131
+ const shown = commit.files.slice(0, 6).join(', ')
132
+ lines.push(`Branch: ${branch} (${plural(commit.files.length, 'file')}: ${shown}${commit.files.length > 6 ? ', ...' : ''})`)
133
+ const sensitive = commit.files.filter((file) => SENSITIVE_RE.test(file))
134
+ if (sensitive.length) lines.push(`Review with care, it changed files that decide what runs: ${sensitive.slice(0, 5).join(', ')}`)
135
+ if (commit.links?.length)
136
+ lines.push(`Review with care, it made symlinks that point outside the repository: ${commit.links.slice(0, 5).join(', ')}`)
137
+ // A small model that could not find the file often makes a new one instead.
138
+ if (commit.added?.length && commit.added.length === commit.files.length)
139
+ lines.push('Note: this run only added new files and changed no existing file; check it edited the right place.')
140
+ } else lines.push('No files changed.')
141
+ if (pr?.url) lines.push(`Pull request: ${pr.url}`)
142
+ else if (pr?.error) lines.push(`No pull request: ${pr.error}`)
143
+ const usage = result?.usage || { turns: 0, tokens: 0 }
144
+ lines.push(
145
+ `Budget: ${usage.turns} of ${budget.turns} turns, ${usage.tokens.toLocaleString('en-US')} of ${budget.tokens.toLocaleString('en-US')} tokens${usage.estimated ? ' (estimated)' : ''}, ${formatDuration(ms)} of ${budget.minutes}m.`,
146
+ )
147
+ const stopText = STOP_TEXT[result?.stop || 'crashed']
148
+ if (stopText) lines.push(stopLine(stopText, result?.error?.message))
149
+ if (blocked.length) {
150
+ lines.push(
151
+ `Blocked network: ${blocked
152
+ .slice(0, 4)
153
+ .map((item) => `${item.host}:${item.port}`)
154
+ .join(', ')}${blocked.length > 4 ? ', ...' : ''}`,
155
+ )
156
+ }
157
+ return lines.join('\n').slice(0, REPLY_MAX)
158
+ }
159
+
160
+ // The final agent status: what the model asked for when it answered, else
161
+ // done when it changed something, needs-human otherwise.
162
+ export function finalStatus(result, commit) {
163
+ if (!result || result.stop !== 'answered') return 'needs-human'
164
+ if (result.status === 'needs-human' || result.status === 'done') return result.status
165
+ return commit ? 'done' : 'needs-human'
166
+ }
167
+
168
+ async function ensureEgressNetwork(runtime) {
169
+ const found = await runtime.exec(['network', 'inspect', EGRESS_NETWORK])
170
+ if (found.code === 0) return
171
+ const made = await runtime.exec(egressNetworkArgs())
172
+ if (made.code !== 0 && !/exist/i.test(made.stderr))
173
+ throw new StarterError('runtime', `could not create the ${EGRESS_NETWORK} network: ${made.stderr.trim().slice(0, 300)}`)
174
+ }
175
+
176
+ // Waits for the proxy's { ready } line. Collects its allow/deny log.
177
+ function watchProxy(child, { onDeny, timeoutMs }) {
178
+ return new Promise((resolve, reject) => {
179
+ let ready = false
180
+ const timer = setTimeout(() => reject(new StarterError('runtime', 'the egress proxy did not start')), timeoutMs)
181
+ readLines(child.stdout, (line) => {
182
+ if (line.ready && !ready) {
183
+ ready = true
184
+ clearTimeout(timer)
185
+ resolve()
186
+ } else if (line.deny)
187
+ onDeny({
188
+ host: String(line.host || '').slice(0, 255),
189
+ port: Number(line.port) || 0,
190
+ reason: String(line.reason || '').slice(0, 200),
191
+ })
192
+ })
193
+ child.stderr?.resume?.()
194
+ child.once('error', (error) => {
195
+ clearTimeout(timer)
196
+ reject(new StarterError('runtime', `the egress proxy did not start (${error.message})`))
197
+ })
198
+ child.once('close', (code) => {
199
+ clearTimeout(timer)
200
+ if (!ready) reject(new StarterError('runtime', `the egress proxy exited (${code})`))
201
+ })
202
+ })
203
+ }
204
+
205
+ // The run network and its egress proxy, up and listening. Pushes the steps
206
+ // that undo it onto `cleanup` (run in order: containers first, then the network).
207
+ export async function startEgress({ runtime, runId, image, allow, cleanup, readyTimeoutMs = 60000, onDeny = () => {} }) {
208
+ const names = runNames(runId)
209
+ await ensureEgressNetwork(runtime)
210
+ const network = await runtime.exec(networkCreateArgs(runId))
211
+ if (network.code !== 0) throw new StarterError('runtime', `could not create the run network: ${network.stderr.trim().slice(0, 300)}`)
212
+ cleanup.push(() => runtime.exec(['network', 'rm', names.network]))
213
+ const created = await runtime.exec(proxyCreateArgs({ runtime: runtime.name, runId, image, allow }))
214
+ cleanup.unshift(() => runtime.exec(['rm', '-f', names.proxy]))
215
+ if (created.code !== 0) throw new StarterError('runtime', `could not create the egress proxy: ${created.stderr.trim().slice(0, 300)}`)
216
+ const connected = await runtime.exec(['network', 'connect', names.network, names.proxy])
217
+ if (connected.code !== 0)
218
+ throw new StarterError('runtime', `could not connect the egress proxy: ${connected.stderr.trim().slice(0, 300)}`)
219
+ const proxy = runtime.spawn(['start', '-a', names.proxy])
220
+ proxy.stdin?.end?.()
221
+ await watchProxy(proxy, { timeoutMs: readyTimeoutMs, onDeny })
222
+ return proxy
223
+ }
224
+
225
+ const APPROVALS_MAX = 10
226
+ const CARDS_MAX = 200
227
+ const STOPS = new Set(['answered', 'turns', 'tokens', 'time', 'stopped', 'error', 'crashed'])
228
+ const DISK_EVERY_MS = 5000
229
+
230
+ // The container's result line, kept only as far as it checks out.
231
+ export function containerResult(message) {
232
+ const text = (value, max) => (typeof value === 'string' ? value.slice(0, max) : null)
233
+ return {
234
+ stop: STOPS.has(message.stop) ? message.stop : 'crashed',
235
+ text: text(message.text, 8000) || '',
236
+ status: ['done', 'needs-human'].includes(message.status) ? message.status : null,
237
+ statusNote: text(message.statusNote, 300),
238
+ error: message.error && typeof message.error === 'object' ? { message: text(message.error.message, 300) || 'error' } : null,
239
+ setup: message.setup && typeof message.setup === 'object' ? { ok: Boolean(message.setup.ok) } : null,
240
+ }
241
+ }
242
+
243
+ // Removes the agent container; true once it is gone (removed now, or not there).
244
+ async function agentRemoved(runtime, name) {
245
+ const removed = await runtime.exec(['rm', '-f', name]).catch(() => ({ code: null, stderr: '' }))
246
+ if (removed.code === 0) return true
247
+ const found = await runtime.exec(['inspect', '--format', '{{.Id}}', name]).catch(() => ({ code: null, stderr: '' }))
248
+ return found.code !== 0 && /no such|not found|does not exist/i.test(String(found.stderr || ''))
249
+ }
250
+
251
+ // Refusal reasons from the gate, as an error.
252
+ function gateError(id, gate) {
253
+ return new StarterError(gate.final ? 'refused' : 'not_dispatched', `#${id} is not dispatched: ${gate.reason}`)
254
+ }
255
+
256
+ export async function runStarter({
257
+ source,
258
+ settings,
259
+ runtime,
260
+ image,
261
+ commentId,
262
+ projectDir,
263
+ outputDir = null,
264
+ workDir = null,
265
+ env = process.env,
266
+ claimFrom = [null],
267
+ overrideGate = false,
268
+ approvals = true,
269
+ pr = null,
270
+ // --allow-ci-push: push even when the change touches CI config or manifests.
271
+ allowGuardedPush = false,
272
+ log = () => {},
273
+ signal = null,
274
+ uid = process.getuid?.() ?? 1000,
275
+ gid = process.getgid?.() ?? 1000,
276
+ now = () => Date.now(),
277
+ approvalMs = APPROVAL_MS,
278
+ readyTimeoutMs = 60000,
279
+ // Tests only: a shorter wall time and host grace than the budget's minutes.
280
+ wallMs: wallMsOverride = null,
281
+ hostGraceMs = HOST_GRACE_MS,
282
+ // Tests only: how often, and how, the disk cap measures the folder.
283
+ diskEveryMs = DISK_EVERY_MS,
284
+ diskMeasure = undefined,
285
+ // The host's fetch for model calls (tests: the stub model).
286
+ fetchImpl = globalThis.fetch,
287
+ }) {
288
+ if (settings.problem) throw new StarterError('not_configured', `The starter agent is not ready: ${settings.problem}`)
289
+ const id = Number(commentId)
290
+ const comment = await source.get(id)
291
+ const gate = gateComment(comment, { trusted: settings.trustedFor(source.board.id), hosted: source.hosted })
292
+ if (!gate.dispatch && (source.hosted || gate.final || !overrideGate)) throw gateError(id, gate)
293
+ const { changed } = await source.claim(id, claimFrom)
294
+ if (!changed)
295
+ throw new StarterError(
296
+ 'taken',
297
+ `#${id} is already taken by an agent (status ${comment.agentStatus || 'changed'}); clear it with "shapewall comments status ${id} clear"`,
298
+ )
299
+
300
+ const runId = newRunId(new Date(now()))
301
+ const names = runNames(runId)
302
+ const budget = settings.budget
303
+ const wallMs = wallMsOverride ?? budget.minutes * 60 * 1000
304
+ const started = now()
305
+ const threadId = `comment-${id}`
306
+ const local = !source.hosted && source.config
307
+ let logFile
308
+ let logFd
309
+ try {
310
+ const logDir = local ? path.join(boardDir(source.config, source.board.id), 'agent') : workRoot(projectDir, { flag: workDir, env })
311
+ fs.mkdirSync(logDir, { recursive: true, mode: 0o700 })
312
+ logFile = path.join(logDir, `starter-${id}-${runId}.log`)
313
+ logFd = fs.openSync(logFile, 'w', 0o600)
314
+ } catch (error) {
315
+ // Claimed but never started: hand the comment back instead of leaving it queued.
316
+ await Promise.resolve()
317
+ .then(() => source.setStatus(id, 'needs-human'))
318
+ .catch(() => {})
319
+ throw error
320
+ }
321
+ let logBytes = 0
322
+ // The key never enters the container, but the log takes text from it
323
+ // (model answers, tool output, stderr): it is removed from every line anyway.
324
+ const logKey = settings.model.keyVar ? agentKey({ keyVar: settings.model.keyVar }, env) : null
325
+ let logClosed = false
326
+ const writeLog = (text) => {
327
+ if (logBytes > LOG_MAX || logClosed) return
328
+ // Control characters escaped: `cat` of the log must not run terminal escapes from the container.
329
+ const clean = showCommand(redact(text, logKey))
330
+ logBytes += Buffer.byteLength(clean)
331
+ try {
332
+ fs.writeSync(logFd, clean)
333
+ } catch {
334
+ /* closed */
335
+ }
336
+ }
337
+ // The answer text of the turn being streamed, for the log (bounded per turn).
338
+ let turnText = ''
339
+ let turnCut = 0
340
+ let turn = 0
341
+ const flushText = () => {
342
+ if (turnText.trim())
343
+ writeLog(`[model] text (turn ${turn}): ${JSON.stringify(turnText)}${turnCut ? ` [${turnCut} more characters]` : ''}\n`)
344
+ turnText = ''
345
+ turnCut = 0
346
+ }
347
+ writeLog(
348
+ `[run] start ${new Date(now()).toISOString()} comment ${source.board.id}/${id} run ${runId}, model ${settings.model.model} at ${settings.model.host || 'the configured endpoint'}, budget ${budget.turns} turns, ${budget.tokens} tokens, ${budget.minutes}m${settings.modelTimeoutMs ? `, model timeout ${Math.round(settings.modelTimeoutMs / 1000)} s` : ''}\n`,
349
+ )
350
+ const blocked = []
351
+ const controller = new AbortController()
352
+ const stopRun = (reason) => controller.abort(reason)
353
+ const onOuterAbort = () => stopRun('stopped')
354
+ signal?.addEventListener('abort', onOuterAbort, { once: true })
355
+
356
+ // Progress in the comment's chat thread (local boards): one message, updated.
357
+ let messageId = null
358
+ const cards = new Map()
359
+ // One write at a time, in order, so the last state of a card wins.
360
+ let cardWrites = Promise.resolve()
361
+ const showCard = (card) => {
362
+ if (!messageId) return cardWrites
363
+ cards.set(card.id, { ...(cards.get(card.id) || {}), ...card })
364
+ const value = cards.get(card.id)
365
+ cardWrites = cardWrites.then(() =>
366
+ changeMessage(source.config, source.board.id, threadId, messageId, (message) => {
367
+ const list = Array.isArray(message.cards) ? message.cards : []
368
+ const index = list.findIndex((item) => item.id === value.id)
369
+ if (index >= 0) list[index] = value
370
+ else list.push(value)
371
+ message.cards = list
372
+ }).catch(() => {}),
373
+ )
374
+ return cardWrites
375
+ }
376
+ if (local) {
377
+ try {
378
+ const message = await appendAssistantMessage(source.config, source.board.id, threadId, {
379
+ id: newId('msg'),
380
+ role: 'assistant',
381
+ text: `Starter agent is working on this comment (run ${runId}).`,
382
+ cards: [],
383
+ model: settings.model.model,
384
+ status: 'done',
385
+ starter: { runId, state: 'running' },
386
+ createdAt: new Date(now()).toISOString(),
387
+ })
388
+ messageId = message.id
389
+ } catch (error) {
390
+ log(`#${id}: could not write the chat thread (${error.message})`)
391
+ }
392
+ }
393
+
394
+ let tree = null
395
+ let relay = null
396
+ let result = null
397
+ let commit = null
398
+ let pull = null
399
+ let status = 'needs-human'
400
+ let failure = null
401
+ let agentSpawned = false
402
+ let agentGone = false
403
+ const cleanup = []
404
+ try {
405
+ await source.setStatus(id, 'working')
406
+ tree = await createWorktree({
407
+ projectDir,
408
+ root: workRoot(projectDir, { flag: workDir, env }),
409
+ runId,
410
+ branch: branchName(settings.branchPrefix, source.board.id, id, runId),
411
+ })
412
+ log(`#${id}: checkout ${tree.dir} for ${tree.branch}`)
413
+ const allow = egressAllowList(settings)
414
+
415
+ await startEgress({
416
+ runtime,
417
+ runId,
418
+ image,
419
+ allow,
420
+ cleanup,
421
+ readyTimeoutMs,
422
+ onDeny: (entry) => {
423
+ writeLog(`[egress] denied ${entry.host}:${entry.port} (${entry.reason})\n`)
424
+ const known = blocked.find((item) => item.host === entry.host && item.port === entry.port)
425
+ if (known) known.count += 1
426
+ else if (blocked.length < 50) blocked.push({ ...entry, count: 1 })
427
+ },
428
+ })
429
+ log(`#${id}: egress proxy up, allowing ${allow.map((entry) => `${entry.host}:${entry.port}`).join(', ')}`)
430
+
431
+ const args = agentRunArgs({ runtime: runtime.name, runId, image, worktree: tree.dir, uid, gid, limits: settings.limits })
432
+ cleanup.unshift(() => runtime.exec(['rm', '-f', names.agent]))
433
+ const child = runtime.spawn(args)
434
+ agentSpawned = true
435
+ child.stdin.on('error', () => {})
436
+ child.stderr.on('data', (chunk) => writeLog(String(chunk)))
437
+ const projectSub = path.relative(tree.dir, tree.projectDir) || '.'
438
+ const send = (value) => {
439
+ if (!child.stdin.destroyed) writeLine(child.stdin, value)
440
+ }
441
+ relay = createModelRelay({
442
+ settings,
443
+ env,
444
+ budget,
445
+ send,
446
+ fetchImpl,
447
+ deadline: started + wallMs,
448
+ now,
449
+ log: (line) => writeLog(`[model] ${line}\n`),
450
+ })
451
+ const capture = captureReachable(source.board.baseUrl, allow)
452
+ const shell = Boolean(approvals && (local || source.approveCommand))
453
+ writeLog(
454
+ `[run] tools: capture ${capture ? 'on' : `off (${source.board.baseUrl || 'no baseUrl'} is not reachable from the sandbox)`}, run_shell ${shell ? 'on (with approval)' : 'off'}, run_tests ${settings.testCommand ? 'on' : 'off'}\n`,
455
+ )
456
+ writeLine(child.stdin, {
457
+ t: 'task',
458
+ // No key and no endpoint: model calls go through `relay`, here.
459
+ model: { model: settings.model.model, maxTokens: settings.model.maxTokens, temperature: settings.model.temperature },
460
+ budget: { turns: budget.turns, tokens: budget.tokens, wallMs },
461
+ tools: {
462
+ testCommand: settings.testCommand,
463
+ setupCommand: settings.setupCommand,
464
+ capture,
465
+ projectDir: projectSub,
466
+ shell,
467
+ },
468
+ text: taskMessage({ board: source.board, comment: commentForTask(comment), gate: gate.dispatch ? gate : null }),
469
+ })
470
+
471
+ let replies = 0
472
+ let cardsShown = 0
473
+ const answer = (callId, value) => send({ t: 'answer', id: callId, result: value })
474
+ const handleCall = async (message) => {
475
+ const params = message.params && typeof message.params === 'object' ? message.params : {}
476
+ if (message.method === 'model') return relay.handle(message)
477
+ if (message.method === 'reply') {
478
+ const body = typeof params.body === 'string' ? params.body.trim().slice(0, REPLY_MAX) : ''
479
+ if (!body || replies >= REPLIES_MAX) return answer(message.id, { ok: false, error: body ? 'too many replies' : 'empty reply' })
480
+ replies += 1
481
+ try {
482
+ await source.reply(id, body)
483
+ return answer(message.id, { ok: true })
484
+ } catch (error) {
485
+ return answer(message.id, { ok: false, error: String(error.message).slice(0, 200) })
486
+ }
487
+ }
488
+ if (message.method === 'approve') {
489
+ if (approving || approvalsAsked >= APPROVALS_MAX)
490
+ return answer(message.id, {
491
+ approved: false,
492
+ error: approving ? 'one approval at a time' : 'too many approvals asked in this run',
493
+ })
494
+ approving = true
495
+ approvalsAsked += 1
496
+ try {
497
+ return await askApproval(message, params)
498
+ } finally {
499
+ approving = false
500
+ }
501
+ }
502
+ return answer(message.id, { ok: false, error: 'unknown call' })
503
+ }
504
+ let approving = false
505
+ let approvalsAsked = 0
506
+ const askApproval = async (message, params) => {
507
+ const command = typeof params.command === 'string' ? params.command : ''
508
+ const reason = typeof params.reason === 'string' ? params.reason.slice(0, 500) : ''
509
+ if (!command.trim() || command.length > SHELL_COMMAND_MAX)
510
+ return answer(message.id, { approved: false, error: 'the command is empty or too long to show' })
511
+ if (approvals && source.hosted && source.approveCommand) {
512
+ const decision = await source.approveCommand(id, { command, reason, signal: controller.signal })
513
+ return answer(message.id, { approved: decision.approved, ...(!decision.approved ? { error: `Command ${decision.state}` } : {}) })
514
+ }
515
+ if (!approvals || !local || !messageId)
516
+ return answer(message.id, { approved: false, error: 'commands need approval on the board, which this run does not have' })
517
+ const record = createApproval(source.config, source.board.id, {
518
+ threadId,
519
+ messageId,
520
+ command,
521
+ reason,
522
+ ms: approvalMs,
523
+ now: new Date(now()),
524
+ })
525
+ await updateCard(source.config, source.board.id, record)
526
+ const shownCommand = JSON.stringify(showCommand(command).slice(0, COMMAND_LOG_MAX))
527
+ const link = approvalLink(source.config, source.board.id, { id, viewport: comment.viewport?.name || comment.viewport }, { env })
528
+ log(
529
+ `#${id}: waiting for approval of a command: open ${link || 'the board'} (comment #${id}, chat thread) -- ${JSON.stringify(showCommand(command).slice(0, 200))}${command.length > 200 ? ' [cut]' : ''}`,
530
+ )
531
+ writeLog(`[approval] ${record.id} asked sha256=${commandHash(command)} command ${shownCommand} reason ${JSON.stringify(reason)}\n`)
532
+ const decision = await waitForApproval(source.config, source.board.id, record, { signal: controller.signal, now })
533
+ writeLog(`[approval] ${record.id} ${decision.state} sha256=${commandHash(command)} command ${shownCommand}\n`)
534
+ return answer(
535
+ message.id,
536
+ decision.approved
537
+ ? { approved: true }
538
+ : {
539
+ approved: false,
540
+ error: `the person ${decision.state === 'denied' ? 'did not allow it' : `did not answer (${decision.state})`}`,
541
+ },
542
+ )
543
+ }
544
+
545
+ const exited = new Promise((resolve) => {
546
+ child.once('error', (error) => resolve({ code: null, error }))
547
+ child.once('close', (code) => resolve({ code }))
548
+ })
549
+ readLines(
550
+ child.stdout,
551
+ (message) => {
552
+ if (message.t === 'result') {
553
+ // The first result counts, checked field by field: anything in the
554
+ // container (the model, a test command) may have written it.
555
+ if (!result) result = containerResult(message)
556
+ return
557
+ }
558
+ if (message.t === 'call') {
559
+ handleCall(message).catch((error) => answer(message.id, { ok: false, error: String(error.message).slice(0, 200) }))
560
+ return
561
+ }
562
+ if (message.t === 'cancel') {
563
+ relay.cancel(message.id)
564
+ return
565
+ }
566
+ if (message.t !== 'event') return
567
+ if (message.type === 'turn') {
568
+ flushText()
569
+ turn = Number.isSafeInteger(message.turn) ? message.turn : turn + 1
570
+ writeLog(`[model] turn ${turn}\n`)
571
+ return
572
+ }
573
+ if (message.type === 'text') {
574
+ const piece = typeof message.text === 'string' ? message.text : ''
575
+ const room = Math.max(0, TEXT_LOG_MAX - turnText.length)
576
+ turnText += piece.slice(0, room)
577
+ turnCut += Math.max(0, piece.length - room)
578
+ return
579
+ }
580
+ if (message.type === 'tool') {
581
+ flushText()
582
+ const card = {
583
+ id: String(message.id || 'tool').slice(0, 80),
584
+ kind: 'tool',
585
+ name: String(message.name || 'tool').slice(0, 80),
586
+ label: String(message.label || message.name || 'Tool').slice(0, 80),
587
+ state: ['running', 'done', 'error'].includes(message.state) ? message.state : 'running',
588
+ detail: message.detail ? String(message.detail).slice(0, 300) : null,
589
+ }
590
+ let args = ''
591
+ if (card.state === 'running' && message.args && typeof message.args === 'object') {
592
+ const json = JSON.stringify(message.args)
593
+ args = ` ${json.length > ARGS_LOG_MAX ? `${json.slice(0, ARGS_LOG_MAX)}...` : json}`
594
+ }
595
+ writeLog(`[tool] ${card.name} ${card.state}${args}${card.detail ? `: ${card.detail}` : ''}\n`)
596
+ if (cardsShown < CARDS_MAX) {
597
+ cardsShown += 1
598
+ showCard(card)
599
+ }
600
+ }
601
+ },
602
+ { onBad: (line) => writeLog(`[bad line] ${line}\n`) },
603
+ )
604
+
605
+ // The wall clock, from the host: stop asks nicely, then the container is killed.
606
+ let killTimer = null
607
+ const hardStop = setTimeout(() => stopRun('time'), wallMs + hostGraceMs)
608
+ // The disk cap: over it, the container is killed at once (it may be filling the disk).
609
+ let overDisk = null
610
+ const stopDisk = watchDisk({
611
+ dir: tree.dir,
612
+ limitBytes: sizeBytes(settings.limits.disk || '2g'),
613
+ everyMs: diskEveryMs,
614
+ measure: diskMeasure,
615
+ onOver: (over) => {
616
+ overDisk = over
617
+ writeLog(`[disk] ${formatBytes(over.bytes)} is over the ${formatBytes(over.limit)} limit: stopping\n`)
618
+ runtime.exec(['kill', names.agent]).catch(() => {})
619
+ stopRun('disk')
620
+ },
621
+ })
622
+ const onStop = () => {
623
+ relay.stopAll()
624
+ if (!child.stdin.destroyed) writeLine(child.stdin, { t: 'stop', reason: String(controller.signal.reason || 'stopped') })
625
+ killTimer = setTimeout(() => runtime.exec(['kill', names.agent]).catch(() => {}), KILL_GRACE_MS)
626
+ }
627
+ controller.signal.addEventListener('abort', onStop, { once: true })
628
+ const exit = await exited
629
+ stopDisk()
630
+ clearTimeout(hardStop)
631
+ clearTimeout(killTimer)
632
+ controller.signal.removeEventListener('abort', onStop)
633
+ if (!controller.signal.aborted) controller.abort('finished')
634
+ relay.stopAll()
635
+ if (exit.error) throw new StarterError('runtime', `the agent container did not start (${exit.error.message})`)
636
+ if (!result) {
637
+ result = { stop: controller.signal.reason === 'time' ? 'time' : 'crashed', text: '', status: null }
638
+ writeLog(`[run] the container exited with ${exit.code} and no result\n`)
639
+ }
640
+ // The host's count is the one that counts: the container only reports its own.
641
+ result.usage = { ...relay.usage }
642
+ if (relay.stop && result.stop !== 'answered') result.stop = relay.stop
643
+ if (overDisk) {
644
+ result.stop = 'disk'
645
+ result.error = { message: `${formatBytes(overDisk.bytes)} of a ${formatBytes(overDisk.limit)} limit` }
646
+ }
647
+
648
+ // The output: a branch with the change.
649
+ const exclude = outputDir ? [path.relative(projectDir, outputDir)].filter((item) => item && !item.startsWith('..')) : []
650
+ const subExclude = exclude.map((item) => path.join(path.relative(tree.dir, tree.projectDir), item)).filter(Boolean)
651
+ // The container is gone before the host reads a file of its folder: a
652
+ // process still running in it could swap a folder for a symlink while
653
+ // the host reads (or removes) it.
654
+ agentGone = await agentRemoved(runtime, names.agent)
655
+ if (!agentGone)
656
+ throw new StarterError('runtime', 'the agent container could not be removed, so nothing was read from its folder or committed')
657
+ const made = overDisk
658
+ ? { committed: false, commit: null, files: [], refused: [] }
659
+ : await commitWorktree({
660
+ tree,
661
+ exclude: subExclude,
662
+ maxBytes: sizeBytes(settings.limits.disk || '2g'),
663
+ message: `Shapewall starter agent: comment ${source.board.id}/${id}\n\nshapewall:${source.board.id}/${id}\nRun ${runId}; review before merging.`,
664
+ })
665
+ for (const item of made.refused) writeLog(`[commit] left out ${item.path}: ${item.reason}\n`)
666
+ commit = made.committed ? made : null
667
+ status = finalStatus(result, commit)
668
+ if (commit && pr) {
669
+ // A pushed branch runs CI, with the repository's secrets, before anyone
670
+ // reviews it: no push of changes to CI config, manifests or build files
671
+ // unless the person said so for this run.
672
+ const guarded = [...new Set([...guardedFiles(commit.files), ...(commit.links || [])])]
673
+ if (guarded.length && !allowGuardedPush) {
674
+ pull = {
675
+ error: `it changed ${guarded.slice(0, 3).join(', ')}${guarded.length > 3 ? ', ...' : ''}, which would run in CI on push; review the branch locally, or rerun with --allow-ci-push`,
676
+ }
677
+ } else {
678
+ try {
679
+ const remote = settings.forkRemote || settings.remote
680
+ await pushBranch({ tree, remote })
681
+ pull = await pr({ branch: tree.branch, comment, commit, runId, remote, fork: Boolean(settings.forkRemote) })
682
+ } catch (error) {
683
+ pull = { error: String(error.message).slice(0, 200) }
684
+ }
685
+ }
686
+ }
687
+ } catch (error) {
688
+ failure = error
689
+ writeLog(`[run] ${error.message}\n`)
690
+ } finally {
691
+ signal?.removeEventListener?.('abort', onOuterAbort)
692
+ for (const step of cleanup) await Promise.resolve(step()).catch(() => {})
693
+ if (agentSpawned && !agentGone) agentGone = await agentRemoved(runtime, names.agent)
694
+ if (tree && agentSpawned && !agentGone) log(`#${id}: left ${tree.dir} in place: the agent container may still be running`)
695
+ else if (tree) await removeWorktree({ tree }).catch((error) => log(`#${id}: could not remove ${tree.dir} (${error.message})`))
696
+ }
697
+
698
+ const ms = now() - started
699
+ // The chat's run summary: the commit's files and diffs, and Undo (delete the unpushed branch).
700
+ const summary =
701
+ messageId && !failure && commit ? await starterSummary({ tree, commit, pr: pull, branch: tree.branch }).catch(() => null) : null
702
+ flushText()
703
+ const used = result?.usage || relay?.usage || { turns: 0, tokens: 0 }
704
+ writeLog(
705
+ `[run] end ${new Date(now()).toISOString()} comment ${source.board.id}/${id}: ${failure ? `failed (${failure.message})` : `${status}, stop ${result?.stop || 'crashed'}${result?.error?.message ? ` (${result.error.message})` : ''}`}, ${commit ? `branch ${tree.branch} (${commit.files.length} files)` : 'no changes'}, ${used.turns} of ${budget.turns} turns, ${used.tokens} of ${budget.tokens} tokens, ${formatDuration(ms)}\n`,
706
+ )
707
+ logClosed = true
708
+ fs.closeSync(logFd)
709
+ const body = failure
710
+ ? `Starter agent could not run: ${String(failure.message).slice(0, 300)}${local ? ` Log: ${logFile}` : ''}`
711
+ : outcomeReply({ result, status, branch: tree?.branch, commit, pr: pull, budget, ms, blocked })
712
+ try {
713
+ await source.reply(id, body)
714
+ } catch (error) {
715
+ log(`#${id}: could not post the reply (${error.message})`)
716
+ }
717
+ // Set only while the comment is still ours (a person may have changed it meanwhile).
718
+ try {
719
+ const current = await source.get(id)
720
+ if (current.agentStatus === 'working' && current.status === 'open') await source.setStatus(id, failure ? 'needs-human' : status)
721
+ } catch {
722
+ /* deleted while the agent ran */
723
+ }
724
+ if (messageId) {
725
+ await cardWrites
726
+ await changeMessage(source.config, source.board.id, threadId, messageId, (message) => {
727
+ message.text = body
728
+ message.starter = { runId, state: 'finished' }
729
+ if (summary) message.summary = summary
730
+ for (const card of message.cards || []) {
731
+ if (card.state === 'running') card.state = failure ? 'error' : 'done'
732
+ if (card.state === 'pending') card.state = 'cancelled'
733
+ }
734
+ }).catch(() => {})
735
+ }
736
+ log(
737
+ `#${id}: ${failure ? `failed: ${failure.message}` : `${status}${commit ? ` on ${tree.branch}` : ', no changes'}`} (${formatDuration(ms)}, log ${logFile})`,
738
+ )
739
+ return {
740
+ id,
741
+ runId,
742
+ status: failure ? 'needs-human' : status,
743
+ failure: failure ? { code: failure.code || 'error', message: failure.message } : null,
744
+ result,
745
+ branch: commit ? tree.branch : null,
746
+ commit: commit ? commit.commit : null,
747
+ files: commit ? commit.files : [],
748
+ pr: pull,
749
+ blocked,
750
+ reply: body,
751
+ logFile,
752
+ ms,
753
+ gate,
754
+ }
755
+ }