shapewall 0.2.4 → 0.3.1

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 (105) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/README.md +35 -1
  3. package/dist/agent/package-lock.json +2 -2
  4. package/dist/client/assets/BoardChat-V1BCXdDB.js +27 -0
  5. package/dist/client/assets/{BuzzDialog-CZdwIQLF.js → BuzzDialog-Ds7Thpfe.js} +1 -1
  6. package/dist/client/assets/ChecksPanel-Cm22bCdQ.js +1 -0
  7. package/dist/client/assets/History-U0bD1BjA.js +1 -0
  8. package/dist/client/assets/{LocalSettings-B5gtfKEl.js → LocalSettings-DOSuW7VC.js} +1 -1
  9. package/dist/client/assets/{Management-BxEUZAmI.js → Management-BFcKFdbp.js} +1 -1
  10. package/dist/client/assets/QuickConnect-DyfPSWbV.js +1 -0
  11. package/dist/client/assets/{ShareDialog-CdVAreaw.js → ShareDialog-BouvNraR.js} +2 -2
  12. package/dist/client/assets/{app-update-dialog-BONq9YFp.js → app-update-dialog-DH2Mgby8.js} +1 -1
  13. package/dist/client/assets/{circle-x-BmT6vHKl.js → circle-x-PRxj7kPi.js} +1 -1
  14. package/dist/client/assets/index-C9S7HL_o.js +37 -0
  15. package/dist/client/assets/index-CXtuJi1K.css +2 -0
  16. package/dist/client/assets/{refresh-cw-ugt3cWKH.js → refresh-cw-aeOBDuGQ.js} +1 -1
  17. package/dist/client/assets/{square-_KG6tM5A.js → shield-check-DuPBJ6H_.js} +1 -1
  18. package/dist/client/assets/ui-Cg4TTrec.js +41 -0
  19. package/dist/client/build-info.json +1 -1
  20. package/dist/client/index.html +3 -3
  21. package/dist/client/sw.js +2 -2
  22. package/dist/console/assets/LocalAccountsApp-lJjkhUcH.js +1 -0
  23. package/dist/console/assets/LocalConsole-BZHqYKnt.js +1 -0
  24. package/dist/console/assets/{app-update-dialog-o4QY_jL9.js → app-update-dialog-DCM9sYE3.js} +1 -1
  25. package/dist/console/assets/console-C2LsvEco.js +55 -0
  26. package/dist/console/assets/console-Fp4k9iyx.css +2 -0
  27. package/dist/console/build-info.json +1 -1
  28. package/dist/console/console.html +2 -2
  29. package/docs/agent-hardening.md +19 -0
  30. package/docs/agent-workflow.md +33 -0
  31. package/docs/agents.md +112 -0
  32. package/docs/config.md +41 -0
  33. package/docs/deployment.md +3 -0
  34. package/docs/hosted.md +10 -0
  35. package/docs/pull-requests.md +193 -0
  36. package/package.json +2 -1
  37. package/src/agent/backends/acp.js +23 -6
  38. package/src/agent/batch-fixes.js +45 -0
  39. package/src/agent/batch-queue.js +204 -0
  40. package/src/agent/branch-mode.js +753 -0
  41. package/src/agent/config.js +6 -0
  42. package/src/agent/fix-prompt.js +5 -0
  43. package/src/agent/routes.js +850 -185
  44. package/src/agent/run.js +8 -1
  45. package/src/agent/settings-routes.js +69 -1
  46. package/src/agent/starter/worktree.js +286 -29
  47. package/src/agent/threads.js +2 -7
  48. package/src/agent/tools.js +51 -0
  49. package/src/agent/user-settings.js +33 -2
  50. package/src/ci/capture.js +103 -0
  51. package/src/ci/compare.js +267 -0
  52. package/src/ci/github.js +307 -0
  53. package/src/ci/images.js +76 -0
  54. package/src/ci/settings.js +211 -0
  55. package/src/ci/start.js +148 -0
  56. package/src/ci/summary.js +262 -0
  57. package/src/ci/workflow.js +249 -0
  58. package/src/cli/args.js +24 -1
  59. package/src/cli/ci.js +229 -0
  60. package/src/cli/help.js +23 -1
  61. package/src/cli/init.js +36 -1
  62. package/src/cli.js +6 -2
  63. package/src/comment-drawing.js +161 -0
  64. package/src/comment-fields.js +51 -0
  65. package/src/comment-media.js +528 -0
  66. package/src/config.js +7 -0
  67. package/src/doctor.js +23 -0
  68. package/src/export.js +8 -0
  69. package/src/feedback.js +89 -12
  70. package/src/github.js +3 -3
  71. package/src/hosted/admin.js +36 -4
  72. package/src/hosted/app.js +5 -0
  73. package/src/hosted/board-routes.js +63 -7
  74. package/src/hosted/clean-worker.js +22 -4
  75. package/src/hosted/cleaner.js +4 -1
  76. package/src/hosted/comment-files.js +200 -0
  77. package/src/hosted/comments.js +68 -15
  78. package/src/hosted/db.js +32 -0
  79. package/src/hosted/guest-view.js +9 -0
  80. package/src/hosted/import.js +213 -18
  81. package/src/hosted/operator.js +3 -1
  82. package/src/hosted/projects.js +3 -2
  83. package/src/hosted/push.js +6 -3
  84. package/src/hosted/retention.js +18 -0
  85. package/src/hosted/tenancy.js +1 -0
  86. package/src/hosted/token-api.js +110 -9
  87. package/src/image-sniff.js +11 -0
  88. package/src/mcp.js +48 -3
  89. package/src/remote/remote-mcp.js +2 -2
  90. package/src/remote/remote.js +6 -2
  91. package/src/remote/share.js +77 -7
  92. package/src/server/app.js +18 -1
  93. package/src/store.js +119 -33
  94. package/src/watch.js +4 -0
  95. package/dist/client/assets/BoardChat-C0a3DDjv.js +0 -28
  96. package/dist/client/assets/ChecksPanel-BHWgwJWO.js +0 -1
  97. package/dist/client/assets/History-C01qLFpP.js +0 -1
  98. package/dist/client/assets/QuickConnect-BzTM3UNO.js +0 -1
  99. package/dist/client/assets/index-C1IsGdGs.js +0 -29
  100. package/dist/client/assets/index-DnsWelgZ.css +0 -2
  101. package/dist/client/assets/ui-BAGBTB_P.js +0 -41
  102. package/dist/console/assets/LocalAccountsApp-DtfTDbQg.js +0 -1
  103. package/dist/console/assets/LocalConsole-BPpsxDq_.js +0 -1
  104. package/dist/console/assets/console-DeOji9Zv.css +0 -2
  105. package/dist/console/assets/console-MvFrlUCC.js +0 -51
