rvlib-shipkit 2.25.0 → 2.27.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 (456) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/package.json +2 -2
  3. package/src/agent-time/diet.ts +77 -0
  4. package/src/agent-time/family.ts +72 -0
  5. package/src/agent-time/hooks.ts +331 -0
  6. package/src/agent-time/limits.ts +44 -0
  7. package/src/agent-time/live.ts +1 -3
  8. package/src/agent-time/mask.ts +96 -18
  9. package/src/agent-time/refused.ts +45 -0
  10. package/src/agent-time/render.ts +130 -17
  11. package/src/agent-time/report-cache.ts +57 -0
  12. package/src/agent-time/report.ts +349 -23
  13. package/src/agent-time/shape.ts +50 -3
  14. package/src/agent-time/store.ts +370 -95
  15. package/src/agent-time/timeline.ts +140 -21
  16. package/src/agent-time/turns.ts +18 -8
  17. package/src/annex/offload.ts +17 -6
  18. package/src/annex/tidy.ts +70 -10
  19. package/src/asks/ask-after.ts +40 -0
  20. package/src/asks/asks-queue.ts +205 -47
  21. package/src/assets/kit-config.ts +7 -3
  22. package/src/assets/kits/identity-kit.ts +289 -10
  23. package/src/check.ts +3 -1
  24. package/src/checks/SK004-gitignore-policy.check.ts +12 -5
  25. package/src/checks/SK013-one-way-in-policy.check.ts +8 -3
  26. package/src/checks/SK026-asks-autoclose.check.ts +5 -5
  27. package/src/checks/SK028-readme.check.ts +7 -66
  28. package/src/checks/SK030-cache-audit.check.ts +6 -1
  29. package/src/checks/SK031-trace-audit.check.ts +2 -0
  30. package/src/checks/SK033-global-contribution.check.ts +3 -1
  31. package/src/checks/SK035-public-clean.check.ts +2 -2
  32. package/src/checks/SK043-scratch-on-the-mac.check.ts +59 -4
  33. package/src/checks/SK045-slow-hooks.check.ts +47 -0
  34. package/src/checks/SK046-hook-records.check.ts +76 -0
  35. package/src/checks/SK047-login-in-description.check.ts +36 -0
  36. package/src/checks/SK048-one-local-file.check.ts +36 -0
  37. package/src/cli/cmd_Asks.ts +71 -5
  38. package/src/cli/cmd_Backup.ts +5 -3
  39. package/src/cli/cmd_Capacity.ts +2 -2
  40. package/src/cli/cmd_CrossRepoActivity.ts +5 -4
  41. package/src/cli/cmd_Db.ts +253 -0
  42. package/src/cli/cmd_History.ts +212 -0
  43. package/src/cli/cmd_Home.ts +31 -3
  44. package/src/cli/cmd_Hq.ts +516 -0
  45. package/src/cli/cmd_Machines.ts +36 -0
  46. package/src/cli/cmd_Media.ts +38 -6
  47. package/src/cli/cmd_Mv.ts +1 -1
  48. package/src/cli/cmd_Reflect.ts +23 -2
  49. package/src/cli/cmd_Releases.ts +30 -0
  50. package/src/cli/cmd_Review.ts +569 -118
  51. package/src/cli/cmd_Secret.ts +62 -12
  52. package/src/cli/cmd_Social.ts +83 -1
  53. package/src/cli/cmd_Space.ts +324 -0
  54. package/src/cli/cmd_Time.ts +82 -5
  55. package/src/cli/cmd_Tmp.ts +28 -4
  56. package/src/cli/cmd_Trailer.ts +14 -10
  57. package/src/cli/cmd_Urls.ts +56 -0
  58. package/src/cli/cmd_Vault.ts +583 -0
  59. package/src/cli/family-help.ts +4 -2
  60. package/src/cli/family-pages.ts +1 -1
  61. package/src/cli/registry.ts +26 -4
  62. package/src/collections/actions.ts +63 -13
  63. package/src/collections/collection-types.ts +23 -1
  64. package/src/collections/providers.ts +117 -23
  65. package/src/collections/review.ts +4 -4
  66. package/src/commands/runner.ts +5 -1
  67. package/src/contracts/contract-manifests.ts +6 -5
  68. package/src/contracts/contract-types.ts +6 -4
  69. package/src/contracts/contracts-engine.ts +11 -0
  70. package/src/contracts/manifests/chrome-web-store.contract.ts +31 -0
  71. package/src/core/budget.ts +12 -17
  72. package/src/core/capacity.ts +27 -18
  73. package/src/core/check-runner.ts +7 -4
  74. package/src/core/history-files.ts +9 -5
  75. package/src/core/layout.ts +3 -0
  76. package/src/core/machine-busy.ts +127 -0
  77. package/src/core/machine-commit.ts +6 -1
  78. package/src/core/machine-db.ts +368 -20
  79. package/src/core/machine-layout.ts +137 -0
  80. package/src/core/machine-read.ts +81 -0
  81. package/src/core/migrate-layout.ts +1 -1
  82. package/src/core/one-way-in-policy.ts +64 -7
  83. package/src/core/polling-policy.ts +27 -0
  84. package/src/core/retention.ts +16 -2
  85. package/src/core/scope.ts +107 -14
  86. package/src/core/signal-name.ts +6 -0
  87. package/src/core/slots.ts +5 -4
  88. package/src/core/trace-audit.ts +6 -1
  89. package/src/core/transient.ts +6 -0
  90. package/src/drill/clean.ts +25 -1
  91. package/src/drill/run.ts +2 -2
  92. package/src/drill/seal.ts +27 -4
  93. package/src/home/activity-shape.ts +2 -0
  94. package/src/home/activity.ts +66 -3
  95. package/src/home/app-sign.ts +15 -4
  96. package/src/home/daemon-jobs.ts +149 -7
  97. package/src/home/database-projects.ts +111 -0
  98. package/src/home/database-words.ts +203 -0
  99. package/src/home/databases.ts +460 -0
  100. package/src/home/global-units.ts +6 -0
  101. package/src/home/gui-app/ShipkitGui.swift +122 -1
  102. package/src/home/gui-app.ts +6 -3
  103. package/src/home/home-move.ts +514 -0
  104. package/src/home/home-paths.ts +12 -3
  105. package/src/home/home-routes.ts +68 -0
  106. package/src/home/inbox-shape.ts +24 -14
  107. package/src/home/machine-fold.ts +820 -0
  108. package/src/home/machine-host.ts +20 -5
  109. package/src/home/machine-repair.ts +503 -0
  110. package/src/home/machine-stores.ts +66 -15
  111. package/src/home/machine-temp.ts +547 -86
  112. package/src/home/resume.ts +79 -0
  113. package/src/home/run-daemon.ts +25 -4
  114. package/src/home/run-pm.ts +18 -5
  115. package/src/home/serve-daemon.ts +18 -3
  116. package/src/home/serve-web.ts +175 -7
  117. package/src/home/space-job.ts +115 -0
  118. package/src/home/space-page.ts +257 -0
  119. package/src/home/space-route.ts +167 -0
  120. package/src/home/space-words.ts +103 -0
  121. package/src/home/time-page-shape.ts +202 -0
  122. package/src/home/time-page.ts +64 -0
  123. package/src/home/vault-page.ts +482 -0
  124. package/src/home/vault-words.ts +132 -0
  125. package/src/hooks/leak-guard.ts +40 -13
  126. package/src/hooks/one-way-in-guard.ts +17 -4
  127. package/src/hq/cards.ts +581 -0
  128. package/src/hq/door-route.ts +43 -0
  129. package/src/hq/door.ts +163 -0
  130. package/src/hq/files-send.ts +348 -0
  131. package/src/hq/forgotten-log.ts +35 -0
  132. package/src/hq/history-check.ts +255 -0
  133. package/src/hq/history-place.ts +146 -0
  134. package/src/hq/history-send.ts +487 -0
  135. package/src/hq/hq-db.ts +99 -0
  136. package/src/hq/hq-local.ts +469 -0
  137. package/src/hq/hq-routes.ts +196 -0
  138. package/src/hq/hq-store.ts +599 -0
  139. package/src/hq/kinds/ask.ts +147 -0
  140. package/src/hq/kinds/card-gone.ts +83 -0
  141. package/src/hq/kinds/card-msg.ts +66 -0
  142. package/src/hq/kinds/card.ts +99 -0
  143. package/src/hq/kinds/index.ts +21 -0
  144. package/src/hq/kinds/kind.ts +126 -0
  145. package/src/hq/kinds/media.ts +125 -0
  146. package/src/hq/kinds/prompt.ts +219 -0
  147. package/src/hq/kinds/reflection.ts +128 -0
  148. package/src/hq/kinds/report.ts +55 -0
  149. package/src/hq/kinds/time-day.ts +236 -0
  150. package/src/hq/ledger.ts +473 -0
  151. package/src/hq/machine-key.ts +44 -0
  152. package/src/hq/machine-notes.ts +131 -0
  153. package/src/hq/outbox.ts +372 -0
  154. package/src/hq/replica.ts +168 -0
  155. package/src/hq/repo-id.ts +4 -0
  156. package/src/hq/report-facts.ts +117 -0
  157. package/src/hq/report.ts +274 -0
  158. package/src/hq/switch.ts +151 -0
  159. package/src/hq/vault-grant.ts +92 -0
  160. package/src/identities/identities.ts +15 -3
  161. package/src/identities/steps.ts +31 -0
  162. package/src/ledger/db.ts +73 -26
  163. package/src/ledger/gate-checks.ts +29 -4
  164. package/src/ledger/git-key.ts +3 -3
  165. package/src/ledger/git-scan.ts +49 -7
  166. package/src/ledger/run-log.ts +11 -3
  167. package/src/ledger/store.ts +26 -4
  168. package/src/media-store/backup/daemon.ts +36 -17
  169. package/src/media-store/backup/kept.ts +241 -0
  170. package/src/media-store/backup/restore.ts +14 -4
  171. package/src/media-store/backup/run.ts +11 -11
  172. package/src/media-store/backup/secret.ts +6 -4
  173. package/src/media-store/backup/snapshot.ts +4 -2
  174. package/src/media-store/backup/sources.ts +68 -20
  175. package/src/media-store/copy/config.ts +77 -0
  176. package/src/media-store/copy/copy.ts +194 -0
  177. package/src/media-store/copy/lifecycle.ts +43 -0
  178. package/src/media-store/copy/night.ts +90 -0
  179. package/src/media-store/copy/proof.ts +151 -0
  180. package/src/media-store/copy/snapshot.ts +152 -0
  181. package/src/media-store/copy/store.ts +62 -0
  182. package/src/media-store/copy/stream.ts +176 -0
  183. package/src/media-store/daemon.ts +66 -16
  184. package/src/media-store/db.ts +22 -11
  185. package/src/media-store/forget.ts +23 -3
  186. package/src/media-store/hand.ts +4 -3
  187. package/src/media-store/hq-find.ts +75 -0
  188. package/src/media-store/hq-send.ts +277 -0
  189. package/src/media-store/kind.ts +2 -0
  190. package/src/media-store/purge.ts +18 -15
  191. package/src/media-store/put.ts +24 -8
  192. package/src/media-store/remote.ts +16 -7
  193. package/src/media-store/space-listing.ts +240 -0
  194. package/src/media-store/space.ts +40 -21
  195. package/src/media-store/sync.ts +5 -5
  196. package/src/media-store/tidy.ts +98 -0
  197. package/src/media-store/upload.ts +3 -3
  198. package/src/media-store/webp.ts +66 -0
  199. package/src/messages/model.ts +16 -7
  200. package/src/messages/routes.ts +14 -4
  201. package/src/messages/score.ts +2 -2
  202. package/src/messages/suggest.ts +96 -0
  203. package/src/pages/frame-client.ts +10 -0
  204. package/src/plan/planner.ts +28 -17
  205. package/src/recordings/media-kind.ts +1 -0
  206. package/src/recordings/model.ts +1 -1
  207. package/src/recordings/recorder-app/Capture.swift +9 -7
  208. package/src/reflect/hq-corpus.ts +56 -0
  209. package/src/reflect/prompt-corpus.ts +3 -1
  210. package/src/reflect/transcript.ts +146 -36
  211. package/src/release/machine-words.ts +67 -0
  212. package/src/release/public-clean.ts +8 -3
  213. package/src/releases/from-card.ts +1 -1
  214. package/src/releases/model.ts +18 -0
  215. package/src/releases/routes.ts +10 -5
  216. package/src/releases/store.ts +23 -2
  217. package/src/repo-config.ts +42 -2
  218. package/src/review/anim.ts +2 -2
  219. package/src/review/auto.ts +2 -2
  220. package/src/review/card-db.ts +244 -0
  221. package/src/review/card-rows.ts +266 -0
  222. package/src/review/cells.ts +76 -5
  223. package/src/review/draft-card.ts +41 -0
  224. package/src/review/draft-model.ts +78 -0
  225. package/src/review/format.ts +29 -10
  226. package/src/review/gone.ts +98 -0
  227. package/src/review/inbox-hook.ts +35 -0
  228. package/src/review/marks.ts +1 -1
  229. package/src/review/media-kind.ts +7 -5
  230. package/src/review/media-read.ts +9 -7
  231. package/src/review/media.ts +32 -5
  232. package/src/review/model.ts +214 -45
  233. package/src/review/move.ts +491 -0
  234. package/src/review/notes.ts +12 -20
  235. package/src/review/previews.ts +15 -19
  236. package/src/review/project.ts +1 -1
  237. package/src/review/rev.ts +8 -0
  238. package/src/review/role.ts +1 -1
  239. package/src/review/routes.ts +64 -13
  240. package/src/review/stage-card.ts +8 -4
  241. package/src/review/stage-routes.ts +1 -1
  242. package/src/review/store.ts +529 -83
  243. package/src/review/thread-fold.ts +51 -0
  244. package/src/review/threads.ts +147 -9
  245. package/src/review/trailer-auto-mock.ts +63 -0
  246. package/src/review/trailer-card.ts +4 -4
  247. package/src/review/trailer-desk.ts +41 -7
  248. package/src/review/trailer-media.ts +1 -0
  249. package/src/review/trailer-model.ts +8 -0
  250. package/src/review/trailer-notes.ts +1 -1
  251. package/src/review/trailer-routes.ts +9 -1
  252. package/src/review/unit.ts +6 -6
  253. package/src/review/watchers.ts +1 -1
  254. package/src/rules/untrack-generated.ts +92 -8
  255. package/src/secrets/held.ts +16 -0
  256. package/src/secrets/keychain.ts +113 -0
  257. package/src/secrets/list.ts +16 -6
  258. package/src/secrets/manager.ts +24 -10
  259. package/src/secrets/read.ts +257 -0
  260. package/src/secrets/who-reads.ts +49 -0
  261. package/src/serve/door-forward.ts +268 -0
  262. package/src/serve/door-names.ts +47 -0
  263. package/src/serve/embed.ts +1 -1
  264. package/src/serve/hub-app/activity-view.tsx +56 -32
  265. package/src/serve/hub-app/activity.css +7 -1
  266. package/src/serve/hub-app/asks-page.tsx +48 -0
  267. package/src/serve/hub-app/asks-st.ts +25 -0
  268. package/src/serve/hub-app/clock-svc.ts +1 -2
  269. package/src/serve/hub-app/col-card.tsx +64 -13
  270. package/src/serve/hub-app/col-list-page.tsx +122 -0
  271. package/src/serve/hub-app/col-list-st.ts +103 -0
  272. package/src/serve/hub-app/col-tables-view.tsx +10 -41
  273. package/src/serve/hub-app/col-tables.ts +76 -8
  274. package/src/serve/hub-app/cols-svc.ts +70 -16
  275. package/src/serve/hub-app/database-st.ts +260 -0
  276. package/src/serve/hub-app/database-view.tsx +289 -0
  277. package/src/serve/hub-app/database.css +32 -0
  278. package/src/serve/hub-app/entry-bar.css +14 -0
  279. package/src/serve/hub-app/entry-bar.tsx +33 -0
  280. package/src/serve/hub-app/entry-pick.ts +31 -0
  281. package/src/serve/hub-app/floating.tsx +1 -0
  282. package/src/serve/hub-app/hash-route.ts +11 -1
  283. package/src/serve/hub-app/header-bar.tsx +14 -18
  284. package/src/serve/hub-app/home-st.ts +89 -9
  285. package/src/serve/hub-app/home-strip.css +5 -19
  286. package/src/serve/hub-app/home-strip.tsx +58 -92
  287. package/src/serve/hub-app/home.html +7 -0
  288. package/src/serve/hub-app/hub-app.tsx +31 -142
  289. package/src/serve/hub-app/hub-types.ts +3 -1
  290. package/src/serve/hub-app/hub.css +109 -23
  291. package/src/serve/hub-app/icons.tsx +18 -1
  292. package/src/serve/hub-app/identities-st.ts +56 -6
  293. package/src/serve/hub-app/identities-view.tsx +100 -58
  294. package/src/serve/hub-app/identities.css +17 -15
  295. package/src/serve/hub-app/identity-address.ts +5 -0
  296. package/src/serve/hub-app/inbox-st.ts +41 -24
  297. package/src/serve/hub-app/inbox-view.tsx +37 -41
  298. package/src/serve/hub-app/inbox.css +2 -5
  299. package/src/serve/hub-app/index.html +7 -0
  300. package/src/serve/hub-app/kept-cols.tsx +7 -0
  301. package/src/serve/hub-app/keys.ts +28 -15
  302. package/src/serve/hub-app/later.tsx +26 -0
  303. package/src/serve/hub-app/list-filter.tsx +14 -0
  304. package/src/serve/hub-app/list-walk.ts +72 -3
  305. package/src/serve/hub-app/machines-st.ts +251 -0
  306. package/src/serve/hub-app/machines-view.tsx +224 -0
  307. package/src/serve/hub-app/machines.css +76 -0
  308. package/src/serve/hub-app/md-copy.tsx +79 -0
  309. package/src/serve/hub-app/media-view.tsx +2 -0
  310. package/src/serve/hub-app/messages/messages-page.tsx +79 -7
  311. package/src/serve/hub-app/messages/messages-st.ts +99 -3
  312. package/src/serve/hub-app/messages.css +14 -0
  313. package/src/serve/hub-app/page-keys.ts +175 -0
  314. package/src/serve/hub-app/pages-frames.tsx +11 -3
  315. package/src/serve/hub-app/palette-st.ts +58 -11
  316. package/src/serve/hub-app/palette-view.tsx +3 -1
  317. package/src/serve/hub-app/project-list.css +54 -8
  318. package/src/serve/hub-app/project-list.tsx +128 -59
  319. package/src/serve/hub-app/projects-svc.ts +78 -7
  320. package/src/serve/hub-app/reader-chips.tsx +13 -0
  321. package/src/serve/hub-app/readme-view.tsx +2 -3
  322. package/src/serve/hub-app/readme.css +27 -5
  323. package/src/serve/hub-app/reflections-page.tsx +42 -0
  324. package/src/serve/hub-app/reflections-st.ts +17 -0
  325. package/src/serve/hub-app/releases/releases-page.tsx +25 -2
  326. package/src/serve/hub-app/releases/releases-st.ts +1 -1
  327. package/src/serve/hub-app/repo-sections.ts +9 -5
  328. package/src/serve/hub-app/repo-st.ts +35 -8
  329. package/src/serve/hub-app/review/draft-card.tsx +163 -0
  330. package/src/serve/hub-app/review/kit.tsx +50 -7
  331. package/src/serve/hub-app/review/note-place.ts +1 -1
  332. package/src/serve/hub-app/review/review-board.tsx +2 -2
  333. package/src/serve/hub-app/review/review-cells.tsx +145 -65
  334. package/src/serve/hub-app/review/review-kit.ts +27 -1
  335. package/src/serve/hub-app/review/review-list.tsx +68 -30
  336. package/src/serve/hub-app/review/review-page.tsx +7 -8
  337. package/src/serve/hub-app/review/review-st.ts +111 -24
  338. package/src/serve/hub-app/review/review-thread.tsx +49 -19
  339. package/src/serve/hub-app/review/review-views.tsx +475 -144
  340. package/src/serve/hub-app/review/thread-control-st.ts +115 -0
  341. package/src/serve/hub-app/review/thread-control.tsx +74 -0
  342. package/src/serve/hub-app/review/thread-st.ts +73 -26
  343. package/src/serve/hub-app/review/trailer-card.tsx +1 -1
  344. package/src/serve/hub-app/review/trailer-page.tsx +37 -16
  345. package/src/serve/hub-app/review/trailer-st.ts +41 -7
  346. package/src/serve/hub-app/review.css +267 -61
  347. package/src/serve/hub-app/side-lists.ts +72 -0
  348. package/src/serve/hub-app/sidebar.tsx +6 -3
  349. package/src/serve/hub-app/site/site-part.tsx +2 -8
  350. package/src/serve/hub-app/site/site-st.ts +4 -4
  351. package/src/serve/hub-app/social/comms-page.tsx +20 -5
  352. package/src/serve/hub-app/social/kit.tsx +30 -4
  353. package/src/serve/hub-app/social/post-card.tsx +139 -49
  354. package/src/serve/hub-app/social/serial-writes.ts +13 -0
  355. package/src/serve/hub-app/social/social-model.ts +11 -0
  356. package/src/serve/hub-app/social/social-page.tsx +94 -35
  357. package/src/serve/hub-app/social/social-st.ts +120 -36
  358. package/src/serve/hub-app/social/stage-popup.tsx +1 -1
  359. package/src/serve/hub-app/social/stage-st.ts +1 -1
  360. package/src/serve/hub-app/social/step-layout.ts +1 -1
  361. package/src/serve/hub-app/social.css +3 -0
  362. package/src/serve/hub-app/space-st.ts +209 -0
  363. package/src/serve/hub-app/space-view.tsx +196 -0
  364. package/src/serve/hub-app/space.css +27 -0
  365. package/src/serve/hub-app/stores/stores-page.tsx +91 -23
  366. package/src/serve/hub-app/stores/stores-st.ts +44 -19
  367. package/src/serve/hub-app/stores.css +6 -1
  368. package/src/serve/hub-app/theme-st.ts +22 -9
  369. package/src/serve/hub-app/time-charts.tsx +291 -0
  370. package/src/serve/hub-app/time-pages.tsx +749 -0
  371. package/src/serve/hub-app/time-st.ts +162 -0
  372. package/src/serve/hub-app/time-view.tsx +76 -0
  373. package/src/serve/hub-app/time.css +138 -0
  374. package/src/serve/hub-app/tree-build.ts +52 -42
  375. package/src/serve/hub-app/tree-row.tsx +4 -4
  376. package/src/serve/hub-app/tree-svc.ts +18 -9
  377. package/src/serve/hub-app/urls-svc.ts +154 -0
  378. package/src/serve/hub-app/urls-view.tsx +117 -0
  379. package/src/serve/hub-app/urls.css +30 -0
  380. package/src/serve/hub-app/vault-st.ts +127 -0
  381. package/src/serve/hub-app/vault-view.tsx +154 -0
  382. package/src/serve/hub-app/vault.css +38 -0
  383. package/src/serve/hub-app/veille/veille-st.ts +12 -10
  384. package/src/serve/hub-app/waiting-svc.ts +29 -0
  385. package/src/serve/hub-app/wikis-view.tsx +2 -2
  386. package/src/serve/hub-runtime.ts +15 -2
  387. package/src/serve/project-gutter-route.ts +27 -14
  388. package/src/serve/project-gutter.ts +25 -12
  389. package/src/serve/same-origin.ts +10 -0
  390. package/src/serve/serve-hub.ts +13 -4
  391. package/src/shipkit-files.ts +16 -2
  392. package/src/shipkit.ts +1 -1
  393. package/src/shot/shooter.ts +616 -420
  394. package/src/shot/shot-ask.ts +246 -155
  395. package/src/site/desk.ts +2 -2
  396. package/src/smoke/run-smoke.ts +3 -2
  397. package/src/social/cover.ts +202 -0
  398. package/src/social/discord-layout.ts +37 -13
  399. package/src/social/drafts.ts +3 -0
  400. package/src/social/load.ts +23 -2
  401. package/src/social/media-kind.ts +2 -1
  402. package/src/social/publish/discord.ts +5 -2
  403. package/src/social/publish/secret.ts +2 -2
  404. package/src/social/release-stage.ts +28 -13
  405. package/src/social/routes.ts +26 -1
  406. package/src/social/stage.ts +1 -1
  407. package/src/social/stats.ts +12 -9
  408. package/src/social/strip.ts +4 -1
  409. package/src/social/thumbnail.ts +255 -0
  410. package/src/stores/collection.ts +15 -11
  411. package/src/stores/desk.ts +236 -42
  412. package/src/stores/model.ts +72 -6
  413. package/src/stores/routes.ts +37 -12
  414. package/src/stores/unit.ts +14 -8
  415. package/src/suite/judge-suites.ts +113 -16
  416. package/src/suite/select.ts +45 -3
  417. package/src/suite/suite-cli.ts +81 -34
  418. package/src/trailer/bed.ts +1 -1
  419. package/src/trailer/build.ts +134 -21
  420. package/src/trailer/ffmpeg.ts +7 -0
  421. package/src/trailer/fonts.ts +3 -2
  422. package/src/trailer/grammar.ts +201 -2
  423. package/src/trailer/graph.ts +42 -5
  424. package/src/trailer/look.ts +21 -0
  425. package/src/trailer/mock.ts +154 -0
  426. package/src/trailer/passage.ts +65 -26
  427. package/src/trailer/score/heard.ts +89 -0
  428. package/src/trailer/score/sound.ts +14 -8
  429. package/src/trailer/sound/run.ts +16 -6
  430. package/src/trailer/stitch.ts +26 -5
  431. package/src/trailer/titles.ts +82 -19
  432. package/src/trailer/versions.ts +1 -3
  433. package/src/trailer/vertical.ts +3 -1
  434. package/src/urls/frame.ts +195 -0
  435. package/src/urls/routes.ts +43 -0
  436. package/src/urls/rows.ts +171 -0
  437. package/src/urls/unit.ts +73 -0
  438. package/src/vault/house-keys.ts +42 -0
  439. package/src/vault/layout.ts +50 -0
  440. package/src/vault/policy.ts +165 -0
  441. package/src/vault/vault-audit.ts +276 -0
  442. package/src/vault/vault-client.ts +40 -0
  443. package/src/vault/vault-config.ts +159 -0
  444. package/src/vault/vault-destroy.ts +78 -0
  445. package/src/vault/vault-handover.ts +305 -0
  446. package/src/vault/vault-http.ts +352 -0
  447. package/src/vault/vault-import.ts +208 -0
  448. package/src/vault/vault-init.ts +328 -0
  449. package/src/vault/vault-minter.ts +271 -0
  450. package/src/vault/vault-restore.ts +333 -0
  451. package/src/vault/vault-serve.ts +342 -0
  452. package/src/vault/vault-snapshot.ts +281 -0
  453. package/src/veille/asks.ts +1 -1
  454. package/src/veille/desk.ts +2 -2
  455. package/src/wiki/run-wiki.ts +2 -1
  456. package/src/wiki/scan-md.ts +42 -2
