@air-jam/devtools 0.9.3 → 1.0.0-beta.10

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 (263) hide show
  1. package/README.md +60 -0
  2. package/dist/{chunk-VKMJO6VN.js → chunk-2O67AXCU.js} +150 -40
  3. package/dist/chunk-2O67AXCU.js.map +1 -0
  4. package/dist/chunk-AO3ZDXF6.js +142 -0
  5. package/dist/chunk-AO3ZDXF6.js.map +1 -0
  6. package/dist/chunk-ART5TEYT.js +472 -0
  7. package/dist/chunk-ART5TEYT.js.map +1 -0
  8. package/dist/chunk-ASC2CK2R.js +77 -0
  9. package/dist/chunk-ASC2CK2R.js.map +1 -0
  10. package/dist/chunk-E3UZZGAV.js +50 -0
  11. package/dist/chunk-E3UZZGAV.js.map +1 -0
  12. package/dist/chunk-EJTE7UKX.js +64 -0
  13. package/dist/chunk-EJTE7UKX.js.map +1 -0
  14. package/dist/chunk-ESFKNN75.js +110 -0
  15. package/dist/chunk-ESFKNN75.js.map +1 -0
  16. package/dist/chunk-EU7A6PXW.js +1339 -0
  17. package/dist/chunk-EU7A6PXW.js.map +1 -0
  18. package/dist/chunk-FN7E6EA7.js +549 -0
  19. package/dist/chunk-FN7E6EA7.js.map +1 -0
  20. package/dist/chunk-GDFAWHGA.js +190 -0
  21. package/dist/chunk-GDFAWHGA.js.map +1 -0
  22. package/dist/chunk-HZUGBH4Y.js +347 -0
  23. package/dist/chunk-HZUGBH4Y.js.map +1 -0
  24. package/dist/chunk-ITKAOEGX.js +93 -0
  25. package/dist/chunk-ITKAOEGX.js.map +1 -0
  26. package/dist/chunk-JBCUTWRI.js +897 -0
  27. package/dist/chunk-JBCUTWRI.js.map +1 -0
  28. package/dist/chunk-JSI5S4WP.js +1174 -0
  29. package/dist/chunk-JSI5S4WP.js.map +1 -0
  30. package/dist/chunk-L4D675BG.js +97 -0
  31. package/dist/chunk-L4D675BG.js.map +1 -0
  32. package/dist/{chunk-GUEZP773.js → chunk-LB3Y64P5.js} +206 -60
  33. package/dist/chunk-LB3Y64P5.js.map +1 -0
  34. package/dist/chunk-M6CJ7GQC.js +133 -0
  35. package/dist/chunk-M6CJ7GQC.js.map +1 -0
  36. package/dist/{chunk-CKFT2ZJE.js → chunk-MEIMOSZC.js} +53 -105
  37. package/dist/chunk-MEIMOSZC.js.map +1 -0
  38. package/dist/chunk-OM6SCJ7H.js +18 -0
  39. package/dist/chunk-OM6SCJ7H.js.map +1 -0
  40. package/dist/chunk-PKD4WVX5.js +63 -0
  41. package/dist/chunk-PKD4WVX5.js.map +1 -0
  42. package/dist/chunk-PMBLXGFM.js +3384 -0
  43. package/dist/chunk-PMBLXGFM.js.map +1 -0
  44. package/dist/chunk-PMNE5GIN.js +279 -0
  45. package/dist/chunk-PMNE5GIN.js.map +1 -0
  46. package/dist/chunk-QAWI64VR.js +148 -0
  47. package/dist/chunk-QAWI64VR.js.map +1 -0
  48. package/dist/chunk-QGADSPHZ.js +449 -0
  49. package/dist/chunk-QGADSPHZ.js.map +1 -0
  50. package/dist/chunk-SPYN66UV.js +338 -0
  51. package/dist/chunk-SPYN66UV.js.map +1 -0
  52. package/dist/context.d.ts +9 -3
  53. package/dist/context.js +2 -1
  54. package/dist/controller.d.ts +33 -7
  55. package/dist/controller.js +18 -8
  56. package/dist/dependencies.d.ts +44 -0
  57. package/dist/dependencies.js +10 -0
  58. package/dist/dev.d.ts +9 -3
  59. package/dist/dev.js +3 -4
  60. package/dist/game-builds.d.ts +200 -0
  61. package/dist/game-builds.js +16 -0
  62. package/dist/game-builds.js.map +1 -0
  63. package/dist/game-run-watch-CEd_SpAT.d.ts +84 -0
  64. package/dist/game-runs.d.ts +1452 -0
  65. package/dist/game-runs.js +76 -0
  66. package/dist/game-runs.js.map +1 -0
  67. package/dist/game-session-broker-CUvORfMd.d.ts +439 -0
  68. package/dist/game-session-broker.d.ts +4 -57
  69. package/dist/game-session-broker.js +33 -228
  70. package/dist/game-session-broker.js.map +1 -1
  71. package/dist/game-session.d.ts +3931 -10
  72. package/dist/game-session.js +186 -15
  73. package/dist/game-session.js.map +1 -1
  74. package/dist/game-settings.d.ts +16 -0
  75. package/dist/game-settings.js +237 -0
  76. package/dist/game-settings.js.map +1 -0
  77. package/dist/games.d.ts +11 -4
  78. package/dist/games.js +4 -2
  79. package/dist/harness/visual.d.ts +34 -151
  80. package/dist/harness/visual.js +6 -1
  81. package/dist/logs.d.ts +8 -2
  82. package/dist/logs.js +3 -4
  83. package/dist/logs.js.map +1 -1
  84. package/dist/managed-process-scope-BicYi_t9.d.ts +3 -0
  85. package/dist/mcp-config.js +2 -1
  86. package/dist/mcp-config.js.map +1 -1
  87. package/dist/platform-auth.d.ts +8 -111
  88. package/dist/platform-auth.js +9 -1
  89. package/dist/platform-game-analytics.d.ts +51 -0
  90. package/dist/platform-game-analytics.js +49 -0
  91. package/dist/platform-game-analytics.js.map +1 -0
  92. package/dist/platform-game-app-id.d.ts +46 -0
  93. package/dist/platform-game-app-id.js +93 -0
  94. package/dist/platform-game-app-id.js.map +1 -0
  95. package/dist/platform-game-media.d.ts +40 -2
  96. package/dist/platform-game-media.js +1 -1
  97. package/dist/platform-games.d.ts +24 -2
  98. package/dist/platform-games.js +16 -84
  99. package/dist/platform-games.js.map +1 -1
  100. package/dist/quality.d.ts +9 -3
  101. package/dist/quality.js +3 -4
  102. package/dist/quality.js.map +1 -1
  103. package/dist/release.d.ts +226 -16
  104. package/dist/release.js +18 -1331
  105. package/dist/release.js.map +1 -1
  106. package/dist/rooms-check.d.ts +90 -0
  107. package/dist/rooms-check.js +717 -0
  108. package/dist/rooms-check.js.map +1 -0
  109. package/dist/source-tree-contract.d.ts +16 -0
  110. package/dist/source-tree-contract.js +13 -0
  111. package/dist/source-tree-contract.js.map +1 -0
  112. package/dist/testing.d.ts +464 -0
  113. package/dist/testing.js +27 -0
  114. package/dist/testing.js.map +1 -0
  115. package/dist/tooling/describe-game.js +54 -0
  116. package/dist/tooling/describe-game.js.map +1 -0
  117. package/dist/tooling/game-session-broker.js +28 -0
  118. package/dist/tooling/game-session-broker.js.map +1 -0
  119. package/dist/tooling/hold-runtime-host.js +770 -68
  120. package/dist/tooling/hold-runtime-host.js.map +1 -1
  121. package/dist/tooling/list-visual-scenarios.js +18 -55
  122. package/dist/tooling/list-visual-scenarios.js.map +1 -1
  123. package/dist/tooling/process-scope-bootstrap.mjs +24 -0
  124. package/dist/tooling/process-scope-guard.d.ts +2 -0
  125. package/dist/tooling/process-scope-guard.js +388 -0
  126. package/dist/tooling/process-scope-guard.js.map +1 -0
  127. package/dist/tooling/process-scope-windows.ps1 +148 -0
  128. package/dist/tooling/rooms-check-device.d.ts +2 -0
  129. package/dist/tooling/rooms-check-device.js +498 -0
  130. package/dist/tooling/rooms-check-device.js.map +1 -0
  131. package/dist/tooling/run-headless-room.d.ts +2 -0
  132. package/dist/tooling/run-headless-room.js +288 -0
  133. package/dist/tooling/run-headless-room.js.map +1 -0
  134. package/dist/tooling/run-test-profile.d.ts +2 -0
  135. package/dist/tooling/run-test-profile.js +181 -0
  136. package/dist/tooling/run-test-profile.js.map +1 -0
  137. package/dist/tooling/run-visual-capture.js +70 -34
  138. package/dist/tooling/run-visual-capture.js.map +1 -1
  139. package/dist/tooling/seal-game-build.d.ts +2 -0
  140. package/dist/tooling/seal-game-build.js +120 -0
  141. package/dist/tooling/seal-game-build.js.map +1 -0
  142. package/dist/{types-C6NrHt88.d.ts → types-CdYrgrG_.d.ts} +337 -76
  143. package/dist/types-DyRD-ym1.d.ts +141 -0
  144. package/dist/visual.d.ts +9 -3
  145. package/dist/visual.js +24 -68
  146. package/dist/visual.js.map +1 -1
  147. package/package.json +51 -14
  148. package/src/bounded-response.ts +28 -0
  149. package/src/built-game-runtime.ts +152 -0
  150. package/src/context.ts +2 -13
  151. package/src/controller-protocol.ts +22 -0
  152. package/src/controller.ts +747 -597
  153. package/src/current-room-host.ts +166 -0
  154. package/src/decision-provider.ts +102 -0
  155. package/src/dependencies.ts +176 -0
  156. package/src/dependency-install-contract.ts +12 -0
  157. package/src/dev.ts +46 -50
  158. package/src/game-action-observation.ts +11 -1
  159. package/src/game-build-protocol.ts +72 -0
  160. package/src/game-build.ts +483 -0
  161. package/src/game-run-owner.ts +2358 -0
  162. package/src/game-run-protocol.ts +448 -0
  163. package/src/game-run-record.ts +92 -0
  164. package/src/game-run-replay.ts +95 -0
  165. package/src/game-run-watch.ts +155 -0
  166. package/src/game-session-broker.ts +689 -197
  167. package/src/game-session-operations.ts +61 -0
  168. package/src/game-session-protocol.ts +180 -0
  169. package/src/game-session-runtime.ts +861 -0
  170. package/src/game-session.ts +164 -417
  171. package/src/game-settings.ts +295 -0
  172. package/src/games.ts +88 -92
  173. package/src/harness/visual/browser.ts +100 -0
  174. package/src/harness/visual/controller-observation.ts +181 -0
  175. package/src/harness/visual/runner.ts +24 -21
  176. package/src/harness/visual/runtime-controller-observations.ts +151 -0
  177. package/src/harness/visual/scenario-contract.ts +1 -2
  178. package/src/harness/visual/scenario-helpers.ts +5 -1
  179. package/src/harness/visual/screencast.ts +155 -0
  180. package/src/harness/visual/session.ts +74 -69
  181. package/src/harness/visual/types.ts +8 -5
  182. package/src/headless-room-protocol.ts +112 -0
  183. package/src/headless-room.ts +222 -0
  184. package/src/helper-channel.ts +229 -0
  185. package/src/helper-scripts.ts +25 -5
  186. package/src/installed-dependencies.ts +74 -0
  187. package/src/managed-process-scope.ts +481 -0
  188. package/src/node-process-environment.ts +10 -0
  189. package/src/platform-auth.ts +194 -55
  190. package/src/platform-game-analytics.ts +50 -0
  191. package/src/platform-game-app-id.ts +99 -0
  192. package/src/platform-games.ts +13 -111
  193. package/src/policy-artifact-contract.ts +52 -0
  194. package/src/policy-artifacts.ts +134 -0
  195. package/src/process-scope-admission.ts +50 -0
  196. package/src/process-scope-protocol.ts +66 -0
  197. package/src/process-scope.ts +327 -0
  198. package/src/release.ts +28 -42
  199. package/src/rooms-check-fleet.ts +194 -0
  200. package/src/rooms-check-link.ts +138 -0
  201. package/src/rooms-check-protocol.ts +244 -0
  202. package/src/rooms-check.ts +797 -0
  203. package/src/runtime-owner-protocol.ts +106 -18
  204. package/src/source-tree-contract.ts +70 -0
  205. package/src/source-tree.ts +84 -0
  206. package/src/test-profile-process.ts +153 -0
  207. package/src/test-profile-protocol.ts +116 -0
  208. package/src/testing.ts +430 -0
  209. package/src/tooling/declaration-hook.ts +52 -0
  210. package/src/tooling/describe-game.ts +49 -0
  211. package/src/tooling/game-session-broker.ts +7 -0
  212. package/src/tooling/hold-runtime-host.ts +254 -42
  213. package/src/tooling/list-visual-scenarios.ts +19 -62
  214. package/src/tooling/load-game.ts +79 -0
  215. package/src/tooling/process-scope-bootstrap.mjs +24 -0
  216. package/src/tooling/process-scope-guard.ts +213 -0
  217. package/src/tooling/process-scope-windows.ps1 +148 -0
  218. package/src/tooling/rooms-check-device.ts +493 -0
  219. package/src/tooling/run-headless-room.ts +329 -0
  220. package/src/tooling/run-test-profile.ts +190 -0
  221. package/src/tooling/run-visual-capture.ts +37 -23
  222. package/src/tooling/runtime-owner-actions.ts +23 -0
  223. package/src/tooling/seal-game-build.ts +118 -0
  224. package/src/tooling/visual-pack.ts +10 -45
  225. package/src/types.ts +54 -77
  226. package/src/visual.ts +14 -22
  227. package/src/windows-process-scopes.ts +189 -0
  228. package/dist/agent.d.ts +0 -15
  229. package/dist/agent.js +0 -19
  230. package/dist/chunk-5YAARMW5.js +0 -36
  231. package/dist/chunk-5YAARMW5.js.map +0 -1
  232. package/dist/chunk-6WQODT37.js +0 -1019
  233. package/dist/chunk-6WQODT37.js.map +0 -1
  234. package/dist/chunk-CKFT2ZJE.js.map +0 -1
  235. package/dist/chunk-ER3Q2C4G.js +0 -358
  236. package/dist/chunk-ER3Q2C4G.js.map +0 -1
  237. package/dist/chunk-GUEZP773.js.map +0 -1
  238. package/dist/chunk-HIDNI56E.js +0 -33
  239. package/dist/chunk-HIDNI56E.js.map +0 -1
  240. package/dist/chunk-QKXS5D35.js +0 -43
  241. package/dist/chunk-QKXS5D35.js.map +0 -1
  242. package/dist/chunk-S4LSCEVE.js +0 -261
  243. package/dist/chunk-S4LSCEVE.js.map +0 -1
  244. package/dist/chunk-VKMJO6VN.js.map +0 -1
  245. package/dist/chunk-WFUR3MXY.js +0 -67
  246. package/dist/chunk-WFUR3MXY.js.map +0 -1
  247. package/dist/chunk-XEMR7KUE.js +0 -361
  248. package/dist/chunk-XEMR7KUE.js.map +0 -1
  249. package/dist/chunk-ZLPD7FY5.js +0 -191
  250. package/dist/chunk-ZLPD7FY5.js.map +0 -1
  251. package/dist/tooling/agent-contract.js +0 -193
  252. package/dist/tooling/agent-contract.js.map +0 -1
  253. package/dist/tooling/inspect-airjam-agent.js +0 -37
  254. package/dist/tooling/inspect-airjam-agent.js.map +0 -1
  255. package/src/agent.ts +0 -380
  256. package/src/tooling/agent-contract.ts +0 -232
  257. package/src/tooling/agent-declaration.ts +0 -36
  258. package/src/tooling/airjam-agent-inspection.ts +0 -45
  259. package/src/tooling/airjam-agent.ts +0 -118
  260. package/src/tooling/inspect-airjam-agent.ts +0 -20
  261. /package/dist/{agent.js.map → dependencies.js.map} +0 -0
  262. /package/dist/tooling/{agent-contract.d.ts → describe-game.d.ts} +0 -0
  263. /package/dist/tooling/{inspect-airjam-agent.d.ts → game-session-broker.d.ts} +0 -0
