rvlib-shipkit 2.25.0 → 2.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (456) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/package.json +2 -2
  3. package/src/agent-time/diet.ts +77 -0
  4. package/src/agent-time/family.ts +72 -0
  5. package/src/agent-time/hooks.ts +331 -0
  6. package/src/agent-time/limits.ts +44 -0
  7. package/src/agent-time/live.ts +1 -3
  8. package/src/agent-time/mask.ts +96 -18
  9. package/src/agent-time/refused.ts +45 -0
  10. package/src/agent-time/render.ts +130 -17
  11. package/src/agent-time/report-cache.ts +57 -0
  12. package/src/agent-time/report.ts +349 -23
  13. package/src/agent-time/shape.ts +50 -3
  14. package/src/agent-time/store.ts +370 -95
  15. package/src/agent-time/timeline.ts +140 -21
  16. package/src/agent-time/turns.ts +18 -8
  17. package/src/annex/offload.ts +17 -6
  18. package/src/annex/tidy.ts +70 -10
  19. package/src/asks/ask-after.ts +40 -0
  20. package/src/asks/asks-queue.ts +205 -47
  21. package/src/assets/kit-config.ts +7 -3
  22. package/src/assets/kits/identity-kit.ts +289 -10
  23. package/src/check.ts +3 -1
  24. package/src/checks/SK004-gitignore-policy.check.ts +12 -5
  25. package/src/checks/SK013-one-way-in-policy.check.ts +8 -3
  26. package/src/checks/SK026-asks-autoclose.check.ts +5 -5
  27. package/src/checks/SK028-readme.check.ts +7 -66
  28. package/src/checks/SK030-cache-audit.check.ts +6 -1
  29. package/src/checks/SK031-trace-audit.check.ts +2 -0
  30. package/src/checks/SK033-global-contribution.check.ts +3 -1
  31. package/src/checks/SK035-public-clean.check.ts +2 -2
  32. package/src/checks/SK043-scratch-on-the-mac.check.ts +59 -4
  33. package/src/checks/SK045-slow-hooks.check.ts +47 -0
  34. package/src/checks/SK046-hook-records.check.ts +76 -0
  35. package/src/checks/SK047-login-in-description.check.ts +36 -0
  36. package/src/checks/SK048-one-local-file.check.ts +36 -0
  37. package/src/cli/cmd_Asks.ts +71 -5
  38. package/src/cli/cmd_Backup.ts +5 -3
  39. package/src/cli/cmd_Capacity.ts +2 -2
  40. package/src/cli/cmd_CrossRepoActivity.ts +5 -4
  41. package/src/cli/cmd_Db.ts +253 -0
  42. package/src/cli/cmd_History.ts +212 -0
  43. package/src/cli/cmd_Home.ts +31 -3
  44. package/src/cli/cmd_Hq.ts +516 -0
  45. package/src/cli/cmd_Machines.ts +36 -0
  46. package/src/cli/cmd_Media.ts +38 -6
  47. package/src/cli/cmd_Mv.ts +1 -1
  48. package/src/cli/cmd_Reflect.ts +23 -2
  49. package/src/cli/cmd_Releases.ts +30 -0
  50. package/src/cli/cmd_Review.ts +569 -118
  51. package/src/cli/cmd_Secret.ts +62 -12
  52. package/src/cli/cmd_Social.ts +83 -1
  53. package/src/cli/cmd_Space.ts +324 -0
  54. package/src/cli/cmd_Time.ts +82 -5
  55. package/src/cli/cmd_Tmp.ts +28 -4
  56. package/src/cli/cmd_Trailer.ts +14 -10
  57. package/src/cli/cmd_Urls.ts +56 -0
  58. package/src/cli/cmd_Vault.ts +583 -0
  59. package/src/cli/family-help.ts +4 -2
  60. package/src/cli/family-pages.ts +1 -1
  61. package/src/cli/registry.ts +26 -4
  62. package/src/collections/actions.ts +63 -13
  63. package/src/collections/collection-types.ts +23 -1
  64. package/src/collections/providers.ts +117 -23
  65. package/src/collections/review.ts +4 -4
  66. package/src/commands/runner.ts +5 -1
  67. package/src/contracts/contract-manifests.ts +6 -5
  68. package/src/contracts/contract-types.ts +6 -4
  69. package/src/contracts/contracts-engine.ts +11 -0
  70. package/src/contracts/manifests/chrome-web-store.contract.ts +31 -0
  71. package/src/core/budget.ts +12 -17
  72. package/src/core/capacity.ts +27 -18
  73. package/src/core/check-runner.ts +7 -4
  74. package/src/core/history-files.ts +9 -5
  75. package/src/core/layout.ts +3 -0
  76. package/src/core/machine-busy.ts +127 -0
  77. package/src/core/machine-commit.ts +6 -1
  78. package/src/core/machine-db.ts +368 -20
  79. package/src/core/machine-layout.ts +137 -0
  80. package/src/core/machine-read.ts +81 -0
  81. package/src/core/migrate-layout.ts +1 -1
  82. package/src/core/one-way-in-policy.ts +64 -7
  83. package/src/core/polling-policy.ts +27 -0
  84. package/src/core/retention.ts +16 -2
  85. package/src/core/scope.ts +107 -14
  86. package/src/core/signal-name.ts +6 -0
  87. package/src/core/slots.ts +5 -4
  88. package/src/core/trace-audit.ts +6 -1
  89. package/src/core/transient.ts +6 -0
  90. package/src/drill/clean.ts +25 -1
  91. package/src/drill/run.ts +2 -2
  92. package/src/drill/seal.ts +27 -4
  93. package/src/home/activity-shape.ts +2 -0
  94. package/src/home/activity.ts +66 -3
  95. package/src/home/app-sign.ts +15 -4
  96. package/src/home/daemon-jobs.ts +149 -7
  97. package/src/home/database-projects.ts +111 -0
  98. package/src/home/database-words.ts +203 -0
  99. package/src/home/databases.ts +460 -0
  100. package/src/home/global-units.ts +6 -0
  101. package/src/home/gui-app/ShipkitGui.swift +122 -1
  102. package/src/home/gui-app.ts +6 -3
  103. package/src/home/home-move.ts +514 -0
  104. package/src/home/home-paths.ts +12 -3
  105. package/src/home/home-routes.ts +68 -0
  106. package/src/home/inbox-shape.ts +24 -14
  107. package/src/home/machine-fold.ts +820 -0
  108. package/src/home/machine-host.ts +20 -5
  109. package/src/home/machine-repair.ts +503 -0
  110. package/src/home/machine-stores.ts +66 -15
  111. package/src/home/machine-temp.ts +547 -86
  112. package/src/home/resume.ts +79 -0
  113. package/src/home/run-daemon.ts +25 -4
  114. package/src/home/run-pm.ts +18 -5
  115. package/src/home/serve-daemon.ts +18 -3
  116. package/src/home/serve-web.ts +175 -7
  117. package/src/home/space-job.ts +115 -0
  118. package/src/home/space-page.ts +257 -0
  119. package/src/home/space-route.ts +167 -0
  120. package/src/home/space-words.ts +103 -0
  121. package/src/home/time-page-shape.ts +202 -0
  122. package/src/home/time-page.ts +64 -0
  123. package/src/home/vault-page.ts +482 -0
  124. package/src/home/vault-words.ts +132 -0
  125. package/src/hooks/leak-guard.ts +40 -13
  126. package/src/hooks/one-way-in-guard.ts +17 -4
  127. package/src/hq/cards.ts +581 -0
  128. package/src/hq/door-route.ts +43 -0
  129. package/src/hq/door.ts +163 -0
  130. package/src/hq/files-send.ts +348 -0
  131. package/src/hq/forgotten-log.ts +35 -0
  132. package/src/hq/history-check.ts +255 -0
  133. package/src/hq/history-place.ts +146 -0
  134. package/src/hq/history-send.ts +487 -0
  135. package/src/hq/hq-db.ts +99 -0
  136. package/src/hq/hq-local.ts +469 -0
  137. package/src/hq/hq-routes.ts +196 -0
  138. package/src/hq/hq-store.ts +599 -0
  139. package/src/hq/kinds/ask.ts +147 -0
  140. package/src/hq/kinds/card-gone.ts +83 -0
  141. package/src/hq/kinds/card-msg.ts +66 -0
  142. package/src/hq/kinds/card.ts +99 -0
  143. package/src/hq/kinds/index.ts +21 -0
  144. package/src/hq/kinds/kind.ts +126 -0
  145. package/src/hq/kinds/media.ts +125 -0
  146. package/src/hq/kinds/prompt.ts +219 -0
  147. package/src/hq/kinds/reflection.ts +128 -0
  148. package/src/hq/kinds/report.ts +55 -0
  149. package/src/hq/kinds/time-day.ts +236 -0
  150. package/src/hq/ledger.ts +473 -0
  151. package/src/hq/machine-key.ts +44 -0
  152. package/src/hq/machine-notes.ts +131 -0
  153. package/src/hq/outbox.ts +372 -0
  154. package/src/hq/replica.ts +168 -0
  155. package/src/hq/repo-id.ts +4 -0
  156. package/src/hq/report-facts.ts +117 -0
  157. package/src/hq/report.ts +274 -0
  158. package/src/hq/switch.ts +151 -0
  159. package/src/hq/vault-grant.ts +92 -0
  160. package/src/identities/identities.ts +15 -3
  161. package/src/identities/steps.ts +31 -0
  162. package/src/ledger/db.ts +73 -26
  163. package/src/ledger/gate-checks.ts +29 -4
  164. package/src/ledger/git-key.ts +3 -3
  165. package/src/ledger/git-scan.ts +49 -7
  166. package/src/ledger/run-log.ts +11 -3
  167. package/src/ledger/store.ts +26 -4
  168. package/src/media-store/backup/daemon.ts +36 -17
  169. package/src/media-store/backup/kept.ts +241 -0
  170. package/src/media-store/backup/restore.ts +14 -4
  171. package/src/media-store/backup/run.ts +11 -11
  172. package/src/media-store/backup/secret.ts +6 -4
  173. package/src/media-store/backup/snapshot.ts +4 -2
  174. package/src/media-store/backup/sources.ts +68 -20
  175. package/src/media-store/copy/config.ts +77 -0
  176. package/src/media-store/copy/copy.ts +194 -0
  177. package/src/media-store/copy/lifecycle.ts +43 -0
  178. package/src/media-store/copy/night.ts +90 -0
  179. package/src/media-store/copy/proof.ts +151 -0
  180. package/src/media-store/copy/snapshot.ts +152 -0
  181. package/src/media-store/copy/store.ts +62 -0
  182. package/src/media-store/copy/stream.ts +176 -0
  183. package/src/media-store/daemon.ts +66 -16
  184. package/src/media-store/db.ts +22 -11
  185. package/src/media-store/forget.ts +23 -3
  186. package/src/media-store/hand.ts +4 -3
  187. package/src/media-store/hq-find.ts +75 -0
  188. package/src/media-store/hq-send.ts +277 -0
  189. package/src/media-store/kind.ts +2 -0
  190. package/src/media-store/purge.ts +18 -15
  191. package/src/media-store/put.ts +24 -8
  192. package/src/media-store/remote.ts +16 -7
  193. package/src/media-store/space-listing.ts +240 -0
  194. package/src/media-store/space.ts +40 -21
  195. package/src/media-store/sync.ts +5 -5
  196. package/src/media-store/tidy.ts +98 -0
  197. package/src/media-store/upload.ts +3 -3
  198. package/src/media-store/webp.ts +66 -0
  199. package/src/messages/model.ts +16 -7
  200. package/src/messages/routes.ts +14 -4
  201. package/src/messages/score.ts +2 -2
  202. package/src/messages/suggest.ts +96 -0
  203. package/src/pages/frame-client.ts +10 -0
  204. package/src/plan/planner.ts +28 -17
  205. package/src/recordings/media-kind.ts +1 -0
  206. package/src/recordings/model.ts +1 -1
  207. package/src/recordings/recorder-app/Capture.swift +9 -7
  208. package/src/reflect/hq-corpus.ts +56 -0
  209. package/src/reflect/prompt-corpus.ts +3 -1
  210. package/src/reflect/transcript.ts +146 -36
  211. package/src/release/machine-words.ts +67 -0
  212. package/src/release/public-clean.ts +8 -3
  213. package/src/releases/from-card.ts +1 -1
  214. package/src/releases/model.ts +18 -0
  215. package/src/releases/routes.ts +10 -5
  216. package/src/releases/store.ts +23 -2
  217. package/src/repo-config.ts +42 -2
  218. package/src/review/anim.ts +2 -2
  219. package/src/review/auto.ts +2 -2
  220. package/src/review/card-db.ts +244 -0
  221. package/src/review/card-rows.ts +266 -0
  222. package/src/review/cells.ts +76 -5
  223. package/src/review/draft-card.ts +41 -0
  224. package/src/review/draft-model.ts +78 -0
  225. package/src/review/format.ts +29 -10
  226. package/src/review/gone.ts +98 -0
  227. package/src/review/inbox-hook.ts +35 -0
  228. package/src/review/marks.ts +1 -1
  229. package/src/review/media-kind.ts +7 -5
  230. package/src/review/media-read.ts +9 -7
  231. package/src/review/media.ts +32 -5
  232. package/src/review/model.ts +214 -45
  233. package/src/review/move.ts +491 -0
  234. package/src/review/notes.ts +12 -20
  235. package/src/review/previews.ts +15 -19
  236. package/src/review/project.ts +1 -1
  237. package/src/review/rev.ts +8 -0
  238. package/src/review/role.ts +1 -1
  239. package/src/review/routes.ts +64 -13
  240. package/src/review/stage-card.ts +8 -4
  241. package/src/review/stage-routes.ts +1 -1
  242. package/src/review/store.ts +529 -83
  243. package/src/review/thread-fold.ts +51 -0
  244. package/src/review/threads.ts +147 -9
  245. package/src/review/trailer-auto-mock.ts +63 -0
  246. package/src/review/trailer-card.ts +4 -4
  247. package/src/review/trailer-desk.ts +41 -7
  248. package/src/review/trailer-media.ts +1 -0
  249. package/src/review/trailer-model.ts +8 -0
  250. package/src/review/trailer-notes.ts +1 -1
  251. package/src/review/trailer-routes.ts +9 -1
  252. package/src/review/unit.ts +6 -6
  253. package/src/review/watchers.ts +1 -1
  254. package/src/rules/untrack-generated.ts +92 -8
  255. package/src/secrets/held.ts +16 -0
  256. package/src/secrets/keychain.ts +113 -0
  257. package/src/secrets/list.ts +16 -6
  258. package/src/secrets/manager.ts +24 -10
  259. package/src/secrets/read.ts +257 -0
  260. package/src/secrets/who-reads.ts +49 -0
  261. package/src/serve/door-forward.ts +268 -0
  262. package/src/serve/door-names.ts +47 -0
  263. package/src/serve/embed.ts +1 -1
  264. package/src/serve/hub-app/activity-view.tsx +56 -32
  265. package/src/serve/hub-app/activity.css +7 -1
  266. package/src/serve/hub-app/asks-page.tsx +48 -0
  267. package/src/serve/hub-app/asks-st.ts +25 -0
  268. package/src/serve/hub-app/clock-svc.ts +1 -2
  269. package/src/serve/hub-app/col-card.tsx +64 -13
  270. package/src/serve/hub-app/col-list-page.tsx +122 -0
  271. package/src/serve/hub-app/col-list-st.ts +103 -0
  272. package/src/serve/hub-app/col-tables-view.tsx +10 -41
  273. package/src/serve/hub-app/col-tables.ts +76 -8
  274. package/src/serve/hub-app/cols-svc.ts +70 -16
  275. package/src/serve/hub-app/database-st.ts +260 -0
  276. package/src/serve/hub-app/database-view.tsx +289 -0
  277. package/src/serve/hub-app/database.css +32 -0
  278. package/src/serve/hub-app/entry-bar.css +14 -0
  279. package/src/serve/hub-app/entry-bar.tsx +33 -0
  280. package/src/serve/hub-app/entry-pick.ts +31 -0
  281. package/src/serve/hub-app/floating.tsx +1 -0
  282. package/src/serve/hub-app/hash-route.ts +11 -1
  283. package/src/serve/hub-app/header-bar.tsx +14 -18
  284. package/src/serve/hub-app/home-st.ts +89 -9
  285. package/src/serve/hub-app/home-strip.css +5 -19
  286. package/src/serve/hub-app/home-strip.tsx +58 -92
  287. package/src/serve/hub-app/home.html +7 -0
  288. package/src/serve/hub-app/hub-app.tsx +31 -142
  289. package/src/serve/hub-app/hub-types.ts +3 -1
  290. package/src/serve/hub-app/hub.css +109 -23
  291. package/src/serve/hub-app/icons.tsx +18 -1
  292. package/src/serve/hub-app/identities-st.ts +56 -6
  293. package/src/serve/hub-app/identities-view.tsx +100 -58
  294. package/src/serve/hub-app/identities.css +17 -15
  295. package/src/serve/hub-app/identity-address.ts +5 -0
  296. package/src/serve/hub-app/inbox-st.ts +41 -24
  297. package/src/serve/hub-app/inbox-view.tsx +37 -41
  298. package/src/serve/hub-app/inbox.css +2 -5
  299. package/src/serve/hub-app/index.html +7 -0
  300. package/src/serve/hub-app/kept-cols.tsx +7 -0
  301. package/src/serve/hub-app/keys.ts +28 -15
  302. package/src/serve/hub-app/later.tsx +26 -0
  303. package/src/serve/hub-app/list-filter.tsx +14 -0
  304. package/src/serve/hub-app/list-walk.ts +72 -3
  305. package/src/serve/hub-app/machines-st.ts +251 -0
  306. package/src/serve/hub-app/machines-view.tsx +224 -0
  307. package/src/serve/hub-app/machines.css +76 -0
  308. package/src/serve/hub-app/md-copy.tsx +79 -0
  309. package/src/serve/hub-app/media-view.tsx +2 -0
  310. package/src/serve/hub-app/messages/messages-page.tsx +79 -7
  311. package/src/serve/hub-app/messages/messages-st.ts +99 -3
  312. package/src/serve/hub-app/messages.css +14 -0
  313. package/src/serve/hub-app/page-keys.ts +175 -0
  314. package/src/serve/hub-app/pages-frames.tsx +11 -3
  315. package/src/serve/hub-app/palette-st.ts +58 -11
  316. package/src/serve/hub-app/palette-view.tsx +3 -1
  317. package/src/serve/hub-app/project-list.css +54 -8
  318. package/src/serve/hub-app/project-list.tsx +128 -59
  319. package/src/serve/hub-app/projects-svc.ts +78 -7
  320. package/src/serve/hub-app/reader-chips.tsx +13 -0
  321. package/src/serve/hub-app/readme-view.tsx +2 -3
  322. package/src/serve/hub-app/readme.css +27 -5
  323. package/src/serve/hub-app/reflections-page.tsx +42 -0
  324. package/src/serve/hub-app/reflections-st.ts +17 -0
  325. package/src/serve/hub-app/releases/releases-page.tsx +25 -2
  326. package/src/serve/hub-app/releases/releases-st.ts +1 -1
  327. package/src/serve/hub-app/repo-sections.ts +9 -5
  328. package/src/serve/hub-app/repo-st.ts +35 -8
  329. package/src/serve/hub-app/review/draft-card.tsx +163 -0
  330. package/src/serve/hub-app/review/kit.tsx +50 -7
  331. package/src/serve/hub-app/review/note-place.ts +1 -1
  332. package/src/serve/hub-app/review/review-board.tsx +2 -2
  333. package/src/serve/hub-app/review/review-cells.tsx +145 -65
  334. package/src/serve/hub-app/review/review-kit.ts +27 -1
  335. package/src/serve/hub-app/review/review-list.tsx +68 -30
  336. package/src/serve/hub-app/review/review-page.tsx +7 -8
  337. package/src/serve/hub-app/review/review-st.ts +111 -24
  338. package/src/serve/hub-app/review/review-thread.tsx +49 -19
  339. package/src/serve/hub-app/review/review-views.tsx +475 -144
  340. package/src/serve/hub-app/review/thread-control-st.ts +115 -0
  341. package/src/serve/hub-app/review/thread-control.tsx +74 -0
  342. package/src/serve/hub-app/review/thread-st.ts +73 -26
  343. package/src/serve/hub-app/review/trailer-card.tsx +1 -1
  344. package/src/serve/hub-app/review/trailer-page.tsx +37 -16
  345. package/src/serve/hub-app/review/trailer-st.ts +41 -7
  346. package/src/serve/hub-app/review.css +267 -61
  347. package/src/serve/hub-app/side-lists.ts +72 -0
  348. package/src/serve/hub-app/sidebar.tsx +6 -3
  349. package/src/serve/hub-app/site/site-part.tsx +2 -8
  350. package/src/serve/hub-app/site/site-st.ts +4 -4
  351. package/src/serve/hub-app/social/comms-page.tsx +20 -5
  352. package/src/serve/hub-app/social/kit.tsx +30 -4
  353. package/src/serve/hub-app/social/post-card.tsx +139 -49
  354. package/src/serve/hub-app/social/serial-writes.ts +13 -0
  355. package/src/serve/hub-app/social/social-model.ts +11 -0
  356. package/src/serve/hub-app/social/social-page.tsx +94 -35
  357. package/src/serve/hub-app/social/social-st.ts +120 -36
  358. package/src/serve/hub-app/social/stage-popup.tsx +1 -1
  359. package/src/serve/hub-app/social/stage-st.ts +1 -1
  360. package/src/serve/hub-app/social/step-layout.ts +1 -1
  361. package/src/serve/hub-app/social.css +3 -0
  362. package/src/serve/hub-app/space-st.ts +209 -0
  363. package/src/serve/hub-app/space-view.tsx +196 -0
  364. package/src/serve/hub-app/space.css +27 -0
  365. package/src/serve/hub-app/stores/stores-page.tsx +91 -23
  366. package/src/serve/hub-app/stores/stores-st.ts +44 -19
  367. package/src/serve/hub-app/stores.css +6 -1
  368. package/src/serve/hub-app/theme-st.ts +22 -9
  369. package/src/serve/hub-app/time-charts.tsx +291 -0
  370. package/src/serve/hub-app/time-pages.tsx +749 -0
  371. package/src/serve/hub-app/time-st.ts +162 -0
  372. package/src/serve/hub-app/time-view.tsx +76 -0
  373. package/src/serve/hub-app/time.css +138 -0
  374. package/src/serve/hub-app/tree-build.ts +52 -42
  375. package/src/serve/hub-app/tree-row.tsx +4 -4
  376. package/src/serve/hub-app/tree-svc.ts +18 -9
  377. package/src/serve/hub-app/urls-svc.ts +154 -0
  378. package/src/serve/hub-app/urls-view.tsx +117 -0
  379. package/src/serve/hub-app/urls.css +30 -0
  380. package/src/serve/hub-app/vault-st.ts +127 -0
  381. package/src/serve/hub-app/vault-view.tsx +154 -0
  382. package/src/serve/hub-app/vault.css +38 -0
  383. package/src/serve/hub-app/veille/veille-st.ts +12 -10
  384. package/src/serve/hub-app/waiting-svc.ts +29 -0
  385. package/src/serve/hub-app/wikis-view.tsx +2 -2
  386. package/src/serve/hub-runtime.ts +15 -2
  387. package/src/serve/project-gutter-route.ts +27 -14
  388. package/src/serve/project-gutter.ts +25 -12
  389. package/src/serve/same-origin.ts +10 -0
  390. package/src/serve/serve-hub.ts +13 -4
  391. package/src/shipkit-files.ts +16 -2
  392. package/src/shipkit.ts +1 -1
  393. package/src/shot/shooter.ts +616 -420
  394. package/src/shot/shot-ask.ts +246 -155
  395. package/src/site/desk.ts +2 -2
  396. package/src/smoke/run-smoke.ts +3 -2
  397. package/src/social/cover.ts +202 -0
  398. package/src/social/discord-layout.ts +37 -13
  399. package/src/social/drafts.ts +3 -0
  400. package/src/social/load.ts +23 -2
  401. package/src/social/media-kind.ts +2 -1
  402. package/src/social/publish/discord.ts +5 -2
  403. package/src/social/publish/secret.ts +2 -2
  404. package/src/social/release-stage.ts +28 -13
  405. package/src/social/routes.ts +26 -1
  406. package/src/social/stage.ts +1 -1
  407. package/src/social/stats.ts +12 -9
  408. package/src/social/strip.ts +4 -1
  409. package/src/social/thumbnail.ts +255 -0
  410. package/src/stores/collection.ts +15 -11
  411. package/src/stores/desk.ts +236 -42
  412. package/src/stores/model.ts +72 -6
  413. package/src/stores/routes.ts +37 -12
  414. package/src/stores/unit.ts +14 -8
  415. package/src/suite/judge-suites.ts +113 -16
  416. package/src/suite/select.ts +45 -3
  417. package/src/suite/suite-cli.ts +81 -34
  418. package/src/trailer/bed.ts +1 -1
  419. package/src/trailer/build.ts +134 -21
  420. package/src/trailer/ffmpeg.ts +7 -0
  421. package/src/trailer/fonts.ts +3 -2
  422. package/src/trailer/grammar.ts +201 -2
  423. package/src/trailer/graph.ts +42 -5
  424. package/src/trailer/look.ts +21 -0
  425. package/src/trailer/mock.ts +154 -0
  426. package/src/trailer/passage.ts +65 -26
  427. package/src/trailer/score/heard.ts +89 -0
  428. package/src/trailer/score/sound.ts +14 -8
  429. package/src/trailer/sound/run.ts +16 -6
  430. package/src/trailer/stitch.ts +26 -5
  431. package/src/trailer/titles.ts +82 -19
  432. package/src/trailer/versions.ts +1 -3
  433. package/src/trailer/vertical.ts +3 -1
  434. package/src/urls/frame.ts +195 -0
  435. package/src/urls/routes.ts +43 -0
  436. package/src/urls/rows.ts +171 -0
  437. package/src/urls/unit.ts +73 -0
  438. package/src/vault/house-keys.ts +42 -0
  439. package/src/vault/layout.ts +50 -0
  440. package/src/vault/policy.ts +165 -0
  441. package/src/vault/vault-audit.ts +276 -0
  442. package/src/vault/vault-client.ts +40 -0
  443. package/src/vault/vault-config.ts +159 -0
  444. package/src/vault/vault-destroy.ts +78 -0
  445. package/src/vault/vault-handover.ts +305 -0
  446. package/src/vault/vault-http.ts +352 -0
  447. package/src/vault/vault-import.ts +208 -0
  448. package/src/vault/vault-init.ts +328 -0
  449. package/src/vault/vault-minter.ts +271 -0
  450. package/src/vault/vault-restore.ts +333 -0
  451. package/src/vault/vault-serve.ts +342 -0
  452. package/src/vault/vault-snapshot.ts +281 -0
  453. package/src/veille/asks.ts +1 -1
  454. package/src/veille/desk.ts +2 -2
  455. package/src/wiki/run-wiki.ts +2 -1
  456. package/src/wiki/scan-md.ts +42 -2
