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,115 @@
1
+ // Quotas per workspace, set by the server operator. Defaults below; the
2
+ // operator overrides them for every workspace or for one in a JSON file
3
+ // (`--quotas FILE`, or <data>/quotas.json when it exists):
4
+ //
5
+ // {
6
+ // "default": { "storageGB": 5, "projects": 20, "boardsPerProject": 10, "members": 100,
7
+ // "shareLinksPerProject": 20, "pushesPerHour": 20 },
8
+ // "workspaces": { "ws_...": { "storageGB": 20 } }
9
+ // }
10
+ //
11
+ // The file is read again when it changes (checked at most every few
12
+ // seconds), so a change needs no restart. A broken file at start stops the
13
+ // server; a broken edit later keeps the last good one and logs why.
14
+ //
15
+ // The kill switch is not a quota: `shapewall hosted admin suspend` sets
16
+ // workspaces.suspended_at (src/hosted/operator.js).
17
+
18
+ import fs from 'node:fs'
19
+ import { ID_PATTERNS } from './crypto.js'
20
+
21
+ const GB = 1024 ** 3
22
+
23
+ export const QUOTA_DEFAULTS = Object.freeze({
24
+ storageGB: 5,
25
+ projects: 20,
26
+ boardsPerProject: 10,
27
+ members: 100,
28
+ shareLinksPerProject: 20,
29
+ pushesPerHour: 20,
30
+ })
31
+
32
+ // Upper bounds keep a typo ("50000" GB) from meaning "unlimited" by accident.
33
+ const BOUNDS = {
34
+ storageGB: [0.001, 10_000],
35
+ projects: [1, 10_000],
36
+ boardsPerProject: [1, 1000],
37
+ members: [1, 100_000],
38
+ shareLinksPerProject: [0, 1000],
39
+ pushesPerHour: [1, 10_000],
40
+ }
41
+
42
+ function cleanSet(raw, where) {
43
+ if (raw === undefined) return {}
44
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new Error(`${where}: an object of quota values`)
45
+ const out = {}
46
+ for (const [key, value] of Object.entries(raw)) {
47
+ if (!Object.hasOwn(BOUNDS, key)) throw new Error(`${where}: unknown quota "${key}" (known: ${Object.keys(BOUNDS).join(', ')})`)
48
+ const [min, max] = BOUNDS[key]
49
+ if (typeof value !== 'number' || !Number.isFinite(value) || value < min || value > max)
50
+ throw new Error(`${where}.${key}: a number from ${min} to ${max}`)
51
+ if (key !== 'storageGB' && !Number.isInteger(value)) throw new Error(`${where}.${key}: a whole number`)
52
+ out[key] = value
53
+ }
54
+ return out
55
+ }
56
+
57
+ export function parseQuotas(text) {
58
+ let raw
59
+ try {
60
+ raw = JSON.parse(text)
61
+ } catch (error) {
62
+ throw new Error('not valid JSON', { cause: error })
63
+ }
64
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new Error('the file holds one JSON object')
65
+ for (const key of Object.keys(raw)) if (!['default', 'workspaces'].includes(key)) throw new Error(`unknown key "${key}"`)
66
+ const workspaces = {}
67
+ if (raw.workspaces !== undefined) {
68
+ if (!raw.workspaces || typeof raw.workspaces !== 'object' || Array.isArray(raw.workspaces))
69
+ throw new Error('workspaces: an object keyed by workspace id')
70
+ for (const [id, set] of Object.entries(raw.workspaces)) {
71
+ if (!ID_PATTERNS.workspace.test(id)) throw new Error(`workspaces: "${id}" is not a workspace id`)
72
+ workspaces[id] = cleanSet(set, `workspaces.${id}`)
73
+ }
74
+ }
75
+ return { default: cleanSet(raw.default, 'default'), workspaces }
76
+ }
77
+
78
+ // createQuotas({ file }) -> { of(workspaceId), file }. Without a file every
79
+ // workspace gets QUOTA_DEFAULTS.
80
+ export function createQuotas({ file = null, log = () => {}, checkEveryMs = 5000, now = () => Date.now() } = {}) {
81
+ let parsed = { default: {}, workspaces: {} }
82
+ let mtime = null
83
+ let checkedAt = 0
84
+
85
+ function load(initial) {
86
+ let stat
87
+ try {
88
+ stat = fs.statSync(file)
89
+ } catch (error) {
90
+ if (error.code !== 'ENOENT') throw error
91
+ if (initial) throw new Error(`quotas: ${file} does not exist`, { cause: error })
92
+ return
93
+ }
94
+ if (mtime !== null && stat.mtimeMs === mtime) return
95
+ try {
96
+ parsed = parseQuotas(fs.readFileSync(file, 'utf8'))
97
+ if (!initial) log(`quotas: reloaded ${file}`)
98
+ } catch (error) {
99
+ if (initial) throw new Error(`quotas: ${file}: ${error.message}`, { cause: error })
100
+ log(`quotas: ${file}: ${error.message}; keeping the previous quotas`)
101
+ }
102
+ mtime = stat.mtimeMs
103
+ }
104
+ if (file) load(true)
105
+
106
+ function of(workspaceId) {
107
+ if (file && now() - checkedAt > checkEveryMs) {
108
+ checkedAt = now()
109
+ load(false)
110
+ }
111
+ const merged = { ...QUOTA_DEFAULTS, ...parsed.default, ...(parsed.workspaces[workspaceId] || {}) }
112
+ return { ...merged, storageBytes: Math.round(merged.storageGB * GB) }
113
+ }
114
+ return { of, file }
115
+ }
@@ -0,0 +1,96 @@
1
+ // Fixed-window rate limits in memory (one process, one host). Keys are
2
+ // bucket + client (IP, token id or handle). Old windows are swept as they go.
3
+
4
+ export function createRateLimiter({ now = () => Date.now() } = {}) {
5
+ const windows = new Map()
6
+ let sweptAt = 0
7
+ // Counts one hit. Returns { ok, retryAfter } (seconds).
8
+ function hit(bucket, key, { limit, windowMs }) {
9
+ const time = now()
10
+ if (time - sweptAt > 60000) {
11
+ sweptAt = time
12
+ for (const [id, entry] of windows) if (entry.resetAt <= time) windows.delete(id)
13
+ }
14
+ const id = `${bucket}\u0000${key}`
15
+ let entry = windows.get(id)
16
+ if (!entry || entry.resetAt <= time) {
17
+ entry = { count: 0, resetAt: time + windowMs }
18
+ windows.set(id, entry)
19
+ }
20
+ entry.count += 1
21
+ return entry.count <= limit ? { ok: true, retryAfter: 0 } : { ok: false, retryAfter: Math.ceil((entry.resetAt - time) / 1000) }
22
+ }
23
+ // Whether the next hit would pass, without counting one (for limits that
24
+ // count only failures).
25
+ function peek(bucket, key, { limit }) {
26
+ const time = now()
27
+ const entry = windows.get(`${bucket}\u0000${key}`)
28
+ if (!entry || entry.resetAt <= time || entry.count < limit) return { ok: true, retryAfter: 0 }
29
+ return { ok: false, retryAfter: Math.ceil((entry.resetAt - time) / 1000) }
30
+ }
31
+ // Takes back one hit (in the same window), e.g. for an attempt that
32
+ // turned out right on a limit that counts only failures.
33
+ function undo(bucket, key) {
34
+ const entry = windows.get(`${bucket}\u0000${key}`)
35
+ if (entry && entry.resetAt > now() && entry.count > 0) entry.count -= 1
36
+ }
37
+ // For limits that count only failures, with checks that take time (scrypt)
38
+ // or could run in parallel: every attempt counts up front, in all buckets
39
+ // ([bucket, key, limit] each), before anything is checked, and a right one
40
+ // gives its count back with `success()`. So N parallel guesses use N
41
+ // attempts, never one. Over a limit, nothing is counted and ok is false.
42
+ function attempt(keys) {
43
+ const results = keys.map(([bucket, key, limit]) => hit(bucket, key, limit))
44
+ const blocked = results.find((result) => !result.ok)
45
+ if (blocked) {
46
+ for (const [bucket, key] of keys) undo(bucket, key)
47
+ return { ok: false, retryAfter: Math.max(...results.map((result) => result.retryAfter)), success: () => {} }
48
+ }
49
+ return { ok: true, retryAfter: 0, success: () => keys.forEach(([bucket, key]) => undo(bucket, key)) }
50
+ }
51
+ return { hit, peek, undo, attempt, reset: () => windows.clear() }
52
+ }
53
+
54
+ // Default limits. Ceremonies are cheap for people (one or two per sign-in)
55
+ // and expensive to abuse; recovery codes get the tightest limit.
56
+ export const LIMITS = {
57
+ ceremony: { limit: 30, windowMs: 60_000 },
58
+ // Failed recovery codes only: per handle and IP, and per IP. A stranger
59
+ // cannot use up someone else's attempts from another address.
60
+ recoveryFail: { limit: 5, windowMs: 15 * 60_000 },
61
+ recoveryFailIp: { limit: 30, windowMs: 60 * 60_000 },
62
+ invite: { limit: 30, windowMs: 60_000 },
63
+ // The console and board APIs per signed-in account (every tab of theirs
64
+ // counts together: a board polls its view, comments and approvals). A
65
+ // board's images, live pages and element maps have their own, larger
66
+ // bucket: a big board loads hundreds of them at once.
67
+ session: { limit: 1200, windowMs: 60_000 },
68
+ sessionFiles: { limit: 6000, windowMs: 60_000 },
69
+ token: { limit: 1200, windowMs: 60_000 },
70
+ tokenBadAuth: { limit: 20, windowMs: 60_000 },
71
+ push: { limit: 20, windowMs: 60 * 60_000 },
72
+ // Invite emails per workspace (the link can always be copied instead).
73
+ inviteEmail: { limit: 20, windowMs: 60 * 60_000 },
74
+ inviteEmailUser: { limit: 50, windowMs: 24 * 60 * 60_000 },
75
+ inviteEmailServer: { limit: 500, windowMs: 24 * 60 * 60_000 },
76
+ // Share links: preview and open per IP; wrong passcodes per link and IP
77
+ // and per IP (only failures count); a guest's API calls per link; guest
78
+ // comments and replies per link and IP.
79
+ share: { limit: 30, windowMs: 60_000 },
80
+ shareFail: { limit: 5, windowMs: 15 * 60_000 },
81
+ shareFailIp: { limit: 30, windowMs: 60 * 60_000 },
82
+ // Wrong passcodes for one link from anywhere: a spread-out guesser stops
83
+ // too (the cost: the link's own guests wait out the hour).
84
+ shareFailLink: { limit: 50, windowMs: 60 * 60_000 },
85
+ guestApi: { limit: 600, windowMs: 60_000 },
86
+ guestComment: { limit: 30, windowMs: 60 * 60_000 },
87
+ // A public project's board API, per client address.
88
+ publicApi: { limit: 600, windowMs: 60_000 },
89
+ // `shapewall login`: starting a login and polling it per IP, and looking up
90
+ // a code in the console per account (a code is guessed one try at a time).
91
+ deviceStart: { limit: 10, windowMs: 60_000 },
92
+ devicePoll: { limit: 60, windowMs: 60_000 },
93
+ deviceLookup: { limit: 10, windowMs: 60_000 },
94
+ // Workspace exports per person (each is a zip of everything).
95
+ export: { limit: 5, windowMs: 60 * 60_000 },
96
+ }
@@ -0,0 +1,316 @@
1
+ // Moving a server to a new origin. Passkeys belong to the RP ID (the host
2
+ // name), so a move would lock everyone out. The flow:
3
+ //
4
+ // 1. The operator runs `shapewall hosted move-origin --to https://new.example`
5
+ // (records the move: from the server's current origin, for up to 30
6
+ // days), points the new host name at the same server and restarts it with
7
+ // `--origin https://new.example --previous-origin https://old.example`.
8
+ // 2. On the old origin, a signed-in person sees "This server is moving";
9
+ // "Move my sign-in" (a fresh passkey check) mints a one-time rebind token
10
+ // for their own account: 32 random bytes, stored as SHA-256, 24 hours,
11
+ // audited, in the URL fragment of https://new.example/rebind#<token>.
12
+ // There they make a new passkey for the new host name, which signs them
13
+ // in. The token works once, only for the account that made it, only for
14
+ // this move, and a newer one voids it. It was made in a fresh passkey
15
+ // session, so the new passkey has no 24-hour hold.
16
+ // 3. Someone who missed the window signs in on the new origin with their
17
+ // handle and a recovery code (codes are not bound to a host name) and adds
18
+ // a passkey (the normal 24-hour hold applies).
19
+ // 4. Best effort: the old origin serves /.well-known/webauthn listing the new
20
+ // origin (WebAuthn Related Origin Requests), so a browser that supports
21
+ // it can use an old passkey on the new origin ("Use a passkey from
22
+ // old.example" on the sign-in page). The rebind flow is the guarantee.
23
+ // 5. Admins never rebind anyone else (that would be a takeover path).
24
+ //
25
+ // During the window the old host serves only what the move needs: sign-in,
26
+ // re-auth, the console page with the move banner, the rebind start, the
27
+ // related-origins file and the token API (so CI tokens keep working); every
28
+ // other page redirects (308) to the new origin, keeping the path (and the
29
+ // fragment, which browsers carry over). After the window, reads redirect
30
+ // and auth/API writes answer server_moved. Clients update their configured
31
+ // origin instead of forwarding bearer credentials to a different origin.
32
+
33
+ import { newId, randomToken, sha256 } from './crypto.js'
34
+ import { transaction } from './db.js'
35
+ import { accounts, auditAccount } from './tenancy.js'
36
+ import { AuthError } from './auth.js'
37
+
38
+ export const MOVE_DAYS_MAX = 30
39
+ export const REBIND_TTL_MS = 24 * 3_600_000
40
+ const DAY = 86_400_000
41
+ const TOKEN_RE = /^[A-Za-z0-9_-]{40,60}$/
42
+
43
+ // ---- Server state (key/value, migration 5) ----
44
+
45
+ export function getState(db, key) {
46
+ const row = accounts(db).get('SELECT value FROM server_state WHERE key = :key', { key })
47
+ if (!row) return null
48
+ try {
49
+ return JSON.parse(row.value)
50
+ } catch {
51
+ return null
52
+ }
53
+ }
54
+ export function setState(db, key, value, now = new Date()) {
55
+ accounts(db).run(
56
+ 'INSERT INTO server_state (key, value, updated_at) VALUES (:key, :value, :at) ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at',
57
+ { key, value: JSON.stringify(value), at: now.toISOString() },
58
+ )
59
+ }
60
+
61
+ // An origin people can sign in on: https, or http on localhost (or a
62
+ // *.localhost name) for trying it out. No path, no query.
63
+ export function cleanOrigin(value, what = 'origin') {
64
+ let url
65
+ try {
66
+ url = new URL(String(value || ''))
67
+ } catch {
68
+ throw new Error(`${what}: not a URL`)
69
+ }
70
+ const local = url.hostname === 'localhost' || url.hostname.endsWith('.localhost')
71
+ if (!(url.protocol === 'https:' || (url.protocol === 'http:' && local)))
72
+ throw new Error(`${what}: https (or http on localhost), got ${url.protocol}//${url.host}`)
73
+ if ((url.pathname !== '/' && url.pathname !== '') || url.search || url.hash) throw new Error(`${what}: an origin without a path`)
74
+ return url.origin
75
+ }
76
+
77
+ export function readMove(db) {
78
+ const move = getState(db, 'move')
79
+ return move && typeof move.from === 'string' && typeof move.to === 'string' ? move : null
80
+ }
81
+
82
+ // Records a move from the server's current origin to `to`.
83
+ export function recordMove(db, { from, to, days = MOVE_DAYS_MAX, now = new Date() }) {
84
+ const source = cleanOrigin(from, 'current origin')
85
+ const target = cleanOrigin(to, '--to')
86
+ if (new URL(source).host === new URL(target).host) throw new Error('--to is the origin this server already has')
87
+ if (new URL(source).protocol !== new URL(target).protocol) throw new Error('the old and new origin need the same scheme (both https)')
88
+ if (!Number.isInteger(days) || days < 1 || days > MOVE_DAYS_MAX) throw new Error(`--days: 1 to ${MOVE_DAYS_MAX}`)
89
+ const move = { from: source, to: target, startedAt: now.toISOString(), endsAt: new Date(now.getTime() + days * DAY).toISOString() }
90
+ setState(db, 'move', move, now)
91
+ return move
92
+ }
93
+
94
+ // Called when the app is made: remembers the origin, labels passkeys made
95
+ // before migration 5 with the host they were made for, and works out the
96
+ // previous origin's mode ('move' while the window is open, then 'redirect').
97
+ export function prepareOrigins({ db, origin, rpId, previousOrigin = null, now = () => new Date(), log = () => {} }) {
98
+ const stored = getState(db, 'origin')
99
+ if (!stored) {
100
+ // Before migration 5 a server never changed origin, so every existing
101
+ // passkey belongs to the origin it runs on now (or, mid-move, the old one).
102
+ const rp = previousOrigin ? new URL(previousOrigin).hostname : rpId
103
+ accounts(db).run('UPDATE passkeys SET rp_id = :rp WHERE rp_id IS NULL', { rp })
104
+ } else if (stored.origin !== origin && stored.origin !== previousOrigin) {
105
+ log(
106
+ `Warning: this server ran at ${stored.origin} and now runs at ${origin} without a move. Passkeys made for ${new URL(stored.origin).hostname} do not work here; people sign in with a recovery code. See "shapewall hosted move-origin".`,
107
+ )
108
+ }
109
+ setState(db, 'origin', { origin }, now())
110
+ if (!previousOrigin) {
111
+ const move = readMove(db)
112
+ if (move && move.to === origin && move.endsAt > now().toISOString())
113
+ log(`A move from ${move.from} is open until ${move.endsAt}: start with --previous-origin ${move.from} to serve it.`)
114
+ return null
115
+ }
116
+ const previous = cleanOrigin(previousOrigin, '--previous-origin')
117
+ if (new URL(previous).host === new URL(origin).host) throw new Error('--previous-origin is the same host as --origin')
118
+ if (new URL(previous).protocol !== new URL(origin).protocol) throw new Error('--previous-origin needs the same scheme as --origin')
119
+ let move = readMove(db)
120
+ if (!move || move.from !== previous || move.to !== origin) {
121
+ move = recordMove(db, { from: previous, to: origin, now: now() })
122
+ log(`Recorded the move from ${previous} to ${origin} (open until ${move.endsAt}).`)
123
+ }
124
+ const open = move.endsAt > now().toISOString()
125
+ if (!open) log(`The move window from ${previous} ended ${move.endsAt}: the old origin now only redirects to ${origin}.`)
126
+ return { origin: previous, host: new URL(previous).host, rpId: new URL(previous).hostname, move, mode: open ? 'move' : 'redirect' }
127
+ }
128
+
129
+ // ---- Requests on the old host ----
130
+
131
+ // What the old host still serves during the window (the rest redirects).
132
+ const OLD_HOST_PATHS = [
133
+ /^\/\.well-known\/webauthn$/,
134
+ /^\/auth\/(me|logout|login\/options|login\/verify|reauth\/options|reauth\/verify|rebind\/start)$/,
135
+ /^\/api\/v1(\/.*)?$/,
136
+ /^\/api\/health$/,
137
+ // The moving console still loads its public legal footer locally. A
138
+ // redirect would violate connect-src 'self' and break that request.
139
+ /^\/api\/legal$/,
140
+ /^\/_console(\/.*)?$/,
141
+ /^\/(signin|account|moving)?$/,
142
+ ]
143
+
144
+ export function previousHostGate(sites) {
145
+ return (req, res, next) => {
146
+ req.site = sites.siteOf(req)
147
+ if (req.site !== sites.previous) return next()
148
+ const previous = sites.previous
149
+ const moveActive = previous.mode === 'move' && previous.move.endsAt > sites.nowIso()
150
+ if (moveActive && OLD_HOST_PATHS.some((re) => re.test(req.path))) return next()
151
+ if (!['GET', 'HEAD'].includes(req.method) && /^\/(auth|api)\//.test(req.path)) {
152
+ return res.status(409).json({
153
+ error: { code: 'server_moved', message: `This server moved to ${sites.primary.origin}. Sign in there.` },
154
+ })
155
+ }
156
+ res.setHeader('Cache-Control', 'no-store')
157
+ res.redirect(308, `${sites.primary.origin}${req.originalUrl}`)
158
+ }
159
+ }
160
+
161
+ // ---- Routes ----
162
+
163
+ // The old origin's related-origins file and the rebind routes on both hosts.
164
+ // deps: the app's helpers (see app.js).
165
+ export function mountRebind(app, authRouter, deps) {
166
+ const {
167
+ db,
168
+ sites,
169
+ iso,
170
+ handle,
171
+ limit,
172
+ needSession,
173
+ needFresh,
174
+ clientIp,
175
+ setCookie,
176
+ ceremonyCookie,
177
+ CEREMONY_COOKIE,
178
+ startSession,
179
+ publicUser,
180
+ } = deps
181
+ const q = accounts(db)
182
+ const onOld = (req) => sites.previous && req.site === sites.previous
183
+ const moveOpen = () => Boolean(sites.previous && sites.previous.mode === 'move' && sites.previous.move.endsAt > iso())
184
+
185
+ // WebAuthn Related Origin Requests: the RP ID's own origin lists the
186
+ // origins that may use its passkeys.
187
+ app.get('/.well-known/webauthn', (req, res, next) => {
188
+ if (!onOld(req) || !moveOpen()) return next()
189
+ res.setHeader('Cache-Control', 'public, max-age=300')
190
+ res.type('application/json').send(JSON.stringify({ origins: [sites.primary.origin] }))
191
+ })
192
+
193
+ // On the old host: a token for my own account, for the new host.
194
+ authRouter.post(
195
+ '/rebind/start',
196
+ limit('ceremony'),
197
+ (req, res, next) =>
198
+ onOld(req) && moveOpen() ? next() : res.status(404).json({ error: { code: 'not_found', message: 'No such route.' } }),
199
+ needSession,
200
+ needFresh,
201
+ handle(async (req, res) => {
202
+ const token = randomToken(32)
203
+ const at = iso()
204
+ const expires = new Date(Math.min(Date.parse(at) + REBIND_TTL_MS, Date.parse(sites.previous.move.endsAt))).toISOString()
205
+ transaction(db, () => {
206
+ // One live token per account: a newer one voids the older ones.
207
+ q.run('DELETE FROM rebind_tokens WHERE user_id = :user AND used_at IS NULL', { user: req.user.id })
208
+ q.run(
209
+ `INSERT INTO rebind_tokens (token_hash, user_id, from_origin, to_origin, fresh, ip, created_at, expires_at)
210
+ VALUES (:hash, :user, :from, :to, 1, :ip, :at, :expires)`,
211
+ { hash: sha256(token), user: req.user.id, from: sites.previous.origin, to: sites.primary.origin, ip: clientIp(req), at, expires },
212
+ )
213
+ auditAccount(db, {
214
+ userId: req.user.id,
215
+ action: 'rebind_started',
216
+ ip: clientIp(req),
217
+ at,
218
+ detail: { from: sites.previous.origin, to: sites.primary.origin, id: newId('rb') },
219
+ })
220
+ })
221
+ res.json({ url: `${sites.primary.origin}/rebind#${token}`, expiresAt: expires })
222
+ }),
223
+ )
224
+
225
+ // On the new host: what a token is for, its passkey ceremony, and the finish.
226
+ const usableToken = (token) => {
227
+ if (!moveOpen())
228
+ throw new AuthError(404, 'invalid_rebind', 'This move link is used, expired, or the move is over. Sign in with a recovery code.')
229
+ const row =
230
+ typeof token === 'string' && TOKEN_RE.test(token)
231
+ ? q.get('SELECT * FROM rebind_tokens WHERE token_hash = :hash', { hash: sha256(token) })
232
+ : null
233
+ if (
234
+ !row ||
235
+ row.used_at ||
236
+ row.expires_at <= iso() ||
237
+ row.to_origin !== sites.primary.origin ||
238
+ row.from_origin !== sites.previous.origin
239
+ )
240
+ throw new AuthError(404, 'invalid_rebind', 'This move link is used, expired, or the move is over. Sign in with a recovery code.')
241
+ const user = q.get('SELECT * FROM users WHERE id = :id', { id: row.user_id })
242
+ if (!user) throw new AuthError(404, 'invalid_rebind', 'This move link is used, expired, or the move is over.')
243
+ return { row, user }
244
+ }
245
+ const newHostOnly = (req, res, next) =>
246
+ sites.previous && !onOld(req) ? next() : res.status(404).json({ error: { code: 'not_found', message: 'No such route.' } })
247
+
248
+ authRouter.post(
249
+ '/rebind/preview',
250
+ limit('invite'),
251
+ newHostOnly,
252
+ handle(async (req, res) => {
253
+ const { row, user } = usableToken(req.body?.token)
254
+ res.json({ user: { handle: user.handle, displayName: user.display_name }, from: row.from_origin, expiresAt: row.expires_at })
255
+ }),
256
+ )
257
+ authRouter.post(
258
+ '/rebind/options',
259
+ limit('ceremony'),
260
+ newHostOnly,
261
+ handle(async (req, res) => {
262
+ const { row, user } = usableToken(req.body?.token)
263
+ const { options, ceremony } = await sites.primary.auth.registrationOptions({ userId: user.id, data: { rebind: row.token_hash } })
264
+ setCookie(res, CEREMONY_COOKIE, ceremony, 300)
265
+ res.json({ options })
266
+ }),
267
+ )
268
+ authRouter.post(
269
+ '/rebind/verify',
270
+ limit('ceremony'),
271
+ newHostOnly,
272
+ handle(async (req, res) => {
273
+ if (!moveOpen()) throw new AuthError(404, 'invalid_rebind', 'The move is over. Sign in with a recovery code.')
274
+ let fresh = false
275
+ const nickname = typeof req.body?.nickname === 'string' && req.body.nickname.trim() ? req.body.nickname : 'Passkey (after the move)'
276
+ const added = await sites.primary.auth.finishAddPasskey(ceremonyCookie(req), req.body?.response, null, nickname, {
277
+ check: (ceremony) => {
278
+ const hash = ceremony.data?.rebind
279
+ if (typeof hash !== 'string') throw new AuthError(400, 'no_ceremony', 'Start again: the sign-in request expired.')
280
+ const token = q.get('SELECT fresh FROM rebind_tokens WHERE token_hash = :hash AND user_id = :user', {
281
+ hash,
282
+ user: ceremony.user_id,
283
+ })
284
+ // Single use, bound to the account, inside the passkey's transaction.
285
+ const used = q.run(
286
+ 'UPDATE rebind_tokens SET used_at = :at WHERE token_hash = :hash AND user_id = :user AND used_at IS NULL AND expires_at > :at AND to_origin = :to AND from_origin = :from',
287
+ { at: iso(), hash, user: ceremony.user_id, to: sites.primary.origin, from: sites.previous.origin },
288
+ )
289
+ if (used.changes !== 1 || !token)
290
+ throw new AuthError(403, 'invalid_rebind', 'This move link was already used or expired. Sign in with a recovery code.')
291
+ fresh = Boolean(token.fresh)
292
+ return { viaRecovery: !fresh }
293
+ },
294
+ })
295
+ const ip = clientIp(req)
296
+ auditAccount(db, {
297
+ userId: added.userId,
298
+ action: 'passkey_added',
299
+ ip,
300
+ at: iso(),
301
+ detail: { via: 'rebind', viaRecovery: !fresh, from: sites.previous.origin },
302
+ })
303
+ auditAccount(db, { userId: added.userId, action: 'signed_in', ip, at: iso(), detail: { via: 'rebind', from: sites.previous.origin } })
304
+ startSession(req, res, added.userId, { verified: fresh })
305
+ res.status(201).json({ user: publicUser(q.get('SELECT * FROM users WHERE id = :id', { id: added.userId })) })
306
+ }),
307
+ )
308
+ }
309
+
310
+ // What /auth/me says about a move, for the console's banners.
311
+ export function moveInfo(sites, req) {
312
+ if (!sites.previous || sites.previous.mode !== 'move' || sites.previous.move.endsAt <= sites.nowIso()) return {}
313
+ const move = sites.previous.move
314
+ if (req.site === sites.previous) return { moving: { to: sites.primary.origin, endsAt: move.endsAt } }
315
+ return { movedFrom: { origin: sites.previous.origin, rpId: sites.previous.rpId, endsAt: move.endsAt } }
316
+ }
@@ -0,0 +1,128 @@
1
+ // Retention: older capture versions on pushed boards are pruned, so a board
2
+ // that is pushed every day does not grow forever.
3
+ //
4
+ // Per page and viewport, a board keeps:
5
+ // - the current capture (captures/, never touched here);
6
+ // - the newest `keep` older versions (versions/), 2 by default, or the
7
+ // workspace's own setting (`shapewall hosted admin retention <ws> --keep N`);
8
+ // - every older version a comment was made on (comments.capture_version),
9
+ // open or resolved, so before and after keeps working for it.
10
+ // Everything else in versions/ goes, and the board's byte count (the storage
11
+ // quota) goes down with it.
12
+ //
13
+ // The server runs it at start and every 6 hours in-process (--retention-hours,
14
+ // 0 turns it off); `shapewall hosted admin retention --run` runs it by hand.
15
+ // Each board is pruned inside one database write transaction: a push commit
16
+ // swaps board folders inside its own transaction, so the two never interleave,
17
+ // also when the CLI runs it from another process.
18
+ //
19
+ // A later push brings back the versions the pushing machine still keeps; the
20
+ // next run trims them again.
21
+
22
+ import fs from 'node:fs'
23
+ import path from 'node:path'
24
+ import { ID_RE } from '../config.js'
25
+ import { listVersions } from '../store.js'
26
+ import { transaction } from './db.js'
27
+ import { boardConfig } from './projects.js'
28
+ import { allWorkspaces, tenant } from './tenancy.js'
29
+
30
+ export const RETENTION_DEFAULT = 2
31
+ export const RETENTION_EVERY_HOURS = 6
32
+
33
+ // Which version files of one board to remove. `versions` is listVersions()
34
+ // output; `referenced` a Set of "page\0viewport\0version".
35
+ export function planBoardRetention(versions, referenced, keep) {
36
+ const remove = []
37
+ for (const [pageId, viewports] of Object.entries(versions)) {
38
+ for (const [viewport, byVersion] of Object.entries(viewports)) {
39
+ const newestFirst = [...byVersion].sort((a, b) => b.version.localeCompare(a.version))
40
+ newestFirst.forEach((item, index) => {
41
+ if (index < keep) return
42
+ if (referenced.has(`${pageId}\u0000${viewport}\u0000${item.version}`)) return
43
+ for (const name of [item.file, item.thumb, item.live]) if (name) remove.push(name)
44
+ })
45
+ }
46
+ }
47
+ return remove
48
+ }
49
+
50
+ export function runRetention({ db, dataDir, keepDefault = RETENTION_DEFAULT, dryRun = false, workspaceId = null }) {
51
+ const result = { workspaces: 0, boards: 0, files: 0, bytes: 0, dryRun }
52
+ for (const workspace of allWorkspaces(db)) {
53
+ if (workspaceId && workspace.id !== workspaceId) continue
54
+ result.workspaces += 1
55
+ const keep = workspace.retention_versions ?? keepDefault
56
+ const t = tenant(db, workspace.id)
57
+ for (const row of t.all('SELECT project_id, board_id FROM boards WHERE workspace_id = :ws ORDER BY project_id, board_id')) {
58
+ if (!ID_RE.test(row.board_id)) continue
59
+ const removed = pruneBoard(db, t, dataDir, row.project_id, row.board_id, keep, dryRun)
60
+ if (removed.files) result.boards += 1
61
+ result.files += removed.files
62
+ result.bytes += removed.bytes
63
+ }
64
+ }
65
+ return result
66
+ }
67
+
68
+ function pruneBoard(db, t, dataDir, projectId, boardId, keep, dryRun) {
69
+ return transaction(db, () => {
70
+ const row = t.get('SELECT * FROM boards WHERE workspace_id = :ws AND project_id = :project AND board_id = :board', {
71
+ project: projectId,
72
+ board: boardId,
73
+ })
74
+ if (!row) return { files: 0, bytes: 0 }
75
+ const config = boardConfig(dataDir, t.workspaceId, projectId, row)
76
+ const referenced = new Set(
77
+ t
78
+ .all(
79
+ `SELECT DISTINCT page_id, viewport, capture_version FROM comments
80
+ WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND capture_version IS NOT NULL`,
81
+ { project: projectId, board: boardId },
82
+ )
83
+ .map((comment) => `${comment.page_id}\u0000${comment.viewport}\u0000${comment.capture_version}`),
84
+ )
85
+ const names = planBoardRetention(listVersions(config, boardId), referenced, keep)
86
+ const dir = path.join(config.outputDir, boardId, 'versions')
87
+ let bytes = 0
88
+ let files = 0
89
+ for (const name of names) {
90
+ const file = path.join(dir, name)
91
+ if (path.dirname(file) !== dir) continue
92
+ let size
93
+ try {
94
+ size = fs.statSync(file).size
95
+ } catch {
96
+ continue
97
+ }
98
+ if (!dryRun) fs.rmSync(file, { force: true })
99
+ bytes += size
100
+ files += 1
101
+ }
102
+ if (!dryRun && bytes)
103
+ t.run('UPDATE boards SET bytes = MAX(0, bytes - :bytes) WHERE workspace_id = :ws AND project_id = :project AND board_id = :board', {
104
+ bytes,
105
+ project: projectId,
106
+ board: boardId,
107
+ })
108
+ return { files, bytes }
109
+ })
110
+ }
111
+
112
+ // The in-process job: at start, then every `hours`. Returns stop().
113
+ export function startRetention({ db, dataDir, hours = RETENTION_EVERY_HOURS, keepDefault = RETENTION_DEFAULT, log = () => {} }) {
114
+ if (!hours) return () => {}
115
+ const run = () => {
116
+ try {
117
+ const result = runRetention({ db, dataDir, keepDefault })
118
+ if (result.files)
119
+ log(`retention: removed ${result.files} old version files (${(result.bytes / 1048576).toFixed(1)} MB) from ${result.boards} boards`)
120
+ } catch (error) {
121
+ log(`retention: ${error.message}`)
122
+ }
123
+ }
124
+ run()
125
+ const timer = setInterval(run, hours * 3_600_000)
126
+ timer.unref()
127
+ return () => clearInterval(timer)
128
+ }