rvlib-shipkit 2.26.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 (442) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/package.json +1 -1
  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 +6 -4
  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 +122 -21
  11. package/src/agent-time/report-cache.ts +57 -0
  12. package/src/agent-time/report.ts +314 -40
  13. package/src/agent-time/shape.ts +50 -3
  14. package/src/agent-time/store.ts +320 -99
  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/SK047-login-in-description.check.ts +36 -0
  34. package/src/checks/SK048-one-local-file.check.ts +36 -0
  35. package/src/cli/cmd_Asks.ts +71 -5
  36. package/src/cli/cmd_Backup.ts +2 -2
  37. package/src/cli/cmd_Capacity.ts +2 -2
  38. package/src/cli/cmd_CrossRepoActivity.ts +5 -4
  39. package/src/cli/cmd_Db.ts +253 -0
  40. package/src/cli/cmd_History.ts +212 -0
  41. package/src/cli/cmd_Home.ts +31 -3
  42. package/src/cli/cmd_Hq.ts +516 -0
  43. package/src/cli/cmd_Machines.ts +36 -0
  44. package/src/cli/cmd_Media.ts +38 -6
  45. package/src/cli/cmd_Reflect.ts +23 -2
  46. package/src/cli/cmd_Review.ts +548 -116
  47. package/src/cli/cmd_Secret.ts +62 -12
  48. package/src/cli/cmd_Social.ts +83 -1
  49. package/src/cli/cmd_Space.ts +324 -0
  50. package/src/cli/cmd_Time.ts +76 -5
  51. package/src/cli/cmd_Tmp.ts +28 -4
  52. package/src/cli/cmd_Trailer.ts +14 -10
  53. package/src/cli/cmd_Urls.ts +56 -0
  54. package/src/cli/cmd_Vault.ts +583 -0
  55. package/src/cli/family-help.ts +4 -2
  56. package/src/cli/family-pages.ts +1 -1
  57. package/src/cli/registry.ts +24 -3
  58. package/src/collections/actions.ts +63 -13
  59. package/src/collections/collection-types.ts +23 -1
  60. package/src/collections/providers.ts +117 -23
  61. package/src/collections/review.ts +4 -4
  62. package/src/contracts/contract-manifests.ts +6 -5
  63. package/src/contracts/contract-types.ts +6 -4
  64. package/src/contracts/contracts-engine.ts +11 -0
  65. package/src/contracts/manifests/chrome-web-store.contract.ts +31 -0
  66. package/src/core/budget.ts +12 -17
  67. package/src/core/capacity.ts +27 -18
  68. package/src/core/check-runner.ts +7 -4
  69. package/src/core/layout.ts +3 -0
  70. package/src/core/machine-busy.ts +127 -0
  71. package/src/core/machine-commit.ts +6 -1
  72. package/src/core/machine-db.ts +368 -20
  73. package/src/core/machine-layout.ts +137 -0
  74. package/src/core/machine-read.ts +81 -0
  75. package/src/core/migrate-layout.ts +1 -1
  76. package/src/core/one-way-in-policy.ts +64 -7
  77. package/src/core/polling-policy.ts +27 -0
  78. package/src/core/retention.ts +16 -2
  79. package/src/core/scope.ts +107 -14
  80. package/src/core/slots.ts +5 -4
  81. package/src/drill/clean.ts +25 -1
  82. package/src/drill/run.ts +1 -1
  83. package/src/drill/seal.ts +27 -4
  84. package/src/home/activity-shape.ts +2 -0
  85. package/src/home/activity.ts +66 -3
  86. package/src/home/app-sign.ts +15 -4
  87. package/src/home/daemon-jobs.ts +149 -7
  88. package/src/home/database-projects.ts +111 -0
  89. package/src/home/database-words.ts +203 -0
  90. package/src/home/databases.ts +460 -0
  91. package/src/home/global-units.ts +6 -0
  92. package/src/home/gui-app/ShipkitGui.swift +122 -1
  93. package/src/home/gui-app.ts +6 -3
  94. package/src/home/home-move.ts +514 -0
  95. package/src/home/home-paths.ts +12 -3
  96. package/src/home/home-routes.ts +68 -0
  97. package/src/home/inbox-shape.ts +24 -14
  98. package/src/home/machine-fold.ts +820 -0
  99. package/src/home/machine-host.ts +20 -5
  100. package/src/home/machine-repair.ts +503 -0
  101. package/src/home/machine-stores.ts +66 -15
  102. package/src/home/machine-temp.ts +547 -86
  103. package/src/home/resume.ts +79 -0
  104. package/src/home/run-daemon.ts +25 -4
  105. package/src/home/run-pm.ts +18 -5
  106. package/src/home/serve-daemon.ts +18 -3
  107. package/src/home/serve-web.ts +175 -7
  108. package/src/home/space-job.ts +115 -0
  109. package/src/home/space-page.ts +257 -0
  110. package/src/home/space-route.ts +167 -0
  111. package/src/home/space-words.ts +103 -0
  112. package/src/home/time-page-shape.ts +202 -0
  113. package/src/home/time-page.ts +64 -0
  114. package/src/home/vault-page.ts +482 -0
  115. package/src/home/vault-words.ts +132 -0
  116. package/src/hooks/leak-guard.ts +40 -13
  117. package/src/hooks/one-way-in-guard.ts +17 -4
  118. package/src/hq/cards.ts +581 -0
  119. package/src/hq/door-route.ts +43 -0
  120. package/src/hq/door.ts +163 -0
  121. package/src/hq/files-send.ts +348 -0
  122. package/src/hq/forgotten-log.ts +35 -0
  123. package/src/hq/history-check.ts +255 -0
  124. package/src/hq/history-place.ts +146 -0
  125. package/src/hq/history-send.ts +487 -0
  126. package/src/hq/hq-db.ts +99 -0
  127. package/src/hq/hq-local.ts +469 -0
  128. package/src/hq/hq-routes.ts +196 -0
  129. package/src/hq/hq-store.ts +599 -0
  130. package/src/hq/kinds/ask.ts +147 -0
  131. package/src/hq/kinds/card-gone.ts +83 -0
  132. package/src/hq/kinds/card-msg.ts +66 -0
  133. package/src/hq/kinds/card.ts +99 -0
  134. package/src/hq/kinds/index.ts +21 -0
  135. package/src/hq/kinds/kind.ts +126 -0
  136. package/src/hq/kinds/media.ts +125 -0
  137. package/src/hq/kinds/prompt.ts +219 -0
  138. package/src/hq/kinds/reflection.ts +128 -0
  139. package/src/hq/kinds/report.ts +55 -0
  140. package/src/hq/kinds/time-day.ts +236 -0
  141. package/src/hq/ledger.ts +473 -0
  142. package/src/hq/machine-key.ts +44 -0
  143. package/src/hq/machine-notes.ts +131 -0
  144. package/src/hq/outbox.ts +372 -0
  145. package/src/hq/replica.ts +168 -0
  146. package/src/hq/repo-id.ts +4 -0
  147. package/src/hq/report-facts.ts +117 -0
  148. package/src/hq/report.ts +274 -0
  149. package/src/hq/switch.ts +151 -0
  150. package/src/hq/vault-grant.ts +92 -0
  151. package/src/identities/identities.ts +15 -3
  152. package/src/identities/steps.ts +31 -0
  153. package/src/ledger/db.ts +73 -26
  154. package/src/ledger/gate-checks.ts +6 -2
  155. package/src/ledger/run-log.ts +11 -3
  156. package/src/ledger/store.ts +26 -4
  157. package/src/media-store/backup/daemon.ts +25 -17
  158. package/src/media-store/backup/kept.ts +241 -0
  159. package/src/media-store/backup/restore.ts +14 -4
  160. package/src/media-store/backup/run.ts +11 -11
  161. package/src/media-store/backup/secret.ts +6 -4
  162. package/src/media-store/backup/snapshot.ts +4 -2
  163. package/src/media-store/backup/sources.ts +67 -16
  164. package/src/media-store/copy/config.ts +77 -0
  165. package/src/media-store/copy/copy.ts +194 -0
  166. package/src/media-store/copy/lifecycle.ts +43 -0
  167. package/src/media-store/copy/night.ts +90 -0
  168. package/src/media-store/copy/proof.ts +151 -0
  169. package/src/media-store/copy/snapshot.ts +152 -0
  170. package/src/media-store/copy/store.ts +62 -0
  171. package/src/media-store/copy/stream.ts +176 -0
  172. package/src/media-store/daemon.ts +66 -16
  173. package/src/media-store/db.ts +22 -11
  174. package/src/media-store/forget.ts +23 -3
  175. package/src/media-store/hand.ts +4 -3
  176. package/src/media-store/hq-find.ts +75 -0
  177. package/src/media-store/hq-send.ts +277 -0
  178. package/src/media-store/kind.ts +2 -0
  179. package/src/media-store/purge.ts +18 -15
  180. package/src/media-store/put.ts +24 -8
  181. package/src/media-store/remote.ts +16 -7
  182. package/src/media-store/space-listing.ts +240 -0
  183. package/src/media-store/space.ts +40 -21
  184. package/src/media-store/sync.ts +5 -5
  185. package/src/media-store/tidy.ts +98 -0
  186. package/src/media-store/upload.ts +3 -3
  187. package/src/media-store/webp.ts +66 -0
  188. package/src/messages/model.ts +16 -7
  189. package/src/messages/routes.ts +14 -4
  190. package/src/messages/score.ts +2 -2
  191. package/src/messages/suggest.ts +96 -0
  192. package/src/pages/frame-client.ts +10 -0
  193. package/src/plan/planner.ts +28 -17
  194. package/src/recordings/media-kind.ts +1 -0
  195. package/src/recordings/model.ts +1 -1
  196. package/src/recordings/recorder-app/Capture.swift +9 -7
  197. package/src/reflect/hq-corpus.ts +56 -0
  198. package/src/reflect/prompt-corpus.ts +3 -1
  199. package/src/reflect/transcript.ts +129 -37
  200. package/src/release/machine-words.ts +67 -0
  201. package/src/release/public-clean.ts +8 -3
  202. package/src/releases/from-card.ts +1 -1
  203. package/src/releases/routes.ts +10 -5
  204. package/src/releases/store.ts +10 -1
  205. package/src/repo-config.ts +32 -2
  206. package/src/review/anim.ts +2 -2
  207. package/src/review/auto.ts +2 -2
  208. package/src/review/card-db.ts +244 -0
  209. package/src/review/card-rows.ts +266 -0
  210. package/src/review/cells.ts +76 -5
  211. package/src/review/draft-card.ts +41 -0
  212. package/src/review/draft-model.ts +78 -0
  213. package/src/review/format.ts +29 -10
  214. package/src/review/gone.ts +98 -0
  215. package/src/review/inbox-hook.ts +4 -4
  216. package/src/review/marks.ts +1 -1
  217. package/src/review/media-kind.ts +7 -5
  218. package/src/review/media-read.ts +9 -7
  219. package/src/review/media.ts +32 -5
  220. package/src/review/model.ts +202 -39
  221. package/src/review/move.ts +491 -0
  222. package/src/review/notes.ts +12 -20
  223. package/src/review/previews.ts +15 -19
  224. package/src/review/project.ts +1 -1
  225. package/src/review/rev.ts +8 -0
  226. package/src/review/role.ts +1 -1
  227. package/src/review/routes.ts +64 -13
  228. package/src/review/stage-card.ts +8 -4
  229. package/src/review/stage-routes.ts +1 -1
  230. package/src/review/store.ts +529 -83
  231. package/src/review/thread-fold.ts +51 -0
  232. package/src/review/threads.ts +147 -9
  233. package/src/review/trailer-auto-mock.ts +63 -0
  234. package/src/review/trailer-card.ts +4 -4
  235. package/src/review/trailer-desk.ts +41 -7
  236. package/src/review/trailer-media.ts +1 -0
  237. package/src/review/trailer-model.ts +8 -0
  238. package/src/review/trailer-notes.ts +1 -1
  239. package/src/review/trailer-routes.ts +9 -1
  240. package/src/review/unit.ts +6 -6
  241. package/src/review/watchers.ts +1 -1
  242. package/src/rules/untrack-generated.ts +92 -8
  243. package/src/secrets/held.ts +16 -0
  244. package/src/secrets/keychain.ts +113 -0
  245. package/src/secrets/list.ts +16 -6
  246. package/src/secrets/manager.ts +24 -10
  247. package/src/secrets/read.ts +257 -0
  248. package/src/secrets/who-reads.ts +49 -0
  249. package/src/serve/door-forward.ts +268 -0
  250. package/src/serve/door-names.ts +47 -0
  251. package/src/serve/embed.ts +1 -1
  252. package/src/serve/hub-app/activity-view.tsx +56 -32
  253. package/src/serve/hub-app/activity.css +7 -1
  254. package/src/serve/hub-app/asks-page.tsx +48 -0
  255. package/src/serve/hub-app/asks-st.ts +25 -0
  256. package/src/serve/hub-app/clock-svc.ts +1 -2
  257. package/src/serve/hub-app/col-card.tsx +64 -13
  258. package/src/serve/hub-app/col-list-page.tsx +122 -0
  259. package/src/serve/hub-app/col-list-st.ts +103 -0
  260. package/src/serve/hub-app/col-tables-view.tsx +10 -41
  261. package/src/serve/hub-app/col-tables.ts +76 -8
  262. package/src/serve/hub-app/cols-svc.ts +70 -16
  263. package/src/serve/hub-app/database-st.ts +260 -0
  264. package/src/serve/hub-app/database-view.tsx +289 -0
  265. package/src/serve/hub-app/database.css +32 -0
  266. package/src/serve/hub-app/entry-bar.css +14 -0
  267. package/src/serve/hub-app/entry-bar.tsx +33 -0
  268. package/src/serve/hub-app/entry-pick.ts +31 -0
  269. package/src/serve/hub-app/floating.tsx +1 -0
  270. package/src/serve/hub-app/hash-route.ts +11 -1
  271. package/src/serve/hub-app/header-bar.tsx +14 -18
  272. package/src/serve/hub-app/home-st.ts +89 -9
  273. package/src/serve/hub-app/home-strip.css +5 -19
  274. package/src/serve/hub-app/home-strip.tsx +58 -92
  275. package/src/serve/hub-app/home.html +7 -0
  276. package/src/serve/hub-app/hub-app.tsx +31 -142
  277. package/src/serve/hub-app/hub-types.ts +3 -1
  278. package/src/serve/hub-app/hub.css +109 -23
  279. package/src/serve/hub-app/icons.tsx +18 -1
  280. package/src/serve/hub-app/identities-st.ts +56 -6
  281. package/src/serve/hub-app/identities-view.tsx +100 -58
  282. package/src/serve/hub-app/identities.css +17 -15
  283. package/src/serve/hub-app/identity-address.ts +5 -0
  284. package/src/serve/hub-app/inbox-st.ts +41 -24
  285. package/src/serve/hub-app/inbox-view.tsx +37 -41
  286. package/src/serve/hub-app/inbox.css +2 -5
  287. package/src/serve/hub-app/index.html +7 -0
  288. package/src/serve/hub-app/kept-cols.tsx +7 -0
  289. package/src/serve/hub-app/keys.ts +28 -15
  290. package/src/serve/hub-app/later.tsx +26 -0
  291. package/src/serve/hub-app/list-filter.tsx +14 -0
  292. package/src/serve/hub-app/list-walk.ts +72 -3
  293. package/src/serve/hub-app/machines-st.ts +251 -0
  294. package/src/serve/hub-app/machines-view.tsx +224 -0
  295. package/src/serve/hub-app/machines.css +76 -0
  296. package/src/serve/hub-app/md-copy.tsx +79 -0
  297. package/src/serve/hub-app/media-view.tsx +2 -0
  298. package/src/serve/hub-app/messages/messages-page.tsx +79 -7
  299. package/src/serve/hub-app/messages/messages-st.ts +99 -3
  300. package/src/serve/hub-app/messages.css +14 -0
  301. package/src/serve/hub-app/page-keys.ts +175 -0
  302. package/src/serve/hub-app/pages-frames.tsx +11 -3
  303. package/src/serve/hub-app/palette-st.ts +58 -11
  304. package/src/serve/hub-app/palette-view.tsx +3 -1
  305. package/src/serve/hub-app/project-list.css +54 -8
  306. package/src/serve/hub-app/project-list.tsx +128 -59
  307. package/src/serve/hub-app/projects-svc.ts +78 -7
  308. package/src/serve/hub-app/reader-chips.tsx +13 -0
  309. package/src/serve/hub-app/readme-view.tsx +2 -3
  310. package/src/serve/hub-app/readme.css +27 -5
  311. package/src/serve/hub-app/reflections-page.tsx +42 -0
  312. package/src/serve/hub-app/reflections-st.ts +17 -0
  313. package/src/serve/hub-app/releases/releases-page.tsx +25 -2
  314. package/src/serve/hub-app/releases/releases-st.ts +1 -1
  315. package/src/serve/hub-app/repo-sections.ts +9 -5
  316. package/src/serve/hub-app/repo-st.ts +35 -8
  317. package/src/serve/hub-app/review/draft-card.tsx +163 -0
  318. package/src/serve/hub-app/review/kit.tsx +50 -7
  319. package/src/serve/hub-app/review/note-place.ts +1 -1
  320. package/src/serve/hub-app/review/review-board.tsx +2 -2
  321. package/src/serve/hub-app/review/review-cells.tsx +145 -65
  322. package/src/serve/hub-app/review/review-kit.ts +27 -1
  323. package/src/serve/hub-app/review/review-list.tsx +68 -30
  324. package/src/serve/hub-app/review/review-page.tsx +7 -8
  325. package/src/serve/hub-app/review/review-st.ts +111 -24
  326. package/src/serve/hub-app/review/review-thread.tsx +49 -19
  327. package/src/serve/hub-app/review/review-views.tsx +475 -144
  328. package/src/serve/hub-app/review/thread-control-st.ts +115 -0
  329. package/src/serve/hub-app/review/thread-control.tsx +74 -0
  330. package/src/serve/hub-app/review/thread-st.ts +73 -26
  331. package/src/serve/hub-app/review/trailer-card.tsx +1 -1
  332. package/src/serve/hub-app/review/trailer-page.tsx +37 -16
  333. package/src/serve/hub-app/review/trailer-st.ts +41 -7
  334. package/src/serve/hub-app/review.css +267 -61
  335. package/src/serve/hub-app/side-lists.ts +72 -0
  336. package/src/serve/hub-app/sidebar.tsx +6 -3
  337. package/src/serve/hub-app/site/site-part.tsx +2 -8
  338. package/src/serve/hub-app/site/site-st.ts +4 -4
  339. package/src/serve/hub-app/social/comms-page.tsx +20 -5
  340. package/src/serve/hub-app/social/kit.tsx +30 -4
  341. package/src/serve/hub-app/social/post-card.tsx +139 -49
  342. package/src/serve/hub-app/social/serial-writes.ts +13 -0
  343. package/src/serve/hub-app/social/social-model.ts +11 -0
  344. package/src/serve/hub-app/social/social-page.tsx +94 -35
  345. package/src/serve/hub-app/social/social-st.ts +120 -36
  346. package/src/serve/hub-app/social/stage-popup.tsx +1 -1
  347. package/src/serve/hub-app/social/stage-st.ts +1 -1
  348. package/src/serve/hub-app/social/step-layout.ts +1 -1
  349. package/src/serve/hub-app/social.css +3 -0
  350. package/src/serve/hub-app/space-st.ts +209 -0
  351. package/src/serve/hub-app/space-view.tsx +196 -0
  352. package/src/serve/hub-app/space.css +27 -0
  353. package/src/serve/hub-app/stores/stores-page.tsx +91 -23
  354. package/src/serve/hub-app/stores/stores-st.ts +44 -19
  355. package/src/serve/hub-app/stores.css +6 -1
  356. package/src/serve/hub-app/theme-st.ts +22 -9
  357. package/src/serve/hub-app/time-charts.tsx +291 -0
  358. package/src/serve/hub-app/time-pages.tsx +749 -0
  359. package/src/serve/hub-app/time-st.ts +162 -0
  360. package/src/serve/hub-app/time-view.tsx +76 -0
  361. package/src/serve/hub-app/time.css +138 -0
  362. package/src/serve/hub-app/tree-build.ts +52 -42
  363. package/src/serve/hub-app/tree-row.tsx +4 -4
  364. package/src/serve/hub-app/tree-svc.ts +18 -9
  365. package/src/serve/hub-app/urls-svc.ts +154 -0
  366. package/src/serve/hub-app/urls-view.tsx +117 -0
  367. package/src/serve/hub-app/urls.css +30 -0
  368. package/src/serve/hub-app/vault-st.ts +127 -0
  369. package/src/serve/hub-app/vault-view.tsx +154 -0
  370. package/src/serve/hub-app/vault.css +38 -0
  371. package/src/serve/hub-app/veille/veille-st.ts +12 -10
  372. package/src/serve/hub-app/waiting-svc.ts +29 -0
  373. package/src/serve/hub-app/wikis-view.tsx +2 -2
  374. package/src/serve/hub-runtime.ts +15 -2
  375. package/src/serve/project-gutter-route.ts +27 -14
  376. package/src/serve/project-gutter.ts +25 -12
  377. package/src/serve/same-origin.ts +10 -0
  378. package/src/serve/serve-hub.ts +13 -4
  379. package/src/shipkit-files.ts +16 -2
  380. package/src/shipkit.ts +1 -1
  381. package/src/shot/shooter.ts +616 -420
  382. package/src/shot/shot-ask.ts +246 -155
  383. package/src/site/desk.ts +2 -2
  384. package/src/smoke/run-smoke.ts +3 -2
  385. package/src/social/cover.ts +202 -0
  386. package/src/social/discord-layout.ts +37 -13
  387. package/src/social/drafts.ts +3 -0
  388. package/src/social/load.ts +23 -2
  389. package/src/social/media-kind.ts +2 -1
  390. package/src/social/publish/discord.ts +5 -2
  391. package/src/social/publish/secret.ts +2 -2
  392. package/src/social/release-stage.ts +28 -13
  393. package/src/social/routes.ts +26 -1
  394. package/src/social/stage.ts +1 -1
  395. package/src/social/stats.ts +12 -9
  396. package/src/social/strip.ts +4 -1
  397. package/src/social/thumbnail.ts +255 -0
  398. package/src/stores/collection.ts +15 -11
  399. package/src/stores/desk.ts +236 -42
  400. package/src/stores/model.ts +72 -6
  401. package/src/stores/routes.ts +37 -12
  402. package/src/stores/unit.ts +14 -8
  403. package/src/suite/judge-suites.ts +41 -11
  404. package/src/suite/select.ts +45 -3
  405. package/src/suite/suite-cli.ts +16 -10
  406. package/src/trailer/bed.ts +1 -1
  407. package/src/trailer/build.ts +134 -21
  408. package/src/trailer/ffmpeg.ts +7 -0
  409. package/src/trailer/fonts.ts +3 -2
  410. package/src/trailer/grammar.ts +201 -2
  411. package/src/trailer/graph.ts +42 -5
  412. package/src/trailer/look.ts +21 -0
  413. package/src/trailer/mock.ts +154 -0
  414. package/src/trailer/passage.ts +65 -26
  415. package/src/trailer/score/heard.ts +89 -0
  416. package/src/trailer/score/sound.ts +14 -8
  417. package/src/trailer/sound/run.ts +16 -6
  418. package/src/trailer/stitch.ts +26 -5
  419. package/src/trailer/titles.ts +82 -19
  420. package/src/trailer/versions.ts +1 -3
  421. package/src/trailer/vertical.ts +3 -1
  422. package/src/urls/frame.ts +195 -0
  423. package/src/urls/routes.ts +43 -0
  424. package/src/urls/rows.ts +171 -0
  425. package/src/urls/unit.ts +73 -0
  426. package/src/vault/house-keys.ts +42 -0
  427. package/src/vault/layout.ts +50 -0
  428. package/src/vault/policy.ts +165 -0
  429. package/src/vault/vault-audit.ts +276 -0
  430. package/src/vault/vault-client.ts +40 -0
  431. package/src/vault/vault-config.ts +159 -0
  432. package/src/vault/vault-destroy.ts +78 -0
  433. package/src/vault/vault-handover.ts +305 -0
  434. package/src/vault/vault-http.ts +352 -0
  435. package/src/vault/vault-import.ts +208 -0
  436. package/src/vault/vault-init.ts +328 -0
  437. package/src/vault/vault-minter.ts +271 -0
  438. package/src/vault/vault-restore.ts +333 -0
  439. package/src/vault/vault-serve.ts +342 -0
  440. package/src/vault/vault-snapshot.ts +281 -0
  441. package/src/veille/asks.ts +1 -1
  442. package/src/veille/desk.ts +2 -2