@@ -0,0 +1,528 @@
1
+ // Images and drawings on comments, server side: checking and cleaning an
2
+ // uploaded image, storing it beside the comments, and rendering a comment's
3
+ // drawing onto its screenshot for agents.
4
+ //
5
+ // Images follow the chat's attachments (src/agent/threads.js): base64 in the
6
+ // JSON body, at most COMMENT_IMAGES_MAX of COMMENT_IMAGE_BYTES_MAX each,
7
+ // PNG, JPEG, WebP or GIF by their bytes (never by name or declared type
8
+ // alone, never SVG), stored as <24 hex>.<ext> files. Before anything is
9
+ // stored each image is cleaned:
10
+ // - PNG: header checked for its size, then decoded and re-encoded (pngjs),
11
+ // which keeps the pixels and drops every other chunk (text, EXIF, ...),
12
+ // the way pushed captures are (src/hosted/push.js cleanPng);
13
+ // - JPEG: walked segment by segment; EXIF, XMP, comments and every APPn but
14
+ // JFIF, ICC colour and Adobe colour are dropped (the board re-encodes
15
+ // photos on a canvas first, so their orientation is already applied);
16
+ // - WebP: walked chunk by chunk; EXIF and XMP chunks are dropped;
17
+ // - GIF: walked block by block; comments and application blocks other than
18
+ // the loop count are dropped.
19
+ // A file that does not parse cleanly is refused. Width and height are read
20
+ // from the file and capped, so no decoder sees a decompression bomb.
21
+ //
22
+ // Local layout: <out>/<board>/comment-files/<24 hex>.<ext> for images and
23
+ // markup-<comment>[-r<reply>].png for rendered drawings (made on demand, the
24
+ // drawing never changes once posted).
25
+
26
+ import fs from 'node:fs'
27
+ import path from 'node:path'
28
+ import { randomBytes } from 'node:crypto'
29
+ import { PNG } from 'pngjs'
30
+ import { sniffImage } from './image-sniff.js'
31
+ import {
32
+ COMMENT_FILE_RE,
33
+ COMMENT_IMAGES_MAX,
34
+ COMMENT_IMAGE_BYTES_MAX,
35
+ COMMENT_IMAGE_TYPES,
36
+ CommentError,
37
+ cleanImageName,
38
+ } from './comment-fields.js'
39
+ import { INK_RGB, drawingBounds } from './comment-drawing.js'
40
+
41
+ export const IMAGE_SIDE_MAX = 8192
42
+ export const IMAGE_PIXELS_MAX = 16_000_000
43
+ export const MEDIA_TYPES = Object.fromEntries(Object.entries(COMMENT_IMAGE_TYPES).map(([type, ext]) => [ext, type]))
44
+ export const MARKUP_RE = /^markup-[1-9][0-9]{0,15}(?:-r[1-9][0-9]{0,5})?\.png$/
45
+
46
+ const bad = (message, code = 'bad_image', status = 400) => new CommentError(status, code, message)
47
+
48
+ // The images of a request body ([{ name, mediaType, data }], data base64 or
49
+ // a data: URL), checked for count, type and size. Returns [{ name,
50
+ // mediaType, buffer }]. Cleaning comes after (cleanCommentImage).
51
+ export function parseImageInputs(raw) {
52
+ if (raw === undefined || raw === null) return []
53
+ if (!Array.isArray(raw) || raw.length > COMMENT_IMAGES_MAX)
54
+ throw bad(`Attach at most ${COMMENT_IMAGES_MAX} images to a comment or reply.`, 'too_many_images')
55
+ return raw.map((item, index) => {
56
+ const declared = String(item?.mediaType || '')
57
+ if (!COMMENT_IMAGE_TYPES[declared]) throw bad('Attach PNG, JPEG, WebP or GIF images.', 'bad_image_type', 415)
58
+ const data = typeof item?.data === 'string' ? item.data.replace(/^data:[^;,]+;base64,/, '') : ''
59
+ if (data.length > Math.ceil(COMMENT_IMAGE_BYTES_MAX / 3) * 4 + 4)
60
+ throw bad(`An image is at most ${COMMENT_IMAGE_BYTES_MAX / 1048576} MB.`, 'image_too_big', 413)
61
+ if (!data || !/^[A-Za-z0-9+/]+={0,2}$/.test(data)) throw bad('An image is not valid base64.')
62
+ const buffer = Buffer.from(data, 'base64')
63
+ if (buffer.length > COMMENT_IMAGE_BYTES_MAX)
64
+ throw bad(`An image is at most ${COMMENT_IMAGE_BYTES_MAX / 1048576} MB.`, 'image_too_big', 413)
65
+ if (sniffImage(buffer) !== declared) throw bad('An image does not match its type.', 'bad_image_type', 415)
66
+ return { name: cleanImageName(item.name, `image-${index + 1}.${COMMENT_IMAGE_TYPES[declared]}`), mediaType: declared, buffer }
67
+ })
68
+ }
69
+
70
+ // ---- Cleaning -----------------------------------------------------------------
71
+
72
+ const unreadable = () => bad('That image could not be read. Try saving it as a PNG or JPEG.')
73
+
74
+ function checkSize(width, height) {
75
+ if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width < 1 || height < 1) throw unreadable()
76
+ if (width > IMAGE_SIDE_MAX || height > IMAGE_SIDE_MAX || width * height > IMAGE_PIXELS_MAX)
77
+ throw bad(
78
+ `An image is at most ${IMAGE_SIDE_MAX} pixels on a side and ${IMAGE_PIXELS_MAX / 1e6} megapixels.`,
79
+ 'image_too_large_pixels',
80
+ 413,
81
+ )
82
+ return { width, height }
83
+ }
84
+
85
+ function cleanPng(buffer) {
86
+ // IHDR first, right after the signature: its size before any decoding.
87
+ if (buffer.length < 33 || buffer.toString('latin1', 12, 16) !== 'IHDR') throw unreadable()
88
+ const size = checkSize(buffer.readUInt32BE(16), buffer.readUInt32BE(20))
89
+ let image
90
+ try {
91
+ image = PNG.sync.read(buffer, { checkCRC: true })
92
+ } catch {
93
+ throw unreadable()
94
+ }
95
+ if (image.width !== size.width || image.height !== size.height) throw unreadable()
96
+ return { buffer: PNG.sync.write(image, { colorType: 6 }), ...size }
97
+ }
98
+
99
+ // Markers that carry pixels or decoding tables, and the APPn kept for colour.
100
+ const JPEG_SOF = new Set([0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf])
101
+ function keepJpegSegment(marker, data) {
102
+ if (marker === 0xe0) return data.toString('latin1', 0, 5) === 'JFIF\0'
103
+ if (marker === 0xe2) return data.toString('latin1', 0, 12) === 'ICC_PROFILE\0'
104
+ if (marker === 0xee) return data.toString('latin1', 0, 5) === 'Adobe'
105
+ if (marker >= 0xe0 && marker <= 0xef) return false // EXIF, XMP and the rest
106
+ if (marker === 0xfe) return false // comments
107
+ return true
108
+ }
109
+
110
+ function cleanJpeg(buffer) {
111
+ const out = [buffer.subarray(0, 2)]
112
+ let at = 2
113
+ let size = null
114
+ while (at < buffer.length) {
115
+ if (buffer[at] !== 0xff) throw unreadable()
116
+ let marker = buffer[at + 1]
117
+ // Fill bytes.
118
+ while (marker === 0xff && at + 2 < buffer.length) {
119
+ at += 1
120
+ marker = buffer[at + 1]
121
+ }
122
+ if (marker === undefined) throw unreadable()
123
+ if (marker === 0xd9) {
124
+ out.push(buffer.subarray(at, at + 2))
125
+ at += 2
126
+ break
127
+ }
128
+ if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) throw unreadable()
129
+ if (at + 4 > buffer.length) throw unreadable()
130
+ const length = buffer.readUInt16BE(at + 2)
131
+ if (length < 2 || at + 2 + length > buffer.length) throw unreadable()
132
+ const data = buffer.subarray(at + 4, at + 2 + length)
133
+ if (JPEG_SOF.has(marker)) {
134
+ if (data.length < 5) throw unreadable()
135
+ size = checkSize(data.readUInt16BE(3), data.readUInt16BE(1))
136
+ }
137
+ if (keepJpegSegment(marker, data)) out.push(buffer.subarray(at, at + 2 + length))
138
+ at += 2 + length
139
+ if (marker === 0xda) {
140
+ // Entropy-coded data up to the next marker that is not a restart or
141
+ // a stuffed zero; progressive files have several scans.
142
+ const start = at
143
+ while (at + 1 < buffer.length) {
144
+ if (buffer[at] === 0xff && buffer[at + 1] !== 0 && !(buffer[at + 1] >= 0xd0 && buffer[at + 1] <= 0xd7)) break
145
+ at += 1
146
+ }
147
+ if (at + 1 >= buffer.length) throw unreadable()
148
+ out.push(buffer.subarray(start, at))
149
+ }
150
+ }
151
+ if (!size) throw unreadable()
152
+ // Bytes after the end of the image (a hidden archive, say) are dropped.
153
+ return { buffer: Buffer.concat(out), ...size }
154
+ }
155
+
156
+ function cleanWebp(buffer) {
157
+ if (buffer.length < 20 || buffer.readUInt32LE(4) + 8 > buffer.length) throw unreadable()
158
+ const end = buffer.readUInt32LE(4) + 8
159
+ const chunks = []
160
+ let at = 12
161
+ let size = null
162
+ let flagsAt = -1
163
+ while (at + 8 <= end) {
164
+ const type = buffer.toString('latin1', at, at + 4)
165
+ const length = buffer.readUInt32LE(at + 4)
166
+ const padded = length + (length & 1)
167
+ if (!/^[A-Z0-9 ]{4}$/i.test(type) || at + 8 + length > end) throw unreadable()
168
+ const data = buffer.subarray(at + 8, at + 8 + length)
169
+ if (type === 'VP8X') {
170
+ if (length < 10) throw unreadable()
171
+ size = checkSize(data.readUIntLE(4, 3) + 1, data.readUIntLE(7, 3) + 1)
172
+ } else if (type === 'VP8 ' && !size) {
173
+ if (length < 10 || data[3] !== 0x9d || data[4] !== 0x01 || data[5] !== 0x2a) throw unreadable()
174
+ size = checkSize(data.readUInt16LE(6) & 0x3fff, data.readUInt16LE(8) & 0x3fff)
175
+ } else if (type === 'VP8L' && !size) {
176
+ if (length < 5 || data[0] !== 0x2f) throw unreadable()
177
+ const bits = data.readUInt32LE(1)
178
+ size = checkSize((bits & 0x3fff) + 1, ((bits >> 14) & 0x3fff) + 1)
179
+ }
180
+ if (type !== 'EXIF' && type !== 'XMP ') {
181
+ if (type === 'VP8X') flagsAt = chunks.reduce((sum, item) => sum + item.length, 12) + 8
182
+ chunks.push(buffer.subarray(at, Math.min(end, at + 8 + padded)))
183
+ }
184
+ at += 8 + padded
185
+ }
186
+ if (!size) throw unreadable()
187
+ const body = Buffer.concat(chunks)
188
+ const head = Buffer.alloc(12)
189
+ head.write('RIFF', 0, 'latin1')
190
+ head.writeUInt32LE(body.length + 4, 4)
191
+ head.write('WEBP', 8, 'latin1')
192
+ const out = Buffer.concat([head, body])
193
+ // VP8X flags: no EXIF (bit 3) or XMP (bit 2) any more.
194
+ if (flagsAt !== -1) out[flagsAt] &= ~0x0c
195
+ return { buffer: out, ...size }
196
+ }
197
+
198
+ function cleanGif(buffer) {
199
+ if (buffer.length < 14) throw unreadable()
200
+ const size = checkSize(buffer.readUInt16LE(6), buffer.readUInt16LE(8))
201
+ const packed = buffer[10]
202
+ let at = 13 + (packed & 0x80 ? 3 * 2 ** ((packed & 7) + 1) : 0)
203
+ const out = [buffer.subarray(0, at)]
204
+ const subBlocks = (from) => {
205
+ let cursor = from
206
+ while (cursor < buffer.length && buffer[cursor] !== 0) cursor += buffer[cursor] + 1
207
+ if (cursor >= buffer.length) throw unreadable()
208
+ return cursor + 1
209
+ }
210
+ let frames = 0
211
+ while (at < buffer.length) {
212
+ const kind = buffer[at]
213
+ if (kind === 0x3b) {
214
+ out.push(buffer.subarray(at, at + 1))
215
+ if (!frames) throw unreadable()
216
+ return { buffer: Buffer.concat(out), ...size }
217
+ }
218
+ if (kind === 0x2c) {
219
+ if (at + 10 > buffer.length) throw unreadable()
220
+ // Every frame: within the pixel cap and inside the logical screen. A
221
+ // decoder sizes its buffers by the frame, not by the screen.
222
+ const [left, top] = [buffer.readUInt16LE(at + 1), buffer.readUInt16LE(at + 3)]
223
+ const frame = checkSize(buffer.readUInt16LE(at + 5), buffer.readUInt16LE(at + 7))
224
+ if (left + frame.width > size.width || top + frame.height > size.height) throw unreadable()
225
+ const local = buffer[at + 9]
226
+ const data = at + 10 + (local & 0x80 ? 3 * 2 ** ((local & 7) + 1) : 0) + 1
227
+ const next = subBlocks(data)
228
+ out.push(buffer.subarray(at, next))
229
+ frames += 1
230
+ at = next
231
+ } else if (kind === 0x21) {
232
+ const label = buffer[at + 1]
233
+ const next = subBlocks(at + 2)
234
+ const app = label === 0xff ? buffer.toString('latin1', at + 3, at + 14) : ''
235
+ // Graphic control (timing, transparency), plain text and the loop count stay.
236
+ if (label === 0xf9 || label === 0x01 || app === 'NETSCAPE2.0' || app === 'ANIMEXTS1.0') out.push(buffer.subarray(at, next))
237
+ at = next
238
+ } else throw unreadable()
239
+ }
240
+ throw unreadable()
241
+ }
242
+
243
+ // { buffer, mediaType, width, height } of a cleaned image, or a CommentError.
244
+ export function cleanCommentImage(buffer, mediaType) {
245
+ if (!Buffer.isBuffer(buffer) || sniffImage(buffer) !== mediaType) throw bad('An image does not match its type.', 'bad_image_type', 415)
246
+ const clean =
247
+ mediaType === 'image/png'
248
+ ? cleanPng(buffer)
249
+ : mediaType === 'image/jpeg'
250
+ ? cleanJpeg(buffer)
251
+ : mediaType === 'image/webp'
252
+ ? cleanWebp(buffer)
253
+ : cleanGif(buffer)
254
+ if (sniffImage(clean.buffer) !== mediaType) throw unreadable()
255
+ if (clean.buffer.length > COMMENT_IMAGE_BYTES_MAX * 2) throw bad('That image is too large once cleaned.', 'image_too_big', 413)
256
+ return { ...clean, mediaType }
257
+ }
258
+
259
+ // ---- Local storage --------------------------------------------------------------
260
+
261
+ export function commentFilesDir(boardFolder) {
262
+ return path.join(boardFolder, 'comment-files')
263
+ }
264
+
265
+ // The stored file for a name, or null: only names Shapewall writes.
266
+ export function commentFilePath(boardFolder, name) {
267
+ const value = String(name || '')
268
+ if (!COMMENT_FILE_RE.test(value) && !MARKUP_RE.test(value)) return null
269
+ const file = path.join(commentFilesDir(boardFolder), value)
270
+ try {
271
+ return fs.lstatSync(file).isFile() ? file : null
272
+ } catch {
273
+ return null
274
+ }
275
+ }
276
+
277
+ function writeNew(file, data) {
278
+ fs.mkdirSync(path.dirname(file), { recursive: true })
279
+ const temp = `${file}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`
280
+ try {
281
+ fs.writeFileSync(temp, data, { mode: 0o644, flag: 'wx' })
282
+ fs.renameSync(temp, file)
283
+ } catch (error) {
284
+ fs.rmSync(temp, { force: true })
285
+ throw error
286
+ }
287
+ }
288
+
289
+ export const newImageFile = (mediaType) => `${randomBytes(12).toString('hex')}.${COMMENT_IMAGE_TYPES[mediaType]}`
290
+
291
+ // Cleans and writes the images; returns their records. On any failure the
292
+ // ones already written are removed again and the error is thrown.
293
+ export function saveCommentImages(boardFolder, inputs) {
294
+ const written = []
295
+ try {
296
+ const cleaned = inputs.map((input) => ({ ...cleanCommentImage(input.buffer, input.mediaType), name: input.name }))
297
+ for (const image of cleaned) {
298
+ const file = newImageFile(image.mediaType)
299
+ writeNew(path.join(commentFilesDir(boardFolder), file), image.buffer)
300
+ written.push({
301
+ id: file.split('.')[0],
302
+ file,
303
+ name: image.name,
304
+ mediaType: image.mediaType,
305
+ bytes: image.buffer.length,
306
+ width: image.width,
307
+ height: image.height,
308
+ })
309
+ }
310
+ return written
311
+ } catch (error) {
312
+ removeImageFiles(boardFolder, written)
313
+ throw error
314
+ }
315
+ }
316
+
317
+ export function removeImageFiles(boardFolder, images) {
318
+ for (const image of images || []) {
319
+ const file = commentFilePath(boardFolder, image.file)
320
+ if (file) fs.rmSync(file, { force: true })
321
+ }
322
+ }
323
+
324
+ // Sends a stored comment image or markup PNG: as the image type its name
325
+ // says (set when it was cleaned), never sniffed, never a document. Ids are
326
+ // random and the files never change, so it may be cached.
327
+ export function sendCommentFile(res, file, name) {
328
+ res.setHeader('Cache-Control', 'private, max-age=604800, immutable')
329
+ res.setHeader('Cross-Origin-Resource-Policy', 'same-origin')
330
+ res.setHeader('Content-Security-Policy', "default-src 'none'; sandbox")
331
+ res.setHeader('X-Content-Type-Options', 'nosniff')
332
+ res.setHeader('Content-Disposition', 'inline')
333
+ res.type(MEDIA_TYPES[name.split('.').pop()] || 'application/octet-stream').sendFile(file, { dotfiles: 'allow' })
334
+ }
335
+
336
+ export const markupName = (commentId, replyId = null) => `markup-${commentId}${replyId ? `-r${replyId}` : ''}.png`
337
+
338
+ // Every file a comment owns: its images, its replies' images, its markup.
339
+ export function removeCommentMedia(boardFolder, comment) {
340
+ removeImageFiles(boardFolder, comment.images)
341
+ for (const reply of comment.replies || []) removeImageFiles(boardFolder, reply.images)
342
+ const dir = commentFilesDir(boardFolder)
343
+ let names = []
344
+ try {
345
+ names = fs.readdirSync(dir)
346
+ } catch {
347
+ return
348
+ }
349
+ const prefix = `markup-${comment.id}`
350
+ for (const name of names)
351
+ if (MARKUP_RE.test(name) && (name === `${prefix}.png` || name.startsWith(`${prefix}-r`)))
352
+ fs.rmSync(path.join(dir, name), { force: true })
353
+ }
354
+
355
+ // Bytes stored for a board's comment files.
356
+ export function commentFilesBytes(boardFolder) {
357
+ const dir = commentFilesDir(boardFolder)
358
+ let total = 0
359
+ try {
360
+ for (const name of fs.readdirSync(dir)) {
361
+ const stat = fs.lstatSync(path.join(dir, name))
362
+ if (stat.isFile()) total += stat.size
363
+ }
364
+ } catch {
365
+ /* none */
366
+ }
367
+ return total
368
+ }
369
+
370
+ // ---- Markup: a drawing rendered on its screenshot --------------------------------
371
+
372
+ // The largest markup image, on its long side; bigger crops are scaled down.
373
+ export const MARKUP_EDGE_MAX = 1200
374
+ const MARKUP_PAD = 48
375
+ const MARKUP_MIN = { width: 360, height: 240 }
376
+
377
+ function stamp(image, cx, cy, radius, [r, g, b]) {
378
+ const r2 = radius * radius
379
+ const x0 = Math.max(0, Math.floor(cx - radius))
380
+ const x1 = Math.min(image.width - 1, Math.ceil(cx + radius))
381
+ const y0 = Math.max(0, Math.floor(cy - radius))
382
+ const y1 = Math.min(image.height - 1, Math.ceil(cy + radius))
383
+ for (let y = y0; y <= y1; y += 1) {
384
+ for (let x = x0; x <= x1; x += 1) {
385
+ const dx = x + 0.5 - cx
386
+ const dy = y + 0.5 - cy
387
+ if (dx * dx + dy * dy > r2) continue
388
+ const at = (y * image.width + x) * 4
389
+ image.data[at] = r
390
+ image.data[at + 1] = g
391
+ image.data[at + 2] = b
392
+ image.data[at + 3] = 255
393
+ }
394
+ }
395
+ }
396
+
397
+ function line(image, [ax, ay], [bx, by], radius, rgb) {
398
+ const steps = Math.max(1, Math.ceil(Math.hypot(bx - ax, by - ay) / Math.max(0.5, radius / 2)))
399
+ for (let i = 0; i <= steps; i += 1) stamp(image, ax + ((bx - ax) * i) / steps, ay + ((by - ay) * i) / steps, radius, rgb)
400
+ }
401
+
402
+ function drawShape(image, shape, toPixel, scale) {
403
+ const rgb = INK_RGB[shape.ink] || INK_RGB.accent
404
+ const radius = Math.max(1, (shape.width * scale) / 2)
405
+ const points = shape.points.map(toPixel)
406
+ if (shape.tool === 'box') {
407
+ const [[x1, y1], [x2, y2]] = points
408
+ for (const [a, b] of [
409
+ [
410
+ [x1, y1],
411
+ [x2, y1],
412
+ ],
413
+ [
414
+ [x2, y1],
415
+ [x2, y2],
416
+ ],
417
+ [
418
+ [x2, y2],
419
+ [x1, y2],
420
+ ],
421
+ [
422
+ [x1, y2],
423
+ [x1, y1],
424
+ ],
425
+ ])
426
+ line(image, a, b, radius, rgb)
427
+ return
428
+ }
429
+ for (let i = 1; i < points.length; i += 1) line(image, points[i - 1], points[i], radius, rgb)
430
+ if (shape.tool === 'arrow') {
431
+ const [[ax, ay], [bx, by]] = points
432
+ const angle = Math.atan2(by - ay, bx - ax)
433
+ const head = Math.max(10 * scale, radius * 5)
434
+ for (const turn of [Math.PI * 0.82, -Math.PI * 0.82]) {
435
+ line(image, [bx, by], [bx + Math.cos(angle + turn) * head, by + Math.sin(angle + turn) * head], radius, rgb)
436
+ }
437
+ }
438
+ }
439
+
440
+ // The PNG of a drawing on its screenshot: the screenshot cropped around the
441
+ // drawing (with a margin), scaled to at most MARKUP_EDGE_MAX on its long
442
+ // side, the ink on top. Deterministic: the same inputs give the same bytes.
443
+ // Returns { png, rect } (rect: the crop in screenshot pixels) or null.
444
+ export function renderMarkup(screenshot, drawing) {
445
+ const bounds = drawingBounds(drawing)
446
+ if (!bounds) return null
447
+ let source
448
+ try {
449
+ source = PNG.sync.read(screenshot)
450
+ } catch {
451
+ return null
452
+ }
453
+ const { width: W, height: H } = source
454
+ const want = {
455
+ x: bounds.x * W - MARKUP_PAD,
456
+ y: bounds.y * H - MARKUP_PAD,
457
+ width: bounds.width * W + MARKUP_PAD * 2,
458
+ height: bounds.height * H + MARKUP_PAD * 2,
459
+ }
460
+ const grow = (start, length, min, total) => {
461
+ const size = Math.min(total, Math.max(length, min))
462
+ const begin = Math.max(0, Math.min(total - size, start - (size - length) / 2))
463
+ return [Math.round(begin), Math.round(size)]
464
+ }
465
+ const [x, width] = grow(want.x, want.width, MARKUP_MIN.width, W)
466
+ const [y, height] = grow(want.y, want.height, MARKUP_MIN.height, H)
467
+ const scale = Math.min(1, MARKUP_EDGE_MAX / Math.max(width, height))
468
+ const out = { width: Math.max(1, Math.round(width * scale)), height: Math.max(1, Math.round(height * scale)) }
469
+ out.data = Buffer.alloc(out.width * out.height * 4)
470
+ // Area average from the source into the output (nearest at scale 1).
471
+ for (let oy = 0; oy < out.height; oy += 1) {
472
+ const sy0 = y + oy / scale
473
+ const sy1 = Math.min(y + height, sy0 + 1 / scale)
474
+ for (let ox = 0; ox < out.width; ox += 1) {
475
+ const sx0 = x + ox / scale
476
+ const sx1 = Math.min(x + width, sx0 + 1 / scale)
477
+ let r = 0
478
+ let g = 0
479
+ let b = 0
480
+ let n = 0
481
+ for (let sy = Math.floor(sy0); sy < Math.max(Math.floor(sy0) + 1, Math.ceil(sy1)); sy += 1) {
482
+ for (let sx = Math.floor(sx0); sx < Math.max(Math.floor(sx0) + 1, Math.ceil(sx1)); sx += 1) {
483
+ const at = (Math.min(H - 1, sy) * W + Math.min(W - 1, sx)) * 4
484
+ const alpha = source.data[at + 3] / 255
485
+ // Transparent parts of a capture read as white.
486
+ r += source.data[at] * alpha + 255 * (1 - alpha)
487
+ g += source.data[at + 1] * alpha + 255 * (1 - alpha)
488
+ b += source.data[at + 2] * alpha + 255 * (1 - alpha)
489
+ n += 1
490
+ }
491
+ }
492
+ const at = (oy * out.width + ox) * 4
493
+ out.data[at] = Math.round(r / n)
494
+ out.data[at + 1] = Math.round(g / n)
495
+ out.data[at + 2] = Math.round(b / n)
496
+ out.data[at + 3] = 255
497
+ }
498
+ }
499
+ const toPixel = ([px, py]) => [(px * W - x) * scale, (py * H - y) * scale]
500
+ for (const shape of drawing.shapes) drawShape(out, shape, toPixel, scale)
501
+ return { png: PNG.sync.write(out, { colorType: 6 }), rect: { x, y, width, height } }
502
+ }
503
+
504
+ // The markup file of a comment (replyId null) or reply: rendered once from
505
+ // `screenshot` (the capture it was drawn on), then read from disk. Returns
506
+ // the path, or null when there is no drawing or no screenshot.
507
+ export function markupFile(boardFolder, { commentId, replyId = null, drawing, screenshot }) {
508
+ if (!drawing?.shapes?.length || !screenshot) return null
509
+ const name = markupName(commentId, replyId)
510
+ const existing = commentFilePath(boardFolder, name)
511
+ if (existing) return existing
512
+ let rendered
513
+ try {
514
+ const stat = fs.lstatSync(screenshot)
515
+ if (!stat.isFile() || stat.size > 40 * 1024 * 1024) return null
516
+ rendered = renderMarkup(fs.readFileSync(screenshot), drawing)
517
+ } catch {
518
+ return null
519
+ }
520
+ if (!rendered) return null
521
+ const file = path.join(commentFilesDir(boardFolder), name)
522
+ try {
523
+ writeNew(file, rendered.png)
524
+ } catch (error) {
525
+ if (error.code !== 'EEXIST') return null
526
+ }
527
+ return commentFilePath(boardFolder, name)
528
+ }
package/src/config.js CHANGED
@@ -12,6 +12,7 @@ import { validateStarterAgent } from './agent/starter/settings.js'
12
12
  import { cliCommand } from './invocation.js'
