ugly-game 0.15.17 → 0.15.19
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/dist/flightCodec.d.ts +12 -0
- package/dist/flightCodec.js +261 -0
- package/dist/flightObservations.d.ts +19 -0
- package/dist/flightObservations.js +141 -0
- package/dist/flightPack.d.ts +28 -0
- package/dist/flightPack.js +104 -0
- package/dist/flightRecorder.d.ts +16 -0
- package/dist/flightRecorder.js +492 -0
- package/dist/flightReplay.d.ts +8 -0
- package/dist/flightReplay.js +65 -0
- package/dist/flightRing.d.ts +38 -0
- package/dist/flightRing.js +133 -0
- package/dist/flightSimHost.d.ts +19 -0
- package/dist/flightSimHost.js +84 -0
- package/dist/flightTestFixture.d.mts +2 -0
- package/dist/flightTestFixture.mjs +57 -0
- package/dist/flightTypes.d.ts +160 -0
- package/dist/flightTypes.js +20 -0
- package/dist/gpuShadow.d.ts +31 -1
- package/dist/gpuShadow.js +58 -4
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/loop/simHost.d.ts +1 -1
- package/dist/loop/simHost.worker.d.ts +3 -1
- package/dist/loop/simHost.worker.js +9 -2
- package/dist/telemetry.d.ts +9 -0
- package/dist/telemetry.js +33 -8
- package/package.json +7 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type FlightCapture, type FlightChannel, type FlightRecord } from './flightTypes';
|
|
2
|
+
export interface FlightDecodeLimits {
|
|
3
|
+
maxBytes?: number;
|
|
4
|
+
maxMetadataBytes?: number;
|
|
5
|
+
maxBlockBytes?: number;
|
|
6
|
+
maxRecordsPerChannel?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function encodeFlightCapture(capture: FlightCapture): Uint8Array;
|
|
9
|
+
export declare function decodeFlightCapture(bytes: Uint8Array, limits?: FlightDecodeLimits): FlightCapture;
|
|
10
|
+
export declare function orderedFlightRecords(capture: FlightCapture): (FlightRecord & {
|
|
11
|
+
channel: FlightChannel;
|
|
12
|
+
})[];
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { FLIGHT_CHANNELS, FLIGHT_CONTAINER_VERSION, FLIGHT_RECORDER_SCHEMA, } from './flightTypes';
|
|
2
|
+
const MAGIC = new Uint8Array([0x55, 0x47, 0x46, 0x52]); // UGFR
|
|
3
|
+
const CONTAINER_HEADER_BYTES = 8;
|
|
4
|
+
const BLOCK_HEADER_BYTES = 8;
|
|
5
|
+
const RECORD_HEADER_BYTES = 32;
|
|
6
|
+
const METADATA_BLOCK = 0;
|
|
7
|
+
const CHECKPOINT_BLOCK = 1;
|
|
8
|
+
const RNG_BLOCK = 2;
|
|
9
|
+
const CHANNEL_BLOCK_BASE = 16;
|
|
10
|
+
const textEncoder = new TextEncoder();
|
|
11
|
+
const textDecoder = new TextDecoder('utf-8', { fatal: true });
|
|
12
|
+
export function encodeFlightCapture(capture) {
|
|
13
|
+
const metadata = {
|
|
14
|
+
header: capture.header,
|
|
15
|
+
...(capture.anchor
|
|
16
|
+
? {
|
|
17
|
+
anchor: {
|
|
18
|
+
requestId: capture.anchor.requestId,
|
|
19
|
+
tick: capture.anchor.tick,
|
|
20
|
+
monotonicMs: capture.anchor.monotonicMs,
|
|
21
|
+
schema: capture.anchor.schema,
|
|
22
|
+
...(capture.anchor.hash !== undefined
|
|
23
|
+
? { hash: capture.anchor.hash }
|
|
24
|
+
: {}),
|
|
25
|
+
encodedBytes: capture.anchor.encodedBytes,
|
|
26
|
+
transferBytes: capture.anchor.transferBytes,
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
: {}),
|
|
30
|
+
truncation: capture.truncation,
|
|
31
|
+
replay: capture.replay,
|
|
32
|
+
selfTelemetry: capture.selfTelemetry,
|
|
33
|
+
};
|
|
34
|
+
const blocks = [
|
|
35
|
+
{ id: METADATA_BLOCK, bytes: textEncoder.encode(JSON.stringify(metadata)) },
|
|
36
|
+
];
|
|
37
|
+
if (capture.anchor) {
|
|
38
|
+
blocks.push({ id: CHECKPOINT_BLOCK, bytes: capture.anchor.checkpoint });
|
|
39
|
+
if (capture.anchor.rng)
|
|
40
|
+
blocks.push({
|
|
41
|
+
id: RNG_BLOCK,
|
|
42
|
+
bytes: new Uint8Array(capture.anchor.rng.buffer, capture.anchor.rng.byteOffset, capture.anchor.rng.byteLength),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
for (let i = 0; i < FLIGHT_CHANNELS.length; i++)
|
|
46
|
+
blocks.push({
|
|
47
|
+
id: CHANNEL_BLOCK_BASE + i,
|
|
48
|
+
bytes: encodeRecords(capture.channels[FLIGHT_CHANNELS[i]]),
|
|
49
|
+
});
|
|
50
|
+
const total = CONTAINER_HEADER_BYTES +
|
|
51
|
+
blocks.reduce((sum, block) => sum + BLOCK_HEADER_BYTES + block.bytes.byteLength, 0);
|
|
52
|
+
const out = new Uint8Array(total);
|
|
53
|
+
const view = new DataView(out.buffer);
|
|
54
|
+
out.set(MAGIC, 0);
|
|
55
|
+
view.setUint16(4, FLIGHT_CONTAINER_VERSION, true);
|
|
56
|
+
view.setUint16(6, blocks.length, true);
|
|
57
|
+
let offset = CONTAINER_HEADER_BYTES;
|
|
58
|
+
for (const block of blocks) {
|
|
59
|
+
view.setUint16(offset, block.id, true);
|
|
60
|
+
view.setUint16(offset + 2, 0, true);
|
|
61
|
+
view.setUint32(offset + 4, block.bytes.byteLength, true);
|
|
62
|
+
offset += BLOCK_HEADER_BYTES;
|
|
63
|
+
out.set(block.bytes, offset);
|
|
64
|
+
offset += block.bytes.byteLength;
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
export function decodeFlightCapture(bytes, limits = {}) {
|
|
69
|
+
const maxBytes = limits.maxBytes ?? 64 * 1024 * 1024;
|
|
70
|
+
const maxMetadata = limits.maxMetadataBytes ?? 1024 * 1024;
|
|
71
|
+
const maxBlock = limits.maxBlockBytes ?? 32 * 1024 * 1024;
|
|
72
|
+
const maxRecords = limits.maxRecordsPerChannel ?? 1_000_000;
|
|
73
|
+
if (bytes.byteLength > maxBytes)
|
|
74
|
+
throw new RangeError('flight capture exceeds maximum bytes');
|
|
75
|
+
if (bytes.byteLength < CONTAINER_HEADER_BYTES)
|
|
76
|
+
throw new Error('truncated flight capture header');
|
|
77
|
+
for (let i = 0; i < MAGIC.length; i++)
|
|
78
|
+
if (bytes[i] !== MAGIC[i])
|
|
79
|
+
throw new Error('invalid flight capture magic');
|
|
80
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
81
|
+
const version = view.getUint16(4, true);
|
|
82
|
+
if (version !== FLIGHT_CONTAINER_VERSION)
|
|
83
|
+
throw new Error(`unsupported flight container version ${version}`);
|
|
84
|
+
const blockCount = view.getUint16(6, true);
|
|
85
|
+
const blocks = new Map();
|
|
86
|
+
let offset = CONTAINER_HEADER_BYTES;
|
|
87
|
+
for (let i = 0; i < blockCount; i++) {
|
|
88
|
+
ensureAvailable(bytes, offset, BLOCK_HEADER_BYTES, 'block header');
|
|
89
|
+
const id = view.getUint16(offset, true);
|
|
90
|
+
const flags = view.getUint16(offset + 2, true);
|
|
91
|
+
const length = view.getUint32(offset + 4, true);
|
|
92
|
+
offset += BLOCK_HEADER_BYTES;
|
|
93
|
+
if (flags !== 0)
|
|
94
|
+
throw new Error(`unsupported flight block flags ${flags}`);
|
|
95
|
+
if (length > maxBlock)
|
|
96
|
+
throw new RangeError(`flight block ${id} exceeds maximum bytes`);
|
|
97
|
+
ensureAvailable(bytes, offset, length, `block ${id}`);
|
|
98
|
+
if (blocks.has(id))
|
|
99
|
+
throw new Error(`duplicate flight block ${id}`);
|
|
100
|
+
if (!knownBlock(id))
|
|
101
|
+
throw new Error(`unknown required flight block ${id}`);
|
|
102
|
+
blocks.set(id, bytes.slice(offset, offset + length));
|
|
103
|
+
offset += length;
|
|
104
|
+
}
|
|
105
|
+
if (offset !== bytes.byteLength)
|
|
106
|
+
throw new Error('trailing bytes after flight container');
|
|
107
|
+
const metadataBytes = blocks.get(METADATA_BLOCK);
|
|
108
|
+
if (!metadataBytes)
|
|
109
|
+
throw new Error('flight capture metadata is missing');
|
|
110
|
+
if (metadataBytes.byteLength > maxMetadata)
|
|
111
|
+
throw new RangeError('flight metadata exceeds maximum bytes');
|
|
112
|
+
const metadata = parseMetadata(metadataBytes);
|
|
113
|
+
const checkpoint = blocks.get(CHECKPOINT_BLOCK);
|
|
114
|
+
const rngBytes = blocks.get(RNG_BLOCK);
|
|
115
|
+
if (!!metadata.anchor !== !!checkpoint)
|
|
116
|
+
throw new Error('flight anchor metadata/payload mismatch');
|
|
117
|
+
if (metadata.anchor &&
|
|
118
|
+
checkpoint &&
|
|
119
|
+
metadata.anchor.encodedBytes !== checkpoint.byteLength)
|
|
120
|
+
throw new Error('flight checkpoint byte count mismatch');
|
|
121
|
+
if (rngBytes && rngBytes.byteLength % Uint32Array.BYTES_PER_ELEMENT !== 0)
|
|
122
|
+
throw new Error('invalid flight RNG byte length');
|
|
123
|
+
const channels = {};
|
|
124
|
+
for (let i = 0; i < FLIGHT_CHANNELS.length; i++) {
|
|
125
|
+
const block = blocks.get(CHANNEL_BLOCK_BASE + i);
|
|
126
|
+
channels[FLIGHT_CHANNELS[i]] = block
|
|
127
|
+
? decodeRecords(block, maxRecords)
|
|
128
|
+
: [];
|
|
129
|
+
}
|
|
130
|
+
const ordered = Object.values(channels)
|
|
131
|
+
.flat()
|
|
132
|
+
.sort((a, b) => a.sequence - b.sequence);
|
|
133
|
+
for (let i = 1; i < ordered.length; i++)
|
|
134
|
+
if (ordered[i - 1].sequence === ordered[i].sequence)
|
|
135
|
+
throw new Error('duplicate recorder-wide flight sequence');
|
|
136
|
+
return {
|
|
137
|
+
header: metadata.header,
|
|
138
|
+
...(metadata.anchor && checkpoint
|
|
139
|
+
? {
|
|
140
|
+
anchor: {
|
|
141
|
+
...metadata.anchor,
|
|
142
|
+
checkpoint,
|
|
143
|
+
...(rngBytes
|
|
144
|
+
? {
|
|
145
|
+
rng: new Uint32Array(rngBytes.buffer.slice(rngBytes.byteOffset, rngBytes.byteOffset + rngBytes.byteLength)),
|
|
146
|
+
}
|
|
147
|
+
: {}),
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
: {}),
|
|
151
|
+
channels,
|
|
152
|
+
truncation: metadata.truncation,
|
|
153
|
+
replay: metadata.replay,
|
|
154
|
+
selfTelemetry: metadata.selfTelemetry,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function encodeRecords(records) {
|
|
158
|
+
const length = 4 +
|
|
159
|
+
records.reduce((sum, record) => sum + RECORD_HEADER_BYTES + record.payload.byteLength, 0);
|
|
160
|
+
const out = new Uint8Array(length);
|
|
161
|
+
const view = new DataView(out.buffer);
|
|
162
|
+
view.setUint32(0, records.length, true);
|
|
163
|
+
let offset = 4;
|
|
164
|
+
for (const record of records) {
|
|
165
|
+
view.setFloat64(offset, record.sequence, true);
|
|
166
|
+
view.setFloat64(offset + 8, record.monotonicMs, true);
|
|
167
|
+
view.setFloat64(offset + 16, record.tick ?? -1, true);
|
|
168
|
+
view.setUint16(offset + 24, record.kind, true);
|
|
169
|
+
view.setUint16(offset + 26, record.flags, true);
|
|
170
|
+
view.setUint32(offset + 28, record.payload.byteLength, true);
|
|
171
|
+
offset += RECORD_HEADER_BYTES;
|
|
172
|
+
out.set(record.payload, offset);
|
|
173
|
+
offset += record.payload.byteLength;
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
function decodeRecords(bytes, maxRecords) {
|
|
178
|
+
if (bytes.byteLength < 4)
|
|
179
|
+
throw new Error('truncated flight record block');
|
|
180
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
181
|
+
const count = view.getUint32(0, true);
|
|
182
|
+
if (count > maxRecords)
|
|
183
|
+
throw new RangeError('flight record count exceeds maximum');
|
|
184
|
+
if (count > Math.floor((bytes.byteLength - 4) / RECORD_HEADER_BYTES))
|
|
185
|
+
throw new Error('impossible flight record count');
|
|
186
|
+
const records = [];
|
|
187
|
+
let offset = 4;
|
|
188
|
+
let previousSequence = 0;
|
|
189
|
+
for (let i = 0; i < count; i++) {
|
|
190
|
+
ensureAvailable(bytes, offset, RECORD_HEADER_BYTES, 'record header');
|
|
191
|
+
const sequence = view.getFloat64(offset, true);
|
|
192
|
+
const monotonicMs = view.getFloat64(offset + 8, true);
|
|
193
|
+
const rawTick = view.getFloat64(offset + 16, true);
|
|
194
|
+
const kind = view.getUint16(offset + 24, true);
|
|
195
|
+
const flags = view.getUint16(offset + 26, true);
|
|
196
|
+
const length = view.getUint32(offset + 28, true);
|
|
197
|
+
offset += RECORD_HEADER_BYTES;
|
|
198
|
+
ensureAvailable(bytes, offset, length, 'record payload');
|
|
199
|
+
if (!Number.isSafeInteger(sequence) || sequence <= previousSequence)
|
|
200
|
+
throw new Error('flight record sequence is not strictly monotonic within channel');
|
|
201
|
+
if (!Number.isFinite(monotonicMs) ||
|
|
202
|
+
(!Number.isSafeInteger(rawTick) && rawTick !== -1))
|
|
203
|
+
throw new Error('invalid flight record coordinates');
|
|
204
|
+
records.push({
|
|
205
|
+
sequence,
|
|
206
|
+
monotonicMs,
|
|
207
|
+
tick: rawTick === -1 ? null : rawTick,
|
|
208
|
+
kind,
|
|
209
|
+
flags,
|
|
210
|
+
payload: bytes.slice(offset, offset + length),
|
|
211
|
+
});
|
|
212
|
+
previousSequence = sequence;
|
|
213
|
+
offset += length;
|
|
214
|
+
}
|
|
215
|
+
if (offset !== bytes.byteLength)
|
|
216
|
+
throw new Error('trailing bytes in flight record block');
|
|
217
|
+
return records;
|
|
218
|
+
}
|
|
219
|
+
function parseMetadata(bytes) {
|
|
220
|
+
let value;
|
|
221
|
+
try {
|
|
222
|
+
value = JSON.parse(textDecoder.decode(bytes));
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
throw new Error(`invalid flight metadata: ${String(error)}`);
|
|
226
|
+
}
|
|
227
|
+
if (!value || typeof value !== 'object')
|
|
228
|
+
throw new Error('invalid flight metadata root');
|
|
229
|
+
const metadata = value;
|
|
230
|
+
if (!metadata.header ||
|
|
231
|
+
!metadata.truncation ||
|
|
232
|
+
!metadata.replay ||
|
|
233
|
+
!metadata.selfTelemetry)
|
|
234
|
+
throw new Error('incomplete flight metadata');
|
|
235
|
+
if (!Number.isSafeInteger(metadata.header.recorderSchema) ||
|
|
236
|
+
!Number.isSafeInteger(metadata.header.gameSchema) ||
|
|
237
|
+
!Number.isSafeInteger(metadata.header.inputSchema))
|
|
238
|
+
throw new Error('invalid flight schema metadata');
|
|
239
|
+
if (metadata.header.recorderSchema !== FLIGHT_RECORDER_SCHEMA)
|
|
240
|
+
throw new Error(`unsupported flight recorder schema ${metadata.header.recorderSchema}`);
|
|
241
|
+
return metadata;
|
|
242
|
+
}
|
|
243
|
+
function knownBlock(id) {
|
|
244
|
+
return (id === METADATA_BLOCK ||
|
|
245
|
+
id === CHECKPOINT_BLOCK ||
|
|
246
|
+
id === RNG_BLOCK ||
|
|
247
|
+
(id >= CHANNEL_BLOCK_BASE &&
|
|
248
|
+
id < CHANNEL_BLOCK_BASE + FLIGHT_CHANNELS.length));
|
|
249
|
+
}
|
|
250
|
+
function ensureAvailable(bytes, offset, length, label) {
|
|
251
|
+
if (offset < 0 || length < 0 || offset > bytes.byteLength - length)
|
|
252
|
+
throw new Error(`truncated flight ${label}`);
|
|
253
|
+
}
|
|
254
|
+
export function orderedFlightRecords(capture) {
|
|
255
|
+
const records = [];
|
|
256
|
+
for (const channel of FLIGHT_CHANNELS)
|
|
257
|
+
for (const record of capture.channels[channel])
|
|
258
|
+
records.push({ ...record, channel });
|
|
259
|
+
records.sort((a, b) => a.sequence - b.sequence);
|
|
260
|
+
return records;
|
|
261
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { FlightRecord, FrameSample, RenderSample, WorkerSample } from './flightTypes';
|
|
2
|
+
export declare const FRAME_SAMPLE_BYTES = 32;
|
|
3
|
+
export declare const WORKER_SAMPLE_BYTES = 48;
|
|
4
|
+
export declare const RENDER_SAMPLE_BYTES = 48;
|
|
5
|
+
export interface CorrelatedWorkerRequest {
|
|
6
|
+
requestId: number;
|
|
7
|
+
generation?: number;
|
|
8
|
+
request?: FlightRecord;
|
|
9
|
+
completion?: FlightRecord;
|
|
10
|
+
status: 'ok' | 'pending' | 'timeout' | 'stale' | 'error' | 'restarted' | 'orphan-reply';
|
|
11
|
+
durationMs?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function writeFrameSample(bytes: Uint8Array, sample: FrameSample): void;
|
|
14
|
+
export declare function readFrameSample(bytes: Uint8Array): FrameSample;
|
|
15
|
+
export declare function writeWorkerSample(bytes: Uint8Array, sample: WorkerSample): void;
|
|
16
|
+
export declare function readWorkerSample(bytes: Uint8Array): WorkerSample;
|
|
17
|
+
export declare function writeRenderSample(bytes: Uint8Array, sample: RenderSample): void;
|
|
18
|
+
export declare function readRenderSample(bytes: Uint8Array): RenderSample;
|
|
19
|
+
export declare function correlateWorkerRecords(records: readonly FlightRecord[]): CorrelatedWorkerRequest[];
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
export const FRAME_SAMPLE_BYTES = 32;
|
|
2
|
+
export const WORKER_SAMPLE_BYTES = 48;
|
|
3
|
+
export const RENDER_SAMPLE_BYTES = 48;
|
|
4
|
+
const WORKER_TYPES = [
|
|
5
|
+
'request',
|
|
6
|
+
'reply',
|
|
7
|
+
'timeout',
|
|
8
|
+
'restart',
|
|
9
|
+
'error',
|
|
10
|
+
'stale',
|
|
11
|
+
];
|
|
12
|
+
export function writeFrameSample(bytes, sample) {
|
|
13
|
+
requireLength(bytes, FRAME_SAMPLE_BYTES);
|
|
14
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
15
|
+
view.setFloat64(0, sample.durationMs, true);
|
|
16
|
+
view.setFloat64(8, sample.fps ?? Number.NaN, true);
|
|
17
|
+
view.setFloat64(16, sample.simulationUps ?? Number.NaN, true);
|
|
18
|
+
view.setFloat64(24, sample.longTaskMs ?? Number.NaN, true);
|
|
19
|
+
}
|
|
20
|
+
export function readFrameSample(bytes) {
|
|
21
|
+
requireLength(bytes, FRAME_SAMPLE_BYTES);
|
|
22
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
23
|
+
const fps = view.getFloat64(8, true);
|
|
24
|
+
const simulationUps = view.getFloat64(16, true);
|
|
25
|
+
const longTaskMs = view.getFloat64(24, true);
|
|
26
|
+
return {
|
|
27
|
+
durationMs: view.getFloat64(0, true),
|
|
28
|
+
...(Number.isNaN(fps) ? {} : { fps }),
|
|
29
|
+
...(Number.isNaN(simulationUps) ? {} : { simulationUps }),
|
|
30
|
+
...(Number.isNaN(longTaskMs) ? {} : { longTaskMs }),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export function writeWorkerSample(bytes, sample) {
|
|
34
|
+
requireLength(bytes, WORKER_SAMPLE_BYTES);
|
|
35
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
36
|
+
view.setUint8(0, WORKER_TYPES.indexOf(sample.type));
|
|
37
|
+
writeOptional(view, 8, sample.requestId);
|
|
38
|
+
writeOptional(view, 16, sample.durationMs);
|
|
39
|
+
writeOptional(view, 24, sample.queueDepth);
|
|
40
|
+
writeOptional(view, 32, sample.generation);
|
|
41
|
+
writeOptional(view, 40, sample.revision);
|
|
42
|
+
}
|
|
43
|
+
export function readWorkerSample(bytes) {
|
|
44
|
+
requireLength(bytes, WORKER_SAMPLE_BYTES);
|
|
45
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
46
|
+
const typeIndex = view.getUint8(0);
|
|
47
|
+
if (typeIndex >= WORKER_TYPES.length)
|
|
48
|
+
throw new Error('invalid flight worker sample type');
|
|
49
|
+
const type = WORKER_TYPES[typeIndex];
|
|
50
|
+
return {
|
|
51
|
+
type,
|
|
52
|
+
...optionalField('requestId', view.getFloat64(8, true)),
|
|
53
|
+
...optionalField('durationMs', view.getFloat64(16, true)),
|
|
54
|
+
...optionalField('queueDepth', view.getFloat64(24, true)),
|
|
55
|
+
...optionalField('generation', view.getFloat64(32, true)),
|
|
56
|
+
...optionalField('revision', view.getFloat64(40, true)),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function writeRenderSample(bytes, sample) {
|
|
60
|
+
requireLength(bytes, RENDER_SAMPLE_BYTES);
|
|
61
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
62
|
+
writeOptional(view, 0, sample.draws);
|
|
63
|
+
writeOptional(view, 8, sample.triangles);
|
|
64
|
+
writeOptional(view, 16, sample.cpuAssemblyMs);
|
|
65
|
+
writeOptional(view, 24, sample.uploadMs);
|
|
66
|
+
writeOptional(view, 32, sample.gpuMs);
|
|
67
|
+
writeOptional(view, 40, sample.revision);
|
|
68
|
+
}
|
|
69
|
+
export function readRenderSample(bytes) {
|
|
70
|
+
requireLength(bytes, RENDER_SAMPLE_BYTES);
|
|
71
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
72
|
+
return {
|
|
73
|
+
...optionalField('draws', view.getFloat64(0, true)),
|
|
74
|
+
...optionalField('triangles', view.getFloat64(8, true)),
|
|
75
|
+
...optionalField('cpuAssemblyMs', view.getFloat64(16, true)),
|
|
76
|
+
...optionalField('uploadMs', view.getFloat64(24, true)),
|
|
77
|
+
...optionalField('gpuMs', view.getFloat64(32, true)),
|
|
78
|
+
...optionalField('revision', view.getFloat64(40, true)),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export function correlateWorkerRecords(records) {
|
|
82
|
+
const correlated = new Map();
|
|
83
|
+
const ordered = [...records].sort((a, b) => a.sequence - b.sequence);
|
|
84
|
+
for (const record of ordered) {
|
|
85
|
+
const sample = readWorkerSample(record.payload);
|
|
86
|
+
if (sample.type === 'restart') {
|
|
87
|
+
for (const entry of correlated.values())
|
|
88
|
+
if (entry.status === 'pending')
|
|
89
|
+
entry.status = 'restarted';
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (sample.requestId === undefined)
|
|
93
|
+
continue;
|
|
94
|
+
const key = `${sample.generation ?? -1}:${sample.requestId}`;
|
|
95
|
+
let entry = correlated.get(key);
|
|
96
|
+
if (!entry) {
|
|
97
|
+
entry = {
|
|
98
|
+
requestId: sample.requestId,
|
|
99
|
+
...(sample.generation !== undefined
|
|
100
|
+
? { generation: sample.generation }
|
|
101
|
+
: {}),
|
|
102
|
+
status: sample.type === 'request' ? 'pending' : 'orphan-reply',
|
|
103
|
+
};
|
|
104
|
+
correlated.set(key, entry);
|
|
105
|
+
}
|
|
106
|
+
if (sample.type === 'request') {
|
|
107
|
+
entry.request = record;
|
|
108
|
+
entry.status = entry.completion ? 'ok' : 'pending';
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
entry.completion = record;
|
|
112
|
+
entry.status =
|
|
113
|
+
sample.type === 'reply'
|
|
114
|
+
? entry.request
|
|
115
|
+
? 'ok'
|
|
116
|
+
: 'orphan-reply'
|
|
117
|
+
: sample.type;
|
|
118
|
+
entry.durationMs =
|
|
119
|
+
sample.durationMs ??
|
|
120
|
+
(entry.request
|
|
121
|
+
? record.monotonicMs - entry.request.monotonicMs
|
|
122
|
+
: undefined);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return [...correlated.values()].sort((a, b) => workerEntrySequence(a) - workerEntrySequence(b));
|
|
126
|
+
}
|
|
127
|
+
function workerEntrySequence(entry) {
|
|
128
|
+
return (entry.request?.sequence ??
|
|
129
|
+
entry.completion?.sequence ??
|
|
130
|
+
Number.MAX_SAFE_INTEGER);
|
|
131
|
+
}
|
|
132
|
+
function writeOptional(view, offset, value) {
|
|
133
|
+
view.setFloat64(offset, value ?? Number.NaN, true);
|
|
134
|
+
}
|
|
135
|
+
function optionalField(key, value) {
|
|
136
|
+
return Number.isNaN(value) ? {} : { [key]: value };
|
|
137
|
+
}
|
|
138
|
+
function requireLength(bytes, expected) {
|
|
139
|
+
if (bytes.byteLength !== expected)
|
|
140
|
+
throw new RangeError(`flight observation needs ${expected} bytes, received ${bytes.byteLength}`);
|
|
141
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type FlightDecodeLimits } from './flightCodec';
|
|
2
|
+
import type { FlightCapture, FlightCaptureHeader } from './flightTypes';
|
|
3
|
+
import { type PackOpts } from './telemetry';
|
|
4
|
+
export interface PackedFlightCapture {
|
|
5
|
+
key: string;
|
|
6
|
+
containerVersion: number;
|
|
7
|
+
gzip: true;
|
|
8
|
+
bytes: number;
|
|
9
|
+
gzBytes: number;
|
|
10
|
+
sha256: string;
|
|
11
|
+
gzB64?: string;
|
|
12
|
+
url?: string;
|
|
13
|
+
replayStatus: FlightCapture['replay']['status'];
|
|
14
|
+
truncated: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface UnpackFlightOpts extends FlightDecodeLimits {
|
|
17
|
+
maxCompressedBytes?: number;
|
|
18
|
+
maxInflatedBytes?: number;
|
|
19
|
+
fetchUrl?: (url: string) => Promise<Uint8Array>;
|
|
20
|
+
}
|
|
21
|
+
export interface PackFlightOpts extends PackOpts {
|
|
22
|
+
redactHeader?: (header: FlightCaptureHeader) => FlightCaptureHeader;
|
|
23
|
+
}
|
|
24
|
+
export declare function registerFlightCaptureProvider(key: string, recorder: {
|
|
25
|
+
capture(reason: string): Promise<FlightCapture>;
|
|
26
|
+
}, reason?: string): () => void;
|
|
27
|
+
export declare function packFlightCapture(key: string, capture: FlightCapture, opts?: PackFlightOpts): Promise<PackedFlightCapture>;
|
|
28
|
+
export declare function unpackFlightCapture(ref: PackedFlightCapture, opts?: UnpackFlightOpts): Promise<FlightCapture>;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { decodeFlightCapture, encodeFlightCapture, } from './flightCodec';
|
|
2
|
+
import { FLIGHT_CONTAINER_VERSION } from './flightTypes';
|
|
3
|
+
import { fromB64, gzipBytes, registerAsyncSnapshotProvider, toB64, } from './telemetry';
|
|
4
|
+
export function registerFlightCaptureProvider(key, recorder, reason = 'feedback') {
|
|
5
|
+
return registerAsyncSnapshotProvider(key, () => recorder.capture(reason));
|
|
6
|
+
}
|
|
7
|
+
export async function packFlightCapture(key, capture, opts = {}) {
|
|
8
|
+
const packedCapture = opts.redactHeader
|
|
9
|
+
? {
|
|
10
|
+
...capture,
|
|
11
|
+
header: opts.redactHeader({
|
|
12
|
+
...capture.header,
|
|
13
|
+
environment: { ...capture.header.environment },
|
|
14
|
+
reason: [...capture.header.reason],
|
|
15
|
+
}),
|
|
16
|
+
}
|
|
17
|
+
: capture;
|
|
18
|
+
const raw = encodeFlightCapture(packedCapture);
|
|
19
|
+
const gz = await gzipBytes(raw);
|
|
20
|
+
const sha256 = await digestHex(raw);
|
|
21
|
+
const truncated = [
|
|
22
|
+
capture.truncation.dropped,
|
|
23
|
+
capture.truncation.droppedBytes,
|
|
24
|
+
capture.truncation.rejectedOversize,
|
|
25
|
+
].some((counts) => Object.values(counts).some((count) => count > 0));
|
|
26
|
+
const ref = {
|
|
27
|
+
key,
|
|
28
|
+
containerVersion: FLIGHT_CONTAINER_VERSION,
|
|
29
|
+
gzip: true,
|
|
30
|
+
bytes: raw.byteLength,
|
|
31
|
+
gzBytes: gz.byteLength,
|
|
32
|
+
sha256,
|
|
33
|
+
replayStatus: capture.replay.status,
|
|
34
|
+
truncated,
|
|
35
|
+
};
|
|
36
|
+
const maxInline = opts.maxInlineBytes ?? 48_000;
|
|
37
|
+
if (gz.byteLength <= maxInline || !opts.upload)
|
|
38
|
+
return { ...ref, gzB64: toB64(gz) };
|
|
39
|
+
return {
|
|
40
|
+
...ref,
|
|
41
|
+
url: await opts.upload(gz, {
|
|
42
|
+
key,
|
|
43
|
+
bytes: raw.byteLength,
|
|
44
|
+
gzBytes: gz.byteLength,
|
|
45
|
+
}),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export async function unpackFlightCapture(ref, opts = {}) {
|
|
49
|
+
const gz = ref.gzB64
|
|
50
|
+
? fromB64(ref.gzB64)
|
|
51
|
+
: ref.url
|
|
52
|
+
? await (opts.fetchUrl ?? defaultFetch)(ref.url)
|
|
53
|
+
: undefined;
|
|
54
|
+
if (!gz)
|
|
55
|
+
throw new Error('packed flight capture has no payload');
|
|
56
|
+
if (ref.containerVersion !== FLIGHT_CONTAINER_VERSION)
|
|
57
|
+
throw new Error(`unsupported packed flight container version ${ref.containerVersion}`);
|
|
58
|
+
const maxCompressed = opts.maxCompressedBytes ?? 32 * 1024 * 1024;
|
|
59
|
+
if (gz.byteLength > maxCompressed)
|
|
60
|
+
throw new RangeError('compressed flight capture exceeds maximum bytes');
|
|
61
|
+
if (gz.byteLength !== ref.gzBytes)
|
|
62
|
+
throw new Error('compressed flight capture byte count mismatch');
|
|
63
|
+
const raw = await gunzipLimited(gz, opts.maxInflatedBytes ?? opts.maxBytes ?? 64 * 1024 * 1024);
|
|
64
|
+
if (raw.byteLength !== ref.bytes)
|
|
65
|
+
throw new Error('flight capture byte count mismatch');
|
|
66
|
+
if ((await digestHex(raw)) !== ref.sha256)
|
|
67
|
+
throw new Error('flight capture checksum mismatch');
|
|
68
|
+
return decodeFlightCapture(raw, opts);
|
|
69
|
+
}
|
|
70
|
+
async function defaultFetch(url) {
|
|
71
|
+
return new Uint8Array(await (await fetch(url)).arrayBuffer());
|
|
72
|
+
}
|
|
73
|
+
async function digestHex(bytes) {
|
|
74
|
+
const digest = new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', bytes.slice().buffer));
|
|
75
|
+
return [...digest]
|
|
76
|
+
.map((value) => value.toString(16).padStart(2, '0'))
|
|
77
|
+
.join('');
|
|
78
|
+
}
|
|
79
|
+
async function gunzipLimited(bytes, limit) {
|
|
80
|
+
const stream = new Blob([bytes])
|
|
81
|
+
.stream()
|
|
82
|
+
.pipeThrough(new DecompressionStream('gzip'));
|
|
83
|
+
const reader = stream.getReader();
|
|
84
|
+
const chunks = [];
|
|
85
|
+
let total = 0;
|
|
86
|
+
for (;;) {
|
|
87
|
+
const { done, value } = await reader.read();
|
|
88
|
+
if (done)
|
|
89
|
+
break;
|
|
90
|
+
total += value.byteLength;
|
|
91
|
+
if (total > limit) {
|
|
92
|
+
await reader.cancel('inflated flight capture exceeds maximum bytes');
|
|
93
|
+
throw new RangeError('inflated flight capture exceeds maximum bytes');
|
|
94
|
+
}
|
|
95
|
+
chunks.push(value);
|
|
96
|
+
}
|
|
97
|
+
const out = new Uint8Array(total);
|
|
98
|
+
let offset = 0;
|
|
99
|
+
for (const chunk of chunks) {
|
|
100
|
+
out.set(chunk, offset);
|
|
101
|
+
offset += chunk.byteLength;
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type FlightCapture, type FlightEscalationState, type FlightRecorderAdapter, type FlightRecorderConfig, type FrameSample, type LifecycleSample, type RenderSample, type WorkerSample } from './flightTypes';
|
|
2
|
+
export interface FlightRecorder<Input, AppEvent = never> {
|
|
3
|
+
readonly escalationState: FlightEscalationState;
|
|
4
|
+
consumedInput(tick: number, input: Input): void;
|
|
5
|
+
inputLoss(tick: number, count?: number): void;
|
|
6
|
+
stateHash(tick: number, hash: string): void;
|
|
7
|
+
frame(sample: FrameSample, tick?: number): void;
|
|
8
|
+
worker(sample: WorkerSample, tick?: number): void;
|
|
9
|
+
renderer(sample: RenderSample, tick?: number): void;
|
|
10
|
+
lifecycle(sample: LifecycleSample, tick?: number): void;
|
|
11
|
+
app(event: AppEvent, tick?: number, kind?: number): void;
|
|
12
|
+
escalate(trigger?: string): void;
|
|
13
|
+
capture(reason: string): Promise<FlightCapture>;
|
|
14
|
+
dispose(): void;
|
|
15
|
+
}
|
|
16
|
+
export declare function createFlightRecorder<Checkpoint, Input, ReplayState, AppEvent = never>(config: FlightRecorderConfig, adapter: FlightRecorderAdapter<Checkpoint, Input, ReplayState, AppEvent>): FlightRecorder<Input, AppEvent>;
|