@@ -0,0 +1,329 @@
1
+ /**
2
+ * One headless room: the Air Jam room server on a loopback port, which
3
+ * phones and agents join over the network, and the game's host core joined
4
+ * straight into its rooms over the loopback link, with no page and no
5
+ * drawing. The game's clock moves only when the owner advances it, so a test
6
+ * plays on game time, as fast as the host runs.
7
+ */
8
+ import {
9
+ airJamInputLogSchema,
10
+ createAirJamConfig,
11
+ createConnectionStore,
12
+ replayAirJamInputLog,
13
+ type AirJamHostEnvironment,
14
+ } from "@air-jam/sdk/core";
15
+ import { AIRJAM_PRESENTATION_CHANNEL } from "@air-jam/sdk/room-protocol";
16
+ import {
17
+ createLoopbackConnector,
18
+ createRoomKey,
19
+ HostRoomSession,
20
+ } from "@air-jam/sdk/room-session";
21
+ import {
22
+ AIR_JAM_RUNTIME_CONTROL_KEY,
23
+ type HostRuntimeControl,
24
+ } from "@air-jam/sdk/runtime-control";
25
+ import {
26
+ resolveRuntimeTopology,
27
+ runtimeTopologyToQueryParams,
28
+ } from "@air-jam/sdk/runtime-topology";
29
+ import { loadServerEnv } from "@air-jam/server";
30
+ import { createRoomServer } from "@air-jam/server/room-server";
31
+ import { createHash } from "node:crypto";
32
+ import { readFile, writeFile } from "node:fs/promises";
33
+ import {
34
+ HEADLESS_ROOM_IPC_MAX_BYTES,
35
+ headlessRoomRequestSchema,
36
+ type HeadlessRoomResponse,
37
+ } from "../headless-room-protocol.js";
38
+ import { loadAirJamGame } from "./load-game.js";
39
+
40
+ const STARTUP_MS = 10_000;
41
+
42
+ type Room = {
43
+ agent: HostRuntimeControl["agent"];
44
+ /** Null for a replay, which has already played. */
45
+ advance: ((ms: number) => number) | null;
46
+ saveInputLog: ((path: string) => Promise<SavedInputLog>) | null;
47
+ close: () => Promise<void>;
48
+ };
49
+
50
+ type SavedInputLog = {
51
+ sha256: string;
52
+ ticks: number;
53
+ stepMs: number;
54
+ entries: number;
55
+ full: boolean;
56
+ };
57
+
58
+ let room: Room | undefined;
59
+ let initializing = false;
60
+ let busy = false;
61
+
62
+ function send(message: HeadlessRoomResponse) {
63
+ if (process.connected) process.send?.(message);
64
+ }
65
+
66
+ async function openRoom(
67
+ configPath: string,
68
+ seed: number,
69
+ settings?: Record<string, string | number | boolean>,
70
+ ) {
71
+ const app = await loadAirJamGame(configPath);
72
+ const server = createRoomServer({
73
+ host: "127.0.0.1",
74
+ port: 0,
75
+ envConfig: loadServerEnv({
76
+ NODE_ENV: "test",
77
+ AIR_JAM_AUTH_MODE: "disabled",
78
+ }),
79
+ });
80
+ const origin = `http://127.0.0.1:${await server.start()}`;
81
+ const topology = (surfaceRole: "host" | "controller") =>
82
+ resolveRuntimeTopology({
83
+ runtimeMode: "self-hosted-production",
84
+ surfaceRole,
85
+ appOrigin: origin,
86
+ backendOrigin: origin,
87
+ socketOrigin: origin,
88
+ publicHost: origin,
89
+ embedded: false,
90
+ proxyStrategy: "none",
91
+ secureTransport: false,
92
+ });
93
+ const connector = createLoopbackConnector(server.rooms);
94
+ let nowMs = 0;
95
+ const controlTarget = {};
96
+ const environment: AirJamHostEnvironment = {
97
+ embedded: null,
98
+ requestedRoomId: null,
99
+ runMode: "standalone",
100
+ defaultStoreDomain: "default",
101
+ presentationChannel: AIRJAM_PRESENTATION_CHANNEL,
102
+ controlTarget,
103
+ openRoom: (options) =>
104
+ new HostRoomSession({
105
+ ...options,
106
+ connector,
107
+ deviceWakeEvents: null,
108
+ heartbeat: null,
109
+ }),
110
+ createKey: createRoomKey,
111
+ readResume: () => null,
112
+ writeResume: () => {},
113
+ requestHostGrant: () =>
114
+ Promise.reject(new Error("A headless room runs without host grants.")),
115
+ controllerUrls: async (roomId) => {
116
+ const url = new URL(app.controllerPath, origin);
117
+ url.searchParams.set("room", roomId);
118
+ for (const [key, value] of Object.entries(
119
+ runtimeTopologyToQueryParams(topology("controller")),
120
+ ))
121
+ url.searchParams.set(key, value);
122
+ return { localControllerUrl: url.toString(), joinUrl: "" };
123
+ },
124
+ reportError: () => {},
125
+ emitDevEvent: () => {},
126
+ setDevLogContext: () => {},
127
+ seed: () => seed,
128
+ now: () => nowMs,
129
+ };
130
+ const store = createConnectionStore();
131
+ // The game's own SDK builds its host, so its stores and host share one runtime.
132
+ const runtime = app.createHostRuntime(
133
+ {
134
+ config: createAirJamConfig({
135
+ topology: topology("host"),
136
+ maxPlayers: app.metadata.maxPlayers,
137
+ }),
138
+ store,
139
+ environment,
140
+ },
141
+ // A game with a simulation keeps the input log that replays the run.
142
+ { seed, settings },
143
+ );
144
+ const stop = runtime.start();
145
+ const close = async () => {
146
+ stop();
147
+ await server.stop();
148
+ };
149
+ try {
150
+ await runtime.ready;
151
+ const joined = await new Promise<{
152
+ roomId: string;
153
+ localControllerUrl: string;
154
+ }>((resolve, reject) => {
155
+ const timer = setTimeout(
156
+ () => reject(new Error("room_startup_timeout: The host never joined.")),
157
+ STARTUP_MS,
158
+ );
159
+ const check = () => {
160
+ const state = store.getState();
161
+ if (
162
+ state.connectionStatus === "connected" &&
163
+ state.roomId &&
164
+ state.localControllerUrl
165
+ ) {
166
+ clearTimeout(timer);
167
+ unsubscribe();
168
+ resolve({
169
+ roomId: state.roomId,
170
+ localControllerUrl: state.localControllerUrl,
171
+ });
172
+ } else if (state.lastError) {
173
+ clearTimeout(timer);
174
+ unsubscribe();
175
+ reject(new Error(`room_unavailable: ${state.lastError}`));
176
+ }
177
+ };
178
+ const unsubscribe = store.subscribe(check);
179
+ check();
180
+ });
181
+ room = {
182
+ agent: (request) => {
183
+ const control = Reflect.get(
184
+ controlTarget,
185
+ AIR_JAM_RUNTIME_CONTROL_KEY,
186
+ ) as HostRuntimeControl | undefined;
187
+ if (!control)
188
+ throw new Error(
189
+ "room_unavailable: The game publishes nothing agents can read.",
190
+ );
191
+ return control.agent(request);
192
+ },
193
+ advance: (ms) => {
194
+ nowMs += ms;
195
+ runtime.simulation?.advance(ms);
196
+ return nowMs;
197
+ },
198
+ saveInputLog: app.simulation
199
+ ? async (path) => {
200
+ const log = runtime.inputLog();
201
+ const source = `${JSON.stringify(log)}\n`;
202
+ await writeFile(path, source, { mode: 0o600 });
203
+ return {
204
+ sha256: createHash("sha256").update(source).digest("hex"),
205
+ ticks: log.ticks,
206
+ stepMs: log.stepMs,
207
+ entries: log.entries.length,
208
+ full: runtime.inputLogFull,
209
+ };
210
+ }
211
+ : null,
212
+ close,
213
+ };
214
+ return {
215
+ gameId: app.metadata.slug,
216
+ roomId: joined.roomId,
217
+ controllerJoinUrl: joined.localControllerUrl,
218
+ nowMs,
219
+ recordsInputLog: Boolean(app.simulation),
220
+ };
221
+ } catch (error) {
222
+ await close();
223
+ throw error;
224
+ }
225
+ }
226
+
227
+ async function replayRoom(configPath: string, logPath: string) {
228
+ const app = await loadAirJamGame(configPath);
229
+ const log = airJamInputLogSchema.parse(
230
+ JSON.parse(await readFile(logPath, "utf8")),
231
+ );
232
+ const replayed = await replayAirJamInputLog(app.createHostRuntime, log);
233
+ room = {
234
+ agent: replayed.agent,
235
+ advance: null,
236
+ saveInputLog: null,
237
+ close: async () => replayed.stop(),
238
+ };
239
+ return {
240
+ gameId: app.metadata.slug,
241
+ roomId: replayed.roomId,
242
+ ticks: log.ticks,
243
+ };
244
+ }
245
+
246
+ process.on("disconnect", () => {
247
+ void (room?.close() ?? Promise.resolve()).finally(() => process.exit(0));
248
+ });
249
+ process.on("message", (message: unknown) => {
250
+ void (async () => {
251
+ if (
252
+ Buffer.byteLength(JSON.stringify(message)) > HEADLESS_ROOM_IPC_MAX_BYTES
253
+ )
254
+ throw new Error("Oversized headless room request.");
255
+ const request = headlessRoomRequestSchema.parse(message);
256
+ if (busy)
257
+ return send({ kind: "error", id: request.id, message: "room_busy" });
258
+ busy = true;
259
+ try {
260
+ if (request.kind === "initialize") {
261
+ if (room || initializing)
262
+ throw new Error("The headless room is already open.");
263
+ initializing = true;
264
+ send({
265
+ kind: "initialized",
266
+ id: request.id,
267
+ ...(await openRoom(
268
+ request.configPath,
269
+ request.seed,
270
+ request.settings,
271
+ )),
272
+ });
273
+ return;
274
+ }
275
+ if (request.kind === "replay") {
276
+ if (room || initializing)
277
+ throw new Error("The headless room is already open.");
278
+ initializing = true;
279
+ send({
280
+ kind: "replayed",
281
+ id: request.id,
282
+ ...(await replayRoom(request.configPath, request.logPath)),
283
+ });
284
+ return;
285
+ }
286
+ if (!room) throw new Error("The headless room is not open.");
287
+ if (request.kind === "agent")
288
+ send({
289
+ kind: "agent",
290
+ id: request.id,
291
+ response: await room.agent(request.request),
292
+ });
293
+ else if (request.kind === "saveInputLog") {
294
+ if (!room.saveInputLog)
295
+ throw new Error(
296
+ "no_input_log: This room keeps no input log; its game declares no simulation.",
297
+ );
298
+ send({
299
+ kind: "savedInputLog",
300
+ id: request.id,
301
+ ...(await room.saveInputLog(request.path)),
302
+ });
303
+ } else {
304
+ if (!room.advance)
305
+ throw new Error("replay_finished: A replay has already played.");
306
+ send({
307
+ kind: "advanced",
308
+ id: request.id,
309
+ nowMs: room.advance(request.ms),
310
+ });
311
+ }
312
+ } catch (error) {
313
+ send({
314
+ kind: "error",
315
+ id: request.id,
316
+ message: (error instanceof Error ? error.message : String(error)).slice(
317
+ 0,
318
+ 2000,
319
+ ),
320
+ });
321
+ } finally {
322
+ busy = false;
323
+ }
324
+ })().catch((error) => {
325
+ console.error(error);
326
+ process.exit(1);
327
+ });
328
+ });
329
+ send({ kind: "ready" });
@@ -0,0 +1,190 @@
1
+ import {
2
+ TEST_PROFILE_IPC_MAX_BYTES,
3
+ createPolicyRandom,
4
+ testProfileRequestSchema,
5
+ type TestProfileResponse,
6
+ } from "../test-profile-protocol.js";
7
+ import {
8
+ PLAY_RUN_PLAYER,
9
+ airJamPolicyDecisionSchema,
10
+ airJamScenarioVerdictSchema,
11
+ createAirJamPlayerPolicy,
12
+ describeAirJamTestProfiles,
13
+ policyPreparationSchema,
14
+ type AirJamRunReceipt,
15
+ type AirJamTestPolicy,
16
+ type AirJamTestProfile,
17
+ } from "../testing.js";
18
+ import { loadAirJamGame, loadAirJamTesting } from "./load-game.js";
19
+
20
+ let role: "discover" | "policy" | "evaluator" | undefined;
21
+ let busy = false;
22
+ let profile: AirJamTestProfile | undefined;
23
+ let evaluator: ReturnType<AirJamTestProfile["evaluator"]["create"]> | undefined;
24
+ let evaluationIndex = 0;
25
+ const policySequences = new Map<string, number>();
26
+ const policies = new Map<
27
+ string,
28
+ Awaited<ReturnType<AirJamTestPolicy["create"]>>
29
+ >();
30
+ function send(message: TestProfileResponse) {
31
+ if (process.connected) process.send?.(message);
32
+ }
33
+ function acceptReceipts(receipts: AirJamRunReceipt[], seatId?: string) {
34
+ let previous =
35
+ seatId === undefined ? evaluationIndex : (policySequences.get(seatId) ?? 0);
36
+ for (const receipt of receipts) {
37
+ const next = seatId === undefined ? receipt.index : receipt.sequence;
38
+ if (
39
+ next !== previous + 1 ||
40
+ (seatId !== undefined && receipt.seatId !== seatId)
41
+ )
42
+ throw new Error(
43
+ "receipt_protocol: Missing, repeated or misdirected effect receipt.",
44
+ );
45
+ previous = next;
46
+ }
47
+ if (seatId === undefined) evaluationIndex = previous;
48
+ else policySequences.set(seatId, previous);
49
+ }
50
+ process.on("disconnect", () => process.exit(0));
51
+ process.on("message", (message: unknown) => {
52
+ void (async () => {
53
+ if (Buffer.byteLength(JSON.stringify(message)) > TEST_PROFILE_IPC_MAX_BYTES)
54
+ throw new Error("Oversized test profile request.");
55
+ const request = testProfileRequestSchema.parse(message);
56
+ if (busy)
57
+ return send({ kind: "error", id: request.id, message: "policy_busy" });
58
+ busy = true;
59
+ try {
60
+ if (request.kind === "initialize") {
61
+ if (role !== undefined)
62
+ throw new Error("Test profile already initialized.");
63
+ const game = await loadAirJamGame(request.configPath);
64
+ const testing = await loadAirJamTesting(request.configPath, game);
65
+ if (!testing)
66
+ throw new Error(
67
+ request.runKind === "play"
68
+ ? "This game has no player contract; it cannot seat a model player."
69
+ : "Game does not declare testing.",
70
+ );
71
+ const profiles = describeAirJamTestProfiles(testing.profiles);
72
+ const preparation = policyPreparationSchema.parse([]);
73
+ const artifacts = request.artifacts;
74
+ role = request.role;
75
+ if (role !== "policy" && artifacts !== undefined)
76
+ throw new Error(
77
+ "Only the policy process can receive model artifacts.",
78
+ );
79
+ if (request.runKind === "play") {
80
+ if (role !== "policy" || artifacts)
81
+ throw new Error("Play initializes only its player policy.");
82
+ if (!testing.player)
83
+ throw new Error(
84
+ "This game has no player contract; it cannot seat a model player.",
85
+ );
86
+ const started = performance.now();
87
+ const instance = await createAirJamPlayerPolicy(
88
+ testing.player,
89
+ ).create({
90
+ seatId: PLAY_RUN_PLAYER.seats[0].id,
91
+ random: createPolicyRandom(request.seed),
92
+ });
93
+ policies.set(PLAY_RUN_PLAYER.seats[0].id, instance);
94
+ preparation.push({
95
+ seatId: PLAY_RUN_PLAYER.seats[0].id,
96
+ prepareMs: performance.now() - started,
97
+ });
98
+ } else if (role !== "discover") {
99
+ profile = testing.profiles[request.profileId ?? ""];
100
+ if (!profile) throw new Error("Unknown test profile.");
101
+ if (role === "policy") {
102
+ const policy = profile.policies[request.policyId ?? ""];
103
+ if (!policy) throw new Error("Unknown test policy.");
104
+ if (Boolean(policy.artifacts) !== Boolean(artifacts))
105
+ throw new Error(
106
+ "Policy artifacts must be verified by the run owner before initialization.",
107
+ );
108
+ for (const [index, seat] of profile.seats.entries()) {
109
+ const started = performance.now();
110
+ const instance = await policy.create({
111
+ seatId: seat.id,
112
+ random: createPolicyRandom(
113
+ (request.seed + Math.imul(index, 0x9e3779b9)) >>> 0,
114
+ ),
115
+ ...(artifacts ? { artifacts } : {}),
116
+ });
117
+ if (typeof instance.decide !== "function")
118
+ throw new Error("Policy must provide decide().");
119
+ preparation.push({
120
+ seatId: seat.id,
121
+ prepareMs: performance.now() - started,
122
+ });
123
+ policies.set(seat.id, instance);
124
+ }
125
+ } else {
126
+ evaluator = profile.evaluator.create({
127
+ seats: profile.seats.map((seat) => seat.id),
128
+ });
129
+ if (typeof evaluator.evaluate !== "function")
130
+ throw new Error("Scenario must provide evaluate().");
131
+ }
132
+ }
133
+ send({
134
+ kind: "initialized",
135
+ id: request.id,
136
+ profiles,
137
+ preparation,
138
+ });
139
+ } else if (request.kind === "decide") {
140
+ const policy = policies.get(request.step.seatId);
141
+ if (role !== "policy" || !policy)
142
+ throw new Error("Policy seat is not initialized.");
143
+ acceptReceipts(request.step.receipts, request.step.seatId);
144
+ const decision = airJamPolicyDecisionSchema.parse(
145
+ await policy.decide(request.step),
146
+ );
147
+ send({ kind: "decision", id: request.id, decision });
148
+ } else {
149
+ if (role !== "evaluator" || !evaluator || !profile)
150
+ throw new Error("Scenario evaluator is not initialized.");
151
+ const activeProfile = profile;
152
+ if (
153
+ request.sample.seats.length !== activeProfile.seats.length ||
154
+ request.sample.seats.some(
155
+ (seat, index) => seat.seatId !== activeProfile.seats[index].id,
156
+ )
157
+ )
158
+ throw new Error(
159
+ "Evaluation seat mapping does not match the scenario.",
160
+ );
161
+ acceptReceipts(request.sample.receipts);
162
+ const verdict = airJamScenarioVerdictSchema.parse(
163
+ evaluator.evaluate(request.sample),
164
+ );
165
+ if (
166
+ Object.keys(verdict.metrics ?? {}).some(
167
+ (id) => !activeProfile.metrics.some((metric) => metric.id === id),
168
+ )
169
+ )
170
+ throw new Error("Evaluator returned an undeclared metric.");
171
+ send({ kind: "evaluation", id: request.id, verdict });
172
+ }
173
+ } catch (error) {
174
+ send({
175
+ kind: "error",
176
+ id: request.id,
177
+ message: (error instanceof Error ? error.message : String(error)).slice(
178
+ 0,
179
+ 2000,
180
+ ),
181
+ });
182
+ } finally {
183
+ busy = false;
184
+ }
185
+ })().catch((error) => {
186
+ console.error(error);
187
+ process.exit(1);
188
+ });
189
+ });
190
+ send({ kind: "ready" });
@@ -1,18 +1,15 @@
1
- import {
2
- closeGameSession,
3
- invokeGameSessionAction,
4
- openGameSession,
5
- readGameSession,
6
- } from "../game-session.js";
1
+ import { createGameSessionRuntime } from "../game-session-runtime.js";
7
2
  import type {
8
3
  AnyAirJamAgentContract,
9
4
  VisualHarnessMode,
5
+ VisualHarnessPageSurface,
10
6
  VisualScenarioAgent,
11
7
  VisualScenarioAgentInvocation,
12
8
  VisualScenarioPack,
13
9
  } from "../harness/visual.js";
