rvlib-shipkit 2.31.0 → 2.33.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 (408) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/package.json +1 -1
  3. package/src/advisors/acts.ts +16 -15
  4. package/src/advisors/advice.ts +56 -102
  5. package/src/advisors/advisor-view.ts +122 -76
  6. package/src/advisors/app-graph.ts +1 -1
  7. package/src/advisors/app.ts +3 -11
  8. package/src/advisors/cf-api.ts +1 -1
  9. package/src/advisors/dir-bucket.ts +4 -3
  10. package/src/advisors/edge/api.ts +20 -221
  11. package/src/advisors/edge/bucket.ts +2 -2
  12. package/src/advisors/edge/handler.ts +13 -155
  13. package/src/advisors/edge/here.ts +5 -5
  14. package/src/advisors/edge/pages.ts +12 -9
  15. package/src/advisors/edge/shell.ts +6 -5
  16. package/src/advisors/edge/sign-in.ts +101 -36
  17. package/src/advisors/edge/status.ts +19 -6
  18. package/src/advisors/edge/types.ts +36 -60
  19. package/src/advisors/expose.ts +29 -29
  20. package/src/advisors/here.ts +12 -12
  21. package/src/advisors/invite-send.ts +79 -35
  22. package/src/advisors/invite.ts +31 -8
  23. package/src/advisors/job.ts +24 -16
  24. package/src/advisors/login.ts +40 -15
  25. package/src/advisors/owner-store.ts +46 -22
  26. package/src/advisors/photos.ts +3 -3
  27. package/src/advisors/project.ts +27 -25
  28. package/src/advisors/reach.ts +89 -0
  29. package/src/advisors/rights.ts +1 -1
  30. package/src/advisors/routes.ts +55 -74
  31. package/src/advisors/scan.ts +0 -4
  32. package/src/advisors/setup-run.ts +14 -14
  33. package/src/advisors/share-trace.ts +3 -3
  34. package/src/advisors/{door-probe.ts → sign-in-probe.ts} +7 -6
  35. package/src/advisors/speaks.ts +29 -0
  36. package/src/advisors/sync.ts +97 -183
  37. package/src/advisors/tunnel.ts +6 -5
  38. package/src/advisors/users-listen.ts +92 -0
  39. package/src/advisors/users-router.ts +261 -87
  40. package/src/advisors/users-store.ts +81 -0
  41. package/src/agent-time/blocked-scan.ts +181 -0
  42. package/src/agent-time/registry.ts +12 -5
  43. package/src/agent-time/store.ts +9 -109
  44. package/src/asks/ask-bar-fold.ts +2 -1
  45. package/src/asks/ask-close.ts +2 -1
  46. package/src/asks/ask-doc.ts +2 -1
  47. package/src/asks/ask-migrate.ts +3 -2
  48. package/src/asks/ask-steps-fold.ts +2 -1
  49. package/src/asks/ask-store.ts +4 -3
  50. package/src/check.ts +3 -43
  51. package/src/checks/SK001-package-json-shape.check.ts +4 -55
  52. package/src/checks/SK004-gitignore-policy.check.ts +24 -69
  53. package/src/checks/SK013-one-way-in-policy.check.ts +3 -7
  54. package/src/checks/SK017-vscode-hub-task.check.ts +4 -4
  55. package/src/checks/SK019-tests-run-in-units.check.ts +3 -14
  56. package/src/checks/SK020-self-spawning-check.check.ts +5 -7
  57. package/src/checks/SK035-public-clean.check.ts +1 -1
  58. package/src/checks/SK039-vscode-watch-generated.check.ts +1 -1
  59. package/src/checks/SK042-post-kinds.check.ts +5 -14
  60. package/src/checks/SK054-agent-allow-list.check.ts +5 -74
  61. package/src/checks/SK055-retired-config-keys.check.ts +45 -0
  62. package/src/cli/cmd_Advisors.ts +11 -45
  63. package/src/cli/cmd_Asks.ts +3 -10
  64. package/src/cli/cmd_Backup.ts +25 -127
  65. package/src/cli/cmd_BackupTargets.ts +1 -1
  66. package/src/cli/cmd_Capacity.ts +6 -2
  67. package/src/cli/cmd_Checks.ts +0 -14
  68. package/src/cli/cmd_Ci.ts +2 -1
  69. package/src/cli/cmd_Db.ts +3 -82
  70. package/src/cli/cmd_Home.ts +4 -32
  71. package/src/cli/cmd_Hq.ts +0 -17
  72. package/src/cli/cmd_Releases.ts +57 -4
  73. package/src/cli/cmd_Review.ts +333 -354
  74. package/src/cli/cmd_Shot.ts +26 -4
  75. package/src/cli/cmd_SocialDrafts.ts +6 -13
  76. package/src/cli/cmd_ThreadSyncNeeds.ts +6 -2
  77. package/src/cli/cmd_Time.ts +1 -1
  78. package/src/cli/cmd_Trailer.ts +1 -1
  79. package/src/cli/inbox-hook-run.ts +5 -23
  80. package/src/cli/registry-heads.ts +5 -55
  81. package/src/cli/registry.ts +1 -5
  82. package/src/cli/review-here.ts +1 -1
  83. package/src/cli.ts +15 -4
  84. package/src/collections/collection-types.ts +1 -1
  85. package/src/collections/review.ts +5 -4
  86. package/src/core/admission.ts +268 -0
  87. package/src/core/agent-allow-policy.ts +32 -2
  88. package/src/core/agent-env.ts +8 -0
  89. package/src/core/allow-rows.ts +74 -0
  90. package/src/core/check-file-names.ts +19 -106
  91. package/src/core/code-lines.ts +36 -21
  92. package/src/core/footprint.ts +17 -13
  93. package/src/core/identities-root.ts +1 -4
  94. package/src/core/layout.ts +1 -2
  95. package/src/core/machine-db.ts +33 -208
  96. package/src/core/machine-layout.ts +14 -127
  97. package/src/core/machine-read.ts +5 -2
  98. package/src/core/mask-source.ts +60 -0
  99. package/src/core/memory-watch.ts +17 -14
  100. package/src/core/motherhome.ts +8 -35
  101. package/src/core/one-way-in-policy.ts +0 -3
  102. package/src/core/peer-sk.ts +8 -0
  103. package/src/core/repo-walk.ts +0 -9
  104. package/src/core/retention.ts +1 -9
  105. package/src/core/retired-checks.ts +2 -0
  106. package/src/core/tmux-policy.ts +1 -1
  107. package/src/core/tree-watch.ts +2 -2
  108. package/src/core/when.ts +7 -45
  109. package/src/gate/admit-heavy.ts +150 -0
  110. package/src/gate/check-runner.ts +6 -0
  111. package/src/gate/discover-checks.ts +42 -32
  112. package/src/gate/gate-checks.ts +21 -131
  113. package/src/gate/head-gate.ts +1 -1
  114. package/src/gate/job-run.ts +2 -1
  115. package/src/gate/pre-commit-discovery.ts +22 -28
  116. package/src/gate/run-check.ts +3 -4
  117. package/src/gate/run-ci.ts +4 -0
  118. package/src/gate/run-fix.ts +5 -17
  119. package/src/gate/run-fixers.ts +6 -42
  120. package/src/home/app-sign.ts +2 -2
  121. package/src/home/backup-status.ts +52 -92
  122. package/src/home/backup-verdict.ts +2 -2
  123. package/src/home/daemon-jobs.ts +51 -95
  124. package/src/home/database-words.ts +1 -2
  125. package/src/home/databases.ts +9 -25
  126. package/src/home/home-launchd.ts +0 -6
  127. package/src/home/home-paths.ts +8 -4
  128. package/src/home/home-routes.ts +3 -10
  129. package/src/home/home-state.ts +2 -14
  130. package/src/home/inbox-shape.ts +14 -15
  131. package/src/home/inbox.ts +1 -1
  132. package/src/home/machine-repair.ts +102 -103
  133. package/src/home/resume.ts +1 -15
  134. package/src/home/run-daemon.ts +7 -12
  135. package/src/home/space-page.ts +3 -2
  136. package/src/home/supervise.ts +3 -7
  137. package/src/hooks/commit-msg-run.ts +61 -0
  138. package/src/hooks/commit-run.ts +16 -166
  139. package/src/hooks/gate-outcome.ts +108 -0
  140. package/src/hooks/git-hook.ts +9 -9
  141. package/src/hooks/hook-config.ts +8 -8
  142. package/src/hooks/leak-guard.ts +1 -1
  143. package/src/hooks/pre-commit-checks.ts +1 -1
  144. package/src/hooks/run-hooks.ts +2 -6
  145. package/src/hooks/shared-file.ts +3 -2
  146. package/src/hq/daemon-db.ts +8 -8
  147. package/src/hq/door.ts +3 -16
  148. package/src/hq/hq-db.ts +4 -3
  149. package/src/hq/hq-local.ts +1 -2
  150. package/src/hq/hq-store.ts +0 -15
  151. package/src/hq/item-direct.ts +8 -10
  152. package/src/hq/item-fill.ts +37 -0
  153. package/src/hq/kinds/item-gone.ts +7 -10
  154. package/src/hq/kinds/item-msg.ts +12 -18
  155. package/src/hq/kinds/item.ts +13 -19
  156. package/src/hq/kinds/report.ts +3 -4
  157. package/src/hq/owner-switch.ts +3 -3
  158. package/src/hq/report.ts +5 -9
  159. package/src/hq/switch.ts +2 -11
  160. package/src/hq/thread-sync.ts +5 -17
  161. package/src/hq/{shapes-words.ts → threads-words.ts} +2 -12
  162. package/src/http/owner-word.ts +1 -1
  163. package/src/identities/machine.ts +16 -0
  164. package/src/init-repo.ts +0 -1
  165. package/src/ledger/check-stats.ts +6 -5
  166. package/src/ledger/db.ts +2 -4
  167. package/src/ledger/record-kept.ts +117 -48
  168. package/src/ledger/run-log.ts +21 -2
  169. package/src/ledger/slow-tests.ts +96 -0
  170. package/src/ledger/store.ts +3 -86
  171. package/src/ledger/suite-log.ts +8 -2
  172. package/src/media-store/backup/facts.ts +3 -37
  173. package/src/media-store/backup/keys.ts +1 -8
  174. package/src/media-store/backup/manifest.ts +6 -67
  175. package/src/media-store/backup/restore.ts +4 -13
  176. package/src/media-store/backup/retention.ts +13 -1
  177. package/src/media-store/backup/run.ts +6 -267
  178. package/src/media-store/backup/snapshot.ts +2 -38
  179. package/src/media-store/backup/sources.ts +3 -113
  180. package/src/media-store/backup/target-send.ts +5 -29
  181. package/src/media-store/db.ts +8 -7
  182. package/src/media-store/forget-one.ts +1 -1
  183. package/src/media-store/forget.ts +1 -80
  184. package/src/orchestrator/guards.ts +0 -1
  185. package/src/orchestrator/presence.ts +2 -2
  186. package/src/orchestrator/routes.ts +1 -1
  187. package/src/orchestrator/started.ts +0 -9
  188. package/src/orchestrator/talk-key.ts +3 -4
  189. package/src/orchestrator/unheard.ts +1 -1
  190. package/src/plan/planner.ts +3 -2
  191. package/src/recordings/edit.ts +3 -3
  192. package/src/recordings/marks-draw.ts +17 -6
  193. package/src/recordings/marks.ts +120 -9
  194. package/src/reflections/reflections-queue.ts +3 -10
  195. package/src/release/notes.ts +1 -1
  196. package/src/release/public-clean.ts +66 -6
  197. package/src/release/run-bump.ts +11 -0
  198. package/src/release/run-release.ts +4 -0
  199. package/src/releases/from-thread.ts +3 -3
  200. package/src/releases/model.ts +186 -36
  201. package/src/releases/store.ts +13 -3
  202. package/src/releases/validate.ts +83 -0
  203. package/src/repo-config.ts +36 -64
  204. package/src/review/answer-kinds.ts +63 -4
  205. package/src/review/blocked-guard.ts +11 -0
  206. package/src/review/blocked-object.ts +298 -0
  207. package/src/review/cells.ts +144 -230
  208. package/src/review/close.ts +39 -1
  209. package/src/review/doc-save.ts +3 -2
  210. package/src/review/draft-item.ts +1 -1
  211. package/src/review/events.ts +3 -7
  212. package/src/review/expect.ts +23 -39
  213. package/src/review/format.ts +79 -46
  214. package/src/review/glyphs.ts +1 -0
  215. package/src/review/hand-over.ts +3 -3
  216. package/src/review/heard.ts +1 -1
  217. package/src/review/holds-sync.ts +2 -1
  218. package/src/review/inbox-hook.ts +2 -2
  219. package/src/review/inbox-read.ts +39 -33
  220. package/src/review/instruction-guards.ts +12 -0
  221. package/src/review/instructions.ts +0 -1
  222. package/src/review/item-db.ts +13 -43
  223. package/src/review/item-hq.ts +2 -2
  224. package/src/review/item-rows.ts +3 -14
  225. package/src/review/kind-catalog.ts +3 -1
  226. package/src/review/kinds.ts +18 -8
  227. package/src/review/live-view.ts +3 -0
  228. package/src/review/media-kind.ts +1 -1
  229. package/src/review/mentions.ts +1 -2
  230. package/src/review/model.ts +158 -114
  231. package/src/review/move.ts +149 -0
  232. package/src/review/need-threads.ts +3 -2
  233. package/src/review/object-card.ts +11 -0
  234. package/src/review/object-read.ts +6 -1
  235. package/src/review/one-block.ts +1 -1
  236. package/src/review/owner-read.ts +1 -1
  237. package/src/review/phone-push.ts +10 -9
  238. package/src/review/previews.ts +32 -4
  239. package/src/review/push.ts +2 -2
  240. package/src/review/recap.ts +129 -0
  241. package/src/review/referring.ts +6 -3
  242. package/src/review/reflection-object.ts +4 -3
  243. package/src/review/routes.ts +51 -47
  244. package/src/review/secret-route.ts +3 -2
  245. package/src/review/settle.ts +0 -6
  246. package/src/review/snooze.ts +5 -3
  247. package/src/review/speaker.ts +68 -0
  248. package/src/review/start-title.ts +19 -0
  249. package/src/review/store-parties.ts +28 -0
  250. package/src/review/store.ts +94 -59
  251. package/src/review/{wakes.ts → thread-events.ts} +92 -7
  252. package/src/review/thread-md.ts +35 -14
  253. package/src/review/thread-state.ts +804 -0
  254. package/src/review/threads.ts +29 -347
  255. package/src/review/traces.ts +104 -7
  256. package/src/review/trailer-item.ts +1 -1
  257. package/src/review/unit.ts +7 -9
  258. package/src/review/verdict-items.ts +12 -10
  259. package/src/review/view-layout.ts +25 -1
  260. package/src/review/watch-feed.ts +1 -2
  261. package/src/review/working-guards.ts +2 -3
  262. package/src/rules/untrack-generated.ts +1 -15
  263. package/src/secrets/copy.ts +4 -4
  264. package/src/secrets/lapse.ts +2 -1
  265. package/src/serve/hub-app/advisor-app.tsx +4 -4
  266. package/src/serve/hub-app/advisor-face.tsx +9 -3
  267. package/src/serve/hub-app/advisor-host.ts +27 -27
  268. package/src/serve/hub-app/advisors-model.ts +17 -8
  269. package/src/serve/hub-app/advisors-rights.tsx +1 -1
  270. package/src/serve/hub-app/advisors-st.ts +15 -14
  271. package/src/serve/hub-app/advisors-view.tsx +13 -11
  272. package/src/serve/hub-app/app-bridge.ts +23 -3
  273. package/src/serve/hub-app/asks-page.tsx +1 -1
  274. package/src/serve/hub-app/auto-text.tsx +71 -16
  275. package/src/serve/hub-app/check-view.tsx +0 -8
  276. package/src/serve/hub-app/controls.css +14 -3
  277. package/src/serve/hub-app/database-view.tsx +1 -2
  278. package/src/serve/hub-app/dock-st.ts +1 -1
  279. package/src/serve/hub-app/git-view.tsx +3 -1
  280. package/src/serve/hub-app/hub.css +12 -5
  281. package/src/serve/hub-app/icons.tsx +1 -1
  282. package/src/serve/hub-app/inbox-view.tsx +1 -2
  283. package/src/serve/hub-app/live-sources.ts +2 -2
  284. package/src/serve/hub-app/main-panel.tsx +2 -1
  285. package/src/serve/hub-app/panel-repo.ts +0 -9
  286. package/src/serve/hub-app/pic.tsx +29 -12
  287. package/src/serve/hub-app/polled.ts +16 -2
  288. package/src/serve/hub-app/recordings-st.ts +23 -2
  289. package/src/serve/hub-app/recordings-view.tsx +68 -10
  290. package/src/serve/hub-app/ref-pill.ts +1 -1
  291. package/src/serve/hub-app/repo-st.ts +1 -2
  292. package/src/serve/hub-app/review/advisor-add.tsx +1 -1
  293. package/src/serve/hub-app/review/advisor-invite.tsx +1 -1
  294. package/src/serve/hub-app/review/draft-body.tsx +2 -2
  295. package/src/serve/hub-app/review/heard-line.tsx +1 -1
  296. package/src/serve/hub-app/review/instructions-view.tsx +1 -8
  297. package/src/serve/hub-app/review/kind-states.tsx +14 -9
  298. package/src/serve/hub-app/review/kit.tsx +4 -11
  299. package/src/serve/hub-app/review/lane-bar.tsx +3 -3
  300. package/src/serve/hub-app/review/lightbox.tsx +15 -11
  301. package/src/serve/hub-app/review/line-window.ts +4 -1
  302. package/src/serve/hub-app/review/live-bar-st.ts +2 -2
  303. package/src/serve/hub-app/review/live-view.tsx +57 -1
  304. package/src/serve/hub-app/review/object-panel.tsx +20 -8
  305. package/src/serve/hub-app/review/question-views.tsx +191 -53
  306. package/src/serve/hub-app/review/recap-bar.tsx +40 -0
  307. package/src/serve/hub-app/review/request-cell.tsx +48 -0
  308. package/src/serve/hub-app/review/review-cells.tsx +154 -96
  309. package/src/serve/hub-app/review/review-host.ts +8 -4
  310. package/src/serve/hub-app/review/review-kit.ts +17 -11
  311. package/src/serve/hub-app/review/review-list.tsx +30 -35
  312. package/src/serve/hub-app/review/review-st.ts +32 -42
  313. package/src/serve/hub-app/review/review-thread.tsx +139 -85
  314. package/src/serve/hub-app/review/review-views.tsx +127 -68
  315. package/src/serve/hub-app/review/share-lines.tsx +4 -4
  316. package/src/serve/hub-app/review/start-st.ts +2 -5
  317. package/src/serve/hub-app/review/superseded-runs.ts +4 -3
  318. package/src/serve/hub-app/review/thread-close.tsx +10 -7
  319. package/src/serve/hub-app/review/thread-st.ts +381 -202
  320. package/src/serve/hub-app/review/tone-bar.tsx +129 -0
  321. package/src/serve/hub-app/review/tones.ts +53 -0
  322. package/src/serve/hub-app/review/trailer-body.tsx +2 -2
  323. package/src/serve/hub-app/review/trailer-st.ts +3 -2
  324. package/src/serve/hub-app/review/trailer-voices-st.ts +3 -2
  325. package/src/serve/hub-app/review.css +189 -106
  326. package/src/serve/hub-app/search-view.tsx +1 -1
  327. package/src/serve/hub-app/social/social-model.ts +0 -4
  328. package/src/serve/hub-app/source-acts.ts +2 -1
  329. package/src/serve/hub-app/stores/stores-page.tsx +2 -2
  330. package/src/serve/hub-app/terminal-sources.tsx +2 -1
  331. package/src/serve/hub-app/thread-row.tsx +3 -4
  332. package/src/serve/hub-app/tree-build.ts +0 -4
  333. package/src/serve/hub-app/urls-view.tsx +2 -1
  334. package/src/serve/hub-app/view-layout.css +13 -3
  335. package/src/serve/hub-app/wikis-view.tsx +5 -2
  336. package/src/serve/hub-runtime.ts +7 -37
  337. package/src/serve/project-gutter-route.ts +6 -2
  338. package/src/serve/read-thread.ts +20 -2
  339. package/src/serve/read-worker.ts +9 -1
  340. package/src/serve/record-pages.ts +46 -0
  341. package/src/serve/serve-hub.ts +2 -2
  342. package/src/serve/terminal-host.ts +1 -1
  343. package/src/serve/terminal-route.ts +1 -3
  344. package/src/serve/tracked-watch.ts +1 -3
  345. package/src/setup-house.ts +2 -2
  346. package/src/shipkit-files.ts +1 -1
  347. package/src/shipkit.ts +1 -6
  348. package/src/shot/client.ts +2 -1
  349. package/src/shot/film-box.ts +0 -20
  350. package/src/shot/on-place.ts +141 -0
  351. package/src/social/drafts.ts +1 -1
  352. package/src/social/kinds.ts +6 -29
  353. package/src/social/load.ts +2 -2
  354. package/src/social/post-object.ts +3 -2
  355. package/src/social/release-stage.ts +1 -1
  356. package/src/social/stage.ts +2 -2
  357. package/src/stores/model.ts +4 -0
  358. package/src/stores/routes.ts +11 -3
  359. package/src/suite/judge-suites.ts +58 -0
  360. package/src/suite/red-alone.ts +54 -18
  361. package/src/suite/runner-shapes.ts +2 -2
  362. package/src/suite/suite-cli.ts +67 -14
  363. package/src/suite/test-units.ts +2 -1
  364. package/src/urls/fill.ts +1 -1
  365. package/src/urls/places.ts +1 -2
  366. package/src/vault/vault-config.ts +2 -2
  367. package/src/vault/vault-init.ts +1 -5
  368. package/src/wiki/graph-page.ts +14 -2
  369. package/src/advisors/copy-truth.ts +0 -49
  370. package/src/advisors/door-child.ts +0 -112
  371. package/src/advisors/door-serve.ts +0 -52
  372. package/src/advisors/door.ts +0 -118
  373. package/src/checks/SK016-legacy-check-shapes.check.ts +0 -189
  374. package/src/checks/SK029-explicit-check-files.check.ts +0 -112
  375. package/src/checks/SK048-one-local-file.check.ts +0 -36
  376. package/src/core/config-rewrite.ts +0 -111
  377. package/src/core/daemon-port.ts +0 -10
  378. package/src/core/legacy-shapes.ts +0 -263
  379. package/src/core/migrate-layout.ts +0 -638
  380. package/src/core/port-check-cli.ts +0 -52
  381. package/src/core/port-check-file.ts +0 -295
  382. package/src/core/port-check-files.ts +0 -31
  383. package/src/gate/discover-legacy-checks.ts +0 -26
  384. package/src/home/home-move.ts +0 -527
  385. package/src/home/machine-fold.ts +0 -810
  386. package/src/hq/item-shapes.ts +0 -54
  387. package/src/hq/kinds/items-read.ts +0 -4
  388. package/src/media-store/backup/daemon.ts +0 -220
  389. package/src/media-store/backup/kept.ts +0 -241
  390. package/src/media-store/backup/prune.ts +0 -359
  391. package/src/media-store/backup/secret.ts +0 -88
  392. package/src/media-store/backup/ssh-space.ts +0 -202
  393. package/src/media-store/backup/watch.ts +0 -117
  394. package/src/review/band.ts +0 -182
  395. package/src/review/folders.ts +0 -216
  396. package/src/review/hook-ask.ts +0 -58
  397. package/src/review/role.ts +0 -48
  398. package/src/review/thread-fold.ts +0 -64
  399. package/src/review/wait.ts +0 -91
  400. package/src/rules/rule.ts +0 -43
  401. package/src/serve/hook-thread.ts +0 -37
  402. package/src/serve/hook-work.ts +0 -92
  403. package/src/serve/hook-worker.ts +0 -57
  404. package/src/serve/hub-app/review/dock.tsx +0 -147
  405. package/src/serve/hub-app/review/faces.tsx +0 -18
  406. package/src/serve/hub-app/review/item-verdict.tsx +0 -112
  407. package/src/serve/hub-app/review/review-board.tsx +0 -48
  408. /package/src/{scaffold → core}/jsonc-spans.ts +0 -0
