@astryxdesign/cli 0.7.16 → 0.7.17-canary.g0404a74

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 (529) hide show
  1. package/README.md +76 -86
  2. package/api/build/_adapter.d.mts +17 -0
  3. package/api/build/_adapter.mjs +53 -34
  4. package/api/build/blocks/blocks.d.mts +7 -5
  5. package/api/build/blocks/blocks.mjs +113 -26
  6. package/api/build/blocks/blocks.weights.json +1 -1
  7. package/api/build/build.doc.mjs +4 -3
  8. package/api/build/build.type.d.mts +2 -2
  9. package/api/build/build.type.mjs +3 -3
  10. package/api/build/kit/kind.d.mts +2 -1
  11. package/api/build/kit/kind.mjs +199 -10
  12. package/api/build/kit/kit.mjs +6 -4
  13. package/api/build/kit/rank.d.mts +6 -4
  14. package/api/build/kit/rank.mjs +132 -28
  15. package/api/build/kit/signals.d.mts +7 -1
  16. package/api/build/kit/signals.mjs +21 -10
  17. package/api/build/kit/start.d.mts +5 -4
  18. package/api/build/kit/start.mjs +14 -9
  19. package/api/build/kit/weights.d.mts +39 -3
  20. package/api/build/kit/weights.json +1 -1
  21. package/api/build/kit/weights.mjs +351 -46
  22. package/api/build/kit/word-model.d.mts +23 -0
  23. package/api/build/kit/word-model.mjs +46 -0
  24. package/api/build/kit/words.d.mts +86 -0
  25. package/api/build/kit/words.mjs +241 -1
  26. package/api/build/tree.mjs +1 -1
  27. package/api/component/__fixtures__/resolutions/modules.txt +5 -6
  28. package/api/component/_adapter.d.mts +26 -2
  29. package/api/component/_adapter.mjs +135 -8
  30. package/api/component/component.mjs +14 -1
  31. package/api/component/list/list.d.mts +1 -1
  32. package/api/component/list/list.mjs +48 -6
  33. package/api/discover/_adapter.d.mts +11 -1
  34. package/api/discover/_adapter.mjs +95 -1
  35. package/api/discover/detail/item/item.mjs +5 -0
  36. package/api/discover/discover.mjs +12 -3
  37. package/api/discover/discover.type.d.mts +16 -3
  38. package/api/discover/discover.type.mjs +13 -3
  39. package/api/discover/search/search.mjs +3 -2
  40. package/api/docs/_adapter.d.mts +57 -2
  41. package/api/docs/_adapter.mjs +266 -33
  42. package/api/docs/_entry-names-lazily.d.mts +22 -0
  43. package/api/docs/_entry-names-lazily.mjs +25 -0
  44. package/api/docs/_entry-names.d.mts +23 -0
  45. package/api/docs/_entry-names.mjs +105 -0
  46. package/api/docs/docs.d.mts +3 -0
  47. package/api/docs/docs.doc.mjs +7 -1
  48. package/api/docs/docs.mjs +12 -0
  49. package/api/docs/docs.type.d.mts +5 -0
  50. package/api/docs/docs.type.mjs +2 -0
  51. package/api/docs/node/node.mjs +1 -1
  52. package/api/doctor/_adapter.d.mts +54 -5
  53. package/api/doctor/_adapter.mjs +162 -11
  54. package/api/doctor/built-checks.mjs +40 -14
  55. package/api/doctor/doctor.d.mts +15 -3
  56. package/api/doctor/doctor.doc.mjs +1 -1
  57. package/api/doctor/doctor.mjs +31 -7
  58. package/api/doctor/theme-checks.d.mts +35 -3
  59. package/api/doctor/theme-checks.mjs +377 -29
  60. package/api/hook/_adapter.d.mts +10 -4
  61. package/api/hook/_adapter.mjs +41 -5
  62. package/api/hook/detail/detail.mjs +5 -2
  63. package/api/hook/detail/params/params.mjs +10 -3
  64. package/api/icon/_records.mjs +2 -1
  65. package/api/index.d.mts +2 -0
  66. package/api/index.mjs +2 -0
  67. package/api/integration/_adapter.d.mts +44 -0
  68. package/api/integration/_adapter.mjs +77 -1
  69. package/api/integration/add-helpers.mjs +10 -0
  70. package/api/integration/authoring-checks.mjs +11 -8
  71. package/api/integration/example-scope.d.mts +14 -0
  72. package/api/integration/example-scope.mjs +44 -0
  73. package/api/integration/example-scope.type.d.mts +29 -0
  74. package/api/integration/example-scope.type.mjs +25 -0
  75. package/api/integration/integrationExampleScope.doc.d.mts +11 -0
  76. package/api/integration/integrationExampleScope.doc.mjs +59 -0
  77. package/api/integration/pack-check.mjs +16 -1
  78. package/api/integration/validate-integration.d.mts +18 -6
  79. package/api/integration/validate-integration.mjs +129 -15
  80. package/api/integration/validate-integration.type.d.mts +22 -0
  81. package/api/integration/validate-integration.type.mjs +2 -1
  82. package/api/integration/validateIntegration.doc.mjs +1 -1
  83. package/api/render/_adapter.mjs +591 -195
  84. package/api/render/render.doc.mjs +11 -3
  85. package/api/render/render.type.d.mts +53 -1
  86. package/api/render/render.type.mjs +32 -2
  87. package/api/search/ORDERING.md +2 -2
  88. package/api/search/_adapter.d.mts +9 -0
  89. package/api/search/_adapter.mjs +85 -0
  90. package/api/search/_candidate-score.d.mts +9 -4
  91. package/api/search/_candidate-score.mjs +133 -46
  92. package/api/search/_fit-order.mjs +3 -3
  93. package/api/search/_gather-components.d.mts +3 -2
  94. package/api/search/_gather-components.mjs +12 -3
  95. package/api/search/_lexical.d.mts +14 -0
  96. package/api/search/_lexical.mjs +93 -11
  97. package/api/search/_result.d.mts +11 -9
  98. package/api/search/_result.mjs +4 -4
  99. package/api/search/_score.d.mts +6 -1
  100. package/api/search/_score.mjs +15 -3
  101. package/api/search/search-index.mjs +7 -1
  102. package/api/search/search.d.mts +1 -1
  103. package/api/template/_adapter.d.mts +16 -0
  104. package/api/template/_adapter.mjs +39 -2
  105. package/api/template/cdn/cdn.mjs +37 -8
  106. package/api/template/copy/copy.d.mts +6 -3
  107. package/api/template/copy/copy.mjs +40 -8
  108. package/api/template/template.doc.mjs +3 -3
  109. package/api/template/template.mjs +63 -11
  110. package/api/template/template.type.d.mts +1 -0
  111. package/api/template/template.type.mjs +1 -0
  112. package/api/theme/_adapter.d.mts +12 -1
  113. package/api/theme/_adapter.mjs +132 -13
  114. package/api/theme/_app-provider.d.mts +31 -0
  115. package/api/theme/_app-provider.mjs +135 -0
  116. package/api/theme/add/add.mjs +3 -1
  117. package/api/theme/build/build.d.mts +142 -1
  118. package/api/theme/build/build.mjs +692 -284
  119. package/api/theme/build/core-interception.d.mts +10 -0
  120. package/api/theme/build/core-interception.mjs +10 -0
  121. package/api/theme/build/font-warning.d.mts +80 -5
  122. package/api/theme/build/font-warning.mjs +323 -55
  123. package/api/theme/build/icon-imports.mjs +51 -9
  124. package/api/theme/build/null-values.d.mts +28 -0
  125. package/api/theme/build/null-values.mjs +191 -0
  126. package/api/theme/list/list.mjs +17 -1
  127. package/api/theme/remove/remove.d.mts +2 -1
  128. package/api/theme/remove/remove.mjs +7 -3
  129. package/api/theme/theme.type.d.mts +78 -1
  130. package/api/theme/theme.type.mjs +38 -2
  131. package/api/theme/themeAdd.doc.mjs +1 -1
  132. package/api/theme/themeListAvailable.doc.mjs +1 -1
  133. package/api/theme/themeRemove.doc.mjs +8 -5
  134. package/api/theme-edit/_adapter.d.mts +116 -4
  135. package/api/theme-edit/_adapter.mjs +1058 -93
  136. package/api/theme-edit/_catalog.d.mts +46 -2
  137. package/api/theme-edit/_catalog.mjs +115 -14
  138. package/api/theme-edit/_check.d.mts +133 -24
  139. package/api/theme-edit/_check.mjs +3392 -531
  140. package/api/theme-edit/_color.d.mts +26 -0
  141. package/api/theme-edit/_color.mjs +92 -2
  142. package/api/theme-edit/_compiler.d.mts +5 -1
  143. package/api/theme-edit/_compiler.mjs +141 -17
  144. package/api/theme-edit/_context.d.mts +74 -6
  145. package/api/theme-edit/_context.mjs +468 -76
  146. package/api/theme-edit/_css.d.mts +21 -0
  147. package/api/theme-edit/_css.mjs +87 -14
  148. package/api/theme-edit/_edit.d.mts +74 -7
  149. package/api/theme-edit/_edit.mjs +4426 -319
  150. package/api/theme-edit/_how-commands.d.mts +76 -1
  151. package/api/theme-edit/_how-commands.mjs +793 -9
  152. package/api/theme-edit/_how-question.d.mts +97 -6
  153. package/api/theme-edit/_how-question.mjs +9184 -747
  154. package/api/theme-edit/_how.d.mts +5 -2
  155. package/api/theme-edit/_how.mjs +255 -46
  156. package/api/theme-edit/_importers.d.mts +38 -0
  157. package/api/theme-edit/_importers.mjs +188 -0
  158. package/api/theme-edit/_marks.d.mts +31 -5
  159. package/api/theme-edit/_marks.mjs +57 -5
  160. package/api/theme-edit/_name-rule.d.mts +11 -0
  161. package/api/theme-edit/_name-rule.mjs +12 -0
  162. package/api/theme-edit/_normal.d.mts +322 -0
  163. package/api/theme-edit/_normal.mjs +1630 -0
  164. package/api/theme-edit/_palette.d.mts +49 -0
  165. package/api/theme-edit/_palette.mjs +180 -38
  166. package/api/theme-edit/_part-words.d.mts +39 -0
  167. package/api/theme-edit/_part-words.mjs +152 -0
  168. package/api/theme-edit/_parts.d.mts +55 -3
  169. package/api/theme-edit/_parts.mjs +421 -36
  170. package/api/theme-edit/_preview-app.d.mts +7 -13
  171. package/api/theme-edit/_preview-app.mjs +1108 -599
  172. package/api/theme-edit/_preview.d.mts +156 -19
  173. package/api/theme-edit/_preview.mjs +809 -180
  174. package/api/theme-edit/_project.d.mts +61 -1
  175. package/api/theme-edit/_project.mjs +177 -6
  176. package/api/theme-edit/_settings.d.mts +619 -48
  177. package/api/theme-edit/_settings.mjs +4910 -617
  178. package/api/theme-edit/_tables.d.mts +41 -0
  179. package/api/theme-edit/_tables.mjs +261 -41
  180. package/api/theme-edit/_vet.d.mts +31 -0
  181. package/api/theme-edit/_vet.mjs +114 -8
  182. package/api/theme-edit/theme-edit.type.d.mts +34 -2
  183. package/api/theme-edit/theme-edit.type.mjs +18 -3
  184. package/api/theme-edit/themeEditHow.doc.mjs +1 -1
  185. package/api/theme-edit/themeEditNew.doc.d.mts +0 -1
  186. package/api/theme-edit/themeEditNew.doc.mjs +7 -4
  187. package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
  188. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +2 -2
  189. package/api/upgrade/run/run.mjs +4 -3
  190. package/api/upgrade/upgrade.mjs +16 -1
  191. package/assets/codemods/integration-discovery.mjs +2 -1
  192. package/assets/codemods/integration-runner.mjs +1 -1
  193. package/assets/codemods/jscodeshift.d.ts +1 -1
  194. package/assets/codemods/run-codemod.mjs +151 -18
  195. package/assets/codemods/runner.mjs +1 -4
  196. package/assets/codemods/transforms/next/index.mjs +11 -1
  197. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  198. package/assets/component-list.generated.json +1 -0
  199. package/assets/docs/author-a-theme.doc.mjs +2 -0
  200. package/assets/docs/theme.doc.dense.mjs +76 -7
  201. package/assets/docs/theme.doc.mjs +66 -2
  202. package/assets/docs/theme.doc.zh.mjs +51 -1
  203. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  204. package/assets/docs/tree/checks.doc.mjs +2 -2
  205. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  206. package/assets/docs/tree/document-the-theme.doc.mjs +1 -1
  207. package/assets/docs/tree/font-setup.doc.mjs +1 -1
  208. package/assets/docs/tree/fonts-and-assets.doc.mjs +1 -1
  209. package/assets/docs/tree/render-pages.doc.mjs +46 -5
  210. package/assets/docs/tree/share-a-page.doc.mjs +157 -0
  211. package/assets/docs/tree/single-component.doc.mjs +4 -0
  212. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  213. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  214. package/assets/docs/tree/write-good-templates.doc.mjs +1 -1
  215. package/assets/docs/use-a-theme.doc.mjs +2 -2
  216. package/assets/docs-routes.generated.json +1 -0
  217. package/assets/docs.generated.json +2 -2
  218. package/assets/search-catalog.generated.json +2 -0
  219. package/assets/self-audit.generated.json +1 -1
  220. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  221. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  222. package/assets/templates/blocks/components/ChatComposerInput/useSpeechRecognitionHookUsage.doc.mjs +14 -0
  223. package/assets/templates/blocks/components/ChatComposerInput/useSpeechRecognitionHookUsage.tsx +43 -0
  224. package/assets/templates/blocks/components/Hooks/useContainerRevealHookUsage.tsx +2 -2
  225. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  226. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  227. package/assets/templates/components.generated.json +1 -1
  228. package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
  229. package/assets/templates/pages/ai-chat-landing/page.tsx +31 -4
  230. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +5 -1
  231. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  232. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  233. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  234. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  235. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  236. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  237. package/assets/templates/pages/dashboard-alert-rail/page.tsx +1 -1
  238. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  239. package/assets/templates/pages/dashboard-cohort-funnel/page.tsx +1 -1
  240. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  241. package/assets/templates/pages/dashboard-comparison/page.tsx +2 -2
  242. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  243. package/assets/templates/pages/dashboard-progress/page.tsx +1 -1
  244. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  245. package/assets/templates/pages/detail-page/page.tsx +60 -7
  246. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  247. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  248. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  249. package/assets/templates/pages/documentation-technical/page.tsx +13 -13
  250. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  251. package/assets/templates/pages/editor/page.tsx +14 -2
  252. package/assets/templates/pages/editor/template.doc.mjs +2 -8
  253. package/assets/templates/pages/file-explorer/page.tsx +9 -0
  254. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  255. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  256. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  257. package/assets/templates/pages/form-wizard-vertical/page.tsx +23 -3
  258. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  259. package/assets/templates/pages/ide/page.tsx +33 -2
  260. package/assets/templates/pages/kanban-board/page.tsx +12 -2
  261. package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
  262. package/assets/templates/pages/library/template.doc.mjs +4 -1
  263. package/assets/templates/pages/login/template.doc.mjs +2 -1
  264. package/assets/templates/pages/login-card/page.tsx +16 -0
  265. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  266. package/assets/templates/pages/login-split/page.tsx +19 -6
  267. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  268. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  269. package/assets/templates/pages/messaging-shell/template.doc.mjs +6 -0
  270. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  271. package/assets/templates/pages/payment-form/page.tsx +30 -4
  272. package/assets/templates/pages/product-detail/page.tsx +26 -2
  273. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  274. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  275. package/assets/templates/pages/settings-dialog/page.tsx +162 -12
  276. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  277. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  278. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  279. package/assets/templates/pages/shell-side-nav/page.tsx +11 -3
  280. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  281. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  282. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  283. package/assets/templates/pages/table-filter/page.tsx +9 -0
  284. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  285. package/assets/templates/pages/table-grouped/page.tsx +161 -133
  286. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  287. package/assets/templates/pages/table-page/page.tsx +14 -1
  288. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  289. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  290. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
  291. package/assets/templates/pages/work-item-detail/page.tsx +47 -7
  292. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  293. package/assets/theme.template.ts +9 -2
  294. package/authoring/debug/parse.d.mts +3 -3
  295. package/authoring/doctypes/_schema.d.mts +9 -9
  296. package/authoring/doctypes/command/command.doc.mjs +1 -1
  297. package/authoring/doctypes/component/categories.d.mts +37 -0
  298. package/authoring/doctypes/component/categories.mjs +54 -0
  299. package/authoring/doctypes/template/template.doc.mjs +1 -1
  300. package/authoring/doctypes/template/type.ts +9 -0
  301. package/authoring/example/example.doc.d.mts +12 -0
  302. package/authoring/example/example.doc.mjs +69 -0
  303. package/authoring/example/preview.d.mts +92 -0
  304. package/authoring/example/preview.mjs +994 -0
  305. package/authoring/example/type.ts +102 -0
  306. package/authoring/index.d.mts +1 -0
  307. package/authoring/index.d.ts +12 -0
  308. package/authoring/index.mjs +3 -1
  309. package/authoring/integration/integration.doc.mjs +8 -0
  310. package/authoring/integration/schema.d.mts +24 -1
  311. package/authoring/integration/schema.mjs +126 -4
  312. package/authoring/integration/type.ts +14 -0
  313. package/clients/cli/bin/astryx.mjs +6 -9
  314. package/clients/cli/build-id.generated.json +1 -1
  315. package/clients/cli/commands/component.doc.mjs +1 -1
  316. package/clients/cli/commands/component.mjs +22 -3
  317. package/clients/cli/commands/discover.mjs +70 -3
  318. package/clients/cli/commands/docs.doc.mjs +7 -1
  319. package/clients/cli/commands/docs.mjs +44 -6
  320. package/clients/cli/commands/doctor.mjs +84 -16
  321. package/clients/cli/commands/experimental/render.mjs +42 -1
  322. package/clients/cli/commands/experimental/theme.mjs +1032 -134
  323. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  324. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  325. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +87 -1
  326. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +10 -3
  327. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +2 -2
  328. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +2 -2
  329. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
  330. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  331. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +2 -2
  332. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
  333. package/clients/cli/commands/experimental-theme-preview.doc.mjs +2 -1
  334. package/clients/cli/commands/experimental-theme-remove.doc.mjs +4 -4
  335. package/clients/cli/commands/experimental-theme.doc.mjs +5 -1
  336. package/clients/cli/commands/search.doc.mjs +2 -1
  337. package/clients/cli/commands/search.mjs +6 -1
  338. package/clients/cli/commands/template.doc.mjs +1 -1
  339. package/clients/cli/commands/template.mjs +15 -2
  340. package/clients/cli/commands/theme-add.doc.mjs +1 -1
  341. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  342. package/clients/cli/commands/theme-list.doc.mjs +1 -1
  343. package/clients/cli/commands/theme-remove.doc.mjs +13 -5
  344. package/clients/cli/commands/theme.doc.mjs +1 -1
  345. package/clients/cli/commands/theme.mjs +849 -441
  346. package/clients/cli/commands/upgrade.mjs +46 -8
  347. package/clients/cli/index.mjs +9 -19
  348. package/clients/cli/lib/cli-error.mjs +2 -1
  349. package/clients/cli/lib/component-format.mjs +205 -98
  350. package/clients/cli/lib/hook-format.mjs +9 -19
  351. package/clients/cli/lib/json-shim.mjs +265 -29
  352. package/clients/cli/lib/manifest.mjs +3 -2
  353. package/clients/cli/lib/stray-option.mjs +478 -0
  354. package/clients/cli/lib/unknown-command.mjs +353 -0
  355. package/clients/cli/manifest.generated.json +1 -1
  356. package/foundation/agent-docs/agent-docs.mjs +1 -1
  357. package/foundation/config/project.d.mts +8 -0
  358. package/foundation/config/project.mjs +26 -29
  359. package/foundation/config/theme-state.d.mts +31 -2
  360. package/foundation/config/theme-state.mjs +140 -8
  361. package/foundation/discovery/authoring-self-docs.mjs +1 -0
  362. package/foundation/discovery/cli-self-doc-names.d.mts +13 -0
  363. package/foundation/discovery/cli-self-doc-names.mjs +51 -0
  364. package/foundation/discovery/cli-self-docs.d.mts +0 -2
  365. package/foundation/discovery/cli-self-docs.mjs +2 -15
  366. package/foundation/discovery/component-discovery.d.mts +11 -0
  367. package/foundation/discovery/component-discovery.mjs +59 -10
  368. package/foundation/discovery/component-replacement.mjs +130 -1
  369. package/foundation/discovery/copied-passages.mjs +2 -1
  370. package/foundation/discovery/core-component-docs.d.mts +29 -0
  371. package/foundation/discovery/core-component-docs.mjs +83 -0
  372. package/foundation/discovery/core-theme-facts.d.mts +70 -0
  373. package/foundation/discovery/core-theme-facts.mjs +384 -0
  374. package/foundation/discovery/doc-types.mjs +4 -3
  375. package/foundation/discovery/docs-discovery.d.mts +66 -9
  376. package/foundation/discovery/docs-discovery.mjs +126 -60
  377. package/foundation/discovery/entry-name-files.d.mts +106 -0
  378. package/foundation/discovery/entry-name-files.mjs +354 -0
  379. package/foundation/discovery/hook-discovery.d.mts +18 -2
  380. package/foundation/discovery/hook-discovery.mjs +93 -42
  381. package/foundation/discovery/icon-definition-discovery.mjs +2 -1
  382. package/foundation/discovery/icon-set-checks.mjs +2 -1
  383. package/foundation/discovery/icon-set-discovery.mjs +2 -1
  384. package/foundation/discovery/integration-component-views.d.mts +15 -0
  385. package/foundation/discovery/integration-component-views.mjs +91 -0
  386. package/foundation/discovery/integration-doc-files.mjs +2 -1
  387. package/foundation/discovery/module-exports.d.mts +26 -1
  388. package/foundation/discovery/module-exports.mjs +114 -40
  389. package/foundation/discovery/name-roots.d.mts +38 -0
  390. package/foundation/discovery/name-roots.mjs +108 -0
  391. package/foundation/discovery/next-routes.d.mts +48 -0
  392. package/foundation/discovery/next-routes.mjs +181 -0
  393. package/foundation/discovery/package-exports.d.mts +10 -0
  394. package/foundation/discovery/package-exports.mjs +56 -3
  395. package/foundation/discovery/prop-drift.mjs +9 -9
  396. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  397. package/foundation/discovery/runner-allowlist.mjs +2 -0
  398. package/foundation/discovery/stored-component-list.d.mts +43 -0
  399. package/foundation/discovery/stored-component-list.mjs +90 -0
  400. package/foundation/discovery/stored-search-catalog.d.mts +115 -0
  401. package/foundation/discovery/stored-search-catalog.mjs +297 -0
  402. package/foundation/discovery/stored-self-audit.d.mts +26 -10
  403. package/foundation/discovery/stored-self-audit.mjs +40 -18
  404. package/foundation/discovery/template-adapter.d.mts +49 -29
  405. package/foundation/discovery/template-adapter.mjs +89 -99
  406. package/foundation/discovery/template-destination.d.mts +28 -0
  407. package/foundation/discovery/template-destination.mjs +104 -0
  408. package/foundation/discovery/template-icons.mjs +2 -1
  409. package/foundation/discovery/template-ids.d.mts +69 -0
  410. package/foundation/discovery/template-ids.mjs +199 -0
  411. package/foundation/discovery/theme-discovery.d.mts +12 -0
  412. package/foundation/discovery/theme-discovery.mjs +34 -4
  413. package/foundation/discovery/theme-imports.d.mts +24 -0
  414. package/foundation/discovery/theme-imports.mjs +61 -4
  415. package/foundation/discovery/theming-targets.d.mts +13 -5
  416. package/foundation/discovery/theming-targets.mjs +41 -47
  417. package/foundation/doc-compiler/generated-levels.d.mts +43 -0
  418. package/foundation/doc-compiler/generated-levels.mjs +72 -0
  419. package/foundation/doc-compiler/import.mjs +7 -1
  420. package/foundation/doc-compiler/inputs.mjs +3 -2
  421. package/foundation/doc-compiler/literal-doc.d.mts +12 -0
  422. package/foundation/doc-compiler/literal-doc.mjs +322 -0
  423. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  424. package/foundation/doc-compiler/stored-docs.d.mts +29 -0
  425. package/foundation/doc-compiler/stored-docs.mjs +63 -1
  426. package/foundation/doc-compiler/tree.d.mts +4 -84
  427. package/foundation/doc-compiler/tree.mjs +4 -62
  428. package/foundation/env/semver.mjs +3 -1
  429. package/foundation/fonts/font-stylesheet.d.mts +46 -0
  430. package/foundation/fonts/font-stylesheet.mjs +173 -0
  431. package/foundation/fs/cache-eviction.d.mts +21 -0
  432. package/foundation/fs/cache-eviction.mjs +73 -0
  433. package/foundation/fs/exact-codec.d.mts +12 -2
  434. package/foundation/fs/exact-codec.mjs +23 -6
  435. package/foundation/fs/file-protection.mjs +16 -5
  436. package/foundation/fs/gather-cache.d.mts +94 -4
  437. package/foundation/fs/gather-cache.mjs +671 -106
  438. package/foundation/fs/glob.d.mts +16 -0
  439. package/foundation/fs/glob.mjs +105 -0
  440. package/foundation/fs/jiti-cache.d.mts +19 -1
  441. package/foundation/fs/jiti-cache.mjs +130 -6
  442. package/foundation/fs/kept-tree.d.mts +80 -0
  443. package/foundation/fs/kept-tree.mjs +240 -0
  444. package/foundation/fs/listing-cache.d.mts +7 -0
  445. package/foundation/fs/listing-cache.mjs +10 -0
  446. package/foundation/fs/module-loader.d.mts +24 -2
  447. package/foundation/fs/module-loader.mjs +94 -21
  448. package/foundation/fs/path-safety.d.mts +11 -2
  449. package/foundation/fs/path-safety.mjs +108 -18
  450. package/foundation/fs/paths.d.mts +18 -0
  451. package/foundation/fs/paths.mjs +15 -2
  452. package/foundation/fs/read-record.d.mts +56 -7
  453. package/foundation/fs/read-record.mjs +280 -56
  454. package/foundation/fs/regular-file.d.mts +29 -0
  455. package/foundation/fs/regular-file.mjs +77 -0
  456. package/foundation/fs/stop-cleanup.d.mts +12 -0
  457. package/foundation/fs/stop-cleanup.mjs +66 -0
  458. package/foundation/identity/provider-identity.d.mts +6 -0
  459. package/foundation/identity/provider-identity.mjs +10 -1
  460. package/foundation/integrations/contribution-inventory.mjs +4 -3
  461. package/foundation/integrations/example-code.d.mts +63 -0
  462. package/foundation/integrations/example-code.mjs +416 -0
  463. package/foundation/integrations/integrations.d.mts +23 -1
  464. package/foundation/integrations/integrations.mjs +18 -1
  465. package/foundation/integrations/pack-state.d.mts +75 -0
  466. package/foundation/integrations/pack-state.mjs +314 -0
  467. package/foundation/integrations/public-imports.d.mts +9 -0
  468. package/foundation/integrations/public-imports.mjs +3 -2
  469. package/foundation/integrations/shipped-imports.d.mts +122 -0
  470. package/foundation/integrations/shipped-imports.mjs +497 -0
  471. package/foundation/integrations/validate-contributions.d.mts +0 -17
  472. package/foundation/integrations/validate-contributions.mjs +59 -333
  473. package/foundation/render/assets.mjs +52 -7
  474. package/foundation/render/charts.d.mts +78 -27
  475. package/foundation/render/charts.mjs +266 -90
  476. package/foundation/render/embedded-files.d.mts +6 -0
  477. package/foundation/render/embedded-files.mjs +25 -5
  478. package/foundation/render/embedded-imports.d.mts +70 -7
  479. package/foundation/render/embedded-imports.mjs +132 -41
  480. package/foundation/render/file-header.d.mts +66 -0
  481. package/foundation/render/file-header.mjs +118 -0
  482. package/foundation/render/host-answer.d.mts +18 -0
  483. package/foundation/render/host-answer.mjs +46 -0
  484. package/foundation/render/host-exit.d.mts +1 -0
  485. package/foundation/render/host-exit.mjs +6 -1
  486. package/foundation/render/html-reader.d.mts +55 -2
  487. package/foundation/render/html-reader.mjs +145 -26
  488. package/foundation/render/layers.d.mts +30 -0
  489. package/foundation/render/layers.mjs +158 -0
  490. package/foundation/render/markup-edits.d.mts +57 -0
  491. package/foundation/render/markup-edits.mjs +182 -0
  492. package/foundation/render/offline.d.mts +32 -0
  493. package/foundation/render/offline.mjs +56 -0
  494. package/foundation/render/page-host.mjs +29 -12
  495. package/foundation/render/page-worker.mjs +354 -21
  496. package/foundation/render/picture-chrome.d.mts +4 -0
  497. package/foundation/render/picture-chrome.mjs +192 -0
  498. package/foundation/render/picture-playwright.d.mts +4 -0
  499. package/foundation/render/picture-playwright.mjs +46 -0
  500. package/foundation/render/rebuild.mjs +2 -1
  501. package/foundation/render/render-page.d.mts +79 -5
  502. package/foundation/render/render-page.mjs +735 -109
  503. package/foundation/render/source-block.d.mts +85 -6
  504. package/foundation/render/source-block.mjs +211 -20
  505. package/foundation/render/ts-paths.d.mts +69 -6
  506. package/foundation/render/ts-paths.mjs +326 -40
  507. package/foundation/response/error-codes.d.mts +3 -1
  508. package/foundation/response/error-codes.d.ts +1 -0
  509. package/foundation/response/error-codes.doc.mjs +12 -1
  510. package/foundation/response/error-codes.mjs +11 -1
  511. package/foundation/response/response-types.doc.mjs +6 -6
  512. package/foundation/suggest/name-index.d.mts +148 -0
  513. package/foundation/suggest/name-index.mjs +317 -0
  514. package/foundation/suggest/other-command.d.mts +140 -0
  515. package/foundation/suggest/other-command.mjs +756 -0
  516. package/foundation/text/compare-text.d.mts +14 -0
  517. package/foundation/text/compare-text.mjs +68 -0
  518. package/foundation/text/css-escape.d.mts +18 -0
  519. package/foundation/text/css-escape.mjs +35 -0
  520. package/foundation/text/entry-names.d.mts +14 -0
  521. package/foundation/text/entry-names.mjs +14 -0
  522. package/foundation/text/string-utils.d.mts +46 -6
  523. package/foundation/text/string-utils.mjs +118 -20
  524. package/foundation/text/suggest-order.d.mts +47 -4
  525. package/foundation/text/suggest-order.mjs +174 -5
  526. package/foundation/xle/browser.d.mts +2 -2
  527. package/foundation/xle/expand.mjs +2 -1
  528. package/host/index.mjs +8 -6
  529. package/package.json +7 -6
