shapewall 0.2.4 → 0.3.0

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 +36 -0
  2. package/README.md +35 -1
  3. package/dist/agent/package-lock.json +2 -2
  4. package/dist/client/assets/BoardChat-CupOxioV.js +27 -0
  5. package/dist/client/assets/{BuzzDialog-CZdwIQLF.js → BuzzDialog-Ds7Thpfe.js} +1 -1
  6. package/dist/client/assets/ChecksPanel-BqrS02Ih.js +1 -0
  7. package/dist/client/assets/History-CQG3NQOC.js +1 -0
  8. package/dist/client/assets/{LocalSettings-B5gtfKEl.js → LocalSettings-CSNMhNyV.js} +1 -1
  9. package/dist/client/assets/{Management-BxEUZAmI.js → Management-CJVNAqAW.js} +1 -1
  10. package/dist/client/assets/QuickConnect-B-4pUgXH.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-rhaw0MRk.js} +1 -1
  13. package/dist/client/assets/{circle-x-BmT6vHKl.js → circle-x-PRxj7kPi.js} +1 -1
  14. package/dist/client/assets/index-CXtuJi1K.css +2 -0
  15. package/dist/client/assets/index-DKOCIQiq.js +37 -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-DOKZI6Lt.js +1 -0
  23. package/dist/console/assets/LocalConsole-DXzldLW9.js +1 -0
  24. package/dist/console/assets/{app-update-dialog-o4QY_jL9.js → app-update-dialog-CrqIMEwN.js} +1 -1
  25. package/dist/console/assets/console-76WovHlh.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 +192 -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 +254 -25
  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 +238 -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 +33 -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
@@ -2,11 +2,15 @@
2
2
  // identities never enter the hosted store. Every batch and its retry result
3
3
  // commit together. A completed session can only be replayed by its own token.
4
4
  import { createHash } from 'node:crypto'
5
- import { AGENT_STATUSES, AUTHOR_MAX, COMMENT_BODY_MAX, CommentError, cleanFixRef, validateAnchor, VERSION_RE } from '../store.js'
5
+ import { STORED_AGENT_STATUSES, AUTHOR_MAX, COMMENT_BODY_MAX, CommentError, cleanFixRef, validateAnchor, VERSION_RE } from '../store.js'
6
6
  import { ID_RE, VIEWPORT_NAME_RE } from '../config.js'
7
7
  import { REACTIONS } from '../reactions.js'
8
8
  import { findApiToken } from './tenancy.js'
9
9
  import { transaction, hasCommentPolicy } from './db.js'
10
+ import { COMMENT_IMAGES_MAX, cleanImageName } from '../comment-fields.js'
11
+ import { DrawingError, validateDrawing } from '../comment-drawing.js'
12
+ import { parseImageInputs } from '../comment-media.js'
13
+ import { checkStorageQuota, cleanImages, storeImages } from './comment-files.js'
10
14
 
11
15
  const hex = /^[a-f0-9]{64}$/
12
16
  const SCOPE = 'workspace_id = :ws AND project_id = :project AND board_id = :board'
@@ -37,6 +41,35 @@ const reactions = (value) => {
37
41
  })
38
42
  }
39
43
 
