@air-jam/devtools 0.0.0-stage → 1.0.0-beta.1

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 (256) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +112 -2
  3. package/dist/chunk-2O67AXCU.js +470 -0
  4. package/dist/chunk-2O67AXCU.js.map +1 -0
  5. package/dist/chunk-AO3ZDXF6.js +142 -0
  6. package/dist/chunk-AO3ZDXF6.js.map +1 -0
  7. package/dist/chunk-ART5TEYT.js +472 -0
  8. package/dist/chunk-ART5TEYT.js.map +1 -0
  9. package/dist/chunk-ASC2CK2R.js +77 -0
  10. package/dist/chunk-ASC2CK2R.js.map +1 -0
  11. package/dist/chunk-E3UZZGAV.js +50 -0
  12. package/dist/chunk-E3UZZGAV.js.map +1 -0
  13. package/dist/chunk-EJTE7UKX.js +64 -0
  14. package/dist/chunk-EJTE7UKX.js.map +1 -0
  15. package/dist/chunk-ESFKNN75.js +110 -0
  16. package/dist/chunk-ESFKNN75.js.map +1 -0
  17. package/dist/chunk-EU7A6PXW.js +1339 -0
  18. package/dist/chunk-EU7A6PXW.js.map +1 -0
  19. package/dist/chunk-FN7E6EA7.js +549 -0
  20. package/dist/chunk-FN7E6EA7.js.map +1 -0
  21. package/dist/chunk-GDFAWHGA.js +190 -0
  22. package/dist/chunk-GDFAWHGA.js.map +1 -0
  23. package/dist/chunk-HZUGBH4Y.js +347 -0
  24. package/dist/chunk-HZUGBH4Y.js.map +1 -0
  25. package/dist/chunk-ITKAOEGX.js +93 -0
  26. package/dist/chunk-ITKAOEGX.js.map +1 -0
  27. package/dist/chunk-JBCUTWRI.js +897 -0
  28. package/dist/chunk-JBCUTWRI.js.map +1 -0
  29. package/dist/chunk-JSI5S4WP.js +1174 -0
  30. package/dist/chunk-JSI5S4WP.js.map +1 -0
  31. package/dist/chunk-L4D675BG.js +97 -0
  32. package/dist/chunk-L4D675BG.js.map +1 -0
  33. package/dist/chunk-M6CJ7GQC.js +133 -0
  34. package/dist/chunk-M6CJ7GQC.js.map +1 -0
  35. package/dist/chunk-MEIMOSZC.js +749 -0
  36. package/dist/chunk-MEIMOSZC.js.map +1 -0
  37. package/dist/chunk-O3XG3FNL.js +1014 -0
  38. package/dist/chunk-O3XG3FNL.js.map +1 -0
  39. package/dist/chunk-OM6SCJ7H.js +18 -0
  40. package/dist/chunk-OM6SCJ7H.js.map +1 -0
  41. package/dist/chunk-PKD4WVX5.js +63 -0
  42. package/dist/chunk-PKD4WVX5.js.map +1 -0
  43. package/dist/chunk-PMBLXGFM.js +3384 -0
  44. package/dist/chunk-PMBLXGFM.js.map +1 -0
  45. package/dist/chunk-PMNE5GIN.js +279 -0
  46. package/dist/chunk-PMNE5GIN.js.map +1 -0
  47. package/dist/chunk-QAWI64VR.js +148 -0
  48. package/dist/chunk-QAWI64VR.js.map +1 -0
  49. package/dist/chunk-QGADSPHZ.js +449 -0
  50. package/dist/chunk-QGADSPHZ.js.map +1 -0
  51. package/dist/chunk-SPYN66UV.js +338 -0
  52. package/dist/chunk-SPYN66UV.js.map +1 -0
  53. package/dist/context.d.ts +19 -0
  54. package/dist/context.js +10 -0
  55. package/dist/context.js.map +1 -0
  56. package/dist/controller.d.ts +67 -0
  57. package/dist/controller.js +40 -0
  58. package/dist/controller.js.map +1 -0
  59. package/dist/dependencies.d.ts +44 -0
  60. package/dist/dependencies.js +10 -0
  61. package/dist/dependencies.js.map +1 -0
  62. package/dist/dev.d.ts +21 -0
  63. package/dist/dev.js +19 -0
  64. package/dist/dev.js.map +1 -0
  65. package/dist/game-builds.d.ts +200 -0
  66. package/dist/game-builds.js +16 -0
  67. package/dist/game-builds.js.map +1 -0
  68. package/dist/game-run-watch-CEd_SpAT.d.ts +84 -0
  69. package/dist/game-runs.d.ts +1452 -0
  70. package/dist/game-runs.js +76 -0
  71. package/dist/game-runs.js.map +1 -0
  72. package/dist/game-session-broker-CUvORfMd.d.ts +439 -0
  73. package/dist/game-session-broker.d.ts +4 -0
  74. package/dist/game-session-broker.js +40 -0
  75. package/dist/game-session-broker.js.map +1 -0
  76. package/dist/game-session.d.ts +3934 -0
  77. package/dist/game-session.js +195 -0
  78. package/dist/game-session.js.map +1 -0
  79. package/dist/game-settings.d.ts +16 -0
  80. package/dist/game-settings.js +237 -0
  81. package/dist/game-settings.js.map +1 -0
  82. package/dist/games.d.ts +28 -0
  83. package/dist/games.js +16 -0
  84. package/dist/games.js.map +1 -0
  85. package/dist/harness/visual.d.ts +204 -0
  86. package/dist/harness/visual.js +46 -0
  87. package/dist/harness/visual.js.map +1 -0
  88. package/dist/logs.d.ts +15 -0
  89. package/dist/logs.js +57 -0
  90. package/dist/logs.js.map +1 -0
  91. package/dist/managed-process-scope-BicYi_t9.d.ts +3 -0
  92. package/dist/mcp-config.d.ts +63 -0
  93. package/dist/mcp-config.js +191 -0
  94. package/dist/mcp-config.js.map +1 -0
  95. package/dist/platform-auth.d.ts +9 -0
  96. package/dist/platform-auth.js +45 -0
  97. package/dist/platform-auth.js.map +1 -0
  98. package/dist/platform-game-analytics.d.ts +51 -0
  99. package/dist/platform-game-analytics.js +49 -0
  100. package/dist/platform-game-analytics.js.map +1 -0
  101. package/dist/platform-game-app-id.d.ts +46 -0
  102. package/dist/platform-game-app-id.js +93 -0
  103. package/dist/platform-game-app-id.js.map +1 -0
  104. package/dist/platform-game-media.d.ts +347 -0
  105. package/dist/platform-game-media.js +168 -0
  106. package/dist/platform-game-media.js.map +1 -0
  107. package/dist/platform-games.d.ts +87 -0
  108. package/dist/platform-games.js +183 -0
  109. package/dist/platform-games.js.map +1 -0
  110. package/dist/quality.d.ts +16 -0
  111. package/dist/quality.js +62 -0
  112. package/dist/quality.js.map +1 -0
  113. package/dist/release.d.ts +1304 -0
  114. package/dist/release.js +34 -0
  115. package/dist/release.js.map +1 -0
  116. package/dist/rooms-check.d.ts +90 -0
  117. package/dist/rooms-check.js +717 -0
  118. package/dist/rooms-check.js.map +1 -0
  119. package/dist/source-tree-contract.d.ts +16 -0
  120. package/dist/source-tree-contract.js +13 -0
  121. package/dist/source-tree-contract.js.map +1 -0
  122. package/dist/testing.d.ts +464 -0
  123. package/dist/testing.js +27 -0
  124. package/dist/testing.js.map +1 -0
  125. package/dist/tooling/describe-game.d.ts +2 -0
  126. package/dist/tooling/describe-game.js +54 -0
  127. package/dist/tooling/describe-game.js.map +1 -0
  128. package/dist/tooling/game-session-broker.d.ts +2 -0
  129. package/dist/tooling/game-session-broker.js +28 -0
  130. package/dist/tooling/game-session-broker.js.map +1 -0
  131. package/dist/tooling/hold-runtime-host.d.ts +2 -0
  132. package/dist/tooling/hold-runtime-host.js +931 -0
  133. package/dist/tooling/hold-runtime-host.js.map +1 -0
  134. package/dist/tooling/list-visual-scenarios.d.ts +2 -0
  135. package/dist/tooling/list-visual-scenarios.js +25 -0
  136. package/dist/tooling/list-visual-scenarios.js.map +1 -0
  137. package/dist/tooling/managed-dev-supervisor.d.ts +2 -0
  138. package/dist/tooling/managed-dev-supervisor.js +51 -0
  139. package/dist/tooling/managed-dev-supervisor.js.map +1 -0
  140. package/dist/tooling/process-scope-bootstrap.mjs +24 -0
  141. package/dist/tooling/process-scope-guard.d.ts +2 -0
  142. package/dist/tooling/process-scope-guard.js +388 -0
  143. package/dist/tooling/process-scope-guard.js.map +1 -0
  144. package/dist/tooling/process-scope-windows.ps1 +148 -0
  145. package/dist/tooling/rooms-check-device.d.ts +2 -0
  146. package/dist/tooling/rooms-check-device.js +498 -0
  147. package/dist/tooling/rooms-check-device.js.map +1 -0
  148. package/dist/tooling/run-headless-room.d.ts +2 -0
  149. package/dist/tooling/run-headless-room.js +287 -0
  150. package/dist/tooling/run-headless-room.js.map +1 -0
  151. package/dist/tooling/run-test-profile.d.ts +2 -0
  152. package/dist/tooling/run-test-profile.js +181 -0
  153. package/dist/tooling/run-test-profile.js.map +1 -0
  154. package/dist/tooling/run-visual-capture.d.ts +2 -0
  155. package/dist/tooling/run-visual-capture.js +203 -0
  156. package/dist/tooling/run-visual-capture.js.map +1 -0
  157. package/dist/tooling/seal-game-build.d.ts +2 -0
  158. package/dist/tooling/seal-game-build.js +120 -0
  159. package/dist/tooling/seal-game-build.js.map +1 -0
  160. package/dist/types-CdYrgrG_.d.ts +1036 -0
  161. package/dist/types-DyRD-ym1.d.ts +141 -0
  162. package/dist/visual.d.ts +21 -0
  163. package/dist/visual.js +219 -0
  164. package/dist/visual.js.map +1 -0
  165. package/package.json +150 -3
  166. package/src/bounded-response.ts +28 -0
  167. package/src/built-game-runtime.ts +152 -0
  168. package/src/commands.ts +40 -0
  169. package/src/context.ts +218 -0
  170. package/src/controller-protocol.ts +22 -0
  171. package/src/controller.ts +1526 -0
  172. package/src/current-room-host.ts +166 -0
  173. package/src/decision-provider.ts +102 -0
  174. package/src/dependencies.ts +176 -0
  175. package/src/dependency-install-contract.ts +12 -0
  176. package/src/dev.ts +995 -0
  177. package/src/fs-utils.ts +93 -0
  178. package/src/game-action-observation.ts +106 -0
  179. package/src/game-build-protocol.ts +72 -0
  180. package/src/game-build.ts +483 -0
  181. package/src/game-run-owner.ts +2358 -0
  182. package/src/game-run-protocol.ts +448 -0
  183. package/src/game-run-record.ts +92 -0
  184. package/src/game-run-replay.ts +95 -0
  185. package/src/game-run-watch.ts +155 -0
  186. package/src/game-session-broker.ts +793 -0
  187. package/src/game-session-operations.ts +61 -0
  188. package/src/game-session-protocol.ts +180 -0
  189. package/src/game-session-runtime.ts +861 -0
  190. package/src/game-session.ts +206 -0
  191. package/src/game-settings.ts +295 -0
  192. package/src/games.ts +262 -0
  193. package/src/harness/visual/browser.ts +100 -0
  194. package/src/harness/visual/controller-observation.ts +181 -0
  195. package/src/harness/visual/prefab-contract.ts +29 -0
  196. package/src/harness/visual/prefab-runner.ts +178 -0
  197. package/src/harness/visual/runner.ts +549 -0
  198. package/src/harness/visual/runtime-controller-observations.ts +151 -0
  199. package/src/harness/visual/scenario-contract.ts +8 -0
  200. package/src/harness/visual/scenario-helpers.ts +67 -0
  201. package/src/harness/visual/screencast.ts +155 -0
  202. package/src/harness/visual/session.ts +400 -0
  203. package/src/harness/visual/types.ts +184 -0
  204. package/src/harness/visual.ts +7 -0
  205. package/src/headless-room-protocol.ts +112 -0
  206. package/src/headless-room.ts +222 -0
  207. package/src/helper-channel.ts +229 -0
  208. package/src/helper-scripts.ts +69 -0
  209. package/src/installed-dependencies.ts +74 -0
  210. package/src/logs.ts +67 -0
  211. package/src/managed-process-scope.ts +481 -0
  212. package/src/mcp-config.ts +262 -0
  213. package/src/node-process-environment.ts +10 -0
  214. package/src/platform-auth.ts +594 -0
  215. package/src/platform-game-analytics.ts +50 -0
  216. package/src/platform-game-app-id.ts +99 -0
  217. package/src/platform-game-media.ts +209 -0
  218. package/src/platform-games.ts +217 -0
  219. package/src/policy-artifact-contract.ts +52 -0
  220. package/src/policy-artifacts.ts +134 -0
  221. package/src/process-scope-admission.ts +50 -0
  222. package/src/process-scope-protocol.ts +66 -0
  223. package/src/process-scope.ts +327 -0
  224. package/src/quality.ts +82 -0
  225. package/src/release.ts +1683 -0
  226. package/src/rooms-check-fleet.ts +194 -0
  227. package/src/rooms-check-link.ts +138 -0
  228. package/src/rooms-check-protocol.ts +244 -0
  229. package/src/rooms-check.ts +797 -0
  230. package/src/runtime-owner-protocol.ts +218 -0
  231. package/src/source-tree-contract.ts +70 -0
  232. package/src/source-tree.ts +84 -0
  233. package/src/test-profile-process.ts +153 -0
  234. package/src/test-profile-protocol.ts +116 -0
  235. package/src/testing.ts +430 -0
  236. package/src/tooling/declaration-hook.ts +52 -0
  237. package/src/tooling/describe-game.ts +49 -0
  238. package/src/tooling/game-session-broker.ts +7 -0
  239. package/src/tooling/hold-runtime-host.ts +441 -0
  240. package/src/tooling/list-visual-scenarios.ts +21 -0
  241. package/src/tooling/load-game.ts +79 -0
  242. package/src/tooling/managed-dev-supervisor.ts +71 -0
  243. package/src/tooling/process-scope-bootstrap.mjs +24 -0
  244. package/src/tooling/process-scope-guard.ts +213 -0
  245. package/src/tooling/process-scope-windows.ps1 +148 -0
  246. package/src/tooling/rooms-check-device.ts +493 -0
  247. package/src/tooling/run-headless-room.ts +326 -0
  248. package/src/tooling/run-test-profile.ts +190 -0
  249. package/src/tooling/run-visual-capture.ts +231 -0
  250. package/src/tooling/runtime-owner-actions.ts +65 -0
  251. package/src/tooling/seal-game-build.ts +118 -0
  252. package/src/tooling/visual-pack.ts +11 -0
  253. package/src/types.ts +928 -0
  254. package/src/visual.ts +296 -0
  255. package/src/windows-process-scopes.ts +189 -0
  256. package/src/workspace-contract.ts +32 -0
