@buffcore/cli 0.8.46 → 0.8.48

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 (271) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/dist/commands/build/server-text.en.d.ts +5 -0
  3. package/dist/commands/build/server-text.en.js +5 -0
  4. package/dist/commands/build/server-text.js +5 -0
  5. package/dist/commands/build/server.d.ts +4 -0
  6. package/dist/commands/build/server.js +10 -1
  7. package/dist/commands/change-merge-remove.d.ts +9 -2
  8. package/dist/commands/change-merge-remove.js +27 -15
  9. package/dist/commands/change-merge.d.ts +2 -2
  10. package/dist/commands/design/check.d.ts +1 -1
  11. package/dist/commands/design/check.js +2 -1
  12. package/dist/commands/design/contract-size.d.ts +7 -0
  13. package/dist/commands/design/contract-size.js +47 -0
  14. package/dist/commands/design/tells.en.d.ts +2 -0
  15. package/dist/commands/design/tells.en.js +2 -0
  16. package/dist/commands/design/tells.es.js +2 -0
  17. package/dist/commands/dev.d.ts +9 -2
  18. package/dist/commands/dev.js +24 -5
  19. package/dist/commands/extension/places-approval.d.ts +4 -0
  20. package/dist/commands/hardening.d.ts +3 -0
  21. package/dist/commands/hardening.en.d.ts +3 -0
  22. package/dist/commands/hardening.en.js +3 -0
  23. package/dist/commands/hardening.es.d.ts +2 -0
  24. package/dist/commands/hardening.es.js +3 -0
  25. package/dist/commands/hardening.js +19 -0
  26. package/dist/commands/process-paths.d.ts +14 -0
  27. package/dist/commands/process-paths.js +90 -0
  28. package/dist/commands/prototype/ai-approval.d.ts +27 -0
  29. package/dist/commands/prototype/ai-approval.js +60 -0
  30. package/dist/commands/prototype/approver.d.ts +71 -0
  31. package/dist/commands/prototype/approver.js +168 -0
  32. package/dist/commands/prototype/comp-record.d.ts +5 -0
  33. package/dist/commands/prototype/comp-record.js +22 -0
  34. package/dist/commands/prototype/critique-gate.d.ts +14 -0
  35. package/dist/commands/prototype/critique-gate.js +48 -0
  36. package/dist/commands/prototype/fence.d.ts +1 -0
  37. package/dist/commands/prototype/fence.js +12 -0
  38. package/dist/commands/prototype/final-approval-grant.js +18 -0
  39. package/dist/commands/prototype/final-approval.js +4 -1
  40. package/dist/commands/prototype/index.js +3 -0
  41. package/dist/commands/prototype/layout-hints.d.ts +34 -0
  42. package/dist/commands/prototype/layout-hints.js +194 -0
  43. package/dist/commands/prototype/layout-measure.d.ts +39 -0
  44. package/dist/commands/prototype/layout-measure.js +97 -0
  45. package/dist/commands/prototype/layout-words.d.ts +2 -0
  46. package/dist/commands/prototype/layout-words.js +11 -0
  47. package/dist/commands/prototype/layout.en.d.ts +6 -0
  48. package/dist/commands/prototype/layout.en.js +7 -0
  49. package/dist/commands/prototype/load-location.js +2 -3
  50. package/dist/commands/prototype/messages.en.d.ts +14 -0
  51. package/dist/commands/prototype/messages.en.js +15 -1
  52. package/dist/commands/prototype/messages.js +15 -1
  53. package/dist/commands/prototype/milestone-versions.d.ts +4 -0
  54. package/dist/commands/prototype/prune-command.d.ts +5 -0
  55. package/dist/commands/prototype/prune-command.js +25 -0
  56. package/dist/commands/prototype/prune-words.d.ts +1 -0
  57. package/dist/commands/prototype/prune-words.js +7 -0
  58. package/dist/commands/prototype/prune.d.ts +16 -0
  59. package/dist/commands/prototype/prune.en.d.ts +9 -0
  60. package/dist/commands/prototype/prune.en.js +9 -0
  61. package/dist/commands/prototype/prune.es.d.ts +2 -0
  62. package/dist/commands/prototype/prune.es.js +9 -0
  63. package/dist/commands/prototype/prune.js +140 -0
  64. package/dist/commands/prototype/reopen-messages.d.ts +9 -0
  65. package/dist/commands/prototype/reopen-messages.js +19 -0
  66. package/dist/commands/prototype/report.d.ts +14 -0
  67. package/dist/commands/prototype/report.js +5 -1
  68. package/dist/commands/prototype/scene-layout.d.ts +17 -0
  69. package/dist/commands/prototype/scene-layout.js +57 -0
  70. package/dist/commands/prototype/scene-ready.js +8 -2
  71. package/dist/commands/prototype/scene.d.ts +15 -2
  72. package/dist/commands/prototype/scene.js +54 -42
  73. package/dist/commands/prototype/selector-answer.d.ts +43 -0
  74. package/dist/commands/prototype/selector-answer.js +73 -0
  75. package/dist/commands/prototype/selector-misses.d.ts +19 -0
  76. package/dist/commands/prototype/selector-misses.js +78 -0
  77. package/dist/commands/prototype/selector-prompt.d.ts +23 -0
  78. package/dist/commands/prototype/selector-prompt.js +32 -0
  79. package/dist/commands/prototype/selector-run.d.ts +13 -0
  80. package/dist/commands/prototype/selector-run.js +113 -0
  81. package/dist/commands/prototype/selector.d.ts +54 -0
  82. package/dist/commands/prototype/selector.js +96 -0
  83. package/dist/commands/prototype/snapshot-ancestor.js +2 -0
  84. package/dist/commands/prototype/snapshot-bundle.d.ts +17 -3
  85. package/dist/commands/prototype/snapshot-bundle.js +69 -7
  86. package/dist/commands/prototype/snapshot-check-files.d.ts +2 -0
  87. package/dist/commands/prototype/snapshot-check-files.js +41 -0
  88. package/dist/commands/prototype/snapshot-command.d.ts +2 -0
  89. package/dist/commands/prototype/snapshot-command.js +3 -0
  90. package/dist/commands/prototype/snapshot-completion.d.ts +4 -0
  91. package/dist/commands/prototype/snapshot-completion.js +30 -2
  92. package/dist/commands/prototype/snapshot-dependencies.d.ts +23 -2
  93. package/dist/commands/prototype/snapshot-dependencies.js +120 -14
  94. package/dist/commands/prototype/snapshot-hashes.d.ts +7 -0
  95. package/dist/commands/prototype/snapshot-hashes.js +28 -0
  96. package/dist/commands/prototype/snapshot-inputs.d.ts +15 -6
  97. package/dist/commands/prototype/snapshot-inputs.js +140 -197
  98. package/dist/commands/prototype/snapshot-matrix.d.ts +58 -0
  99. package/dist/commands/prototype/snapshot-matrix.js +95 -0
  100. package/dist/commands/prototype/snapshot-metro-inputs.d.ts +8 -0
  101. package/dist/commands/prototype/snapshot-metro-inputs.js +81 -0
  102. package/dist/commands/prototype/snapshot-owners.d.ts +6 -2
  103. package/dist/commands/prototype/snapshot-owners.js +12 -3
  104. package/dist/commands/prototype/snapshot-progress-entry.d.ts +14 -0
  105. package/dist/commands/prototype/snapshot-progress-entry.js +85 -0
  106. package/dist/commands/prototype/snapshot-progress.d.ts +19 -2
  107. package/dist/commands/prototype/snapshot-progress.js +54 -102
  108. package/dist/commands/prototype/snapshot-registry.d.ts +14 -0
  109. package/dist/commands/prototype/snapshot-registry.js +121 -0
  110. package/dist/commands/prototype/snapshot-report.d.ts +4 -0
  111. package/dist/commands/prototype/snapshot-report.js +77 -12
  112. package/dist/commands/prototype/snapshot-requests.d.ts +13 -3
  113. package/dist/commands/prototype/snapshot-requests.js +68 -30
  114. package/dist/commands/prototype/snapshot-reuse.d.ts +6 -1
  115. package/dist/commands/prototype/snapshot-reuse.js +17 -7
  116. package/dist/commands/prototype/snapshot-scene-inputs.d.ts +30 -0
  117. package/dist/commands/prototype/snapshot-scene-inputs.js +210 -0
  118. package/dist/commands/prototype/snapshot-served-inputs.d.ts +3 -0
  119. package/dist/commands/prototype/snapshot-served-inputs.js +12 -0
  120. package/dist/commands/prototype/snapshot-server-record.d.ts +26 -0
  121. package/dist/commands/prototype/snapshot-server-record.js +135 -0
  122. package/dist/commands/prototype/snapshot-servers.d.ts +3 -1
  123. package/dist/commands/prototype/snapshot-servers.js +50 -2
  124. package/dist/commands/prototype/snapshot-setup.d.ts +3 -1
  125. package/dist/commands/prototype/snapshot-setup.js +2 -1
  126. package/dist/commands/prototype/snapshot-surface-inputs.d.ts +23 -0
  127. package/dist/commands/prototype/snapshot-surface-inputs.js +161 -0
  128. package/dist/commands/prototype/snapshot-vite-inputs.d.ts +22 -0
  129. package/dist/commands/prototype/snapshot-vite-inputs.js +209 -0
  130. package/dist/commands/prototype/snapshot.d.ts +5 -0
  131. package/dist/commands/prototype/snapshot.js +72 -10
  132. package/dist/commands/prototype/version-commands.d.ts +4 -0
  133. package/dist/commands/prototype/version-commands.js +36 -8
  134. package/dist/commands/prototype/versions.d.ts +28 -2
  135. package/dist/commands/prototype/versions.js +25 -1
  136. package/dist/commands/prototype/vite-fs-path.d.ts +4 -0
  137. package/dist/commands/prototype/vite-fs-path.js +15 -0
  138. package/dist/commands/remove-links.d.ts +6 -0
  139. package/dist/commands/remove-links.js +17 -0
  140. package/dist/commands/server/build.d.ts +2 -1
  141. package/dist/commands/server/build.js +10 -2
  142. package/dist/commands/server/deploy-local.d.ts +3 -0
  143. package/dist/commands/server/deploy-local.js +9 -0
  144. package/dist/commands/server/deploy-plan.d.ts +5 -0
  145. package/dist/commands/server/deploy-plan.js +56 -0
  146. package/dist/commands/server/deploy-result.d.ts +7 -0
  147. package/dist/commands/server/deploy-result.js +1 -0
  148. package/dist/commands/server/deploy-script.d.ts +2 -0
  149. package/dist/commands/server/deploy-script.js +3 -2
  150. package/dist/commands/server/deploy.d.ts +9 -6
  151. package/dist/commands/server/deploy.js +39 -61
  152. package/dist/commands/server/files.d.ts +9 -1
  153. package/dist/commands/server/files.js +25 -5
  154. package/dist/commands/server/image.d.ts +4 -2
  155. package/dist/commands/server/image.js +6 -5
  156. package/dist/commands/server/setup.js +4 -0
  157. package/dist/commands/server/system-packages.d.ts +29 -0
  158. package/dist/commands/server/system-packages.js +103 -0
  159. package/dist/commands/services/compose.d.ts +1 -1
  160. package/dist/commands/services/compose.js +5 -3
  161. package/dist/commands/size-docs.d.ts +9 -0
  162. package/dist/commands/size-docs.en.d.ts +6 -0
  163. package/dist/commands/size-docs.en.js +6 -0
  164. package/dist/commands/size-docs.es.d.ts +2 -0
  165. package/dist/commands/size-docs.es.js +6 -0
  166. package/dist/commands/size-docs.js +28 -0
  167. package/dist/commands/size.d.ts +5 -2
  168. package/dist/commands/size.js +27 -9
  169. package/dist/commands/ui/agents-route.js +2 -0
  170. package/dist/commands/ui/approval-authority.d.ts +10 -2
  171. package/dist/commands/ui/approval-authority.en.d.ts +1 -0
  172. package/dist/commands/ui/approval-authority.en.js +1 -0
  173. package/dist/commands/ui/approval-authority.es.d.ts +1 -0
  174. package/dist/commands/ui/approval-authority.es.js +1 -0
  175. package/dist/commands/ui/approval-authority.js +8 -2
  176. package/dist/commands/ui/approve.js +2 -4
  177. package/dist/commands/ui/autopilot-ai-approve.d.ts +18 -0
  178. package/dist/commands/ui/autopilot-ai-approve.js +68 -0
  179. package/dist/commands/ui/autopilot-loop.js +5 -6
  180. package/dist/commands/ui/autopilot-merge.js +3 -0
  181. package/dist/commands/ui/autopilot-records.d.ts +2 -0
  182. package/dist/commands/ui/autopilot-records.js +5 -0
  183. package/dist/commands/ui/autopilot.en.d.ts +23 -0
  184. package/dist/commands/ui/autopilot.en.js +23 -0
  185. package/dist/commands/ui/autopilot.es.d.ts +23 -0
  186. package/dist/commands/ui/autopilot.es.js +23 -0
  187. package/dist/commands/ui/brand-approval.js +5 -1
  188. package/dist/commands/ui/chat/find.d.ts +2 -0
  189. package/dist/commands/ui/chat/find.js +13 -0
  190. package/dist/commands/ui/chat/fresh-codex.d.ts +39 -0
  191. package/dist/commands/ui/chat/fresh-codex.js +175 -0
  192. package/dist/commands/ui/chat/fresh-critique.js +13 -5
  193. package/dist/commands/ui/chat/fresh-launch.d.ts +19 -0
  194. package/dist/commands/ui/chat/fresh-launch.js +67 -11
  195. package/dist/commands/ui/chat/fresh-prompts.d.ts +2 -0
  196. package/dist/commands/ui/chat/fresh-prompts.js +2 -0
  197. package/dist/commands/ui/chat/fresh-secrets.d.ts +6 -0
  198. package/dist/commands/ui/chat/fresh-secrets.js +172 -0
  199. package/dist/commands/ui/chat/fresh.en.d.ts +1 -0
  200. package/dist/commands/ui/chat/fresh.en.js +2 -1
  201. package/dist/commands/ui/chat/fresh.es.d.ts +1 -0
  202. package/dist/commands/ui/chat/fresh.es.js +2 -1
  203. package/dist/commands/ui/chat/permissions.js +1 -1
  204. package/dist/commands/ui/chat/routes.js +4 -0
  205. package/dist/commands/ui/chat/worktree.js +25 -5
  206. package/dist/commands/ui/comp-approval.d.ts +14 -2
  207. package/dist/commands/ui/comp-approval.js +45 -8
  208. package/dist/commands/ui/index.js +3 -0
  209. package/dist/commands/ui/level-route.js +3 -0
  210. package/dist/commands/ui/loop-approve.d.ts +30 -1
  211. package/dist/commands/ui/loop-approve.js +2 -2
  212. package/dist/commands/ui/merge-authority.d.ts +4 -1
  213. package/dist/commands/ui/merge-authority.en.d.ts +4 -0
  214. package/dist/commands/ui/merge-authority.en.js +5 -1
  215. package/dist/commands/ui/merge-authority.es.d.ts +4 -0
  216. package/dist/commands/ui/merge-authority.es.js +5 -1
  217. package/dist/commands/ui/merge-authority.js +6 -3
  218. package/dist/commands/ui/merge-claims.d.ts +7 -0
  219. package/dist/commands/ui/merge-claims.js +21 -0
  220. package/dist/commands/ui/merge-delegation.js +8 -3
  221. package/dist/commands/ui/merge-hold-name.d.ts +5 -0
  222. package/dist/commands/ui/merge-hold-name.js +5 -0
  223. package/dist/commands/ui/merge-hold.d.ts +6 -1
  224. package/dist/commands/ui/merge-hold.js +23 -5
  225. package/dist/commands/ui/merge-publish-host.d.ts +1 -0
  226. package/dist/commands/ui/merge-publish-host.js +2 -2
  227. package/dist/commands/ui/merge-record.d.ts +2 -0
  228. package/dist/commands/ui/merge-record.js +1 -1
  229. package/dist/commands/ui/merge-route.js +20 -1
  230. package/dist/commands/ui/merge-settle.d.ts +32 -0
  231. package/dist/commands/ui/merge-settle.js +133 -0
  232. package/dist/commands/ui/merge-state.d.ts +5 -0
  233. package/dist/commands/ui/merge-state.js +7 -1
  234. package/dist/commands/ui/merge-withdraw.d.ts +9 -0
  235. package/dist/commands/ui/merge-withdraw.js +29 -0
  236. package/dist/commands/ui/places-change.d.ts +4 -0
  237. package/dist/commands/ui/plan-files.js +5 -2
  238. package/dist/commands/ui/plan-structure.js +5 -7
  239. package/dist/commands/ui/prototype-comp-route.d.ts +12 -3
  240. package/dist/commands/ui/prototype-comp-route.js +35 -17
  241. package/dist/commands/ui/prototype-main.d.ts +4 -0
  242. package/dist/commands/ui/real-file.d.ts +11 -0
  243. package/dist/commands/ui/real-file.js +84 -0
  244. package/dist/commands/ui/reopen-route.d.ts +117 -0
  245. package/dist/commands/ui/reopen-route.js +97 -0
  246. package/dist/commands/ui/save-route.d.ts +2 -0
  247. package/dist/commands/ui/save-route.js +8 -1
  248. package/dist/commands/ui/server.d.ts +1 -0
  249. package/dist/commands/ui/server.js +1 -0
  250. package/dist/commands/ui/studio-routes.js +2 -0
  251. package/dist/commands/ui/studio-state.d.ts +34 -0
  252. package/dist/commands/ui/studio-versions.d.ts +24 -0
  253. package/dist/commands/ui/studio-versions.js +19 -3
  254. package/dist/commands/ui/worktrees.d.ts +1 -1
  255. package/dist/commands/ui/worktrees.js +19 -3
  256. package/dist/commands/worktree-sweep.d.ts +45 -0
  257. package/dist/commands/worktree-sweep.js +188 -0
  258. package/dist/commands/worktrees-command.d.ts +32 -0
  259. package/dist/commands/worktrees-command.en.d.ts +25 -0
  260. package/dist/commands/worktrees-command.en.js +55 -0
  261. package/dist/commands/worktrees-command.js +96 -0
  262. package/dist/main.js +2 -0
  263. package/dist/ui/page.js +89 -89
  264. package/docs/PROTOTYPE.md +19 -3
  265. package/docs/WORKBENCH.md +41 -6
  266. package/package.json +11 -11
  267. package/skills/ape-critique/SKILL.md +41 -6
  268. package/skills/ape-deploy/SKILL.md +4 -2
  269. package/skills/ape-design-system/SKILL.md +3 -0
  270. package/skills/ape-plan-change/SKILL.md +6 -0
  271. package/skills/ape-prototype/SKILL.md +8 -4
