@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.
- package/LICENSE +21 -0
- package/README.md +112 -2
- package/dist/chunk-2O67AXCU.js +470 -0
- package/dist/chunk-2O67AXCU.js.map +1 -0
- package/dist/chunk-AO3ZDXF6.js +142 -0
- package/dist/chunk-AO3ZDXF6.js.map +1 -0
- package/dist/chunk-ART5TEYT.js +472 -0
- package/dist/chunk-ART5TEYT.js.map +1 -0
- package/dist/chunk-ASC2CK2R.js +77 -0
- package/dist/chunk-ASC2CK2R.js.map +1 -0
- package/dist/chunk-E3UZZGAV.js +50 -0
- package/dist/chunk-E3UZZGAV.js.map +1 -0
- package/dist/chunk-EJTE7UKX.js +64 -0
- package/dist/chunk-EJTE7UKX.js.map +1 -0
- package/dist/chunk-ESFKNN75.js +110 -0
- package/dist/chunk-ESFKNN75.js.map +1 -0
- package/dist/chunk-EU7A6PXW.js +1339 -0
- package/dist/chunk-EU7A6PXW.js.map +1 -0
- package/dist/chunk-FN7E6EA7.js +549 -0
- package/dist/chunk-FN7E6EA7.js.map +1 -0
- package/dist/chunk-GDFAWHGA.js +190 -0
- package/dist/chunk-GDFAWHGA.js.map +1 -0
- package/dist/chunk-HZUGBH4Y.js +347 -0
- package/dist/chunk-HZUGBH4Y.js.map +1 -0
- package/dist/chunk-ITKAOEGX.js +93 -0
- package/dist/chunk-ITKAOEGX.js.map +1 -0
- package/dist/chunk-JBCUTWRI.js +897 -0
- package/dist/chunk-JBCUTWRI.js.map +1 -0
- package/dist/chunk-JSI5S4WP.js +1174 -0
- package/dist/chunk-JSI5S4WP.js.map +1 -0
- package/dist/chunk-L4D675BG.js +97 -0
- package/dist/chunk-L4D675BG.js.map +1 -0
- package/dist/chunk-M6CJ7GQC.js +133 -0
- package/dist/chunk-M6CJ7GQC.js.map +1 -0
- package/dist/chunk-MEIMOSZC.js +749 -0
- package/dist/chunk-MEIMOSZC.js.map +1 -0
- package/dist/chunk-O3XG3FNL.js +1014 -0
- package/dist/chunk-O3XG3FNL.js.map +1 -0
- package/dist/chunk-OM6SCJ7H.js +18 -0
- package/dist/chunk-OM6SCJ7H.js.map +1 -0
- package/dist/chunk-PKD4WVX5.js +63 -0
- package/dist/chunk-PKD4WVX5.js.map +1 -0
- package/dist/chunk-PMBLXGFM.js +3384 -0
- package/dist/chunk-PMBLXGFM.js.map +1 -0
- package/dist/chunk-PMNE5GIN.js +279 -0
- package/dist/chunk-PMNE5GIN.js.map +1 -0
- package/dist/chunk-QAWI64VR.js +148 -0
- package/dist/chunk-QAWI64VR.js.map +1 -0
- package/dist/chunk-QGADSPHZ.js +449 -0
- package/dist/chunk-QGADSPHZ.js.map +1 -0
- package/dist/chunk-SPYN66UV.js +338 -0
- package/dist/chunk-SPYN66UV.js.map +1 -0
- package/dist/context.d.ts +19 -0
- package/dist/context.js +10 -0
- package/dist/context.js.map +1 -0
- package/dist/controller.d.ts +67 -0
- package/dist/controller.js +40 -0
- package/dist/controller.js.map +1 -0
- package/dist/dependencies.d.ts +44 -0
- package/dist/dependencies.js +10 -0
- package/dist/dependencies.js.map +1 -0
- package/dist/dev.d.ts +21 -0
- package/dist/dev.js +19 -0
- package/dist/dev.js.map +1 -0
- package/dist/game-builds.d.ts +200 -0
- package/dist/game-builds.js +16 -0
- package/dist/game-builds.js.map +1 -0
- package/dist/game-run-watch-CEd_SpAT.d.ts +84 -0
- package/dist/game-runs.d.ts +1452 -0
- package/dist/game-runs.js +76 -0
- package/dist/game-runs.js.map +1 -0
- package/dist/game-session-broker-CUvORfMd.d.ts +439 -0
- package/dist/game-session-broker.d.ts +4 -0
- package/dist/game-session-broker.js +40 -0
- package/dist/game-session-broker.js.map +1 -0
- package/dist/game-session.d.ts +3934 -0
- package/dist/game-session.js +195 -0
- package/dist/game-session.js.map +1 -0
- package/dist/game-settings.d.ts +16 -0
- package/dist/game-settings.js +237 -0
- package/dist/game-settings.js.map +1 -0
- package/dist/games.d.ts +28 -0
- package/dist/games.js +16 -0
- package/dist/games.js.map +1 -0
- package/dist/harness/visual.d.ts +204 -0
- package/dist/harness/visual.js +46 -0
- package/dist/harness/visual.js.map +1 -0
- package/dist/logs.d.ts +15 -0
- package/dist/logs.js +57 -0
- package/dist/logs.js.map +1 -0
- package/dist/managed-process-scope-BicYi_t9.d.ts +3 -0
- package/dist/mcp-config.d.ts +63 -0
- package/dist/mcp-config.js +191 -0
- package/dist/mcp-config.js.map +1 -0
- package/dist/platform-auth.d.ts +9 -0
- package/dist/platform-auth.js +45 -0
- package/dist/platform-auth.js.map +1 -0
- package/dist/platform-game-analytics.d.ts +51 -0
- package/dist/platform-game-analytics.js +49 -0
- package/dist/platform-game-analytics.js.map +1 -0
- package/dist/platform-game-app-id.d.ts +46 -0
- package/dist/platform-game-app-id.js +93 -0
- package/dist/platform-game-app-id.js.map +1 -0
- package/dist/platform-game-media.d.ts +347 -0
- package/dist/platform-game-media.js +168 -0
- package/dist/platform-game-media.js.map +1 -0
- package/dist/platform-games.d.ts +87 -0
- package/dist/platform-games.js +183 -0
- package/dist/platform-games.js.map +1 -0
- package/dist/quality.d.ts +16 -0
- package/dist/quality.js +62 -0
- package/dist/quality.js.map +1 -0
- package/dist/release.d.ts +1304 -0
- package/dist/release.js +34 -0
- package/dist/release.js.map +1 -0
- package/dist/rooms-check.d.ts +90 -0
- package/dist/rooms-check.js +717 -0
- package/dist/rooms-check.js.map +1 -0
- package/dist/source-tree-contract.d.ts +16 -0
- package/dist/source-tree-contract.js +13 -0
- package/dist/source-tree-contract.js.map +1 -0
- package/dist/testing.d.ts +464 -0
- package/dist/testing.js +27 -0
- package/dist/testing.js.map +1 -0
- package/dist/tooling/describe-game.d.ts +2 -0
- package/dist/tooling/describe-game.js +54 -0
- package/dist/tooling/describe-game.js.map +1 -0
- package/dist/tooling/game-session-broker.d.ts +2 -0
- package/dist/tooling/game-session-broker.js +28 -0
- package/dist/tooling/game-session-broker.js.map +1 -0
- package/dist/tooling/hold-runtime-host.d.ts +2 -0
- package/dist/tooling/hold-runtime-host.js +931 -0
- package/dist/tooling/hold-runtime-host.js.map +1 -0
- package/dist/tooling/list-visual-scenarios.d.ts +2 -0
- package/dist/tooling/list-visual-scenarios.js +25 -0
- package/dist/tooling/list-visual-scenarios.js.map +1 -0
- package/dist/tooling/managed-dev-supervisor.d.ts +2 -0
- package/dist/tooling/managed-dev-supervisor.js +51 -0
- package/dist/tooling/managed-dev-supervisor.js.map +1 -0
- package/dist/tooling/process-scope-bootstrap.mjs +24 -0
- package/dist/tooling/process-scope-guard.d.ts +2 -0
- package/dist/tooling/process-scope-guard.js +388 -0
- package/dist/tooling/process-scope-guard.js.map +1 -0
- package/dist/tooling/process-scope-windows.ps1 +148 -0
- package/dist/tooling/rooms-check-device.d.ts +2 -0
- package/dist/tooling/rooms-check-device.js +498 -0
- package/dist/tooling/rooms-check-device.js.map +1 -0
- package/dist/tooling/run-headless-room.d.ts +2 -0
- package/dist/tooling/run-headless-room.js +287 -0
- package/dist/tooling/run-headless-room.js.map +1 -0
- package/dist/tooling/run-test-profile.d.ts +2 -0
- package/dist/tooling/run-test-profile.js +181 -0
- package/dist/tooling/run-test-profile.js.map +1 -0
- package/dist/tooling/run-visual-capture.d.ts +2 -0
- package/dist/tooling/run-visual-capture.js +203 -0
- package/dist/tooling/run-visual-capture.js.map +1 -0
- package/dist/tooling/seal-game-build.d.ts +2 -0
- package/dist/tooling/seal-game-build.js +120 -0
- package/dist/tooling/seal-game-build.js.map +1 -0
- package/dist/types-CdYrgrG_.d.ts +1036 -0
- package/dist/types-DyRD-ym1.d.ts +141 -0
- package/dist/visual.d.ts +21 -0
- package/dist/visual.js +219 -0
- package/dist/visual.js.map +1 -0
- package/package.json +150 -3
- package/src/bounded-response.ts +28 -0
- package/src/built-game-runtime.ts +152 -0
- package/src/commands.ts +40 -0
- package/src/context.ts +218 -0
- package/src/controller-protocol.ts +22 -0
- package/src/controller.ts +1526 -0
- package/src/current-room-host.ts +166 -0
- package/src/decision-provider.ts +102 -0
- package/src/dependencies.ts +176 -0
- package/src/dependency-install-contract.ts +12 -0
- package/src/dev.ts +995 -0
- package/src/fs-utils.ts +93 -0
- package/src/game-action-observation.ts +106 -0
- package/src/game-build-protocol.ts +72 -0
- package/src/game-build.ts +483 -0
- package/src/game-run-owner.ts +2358 -0
- package/src/game-run-protocol.ts +448 -0
- package/src/game-run-record.ts +92 -0
- package/src/game-run-replay.ts +95 -0
- package/src/game-run-watch.ts +155 -0
- package/src/game-session-broker.ts +793 -0
- package/src/game-session-operations.ts +61 -0
- package/src/game-session-protocol.ts +180 -0
- package/src/game-session-runtime.ts +861 -0
- package/src/game-session.ts +206 -0
- package/src/game-settings.ts +295 -0
- package/src/games.ts +262 -0
- package/src/harness/visual/browser.ts +100 -0
- package/src/harness/visual/controller-observation.ts +181 -0
- package/src/harness/visual/prefab-contract.ts +29 -0
- package/src/harness/visual/prefab-runner.ts +178 -0
- package/src/harness/visual/runner.ts +549 -0
- package/src/harness/visual/runtime-controller-observations.ts +151 -0
- package/src/harness/visual/scenario-contract.ts +8 -0
- package/src/harness/visual/scenario-helpers.ts +67 -0
- package/src/harness/visual/screencast.ts +155 -0
- package/src/harness/visual/session.ts +400 -0
- package/src/harness/visual/types.ts +184 -0
- package/src/harness/visual.ts +7 -0
- package/src/headless-room-protocol.ts +112 -0
- package/src/headless-room.ts +222 -0
- package/src/helper-channel.ts +229 -0
- package/src/helper-scripts.ts +69 -0
- package/src/installed-dependencies.ts +74 -0
- package/src/logs.ts +67 -0
- package/src/managed-process-scope.ts +481 -0
- package/src/mcp-config.ts +262 -0
- package/src/node-process-environment.ts +10 -0
- package/src/platform-auth.ts +594 -0
- package/src/platform-game-analytics.ts +50 -0
- package/src/platform-game-app-id.ts +99 -0
- package/src/platform-game-media.ts +209 -0
- package/src/platform-games.ts +217 -0
- package/src/policy-artifact-contract.ts +52 -0
- package/src/policy-artifacts.ts +134 -0
- package/src/process-scope-admission.ts +50 -0
- package/src/process-scope-protocol.ts +66 -0
- package/src/process-scope.ts +327 -0
- package/src/quality.ts +82 -0
- package/src/release.ts +1683 -0
- package/src/rooms-check-fleet.ts +194 -0
- package/src/rooms-check-link.ts +138 -0
- package/src/rooms-check-protocol.ts +244 -0
- package/src/rooms-check.ts +797 -0
- package/src/runtime-owner-protocol.ts +218 -0
- package/src/source-tree-contract.ts +70 -0
- package/src/source-tree.ts +84 -0
- package/src/test-profile-process.ts +153 -0
- package/src/test-profile-protocol.ts +116 -0
- package/src/testing.ts +430 -0
- package/src/tooling/declaration-hook.ts +52 -0
- package/src/tooling/describe-game.ts +49 -0
- package/src/tooling/game-session-broker.ts +7 -0
- package/src/tooling/hold-runtime-host.ts +441 -0
- package/src/tooling/list-visual-scenarios.ts +21 -0
- package/src/tooling/load-game.ts +79 -0
- package/src/tooling/managed-dev-supervisor.ts +71 -0
- package/src/tooling/process-scope-bootstrap.mjs +24 -0
- package/src/tooling/process-scope-guard.ts +213 -0
- package/src/tooling/process-scope-windows.ps1 +148 -0
- package/src/tooling/rooms-check-device.ts +493 -0
- package/src/tooling/run-headless-room.ts +326 -0
- package/src/tooling/run-test-profile.ts +190 -0
- package/src/tooling/run-visual-capture.ts +231 -0
- package/src/tooling/runtime-owner-actions.ts +65 -0
- package/src/tooling/seal-game-build.ts +118 -0
- package/src/tooling/visual-pack.ts +11 -0
- package/src/types.ts +928 -0
- package/src/visual.ts +296 -0
- package/src/windows-process-scopes.ts +189 -0
- package/src/workspace-contract.ts +32 -0
|
@@ -0,0 +1,326 @@
|
|
|
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
|
+
controllerUrl: 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 url.toString();
|
|
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<{ roomId: string; joinUrl: string }>(
|
|
152
|
+
(resolve, reject) => {
|
|
153
|
+
const timer = setTimeout(
|
|
154
|
+
() =>
|
|
155
|
+
reject(new Error("room_startup_timeout: The host never joined.")),
|
|
156
|
+
STARTUP_MS,
|
|
157
|
+
);
|
|
158
|
+
const check = () => {
|
|
159
|
+
const state = store.getState();
|
|
160
|
+
if (
|
|
161
|
+
state.connectionStatus === "connected" &&
|
|
162
|
+
state.roomId &&
|
|
163
|
+
state.joinUrl
|
|
164
|
+
) {
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
unsubscribe();
|
|
167
|
+
resolve({ roomId: state.roomId, joinUrl: state.joinUrl });
|
|
168
|
+
} else if (state.lastError) {
|
|
169
|
+
clearTimeout(timer);
|
|
170
|
+
unsubscribe();
|
|
171
|
+
reject(new Error(`room_unavailable: ${state.lastError}`));
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
const unsubscribe = store.subscribe(check);
|
|
175
|
+
check();
|
|
176
|
+
},
|
|
177
|
+
);
|
|
178
|
+
room = {
|
|
179
|
+
agent: (request) => {
|
|
180
|
+
const control = Reflect.get(
|
|
181
|
+
controlTarget,
|
|
182
|
+
AIR_JAM_RUNTIME_CONTROL_KEY,
|
|
183
|
+
) as HostRuntimeControl | undefined;
|
|
184
|
+
if (!control)
|
|
185
|
+
throw new Error(
|
|
186
|
+
"room_unavailable: The game publishes nothing agents can read.",
|
|
187
|
+
);
|
|
188
|
+
return control.agent(request);
|
|
189
|
+
},
|
|
190
|
+
advance: (ms) => {
|
|
191
|
+
nowMs += ms;
|
|
192
|
+
runtime.simulation?.advance(ms);
|
|
193
|
+
return nowMs;
|
|
194
|
+
},
|
|
195
|
+
saveInputLog: app.simulation
|
|
196
|
+
? async (path) => {
|
|
197
|
+
const log = runtime.inputLog();
|
|
198
|
+
const source = `${JSON.stringify(log)}\n`;
|
|
199
|
+
await writeFile(path, source, { mode: 0o600 });
|
|
200
|
+
return {
|
|
201
|
+
sha256: createHash("sha256").update(source).digest("hex"),
|
|
202
|
+
ticks: log.ticks,
|
|
203
|
+
stepMs: log.stepMs,
|
|
204
|
+
entries: log.entries.length,
|
|
205
|
+
full: runtime.inputLogFull,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
: null,
|
|
209
|
+
close,
|
|
210
|
+
};
|
|
211
|
+
return {
|
|
212
|
+
gameId: app.metadata.slug,
|
|
213
|
+
roomId: joined.roomId,
|
|
214
|
+
controllerJoinUrl: joined.joinUrl,
|
|
215
|
+
nowMs,
|
|
216
|
+
recordsInputLog: Boolean(app.simulation),
|
|
217
|
+
};
|
|
218
|
+
} catch (error) {
|
|
219
|
+
await close();
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function replayRoom(configPath: string, logPath: string) {
|
|
225
|
+
const app = await loadAirJamGame(configPath);
|
|
226
|
+
const log = airJamInputLogSchema.parse(
|
|
227
|
+
JSON.parse(await readFile(logPath, "utf8")),
|
|
228
|
+
);
|
|
229
|
+
const replayed = await replayAirJamInputLog(app.createHostRuntime, log);
|
|
230
|
+
room = {
|
|
231
|
+
agent: replayed.agent,
|
|
232
|
+
advance: null,
|
|
233
|
+
saveInputLog: null,
|
|
234
|
+
close: async () => replayed.stop(),
|
|
235
|
+
};
|
|
236
|
+
return {
|
|
237
|
+
gameId: app.metadata.slug,
|
|
238
|
+
roomId: replayed.roomId,
|
|
239
|
+
ticks: log.ticks,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
process.on("disconnect", () => {
|
|
244
|
+
void (room?.close() ?? Promise.resolve()).finally(() => process.exit(0));
|
|
245
|
+
});
|
|
246
|
+
process.on("message", (message: unknown) => {
|
|
247
|
+
void (async () => {
|
|
248
|
+
if (
|
|
249
|
+
Buffer.byteLength(JSON.stringify(message)) > HEADLESS_ROOM_IPC_MAX_BYTES
|
|
250
|
+
)
|
|
251
|
+
throw new Error("Oversized headless room request.");
|
|
252
|
+
const request = headlessRoomRequestSchema.parse(message);
|
|
253
|
+
if (busy)
|
|
254
|
+
return send({ kind: "error", id: request.id, message: "room_busy" });
|
|
255
|
+
busy = true;
|
|
256
|
+
try {
|
|
257
|
+
if (request.kind === "initialize") {
|
|
258
|
+
if (room || initializing)
|
|
259
|
+
throw new Error("The headless room is already open.");
|
|
260
|
+
initializing = true;
|
|
261
|
+
send({
|
|
262
|
+
kind: "initialized",
|
|
263
|
+
id: request.id,
|
|
264
|
+
...(await openRoom(
|
|
265
|
+
request.configPath,
|
|
266
|
+
request.seed,
|
|
267
|
+
request.settings,
|
|
268
|
+
)),
|
|
269
|
+
});
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (request.kind === "replay") {
|
|
273
|
+
if (room || initializing)
|
|
274
|
+
throw new Error("The headless room is already open.");
|
|
275
|
+
initializing = true;
|
|
276
|
+
send({
|
|
277
|
+
kind: "replayed",
|
|
278
|
+
id: request.id,
|
|
279
|
+
...(await replayRoom(request.configPath, request.logPath)),
|
|
280
|
+
});
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (!room) throw new Error("The headless room is not open.");
|
|
284
|
+
if (request.kind === "agent")
|
|
285
|
+
send({
|
|
286
|
+
kind: "agent",
|
|
287
|
+
id: request.id,
|
|
288
|
+
response: await room.agent(request.request),
|
|
289
|
+
});
|
|
290
|
+
else if (request.kind === "saveInputLog") {
|
|
291
|
+
if (!room.saveInputLog)
|
|
292
|
+
throw new Error(
|
|
293
|
+
"no_input_log: This room keeps no input log; its game declares no simulation.",
|
|
294
|
+
);
|
|
295
|
+
send({
|
|
296
|
+
kind: "savedInputLog",
|
|
297
|
+
id: request.id,
|
|
298
|
+
...(await room.saveInputLog(request.path)),
|
|
299
|
+
});
|
|
300
|
+
} else {
|
|
301
|
+
if (!room.advance)
|
|
302
|
+
throw new Error("replay_finished: A replay has already played.");
|
|
303
|
+
send({
|
|
304
|
+
kind: "advanced",
|
|
305
|
+
id: request.id,
|
|
306
|
+
nowMs: room.advance(request.ms),
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
} catch (error) {
|
|
310
|
+
send({
|
|
311
|
+
kind: "error",
|
|
312
|
+
id: request.id,
|
|
313
|
+
message: (error instanceof Error ? error.message : String(error)).slice(
|
|
314
|
+
0,
|
|
315
|
+
2000,
|
|
316
|
+
),
|
|
317
|
+
});
|
|
318
|
+
} finally {
|
|
319
|
+
busy = false;
|
|
320
|
+
}
|
|
321
|
+
})().catch((error) => {
|
|
322
|
+
console.error(error);
|
|
323
|
+
process.exit(1);
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
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" });
|