@@ -0,0 +1,599 @@
1
+ // what the hq holds and decides, over one database handle and with no http in it (wiki tools/shipkit/hq.md): who
2
+ // joined, what each node may see, the rows the daemons send, and the feed of the rows that go back down. A row is
3
+ // applied once: a node's rows carry a sequence number of its outbox (`epoch`), and the hq takes only what is above
4
+ // the highest it holds. The feed's numbers only grow inside one generation: an hq that went back in time (a restore
5
+ // of an older copy) starts another, and every daemon reads it again from 0
6
+ import type { Database } from 'bun:sqlite'
7
+ import { createHash, randomBytes } from 'crypto'
8
+ import { isRepoKey, nameProblem } from '../core/scope.ts'
9
+ import { SLOTS } from '../vault/policy.ts'
10
+ import type { ForgottenLog } from './forgotten-log.ts'
11
+ import { HQ_SCHEMA_VERSION, recountUsage } from './hq-db.ts'
12
+ import { KINDS, kindOf } from './kinds/index.ts'
13
+ import { maskFields, secretName, type Asked, type Lack, type ReadPage } from './kinds/kind.ts'
14
+ import { machineLabel } from '../serve/door-forward.ts'
15
+ import type { Report } from './report.ts'
16
+ import { afarRefusal, type Wanted } from './switch.ts'
17
+
18
+ export type Grant = { kind: 'all' } | { kind: 'repo' | 'identity' | 'machine'; name: string }
19
+
20
+ /** why these grants cannot be an invite, null when they can: a repo is named by its key (host/path of its first
21
+ * remote), every name is a safe part of a vault path, and no more of a kind than the vault's node template carries */
22
+ export function grantsProblem(grants: unknown[]): string | null {
23
+ if (grants.length === 0) return 'an invite names what the daemon may see: all, or repos, identities and machines'
24
+ const seen: Record<string, number> = {}
25
+ for (const g of grants) {
26
+ const kind = (g as { kind?: unknown } | null)?.kind
27
+ const name = (g as { name?: unknown } | null)?.name
28
+ if (kind === 'all') continue
29
+ if (kind !== 'repo' && kind !== 'identity' && kind !== 'machine') return `no grant kind ${JSON.stringify(kind)}: all, repo, identity or machine`
30
+ if (typeof name !== 'string') return `a grant of kind ${kind} names what it opens`
31
+ const problem = nameProblem(kind, name)
32
+ if (problem != null) return problem
33
+ if (kind === 'repo' && !isRepoKey(name)) return `repo:${name} names a folder: a repo is granted by its key, the host and path of its first remote (git.example.org/studio/${name})`
34
+ seen[kind] = (seen[kind] ?? 0) + 1
35
+ if (seen[kind]! > SLOTS[kind]) return `a node holds at most ${SLOTS[kind]} grants of the kind ${kind}`
36
+ }
37
+ return null
38
+ }
39
+ export type HqNode = { id: string; machine: string; osUser: string; person: string; joined: number; ended: number | null; epoch: string; applied: number; seen: number }
40
+ /** one row of a daemon's outbox, as it travels */
41
+ export type RowIn = { seq: number; kind: string; at: number; body: unknown }
42
+ export type Refused = { refused: string }
43
+ /** one entry of the feed as a daemon gets it: its number, its kind, its time and its row's body */
44
+ export type FeedRow = { n: number; kind: string; at: number; body: unknown }
45
+ /** a page of the feed: the generation, the highest number the hq gave, where the next page starts, and the rows of
46
+ * the caller's grant between. `next` reaches `top` once nothing is left */
47
+ export type FeedPage = { gen: string; top: number; next: number; rows: FeedRow[] }
48
+ /** the highest feed number this hq's file ever gave, kept OUTSIDE the file (the machine db of the machine that
49
+ * carries it), by the file's name: a file found under it is an older copy, and its feed takes a new generation */
50
+ export type FeedMark = { get(hq: string): number | null; set(hq: string, n: number): void }
51
+ export type Caps = { maxBytes?: number; maxPerNode?: number; maxBytesPerNode?: number }
52
+ /** `forgotten`: the rows forgotten for good, kept outside the file (forgotten-log.ts), applied again at each open */
53
+ export type StoreOptions = { mark: FeedMark; forgotten?: ForgottenLog } & Caps
54
+
55
+ /** how long a join code is good for */
56
+ export const INVITE_MS = 10 * 60_000
57
+ /** the most one call may carry: a daemon that was away sends the rest in the next ones */
58
+ export const MAX_ROWS = 200
59
+ /** one row's body, in bytes: a report of a hundred repos is about 12 kB */
60
+ export const MAX_ROW_BYTES = 64 * 1024
61
+ /** the rows of one call, in bytes: an outbox cuts a call there, and always sends one row */
62
+ export const MAX_CALL_ROW_BYTES = MAX_ROWS * MAX_ROW_BYTES
63
+ /** the body of one call of rows: a full call and one more row of the largest kind, with their envelope */
64
+ export const MAX_BODY_BYTES = Math.ceil((MAX_CALL_ROW_BYTES + Math.max(MAX_ROW_BYTES, ...KINDS.map((k) => k.maxRowBytes ?? 0))) * 1.25)
65
+ /** the hq's file may be this big, in pages it uses: past it the hq sweeps, and if that makes no room it takes no row
66
+ * of any kind, the cards' included, and the outboxes hold them. Each kind gives one node a share (kinds/): the
67
+ * prompts 160 MB, the day rows 24, the reports 40, the cards, their messages and tombstones 144, so 368 MB a node,
68
+ * and two nodes at their shares fit under it (hq-store.test.ts says so) */
69
+ export const HQ_MAX_BYTES = 800 * 1024 * 1024
70
+ /** the most rows one read gives, and the most names one ask of what the hq lacks */
71
+ export const READ_MAX_ROWS = 2_000
72
+ /** the hq sweeps its own rows this often, as rows land: it is the one writer of its file. At its cap, once a minute */
73
+ export const SWEEP_EVERY_MS = 3600_000
74
+ const SWEEP_AT_CAP_MS = 60_000
75
+
76
+ const sha = (s: string): string => createHash('sha256').update(s).digest('hex')
77
+ const mint = (bytes: number): string => randomBytes(bytes).toString('base64url')
78
+
79
+ type NodeSql = { id: string; machine: string; os_user: string; person: string; joined: number; ended: number | null; epoch: string; applied: number; seen: number }
80
+ const node = (r: NodeSql): HqNode => ({ id: r.id, machine: r.machine, osUser: r.os_user, person: r.person, joined: r.joined, ended: r.ended, epoch: r.epoch, applied: r.applied, seen: r.seen })
81
+ const NODE_COLS = 'id, machine, os_user, person, joined, ended, epoch, applied, seen'
82
+
83
+ const grantRows = (g: Grant[]): { kind: string; name: string }[] => g.map((x) => ({ kind: x.kind, name: x.kind === 'all' ? '' : x.name }))
84
+
85
+ export type MachineView = {
86
+ id: string
87
+ name: string
88
+ /** `wanted`: the switch values the hq's console asked of it that its reports do not show yet */
89
+ nodes: { id: string; osUser: string; joined: number; ended: number | null; seen: number; report: { at: number; body: Report } | null; wanted?: Wanted[] }[]
90
+ }
91
+
92
+ export class HqStore {
93
+ private sweptAt = 0
94
+ private maxBytes: number
95
+ private caps: Caps
96
+ private mark: FeedMark
97
+ /** the file's name, made once: a restore keeps it, so the mark of this hq is found again */
98
+ readonly name: string
99
+
100
+ /** the mark of the feed is the machine's (hq-local.ts); a test gives one in memory. The caps replace the file's
101
+ * cap and every kind's share of one node, for a test */
102
+ private forgotten: ForgottenLog | null
103
+ constructor(readonly db: Database, o: StoreOptions) {
104
+ this.caps = o
105
+ this.mark = o.mark
106
+ this.forgotten = o.forgotten ?? null
107
+ this.maxBytes = o.maxBytes ?? HQ_MAX_BYTES
108
+ this.name = this.word('name', () => mint(9))
109
+ const mark = this.mark.get(this.name)
110
+ const top = this.feedTop()
111
+ // no mark here (a first open, a copy opened on another machine) or a file under its mark (an older copy): the
112
+ // numbers above the file's top may already be passed by a daemon, so they start a new generation
113
+ if (mark == null || top < mark) this.setWord('gen', mint(9))
114
+ this.mark.set(this.name, top)
115
+ // a restore of an older copy holds neither a forgotten row nor its tombstone: both are made again
116
+ const gone = this.forgotten?.all() ?? []
117
+ if (gone.length > 0)
118
+ this.db.transaction(() => {
119
+ let held = false
120
+ for (const g of gone) held = (kindOf(g.kind)?.forget?.(this.db, g.name, Date.now()) ?? false) || held
121
+ if (held) recountUsage(this.db)
122
+ }).immediate()
123
+ }
124
+
125
+ private word(name: string, make: () => string): string {
126
+ const held = this.db.query('select value from meta where name = ?').get(name) as { value: string } | null
127
+ if (held != null) return held.value
128
+ this.db.query('insert or ignore into meta (name, value) values (?, ?)').run(name, make())
129
+ return (this.db.query('select value from meta where name = ?').get(name) as { value: string }).value
130
+ }
131
+
132
+ private setWord(name: string, value: string): void {
133
+ this.db.query('insert into meta (name, value) values (?, ?) on conflict (name) do update set value = excluded.value').run(name, value)
134
+ }
135
+
136
+ /** the feed's generation: a daemon that read another one reads again from 0 */
137
+ gen(): string {
138
+ return this.word('gen', () => mint(9))
139
+ }
140
+
141
+ /** the highest number the feed ever gave, 0 for none: a removed entry keeps its number given */
142
+ feedTop(): number {
143
+ return (this.db.query("select seq from sqlite_sequence where name = 'feed'").get() as { seq: number } | null)?.seq ?? 0
144
+ }
145
+
146
+ /** the size of the pages the file uses, with no walk: a page a delete freed is room again */
147
+ bytes(): number {
148
+ const n = (name: string): number => Number(Object.values(this.db.query(`pragma ${name}`).get() as Record<string, number>)[0])
149
+ return (n('page_count') - n('freelist_count')) * n('page_size')
150
+ }
151
+
152
+ /** is the file at its cap, after a sweep when it looks so */
153
+ private full(now: number): boolean {
154
+ if (this.bytes() < this.maxBytes) return false
155
+ if (now - this.sweptAt >= SWEEP_AT_CAP_MS) this.sweep(now)
156
+ return this.bytes() >= this.maxBytes
157
+ }
158
+
159
+ /** the person of that name, made at the first ask: one row on an hq with one owner */
160
+ person(name: string, now: number): string {
161
+ const held = this.db.query('select id from people where name = ?').get(name) as { id: string } | null
162
+ if (held != null) return held.id
163
+ const id = mint(9)
164
+ this.db.query('insert into people (id, name, at) values (?, ?, ?)').run(id, name, now)
165
+ return id
166
+ }
167
+
168
+ /** a join code, good once and until `now + ms`: only its hash is kept. `on`: the key of a machine the join may add
169
+ * a node to (a second OS user of it); with none, the join must name a machine the hq holds no live node of */
170
+ invite(o: { person: string; grants: Grant[]; now: number; ms?: number; on?: string }): string {
171
+ const code = mint(12)
172
+ this.db.query('insert into invites (code_hash, person, grants, expires, used, on_machine) values (?, ?, ?, ?, null, ?)').run(sha(code), o.person, JSON.stringify(grantRows(o.grants)), o.now + (o.ms ?? INVITE_MS), o.on ?? null)
173
+ return code
174
+ }
175
+
176
+ /** why a join may not name this machine: its key holds a live node the invite does not name, or another machine
177
+ * the hq holds has its name (one dns label: a door is named by it). Null when it may */
178
+ private machineTaken(key: string, name: string, on: string | null, live: string | null): string | null {
179
+ const held = this.db.query('select 1 from nodes where machine = ? and ended is null and id <> ?').get(key, live ?? '') != null
180
+ if (held && on !== key) return 'a machine of that key holds a live node already: a join adds a node to it only when the invite names it (sk hq invite --on <machine>)'
181
+ const label = machineLabel(name)
182
+ if (label === '') return 'a join names its machine by a name with a letter or a digit'
183
+ // every machine row, its nodes live or ended: a door's origin is a machine's name, and the browser keeps what that
184
+ // origin stored, so a name is never handed to another machine while the hq holds the row
185
+ const twin = (this.db.query('select id, name from machines where id <> ?').all(key) as { id: string; name: string }[]).find((m) => machineLabel(m.name) === label)
186
+ return twin == null ? null : `another machine of the hq is named ${label} already: a join takes no name of another machine, even one whose daemons all ended`
187
+ }
188
+
189
+ /** a daemon joins with a code: its machine's row, its node, its grant, and a token said once. What a join says
190
+ * of its machine is a claim, so it never touches a node or a machine that exists: it makes a new node, under
191
+ * the name the machine already has. Only a daemon that shows the token of its live node (`held`) keeps that
192
+ * node, with a new token, the new grant and its machine's new name */
193
+ join(o: { code: string; machine: { key: string; name: string }; osUser: string; now: number; held?: string }): { node: string; token: string } | Refused {
194
+ if (o.machine.key === '' || o.osUser === '') return { refused: 'a join names its machine and its OS user' }
195
+ const token = mint(32)
196
+ let out: { node: string; token: string } | Refused = { refused: 'this code is unknown, used or past its time: ask for a new one on the hq (sk hq invite)' }
197
+ this.db.transaction(() => {
198
+ const inv = this.db.query('select person, grants, on_machine from invites where code_hash = ? and used is null and expires > ?').get(sha(o.code), o.now) as { person: string; grants: string; on_machine: string | null } | null
199
+ if (inv == null) return
200
+ const mine = o.held == null || o.held === '' ? null : this.nodeOf(o.held)
201
+ const live = mine != null && mine.machine === o.machine.key && mine.osUser === o.osUser && mine.person === inv.person ? mine : null
202
+ // the name the machine will hold: a node added to a machine keeps that machine's name, a live one renames it
203
+ const had = this.db.query('select name from machines where id = ?').get(o.machine.key) as { name: string } | null
204
+ const taken = this.machineTaken(o.machine.key, live == null && had != null ? had.name : o.machine.name, inv.on_machine, live?.id ?? null)
205
+ if (taken != null) {
206
+ out = { refused: taken }
207
+ return
208
+ }
209
+ this.db.query('update invites set used = ? where code_hash = ?').run(o.now, sha(o.code))
210
+ this.db.query(`insert into machines (id, name, at) values (?, ?, ?) on conflict(id) do ${live == null ? 'nothing' : 'update set name = excluded.name'}`).run(o.machine.key, o.machine.name, o.now)
211
+ const id = live?.id ?? mint(9)
212
+ if (live == null) this.db.query('insert into nodes (id, machine, os_user, person, token_hash, joined) values (?, ?, ?, ?, ?, ?)').run(id, o.machine.key, o.osUser, inv.person, sha(token), o.now)
213
+ else this.db.query('update nodes set token_hash = ? where id = ?').run(sha(token), id)
214
+ this.db.query('delete from grants where node = ?').run(id)
215
+ for (const g of JSON.parse(inv.grants) as { kind: string; name: string }[]) this.db.query('insert or ignore into grants (node, kind, name, at) values (?, ?, ?, ?)').run(id, g.kind, g.name, o.now)
216
+ out = { node: id, token }
217
+ }).immediate()
218
+ return out
219
+ }
220
+
221
+ /** the live node a token names, null for an unknown or an ended one */
222
+ nodeOf(token: string): HqNode | null {
223
+ if (token === '') return null
224
+ const r = this.db.query(`select ${NODE_COLS} from nodes where token_hash = ? and ended is null`).get(sha(token)) as NodeSql | null
225
+ return r == null ? null : node(r)
226
+ }
227
+
228
+ /** end every live node of one machine and OS user, for the daemon that carries this hq and lost its token: it
229
+ * writes the file itself, so it needs no proof. Never reached from a route */
230
+ endNodesOf(machine: string, osUser: string, now: number, except = ''): number {
231
+ return this.db.query('update nodes set ended = ? where machine = ? and os_user = ? and ended is null and id <> ?').run(now, machine, osUser, except).changes
232
+ }
233
+
234
+ /** a retired machine forgotten by the hq's console, never by a route a token opens: its row and its ended nodes
235
+ * (their grants, outboxes, wanted values, counts), so its name, and the door's name of it, are free again. What its
236
+ * nodes sent stays (cards, prompts, media, reports). Refused for the hq's own machine (`own`), for a machine with a
237
+ * live node, and while a vault token of its nodes waits to be revoked. `go` false: a look, nothing changed */
238
+ forgetMachine(o: { machine: string; own: string; go: boolean }): { go: boolean; machine: string; name: string; door: string; nodes: number; keeps: Record<string, number> } | (Refused & { status: number }) {
239
+ const m = this.db.query('select id, name from machines where id = ?1 or name = ?1').all(o.machine) as { id: string; name: string }[]
240
+ if (m.length === 0) return { refused: `the hq knows no machine ${JSON.stringify(o.machine).slice(0, 80)}`, status: 404 }
241
+ if (m.length > 1) return { refused: `${m.length} machines are named ${JSON.stringify(o.machine).slice(0, 80)}: name it by its id (sk machines --json)`, status: 409 }
242
+ const { id, name } = m[0]!
243
+ if (id === o.own) return { refused: 'this is the machine that carries the hq: it is never forgotten', status: 409 }
244
+ const nodes = (this.db.query('select id, ended from nodes where machine = ?').all(id) as { id: string; ended: number | null }[])
245
+ const alive = nodes.filter((n) => n.ended == null).length
246
+ if (alive > 0) return { refused: `${name} has ${alive} live ${alive === 1 ? 'node' : 'nodes'}: end ${alive === 1 ? 'it' : 'them'} first (sk hq end <node>)`, status: 409 }
247
+ const ids = new Set(nodes.map((n) => n.id))
248
+ const tokens = (this.db.query('select node from vault_tokens').all() as { node: string }[]).filter((t) => ids.has(t.node)).length
249
+ if (tokens > 0) return { refused: `a vault token of ${name} waits to be revoked: the next vault check revokes it, then forget it`, status: 409 }
250
+ const keeps: Record<string, number> = {}
251
+ for (const k of KINDS) {
252
+ const rows = k.usage(this.db).filter((u) => ids.has(u.node)).reduce((a, u) => a + u.rows, 0)
253
+ if (rows > 0) keeps[k.kind] = rows
254
+ }
255
+ const out = { go: o.go, machine: id, name, door: machineLabel(name), nodes: nodes.length, keeps }
256
+ if (!o.go) return out
257
+ this.db.transaction(() => {
258
+ for (const n of ids) for (const t of ['grants', 'outboxes', 'wanted', 'usage']) this.db.query(`delete from ${t} where node = ?`).run(n)
259
+ this.db.query('delete from nodes where machine = ?').run(id)
260
+ this.db.query('delete from machines where id = ?').run(id)
261
+ }).immediate()
262
+ return out
263
+ }
264
+
265
+ /** end a node: its token is refused from now on. False when no live node has that id */
266
+ end(id: string, now: number): boolean {
267
+ return this.db.query('update nodes set ended = ? where id = ? and ended is null').run(now, id).changes > 0
268
+ }
269
+
270
+ /** a row of a kind this code does not know opens nothing: never read as `all` */
271
+ grantsOf(id: string): Grant[] {
272
+ const rows = this.db.query('select kind, name from grants where node = ? order by kind, name').all(id) as { kind: string; name: string }[]
273
+ return rows.flatMap((r): Grant[] => (r.kind === 'all' ? [{ kind: 'all' }] : r.kind === 'repo' || r.kind === 'identity' || r.kind === 'machine' ? [{ kind: r.kind, name: r.name }] : []))
274
+ }
275
+
276
+ /** a node by its id, ended or not */
277
+ nodeById(id: string): HqNode | null {
278
+ const r = this.db.query(`select ${NODE_COLS} from nodes where id = ?`).get(id) as NodeSql | null
279
+ return r == null ? null : node(r)
280
+ }
281
+
282
+ /** one grant more for a live node: true when it did not hold it */
283
+ addGrant(id: string, g: Grant, now: number): boolean {
284
+ const [row] = grantRows([g])
285
+ return this.db.query('insert or ignore into grants (node, kind, name, at) select ?, ?, ?, ? where exists (select 1 from nodes where id = ? and ended is null)').run(id, row!.kind, row!.name, now, id).changes > 0
286
+ }
287
+
288
+ /** does this node see everything */
289
+ seesAll(id: string): boolean {
290
+ return this.db.query("select 1 from grants where node = ? and kind = 'all'").get(id) != null
291
+ }
292
+
293
+ /** may this node see that repo or identity: the grant `all`, or a grant that names it exactly */
294
+ sees(id: string, kind: 'repo' | 'identity', name: string): boolean {
295
+ return this.db.query("select 1 from grants where node = ? and (kind = 'all' or (kind = ? and name = ?))").get(id, kind, name) != null
296
+ }
297
+
298
+ /** the entries of the feed above `after` that this node may see, in order, each with its row's body. A node with
299
+ * the grant `all` sees every entry and the hq's top; any other an entry whose repo one of its repo grants names
300
+ * exactly, no entry of no repo, and the top of those entries. An entry whose row went is passed */
301
+ feed(viewer: string, after: number, limit: number): FeedPage {
302
+ const lim = Math.min(MAX_ROWS, Math.max(0, Math.floor(limit) || 0))
303
+ const from = Math.max(0, Math.floor(after) || 0)
304
+ const all = this.seesAll(viewer)
305
+ // a narrow node is told the highest number it may see, never the hq's: another repo's activity is not its own
306
+ const top = all ? this.feedTop() : ((this.db.query("select max(n) as n from feed where repo in (select name from grants where node = ? and kind = 'repo')").get(viewer) as { n: number | null }).n ?? 0)
307
+ const entries = (
308
+ lim === 0
309
+ ? []
310
+ : all
311
+ ? this.db.query('select n, kind, name, at from feed where n > ? order by n limit ?').all(from, lim)
312
+ : this.db.query("select n, kind, name, at from feed where n > ? and repo in (select name from grants where node = ? and kind = 'repo') order by n limit ?").all(from, viewer, lim)
313
+ ) as { n: number; kind: string; name: string; at: number }[]
314
+ const rows: FeedRow[] = []
315
+ for (const e of entries) {
316
+ const text = kindOf(e.kind)?.feed?.read(this.db, e.name)
317
+ if (text != null) rows.push({ n: e.n, kind: e.kind, at: e.at, body: JSON.parse(text) as unknown })
318
+ }
319
+ // a short page read every entry up to the top; a full one goes on from its last entry, read or passed. A
320
+ // page of no row asked is the generation and the top alone
321
+ const next = lim === 0 ? from : entries.length < lim ? Math.max(from, top) : entries.at(-1)!.n
322
+ return { gen: this.gen(), top, next, rows }
323
+ }
324
+
325
+ /** every name and rev the hq holds of one repo, per kind that feeds, for a node that may see that repo */
326
+ revs(viewer: string, repo: string): { revs: Record<string, { name: string; rev: string }[]> } | Refused {
327
+ if (repo === '' || !this.sees(viewer, 'repo', repo)) return { refused: `this node's grant does not name the repo ${JSON.stringify(repo)}` }
328
+ return { revs: Object.fromEntries(KINDS.flatMap((k) => (k.feed == null ? [] : [[k.kind, k.feed.revs(this.db, repo)]]))) }
329
+ }
330
+
331
+ /** the highest row the hq holds of this outbox: what the daemon sends from. An outbox the hq never saw is 0. A
332
+ * node has one outbox a group of kinds, each its own name */
333
+ applied(id: string, epoch: string): number {
334
+ return (this.db.query('select applied from outboxes where node = ? and epoch = ?').get(id, epoch) as { applied: number } | null)?.applied ?? 0
335
+ }
336
+
337
+ /** take a daemon's rows, each once and in order, all or none: a row at or under the highest held is one the hq
338
+ * already has. A kind this hq does not know refuses the call, so the rows wait in the outbox for an hq that
339
+ * does, and so does a row that cannot be stored. A row of a repo the node's grant does not name is never
340
+ * stored: it is passed and counted in `dropped`, so a grant made narrower never holds the outbox for good. A
341
+ * row's time is the daemon's, never later than now. The file and each node have a cap, so no node grows the hq
342
+ * without bound. A row of a kind that feeds moves its feed entry to the top */
343
+ apply(id: string, epoch: string, rows: RowIn[], now: number): { applied: number; dropped: number; kept?: number; keptRows?: number[]; why?: string[] } | Refused {
344
+ if (epoch === '') return { refused: 'rows name the outbox they come from' }
345
+ if (rows.length > MAX_ROWS) return { refused: `at most ${MAX_ROWS} rows a call` }
346
+ if (this.full(now)) return { refused: `the hq's file is at its cap (${Math.round(this.maxBytes / 1e6)} MB) and its sweep made no room: it takes no row` }
347
+ let out: { applied: number; dropped: number; kept?: number; keptRows?: number[]; why?: string[] } | Refused = { applied: 0, dropped: 0 }
348
+ const fed = this.feedTop()
349
+ try {
350
+ this.db.transaction(() => {
351
+ let top = this.applied(id, epoch)
352
+ let dropped = 0
353
+ const why: string[] = []
354
+ const all = this.seesAll(id)
355
+ const use = new Map<string, { rows: number; bytes: number }>()
356
+ const usageOf = (kind: string): { rows: number; bytes: number } => {
357
+ let u = use.get(kind)
358
+ if (u == null) use.set(kind, (u = (this.db.query('select rows, bytes from usage where node = ? and kind = ?').get(id, kind) as { rows: number; bytes: number } | null) ?? { rows: 0, bytes: 0 }))
359
+ return u
360
+ }
361
+ const keptRows: number[] = []
362
+ const callBytes = new Map<string, number>()
363
+ // a row's own defect: passed, said and named by its number, so one row never holds the rows behind it
364
+ // and its daemon never counts it as taken
365
+ const keep = (seq: number, line: string): void => {
366
+ keptRows.push(seq)
367
+ if (why.length < 20) why.push(`${line}: kept out`)
368
+ top = seq
369
+ }
370
+ for (let r of rows.filter((x) => x != null && Number.isSafeInteger(x.seq)).sort((a, b) => a.seq - b.seq)) {
371
+ if (r.seq <= top) continue
372
+ const k = typeof r.kind === 'string' ? kindOf(r.kind) : null
373
+ // a kind this hq does not know waits for an hq that does
374
+ if (k == null) throw new Error(`this hq (schema ${HQ_SCHEMA_VERSION}) does not know a row of kind ${JSON.stringify(r.kind)}: update the hq`)
375
+ if (!Number.isSafeInteger(r.at) || r.at <= 0) {
376
+ keep(r.seq, `row ${r.seq} has no time`)
377
+ continue
378
+ }
379
+ const text = JSON.stringify(r.body ?? null)
380
+ const bytes = Buffer.byteLength(text)
381
+ const max = k.maxRowBytes ?? MAX_ROW_BYTES
382
+ if (bytes > max) {
383
+ keep(r.seq, `row ${r.seq} is over ${max} bytes`)
384
+ continue
385
+ }
386
+ // the call's budget of this kind: the rows past it wait for the next call, the first one always goes
387
+ const spent = callBytes.get(k.kind) ?? 0
388
+ if (k.maxCallBytes != null && spent > 0 && spent + bytes > k.maxCallBytes) break
389
+ callBytes.set(k.kind, spent + bytes)
390
+ const named = k.names == null ? null : secretName(r.body, k.names)
391
+ if (named != null) {
392
+ keep(r.seq, `row ${r.seq}, its ${named} has the shape of a secret`)
393
+ continue
394
+ }
395
+ if (k.texts != null) r = { ...r, body: maskFields(r.body, k.texts) }
396
+ if (!k.check(r.body)) {
397
+ keep(r.seq, `row ${r.seq} is no ${k.kind}`)
398
+ continue
399
+ }
400
+ const repo = k.repoOf(r.body)
401
+ if (repo !== '' && !all && !this.sees(id, 'repo', repo)) {
402
+ dropped++
403
+ top = r.seq
404
+ continue
405
+ }
406
+ const refused = k.refuses?.(this.db, r.body, { node: id, now }) ?? null
407
+ if (refused != null) {
408
+ // said in `why`, never counted as a row of a repo outside the grant
409
+ if (why.length < 20) why.push(refused)
410
+ top = r.seq
411
+ continue
412
+ }
413
+ const later = k.keeps?.(this.db, r.body) ?? null
414
+ if (later != null) {
415
+ keep(r.seq, `row ${r.seq}, ${later}`)
416
+ continue
417
+ }
418
+ const u = usageOf(k.kind)
419
+ const at = Math.min(r.at, now)
420
+ // the count moves by what the row changed: a head rewritten is one row, its bytes the latest head's.
421
+ // Past a share the call is refused, and the transaction takes the insert back
422
+ const added = k.insert(this.db, { node: id, epoch, seq: r.seq, at, body: r.body, text: k.texts == null ? text : JSON.stringify(r.body) })
423
+ if (added != null) {
424
+ if (added.rows > 0 && u.rows + added.rows > (this.caps.maxPerNode ?? k.maxPerNode)) throw new Error(`this node holds ${u.rows} rows of the kind ${k.kind}, its cap: the hq takes no more from it until its old ones are swept`)
425
+ if (added.bytes > 0 && u.bytes + added.bytes > (this.caps.maxBytesPerNode ?? k.maxBytesPerNode)) throw new Error(`this node holds ${u.bytes} bytes of the kind ${k.kind}, its cap: the hq takes no more from it until its old ones are swept`)
426
+ u.rows += added.rows
427
+ u.bytes += added.bytes
428
+ if (k.feed != null) {
429
+ const name = k.feed.nameOf(r.body)
430
+ this.db.query('delete from feed where kind = ? and name = ?').run(k.kind, name)
431
+ this.db.query('insert into feed (kind, repo, name, at) values (?, ?, ?, ?)').run(k.kind, repo, name, at)
432
+ }
433
+ }
434
+ top = r.seq
435
+ }
436
+ for (const [kind, u] of use) this.db.query('insert into usage (node, kind, rows, bytes) values (?, ?, ?, ?) on conflict (node, kind) do update set rows = excluded.rows, bytes = excluded.bytes').run(id, kind, u.rows, u.bytes)
437
+ this.db.query('insert into outboxes (node, epoch, applied) values (?, ?, ?) on conflict (node, epoch) do update set applied = excluded.applied').run(id, epoch, top)
438
+ this.db.query('update nodes set seen = ? where id = ?').run(now, id)
439
+ out = { applied: top, dropped, ...(keptRows.length > 0 ? { kept: keptRows.length, keptRows } : {}), ...(why.length > 0 ? { why } : {}) }
440
+ }).immediate()
441
+ } catch (e) {
442
+ return { refused: (e as Error).message }
443
+ }
444
+ // the mark after the commit: a crash between the two leaves the file ahead of its mark, which is no restore
445
+ const top = this.feedTop()
446
+ if (top > fed) this.mark.set(this.name, top)
447
+ if (now - this.sweptAt >= SWEEP_EVERY_MS) this.sweep(now)
448
+ return out
449
+ }
450
+
451
+ /** the repos a node reads: null for the grant `all`, else the ones its repo grants name, maybe none */
452
+ private readable(viewer: string): string[] | null {
453
+ if (this.seesAll(viewer)) return null
454
+ return (this.db.query("select name from grants where node = ? and kind = 'repo' order by name").all(viewer) as { name: string }[]).map((g) => g.name)
455
+ }
456
+
457
+ /** a page of the rows of one kind a node may read: every repo for the grant `all`, its repos for a repo grant,
458
+ * nothing for an identity grant. `repo` narrows it to one repo, which the grant must name; `node` to the rows of
459
+ * one node */
460
+ read(viewer: string, kind: string, q: { repo?: string; node?: string; since?: number; after?: string; limit?: number; ids?: boolean; name?: string }): ReadPage | Refused {
461
+ const k = kindOf(kind)
462
+ if (k?.read == null) return { refused: `this hq reads no rows of kind ${JSON.stringify(kind)}` }
463
+ const repos = this.readable(viewer)
464
+ if (repos != null && repos.length === 0) return { refused: "this node's grant names no repo: it reads no row" }
465
+ if (q.repo != null && q.repo !== '' && repos != null && !repos.includes(q.repo)) return { refused: `this node's grant does not name the repo ${JSON.stringify(q.repo)}` }
466
+ const limit = Math.min(READ_MAX_ROWS, Math.max(1, Math.floor(q.limit ?? READ_MAX_ROWS) || READ_MAX_ROWS))
467
+ const since = Number.isFinite(q.since) ? Number(q.since) : 0
468
+ return k.read.page(this.db, { repos: q.repo != null && q.repo !== '' ? [q.repo] : repos, node: q.node != null && q.node !== '' ? q.node : null, since, after: typeof q.after === 'string' ? q.after.slice(0, 512) : '', limit, ids: q.ids === true, ...(typeof q.name === 'string' && q.name !== '' ? { name: q.name.slice(0, 512) } : {}) })
469
+ }
470
+
471
+ /** of these rows of one kind, each a name and the rev the node holds, the ones the hq does not hold as this node
472
+ * does, in a repo it reads, and why */
473
+ lacks(viewer: string, kind: string, asked: unknown[]): { lacks: Lack[] } | Refused {
474
+ const k = kindOf(kind)
475
+ if (k?.read == null) return { refused: `this hq reads no rows of kind ${JSON.stringify(kind)}` }
476
+ if (asked.length > READ_MAX_ROWS) return { refused: `at most ${READ_MAX_ROWS} names an ask` }
477
+ const repos = this.readable(viewer)
478
+ if (repos != null && repos.length === 0) return { refused: "this node's grant names no repo: it reads no row" }
479
+ const ok = (v: unknown): v is Asked => v != null && typeof v === 'object' && typeof (v as Asked).name === 'string' && (v as Asked).name.length <= 512 && ((v as Asked).rev === undefined || (typeof (v as Asked).rev === 'string' && (v as Asked).rev!.length <= 128))
480
+ return { lacks: k.read.lacks(this.db, asked.filter(ok).map((a) => (a.rev === undefined ? { name: a.name } : { name: a.name, rev: a.rev })), repos, viewer) }
481
+ }
482
+
483
+ /** how many rows of one kind the hq holds in the repos a node reads, of one node or all, since a time */
484
+ count(viewer: string, kind: string, q: { node?: string; since?: number; forgotten?: boolean }): { count: number } | Refused {
485
+ const k = kindOf(kind)
486
+ if (k?.read == null) return { refused: `this hq reads no rows of kind ${JSON.stringify(kind)}` }
487
+ const repos = this.readable(viewer)
488
+ if (repos != null && repos.length === 0) return { refused: "this node's grant names no repo: it reads no row" }
489
+ return { count: k.read.count(this.db, { repos, node: q.node != null && q.node !== '' ? q.node : null, since: Number.isFinite(q.since) ? Number(q.since) : 0, forgotten: q.forgotten === true }) }
490
+ }
491
+
492
+ /** rows a node sent, forgotten by that node: only the rows held from it, so no node takes out another's. How many */
493
+ forgetOwn(viewer: string, kind: string, names: readonly string[], now: number): { forgotten: number } | Refused {
494
+ const k = kindOf(kind)
495
+ if (k?.forget == null) return { refused: `this hq forgets no row of kind ${JSON.stringify(kind)}` }
496
+ if (names.length > READ_MAX_ROWS) return { refused: `at most ${READ_MAX_ROWS} names a call` }
497
+ const table = k.tables?.[0]
498
+ // a node forgets the rows it sent by their id: a kind whose rows have none is forgotten on the hq's console only
499
+ if (table == null || this.db.query("select 1 from pragma_table_info(?) where name = 'id'").get(table) == null) return { refused: `a node forgets no row of kind ${JSON.stringify(kind)}: the hq's console does (sk hq)` }
500
+ let n = 0
501
+ this.db.transaction(() => {
502
+ const own = this.db.query(`select 1 from ${table} where id = ? and node = ?`)
503
+ for (const name of names)
504
+ if (typeof name === 'string' && own.get(name, viewer) != null && k.forget!(this.db, name, now)) {
505
+ n++
506
+ this.forgotten?.add(kind, name)
507
+ }
508
+ if (n > 0) recountUsage(this.db)
509
+ }).immediate()
510
+ return { forgotten: n }
511
+ }
512
+
513
+ /** a row kept for good removed, by the hq's console: never reached from a route a token opens */
514
+ forget(kind: string, name: string, now: number): { held: boolean } | Refused {
515
+ const k = kindOf(kind)
516
+ if (k?.forget == null) return { refused: `this hq forgets no row of kind ${JSON.stringify(kind)}` }
517
+ let held = false
518
+ this.db.transaction(() => {
519
+ held = k.forget!(this.db, name, now)
520
+ if (held) recountUsage(this.db)
521
+ }).immediate()
522
+ this.forgotten?.add(kind, name)
523
+ return { held }
524
+ }
525
+
526
+ /** every machine, its nodes and each node's newest report, for a viewer with the grant `all` and for the hq's
527
+ * own console (`null`). Any other viewer sees its own node and nothing of another machine: what a narrow
528
+ * grant shows of the others is decided when a step needs it */
529
+ machines(viewer: string | null): MachineView[] {
530
+ const all = viewer == null || this.seesAll(viewer)
531
+ const nodes = (this.db.query(`select ${NODE_COLS} from nodes order by joined, os_user, id`).all() as NodeSql[]).filter((n) => all || n.id === viewer)
532
+ const machines = (this.db.query('select id, name from machines order by name, id').all() as { id: string; name: string }[]).filter((m) => nodes.some((n) => n.machine === m.id))
533
+ const newest = this.db.query('select at, body from reports where node = ? order by at desc, seq desc limit 1')
534
+ return machines.map((m) => ({
535
+ id: m.id,
536
+ name: m.name,
537
+ nodes: nodes
538
+ .filter((n) => n.machine === m.id)
539
+ .map((n) => {
540
+ const r = newest.get(n.id) as { at: number; body: string } | null
541
+ const body = r == null ? null : (JSON.parse(r.body) as Report)
542
+ const wanted = n.ended == null ? this.wantedOf(n.id) : []
543
+ return { id: n.id, osUser: n.os_user, joined: n.joined, ended: n.ended, seen: n.seen, report: r == null || body == null ? null : { at: r.at, body }, ...(wanted.length > 0 ? { wanted } : {}) }
544
+ }),
545
+ }))
546
+ }
547
+
548
+ /** a node's free disk over time, oldest first, at most `points` of them */
549
+ diskLine(id: string, since: number, points = 120): { at: number; free: number }[] {
550
+ const rows = this.db.query("select at, json_extract(body, '$.disk.free') as free from reports where node = ? and at >= ? order by at").all(id, since) as { at: number; free: number | null }[]
551
+ const kept = rows.filter((r): r is { at: number; free: number } => typeof r.free === 'number')
552
+ const step = Math.max(1, Math.ceil(kept.length / points))
553
+ return kept.filter((_, i) => i % step === 0 || i === kept.length - 1)
554
+ }
555
+
556
+ /** the retention of the hq's own rows: each kind's (kinds/), then a code that was used or is past its time goes
557
+ * after a day. The count a node is set right from the tables */
558
+ sweep(now: number): { rows: number; invites: number } {
559
+ this.sweptAt = now
560
+ let rows = 0
561
+ this.db.transaction(() => {
562
+ for (const k of KINDS) rows += k.sweep(this.db, now)
563
+ recountUsage(this.db)
564
+ }).immediate()
565
+ const invites = this.db.query('delete from invites where (used is not null and used < ?1) or expires < ?1').run(now - 24 * 3600_000).changes
566
+ return { rows, invites }
567
+ }
568
+
569
+ /** a switch value for every live node of one machine with the grant all (its id or its name), from the hq's console alone: never
570
+ * reached from a route a token opens. The switch must be one this code declares settable from afar; the node's
571
+ * own code decides again when it applies it */
572
+ want(machine: string, name: string, on: boolean, now: number): { nodes: string[] } | Refused {
573
+ const refused = afarRefusal(name, on)
574
+ if (refused != null) return { refused }
575
+ const m = this.db.query('select id from machines where id = ?1 or name = ?1').all(machine) as { id: string }[]
576
+ if (m.length !== 1) return { refused: m.length === 0 ? `the hq knows no machine ${JSON.stringify(machine)}` : `${m.length} machines are named ${JSON.stringify(machine)}: name it by its id (sk machines --json)` }
577
+ // the owner's daemons of that machine (the grant all): another OS user's narrow daemon there is not his to set
578
+ const nodes = (this.db.query("select id from nodes where machine = ? and ended is null and id in (select node from grants where kind = 'all') order by id").all(m[0]!.id) as { id: string }[]).map((n) => n.id)
579
+ if (nodes.length === 0) return { refused: `the machine ${JSON.stringify(machine)} has no live daemon with the grant all` }
580
+ this.db.transaction(() => {
581
+ for (const n of nodes) this.db.query('insert into wanted (node, name, value, at, done) values (?, ?, ?, ?, null) on conflict (node, name) do update set value = excluded.value, at = excluded.at, done = null').run(n, name, on ? 1 : 0, now)
582
+ }).immediate()
583
+ return { nodes }
584
+ }
585
+
586
+ /** the switch values the hq's console wants this node to take and its reports do not show yet: its own only */
587
+ wantedOf(node: string): Wanted[] {
588
+ return (this.db.query('select name, value, at from wanted where node = ? and done is null order by name').all(node) as { name: string; value: number; at: number }[]).map((w) => ({ name: w.name, on: w.value === 1, at: w.at }))
589
+ }
590
+
591
+ /** every wanted value, done or not, for the console and the machines page */
592
+ wantedAll(): { node: string; name: string; on: boolean; at: number; done: number | null }[] {
593
+ return (this.db.query('select node, name, value, at, done from wanted order by node, name').all() as { node: string; name: string; value: number; at: number; done: number | null }[]).map((w) => ({ node: w.node, name: w.name, on: w.value === 1, at: w.at, done: w.done }))
594
+ }
595
+
596
+ counts(): { table: string; rows: number }[] {
597
+ return ['people', 'machines', 'nodes', 'grants', 'invites', 'usage', 'feed', 'wanted', ...KINDS.flatMap((k) => k.tables)].map((table) => ({ table, rows: (this.db.query(`select count(*) as n from ${table}`).get() as { n: number }).n }))
598
+ }
599
+ }