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
@@ -1,48 +1,392 @@
1
1
  // THE MACHINE DB: what shipkit remembers about this machine, whatever repo
2
- // asks, in one sqlite file: pm's children and resources (their pid records),
2
+ // asks, in one sqlite file, the one local file of the machine beside the hq: the
3
+ // media index's tables (`media_*`) and the agent time index's (`time_*`) are in
4
+ // it too once folded (machine-fold.ts). Its own: pm's children and resources (their pid records),
3
5
  // the repos flagged active, the rail order, the self update answer, the
4
- // instance registry. One `state` table of named json values.
6
+ // instance registry. A `state` table of named json values, and a table of
7
+ // its own for a value that grows without bound and would be rewritten whole
8
+ // at each change (the backup's list of sources and the files its runs held).
5
9
  //
6
10
  // The one constraint a reader breaks first: a failure reads as "nothing
7
11
  // remembered", and every caller treats that as the safe side (no adoption,
8
12
  // no replay, the pid rule). Slot LOCKS are not here: they are leases a dead
9
13
  // pid must release at once, which a file does and a row does not.
10
14
  import { Database } from 'bun:sqlite'
11
- import { existsSync, mkdirSync, readFileSync, rmSync } from 'fs'
12
- import { homedir } from 'os'
15
+ import { chmodSync, closeSync, existsSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, rmSync, statSync, writeFileSync } from 'fs'
13
16
  import { dirname, join } from 'path'
14
- import { ensureWal } from '../ledger/db.ts'
17
+ import { homeDir } from '../home/home-paths.ts'
18
+ import { ensureWal, migrateTo } from '../ledger/db.ts'
19
+ import { createRefusal, machinePath } from './machine-layout.ts'
15
20
 
16
21
  export const MACHINE_DB_ENV = 'SHIPKIT_MACHINE_DB'
22
+ export const MACHINE_DB_FILE = 'shipkit.sqlite'
17
23
 
18
- /** `~/.config/shipkit/shipkit.sqlite`; beside a moved home dir when one is
19
- * set, so a smoke or a suite with its own home never touches the real one */
24
+ /** `~/.shipkit/shipkit.sqlite` (in `~/.config/shipkit/` until the machine moved: layout.ts machinePath); beside a
25
+ * moved home dir when one is set, so a smoke or a suite with its own home never touches the real one */
20
26
  export function machineDbPath(): string {
21
27
  const env = process.env[MACHINE_DB_ENV]
22
28
  if (env != null && env !== '') return env
23
29
  const home = process.env.SHIPKIT_HOME_DIR
24
30
  if (home != null && home !== '') return join(home, 'machine.sqlite')
25
- return join(homedir(), '.config', 'shipkit', 'shipkit.sqlite')
31
+ return machinePath(MACHINE_DB_FILE)
26
32
  }
27
33
 
28
- /** a machine write waits this long on another process's */
29
- const BUSY_MS = 2_000
34
+ /** the stores that were files of their own and are tables of the machine db once folded (`sk db fold`) */
35
+ export type FoldedStore = 'media index' | 'agent time index' | 'daemon file'
36
+ export const FOLDED_STORES: readonly FoldedStore[] = ['media index', 'agent time index', 'daemon file']
37
+ /** the variable that moves a store for one process: never refused there */
38
+ export const STORE_ENV: Record<FoldedStore, string> = { 'media index': 'SHIPKIT_MEDIA_DB', 'agent time index': 'SHIPKIT_AGENT_TIME_DB', 'daemon file': 'SHIPKIT_DAEMON_DB' }
39
+
40
+ /** where a folded store's own file was: beside the machine db when a home dir is set (a suite, a scratch) */
41
+ export function oldStorePath(store: FoldedStore): string {
42
+ if (store === 'media index') return join(homeDir(), 'media.sqlite')
43
+ if (store === 'daemon file') return join(homeDir(), 'daemon.sqlite')
44
+ const home = process.env.SHIPKIT_HOME_DIR
45
+ return home != null && home !== '' ? join(home, 'agent-time.sqlite') : machinePath('agent-time.sqlite')
46
+ }
30
47
 
31
- const SCHEMA = 'create table if not exists state (name text primary key, value text not null, at integer not null)'
48
+ /** each folded store's table prefix in the machine db: its file of its own has the same tables after a way back */
49
+ export const STORE_PREFIX: Record<FoldedStore, string> = { 'media index': 'media_', 'agent time index': 'time_', 'daemon file': '' }
50
+ // the daemon's tables keep their names (outbox, review_cards, ...): none is a name of the machine db's own, and its
51
+ // own file has one shape before and after a fold
32
52
 