@@ -0,0 +1,469 @@
1
+ // the daemon's side of the hq (wiki tools/shipkit/hq.md): the hq it carries when it joined no other, the token it
2
+ // keeps, the report it sends. One writer a file: the daemon process holds hq.sqlite, and the report's child holds
3
+ // the outbox and talks to the hq over http like a daemon of another machine
4
+ import { chmodSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'fs'
5
+ import { userInfo } from 'os'
6
+ import { join } from 'path'
7
+ import { machineDelete, machineGet, machineSet } from '../core/machine-db.ts'
8
+ import { daemonPort, homeDir, homePort, hqPort } from '../home/home-paths.ts'
9
+ import { REACH_HEADER } from '../serve/same-origin.ts'
10
+ import { machineName, readTailscaleStatus } from '../home/machine-host.ts'
11
+ import { hqDbPath, openHq } from './hq-db.ts'
12
+ import { hqRoute, httpHq, httpJoin } from './hq-routes.ts'
13
+ import { fileForgottenLog, forgottenLogPath } from './forgotten-log.ts'
14
+ import { grantsProblem, HqStore, INVITE_MS, type FeedMark, type Grant, type MachineView, type Refused } from './hq-store.ts'
15
+ import { machineKey } from './machine-key.ts'
16
+ import { machineNotes, type MachineNote } from './machine-notes.ts'
17
+ import { flush, openDaemonDb, Outbox } from './outbox.ts'
18
+ import { buildReport } from './report.ts'
19
+ import type { AskSeen } from './files-send.ts'
20
+ import { markVault, settleVault, vaultDrift, type VaultMinter } from './vault-grant.ts'
21
+ import { nameProblem } from '../core/scope.ts'
22
+ import { vaultInitialised, vaultPlace, vaultUrl } from '../vault/vault-config.ts'
23
+ import { VaultApi } from '../vault/vault-http.ts'
24
+ import type { CompareSummary } from '../vault/vault-import.ts'
25
+ import { minterOf } from '../vault/vault-minter.ts'
26
+
27
+ /** the machine db row that names an hq on another machine: with none, this daemon carries its own */
28
+ export const HQ_URL = 'hq-url'
29
+ export const HQ_TOKEN_ENV = 'SHIPKIT_HQ_TOKEN'
30
+
31
+ /** the machine db switch of an hq that answers other machines: on, the daemon opens the hq's port (below) */
32
+ export const HQ_OPEN = 'hq-open'
33
+
34
+ export const carriesHq = (): boolean => machineGet<string>(HQ_URL) == null
35
+ export const hqOpen = (): boolean => machineGet<boolean>(HQ_OPEN) === true
36
+
37
+ export const hqUrl = (): string => machineGet<string>(HQ_URL) ?? `http://127.0.0.1:${daemonPort()}`
38
+
39
+ const tokenFile = (): string => join(homeDir(), 'hq-token')
40
+ const vaultTokenFile = (): string => join(homeDir(), 'vault-token')
41
+ export const VAULT_TOKEN_ENV = 'SHIPKIT_VAULT_TOKEN'
42
+
43
+ /** this daemon's token for its hq, null before its join */
44
+ export function readHqToken(): string | null {
45
+ const env = process.env[HQ_TOKEN_ENV]
46
+ if (env) return env
47
+ try {
48
+ return readFileSync(tokenFile(), 'utf8').trim() || null
49
+ } catch {
50
+ return null
51
+ }
52
+ }
53
+
54
+ /** a token kept for this OS user alone: written beside its place, closed to others, then moved in */
55
+ export function keepPrivateFile(file: string, token: string): void {
56
+ mkdirSync(homeDir(), { recursive: true })
57
+ const tmp = `${file}.tmp-${process.pid}`
58
+ writeFileSync(tmp, `${token}\n`, { mode: 0o600 })
59
+ chmodSync(tmp, 0o600)
60
+ renameSync(tmp, file)
61
+ }
62
+
63
+ export const keepHqToken = (token: string): void => keepPrivateFile(tokenFile(), token)
64
+
65
+ /** this daemon's vault token, null before its hq gave one */
66
+ export function readVaultToken(): string | null {
67
+ const env = process.env[VAULT_TOKEN_ENV]
68
+ if (env) return env
69
+ try {
70
+ return readFileSync(vaultTokenFile(), 'utf8').trim() || null
71
+ } catch {
72
+ return null
73
+ }
74
+ }
75
+
76
+ /** a new vault token from this daemon's hq, kept beside the hq token. A token lives a day: a daemon asks again well
77
+ * before (2e's job), and each ask revokes the one it held */
78
+ export async function refreshVaultToken(): Promise<{ expires: number } | Refused> {
79
+ const hq = myHq()
80
+ if ('refused' in hq) return hq
81
+ const r = await hq.vaultToken()
82
+ if ('refused' in r) return r
83
+ keepPrivateFile(vaultTokenFile(), r.token)
84
+ return { expires: r.expires }
85
+ }
86
+
87
+ /** a vault token lives a day from its mint: the daemon asks a new one once it is this old */
88
+ export const VAULT_TOKEN_AGE_MS = 12 * 3600_000
89
+
90
+ /** the age of the vault token this daemon keeps, null with none (or one given by the environment, never renewed) */
91
+ function vaultTokenAge(now: number): number | null {
92
+ if (process.env[VAULT_TOKEN_ENV]) return 0
93
+ try {
94
+ return now - statSync(vaultTokenFile()).mtimeMs
95
+ } catch {
96
+ return null
97
+ }
98
+ }
99
+
100
+ /** the vault this daemon reads: the one beside the hq it carries, on loopback. An hq on another machine has no door
101
+ * to its vault yet, so such a daemon fills nothing and every read stays as before */
102
+ export const vaultJobDue = (): boolean => carriesHq() && vaultInitialised()
103
+
104
+ /** the vault job's body, in its child: the hq's rows settled (through the daemon, the one writer of its file), then
105
+ * this daemon's vault token asked again when it is half a day old or the vault refuses it. It never touches a keychain */
106
+ export async function vaultJob(o: { print: (l: string) => void; printErr: (l: string) => void; now?: number }): Promise<void> {
107
+ const now = o.now ?? Date.now()
108
+ if (!vaultJobDue()) return
109
+ const checked = await askVaultCheck()
110
+ if ('refused' in checked) o.printErr(`WARN vault: the hq's rows were not settled: ${checked.refused}`)
111
+ const age = vaultTokenAge(now)
112
+ const token = readVaultToken()
113
+ const alive = token != null && (await new VaultApi(vaultUrl(vaultPlace()), token).lookupSelf().then(() => true, () => false))
114
+ if (age == null || age >= VAULT_TOKEN_AGE_MS || !alive) {
115
+ const r = await refreshVaultToken()
116
+ if ('refused' in r) o.printErr(`WARN vault: no vault token for this daemon: ${r.refused}`)
117
+ }
118
+ const { vaultSnapshotsOn } = await import('./switch.ts')
119
+ if (vaultSnapshotsOn()) await vaultSnapshotJob(o, now)
120
+ }
121
+
122
+ /** the vault's sealed copy in the Space, once a day and after a change (vault/vault-snapshot.ts) */
123
+ async function vaultSnapshotJob(o: { print: (l: string) => void; printErr: (l: string) => void }, now: number): Promise<void> {
124
+ const { snapshotTick } = await import('../vault/vault-snapshot.ts')
125
+ const { minterOf } = await import('../vault/vault-minter.ts')
126
+ const { baoBin, keychainSeal } = await import('../vault/vault-serve.ts')
127
+ const { minterTokenFile } = await import('../vault/vault-config.ts')
128
+ const { machineSpace } = await import('../media-store/space.ts')
129
+ const { machineName } = await import('../home/machine-host.ts')
130
+ const place = vaultPlace()
131
+ const minter = minterOf(place)
132
+ const bin = baoBin()
133
+ const sealKey = keychainSeal(place).read()
134
+ const { space, why } = await machineSpace()
135
+ if (minter == null || bin == null || sealKey == null || space == null) return o.printErr(`WARN vault: no sealed copy to the Space: ${minter == null ? 'no minter token' : bin == null ? 'no bao' : sealKey == null ? 'no seal key in the keychain' : `no Space (${why})`}`)
136
+ try {
137
+ for (const l of await snapshotTick({ minter, place, space, machine: machineName(), bin, sealKey, minterToken: readFileSync(minterTokenFile(place), 'utf8').trim(), get: machineGet, set: machineSet, now, pruneOn: (await import('./switch.ts')).vaultPruneOn })) o.print(l)
138
+ } catch (e) {
139
+ o.printErr(`WARN vault: the sealed copy to the Space failed: ${(e as Error).message}`)
140
+ }
141
+ }
142
+
143
+ export const comparedFile = (): string => join(homeDir(), 'vault-compared.json')
144
+ /** the counts of the last sk vault compare, names only: what the house check says */
145
+ export const keepCompared = (sum: CompareSummary): void => keepPrivateFile(comparedFile(), JSON.stringify(sum))
146
+ export function lastCompared(): CompareSummary | null {
147
+ try {
148
+ return JSON.parse(readFileSync(comparedFile(), 'utf8')) as CompareSummary
149
+ } catch {
150
+ return null
151
+ }
152
+ }
153
+
154
+ /** the minter of the vault beside the hq this daemon carries: the vault runs on the hq's machine */
155
+ export const carriedMinter: VaultMinter = () => (vaultInitialised() ? minterOf(vaultPlace()) : null)
156
+
157
+ /** the machine db rows of the feed's mark, one an hq by its file's name. An `hq-` row, so the backup never carries
158
+ * it to another machine: a copy of the file opened there takes a new generation */
159
+ const FEED_MARK = 'hq-feed-mark:'
160
+
161
+ export const machineFeedMark: FeedMark = {
162
+ get: (hq) => machineGet<number>(`${FEED_MARK}${hq}`),
163
+ set: (hq, n) => void machineSet(`${FEED_MARK}${hq}`, n),
164
+ }
165
+
166
+ let carried: HqStore | null | undefined
167
+
168
+ /** the hq this daemon carries, opened once a process; null when its file does not open */
169
+ export function carriedHq(): HqStore | null {
170
+ if (carried === undefined) {
171
+ const db = openHq()
172
+ carried = db == null ? null : new HqStore(db, { mark: machineFeedMark, forgotten: fileForgottenLog(forgottenLogPath(hqDbPath())) })
173
+ }
174
+ return carried
175
+ }
176
+
177
+ /** forget the open store, for a test that moves the file */
178
+ export const forgetCarriedHq = (): void => {
179
+ try {
180
+ carried?.db.close()
181
+ } catch {
182
+ // already closed
183
+ }
184
+ carried = undefined
185
+ }
186
+
187
+ /** the grant of this machine's own folder in the vault: its name in lowercase, null (said in the log) when that is
188
+ * no safe part of a path */
189
+ function ownMachineGrant(): Grant | null {
190
+ let name: string
191
+ try {
192
+ name = machineName().toLowerCase()
193
+ } catch (e) {
194
+ console.error(`WARN the hq grants this daemon no machine folder in the vault: ${(e as Error).message}`)
195
+ return null
196
+ }
197
+ const problem = nameProblem('machine', name)
198
+ if (problem == null) return { kind: 'machine', name }
199
+ console.error(`WARN the hq grants this daemon no machine folder in the vault: ${problem}`)
200
+ return null
201
+ }
202
+
203
+ /** the daemon that carries an hq is its first node, with the grant `all` and its own machine: joined here, once,
204
+ * with no code typed */
205
+ export function joinCarriedHq(store: HqStore, now: number): { node: string } | Refused {
206
+ const held = readHqToken()
207
+ const live = held == null ? null : store.nodeOf(held)
208
+ const mine = ownMachineGrant()
209
+ if (live != null) {
210
+ // a node made before the kind machine gets its own machine beside all; its vault token follows at the next settle
211
+ if (mine != null && store.addGrant(live.id, mine, now)) markVault(store, live.id)
212
+ return { node: live.id }
213
+ }
214
+ // the token is lost or the hq no longer knows it: it joins as a new node, and only once that holds are the nodes
215
+ // this daemon had here ended (it writes the file itself). A refused join ends nothing and is kept, so the machines
216
+ // page and the hq check say it with its way out
217
+ const machine = { key: machineKey(), name: machineName() }
218
+ // its own machine, named in its own invite: another OS user's daemon may hold a node of it already
219
+ const code = store.invite({ person: store.person('owner', now), grants: [{ kind: 'all' }, ...(mine == null ? [] : [mine])], now, on: machine.key })
220
+ const r = store.join({ code, machine, osUser: userInfo().username, now })
221
+ if ('refused' in r) {
222
+ machineSet(OWN_JOIN, { refused: r.refused, at: now })
223
+ return r
224
+ }
225
+ store.endNodesOf(machine.key, userInfo().username, now, r.node)
226
+ machineDelete(OWN_JOIN)
227
+ keepHqToken(r.token)
228
+ return { node: r.node }
229
+ }
230
+
231
+ /** the machine db row of the hq's own join refused: an `hq-` row, so the backup never carries it */
232
+ const OWN_JOIN = 'hq-own-join'
233
+
234
+ /** why the daemon that carries the hq is no node of it, with the way out; null while it is one */
235
+ export function ownJoinRefused(): string | null {
236
+ const r = machineGet<{ refused: string; at: number }>(OWN_JOIN)
237
+ return r == null ? null : `the hq this machine carries refused its own daemon at its start: ${r.refused}. The way out: sk hq forget <the older machine of that name> (sk machines --json lists them), then sk pm restart daemon`
238
+ }
239
+
240
+ /** a node's door key while the hq's proxy is on; none while it is off, so no door opens to a proxy that is off */
241
+ async function carriedDoorKey(node: string): Promise<string | null> {
242
+ const { doorKeyOf, doorSecret, proxyOn } = await import('./door.ts')
243
+ return proxyOn() ? doorKeyOf(await doorSecret(), node) : null
244
+ }
245
+
246
+ /** the hq's routes on the daemon's own port, while it carries one; null for another path or a daemon that joined
247
+ * an hq elsewhere */
248
+ export async function carriedHqRoute(req: Request, path: string): Promise<Response | null> {
249
+ if (!path.startsWith('/hq/') || !carriesHq()) return null
250
+ const store = carriedHq()
251
+ if (store == null) return Response.json({ refused: 'the hq of this machine does not open its file' }, { status: 503 })
252
+ try {
253
+ return await hqRoute(store, req, path, Date.now, carriedMinter, carriedDoorKey)
254
+ } catch (e) {
255
+ console.error(`WARN the hq failed ${req.method} ${path}: ${(e as Error).message}`)
256
+ return Response.json({ refused: 'the hq failed this call: the log of its daemon says why' }, { status: 500 })
257
+ }
258
+ }
259
+
260
+ /** this daemon's link to its hq, or why there is none */
261
+ export function myHq(): ReturnType<typeof httpHq> | Refused {
262
+ const token = readHqToken()
263
+ if (token != null) return httpHq(hqUrl(), token)
264
+ const own = carriesHq() ? ownJoinRefused() : null
265
+ return { refused: own ?? 'this daemon has not joined an hq yet: the daemon joins the one it carries at its start, `sk hq join` joins another' }
266
+ }
267
+
268
+ /** the report job's body, in its child: the switch values the hq's machine asked of this node, this machine's report
269
+ * into the outbox, everything the hq lacks, then the asks and the reflections */
270
+ export async function hqReportTick(o: { repos: { name: string; path: string }[]; house: string; print: (l: string) => void; printErr: (l: string) => void; now?: number }): Promise<void> {
271
+ const hq = myHq()
272
+ if ('refused' in hq) return o.printErr(`WARN hq report: ${hq.refused}`)
273
+ const db = openDaemonDb()
274
+ if (db == null) return o.printErr('WARN hq report: the daemon\'s own database does not open, nothing is sent')
275
+ try {
276
+ const now = o.now ?? Date.now()
277
+ const status = readTailscaleStatus()
278
+ const outbox = new Outbox(db, 'report')
279
+ // the switch values the hq's console asked of this node, applied before the report, which then shows them
280
+ const hello = await hq.hello(outbox.epoch())
281
+ if (!('refused' in hello) && hello.wanted != null && hello.wanted.length > 0) {
282
+ const { applyWanted } = await import('./switch.ts')
283
+ for (const l of (await applyWanted(hello.wanted)).lines) (l.startsWith('WARN') ? o.printErr : o.print)(`${l.startsWith('WARN') ? 'WARN hq report: ' : 'ok: hq report: '}${l.replace(/^(WARN|ok:) /, '')}`)
284
+ }
285
+ // the key of this machine's door, which only the hq's home shows, kept as its hash; a hello with none (the
286
+ // hq's proxy is off) ends it, so the key dies with the switch at the next report
287
+ if (!('refused' in hello)) {
288
+ const door = await import('./door.ts')
289
+ if (typeof hello.door === 'string') {
290
+ if (await door.keepDoorKey(hello.door)) o.print('ok: hq report: this machine keeps the key of its door from the hq')
291
+ } else if (door.forgetDoorKey()) o.print("ok: hq report: the hq's proxy is off: this machine forgot the key of its door")
292
+ }
293
+ const waiting = db.query('select count(*) as waiting, min(since) as since from retry').get() as { waiting: number; since: number | null }
294
+ const { machineFacts, realReaders } = await import('./report-facts.ts')
295
+ const { mediaReport } = await import('../media-store/daemon.ts')
296
+ const facts = await machineFacts({ repos: o.repos, now, ...(await realReaders()) })
297
+ outbox.push('report', await buildReport({ repos: o.repos, house: o.house, tailnet: { name: status?.Self?.DNSName?.replace(/\.$/, '') || null, up: status?.BackendState === 'Running' }, diskOf: homeDir(), cards: waiting, media: mediaReport(), ...facts }), now)
298
+ const r = await flush(outbox, hq, now)
299
+ if ('refused' in r) return o.printErr(`WARN hq report: ${r.refused} (${outbox.size()} rows wait in the outbox)`)
300
+ if (r.renamed) o.printErr('WARN hq report: the hq held rows this outbox never made (the outbox is an older copy of itself): it took a new name and sent every row it holds again')
301
+ if (r.gone > 0) o.printErr(`WARN hq report: the hq lacks ${r.gone} rows this outbox no longer holds: it came back from an older copy than the outbox keeps`)
302
+ if (r.overCap > 0) o.printErr(`WARN hq report: ${r.overCap} rows the hq never took left the outbox, over its cap`)
303
+ // then the asks, and the reflections while their switch is on: a look whose folders did not move makes no call
304
+ await (await import('./files-send.ts')).hqFilesTick({ repos: o.repos, link: hq, db, print: o.print, printErr: o.printErr, now })
305
+ } finally {
306
+ db.close()
307
+ }
308
+ }
309
+
310
+ /** what the hq's port answers: the hq's routes and nothing else, whatever name or header the caller sends */
311
+ export async function hqPortFetch(req: Request): Promise<Response> {
312
+ return (await carriedHqRoute(req, new URL(req.url).pathname)) ?? Response.json({ refused: 'this port answers the hq only' }, { status: 404 })
313
+ }
314
+
315
+ let listener: { stop(closeActiveConnections?: boolean): void } | null = null
316
+
317
+ /** the hq's port: a second local port of the daemon that carries the hq, the only thing `tailscale serve` hands to
318
+ * the other machines. The daemon's own port, with every route of the home, is never handed to anyone: a caller
319
+ * from another machine cannot reach it by a Host header or a path, since the door has no such route */
320
+ export function setHqPort(open: boolean): { open: boolean; port: number } | Refused {
321
+ const port = hqPort()
322
+ if (!open) {
323
+ listener?.stop(true)
324
+ listener = null
325
+ machineSet(HQ_OPEN, false)
326
+ return { open: false, port }
327
+ }
328
+ if (!carriesHq()) return { refused: `this machine joined the hq at ${hqUrl()}: it carries none to open` }
329
+ if (listener == null) {
330
+ try {
331
+ listener = Bun.serve({
332
+ port,
333
+ hostname: '127.0.0.1',
334
+ fetch: hqPortFetch,
335
+ error: (e) => {
336
+ console.error(`WARN the hq's port failed a request: ${e.message}`)
337
+ return Response.json({ refused: 'the hq failed this call: the log of its daemon says why' }, { status: 500 })
338
+ },
339
+ })
340
+ } catch (e) {
341
+ return { refused: `the hq's port does not open on port ${port}: ${(e as Error).message}` }
342
+ }
343
+ }
344
+ machineSet(HQ_OPEN, true)
345
+ return { open: true, port }
346
+ }
347
+
348
+ /** the owner's acts on the hq this daemon carries, asked by `sk hq` over loopback so the daemon stays the one
349
+ * writer of its file: the door opened or closed, a join code for another machine, the end of a node, a prompt
350
+ * forgotten, a switch wanted of another machine */
351
+ export async function hqAdminRoute(req: Request, path: string, now: number = Date.now()): Promise<Response | null> {
352
+ if (!path.startsWith('/api/home/hq/') || req.method !== 'POST') return null
353
+ // the web hands the home's routes to the whole tailnet, and tags each call it forwards with where it came from.
354
+ // A device there that names the web `localhost` gets the tag « local », so the tag proves nothing: a call that
355
+ // carries one came through the web and is refused. `sk hq` asks the daemon's own port, which is loopback only
356
+ if (req.headers.has(REACH_HEADER)) return Response.json({ refused: 'a join code, the end of a node, a forget and a switch are asked on the machine that carries the hq, in its console' }, { status: 403 })
357
+ if (path === '/api/home/hq/open' || path === '/api/home/hq/close') {
358
+ const r = setHqPort(path.endsWith('/open'))
359
+ return Response.json(r, { status: 'refused' in r ? 409 : 200 })
360
+ }
361
+ const store = carriesHq() ? carriedHq() : null
362
+ if (store == null) return Response.json({ refused: 'this daemon carries no hq: it joined one on another machine, ask there' }, { status: 409 })
363
+ const b = (await req.json().catch(() => ({}))) as { grants?: unknown; node?: unknown; kind?: unknown; name?: unknown; machine?: unknown; on?: unknown; onMachine?: unknown; go?: unknown }
364
+ if (path === '/api/home/hq/invite') {
365
+ const asked = Array.isArray(b.grants) ? (b.grants as unknown[]) : []
366
+ const problem = grantsProblem(asked)
367
+ if (problem != null) return Response.json({ refused: problem }, { status: 400 })
368
+ const grants = asked.map((g): Grant => ((g as Grant).kind === 'all' ? { kind: 'all' } : { kind: (g as { kind: 'repo' | 'identity' | 'machine' }).kind, name: (g as { name: string }).name }))
369
+ // a second OS user of a machine the hq knows: the owner names that machine, by its name or its id
370
+ let on: string | undefined
371
+ if (typeof b.onMachine === 'string' && b.onMachine !== '') {
372
+ const m = store.db.query('select id from machines where id = ?1 or name = ?1').all(b.onMachine) as { id: string }[]
373
+ if (m.length !== 1) return Response.json({ refused: m.length === 0 ? `the hq knows no machine ${JSON.stringify(b.onMachine).slice(0, 80)}` : `${m.length} machines are named ${JSON.stringify(b.onMachine).slice(0, 80)}: name it by its id (sk machines --json)` }, { status: 400 })
374
+ on = m[0]!.id
375
+ }
376
+ return Response.json({ code: store.invite({ person: store.person('owner', now), grants, now, ...(on == null ? {} : { on }) }), expires: now + INVITE_MS })
377
+ }
378
+ if (path === '/api/home/hq/end') {
379
+ const ended = typeof b.node === 'string' && store.end(b.node, now)
380
+ if (!ended) return Response.json({ ended })
381
+ const api = carriedMinter()
382
+ if (api == null) return Response.json({ ended, vault: 'no vault runs beside this hq' })
383
+ const { failed } = await settleVault(store, api).catch((e: unknown) => ({ failed: [(e as Error).message] }))
384
+ return Response.json({ ended, vault: failed.length === 0 ? 'its vault token is revoked and its folders closed' : `NOT reached, retried at the next mint or check: ${failed.join('; ')}` })
385
+ }
386
+ // what differs between the vault and the grants, for the house check: never a value, never a token
387
+ if (path === '/api/home/hq/vault-check') {
388
+ const api = carriedMinter()
389
+ if (api == null) return Response.json({ vault: false })
390
+ try {
391
+ return Response.json({ vault: true, ...(await vaultDrift(store, api)) })
392
+ } catch (e) {
393
+ return Response.json({ vault: true, refused: `the vault did not answer: ${(e as Error).message}` }, { status: 502 })
394
+ }
395
+ }
396
+ // a switch value for one machine's daemons, which they take at their next report (sk hq switch)
397
+ if (path === '/api/home/hq/switch') {
398
+ const r = typeof b.machine === 'string' && typeof b.name === 'string' && typeof b.on === 'boolean' ? store.want(b.machine, b.name, b.on, now) : { refused: 'a switch names its machine, its name and on or off' }
399
+ return Response.json(r, { status: 'refused' in r ? 400 : 200 })
400
+ }
401
+ // a retired machine forgotten: its row and its ended nodes, so its name is free again (sk hq forget)
402
+ if (path === '/api/home/hq/forget-machine') {
403
+ const r = store.forgetMachine({ machine: typeof b.machine === 'string' ? b.machine : '', own: machineKey(), go: b.go === true })
404
+ return Response.json(r, { status: 'refused' in r ? r.status : 200 })
405
+ }
406
+ // a row kept for good removed, once, and refused from any node from then on (sk history forget)
407
+ if (path === '/api/home/hq/forget') {
408
+ const r = store.forget(typeof b.kind === 'string' ? b.kind : '', typeof b.name === 'string' ? b.name : '', now)
409
+ return Response.json(r, { status: 'refused' in r ? 400 : 200 })
410
+ }
411
+ return null
412
+ }
413
+
414
+ export type VaultCheck = { vault: false } | { vault: true; problems: string[]; nodes: number } | Refused
415
+
416
+ /** what differs between this machine's vault and the grants of the hq it carries, asked of its daemon (the one
417
+ * writer of the hq's file) over loopback */
418
+ export async function askVaultCheck(): Promise<VaultCheck> {
419
+ try {
420
+ const r = await fetch(`http://127.0.0.1:${daemonPort()}/api/home/hq/vault-check`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}', signal: AbortSignal.timeout(30_000) })
421
+ return (await r.json()) as VaultCheck
422
+ } catch (e) {
423
+ return { refused: `the daemon of this machine does not answer (${(e as Error).message}): sk pm starts it` }
424
+ }
425
+ }
426
+
427
+ /** this daemon leaves the hq it carries for one on another machine: the join there, the token kept, the url kept */
428
+ export async function joinOtherHq(url: string, code: string): Promise<{ node: string; vault: string } | Refused> {
429
+ const to = url.replace(/\/$/, '')
430
+ // the token this daemon holds goes only to the hq that gave it: a join of that same hq again keeps its node
431
+ const held = machineGet<string>(HQ_URL) === to ? (readHqToken() ?? undefined) : undefined
432
+ const r = await httpJoin(to, { code, machine: { key: machineKey(), name: machineName() }, osUser: userInfo().username }, fetch, held)
433
+ if ('refused' in r) return r
434
+ keepHqToken(r.token)
435
+ machineSet(HQ_URL, to)
436
+ const v = await refreshVaultToken()
437
+ return { node: r.node, vault: 'refused' in v ? `no vault token yet: ${v.refused}` : 'its vault token is kept beside the hq token' }
438
+ }
439
+
440
+ /** back to the hq this daemon carries: its next start joins it again */
441
+ export const leaveOtherHq = (): void => machineDelete(HQ_URL)
442
+
443
+ /** `asks`: the open asks of every machine, once each; absent while the hq does not read them. `door`: the home's port,
444
+ * while this machine carries the hq with its proxy on, so the page links a process through the door */
445
+ export type MachinesPage = { you: string; at: number; machines: (MachineView & { nodes: (MachineView['nodes'][number] & { line: { at: number; free: number }[] })[] })[]; notes: MachineNote[]; asks?: AskSeen[]; door?: { port: number } }
446
+
447
+ /** what the machines page and `sk machines` show: every machine the hq knows, each live node's free disk over
448
+ * `days`, what to look at, and the open asks of every machine */
449
+ export async function machinesPage(days = 30, now = Date.now()): Promise<MachinesPage | Refused> {
450
+ const hq = myHq()
451
+ if ('refused' in hq) return hq
452
+ const r = await hq.machines()
453
+ if ('refused' in r) return r
454
+ const machines = await Promise.all(
455
+ r.machines.map(async (m) => ({
456
+ ...m,
457
+ nodes: await Promise.all(
458
+ m.nodes.map(async (n) => {
459
+ const d = n.ended == null ? await hq.disk(n.id, days) : { line: [] }
460
+ return { ...n, line: 'refused' in d ? [] : d.line }
461
+ }),
462
+ ),
463
+ })),
464
+ )
465
+ const asks = await (await import('./files-send.ts')).readAsks(hq, r)
466
+ const { proxyOn } = await import('./door.ts')
467
+ const door = carriesHq() && proxyOn() ? { door: { port: homePort() } } : {}
468
+ return { you: r.you, at: now, machines, notes: machineNotes(r.machines, now), ...('refused' in asks ? {} : { asks: asks.asks.filter((a) => a.state === 'open') }), ...door }
469
+ }