@buffcore/cli 0.8.46 → 0.8.47

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 (187) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/dist/commands/change-merge-remove.d.ts +9 -2
  3. package/dist/commands/change-merge-remove.js +27 -15
  4. package/dist/commands/design/check.d.ts +1 -1
  5. package/dist/commands/design/check.js +2 -1
  6. package/dist/commands/design/contract-size.d.ts +7 -0
  7. package/dist/commands/design/contract-size.js +47 -0
  8. package/dist/commands/design/tells.en.d.ts +2 -0
  9. package/dist/commands/design/tells.en.js +2 -0
  10. package/dist/commands/design/tells.es.js +2 -0
  11. package/dist/commands/extension/places-approval.d.ts +4 -0
  12. package/dist/commands/process-paths.d.ts +14 -0
  13. package/dist/commands/process-paths.js +90 -0
  14. package/dist/commands/prototype/ai-approval.d.ts +27 -0
  15. package/dist/commands/prototype/ai-approval.js +60 -0
  16. package/dist/commands/prototype/approver.d.ts +71 -0
  17. package/dist/commands/prototype/approver.js +168 -0
  18. package/dist/commands/prototype/critique-gate.d.ts +14 -0
  19. package/dist/commands/prototype/critique-gate.js +48 -0
  20. package/dist/commands/prototype/fence.d.ts +1 -0
  21. package/dist/commands/prototype/fence.js +12 -0
  22. package/dist/commands/prototype/final-approval-grant.js +18 -0
  23. package/dist/commands/prototype/final-approval.js +4 -1
  24. package/dist/commands/prototype/index.js +3 -0
  25. package/dist/commands/prototype/layout-hints.d.ts +34 -0
  26. package/dist/commands/prototype/layout-hints.js +194 -0
  27. package/dist/commands/prototype/layout-measure.d.ts +39 -0
  28. package/dist/commands/prototype/layout-measure.js +97 -0
  29. package/dist/commands/prototype/layout-words.d.ts +2 -0
  30. package/dist/commands/prototype/layout-words.js +11 -0
  31. package/dist/commands/prototype/layout.en.d.ts +6 -0
  32. package/dist/commands/prototype/layout.en.js +7 -0
  33. package/dist/commands/prototype/load-location.js +2 -3
  34. package/dist/commands/prototype/messages.en.d.ts +14 -0
  35. package/dist/commands/prototype/messages.en.js +15 -1
  36. package/dist/commands/prototype/messages.js +15 -1
  37. package/dist/commands/prototype/milestone-versions.d.ts +4 -0
  38. package/dist/commands/prototype/prune-command.d.ts +5 -0
  39. package/dist/commands/prototype/prune-command.js +25 -0
  40. package/dist/commands/prototype/prune-words.d.ts +1 -0
  41. package/dist/commands/prototype/prune-words.js +7 -0
  42. package/dist/commands/prototype/prune.d.ts +16 -0
  43. package/dist/commands/prototype/prune.en.d.ts +9 -0
  44. package/dist/commands/prototype/prune.en.js +9 -0
  45. package/dist/commands/prototype/prune.es.d.ts +2 -0
  46. package/dist/commands/prototype/prune.es.js +9 -0
  47. package/dist/commands/prototype/prune.js +140 -0
  48. package/dist/commands/prototype/reopen-messages.d.ts +9 -0
  49. package/dist/commands/prototype/reopen-messages.js +19 -0
  50. package/dist/commands/prototype/report.d.ts +14 -0
  51. package/dist/commands/prototype/report.js +5 -1
  52. package/dist/commands/prototype/scene-layout.d.ts +17 -0
  53. package/dist/commands/prototype/scene-layout.js +57 -0
  54. package/dist/commands/prototype/scene-ready.js +8 -2
  55. package/dist/commands/prototype/scene.d.ts +15 -2
  56. package/dist/commands/prototype/scene.js +54 -42
  57. package/dist/commands/prototype/selector-answer.d.ts +43 -0
  58. package/dist/commands/prototype/selector-answer.js +73 -0
  59. package/dist/commands/prototype/selector-misses.d.ts +19 -0
  60. package/dist/commands/prototype/selector-misses.js +78 -0
  61. package/dist/commands/prototype/selector-prompt.d.ts +23 -0
  62. package/dist/commands/prototype/selector-prompt.js +32 -0
  63. package/dist/commands/prototype/selector-run.d.ts +13 -0
  64. package/dist/commands/prototype/selector-run.js +113 -0
  65. package/dist/commands/prototype/selector.d.ts +54 -0
  66. package/dist/commands/prototype/selector.js +96 -0
  67. package/dist/commands/prototype/snapshot-ancestor.js +2 -0
  68. package/dist/commands/prototype/snapshot-bundle.d.ts +17 -3
  69. package/dist/commands/prototype/snapshot-bundle.js +69 -7
  70. package/dist/commands/prototype/snapshot-check-files.d.ts +2 -0
  71. package/dist/commands/prototype/snapshot-check-files.js +41 -0
  72. package/dist/commands/prototype/snapshot-command.d.ts +2 -0
  73. package/dist/commands/prototype/snapshot-command.js +3 -0
  74. package/dist/commands/prototype/snapshot-completion.d.ts +4 -0
  75. package/dist/commands/prototype/snapshot-completion.js +30 -2
  76. package/dist/commands/prototype/snapshot-dependencies.d.ts +23 -2
  77. package/dist/commands/prototype/snapshot-dependencies.js +120 -14
  78. package/dist/commands/prototype/snapshot-hashes.d.ts +7 -0
  79. package/dist/commands/prototype/snapshot-hashes.js +28 -0
  80. package/dist/commands/prototype/snapshot-inputs.d.ts +15 -6
  81. package/dist/commands/prototype/snapshot-inputs.js +140 -197
  82. package/dist/commands/prototype/snapshot-matrix.d.ts +58 -0
  83. package/dist/commands/prototype/snapshot-matrix.js +95 -0
  84. package/dist/commands/prototype/snapshot-metro-inputs.d.ts +8 -0
  85. package/dist/commands/prototype/snapshot-metro-inputs.js +81 -0
  86. package/dist/commands/prototype/snapshot-owners.d.ts +6 -2
  87. package/dist/commands/prototype/snapshot-owners.js +12 -3
  88. package/dist/commands/prototype/snapshot-progress-entry.d.ts +14 -0
  89. package/dist/commands/prototype/snapshot-progress-entry.js +85 -0
  90. package/dist/commands/prototype/snapshot-progress.d.ts +19 -2
  91. package/dist/commands/prototype/snapshot-progress.js +54 -102
  92. package/dist/commands/prototype/snapshot-registry.d.ts +14 -0
  93. package/dist/commands/prototype/snapshot-registry.js +121 -0
  94. package/dist/commands/prototype/snapshot-report.d.ts +4 -0
  95. package/dist/commands/prototype/snapshot-report.js +77 -12
  96. package/dist/commands/prototype/snapshot-requests.d.ts +13 -3
  97. package/dist/commands/prototype/snapshot-requests.js +68 -30
  98. package/dist/commands/prototype/snapshot-reuse.d.ts +6 -1
  99. package/dist/commands/prototype/snapshot-reuse.js +17 -7
  100. package/dist/commands/prototype/snapshot-scene-inputs.d.ts +30 -0
  101. package/dist/commands/prototype/snapshot-scene-inputs.js +210 -0
  102. package/dist/commands/prototype/snapshot-served-inputs.d.ts +3 -0
  103. package/dist/commands/prototype/snapshot-served-inputs.js +12 -0
  104. package/dist/commands/prototype/snapshot-server-record.d.ts +26 -0
  105. package/dist/commands/prototype/snapshot-server-record.js +135 -0
  106. package/dist/commands/prototype/snapshot-servers.d.ts +3 -1
  107. package/dist/commands/prototype/snapshot-servers.js +50 -2
  108. package/dist/commands/prototype/snapshot-setup.d.ts +3 -1
  109. package/dist/commands/prototype/snapshot-setup.js +2 -1
  110. package/dist/commands/prototype/snapshot-surface-inputs.d.ts +23 -0
  111. package/dist/commands/prototype/snapshot-surface-inputs.js +161 -0
  112. package/dist/commands/prototype/snapshot-vite-inputs.d.ts +22 -0
  113. package/dist/commands/prototype/snapshot-vite-inputs.js +209 -0
  114. package/dist/commands/prototype/snapshot.d.ts +5 -0
  115. package/dist/commands/prototype/snapshot.js +72 -10
  116. package/dist/commands/prototype/version-commands.d.ts +4 -0
  117. package/dist/commands/prototype/version-commands.js +36 -8
  118. package/dist/commands/prototype/versions.d.ts +28 -2
  119. package/dist/commands/prototype/versions.js +25 -1
  120. package/dist/commands/prototype/vite-fs-path.d.ts +4 -0
  121. package/dist/commands/prototype/vite-fs-path.js +15 -0
  122. package/dist/commands/remove-links.d.ts +6 -0
  123. package/dist/commands/remove-links.js +17 -0
  124. package/dist/commands/size-docs.d.ts +9 -0
  125. package/dist/commands/size-docs.en.d.ts +6 -0
  126. package/dist/commands/size-docs.en.js +6 -0
  127. package/dist/commands/size-docs.es.d.ts +2 -0
  128. package/dist/commands/size-docs.es.js +6 -0
  129. package/dist/commands/size-docs.js +28 -0
  130. package/dist/commands/size.d.ts +5 -2
  131. package/dist/commands/size.js +27 -9
  132. package/dist/commands/ui/approval-authority.d.ts +10 -2
  133. package/dist/commands/ui/approval-authority.js +1 -0
  134. package/dist/commands/ui/autopilot-ai-approve.d.ts +18 -0
  135. package/dist/commands/ui/autopilot-ai-approve.js +68 -0
  136. package/dist/commands/ui/autopilot-loop.js +5 -6
  137. package/dist/commands/ui/autopilot-merge.js +3 -0
  138. package/dist/commands/ui/autopilot.en.d.ts +23 -0
  139. package/dist/commands/ui/autopilot.en.js +23 -0
  140. package/dist/commands/ui/autopilot.es.d.ts +23 -0
  141. package/dist/commands/ui/autopilot.es.js +23 -0
  142. package/dist/commands/ui/chat/find.d.ts +2 -0
  143. package/dist/commands/ui/chat/find.js +13 -0
  144. package/dist/commands/ui/chat/fresh-codex.d.ts +39 -0
  145. package/dist/commands/ui/chat/fresh-codex.js +175 -0
  146. package/dist/commands/ui/chat/fresh-critique.js +13 -5
  147. package/dist/commands/ui/chat/fresh-launch.d.ts +19 -0
  148. package/dist/commands/ui/chat/fresh-launch.js +67 -11
  149. package/dist/commands/ui/chat/fresh-prompts.d.ts +2 -0
  150. package/dist/commands/ui/chat/fresh-prompts.js +2 -0
  151. package/dist/commands/ui/chat/fresh-secrets.d.ts +6 -0
  152. package/dist/commands/ui/chat/fresh-secrets.js +172 -0
  153. package/dist/commands/ui/chat/fresh.en.d.ts +1 -0
  154. package/dist/commands/ui/chat/fresh.en.js +2 -1
  155. package/dist/commands/ui/chat/fresh.es.d.ts +1 -0
  156. package/dist/commands/ui/chat/fresh.es.js +2 -1
  157. package/dist/commands/ui/chat/worktree.js +25 -5
  158. package/dist/commands/ui/index.js +3 -0
  159. package/dist/commands/ui/loop-approve.d.ts +30 -1
  160. package/dist/commands/ui/loop-approve.js +2 -2
  161. package/dist/commands/ui/merge-route.js +3 -0
  162. package/dist/commands/ui/places-change.d.ts +4 -0
  163. package/dist/commands/ui/prototype-main.d.ts +4 -0
  164. package/dist/commands/ui/reopen-route.d.ts +117 -0
  165. package/dist/commands/ui/reopen-route.js +97 -0
  166. package/dist/commands/ui/save-route.d.ts +2 -0
  167. package/dist/commands/ui/save-route.js +4 -0
  168. package/dist/commands/ui/studio-routes.js +2 -0
  169. package/dist/commands/ui/studio-state.d.ts +33 -0
  170. package/dist/commands/ui/studio-versions.d.ts +24 -0
  171. package/dist/commands/ui/worktrees.d.ts +1 -1
  172. package/dist/commands/ui/worktrees.js +19 -3
  173. package/dist/commands/worktree-sweep.d.ts +45 -0
  174. package/dist/commands/worktree-sweep.js +188 -0
  175. package/dist/commands/worktrees-command.d.ts +32 -0
  176. package/dist/commands/worktrees-command.en.d.ts +25 -0
  177. package/dist/commands/worktrees-command.en.js +55 -0
  178. package/dist/commands/worktrees-command.js +96 -0
  179. package/dist/main.js +2 -0
  180. package/dist/ui/page.js +82 -82
  181. package/docs/PROTOTYPE.md +6 -3
  182. package/docs/WORKBENCH.md +11 -6
  183. package/package.json +11 -11
  184. package/skills/ape-critique/SKILL.md +41 -6
  185. package/skills/ape-design-system/SKILL.md +3 -0
  186. package/skills/ape-plan-change/SKILL.md +6 -0
  187. package/skills/ape-prototype/SKILL.md +8 -4