33
- function withMachine<R>(body: (db: Database) => R, path = machineDbPath()): R | null {
53
+ /** the switch of the daemon's fold (home/machine-fold.ts, hq/switch.ts): off, a store not folded is read and written
54
+ * in its own file. A machine db that does not read THROWS */
55
+ export const FOLD_AUTO = 'db-fold-auto'
56
+ export const foldAutoOn = (): boolean => machineRead<boolean>(FOLD_AUTO) !== false
57
+
58
+ /** the shape of a store's own file: `tables`, the machine db's tables (`media_files`, written by a way back), `old`,
59
+ * the names of before the fold (`files`), null when no sqlite file is there (none, or a tombstone) */
60
+ export function ownShape(store: FoldedStore, old = oldStorePath(store)): 'tables' | 'old' | null {
61
+ if (lstatSync(old, { throwIfNoEntry: false }) == null || isTombstone(old)) return null
34
62
  let db: Database | null = null
63
+ try {
64
+ // read write and never made: a read only open of a WAL file with no shared memory beside it fails
65
+ db = new Database(old, { readwrite: true, create: false })
66
+ db.exec('pragma busy_timeout = 3000')
67
+ const names = (db.query("select name from sqlite_master where type = 'table'").all() as { name: string }[]).map((r) => r.name)
68
+ return STORE_PREFIX[store] === '' || names.some((n) => n.startsWith(STORE_PREFIX[store])) ? 'tables' : 'old'
69
+ } catch {
70
+ return 'old'
71
+ } finally {
72
+ db?.close()
73
+ }
74
+ }
75
+
76
+ /** where this machine keeps a store: the machine db, or, while the store is not folded, its own file when the fold is
77
+ * off or the file has the tables this code reads (a fold of several stores leaves each in its file until its turn).
78
+ * One branch, at the place a store is opened: its readers and writers use the same tables in both */
79
+ export function storeHome(store: FoldedStore, now = Date.now()): string {
80
+ const old = oldStorePath(store)
81
+ const machine = machineDbPath()
82
+ const kept = homes.get(store)
83
+ // a process asks at every read and write of its store: the answer holds a second, as the switch of the cards does
84
+ if (kept != null && kept.old === old && kept.machine === machine && now - kept.at < HOME_EVERY_MS) return kept.home
85
+ const home = lstatSync(old, { throwIfNoEntry: false }) != null && !isTombstone(old) && (!foldAutoOn() || ownShape(store, old) === 'tables') ? old : machine
86
+ homes.set(store, { old, machine, home, at: now })
87
+ return home
88
+ }
89
+ const HOME_EVERY_MS = 1000
90
+ const homes = new Map<FoldedStore, { old: string; machine: string; home: string; at: number }>()
91
+ /** this process reads where its stores are again at once: a fold or a way back of its own just moved them */
92
+ export const forgetStoreHomes = (): void => homes.clear()
93
+
94
+ /** the mark a fold or a way back sets on a store while it walks (`<FOLD_MARK><store>` in `state`) */
95
+ export const FOLD_MARK = 'db-fold-partial:'
96
+ export type FoldMark = { way: 'go' | 'back'; pid: number; at: number }
97
+ export function foldMarkOf(store: FoldedStore): FoldMark | null {
98
+ const row = machineDb((db) => db.query('select value from state where name = ?').get(`${FOLD_MARK}${store}`) as { value: string } | null)
99
+ return row == null ? null : (JSON.parse(row.value) as FoldMark)
100
+ }
101
+ /** the head of a refusal that ends by itself: a fold, a way back or a repair at work. `sk time` exits WAIT_EXIT on it */
102
+ const WAIT = 'wait: '
103
+ export const WAIT_EXIT = 75
104
+ export const isWaitRefusal = (msg: string): boolean => msg.startsWith(WAIT)
105
+ export const refusalText = (msg: string): string => (isWaitRefusal(msg) ? msg.slice(WAIT.length) : msg)
106
+ /** a store's open refused: its words, and whether it ends by itself (a walk at work) */
107
+ export class StoreRefused extends Error {
108
+ override readonly name = 'StoreRefused'
109
+ readonly wait: boolean
110
+ constructor(refusal: string) {
111
+ super(refusalText(refusal))
112
+ this.wait = isWaitRefusal(refusal)
113
+ }
114
+ }
115
+ /** a StoreRefused by its name: a process may hold two copies of this module (the daemon's hubs load the lib again) */
116
+ export const isStoreRefused = (e: unknown): e is StoreRefused => e instanceof Error && e.name === 'StoreRefused'
117
+ /** a refusal that ends once what it names ends: `sk time`, `sk db fold` exit WAIT_EXIT on it */
118
+ export const waitRefused = (text: string): StoreRefused => new StoreRefused(`${WAIT}${text}`)
119
+ /** a time of today as the owner's clock says it, the day before it when it is not today */
120
+ const localClock = (at: number): string => {
121
+ const d = new Date(at)
122
+ const day = d.toLocaleDateString('sv-SE')
123
+ return `${day === new Date().toLocaleDateString('sv-SE') ? '' : `${day} `}${d.toLocaleTimeString('sv-SE')}`
124
+ }
125
+ /** how long the last walk of a store took on this machine, by its way (`<store>:<go|back>`): `{ ms, bytes }`, the size
126
+ * of its file then */
127
+ export const WALK_TOOK = 'db-walk-took:'
128
+ /** the size a walk of a store moves: its file under the fold's name, its own file, else the fold's copy */
129
+ export function walkBytes(old: string): number {
130
+ for (const f of [`${old}.fold-src`, old, `${old}.before-fold`]) {
131
+ try {
132
+ const st = statSync(f)
133
+ if (st.size > 4096) return st.size
134
+ } catch { /* not there */ }
135
+ }
136
+ return 0
137
+ }
138
+ /** what is left of a walk: from the last walk of that store and way on this machine, scaled by its size, else a bound
139
+ * from its size (about 8 MB a second on a loaded Mac: 401 MB in 47 s, measured) */
140
+ function waitLeft(store: FoldedStore, old: string, mark: FoldMark): string {
141
+ const bytes = walkBytes(old)
142
+ const gone = (Date.now() - mark.at) / 1000
143
+ const row = machineDb((db) => db.query('select value from state where name = ?').get(`${WALK_TOOK}${store}:${mark.way}`) as { value: string } | null)
144
+ const last = row == null ? null : (JSON.parse(row.value) as { ms: number; bytes: number })
145
+ const word = mark.way === 'back' ? 'way back' : 'fold'
146
+ if (last != null && last.ms > 0) {
147
+ const took = (last.ms / 1000) * (last.bytes > 0 && bytes > 0 ? bytes / last.bytes : 1)
148
+ return `about ${Math.max(1, Math.round(took - gone))} s left (its last ${word} here took ${(last.ms / 1000).toFixed(1)} s)`
149
+ }
150
+ return `under ${Math.max(1, Math.ceil(bytes / 8e6 + 5 - gone))} s left (a bound by its size)`
151
+ }
152
+
153
+ /** the mark of a repair of a store's damaged tables (home/machine-repair.ts): `saving` while their rows go aside, `saved`
154
+ * once all are there and the tables may be made again */
155
+ export const REPAIR_MARK = 'db-fold-repair:'
156
+ export type RepairMark = { tables: string[]; phase: 'saving' | 'saved'; pid: number; at: number }
157
+ export function repairMarkOf(store: FoldedStore): RepairMark | null {
158
+ const row = machineDb((db) => db.query('select value from state where name = ?').get(`${REPAIR_MARK}${store}`) as { value: string } | null)
159
+ return row == null ? null : (JSON.parse(row.value) as RepairMark)
160
+ }
161
+ const alive = (pid: number): boolean => {
162
+ try {
163
+ process.kill(pid, 0)
164
+ return true
165
+ } catch (e) {
166
+ return (e as { code?: string }).code === 'EPERM'
167
+ }
168
+ }
169
+
170
+ /** what a folded store's old file holds once its tables moved: text, so sqlite refuses it (« file is not a
171
+ * database ») and code that predates the fold fails loud instead of making an empty store there */
172
+ export const TOMBSTONE = 'shipkit: this store moved into the machine db'
173
+ export const tombstoneText = (store: FoldedStore, to: string): string => `${TOMBSTONE} ${to} (sk db fold). The ${store} is never read here again; sk db fold --back puts its copy back.\n`
174
+
175
+ /** the file at `path` is a folded store's tombstone */
176
+ export function isTombstone(path: string): boolean {
177
+ let fd: number | null = null
178
+ try {
179
+ fd = openSync(path, 'r')
180
+ const buf = Buffer.alloc(TOMBSTONE.length)
181
+ return readSync(fd, buf, 0, buf.length, 0) === buf.length && buf.toString('utf8') === TOMBSTONE
182
+ } catch {
183
+ return false
184
+ } finally {
185
+ if (fd != null) closeSync(fd)
186
+ }
187
+ }
188
+
189
+ /** why a folded store's tables may not be opened in the machine db at `path`, null when they may: its old file still
190
+ * holds it, so tables made here would be an empty store beside the live one. A path that is not the machine db (a
191
+ * store moved for one process) is never refused. An old file that does not exist gets its tombstone. An lstat, and a
192
+ * read of a few bytes */
193
+ export function foldRefusal(store: FoldedStore, path: string): string | null {
194
+ const old = oldStorePath(store)
195
+ // a store moved for one process (its variable) is never refused there
196
+ const moved = process.env[STORE_ENV[store]]
197
+ const own = path === old && (moved == null || moved === '')
198
+ if (path !== machineDbPath() && !own) return null
199
+ if (lstatSync(old, { throwIfNoEntry: false }) == null) {
200
+ // a machine that never had the file: its tombstone, so code that predates the fold never makes one there
201
+ try {
202
+ mkdirSync(dirname(old), { recursive: true })
203
+ writeFileSync(old, tombstoneText(store, path), { flag: 'wx', mode: 0o600 })
204
+ } catch {
205
+ // made by another process meanwhile, or a folder that refuses: read below
206
+ }
207
+ }
208
+ // a fold or a way back of this store on the way (home/machine-fold.ts): neither its file nor its tables are whole
209
+ const mark = foldMarkOf(store)
210
+ if (mark != null) {
211
+ if (!alive(mark.pid)) return `a ${mark.way === 'back' ? 'way back' : 'fold'} of the ${store} stopped half way at ${localClock(mark.at)} (pid ${mark.pid} is gone): sk db fold ${mark.way === 'back' ? '--back ' : ''}--go ends it`
212
+ return `${WAIT}a ${mark.way === 'back' ? 'way back' : 'fold'} of the ${store} runs since ${localClock(mark.at)}, ${waitLeft(store, old, mark)}: run it again then`
213
+ }
214
+ const repair = repairMarkOf(store)
215
+ if (repair?.phase === 'saved' || (repair != null && alive(repair.pid))) return alive(repair.pid) ? `${WAIT}a repair of the ${store} runs since ${localClock(repair.at)}: its tables ${repair.tables.join(', ')} are made again; run it again in a minute` : `a repair of the ${store} stopped at ${localClock(repair.at)} (pid ${repair.pid} is gone): sk db repair --go ends it`
216
+ if (own) {
217
+ // its own file, the fold off: read and written there once it has the store's tables
218
+ if (ownShape(store, old) === 'tables') return null
219
+ return `the ${store} is its own file ${old} in the shape of before the fold, and the daemon's fold is off: sk db fold --go folds it, sk db fold --back --go gives its file the tables this code reads`
220
+ }
221
+ if (lstatSync(old, { throwIfNoEntry: false }) == null || isTombstone(old)) return null
222
+ const hurt = machineDb((db) => db.query('select value from state where name = ?').get(`db-fold-damaged:${store}`) as { value: string } | null)
223
+ if (hurt != null) return `the ${store} is still its own file ${old}, and the fold found it damaged at ${new Date((JSON.parse(hurt.value) as { at: number }).at).toLocaleString('sv-SE').slice(0, 16)}: sk db fold says what to do`
224
+ const lock = machineDb((db) => db.query('select value from state where name = ?').get(FOLD_LOCK) as { value: string } | null)
225
+ const walker = lock == null ? null : (JSON.parse(lock.value) as { pid: number }).pid
226
+ if (walker != null && alive(walker)) return `${WAIT}a fold runs (pid ${walker}): the ${store} folds at its turn, run it again in a moment`
227
+ return `the ${store} is still its own file ${old}: \`sk db fold\` moves it into ${path}, until then it is not read`
228
+ }
229
+
230
+ /** the row of the one walk at a time (home/machine-fold.ts): the pid of the fold or the way back that runs */
231
+ export const FOLD_LOCK = 'db-fold-lock'
232
+
233
+ /** a new file of the machine's: mode 0600 (it holds prompts and commands) and incremental auto vacuum, which must be
234
+ * set before its first table, so a delete gives its pages back in steps and never by a VACUUM of the whole file */
235
+ export function machineFileCreate(path: string, db: Database): void {
236
+ if (db.query("select 1 from sqlite_master where type = 'table' limit 1").get() != null) return
237
+ db.exec('pragma auto_vacuum = incremental')
238
+ for (const f of [path, `${path}-wal`, `${path}-shm`]) try { chmodSync(f, 0o600) } catch { /* not made yet */ }
239
+ }
240
+
241
+ /** free pages one step of `vacuumSteps` gives back: about 8 MB, a few ms of the write lock */
242
+ export const VACUUM_STEP_PAGES = 2_048
243
+
244
+ /** the free pages of a file of incremental auto vacuum given back to the disk in steps, `between` awaited after each
245
+ * (a loop given back): never a VACUUM, which holds the whole file's write lock for seconds. False, nothing done, for a
246
+ * file of another auto vacuum mode. The longest step's ms */
247
+ export async function vacuumSteps(db: Database, between: () => Promise<void> = async () => {}): Promise<{ pages: number; ms: number } | false> {
248
+ const n = (q: string): number => Number(Object.values(db.query(q).get() as Record<string, number>)[0] ?? 0)
249
+ if (n('pragma auto_vacuum') !== 2) return false
250
+ // a repair cut a table out of this file: its pages are unused and unknown to the page map that a vacuum moves pages
251
+ // by, so nothing is given back until a rebuild writes the file again
252
+ const hasState = db.query("select 1 from sqlite_master where type = 'table' and name = 'state'").get() != null
253
+ if (hasState && db.query('select 1 from state where name = ?').get(REPAIR_UNUSED) != null) return false
254
+ let pages = 0
255
+ let ms = 0
256
+ try {
257
+ for (;;) {
258
+ const free = n('pragma freelist_count')
259
+ if (free === 0) break
260
+ const t = performance.now()
261
+ db.query(`pragma incremental_vacuum(${VACUUM_STEP_PAGES})`).all()
262
+ ms = Math.max(ms, performance.now() - t)
263
+ pages += free - n('pragma freelist_count')
264
+ if (n('pragma freelist_count') >= free) break
265
+ await between()
266
+ }
267
+ } catch (e) {
268
+ if (!/malformed|corrupt/i.test((e as Error).message)) throw e
269
+ // pages it cannot move: given back by a rebuild, never the end of what called it
270
+ if (hasState) db.query('insert or replace into state (name, value, at) values (?, ?, ?)').run(REPAIR_UNUSED, JSON.stringify({ why: (e as Error).message }), Date.now())
271
+ console.error(`WARN ${db.filename}: its free pages are not given back (${(e as Error).message}); sk db rebuild --go, with the home down, writes the file again whole`)
272
+ return false
273
+ }
274
+ return { pages, ms: Math.round(ms) }
275
+ }
276
+
277
+ /** the row a repair leaves while the pages of the tables it cut are unused: no vacuum runs until a rebuild clears it */
278
+ export const REPAIR_UNUSED = 'db-repair-unused'
279
+
280
+ /** the file made owner only before sqlite opens it */
281
+ export function ownerOnlyFile(path: string): void {
282
+ if (existsSync(path)) return
35
283
  try {
36
284
  mkdirSync(dirname(path), { recursive: true })
37
- db = new Database(path, { create: true, readwrite: true })
285
+ closeSync(openSync(path, 'a', 0o600))
286
+ } catch {
287
+ // a path that cannot be made: the open says so
288
+ }
289
+ }
290
+
291
+ /** a machine write waits this long on another process's */
292
+ const BUSY_MS = 2_000
293
+
294
+ /** v1 is the file with no version: the `state` table alone. v2 adds the backup's two tables: a source is keyed by
295
+ * its path under the home and whether it is a refusal, its absolute path is the home's join (media-store/backup/kept.ts
296
+ * owns what a row means) */
297
+ export const MACHINE_DB_VERSION = 2
298
+
299
+ const SCHEMA = `
300
+ create table if not exists state (name text primary key, value text not null, at integer not null);
301
+ create table if not exists backup_sources (
302
+ rel text not null,
303
+ refused integer not null,
304
+ size integer not null,
305
+ mtime integer not null,
306
+ sqlite text,
307
+ whole integer not null,
308
+ broken text,
309
+ primary key (rel, refused)
310
+ ) without rowid;
311
+ create table if not exists backup_seen (
312
+ path text primary key,
313
+ size integer not null,
314
+ mtime integer not null,
315
+ sha text not null,
316
+ key text not null
317
+ ) without rowid;
318
+ `
319
+
320
+ const versionOf = (db: Database): number => Number((db.query('pragma user_version').get() as { user_version?: number } | null)?.user_version ?? 0)
321
+
322
+ /** the file to v2, once. A file of no version that holds rows is v1, so migrateTo keeps its copy first */
323
+ function migrate(db: Database): void {
324
+ if (versionOf(db) >= MACHINE_DB_VERSION) return
325
+ if (versionOf(db) === 0 && db.query("select 1 from sqlite_master where type = 'table' and name = 'state'").get() != null && db.query('select 1 from state limit 1').get() != null) db.exec('pragma user_version = 1')
326
+ migrateTo(db, MACHINE_DB_VERSION, () => db.exec(SCHEMA))
327
+ }
328
+
329
+ function openMachine(path: string): Database {
330
+ const refused = createRefusal(path)
331
+ if (refused != null) throw new Error(refused)
332
+ ownerOnlyFile(path)
333
+ const db = new Database(path, { create: true, readwrite: true })
334
+ try {
38
335
  db.exec(`pragma busy_timeout = ${BUSY_MS}`)
336
+ machineFileCreate(path, db)
39
337
  ensureWal(db, BUSY_MS)
40
- db.exec(SCHEMA)
41
- return body(db)
42
- } catch {
338
+ migrate(db)
339
+ return db
340
+ } catch (e) {
341
+ db.close()
342
+ throw e
343
+ }
344
+ }
345
+
346
+ /** the connection a `machineHeld` body shares, by path */
347
+ let held: { path: string; db: Database } | null = null
348
+
349
+ /** every read and write of the machine db inside `body` on ONE connection, opened once and closed at the end: a look
350
+ * that reads and writes a few rows pays one open and one checkpoint, not one per row. Its writes commit without
351
+ * waiting for the disk (WAL, synchronous NORMAL): a power cut can lose the last ones, never the file */
352
+ export async function machineHeld<R>(body: () => Promise<R>): Promise<R> {
353
+ if (held != null) return body()
354
+ const path = machineDbPath()
355
+ const db = openMachine(path)
356
+ db.exec('pragma synchronous = NORMAL')
357
+ held = { path, db }
358
+ try {
359
+ return await body()
360
+ } finally {
361
+ held = null
362
+ try { db.close() } catch { /* already gone */ }
363
+ }
364
+ }
365
+
366
+ /** each failure said once on stderr per process: a read that turns to null or a write to false is never silent */
367
+ const said = new Set<string>()
368
+ function withMachine<R>(body: (db: Database) => R, path = machineDbPath()): R | null {
369
+ try {
370
+ return machineDb(body, path)
371
+ } catch (e) {
372
+ const line = `WARN the machine db ${path}: ${(e as Error).message}`
373
+ if (!said.has(line)) {
374
+ said.add(line)
375
+ process.stderr.write(`${line}\n`)
376
+ }
43
377
  return null
378
+ }
379
+ }
380
+
381
+ /** the machine db open for `body`, closed after; a db that does not open, or a body that throws, THROWS: for a
382
+ * table's reader or writer whose failure must be said, never read as "nothing" */
383
+ export function machineDb<R>(body: (db: Database) => R, path = machineDbPath()): R {
384
+ if (held?.path === path) return body(held.db)
385
+ const db = openMachine(path)
386
+ try {
387
+ return body(db)
44
388
  } finally {
45
- try { db?.close() } catch { /* never opened */ }
389
+ try { db.close() } catch { /* already gone */ }
46
390
  }
47
391
  }
48
392
 
@@ -55,9 +399,13 @@ export function machineGet<T>(name: string): T | null {
55
399
  /** the value, null when no row: a db that does not open THROWS, for a read a write builds on (a busy db read as
56
400
  * "nothing" would save one record over all the others) */
57
401
  export function machineRead<T>(name: string): T | null {
58
- const got = withMachine((db) => ({ row: db.query('select value from state where name = ?').get(name) as { value: string } | null }))
59
- if (got == null) throw new Error(`the machine db does not open: ${name} is not read`)
60
- return got.row == null ? null : (JSON.parse(got.row.value) as T)
402
+ let row: { value: string } | null
403
+ try {
404
+ row = machineDb((db) => db.query('select value from state where name = ?').get(name) as { value: string } | null)
405
+ } catch (e) {
406
+ throw new Error(`the machine db does not read: ${name} is not read (${(e as Error).message})`)
407
+ }
408
+ return row == null ? null : (JSON.parse(row.value) as T)
61
409
  }
62
410
 
63
411
  export function machineSet(name: string, value: unknown): boolean {
@@ -0,0 +1,137 @@
1
+ // Where the machine keeps its own state: ONE folder, `~/.shipkit/`, and every path of it comes from machinePath.
2
+ // A machine that has not moved (sk home move) is still read in its old root. This half reads the disk, so it
3
+ // lives apart from core/layout.ts, which cached checks import.
4
+ import { lstatSync, readFileSync, realpathSync, statSync } from 'fs'
5
+ import { homedir } from 'os'
6
+ import { join } from 'path'
7
+ import { SHIPKIT_DIR } from './layout.ts'
8
+
9
+ /** where the machine kept its home and its db before one root: `~/.config/shipkit/` */
10
+ export const OLD_MACHINE_ROOT = join('.config', 'shipkit')
11
+ /** the entries the old root holds: each one stays read there until the machine moves (sk home move) */
12
+ export const OLD_ROOT_ENTRIES: readonly string[] = ['home', 'shipkit.sqlite', 'projects-order.txt']
13
+
14
+ /** where the machine's state is now: `old`, every entry in `~/.config/shipkit`; `moved`, that folder is a link to
15
+ * `~/.shipkit` or is gone; `split`, a move stopped between two entries; `both`, one entry in both places */
16
+ export type MachineRootState = 'old' | 'moved' | 'split' | 'both'
17
+
18
+ /** the move's record (home/home-move.ts writes it first and removes it last, under the new root, which both layouts
19
+ * read): while it exists, each entry it names is where it says */
20
+ export const MOVE_RECORD = 'home-move.json'
21
+ export type MoveRecord = { v: 1; way?: 'go' | 'back'; from?: string; to?: string; mode?: number; at?: number; entries: string[]; moved?: string[]; done: boolean }
22
+
23
+ export const moveRecordPath = (home: string = homedir()): string => join(home, SHIPKIT_DIR, MOVE_RECORD)
24
+
25
+ export function readMoveRecord(home: string = homedir()): MoveRecord | null {
26
+ try {
27
+ return JSON.parse(readFileSync(moveRecordPath(home), 'utf8')) as MoveRecord
28
+ } catch {
29
+ return null
30
+ }
31
+ }
32
+
33
+ /** the root a recorded entry is in now: a move that went keeps it new once moved or done, one that goes back keeps
34
+ * it new until moved back */
35
+ const recordedAt = (r: MoveRecord, entry: string): 'old' | 'new' => (r.way === 'back' ? ((r.moved ?? []).includes(entry) ? 'old' : 'new') : r.done || (r.moved ?? []).includes(entry) ? 'new' : 'old')
36
+
37
+ type Place = { old: string; neu: string; oldIsDir: boolean; oldIsLink: boolean; linksHere: boolean }
38
+
39
+ function placeOf(home: string): Place {
40
+ const old = join(home, OLD_MACHINE_ROOT)
41
+ const neu = join(home, SHIPKIT_DIR)
42
+ const l = lstatSync(old, { throwIfNoEntry: false })
43
+ const oldIsLink = l?.isSymbolicLink() === true
44
+ let linksHere = false
45
+ if (oldIsLink) {
46
+ try { linksHere = realpathSync(old) === realpathSync(neu) } catch { linksHere = false }
47
+ }
48
+ // a link the owner made to a folder elsewhere is still the old place: only a link to the new one marks a move
49
+ const oldIsDir = !linksHere && (l?.isDirectory() === true || (oldIsLink && statSync(old, { throwIfNoEntry: false })?.isDirectory() === true))
50
+ return { old, neu, oldIsDir, oldIsLink, linksHere }
51
+ }
52
+
53
+ /** a path under a FILE answers ENOTDIR: absent, never a throw */
54
+ const has = (p: string): boolean => {
55
+ try {
56
+ return lstatSync(p, { throwIfNoEntry: false }) != null
57
+ } catch {
58
+ return false
59
+ }
60
+ }
61
+
62
+ /** where one entry of the old root is read: the old place while the old folder holds it, or holds the others and the
63
+ * new one lacks it; the new place once the old folder is a link to it, is gone, or lacks the entry the new one has */
64
+ function resolveEntry(entry: string, home: string): string {
65
+ const r = readMoveRecord(home)
66
+ if (r != null && r.entries.includes(entry)) return join(home, recordedAt(r, entry) === 'new' ? SHIPKIT_DIR : OLD_MACHINE_ROOT, entry)
67
+ const p = placeOf(home)
68
+ if (!p.oldIsDir) return join(p.neu, entry)
69
+ if (has(join(p.old, entry))) return join(p.old, entry)
70
+ if (has(join(p.neu, entry))) return join(p.neu, entry)
71
+ return join(p.old, entry)
72
+ }
73
+
74
+ const resolved = new Map<string, string>()
75
+
76
+ /** THE path of one entry of the machine's state, `~/.shipkit/<entry>`: an entry of the old root is resolved once a
77
+ * process (a few lstats), every other one is a join */
78
+ export function machinePath(entry: string, home: string = homedir()): string {
79
+ if (!OLD_ROOT_ENTRIES.includes(entry)) return join(home, SHIPKIT_DIR, entry)
80
+ const k = `${home}\0${entry}`
81
+ let p = resolved.get(k)
82
+ if (p == null) {
83
+ p = resolveEntry(entry, home)
84
+ resolved.set(k, p)
85
+ }
86
+ return p
87
+ }
88
+
89
+ /** the resolved paths forgotten: the move, and a test that builds a home of its own */
90
+ export const forgetMachinePaths = (): void => resolved.clear()
91
+
92
+ /** where the old root's entries are now, read fresh (no cache) */
93
+ export function machineRootState(home: string = homedir()): { state: MachineRootState; old: string; neu: string; oldHolds: string[]; newHolds: string[]; link: boolean } {
94
+ const p = placeOf(home)
95
+ const oldHolds = p.oldIsDir ? OLD_ROOT_ENTRIES.filter((e) => has(join(p.old, e))) : []
96
+ const newHolds = OLD_ROOT_ENTRIES.filter((e) => has(join(p.neu, e)))
97
+ const state: MachineRootState = !p.oldIsDir ? 'moved' : oldHolds.some((e) => newHolds.includes(e)) ? 'both' : newHolds.length > 0 ? 'split' : 'old'
98
+ return { state, old: p.old, neu: p.neu, oldHolds, newHolds, link: p.linksHere }
99
+ }
100
+
101
+ /** a path under the user home in its one-root form: one under `~/.config/shipkit/<entry>` of an old root entry is
102
+ * named under `~/.shipkit/<entry>`, every other path is itself. Two forms of one file compare equal through it */
103
+ export function oneRootRel(rel: string): string {
104
+ const old = `${OLD_MACHINE_ROOT}/`
105
+ if (!rel.startsWith(old) && rel !== OLD_MACHINE_ROOT) return rel
106
+ const rest = rel.slice(old.length)
107
+ return OLD_ROOT_ENTRIES.includes(rest.split('/')[0] ?? '') ? `${SHIPKIT_DIR}/${rest}` : rel
108
+ }
109
+
110
+ /** where a file named by its path under the user home is now: one under either root of an old root entry is where
111
+ * that entry is (machinePath), so a backup run of either shape lands on the machine's live state */
112
+ export function livePath(home: string, rel: string): string {
113
+ const one = oneRootRel(rel)
114
+ const parts = one.split('/')
115
+ if (parts[0] === SHIPKIT_DIR && parts.length > 1 && OLD_ROOT_ENTRIES.includes(parts[1]!)) return join(machinePath(parts[1]!, home), ...parts.slice(2))
116
+ return join(home, rel)
117
+ }
118
+
119
+ /** why no store may be CREATED at `path` now, null when it may (or exists already, or lies outside both roots'
120
+ * entries): a move of the machine's state runs (a process that resolved its paths before it would make a second,
121
+ * empty home), or the other root holds the live entry this one lacks. Every store's open asks it first, before it
122
+ * makes the folder: a few lstats and one read, on a create only */
123
+ export function createRefusal(path: string, home: string = homedir()): string | null {
124
+ if (has(path)) return null
125
+ const old = join(home, OLD_MACHINE_ROOT)
126
+ const neu = join(home, SHIPKIT_DIR)
127
+ for (const [root, other] of [[old, neu], [neu, old]] as const) {
128
+ if (!path.startsWith(`${root}/`)) continue
129
+ const entry = path.slice(root.length + 1).split('/')[0]!
130
+ if (!OLD_ROOT_ENTRIES.includes(entry)) return null
131
+ const r = readMoveRecord(home)
132
+ if (r != null && !r.done) return `a move of the machine's state runs (${moveRecordPath(home)}): no store is made under ${root} until it ends`
133
+ if (!has(join(root, entry)) && has(join(other, entry))) return `the live ${entry} is in ${other}, not ${root}: no store is made beside it`
134
+ return null
135
+ }
136
+ return null
137
+ }
@@ -0,0 +1,81 @@
1
+ // THIS PROCESS'S READING OF THE MACHINE (the measures: core/machine-busy.ts):
2
+ // the smoothed cores in use a long lived process published, else this
3
+ // process's own window, opened at import.
4
+ //
5
+ // the one constraint a reader breaks first: a window shorter than a reading
6
+ // asks is waited for once per process at most, and never in a long lived one.
7
+ import { readFileSync } from 'fs'
8
+ import { availableParallelism, loadavg } from 'os'
9
+ import { join } from 'path'
10
+ import { homeDir } from '../home/home-paths.ts'
11
+ import { atomicWrite } from './locked-write.ts'
12
+ import { BusyWatch, calmestWindow, type MachineLoad } from './machine-busy.ts'
13
+
14
+ /** `<load1>/<cores>` or `<load1>/<cores>/<busy>` in this variable pins the reading: a test of a verdict must not
15
+ * depend on what else the machine runs. Without the busy part, no CPU reading */
16
+ export const MACHINE_LOAD_ENV = 'SHIPKIT_MACHINE_LOAD'
17
+
18
+ /** a published reading older than this is the past: its writer stopped */
19
+ export const PUBLISHED_FRESH_MS = 15_000
20
+ /** how often a long lived process publishes */
21
+ export const PUBLISH_EVERY_MS = 5_000
22
+
23
+ const publishedFile = (): string | null => {
24
+ try { return join(homeDir(), 'machine-busy.json') } catch { return null }
25
+ }
26
+
27
+ /** the cores in use a long lived process published, while fresh and for these cores */
28
+ export function readPublished(file: string, cores: number, now = Date.now()): number | null {
29
+ try {
30
+ const r = JSON.parse(readFileSync(file, 'utf8')) as { busy?: unknown; cores?: unknown; at?: unknown }
31
+ return typeof r.busy === 'number' && r.cores === cores && typeof r.at === 'number' && now - r.at <= PUBLISHED_FRESH_MS ? r.busy : null
32
+ } catch {
33
+ return null
34
+ }
35
+ }
36
+
37
+ const cores = availableParallelism()
38
+ /** this process's own watch, its first window opened at import */
39
+ const watch = new BusyWatch({ cores })
40
+ let cached: { busy: number | null; at: number } | null = null
41
+
42
+ /** the smoothed reading of this process's watch, written where every short lived process reads it: a long lived
43
+ * one (the daemon) calls it on PUBLISH_EVERY_MS, so a command reads twenty seconds of the machine, not its own
44
+ * fraction of one */
45
+ export function publishMachine(file = publishedFile()): void {
46
+ const busy = watch.read(0)
47
+ if (file == null || busy == null) return
48
+ try { atomicWrite(file, JSON.stringify({ busy: Math.round(busy * 100) / 100, cores, at: Date.now() })) } catch { /* a read only home: each process reads its own window */ }
49
+ }
50
+
51
+ /** publishes on PUBLISH_EVERY_MS until the function it returns is called */
52
+ export function startPublishing(file = publishedFile()): () => void {
53
+ const t = setInterval(() => publishMachine(file), PUBLISH_EVERY_MS)
54
+ t.unref?.()
55
+ return () => clearInterval(t)
56
+ }
57
+
58
+ /** the machine now. `minMs`: the shortest own window a reading takes when none was published, waited for once
59
+ * per process at most (a verdict asks 400, a hot path keeps the default) */
60
+ /** from this window on a caller asks for a verdict (may a lane start), not a hint in a hot path */
61
+ export const VERDICT_MS = 400
62
+ let verdict: { busy: number | null } | null = null
63
+
64
+ export function machineLoad(minMs = 100): MachineLoad {
65
+ const pinned = process.env[MACHINE_LOAD_ENV]?.match(/^(\d+(?:\.\d+)?)\/(\d+)(?:\/(\d+(?:\.\d+)?))?$/)
66
+ if (pinned != null) return { load1: Number(pinned[1]), cores: Number(pinned[2]), ...(pinned[3] != null ? { busy: Number(pinned[3]) } : {}) }
67
+ const load1 = loadavg()[0] ?? 0
68
+ const now = Date.now()
69
+ if (cached == null || now - cached.at > 1_000) {
70
+ const file = publishedFile()
71
+ cached = { busy: file == null ? null : readPublished(file, cores, now), at: now }
72
+ }
73
+ // a verdict with nothing published: two windows of its own, the calmer one, read once a process
74
+ if (cached.busy == null && minMs >= VERDICT_MS) verdict ??= { busy: calmestWindow({ cores, windowMs: minMs / 2, wait: (ms) => Bun.sleepSync(Math.ceil(ms)) }) }
75
+ let busy = cached.busy ?? verdict?.busy ?? watch.read(minMs)
76
+ if (busy == null && watch.age() < minMs) {
77
+ Bun.sleepSync(Math.ceil(minMs - watch.age()))
78
+ busy = watch.read(minMs)
79
+ }
80
+ return busy == null ? { load1, cores } : { load1, cores, busy }
81
+ }
@@ -52,7 +52,7 @@ export function layoutSummary(r: { moved: LayoutMove[]; removed: string[] }): st
52
52
  }
53
53
 
54
54
  /** the contract names a typo is matched against */
55
- const KNOWN = ['identity', 'assets', 'comfy', 'private', 'reflections', 'social', 'journal', 'domain', 'itch', 'steam', 'discord'] as const
55
+ const KNOWN = ['identity', 'assets', 'comfy', 'private', 'reflections', 'social', 'journal', 'domain', 'itch', 'steam', 'discord', 'chrome-web-store'] as const
56
56
 
57
57
  /** root files shipkit used to generate on every run */
58
58
  export const LEGACY_GENERATED = ['STATUS.md', 'STATUS.md.lock', 'STATUS.full.md', 'STATUS.full.md.lock'] as const