@@ -1,54 +0,0 @@
1
- // the hq's items in the store's own tables (wiki tools/shipkit/threads-store.md): `review_cards`, `review_msgs`,
2
- // `review_gone`, `review_replaced`, each row with its node, beside the kinds' `cards` and `card_msgs`, which the three
3
- // item kinds still write in the same transaction. Filled once at file version 20, each rev computed again; the compare
4
- // says whether both shapes hold the same rows.
5
- // The one constraint a reader breaks first: a head is the store's text of it (`JSON.stringify(head)`), never the
6
- // body the kind keeps, so a rev is always the sha of what the store reads
7
- import type { Database } from 'bun:sqlite'
8
- import { HQ_ITEM_SCHEMA } from '../review/item-db.ts'
9
- import { putHead, putMessage } from '../review/item-rows.ts'
10
- import { revOf } from '../review/rev.ts'
11
- import { shapesLine, type ShapesDiff } from './shapes-words.ts'
12
-
13
- /** what a fill of the store's tables did, and how long it took */
14
- export type ItemFill = { heads: number; messages: number; ms: number }
15
-
16
- /** the store's tables made, and filled once from `cards` and `card_msgs`: a row whose rev does not compute again throws,
17
- * so the migration takes nothing back but its own transaction and the file stays at its version */
18
- export function fillItemRows(db: Database): ItemFill {
19
- const t0 = performance.now()
20
- db.exec(HQ_ITEM_SCHEMA)
21
- const held = (db.query('select (select count(*) from review_cards) + (select count(*) from review_msgs) as n').get() as { n: number }).n
22
- if (held > 0) return { heads: 0, messages: 0, ms: performance.now() - t0 }
23
- let heads = 0
24
- for (const r of db.query('select repo, id, node, at, rev, body from cards order by at, repo, id').iterate() as IterableIterator<{ repo: string; id: string; node: string; at: number; rev: string; body: string }>) {
25
- const b = JSON.parse(r.body) as { head: unknown; base?: string }
26
- if (revOf(b.head) !== r.rev) throw new Error(`the hq file stays at its version: head ${r.id} of ${r.repo} holds rev ${r.rev.slice(0, 12)}, its head computes another, nothing is filled`)
27
- putHead(db, r.repo, r.id, b.head, r.at, { node: r.node, base: b.base ?? null })
28
- heads++
29
- }
30
- let messages = 0
31
- for (const r of db.query('select repo, card, id, node, rev, body from card_msgs order by repo, card, id').iterate() as IterableIterator<{ repo: string; card: string; id: string; node: string; rev: string; body: string }>) {
32
- const m = (JSON.parse(r.body) as { msg: { id: string } }).msg
33
- if (revOf(m) !== r.rev) throw new Error(`the hq file stays at its version: message ${r.card}/${r.id} of ${r.repo} holds rev ${r.rev.slice(0, 12)}, its body computes another, nothing is filled`)
34
- putMessage(db, r.repo, r.card, m, { node: r.node })
35
- messages++
36
- }
37
- return { heads, messages, ms: performance.now() - t0 }
38
- }
39
-
40
- export { shapesLine, type ShapesDiff }
41
-
42
- /** both shapes by rev, null before the store's tables exist */
43
- export function compareShapes(db: Database): ShapesDiff | null {
44
- if (db.query("select 1 from sqlite_master where name = 'review_cards'").get() == null) return null
45
- const n = (sql: string): number => (db.query(sql).get() as { n: number }).n
46
- const heads = n('select count(*) as n from cards')
47
- const messages = n('select count(*) as n from card_msgs')
48
- const storeHeads = n('select count(*) as n from review_cards')
49
- const storeMessages = n('select count(*) as n from review_msgs')
50
- const differ = n('select count(*) as n from cards c join review_cards r on r.repo = c.repo and r.id = c.id where r.rev <> c.rev') + n('select count(*) as n from card_msgs c join review_msgs r on r.repo = c.repo and r.card = c.card and r.id = c.id where r.rev <> c.rev')
51
- const onlyOld = n('select count(*) as n from cards c where not exists (select 1 from review_cards r where r.repo = c.repo and r.id = c.id)') + n('select count(*) as n from card_msgs c where not exists (select 1 from review_msgs r where r.repo = c.repo and r.card = c.card and r.id = c.id)')
52
- const onlyStore = n('select count(*) as n from review_cards r where not exists (select 1 from cards c where c.repo = r.repo and c.id = r.id)') + n('select count(*) as n from review_msgs r where not exists (select 1 from card_msgs c where c.repo = r.repo and c.card = r.card and c.id = r.id)')
53
- return { heads, messages, storeHeads, storeMessages, differ, onlyOld, onlyStore, ok: differ + onlyOld + onlyStore === 0 }
54
- }
@@ -1,4 +0,0 @@
1
- // where the three item kinds read the hq's items (the feed, the revs, what a node holds, what is deleted): the store's
2
- // tables, or `cards` and `card_msgs` as before them (wiki tools/shipkit/threads-store.md). Both are written; this one
3
- // constant is the way back
4
- export const ITEMS_READ: 'store' | 'cards' = 'store' as 'store' | 'cards'
@@ -1,220 +0,0 @@
1
- // the daemon's backup job (wiki tools/shipkit/media-store-backup.md, The daemon). The daemon watches the sources
2
- // (watch.ts) and starts a look only when one may have moved, a run waits, or a clock says so (`backupDue`, a few
3
- // reads of the machine db). The look, in its child process, reads again only the paths the watch heard, on the list
4
- // the last look kept; a full walk every SAFETY_MS catches what no event told. Once the stamp moved and then held
5
- // still for one look (or waited WAIT_MAX_MS while sources kept moving), a run, at most one each 10 min; a run every
6
- // 6 h whatever moved, which reads every byte again
7
- import { machineGet, machineHeld, machineSet, machineUpdate } from '../../core/machine-db.ts'
8
- import type { RepoBackup } from '../../repo-config.ts'
9
- import type { GitIo } from '../../core/git.ts'
10
- import type { BackupRetention } from './retention.ts'
11
- import type { Space } from '../space.ts'
12
- import { backupRun, lastRun } from './run.ts'
13
- import { keepSources, keptSources, keptStamp, patchSources } from './kept.ts'
14
- import { backupOfRepo, collect, localFiles, missedBy, movesLocals, patchLocals, refreshSources, scopeOf, type Collect, type Source } from './sources.ts'
15
-
16
- /** the daemon looks this often: a look starts the child only when `backupDue` says so */
17
- export const BACKUP_EVERY_MS = 120_000
18
- export const FLOOR_MS = 10 * 60_000
19
- export const EVERY_MS = 6 * 3600_000
20
- /** a full walk at least this often: a watch that died, a root nobody watches, an event the system dropped */
21
- export const SAFETY_MS = 30 * 60_000
22
- /** a change that waits this long goes up even while other sources keep moving: an agent that writes every minute
23
- * never holds the rest back */
24
- export const WAIT_MAX_MS = 30 * 60_000
25
- /** the git walk for `*.local.*` files, kept this long per repo */
26
- const LOCALS_MS = 30 * 60_000
27
-
28
- /** the stamp the last look saw, the one the last good run sent, and the last full walk; `backup-` rows are left out
29
- * of the snapshot */
30
- export const STAMPS = 'backup-stamps'
31
- const LOCALS = 'backup-locals'
32
- const NO_SPACE = 'backup-no-space'
33
- /** the paths the daemon heard, handed to the look it starts: kept undone until a look took them */
34
- export const BATCH = 'backup-moved'
35
- /** each repo's `backup.paths`, for the daemon's watch: a look writes them, the watch reads them */
36
- export const NAMED = 'backup-named'
37
-
38
- /** `waitAt`: since when a stamp the last run did not send waits */
39
- export type Stamps = { seen: string; ran: string; fullAt?: number; waitAt?: number }
40
-
41
- export type Batch = { paths: string[]; all: boolean; done: boolean }
42
- type Locals = Record<string, { at: number; files: string[] }>
43
-
44
- /** each repo's `*.local.*` files as the daemon's looks keep them, for a reader that only looks (`sk backup --check`):
45
- * the kept list while it is fresh, else the repo's own git walk, which takes seconds in a repo with a large ignored
46
- * folder. Nothing is written */
47
- export function keptLocals(now: () => number = Date.now): (repo: string) => Promise<string[]> {
48
- const locals: Locals = machineGet<Locals>(LOCALS) ?? {}
49
- return async (repo) => {
50
- const held = locals[repo]
51
- return held && now() - held.at < LOCALS_MS ? held.files : localFiles(repo)
52
- }
53
- }
54
-
55
- export interface BackupTick {
56
- home: string
57
- repos: string[]
58
- space: () => Promise<{ space: Space | null; why: string }>
59
- print: (l: string) => void
60
- printErr: (l: string) => void
61
- now?: () => number
62
- /** each repo's `backup` key; by default its config */
63
- backupOf?: (repo: string) => Promise<RepoBackup | null>
64
- /** the retention after a run; by default the house config's, null prunes nothing */
65
- retention?: BackupRetention | null
66
- /** what git tracks and ignores in each repo; the real git by default */
67
- git?: GitIo
68
- /** the weekly restore drill of the last run (drillIfDue): the daemon's job turns it on, a suite leaves it off */
69
- drill?: boolean
70
- }
71
-
72
- /** the daemon's read before it starts a look (no walk, no child): something heard, a batch a look did not take, the
73
- * safety walk due, a run the floor held back, or the 6 h run */
74
- export function backupDue(now: number, heard: boolean): boolean {
75
- if (heard) return true
76
- const s = machineGet<Stamps>(STAMPS)
77
- if (s == null || now - (s.fullAt ?? 0) >= SAFETY_MS) return true
78
- if (machineGet<Batch>(BATCH)?.done === false) return true
79
- const last = lastRun()
80
- const since = last ? now - Date.parse(last.at) : Infinity
81
- if (since >= EVERY_MS) return true
82
- // with no Space a run never lands: only a change or a clock brings the next look
83
- return s.seen !== s.ran && since >= FLOOR_MS && !machineGet<string>(NO_SPACE)
84
- }
85
-
86
- /** one look of the daemon's backup job, in its child process: the moved paths read again (or every source, on the
87
- * safety clock), then a run when it is due. What a look remembers lives in the machine db, so a daemon restart
88
- * never makes a run of its own */
89
- export const backupTick = (o: BackupTick): Promise<void> => machineHeld(() => tick(o))
90
-
91
- async function tick(o: BackupTick): Promise<void> {
92
- const now = o.now ?? Date.now
93
- const locals: Locals = machineGet<Locals>(LOCALS) ?? {}
94
- let walked = false
95
- const cachedLocals = async (repo: string): Promise<string[]> => {
96
- const held = locals[repo]
97
- if (held && now() - held.at < LOCALS_MS) return held.files
98
- const files = await localFiles(repo, o.git)
99
- locals[repo] = { at: now(), files }
100
- walked = true
101
- return files
102
- }
103
- try {
104
- const started = now()
105
- const held: Stamps = machineGet<Stamps>(STAMPS) ?? { seen: '', ran: '' }
106
- const batch = machineGet<Batch>(BATCH)
107
- const moved = batch && !batch.done ? batch.paths : []
108
- // the list the last look kept, as rows (kept.ts): a look reads and writes only those under what moved
109
- const keptList = keptStamp() != null
110
- const last = lastRun()
111
- const since = last ? started - Date.parse(last.at) : Infinity
112
- const sixHours = since >= EVERY_MS
113
- const all = batch?.done === false && batch.all
114
- const full = !keptList || all || started - (held.fullAt ?? 0) >= SAFETY_MS || sixHours
115
- // a heard `*.local.*` path is read again in the kept git walk, alone
116
- for (const r of new Set(moved.map((p) => movesLocals(p, o.repos)).filter((r) => r != null))) {
117
- const kept = locals[r]
118
- if (kept == null) continue
119
- locals[r] = { at: kept.at, files: await patchLocals(r, kept.files, moved, o.git) }
120
- walked = true
121
- }
122
- // each config read says its named paths: the daemon's watch hears them
123
- const named = machineGet<Record<string, string[]>>(NAMED) ?? {}
124
- const namedWas = JSON.stringify(named)
125
- const backupOf = async (repo: string): Promise<RepoBackup | null> => {
126
- const b = await (o.backupOf ?? backupOfRepo)(repo)
127
- named[repo] = b?.paths ?? []
128
- return b
129
- }
130
- const c: Collect = { home: o.home, repos: o.repos, locals: cachedLocals, backupOf, git: o.git }
131
- let list: Source[] | null = null
132
- let stamp: string
133
- if (full) {
134
- // on a full walk, what the events told is read first: what the walk finds beyond it, no event told
135
- const told = keptList && !all ? await refreshSources(keptSources(o.home) ?? [], moved, c) : null
136
- list = await collect(c)
137
- if (told != null) {
138
- // said: a watch that went quiet is seen here, not after a lost file
139
- const missed = missedBy(told, list)
140
- if (missed.length > 0) o.printErr(`note: backup: the safety walk found ${missed.length} changes no file event told, first ${missed[0]}`)
141
- }
142
- stamp = keepSources(list)
143
- } else {
144
- const scope = scopeOf(moved, o.repos)
145
- stamp = patchSources(o.home, scope, await collect({ ...c, scope }))
146
- }
147
- if (walked) machineSet(LOCALS, locals)
148
- if (JSON.stringify(named) !== namedWas) machineSet(NAMED, named)
149
- // done only while it is still the batch this look took: paths the watch added meanwhile wait for the next look
150
- if (batch && !batch.done) machineUpdate<Batch>(BATCH, (now) => (now != null && !now.done && now.all === batch.all && JSON.stringify(now.paths) === JSON.stringify(batch.paths) ? { ...now, done: true } : now))
151
- const waitAt = stamp === held.ran ? undefined : (held.waitAt ?? started)
152
- const waited = waitAt != null && started - waitAt >= WAIT_MAX_MS
153
- const due = sixHours || (stamp !== held.ran && (stamp === held.seen || waited) && since >= FLOOR_MS)
154
- const fullAt = full ? started : held.fullAt
155
- machineSet(STAMPS, { seen: stamp, ran: held.ran, ...(fullAt != null ? { fullAt } : {}), ...(waitAt != null ? { waitAt } : {}) } satisfies Stamps)
156
- if (!due) return await drillIfDue(o, now())
157
- const { space, why } = await o.space()
158
- // said once per reason: a look is a child of its own
159
- if (!space) {
160
- if (machineGet<string>(NO_SPACE) !== why) o.printErr(`WARN backup: nothing goes up: ${why}`)
161
- machineSet(NO_SPACE, why)
162
- return
163
- }
164
- machineSet(NO_SPACE, '')
165
- const r = await backupRun(space, { ...c, sources: list ?? keptSources(o.home) ?? [], rehash: sixHours, now: new Date(now()) })
166
- if (r.failed.length === 0) machineSet(STAMPS, { seen: stamp, ran: stamp, ...(fullAt != null ? { fullAt } : {}) } satisfies Stamps)
167
- if (r.failed.length > 0) o.printErr(`WARN backup: ${r.failed.length} objects did not go up, no manifest written; first: ${r.failed[0]?.key}: ${r.failed[0]?.error}`)
168
- else if (r.manifest) {
169
- o.print(`ok: backup: ${r.files} files, ${r.sent} objects up (${(r.sentBytes / 1e6).toFixed(1)} MB), ${r.read} read again, ${r.refused.length} refused, manifest ${r.manifest}`)
170
- await pruneAfterRun(space, { ...o, now: now() })
171
- await drillIfDue(o, now())
172
- }
173
- } catch (e) {
174
- // a hand run holds the lock: the next look tries again, and says so (a lock that never goes stops every backup)
175
- const msg = (e as Error).message
176
- o.printErr(/^another backup runs/.test(msg) ? `note: backup: not now, ${msg}` : `WARN backup: the run broke: ${msg}`)
177
- }
178
- }
179
-
180
- /** a run is proven by a restore once a week: this machine's last run brought back from the Space into a scratch folder,
181
- * every sha checked (restore.ts drill), its result the record the status reads. A drill that failed is done again
182
- * after an hour; a run nobody restored is no backup */
183
- export const DRILL_EVERY_MS = 7 * 24 * 3600_000
184
- export const DRILL_RETRY_MS = 3600_000
185
- export function drillDue(last: { at: number; failed: number } | null, hasRun: boolean, now: number): boolean {
186
- if (!hasRun) return false
187
- if (last == null) return true
188
- return now - last.at >= (last.failed > 0 ? DRILL_RETRY_MS : DRILL_EVERY_MS)
189
- }
190
-
191
- async function drillIfDue(o: BackupTick, now: number): Promise<void> {
192
- if (o.drill !== true) return
193
- const { drill, lastBackupDrill } = await import('./restore.ts')
194
- if (!drillDue(lastBackupDrill(), lastRun() != null, now)) return
195
- const { space } = await o.space()
196
- if (space == null) return
197
- try {
198
- const d = await drill(space, { home: o.home })
199
- const line = `backup drill: the run ${d.run} restored from the Space, ${d.restored} of ${d.files} files with their sha`
200
- if (d.failed.length === 0) o.print(`ok: ${line}`)
201
- else o.printErr(`WARN ${line}, ${d.failed.length} FAILED (${d.failed[0]!.path}: ${d.failed[0]!.error})`)
202
- } catch (e) {
203
- o.printErr(`WARN backup drill: it did not run: ${(e as Error).message}`)
204
- }
205
- }
206
-
207
- /** the retention after each run that wrote one (prune.ts): quiet when nothing goes or the house names no retention */
208
- async function pruneAfterRun(space: Space, o: { print: (l: string) => void; printErr: (l: string) => void; now: number; retention?: BackupRetention | null }): Promise<void> {
209
- try {
210
- const { pruneBackups } = await import('./prune.ts')
211
- const p = await pruneBackups(space, { apply: true, auto: true, now: o.now, ...(o.retention !== undefined ? { policy: o.retention } : {}) })
212
- for (const l of p.lines) {
213
- if (l.startsWith('WARN')) o.printErr(l)
214
- else if (l.startsWith('ok:')) o.print(l)
215
- }
216
- } catch (e) {
217
- const msg = (e as Error).message
218
- o.printErr(/^another backup runs/.test(msg) ? `note: backup prune: not now, ${msg}` : `WARN backup prune: ${msg}`)
219
- }
220
- }
@@ -1,241 +0,0 @@
1
- // what the backup keeps between two looks and two runs, as rows of the machine db (core/machine-db.ts owns the
2
- // tables): the list of sources the last look saw, and every file the last run held with its sha. A look writes only
3
- // the rows under the paths it heard, and the list's stamp is kept beside them as the xor of each row's hash, so no
4
- // look reads the list whole. The two json values a file of the older shape holds are imported at the first access,
5
- // then their key goes; one an older process writes after that is newer than the rows and replaces them
6
- import type { Database } from 'bun:sqlite'
7
- import { join, relative, sep } from 'node:path'
8
- import { machineDb } from '../../core/machine-db.ts'
9
- import { stampOfSources, type Scope, type Source } from './sources.ts'
10
-
11
- /** the older shape: the list and the seen files as two json values */
12
- const OLD_SOURCES = 'backup-sources'
13
- const OLD_SEEN = 'backup-seen'
14
- /** the daemon's stamps (daemon.ts STAMPS): translated at the import, so a new formula never reads as a change */
15
- const STAMPS = 'backup-stamps'
16
- /** the list's stamp, `{ n, x }`: its rows that are files and the xor of their hashes. Its presence is "a list kept" */
17
- const XOR = 'backup-sources-xor'
18
-
19
- export type SeenRow = { size: number; mtime: number; sha: string; key: string }
20
-
21
- type Row = { rel: string; refused: number; size: number; mtime: number; sqlite: string | null; whole: number; broken: string | null }
22
-
23
- const rowOf = (s: Source): Row => ({ rel: s.rel, refused: s.broken != null ? 1 : 0, size: s.size, mtime: s.mtime, sqlite: s.sqlite ? JSON.stringify(s.sqlite) : null, whole: s.whole ? 1 : 0, broken: s.broken ?? null })
24
-
25
- const sourceOf = (r: Row, home: string): Source => ({ abs: join(home, r.rel), rel: r.rel, size: r.size, mtime: r.mtime, ...(r.sqlite != null ? { sqlite: JSON.parse(r.sqlite) } : r.whole === 1 ? { whole: true as const } : {}), ...(r.broken != null ? { broken: r.broken } : {}) })
26
-
27
- /** the same line stampOfSources hashes, as 64 bits: a sqlite moves by itself and is snapshotted on the clock */
28
- const hashOf = (r: Pick<Row, 'rel' | 'size' | 'mtime' | 'broken' | 'sqlite'>): bigint => (r.sqlite != null ? 0n : BigInt.asUintN(64, BigInt(Bun.hash.wyhash(`${r.rel}\0${r.size}\0${r.mtime}\0${r.broken ?? ''}\n`))))
29
-
30
- type Xor = { n: number; x: string }
31
- const stampText = (n: number, x: bigint): string => `${n}:${x.toString(16).padStart(16, '0')}`
32
-
33
- /** the stamp of a whole list, as the rows keep it */
34
- export function stampOfRows(list: readonly Source[]): string {
35
- let x = 0n
36
- let n = 0
37
- for (const s of list) {
38
- const h = hashOf(rowOf(s))
39
- if (h === 0n) continue
40
- x ^= h
41
- n++
42
- }
43
- return stampText(n, x)
44
- }
45
-
46
- const ROWS = 'select rel, refused, size, mtime, sqlite, whole, broken from backup_sources'
47
- const INSERT = 'insert or replace into backup_sources (rel, refused, size, mtime, sqlite, whole, broken) values ($rel, $refused, $size, $mtime, $sqlite, $whole, $broken)'
48
- const bind = (r: Row): Record<string, string | number | null> => ({ $rel: r.rel, $refused: r.refused, $size: r.size, $mtime: r.mtime, $sqlite: r.sqlite, $whole: r.whole, $broken: r.broken })
49
-
50
- const readXor = (db: Database): Xor | null => {
51
- const row = db.query('select value from state where name = ?').get(XOR) as { value: string } | null
52
- return row == null ? null : (JSON.parse(row.value) as Xor)
53
- }
54
- const writeXor = (db: Database, n: number, x: bigint): string => {
55
- db.query('insert or replace into state (name, value, at) values (?, ?, ?)').run(XOR, JSON.stringify({ n, x: x.toString(16) } satisfies Xor), Date.now())
56
- return stampText(n, x)
57
- }
58
-
59
- /** a value of the older shape, null when absent; one that does not parse goes and is said (an older reader took it
60
- * as none too) */
61
- function oldValue<T>(db: Database, name: string): T | null {
62
- const row = db.query('select value from state where name = ?').get(name) as { value: string } | null
63
- if (row == null) return null
64
- try {
65
- return JSON.parse(row.value) as T
66
- } catch {
67
- console.error(`WARN backup: the machine db's ${name} does not parse: it goes, the next look walks every source`)
68
- db.query('delete from state where name = ?').run(name)
69
- return null
70
- }
71
- }
72
-
73
- /** the list replaced whole by `list`: only the rows that moved are written. The new stamp */
74
- function replaceAll(db: Database, list: readonly Source[]): string {
75
- const held = new Map<string, Row>()
76
- for (const r of db.query(ROWS).all() as Row[]) held.set(`${r.rel}\0${r.refused}`, r)
77
- const put = db.query(INSERT)
78
- const drop = db.query('delete from backup_sources where rel = ? and refused = ?')
79
- let x = 0n
80
- let n = 0
81
- for (const s of list) {
82
- const r = rowOf(s)
83
- const k = `${r.rel}\0${r.refused}`
84
- const was = held.get(k)
85
- held.delete(k)
86
- if (was == null || JSON.stringify(was) !== JSON.stringify(r)) put.run(bind(r))
87
- const h = hashOf(r)
88
- if (h !== 0n) {
89
- x ^= h
90
- n++
91
- }
92
- }
93
- for (const r of held.values()) drop.run(r.rel, r.refused)
94
- return writeXor(db, n, x)
95
- }
96
-
97
- /** the two values of the older shape become rows, once, under the write lock; then the file gives back the pages they
98
- * held (megabytes: a delete never shrinks a file) when no other process holds it open */
99
- function importOld(db: Database): void {
100
- if (db.query('select 1 from state where name in (?, ?) limit 1').get(OLD_SOURCES, OLD_SEEN) == null) return
101
- importRows(db)
102
- try {
103
- db.exec('vacuum')
104
- } catch { /* another process reads it now: the pages are reused by the next rows */ }
105
- }
106
-
107
- function importRows(db: Database): void {
108
- db.transaction(() => {
109
- const list = oldValue<Source[]>(db, OLD_SOURCES)
110
- if (list != null) {
111
- const stamp = replaceAll(db, list)
112
- const old = stampOfSources(list)
113
- const s = oldValue<Record<string, unknown>>(db, STAMPS)
114
- if (s != null && (s.seen === old || s.ran === old)) {
115
- const next = { ...s, ...(s.seen === old ? { seen: stamp } : {}), ...(s.ran === old ? { ran: stamp } : {}) }
116
- db.query('update state set value = ? where name = ?').run(JSON.stringify(next), STAMPS)
117
- }
118
- db.query('delete from state where name = ?').run(OLD_SOURCES)
119
- }
120
- const seen = oldValue<Record<string, SeenRow>>(db, OLD_SEEN)
121
- if (seen != null) {
122
- writeSeen(db, new Map(Object.entries(seen)))
123
- db.query('delete from state where name = ?').run(OLD_SEEN)
124
- }
125
- }).immediate()
126
- }
127
-
128
- /** the kept rows, imported first from a value of the older shape */
129
- const kept = <R>(body: (db: Database) => R): R =>
130
- machineDb((db) => {
131
- importOld(db)
132
- return body(db)
133
- })
134
-
135
- /** the list the last look kept, sorted as collect sorts it, under `home` (a source is always the home's join of its
136
- * path: collect.ts keeps nothing outside it); null when no look kept one */
137
- export function keptSources(home: string): Source[] | null {
138
- return kept((db) => {
139
- if (readXor(db) == null) return null
140
- const rows = db.query(ROWS).all() as Row[]
141
- return rows.map((r) => sourceOf(r, home)).sort((a, b) => (a.rel < b.rel ? -1 : a.rel > b.rel ? 1 : a.broken === b.broken ? 0 : a.broken ? 1 : -1))
142
- })
143
- }
144
-
145
- /** the stamp of the kept list, null when none: what a look compares, with no read of the list */
146
- export function keptStamp(): string | null {
147
- return kept((db) => {
148
- const x = readXor(db)
149
- return x == null ? null : stampText(x.n, BigInt(`0x${x.x}`))
150
- })
151
- }
152
-
153
- /** a full walk's list kept in place of the last: its stamp */
154
- export function keepSources(list: readonly Source[]): string {
155
- return kept((db) => db.transaction(() => replaceAll(db, list)).immediate())
156
- }
157
-
158
- /** the `rel` ranges a scope covers under `home`: every row (a path at or above the home), none (a path beside it), or
159
- * each path and what is under it */
160
- function rangesOf(home: string, scope: Scope): 'all' | string[] {
161
- const out: string[] = []
162
- for (const p of scope.paths) {
163
- const r = relative(home, p)
164
- if (r === '' || (r.startsWith('..') && !relative(p, home).startsWith('..'))) return 'all'
165
- if (r.startsWith('..')) continue
166
- out.push(r)
167
- }
168
- return out
169
- }
170
-
171
- /** the rows under the scope's paths replaced by `fresh` (a scoped collect of them), in one transaction: the new stamp */
172
- export function patchSources(home: string, scope: Scope, fresh: readonly Source[]): string {
173
- return kept((db) =>
174
- db.transaction(() => {
175
- const ranges = rangesOf(home, scope)
176
- const was = readXor(db)
177
- if (ranges === 'all' || was == null) return replaceAll(db, fresh)
178
- let x = BigInt(`0x${was.x}`)
179
- let n = was.n
180
- const under = db.query(`${ROWS} where rel = ?1 or (rel > ?1 || '${sep}' and rel < ?1 || '${String.fromCharCode(sep.charCodeAt(0) + 1)}')`)
181
- const drop = db.query(`delete from backup_sources where rel = ?1 or (rel > ?1 || '${sep}' and rel < ?1 || '${String.fromCharCode(sep.charCodeAt(0) + 1)}')`)
182
- for (const r of ranges) {
183
- for (const row of under.all(r) as Row[]) {
184
- const h = hashOf(row)
185
- if (h === 0n) continue
186
- x ^= h
187
- n--
188
- }
189
- drop.run(r)
190
- }
191
- const put = db.query(INSERT)
192
- const one = db.query('select rel, size, mtime, broken, sqlite from backup_sources where rel = ? and refused = ?')
193
- for (const s of fresh) {
194
- const row = rowOf(s)
195
- // a fresh source the scope does not cover would be counted twice
196
- const k = one.get(row.rel, row.refused) as Row | null
197
- if (k != null) {
198
- const h = hashOf(k)
199
- if (h !== 0n) {
200
- x ^= h
201
- n--
202
- }
203
- }
204
- put.run(bind(row))
205
- const h = hashOf(row)
206
- if (h !== 0n) {
207
- x ^= h
208
- n++
209
- }
210
- }
211
- return writeXor(db, n, x)
212
- }).immediate(),
213
- )
214
- }
215
-
216
- function writeSeen(db: Database, next: ReadonlyMap<string, SeenRow>): void {
217
- const held = new Map<string, SeenRow>()
218
- for (const r of db.query('select path, size, mtime, sha, key from backup_seen').all() as (SeenRow & { path: string })[]) held.set(r.path, { size: r.size, mtime: r.mtime, sha: r.sha, key: r.key })
219
- const put = db.query('insert or replace into backup_seen (path, size, mtime, sha, key) values (?, ?, ?, ?, ?)')
220
- const drop = db.query('delete from backup_seen where path = ?')
221
- for (const [path, r] of next) {
222
- const was = held.get(path)
223
- held.delete(path)
224
- if (was == null || was.size !== r.size || was.mtime !== r.mtime || was.sha !== r.sha || was.key !== r.key) put.run(path, r.size, r.mtime, r.sha, r.key)
225
- }
226
- for (const path of held.keys()) drop.run(path)
227
- }
228
-
229
- /** every file the last run held, by its path under the home */
230
- export function keptSeen(): Map<string, SeenRow> {
231
- return kept((db) => {
232
- const out = new Map<string, SeenRow>()
233
- for (const r of db.query('select path, size, mtime, sha, key from backup_seen order by path').all() as (SeenRow & { path: string })[]) out.set(r.path, { size: r.size, mtime: r.mtime, sha: r.sha, key: r.key })
234
- return out
235
- })
236
- }
237
-
238
- /** the files this run held, in place of the last run's: only the rows that moved are written */
239
- export function keepSeen(next: ReadonlyMap<string, SeenRow>): void {
240
- kept((db) => db.transaction(() => writeSeen(db, next)).immediate())
241
- }