13
13
  import { normalizeBudgets } from './checks/budgets.js'
14
14
  import { CAPTURE_PACING_DEFAULTS, CAPTURE_PACING_MAX } from './capture/pacing.js'
15
+ import { validateCi } from './ci/settings.js'
15
16
 
16
17
  export const CONFIG_FILE = 'shapewall.config.json'
17
18
  export const ID_RE = /^[a-z0-9][a-z0-9-]{0,63}$/
@@ -939,6 +940,11 @@ export function normalizeConfig(raw, { dir = process.cwd(), file = null, env = p
939
940
  if (github?.board && !ids.has(github.board)) problems.push(`github.board: no board "${github.board}"`)
940
941
  const agent = validateAgent(raw.agent, problems)
941
942
  const starterAgent = validateStarterAgent(raw.starterAgent, problems, { boardIds: [...ids] })
943
+ // Capture on pull requests (src/ci/settings.js): allow-lists name pages and viewports of any board.
944
+ const ci = validateCi(raw.ci, problems, {
945
+ pageIds: boards.flatMap((board) => board.pages.map((page) => page.id)),
946
+ viewportNames: [...new Set([...Object.keys(viewports || {}), ...boards.flatMap((board) => Object.keys(board.viewports || {}))])],
947
+ })
942
948
  if (problems.length) throw new ConfigError(problems, file)
943
949
  return {
944
950
  file,
@@ -960,6 +966,7 @@ export function normalizeConfig(raw, { dir = process.cwd(), file = null, env = p
960
966
  github,
961
967
  agent,
962
968
  starterAgent,
969
+ ci,
963
970
  boards,
964
971
  notifications,
965
972
  }
package/src/doctor.js CHANGED
@@ -15,6 +15,7 @@ import { agentSettings } from './agent/config.js'
15
15
  import { formatCommand, loginHint } from './agent/acp/command.js'
16
16
  import { acpHandshake } from './agent/backends/acp.js'
17
17
  import { cliCommand, invocation } from './invocation.js'
18
+ import { listAgentBranches } from './agent/branch-mode.js'
18
19
 
19
20
  const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
20
21
  const PACKAGE = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8'))
@@ -247,6 +248,26 @@ export async function checkAcpAgent(config, env = process.env, { acp = null, lau
247
248
  }
248
249
  }
249
250
 
251
+ // Agent branch mode (src/agent/branch-mode.js): branches and worktrees the
252
+ // agent left in .shapewall/agent-worktrees/. Shown only when there are some.
253
+ export async function checkAgentBranches(config) {
254
+ if (!config) return null
255
+ const items = await listAgentBranches(config).catch(() => [])
256
+ if (!items.length) return null
257
+ const waiting = items.filter((item) => item.state === 'ready')
258
+ const folders = items.filter((item) => item.worktree).length
259
+ const names = items
260
+ .slice(0, 3)
261
+ .map((item) => item.branch)
262
+ .join(', ')
263
+ return {
264
+ name: 'agent-branches',
265
+ status: 'warn',
266
+ message: `Agent branches: ${items.length} left (${waiting.length} ready to apply, ${folders} worktree folder${folders === 1 ? '' : 's'}): ${names}${items.length > 3 ? ', ...' : ''}`,
267
+ fix: 'Apply or discard each one from its answer in the chat. Folders of finished turns are swept when Shapewall starts; ones with unapplied commits are kept.',
268
+ }
269
+ }
270
+
250
271
  export async function runDoctor({
251
272
  configFile = null,
252
273
  port = 4600,
@@ -272,6 +293,8 @@ export async function runDoctor({
272
293
  useAcp ? await checkAcpAgent(config, env, { acp, launch }) : checkAgent(config, env),
273
294
  await checkGithub(config, { env, fetchImpl, network }),
274
295
  ]
296
+ const branches = await checkAgentBranches(config)
297
+ if (branches) checks.push(branches)
275
298
  return { ok: !checks.some((check) => check.status === 'fail'), checks }
276
299
  }
277
300
 
package/src/export.js CHANGED
@@ -86,6 +86,14 @@ export async function exportStatic(
86
86
  files += copyMatching(path.join(source, 'variants'), path.join(dir, 'variants'), media)
87
87
  // Previous screenshots, for before/after on resolved comments (not their live HTML).
88
88
  if (includeComments) files += copyMatching(path.join(source, 'versions'), path.join(dir, 'versions'), (name) => name.endsWith('.png'))
89
+ // Images on the exported comments and their replies (not markup PNGs,
90
+ // which are for agents, nor files of deleted comments).
91
+ if (includeComments) {
92
+ const kept = new Set()
93
+ for (const comment of listComments(config, board.id))
94
+ for (const item of [comment, ...comment.replies]) for (const image of item.images || []) kept.add(image.file)
95
+ files += copyMatching(path.join(source, 'comment-files'), path.join(dir, 'comment-files'), (name) => kept.has(name))
96
+ }
89
97
  files += await exportCompare(config, board, dir)
90
98
  }
91
99
  return { out: target, boards: config.boards.length, files }