package/src/visual.ts ADDED
@@ -0,0 +1,296 @@
1
+ import path from "node:path";
2
+ import { z } from "zod";
3
+ import { runCommandResult } from "./commands.js";
4
+ import {
5
+ getTopology,
6
+ startDev,
7
+ stopDev,
8
+ tryAttachToRunningDev,
9
+ } from "./dev.js";
10
+ import { pathExists, readJsonFile } from "./fs-utils.js";
11
+ import { inspectGame } from "./games.js";
12
+ import {
13
+ resolveDevtoolsHelperArgs,
14
+ resolveDevtoolsHelperScript,
15
+ } from "./helper-scripts.js";
16
+ import type {
17
+ AirJamSurfaceUrlSummary,
18
+ AirJamVisualArtifactMode,
19
+ AirJamVisualCaptureInspection,
20
+ AirJamVisualCaptureSummary,
21
+ AirJamVisualScenarioList,
22
+ AirJamVisualScenarioMetadata,
23
+ CaptureVisualsOptions,
24
+ CaptureVisualsResult,
25
+ ListVisualScenariosOptions,
26
+ } from "./types.js";
27
+
28
+ export type {
29
+ AirJamVisualArtifactMode,
30
+ AirJamVisualCaptureInspection,
31
+ AirJamVisualCaptureSummary,
32
+ AirJamVisualScenarioList,
33
+ AirJamVisualScenarioMetadata,
34
+ CaptureVisualsOptions,
35
+ CaptureVisualsResult,
36
+ ListVisualScenariosOptions,
37
+ } from "./types.js";
38
+
39
+ type ResolvedVisualSource = {
40
+ configPath: string;
41
+ };
42
+
43
+ const captureUrlsSchema = z.object({
44
+ appOrigin: z.url(),
45
+ hostUrl: z.url(),
46
+ controllerBaseUrl: z.url(),
47
+ publicHost: z.url(),
48
+ localBuildUrl: z.url().nullable(),
49
+ browserBuildUrl: z.url().nullable(),
50
+ });
51
+
52
+ export type CaptureVisualsAtRuntimeOptions = Pick<
53
+ CaptureVisualsOptions,
54
+ "cwd" | "gameId" | "scenarioId" | "secure"
55
+ > & {
56
+ mode: AirJamVisualArtifactMode;
57
+ urls: AirJamSurfaceUrlSummary;
58
+ artifactRoot: string;
59
+ };
60
+
61
+ const resolveVisualArtifactRoot = (rootDir: string): string =>
62
+ path.join(rootDir, ".airjam", "artifacts", "visual");
63
+
64
+ const parseHelperJson = <T>(output: string): T => {
65
+ const startIndex = output.indexOf("{");
66
+ const endIndex = output.lastIndexOf("}");
67
+ if (startIndex === -1 || endIndex === -1 || endIndex < startIndex) {
68
+ throw new Error(`Expected JSON helper output but received:\n${output}`);
69
+ }
70
+
71
+ return JSON.parse(output.slice(startIndex, endIndex + 1)) as T;
72
+ };
73
+
74
+ const runTsxHelper = <T>({
75
+ helperFile,
76
+ args,
77
+ cwd,
78
+ }: {
79
+ helperFile: string;
80
+ args: string[];
81
+ cwd: string;
82
+ }): T => {
83
+ const result = runCommandResult({
84
+ command: process.execPath,
85
+ args: [...resolveDevtoolsHelperArgs(helperFile), ...args],
86
+ cwd,
87
+ });
88
+ if (!result.ok) {
89
+ throw new Error(
90
+ `Air Jam visual helper failed.\n\n${result.stderr || result.stdout}`,
91
+ );
92
+ }
93
+
94
+ return parseHelperJson<T>(result.stdout);
95
+ };
96
+
97
+ const resolveVisualSource = (
98
+ game: Awaited<ReturnType<typeof inspectGame>>,
99
+ ): ResolvedVisualSource | null =>
100
+ game.declaration.testing ? { configPath: game.configPath } : null;
101
+
102
+ const readScenarioMetadata = async ({
103
+ artifactRoot,
104
+ summary,
105
+ }: {
106
+ artifactRoot: string;
107
+ summary: AirJamVisualCaptureInspection["summary"];
108
+ }): Promise<AirJamVisualScenarioMetadata[]> => {
109
+ const entries = await Promise.all(
110
+ summary.scenarios.map(async (scenario) => {
111
+ const metadataPath = path.join(
112
+ artifactRoot,
113
+ scenario.relativeDir,
114
+ "metadata.json",
115
+ );
116
+ return (await pathExists(metadataPath))
117
+ ? readJsonFile<AirJamVisualScenarioMetadata>(metadataPath)
118
+ : null;
119
+ }),
120
+ );
121
+
122
+ return entries.filter(
123
+ (entry): entry is AirJamVisualScenarioMetadata => entry !== null,
124
+ );
125
+ };
126
+
127
+ export const listVisualScenarios = async ({
128
+ cwd = process.cwd(),
129
+ gameId,
130
+ }: ListVisualScenariosOptions = {}): Promise<AirJamVisualScenarioList> => {
131
+ const game = await inspectGame({ cwd, gameId });
132
+ const source = resolveVisualSource(game);
133
+ if (!source) {
134
+ throw new Error(
135
+ `No visual scenarios declared for "${game.id}" in ${game.rootDir}.`,
136
+ );
137
+ }
138
+
139
+ const result = runTsxHelper<{
140
+ hasVisualScenarios: boolean;
141
+ gameId: string;
142
+ scenarios: AirJamVisualScenarioList["scenarios"];
143
+ }>({
144
+ helperFile: resolveDevtoolsHelperScript("list-visual-scenarios.ts"),
145
+ cwd: game.rootDir,
146
+ args: [`--config=${source.configPath}`],
147
+ });
148
+ if (!result.hasVisualScenarios)
149
+ throw new Error(`No visual scenarios declared for "${game.id}".`);
150
+ return {
151
+ gameId: result.gameId,
152
+ configPath: source.configPath,
153
+ scenarios: result.scenarios,
154
+ };
155
+ };
156
+
157
+ const withVisualSession = async <T>({
158
+ cwd = process.cwd(),
159
+ gameId,
160
+ mode = "standalone-dev",
161
+ secure = false,
162
+ run,
163
+ }: {
164
+ cwd?: string;
165
+ gameId?: string;
166
+ mode?: "standalone-dev" | "arcade-dev" | "arcade-test";
167
+ secure?: boolean;
168
+ run: (input: {
169
+ game: Awaited<ReturnType<typeof inspectGame>>;
170
+ visualSource: ResolvedVisualSource;
171
+ topology: Awaited<ReturnType<typeof getTopology>>;
172
+ }) => Promise<T>;
173
+ }): Promise<T> => {
174
+ const game = await inspectGame({ cwd, gameId });
175
+ const visualSource = resolveVisualSource(game);
176
+ if (!visualSource) {
177
+ throw new Error(
178
+ `No visual scenarios declared for "${game.id}" in ${game.rootDir}.`,
179
+ );
180
+ }
181
+
182
+ const attachedTopology = await tryAttachToRunningDev({
183
+ cwd,
184
+ gameId: game.id,
185
+ mode,
186
+ secure,
187
+ });
188
+ const session = attachedTopology
189
+ ? {
190
+ topology: attachedTopology,
191
+ reusedExistingProcess: true,
192
+ managedProcessId: attachedTopology.process?.id ?? null,
193
+ }
194
+ : await startDev({ cwd, gameId: game.id, mode, secure }).then(
195
+ (started) => ({
196
+ topology: started.topology,
197
+ reusedExistingProcess: started.reusedExistingProcess,
198
+ managedProcessId: started.process.id,
199
+ }),
200
+ );
201
+
202
+ try {
203
+ return await run({ game, visualSource, topology: session.topology });
204
+ } finally {
205
+ if (!session.reusedExistingProcess && session.managedProcessId) {
206
+ await stopDev({ cwd, processId: session.managedProcessId });
207
+ }
208
+ }
209
+ };
210
+
211
+ const captureGameAtRuntime = async ({
212
+ game,
213
+ visualSource,
214
+ scenarioId,
215
+ mode,
216
+ secure,
217
+ urls,
218
+ artifactRoot,
219
+ }: Omit<CaptureVisualsAtRuntimeOptions, "cwd" | "gameId"> & {
220
+ game: Awaited<ReturnType<typeof inspectGame>>;
221
+ visualSource: ResolvedVisualSource;
222
+ }): Promise<CaptureVisualsResult> => {
223
+ const resolvedUrls = captureUrlsSchema.parse(urls);
224
+ runTsxHelper<AirJamVisualCaptureSummary>({
225
+ helperFile: resolveDevtoolsHelperScript("run-visual-capture.ts"),
226
+ cwd: game.rootDir,
227
+ args: [
228
+ `--game-id=${game.id}`,
229
+ `--config=${visualSource.configPath}`,
230
+ `--artifact-root=${artifactRoot}`,
231
+ `--mode=${mode === "arcade-built" ? "arcade-test" : "standalone-dev"}`,
232
+ `--app-origin=${resolvedUrls.appOrigin}`,
233
+ `--host-url=${resolvedUrls.hostUrl}`,
234
+ `--controller-base-url=${resolvedUrls.controllerBaseUrl}`,
235
+ `--public-host=${resolvedUrls.publicHost}`,
236
+ ...(resolvedUrls.localBuildUrl
237
+ ? [`--local-build-url=${resolvedUrls.localBuildUrl}`]
238
+ : []),
239
+ ...(resolvedUrls.browserBuildUrl
240
+ ? [`--browser-build-url=${resolvedUrls.browserBuildUrl}`]
241
+ : []),
242
+ ...(scenarioId ? [`--scenario-id=${scenarioId}`] : []),
243
+ ...(secure ? ["--secure"] : []),
244
+ ],
245
+ });
246
+ const summaryPath = path.join(artifactRoot, game.id, "capture-summary.json");
247
+ const summary = await readJsonFile<AirJamVisualCaptureSummary>(summaryPath);
248
+ return {
249
+ gameId: game.id,
250
+ artifactRoot,
251
+ summaryPath,
252
+ summary,
253
+ scenarios: await readScenarioMetadata({ artifactRoot, summary }),
254
+ };
255
+ };
256
+
257
+ export const captureVisualsAtRuntime = async ({
258
+ cwd = process.cwd(),
259
+ gameId,
260
+ ...runtime
261
+ }: CaptureVisualsAtRuntimeOptions): Promise<CaptureVisualsResult> => {
262
+ const game = await inspectGame({ cwd, gameId });
263
+ const visualSource = resolveVisualSource(game);
264
+ if (!visualSource) {
265
+ throw new Error(
266
+ `No visual scenarios declared for "${game.id}" in ${game.rootDir}.`,
267
+ );
268
+ }
269
+ return captureGameAtRuntime({ game, visualSource, ...runtime });
270
+ };
271
+
272
+ export const captureVisuals = async ({
273
+ cwd = process.cwd(),
274
+ gameId,
275
+ scenarioId,
276
+ mode = "standalone-dev",
277
+ secure = false,
278
+ }: CaptureVisualsOptions = {}): Promise<CaptureVisualsResult> =>
279
+ withVisualSession({
280
+ cwd,
281
+ gameId,
282
+ mode,
283
+ secure,
284
+ run: ({ game, visualSource, topology }) =>
285
+ captureGameAtRuntime({
286
+ game,
287
+ visualSource,
288
+ scenarioId,
289
+ mode: mode === "arcade-test" ? "arcade-built" : "standalone-dev",
290
+ secure,
291
+ urls: topology.urls,
292
+ artifactRoot: resolveVisualArtifactRoot(
293
+ topology.process?.cwd ?? game.rootDir,
294
+ ),
295
+ }),
296
+ });
@@ -0,0 +1,189 @@
1
+ import { spawn, type ChildProcess } from "node:child_process";
2
+ import { randomUUID } from "node:crypto";
3
+ import { createInterface } from "node:readline";
4
+ import { debuglog } from "node:util";
5
+ import { resolveDevtoolsHelperScript } from "./helper-scripts.js";
6
+ import {
7
+ processGuardResponseSchema,
8
+ processGuardStartupTimeoutMs,
9
+ type ProcessGuardResponse,
10
+ } from "./process-scope-protocol.js";
11
+ import { processScopeReceiptSchema } from "./process-scope.js";
12
+
13
+ const debug = debuglog("airjam-process-scope");
14
+
15
+ /** Kernel job handles remain in this guardian; EOF or guardian death kills every member. */
16
+ export class WindowsProcessScopes {
17
+ private readonly child: ChildProcess;
18
+ private readonly pending = new Map<
19
+ string,
20
+ {
21
+ resolve: (
22
+ value: Extract<ProcessGuardResponse, { kind: "result" }>,
23
+ ) => void;
24
+ reject: (error: Error) => void;
25
+ timer: ReturnType<typeof setTimeout>;
26
+ }
27
+ >();
28
+ private failure: Error | null = null;
29
+ private readonly ready: Promise<void>;
30
+ private constructor(startupTimeoutMs: number) {
31
+ const started = performance.now();
32
+ let firstOutput = Buffer.alloc(0);
33
+ let diagnosticTail = "";
34
+ const failure = (message: string) =>
35
+ new Error(
36
+ `${message} Elapsed ${Math.round(performance.now() - started)} ms; stdout prefix ${firstOutput.toString("hex") || "none"}; diagnostics: ${diagnosticTail || "none"}`,
37
+ );
38
+ this.child = spawn(
39
+ "pwsh.exe",
40
+ [
41
+ "-NoProfile",
42
+ "-NonInteractive",
43
+ "-Command",
44
+ `& ([scriptblock]::Create([IO.File]::ReadAllText('${resolveDevtoolsHelperScript("process-scope-windows.ps1").replaceAll("'", "''")}')))`,
45
+ ],
46
+ { stdio: ["pipe", "pipe", "pipe"], windowsHide: true },
47
+ );
48
+ this.child.once("spawn", () =>
49
+ debug(
50
+ "PowerShell spawned in %d ms",
51
+ Math.round(performance.now() - started),
52
+ ),
53
+ );
54
+ this.child.stderr?.on("data", (chunk: Buffer) => {
55
+ diagnosticTail = (diagnosticTail + chunk.toString()).slice(-4096);
56
+ debug("%s", chunk.toString().trim());
57
+ });
58
+ this.ready = new Promise<void>((resolve, reject) => {
59
+ const timer = setTimeout(() => {
60
+ const error = failure(
61
+ "process_scope_unavailable: Windows job guardian startup timed out.",
62
+ );
63
+ abort(error);
64
+ }, startupTimeoutMs);
65
+ const abort = (error: Error) => {
66
+ clearTimeout(timer);
67
+ reject(error);
68
+ this.fail(error);
69
+ };
70
+ const output = this.child.stdout;
71
+ if (!output) throw new Error("Windows guardian output is unavailable.");
72
+ output.on("error", abort);
73
+ this.child.stdin?.on("error", abort);
74
+ this.child.stderr?.on("error", abort);
75
+ output.on("data", (chunk: Buffer) => {
76
+ if (firstOutput.length === 0)
77
+ debug(
78
+ "First PowerShell output at %d ms: %s",
79
+ Math.round(performance.now() - started),
80
+ chunk.subarray(0, 64).toString("hex"),
81
+ );
82
+ if (firstOutput.length < 64)
83
+ firstOutput = Buffer.concat([
84
+ firstOutput,
85
+ chunk.subarray(0, 64 - firstOutput.length),
86
+ ]);
87
+ });
88
+ const lines = createInterface({ input: output });
89
+ lines.on("line", (line) => {
90
+ try {
91
+ const result = processGuardResponseSchema.parse(JSON.parse(line));
92
+ if (result.kind === "ready") {
93
+ clearTimeout(timer);
94
+ resolve();
95
+ return;
96
+ }
97
+ if (result.kind === "startup-error") throw new Error(result.message);
98
+ const request = this.pending.get(result.id);
99
+ if (!request) throw new Error("Unknown Windows job acknowledgement.");
100
+ this.pending.delete(result.id);
101
+ clearTimeout(request.timer);
102
+ if (result.kind === "error")
103
+ request.reject(new Error(result.message));
104
+ else request.resolve(result);
105
+ } catch (error) {
106
+ const invalid = failure(
107
+ `process_scope_unavailable: Invalid Windows guardian response ${JSON.stringify(line.slice(0, 256))}: ${error instanceof Error ? error.message : String(error)}`,
108
+ );
109
+ abort(invalid);
110
+ }
111
+ });
112
+ this.child.once("error", (error) =>
113
+ abort(
114
+ failure(
115
+ `process_scope_unavailable: Windows process scopes require PowerShell 7.4+ (pwsh). Install: winget install Microsoft.PowerShell. ${error.message}`,
116
+ ),
117
+ ),
118
+ );
119
+ this.child.once("exit", (code, signal) => {
120
+ const error = failure(
121
+ `process_scope_unavailable: Windows job guardian exited (code ${code}, signal ${signal}).`,
122
+ );
123
+ abort(error);
124
+ });
125
+ });
126
+ }
127
+ static async open(startupTimeoutMs = processGuardStartupTimeoutMs - 2500) {
128
+ const owner = new WindowsProcessScopes(startupTimeoutMs);
129
+ await owner.ready;
130
+ return owner;
131
+ }
132
+ private fail(error: Error) {
133
+ const first = this.failure === null;
134
+ this.failure ??= error;
135
+ for (const request of this.pending.values()) {
136
+ clearTimeout(request.timer);
137
+ request.reject(error);
138
+ }
139
+ this.pending.clear();
140
+ if (first) this.child.kill();
141
+ }
142
+ private async request(input: Record<string, unknown>) {
143
+ await this.ready;
144
+ if (this.failure) throw this.failure;
145
+ const id = randomUUID();
146
+ return new Promise<Extract<ProcessGuardResponse, { kind: "result" }>>(
147
+ (resolve, reject) => {
148
+ const timer = setTimeout(() => {
149
+ this.fail(
150
+ new Error(
151
+ "process_scope_unavailable: Windows job request timed out.",
152
+ ),
153
+ );
154
+ }, 5000);
155
+ this.pending.set(id, { resolve, reject, timer });
156
+ this.child.stdin?.write(
157
+ `${JSON.stringify({ ...input, id })}\n`,
158
+ (error) => {
159
+ if (error) this.fail(error);
160
+ },
161
+ );
162
+ },
163
+ );
164
+ }
165
+ async create(token: string) {
166
+ await this.request({ kind: "open", token });
167
+ }
168
+ async register(token: string, processId: number, root: boolean) {
169
+ await this.request({ kind: "register", token, processId, root });
170
+ }
171
+ async close(token: string) {
172
+ const result = await this.request({ kind: "close", token });
173
+ return processScopeReceiptSchema.parse(result.receipt);
174
+ }
175
+ async shutdown() {
176
+ this.child.stdin?.end();
177
+ await new Promise<void>((resolve) => {
178
+ if (this.child.exitCode !== null) return resolve();
179
+ const timer = setTimeout(() => {
180
+ this.child.kill();
181
+ resolve();
182
+ }, 4000);
183
+ this.child.once("exit", () => {
184
+ clearTimeout(timer);
185
+ resolve();
186
+ });
187
+ });
188
+ }
189
+ }
@@ -0,0 +1,32 @@
1
+ import { z } from "zod";
2
+ import type { AirJamDevMode, PackageJson } from "./types.js";
3
+
4
+ const workspaceSchema = z
5
+ .object({
6
+ cli: z.string().min(1),
7
+ modes: z
8
+ .array(z.enum(["standalone-dev", "arcade-dev", "arcade-test"]))
9
+ .min(1),
10
+ })
11
+ .strict();
12
+
13
+ export const readWorkspaceContract = (packageJson: PackageJson | null) => {
14
+ const declaration = z
15
+ .object({ workspace: workspaceSchema.optional() })
16
+ .passthrough()
17
+ .parse(packageJson?.airjam ?? {});
18
+ return declaration.workspace ?? null;
19
+ };
20
+
21
+ export const assertSupportedDevMode = (
22
+ packageJson: PackageJson | null,
23
+ mode: AirJamDevMode,
24
+ ) => {
25
+ const workspace = readWorkspaceContract(packageJson);
26
+ const supportedModes = workspace?.modes ?? ["standalone-dev"];
27
+ if (!supportedModes.includes(mode)) {
28
+ throw new Error(
29
+ `Mode "${mode}" is not supported by this project. Supported modes: ${supportedModes.join(", ")}.`,
30
+ );
31
+ }
32
+ };