@@ -17,38 +17,70 @@
17
17
  * integration's contribution-check results and the whole list (with the
18
18
  * template and component replacement passes), which `component`,
19
19
  * `template`, `theme`, `discover` and `search` report, by template
20
- * discovery (foundation/discovery/template-adapter.mjs) for its records, by theme discovery
21
- * (foundation/discovery/theme-discovery.mjs) for each integration's themes,
22
- * and by `doctor` (api/doctor/_adapter.mjs) for its file-only docs checks,
23
- * in a run that records modules and variables to its end. The commands
24
- * whose runs keep these entries are `search`, `build`, `component`,
25
- * `template`, `theme`, `discover`, `docs` and `doctor`. Gathers are kept only once the folder exists: the first such run
26
- * on a machine just makes it, so a one-off run pays nothing to store one. Only a published CLI stores anything:
20
+ * discovery (foundation/discovery/template-adapter.mjs) for its records, by
21
+ * the component list (api/component/list/list.mjs) and component
22
+ * replacement (foundation/discovery/component-replacement.mjs), by theme
23
+ * discovery (foundation/discovery/theme-discovery.mjs) for each
24
+ * integration's themes, by theme build
25
+ * (foundation/discovery/core-theme-facts.mjs) for what it checks a theme
26
+ * against in Core's component docs,
27
+ * by `doctor` (api/doctor/_adapter.mjs) for its file-only docs checks and
28
+ * its theme-wiring scan, by the docs adapter (api/docs/_adapter.mjs) for
29
+ * the CLI's own docs tree, which `docs` and `search` read,
30
+ * in a run that records modules and variables to its end, and, through
31
+ * `persistedSummary`, by kept-tree.mjs for summaries of folder trees, the
32
+ * lookup commands' not-found hint's name index among them
33
+ * (foundation/suggest/name-index.mjs), each kept while every path it
34
+ * stamped but the CLI's own files (which its build id covers) is
35
+ * unchanged, in any command. The commands whose runs keep the other
36
+ * entries are `search`, `build`, `component`, `template`, `theme`,
37
+ * `discover`, `docs` and `doctor`. The first such run on a machine makes the
38
+ * folder and keeps its gathers at once, so the next run (most often the same
39
+ * session's next command) uses them; doctor never makes it. A run through
40
+ * the host entry keeps them too, keyed on what the host passed
41
+ * (hostInputs). Only a published CLI stores anything:
27
42
  * its build id (clients/cli/build-id.generated.json, written at `prepack`)
28
43
  * names its exact code, so a checkout, whose code may change between runs
29
44
  * under the same version, always gathers.
30
45
  *
46
+ * An entry stores only the states that the kept entries it builds on do
47
+ * not hold: it names each of them, at the version it read, and is used
48
+ * only while each is still there at that version and holds. A run checks
49
+ * each kept entry once, so entries that read the same files (the component
50
+ * list, its replacements, the project's issues) check those files once. An
51
+ * entry built on is marked as used after the entries built on it, so
52
+ * eviction removes it after them. A gather's entry file holds two lines:
53
+ * what the entry is and stands for (its format, key, version, the entries
54
+ * it builds on, its states and variables), then its value. Checking an
55
+ * entry, or one an entry builds on, parses only the first line.
56
+ *
31
57
  * The folder is `catalogs` in the user's cache folder (userCacheDir):
32
58
  * created 0700, used only while it is a real folder owned by the user and
33
59
  * closed to others (isPrivateDir; never on a system with no user ids), each file written 0600 by a rename, oldest first evicted past
34
- * CACHE_LIMIT_BYTES. Any failure to read, check, parse or write is silent
60
+ * CACHE_LIMIT_BYTES (cache-eviction.mjs). Any failure to read, check, parse or write is silent
35
61
  * and the run gathers fresh, as it would with no cache.
36
62
  */
37
63
 
38
- import {createHash} from 'node:crypto';
64
+ import {createHash, randomBytes} from 'node:crypto';
39
65
  import * as fs from 'node:fs';
40
66
  import * as nodeModule from 'node:module';
41
67
  import * as os from 'node:os';
42
68
  import * as path from 'node:path';
43
69
  import {fileURLToPath} from 'node:url';
44
70
  import {isDeepStrictEqual} from 'node:util';
45
- import {CLI_ROOT} from './paths.mjs';
71
+ import {CLI_ROOT, realDir} from './paths.mjs';
46
72
  import {mayHaveIntegrations} from '../integrations/may-have-integrations.mjs';
47
73
  import {currentUid, isPrivateDir} from '../env/private-dir.mjs';
48
74
  import {userCacheDir} from '../env/user-dirs.mjs';
49
75
  import {jitiCacheDir} from './jiti-cache.mjs';
76
+ import {hostedRun} from '../runtime/package-root.mjs';
77
+ import {cliDoor} from '../env/cli-command.mjs';
78
+ import {CACHE_LIMIT_BYTES, evictOldest} from './cache-eviction.mjs';
50
79
  import {
80
+ PRESENT,
81
+ lookedUp,
51
82
  fileStamp,
83
+ keepState,
52
84
  recordReads,
53
85
  recordStates,
54
86
  stillInState,
@@ -78,7 +110,9 @@ const KEPT_COMMANDS = new Set([
78
110
  'theme',
79
111
  'discover',
80
112
  'docs',
81
- // The project check alone (no subcommand): its docs checks are kept.
113
+ // Its project check, with or without `--built`, keeps its docs checks and
114
+ // theme-wiring scan; its `integration` checks run neither, and keep only
115
+ // the project gathers they load, as the commands above do.
82
116
  'doctor',
83
117
  ]);
84
118
 
@@ -90,11 +124,15 @@ export const BUILD_ID_FILE = path.join(
90
124
  'build-id.generated.json',
91
125
  );
92
126
 
93
- /** The most the folder holds; the oldest entries go first past it. */
94
- export const CACHE_LIMIT_BYTES = 64 * 1024 * 1024;
127
+ /**
128
+ * Whether a file in the folder is a kept entry, which eviction counts.
129
+ * @param {string} name
130
+ * @returns {boolean}
131
+ */
132
+ const isEntry = name => name.startsWith('gather-');
95
133
 
96
134
  /** The stored format; a different one is a miss. */
97
- const FORMAT = 1;
135
+ const FORMAT = 2;
98
136
 
99
137
  /** A variable the run read that was unset, as stored. */
100
138
  const UNSET = null;
@@ -106,7 +144,7 @@ const UNSET = null;
106
144
  * variables only while the run is armed: from its start to the end of its
107
145
  * first gather, and during each later gather, or, in a run armed throughout
108
146
  * (`doctor`), to its end. Null outside a recorded run.
109
- * @type {{reads: Map<string, string>, env: Map<string, string | null>, arm: () => boolean, disarm: () => void, armedThroughout: boolean, realEnv: NodeJS.ProcessEnv} | null}
147
+ * @type {{reads: Map<string, string>, env: Map<string, string | null>, arm: () => boolean, disarm: () => void, armedThroughout: boolean, realEnv: NodeJS.ProcessEnv, confirmed: Confirmed} | null}
110
148
  */
111
149
  let run = null;
112
150
 
@@ -129,14 +167,27 @@ let lastOutcome = null;
129
167
  * those it was stored with and that a hit has just found unchanged, or those
130
168
  * a fresh gather was stored with. A derived value keys on them, so an edit to
131
169
  * the CLI's code in an installed copy, which misses the gather, misses the
132
- * derived value too. Null before any kept gather in the run.
133
- * @type {[string, string][] | null}
170
+ * derived value too. Null before any kept gather in the run. A hit leaves
171
+ * how to work them out, since only a derived value after it reads them.
172
+ * @type {[string, string][] | (() => [string, string][]) | null}
134
173
  */
135
174
  let gatherCode = null;
136
175
 
137
176
  /** How many persistedGather calls are running, nested inside one another. */
138
177
  let gatherDepth = 0;
139
178
 
179
+ /**
180
+ * The kept entries a recorded run has read back or stored, by file: the
181
+ * version each holds, every state it stands for (its own and those of the
182
+ * entries it builds on, as the lists each stores, once each), and every entry
183
+ * it builds on, directly or not. A later entry stores only the states none of
184
+ * them holds and names the ones it builds on, so a run checks each state
185
+ * once. A kept entry is checked before the run's command writes anything; the
186
+ * record ends with the run.
187
+ * @typedef {{version: string, lists: [string, string][][], under: Set<string>, deps: string[], merged?: [string, string][]}} ConfirmedEntry
188
+ * @typedef {Map<string, ConfirmedEntry>} Confirmed
189
+ */
190
+
140
191
  /**
141
192
  * The outcome of the last kept gather in this process: whether it was a hit,
142
193
  * and why it missed. Null when no kept gather ran.
@@ -241,8 +292,6 @@ export async function recordRunFor(argv, fn) {
241
292
  if (
242
293
  !command ||
243
294
  !KEPT_COMMANDS.has(command) ||
244
- (command === 'doctor' &&
245
- argv.filter(arg => !arg.startsWith('-')).length > 1) ||
246
295
  runsUnderPnp() ||
247
296
  currentUid() == null ||
248
297
  withoutRecording(buildId) == null ||
@@ -254,22 +303,11 @@ export async function recordRunFor(argv, fn) {
254
303
  ) {
255
304
  return fn();
256
305
  }
257
- // Gathers are kept only once the folder exists: the first recorded run on a
258
- // machine (search, build, component, template, theme, discover or docs) just makes
259
- // it, so a one-off run never pays to store one.
260
- const dir = cacheDirPath();
261
- if (!withoutRecording(() => fs.existsSync(dir))) {
262
- // Once the per-user cache folder exists doctor uses it; it never makes
263
- // it, so a doctor alone changes nothing on the machine (nor its own
264
- // cache row).
265
- if (command === 'doctor') return fn();
266
- try {
267
- withoutRecording(() => fs.mkdirSync(dir, {recursive: true, mode: 0o700}));
268
- } catch {
269
- // A folder that cannot be made keeps no catalogs; nothing more.
270
- }
271
- return fn();
272
- }
306
+ // The first recorded run on a machine (search, build, component, template,
307
+ // theme, discover or docs) makes the folder and keeps its gathers at once.
308
+ // Once the per-user cache folder exists doctor uses it; it never makes it,
309
+ // so a doctor alone changes nothing on the machine (nor its own cache row).
310
+ if (command !== 'doctor') makeGatherCacheDir();
273
311
  // A folder the cache would refuse (not the user's own, open to others, a
274
312
  // link) leaves the run exactly as without the cache: no hook is installed.
275
313
  if (withoutRecording(gatherCacheDir) == null) return fn();
@@ -279,6 +317,35 @@ export async function recordRunFor(argv, fn) {
279
317
  return recordRun(fn, {armedThroughout: command === 'doctor'});
280
318
  }
281
319
 
320
+ /**
321
+ * Make the kept catalogs' folder where this run could keep entries in it and
322
+ * it does not exist yet: an installed CLI with a build id, outside Yarn
323
+ * Plug'n'Play, on a system with user ids. Recorded runs make it at their
324
+ * start; the lookup commands' not-found hint makes it before it keeps its
325
+ * name index, so a project's first miss keeps what later misses read. A
326
+ * folder that cannot be made keeps nothing; nothing more.
327
+ * @returns {boolean} whether this run keeps summaries there
328
+ */
329
+ export function makeGatherCacheDir() {
330
+ if (
331
+ runsUnderPnp() ||
332
+ currentUid() == null ||
333
+ withoutRecording(buildId) == null
334
+ ) {
335
+ return false;
336
+ }
337
+ const dir = cacheDirPath();
338
+ try {
339
+ withoutRecording(() => {
340
+ if (!fs.existsSync(dir))
341
+ fs.mkdirSync(dir, {recursive: true, mode: 0o700});
342
+ });
343
+ } catch {
344
+ // A folder that cannot be made keeps no catalogs.
345
+ }
346
+ return withoutRecording(gatherCacheDir) != null;
347
+ }
348
+
282
349
  /**
283
350
  * Run `fn` as a recorded run (see `run`), armed from its start. Exported for
284
351
  * tests; `recordRunFor` decides which runs are recorded.
@@ -373,7 +440,8 @@ export async function recordRun(fn, {armedThroughout = false} = {}) {
373
440
  if (hooks && url.startsWith('file:')) {
374
441
  try {
375
442
  const file = fileURLToPath(url);
376
- if (!reads.has(file)) reads.set(file, fileStamp(file));
443
+ // The load reads the module's content: its stamp.
444
+ keepState(reads, file, fileStamp, 'stamp');
377
445
  } catch {
378
446
  // A URL with no file path holds nothing to check.
379
447
  }
@@ -389,7 +457,15 @@ export async function recordRun(fn, {armedThroughout = false} = {}) {
389
457
  }
390
458
  };
391
459
  if (!arm()) return fn();
392
- run = {reads, env, arm, disarm, armedThroughout, realEnv};
460
+ run = {
461
+ reads,
462
+ env,
463
+ arm,
464
+ disarm,
465
+ armedThroughout,
466
+ realEnv,
467
+ confirmed: new Map(),
468
+ };
393
469
  try {
394
470
  return await recordReads(reads, fn, {exact: true});
395
471
  } finally {
@@ -424,7 +500,7 @@ function noteLookups(specifier, parentURL, reads) {
424
500
  for (;;) {
425
501
  if (path.basename(dir) !== 'node_modules') {
426
502
  const candidate = path.join(dir, 'node_modules', name);
427
- if (!reads.has(candidate)) reads.set(candidate, lookupStamp(candidate));
503
+ keepState(reads, candidate, lookupStamp);
428
504
  }
429
505
  const up = path.dirname(dir);
430
506
  if (up === dir) break;
@@ -516,9 +592,29 @@ function entryKey(key, id) {
516
592
  process.platform,
517
593
  process.arch,
518
594
  ...key,
595
+ hostInputs(),
519
596
  ]);
520
597
  }
521
598
 
599
+ /**
600
+ * What a host passed that decides a gather, or null outside a hosted run: its
601
+ * package sets, by real path and in its order, the integrations it approves,
602
+ * and the command it runs the CLI as, which kept findings print. A run reads
603
+ * only some of a package set's files, so their stamps cannot tell a hosted
604
+ * run from a run of the bin in the same folder, and no file holds the
605
+ * command. The run's folder is in each gather's own key.
606
+ * @returns {{roots: string[], integrations: string[], door: string | null} | null}
607
+ */
608
+ function hostInputs() {
609
+ const hosted = hostedRun();
610
+ if (hosted == null) return null;
611
+ return {
612
+ roots: hosted.roots.map(root => withoutRecording(() => realDir(root))),
613
+ integrations: [...hosted.integrations],
614
+ door: cliDoor() ?? null,
615
+ };
616
+ }
617
+
522
618
  /**
523
619
  * Whether every stored state still holds: files, folders and modules, and the
524
620
  * environment.
@@ -541,11 +637,214 @@ function staleReason(stamps, env) {
541
637
  }
542
638
  }
543
639
  for (const [file, state] of stamps) {
544
- if (!stillInState(file, state)) return 'a file or folder it read changed';
640
+ if (!stillInState(file, state)) {
641
+ return 'a file or folder it read changed';
642
+ }
545
643
  }
546
644
  return null;
547
645
  }
548
646
 
647
+ /**
648
+ * The states of the kept entries an entry builds on, each found unchanged
649
+ * once per run, with every entry under them and the files of the ones it
650
+ * names; or null when one is gone, holds another version, or changed.
651
+ * @param {string} dir
652
+ * @param {unknown} deps - [entry file name, version] pairs, as stored
653
+ * @param {Confirmed} confirmed - the entries already found unchanged
654
+ * @param {Set<string>} [reading] - entries being checked, so a loop of
655
+ * entries that name each other is a miss
656
+ * @returns {{lists: [string, string][][], under: Set<string>, direct: string[]} | null}
657
+ */
658
+ function builtOn(dir, deps, confirmed, reading = new Set()) {
659
+ if (!Array.isArray(deps)) return null;
660
+ /** @type {[string, string][][]} */
661
+ const lists = [];
662
+ // Two entries this one names may build on one entry: its list counts once.
663
+ /** @type {Set<[string, string][]>} */
664
+ const listed = new Set();
665
+ /** @type {Set<string>} */
666
+ const under = new Set();
667
+ /** @type {string[]} */
668
+ const direct = [];
669
+ for (const dep of deps) {
670
+ if (!Array.isArray(dep) || typeof dep[0] !== 'string') return null;
671
+ const file = path.join(dir, path.basename(dep[0]));
672
+ const entry = confirmedEntry(file, dep[1], confirmed, reading);
673
+ if (entry == null) return null;
674
+ for (const list of entry.lists) {
675
+ if (listed.has(list)) continue;
676
+ listed.add(list);
677
+ lists.push(list);
678
+ }
679
+ direct.push(file);
680
+ under.add(file);
681
+ for (const below of entry.under) under.add(below);
682
+ }
683
+ return {lists, under, direct};
684
+ }
685
+
686
+ /**
687
+ * Every state a confirmed entry stands for, one per path: its lists, merged
688
+ * the first time something needs them as one (choosing what a new entry
689
+ * builds on).
690
+ * @param {ConfirmedEntry} entry
691
+ * @returns {[string, string][]}
692
+ */
693
+ function statesOf(entry) {
694
+ entry.merged ??= mergeStates(...entry.lists);
695
+ return entry.merged;
696
+ }
697
+
698
+ /**
699
+ * States from several lists, one per path, as a run's record keeps them
700
+ * (keepState).
701
+ * @param {...[string, string][]} lists
702
+ * @returns {[string, string][]}
703
+ */
704
+ function mergeStates(...lists) {
705
+ /** @type {Map<string, string>} */
706
+ const merged = new Map();
707
+ for (const list of lists) {
708
+ for (const [at, state] of list) {
709
+ const had = merged.get(at);
710
+ if (had === undefined) merged.set(at, state);
711
+ else if (had !== state) keepState(merged, at, state);
712
+ }
713
+ }
714
+ return [...merged];
715
+ }
716
+
717
+ /**
718
+ * Mark an entry as used now, and each kept entry it builds on as used a
719
+ * millisecond (the finest step of a file time set from a Date) after the
720
+ * latest entry built on it, so eviction, which removes the least recently
721
+ * used entries first, never removes an entry before one built on it.
722
+ * @param {string} file
723
+ * @param {Confirmed} confirmed
724
+ */
725
+ function markUsed(file, confirmed) {
726
+ /** @type {Map<string, number>} */
727
+ const times = new Map();
728
+ /** @type {Set<string>} */
729
+ const onPath = new Set();
730
+ /**
731
+ * @param {string} at
732
+ * @param {number} time
733
+ */
734
+ const visit = (at, time) => {
735
+ if (onPath.has(at) || (times.get(at) ?? -Infinity) >= time) return;
736
+ times.set(at, time);
737
+ onPath.add(at);
738
+ for (const dep of confirmed.get(at)?.deps ?? []) visit(dep, time + 1);
739
+ onPath.delete(at);
740
+ };
741
+ visit(file, Date.now());
742
+ for (const [at, time] of times) {
743
+ try {
744
+ const when = new Date(time);
745
+ withoutRecording(() => fs.utimesSync(at, when, when));
746
+ } catch {
747
+ // An entry that cannot be touched is evicted sooner; nothing more.
748
+ }
749
+ }
750
+ }
751
+
752
+ /**
753
+ * A kept entry at `version`, found unchanged as a hit finds it: read and
754
+ * checked the first time the run meets it, after that from `confirmed`.
755
+ * @param {string} file
756
+ * @param {unknown} version
757
+ * @param {Confirmed} confirmed
758
+ * @param {Set<string>} reading
759
+ * @returns {ConfirmedEntry | null}
760
+ */
761
+ function confirmedEntry(file, version, confirmed, reading) {
762
+ const known = confirmed.get(file);
763
+ if (known) return known.version === version ? known : null;
764
+ if (reading.has(file)) return null;
765
+ reading.add(file);
766
+ try {
767
+ const stored = readHead(file);
768
+ if (
769
+ stored?.format !== FORMAT ||
770
+ typeof stored.version !== 'string' ||
771
+ stored.version !== version ||
772
+ staleReason(stored.stamps, stored.env) != null
773
+ ) {
774
+ return null;
775
+ }
776
+ const base = builtOn(path.dirname(file), stored.deps, confirmed, reading);
777
+ if (base == null) return null;
778
+ /** @type {ConfirmedEntry} */
779
+ const entry = {
780
+ version: stored.version,
781
+ lists: [...base.lists, stored.stamps],
782
+ under: base.under,
783
+ deps: base.direct,
784
+ };
785
+ confirmed.set(file, entry);
786
+ return entry;
787
+ } catch {
788
+ return null;
789
+ } finally {
790
+ reading.delete(file);
791
+ }
792
+ }
793
+
794
+ /**
795
+ * The kept entries a new entry builds on: those the run has read back or
796
+ * stored whose every state the run read too, as it holds them, largest
797
+ * first, each only when it holds a state the others do not. Never one that
798
+ * builds on the new entry's own file. An entry that only checked whether a
799
+ * path is there still holds where the run read more of that path (holdsIn);
800
+ * the new entry stores that fuller state, and the rest of its states,
801
+ * itself.
802
+ * @param {string} file - the new entry's file
803
+ * @param {[string, string][]} stamps - every state the run read
804
+ * @param {Confirmed} confirmed
805
+ * @returns {{deps: [string, string][], own: [string, string][], under: Set<string>}}
806
+ */
807
+ function chooseBase(file, stamps, confirmed) {
808
+ const states = new Map(stamps);
809
+ /** @type {Set<string>} */
810
+ const covered = new Set();
811
+ /** @type {[string, string][]} */
812
+ const deps = [];
813
+ /** @type {Set<string>} */
814
+ const under = new Set();
815
+ const candidates = [...confirmed]
816
+ .filter(([other, entry]) => other !== file && !entry.under.has(file))
817
+ .map(([other, entry]) => ({other, entry, held: statesOf(entry)}))
818
+ .sort((a, b) => b.held.length - a.held.length);
819
+ for (const {other, entry, held} of candidates) {
820
+ if (held.length === 0) continue;
821
+ if (!held.every(([at, state]) => holdsIn(states.get(at), state))) continue;
822
+ const same = held.filter(([at, state]) => states.get(at) === state);
823
+ if (same.every(([at]) => covered.has(at))) continue;
824
+ deps.push([path.basename(other), entry.version]);
825
+ under.add(other);
826
+ for (const below of entry.under) under.add(below);
827
+ for (const [at] of same) covered.add(at);
828
+ }
829
+ return {deps, own: stamps.filter(([at]) => !covered.has(at)), under};
830
+ }
831
+
832
+ /**
833
+ * Whether a state an entry holds agrees with the state the run read of the
834
+ * same path: the same state, or the entry holds only that the path is there,
835
+ * or what a real-path lookup of it gave (confirmed this run), and the run
836
+ * read it there.
837
+ * @param {string | undefined} read - the run's state of the path
838
+ * @param {string} held - the entry's state of it
839
+ * @returns {boolean}
840
+ */
841
+ function holdsIn(read, held) {
842
+ return (
843
+ read === held ||
844
+ ((held === PRESENT || lookedUp(held)) && read !== undefined && read !== '-')
845
+ );
846
+ }
847
+
549
848
  /**
550
849
  * The gather `load` returns, or the one a run with the same key stored while
551
850
  * every input it read is unchanged (see the file header).
@@ -604,21 +903,45 @@ async function gatherAt(key, load, codec, keep) {
604
903
  const file = entryFile(dir, key, id);
605
904
  let reason = 'no entry';
606
905
  try {
607
- const stored = JSON.parse(
608
- withoutRecording(() => fs.readFileSync(file, 'utf-8')),
609
- );
906
+ const {head: stored, value: storedValue} = readEntry(file);
610
907
  if (stored?.format !== FORMAT) {
611
908
  reason = 'another format';
612
909
  } else if (stored.key !== entryKey(key, id)) {
613
910
  reason = 'an entry for other inputs';
614
911
  } else {
615
- const stale = staleReason(stored.stamps, stored.env);
616
- const value = stale == null ? readBack(stored, codec) : undefined;
912
+ // An entry the run already found unchanged, as one an earlier entry
913
+ // builds on, is not checked again. Outside a recorded run nothing is
914
+ // remembered from one gather to the next.
915
+ const confirmed = run?.confirmed ?? new Map();
916
+ const known = confirmed.get(file);
917
+ const seen =
918
+ typeof stored.version === 'string' && known?.version === stored.version
919
+ ? known
920
+ : null;
921
+ const own = seen ? null : staleReason(stored.stamps, stored.env);
922
+ const base =
923
+ seen || own != null ? null : builtOn(dir, stored.deps, confirmed);
924
+ const stale =
925
+ own ??
926
+ (seen == null && base == null
927
+ ? 'a kept entry it builds on changed'
928
+ : null);
929
+ const value = stale == null ? readBack(storedValue, codec) : undefined;
617
930
  if (stale == null && value === undefined) {
618
931
  // Gathered again below, and stored over the bad entry.
619
932
  reason = 'a malformed value';
620
933
  } else if (stale == null) {
621
- recordStates(stored.stamps);
934
+ /** @type {ConfirmedEntry} */
935
+ const entry = seen ?? {
936
+ version: stored.version,
937
+ lists: [...(base?.lists ?? []), stored.stamps],
938
+ under: base?.under ?? new Set(),
939
+ deps: base?.direct ?? [],
940
+ };
941
+ confirmed.set(file, entry);
942
+ // Recorded list by list, which keeps one state per path as a merge
943
+ // would (recordStates).
944
+ for (const list of entry.lists) recordStates(list);
622
945
  // A gather around this one depends on what this one read, the
623
946
  // environment included: a hit hands its variables on, as a fresh
624
947
  // gather's reads would be.
@@ -627,16 +950,15 @@ async function gatherAt(key, load, codec, keep) {
627
950
  if (!run.env.has(name)) run.env.set(name, value);
628
951
  }
629
952
  }
630
- gatherCode = cliStates(stored.stamps);
953
+ // The CLI's own files, one state per path, in the order a fresh
954
+ // gather reads them: merging only those is merging all, then picking
955
+ // them out.
956
+ const {lists} = entry;
957
+ gatherCode = () => mergeStates(...lists.map(cliStates));
631
958
  lastOutcome = {hit: true, reason: 'unchanged'};
632
- try {
633
- const now = new Date();
634
- withoutRecording(() => fs.utimesSync(file, now, now));
635
- } catch {
636
- // An entry that cannot be touched is evicted sooner; nothing more.
637
- }
959
+ markUsed(file, confirmed);
638
960
  return value;
639
- } else {
961
+ } else if (stale != null) {
640
962
  reason = stale;
641
963
  }
642
964
  }
