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,409 @@
1
+ // The starter agent's git side, on the host: a fresh checkout per run, the
2
+ // commit of what the agent changed on a new branch, and (when asked) a push
3
+ // of that branch. Never a push to the base or the main branch.
4
+ //
5
+ // The folder mounted in the container is a plain checkout of the base
6
+ // commit with no .git in it: not a git worktree, so nothing the agent writes
7
+ // there can point git anywhere. Git on the host never runs with that folder
8
+ // as its repository or (except to list untracked files) as its work tree:
9
+ //
10
+ // - every git call names the repository explicitly (--git-dir, the real
11
+ // common directory found at the start of the run), uses a temporary index
12
+ // kept outside the mount, ignores the system config (GIT_CONFIG_NOSYSTEM),
13
+ // and runs with hooks, fsmonitor and signing off;
14
+ // - the commit is built by reading files ourselves (no symlink is followed,
15
+ // by path or as a parent folder, and hard-linked files are refused) and
16
+ // writing blobs with `hash-object --no-filters --stdin`, so no clean or
17
+ // smudge filter, no attribute and no config from the agent's files runs;
18
+ // - .gitattributes and .gitmodules keep the base's version whatever the
19
+ // agent did to them, and submodules (gitlinks) are left as they were;
20
+ // - the branch is created with update-ref, only if it does not exist.
21
+ //
22
+ // The folder lives outside the project (the cache folder, or --work-dir)
23
+ // and is removed after the run; the branch stays. A run that died leaves an
24
+ // owner file; `sweepWorkRoot` removes what dead runs left.
25
+
26
+ import fs from 'node:fs'
27
+ import os from 'node:os'
28
+ import path from 'node:path'
29
+ import { execFile } from 'node:child_process'
30
+ import { createHash, randomBytes } from 'node:crypto'
31
+ import { absoluteProgram } from '../../which.js'
32
+
33
+ export const SAFE_GIT_FLAGS = [
34
+ '-c',
35
+ 'core.hooksPath=/dev/null',
36
+ '-c',
37
+ 'core.fsmonitor=false',
38
+ '-c',
39
+ 'core.attributesFile=/dev/null',
40
+ '-c',
41
+ 'commit.gpgSign=false',
42
+ '-c',
43
+ 'tag.gpgSign=false',
44
+ '-c',
45
+ 'core.untrackedCache=false',
46
+ ]
47
+ export const AGENT_AUTHOR = { name: 'Shapewall starter agent', email: 'starter-agent@shapewall.invalid' }
48
+ const PROTECTED_BRANCHES = new Set(['main', 'master', 'trunk', 'develop', 'production', 'staging', 'release'])
49
+ const KEEP_BASE = new Set(['.gitattributes', '.gitmodules'])
50
+ const SKIP_PARTS = new Set(['.git', 'node_modules'])
51
+ export const FILE_MAX = 100 * 1024 * 1024
52
+ // The default bound on the bytes of changed files one commit reads in.
53
+ export const COMMIT_MAX = 2 * 1024 ** 3
54
+ const RUN_DIR_RE = /^\d{14}-[a-f0-9]{6}$/
55
+
56
+ // The environment for git: the person's, without anything that would point
57
+ // git somewhere else, and without the system config.
58
+ function gitEnv(env, extra = {}) {
59
+ const out = {}
60
+ for (const [key, value] of Object.entries(env)) {
61
+ if (/^GIT_/.test(key) && !/^GIT_(AUTHOR|COMMITTER)_/.test(key) && !['GIT_SSH', 'GIT_SSH_COMMAND', 'GIT_ASKPASS'].includes(key)) continue
62
+ out[key] = value
63
+ }
64
+ return { ...out, GIT_TERMINAL_PROMPT: '0', GIT_CONFIG_NOSYSTEM: '1', ...extra }
65
+ }
66
+
67
+ export function git(args, { cwd, env = process.env, extraEnv = {}, input = null, timeoutMs = 120000 } = {}) {
68
+ // An absolute path from absolute PATH entries only: a relative entry would
69
+ // let a `git` in the folder (the agent's checkout) decide what runs.
70
+ const program = absoluteProgram('git', env)
71
+ if (!program) return Promise.reject(new Error('git: not found on PATH'))
72
+ return new Promise((resolve, reject) => {
73
+ const child = execFile(
74
+ program,
75
+ [...SAFE_GIT_FLAGS, ...args],
76
+ { cwd, env: gitEnv(env, extraEnv), timeout: timeoutMs, maxBuffer: 256 * 1024 * 1024, encoding: 'buffer' },
77
+ (error, stdout, stderr) => {
78
+ if (error) {
79
+ const message = String(stderr || error.message)
80
+ .trim()
81
+ .split('\n')
82
+ .slice(-3)
83
+ .join(' ')
84
+ reject(new Error(`git ${args.find((arg) => !arg.startsWith('-')) || args[0]}: ${message.slice(0, 400)}`))
85
+ } else resolve(stdout.toString('utf8'))
86
+ },
87
+ )
88
+ if (input !== null) {
89
+ // git can exit before reading all of it (EPIPE): the exit is reported above.
90
+ child.stdin.on('error', () => {})
91
+ child.stdin.end(input)
92
+ }
93
+ })
94
+ }
95
+
96
+ // Where run folders go: --work-dir, else the cache folder.
97
+ export function workRoot(projectDir, { flag = null, env = process.env } = {}) {
98
+ if (flag) return path.resolve(flag)
99
+ const cache =
100
+ env.XDG_CACHE_HOME || (process.platform === 'darwin' ? path.join(os.homedir(), 'Library', 'Caches') : path.join(os.homedir(), '.cache'))
101
+ const tag = createHash('sha256').update(fs.realpathSync(projectDir)).digest('hex').slice(0, 12)
102
+ return path.join(cache, 'shapewall', 'starter-agent', tag)
103
+ }
104
+
105
+ export function newRunId(date = new Date()) {
106
+ const stamp = date.toISOString().replace(/[-:T]/g, '').slice(0, 14)
107
+ return `${stamp}-${randomBytes(3).toString('hex')}`
108
+ }
109
+
110
+ // The branch for a comment: <prefix><board>-<comment>-<run suffix>.
111
+ export function branchName(prefix, boardId, commentId, runId) {
112
+ const name = `${prefix}${boardId}-${commentId}-${runId.split('-').pop()}`
113
+ const last = name.split('/').pop()
114
+ if (PROTECTED_BRANCHES.has(name) || PROTECTED_BRANCHES.has(last)) throw new Error(`refusing branch name "${name}"`)
115
+ return name
116
+ }
117
+
118
+ const ownerFile = (root, runId) => path.join(root, `${runId}.owner`)
119
+ const indexFile = (root, runId, kind) => path.join(root, `${runId}.${kind}.index`)
120
+
121
+ // A plain checkout of the project's commit (or `base`), with no .git, for
122
+ // the container. Nothing is written to the repository yet.
123
+ export async function createWorktree({ projectDir, root, runId, branch, base = 'HEAD' }) {
124
+ if (!RUN_DIR_RE.test(runId)) throw new Error('bad run id')
125
+ const top = (await git(['rev-parse', '--show-toplevel'], { cwd: projectDir })).trim()
126
+ const gitDir = path.resolve(top, (await git(['rev-parse', '--git-common-dir'], { cwd: top })).trim())
127
+ const format = (await git(['--git-dir', gitDir, 'rev-parse', '--show-object-format'], { cwd: top }).catch(() => 'sha1')).trim() || 'sha1'
128
+ // HEAD is per worktree: resolve it where the project is (a linked worktree
129
+ // has its own), never through the shared git dir, which is used for writing.
130
+ const commit = (await git(['rev-parse', '--verify', `${base}^{commit}`], { cwd: projectDir })).trim()
131
+ const exists = await git(['--git-dir', gitDir, 'show-ref', '--verify', '--quiet', `refs/heads/${branch}`], { cwd: top }).then(
132
+ () => true,
133
+ () => false,
134
+ )
135
+ if (exists) throw new Error(`the branch ${branch} exists already`)
136
+ fs.mkdirSync(root, { recursive: true, mode: 0o700 })
137
+ fs.writeFileSync(ownerFile(root, runId), `${process.pid}\n`, { mode: 0o600 })
138
+ const dir = path.join(root, runId)
139
+ const index = indexFile(root, runId, 'checkout')
140
+ const extraEnv = { GIT_INDEX_FILE: index }
141
+ let made = false
142
+ try {
143
+ fs.mkdirSync(dir, { mode: 0o700 })
144
+ made = true
145
+ await git(['--git-dir', gitDir, 'read-tree', commit], { cwd: root, extraEnv })
146
+ // The base is the person's own commit: checking it out is what `git worktree add` would do.
147
+ await git(['--git-dir', gitDir, '--work-tree', dir, 'checkout-index', '--all', '--force'], { cwd: dir, extraEnv })
148
+ } catch (error) {
149
+ // A half-made checkout is not left for the sweep: remove it and the owner file now.
150
+ if (made) fs.rmSync(dir, { recursive: true, force: true })
151
+ fs.rmSync(ownerFile(root, runId), { force: true })
152
+ throw error
153
+ } finally {
154
+ fs.rmSync(index, { force: true })
155
+ }
156
+ const real = fs.realpathSync(dir)
157
+ const sub = path.relative(top, fs.realpathSync(projectDir))
158
+ return { repo: top, gitDir, format, root, runId, dir: real, projectDir: path.join(real, sub), base: commit, branch }
159
+ }
160
+
161
+ // The blob id git would give `data`.
162
+ function blobId(format, data) {
163
+ return createHash(format === 'sha256' ? 'sha256' : 'sha1')
164
+ .update(`blob ${data.length}\0`)
165
+ .update(data)
166
+ .digest('hex')
167
+ }
168
+
169
+ // Reads one path under `root` without following a symlink anywhere:
170
+ // { kind: 'missing' } | { kind: 'refused', reason } | { kind: 'entry', mode, data }.
171
+ // `maxSize`: refuse a file larger than this without reading it.
172
+ export function readEntry(root, rel, { maxSize = FILE_MAX } = {}) {
173
+ const parts = rel.split('/')
174
+ let current = root
175
+ for (let index = 0; index < parts.length - 1; index += 1) {
176
+ current = path.join(current, parts[index])
177
+ let stat
178
+ try {
179
+ stat = fs.lstatSync(current)
180
+ } catch {
181
+ return { kind: 'missing' }
182
+ }
183
+ if (!stat.isDirectory()) return { kind: 'missing' }
184
+ }
185
+ const full = path.join(root, rel)
186
+ let stat
187
+ try {
188
+ stat = fs.lstatSync(full)
189
+ } catch {
190
+ return { kind: 'missing' }
191
+ }
192
+ if (stat.isSymbolicLink()) return { kind: 'entry', mode: '120000', data: Buffer.from(fs.readlinkSync(full)) }
193
+ if (!stat.isFile()) return { kind: 'missing' }
194
+ if (stat.nlink > 1) return { kind: 'refused', reason: 'a hard link' }
195
+ if (stat.size > FILE_MAX) return { kind: 'refused', reason: 'over 100 MB' }
196
+ if (stat.size > maxSize) return { kind: 'refused', reason: "over the run's size limit" }
197
+ let fd
198
+ try {
199
+ fd = fs.openSync(full, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK)
200
+ } catch (error) {
201
+ return { kind: 'refused', reason: error.code || 'unreadable' }
202
+ }
203
+ try {
204
+ const opened = fs.fstatSync(fd)
205
+ if (!opened.isFile() || opened.ino !== stat.ino || opened.dev !== stat.dev || opened.nlink > 1)
206
+ return { kind: 'refused', reason: 'changed while read' }
207
+ const data = fs.readFileSync(fd)
208
+ return { kind: 'entry', mode: opened.mode & 0o100 ? '100755' : '100644', data }
209
+ } finally {
210
+ fs.closeSync(fd)
211
+ }
212
+ }
213
+
214
+ // Whether a symlink at `rel` with this target points outside the repository.
215
+ export function linkLeaves(rel, target) {
216
+ if (!target || path.posix.isAbsolute(target) || /^[A-Za-z]:|^\\\\/.test(target)) return true
217
+ const resolved = path.posix.normalize(path.posix.join(path.posix.dirname(rel), target))
218
+ return resolved === '..' || resolved.startsWith('../')
219
+ }
220
+
221
+ const skipped = (rel, exclude) => {
222
+ const parts = rel.split('/')
223
+ if (parts.some((part) => SKIP_PARTS.has(part))) return true
224
+ return exclude.some((item) => rel === item || rel.startsWith(`${item}/`))
225
+ }
226
+
227
+ // Commits everything the agent changed (except dependencies, the Shapewall
228
+ // store and the files that keep the base's version) as the agent, on the
229
+ // run's new branch. Returns { committed, commit, files, added, modified,
230
+ // deleted, refused } (files is all of added, modified and deleted).
231
+ // `maxBytes` bounds what is read in for changed files: `du` sees a sparse file
232
+ // as small, and a folder of them could keep the host reading for hours.
233
+ export async function commitWorktree({ tree, message, exclude = [], maxBytes = COMMIT_MAX }) {
234
+ const { gitDir, dir, base, branch, format, root, runId } = tree
235
+ const index = indexFile(root, runId, 'commit')
236
+ const extraEnv = { GIT_INDEX_FILE: index }
237
+ const run = (args, options = {}) => git(['--git-dir', gitDir, ...args], { cwd: root, extraEnv, ...options })
238
+ try {
239
+ await run(['read-tree', base])
240
+ const baseEntries = new Map()
241
+ for (const line of (await run(['ls-tree', '-r', '-z', '--full-tree', base])).split('\0')) {
242
+ if (!line) continue
243
+ const tab = line.indexOf('\t')
244
+ const [mode, type, oid] = line.slice(0, tab).split(' ')
245
+ baseEntries.set(line.slice(tab + 1), { mode, type, oid })
246
+ }
247
+ // Untracked files the repository's ignore rules let through (git lists
248
+ // names here; it reads no file content and follows no symlink).
249
+ const untracked = (await run(['--work-tree', dir, 'ls-files', '--others', '--exclude-standard', '-z'])).split('\0').filter(Boolean)
250
+ const zero = '0'.repeat(format === 'sha256' ? 64 : 40)
251
+ const updates = []
252
+ const files = []
253
+ // Which files are new: a run that only adds files may have missed the real one.
254
+ const added = []
255
+ const deleted = []
256
+ const refused = []
257
+ let total = 0
258
+ // Symlinks that point outside the repository (absolute, or up past its root).
259
+ const links = []
260
+ const gitlinks = [...baseEntries].filter(([, entry]) => entry.mode === '160000').map(([rel]) => rel)
261
+ for (const rel of new Set([...baseEntries.keys(), ...untracked])) {
262
+ if (skipped(rel, exclude) || KEEP_BASE.has(rel.split('/').pop())) continue
263
+ if (gitlinks.some((link) => rel === link || rel.startsWith(`${link}/`))) continue
264
+ const before = baseEntries.get(rel)
265
+ const now = readEntry(dir, rel, { maxSize: Math.max(0, maxBytes - total) })
266
+ if (now.kind === 'refused') {
267
+ refused.push({ path: rel, reason: now.reason })
268
+ continue
269
+ }
270
+ if (now.kind === 'missing') {
271
+ if (before) {
272
+ updates.push(`0 ${zero}\t${rel}\0`)
273
+ files.push(rel)
274
+ deleted.push(rel)
275
+ }
276
+ continue
277
+ }
278
+ const oid = blobId(format, now.data)
279
+ if (before && before.oid === oid && before.mode === now.mode) continue
280
+ total += now.data.length
281
+ const written = (await run(['hash-object', '-w', '--no-filters', '--stdin'], { input: now.data })).trim()
282
+ if (written !== oid) throw new Error(`git wrote ${written} for ${rel}, expected ${oid}`)
283
+ updates.push(`${now.mode} ${oid}\t${rel}\0`)
284
+ files.push(rel)
285
+ if (!before) added.push(rel)
286
+ if (now.mode === '120000' && linkLeaves(rel, now.data.toString('utf8'))) links.push(rel)
287
+ }
288
+ if (!files.length) return { committed: false, commit: null, files: [], added: [], modified: [], deleted: [], links: [], refused }
289
+ await run(['update-index', '-z', '--index-info'], { input: updates.join('') })
290
+ const treeId = (await run(['write-tree'])).trim()
291
+ const env = {
292
+ ...process.env,
293
+ GIT_AUTHOR_NAME: AGENT_AUTHOR.name,
294
+ GIT_AUTHOR_EMAIL: AGENT_AUTHOR.email,
295
+ GIT_COMMITTER_NAME: AGENT_AUTHOR.name,
296
+ GIT_COMMITTER_EMAIL: AGENT_AUTHOR.email,
297
+ }
298
+ const commit = (await run(['commit-tree', treeId, '-p', base, '-F', '-'], { env, input: message })).trim()
299
+ // Only a new branch: the zero old value makes update-ref refuse an existing one.
300
+ await run(['update-ref', '-m', 'shapewall starter agent', `refs/heads/${branch}`, commit, zero])
301
+ const modified = files.filter((rel) => !added.includes(rel) && !deleted.includes(rel)).sort()
302
+ return {
303
+ committed: true,
304
+ commit,
305
+ files: files.sort(),
306
+ added: added.sort(),
307
+ modified,
308
+ deleted: deleted.sort(),
309
+ links: links.sort(),
310
+ refused,
311
+ }
312
+ } finally {
313
+ fs.rmSync(index, { force: true })
314
+ fs.rmSync(`${index}.lock`, { force: true })
315
+ }
316
+ }
317
+
318
+ // Removes a folder the agent had, whatever modes it set (never following a symlink).
319
+ function removeTree(dir) {
320
+ try {
321
+ fs.rmSync(dir, { recursive: true, force: true })
322
+ return
323
+ } catch {
324
+ /* modes: open them up and try again */
325
+ }
326
+ const open = (current) => {
327
+ let stat
328
+ try {
329
+ stat = fs.lstatSync(current)
330
+ } catch {
331
+ return
332
+ }
333
+ if (!stat.isDirectory()) return
334
+ try {
335
+ fs.chmodSync(current, 0o700)
336
+ } catch {
337
+ return
338
+ }
339
+ for (const name of fs.readdirSync(current)) open(path.join(current, name))
340
+ }
341
+ open(dir)
342
+ fs.rmSync(dir, { recursive: true, force: true })
343
+ }
344
+
345
+ export async function removeWorktree({ tree }) {
346
+ removeTree(tree.dir)
347
+ for (const kind of ['checkout', 'commit']) fs.rmSync(indexFile(tree.root, tree.runId, kind), { force: true })
348
+ fs.rmSync(ownerFile(tree.root, tree.runId), { force: true })
349
+ }
350
+
351
+ const alive = (pid) => {
352
+ if (!Number.isInteger(pid) || pid <= 0) return false
353
+ try {
354
+ process.kill(pid, 0)
355
+ return true
356
+ } catch (error) {
357
+ return error.code === 'EPERM'
358
+ }
359
+ }
360
+
361
+ // Removes run folders whose run is gone (its owner process ended, or no
362
+ // owner file and older than a day). Returns the run ids removed.
363
+ export function sweepWorkRoot(root, { now = Date.now(), isAlive = alive } = {}) {
364
+ let names
365
+ try {
366
+ names = fs.readdirSync(root)
367
+ } catch {
368
+ return []
369
+ }
370
+ const removed = []
371
+ for (const name of names) {
372
+ if (!RUN_DIR_RE.test(name)) continue
373
+ let pid = null
374
+ try {
375
+ pid = Number(fs.readFileSync(ownerFile(root, name), 'utf8').trim())
376
+ } catch {
377
+ /* no owner file */
378
+ }
379
+ if (pid !== null && isAlive(pid)) continue
380
+ if (pid === null) {
381
+ try {
382
+ if (now - fs.lstatSync(path.join(root, name)).mtimeMs < 24 * 60 * 60 * 1000) continue
383
+ } catch {
384
+ continue
385
+ }
386
+ }
387
+ removeTree(path.join(root, name))
388
+ for (const kind of ['checkout', 'commit']) fs.rmSync(indexFile(root, name, kind), { force: true })
389
+ fs.rmSync(ownerFile(root, name), { force: true })
390
+ removed.push(name)
391
+ }
392
+ return removed
393
+ }
394
+
395
+ // Pushes the agent's branch, and only it, to the same name on the remote.
396
+ export async function pushBranch({ tree, remote }) {
397
+ const { branch } = tree
398
+ const last = branch.split('/').pop()
399
+ if (PROTECTED_BRANCHES.has(branch) || PROTECTED_BRANCHES.has(last)) throw new Error(`refusing to push "${branch}"`)
400
+ // No tags (push.followTags) and no submodule pushes (push.recurseSubmodules)
401
+ // from the person's config: the branch is all that goes.
402
+ const refspec = `refs/heads/${branch}:refs/heads/${branch}`
403
+ await git(['--git-dir', tree.gitDir, 'push', '--no-verify', '--no-follow-tags', '--recurse-submodules=no', remote, refspec], {
404
+ cwd: tree.repo,
405
+ timeoutMs: 180000,
406
+ })
407
+ }
408
+
409
+ export { alive as processAlive }
@@ -0,0 +1,210 @@
1
+ // A small line diff for approval cards: when an agent asks to edit or write
2
+ // a file, the card shows what changes, not just the file name. Computed on
3
+ // the server from the old and the new text, and bounded both ways: a short
4
+ // preview the card shows at once, and a longer (still bounded) full diff
5
+ // behind "Show the whole diff".
6
+ //
7
+ // (src/diff.js is the pixel diff between captures; this is plain text.)
8
+ //
9
+ // Every line goes through showableLine(): control, zero-width and
10
+ // bidirectional characters are shown as escapes, so the card shows what the
11
+ // file will hold. A line too long for the preview is cut with a visible
12
+ // marker that gives its length, and the whole diff shows it in full (up to
13
+ // LINE_FULL_MAX, marked the same way beyond that). Nothing is cut silently.
14
+
15
+ import { showable, showableLine } from './showable.js'
16
+
17
+ export const DIFF_PREVIEW_LINES = 80
18
+ export const DIFF_PREVIEW_BYTES = 8 * 1024
19
+ export const DIFF_FULL_LINES = 1500
20
+ export const DIFF_FULL_BYTES = 64 * 1024
21
+ const CONTEXT = 3
22
+ // The LCS table is (old lines x new lines) after the common start and end
23
+ // are cut; beyond this the changed block is shown as removed then added.
24
+ const LCS_CELLS_MAX = 4_000_000
25
+ export const LINE_PREVIEW_MAX = 400
26
+ export const LINE_FULL_MAX = 16 * 1024
27
+
28
+ const splitLines = (text) => {
29
+ const value = String(text ?? '')
30
+ if (!value) return []
31
+ const lines = value.split(/\r?\n/)
32
+ if (lines.at(-1) === '') lines.pop()
33
+ return lines
34
+ }
35
+
36
+ // The edit script between two lists of lines: [{ op: ' ' | '-' | '+', text }].
37
+ function editScript(a, b) {
38
+ let start = 0
39
+ while (start < a.length && start < b.length && a[start] === b[start]) start += 1
40
+ let endA = a.length
41
+ let endB = b.length
42
+ while (endA > start && endB > start && a[endA - 1] === b[endB - 1]) {
43
+ endA -= 1
44
+ endB -= 1
45
+ }
46
+ const out = a.slice(0, start).map((text) => ({ op: ' ', text }))
47
+ const midA = a.slice(start, endA)
48
+ const midB = b.slice(start, endB)
49
+ if (midA.length * midB.length > LCS_CELLS_MAX) {
50
+ for (const text of midA) out.push({ op: '-', text })
51
+ for (const text of midB) out.push({ op: '+', text })
52
+ } else {
53
+ // Longest common subsequence, filled from the end so the walk goes forward.
54
+ const n = midA.length
55
+ const m = midB.length
56
+ const width = m + 1
57
+ const table = new Uint32Array((n + 1) * width)
58
+ for (let i = n - 1; i >= 0; i -= 1) {
59
+ for (let j = m - 1; j >= 0; j -= 1) {
60
+ table[i * width + j] =
61
+ midA[i] === midB[j] ? table[(i + 1) * width + j + 1] + 1 : Math.max(table[(i + 1) * width + j], table[i * width + j + 1])
62
+ }
63
+ }
64
+ let i = 0
65
+ let j = 0
66
+ while (i < n || j < m) {
67
+ if (i < n && j < m && midA[i] === midB[j]) {
68
+ out.push({ op: ' ', text: midA[i] })
69
+ i += 1
70
+ j += 1
71
+ } else if (i < n && (j === m || table[(i + 1) * width + j] >= table[i * width + j + 1])) {
72
+ // Removed lines first, then the added ones, like `diff -u`.
73
+ out.push({ op: '-', text: midA[i] })
74
+ i += 1
75
+ } else {
76
+ out.push({ op: '+', text: midB[j] })
77
+ j += 1
78
+ }
79
+ }
80
+ }
81
+ for (const text of a.slice(endA)) out.push({ op: ' ', text })
82
+ return out
83
+ }
84
+
85
+ // Unified diff lines (hunks with CONTEXT lines around each change), and the counts.
86
+ export function lineDiff(oldText, newText) {
87
+ const a = splitLines(oldText)
88
+ const b = splitLines(newText)
89
+ const script = editScript(a, b)
90
+ const added = script.filter((item) => item.op === '+').length
91
+ const removed = script.filter((item) => item.op === '-').length
92
+ const keep = new Uint8Array(script.length)
93
+ script.forEach((item, index) => {
94
+ if (item.op === ' ') return
95
+ for (let k = Math.max(0, index - CONTEXT); k <= Math.min(script.length - 1, index + CONTEXT); k += 1) keep[k] = 1
96
+ })
97
+ const lines = []
98
+ let lineA = 1
99
+ let lineB = 1
100
+ let inHunk = false
101
+ script.forEach((item, index) => {
102
+ if (keep[index]) {
103
+ if (!inHunk) {
104
+ // The hunk header: where it starts in the old and the new file.
105
+ lines.push(`@@ -${lineA} +${lineB} @@`)
106
+ inHunk = true
107
+ }
108
+ lines.push(`${item.op}${showableLine(item.text)}`)
109
+ } else inHunk = false
110
+ if (item.op !== '+') lineA += 1
111
+ if (item.op !== '-') lineB += 1
112
+ })
113
+ return { lines, added, removed }
114
+ }
115
+
116
+ // A line cut to `max` characters, with a marker that says so and how long it is.
117
+ export function clipLine(line, max) {
118
+ if (line.length <= max) return { line, clipped: false }
119
+ return { line: `${line.slice(0, max)}… [line cut: ${line.length} characters in all]`, clipped: true }
120
+ }
121
+
122
+ // Joins lines until either bound is reached. { text, cut }
123
+ function bounded(lines, maxLines, maxBytes) {
124
+ const out = []
125
+ let bytes = 0
126
+ for (const line of lines) {
127
+ const size = Buffer.byteLength(line) + 1
128
+ if (out.length >= maxLines || bytes + size > maxBytes) return { text: out.join('\n'), cut: true }
129
+ out.push(line)
130
+ bytes += size
131
+ }
132
+ return { text: out.join('\n'), cut: false }
133
+ }
134
+
135
+ // What an approval card carries: { path, added, removed, preview, full,
136
+ // truncated, created, outside }. `outside`: a file outside the project. `full` is null when the preview is already all of
137
+ // it; `truncated` says even the full diff was cut. `oldText` null: a new file.
138
+ export function diffForCard(file, oldText, newText) {
139
+ return diffForFiles([{ path: file, oldText, newText }])
140
+ }
141
+
142
+ // The same for several files (an edit that touches more than one): each
143
+ // file's hunks under a "--- path" line. null when there is nothing to show.
144
+ export function diffForFiles(files) {
145
+ const list = files.filter((item) => item && (typeof item.newText === 'string' || typeof item.oldText === 'string'))
146
+ if (!list.length) return null
147
+ const lines = []
148
+ let added = 0
149
+ let removed = 0
150
+ for (const item of list) {
151
+ const result = lineDiff(item.oldText ?? '', item.newText ?? '')
152
+ if (list.length > 1) lines.push(`--- ${showable(item.path || 'file')}${item.outside ? ' (outside the project)' : ''}`)
153
+ lines.push(...result.lines)
154
+ added += result.added
155
+ removed += result.removed
156
+ }
157
+ const file = list.length === 1 ? list[0].path : `${list.length} files`
158
+ const created = list.length === 1 && (list[0].oldText === null || list[0].oldText === undefined)
159
+ const outside = list.some((item) => item.outside === true)
160
+ return cardShape(file, lines, added, removed, created, outside)
161
+ }
162
+
163
+ function cardShape(file, lines, added, removed, created, outside) {
164
+ const short = lines.map((line) => clipLine(line, LINE_PREVIEW_MAX))
165
+ const preview = bounded(
166
+ short.map((item) => item.line),
167
+ DIFF_PREVIEW_LINES,
168
+ DIFF_PREVIEW_BYTES,
169
+ )
170
+ // A line cut in the preview is shown whole behind "Show the whole diff".
171
+ const shown = preview.text ? preview.text.split('\n').length : 0
172
+ const clipped = short.slice(0, shown).some((item) => item.clipped)
173
+ const full =
174
+ preview.cut || clipped
175
+ ? bounded(
176
+ lines.map((line) => clipLine(line, LINE_FULL_MAX).line),
177
+ DIFF_FULL_LINES,
178
+ DIFF_FULL_BYTES,
179
+ )
180
+ : null
181
+ return {
182
+ path: file ? showable(String(file)).slice(0, 300) : null,
183
+ added,
184
+ removed,
185
+ created,
186
+ outside,
187
+ preview: preview.text,
188
+ full: full ? full.text : null,
189
+ truncated: Boolean(full?.cut),
190
+ }
191
+ }
192
+
193
+ // A diff from a card event, checked and bounded again (it is stored in the thread).
194
+ export function cleanCardDiff(diff) {
195
+ if (!diff || typeof diff !== 'object' || typeof diff.preview !== 'string') return null
196
+ const count = (value) => (Number.isSafeInteger(value) && value >= 0 ? value : 0)
197
+ // Escaping again changes nothing in a diff made here (an escape is plain
198
+ // text); the +60 leaves room for the cut marker.
199
+ const lines = (text, max) => text.split('\n').map((line) => clipLine(showableLine(line), max + 60).line)
200
+ return {
201
+ path: typeof diff.path === 'string' ? showable(diff.path).slice(0, 300) : null,
202
+ added: count(diff.added),
203
+ removed: count(diff.removed),
204
+ created: diff.created === true,
205
+ outside: diff.outside === true,
206
+ preview: bounded(lines(diff.preview, LINE_PREVIEW_MAX), DIFF_PREVIEW_LINES, DIFF_PREVIEW_BYTES).text,
207
+ full: typeof diff.full === 'string' ? bounded(lines(diff.full, LINE_FULL_MAX), DIFF_FULL_LINES, DIFF_FULL_BYTES).text : null,
208
+ truncated: diff.truncated === true,
209
+ }
210
+ }