@@ -0,0 +1,12 @@
1
+ import { sha256 } from './snapshot-hashes.js';
2
+ export async function servedBytes(url, mime, timeout = 110_000) {
3
+ if (!['localhost', '127.0.0.1', '[::1]'].includes(url.hostname))
4
+ return;
5
+ const response = await fetch(url, { redirect: 'error', signal: AbortSignal.timeout(timeout) });
6
+ if (!response.ok || !response.headers.get('content-type')?.includes(mime))
7
+ return;
8
+ return Buffer.from(await response.arrayBuffer());
9
+ }
10
+ export async function matchesBytes(hash, response) {
11
+ return !!hash && !!response?.ok() && sha256(await response.body()) === hash;
12
+ }
@@ -0,0 +1,26 @@
1
+ export interface ServerProcess {
2
+ pid: number;
3
+ parent: number;
4
+ start: string;
5
+ command: string;
6
+ }
7
+ /** Every process now, with its start time and command line. */
8
+ export declare function processTable(): Promise<ServerProcess[]>;
9
+ /** A start in epoch milliseconds: Windows gives a FILETIME (100 ns since 1601), `ps` a date. */
10
+ export declare function startTime(start: string): number | undefined;
11
+ /** The trees below `roots`, each child started at or after its parent (see `descendants`). */
12
+ export declare function processTrees(table: readonly ServerProcess[], roots: readonly number[]): Set<number>;
13
+ /** Whether a command line holds `path` itself: a separator, a quote, a space or the end follows. */
14
+ export declare function holdsPath(command: string, path: string): boolean;
15
+ /** The servers' process trees, from their own processes down. */
16
+ export declare function serverTrees(table: readonly ServerProcess[], roots: readonly number[]): ServerProcess[];
17
+ /** The recorded processes that are still the very same ones, and this checkout's. */
18
+ export declare function reapable(recorded: readonly ServerProcess[], table: readonly ServerProcess[], checkout: string): number[];
19
+ export declare function recordServers(file: string, entries: readonly ServerProcess[]): void;
20
+ export declare function forgetServers(file: string): void;
21
+ /**
22
+ * Stop what an earlier run of this worktree left running, then forget the record. Called with the
23
+ * worktree's lock held, so the run that wrote it is over. A table that cannot be read keeps the
24
+ * record for the next run.
25
+ */
26
+ export declare function reapServers(file: string, checkout: string, table?: () => Promise<ServerProcess[]>, kill?: (pid: number) => void): Promise<number[]>;
@@ -0,0 +1,135 @@
1
+ // The dev servers a linked worktree's snapshot started, recorded beside its lock so that the next run
2
+ // of the same worktree can stop what a hard kill (a crash, a closed terminal) left running. A process
3
+ // is stopped only when its id, its start time and its whole command line all still match the record
4
+ // and that line holds this checkout's own path: an id the system gave to another process is never hit.
5
+ import { execFile } from 'node:child_process';
6
+ import { readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
7
+ import { promisify } from 'node:util';
8
+ import { descendants } from './snapshot-owners.js';
9
+ const run = promisify(execFile);
10
+ const options = {
11
+ encoding: 'utf8',
12
+ windowsHide: true,
13
+ timeout: 30_000,
14
+ maxBuffer: 64 << 20,
15
+ };
16
+ const WINDOWS = 'Get-CimInstance Win32_Process | ForEach-Object { "$($_.ProcessId)`t$($_.ParentProcessId)`t$(if ($_.CreationDate) { $_.CreationDate.ToFileTimeUtc() })`t$($_.CommandLine)" }';
17
+ const POSIX = /^\s*(\d+)\s+(\d+)\s+(\S+\s+\S+\s+\d+\s+[\d:]+\s+\d+)\s+(.*)$/;
18
+ /** Every process now, with its start time and command line. */
19
+ export async function processTable() {
20
+ if (process.platform === 'win32') {
21
+ const { stdout } = await run('powershell', ['-NoProfile', '-Command', WINDOWS], options);
22
+ return stdout.split(/\r?\n/).flatMap((line) => {
23
+ const [pid, parent, start = '', ...command] = line.split('\t');
24
+ return pid && parent
25
+ ? [{ pid: Number(pid), parent: Number(parent), start, command: command.join('\t') }]
26
+ : [];
27
+ });
28
+ }
29
+ const { stdout } = await run('ps', ['-A', '-o', 'pid=,ppid=,lstart=,args='], {
30
+ ...options,
31
+ env: { ...process.env, LC_ALL: 'C' },
32
+ });
33
+ return stdout.split('\n').flatMap((line) => {
34
+ const cells = POSIX.exec(line);
35
+ return cells
36
+ ? [
37
+ {
38
+ pid: Number(cells[1]),
39
+ parent: Number(cells[2]),
40
+ start: cells[3],
41
+ command: cells[4],
42
+ },
43
+ ]
44
+ : [];
45
+ });
46
+ }
47
+ /** A start in epoch milliseconds: Windows gives a FILETIME (100 ns since 1601), `ps` a date. */
48
+ export function startTime(start) {
49
+ const time = /^\d+$/.test(start)
50
+ ? Number(start) / 10_000 - 11_644_473_600_000
51
+ : Date.parse(start);
52
+ return Number.isFinite(time) ? time : undefined;
53
+ }
54
+ /** The trees below `roots`, each child started at or after its parent (see `descendants`). */
55
+ export function processTrees(table, roots) {
56
+ const pairs = table.map(({ pid, parent }) => `${pid} ${parent}`).join('\n');
57
+ const starts = new Map();
58
+ for (const { pid, start } of table) {
59
+ const time = startTime(start);
60
+ if (time !== undefined)
61
+ starts.set(pid, time);
62
+ }
63
+ return new Set(roots.flatMap((root) => [...descendants(pairs, root, starts)]));
64
+ }
65
+ /** Whether a command line holds `path` itself: a separator, a quote, a space or the end follows. */
66
+ export function holdsPath(command, path) {
67
+ for (const form of new Set([path, path.replaceAll('\\', '/')]))
68
+ for (let at = command.indexOf(form); at >= 0; at = command.indexOf(form, at + 1))
69
+ if (/^($|[\\/"' ])/.test(command.slice(at + form.length)))
70
+ return true;
71
+ return false;
72
+ }
73
+ /** The servers' process trees, from their own processes down. */
74
+ export function serverTrees(table, roots) {
75
+ const tree = processTrees(table, roots);
76
+ return table.filter((entry) => tree.has(entry.pid) && entry.start !== '');
77
+ }
78
+ /** The recorded processes that are still the very same ones, and this checkout's. */
79
+ export function reapable(recorded, table, checkout) {
80
+ const now = new Map(table.map((entry) => [entry.pid, entry]));
81
+ return recorded
82
+ .filter((entry) => {
83
+ const current = now.get(entry.pid);
84
+ return (!!current &&
85
+ entry.start !== '' &&
86
+ current.start === entry.start &&
87
+ current.command === entry.command &&
88
+ holdsPath(entry.command, checkout));
89
+ })
90
+ .map((entry) => entry.pid);
91
+ }
92
+ export function recordServers(file, entries) {
93
+ writeFileSync(`${file}.tmp`, JSON.stringify(entries));
94
+ renameSync(`${file}.tmp`, file);
95
+ }
96
+ export function forgetServers(file) {
97
+ rmSync(file, { force: true });
98
+ }
99
+ function recorded(file) {
100
+ try {
101
+ const value = JSON.parse(readFileSync(file, 'utf8'));
102
+ if (!Array.isArray(value))
103
+ return [];
104
+ return value.filter((entry) => Number.isSafeInteger(entry?.pid) &&
105
+ entry.pid > 0 &&
106
+ typeof entry.start === 'string' &&
107
+ typeof entry.command === 'string');
108
+ }
109
+ catch (error) {
110
+ return error.code === 'ENOENT' ? undefined : [];
111
+ }
112
+ }
113
+ /**
114
+ * Stop what an earlier run of this worktree left running, then forget the record. Called with the
115
+ * worktree's lock held, so the run that wrote it is over. A table that cannot be read keeps the
116
+ * record for the next run.
117
+ */
118
+ export async function reapServers(file, checkout, table = processTable, kill = (pid) => process.kill(pid, 'SIGKILL')) {
119
+ const entries = recorded(file);
120
+ if (entries === undefined)
121
+ return [];
122
+ const now = await table().catch(() => undefined);
123
+ if (!now)
124
+ return [];
125
+ const pids = reapable(entries, now, checkout);
126
+ for (const pid of pids)
127
+ try {
128
+ kill(pid);
129
+ }
130
+ catch {
131
+ // Gone between the lookup and the kill.
132
+ }
133
+ forgetServers(file);
134
+ return pids;
135
+ }
@@ -14,6 +14,8 @@ export interface Dependencies {
14
14
  owners?: Owners;
15
15
  listen?: Listen;
16
16
  readyMs?: number;
17
+ /** Where the servers' processes are recorded; by default beside the worktree's snapshot lock. */
18
+ record?: string | undefined;
17
19
  }
18
20
  /** What the command asks of a run's servers. */
19
21
  export interface SnapshotServers {
@@ -28,5 +30,5 @@ export interface SnapshotServers {
28
30
  exit: NonNullable<Run['serverExit']>;
29
31
  stop(): Promise<void>;
30
32
  }
31
- export declare function snapshotServers(root: string, environment: SnapshotEnvironment, urls: Run['urls'], { owners, listen, readyMs }?: Dependencies): SnapshotServers;
33
+ export declare function snapshotServers(root: string, environment: SnapshotEnvironment, urls: Run['urls'], { owners, listen, readyMs, record, }?: Dependencies): SnapshotServers;
32
34
  export {};
@@ -14,8 +14,12 @@ import { t } from './messages.js';
14
14
  import { portOwner, systemOwners } from './snapshot-owners.js';
15
15
  import { serverDown } from './snapshot-recovery.js';
16
16
  import { SnapshotAccessRefused, SnapshotRefused } from './snapshot-refused.js';
17
+ import { forgetServers, processTable, reapServers, recordServers, serverTrees, } from './snapshot-server-record.js';
18
+ import { gitFolders } from './snapshot-slots.js';
17
19
  /** A cold Metro start is the slow one. */
18
20
  const READY_MS = 180_000;
21
+ /** How often the servers' trees are recorded again while they start. */
22
+ const STARTING_MS = 2_000;
19
23
  const LOOPBACK = '127.0.0.1';
20
24
  const portFailure = (error) => accessDenied(error)
21
25
  ? new SnapshotAccessRefused('prototype.portAccess')
@@ -72,7 +76,31 @@ function installFiles(root) {
72
76
  }
73
77
  }
74
78
  const bytes = (files) => files.map((file) => readFileSync(file).toString('base64'));
75
- export function snapshotServers(root, environment, urls, { owners = systemOwners, listen, readyMs = READY_MS } = {}) {
79
+ /** The record beside the worktree's lock, or none outside Git. */
80
+ function recordFile(root) {
81
+ try {
82
+ return join(gitFolders(root).worktree, 'ape-snapshot-servers.json');
83
+ }
84
+ catch {
85
+ return undefined;
86
+ }
87
+ }
88
+ export function snapshotServers(root, environment, urls, { owners = systemOwners, listen, readyMs = READY_MS, record = recordFile(root), } = {}) {
89
+ let beat;
90
+ // Each server's whole tree, written again with the heartbeat: a hard kill cannot stop them.
91
+ // One write at a time, in order: an older process table never replaces a newer one.
92
+ let recording = Promise.resolve();
93
+ let stopped = false;
94
+ const write = async () => {
95
+ if (!record || stopped)
96
+ return;
97
+ const roots = [...children.values()].flatMap((child) => (child.pid ? [child.pid] : []));
98
+ const table = await processTable().catch(() => undefined);
99
+ // Once stopped, the record is forgotten and stays so.
100
+ if (table && !stopped)
101
+ recordServers(record, serverTrees(table, roots));
102
+ };
103
+ const remember = () => (recording = recording.then(write, write));
76
104
  const children = new Map();
77
105
  const exits = new Map();
78
106
  let api;
@@ -90,6 +118,9 @@ export function snapshotServers(root, environment, urls, { owners = systemOwners
90
118
  return serverDown(scene, server, { root, serverExit: (name) => exits.get(name) });
91
119
  };
92
120
  async function begin() {
121
+ // The worktree's lock is held: what an earlier run recorded is left over from a hard kill.
122
+ if (record)
123
+ await reapServers(record, root).catch(() => { });
93
124
  installFilesRead = installFiles(root);
94
125
  installed = bytes(installFilesRead);
95
126
  api = await standIn();
@@ -170,7 +201,20 @@ export function snapshotServers(root, environment, urls, { owners = systemOwners
170
201
  const need = SURFACES.filter((surface) => !children.has(surface) && uiFolder(root, surface));
171
202
  for (const surface of need)
172
203
  launch(surface);
173
- await Promise.all(need.map(ready));
204
+ // Recorded at once and every few seconds while they start (a kill during a cold start leaves
205
+ // them too; a shell's children may not exist yet at once), then again when ready.
206
+ await remember().catch(() => { });
207
+ beat ??= setInterval(() => void remember().catch(() => { }), 60_000);
208
+ beat.unref();
209
+ const starting = setInterval(() => void remember().catch(() => { }), STARTING_MS);
210
+ starting.unref();
211
+ try {
212
+ await Promise.all(need.map(ready));
213
+ }
214
+ finally {
215
+ clearInterval(starting);
216
+ }
217
+ await remember().catch(() => { });
174
218
  },
175
219
  check() {
176
220
  for (const server of children.keys())
@@ -188,9 +232,13 @@ export function snapshotServers(root, environment, urls, { owners = systemOwners
188
232
  kill,
189
233
  exit: (server) => exits.get(server),
190
234
  async stop() {
235
+ stopped = true;
191
236
  process.removeListener('exit', kill);
237
+ clearInterval(beat);
192
238
  for (const child of children.values())
193
239
  stopTree(child, process.platform !== 'win32');
240
+ if (record)
241
+ forgetServers(record);
194
242
  await api?.close();
195
243
  },
196
244
  };
@@ -16,6 +16,8 @@ export interface SetupFlags {
16
16
  sceneTimeout?: number;
17
17
  remake?: boolean;
18
18
  pages?: number;
19
+ /** `--full`: the full matrix. */
20
+ full?: boolean;
19
21
  }
20
22
  export interface Setup {
21
23
  root: string;
@@ -30,7 +32,7 @@ export interface Setup {
30
32
  progress: SnapshotProgressSource | undefined;
31
33
  urls: SnapshotOptions['urls'];
32
34
  dir: string;
33
- options: Pick<SnapshotOptions, 'scenes' | 'locales' | 'frames' | 'urls' | 'dir' | 'scene' | 'sceneTimeout' | 'root' | 'pages' | 'ownScenes'>;
35
+ options: Pick<SnapshotOptions, 'scenes' | 'locales' | 'frames' | 'urls' | 'dir' | 'scene' | 'sceneTimeout' | 'root' | 'pages' | 'full' | 'ownScenes'>;
34
36
  seconds(): number;
35
37
  }
36
38
  export type Servers = {
@@ -73,7 +73,7 @@ export async function setup(flags, servers) {
73
73
  const checkout = flags.remake ? undefined : pinSnapshotCheckout(root);
74
74
  const [product, all] = await Promise.all([loadProduct(root), loadScenes(root)]);
75
75
  const { comp, scenes } = runScenes(root, all, scene);
76
- const progress = scene === undefined && !comp ? checkout?.progress() : undefined;
76
+ const progress = comp ? undefined : checkout?.progress();
77
77
  // A linked worktree's addresses are the run's own: they are filled in when its servers start.
78
78
  const urls = own
79
79
  ? {}
@@ -120,6 +120,7 @@ export async function setup(flags, servers) {
120
120
  sceneTimeout,
121
121
  root,
122
122
  pages: flags.pages,
123
+ full: flags.full,
123
124
  ownScenes,
124
125
  },
125
126
  seconds: () => Math.round((performance.now() - started) / 1000),
@@ -0,0 +1,23 @@
1
+ import type { Surface } from '@buffcore/config';
2
+ import type { SurfaceEntry } from './snapshot-bundle.ts';
3
+ import type { snapshotDependencies } from './snapshot-dependencies.ts';
4
+ type Dependencies = ReturnType<typeof snapshotDependencies>;
5
+ /** A product inside a workspace uses the enclosing lockfile when it has none of its own. */
6
+ export declare function lockfile(root: string): string[];
7
+ export declare const publicEnvironment: (env: Record<string, string | undefined>) => [string, string | undefined][];
8
+ /** The design font a console request under its `_fonts/` path names, when the scene knows it. */
9
+ export declare function designFont(root: string, base: string, url: URL, assets: ReadonlySet<string>): string | undefined;
10
+ export declare function surfaceEntry(root: string, surface: Surface, dependencies: Dependencies): Promise<{
11
+ entry: SurfaceEntry;
12
+ settings: (string[][] | undefined)[];
13
+ assets: string[];
14
+ vite: {
15
+ runtime: Set<string>;
16
+ settings: string[][];
17
+ files: string[];
18
+ } | undefined;
19
+ metro: boolean;
20
+ document: string | undefined;
21
+ } | undefined>;
22
+ export declare function sharedSettings(root: string, dependencies: ReturnType<typeof snapshotDependencies>): Promise<[string[], string[][]]>;
23
+ export {};
@@ -0,0 +1,161 @@
1
+ import { existsSync, readdirSync } from 'node:fs';
2
+ import { basename, dirname, join, relative } from 'node:path';
3
+ import { parseEnv } from 'node:util';
4
+ import { surfaceDir } from './export.js';
5
+ import { isStock } from './milestone-stock.js';
6
+ import { sceneCheckFiles } from './snapshot-check-files.js';
7
+ import { sha256 } from './snapshot-hashes.js';
8
+ import { expoRoutes, snapshotFiles } from './snapshot-report.js';
9
+ import { pageInputs, viteFiles } from './snapshot-vite-inputs.js';
10
+ const SURFACE_ROOTS = {
11
+ admin: ['src/main.tsx', 'src/prototype/index.tsx', 'src/setup.ts'],
12
+ app: [
13
+ 'src/app/_scene.tsx',
14
+ 'src/app/_scene.web.tsx',
15
+ 'src/app/_layout.tsx',
16
+ 'src/app/_layout.web.tsx',
17
+ 'src/prototype/scene.tsx',
18
+ 'src/fonts.ts',
19
+ 'src/themes.ts',
20
+ ],
21
+ };
22
+ const SHARED_ROOTS = [
23
+ 'design/theme.ts',
24
+ 'prototype/setup.ts',
25
+ 'prototype/store.ts',
26
+ 'prototype/store.tsx',
27
+ ];
28
+ const SETTINGS = [
29
+ 'package.json',
30
+ 'tsconfig.json',
31
+ 'jsconfig.json',
32
+ 'app.json',
33
+ 'index.html',
34
+ '.env',
35
+ '.env.local',
36
+ '.env.development',
37
+ '.env.development.local',
38
+ ];
39
+ const existing = (dir, paths) => paths.map((path) => join(dir, path)).filter(existsSync);
40
+ const TOOLCHAINS = ['vite.config', 'metro.config', 'babel.config', 'postcss.config', 'app.config']
41
+ .flatMap((name) => ['ts', 'js', 'mts', 'mjs', 'cts', 'cjs'].map((ext) => `${name}.${ext}`))
42
+ .concat(['.babelrc', '.babelrc.json', '.postcssrc', '.postcssrc.json']);
43
+ // The in-repository dev configs use the same resolvers as the product templates.
44
+ const BUNDLED_TOOLCHAINS = {
45
+ 'apps/admin/vite.config.ts': '0fe0ec6d310d92e37ab052f230ecb6b9ff7a19241061300eebf85920018a6638',
46
+ 'apps/app/metro.config.js': 'd2261b4852313fdf42fa63d97d162c691cc89757df4de8ecdff1d8bac939b117',
47
+ 'apps/app/babel.config.js': '51ba161016afa8ae07b41bb4305f1c6e71ff30d007e58fbebe0cf383bbce67c0',
48
+ };
49
+ /** esbuild can model the shipped surface configuration. A product loader may resolve differently. */
50
+ async function toolchains(dir, surface, read) {
51
+ try {
52
+ const files = existing(dir, TOOLCHAINS);
53
+ for (const file of files) {
54
+ const name = `apps/${surface}/${relative(dir, file)}`;
55
+ const text = (await read(file)).toString('utf8').replaceAll('\r\n', '\n');
56
+ if (!isStock(name, text) && BUNDLED_TOOLCHAINS[name] !== sha256(text))
57
+ return;
58
+ }
59
+ return files;
60
+ }
61
+ catch {
62
+ return;
63
+ }
64
+ }
65
+ /** A product inside a workspace uses the enclosing lockfile when it has none of its own. */
66
+ export function lockfile(root) {
67
+ for (let dir = root;; dir = dirname(dir)) {
68
+ const files = existing(dir, ['pnpm-lock.yaml', 'package-lock.json', 'yarn.lock']);
69
+ if (files.length || dirname(dir) === dir)
70
+ return files;
71
+ }
72
+ }
73
+ export const publicEnvironment = (env) => Object.entries(env)
74
+ .filter(([key]) => /^(VITE_|EXPO_PUBLIC_)/.test(key))
75
+ .sort();
76
+ async function settingHashes(root, files, dependencies) {
77
+ return Promise.all(files.map(async (file) => {
78
+ const name = relative(root, file);
79
+ if (!/^\.env(?:\.|$)/.test(basename(file)))
80
+ return [name, await dependencies.digest(file)];
81
+ await dependencies.digest(file);
82
+ const environment = publicEnvironment(parseEnv((await dependencies.read(file)).toString('utf8')));
83
+ if (environment.some(([, value]) => value?.includes('$')))
84
+ throw new Error();
85
+ return [name, sha256(JSON.stringify(environment))];
86
+ }));
87
+ }
88
+ /** The design system's font files the console serves under `_fonts/`, as its Vite plugin lists them. */
89
+ function designFonts(root) {
90
+ const dir = join(root, 'design', 'fonts');
91
+ try {
92
+ return readdirSync(dir, { withFileTypes: true })
93
+ .filter((entry) => entry.isFile() && /\.(?:woff2?|ttf|otf)$/i.test(entry.name))
94
+ .map((entry) => join(dir, entry.name))
95
+ .sort();
96
+ }
97
+ catch {
98
+ return [];
99
+ }
100
+ }
101
+ /** The design font a console request under its `_fonts/` path names, when the scene knows it. */
102
+ export function designFont(root, base, url, assets) {
103
+ const path = decodeURIComponent(url.pathname);
104
+ if (url.search || !path.startsWith(base))
105
+ return;
106
+ const file = join(root, 'design', 'fonts', path.slice(base.length));
107
+ return assets.has(file) ? file : undefined;
108
+ }
109
+ export async function surfaceEntry(root, surface, dependencies) {
110
+ const dir = surfaceDir(root, surface);
111
+ const registry = dir && join(dir, 'src', 'prototype', 'views.ts');
112
+ if (!dir || !registry || !existsSync(registry))
113
+ return;
114
+ const configuration = await toolchains(dir, surface, dependencies.read);
115
+ if (!configuration)
116
+ return;
117
+ const page = await pageInputs(dir, dependencies.read).catch(() => undefined);
118
+ const routes = surface === 'app' ? await expoRoutes(dir).catch(() => undefined) : [];
119
+ const publicFiles = await snapshotFiles(join(dir, 'public')).catch(() => undefined);
120
+ if (!page || !routes || !publicFiles)
121
+ return;
122
+ const fonts = surface === 'admin' ? designFonts(root) : [];
123
+ const assets = [...new Set([...page.assets, ...publicFiles, ...fonts])];
124
+ const entry = {
125
+ surface,
126
+ dir,
127
+ registry,
128
+ routes,
129
+ roots: [
130
+ ...new Set([
131
+ ...existing(dir, SURFACE_ROOTS[surface]),
132
+ ...existing(root, SHARED_ROOTS),
133
+ ...page.roots,
134
+ ]),
135
+ ],
136
+ };
137
+ if (assets.some((file) => /^\.env(?:\.|$)/.test(basename(file))))
138
+ return;
139
+ const settings = await settingHashes(root, [...existing(dir, SETTINGS), ...configuration, ...assets], dependencies).catch(() => undefined);
140
+ const vite = surface === 'admin' && configuration.some((file) => basename(file) === 'vite.config.ts')
141
+ ? await viteFiles(dir, dependencies).catch(() => undefined)
142
+ : undefined;
143
+ const metro = surface === 'app' &&
144
+ configuration.some((file) => /^metro\.config\./.test(basename(file))) &&
145
+ existsSync(join(dir, 'index.ts'));
146
+ if (settings) {
147
+ const index = join(dir, 'index.html');
148
+ const document = existsSync(index) ? await dependencies.digest(index) : undefined;
149
+ return { entry, settings: [settings, vite?.settings], assets, vite, metro, document };
150
+ }
151
+ }
152
+ export async function sharedSettings(root, dependencies) {
153
+ // The root `.env` is not a file input: `loadEnv` puts its values into the environment the
154
+ // fingerprint reads, so main (which has one) and a worktree (which has none) agree.
155
+ const settings = SETTINGS.filter((name) => !name.startsWith('.env'));
156
+ const files = [...existing(root, [...settings, 'buffcore.config.ts']), ...lockfile(root)];
157
+ return Promise.all([
158
+ Promise.all(sceneCheckFiles().map(dependencies.digest)),
159
+ settingHashes(root, files, dependencies),
160
+ ]);
161
+ }
@@ -0,0 +1,22 @@
1
+ import type { Response } from 'playwright-core';
2
+ import { type snapshotDependencies } from './snapshot-dependencies.ts';
3
+ type Dependencies = ReturnType<typeof snapshotDependencies>;
4
+ export declare function pageInputs(dir: string, read: Dependencies['read']): Promise<{
5
+ assets: string[];
6
+ roots: string[];
7
+ }>;
8
+ export declare function surfaceFile(dir: string, url: URL, assets: ReadonlySet<string>): string | undefined;
9
+ export declare function viteFiles(dir: string, deps: Dependencies): Promise<{
10
+ runtime: Set<string>;
11
+ settings: string[][];
12
+ files: string[];
13
+ }>;
14
+ /** Bind the scripts that Vite actually serves, including the bytes of its generated output. */
15
+ export declare function viteRequests(dir: string, runtime: ReadonlySet<string>, modules: ReadonlySet<string>, assets: ReadonlySet<string>, runtimeFiles?: readonly string[], page?: URL): {
16
+ prepare(): Promise<void>;
17
+ proof: () => string | undefined;
18
+ same(): Promise<boolean>;
19
+ hash(force?: boolean): string | undefined;
20
+ tracks(url: URL, kind: string | undefined, response?: Response): boolean | Promise<boolean>;
21
+ };
22
+ export {};