44
+ // Images of an imported comment or reply: refs of images uploaded first in
45
+ // the same session (POST .../import/images), with their display names.
46
+ const importImages = (value) => {
47
+ if (value === undefined || value === null) return []
48
+ if (!Array.isArray(value) || value.length > COMMENT_IMAGES_MAX) bad(`At most ${COMMENT_IMAGES_MAX} images on a comment or reply.`)
49
+ return value.map((item) => {
50
+ if (!item || typeof item.ref !== 'string' || !hex.test(item.ref)) bad('An imported image needs the ref it was uploaded with.')
51
+ return { ref: item.ref, name: cleanImageName(item.name, 'image') }
52
+ })
53
+ }
54
+ // A drawing, checked like a new one; its capture version is the one the
55
+ // local copy recorded (a comment's own, or the reply's when valid).
56
+ const importDrawing = (value, version) => {
57
+ try {
58
+ const clean = validateDrawing(value)
59
+ return clean ? { ...clean, captureVersion: typeof version === 'string' && VERSION_RE.test(version) ? version : null } : null
60
+ } catch (error) {
61
+ if (error instanceof DrawingError) bad(error.message)
62
+ throw error
63
+ }
64
+ }
65
+ // Only when present, so comments without media hash as they always did
66
+ // (an earlier import of them still reads as unchanged).
67
+ const withImportMedia = (out, images, drawing) => ({
68
+ ...out,
69
+ ...(images.length ? { images } : {}),
70
+ ...(drawing ? { drawing } : {}),
71
+ })
72
+
40
73
  export function cleanImportedComment(raw) {
41
74
  if (!raw || typeof raw !== 'object' || typeof raw.importKey !== 'string' || !hex.test(raw.importKey))
42
75
  bad('An import needs a SHA-256 importKey.')
@@ -49,11 +82,12 @@ export function cleanImportedComment(raw) {
49
82
  const body = text(raw.body, COMMENT_BODY_MAX)
50
83
  if (!body) bad('A comment needs text.')
51
84
  if (!['open', 'resolved'].includes(raw.status)) bad('Invalid comment state.')
52
- if (raw.agentStatus != null && !AGENT_STATUSES.includes(raw.agentStatus)) bad('Invalid agent status.')
85
+ if (raw.agentStatus != null && !STORED_AGENT_STATUSES.includes(raw.agentStatus)) bad('Invalid agent status.')
53
86
  if (!Number.isInteger(raw.reopenCount ?? 0) || (raw.reopenCount ?? 0) < 0 || (raw.reopenCount ?? 0) > 100000) bad('Invalid reopen count.')
54
87
  if (raw.captureVersion != null && !VERSION_RE.test(raw.captureVersion)) bad('Invalid capture version.')
55
88
  if (!Array.isArray(raw.replies) || raw.replies.length > 200) bad('At most 200 replies per comment.')
56
- return {
89
+ const media = { images: importImages(raw.images), drawing: importDrawing(raw.drawing, raw.captureVersion) }
90
+ const cleanItem = {
57
91
  importKey: raw.importKey,
58
92
  authorKind: raw.authorKind || 'local',
59
93
  localId: raw.localId,
@@ -80,24 +114,79 @@ export function cleanImportedComment(raw) {
80
114
  replies: raw.replies.map((reply, i) => {
81
115
  const body = text(reply?.body, COMMENT_BODY_MAX)
82
116
  if (!body) bad('A reply needs text.')
83
- return {
84
- id: i + 1,
85
- author: author(reply.author),
86
- body,
87
- createdAt: date(reply.createdAt),
88
- ...(reply.agent === true ? { agent: true } : {}),
89
- ...(reply.guest === true ? { guest: true } : {}),
90
- imported: true,
91
- ...(reply.editedAt ? { editedAt: date(reply.editedAt) } : {}),
92
- importedReactions: reactions(reply.reactions),
93
- }
117
+ return withImportMedia(
118
+ {
119
+ id: i + 1,
120
+ author: author(reply.author),
121
+ body,
122
+ createdAt: date(reply.createdAt),
123
+ ...(reply.agent === true ? { agent: true } : {}),
124
+ ...(reply.guest === true ? { guest: true } : {}),
125
+ imported: true,
126
+ ...(reply.editedAt ? { editedAt: date(reply.editedAt) } : {}),
127
+ importedReactions: reactions(reply.reactions),
128
+ },
129
+ importImages(reply.images),
130
+ importDrawing(reply.drawing, reply.drawing?.captureVersion),
131
+ )
94
132
  }),
95
133
  }
134
+ return withImportMedia(cleanItem, media.images, media.drawing)
135
+ }
136
+
137
+ // Attaches the staged images an imported comment lists (by ref, uploaded in
138
+ // this session) to it and its replies, and removes the images it had from
139
+ // an earlier import that it no longer lists. Returns { images, replies } with
140
+ // image records in place of refs. Inside the batch's transaction.
141
+ function attachImportImages(t, { project, board, number, session, item }) {
142
+ const keep = new Set()
143
+ const attach = (list, replyId) =>
144
+ list.map(({ ref, name }) => {
145
+ const row = t.get(
146
+ `SELECT file, comment_number, reply_id, media_type, bytes, width, height FROM comment_files
147
+ WHERE ${SCOPE} AND import_session = :session AND import_ref = :ref`,
148
+ { project, board, session, ref },
149
+ )
150
+ if (!row) throw new CommentError(400, 'import_image_missing', 'An imported image was not uploaded first. Run shapewall share again.')
151
+ if (keep.has(row.file) || (row.comment_number !== null && (row.comment_number !== number || (row.reply_id ?? null) !== replyId)))
152
+ bad('An imported image belongs to another comment.')
153
+ t.run(`UPDATE comment_files SET comment_number = :number, reply_id = :reply, name = :name WHERE ${SCOPE} AND file = :file`, {
154
+ project,
155
+ board,
156
+ number,
157
+ reply: replyId,
158
+ name,
159
+ file: row.file,
160
+ })
161
+ keep.add(row.file)
162
+ return {
163
+ id: row.file.split('.')[0],
164
+ file: row.file,
165
+ name,
166
+ mediaType: row.media_type,
167
+ bytes: row.bytes,
168
+ width: row.width,
169
+ height: row.height,
170
+ }
171
+ })
172
+ const images = attach(item.images || [], null)
173
+ const replies = item.replies.map(({ images: refs, ...reply }) => {
174
+ const records = attach(refs || [], reply.id)
175
+ return records.length ? { ...reply, images: records } : reply
176
+ })
177
+ for (const old of t.all(`SELECT file FROM comment_files WHERE ${SCOPE} AND comment_number = :number`, { project, board, number }))
178
+ if (!keep.has(old.file)) t.run(`DELETE FROM comment_files WHERE ${SCOPE} AND file = :file`, { project, board, file: old.file })
179
+ return { images, replies }
96
180
  }
97
181
 
98
182
  // Includes reactions as well as text: a remote reaction must not be silently
99
183
  // overwritten by a later local snapshot. Identities remain server-side here.
100
- const fingerprint = (row) => digest(row)
184
+ // Rows without images or a drawing hash as they did before those columns
185
+ // existed, so comments imported earlier do not all read as changed.
186
+ const fingerprint = (row) => {
187
+ const { images, drawing, ...rest } = row
188
+ return digest(images || drawing ? row : rest)
189
+ }
101
190
 
102
191
  export function importBatch({ db, t, projectId, board, token, tokenHash, input, now = new Date() }) {
103
192
  if (
@@ -200,8 +289,9 @@ export function importBatch({ db, t, projectId, board, token, tokenHash, input,
200
289
  },
201
290
  )
202
291
  }
292
+ const media = attachImportImages(t, { ...key, number, session: input.sessionId, item })
203
293
  t.run(
204
- `UPDATE comments SET page_id=:page, viewport=:viewport, x=:x, y=:y, body=:body, author=:author, author_handle=NULL, author_user_id=NULL, author_token_id=NULL, author_share_id=NULL, anchor=:anchor, capture_version=:version, status=:status, created_at=:created, updated_at=:updated, resolved_at=:resolved, resolution=:resolution, replies=:replies, agent_status=:agent, agent_status_at=:agentAt, reopen_count=:reopens, reopened_at=:reopened, fix_ref=:fix, edited_at=:edited, reactions='[]', imported_by=:user, import_author_kind=:kind, imported_at=:at, import_orphan=:orphan, imported_reactions=:reactions ${hasPolicy ? ', imported=1, dispatch_hash=NULL, dispatch_by=NULL, dispatch_at=NULL, run_approved=0' : ''} WHERE ${SCOPE} AND number = :number`,
294
+ `UPDATE comments SET page_id=:page, viewport=:viewport, x=:x, y=:y, body=:body, author=:author, author_handle=NULL, author_user_id=NULL, author_token_id=NULL, author_share_id=NULL, anchor=:anchor, capture_version=:version, status=:status, created_at=:created, updated_at=:updated, resolved_at=:resolved, resolution=:resolution, replies=:replies, images=:images, drawing=:drawing, agent_status=:agent, agent_status_at=:agentAt, reopen_count=:reopens, reopened_at=:reopened, fix_ref=:fix, edited_at=:edited, reactions='[]', imported_by=:user, import_author_kind=:kind, imported_at=:at, import_orphan=:orphan, imported_reactions=:reactions ${hasPolicy ? ', imported=1, dispatch_hash=NULL, dispatch_by=NULL, dispatch_at=NULL, run_approved=0' : ''} WHERE ${SCOPE} AND number = :number`,
205
295
  {
206
296
  ...key,
207
297
  number,
@@ -218,7 +308,9 @@ export function importBatch({ db, t, projectId, board, token, tokenHash, input,
218
308
  updated: item.updatedAt,
219
309
  resolved: item.resolvedAt,
220
310
  resolution: item.resolution,
221
- replies: JSON.stringify(item.replies),
311
+ replies: JSON.stringify(media.replies),
312
+ images: media.images.length ? JSON.stringify(media.images) : null,
313
+ drawing: item.drawing ? JSON.stringify(item.drawing) : null,
222
314
  agent: item.agentStatus,
223
315
  agentAt: item.agentStatusAt,
224
316
  reopens: item.reopenCount,
@@ -257,6 +349,11 @@ export function importBatch({ db, t, projectId, board, token, tokenHash, input,
257
349
  })
258
350
  }
259
351
  if (commit) {
352
+ // Images uploaded for this move that no batch attached go now.
353
+ t.run(
354
+ 'DELETE FROM comment_files WHERE workspace_id = :ws AND project_id = :project AND import_session = :session AND comment_number IS NULL',
355
+ session,
356
+ )
260
357
  t.run('UPDATE import_sessions SET committed_at=:at WHERE workspace_id = :ws AND project_id = :project AND session_id=:session', {
261
358
  ...session,
262
359
  at: now.toISOString(),
@@ -275,7 +372,105 @@ export function importBatch({ db, t, projectId, board, token, tokenHash, input,
275
372
  })
276
373
  }
277
374
 
278
- export function mountImports(v1, { db, boardContext, handle, now }) {
375
+ // One image of an imported comment, uploaded before the batch that lists it
376
+ // by `ref` (any 64 hex the sender picks, unique in its session; the CLI
377
+ // hashes the comment, the slot and the bytes, so a retry sends the same
378
+ // ref and gets the same file back). Checked and cleaned exactly like an
379
+ // image posted on the board, counted against the storage quota, and staged
380
+ // (no comment yet) until a batch attaches it; the session's commit or the
381
+ // staging sweep removes it otherwise.
382
+ export async function importImage({ db, t, projectId, board, token, tokenHash, input, cleaner, quotaBytes, now = new Date() }) {
383
+ if (typeof input?.sessionId !== 'string' || !hex.test(input.sessionId) || typeof input?.ref !== 'string' || !hex.test(input.ref))
384
+ bad('An image upload needs the import session and a ref.')
385
+ if (!token.scopes.includes('import'))
386
+ throw new CommentError(
387
+ 403,
388
+ 'missing_scope',
389
+ 'This token needs the one-time import scope. Run shapewall share again and approve the move.',
390
+ )
391
+ const parsed = parseImageInputs([input.image])
392
+ const key = { project: projectId, board: board.id, session: input.sessionId, ref: input.ref }
393
+ const staged = () =>
394
+ t.get(
395
+ `SELECT file, media_type, bytes, width, height FROM comment_files WHERE ${SCOPE} AND import_session = :session AND import_ref = :ref`,
396
+ key,
397
+ )
398
+ const shape = (row) => ({
399
+ ref: input.ref,
400
+ file: row.file,
401
+ mediaType: row.media_type,
402
+ bytes: row.bytes,
403
+ width: row.width,
404
+ height: row.height,
405
+ })
406
+ const known = staged()
407
+ if (known) return shape(known)
408
+ const [image] = await cleanImages(cleaner, parsed)
409
+ return transaction(db, () => {
410
+ const live = typeof tokenHash === 'string' ? findApiToken(db, tokenHash, now.toISOString()) : null
411
+ if (!live || live.id !== token.id || live.workspace_id !== t.workspaceId || live.project_id !== projectId)
412
+ throw new CommentError(401, 'bad_token', 'This token is no longer valid for this project.')
413
+ if (!JSON.parse(live.scopes).includes('import'))
414
+ throw new CommentError(
415
+ 403,
416
+ 'missing_scope',
417
+ 'This token needs the one-time import scope. Run shapewall share again and approve the move.',
418
+ )
419
+ const prior = t.get(
420
+ 'SELECT token_id, committed_at FROM import_sessions WHERE workspace_id = :ws AND project_id = :project AND session_id = :session',
421
+ { project: projectId, session: input.sessionId },
422
+ )
423
+ if (prior && (prior.token_id !== token.id || prior.committed_at))
424
+ throw new CommentError(409, 'import_finished', 'This import session has finished or belongs to another token.')
425
+ const again = staged()
426
+ if (again) return shape(again)
427
+ checkStorageQuota(t, image.buffer.length, quotaBytes)
428
+ const [record] = storeImages(t, {
429
+ projectId,
430
+ boardId: board.id,
431
+ commentNumber: null,
432
+ images: [image],
433
+ at: now.toISOString(),
434
+ importSession: input.sessionId,
435
+ importRef: input.ref,
436
+ })
437
+ return {
438
+ ref: input.ref,
439
+ file: record.file,
440
+ mediaType: record.mediaType,
441
+ bytes: record.bytes,
442
+ width: record.width,
443
+ height: record.height,
444
+ }
445
+ })
446
+ }
447
+
448
+ export function mountImports(v1, { db, boardContext, handle, now, cleaner = null, quotaOf = () => Infinity }) {
449
+ // One image per request (its JSON parser: src/hosted/token-api.js).
450
+ v1.post(
451
+ '/boards/:bid/import/images',
452
+ (req, res, next) => {
453
+ if (!req.is('application/json')) return res.status(415).json({ error: { code: 'json_required', message: 'Send JSON.' } })
454
+ next()
455
+ },
456
+ handle(async (req, res) => {
457
+ const ctx = boardContext(req.t, req.project, req.params.bid)
458
+ res.status(201).json(
459
+ await importImage({
460
+ db,
461
+ t: req.t,
462
+ projectId: req.project.id,
463
+ board: ctx.board,
464
+ token: req.token,
465
+ tokenHash: req.authTokenHash,
466
+ input: req.body,
467
+ cleaner,
468
+ quotaBytes: quotaOf(req.t),
469
+ now: now(),
470
+ }),
471
+ )
472
+ }),
473
+ )
279
474
  v1.post(
280
475
  '/boards/:bid/import',
281
476
  (req, res, next) => {
@@ -22,6 +22,7 @@ import path from 'node:path'
22
22
  import express from 'express'
23
23
  import { ID_PATTERNS, newId, sha256 } from './crypto.js'
24
24
  import { transaction } from './db.js'
25
+ import { commentFileBytes } from './comment-files.js'
25
26
  import { deleteWorkspaceRows, planWorkspaceExport, writeWorkspaceExport } from './admin.js'
26
27
  import { createProjectStore } from './projects.js'
27
28
  import { ZIP_MAX_BYTES } from './zip.js'
@@ -140,7 +141,8 @@ export function listWorkspaces(db, { quotas = null } = {}) {
140
141
  .map((row) => row.handle),
141
142
  projects: t.get('SELECT COUNT(*) AS n FROM projects WHERE workspace_id = :ws').n,
142
143
  boards: boards.n,
143
- bytes: boards.bytes,
144
+ // Pushed boards plus images on comments: what the storage quota counts.
145
+ bytes: boards.bytes + commentFileBytes(t),
144
146
  storageQuotaBytes: quota ? quota.storageBytes : null,
145
147
  lastPushAt: boards.last_push || null,
146
148
  shareLinksOn: Boolean(workspace.share_links),
@@ -98,12 +98,13 @@ export function createProjectStore(t, { db, dataDir, maxProjects = PROJECTS_MAX
98
98
  })
99
99
  }
100
100
 
101
- // Rows first (comments, boards, tokens and pushes go with them), then files.
101
+ // Rows first (comment images, comments, boards, tokens and pushes go with
102
+ // them), then files.
102
103
  function remove(projectId) {
103
104
  const project = get(projectId)
104
105
  if (!project) throw new ProjectError(404, 'unknown_project', 'No such project.')
105
106
  transaction(db, () => {
106
- for (const table of ['comments', 'boards', 'api_tokens', 'pushes']) {
107
+ for (const table of ['comment_files', 'comments', 'boards', 'api_tokens', 'pushes']) {
107
108
  t.run(`DELETE FROM ${table} WHERE workspace_id = :ws AND project_id = :id`, { id: project.id })
108
109
  }
109
110
  t.run('DELETE FROM projects WHERE workspace_id = :ws AND id = :id', { id: project.id })
@@ -25,6 +25,7 @@ import { ELEMENT_MAP_LIMITS, ElementMapError, parseElementMap } from '../capture
25
25
  import { CAPTURE_FILE_RE, VERSION_FILE_RE, VERSION_RE } from '../store.js'
26
26
  import { ID_PATTERNS, newId } from './crypto.js'
27
27
  import { transaction } from './db.js'
28
+ import { commentFileBytes } from './comment-files.js'
28
29
  import { openPushIds, staleOpenPushes, tenant } from './tenancy.js'
29
30
  import { mergeCheckEnvironments, validateCheckEnvironments } from '../checks/environments.js'
30
31
  import { projectDir } from './projects.js'
@@ -378,14 +379,15 @@ export function createPushStore(
378
379
  }
379
380
 
380
381
  // Bytes the workspace holds apart from `boardId` of `projectId` (which a
381
- // push replaces): committed boards plus every open push.
382
+ // push replaces): committed boards, every open push and the images on
383
+ // comments (src/hosted/comment-files.js; a push never replaces those).
382
384
  function workspaceBytesBesides(projectId, boardId) {
383
385
  const boards = t.get(
384
386
  'SELECT COALESCE(SUM(bytes), 0) AS n FROM boards WHERE workspace_id = :ws AND NOT (project_id = :project AND board_id = :board)',
385
387
  { project: projectId, board: boardId },
386
388
  ).n
387
389
  const open = t.get("SELECT COALESCE(SUM(bytes), 0) AS n FROM pushes WHERE workspace_id = :ws AND status = 'open'").n
388
- return boards + open
390
+ return boards + open + commentFileBytes(t)
389
391
  }
390
392
 
391
393
  // Cleans the file off the event loop (cleaner), then writes it to the
@@ -551,7 +553,8 @@ export function createPushStore(
551
553
  'SELECT COALESCE(SUM(bytes), 0) AS n FROM boards WHERE workspace_id = :ws AND NOT (project_id = :project AND board_id = :board)',
552
554
  { project: projectId, board: board.id },
553
555
  ).n
554
- if (others + bytes > workspaceQuota) throw new PushError(413, 'quota_exceeded', 'This workspace is over its storage quota.')
556
+ if (others + commentFileBytes(t) + bytes > workspaceQuota)
557
+ throw new PushError(413, 'quota_exceeded', 'This workspace is over its storage quota.')
555
558
  const definition = JSON.stringify(push.definition)
556
559
  if (existing) {
557
560
  t.run(
@@ -82,6 +82,24 @@ function pruneBoard(db, t, dataDir, projectId, boardId, keep, dryRun) {
82
82
  )
83
83
  .map((comment) => `${comment.page_id}\u0000${comment.viewport}\u0000${comment.capture_version}`),
84
84
  )
85
+ // A reply's drawing is on the capture that was current when it was
86
+ // posted: that version stays too, so its markup keeps rendering.
87
+ for (const comment of t.all(
88
+ `SELECT page_id, viewport, replies FROM comments
89
+ WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND replies LIKE '%"drawing"%'`,
90
+ { project: projectId, board: boardId },
91
+ )) {
92
+ let replies = []
93
+ try {
94
+ replies = JSON.parse(comment.replies)
95
+ } catch {
96
+ /* unreadable: nothing referenced */
97
+ }
98
+ for (const reply of Array.isArray(replies) ? replies : []) {
99
+ const version = reply?.drawing?.captureVersion
100
+ if (typeof version === 'string') referenced.add(`${comment.page_id}\u0000${comment.viewport}\u0000${version}`)
101
+ }
102
+ }
85
103
  const names = planBoardRetention(listVersions(config, boardId), referenced, keep)
86
104
  const dir = path.join(config.outputDir, boardId, 'versions')
87
105
  let bytes = 0
@@ -29,6 +29,7 @@ export const TENANT_TABLES = [
29
29
  'import_sessions',
30
30
  'import_batches',
31
31
  'comment_imports',
32
+ 'comment_files',
32
33
  ]
33
34
  export { ROLES, roleAtLeast } from './permissions.js'
34
35
 
@@ -6,6 +6,7 @@
6
6
  // mountTokenApi(deps) returns { v1, tokenAuth }; app.js mounts v1 (and MCP
7
7
  // with tokenAuth) in its place in the route order.
8
8
 
9
+ import fs from 'node:fs'
9
10
  import path from 'node:path'
10
11
  import express from 'express'
11
12
  import { feedbackContext, feedbackItem, fileKind } from '../feedback.js'
@@ -16,6 +17,8 @@ import { captureFiles, listVersions, readManifest, resolveFile, variantsWithFile
16
17
  import { agentPolicy, expandScopes, trustedMember } from './agent-policy.js'
17
18
  import { mountHostedApprovals } from './agent-approvals.js'
18
19
  import { sendPng } from './board-context.js'
20
+ import { markupName } from '../comment-media.js'
21
+ import { commentListsFile, createMarkupCache, readCommentFile, renderMarkupPng, sendImage } from './comment-files.js'
19
22
  import { REPLY_AUTHOR, cleanAuthor, rowToComment } from './comments.js'
20
23
  import { API_TOKEN_RE, sha256 } from './crypto.js'
21
24
  import { transaction } from './db.js'
@@ -104,8 +107,18 @@ export function mountTokenApi({
104
107
  req.ctx = boardContext(req.t, req.project, req.params.bid)
105
108
  await fn(req, res, req.ctx)
106
109
  })
110
+ // An imported comment's image comes in its own request: 5 MB as base64
111
+ // and its envelope. Batches of comments keep 2 MB.
112
+ v1.use('/boards/:bid/import/images', express.json({ limit: '7mb' }))
107
113
  v1.use('/boards/:bid/import', express.json({ limit: '2mb' }))
108
- mountImports(v1, { db, boardContext, handle, now })
114
+ mountImports(v1, {
115
+ db,
116
+ boardContext,
117
+ handle,
118
+ now,
119
+ cleaner,
120
+ quotaOf: (t) => workspaceQuota ?? quotas.of(t.workspaceId).storageBytes,
121
+ })
109
122
  // Guest comments stay away from agents unless asked for, on every route
110
123
  // and not only in lists: reading one, replying, statuses, resolving and
111
124
  // reopening need the same opt-in (?guests=1) as listing them.
@@ -152,6 +165,22 @@ export function mountTokenApi({
152
165
  return req.query.guests === '1' ? replies : replies.filter((reply) => !reply.guest)
153
166
  }
154
167
 
168
+ // A board's feedback context whose comment media are token API URLs (GET
169
+ // /boards/:bid/comment-files/:file below) instead of server paths.
170
+ // `captured` collects what each markup is rendered from (the route below).
171
+ const commentFileUrl = (ctx, name) => `${publicOrigin}/api/v1/boards/${ctx.board.id}/comment-files/${name}`
172
+ function contextFor(ctx, captured = null) {
173
+ return feedbackContext(ctx.config, ctx.board, {
174
+ media: {
175
+ imagePath: (image) => commentFileUrl(ctx, image.file),
176
+ markupPath: (input) => {
177
+ captured?.push(input)
178
+ return commentFileUrl(ctx, markupName(input.commentId, input.replyId))
179
+ },
180
+ },
181
+ })
182
+ }
183
+
155
184
  // Feedback items with download URLs instead of server paths.
156
185
  function remoteItem(req, ctx, context, comment) {
157
186
  const item = {
@@ -162,6 +191,17 @@ export function mountTokenApi({
162
191
  // The capture a comment was made on can be an archived version (versions/).
163
192
  const url = (file) =>
164
193
  file ? `${publicOrigin}/api/v1/boards/${ctx.board.id}/files/${fileKind(item, file)}/${path.basename(file)}` : null
194
+ // Files of a guest's comment or reply need the same opt-in as its text.
195
+ const optIn = (link, guest) => (link && guest ? `${link}?guests=1` : link || null)
196
+ const images = (list, guest) =>
197
+ (list || []).map((image) => ({
198
+ name: image.name,
199
+ url: optIn(image.path, guest),
200
+ mediaType: image.mediaType,
201
+ bytes: image.bytes,
202
+ width: image.width,
203
+ height: image.height,
204
+ }))
165
205
  const files = item.files
166
206
  item.files = {
167
207
  screenshot: url(files.screenshot),
@@ -170,13 +210,39 @@ export function mountTokenApi({
170
210
  latestScreenshot: url(files.latestScreenshot),
171
211
  latestLiveHtml: url(files.latestLiveHtml),
172
212
  latestVersion: files.latestVersion,
213
+ // What the reviewer attached, and the drawing rendered on its capture.
214
+ images: images(files.images, comment.authorGuest),
215
+ markup: optIn(files.markup, comment.authorGuest),
173
216
  }
217
+ item.replies = item.replies.map((reply) => ({
218
+ ...reply,
219
+ ...(reply.images ? { images: images(reply.images, comment.authorGuest || reply.guest) } : {}),
220
+ ...(reply.drawing ? { markup: optIn(reply.markup, comment.authorGuest || reply.guest) } : {}),
221
+ }))
174
222
  return item
175
223
  }
224
+
225
+ // The comment as this token may read it (its replies through
226
+ // tokenReplies), or null: the same rules as v1Comment, for its files.
227
+ function readableComment(req, ctx, number, replyId = null) {
228
+ const row = ctx.comments.get(number)
229
+ if (
230
+ req.token.scopes.includes('claim') &&
231
+ !policyFor(req.t).state(req.project.id, ctx.board.id, { ...row, status: 'open' }).dispatchable
232
+ )
233
+ return null
234
+ const comment = dispatchView(req.t, req.project.id, ctx.board.id, rowToComment(row))
235
+ if (comment.authorGuest && req.query.guests !== '1') return null
236
+ const replies = tokenReplies(req, ctx, comment.replies)
237
+ if (replyId && !replies.some((reply) => Number(reply.id) === Number(replyId))) return null
238
+ return { ...comment, replies }
239
+ }
240
+ const markups = createMarkupCache()
241
+ const MARKUP_FILE_RE = /^markup-([1-9][0-9]{0,15})(?:-r([1-9][0-9]{0,5}))?\.png$/
176
242
  // Guest comments (from share links) are untrusted: a token sees them only
177
243
  // when it asks (`guests=1`), so an agent never picks one up by default.
178
244
  function remoteReport(req, ctx, { status = 'open', pageId = null, mention = null, guests = false } = {}) {
179
- const context = feedbackContext(ctx.config, ctx.board)
245
+ const context = contextFor(ctx)
180
246
  let guestsHidden = 0
181
247
  const comments = ctx.comments
182
248
  .list({ status: status === 'all' ? null : status, mention })
@@ -347,12 +413,7 @@ export function mountTokenApi({
347
413
  const row = ctx.comments.get(req.params.cid)
348
414
  res.json({
349
415
  board: ctx.board.id,
350
- comment: remoteItem(
351
- req,
352
- ctx,
353
- feedbackContext(ctx.config, ctx.board),
354
- dispatchView(req.t, req.project.id, ctx.board.id, rowToComment(row)),
355
- ),
416
+ comment: remoteItem(req, ctx, contextFor(ctx), dispatchView(req.t, req.project.id, ctx.board.id, rowToComment(row))),
356
417
  })
357
418
  }),
358
419
  )
@@ -371,8 +432,48 @@ export function mountTokenApi({
371
432
  res.type('html').sendFile(file, { dotfiles: 'allow' })
372
433
  }),
373
434
  )
435
+ // A comment's image, or its drawing rendered on the capture it was drawn
436
+ // on (markup-<comment>[-r<reply>].png), for a token that may read that
437
+ // comment and reply. Images come from the database as their stored type;
438
+ // markups are rendered in the cleaner's pool and kept in a bounded memory
439
+ // cache (the same drawing and capture give the same bytes).
440
+ v1.get(
441
+ '/boards/:bid/comment-files/:file',
442
+ scope('read'),
443
+ v1Board(async (req, res, ctx) => {
444
+ const name = String(req.params.file)
445
+ const markup = MARKUP_FILE_RE.exec(name)
446
+ if (!markup) {
447
+ const row = readCommentFile(req.t, req.project.id, ctx.board.id, name)
448
+ const comment = row ? readableComment(req, ctx, row.comment_number, row.reply_id) : null
449
+ if (!comment || !commentListsFile(comment, row)) return fail(res, 404, 'not_found', 'No such file.')
450
+ return sendImage(res, row.data, row.media_type)
451
+ }
452
+ const number = Number(markup[1])
453
+ const replyId = markup[2] ? Number(markup[2]) : null
454
+ const comment = readableComment(req, ctx, number, replyId)
455
+ const captured = []
456
+ if (comment) feedbackItem(contextFor(ctx, captured), comment)
457
+ const input = captured.find((item) => Number(item.commentId) === number && (item.replyId || null) === replyId)
458
+ let stat = null
459
+ try {
460
+ stat = input?.screenshot ? fs.lstatSync(input.screenshot) : null
461
+ } catch {
462
+ stat = null
463
+ }
464
+ if (!stat?.isFile() || stat.size > 40 * 1024 * 1024) return fail(res, 404, 'not_found', 'No such file.')
465
+ const key = [req.t.workspaceId, req.project.id, ctx.board.id, name, input.screenshot, stat.size, stat.mtimeMs].join('\u0000')
466
+ let png = markups.get(key)
467
+ if (!png) {
468
+ png = await renderMarkupPng(cleaner, fs.readFileSync(input.screenshot), input.drawing)
469
+ if (!png) return fail(res, 404, 'not_found', 'No such file.')
470
+ markups.set(key, png)
471
+ }
472
+ sendImage(res, png, 'image/png')
473
+ }),
474
+ )
374
475
  const v1Json = express.json({ limit: '64kb' })
375
- const v1Item = (req, ctx, comment) => remoteItem(req, ctx, feedbackContext(ctx.config, ctx.board), comment)
476
+ const v1Item = (req, ctx, comment) => remoteItem(req, ctx, contextFor(ctx), comment)
376
477
  v1.post(
377
478
  '/boards/:bid/comments/:cid/replies',
378
479
  scope('reply'),
@@ -0,0 +1,11 @@
1
+ // An uploaded image's real type from its first bytes, whatever its name or
2
+ // declared type says. Shared by chat attachments (src/agent/threads.js) and
3
+ // comment images (src/comment-media.js). Never SVG: only these four.
4
+
5
+ export function sniffImage(buffer) {
6
+ if (buffer.length >= 8 && buffer.subarray(0, 8).equals(Buffer.from('89504e470d0a1a0a', 'hex'))) return 'image/png'
7
+ if (buffer.length >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) return 'image/jpeg'
8
+ if (buffer.length >= 12 && buffer.toString('latin1', 0, 4) === 'RIFF' && buffer.toString('latin1', 8, 12) === 'WEBP') return 'image/webp'
9
+ if (buffer.length >= 6 && /^GIF8[79]a$/.test(buffer.toString('latin1', 0, 6))) return 'image/gif'
10
+ return null
11
+ }