@@ -0,0 +1,58 @@
1
+ import type { Theme } from '@buffcore/tokens';
2
+ import type { SnapshotReport } from './report.ts';
3
+ /**
4
+ * `floor`: the product's theme (the first starter when it names none), light and dark, its languages
5
+ * and `en-XA` (`ar-XB` too with a right-to-left language). `full`: every theme, mode and pseudo-locale.
6
+ */
7
+ export type Matrix = 'floor' | 'full';
8
+ /** Why: the default, the `--full` switch, or a shared input changed since the last full report. */
9
+ export type MatrixReason = 'floor' | 'switch' | 'shared';
10
+ export interface MatrixRun {
11
+ looks: readonly Theme[];
12
+ product: string;
13
+ locales: readonly string[];
14
+ /** Unset: the full matrix (a caller that names none keeps what every run drew before). */
15
+ matrix?: Matrix | undefined;
16
+ /** The selector's added looks (S3c), by scene: drawn on top of the floor, never in its place. */
17
+ extraLooks?: ReadonlyMap<string, ReadonlySet<string>> | undefined;
18
+ }
19
+ /** The product's languages and `en-XA`, and `ar-XB` when one of them is written right to left. */
20
+ export declare function floorLocales(locales: readonly string[]): string[];
21
+ type Look = {
22
+ theme: Theme;
23
+ mode: string;
24
+ locale: string;
25
+ };
26
+ /** A look's name, `<theme>.<mode>.<locale>`, as the selector and the report name it. */
27
+ export declare const lookName: ({ theme, mode, locale }: Look) => string;
28
+ /**
29
+ * The looks a scene is drawn and checked in, in order: theme, then mode, then language. In the
30
+ * floor, a scene the selector added looks for draws those too, in the full matrix's order.
31
+ */
32
+ export declare function drawnLooks(run: MatrixRun, scene?: string): Look[];
33
+ /** Whether a scene drawn in `had` covers a run that draws `wants` (an older report's scene: full). */
34
+ export declare const covers: (had: Matrix | undefined, wants: Matrix | undefined) => boolean;
35
+ /**
36
+ * The matrix a run draws: full with the switch; full when the shared inputs differ from those of the
37
+ * last full report (the nearest ancestor's `fullInputs`), or are unknown; else the floor.
38
+ */
39
+ export declare function chooseMatrix(full: boolean | undefined, current: string | undefined, ancestor: Pick<SnapshotReport, 'fullInputs'> | undefined): {
40
+ matrix: Matrix;
41
+ reason: MatrixReason;
42
+ };
43
+ /** The run's matrix and what its report records, from the switch and the nearest ancestor report. */
44
+ export declare function runMatrix(full: boolean | undefined, parts: string | undefined, root?: string, commit?: string): {
45
+ matrix: Matrix;
46
+ matrixReason: MatrixReason;
47
+ fullInputs: string | undefined;
48
+ };
49
+ /**
50
+ * What `ape prototype version` accepts, judged scene by scene on what was drawn: each scene's matrix
51
+ * is the floor or the full one, covers the report's (`covers`, as reuse: a full report holds no floor
52
+ * scene, reused or not), and its pictures are exactly that matrix's: the product's theme light and
53
+ * dark (the floor), or every theme light and the product's dark (full). The report's languages hold
54
+ * the floor's (`en-XA`, and `ar-XB` with a right-to-left language). A report written before S3b names
55
+ * no matrix and holds no floor scene: it is judged as before.
56
+ */
57
+ export declare function meetsFloor(report: Pick<SnapshotReport, 'matrix' | 'scenes' | 'themes' | 'locales'>): boolean;
58
+ export {};
@@ -0,0 +1,95 @@
1
+ // Which looks a snapshot draws (F113b, F113c): the floor by default, the full matrix with `--full`
2
+ // or when an input every scene shares changed since the last full report.
3
+ import { isRtl, PSEUDO_LOCALES } from '@buffcore/i18n';
4
+ import { findAncestor } from './snapshot-ancestor.js';
5
+ import { lookKey, photographed } from './snapshot-themes.js';
6
+ const FLOOR_MODES = ['light', 'dark'];
7
+ /** The product's languages and `en-XA`, and `ar-XB` when one of them is written right to left. */
8
+ export function floorLocales(locales) {
9
+ const own = locales.filter((locale) => !PSEUDO_LOCALES.some((pseudo) => pseudo === locale));
10
+ return [...own, 'en-XA', ...(own.some(isRtl) ? ['ar-XB'] : [])];
11
+ }
12
+ /** A look's name, `<theme>.<mode>.<locale>`, as the selector and the report name it. */
13
+ export const lookName = ({ theme, mode, locale }) => `${theme.key}.${mode}.${locale}`;
14
+ /**
15
+ * The looks a scene is drawn and checked in, in order: theme, then mode, then language. In the
16
+ * floor, a scene the selector added looks for draws those too, in the full matrix's order.
17
+ */
18
+ export function drawnLooks(run, scene) {
19
+ const extra = scene === undefined ? undefined : run.extraLooks?.get(scene);
20
+ if (run.matrix === 'floor' && extra?.size) {
21
+ const floor = new Set(drawnLooks({ ...run, extraLooks: undefined }).map(lookName));
22
+ return drawnLooks({ ...run, matrix: 'full' }).filter((look) => floor.has(lookName(look)) || extra.has(lookName(look)));
23
+ }
24
+ const floor = run.matrix === 'floor';
25
+ const own = run.looks.find((theme) => theme.key === run.product) ?? run.looks[0];
26
+ const themes = floor ? (own ? [own] : []) : run.looks;
27
+ const locales = floor ? floorLocales(run.locales) : run.locales;
28
+ return themes.flatMap((theme) => Object.keys(theme.color)
29
+ .filter((mode) => !floor || FLOOR_MODES.includes(mode))
30
+ .flatMap((mode) => locales.map((locale) => ({ theme, mode, locale }))));
31
+ }
32
+ /** Whether a scene drawn in `had` covers a run that draws `wants` (an older report's scene: full). */
33
+ export const covers = (had, wants) => (had ?? 'full') === 'full' || wants === 'floor';
34
+ /**
35
+ * The matrix a run draws: full with the switch; full when the shared inputs differ from those of the
36
+ * last full report (the nearest ancestor's `fullInputs`), or are unknown; else the floor.
37
+ */
38
+ export function chooseMatrix(full, current, ancestor) {
39
+ if (full)
40
+ return { matrix: 'full', reason: 'switch' };
41
+ if (current === undefined || ancestor?.fullInputs !== current)
42
+ return { matrix: 'full', reason: 'shared' };
43
+ return { matrix: 'floor', reason: 'floor' };
44
+ }
45
+ /** The run's matrix and what its report records, from the switch and the nearest ancestor report. */
46
+ export function runMatrix(full, parts, root, commit) {
47
+ const ancestor = full || !root || !commit ? undefined : findAncestor(root, commit)?.report;
48
+ const { matrix, reason } = chooseMatrix(full, parts, ancestor);
49
+ return {
50
+ matrix,
51
+ matrixReason: reason,
52
+ fullInputs: matrix === 'full' ? parts : ancestor?.fullInputs,
53
+ };
54
+ }
55
+ const MATRICES = [undefined, 'floor', 'full'];
56
+ /**
57
+ * What `ape prototype version` accepts, judged scene by scene on what was drawn: each scene's matrix
58
+ * is the floor or the full one, covers the report's (`covers`, as reuse: a full report holds no floor
59
+ * scene, reused or not), and its pictures are exactly that matrix's: the product's theme light and
60
+ * dark (the floor), or every theme light and the product's dark (full). The report's languages hold
61
+ * the floor's (`en-XA`, and `ar-XB` with a right-to-left language). A report written before S3b names
62
+ * no matrix and holds no floor scene: it is judged as before.
63
+ */
64
+ export function meetsFloor(report) {
65
+ if (!MATRICES.includes(report.matrix))
66
+ return false;
67
+ // An older ape's report names no matrix: it drew every scene in the full one, as it always did.
68
+ if (report.matrix === undefined)
69
+ return report.scenes.every((scene) => scene.matrix === undefined);
70
+ const locales = report.locales ?? [];
71
+ if (!floorLocales(locales).every((locale) => locales.includes(locale)))
72
+ return false;
73
+ const darks = report.scenes.flatMap((scene) => Object.keys(scene.screenshots).filter((look) => look.endsWith('.dark')));
74
+ const dark = darks[0];
75
+ const product = dark?.slice(0, -'.dark'.length);
76
+ if (!dark || !product || !report.themes?.includes(product))
77
+ return false;
78
+ return report.scenes.every((scene) => {
79
+ if (!MATRICES.includes(scene.matrix) || !covers(scene.matrix, report.matrix))
80
+ return false;
81
+ const want = [...(scene.matrix === 'floor' ? [product] : report.themes), dark].sort();
82
+ // A floor scene's selector picks (S3c) may add their own photos: the picked looks' keys, no other.
83
+ const extra = new Set(scene.matrix === 'floor'
84
+ ? (scene.extraLooks ?? []).flatMap((look) => {
85
+ const [theme = '', mode = ''] = look.split('.');
86
+ return report.themes?.includes(theme) && photographed(theme, mode, product)
87
+ ? [lookKey(theme, mode)]
88
+ : [];
89
+ })
90
+ : []);
91
+ const drawn = Object.keys(scene.screenshots).sort();
92
+ const own = drawn.filter((key) => !(extra.has(key) && !want.includes(key)));
93
+ return JSON.stringify(own) === JSON.stringify(want);
94
+ });
95
+ }
@@ -0,0 +1,8 @@
1
+ import type { Response } from 'playwright-core';
2
+ /** An Expo bundle contains all its views. Bind the actual entry and document bytes as app inputs. */
3
+ export declare function metroInputs(dir: string, workspace: string, page: URL): {
4
+ prepare(): Promise<void>;
5
+ hash: () => string | undefined;
6
+ same(): Promise<boolean>;
7
+ covers(url: URL, kind: string, response?: Response): Promise<boolean>;
8
+ };
@@ -0,0 +1,81 @@
1
+ import { resolve } from 'node:path';
2
+ import { sha256 } from './snapshot-hashes.js';
3
+ import { matchesBytes, servedBytes } from './snapshot-served-inputs.js';
4
+ const webQuery = new URLSearchParams({
5
+ platform: 'web',
6
+ dev: 'true',
7
+ hot: 'false',
8
+ lazy: 'true',
9
+ 'transform.engine': 'hermes',
10
+ 'transform.routerRoot': 'src/app',
11
+ unstable_transformProfile: 'hermes-stable',
12
+ });
13
+ function bundleUrl(html, page, dir, workspace) {
14
+ const scripts = [...html.matchAll(/<script\b([^>]*)>/gi)];
15
+ const [script] = scripts;
16
+ if (scripts.length !== 1 || !script)
17
+ return;
18
+ const path = /^ src="(\/[^"\s]+)" defer$/.exec(script[1] ?? '')?.[1];
19
+ if (!path)
20
+ return;
21
+ const bundle = new URL(path, page);
22
+ if (bundle.origin !== page.origin || bundle.hash || !bundle.pathname.endsWith('.bundle'))
23
+ return;
24
+ const entry = decodeURIComponent(bundle.pathname.slice(0, -'.bundle'.length));
25
+ if (![dir, workspace].some((root) => resolve(root, `.${entry}`) === resolve(dir, 'index.ts')))
26
+ return;
27
+ if (bundle.searchParams.size !== webQuery.size)
28
+ return;
29
+ if ([...webQuery].some(([key, value]) => bundle.searchParams.get(key) !== value))
30
+ return;
31
+ return bundle;
32
+ }
33
+ /**
34
+ * The served text with every loopback address's port taken out: a run's ports are its own (a
35
+ * worktree's are new every time), and Metro writes them into the bundle (its links, and public
36
+ * values such as the API's address).
37
+ */
38
+ const portless = (bytes) => bytes.toString('utf8').replace(/(127\.0\.0\.1|localhost|\[::1\]):\d+/g, '$1:port');
39
+ /** An Expo bundle contains all its views. Bind the actual entry and document bytes as app inputs. */
40
+ export function metroInputs(dir, workspace, page) {
41
+ async function read() {
42
+ const html = await servedBytes(page, 'html');
43
+ if (!html)
44
+ return;
45
+ const bundle = bundleUrl(html.toString('utf8'), page, dir, workspace);
46
+ if (!bundle)
47
+ return;
48
+ const js = await servedBytes(bundle, 'javascript');
49
+ if (!js)
50
+ return;
51
+ return {
52
+ path: bundle.pathname + bundle.search,
53
+ html: sha256(html),
54
+ js: sha256(js),
55
+ code: sha256(JSON.stringify([portless(html), portless(js)])),
56
+ };
57
+ }
58
+ let prepared;
59
+ const safeRead = () => read().catch(() => undefined);
60
+ return {
61
+ async prepare() {
62
+ prepared = await safeRead();
63
+ },
64
+ hash: () => prepared && sha256(JSON.stringify([prepared.path, prepared.code])),
65
+ async same() {
66
+ return !prepared || JSON.stringify(prepared) === JSON.stringify(await safeRead());
67
+ },
68
+ async covers(url, kind, response) {
69
+ if (!prepared)
70
+ return false;
71
+ if (kind === 'websocket')
72
+ return !url.search && ['/hot', '/message'].includes(url.pathname);
73
+ const hash = kind === 'document' && url.pathname === '/_scene'
74
+ ? prepared.html
75
+ : kind === 'script' && url.pathname + url.search === prepared.path
76
+ ? prepared.js
77
+ : undefined;
78
+ return matchesBytes(hash, response);
79
+ },
80
+ };
81
+ }
@@ -13,8 +13,12 @@ export interface Owners {
13
13
  export declare function parseNetstat(text: string, port: number): number[];
14
14
  /** `lsof -Fp` lines: `p4242`. */
15
15
  export declare function parseLsof(text: string): number[];
16
- /** `pid ppid` pairs, one per line, from either platform's process table. */
17
- export declare function descendants(table: string, root: number): Set<number>;
16
+ /**
17
+ * `pid ppid` pairs, one per line, from either platform's process table. With `starts` (each
18
+ * process's start, in milliseconds), a child counts only when it started at or after its parent:
19
+ * Windows keeps a dead parent's id on its children, and that id can be given to a later process.
20
+ */
21
+ export declare function descendants(table: string, root: number, starts?: ReadonlyMap<number, number>): Set<number>;
18
22
  export declare const systemOwners: Owners;
19
23
  /** Who has the port: `ours`, `foreign` (a listener outside the tree, or none), or `failed` (no answer). */
20
24
  export declare function portOwner(port: number, pid: number, owners?: Owners): Promise<'ours' | 'foreign' | 'failed'>;
@@ -28,8 +28,12 @@ export function parseLsof(text) {
28
28
  .filter((line) => /^p\d+$/.test(line))
29
29
  .map((line) => Number(line.slice(1)));
30
30
  }
31
- /** `pid ppid` pairs, one per line, from either platform's process table. */
32
- export function descendants(table, root) {
31
+ /**
32
+ * `pid ppid` pairs, one per line, from either platform's process table. With `starts` (each
33
+ * process's start, in milliseconds), a child counts only when it started at or after its parent:
34
+ * Windows keeps a dead parent's id on its children, and that id can be given to a later process.
35
+ */
36
+ export function descendants(table, root, starts) {
33
37
  const children = new Map();
34
38
  for (const line of table.split(/\r?\n/)) {
35
39
  const [pid, parent] = line.trim().split(/\s+/).map(Number);
@@ -38,9 +42,14 @@ export function descendants(table, root) {
38
42
  children.set(parent, [...(children.get(parent) ?? []), pid]);
39
43
  }
40
44
  const tree = new Set([root]);
45
+ const later = (child, parent) => {
46
+ const [mine, theirs] = [starts?.get(child), starts?.get(parent)];
47
+ return mine === undefined || theirs === undefined || mine >= theirs;
48
+ };
41
49
  for (const pid of tree)
42
50
  for (const child of children.get(pid) ?? [])
43
- tree.add(child);
51
+ if (later(child, pid))
52
+ tree.add(child);
44
53
  return tree;
45
54
  }
46
55
  const windows = {
@@ -0,0 +1,14 @@
1
+ import type { SceneResult } from './scene.ts';
2
+ export declare function atomicWrite(path: string, text: string): Promise<void>;
3
+ export declare function readIfPresent(path: string): Promise<string | undefined>;
4
+ export declare function shotNames(result: SceneResult, commit: string): string[];
5
+ export declare function stageShot(from: string, to: string): Promise<void>;
6
+ /**
7
+ * A scene's entry is bound to the run's identity, its place in the scene list, its own inputs and
8
+ * the dev servers' runtime files its requests were checked against.
9
+ */
10
+ export declare const sceneBinding: (identity: string, index: number, key: string, inputs: unknown, runtime: string | undefined) => string;
11
+ export declare function fingerprint(binding: string, result: SceneResult, commit: string, folder: string): Promise<string>;
12
+ export declare function readScene(dir: string, commit: string, binding: string): Promise<SceneResult | undefined>;
13
+ /** Only a scene whose every request its fingerprint covers is kept. */
14
+ export declare const tracked: ({ apiCalls, untrackedRequests }: SceneResult) => boolean;
@@ -0,0 +1,85 @@
1
+ // One entry of the snapshot journal: a drawn scene's result, its pictures, its binding and checksum.
2
+ import { createHash } from 'node:crypto';
3
+ import { copyFile, link, lstat, readFile, rename, rm, writeFile } from 'node:fs/promises';
4
+ import { basename, join } from 'node:path';
5
+ import { PrototypeProblem } from '@buffcore/contracts/prototype';
6
+ import { z } from 'zod';
7
+ import { sha256 } from './snapshot-hashes.js';
8
+ const resultShape = z.object({
9
+ screenshots: z.record(z.string(), z.string()),
10
+ cut: z.array(z.string()),
11
+ apiCalls: z.boolean().optional(),
12
+ untrackedRequests: z.boolean().optional(),
13
+ retried: z.literal(true).optional(),
14
+ retryError: z.string().optional(),
15
+ problems: z.array(PrototypeProblem),
16
+ hints: z.array(z.object({
17
+ scene: z.string(),
18
+ theme: z.string(),
19
+ mode: z.string(),
20
+ locale: z.string(),
21
+ detail: z.string(),
22
+ })),
23
+ });
24
+ export async function atomicWrite(path, text) {
25
+ const temp = `${path}.tmp`;
26
+ await writeFile(temp, text);
27
+ await rename(temp, path);
28
+ }
29
+ export async function readIfPresent(path) {
30
+ try {
31
+ return await readFile(path, 'utf8');
32
+ }
33
+ catch (error) {
34
+ if (error.code !== 'ENOENT')
35
+ throw error;
36
+ }
37
+ }
38
+ export function shotNames(result, commit) {
39
+ return Object.values(result.screenshots).map((path) => {
40
+ const file = basename(path);
41
+ if (path !== `${commit}/${file}` || !/^[^/\\:]+\.png$/.test(file))
42
+ throw new Error();
43
+ return file;
44
+ });
45
+ }
46
+ export async function stageShot(from, to) {
47
+ try {
48
+ await link(from, to);
49
+ }
50
+ catch (error) {
51
+ // A worktree can be on a different disk. Unlink a name collision before copying.
52
+ if (error.code === 'EEXIST')
53
+ await rm(to);
54
+ await copyFile(from, to);
55
+ }
56
+ }
57
+ /**
58
+ * A scene's entry is bound to the run's identity, its place in the scene list, its own inputs and
59
+ * the dev servers' runtime files its requests were checked against.
60
+ */
61
+ export const sceneBinding = (identity, index, key, inputs, runtime) => JSON.stringify([identity, index, key, inputs ?? null, runtime ?? null]);
62
+ export async function fingerprint(binding, result, commit, folder) {
63
+ const sum = createHash('sha256').update(binding).update(JSON.stringify(result));
64
+ for (const file of shotNames(result, commit)) {
65
+ const path = join(folder, file);
66
+ if (!(await lstat(path)).isFile())
67
+ throw new Error();
68
+ sum.update(sha256(await readFile(path)));
69
+ }
70
+ return sum.digest('hex');
71
+ }
72
+ export async function readScene(dir, commit, binding) {
73
+ const saved = await readIfPresent(join(dir, 'result.json'));
74
+ if (saved === undefined)
75
+ return;
76
+ const { checksum, result } = JSON.parse(saved);
77
+ if (!resultShape.safeParse(result).success)
78
+ throw new Error();
79
+ if (checksum !== (await fingerprint(binding, result, commit, dir)))
80
+ throw new Error();
81
+ if (tracked(result))
82
+ return result;
83
+ }
84
+ /** Only a scene whose every request its fingerprint covers is kept. */
85
+ export const tracked = ({ apiCalls, untrackedRequests }) => apiCalls === false && untrackedRequests === false;
@@ -16,6 +16,23 @@ export interface SnapshotProgress {
16
16
  draw(scene: Scene, run: Run, draw: Draw): Promise<SceneResult>;
17
17
  check(): void;
18
18
  }
19
- /** The one saved run lives in Git's validation storage, outside publication and child cleanup. */
20
- export declare function withSnapshotProgress<T>(source: SnapshotProgressSource, inputs: () => Promise<unknown>, scenes: readonly Scene[], commit: string, write: (remaining: readonly Scene[], progress?: SnapshotProgress) => Promise<T>): Promise<T>;
19
+ interface Scenes {
20
+ /** Every scene the product has: a scene's entry is named by its place here. */
21
+ all: readonly Scene[];
22
+ /** The scenes this run draws (`--scene` picks some); the others' entries are left alone. */
23
+ picked: readonly Scene[];
24
+ /** Each scene's own inputs (its fingerprint), bound into its entry. */
25
+ inputs: ReadonlyMap<string, unknown> | undefined;
26
+ }
27
+ /**
28
+ * The one saved run lives in Git's validation storage, outside publication and child cleanup. A run
29
+ * that stops, crashes or has a scene that failed to draw keeps its finished scenes; a run that drew
30
+ * every scene it picked (findings or not) drops their entries.
31
+ */
32
+ export declare function withSnapshotProgress<T extends {
33
+ problems: readonly {
34
+ check: string;
35
+ scene?: string;
36
+ }[];
37
+ }>(source: SnapshotProgressSource, inputs: () => Promise<unknown>, { all, picked, inputs: own }: Scenes, commit: string, write: (remaining: readonly Scene[], progress?: SnapshotProgress) => Promise<T>, runtime?: () => string | undefined): Promise<T>;
21
38
  export {};