14
10
  import { runVisualHarness } from "../harness/visual.js";
15
- import { loadVisualScenarioPackFromModuleOrConfig } from "./visual-pack.js";
11
+ import { evaluateOwnedGameContract } from "./runtime-owner-actions.js";
12
+ import { loadVisualScenarioPackFromConfig } from "./visual-pack.js";
16
13
 
17
14
  const getFlagValue = (flag: string): string | null => {
18
15
  const inline = process.argv.find((value) => value.startsWith(`${flag}=`));
@@ -24,7 +21,6 @@ const getFlagValue = (flag: string): string | null => {
24
21
  return index === -1 ? null : (process.argv[index + 1] ?? null);
25
22
  };
26
23
 
27
- const modulePath = getFlagValue("--module-path");
28
24
  const configPath = getFlagValue("--config");
29
25
  const artifactRoot = getFlagValue("--artifact-root");
30
26
  const hostUrl = getFlagValue("--host-url");
@@ -39,7 +35,7 @@ const gameId = getFlagValue("--game-id");
39
35
  const secure = process.argv.includes("--secure");
40
36
 
41
37
  if (
42
- (!modulePath && !configPath) ||
38
+ !configPath ||
43
39
  !artifactRoot ||
44
40
  !hostUrl ||
45
41
  !appOrigin ||
@@ -48,14 +44,19 @@ if (
48
44
  !gameId
49
45
  ) {
50
46
  throw new Error(
51
- "Missing required visual capture inputs. Expected game id, module path, artifact root, and resolved topology URLs.",
47
+ "Missing required visual capture inputs. Expected game id, config path, artifact root, and resolved topology URLs.",
52
48
  );
53
49
  }
54
50
 
55
- const scenarioPack = (await loadVisualScenarioPackFromModuleOrConfig({
56
- modulePath,
51
+ const scenarioPack = (await loadVisualScenarioPackFromConfig(
57
52
  configPath,
58
- })) as VisualScenarioPack<AnyAirJamAgentContract>;
53
+ )) as VisualScenarioPack<AnyAirJamAgentContract>;
54
+ const {
55
+ closeGameSession,
56
+ invokeGameSessionAction,
57
+ openGameSession,
58
+ readGameSession,
59
+ } = createGameSessionRuntime();
59
60
  const mode =
60
61
  requestedMode === "arcade-test" ? "arcade-built" : "standalone-dev";
61
62
 
@@ -67,28 +68,35 @@ const asSnapshotRecord = (snapshot: unknown): Record<string, unknown> | null =>
67
68
  const createScenarioAgentSession = async ({
68
69
  gameId,
69
70
  urls,
71
+ host,
70
72
  mode,
71
73
  secure,
72
74
  }: {
73
75
  gameId: string;
76
+ host: VisualHarnessPageSurface;
74
77
  mode: VisualHarnessMode;
75
78
  secure: boolean;
76
79
  urls: {
77
80
  controllerJoinUrl: string;
78
81
  };
79
82
  }): Promise<VisualScenarioAgent> => {
80
- const opened = await openGameSession({
81
- cwd: process.cwd(),
82
- gameId,
83
- mode: mode === "arcade-built" ? "arcade-test" : "standalone-dev",
84
- secure,
85
- controllerJoinUrl: urls.controllerJoinUrl,
86
- });
83
+ const opened = await openGameSession(
84
+ {
85
+ cwd: process.cwd(),
86
+ gameId,
87
+ mode: mode === "arcade-built" ? "arcade-test" : "standalone-dev",
88
+ secure,
89
+ controllerJoinUrl: urls.controllerJoinUrl,
90
+ },
91
+ {
92
+ access: "owned-host",
93
+ request: (request) => evaluateOwnedGameContract(host, request),
94
+ },
95
+ );
87
96
 
88
97
  const read = async (): Promise<Record<string, unknown>> => {
89
98
  const inspection = await readGameSession({
90
99
  gameSessionId: opened.gameSessionId,
91
- requestSync: true,
92
100
  });
93
101
  if (!inspection.gameSnapshot?.snapshot) {
94
102
  throw new Error(
@@ -104,7 +112,6 @@ const createScenarioAgentSession = async ({
104
112
  listActions: async () => {
105
113
  const inspection = await readGameSession({
106
114
  gameSessionId: opened.gameSessionId,
107
- requestSync: true,
108
115
  });
109
116
  return inspection.actions;
110
117
  },
@@ -187,9 +194,16 @@ try {
187
194
  secure,
188
195
  artifactRoot,
189
196
  loadScenarioPack: async () => scenarioPack,
190
- createAgentSession: async ({ gameId: activeGameId, urls, mode, secure }) =>
197
+ createAgentSession: async ({
198
+ gameId: activeGameId,
199
+ urls,
200
+ host,
201
+ mode,
202
+ secure,
203
+ }) =>
191
204
  createScenarioAgentSession({
192
205
  gameId: activeGameId,
206
+ host,
193
207
  urls,
194
208
  mode,
195
209
  secure,
@@ -1,6 +1,9 @@
1
1
  import type { AirJamActionInvocationResult } from "@air-jam/sdk";
2
2
  import {
3
3
  AIR_JAM_RUNTIME_CONTROL_KEY,
4
+ hostAgentResponseSchema,
5
+ type HostAgentRequest,
6
+ type HostAgentResponse,
4
7
  type HostRuntimeActionRequest,
5
8
  type HostRuntimeControl,
6
9
  } from "@air-jam/sdk/runtime-control";
@@ -40,3 +43,23 @@ export const invokeOwnedHostAction = async (
40
43
  };
41
44
  }
42
45
  };
46
+
47
+ export async function evaluateOwnedGameContract(
48
+ host: VisualHarnessPageSurface,
49
+ request: HostAgentRequest,
50
+ ): Promise<HostAgentResponse> {
51
+ const result = await host.game.locator("body").evaluate(
52
+ async (_, { key, request }) => {
53
+ const control = (
54
+ window as unknown as Record<string, HostRuntimeControl | undefined>
55
+ )[key];
56
+ if (!control)
57
+ throw new Error(
58
+ "contract_runtime_unavailable: Owned game control is not mounted.",
59
+ );
60
+ return control.agent(request);
61
+ },
62
+ { key: AIR_JAM_RUNTIME_CONTROL_KEY, request },
63
+ );
64
+ return hostAgentResponseSchema.parse(result);
65
+ }