@@ -650,6 +972,10 @@ async function gatherAt(key, load, codec, keep) {
650
972
  const value = await load();
651
973
  if (run && armed && keep(value)) {
652
974
  withoutRecording(() => store(dir, file, entryKey(key, id), value, codec));
975
+ } else {
976
+ // A value worked out from a gather that is not kept is not kept either.
977
+ // The code a gather inside this one recorded did not build this one.
978
+ gatherCode = null;
653
979
  }
654
980
  return value;
655
981
  }
@@ -702,6 +1028,7 @@ function codeStamp(code, inputs) {
702
1028
  */
703
1029
  export function persistedDerived(key, compute, codec, inputs = []) {
704
1030
  if (!run || gatherCode == null) return compute();
1031
+ if (typeof gatherCode === 'function') gatherCode = gatherCode();
705
1032
  const id = withoutRecording(buildId);
706
1033
  const dir = id == null ? null : withoutRecording(gatherCacheDir);
707
1034
  if (id == null || dir == null) return compute();
@@ -716,7 +1043,7 @@ export function persistedDerived(key, compute, codec, inputs = []) {
716
1043
  stored?.format === FORMAT &&
717
1044
  stored.derived === true &&
718
1045
  stored.key === entry
719
- ? readBack(stored, codec)
1046
+ ? readBack(() => stored.value, codec)
720
1047
  : undefined;
721
1048
  if (value !== undefined) {
722
1049
  try {
@@ -733,37 +1060,266 @@ export function persistedDerived(key, compute, codec, inputs = []) {
733
1060
  const value = compute();
734
1061
  withoutRecording(() => {
735
1062
  try {
736
- const encoded = codec.encode(value);
1063
+ const body = storedText(codec, value);
1064
+ if (body === undefined) return;
1065
+ const head = JSON.stringify({format: FORMAT, derived: true, key: entry});
1066
+ // The value's text goes in as checked, as the head's last field.
1067
+ const text = `${head.slice(0, -1)},"value":${body}}`;
1068
+ const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
1069
+ fs.writeFileSync(temp, text, {mode: 0o600, flag: 'wx'});
1070
+ fs.renameSync(temp, file);
1071
+ evictOldest(dir, isEntry);
1072
+ } catch {
1073
+ // A value that cannot be stored is computed again next time.
1074
+ }
1075
+ });
1076
+ return value;
1077
+ }
1078
+
1079
+ /**
1080
+ * How old every stamped time must be before a summary is stored. A file
1081
+ * system can store times as coarsely as 2 s, so a change within one tick of
1082
+ * the last can leave a stamp as it was.
1083
+ */
1084
+ const SUMMARY_SETTLE_MS = 2000;
1085
+
1086
+ /**
1087
+ * Whether every stamp in `stamps` was taken longer than
1088
+ * {@link SUMMARY_SETTLE_MS} ago, by its modification and change times.
1089
+ * @param {[string, string][]} stamps
1090
+ * @returns {boolean}
1091
+ */
1092
+ function settled(stamps) {
1093
+ const limit = Date.now() - SUMMARY_SETTLE_MS;
1094
+ return stamps.every(([, stamp]) => {
1095
+ if (stamp === '-') return true;
1096
+ const [, mtime, ctime] = stamp.split(':').map(Number);
1097
+ return mtime < limit && ctime < limit;
1098
+ });
1099
+ }
1100
+
1101
+ /**
1102
+ * A summary of files and folders, made at once, kept as an entry beside the
1103
+ * gathers, in or out of a recorded run: the stored value while every path
1104
+ * `make` stamped has the stamp it was stored with (the CLI's own files are
1105
+ * covered by the build id instead: inCliBuild), else the one
1106
+ * `make` gives now, which is stored unless a stamp is younger than
1107
+ * {@link SUMMARY_SETTLE_MS}. The folder, the build id, the Yarn Plug'n'Play
1108
+ * rule, the 0600 rename write and the eviction are those of a kept gather;
1109
+ * anywhere a gather is not kept, or on any read or write error, `make` runs
1110
+ * as before.
1111
+ * @template T
1112
+ * @param {unknown[]} key - Everything else the summary depends on.
1113
+ * @param {() => {value: T, stamps: [string, string][]}} make - The summary,
1114
+ * with the stamp ({@link fileStamp}) of each path it depends on, each taken
1115
+ * before the path was read.
1116
+ * @param {{accept?: (value: T) => boolean}} [options] - `accept`: whether a
1117
+ * stored value serves this call, else it is made again and stored over (a
1118
+ * summary that holds more only once a caller asks for it).
1119
+ * @returns {T}
1120
+ */
1121
+ export function persistedSummary(key, make, {accept = () => true} = {}) {
1122
+ const at = summaryAt(key);
1123
+ if (!at) return make().value;
1124
+ const stored = storedSummary(at);
1125
+ if (stored && accept(stored.value)) return stored.value;
1126
+ const {value, stamps} = make();
1127
+ storeSummary(at, value, stamps);
1128
+ return value;
1129
+ }
1130
+
1131
+ /**
1132
+ * {@link persistedSummary} for a summary that `make` works out
1133
+ * asynchronously.
1134
+ * @template T
1135
+ * @param {unknown[]} key
1136
+ * @param {() => Promise<{value: T, stamps: [string, string][]}>} make
1137
+ * @param {{accept?: (value: T) => boolean}} [options] - As for
1138
+ * {@link persistedSummary}.
1139
+ * @returns {Promise<T>}
1140
+ */
1141
+ export async function persistedSummaryAsync(
1142
+ key,
1143
+ make,
1144
+ {accept = () => true} = {},
1145
+ ) {
1146
+ const at = summaryAt(key);
1147
+ if (!at) return (await make()).value;
1148
+ const stored = storedSummary(at);
1149
+ if (stored && accept(stored.value)) return stored.value;
1150
+ const {value, stamps} = await make();
1151
+ storeSummary(at, value, stamps);
1152
+ return value;
1153
+ }
1154
+
1155
+ /**
1156
+ * Where the summary for `key` is kept, or null where none is kept.
1157
+ * @param {unknown[]} key
1158
+ * @returns {{dir: string, file: string, entry: string} | null}
1159
+ */
1160
+ function summaryAt(key) {
1161
+ const id = withoutRecording(buildId);
1162
+ const dir =
1163
+ id == null || runsUnderPnp() ? null : withoutRecording(gatherCacheDir);
1164
+ if (id == null || dir == null) return null;
1165
+ const summaryKey = ['summary', ...key];
1166
+ return {
1167
+ dir,
1168
+ file: entryFile(dir, summaryKey, id),
1169
+ entry: entryKey(summaryKey, id),
1170
+ };
1171
+ }
1172
+
1173
+ /**
1174
+ * The kept summary at `at`, while every path it stamped has its stamp, else
1175
+ * undefined.
1176
+ * @param {{file: string, entry: string}} at
1177
+ * @returns {{value: any} | undefined}
1178
+ */
1179
+ function storedSummary({file, entry}) {
1180
+ try {
1181
+ const stored = JSON.parse(
1182
+ withoutRecording(() => fs.readFileSync(file, 'utf-8')),
1183
+ );
1184
+ if (
1185
+ stored?.format === FORMAT &&
1186
+ stored.summary === true &&
1187
+ stored.key === entry &&
1188
+ Object.hasOwn(stored, 'value') &&
1189
+ Array.isArray(stored.stamps) &&
1190
+ stored.stamps.every(
1191
+ (/** @type {[string, string]} */ [at, stamp]) =>
1192
+ typeof at === 'string' && fileStamp(at) === stamp,
1193
+ )
1194
+ ) {
1195
+ try {
1196
+ const now = new Date();
1197
+ withoutRecording(() => fs.utimesSync(file, now, now));
1198
+ } catch {
1199
+ // An entry that cannot be touched is evicted sooner; nothing more.
1200
+ }
1201
+ return {value: stored.value};
1202
+ }
1203
+ } catch {
1204
+ // No usable entry: made again.
1205
+ }
1206
+ return undefined;
1207
+ }
1208
+
1209
+ /**
1210
+ * Whether `file` is one of the installed CLI's own files: under its folder,
1211
+ * outside the node_modules folder inside it. A summary is kept only by an
1212
+ * installed CLI, under its build id (summaryAt), which names the exact code
1213
+ * of that build, so the build id covers its files; a package placed in that
1214
+ * node_modules folder is not part of the build.
1215
+ * @param {string} file
1216
+ * @returns {boolean}
1217
+ */
1218
+ function inCliBuild(file) {
1219
+ const root = CLI_ROOT + path.sep;
1220
+ return (
1221
+ file.startsWith(root) && !file.startsWith(root + 'node_modules' + path.sep)
1222
+ );
1223
+ }
1224
+
1225
+ /**
1226
+ * Keep `value` at `at` with its stamps, unless a stamp is too young or the
1227
+ * value does not read back the same. Silent on any failure. A file in jiti's
1228
+ * cache is not kept as a stamp, as for a kept gather (inJitiCache), nor is
1229
+ * one of the CLI's own files: the build id in the summary's key covers them
1230
+ * (inCliBuild), so a new build makes the summary again. Every other file,
1231
+ * an installed package's included, keeps its own stamp: an edit in place
1232
+ * makes the summary again.
1233
+ * @param {{dir: string, file: string, entry: string}} at
1234
+ * @param {unknown} value
1235
+ * @param {[string, string][]} made the stamps the summary was made with
1236
+ */
1237
+ function storeSummary({dir, file, entry}, value, made) {
1238
+ const stamps = made.filter(([at]) => !inJitiCache(at) && !inCliBuild(at));
1239
+ if (!settled(stamps)) return;
1240
+ withoutRecording(() => {
1241
+ try {
737
1242
  const text = JSON.stringify({
738
1243
  format: FORMAT,
739
- derived: true,
1244
+ summary: true,
740
1245
  key: entry,
741
- value: encoded,
1246
+ stamps,
1247
+ value,
742
1248
  });
743
- if (!isDeepStrictEqual(JSON.parse(text).value, encoded)) return;
1249
+ if (!isDeepStrictEqual(JSON.parse(text).value, value)) return;
744
1250
  const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
745
1251
  fs.writeFileSync(temp, text, {mode: 0o600, flag: 'wx'});
746
1252
  fs.renameSync(temp, file);
747
- evict(dir);
1253
+ evictOldest(dir, isEntry);
748
1254
  } catch {
749
- // A value that cannot be stored is computed again next time.
1255
+ // A summary that cannot be stored is made again next time.
1256
+ }
1257
+ });
1258
+ }
1259
+
1260
+ /**
1261
+ * A gather's entry file: its first line, parsed, and a reader of its value,
1262
+ * the second line, parsed only when called. A file of one line, as an entry
1263
+ * of another format, is all head and has no value.
1264
+ * @param {string} file
1265
+ * @returns {{head: any, value: () => unknown}}
1266
+ */
1267
+ function readEntry(file) {
1268
+ const text = withoutRecording(() => fs.readFileSync(file, 'utf-8'));
1269
+ const cut = text.indexOf('\n');
1270
+ return {
1271
+ head: JSON.parse(cut < 0 ? text : text.slice(0, cut)),
1272
+ value: () => (cut < 0 ? undefined : JSON.parse(text.slice(cut + 1))),
1273
+ };
1274
+ }
1275
+
1276
+ /** How much of an entry file one read takes while looking for its first line. */
1277
+ const HEAD_READ_BYTES = 64 * 1024;
1278
+
1279
+ /**
1280
+ * A gather's entry file's first line, parsed, read without the value after
1281
+ * it: an entry another builds on is checked, never read back. A newline byte
1282
+ * is never part of a longer UTF-8 character, so the bytes before the first
1283
+ * one are the whole line.
1284
+ * @param {string} file
1285
+ * @returns {any}
1286
+ */
1287
+ function readHead(file) {
1288
+ return withoutRecording(() => {
1289
+ const fd = fs.openSync(file, 'r');
1290
+ try {
1291
+ /** @type {Buffer[]} */
1292
+ const chunks = [];
1293
+ const chunk = Buffer.allocUnsafe(HEAD_READ_BYTES);
1294
+ for (let at = 0; ;) {
1295
+ const read = fs.readSync(fd, chunk, 0, chunk.length, at);
1296
+ if (read === 0) break;
1297
+ const piece = chunk.subarray(0, read);
1298
+ const end = piece.indexOf(0x0a);
1299
+ chunks.push(Buffer.from(end < 0 ? piece : piece.subarray(0, end)));
1300
+ if (end >= 0) break;
1301
+ at += read;
1302
+ }
1303
+ return JSON.parse(Buffer.concat(chunks).toString('utf-8'));
1304
+ } finally {
1305
+ fs.closeSync(fd);
750
1306
  }
751
1307
  });
752
- return value;
753
1308
  }
754
1309
 
755
1310
  /**
756
1311
  * A stored entry's value, decoded, or undefined when it has none, its decode
757
1312
  * throws, or the decoded value fails the codec's shape check (`valid`): such
758
1313
  * an entry is a miss, worked out again and stored over.
759
- * @param {any} stored
1314
+ * @param {() => unknown} read - reads the entry's value
760
1315
  * @param {{decode: (stored: any) => any, valid?: (value: any) => boolean}} codec
761
1316
  * @returns {any}
762
1317
  */
763
- function readBack(stored, codec) {
764
- if (!Object.hasOwn(stored, 'value') || stored.value == null) return undefined;
1318
+ function readBack(read, codec) {
765
1319
  try {
766
- const value = codec.decode(stored.value);
1320
+ const stored = read();
1321
+ if (stored == null) return undefined;
1322
+ const value = codec.decode(stored);
767
1323
  if (value == null) return undefined;
768
1324
  return !codec.valid || codec.valid(value) ? value : undefined;
769
1325
  } catch {
@@ -771,6 +1327,26 @@ function readBack(stored, codec) {
771
1327
  }
772
1328
  }
773
1329
 
1330
+ /**
1331
+ * The JSON text a kept value is stored as, checked once to read back as what
1332
+ * the codec meant: the codec's own checked text when it gives one
1333
+ * (`encodeText`, as EXACT_CODEC does), else its plain form, written and
1334
+ * checked to read back as that form. Undefined for a value that does not
1335
+ * read back exactly, which is never kept; a codec's refusal throws.
1336
+ * @template T
1337
+ * @param {{encode: (value: T) => unknown, encodeText?: (value: T) => string}} codec
1338
+ * @param {T} value
1339
+ * @returns {string | undefined}
1340
+ */
1341
+ function storedText(codec, value) {
1342
+ if (codec.encodeText) return codec.encodeText(value);
1343
+ const encoded = codec.encode(value);
1344
+ const text = JSON.stringify(encoded);
1345
+ return text !== undefined && isDeepStrictEqual(JSON.parse(text), encoded)
1346
+ ? text
1347
+ : undefined;
1348
+ }
1349
+
774
1350
  /**
775
1351
  * Store a fresh gather with what the run read, unless its plain form does not
776
1352
  * read back as the same value. Silent on any failure.
@@ -784,57 +1360,38 @@ function readBack(stored, codec) {
784
1360
  function store(dir, file, entry, value, codec) {
785
1361
  if (!run) return;
786
1362
  try {
787
- const encoded = codec.encode(value);
1363
+ const body = storedText(codec, value);
1364
+ if (body === undefined) return;
788
1365
  const stamps = [...run.reads].filter(([file]) => !inJitiCache(file));
789
1366
  gatherCode = cliStates(stamps);
790
- const text = JSON.stringify({
1367
+ const {deps, own, under} = chooseBase(file, stamps, run.confirmed);
1368
+ const version = randomBytes(12).toString('hex');
1369
+ const head = JSON.stringify({
791
1370
  format: FORMAT,
792
1371
  key: entry,
793
- stamps,
1372
+ version,
1373
+ deps,
1374
+ stamps: own,
794
1375
  env: [...run.env],
795
- value: encoded,
796
1376
  });
797
- if (!isDeepStrictEqual(JSON.parse(text).value, encoded)) return;
1377
+ // JSON text holds no line break, so the first one ends the head.
1378
+ const text = `${head}\n${body}`;
798
1379
  const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
799
1380
  fs.writeFileSync(temp, text, {mode: 0o600, flag: 'wx'});
800
1381
  fs.renameSync(temp, file);
801
- evict(dir);
1382
+ run.confirmed.set(file, {
1383
+ version,
1384
+ lists: [stamps],
1385
+ under,
1386
+ deps: deps.map(([name]) => path.join(dir, name)),
1387
+ });
1388
+ markUsed(file, run.confirmed);
1389
+ evictOldest(dir, isEntry);
802
1390
  } catch {
803
1391
  // A gather that cannot be stored is gathered again next time.
804
1392
  }
805
1393
  }
806
1394
 
807
- /**
808
- * Delete the oldest entries until the folder holds at most CACHE_LIMIT_BYTES.
809
- * @param {string} dir
810
- */
811
- function evict(dir) {
812
- const entries = fs
813
- .readdirSync(dir)
814
- .filter(name => name.startsWith('gather-'))
815
- .map(name => {
816
- const entry = path.join(dir, name);
817
- try {
818
- const {size, mtimeMs} = fs.statSync(entry);
819
- return {entry, size, mtimeMs};
820
- } catch {
821
- return null;
822
- }
823
- })
824
- .filter(e => e != null)
825
- .sort((a, b) => a.mtimeMs - b.mtimeMs);
826
- let total = entries.reduce((sum, e) => sum + e.size, 0);
827
- for (const {entry, size} of entries) {
828
- if (total <= CACHE_LIMIT_BYTES) break;
829
- try {
830
- fs.unlinkSync(entry);
831
- total -= size;
832
- } catch {
833
- // Another run removed it.
834
- }
835
- }
836
- }
837
-
838
1395
  /**
839
1396
  * Whether this run is under Yarn Plug'n'Play's loader (`yarn astryx` in a PnP
840
1397
  * project): Node's module hooks hand PnP's resolver options it refuses, so
@@ -849,15 +1406,16 @@ export function runsUnderPnp() {
849
1406
  /**
850
1407
  * The cache folder and how much it holds, for `doctor`: null when the CLI
851
1408
  * keeps no gathers (a checkout) or has no usable folder.
852
- * @returns {{dir: string, bytes: number, entries: number, pnp: boolean} | null}
853
- * `pnp`: whether this run is under Yarn Plug'n'Play's loader (runsUnderPnp)
1409
+ * @returns {{dir: string, bytes: number, entries: number, pnp: boolean, limitBytes: number, jitiDir: string} | null}
1410
+ * `pnp`: whether this run is under Yarn Plug'n'Play's loader (runsUnderPnp);
1411
+ * `limitBytes`: the most the folder, and jiti's folder `jitiDir`, holds
854
1412
  */
855
1413
  export function gatherCacheSummary() {
856
1414
  // The cache's own bookkeeping, never an input of what it keeps.
857
1415
  return withoutRecording(summarize);
858
1416
  }
859
1417
 
860
- /** @returns {{dir: string, bytes: number, entries: number, pnp: boolean} | null} */
1418
+ /** @returns {{dir: string, bytes: number, entries: number, pnp: boolean, limitBytes: number, jitiDir: string} | null} */
861
1419
  function summarize() {
862
1420
  if (buildId() == null) return null;
863
1421
  const dir = gatherCacheDir();
@@ -877,5 +1435,12 @@ function summarize() {
877
1435
  } catch {
878
1436
  return null;
879
1437
  }
880
- return {dir, bytes, entries, pnp: runsUnderPnp()};
1438
+ return {
1439
+ dir,
1440
+ bytes,
1441
+ entries,
1442
+ pnp: runsUnderPnp(),
1443
+ limitBytes: CACHE_LIMIT_BYTES,
1444
+ jitiDir: jitiCacheDir(),
1445
+ };
881
1446
  }