@@ -0,0 +1,460 @@
1
+ // the machine's databases, for the database page and `sk db`: every sqlite file shipkit keeps on this machine, its
2
+ // size on disk, its pages, its tables with their rows and bytes, its cap, the copies kept before a migration, and for
3
+ // the hq its kinds against each node's share, its replica and its last restore drill.
4
+ //
5
+ // The constraint a reader breaks first: a count of every table and a dbstat pass take seconds on the big files (the
6
+ // media index, the agent time index), so they run in the daemon's job `db census`, in a child, which keeps them in the
7
+ // machine db with their time. An ask reads what it kept, a stat and four pragmas a file. Every file is opened read
8
+ // only and never made, and nothing here reads a row's content: names, counts and sizes only.
9
+ import { Database } from 'bun:sqlite'
10
+ import { existsSync, readdirSync, readFileSync, statSync } from 'fs'
11
+ import { homedir } from 'os'
12
+ import { basename, dirname, join } from 'path'
13
+ import { agentTimeDbPath } from '../agent-time/store.ts'
14
+ import { FOLDED_STORES, isTombstone, machineDbPath, machineGet, machineSet, oldStorePath } from '../core/machine-db.ts'
15
+ import { HQ_FILE_VERSION, HQ_SCHEMA_VERSION, hqDbPath } from '../hq/hq-db.ts'
16
+ import { HQ_MAX_BYTES } from '../hq/hq-store.ts'
17
+ import { KINDS } from '../hq/kinds/index.ts'
18
+ import { DAEMON_TABLES, daemonDbPath } from '../hq/outbox.ts'
19
+ import type { Drill } from '../hq/replica.ts'
20
+ import { yieldLoop } from '../core/loop-watch.ts'
21
+ import { dbPath } from '../ledger/db.ts'
22
+ import { mediaDbPath } from '../media-store/db.ts'
23
+ import { partOf } from './database-words.ts'
24
+ import { groupsOf, projectsOf, type Project, type ProjectView, type Split, type Unsplit } from './database-projects.ts'
25
+ import { DAEMON_DB_MAX_BYTES, MEDIA_INDEX_MAX_BYTES } from './machine-stores.ts'
26
+
27
+ export type DbKind = 'hq' | 'daemon' | 'machine' | 'media' | 'agent-time' | 'record'
28
+ /** what a database is to the machine: the truth (its loss is a loss), a cache (it builds again from its sources), or
29
+ * this machine's own state */
30
+ export type DbRole = 'truth' | 'cache' | 'machine'
31
+ /** one store inside a file that holds several (the machine db): its tables are those its `prefix` starts, the
32
+ * tables no other part names when null */
33
+ /** a part of a file of several stores: its tables are those its prefix starts, or `tables` named whole (a store whose
34
+ * tables keep their own names, as the daemon's file folded) */
35
+ export type DbPart = { id: string; kind: DbKind; name: string; prefix: string | null; tables?: readonly string[]; role: DbRole; roleWords: string; split: Split | null }
36
+ /** one database of the machine: `cap` in bytes of pages used, where shipkit gives one; `role` and its words say what
37
+ * deleting it costs; `split` how it names the repo of a row (database-projects.ts); `parts` the stores it holds */
38
+ export type DbSpec = { id: string; name: string; kind: DbKind; what: string; path: string; cap: number | null; repo: string | null; role: DbRole; roleWords: string; roleShort: string; split: Split | null; parts: DbPart[] | null }
39
+
40
+ /** what a stat and the header pragmas say, at each ask. `bytes` is the file and its write ahead log */
41
+ export type Live = { bytes: number; file: number; wal: number; changed: number; pageSize: number | null; pages: number | null; free: number | null; version: number | null; error: string | null }
42
+ /** a table of the last count: its rows (null when they do not read), its bytes with its indexes, and theirs */
43
+ export type TableCount = { name: string; rows: number | null; bytes: number; indexBytes: number; by?: Record<string, number> }
44
+ /** a damage sqlite's check found: the table it is in (null for the file's own pages, as « Page N: never used »), the
45
+ * index when the damaged tree is one of the table's indexes, and its words */
46
+ export type Damage = { table: string | null; index?: string; what: string }
47
+ export type DbCount = { at: number; ms: number; stamp: string; v?: number; tables: TableCount[]; damaged?: Damage[] } | { at: number; ms: number; stamp: string; v?: number; error: string; damaged?: Damage[] }
48
+ /** what a count holds: a count of another version is made again at the next look, whatever moved */
49
+ export const COUNT_VERSION = 2
50
+ export type Copy = { name: string; bytes: number; at: number }
51
+ export type DbView = DbSpec & { where: string; live: Live | null; count: DbCount | null; copies: Copy[] }
52
+ /** what one node holds of one kind, against its share */
53
+ export type KindUse = { kind: string; node: string; who: string; rows: number; bytes: number; maxRows: number; maxBytes: number }
54
+ export type Replica = { configured: boolean; to: string | null; running: boolean; since: number | null }
55
+ export type HqFacts = { carries: boolean; url: string; fileVersion: number | null; codeVersion: number; wire: number; feedTop: number | null; kinds: KindUse[]; replica: Replica; drill: Drill | null; error: string | null }
56
+ /** `fold`: where the machine's fold stands, its words (home/machine-fold.ts foldWords) */
57
+ export type DatabasesPage = { at: number; dbs: DbView[]; hq: HqFacts | null; projects: ProjectView[]; unsplit: Unsplit[]; fold?: string[] }
58
+
59
+ /** the counts the census job keeps, by file */
60
+ export type Census = { at: number; counts: Record<string, DbCount>; projects?: Project[] }
61
+ export { DB_CENSUS } from './machine-stores.ts'
62
+ import { DB_CENSUS } from './machine-stores.ts'
63
+ /** a file that moved is counted again once its count is this old: the media index moves every few seconds */
64
+ export const CENSUS_EVERY_MS = 10 * 60_000
65
+ /** the census looks for files that came or went this often, whatever moved */
66
+ export const CENSUS_MAX_MS = 60 * 60_000
67
+ /** what a count waits on a writer's lock before it says the file is locked */
68
+ const COUNT_BUSY_MS = 1_000
69
+
70
+ /** every database shipkit keeps on this machine: its own, then the run record of each repo (worktrees share one) */
71
+ export function machineDatabases(repos: readonly { name: string; path: string }[]): DbSpec[] {
72
+ // the hq and the daemon db name a repo by its key or name; the media index by its store's folder, objects through
73
+ // the files that name them; the agent time index by Claude Code's project folder, every table through its file
74
+ const byRepo: Split = { column: 'repo', names: 'repo' }
75
+ const media = (files: string): Split => ({ column: 'store', names: 'path', join: { table: files, on: 'key', key: 'key', distinct: true } })
76
+ const time = (files: string): Split => ({ column: 'project', names: 'claude-project', join: { table: files, on: 'file_id', key: 'id', distinct: false } })
77
+ const MEDIA_WORDS = 'a cache: it builds again from the files and the Space, `sk db drop media --go` drops it; only when a purged file was fetched back is lost'
78
+ const DAEMON_WORDS = 'this machine only: its review cards, rows that wait to go to the hq, and what it already sent'
79
+ const TIME_WORDS = 'a cache of the transcripts: it builds again from them, except those Claude Code already pruned, which it alone still holds'
80
+ const machine = machineDbPath()
81
+ const parts: DbPart[] = [
82
+ { id: 'machine db', kind: 'machine', name: 'its own state', prefix: null, role: 'machine', roleWords: 'this machine only: its switches, its process ids, the last run of each job', split: null },
83
+ ...(mediaDbPath() === machine ? [{ id: 'media index', kind: 'media' as const, name: 'media index', prefix: 'media_', role: 'cache' as const, roleWords: MEDIA_WORDS, split: media('media_files') }] : []),
84
+ ...(agentTimeDbPath() === machine ? [{ id: 'agent time index', kind: 'agent-time' as const, name: 'agent time index', prefix: 'time_', role: 'cache' as const, roleWords: TIME_WORDS, split: time('time_files') }] : []),
85
+ ...(daemonDbPath() === machine ? [{ id: 'daemon file', kind: 'daemon' as const, name: "daemon's file", prefix: null, tables: DAEMON_TABLES, role: 'machine' as const, roleWords: DAEMON_WORDS, split: byRepo }] : []),
86
+ ]
87
+ const own: DbSpec[] = [
88
+ { id: 'hq', name: 'hq', kind: 'hq', what: 'what every machine sends: reports, cards, prompts, day figures', path: hqDbPath(), cap: HQ_MAX_BYTES, repo: null, role: 'truth', roleWords: 'the truth: copied to the Space as it is written, restored whole by the drill', roleShort: 'copied to the Space, drilled', split: byRepo, parts: null },
89
+ ...(daemonDbPath() === machine ? [] : [{ id: 'daemon db', name: 'daemon db', kind: 'daemon' as const, what: "the daemon's outbox to the hq, its ledger of the feed, and the review cards with the switch cards-in-db", path: daemonDbPath(), cap: DAEMON_DB_MAX_BYTES, repo: null, role: 'machine' as const, roleWords: DAEMON_WORDS, roleShort: "this machine's cards, and rows that wait to go to the hq", split: byRepo, parts: null }]),
90
+ { id: 'machine db', name: 'machine db', kind: 'machine', what: `the machine's one local file: its own state${parts.length > 1 ? `, and ${parts.slice(1).map((x) => x.name).join(', ')} as tables` : ''}`, path: machine, cap: null, repo: null, role: 'machine', roleWords: parts.length > 1 ? `this machine only: its own state, and ${parts.slice(1).map((x) => `${x.name} (${x.prefix != null ? `${x.prefix} tables` : 'its tables under their own names'})`).join(', ')}` : 'this machine only: its switches, its process ids, the last run of each job', roleShort: parts.length > 1 ? `switches, process ids, ${parts.slice(1).map((x) => x.name).join(', ')}` : 'switches, process ids', split: null, parts: parts.length > 1 ? parts : null },
91
+ ...(mediaDbPath() === machine ? [] : [{ id: 'media index', name: 'media index', kind: 'media' as const, what: 'every media file of this machine and its object in the Space', path: mediaDbPath(), cap: MEDIA_INDEX_MAX_BYTES, repo: null, role: 'cache' as const, roleWords: MEDIA_WORDS, roleShort: 'builds again from the files', split: media('media_files'), parts: null }]),
92
+ ...(agentTimeDbPath() === machine ? [] : [{ id: 'agent time index', name: 'agent time index', kind: 'agent-time' as const, what: 'every Claude Code transcript, read for sk time', path: agentTimeDbPath(), cap: null, repo: null, role: 'cache' as const, roleWords: TIME_WORDS, roleShort: 'builds again, but holds what Claude Code pruned', split: time('time_files'), parts: null }]),
93
+ ]
94
+ // a store's own file still there, not folded yet: listed under its old tables, so a page before the fold shows it
95
+ for (const store of FOLDED_STORES) {
96
+ const old = oldStorePath(store)
97
+ if (!existsSync(old) || isTombstone(old) || own.some((s) => s.path === old)) continue
98
+ const isMedia = store === 'media index'
99
+ own.push({ id: `${store} file`, name: `${store} (not folded)`, kind: isMedia ? 'media' : 'agent-time', what: `its own file until sk db fold moves it into ${tilde(machine)}`, path: old, cap: null, repo: null, role: 'cache', roleWords: `${isMedia ? MEDIA_WORDS : TIME_WORDS}; not read by this code until sk db fold`, roleShort: 'the daemon folds it at its start', split: isMedia ? media('files') : time('files'), parts: null })
100
+ }
101
+ const seen = new Set(own.map((s) => s.path))
102
+ const records: DbSpec[] = []
103
+ for (const r of repos) {
104
+ const path = dbPath(r.path)
105
+ if (seen.has(path)) continue
106
+ seen.add(path)
107
+ records.push({ id: `run record ${r.name}`, name: r.name, kind: 'record', what: "the repo's gates, checks, suites and answers", path, cap: null, repo: r.name, role: 'cache', roleWords: "the repo's gate history and cache: it builds again as its gates run; no command deletes it, and a file deleted by hand loses that history", roleShort: 'builds again as gates run', split: null, parts: null })
108
+ }
109
+ return [...own, ...records]
110
+ }
111
+
112
+ const sizeOf = (f: string): { size: number; mtime: number } | null => {
113
+ try {
114
+ const s = statSync(f)
115
+ return s.isFile() ? { size: s.size, mtime: s.mtimeMs } : null
116
+ } catch {
117
+ return null
118
+ }
119
+ }
120
+
121
+ /** what moves when a file is written: its size and time, and its log's */
122
+ export function stampOf(path: string): string | null {
123
+ const f = sizeOf(path)
124
+ if (f == null) return null
125
+ const w = sizeOf(`${path}-wal`)
126
+ return `${f.size}:${Math.round(f.mtime)}:${w?.size ?? 0}:${Math.round(w?.mtime ?? 0)}`
127
+ }
128
+
129
+ /** a database read only; it throws for a file that is not one or that a writer holds past `busyMs`. The caller sees it
130
+ * exists first: a read only open never makes a file, a create would */
131
+ function openRead(path: string, busyMs: number): Database {
132
+ const db = new Database(path, { readonly: true })
133
+ try {
134
+ db.exec(`pragma busy_timeout = ${busyMs}`)
135
+ return db
136
+ } catch (e) {
137
+ db.close()
138
+ throw e
139
+ }
140
+ }
141
+
142
+ /** sqlite's words for a file it refuses, cut to the reason */
143
+ const why = (e: unknown): string => {
144
+ const m = (e as Error).message ?? String(e)
145
+ return /locked|busy/i.test(m) ? 'a writer holds it' : /not a database|malformed|corrupt/i.test(m) ? 'not a sound sqlite file' : m.split('\n')[0]!
146
+ }
147
+
148
+ const pragma = (db: Database, name: string): number => Number(Object.values(db.query(`pragma ${name}`).get() as Record<string, number>)[0])
149
+
150
+ /** the stat and the header of one file, null when it is not there */
151
+ export function liveOf(path: string): Live | null {
152
+ const f = sizeOf(path)
153
+ if (f == null) return null
154
+ const w = sizeOf(`${path}-wal`)
155
+ const out: Live = { bytes: f.size + (w?.size ?? 0), file: f.size, wal: w?.size ?? 0, changed: Math.max(f.mtime, w?.mtime ?? 0), pageSize: null, pages: null, free: null, version: null, error: null }
156
+ let db: Database | null = null
157
+ try {
158
+ db = openRead(path, 0)
159
+ out.pageSize = pragma(db, 'page_size')
160
+ out.pages = pragma(db, 'page_count')
161
+ out.free = pragma(db, 'freelist_count')
162
+ out.version = pragma(db, 'user_version')
163
+ } catch (e) {
164
+ out.error = why(e)
165
+ } finally {
166
+ db?.close()
167
+ }
168
+ return out
169
+ }
170
+
171
+ export const quoted = (name: string): string => `"${name.replace(/"/g, '""')}"`
172
+
173
+ /** what sqlite's check finds, by table: a message of `pragma quick_check` names a b-tree by its root page, which
174
+ * sqlite_master maps to its table. In a file of incremental auto vacuum the check itself stops at a damaged page, so
175
+ * each table is then walked whole on its own (a count of no index reads every page of it). Seconds on a big file */
176
+ export function damageOf(db: Database, o: { deep?: boolean } = {}): Damage[] {
177
+ const objects = db.query("select type, name, tbl_name, rootpage from sqlite_master where rootpage > 0").all() as { type: string; name: string; tbl_name: string; rootpage: number }[]
178
+ const roots = new Map(objects.map((o) => [o.rootpage, o]))
179
+ const of = (o: { type: string; name: string; tbl_name: string } | undefined): Pick<Damage, 'table' | 'index'> => (o == null ? { table: null } : o.type === 'index' ? { table: o.tbl_name, index: o.name } : { table: o.tbl_name })
180
+ let checked: Damage[] | null = null
181
+ let stopped: string | null = null
182
+ try {
183
+ const lines = (db.query('pragma quick_check(50)').all() as { quick_check: string }[]).flatMap((r) => r.quick_check.split('\n')).filter((l) => l !== 'ok' && !l.startsWith('*** in database'))
184
+ checked = lines.map((l) => {
185
+ const tree = /^Tree (\d+)/.exec(l)?.[1]
186
+ // « wrong # of entries in index X »: the index X, whichever tree the check walked
187
+ const index = /in index (\S+)/.exec(l)?.[1]
188
+ const named = index == null ? undefined : objects.find((o) => o.type === 'index' && o.name === index)
189
+ return { ...of(named ?? (tree == null ? undefined : roots.get(Number(tree)))), what: l.slice(0, 200) }
190
+ })
191
+ } catch (e) {
192
+ stopped = (e as Error).message
193
+ }
194
+ if (checked != null && o.deep !== true) return checked
195
+ // each tree walked whole on its own: a table by every value of every row (a count reads only the pages' heads), an
196
+ // index by a count through it alone. The check stops at the first damage it meets, and a cap of lines may name one
197
+ // tree only: `deep` walks every tree always
198
+ const out: Damage[] = [...(checked ?? [])]
199
+ const has = (d: Pick<Damage, 'table' | 'index'>): boolean => out.some((x) => x.table === d.table && x.index === d.index)
200
+ for (const ob of objects.filter((x) => !x.name.startsWith('sqlite_') && (x.type === 'table' || x.type === 'index'))) {
201
+ try {
202
+ if (ob.type === 'index') db.query(`select count(*) from ${quoted(ob.tbl_name)} indexed by ${quoted(ob.name)}`).get()
203
+ else {
204
+ const cols = (db.query('select name from pragma_table_info(?)').all(ob.name) as { name: string }[]).map((c) => `length(quote(${quoted(c.name)}))`)
205
+ db.query(`select sum(${cols.join(' + ') || '0'}) from ${quoted(ob.name)} not indexed`).get()
206
+ }
207
+ } catch (x) {
208
+ // an index sqlite does not take for a count (a partial one) says nothing of its pages
209
+ const d = of(ob)
210
+ if (/malformed|corrupt/i.test((x as Error).message) && !has(d)) out.push({ ...d, what: (x as Error).message })
211
+ }
212
+ }
213
+ return out.length > 0 ? out : stopped == null ? [] : [{ table: null, what: `the check stops: ${stopped}` }]
214
+ }
215
+
216
+ /** every table's rows and bytes, its indexes' bytes beside: the slow read, for the census job only. `check`: sqlite's
217
+ * own check of every page too (seconds), its damage said by table; a file whose pages do not all read is still
218
+ * counted where it can be, and says so */
219
+ export function countDb(path: string, now = Date.now(), busyMs = COUNT_BUSY_MS, split: Split | null | ((table: string) => Split | null) = null, check = false): DbCount {
220
+ const t0 = performance.now()
221
+ const stamp = stampOf(path) ?? ''
222
+ const damaged: Damage[] = []
223
+ const done = (x: { tables: TableCount[] } | { error: string }): DbCount => ({ at: now, ms: Math.round(performance.now() - t0), stamp, v: COUNT_VERSION, ...x, ...(damaged.length > 0 ? { damaged } : {}) })
224
+ if (!existsSync(path)) return done({ error: 'not on this disk' })
225
+ let db: Database | null = null
226
+ try {
227
+ db = openRead(path, busyMs)
228
+ if (check) {
229
+ try {
230
+ damaged.push(...damageOf(db))
231
+ } catch (e) {
232
+ damaged.push({ table: null, what: `the check stops: ${(e as Error).message}` })
233
+ }
234
+ }
235
+ const objects = db.query("select name, type, tbl_name as of from sqlite_master where type in ('table', 'index')").all() as { name: string; type: string; of: string }[]
236
+ let pages = new Map<string, number>()
237
+ try {
238
+ pages = new Map((db.query('select name, pgsize from dbstat where aggregate = 1').all() as { name: string; pgsize: number }[]).map((r) => [r.name, r.pgsize]))
239
+ } catch (e) {
240
+ // a page that does not read: no bytes, and the damage said, never a count of before shown as now
241
+ damaged.push({ table: null, what: `its pages do not all read: ${why(e)}` })
242
+ }
243
+ const tables = objects.filter((o) => o.type === 'table' && !o.name.startsWith('sqlite_'))
244
+ const out = tables.map((t): TableCount => {
245
+ let rows: number | null = null
246
+ try {
247
+ rows = (db!.query(`select count(*) as n from ${quoted(t.name)}`).get() as { n: number }).n
248
+ } catch (e) {
249
+ // a virtual table of a module this sqlite lacks reads no rows; a damaged one says so
250
+ if (/malformed|corrupt/i.test((e as Error).message)) damaged.push({ table: t.name, what: (e as Error).message })
251
+ }
252
+ const indexBytes = objects.filter((o) => o.type === 'index' && o.of === t.name).reduce((n, o) => n + (pages.get(o.name) ?? 0), 0)
253
+ const out: TableCount = { name: t.name, rows, bytes: (pages.get(t.name) ?? 0) + indexBytes, indexBytes }
254
+ const sp = typeof split === 'function' ? split(t.name) : split
255
+ if (sp != null && rows != null) {
256
+ try {
257
+ const columns = (db!.query(`select name from pragma_table_info(?)`).all(t.name) as { name: string }[]).map((c) => c.name)
258
+ const by = groupsOf(db!, t.name, columns, sp)
259
+ if (by != null) out.by = by
260
+ } catch (e) {
261
+ damaged.push({ table: t.name, what: (e as Error).message })
262
+ }
263
+ }
264
+ return out
265
+ })
266
+ return done({ tables: out.sort((a, b) => b.bytes - a.bytes || (b.rows ?? 0) - (a.rows ?? 0) || a.name.localeCompare(b.name)) })
267
+ } catch (e) {
268
+ return done({ error: why(e) })
269
+ } finally {
270
+ db?.close()
271
+ }
272
+ }
273
+
274
+ export const readCensus = (): Census | null => machineGet<Census>(DB_CENSUS)
275
+
276
+ /** is a count owed: none kept, a file that moved since its count and that count old enough, or the last look old */
277
+ export function censusDue(now: number, census: Census | null = readCensus()): boolean {
278
+ if (census == null || now - census.at >= CENSUS_MAX_MS || Object.values(census.counts).some((c) => c.v !== COUNT_VERSION)) return true
279
+ return Object.entries(census.counts).some(([path, c]) => now - c.at >= CENSUS_EVERY_MS && stampOf(path) !== c.stamp)
280
+ }
281
+
282
+ /** the census job's body, in its child: count each database that is new, moved past CENSUS_EVERY_MS or failed, keep
283
+ * the others' counts, drop the files that went. `all` counts every one now (`sk db --fresh`) */
284
+ export function censusTick(o: { repos: readonly { name: string; path: string }[]; projects?: Project[]; now?: () => number; all?: boolean; print?: (l: string) => void }): Census {
285
+ const now = o.now ?? Date.now
286
+ const kept = readCensus()
287
+ const held = kept?.counts ?? {}
288
+ const counts: Record<string, DbCount> = {}
289
+ let n = 0
290
+ let ms = 0
291
+ for (const s of machineDatabases(o.repos)) {
292
+ const stamp = stampOf(s.path)
293
+ if (stamp == null) continue
294
+ const was = held[s.path]
295
+ const fresh = was != null && !('error' in was) && was.v === COUNT_VERSION && (was.stamp === stamp || now() - was.at < CENSUS_EVERY_MS)
296
+ if (fresh && o.all !== true) {
297
+ counts[s.path] = was
298
+ continue
299
+ }
300
+ const c = countDb(s.path, now(), COUNT_BUSY_MS, s.parts == null ? s.split : (t) => partOf(s, t)?.split ?? null, o.all === true)
301
+ for (const d of c.damaged ?? []) o.print?.(`FAIL: ${s.name}${d.table == null ? '' : `, table ${d.table}`}${d.index == null ? '' : `, its index ${d.index}`}: damaged: ${d.what}${/never used/.test(d.what) ? ' (pages a repair left: sk db rebuild --go clears them)' : d.table != null ? ` (sk db repair says what a repair keeps)` : ''}`)
302
+ if ('error' in c) o.print?.(`FAIL: ${s.name} does not count: ${c.error}`)
303
+ counts[s.path] = c
304
+ n++
305
+ ms += c.ms
306
+ }
307
+ const projects = o.projects ?? kept?.projects
308
+ const census: Census = { at: now(), counts, ...(projects == null ? {} : { projects }) }
309
+ machineSet(DB_CENSUS, census)
310
+ if (n > 0) o.print?.(`ok: counted ${n} databases in ${(ms / 1000).toFixed(1)} s`)
311
+ return census
312
+ }
313
+
314
+ /** the house repos as projects: the id each config gives (its folder's name when none or when it does not load) and
315
+ * its git key, read in the census job's child, so an ask never loads a config */
316
+ export async function houseProjects(repos: readonly { name: string; path: string }[]): Promise<Project[]> {
317
+ const { loadRepoConfig } = await import('../repo-config.ts')
318
+ const { repoIdOf } = await import('../core/scope.ts')
319
+ const { repoKey } = await import('../hq/report.ts')
320
+ return Promise.all(
321
+ repos.map(async (r): Promise<Project> => {
322
+ let id: string | null = null
323
+ try {
324
+ id = repoIdOf(await loadRepoConfig(r.path))
325
+ } catch {
326
+ // a config that does not load names its repo by its folder
327
+ }
328
+ return { name: r.name, id: id ?? r.name, path: r.path, key: (await repoKey(r.path).catch(() => undefined)) ?? null }
329
+ }),
330
+ )
331
+ }
332
+
333
+ /** the copies `migrateTo` keeps beside a file before its shape changes (ledger/db.ts `beforeCopy`) */
334
+ function copiesOf(path: string, list: (dir: string) => string[]): Copy[] {
335
+ const base = `${basename(path)}.before-v`
336
+ return list(dirname(path))
337
+ .filter((f) => f.startsWith(base))
338
+ .flatMap((f) => {
339
+ const s = sizeOf(join(dirname(path), f))
340
+ return s == null ? [] : [{ name: f, bytes: s.size, at: s.mtime }]
341
+ })
342
+ }
343
+
344
+ export const tilde = (path: string, home = homedir()): string => (home !== '' && (path === home || path.startsWith(`${home}/`)) ? `~${path.slice(home.length)}` : path)
345
+
346
+ /** where a Litestream config sends: `bucket/path`, read from the lines `replica.ts` writes (each value a JSON string) */
347
+ export function replicaTo(yml: string): string | null {
348
+ const val = (key: string): string | null => {
349
+ const m = new RegExp(`^\\s*${key}:\\s*(".*")\\s*$`, 'm').exec(yml)
350
+ try {
351
+ return m == null ? null : (JSON.parse(m[1]!) as string)
352
+ } catch {
353
+ return null
354
+ }
355
+ }
356
+ const bucket = val('bucket')
357
+ return bucket == null ? null : `${bucket}/${val('path') ?? 'hq'}`
358
+ }
359
+
360
+ /** a running process of that command line and when it started, null when none runs; two small spawns, no walk */
361
+ export type Running = (command: string) => Promise<{ since: number | null } | null>
362
+
363
+ const spawnText = async (cmd: string[]): Promise<string> => {
364
+ try {
365
+ const p = Bun.spawn(cmd, { stdout: 'pipe', stderr: 'ignore', stdin: 'ignore' })
366
+ const [out] = await Promise.all([new Response(p.stdout).text(), p.exited])
367
+ return out
368
+ } catch {
369
+ return ''
370
+ }
371
+ }
372
+
373
+ export const running: Running = async (command) => {
374
+ const pid = (await spawnText(['pgrep', '-f', command.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')])).trim().split('\n')[0]
375
+ if (pid == null || !/^\d+$/.test(pid)) return null
376
+ const at = Date.parse((await spawnText(['ps', '-o', 'lstart=', '-p', pid])).trim())
377
+ return { since: Number.isFinite(at) ? at : null }
378
+ }
379
+
380
+ /** Litestream replicating on that config, and since when, whoever started it, and where it sends */
381
+ export async function replicaOf(config: string, on: Running = running): Promise<Replica> {
382
+ let yml: string | null = null
383
+ try {
384
+ yml = readFileSync(config, 'utf8')
385
+ } catch {
386
+ // no config: replicate never ran on this machine
387
+ }
388
+ // no config: replicate never ran here, so no process to look for
389
+ const r = yml == null ? null : await on(`litestream replicate -config ${config}`)
390
+ return { configured: yml != null, to: yml == null ? null : replicaTo(yml), running: r != null, since: r?.since ?? null }
391
+ }
392
+
393
+ /** the hq's kinds by node, the feed's top, read live from the file: a few small tables */
394
+ function hqTables(path: string): Pick<HqFacts, 'kinds' | 'feedTop' | 'error'> {
395
+ if (!existsSync(path)) return { kinds: [], feedTop: null, error: null }
396
+ let db: Database | null = null
397
+ try {
398
+ db = openRead(path, 0)
399
+ const there = new Set((db.query("select name from sqlite_master where type = 'table'").all() as { name: string }[]).map((r) => r.name))
400
+ const who = new Map<string, string>()
401
+ if (there.has('nodes') && there.has('machines')) for (const r of db.query('select n.id as id, m.name as machine, n.os_user as user from nodes n left join machines m on m.id = n.machine').all() as { id: string; machine: string | null; user: string }[]) who.set(r.id, `${r.machine ?? 'a machine'} (${r.user})`)
402
+ const usage = there.has('usage') ? (db.query('select node, kind, rows, bytes from usage order by bytes desc, kind').all() as { node: string; kind: string; rows: number; bytes: number }[]) : []
403
+ const kinds = usage.map((u): KindUse => {
404
+ const k = KINDS.find((x) => x.kind === u.kind)
405
+ return { kind: u.kind, node: u.node, who: who.get(u.node) ?? u.node.slice(0, 8), rows: u.rows, bytes: u.bytes, maxRows: k?.maxPerNode ?? 0, maxBytes: k?.maxBytesPerNode ?? 0 }
406
+ })
407
+ const top = there.has('sqlite_sequence') ? (db.query("select seq from sqlite_sequence where name = 'feed'").get() as { seq: number } | null) : null
408
+ return { kinds, feedTop: top?.seq ?? (there.has('feed') ? 0 : null), error: null }
409
+ } catch (e) {
410
+ return { kinds: [], feedTop: null, error: why(e) }
411
+ } finally {
412
+ db?.close()
413
+ }
414
+ }
415
+
416
+ /** what the hq's part of the page says, apart from its tables */
417
+ export async function hqFactsOf(o: { path: string; running: Running }): Promise<Omit<HqFacts, 'fileVersion'>> {
418
+ const { carriesHq, hqUrl } = await import('../hq/hq-local.ts')
419
+ const { lastDrill, replicaConfigPath } = await import('../hq/replica.ts')
420
+ const carries = carriesHq()
421
+ const replica = carries ? await replicaOf(replicaConfigPath(), o.running) : { configured: false, to: null, running: false, since: null }
422
+ return { carries, url: hqUrl(), codeVersion: HQ_FILE_VERSION, wire: HQ_SCHEMA_VERSION, ...hqTables(o.path), replica, drill: lastDrill() }
423
+ }
424
+
425
+ /** the page's answer: the hq first, then every other database by what it takes on disk, the missing ones last. The
426
+ * loop is given back between two files, and the replica's two spawns run while the files are read */
427
+ export async function databasesPage(o: { repos: readonly { name: string; path: string }[]; now?: number; running?: Running; census?: Census | null }): Promise<DatabasesPage> {
428
+ const now = o.now ?? Date.now()
429
+ const census = o.census === undefined ? readCensus() : o.census
430
+ const specs = machineDatabases(o.repos)
431
+ const hqSpec = specs.find((d) => d.kind === 'hq')
432
+ const hqFacts = hqSpec == null ? null : hqFactsOf({ path: hqSpec.path, running: o.running ?? running })
433
+ const dirs = new Map<string, string[]>()
434
+ const list = (dir: string): string[] => {
435
+ let l = dirs.get(dir)
436
+ if (l == null) {
437
+ try { l = readdirSync(dir) } catch { l = [] }
438
+ dirs.set(dir, l)
439
+ }
440
+ return l
441
+ }
442
+ const dbs: DbView[] = []
443
+ for (const s of specs) {
444
+ const live = liveOf(s.path)
445
+ dbs.push({ ...s, where: tilde(s.path), live, count: live == null ? null : (census?.counts[s.path] ?? null), copies: live == null ? [] : copiesOf(s.path, list) })
446
+ await yieldLoop()
447
+ }
448
+ const disk = (v: DbView): number => (v.live == null ? -1 : v.live.bytes + v.copies.reduce((n, c) => n + c.bytes, 0))
449
+ dbs.sort((a, b) => Number(b.kind === 'hq') - Number(a.kind === 'hq') || disk(b) - disk(a) || a.name.localeCompare(b.name))
450
+ const hq = hqFacts == null ? null : { ...(await hqFacts), fileVersion: dbs.find((d) => d.kind === 'hq')?.live?.version ?? null }
451
+ // the projects the census read the configs of, else the repos by their folder's name
452
+ const projects = census?.projects ?? o.repos.map((r) => ({ name: r.name, id: r.name, path: r.path, key: null }))
453
+ let fold: string[] | undefined
454
+ try {
455
+ fold = (await import('./machine-fold.ts')).foldWords()
456
+ } catch (e) {
457
+ fold = [`the fold's state does not read: ${why(e)}`]
458
+ }
459
+ return { at: now, dbs, hq, ...projectsOf(dbs, projects), fold }
460
+ }
@@ -83,6 +83,12 @@ export async function scanGlobalUnits(repos: RepoRef[], o: { files?: (root: stri
83
83
  const kind = unitKind(d)
84
84
  if (kind === 'globalProcess') {
85
85
  const u = d as GlobalProcessUnit
86
+ try {
87
+ if (u.here != null && !u.here()) continue
88
+ } catch (e) {
89
+ out.errors.push(`${repo.name}/${rel}: global process '${u.id}' cannot say whether it is of this machine: ${(e as Error).message}`)
90
+ continue
91
+ }
86
92
  const first = out.processes.find((p) => p.id === u.id)
87
93
  if (first != null) out.errors.push(`${repo.name}/${rel}: global process '${u.id}' is already declared by ${first.repo}/${first.file}`)
88
94
  else out.processes.push(toDecl(u, repo, f))